fuzzer_parser_init:
   33|  1.90k|readstat_parser_t *fuzzer_parser_init(const uint8_t *Data, size_t Size) {
   34|  1.90k|    readstat_parser_t *parser = readstat_parser_init();
   35|  1.90k|    readstat_set_open_handler(parser, rt_open_handler);
   36|  1.90k|    readstat_set_close_handler(parser, rt_close_handler);
   37|  1.90k|    readstat_set_seek_handler(parser, rt_seek_handler);
   38|  1.90k|    readstat_set_read_handler(parser, rt_read_handler);
   39|  1.90k|    readstat_set_update_handler(parser, rt_update_handler);
   40|       |
   41|  1.90k|    readstat_set_metadata_handler(parser, &handle_metadata);
   42|  1.90k|    readstat_set_note_handler(parser, &handle_note);
   43|  1.90k|    readstat_set_variable_handler(parser, &handle_variable);
   44|  1.90k|    readstat_set_fweight_handler(parser, &handle_fweight);
   45|  1.90k|    readstat_set_value_handler(parser, &handle_value);
   46|  1.90k|    readstat_set_value_label_handler(parser, &handle_value_label);
   47|       |
   48|  1.90k|    return parser;
   49|  1.90k|}
fuzz_format.c:handle_metadata:
    8|  1.36k|static int handle_metadata(readstat_metadata_t *metadata, void *ctx) {
    9|  1.36k|    return READSTAT_HANDLER_OK;
  ------------------
  |  |   22|  1.36k|#define READSTAT_HANDLER_OK             0
  ------------------
   10|  1.36k|}
fuzz_format.c:handle_value_label:
   29|    768|static int handle_value_label(const char *val_labels, readstat_value_t value, const char *label, void *ctx) {
   30|    768|    return READSTAT_HANDLER_OK;
  ------------------
  |  |   22|    768|#define READSTAT_HANDLER_OK             0
  ------------------
   31|    768|}

LLVMFuzzerTestOneInput:
   10|  1.90k|int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
   11|  1.90k|    rt_buffer_t buffer = { .bytes = (char *)Data, .size = Size, .used = Size };
   12|  1.90k|    rt_buffer_ctx_t buffer_ctx = { .buffer = &buffer };
   13|       |
   14|  1.90k|    readstat_parser_t *parser = fuzzer_parser_init(Data, Size);
   15|  1.90k|    readstat_set_io_ctx(parser, &buffer_ctx);
   16|       |
   17|  1.90k|    readstat_parse_sas7bcat(parser, NULL, NULL);
   18|  1.90k|    readstat_parser_free(parser);
   19|  1.90k|    return 0;
   20|  1.90k|}

machine_is_little_endian:
   11|  3.83k|int machine_is_little_endian(void) {
   12|  3.83k|    int test_byte_order = 1;
   13|  3.83k|    return ((char *)&test_byte_order)[0];
   14|  3.83k|}
byteswap2:
   40|  14.8k|uint16_t byteswap2(uint16_t num) {
   41|  14.8k|    return ((num & 0xFF00) >> 8) | ((num & 0x00FF) << 8);
   42|  14.8k|}
byteswap4:
   44|  9.01k|uint32_t byteswap4(uint32_t num) {
   45|  9.01k|    num = ((num & 0xFFFF0000) >> 16) | ((num & 0x0000FFFF) << 16);
   46|  9.01k|    return ((num & 0xFF00FF00) >> 8) | ((num & 0x00FF00FF) << 8);
   47|  9.01k|}
byteswap8:
   49|  2.51k|uint64_t byteswap8(uint64_t num) {
   50|  2.51k|    num = ((num & 0xFFFFFFFF00000000) >> 32) | ((num & 0x00000000FFFFFFFF) << 32);
   51|  2.51k|    num = ((num & 0xFFFF0000FFFF0000) >> 16) | ((num & 0x0000FFFF0000FFFF) << 16);
   52|  2.51k|    return ((num & 0xFF00FF00FF00FF00) >> 8) | ((num & 0x00FF00FF00FF00FF) << 8);
   53|  2.51k|}
byteswap_double:
   63|  1.76k|double byteswap_double(double num) {
   64|  1.76k|    uint64_t answer = 0;
   65|  1.76k|    memcpy(&answer, &num, 8);
   66|  1.76k|    answer = byteswap8(answer);
   67|  1.76k|    memcpy(&num, &answer, 8);
   68|  1.76k|    return num;
   69|  1.76k|}

readstat_convert:
   39|  3.52k|readstat_error_t readstat_convert(char *dst, size_t dst_len, const char *src, size_t src_len, iconv_t converter) {
   40|  3.52k|    return readstat_convert_impl(dst, dst_len, src, src_len, converter, 1);
   41|  3.52k|}
readstat_convert.c:readstat_convert_impl:
    8|  3.52k|        iconv_t converter, int trim_spaces) {
    9|       |    /* strip off spaces from the input because the programs use ASCII space
   10|       |     * padding even with non-ASCII encoding. */
   11|  21.8k|    while (src_len && ((trim_spaces && src[src_len-1] == ' ') || src[src_len-1] == '\0')) {
  ------------------
  |  Branch (11:12): [True: 21.0k, False: 786]
  |  Branch (11:25): [True: 21.0k, False: 0]
  |  Branch (11:40): [True: 2.31k, False: 18.7k]
  |  Branch (11:66): [True: 16.0k, False: 2.73k]
  ------------------
   12|  18.3k|        src_len--;
   13|  18.3k|    }
   14|  3.52k|    if (dst_len == 0) {
  ------------------
  |  Branch (14:9): [True: 0, False: 3.52k]
  ------------------
   15|      0|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   16|  3.52k|    } else if (converter) {
  ------------------
  |  Branch (16:16): [True: 2.39k, False: 1.13k]
  ------------------
   17|  2.39k|        size_t dst_left = dst_len - 1;
   18|  2.39k|        char *dst_end = dst;
   19|  2.39k|        size_t status = iconv(converter, (readstat_iconv_inbuf_t)&src, &src_len, &dst_end, &dst_left);
   20|  2.39k|        if (status == (size_t)-1) {
  ------------------
  |  Branch (20:13): [True: 268, False: 2.12k]
  ------------------
   21|    268|            if (errno == E2BIG) {
  ------------------
  |  Branch (21:17): [True: 1, False: 267]
  ------------------
   22|      1|                return READSTAT_ERROR_CONVERT_LONG_STRING;
   23|    267|            } else if (errno == EILSEQ) {
  ------------------
  |  Branch (23:24): [True: 41, False: 226]
  ------------------
   24|     41|                return READSTAT_ERROR_CONVERT_BAD_STRING;
   25|    226|            } else if (errno != EINVAL) { /* EINVAL indicates improper truncation; accept it */
  ------------------
  |  Branch (25:24): [True: 0, False: 226]
  ------------------
   26|      0|                return READSTAT_ERROR_CONVERT;
   27|      0|            }
   28|    268|        }
   29|  2.35k|        dst[dst_len - dst_left - 1] = '\0';
   30|  2.35k|    } else if (src_len + 1 > dst_len) {
  ------------------
  |  Branch (30:16): [True: 0, False: 1.13k]
  ------------------
   31|      0|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   32|  1.13k|    } else {
   33|  1.13k|        memcpy(dst, src, src_len);
   34|  1.13k|        dst[src_len] = '\0';
   35|  1.13k|    }
   36|  3.48k|    return READSTAT_OK;
   37|  3.52k|}

unistd_io_init:
  121|  1.90k|readstat_error_t unistd_io_init(readstat_parser_t *parser) {
  122|  1.90k|    readstat_error_t retval = READSTAT_OK;
  123|  1.90k|    unistd_io_ctx_t *io_ctx = NULL;
  124|       |
  125|  1.90k|    if ((retval = readstat_set_open_handler(parser, unistd_open_handler)) != READSTAT_OK)
  ------------------
  |  Branch (125:9): [True: 0, False: 1.90k]
  ------------------
  126|      0|        return retval;
  127|       |
  128|  1.90k|    if ((retval = readstat_set_close_handler(parser, unistd_close_handler)) != READSTAT_OK)
  ------------------
  |  Branch (128:9): [True: 0, False: 1.90k]
  ------------------
  129|      0|        return retval;
  130|       |
  131|  1.90k|    if ((retval = readstat_set_seek_handler(parser, unistd_seek_handler)) != READSTAT_OK)
  ------------------
  |  Branch (131:9): [True: 0, False: 1.90k]
  ------------------
  132|      0|        return retval;
  133|       |
  134|  1.90k|    if ((retval = readstat_set_read_handler(parser, unistd_read_handler)) != READSTAT_OK)
  ------------------
  |  Branch (134:9): [True: 0, False: 1.90k]
  ------------------
  135|      0|        return retval;
  136|       |
  137|  1.90k|    if ((readstat_set_update_handler(parser, unistd_update_handler)) != READSTAT_OK)
  ------------------
  |  Branch (137:9): [True: 0, False: 1.90k]
  ------------------
  138|      0|        return retval;
  139|       |
  140|  1.90k|    io_ctx = calloc(1, sizeof(unistd_io_ctx_t));
  141|  1.90k|    io_ctx->fd = -1;
  142|       |
  143|  1.90k|    retval = readstat_set_io_ctx(parser, (void*) io_ctx);
  144|  1.90k|    parser->io->io_ctx_needs_free = 1;
  145|       |
  146|  1.90k|    return retval;
  147|  1.90k|}

readstat_malloc:
   10|  1.36k|void *readstat_malloc(size_t len) {
   11|  1.36k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|  2.72k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (11:9): [True: 0, False: 1.36k]
  |  Branch (11:34): [True: 0, False: 1.36k]
  ------------------
   12|      0|        return NULL;
   13|      0|    }
   14|  1.36k|    return malloc(len);
   15|  1.36k|}
readstat_calloc:
   17|    695|void *readstat_calloc(size_t count, size_t size) {
   18|    695|    if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  1.39k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  1.27k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|    584|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (18:9): [True: 111, False: 584]
  |  Branch (18:36): [True: 0, False: 584]
  |  Branch (18:62): [True: 30, False: 554]
  ------------------
   19|    141|        return NULL;
   20|    141|    }
   21|    554|    if (count == 0 || size == 0) {
  ------------------
  |  Branch (21:9): [True: 3, False: 551]
  |  Branch (21:23): [True: 0, False: 551]
  ------------------
   22|      3|        return NULL;
   23|      3|    }
   24|    551|    return calloc(count, size);
   25|    554|}
