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

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

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

readstat_convert:
   39|  9.63k|readstat_error_t readstat_convert(char *dst, size_t dst_len, const char *src, size_t src_len, iconv_t converter) {
   40|  9.63k|    return readstat_convert_impl(dst, dst_len, src, src_len, converter, 1);
   41|  9.63k|}
readstat_convert.c:readstat_convert_impl:
    8|  9.63k|        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|  94.6k|    while (src_len && ((trim_spaces && src[src_len-1] == ' ') || src[src_len-1] == '\0')) {
  ------------------
  |  Branch (11:12): [True: 90.3k, False: 4.30k]
  |  Branch (11:25): [True: 90.3k, False: 0]
  |  Branch (11:40): [True: 2.72k, False: 87.6k]
  |  Branch (11:66): [True: 82.3k, False: 5.32k]
  ------------------
   12|  85.0k|        src_len--;
   13|  85.0k|    }
   14|  9.63k|    if (dst_len == 0) {
  ------------------
  |  Branch (14:9): [True: 0, False: 9.63k]
  ------------------
   15|      0|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   16|  9.63k|    } else if (converter) {
  ------------------
  |  Branch (16:16): [True: 8.00k, False: 1.63k]
  ------------------
   17|  8.00k|        size_t dst_left = dst_len - 1;
   18|  8.00k|        char *dst_end = dst;
   19|  8.00k|        size_t status = iconv(converter, (readstat_iconv_inbuf_t)&src, &src_len, &dst_end, &dst_left);
   20|  8.00k|        if (status == (size_t)-1) {
  ------------------
  |  Branch (20:13): [True: 372, False: 7.63k]
  ------------------
   21|    372|            if (errno == E2BIG) {
  ------------------
  |  Branch (21:17): [True: 0, False: 372]
  ------------------
   22|      0|                return READSTAT_ERROR_CONVERT_LONG_STRING;
   23|    372|            } else if (errno == EILSEQ) {
  ------------------
  |  Branch (23:24): [True: 29, False: 343]
  ------------------
   24|     29|                return READSTAT_ERROR_CONVERT_BAD_STRING;
   25|    343|            } else if (errno != EINVAL) { /* EINVAL indicates improper truncation; accept it */
  ------------------
  |  Branch (25:24): [True: 0, False: 343]
  ------------------
   26|      0|                return READSTAT_ERROR_CONVERT;
   27|      0|            }
   28|    372|        }
   29|  7.97k|        dst[dst_len - dst_left - 1] = '\0';
   30|  7.97k|    } else if (src_len + 1 > dst_len) {
  ------------------
  |  Branch (30:16): [True: 0, False: 1.63k]
  ------------------
   31|      0|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   32|  1.63k|    } else {
   33|  1.63k|        memcpy(dst, src, src_len);
   34|  1.63k|        dst[src_len] = '\0';
   35|  1.63k|    }
   36|  9.60k|    return READSTAT_OK;
   37|  9.63k|}

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

readstat_malloc:
   10|  1.37k|void *readstat_malloc(size_t len) {
   11|  1.37k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|  2.74k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (11:9): [True: 0, False: 1.37k]
  |  Branch (11:34): [True: 0, False: 1.37k]
  ------------------
   12|      0|        return NULL;
   13|      0|    }
   14|  1.37k|    return malloc(len);
   15|  1.37k|}
readstat_calloc:
   17|  2.28k|void *readstat_calloc(size_t count, size_t size) {
   18|  2.28k|    if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  4.57k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  4.45k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  2.16k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (18:9): [True: 119, False: 2.16k]
  |  Branch (18:36): [True: 0, False: 2.16k]
  |  Branch (18:62): [True: 29, False: 2.13k]
  ------------------
   19|    148|        return NULL;
   20|    148|    }
   21|  2.13k|    if (count == 0 || size == 0) {
  ------------------
  |  Branch (21:9): [True: 6, False: 2.13k]
  |  Branch (21:23): [True: 0, False: 2.13k]
  ------------------
   22|      6|        return NULL;
   23|      6|    }
   24|  2.13k|    return calloc(count, size);
   25|  2.13k|}
readstat_realloc:
   27|  5.61k|void *readstat_realloc(void *ptr, size_t len) {
   28|  5.61k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|  11.2k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (28:9): [True: 4, False: 5.61k]
  |  Branch (28:34): [True: 0, False: 5.61k]
  ------------------
   29|      4|        if (ptr)
  ------------------
  |  Branch (29:13): [True: 1, False: 3]
  ------------------
   30|      1|            free(ptr);
   31|      4|        return NULL;
   32|      4|    }
   33|  5.61k|    return realloc(ptr, len);
   34|  5.61k|}

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

sas_read8:
  140|  4.18k|uint64_t sas_read8(const char *data, int bswap) {
  141|  4.18k|    uint64_t tmp;
  142|  4.18k|    memcpy(&tmp, data, 8);
  143|  4.18k|    return bswap ? byteswap8(tmp) : tmp;
  ------------------
  |  Branch (143:12): [True: 2.04k, False: 2.14k]
  ------------------
  144|  4.18k|}
sas_read4:
  146|   164k|uint32_t sas_read4(const char *data, int bswap) {
  147|   164k|    uint32_t tmp;
  148|   164k|    memcpy(&tmp, data, 4);
  149|   164k|    return bswap ? byteswap4(tmp) : tmp;
  ------------------
  |  Branch (149:12): [True: 6.99k, False: 157k]
  ------------------
  150|   164k|}
sas_read2:
  152|  2.56M|uint16_t sas_read2(const char *data, int bswap) {
  153|  2.56M|    uint16_t tmp;
  154|  2.56M|    memcpy(&tmp, data, 2);
  155|  2.56M|    return bswap ? byteswap2(tmp) : tmp;
  ------------------
  |  Branch (155:12): [True: 14.3k, False: 2.54M]
  ------------------
  156|  2.56M|}