readstat_realloc:
   27|  1.10k|void *readstat_realloc(void *ptr, size_t len) {
   28|  1.10k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|  2.21k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (28:9): [True: 4, False: 1.10k]
  |  Branch (28:34): [True: 0, False: 1.10k]
  ------------------
   29|      4|        if (ptr)
  ------------------
  |  Branch (29:13): [True: 2, False: 2]
  ------------------
   30|      2|            free(ptr);
   31|      4|        return NULL;
   32|      4|    }
   33|  1.10k|    return realloc(ptr, len);
   34|  1.10k|}

readstat_parser_init:
    6|  1.90k|readstat_parser_t *readstat_parser_init(void) {
    7|  1.90k|    readstat_parser_t *parser = calloc(1, sizeof(readstat_parser_t));
    8|  1.90k|    parser->io = calloc(1, sizeof(readstat_io_t));
    9|  1.90k|    if (unistd_io_init(parser) != READSTAT_OK) {
  ------------------
  |  Branch (9:9): [True: 0, False: 1.90k]
  ------------------
   10|      0|        readstat_parser_free(parser);
   11|      0|        return NULL;
   12|      0|    }
   13|  1.90k|    parser->output_encoding = "UTF-8";
   14|  1.90k|    return parser;
   15|  1.90k|}
readstat_parser_free:
   17|  1.90k|void readstat_parser_free(readstat_parser_t *parser) {
   18|  1.90k|    if (parser) {
  ------------------
  |  Branch (18:9): [True: 1.90k, False: 0]
  ------------------
   19|  1.90k|        if (parser->io) {
  ------------------
  |  Branch (19:13): [True: 1.90k, False: 0]
  ------------------
   20|       |            readstat_set_io_ctx(parser, NULL);
   21|  1.90k|            free(parser->io);
   22|  1.90k|        }
   23|  1.90k|        free(parser);
   24|  1.90k|    }
   25|  1.90k|}
readstat_set_metadata_handler:
   27|  1.90k|readstat_error_t readstat_set_metadata_handler(readstat_parser_t *parser, readstat_metadata_handler metadata_handler) {
   28|  1.90k|    parser->handlers.metadata = metadata_handler;
   29|  1.90k|    return READSTAT_OK;
   30|  1.90k|}
readstat_set_note_handler:
   32|  1.90k|readstat_error_t readstat_set_note_handler(readstat_parser_t *parser, readstat_note_handler note_handler) {
   33|  1.90k|    parser->handlers.note = note_handler;
   34|  1.90k|    return READSTAT_OK;
   35|  1.90k|}
readstat_set_variable_handler:
   37|  1.90k|readstat_error_t readstat_set_variable_handler(readstat_parser_t *parser, readstat_variable_handler variable_handler) {
   38|  1.90k|    parser->handlers.variable = variable_handler;
   39|  1.90k|    return READSTAT_OK;
   40|  1.90k|}
readstat_set_value_handler:
   42|  1.90k|readstat_error_t readstat_set_value_handler(readstat_parser_t *parser, readstat_value_handler value_handler) {
   43|  1.90k|    parser->handlers.value = value_handler;
   44|  1.90k|    return READSTAT_OK;
   45|  1.90k|}
readstat_set_value_label_handler:
   47|  1.90k|readstat_error_t readstat_set_value_label_handler(readstat_parser_t *parser, readstat_value_label_handler label_handler) {
   48|  1.90k|    parser->handlers.value_label = label_handler;
   49|  1.90k|    return READSTAT_OK;
   50|  1.90k|}
readstat_set_fweight_handler:
   62|  1.90k|readstat_error_t readstat_set_fweight_handler(readstat_parser_t *parser, readstat_fweight_handler fweight_handler) {
   63|  1.90k|    parser->handlers.fweight = fweight_handler;
   64|  1.90k|    return READSTAT_OK;
   65|  1.90k|}
readstat_set_open_handler:
   67|  3.81k|readstat_error_t readstat_set_open_handler(readstat_parser_t *parser, readstat_open_handler open_handler) {
   68|  3.81k|    parser->io->open = open_handler;
   69|  3.81k|    return READSTAT_OK;
   70|  3.81k|}
readstat_set_close_handler:
   72|  3.81k|readstat_error_t readstat_set_close_handler(readstat_parser_t *parser, readstat_close_handler close_handler) {
   73|  3.81k|    parser->io->close = close_handler;
   74|  3.81k|    return READSTAT_OK;
   75|  3.81k|}
readstat_set_seek_handler:
   77|  3.81k|readstat_error_t readstat_set_seek_handler(readstat_parser_t *parser, readstat_seek_handler seek_handler) {
   78|  3.81k|    parser->io->seek = seek_handler;
   79|  3.81k|    return READSTAT_OK;
   80|  3.81k|}
readstat_set_read_handler:
   82|  3.81k|readstat_error_t readstat_set_read_handler(readstat_parser_t *parser, readstat_read_handler read_handler) {
   83|  3.81k|    parser->io->read = read_handler;
   84|  3.81k|    return READSTAT_OK;
   85|  3.81k|}
readstat_set_update_handler:
   87|  3.81k|readstat_error_t readstat_set_update_handler(readstat_parser_t *parser, readstat_update_handler update_handler) {
   88|  3.81k|    parser->io->update = update_handler;
   89|  3.81k|    return READSTAT_OK;
   90|  3.81k|}
readstat_set_io_ctx:
   92|  5.72k|readstat_error_t readstat_set_io_ctx(readstat_parser_t *parser, void *io_ctx) {
   93|  5.72k|    if (parser->io->io_ctx_needs_free) {
  ------------------
  |  Branch (93:9): [True: 1.90k, False: 3.81k]
  ------------------
   94|  1.90k|        free(parser->io->io_ctx);
   95|  1.90k|    }
   96|       |
   97|  5.72k|    parser->io->io_ctx = io_ctx;
   98|  5.72k|    parser->io->io_ctx_needs_free = 0;
   99|       |
  100|  5.72k|    return READSTAT_OK;
  101|  5.72k|}

sas_read8:
  147|  2.01k|uint64_t sas_read8(const char *data, int bswap) {
  148|  2.01k|    uint64_t tmp;
  149|  2.01k|    memcpy(&tmp, data, 8);
  150|  2.01k|    return bswap ? byteswap8(tmp) : tmp;
  ------------------
  |  Branch (150:12): [True: 724, False: 1.29k]
  ------------------
  151|  2.01k|}
sas_read4:
  153|   130k|uint32_t sas_read4(const char *data, int bswap) {
  154|   130k|    uint32_t tmp;
  155|   130k|    memcpy(&tmp, data, 4);
  156|   130k|    return bswap ? byteswap4(tmp) : tmp;
  ------------------
  |  Branch (156:12): [True: 7.78k, False: 122k]
  ------------------
  157|   130k|}
sas_read2:
  159|  6.81M|uint16_t sas_read2(const char *data, int bswap) {
  160|  6.81M|    uint16_t tmp;
  161|  6.81M|    memcpy(&tmp, data, 2);
  162|  6.81M|    return bswap ? byteswap2(tmp) : tmp;
  ------------------
  |  Branch (162:12): [True: 14.8k, False: 6.79M]
  ------------------
  163|  6.81M|}
sas_read_header:
  170|  1.90k|        readstat_error_handler error_handler, void *user_ctx) {
  171|  1.90k|    sas_header_start_t  header_start;
  172|  1.90k|    sas_header_end_t    header_end;
  173|  1.90k|    int retval = READSTAT_OK;
  174|  1.90k|    char error_buf[1024];
  175|  1.90k|    time_t epoch = sas_epoch();
  176|       |
  177|  1.90k|    if (io->read(&header_start, sizeof(sas_header_start_t), io->io_ctx) < sizeof(sas_header_start_t)) {
  ------------------
  |  Branch (177:9): [True: 20, False: 1.88k]
  ------------------
  178|     20|        retval = READSTAT_ERROR_READ;
  179|     20|        goto cleanup;
  180|     20|    }
  181|  1.88k|    if (memcmp(header_start.magic, sas7bdat_magic_number, sizeof(sas7bdat_magic_number)) != 0 &&
  ------------------
  |  Branch (181:9): [True: 1.18k, False: 703]
  ------------------
  182|  1.18k|            memcmp(header_start.magic, sas7bdat_magic_number_alt, sizeof(sas7bdat_magic_number_alt)) != 0 &&
  ------------------
  |  Branch (182:13): [True: 1.13k, False: 54]
  ------------------
  183|  1.13k|            memcmp(header_start.magic, sas7bcat_magic_number, sizeof(sas7bcat_magic_number)) != 0) {
  ------------------
  |  Branch (183:13): [True: 135, False: 997]
  ------------------
  184|    135|        retval = READSTAT_ERROR_PARSE;
  185|    135|        goto cleanup;
  186|    135|    }
  187|  1.75k|    if (header_start.a1 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   88|  1.75k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (187:9): [True: 10, False: 1.74k]
  ------------------
  188|     10|        hinfo->pad1 = 4;
  189|     10|    }
  190|  1.75k|    if (header_start.a2 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   88|  1.75k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (190:9): [True: 471, False: 1.28k]
  ------------------
  191|    471|        hinfo->u64 = 1;
  192|    471|    }
  193|  1.75k|    int bswap = 0;
  194|  1.75k|    if (header_start.endian == SAS_ENDIAN_BIG) {
  ------------------
  |  |   81|  1.75k|#define SAS_ENDIAN_BIG       0x00
  ------------------
  |  Branch (194:9): [True: 458, False: 1.29k]
  ------------------
  195|    458|        bswap = machine_is_little_endian();
  196|    458|        hinfo->little_endian = 0;
  197|  1.29k|    } else if (header_start.endian == SAS_ENDIAN_LITTLE) {
  ------------------
  |  |   82|  1.29k|#define SAS_ENDIAN_LITTLE    0x01
  ------------------
  |  Branch (197:16): [True: 1.28k, False: 11]
  ------------------
  198|  1.28k|        bswap = !machine_is_little_endian();
  199|  1.28k|        hinfo->little_endian = 1;
  200|  1.28k|    } else {
  201|     11|        retval = READSTAT_ERROR_PARSE;
  202|     11|        goto cleanup;
  203|     11|    }
  204|  1.74k|    int i;
  205|  59.8k|    for (i=0; i<sizeof(_charset_table)/sizeof(_charset_table[0]); i++) {
  ------------------
  |  Branch (205:15): [True: 59.2k, False: 596]
  ------------------
  206|  59.2k|        if (header_start.encoding == _charset_table[i].code) {
  ------------------
  |  Branch (206:13): [True: 1.14k, False: 58.1k]
  ------------------
  207|  1.14k|            hinfo->encoding = _charset_table[i].name;
  208|  1.14k|            break;
  209|  1.14k|        }
  210|  59.2k|    }
  211|  1.74k|    if (hinfo->encoding == NULL && header_start.encoding != 0) {
  ------------------
  |  Branch (211:9): [True: 596, False: 1.14k]
  |  Branch (211:36): [True: 12, False: 584]
  ------------------
  212|     12|        if (error_handler) {
  ------------------
  |  Branch (212:13): [True: 0, False: 12]
  ------------------
  213|      0|            snprintf(error_buf, sizeof(error_buf), "Unsupported character set code: %d", header_start.encoding);
  214|      0|            error_handler(error_buf, user_ctx);
  215|      0|        }
  216|     12|        retval = READSTAT_ERROR_UNSUPPORTED_CHARSET;
  217|     12|        goto cleanup;
  218|     12|    }
  219|  1.73k|    memcpy(hinfo->table_name, header_start.table_name, sizeof(header_start.table_name));
  220|  1.73k|    if (io->seek(hinfo->pad1, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (220:9): [True: 4, False: 1.72k]
  ------------------
  221|      4|        retval = READSTAT_ERROR_SEEK;
  222|      4|        goto cleanup;
  223|      4|    }
  224|       |
  225|  1.72k|    double creation_time, modification_time, creation_time_diff, modification_time_diff;
  226|       |
  227|  1.72k|    if (io->read(&creation_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (227:9): [True: 9, False: 1.71k]
  ------------------
  228|      9|        retval = READSTAT_ERROR_READ;
  229|      9|        goto cleanup;
  230|      9|    }
  231|  1.71k|    if (bswap)
  ------------------
  |  Branch (231:9): [True: 446, False: 1.27k]
  ------------------
  232|    446|        creation_time = byteswap_double(creation_time);
  233|       |
  234|  1.71k|    if (io->read(&modification_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (234:9): [True: 7, False: 1.71k]
  ------------------
  235|      7|        retval = READSTAT_ERROR_READ;
  236|      7|        goto cleanup;
  237|      7|    }
  238|  1.71k|    if (bswap)
  ------------------
  |  Branch (238:9): [True: 443, False: 1.26k]
  ------------------
  239|    443|        modification_time = byteswap_double(modification_time);
  240|       |
  241|  1.71k|    if (io->read(&creation_time_diff, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (241:9): [True: 6, False: 1.70k]
  ------------------
  242|      6|        retval = READSTAT_ERROR_READ;
  243|      6|        goto cleanup;
  244|      6|    }
  245|  1.70k|    if (bswap)
  ------------------
  |  Branch (245:9): [True: 441, False: 1.26k]
  ------------------
  246|    441|        creation_time_diff = byteswap_double(creation_time_diff);
  247|       |    
  248|  1.70k|    if (io->read(&modification_time_diff, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (248:9): [True: 7, False: 1.69k]
  ------------------
  249|      7|        retval = READSTAT_ERROR_READ;
  250|      7|        goto cleanup;
  251|      7|    }
  252|  1.69k|    if (bswap)
  ------------------
  |  Branch (252:9): [True: 438, False: 1.26k]
  ------------------
  253|    438|        modification_time_diff = byteswap_double(modification_time_diff);
  254|       |    
  255|  1.69k|    hinfo->creation_time = sas_convert_time(creation_time, creation_time_diff, epoch);
  256|  1.69k|    hinfo->modification_time = sas_convert_time(modification_time, modification_time_diff, epoch);
  257|       |
  258|  1.69k|    uint32_t header_size, page_size;
  259|       |
  260|  1.69k|    if (io->read(&header_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (260:9): [True: 7, False: 1.69k]
  ------------------
  261|      7|        retval = READSTAT_ERROR_READ;
  262|      7|        goto cleanup;
  263|      7|    }
  264|  1.69k|    if (io->read(&page_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (264:9): [True: 3, False: 1.68k]
  ------------------
  265|      3|        retval = READSTAT_ERROR_READ;
  266|      3|        goto cleanup;
  267|      3|    }
  268|       |
  269|  1.68k|    hinfo->header_size = bswap ? byteswap4(header_size) : header_size;
  ------------------
  |  Branch (269:26): [True: 434, False: 1.25k]
  ------------------
  270|  1.68k|    hinfo->page_size = bswap ? byteswap4(page_size) : page_size;
  ------------------
  |  Branch (270:24): [True: 434, False: 1.25k]
  ------------------
  271|       |
  272|  1.68k|    if (hinfo->header_size < 1024 || hinfo->page_size < 1024) {
  ------------------
  |  Branch (272:9): [True: 10, False: 1.67k]
  |  Branch (272:38): [True: 11, False: 1.66k]
  ------------------
  273|     21|        retval = READSTAT_ERROR_PARSE;
  274|     21|        goto cleanup;
  275|     21|    }
  276|  1.66k|    if (hinfo->header_size > (1<<24) || hinfo->page_size > (1<<24)) {
  ------------------
  |  Branch (276:9): [True: 16, False: 1.65k]
  |  Branch (276:41): [True: 30, False: 1.62k]
  ------------------
  277|     46|        retval = READSTAT_ERROR_PARSE;
  278|     46|        goto cleanup;
  279|     46|    }
  280|       |
  281|  1.62k|    if (hinfo->u64) {
  ------------------
  |  Branch (281:9): [True: 440, False: 1.18k]
  ------------------
  282|    440|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_64BIT;
  ------------------
  |  |  124|    440|#define SAS_PAGE_HEADER_SIZE_64BIT  40
  ------------------
  283|    440|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_64BIT;
  ------------------
  |  |  121|    440|#define SAS_SUBHEADER_POINTER_SIZE_64BIT    24
  ------------------
  284|  1.18k|    } else {
  285|  1.18k|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_32BIT;
  ------------------
  |  |  123|  1.18k|#define SAS_PAGE_HEADER_SIZE_32BIT  24
  ------------------
  286|  1.18k|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_32BIT;
  ------------------
  |  |  120|  1.18k|#define SAS_SUBHEADER_POINTER_SIZE_32BIT    12
  ------------------
  287|  1.18k|    }
  288|       |
  289|  1.62k|    if (hinfo->u64) {
  ------------------
  |  Branch (289:9): [True: 440, False: 1.18k]
  ------------------
  290|    440|        uint64_t page_count;
  291|    440|        if (io->read(&page_count, sizeof(uint64_t), io->io_ctx) < sizeof(uint64_t)) {
  ------------------
  |  Branch (291:13): [True: 17, False: 423]
  ------------------
  292|     17|            retval = READSTAT_ERROR_READ;
  293|     17|            goto cleanup;
  294|     17|        }
  295|    423|        hinfo->page_count = bswap ? byteswap8(page_count) : page_count;
  ------------------
  |  Branch (295:29): [True: 22, False: 401]
  ------------------
  296|  1.18k|    } else {
  297|  1.18k|        uint32_t page_count;
  298|  1.18k|        if (io->read(&page_count, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (298:13): [True: 22, False: 1.15k]
  ------------------
  299|     22|            retval = READSTAT_ERROR_READ;
  300|     22|            goto cleanup;
  301|     22|        }
  302|  1.15k|        hinfo->page_count = bswap ? byteswap4(page_count) : page_count;
  ------------------
  |  Branch (302:29): [True: 357, False: 802]
  ------------------
  303|  1.15k|    }
  304|  1.58k|    if (hinfo->page_count > (1<<24)) {
  ------------------
  |  Branch (304:9): [True: 10, False: 1.57k]
  ------------------
  305|     10|        retval = READSTAT_ERROR_PARSE;
  306|     10|        goto cleanup;
  307|     10|    }
  308|       |    
  309|  1.57k|    if (io->seek(8, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (309:9): [True: 77, False: 1.49k]
  ------------------
  310|     77|        retval = READSTAT_ERROR_SEEK;
  311|     77|        if (error_handler) {
  ------------------
  |  Branch (311:13): [True: 0, False: 77]
  ------------------
  312|      0|            snprintf(error_buf, sizeof(error_buf), "ReadStat: Failed to seek forward by %d", 8);
  313|      0|            error_handler(error_buf, user_ctx);
  314|      0|        }
  315|     77|        goto cleanup;
  316|     77|    }
  317|  1.49k|    if (io->read(&header_end, sizeof(sas_header_end_t), io->io_ctx) < sizeof(sas_header_end_t)) {
  ------------------
  |  Branch (317:9): [True: 17, False: 1.47k]
  ------------------
  318|     17|        retval = READSTAT_ERROR_READ;
  319|     17|        goto cleanup;
  320|     17|    }
  321|  1.47k|    char major, revision_tag, revision;
  322|  1.47k|    int minor;
  323|       |    /* The release field is not NUL-terminated in the file; copy it into a
  324|       |     * terminated buffer before handing it to sscanf, which calls strlen on it. */
  325|  1.47k|    char release[sizeof(header_end.release)+1];
  326|  1.47k|    memcpy(release, header_end.release, sizeof(header_end.release));
  327|  1.47k|    release[sizeof(header_end.release)] = '\0';
  328|  1.47k|    if (sscanf(release, "%c.%04d%c%c", &major, &minor, &revision_tag, &revision) != 4) {
  ------------------
  |  Branch (328:9): [True: 3, False: 1.47k]
  ------------------
  329|      3|        retval = READSTAT_ERROR_PARSE;
  330|      3|        goto cleanup;
  331|      3|    }
  332|       |
  333|  1.47k|    if (major >= '1' && major <= '9') {
  ------------------
  |  Branch (333:9): [True: 1.46k, False: 11]
  |  Branch (333:25): [True: 1.45k, False: 11]
  ------------------
  334|  1.45k|        hinfo->major_version = major - '0';
  335|  1.45k|    } else if (major == 'V') {
  ------------------
  |  Branch (335:16): [True: 4, False: 18]
  ------------------
  336|       |        // It appears that SAS Visual Forecaster reports the major version as "V"
  337|       |        // Treat it as version 9 for all intents and purposes
  338|      4|        hinfo->major_version = 9;
  339|     18|    } else {
  340|     18|        retval = READSTAT_ERROR_PARSE;
  341|     18|        goto cleanup;
  342|     18|    }
  343|       |    // revision_tag is usually M, but J has been observed in the wild (not created with SAS?)
  344|       |    // files have also been observed with TK in place of the revision details.
  345|  1.45k|    if ((major == '8' || major == '9') && revision_tag != 'M' && revision_tag != 'J' && revision_tag != 'T') {
  ------------------
  |  Branch (345:10): [True: 122, False: 1.33k]
  |  Branch (345:26): [True: 361, False: 974]
  |  Branch (345:43): [True: 34, False: 449]
  |  Branch (345:66): [True: 28, False: 6]
  |  Branch (345:89): [True: 15, False: 13]
  ------------------
  346|     15|        retval = READSTAT_ERROR_PARSE;
  347|     15|        goto cleanup;
  348|     15|    }
  349|  1.44k|    hinfo->minor_version = minor;
  350|  1.44k|    if (revision >= '0' && revision <= '9') {
  ------------------
  |  Branch (350:9): [True: 1.43k, False: 10]
  |  Branch (350:28): [True: 1.41k, False: 17]
  ------------------
  351|  1.41k|        hinfo->revision = revision - '0';
  352|  1.41k|    } else if (revision_tag == 'T' && revision == 'K') {
  ------------------
  |  Branch (352:16): [True: 16, False: 11]
  |  Branch (352:39): [True: 4, False: 12]
  ------------------
  353|       |        // files have been observed with TK in place of the revision details.
  354|      4|        hinfo->revision = 0;
  355|     23|    } else {
  356|     23|        retval = READSTAT_ERROR_PARSE;
  357|     23|        goto cleanup;
  358|     23|    }
  359|       |
  360|  1.41k|    if ((major == '8' || major == '9') && minor == 0 && hinfo->revision == 0) {
  ------------------
  |  Branch (360:10): [True: 107, False: 1.31k]
  |  Branch (360:26): [True: 352, False: 960]
  |  Branch (360:43): [True: 36, False: 423]
  |  Branch (360:57): [True: 3, False: 33]
  ------------------
  361|       |        /* A bit of a hack, but most SAS installations are running a minor update */
  362|      3|        hinfo->vendor = READSTAT_VENDOR_STAT_TRANSFER;
  363|  1.41k|    } else {
  364|  1.41k|        hinfo->vendor = READSTAT_VENDOR_SAS;
  365|  1.41k|    }
  366|  1.41k|    if (io->seek(hinfo->header_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (366:9): [True: 53, False: 1.36k]
  ------------------
  367|     53|        retval = READSTAT_ERROR_SEEK;
  368|     53|        if (error_handler) {
  ------------------
  |  Branch (368:13): [True: 0, False: 53]
  ------------------
  369|      0|            snprintf(error_buf, sizeof(error_buf), 
  370|      0|                    "ReadStat: Failed to seek to position %" PRId64, hinfo->header_size);
  371|      0|            error_handler(error_buf, user_ctx);
  372|      0|        }
  373|     53|        goto cleanup;
  374|     53|    }
  375|       |
  376|  1.90k|cleanup:
  377|  1.90k|    return retval;
  378|  1.41k|}
sas_validate_tag:
  599|    257|readstat_error_t sas_validate_tag(char tag) {
  600|    257|    if (tag == '_' || (tag >= 'A' && tag <= 'Z'))
  ------------------
  |  Branch (600:9): [True: 45, False: 212]
  |  Branch (600:24): [True: 95, False: 117]
  |  Branch (600:38): [True: 55, False: 40]
  ------------------
  601|    100|        return READSTAT_OK;
  602|       |
  603|    157|    return READSTAT_ERROR_TAGGED_VALUE_IS_OUT_OF_RANGE;
  604|    257|}
sas_assign_tag:
  639|    257|void sas_assign_tag(readstat_value_t *value, uint8_t tag) {
  640|       |    /* We accommodate two tag schemes. In the first, the tag is an ASCII code
  641|       |     * given by uint8_t tag above. System missing is represented by an ASCII
  642|       |     * period. In the second scheme, (tag-2) is an offset from 'A', except when
  643|       |     * tag == 0, in which case it represents an underscore, or tag == 1, in
  644|       |     * which case it represents system-missing.
  645|       |     */
  646|    257|    if (tag == 0) {
  ------------------
  |  Branch (646:9): [True: 44, False: 213]
  ------------------
  647|     44|        tag = '_';
  648|    213|    } else if (tag >= 2 && tag < 28) {
  ------------------
  |  Branch (648:16): [True: 179, False: 34]
  |  Branch (648:28): [True: 51, False: 128]
  ------------------
  649|     51|        tag = 'A' + (tag - 2);
  650|     51|    }
  651|    257|    if (sas_validate_tag(tag) == READSTAT_OK) {
  ------------------
  |  Branch (651:9): [True: 100, False: 157]
  ------------------
  652|    100|        value->tag = tag;
  653|    100|        value->is_tagged_missing = 1;
  654|    157|    } else {
  655|    157|        value->tag = 0;
  656|    157|        value->is_system_missing = 1;
  657|    157|    }
  658|    257|}
readstat_sas.c:sas_epoch:
  131|  1.90k|static time_t sas_epoch(void) {
  132|  1.90k|    return - 3653 * 86400; // seconds between 01-01-1960 and 01-01-1970
  133|  1.90k|}
readstat_sas.c:sas_convert_time:
  135|  3.39k|static time_t sas_convert_time(double time, double time_diff, time_t epoch) {
  136|  3.39k|    time -= time_diff;
  137|  3.39k|    time += epoch;
  138|  3.39k|    if (isnan(time))
  ------------------
  |  Branch (138:9): [True: 113, False: 3.28k]
  ------------------
  139|    113|        return 0;
  140|  3.28k|    if (time > (double)LONG_MAX)
  ------------------
  |  Branch (140:9): [True: 516, False: 2.76k]
  ------------------
  141|    516|        return LONG_MAX;
  142|  2.76k|    if (time < (double)LONG_MIN)
  ------------------
  |  Branch (142:9): [True: 291, False: 2.47k]
  ------------------
  143|    291|        return LONG_MIN;
  144|  2.47k|    return time;
  145|  2.76k|}

readstat_parse_sas7bcat:
  435|  1.90k|readstat_error_t readstat_parse_sas7bcat(readstat_parser_t *parser, const char *path, void *user_ctx) {
  436|  1.90k|    readstat_error_t retval = READSTAT_OK;
  437|  1.90k|    readstat_io_t *io = parser->io;
  438|  1.90k|    int64_t i;
  439|  1.90k|    char *page = NULL;
  440|  1.90k|    char *buffer = NULL;
  441|       |
  442|  1.90k|    sas7bcat_ctx_t *ctx = calloc(1, sizeof(sas7bcat_ctx_t));
  443|  1.90k|    sas_header_info_t *hinfo = calloc(1, sizeof(sas_header_info_t));
  444|       |
  445|  1.90k|    ctx->block_pointers = malloc((ctx->block_pointers_capacity = 200) * sizeof(uint64_t));
  446|       |
  447|  1.90k|    ctx->value_label_handler = parser->handlers.value_label;
  448|  1.90k|    ctx->metadata_handler = parser->handlers.metadata;
  449|  1.90k|    ctx->input_encoding = parser->input_encoding;
  450|  1.90k|    ctx->output_encoding = parser->output_encoding;
  451|  1.90k|    ctx->user_ctx = user_ctx;
  452|  1.90k|    ctx->io = io;
  453|       |
  454|  1.90k|    if (io->open(path, io->io_ctx) == -1) {
  ------------------
  |  Branch (454:9): [True: 0, False: 1.90k]
  ------------------
  455|      0|        retval = READSTAT_ERROR_OPEN;
  456|      0|        goto cleanup;
  457|      0|    }
  458|       |
  459|  1.90k|    if ((retval = sas_read_header(io, hinfo, parser->handlers.error, user_ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (459:9): [True: 543, False: 1.36k]
  ------------------
  460|    543|        goto cleanup;
  461|    543|    }
  462|       |
  463|  1.36k|    ctx->u64 = hinfo->u64;
  464|  1.36k|    ctx->pad1 = hinfo->pad1;
  465|  1.36k|    ctx->bswap = machine_is_little_endian() ^ hinfo->little_endian;
  466|  1.36k|    ctx->header_size = hinfo->header_size;
  467|  1.36k|    ctx->page_count = hinfo->page_count;
  468|  1.36k|    ctx->page_size = hinfo->page_size;
  469|  1.36k|    if (ctx->input_encoding == NULL) {
  ------------------
  |  Branch (469:9): [True: 1.36k, False: 0]
  ------------------
  470|  1.36k|        ctx->input_encoding = hinfo->encoding;
  471|  1.36k|    }
  472|       |
  473|  1.36k|    ctx->xlsr_size = 212 + ctx->pad1;
  474|  1.36k|    ctx->xlsr_offset = 856 + 2 * ctx->pad1;
  475|  1.36k|    ctx->xlsr_O_offset = 50 + ctx->pad1;
  476|  1.36k|    if (ctx->u64) {
  ------------------
  |  Branch (476:9): [True: 367, False: 999]
  ------------------
  477|    367|        ctx->xlsr_offset += 144;
  478|    367|        ctx->xlsr_size += 72;
  479|    367|        ctx->xlsr_O_offset += 24;
  480|    367|    }
  481|       |
  482|  1.36k|    const char *input_encoding = ctx->input_encoding;
  483|  1.36k|    if (input_encoding == NULL) {
  ------------------
  |  Branch (483:9): [True: 388, False: 978]
  ------------------
  484|    388|        input_encoding = SAS_DEFAULT_STRING_ENCODING;
  ------------------
  |  |  142|    388|#define SAS_DEFAULT_STRING_ENCODING "WINDOWS-1252"
  ------------------
  485|    388|    }
  486|  1.36k|    if (ctx->output_encoding && strcmp(input_encoding, ctx->output_encoding) != 0) {
  ------------------
  |  Branch (486:9): [True: 1.36k, False: 0]
  |  Branch (486:33): [True: 951, False: 415]
  ------------------
  487|    951|        iconv_t converter = iconv_open(ctx->output_encoding, input_encoding);
  488|    951|        if (converter == (iconv_t)-1) {
  ------------------
  |  Branch (488:13): [True: 2, False: 949]
  ------------------
  489|      2|            retval = READSTAT_ERROR_UNSUPPORTED_CHARSET;
  490|      2|            goto cleanup;
  491|      2|        }
  492|    949|        ctx->converter = converter;
  493|    949|    }
  494|       |
  495|  1.36k|    if (ctx->metadata_handler) {
  ------------------
  |  Branch (495:9): [True: 1.36k, False: 0]
  ------------------
  496|  1.36k|        char table_name[4*32+1];
  497|  1.36k|        readstat_metadata_t metadata = { 
  498|  1.36k|            .file_encoding = ctx->input_encoding, /* orig encoding? */
  499|  1.36k|            .modified_time = hinfo->modification_time,
  500|  1.36k|            .creation_time = hinfo->creation_time,
  501|  1.36k|            .file_format_version = hinfo->major_version,
  502|  1.36k|            .endianness = hinfo->little_endian ? READSTAT_ENDIAN_LITTLE : READSTAT_ENDIAN_BIG,
  ------------------
  |  Branch (502:27): [True: 1.05k, False: 305]
  ------------------
  503|  1.36k|            .is64bit = ctx->u64
  504|  1.36k|        };
  505|  1.36k|        retval = readstat_convert(table_name, sizeof(table_name),
  506|  1.36k|                hinfo->table_name, sizeof(hinfo->table_name), ctx->converter);
  507|  1.36k|        if (retval != READSTAT_OK)
  ------------------
  |  Branch (507:13): [True: 4, False: 1.36k]
  ------------------
  508|      4|            goto cleanup;
  509|       |
  510|  1.36k|        metadata.table_name = table_name;
  511|       |
  512|  1.36k|        if (ctx->metadata_handler(&metadata, ctx->user_ctx) != READSTAT_HANDLER_OK) {
  ------------------
  |  |   22|  1.36k|#define READSTAT_HANDLER_OK             0
  ------------------
  |  Branch (512:13): [True: 0, False: 1.36k]
  ------------------
  513|      0|            retval = READSTAT_ERROR_USER_ABORT;
  514|      0|            goto cleanup;
  515|      0|        }
  516|  1.36k|    }
  517|       |
  518|  1.36k|    if ((page = readstat_malloc(ctx->page_size)) == NULL) {
  ------------------
  |  Branch (518:9): [True: 0, False: 1.36k]
  ------------------
  519|      0|        retval = READSTAT_ERROR_MALLOC;
  520|      0|        goto cleanup;
  521|      0|    }
  522|  1.36k|    if (io->seek(ctx->header_size+SAS_CATALOG_FIRST_INDEX_PAGE*ctx->page_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  |   11|  1.36k|#define SAS_CATALOG_FIRST_INDEX_PAGE 1
  ------------------
  |  Branch (522:9): [True: 60, False: 1.30k]
  ------------------
  523|     60|        retval = READSTAT_ERROR_SEEK;
  524|     60|        goto cleanup;
  525|     60|    }
  526|  1.30k|    if (io->read(page, ctx->page_size, io->io_ctx) < ctx->page_size) {
  ------------------
  |  Branch (526:9): [True: 41, False: 1.25k]
  ------------------
  527|     41|        retval = READSTAT_ERROR_READ;
  528|     41|        goto cleanup;
  529|     41|    }
  530|       |
  531|  1.25k|    retval = sas7bcat_augment_index(&page[ctx->xlsr_offset], ctx->page_size - ctx->xlsr_offset, ctx);
  532|  1.25k|    if (retval != READSTAT_OK)
  ------------------
  |  Branch (532:9): [True: 0, False: 1.25k]
  ------------------
  533|      0|        goto cleanup;
  534|       |
  535|       |    // Pass 1 -- find the XLSR entries
  536|  22.1k|    for (i=SAS_CATALOG_USELESS_PAGES; i<ctx->page_count; i++) {
  ------------------
  |  |   12|  1.25k|#define SAS_CATALOG_USELESS_PAGES    3
  ------------------
  |  Branch (536:39): [True: 20.9k, False: 1.13k]
  ------------------
  537|  20.9k|        if (io->seek(ctx->header_size+i*ctx->page_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (537:13): [True: 32, False: 20.9k]
  ------------------
  538|     32|            retval = READSTAT_ERROR_SEEK;
  539|     32|            goto cleanup;
  540|     32|        }
  541|  20.9k|        if (io->read(page, ctx->page_size, io->io_ctx) < ctx->page_size) {
  ------------------
  |  Branch (541:13): [True: 89, False: 20.8k]
  ------------------
  542|     89|            retval = READSTAT_ERROR_READ;
  543|     89|            goto cleanup;
  544|     89|        }
  545|  20.8k|        int offset = ctx->u64 ? 32 : 16;
  ------------------
  |  Branch (545:22): [True: 871, False: 19.9k]
  ------------------
  546|  20.8k|        if (memcmp(&page[offset], "XLSR", sizeof("XLSR")-1) == 0) {
  ------------------
  |  Branch (546:13): [True: 5.68k, False: 15.1k]
  ------------------
  547|  5.68k|            retval = sas7bcat_augment_index(&page[offset], ctx->page_size - offset, ctx);
  548|  5.68k|            if (retval != READSTAT_OK)
  ------------------
  |  Branch (548:17): [True: 0, False: 5.68k]
  ------------------
  549|      0|                goto cleanup;
  550|  5.68k|        }
  551|  20.8k|    }
  552|       |
  553|  1.13k|    sas7bcat_sort_index(ctx);
  554|  1.13k|    sas7bcat_uniq_index(ctx);
  555|       |
  556|       |    // Pass 2 -- look up the individual block pointers
  557|  2.58k|    for (i=0; i<ctx->block_pointers_used; i++) {
  ------------------
  |  Branch (557:15): [True: 2.19k, False: 394]
  ------------------
  558|  2.19k|        int start_page = ctx->block_pointers[i] >> 32;
  559|  2.19k|        int start_page_pos = (ctx->block_pointers[i]) & 0xFFFF;
  560|       |
  561|  2.19k|        int buffer_len = sas7bcat_block_size(start_page, start_page_pos, ctx, &retval);
  562|  2.19k|        if (buffer_len == -1) {
  ------------------
  |  Branch (562:13): [True: 23, False: 2.17k]
  ------------------
  563|     23|            goto cleanup;
  564|  2.17k|        } else if (buffer_len == 0) {
  ------------------
  |  Branch (564:20): [True: 1.10k, False: 1.06k]
  ------------------
  565|  1.10k|            continue;
  566|  1.10k|        }
  567|  1.06k|        if ((buffer = readstat_realloc(buffer, buffer_len)) == NULL) {
  ------------------
  |  Branch (567:13): [True: 4, False: 1.06k]
  ------------------
  568|      4|            retval = READSTAT_ERROR_MALLOC;
  569|      4|            goto cleanup;
  570|      4|        }
  571|  1.06k|        if ((retval = sas7bcat_read_block(buffer, buffer_len, start_page, start_page_pos, ctx)) != READSTAT_OK)
  ------------------
  |  Branch (571:13): [True: 42, False: 1.01k]
  ------------------
  572|     42|            goto cleanup;
  573|  1.01k|        if ((retval = sas7bcat_parse_block(buffer, buffer_len, ctx)) != READSTAT_OK)
  ------------------
  |  Branch (573:13): [True: 675, False: 343]
  ------------------
  574|    675|            goto cleanup;
  575|  1.01k|    }
  576|       |
  577|  1.90k|cleanup:
  578|  1.90k|    io->close(io->io_ctx);
  579|  1.90k|    if (page)
  ------------------
  |  Branch (579:9): [True: 1.36k, False: 549]
  ------------------
  580|  1.36k|        free(page);
  581|  1.90k|    if (buffer)
  ------------------
  |  Branch (581:9): [True: 848, False: 1.06k]
  ------------------
  582|    848|        free(buffer);
  583|  1.90k|    if (ctx)
  ------------------
  |  Branch (583:9): [True: 1.90k, False: 0]
  ------------------
  584|  1.90k|        sas7bcat_ctx_free(ctx);
  585|  1.90k|    if (hinfo)
  ------------------
  |  Branch (585:9): [True: 1.90k, False: 0]
  ------------------
  586|  1.90k|        free(hinfo);
  587|       |
  588|  1.90k|    return retval;
  589|  1.13k|}
readstat_sas7bcat_read.c:sas7bcat_augment_index:
  264|  6.94k|static readstat_error_t sas7bcat_augment_index(const char *index, size_t len, sas7bcat_ctx_t *ctx) {
  265|  6.94k|    const char *xlsr = index;
  266|  6.94k|    readstat_error_t retval = READSTAT_OK;
  267|  27.8k|    while (xlsr + ctx->xlsr_size <= index + len) {
  ------------------
  |  Branch (267:12): [True: 22.1k, False: 5.64k]
  ------------------
  268|  22.1k|        if (memcmp(xlsr, "XLSR", 4) != 0) // some block pointers seem to have 8 bytes of extra padding
  ------------------
  |  Branch (268:13): [True: 10.8k, False: 11.3k]
  ------------------
  269|  10.8k|            xlsr += 8;
  270|  22.1k|        if (memcmp(xlsr, "XLSR", 4) != 0)
  ------------------
  |  Branch (270:13): [True: 1.30k, False: 20.8k]
  ------------------
  271|  1.30k|            break;
  272|       |
  273|  20.8k|        if (xlsr[ctx->xlsr_O_offset] == 'O') {
  ------------------
  |  Branch (273:13): [True: 19.5k, False: 1.35k]
  ------------------
  274|  19.5k|            uint64_t page = 0, pos = 0;
  275|  19.5k|            if (ctx->u64) {
  ------------------
  |  Branch (275:17): [True: 615, False: 18.8k]
  ------------------
  276|    615|                page = sas_read8(&xlsr[8], ctx->bswap);
  277|    615|                pos = sas_read2(&xlsr[16], ctx->bswap);
  278|  18.8k|            } else {
  279|  18.8k|                page = sas_read4(&xlsr[4], ctx->bswap);
  280|  18.8k|                pos = sas_read2(&xlsr[8], ctx->bswap);
  281|  18.8k|            }
  282|  19.5k|            ctx->block_pointers[ctx->block_pointers_used++] = (page << 32) + pos;
  283|  19.5k|        }
  284|       |
  285|  20.8k|        if (ctx->block_pointers_used == ctx->block_pointers_capacity) {
  ------------------
  |  Branch (285:13): [True: 45, False: 20.8k]
  ------------------
  286|     45|            ctx->block_pointers = readstat_realloc(ctx->block_pointers, (ctx->block_pointers_capacity *= 2) * sizeof(uint64_t));
  287|     45|            if (ctx->block_pointers == NULL) {
  ------------------
  |  Branch (287:17): [True: 0, False: 45]
  ------------------
  288|      0|                retval = READSTAT_ERROR_MALLOC;
  289|      0|                goto cleanup;
  290|      0|            }
  291|     45|        }
  292|       |
  293|  20.8k|        xlsr += ctx->xlsr_size;
  294|  20.8k|    }
  295|  6.94k|cleanup:
  296|  6.94k|    return retval;
  297|  6.94k|}
readstat_sas7bcat_read.c:sas7bcat_sort_index:
  308|  1.13k|static void sas7bcat_sort_index(sas7bcat_ctx_t *ctx) {
  309|  1.13k|    if (ctx->block_pointers_used == 0)
  ------------------
  |  Branch (309:9): [True: 121, False: 1.01k]
  ------------------
  310|    121|        return;
  311|       |
  312|  1.01k|    int i;
  313|  1.44k|    for (i=1; i<ctx->block_pointers_used; i++) {
  ------------------
  |  Branch (313:15): [True: 636, False: 810]
  ------------------
  314|    636|        if (ctx->block_pointers[i] < ctx->block_pointers[i-1]) {
  ------------------
  |  Branch (314:13): [True: 207, False: 429]
  ------------------
  315|    207|            qsort(ctx->block_pointers, ctx->block_pointers_used, sizeof(uint64_t), &compare_block_pointers);
  316|    207|            break;
  317|    207|        }
  318|    636|    }
  319|  1.01k|}
readstat_sas7bcat_read.c:compare_block_pointers:
  299|  67.1k|static int compare_block_pointers(const void *elem1, const void *elem2) {
  300|  67.1k|    uint64_t v1 = *(const uint64_t *)elem1;
  301|  67.1k|    uint64_t v2 = *(const uint64_t *)elem2;
  302|       |    /* The values pack a page number into the high 32 bits, so a plain
  303|       |     * subtraction truncated to int loses that half and misorders (or reports
  304|       |     * as equal) pointers on different pages. Compare three-way instead. */
  305|  67.1k|    return (v1 > v2) - (v1 < v2);
  306|  67.1k|}
readstat_sas7bcat_read.c:sas7bcat_uniq_index:
  321|  1.13k|static void sas7bcat_uniq_index(sas7bcat_ctx_t *ctx) {
  322|  1.13k|    if (ctx->block_pointers_used == 0)
  ------------------
  |  Branch (322:9): [True: 121, False: 1.01k]
  ------------------
  323|    121|        return;
  324|       |
  325|  1.01k|    int i;
  326|  1.01k|    int out_i = 1;
  327|  10.9k|    for (i=1; i<ctx->block_pointers_used; i++) {
  ------------------
  |  Branch (327:15): [True: 9.90k, False: 1.01k]
  ------------------
  328|  9.90k|        if (ctx->block_pointers[i] != ctx->block_pointers[i-1]) {
  ------------------
  |  Branch (328:13): [True: 1.55k, False: 8.34k]
  ------------------
  329|  1.55k|            if (out_i != i) {
  ------------------
  |  Branch (329:17): [True: 1.06k, False: 495]
  ------------------
  330|  1.06k|                ctx->block_pointers[out_i] = ctx->block_pointers[i];
  331|  1.06k|            }
  332|  1.55k|            out_i++;
  333|  1.55k|        }
  334|  9.90k|    }
  335|  1.01k|    ctx->block_pointers_used = out_i;
  336|  1.01k|}
readstat_sas7bcat_read.c:sas7bcat_block_size:
  338|  2.19k|static int sas7bcat_block_size(int start_page, int start_page_pos, sas7bcat_ctx_t *ctx, readstat_error_t *outError) {
  339|  2.19k|    readstat_error_t retval = READSTAT_OK;
  340|  2.19k|    readstat_io_t *io = ctx->io;
  341|  2.19k|    int next_page = start_page;
  342|  2.19k|    int next_page_pos = start_page_pos;
  343|  2.19k|    int link_count = 0;
  344|       |
  345|  2.19k|    int buffer_len = 0;
  346|  2.19k|    int chain_link_len = 0;
  347|       |
  348|  2.19k|    char chain_link[32];
  349|  2.19k|    int chain_link_header_len = 16;
  350|  2.19k|    if (ctx->u64) {
  ------------------
  |  Branch (350:9): [True: 330, False: 1.86k]
  ------------------
  351|    330|        chain_link_header_len = 32;
  352|    330|    }
  353|       |
  354|       |    // calculate buffer size needed
  355|  15.0k|    while (next_page > 0 && next_page_pos > 0 && next_page <= ctx->page_count && link_count++ < ctx->page_count) {
  ------------------
  |  Branch (355:12): [True: 14.2k, False: 784]
  |  Branch (355:29): [True: 13.9k, False: 235]
  |  Branch (355:50): [True: 13.0k, False: 909]
  |  Branch (355:82): [True: 12.8k, False: 242]
  ------------------
  356|  12.8k|        if (io->seek(ctx->header_size+(next_page-1)*ctx->page_size+next_page_pos, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (356:13): [True: 10, False: 12.8k]
  ------------------
  357|     10|            retval = READSTAT_ERROR_SEEK;
  358|     10|            goto cleanup;
  359|     10|        }
  360|  12.8k|        if (io->read(chain_link, chain_link_header_len, io->io_ctx) < chain_link_header_len) {
  ------------------
  |  Branch (360:13): [True: 13, False: 12.8k]
  ------------------
  361|     13|            retval = READSTAT_ERROR_READ;
  362|     13|            goto cleanup;
  363|     13|        }
  364|       |
  365|  12.8k|        if (ctx->u64) {
  ------------------
  |  Branch (365:13): [True: 880, False: 11.9k]
  ------------------
  366|    880|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  367|    880|            next_page_pos = sas_read2(&chain_link[8], ctx->bswap);
  368|    880|            chain_link_len = sas_read2(&chain_link[10], ctx->bswap);
  369|  11.9k|        } else {
  370|  11.9k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  371|  11.9k|            next_page_pos = sas_read2(&chain_link[4], ctx->bswap);
  372|  11.9k|            chain_link_len = sas_read2(&chain_link[6], ctx->bswap);
  373|  11.9k|        }
  374|       |
  375|  12.8k|        buffer_len += chain_link_len;
  376|  12.8k|    }
  377|       |
  378|  2.19k|cleanup:
  379|  2.19k|    if (outError)
  ------------------
  |  Branch (379:9): [True: 2.19k, False: 0]
  ------------------
  380|  2.19k|        *outError = retval;
  381|       |
  382|  2.19k|    return retval == READSTAT_OK ? buffer_len : -1;
  ------------------
  |  Branch (382:12): [True: 2.17k, False: 23]
  ------------------
  383|  2.19k|}
readstat_sas7bcat_read.c:sas7bcat_read_block:
  386|  1.06k|        int start_page, int start_page_pos, sas7bcat_ctx_t *ctx) {
  387|  1.06k|    readstat_error_t retval = READSTAT_OK;
  388|  1.06k|    readstat_io_t *io = ctx->io;
  389|  1.06k|    int next_page = start_page;
  390|  1.06k|    int next_page_pos = start_page_pos;
  391|  1.06k|    int link_count = 0;
  392|       |
  393|  1.06k|    int chain_link_len = 0;
  394|  1.06k|    int buffer_offset = 0;
  395|       |
  396|  1.06k|    char chain_link[32];
  397|  1.06k|    int chain_link_header_len = 16;
  398|  1.06k|    if (ctx->u64) {
  ------------------
  |  Branch (398:9): [True: 255, False: 805]
  ------------------
  399|    255|        chain_link_header_len = 32;
  400|    255|    }
  401|       |
  402|  11.1k|    while (next_page > 0 && next_page_pos > 0 && next_page <= ctx->page_count && link_count++ < ctx->page_count) {
  ------------------
  |  Branch (402:12): [True: 10.8k, False: 358]
  |  Branch (402:29): [True: 10.6k, False: 135]
  |  Branch (402:50): [True: 10.3k, False: 306]
  |  Branch (402:82): [True: 10.1k, False: 219]
  ------------------
  403|  10.1k|        if (io->seek(ctx->header_size+(next_page-1)*ctx->page_size+next_page_pos, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (403:13): [True: 0, False: 10.1k]
  ------------------
  404|      0|            retval = READSTAT_ERROR_SEEK;
  405|      0|            goto cleanup;
  406|      0|        }
  407|  10.1k|        if (io->read(chain_link, chain_link_header_len, io->io_ctx) < chain_link_header_len) {
  ------------------
  |  Branch (407:13): [True: 0, False: 10.1k]
  ------------------
  408|      0|            retval = READSTAT_ERROR_READ;
  409|      0|            goto cleanup;
  410|      0|        }
  411|  10.1k|        if (ctx->u64) {
  ------------------
  |  Branch (411:13): [True: 861, False: 9.30k]
  ------------------
  412|    861|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  413|    861|            next_page_pos = sas_read2(&chain_link[8], ctx->bswap);
  414|    861|            chain_link_len = sas_read2(&chain_link[10], ctx->bswap);
  415|  9.30k|        } else {
  416|  9.30k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  417|  9.30k|            next_page_pos = sas_read2(&chain_link[4], ctx->bswap);
  418|  9.30k|            chain_link_len = sas_read2(&chain_link[6], ctx->bswap);
  419|  9.30k|        }
  420|  10.1k|        if (buffer_offset + chain_link_len > buffer_len) {
  ------------------
  |  Branch (420:13): [True: 0, False: 10.1k]
  ------------------
  421|      0|            retval = READSTAT_ERROR_PARSE;
  422|      0|            goto cleanup;
  423|      0|        }
  424|  10.1k|        if (io->read(buffer + buffer_offset, chain_link_len, io->io_ctx) < chain_link_len) {
  ------------------
  |  Branch (424:13): [True: 42, False: 10.1k]
  ------------------
  425|     42|            retval = READSTAT_ERROR_READ;
  426|     42|            goto cleanup;
  427|     42|        }
  428|  10.1k|        buffer_offset += chain_link_len;
  429|  10.1k|    }
  430|  1.06k|cleanup:
  431|       |
  432|  1.06k|    return retval;
  433|  1.06k|}
readstat_sas7bcat_read.c:sas7bcat_parse_block:
  188|  1.01k|static readstat_error_t sas7bcat_parse_block(const char *data, size_t data_size, sas7bcat_ctx_t *ctx) {
  189|  1.01k|    readstat_error_t retval = READSTAT_OK;
  190|       |
  191|  1.01k|    size_t pad = 0;
  192|  1.01k|    uint64_t label_count_capacity = 0;
  193|  1.01k|    uint64_t label_count_used = 0;
  194|  1.01k|    uint64_t default_label_pos = 0;
  195|  1.01k|    int string_offset = 0;
  196|  1.01k|    int payload_offset = 106;
  197|  1.01k|    uint16_t flags = 0;
  198|  1.01k|    char name[4*32+2]; /* room for the trailing dot */
  199|       |
  200|  1.01k|    if (data_size < payload_offset)
  ------------------
  |  Branch (200:9): [True: 52, False: 966]
  ------------------
  201|     52|        goto cleanup;
  202|       |
  203|    966|    flags = sas_read2(&data[2], ctx->bswap);
  204|    966|    pad = (flags & 0x08) ? 4 : 0; // might be 0x10, not sure
  ------------------
  |  Branch (204:11): [True: 408, False: 558]
  ------------------
  205|    966|    if (ctx->u64) {
  ------------------
  |  Branch (205:9): [True: 239, False: 727]
  ------------------
  206|    239|        if (data_size < 128 + pad)
  ------------------
  |  Branch (206:13): [True: 14, False: 225]
  ------------------
  207|     14|            goto cleanup;
  208|    225|        label_count_capacity = sas_read8(&data[42+pad], ctx->bswap);
  209|    225|        label_count_used = sas_read8(&data[50+pad], ctx->bswap);
  210|    225|        default_label_pos = sas_read8(&data[74+pad], ctx->bswap);
  211|       |        /* Nonzero when the format's values are strings longer than 16 bytes,
  212|       |         * which shifts each value one byte to the right and NUL-pads it */
  213|    225|        string_offset = sas_read4(&data[124+pad], ctx->bswap);
  214|       |
  215|    225|        payload_offset += 32;
  216|    727|    } else {
  217|    727|        if (data_size < 106 + pad)
  ------------------
  |  Branch (217:13): [True: 18, False: 709]
  ------------------
  218|     18|            goto cleanup;
  219|    709|        label_count_capacity = sas_read4(&data[38+pad], ctx->bswap);
  220|    709|        label_count_used = sas_read4(&data[42+pad], ctx->bswap);
  221|    709|        default_label_pos = sas_read4(&data[66+pad], ctx->bswap);
  222|    709|        string_offset = sas_read2(&data[104+pad], ctx->bswap);
  223|    709|    }
  224|       |
  225|    934|    if (string_offset < 0 || default_label_pos > label_count_used) {
  ------------------
  |  Branch (225:9): [True: 30, False: 904]
  |  Branch (225:30): [True: 56, False: 848]
  ------------------
  226|     86|        retval = READSTAT_ERROR_PARSE;
  227|     86|        goto cleanup;
  228|     86|    }
  229|       |
  230|    848|    if ((retval = readstat_convert(name, sizeof(name) - 1, &data[8], 8, ctx->converter)) != READSTAT_OK)
  ------------------
  |  Branch (230:9): [True: 4, False: 844]
  ------------------
  231|      4|        goto cleanup;
  232|       |
  233|    844|    if (pad) {
  ------------------
  |  Branch (233:9): [True: 343, False: 501]
  ------------------
  234|    343|        pad += 16;
  235|    343|    }
  236|       |
  237|    844|    if (((flags & 0x80) && !ctx->u64) || ((flags & 0x20) && ctx->u64)) { // has long name
  ------------------
  |  Branch (237:10): [True: 402, False: 442]
  |  Branch (237:28): [True: 346, False: 56]
  |  Branch (237:43): [True: 160, False: 338]
  |  Branch (237:61): [True: 79, False: 81]
  ------------------
  238|    425|        if (data_size < payload_offset + pad + 32)
  ------------------
  |  Branch (238:13): [True: 16, False: 409]
  ------------------
  239|     16|            goto cleanup;
  240|       |
  241|    409|        retval = readstat_convert(name, sizeof(name) - 1, &data[payload_offset+pad], 32, ctx->converter);
  242|    409|        if (retval != READSTAT_OK)
  ------------------
  |  Branch (242:13): [True: 1, False: 408]
  ------------------
  243|      1|            goto cleanup;
  244|    408|        pad += 32;
  245|    408|    }
  246|       |
  247|       |    /* Present format names the way SAS does, e.g. "MYFMT." or "$MYFMT." */
  248|    827|    strcat(name, ".");
  249|       |
  250|    827|    if (data_size < payload_offset + pad)
  ------------------
  |  Branch (250:9): [True: 21, False: 806]
  ------------------
  251|     21|        goto cleanup;
  252|       |
  253|    806|    if (label_count_used == 0)
  ------------------
  |  Branch (253:9): [True: 80, False: 726]
  ------------------
  254|     80|        goto cleanup;
  255|       |
  256|    726|    if ((retval = sas7bcat_parse_value_labels(&data[payload_offset+pad], data_size - payload_offset - pad,
  ------------------
  |  Branch (256:9): [True: 584, False: 142]
  ------------------
  257|    726|            label_count_used, label_count_capacity, default_label_pos, string_offset, name, ctx)) != READSTAT_OK)
  258|    584|        goto cleanup;
  259|       |
  260|  1.01k|cleanup:
  261|  1.01k|    return retval;
  262|    726|}
readstat_sas7bcat_read.c:sas7bcat_parse_value_labels:
   47|    726|        const char *name, sas7bcat_ctx_t *ctx) {
   48|    726|    readstat_error_t retval = READSTAT_OK;
   49|    726|    int i;
   50|    726|    const char *lbp1 = value_start;
   51|    726|    uint32_t *value_offset = NULL;
   52|       |    /* Doubles appear to be stored as big-endian, always */
   53|    726|    int bswap_doubles = machine_is_little_endian();
   54|    726|    int is_string = 0;
   55|    726|    char *label = NULL;
   56|    726|    char *string_val = NULL;
   57|       |
   58|    726|    if (name[0] == '$') {
  ------------------
  |  Branch (58:9): [True: 117, False: 609]
  ------------------
   59|    117|        is_string = 1;
   60|    609|    } else if (name[0] == '@') {
  ------------------
  |  Branch (60:16): [True: 31, False: 578]
  ------------------
   61|     31|        goto cleanup; // Skip informats
   62|     31|    }
   63|       |
   64|    695|    value_offset = readstat_calloc(label_count_used, sizeof(uint32_t));
   65|    695|    if (value_offset == NULL) {
  ------------------
  |  Branch (65:9): [True: 144, False: 551]
  ------------------
   66|    144|        retval = READSTAT_ERROR_MALLOC;
   67|    144|        goto cleanup;
   68|    144|    }
   69|       |    /* Mark every slot as unset so that a label index which no value entry
   70|       |     * references is detected in pass 2 instead of silently reading entry 0 */
   71|   461M|    for (i=0; i<label_count_used; i++) {
  ------------------
  |  Branch (71:15): [True: 461M, False: 551]
  ------------------
   72|   461M|        value_offset[i] = UINT32_MAX;
   73|   461M|    }
   74|       |
   75|       |    /* Pass 1 -- find out the offset of the labels */
   76|  3.37M|    for (i=0; i<label_count_capacity; i++) {
  ------------------
  |  Branch (76:15): [True: 3.37M, False: 415]
  ------------------
   77|  3.37M|        if (&lbp1[4] - value_start > value_labels_len || sas_read2(&lbp1[2], ctx->bswap) < 0) {
  ------------------
  |  Branch (77:13): [True: 49, False: 3.37M]
  |  Branch (77:58): [True: 0, False: 3.37M]
  ------------------
   78|     49|            retval = READSTAT_ERROR_PARSE;
   79|     49|            goto cleanup;
   80|     49|        }
   81|  3.37M|        if (i<label_count_used) {
  ------------------
  |  Branch (81:13): [True: 85.9k, False: 3.28M]
  ------------------
   82|  85.9k|            if (&lbp1[10+ctx->pad1+4] - value_start > value_labels_len) {
  ------------------
  |  Branch (82:17): [True: 6, False: 85.9k]
  ------------------
   83|      6|                retval = READSTAT_ERROR_PARSE;
   84|      6|                goto cleanup;
   85|      6|            }
   86|  85.9k|            uint32_t label_pos = sas_read4(&lbp1[10+ctx->pad1], ctx->bswap);
   87|  85.9k|            if (label_pos >= label_count_used || (default_label_pos > 0 && label_pos == default_label_pos - 1)) {
  ------------------
  |  Branch (87:17): [True: 80, False: 85.9k]
  |  Branch (87:51): [True: 7.89k, False: 78.0k]
  |  Branch (87:76): [True: 1, False: 7.89k]
  ------------------
   88|     81|                retval = READSTAT_ERROR_PARSE;
   89|     81|                goto cleanup;
   90|     81|            }
   91|  85.9k|            value_offset[label_pos] = lbp1 - value_start;
   92|  85.9k|        }
   93|  3.37M|        lbp1 += 6 + sas_read2(&lbp1[2], ctx->bswap);
   94|  3.37M|    }
   95|       |
   96|    415|    const char *lbp2 = lbp1;
   97|       |
   98|       |    /* Pass 2 -- parse pairs of values & labels */
   99|  1.21k|    for (i=0; i<label_count_used; i++) {
  ------------------
  |  Branch (99:15): [True: 1.10k, False: 111]
  ------------------
  100|  1.10k|        readstat_value_t value = { 0 };
  101|  1.10k|        int is_default_label = default_label_pos > 0 && i == default_label_pos - 1;
  ------------------
  |  Branch (101:32): [True: 407, False: 693]
  |  Branch (101:57): [True: 30, False: 377]
  ------------------
  102|  1.10k|        if (!is_default_label) {
  ------------------
  |  Branch (102:13): [True: 1.07k, False: 30]
  ------------------
  103|       |            /* A non-default label with no value entry pointing at it would
  104|       |             * otherwise read from offset 0 and emit a duplicate value */
  105|  1.07k|            if (value_offset[i] == UINT32_MAX) {
  ------------------
  |  Branch (105:17): [True: 190, False: 880]
  ------------------
  106|    190|                retval = READSTAT_ERROR_PARSE;
  107|    190|                goto cleanup;
  108|    190|            }
  109|    880|            lbp1 = value_start + value_offset[i];
  110|       |
  111|    880|            if (&lbp1[30] - value_start > value_labels_len) {
  ------------------
  |  Branch (111:17): [True: 7, False: 873]
  ------------------
  112|      7|                retval = READSTAT_ERROR_PARSE;
  113|      7|                goto cleanup;
  114|      7|            }
  115|    873|            value.type = is_string ? READSTAT_TYPE_STRING : READSTAT_TYPE_DOUBLE;
  ------------------
  |  Branch (115:26): [True: 149, False: 724]
  ------------------
  116|    873|            if (is_string) {
  ------------------
  |  Branch (116:17): [True: 149, False: 724]
  ------------------
  117|    149|                size_t value_entry_len = 6 + sas_read2(&lbp1[2], ctx->bswap);
  118|    149|                size_t string_start = 22 + string_offset;
  119|    149|                if (string_start > value_entry_len ||
  ------------------
  |  Branch (119:21): [True: 19, False: 130]
  ------------------
  120|    130|                        &lbp1[value_entry_len] - value_start > value_labels_len) {
  ------------------
  |  Branch (120:25): [True: 25, False: 105]
  ------------------
  121|     44|                    retval = READSTAT_ERROR_PARSE;
  122|     44|                    goto cleanup;
  123|     44|                }
  124|    105|                size_t string_len = value_entry_len - string_start;
  125|    105|                char *new_string_val = realloc(string_val, 4 * string_len + 1);
  126|    105|                if (new_string_val == NULL) {
  ------------------
  |  Branch (126:21): [True: 0, False: 105]
  ------------------
  127|      0|                    retval = READSTAT_ERROR_MALLOC;
  128|      0|                    goto cleanup;
  129|      0|                }
  130|    105|                string_val = new_string_val;
  131|    105|                retval = readstat_convert(string_val, 4 * string_len + 1,
  132|    105|                        &lbp1[string_start], string_len, ctx->converter);
  133|    105|                if (retval != READSTAT_OK)
  ------------------
  |  Branch (133:21): [True: 3, False: 102]
  ------------------
  134|      3|                    goto cleanup;
  135|       |
  136|    102|                value.v.string_value = string_val;
  137|    724|            } else {
  138|    724|                uint64_t val = sas_read8(&lbp1[22], bswap_doubles);
  139|    724|                double dval = NAN;
  140|    724|                if ((val | 0xFF0000000000) == 0xFFFFFFFFFFFF) {
  ------------------
  |  Branch (140:21): [True: 257, False: 467]
  ------------------
  141|    257|                    sas_assign_tag(&value, (val >> 40));
  142|    467|                } else {
  143|    467|                    memcpy(&dval, &val, 8);
  144|    467|                    if (dval > 0.0) {
  ------------------
  |  Branch (144:25): [True: 237, False: 230]
  ------------------
  145|    237|                        val = ~val;
  146|    237|                        memcpy(&dval, &val, 8);
  147|    237|                    } else {
  148|    230|                        dval *= -1.0;
  149|    230|                    }
  150|    467|                }
  151|       |
  152|    724|                value.v.double_value = dval;
  153|    724|            }
  154|    873|        }
  155|    856|        if (&lbp2[10] > value_start + value_labels_len) {
  ------------------
  |  Branch (155:13): [True: 30, False: 826]
  ------------------
  156|     30|            retval = READSTAT_ERROR_PARSE;
  157|     30|            goto cleanup;
  158|     30|        }
  159|    826|        size_t label_len = sas_read2(&lbp2[8], ctx->bswap);
  160|       |        /* Some labels seem to overflow the reported block length, truncate it */
  161|       |        /* (Observed with formats.sasbcat from GSS2021, produced with 9.0401M6X64_SR12R2 */
  162|    826|        if (label_len > value_start + value_labels_len - &lbp2[10]) {
  ------------------
  |  Branch (162:13): [True: 123, False: 703]
  ------------------
  163|    123|            label_len = value_start + value_labels_len - &lbp2[10];
  164|    123|        }
  165|    826|        if (ctx->value_label_handler && !is_default_label) {
  ------------------
  |  Branch (165:13): [True: 826, False: 0]
  |  Branch (165:41): [True: 798, False: 28]
  ------------------
  166|    798|            label = realloc(label, 4 * label_len + 1);
  167|    798|            retval = readstat_convert(label, 4 * label_len + 1,
  168|    798|                    &lbp2[10], label_len, ctx->converter);
  169|    798|            if (retval != READSTAT_OK)
  ------------------
  |  Branch (169:17): [True: 30, False: 768]
  ------------------
  170|     30|                goto cleanup;
  171|       |
  172|    768|            if (ctx->value_label_handler(name, value, label, ctx->user_ctx) != READSTAT_HANDLER_OK) {
  ------------------
  |  |   22|    768|#define READSTAT_HANDLER_OK             0
  ------------------
  |  Branch (172:17): [True: 0, False: 768]
  ------------------
  173|      0|                retval = READSTAT_ERROR_USER_ABORT;
  174|      0|                goto cleanup;
  175|      0|            }
  176|    768|        }
  177|       |
  178|    796|        lbp2 += 8 + 2 + label_len + 1;
  179|    796|    }
  180|       |
  181|    726|cleanup:
  182|    726|    free(label);
  183|    726|    free(string_val);
  184|    726|    free(value_offset);
  185|    726|    return retval;
  186|    415|}
readstat_sas7bcat_read.c:sas7bcat_ctx_free:
   36|  1.90k|static void sas7bcat_ctx_free(sas7bcat_ctx_t *ctx) {
   37|  1.90k|    if (ctx->converter)
  ------------------
  |  Branch (37:9): [True: 949, False: 960]
  ------------------
   38|    949|        iconv_close(ctx->converter);
   39|  1.90k|    if (ctx->block_pointers)
  ------------------
  |  Branch (39:9): [True: 1.90k, False: 0]
  ------------------
   40|  1.90k|        free(ctx->block_pointers);
   41|       |
   42|  1.90k|    free(ctx);
   43|  1.90k|}

rt_open_handler:
    8|  1.90k|int rt_open_handler(const char *path, void *io_ctx) {
    9|  1.90k|    return 0;
   10|  1.90k|}
rt_close_handler:
   12|  1.90k|int rt_close_handler(void *io_ctx) {
   13|  1.90k|    return 0;
   14|  1.90k|}
rt_seek_handler:
   17|  50.0k|        readstat_io_flags_t whence, void *io_ctx) {
   18|  50.0k|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   19|  50.0k|    readstat_off_t newpos = -1;
   20|  50.0k|    if (whence == READSTAT_SEEK_SET) {
  ------------------
  |  Branch (20:9): [True: 46.7k, False: 3.30k]
  ------------------
   21|  46.7k|        newpos = offset;
   22|  46.7k|    } else if (whence == READSTAT_SEEK_CUR) {
  ------------------
  |  Branch (22:16): [True: 3.30k, False: 0]
  ------------------
   23|  3.30k|        newpos = buffer_ctx->pos + offset;
   24|  3.30k|    } else if (whence == READSTAT_SEEK_END) {
  ------------------
  |  Branch (24:16): [True: 0, False: 0]
  ------------------
   25|      0|        newpos = buffer_ctx->buffer->used + offset;
   26|      0|    }
   27|       |
   28|  50.0k|    if (newpos < 0)
  ------------------
  |  Branch (28:9): [True: 0, False: 50.0k]
  ------------------
   29|      0|        return -1;
   30|       |
   31|  50.0k|    if (newpos > buffer_ctx->buffer->used)
  ------------------
  |  Branch (31:9): [True: 236, False: 49.8k]
  ------------------
   32|    236|        return -1;
   33|       |
   34|  49.8k|    buffer_ctx->pos = newpos;
   35|  49.8k|    return newpos;
   36|  50.0k|}
rt_read_handler:
   38|  70.6k|ssize_t rt_read_handler(void *buf, size_t nbytes, void *io_ctx) {
   39|  70.6k|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   40|  70.6k|    ssize_t bytes_copied = 0;
   41|  70.6k|    ssize_t bytes_left = buffer_ctx->buffer->used - buffer_ctx->pos;
   42|  70.6k|    if (nbytes <= bytes_left) {
  ------------------
  |  Branch (42:9): [True: 70.3k, False: 300]
  ------------------
   43|  70.3k|        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, nbytes);
   44|  70.3k|        bytes_copied = nbytes;
   45|  70.3k|    } else if (bytes_left > 0) {
  ------------------
  |  Branch (45:16): [True: 145, False: 155]
  ------------------
   46|    145|        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, bytes_left);
   47|    145|        bytes_copied = bytes_left;
   48|    145|    }
   49|  70.6k|    buffer_ctx->pos += bytes_copied;
   50|  70.6k|    return bytes_copied;
   51|  70.6k|}