sas_read_header:
  163|  1.87k|        readstat_error_handler error_handler, void *user_ctx) {
  164|  1.87k|    sas_header_start_t  header_start;
  165|  1.87k|    sas_header_end_t    header_end;
  166|  1.87k|    int retval = READSTAT_OK;
  167|  1.87k|    char error_buf[1024];
  168|  1.87k|    time_t epoch = sas_epoch();
  169|       |
  170|  1.87k|    if (io->read(&header_start, sizeof(sas_header_start_t), io->io_ctx) < sizeof(sas_header_start_t)) {
  ------------------
  |  Branch (170:9): [True: 20, False: 1.85k]
  ------------------
  171|     20|        retval = READSTAT_ERROR_READ;
  172|     20|        goto cleanup;
  173|     20|    }
  174|  1.85k|    if (memcmp(header_start.magic, sas7bdat_magic_number, sizeof(sas7bdat_magic_number)) != 0 &&
  ------------------
  |  Branch (174:9): [True: 1.13k, False: 723]
  ------------------
  175|  1.13k|            memcmp(header_start.magic, sas7bcat_magic_number, sizeof(sas7bcat_magic_number)) != 0) {
  ------------------
  |  Branch (175:13): [True: 129, False: 1.00k]
  ------------------
  176|    129|        retval = READSTAT_ERROR_PARSE;
  177|    129|        goto cleanup;
  178|    129|    }
  179|  1.72k|    if (header_start.a1 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   88|  1.72k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (179:9): [True: 12, False: 1.71k]
  ------------------
  180|     12|        hinfo->pad1 = 4;
  181|     12|    }
  182|  1.72k|    if (header_start.a2 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   88|  1.72k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (182:9): [True: 477, False: 1.25k]
  ------------------
  183|    477|        hinfo->u64 = 1;
  184|    477|    }
  185|  1.72k|    int bswap = 0;
  186|  1.72k|    if (header_start.endian == SAS_ENDIAN_BIG) {
  ------------------
  |  |   81|  1.72k|#define SAS_ENDIAN_BIG       0x00
  ------------------
  |  Branch (186:9): [True: 447, False: 1.28k]
  ------------------
  187|    447|        bswap = machine_is_little_endian();
  188|    447|        hinfo->little_endian = 0;
  189|  1.28k|    } else if (header_start.endian == SAS_ENDIAN_LITTLE) {
  ------------------
  |  |   82|  1.28k|#define SAS_ENDIAN_LITTLE    0x01
  ------------------
  |  Branch (189:16): [True: 1.26k, False: 13]
  ------------------
  190|  1.26k|        bswap = !machine_is_little_endian();
  191|  1.26k|        hinfo->little_endian = 1;
  192|  1.26k|    } else {
  193|     13|        retval = READSTAT_ERROR_PARSE;
  194|     13|        goto cleanup;
  195|     13|    }
  196|  1.71k|    int i;
  197|  19.2k|    for (i=0; i<sizeof(_charset_table)/sizeof(_charset_table[0]); i++) {
  ------------------
  |  Branch (197:15): [True: 19.2k, False: 6]
  ------------------
  198|  19.2k|        if (header_start.encoding == _charset_table[i].code) {
  ------------------
  |  Branch (198:13): [True: 1.70k, False: 17.4k]
  ------------------
  199|  1.70k|            hinfo->encoding = _charset_table[i].name;
  200|  1.70k|            break;
  201|  1.70k|        }
  202|  19.2k|    }
  203|  1.71k|    if (hinfo->encoding == NULL) {
  ------------------
  |  Branch (203:9): [True: 6, False: 1.70k]
  ------------------
  204|      6|        if (error_handler) {
  ------------------
  |  Branch (204:13): [True: 0, False: 6]
  ------------------
  205|      0|            snprintf(error_buf, sizeof(error_buf), "Unsupported character set code: %d", header_start.encoding);
  206|      0|            error_handler(error_buf, user_ctx);
  207|      0|        }
  208|      6|        retval = READSTAT_ERROR_UNSUPPORTED_CHARSET;
  209|      6|        goto cleanup;
  210|      6|    }
  211|  1.70k|    memcpy(hinfo->table_name, header_start.table_name, sizeof(header_start.table_name));
  212|  1.70k|    if (io->seek(hinfo->pad1, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (212:9): [True: 3, False: 1.70k]
  ------------------
  213|      3|        retval = READSTAT_ERROR_SEEK;
  214|      3|        goto cleanup;
  215|      3|    }
  216|       |
  217|  1.70k|    double creation_time, modification_time, creation_time_diff, modification_time_diff;
  218|       |
  219|  1.70k|    if (io->read(&creation_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (219:9): [True: 10, False: 1.69k]
  ------------------
  220|     10|        retval = READSTAT_ERROR_READ;
  221|     10|        goto cleanup;
  222|     10|    }
  223|  1.69k|    if (bswap)
  ------------------
  |  Branch (223:9): [True: 432, False: 1.26k]
  ------------------
  224|    432|        creation_time = byteswap_double(creation_time);
  225|       |
  226|  1.69k|    if (io->read(&modification_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (226:9): [True: 7, False: 1.68k]
  ------------------
  227|      7|        retval = READSTAT_ERROR_READ;
  228|      7|        goto cleanup;
  229|      7|    }
  230|  1.68k|    if (bswap)
  ------------------
  |  Branch (230:9): [True: 429, False: 1.26k]
  ------------------
  231|    429|        modification_time = byteswap_double(modification_time);
  232|       |
  233|  1.68k|    if (io->read(&creation_time_diff, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (233:9): [True: 6, False: 1.68k]
  ------------------
  234|      6|        retval = READSTAT_ERROR_READ;
  235|      6|        goto cleanup;
  236|      6|    }
  237|  1.68k|    if (bswap)
  ------------------
  |  Branch (237:9): [True: 425, False: 1.25k]
  ------------------
  238|    425|        creation_time_diff = byteswap_double(creation_time_diff);
  239|       |    
  240|  1.68k|    if (io->read(&modification_time_diff, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (240:9): [True: 7, False: 1.67k]
  ------------------
  241|      7|        retval = READSTAT_ERROR_READ;
  242|      7|        goto cleanup;
  243|      7|    }
  244|  1.67k|    if (bswap)
  ------------------
  |  Branch (244:9): [True: 420, False: 1.25k]
  ------------------
  245|    420|        modification_time_diff = byteswap_double(modification_time_diff);
  246|       |    
  247|  1.67k|    hinfo->creation_time = sas_convert_time(creation_time, creation_time_diff, epoch);
  248|  1.67k|    hinfo->modification_time = sas_convert_time(modification_time, modification_time_diff, epoch);
  249|       |
  250|  1.67k|    uint32_t header_size, page_size;
  251|       |
  252|  1.67k|    if (io->read(&header_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (252:9): [True: 8, False: 1.66k]
  ------------------
  253|      8|        retval = READSTAT_ERROR_READ;
  254|      8|        goto cleanup;
  255|      8|    }
  256|  1.66k|    if (io->read(&page_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (256:9): [True: 3, False: 1.66k]
  ------------------
  257|      3|        retval = READSTAT_ERROR_READ;
  258|      3|        goto cleanup;
  259|      3|    }
  260|       |
  261|  1.66k|    hinfo->header_size = bswap ? byteswap4(header_size) : header_size;
  ------------------
  |  Branch (261:26): [True: 414, False: 1.25k]
  ------------------
  262|  1.66k|    hinfo->page_size = bswap ? byteswap4(page_size) : page_size;
  ------------------
  |  Branch (262:24): [True: 414, False: 1.25k]
  ------------------
  263|       |
  264|  1.66k|    if (hinfo->header_size < 1024 || hinfo->page_size < 1024) {
  ------------------
  |  Branch (264:9): [True: 11, False: 1.65k]
  |  Branch (264:38): [True: 11, False: 1.64k]
  ------------------
  265|     22|        retval = READSTAT_ERROR_PARSE;
  266|     22|        goto cleanup;
  267|     22|    }
  268|  1.64k|    if (hinfo->header_size > (1<<24) || hinfo->page_size > (1<<24)) {
  ------------------
  |  Branch (268:9): [True: 14, False: 1.62k]
  |  Branch (268:41): [True: 23, False: 1.60k]
  ------------------
  269|     37|        retval = READSTAT_ERROR_PARSE;
  270|     37|        goto cleanup;
  271|     37|    }
  272|       |
  273|  1.60k|    if (hinfo->u64) {
  ------------------
  |  Branch (273:9): [True: 451, False: 1.15k]
  ------------------
  274|    451|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_64BIT;
  ------------------
  |  |  123|    451|#define SAS_PAGE_HEADER_SIZE_64BIT  40
  ------------------
  275|    451|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_64BIT;
  ------------------
  |  |  120|    451|#define SAS_SUBHEADER_POINTER_SIZE_64BIT    24
  ------------------
  276|  1.15k|    } else {
  277|  1.15k|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_32BIT;
  ------------------
  |  |  122|  1.15k|#define SAS_PAGE_HEADER_SIZE_32BIT  24
  ------------------
  278|  1.15k|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_32BIT;
  ------------------
  |  |  119|  1.15k|#define SAS_SUBHEADER_POINTER_SIZE_32BIT    12
  ------------------
  279|  1.15k|    }
  280|       |
  281|  1.60k|    if (hinfo->u64) {
  ------------------
  |  Branch (281:9): [True: 451, False: 1.15k]
  ------------------
  282|    451|        uint64_t page_count;
  283|    451|        if (io->read(&page_count, sizeof(uint64_t), io->io_ctx) < sizeof(uint64_t)) {
  ------------------
  |  Branch (283:13): [True: 16, False: 435]
  ------------------
  284|     16|            retval = READSTAT_ERROR_READ;
  285|     16|            goto cleanup;
  286|     16|        }
  287|    435|        hinfo->page_count = bswap ? byteswap8(page_count) : page_count;
  ------------------
  |  Branch (287:29): [True: 30, False: 405]
  ------------------
  288|  1.15k|    } else {
  289|  1.15k|        uint32_t page_count;
  290|  1.15k|        if (io->read(&page_count, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (290:13): [True: 25, False: 1.13k]
  ------------------
  291|     25|            retval = READSTAT_ERROR_READ;
  292|     25|            goto cleanup;
  293|     25|        }
  294|  1.13k|        hinfo->page_count = bswap ? byteswap4(page_count) : page_count;
  ------------------
  |  Branch (294:29): [True: 323, False: 807]
  ------------------
  295|  1.13k|    }
  296|  1.56k|    if (hinfo->page_count > (1<<24)) {
  ------------------
  |  Branch (296:9): [True: 14, False: 1.55k]
  ------------------
  297|     14|        retval = READSTAT_ERROR_PARSE;
  298|     14|        goto cleanup;
  299|     14|    }
  300|       |    
  301|  1.55k|    if (io->seek(8, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (301:9): [True: 72, False: 1.47k]
  ------------------
  302|     72|        retval = READSTAT_ERROR_SEEK;
  303|     72|        if (error_handler) {
  ------------------
  |  Branch (303:13): [True: 0, False: 72]
  ------------------
  304|      0|            snprintf(error_buf, sizeof(error_buf), "ReadStat: Failed to seek forward by %d", 8);
  305|      0|            error_handler(error_buf, user_ctx);
  306|      0|        }
  307|     72|        goto cleanup;
  308|     72|    }
  309|  1.47k|    if (io->read(&header_end, sizeof(sas_header_end_t), io->io_ctx) < sizeof(sas_header_end_t)) {
  ------------------
  |  Branch (309:9): [True: 17, False: 1.46k]
  ------------------
  310|     17|        retval = READSTAT_ERROR_READ;
  311|     17|        goto cleanup;
  312|     17|    }
  313|  1.46k|    char major, revision_tag;
  314|  1.46k|    int minor, revision;
  315|       |    /* The release field is not NUL-terminated in the file; copy it into a
  316|       |     * terminated buffer before handing it to sscanf, which calls strlen on it. */
  317|  1.46k|    char release[sizeof(header_end.release)+1];
  318|  1.46k|    memcpy(release, header_end.release, sizeof(header_end.release));
  319|  1.46k|    release[sizeof(header_end.release)] = '\0';
  320|  1.46k|    if (sscanf(release, "%c.%04d%c%1d", &major, &minor, &revision_tag, &revision) != 4) {
  ------------------
  |  Branch (320:9): [True: 5, False: 1.45k]
  ------------------
  321|      5|        retval = READSTAT_ERROR_PARSE;
  322|      5|        goto cleanup;
  323|      5|    }
  324|       |
  325|  1.45k|    if (major >= '1' && major <= '9') {
  ------------------
  |  Branch (325:9): [True: 1.44k, False: 10]
  |  Branch (325:25): [True: 1.43k, False: 9]
  ------------------
  326|  1.43k|        hinfo->major_version = major - '0';
  327|  1.43k|    } else if (major == 'V') {
  ------------------
  |  Branch (327:16): [True: 3, False: 16]
  ------------------
  328|       |        // It appears that SAS Visual Forecaster reports the major version as "V"
  329|       |        // Treat it as version 9 for all intents and purposes
  330|      3|        hinfo->major_version = 9;
  331|     16|    } else {
  332|     16|        retval = READSTAT_ERROR_PARSE;
  333|     16|        goto cleanup;
  334|     16|    }
  335|       |    // revision_tag is usually M, but J has been observed in the wild (not created with SAS?)
  336|  1.44k|    if ((major == '8' || major == '9') && revision_tag != 'M' && revision_tag != 'J') {
  ------------------
  |  Branch (336:10): [True: 180, False: 1.26k]
  |  Branch (336:26): [True: 247, False: 1.01k]
  |  Branch (336:43): [True: 20, False: 407]
  |  Branch (336:66): [True: 13, False: 7]
  ------------------
  337|     13|        retval = READSTAT_ERROR_PARSE;
  338|     13|        goto cleanup;
  339|     13|    }
  340|  1.42k|    hinfo->minor_version = minor;
  341|  1.42k|    hinfo->revision = revision;
  342|       |
  343|  1.42k|    if ((major == '8' || major == '9') && minor == 0 && revision == 0) {
  ------------------
  |  Branch (343:10): [True: 174, False: 1.25k]
  |  Branch (343:26): [True: 240, False: 1.01k]
  |  Branch (343:43): [True: 16, False: 398]
  |  Branch (343:57): [True: 4, False: 12]
  ------------------
  344|       |        /* A bit of a hack, but most SAS installations are running a minor update */
  345|      4|        hinfo->vendor = READSTAT_VENDOR_STAT_TRANSFER;
  346|  1.42k|    } else {
  347|  1.42k|        hinfo->vendor = READSTAT_VENDOR_SAS;
  348|  1.42k|    }
  349|  1.42k|    if (io->seek(hinfo->header_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (349:9): [True: 49, False: 1.37k]
  ------------------
  350|     49|        retval = READSTAT_ERROR_SEEK;
  351|     49|        if (error_handler) {
  ------------------
  |  Branch (351:13): [True: 0, False: 49]
  ------------------
  352|      0|            snprintf(error_buf, sizeof(error_buf), 
  353|      0|                    "ReadStat: Failed to seek to position %" PRId64, hinfo->header_size);
  354|      0|            error_handler(error_buf, user_ctx);
  355|      0|        }
  356|     49|        goto cleanup;
  357|     49|    }
  358|       |
  359|  1.87k|cleanup:
  360|  1.87k|    return retval;
  361|  1.42k|}
sas_validate_tag:
  576|  1.09k|readstat_error_t sas_validate_tag(char tag) {
  577|  1.09k|    if (tag == '_' || (tag >= 'A' && tag <= 'Z'))
  ------------------
  |  Branch (577:9): [True: 203, False: 896]
  |  Branch (577:24): [True: 619, False: 277]
  |  Branch (577:38): [True: 419, False: 200]
  ------------------
  578|    622|        return READSTAT_OK;
  579|       |
  580|    477|    return READSTAT_ERROR_TAGGED_VALUE_IS_OUT_OF_RANGE;
  581|  1.09k|}
sas_assign_tag:
  583|  1.09k|void sas_assign_tag(readstat_value_t *value, uint8_t tag) {
  584|       |    /* We accommodate two tag schemes. In the first, the tag is an ASCII code
  585|       |     * given by uint8_t tag above. System missing is represented by an ASCII
  586|       |     * period. In the second scheme, (tag-2) is an offset from 'A', except when
  587|       |     * tag == 0, in which case it represents an underscore, or tag == 1, in
  588|       |     * which case it represents system-missing.
  589|       |     */
  590|  1.09k|    if (tag == 0) {
  ------------------
  |  Branch (590:9): [True: 202, False: 897]
  ------------------
  591|    202|        tag = '_';
  592|    897|    } else if (tag >= 2 && tag < 28) {
  ------------------
  |  Branch (592:16): [True: 700, False: 197]
  |  Branch (592:28): [True: 273, False: 427]
  ------------------
  593|    273|        tag = 'A' + (tag - 2);
  594|    273|    }
  595|  1.09k|    if (sas_validate_tag(tag) == READSTAT_OK) {
  ------------------
  |  Branch (595:9): [True: 622, False: 477]
  ------------------
  596|    622|        value->tag = tag;
  597|    622|        value->is_tagged_missing = 1;
  598|    622|    } else {
  599|    477|        value->tag = 0;
  600|    477|        value->is_system_missing = 1;
  601|    477|    }
  602|  1.09k|}
readstat_sas.c:sas_epoch:
  124|  1.87k|static time_t sas_epoch(void) {
  125|  1.87k|    return - 3653 * 86400; // seconds between 01-01-1960 and 01-01-1970
  126|  1.87k|}
readstat_sas.c:sas_convert_time:
  128|  3.35k|static time_t sas_convert_time(double time, double time_diff, time_t epoch) {
  129|  3.35k|    time -= time_diff;
  130|  3.35k|    time += epoch;
  131|  3.35k|    if (isnan(time))
  ------------------
  |  Branch (131:9): [True: 109, False: 3.24k]
  ------------------
  132|    109|        return 0;
  133|  3.24k|    if (time > (double)LONG_MAX)
  ------------------
  |  Branch (133:9): [True: 377, False: 2.86k]
  ------------------
  134|    377|        return LONG_MAX;
  135|  2.86k|    if (time < (double)LONG_MIN)
  ------------------
  |  Branch (135:9): [True: 273, False: 2.59k]
  ------------------
  136|    273|        return LONG_MIN;
  137|  2.59k|    return time;
  138|  2.86k|}

readstat_parse_sas7bcat:
  429|  1.87k|readstat_error_t readstat_parse_sas7bcat(readstat_parser_t *parser, const char *path, void *user_ctx) {
  430|  1.87k|    readstat_error_t retval = READSTAT_OK;
  431|  1.87k|    readstat_io_t *io = parser->io;
  432|  1.87k|    int64_t i;
  433|  1.87k|    char *page = NULL;
  434|  1.87k|    char *buffer = NULL;
  435|       |
  436|  1.87k|    sas7bcat_ctx_t *ctx = calloc(1, sizeof(sas7bcat_ctx_t));
  437|  1.87k|    sas_header_info_t *hinfo = calloc(1, sizeof(sas_header_info_t));
  438|       |
  439|  1.87k|    ctx->block_pointers = malloc((ctx->block_pointers_capacity = 200) * sizeof(uint64_t));
  440|       |
  441|  1.87k|    ctx->value_label_handler = parser->handlers.value_label;
  442|  1.87k|    ctx->metadata_handler = parser->handlers.metadata;
  443|  1.87k|    ctx->input_encoding = parser->input_encoding;
  444|  1.87k|    ctx->output_encoding = parser->output_encoding;
  445|  1.87k|    ctx->user_ctx = user_ctx;
  446|  1.87k|    ctx->io = io;
  447|       |
  448|  1.87k|    if (io->open(path, io->io_ctx) == -1) {
  ------------------
  |  Branch (448:9): [True: 0, False: 1.87k]
  ------------------
  449|      0|        retval = READSTAT_ERROR_OPEN;
  450|      0|        goto cleanup;
  451|      0|    }
  452|       |
  453|  1.87k|    if ((retval = sas_read_header(io, hinfo, parser->handlers.error, user_ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (453:9): [True: 498, False: 1.37k]
  ------------------
  454|    498|        goto cleanup;
  455|    498|    }
  456|       |
  457|  1.37k|    ctx->u64 = hinfo->u64;
  458|  1.37k|    ctx->pad1 = hinfo->pad1;
  459|  1.37k|    ctx->bswap = machine_is_little_endian() ^ hinfo->little_endian;
  460|  1.37k|    ctx->header_size = hinfo->header_size;
  461|  1.37k|    ctx->page_count = hinfo->page_count;
  462|  1.37k|    ctx->page_size = hinfo->page_size;
  463|  1.37k|    if (ctx->input_encoding == NULL) {
  ------------------
  |  Branch (463:9): [True: 1.37k, False: 0]
  ------------------
  464|  1.37k|        ctx->input_encoding = hinfo->encoding;
  465|  1.37k|    }
  466|       |
  467|  1.37k|    ctx->xlsr_size = 212 + ctx->pad1;
  468|  1.37k|    ctx->xlsr_offset = 856 + 2 * ctx->pad1;
  469|  1.37k|    ctx->xlsr_O_offset = 50 + ctx->pad1;
  470|  1.37k|    if (ctx->u64) {
  ------------------
  |  Branch (470:9): [True: 385, False: 994]
  ------------------
  471|    385|        ctx->xlsr_offset += 144;
  472|    385|        ctx->xlsr_size += 72;
  473|    385|        ctx->xlsr_O_offset += 24;
  474|    385|    }
  475|       |
  476|  1.37k|    if (ctx->input_encoding && ctx->output_encoding && strcmp(ctx->input_encoding, ctx->output_encoding) != 0) {
  ------------------
  |  Branch (476:9): [True: 1.37k, False: 0]
  |  Branch (476:32): [True: 1.37k, False: 0]
  |  Branch (476:56): [True: 959, False: 420]
  ------------------
  477|    959|        iconv_t converter = iconv_open(ctx->output_encoding, ctx->input_encoding);
  478|    959|        if (converter == (iconv_t)-1) {
  ------------------
  |  Branch (478:13): [True: 2, False: 957]
  ------------------
  479|      2|            retval = READSTAT_ERROR_UNSUPPORTED_CHARSET;
  480|      2|            goto cleanup;
  481|      2|        }
  482|    957|        ctx->converter = converter;
  483|    957|    }
  484|       |
  485|  1.37k|    if (ctx->metadata_handler) {
  ------------------
  |  Branch (485:9): [True: 1.37k, False: 0]
  ------------------
  486|  1.37k|        char table_name[4*32+1];
  487|  1.37k|        readstat_metadata_t metadata = { 
  488|  1.37k|            .file_encoding = ctx->input_encoding, /* orig encoding? */
  489|  1.37k|            .modified_time = hinfo->modification_time,
  490|  1.37k|            .creation_time = hinfo->creation_time,
  491|  1.37k|            .file_format_version = hinfo->major_version,
  492|  1.37k|            .endianness = hinfo->little_endian ? READSTAT_ENDIAN_LITTLE : READSTAT_ENDIAN_BIG,
  ------------------
  |  Branch (492:27): [True: 1.10k, False: 273]
  ------------------
  493|  1.37k|            .is64bit = ctx->u64
  494|  1.37k|        };
  495|  1.37k|        retval = readstat_convert(table_name, sizeof(table_name),
  496|  1.37k|                hinfo->table_name, sizeof(hinfo->table_name), ctx->converter);
  497|  1.37k|        if (retval != READSTAT_OK)
  ------------------
  |  Branch (497:13): [True: 3, False: 1.37k]
  ------------------
  498|      3|            goto cleanup;
  499|       |
  500|  1.37k|        metadata.table_name = table_name;
  501|       |
  502|  1.37k|        if (ctx->metadata_handler(&metadata, ctx->user_ctx) != READSTAT_HANDLER_OK) {
  ------------------
  |  Branch (502:13): [True: 0, False: 1.37k]
  ------------------
  503|      0|            retval = READSTAT_ERROR_USER_ABORT;
  504|      0|            goto cleanup;
  505|      0|        }
  506|  1.37k|    }
  507|       |
  508|  1.37k|    if ((page = readstat_malloc(ctx->page_size)) == NULL) {
  ------------------
  |  Branch (508:9): [True: 0, False: 1.37k]
  ------------------
  509|      0|        retval = READSTAT_ERROR_MALLOC;
  510|      0|        goto cleanup;
  511|      0|    }
  512|  1.37k|    if (io->seek(ctx->header_size+SAS_CATALOG_FIRST_INDEX_PAGE*ctx->page_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  |   11|  1.37k|#define SAS_CATALOG_FIRST_INDEX_PAGE 1
  ------------------
  |  Branch (512:9): [True: 60, False: 1.31k]
  ------------------
  513|     60|        retval = READSTAT_ERROR_SEEK;
  514|     60|        goto cleanup;
  515|     60|    }
  516|  1.31k|    if (io->read(page, ctx->page_size, io->io_ctx) < ctx->page_size) {
  ------------------
  |  Branch (516:9): [True: 39, False: 1.27k]
  ------------------
  517|     39|        retval = READSTAT_ERROR_READ;
  518|     39|        goto cleanup;
  519|     39|    }
  520|       |
  521|  1.27k|    retval = sas7bcat_augment_index(&page[ctx->xlsr_offset], ctx->page_size - ctx->xlsr_offset, ctx);
  522|  1.27k|    if (retval != READSTAT_OK)
  ------------------
  |  Branch (522:9): [True: 0, False: 1.27k]
  ------------------
  523|      0|        goto cleanup;
  524|       |
  525|       |    // Pass 1 -- find the XLSR entries
  526|  25.3k|    for (i=SAS_CATALOG_USELESS_PAGES; i<ctx->page_count; i++) {
  ------------------
  |  |   12|  1.27k|#define SAS_CATALOG_USELESS_PAGES    3
  ------------------
  |  Branch (526:39): [True: 24.2k, False: 1.18k]
  ------------------
  527|  24.2k|        if (io->seek(ctx->header_size+i*ctx->page_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (527:13): [True: 20, False: 24.1k]
  ------------------
  528|     20|            retval = READSTAT_ERROR_SEEK;
  529|     20|            goto cleanup;
  530|     20|        }
  531|  24.1k|        if (io->read(page, ctx->page_size, io->io_ctx) < ctx->page_size) {
  ------------------
  |  Branch (531:13): [True: 75, False: 24.1k]
  ------------------
  532|     75|            retval = READSTAT_ERROR_READ;
  533|     75|            goto cleanup;
  534|     75|        }
  535|  24.1k|        int offset = ctx->u64 ? 32 : 16;
  ------------------
  |  Branch (535:22): [True: 980, False: 23.1k]
  ------------------
  536|  24.1k|        if (memcmp(&page[offset], "XLSR", sizeof("XLSR")-1) == 0) {
  ------------------
  |  Branch (536:13): [True: 9.52k, False: 14.5k]
  ------------------
  537|  9.52k|            retval = sas7bcat_augment_index(&page[offset], ctx->page_size - offset, ctx);
  538|  9.52k|            if (retval != READSTAT_OK)
  ------------------
  |  Branch (538:17): [True: 0, False: 9.52k]
  ------------------
  539|      0|                goto cleanup;
  540|  9.52k|        }
  541|  24.1k|    }
  542|       |
  543|  1.18k|    sas7bcat_sort_index(ctx);
  544|  1.18k|    sas7bcat_uniq_index(ctx);
  545|       |
  546|       |    // Pass 2 -- look up the individual block pointers
  547|  19.1k|    for (i=0; i<ctx->block_pointers_used; i++) {
  ------------------
  |  Branch (547:15): [True: 18.7k, False: 398]
  ------------------
  548|  18.7k|        int start_page = ctx->block_pointers[i] >> 32;
  549|  18.7k|        int start_page_pos = (ctx->block_pointers[i]) & 0xFFFF;
  550|       |
  551|  18.7k|        int buffer_len = sas7bcat_block_size(start_page, start_page_pos, ctx, &retval);
  552|  18.7k|        if (buffer_len == -1) {
  ------------------
  |  Branch (552:13): [True: 28, False: 18.7k]
  ------------------
  553|     28|            goto cleanup;
  554|  18.7k|        } else if (buffer_len == 0) {
  ------------------
  |  Branch (554:20): [True: 13.2k, False: 5.53k]
  ------------------
  555|  13.2k|            continue;
  556|  13.2k|        }
  557|  5.53k|        if ((buffer = readstat_realloc(buffer, buffer_len)) == NULL) {
  ------------------
  |  Branch (557:13): [True: 4, False: 5.53k]
  ------------------
  558|      4|            retval = READSTAT_ERROR_MALLOC;
  559|      4|            goto cleanup;
  560|      4|        }
  561|  5.53k|        if ((retval = sas7bcat_read_block(buffer, buffer_len, start_page, start_page_pos, ctx)) != READSTAT_OK)
  ------------------
  |  Branch (561:13): [True: 45, False: 5.48k]
  ------------------
  562|     45|            goto cleanup;
  563|  5.48k|        if ((retval = sas7bcat_parse_block(buffer, buffer_len, ctx)) != READSTAT_OK)
  ------------------
  |  Branch (563:13): [True: 705, False: 4.78k]
  ------------------
  564|    705|            goto cleanup;
  565|  5.48k|    }
  566|       |
  567|  1.87k|cleanup:
  568|  1.87k|    io->close(io->io_ctx);
  569|  1.87k|    if (page)
  ------------------
  |  Branch (569:9): [True: 1.37k, False: 503]
  ------------------
  570|  1.37k|        free(page);
  571|  1.87k|    if (buffer)
  ------------------
  |  Branch (571:9): [True: 921, False: 956]
  ------------------
  572|    921|        free(buffer);
  573|  1.87k|    if (ctx)
  ------------------
  |  Branch (573:9): [True: 1.87k, False: 0]
  ------------------
  574|  1.87k|        sas7bcat_ctx_free(ctx);
  575|  1.87k|    if (hinfo)
  ------------------
  |  Branch (575:9): [True: 1.87k, False: 0]
  ------------------
  576|  1.87k|        free(hinfo);
  577|       |
  578|  1.87k|    return retval;
  579|  1.18k|}
readstat_sas7bcat_read.c:sas7bcat_augment_index:
  261|  10.8k|static readstat_error_t sas7bcat_augment_index(const char *index, size_t len, sas7bcat_ctx_t *ctx) {
  262|  10.8k|    const char *xlsr = index;
  263|  10.8k|    readstat_error_t retval = READSTAT_OK;
  264|  46.3k|    while (xlsr + ctx->xlsr_size <= index + len) {
  ------------------
  |  Branch (264:12): [True: 37.2k, False: 9.05k]
  ------------------
  265|  37.2k|        if (memcmp(xlsr, "XLSR", 4) != 0) // some block pointers seem to have 8 bytes of extra padding
  ------------------
  |  Branch (265:13): [True: 17.8k, False: 19.4k]
  ------------------
  266|  17.8k|            xlsr += 8;
  267|  37.2k|        if (memcmp(xlsr, "XLSR", 4) != 0)
  ------------------
  |  Branch (267:13): [True: 1.74k, False: 35.5k]
  ------------------
  268|  1.74k|            break;
  269|       |
  270|  35.5k|        if (xlsr[ctx->xlsr_O_offset] == 'O') {
  ------------------
  |  Branch (270:13): [True: 33.1k, False: 2.39k]
  ------------------
  271|  33.1k|            uint64_t page = 0, pos = 0;
  272|  33.1k|            if (ctx->u64) {
  ------------------
  |  Branch (272:17): [True: 877, False: 32.2k]
  ------------------
  273|    877|                page = sas_read8(&xlsr[8], ctx->bswap);
  274|    877|                pos = sas_read2(&xlsr[16], ctx->bswap);
  275|  32.2k|            } else {
  276|  32.2k|                page = sas_read4(&xlsr[4], ctx->bswap);
  277|  32.2k|                pos = sas_read2(&xlsr[8], ctx->bswap);
  278|  32.2k|            }
  279|  33.1k|            ctx->block_pointers[ctx->block_pointers_used++] = (page << 32) + pos;
  280|  33.1k|        }
  281|       |
  282|  35.5k|        if (ctx->block_pointers_used == ctx->block_pointers_capacity) {
  ------------------
  |  Branch (282:13): [True: 83, False: 35.4k]
  ------------------
  283|     83|            ctx->block_pointers = readstat_realloc(ctx->block_pointers, (ctx->block_pointers_capacity *= 2) * sizeof(uint64_t));
  284|     83|            if (ctx->block_pointers == NULL) {
  ------------------
  |  Branch (284:17): [True: 0, False: 83]
  ------------------
  285|      0|                retval = READSTAT_ERROR_MALLOC;
  286|      0|                goto cleanup;
  287|      0|            }
  288|     83|        }
  289|       |
  290|  35.5k|        xlsr += ctx->xlsr_size;
  291|  35.5k|    }
  292|  10.8k|cleanup:
  293|  10.8k|    return retval;
  294|  10.8k|}
readstat_sas7bcat_read.c:sas7bcat_sort_index:
  302|  1.18k|static void sas7bcat_sort_index(sas7bcat_ctx_t *ctx) {
  303|  1.18k|    if (ctx->block_pointers_used == 0)
  ------------------
  |  Branch (303:9): [True: 104, False: 1.07k]
  ------------------
  304|    104|        return;
  305|       |
  306|  1.07k|    int i;
  307|  1.54k|    for (i=1; i<ctx->block_pointers_used; i++) {
  ------------------
  |  Branch (307:15): [True: 738, False: 808]
  ------------------
  308|    738|        if (ctx->block_pointers[i] < ctx->block_pointers[i-1]) {
  ------------------
  |  Branch (308:13): [True: 268, False: 470]
  ------------------
  309|    268|            qsort(ctx->block_pointers, ctx->block_pointers_used, sizeof(uint64_t), &compare_block_pointers);
  310|    268|            break;
  311|    268|        }
  312|    738|    }
  313|  1.07k|}
readstat_sas7bcat_read.c:compare_block_pointers:
  296|   139k|static int compare_block_pointers(const void *elem1, const void *elem2) {
  297|   139k|    uint64_t v1 = *(const uint64_t *)elem1;
  298|   139k|    uint64_t v2 = *(const uint64_t *)elem2;
  299|   139k|    return v1 - v2;
  300|   139k|}
readstat_sas7bcat_read.c:sas7bcat_uniq_index:
  315|  1.18k|static void sas7bcat_uniq_index(sas7bcat_ctx_t *ctx) {
  316|  1.18k|    if (ctx->block_pointers_used == 0)
  ------------------
  |  Branch (316:9): [True: 104, False: 1.07k]
  ------------------
  317|    104|        return;
  318|       |
  319|  1.07k|    int i;
  320|  1.07k|    int out_i = 1;
  321|  25.0k|    for (i=1; i<ctx->block_pointers_used; i++) {
  ------------------
  |  Branch (321:15): [True: 24.0k, False: 1.07k]
  ------------------
  322|  24.0k|        if (ctx->block_pointers[i] != ctx->block_pointers[i-1]) {
  ------------------
  |  Branch (322:13): [True: 18.2k, False: 5.70k]
  ------------------
  323|  18.2k|            if (out_i != i) {
  ------------------
  |  Branch (323:17): [True: 16.2k, False: 2.03k]
  ------------------
  324|  16.2k|                ctx->block_pointers[out_i] = ctx->block_pointers[i];
  325|  16.2k|            }
  326|  18.2k|            out_i++;
  327|  18.2k|        }
  328|  24.0k|    }
  329|  1.07k|    ctx->block_pointers_used = out_i;
  330|  1.07k|}
readstat_sas7bcat_read.c:sas7bcat_block_size:
  332|  18.7k|static int sas7bcat_block_size(int start_page, int start_page_pos, sas7bcat_ctx_t *ctx, readstat_error_t *outError) {
  333|  18.7k|    readstat_error_t retval = READSTAT_OK;
  334|  18.7k|    readstat_io_t *io = ctx->io;
  335|  18.7k|    int next_page = start_page;
  336|  18.7k|    int next_page_pos = start_page_pos;
  337|  18.7k|    int link_count = 0;
  338|       |
  339|  18.7k|    int buffer_len = 0;
  340|  18.7k|    int chain_link_len = 0;
  341|       |
  342|  18.7k|    char chain_link[32];
  343|  18.7k|    int chain_link_header_len = 16;
  344|  18.7k|    if (ctx->u64) {
  ------------------
  |  Branch (344:9): [True: 758, False: 18.0k]
  ------------------
  345|    758|        chain_link_header_len = 32;
  346|    758|    }
  347|       |
  348|       |    // calculate buffer size needed
  349|  39.1k|    while (next_page > 0 && next_page_pos > 0 && next_page <= ctx->page_count && link_count++ < ctx->page_count) {
  ------------------
  |  Branch (349:12): [True: 31.9k, False: 7.17k]
  |  Branch (349:29): [True: 30.7k, False: 1.25k]
  |  Branch (349:50): [True: 20.7k, False: 9.96k]
  |  Branch (349:82): [True: 20.3k, False: 373]
  ------------------
  350|  20.3k|        if (io->seek(ctx->header_size+(next_page-1)*ctx->page_size+next_page_pos, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (350:13): [True: 16, False: 20.3k]
  ------------------
  351|     16|            retval = READSTAT_ERROR_SEEK;
  352|     16|            goto cleanup;
  353|     16|        }
  354|  20.3k|        if (io->read(chain_link, chain_link_header_len, io->io_ctx) < chain_link_header_len) {
  ------------------
  |  Branch (354:13): [True: 12, False: 20.3k]
  ------------------
  355|     12|            retval = READSTAT_ERROR_READ;
  356|     12|            goto cleanup;
  357|     12|        }
  358|       |
  359|  20.3k|        if (ctx->u64) {
  ------------------
  |  Branch (359:13): [True: 1.41k, False: 18.9k]
  ------------------
  360|  1.41k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  361|  1.41k|            next_page_pos = sas_read2(&chain_link[8], ctx->bswap);
  362|  1.41k|            chain_link_len = sas_read2(&chain_link[10], ctx->bswap);
  363|  18.9k|        } else {
  364|  18.9k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  365|  18.9k|            next_page_pos = sas_read2(&chain_link[4], ctx->bswap);
  366|  18.9k|            chain_link_len = sas_read2(&chain_link[6], ctx->bswap);
  367|  18.9k|        }
  368|       |
  369|  20.3k|        buffer_len += chain_link_len;
  370|  20.3k|    }
  371|       |
  372|  18.7k|cleanup:
  373|  18.7k|    if (outError)
  ------------------
  |  Branch (373:9): [True: 18.7k, False: 0]
  ------------------
  374|  18.7k|        *outError = retval;
  375|       |
  376|  18.7k|    return retval == READSTAT_OK ? buffer_len : -1;
  ------------------
  |  Branch (376:12): [True: 18.7k, False: 28]
  ------------------
  377|  18.7k|}
readstat_sas7bcat_read.c:sas7bcat_read_block:
  380|  5.53k|        int start_page, int start_page_pos, sas7bcat_ctx_t *ctx) {
  381|  5.53k|    readstat_error_t retval = READSTAT_OK;
  382|  5.53k|    readstat_io_t *io = ctx->io;
  383|  5.53k|    int next_page = start_page;
  384|  5.53k|    int next_page_pos = start_page_pos;
  385|  5.53k|    int link_count = 0;
  386|       |
  387|  5.53k|    int chain_link_len = 0;
  388|  5.53k|    int buffer_offset = 0;
  389|       |
  390|  5.53k|    char chain_link[32];
  391|  5.53k|    int chain_link_header_len = 16;
  392|  5.53k|    if (ctx->u64) {
  ------------------
  |  Branch (392:9): [True: 589, False: 4.94k]
  ------------------
  393|    589|        chain_link_header_len = 32;
  394|    589|    }
  395|       |
  396|  22.3k|    while (next_page > 0 && next_page_pos > 0 && next_page <= ctx->page_count && link_count++ < ctx->page_count) {
  ------------------
  |  Branch (396:12): [True: 19.5k, False: 2.80k]
  |  Branch (396:29): [True: 18.5k, False: 1.00k]
  |  Branch (396:50): [True: 17.2k, False: 1.33k]
  |  Branch (396:82): [True: 16.8k, False: 339]
  ------------------
  397|  16.8k|        if (io->seek(ctx->header_size+(next_page-1)*ctx->page_size+next_page_pos, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (397:13): [True: 0, False: 16.8k]
  ------------------
  398|      0|            retval = READSTAT_ERROR_SEEK;
  399|      0|            goto cleanup;
  400|      0|        }
  401|  16.8k|        if (io->read(chain_link, chain_link_header_len, io->io_ctx) < chain_link_header_len) {
  ------------------
  |  Branch (401:13): [True: 0, False: 16.8k]
  ------------------
  402|      0|            retval = READSTAT_ERROR_READ;
  403|      0|            goto cleanup;
  404|      0|        }
  405|  16.8k|        if (ctx->u64) {
  ------------------
  |  Branch (405:13): [True: 1.16k, False: 15.7k]
  ------------------
  406|  1.16k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  407|  1.16k|            next_page_pos = sas_read2(&chain_link[8], ctx->bswap);
  408|  1.16k|            chain_link_len = sas_read2(&chain_link[10], ctx->bswap);
  409|  15.7k|        } else {
  410|  15.7k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  411|  15.7k|            next_page_pos = sas_read2(&chain_link[4], ctx->bswap);
  412|  15.7k|            chain_link_len = sas_read2(&chain_link[6], ctx->bswap);
  413|  15.7k|        }
  414|  16.8k|        if (buffer_offset + chain_link_len > buffer_len) {
  ------------------
  |  Branch (414:13): [True: 0, False: 16.8k]
  ------------------
  415|      0|            retval = READSTAT_ERROR_PARSE;
  416|      0|            goto cleanup;
  417|      0|        }
  418|  16.8k|        if (io->read(buffer + buffer_offset, chain_link_len, io->io_ctx) < chain_link_len) {
  ------------------
  |  Branch (418:13): [True: 45, False: 16.8k]
  ------------------
  419|     45|            retval = READSTAT_ERROR_READ;
  420|     45|            goto cleanup;
  421|     45|        }
  422|  16.8k|        buffer_offset += chain_link_len;
  423|  16.8k|    }
  424|  5.53k|cleanup:
  425|       |
  426|  5.53k|    return retval;
  427|  5.53k|}
readstat_sas7bcat_read.c:sas7bcat_parse_block:
  188|  5.48k|static readstat_error_t sas7bcat_parse_block(const char *data, size_t data_size, sas7bcat_ctx_t *ctx) {
  189|  5.48k|    readstat_error_t retval = READSTAT_OK;
  190|       |
  191|  5.48k|    size_t pad = 0;
  192|  5.48k|    uint64_t label_count_capacity = 0;
  193|  5.48k|    uint64_t label_count_used = 0;
  194|  5.48k|    uint64_t default_label_pos = 0;
  195|  5.48k|    int string_offset = 0;
  196|  5.48k|    int payload_offset = 106;
  197|  5.48k|    uint16_t flags = 0;
  198|  5.48k|    char name[4*32+1];
  199|       |
  200|  5.48k|    if (data_size < payload_offset)
  ------------------
  |  Branch (200:9): [True: 1.17k, False: 4.31k]
  ------------------
  201|  1.17k|        goto cleanup;
  202|       |
  203|  4.31k|    flags = sas_read2(&data[2], ctx->bswap);
  204|  4.31k|    pad = (flags & 0x08) ? 4 : 0; // might be 0x10, not sure
  ------------------
  |  Branch (204:11): [True: 2.56k, False: 1.74k]
  ------------------
  205|  4.31k|    if (ctx->u64) {
  ------------------
  |  Branch (205:9): [True: 508, False: 3.80k]
  ------------------
  206|    508|        if (data_size < 128 + pad)
  ------------------
  |  Branch (206:13): [True: 85, False: 423]
  ------------------
  207|     85|            goto cleanup;
  208|    423|        label_count_capacity = sas_read8(&data[42+pad], ctx->bswap);
  209|    423|        label_count_used = sas_read8(&data[50+pad], ctx->bswap);
  210|    423|        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|    423|        string_offset = sas_read4(&data[124+pad], ctx->bswap);
  214|       |
  215|    423|        payload_offset += 32;
  216|  3.80k|    } else {
  217|  3.80k|        if (data_size < 106 + pad)
  ------------------
  |  Branch (217:13): [True: 210, False: 3.59k]
  ------------------
  218|    210|            goto cleanup;
  219|  3.59k|        label_count_capacity = sas_read4(&data[38+pad], ctx->bswap);
  220|  3.59k|        label_count_used = sas_read4(&data[42+pad], ctx->bswap);
  221|  3.59k|        default_label_pos = sas_read4(&data[66+pad], ctx->bswap);
  222|  3.59k|        string_offset = sas_read2(&data[104+pad], ctx->bswap);
  223|  3.59k|    }
  224|       |
  225|  4.01k|    if (string_offset < 0 || default_label_pos > label_count_used) {
  ------------------
  |  Branch (225:9): [True: 23, False: 3.99k]
  |  Branch (225:30): [True: 85, False: 3.90k]
  ------------------
  226|    108|        retval = READSTAT_ERROR_PARSE;
  227|    108|        goto cleanup;
  228|    108|    }
  229|       |
  230|  3.90k|    if ((retval = readstat_convert(name, sizeof(name), &data[8], 8, ctx->converter)) != READSTAT_OK)
  ------------------
  |  Branch (230:9): [True: 4, False: 3.90k]
  ------------------
  231|      4|        goto cleanup;
  232|       |
  233|  3.90k|    if (pad) {
  ------------------
  |  Branch (233:9): [True: 2.30k, False: 1.60k]
  ------------------
  234|  2.30k|        pad += 16;
  235|  2.30k|    }
  236|       |
  237|  3.90k|    if (((flags & 0x80) && !ctx->u64) || ((flags & 0x20) && ctx->u64)) { // has long name
  ------------------
  |  Branch (237:10): [True: 2.26k, False: 1.63k]
  |  Branch (237:28): [True: 2.09k, False: 166]
  |  Branch (237:43): [True: 818, False: 987]
  |  Branch (237:61): [True: 200, False: 618]
  ------------------
  238|  2.29k|        if (data_size < payload_offset + pad + 32)
  ------------------
  |  Branch (238:13): [True: 231, False: 2.06k]
  ------------------
  239|    231|            goto cleanup;
  240|       |
  241|  2.06k|        retval = readstat_convert(name, sizeof(name), &data[payload_offset+pad], 32, ctx->converter);
  242|  2.06k|        if (retval != READSTAT_OK)
  ------------------
  |  Branch (242:13): [True: 3, False: 2.06k]
  ------------------
  243|      3|            goto cleanup;
  244|  2.06k|        pad += 32;
  245|  2.06k|    }
  246|       |
  247|  3.67k|    if (data_size < payload_offset + pad)
  ------------------
  |  Branch (247:9): [True: 203, False: 3.46k]
  ------------------
  248|    203|        goto cleanup;
  249|       |
  250|  3.46k|    if (label_count_used == 0)
  ------------------
  |  Branch (250:9): [True: 971, False: 2.49k]
  ------------------
  251|    971|        goto cleanup;
  252|       |
  253|  2.49k|    if ((retval = sas7bcat_parse_value_labels(&data[payload_offset+pad], data_size - payload_offset - pad,
  ------------------
  |  Branch (253:9): [True: 590, False: 1.90k]
  ------------------
  254|  2.49k|            label_count_used, label_count_capacity, default_label_pos, string_offset, name, ctx)) != READSTAT_OK)
  255|    590|        goto cleanup;
  256|       |
  257|  5.48k|cleanup:
  258|  5.48k|    return retval;
  259|  2.49k|}
readstat_sas7bcat_read.c:sas7bcat_parse_value_labels:
   47|  2.49k|        const char *name, sas7bcat_ctx_t *ctx) {
   48|  2.49k|    readstat_error_t retval = READSTAT_OK;
   49|  2.49k|    int i;
   50|  2.49k|    const char *lbp1 = value_start;
   51|  2.49k|    uint32_t *value_offset = NULL;
   52|       |    /* Doubles appear to be stored as big-endian, always */
   53|  2.49k|    int bswap_doubles = machine_is_little_endian();
   54|  2.49k|    int is_string = 0;
   55|  2.49k|    char *label = NULL;
   56|  2.49k|    char *string_val = NULL;
   57|       |
   58|  2.49k|    if (name[0] == '$') {
  ------------------
  |  Branch (58:9): [True: 137, False: 2.35k]
  ------------------
   59|    137|        is_string = 1;
   60|  2.35k|    } else if (name[0] == '@') {
  ------------------
  |  Branch (60:16): [True: 209, False: 2.15k]
  ------------------
   61|    209|        goto cleanup; // Skip informats
   62|    209|    }
   63|       |
   64|  2.28k|    value_offset = readstat_calloc(label_count_used, sizeof(uint32_t));
   65|  2.28k|    if (value_offset == NULL) {
  ------------------
  |  Branch (65:9): [True: 154, False: 2.13k]
  ------------------
   66|    154|        retval = READSTAT_ERROR_MALLOC;
   67|    154|        goto cleanup;
   68|    154|    }
   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|   527M|    for (i=0; i<label_count_used; i++) {
  ------------------
  |  Branch (71:15): [True: 527M, False: 2.13k]
  ------------------
   72|   527M|        value_offset[i] = UINT32_MAX;
   73|   527M|    }
   74|       |
   75|       |    /* Pass 1 -- find out the offset of the labels */
   76|  1.22M|    for (i=0; i<label_count_capacity; i++) {
  ------------------
  |  Branch (76:15): [True: 1.22M, False: 1.98k]
  ------------------
   77|  1.22M|        if (&lbp1[4] - value_start > value_labels_len || sas_read2(&lbp1[2], ctx->bswap) < 0) {
  ------------------
  |  Branch (77:13): [True: 57, False: 1.22M]
  |  Branch (77:58): [True: 0, False: 1.22M]
  ------------------
   78|     57|            retval = READSTAT_ERROR_PARSE;
   79|     57|            goto cleanup;
   80|     57|        }
   81|  1.22M|        if (i<label_count_used) {
  ------------------
  |  Branch (81:13): [True: 84.0k, False: 1.13M]
  ------------------
   82|  84.0k|            if (&lbp1[10+ctx->pad1+4] - value_start > value_labels_len) {
  ------------------
  |  Branch (82:17): [True: 5, False: 84.0k]
  ------------------
   83|      5|                retval = READSTAT_ERROR_PARSE;
   84|      5|                goto cleanup;
   85|      5|            }
   86|  84.0k|            uint32_t label_pos = sas_read4(&lbp1[10+ctx->pad1], ctx->bswap);
   87|  84.0k|            if (label_pos >= label_count_used || (default_label_pos > 0 && label_pos == default_label_pos - 1)) {
  ------------------
  |  Branch (87:17): [True: 80, False: 83.9k]
  |  Branch (87:51): [True: 6.98k, False: 76.9k]
  |  Branch (87:76): [True: 3, False: 6.97k]
  ------------------
   88|     83|                retval = READSTAT_ERROR_PARSE;
   89|     83|                goto cleanup;
   90|     83|            }
   91|  83.9k|            value_offset[label_pos] = lbp1 - value_start;
   92|  83.9k|        }
   93|  1.22M|        lbp1 += 6 + sas_read2(&lbp1[2], ctx->bswap);
   94|  1.22M|    }
   95|       |
   96|  1.98k|    const char *lbp2 = lbp1;
   97|       |
   98|       |    /* Pass 2 -- parse pairs of values & labels */
   99|  4.18k|    for (i=0; i<label_count_used; i++) {
  ------------------
  |  Branch (99:15): [True: 2.48k, False: 1.69k]
  ------------------
  100|  2.48k|        readstat_value_t value = { 0 };
  101|  2.48k|        int is_default_label = default_label_pos > 0 && i == default_label_pos - 1;
  ------------------
  |  Branch (101:32): [True: 387, False: 2.10k]
  |  Branch (101:57): [True: 70, False: 317]
  ------------------
  102|  2.48k|        if (!is_default_label) {
  ------------------
  |  Branch (102:13): [True: 2.41k, False: 70]
  ------------------
  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|  2.41k|            if (value_offset[i] == UINT32_MAX) {
  ------------------
  |  Branch (105:17): [True: 198, False: 2.22k]
  ------------------
  106|    198|                retval = READSTAT_ERROR_PARSE;
  107|    198|                goto cleanup;
  108|    198|            }
  109|  2.22k|            lbp1 = value_start + value_offset[i];
  110|       |
  111|  2.22k|            if (&lbp1[30] - value_start > value_labels_len) {
  ------------------
  |  Branch (111:17): [True: 4, False: 2.21k]
  ------------------
  112|      4|                retval = READSTAT_ERROR_PARSE;
  113|      4|                goto cleanup;
  114|      4|            }
  115|  2.21k|            value.type = is_string ? READSTAT_TYPE_STRING : READSTAT_TYPE_DOUBLE;
  ------------------
  |  Branch (115:26): [True: 176, False: 2.04k]
  ------------------
  116|  2.21k|            if (is_string) {
  ------------------
  |  Branch (116:17): [True: 176, False: 2.04k]
  ------------------
  117|    176|                size_t value_entry_len = 6 + sas_read2(&lbp1[2], ctx->bswap);
  118|    176|                size_t string_start = 22 + string_offset;
  119|    176|                if (string_start > value_entry_len ||
  ------------------
  |  Branch (119:21): [True: 10, False: 166]
  ------------------
  120|    166|                        &lbp1[value_entry_len] - value_start > value_labels_len) {
  ------------------
  |  Branch (120:25): [True: 25, False: 141]
  ------------------
  121|     35|                    retval = READSTAT_ERROR_PARSE;
  122|     35|                    goto cleanup;
  123|     35|                }
  124|    141|                size_t string_len = value_entry_len - string_start;
  125|    141|                char *new_string_val = realloc(string_val, 4 * string_len + 1);
  126|    141|                if (new_string_val == NULL) {
  ------------------
  |  Branch (126:21): [True: 0, False: 141]
  ------------------
  127|      0|                    retval = READSTAT_ERROR_MALLOC;
  128|      0|                    goto cleanup;
  129|      0|                }
  130|    141|                string_val = new_string_val;
  131|    141|                retval = readstat_convert(string_val, 4 * string_len + 1,
  132|    141|                        &lbp1[string_start], string_len, ctx->converter);
  133|    141|                if (retval != READSTAT_OK)
  ------------------
  |  Branch (133:21): [True: 2, False: 139]
  ------------------
  134|      2|                    goto cleanup;
  135|       |
  136|    139|                value.v.string_value = string_val;
  137|  2.04k|            } else {
  138|  2.04k|                uint64_t val = sas_read8(&lbp1[22], bswap_doubles);
  139|  2.04k|                double dval = NAN;
  140|  2.04k|                if ((val | 0xFF0000000000) == 0xFFFFFFFFFFFF) {
  ------------------
  |  Branch (140:21): [True: 1.09k, False: 941]
  ------------------
  141|  1.09k|                    sas_assign_tag(&value, (val >> 40));
  142|  1.09k|                } else {
  143|    941|                    memcpy(&dval, &val, 8);
  144|    941|                    if (dval > 0.0) {
  ------------------
  |  Branch (144:25): [True: 576, False: 365]
  ------------------
  145|    576|                        val = ~val;
  146|    576|                        memcpy(&dval, &val, 8);
  147|    576|                    } else {
  148|    365|                        dval *= -1.0;
  149|    365|                    }
  150|    941|                }
  151|       |
  152|  2.04k|                value.v.double_value = dval;
  153|  2.04k|            }
  154|  2.21k|        }
  155|  2.24k|        if (&lbp2[10] > value_start + value_labels_len) {
  ------------------
  |  Branch (155:13): [True: 35, False: 2.21k]
  ------------------
  156|     35|            retval = READSTAT_ERROR_PARSE;
  157|     35|            goto cleanup;
  158|     35|        }
  159|  2.21k|        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|  2.21k|        if (label_len > value_start + value_labels_len - &lbp2[10]) {
  ------------------
  |  Branch (162:13): [True: 508, False: 1.70k]
  ------------------
  163|    508|            label_len = value_start + value_labels_len - &lbp2[10];
  164|    508|        }
  165|  2.21k|        if (ctx->value_label_handler && !is_default_label) {
  ------------------
  |  Branch (165:13): [True: 2.21k, False: 0]
  |  Branch (165:41): [True: 2.14k, False: 70]
  ------------------
  166|  2.14k|            label = realloc(label, 4 * label_len + 1);
  167|  2.14k|            retval = readstat_convert(label, 4 * label_len + 1,
  168|  2.14k|                    &lbp2[10], label_len, ctx->converter);
  169|  2.14k|            if (retval != READSTAT_OK)
  ------------------
  |  Branch (169:17): [True: 17, False: 2.12k]
  ------------------
  170|     17|                goto cleanup;
  171|       |
  172|  2.12k|            if (ctx->value_label_handler(name, value, label, ctx->user_ctx) != READSTAT_HANDLER_OK) {
  ------------------
  |  Branch (172:17): [True: 0, False: 2.12k]
  ------------------
  173|      0|                retval = READSTAT_ERROR_USER_ABORT;
  174|      0|                goto cleanup;
  175|      0|            }
  176|  2.12k|        }
  177|       |
  178|  2.19k|        lbp2 += 8 + 2 + label_len + 1;
  179|  2.19k|    }
  180|       |
  181|  2.49k|cleanup:
  182|  2.49k|    free(label);
  183|  2.49k|    free(string_val);
  184|  2.49k|    free(value_offset);
  185|  2.49k|    return retval;
  186|  1.98k|}
readstat_sas7bcat_read.c:sas7bcat_ctx_free:
   36|  1.87k|static void sas7bcat_ctx_free(sas7bcat_ctx_t *ctx) {
   37|  1.87k|    if (ctx->converter)
  ------------------
  |  Branch (37:9): [True: 957, False: 920]
  ------------------
   38|    957|        iconv_close(ctx->converter);
   39|  1.87k|    if (ctx->block_pointers)
  ------------------
  |  Branch (39:9): [True: 1.87k, False: 0]
  ------------------
   40|  1.87k|        free(ctx->block_pointers);
   41|       |
   42|  1.87k|    free(ctx);
   43|  1.87k|}

rt_open_handler:
    8|  1.87k|int rt_open_handler(const char *path, void *io_ctx) {
    9|  1.87k|    return 0;
   10|  1.87k|}
rt_close_handler:
   12|  1.87k|int rt_close_handler(void *io_ctx) {
   13|  1.87k|    return 0;
   14|  1.87k|}
rt_seek_handler:
   17|  67.5k|        readstat_io_flags_t whence, void *io_ctx) {
   18|  67.5k|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   19|  67.5k|    readstat_off_t newpos = -1;
   20|  67.5k|    if (whence == READSTAT_SEEK_SET) {
  ------------------
  |  Branch (20:9): [True: 64.2k, False: 3.26k]
  ------------------
   21|  64.2k|        newpos = offset;
   22|  64.2k|    } else if (whence == READSTAT_SEEK_CUR) {
  ------------------
  |  Branch (22:16): [True: 3.26k, False: 0]
  ------------------
   23|  3.26k|        newpos = buffer_ctx->pos + offset;
   24|  3.26k|    } 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|  67.5k|    if (newpos < 0)
  ------------------
  |  Branch (28:9): [True: 0, False: 67.5k]
  ------------------
   29|      0|        return -1;
   30|       |
   31|  67.5k|    if (newpos > buffer_ctx->buffer->used)
  ------------------
  |  Branch (31:9): [True: 220, False: 67.3k]
  ------------------
   32|    220|        return -1;
   33|       |
   34|  67.3k|    buffer_ctx->pos = newpos;
   35|  67.3k|    return newpos;
   36|  67.5k|}
rt_read_handler:
   38|  94.7k|ssize_t rt_read_handler(void *buf, size_t nbytes, void *io_ctx) {
   39|  94.7k|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   40|  94.7k|    ssize_t bytes_copied = 0;
   41|  94.7k|    ssize_t bytes_left = buffer_ctx->buffer->used - buffer_ctx->pos;
   42|  94.7k|    if (nbytes <= bytes_left) {
  ------------------
  |  Branch (42:9): [True: 94.4k, False: 290]
  ------------------
   43|  94.4k|        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, nbytes);
   44|  94.4k|        bytes_copied = nbytes;
   45|  94.4k|    } else if (bytes_left > 0) {
  ------------------
  |  Branch (45:16): [True: 145, False: 145]
  ------------------
   46|    145|        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, bytes_left);
   47|    145|        bytes_copied = bytes_left;
   48|    145|    }
   49|  94.7k|    buffer_ctx->pos += bytes_copied;
   50|  94.7k|    return bytes_copied;
   51|  94.7k|}

