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

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

machine_is_little_endian:
   11|  3.78k|int machine_is_little_endian(void) {
   12|  3.78k|    int test_byte_order = 1;
   13|  3.78k|    return ((char *)&test_byte_order)[0];
   14|  3.78k|}
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|  8.74k|uint32_t byteswap4(uint32_t num) {
   45|  8.74k|    num = ((num & 0xFFFF0000) >> 16) | ((num & 0x0000FFFF) << 16);
   46|  8.74k|    return ((num & 0xFF00FF00) >> 8) | ((num & 0x00FF00FF) << 8);
   47|  8.74k|}
byteswap8:
   49|  2.36k|uint64_t byteswap8(uint64_t num) {
   50|  2.36k|    num = ((num & 0xFFFFFFFF00000000) >> 32) | ((num & 0x00000000FFFFFFFF) << 32);
   51|  2.36k|    num = ((num & 0xFFFF0000FFFF0000) >> 16) | ((num & 0x0000FFFF0000FFFF) << 16);
   52|  2.36k|    return ((num & 0xFF00FF00FF00FF00) >> 8) | ((num & 0x00FF00FF00FF00FF) << 8);
   53|  2.36k|}
byteswap_double:
   63|  1.63k|double byteswap_double(double num) {
   64|  1.63k|    uint64_t answer = 0;
   65|  1.63k|    memcpy(&answer, &num, 8);
   66|  1.63k|    answer = byteswap8(answer);
   67|  1.63k|    memcpy(&num, &answer, 8);
   68|  1.63k|    return num;
   69|  1.63k|}

readstat_convert:
   39|  3.47k|readstat_error_t readstat_convert(char *dst, size_t dst_len, const char *src, size_t src_len, iconv_t converter) {
   40|  3.47k|    return readstat_convert_impl(dst, dst_len, src, src_len, converter, 1);
   41|  3.47k|}
readstat_convert.c:readstat_convert_impl:
    8|  3.47k|        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|  20.7k|    while (src_len && ((trim_spaces && src[src_len-1] == ' ') || src[src_len-1] == '\0')) {
  ------------------
  |  Branch (11:12): [True: 20.0k, False: 790]
  |  Branch (11:25): [True: 20.0k, False: 0]
  |  Branch (11:40): [True: 2.54k, False: 17.4k]
  |  Branch (11:66): [True: 14.7k, False: 2.68k]
  ------------------
   12|  17.3k|        src_len--;
   13|  17.3k|    }
   14|  3.47k|    if (dst_len == 0) {
  ------------------
  |  Branch (14:9): [True: 0, False: 3.47k]
  ------------------
   15|      0|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   16|  3.47k|    } else if (converter) {
  ------------------
  |  Branch (16:16): [True: 2.32k, False: 1.14k]
  ------------------
   17|  2.32k|        size_t dst_left = dst_len - 1;
   18|  2.32k|        char *dst_end = dst;
   19|  2.32k|        size_t status = iconv(converter, (readstat_iconv_inbuf_t)&src, &src_len, &dst_end, &dst_left);
   20|  2.32k|        if (status == (size_t)-1) {
  ------------------
  |  Branch (20:13): [True: 171, False: 2.15k]
  ------------------
   21|    171|            if (errno == E2BIG) {
  ------------------
  |  Branch (21:17): [True: 0, False: 171]
  ------------------
   22|      0|                return READSTAT_ERROR_CONVERT_LONG_STRING;
   23|    171|            } else if (errno == EILSEQ) {
  ------------------
  |  Branch (23:24): [True: 31, False: 140]
  ------------------
   24|     31|                return READSTAT_ERROR_CONVERT_BAD_STRING;
   25|    140|            } else if (errno != EINVAL) { /* EINVAL indicates improper truncation; accept it */
  ------------------
  |  Branch (25:24): [True: 0, False: 140]
  ------------------
   26|      0|                return READSTAT_ERROR_CONVERT;
   27|      0|            }
   28|    171|        }
   29|  2.29k|        dst[dst_len - dst_left - 1] = '\0';
   30|  2.29k|    } else if (src_len + 1 > dst_len) {
  ------------------
  |  Branch (30:16): [True: 0, False: 1.14k]
  ------------------
   31|      0|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   32|  1.14k|    } else {
   33|  1.14k|        memcpy(dst, src, src_len);
   34|  1.14k|        dst[src_len] = '\0';
   35|  1.14k|    }
   36|  3.44k|    return READSTAT_OK;
   37|  3.47k|}

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

readstat_malloc:
   10|  1.33k|void *readstat_malloc(size_t len) {
   11|  1.33k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|  2.67k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (11:9): [True: 0, False: 1.33k]
  |  Branch (11:34): [True: 0, False: 1.33k]
  ------------------
   12|      0|        return NULL;
   13|      0|    }
   14|  1.33k|    return malloc(len);
   15|  1.33k|}
readstat_calloc:
   17|    706|void *readstat_calloc(size_t count, size_t size) {
   18|    706|    if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  1.41k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  1.28k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|    582|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (18:9): [True: 124, False: 582]
  |  Branch (18:36): [True: 0, False: 582]
  |  Branch (18:62): [True: 29, False: 553]
  ------------------
   19|    153|        return NULL;
   20|    153|    }
   21|    553|    if (count == 0 || size == 0) {
  ------------------
  |  Branch (21:9): [True: 5, False: 548]
  |  Branch (21:23): [True: 0, False: 548]
  ------------------
   22|      5|        return NULL;
   23|      5|    }
   24|    548|    return calloc(count, size);
   25|    553|}
readstat_realloc:
   27|  1.12k|void *readstat_realloc(void *ptr, size_t len) {
   28|  1.12k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|  2.25k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (28:9): [True: 4, False: 1.12k]
  |  Branch (28:34): [True: 0, False: 1.12k]
  ------------------
   29|      4|        if (ptr)
  ------------------
  |  Branch (29:13): [True: 2, False: 2]
  ------------------
   30|      2|            free(ptr);
   31|      4|        return NULL;
   32|      4|    }
   33|  1.12k|    return realloc(ptr, len);
   34|  1.12k|}

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

sas_read8:
  147|  1.97k|uint64_t sas_read8(const char *data, int bswap) {
  148|  1.97k|    uint64_t tmp;
  149|  1.97k|    memcpy(&tmp, data, 8);
  150|  1.97k|    return bswap ? byteswap8(tmp) : tmp;
  ------------------
  |  Branch (150:12): [True: 708, False: 1.26k]
  ------------------
  151|  1.97k|}
sas_read4:
  153|   132k|uint32_t sas_read4(const char *data, int bswap) {
  154|   132k|    uint32_t tmp;
  155|   132k|    memcpy(&tmp, data, 4);
  156|   132k|    return bswap ? byteswap4(tmp) : tmp;
  ------------------
  |  Branch (156:12): [True: 7.60k, False: 124k]
  ------------------
  157|   132k|}
sas_read2:
  159|  6.80M|uint16_t sas_read2(const char *data, int bswap) {
  160|  6.80M|    uint16_t tmp;
  161|  6.80M|    memcpy(&tmp, data, 2);
  162|  6.80M|    return bswap ? byteswap2(tmp) : tmp;
  ------------------
  |  Branch (162:12): [True: 14.8k, False: 6.79M]
  ------------------
  163|  6.80M|}
sas_read_header:
  170|  1.86k|        readstat_error_handler error_handler, void *user_ctx) {
  171|  1.86k|    sas_header_start_t  header_start;
  172|  1.86k|    sas_header_end_t    header_end;
  173|  1.86k|    int retval = READSTAT_OK;
  174|  1.86k|    char error_buf[1024];
  175|  1.86k|    time_t epoch = sas_epoch();
  176|       |
  177|  1.86k|    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.84k]
  ------------------
  178|     20|        retval = READSTAT_ERROR_READ;
  179|     20|        goto cleanup;
  180|     20|    }
  181|  1.84k|    if (memcmp(header_start.magic, sas7bdat_magic_number, sizeof(sas7bdat_magic_number)) != 0 &&
  ------------------
  |  Branch (181:9): [True: 1.09k, False: 753]
  ------------------
  182|  1.09k|            memcmp(header_start.magic, sas7bdat_magic_number_alt, sizeof(sas7bdat_magic_number_alt)) != 0 &&
  ------------------
  |  Branch (182:13): [True: 1.08k, False: 8]
  ------------------
  183|  1.08k|            memcmp(header_start.magic, sas7bcat_magic_number, sizeof(sas7bcat_magic_number)) != 0) {
  ------------------
  |  Branch (183:13): [True: 129, False: 953]
  ------------------
  184|    129|        retval = READSTAT_ERROR_PARSE;
  185|    129|        goto cleanup;
  186|    129|    }
  187|  1.71k|    if (header_start.a1 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   88|  1.71k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (187:9): [True: 9, False: 1.70k]
  ------------------
  188|      9|        hinfo->pad1 = 4;
  189|      9|    }
  190|  1.71k|    if (header_start.a2 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   88|  1.71k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (190:9): [True: 468, False: 1.24k]
  ------------------
  191|    468|        hinfo->u64 = 1;
  192|    468|    }
  193|  1.71k|    int bswap = 0;
  194|  1.71k|    if (header_start.endian == SAS_ENDIAN_BIG) {
  ------------------
  |  |   81|  1.71k|#define SAS_ENDIAN_BIG       0x00
  ------------------
  |  Branch (194:9): [True: 430, False: 1.28k]
  ------------------
  195|    430|        bswap = machine_is_little_endian();
  196|    430|        hinfo->little_endian = 0;
  197|  1.28k|    } else if (header_start.endian == SAS_ENDIAN_LITTLE) {
  ------------------
  |  |   82|  1.28k|#define SAS_ENDIAN_LITTLE    0x01
  ------------------
  |  Branch (197:16): [True: 1.27k, False: 11]
  ------------------
  198|  1.27k|        bswap = !machine_is_little_endian();
  199|  1.27k|        hinfo->little_endian = 1;
  200|  1.27k|    } else {
  201|     11|        retval = READSTAT_ERROR_PARSE;
  202|     11|        goto cleanup;
  203|     11|    }
  204|  1.70k|    int i;
  205|  53.8k|    for (i=0; i<sizeof(_charset_table)/sizeof(_charset_table[0]); i++) {
  ------------------
  |  Branch (205:15): [True: 53.3k, False: 509]
  ------------------
  206|  53.3k|        if (header_start.encoding == _charset_table[i].code) {
  ------------------
  |  Branch (206:13): [True: 1.19k, False: 52.1k]
  ------------------
  207|  1.19k|            hinfo->encoding = _charset_table[i].name;
  208|  1.19k|            break;
  209|  1.19k|        }
  210|  53.3k|    }
  211|  1.70k|    if (hinfo->encoding == NULL && header_start.encoding != 0) {
  ------------------
  |  Branch (211:9): [True: 509, False: 1.19k]
  |  Branch (211:36): [True: 9, False: 500]
  ------------------
  212|      9|        if (error_handler) {
  ------------------
  |  Branch (212:13): [True: 0, False: 9]
  ------------------
  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|      9|        retval = READSTAT_ERROR_UNSUPPORTED_CHARSET;
  217|      9|        goto cleanup;
  218|      9|    }
  219|  1.69k|    memcpy(hinfo->table_name, header_start.table_name, sizeof(header_start.table_name));
  220|  1.69k|    if (io->seek(hinfo->pad1, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (220:9): [True: 3, False: 1.69k]
  ------------------
  221|      3|        retval = READSTAT_ERROR_SEEK;
  222|      3|        goto cleanup;
  223|      3|    }
  224|       |
  225|  1.69k|    double creation_time, modification_time, creation_time_diff, modification_time_diff;
  226|       |
  227|  1.69k|    if (io->read(&creation_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (227:9): [True: 12, False: 1.67k]
  ------------------
  228|     12|        retval = READSTAT_ERROR_READ;
  229|     12|        goto cleanup;
  230|     12|    }
  231|  1.67k|    if (bswap)
  ------------------
  |  Branch (231:9): [True: 412, False: 1.26k]
  ------------------
  232|    412|        creation_time = byteswap_double(creation_time);
  233|       |
  234|  1.67k|    if (io->read(&modification_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (234:9): [True: 6, False: 1.67k]
  ------------------
  235|      6|        retval = READSTAT_ERROR_READ;
  236|      6|        goto cleanup;
  237|      6|    }
  238|  1.67k|    if (bswap)
  ------------------
  |  Branch (238:9): [True: 410, False: 1.26k]
  ------------------
  239|    410|        modification_time = byteswap_double(modification_time);
  240|       |
  241|  1.67k|    if (io->read(&creation_time_diff, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (241:9): [True: 6, False: 1.66k]
  ------------------
  242|      6|        retval = READSTAT_ERROR_READ;
  243|      6|        goto cleanup;
  244|      6|    }
  245|  1.66k|    if (bswap)
  ------------------
  |  Branch (245:9): [True: 408, False: 1.25k]
  ------------------
  246|    408|        creation_time_diff = byteswap_double(creation_time_diff);
  247|       |    
  248|  1.66k|    if (io->read(&modification_time_diff, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (248:9): [True: 6, False: 1.66k]
  ------------------
  249|      6|        retval = READSTAT_ERROR_READ;
  250|      6|        goto cleanup;
  251|      6|    }
  252|  1.66k|    if (bswap)
  ------------------
  |  Branch (252:9): [True: 406, False: 1.25k]
  ------------------
  253|    406|        modification_time_diff = byteswap_double(modification_time_diff);
  254|       |    
  255|  1.66k|    hinfo->creation_time = sas_convert_time(creation_time, creation_time_diff, epoch);
  256|  1.66k|    hinfo->modification_time = sas_convert_time(modification_time, modification_time_diff, epoch);
  257|       |
  258|  1.66k|    uint32_t header_size, page_size;
  259|       |
  260|  1.66k|    if (io->read(&header_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (260:9): [True: 8, False: 1.65k]
  ------------------
  261|      8|        retval = READSTAT_ERROR_READ;
  262|      8|        goto cleanup;
  263|      8|    }
  264|  1.65k|    if (io->read(&page_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (264:9): [True: 3, False: 1.65k]
  ------------------
  265|      3|        retval = READSTAT_ERROR_READ;
  266|      3|        goto cleanup;
  267|      3|    }
  268|       |
  269|  1.65k|    hinfo->header_size = bswap ? byteswap4(header_size) : header_size;
  ------------------
  |  Branch (269:26): [True: 401, False: 1.24k]
  ------------------
  270|  1.65k|    hinfo->page_size = bswap ? byteswap4(page_size) : page_size;
  ------------------
  |  Branch (270:24): [True: 401, False: 1.24k]
  ------------------
  271|       |
  272|  1.65k|    if (hinfo->header_size < 1024 || hinfo->page_size < 1024) {
  ------------------
  |  Branch (272:9): [True: 11, False: 1.63k]
  |  Branch (272:38): [True: 11, False: 1.62k]
  ------------------
  273|     22|        retval = READSTAT_ERROR_PARSE;
  274|     22|        goto cleanup;
  275|     22|    }
  276|  1.62k|    if (hinfo->header_size > (1<<24) || hinfo->page_size > (1<<24)) {
  ------------------
  |  Branch (276:9): [True: 18, False: 1.61k]
  |  Branch (276:41): [True: 19, False: 1.59k]
  ------------------
  277|     37|        retval = READSTAT_ERROR_PARSE;
  278|     37|        goto cleanup;
  279|     37|    }
  280|       |
  281|  1.59k|    if (hinfo->u64) {
  ------------------
  |  Branch (281:9): [True: 435, False: 1.15k]
  ------------------
  282|    435|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_64BIT;
  ------------------
  |  |  123|    435|#define SAS_PAGE_HEADER_SIZE_64BIT  40
  ------------------
  283|    435|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_64BIT;
  ------------------
  |  |  120|    435|#define SAS_SUBHEADER_POINTER_SIZE_64BIT    24
  ------------------
  284|  1.15k|    } else {
  285|  1.15k|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_32BIT;
  ------------------
  |  |  122|  1.15k|#define SAS_PAGE_HEADER_SIZE_32BIT  24
  ------------------
  286|  1.15k|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_32BIT;
  ------------------
  |  |  119|  1.15k|#define SAS_SUBHEADER_POINTER_SIZE_32BIT    12
  ------------------
  287|  1.15k|    }
  288|       |
  289|  1.59k|    if (hinfo->u64) {
  ------------------
  |  Branch (289:9): [True: 435, False: 1.15k]
  ------------------
  290|    435|        uint64_t page_count;
  291|    435|        if (io->read(&page_count, sizeof(uint64_t), io->io_ctx) < sizeof(uint64_t)) {
  ------------------
  |  Branch (291:13): [True: 12, False: 423]
  ------------------
  292|     12|            retval = READSTAT_ERROR_READ;
  293|     12|            goto cleanup;
  294|     12|        }
  295|    423|        hinfo->page_count = bswap ? byteswap8(page_count) : page_count;
  ------------------
  |  Branch (295:29): [True: 24, False: 399]
  ------------------
  296|  1.15k|    } else {
  297|  1.15k|        uint32_t page_count;
  298|  1.15k|        if (io->read(&page_count, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (298:13): [True: 26, False: 1.13k]
  ------------------
  299|     26|            retval = READSTAT_ERROR_READ;
  300|     26|            goto cleanup;
  301|     26|        }
  302|  1.13k|        hinfo->page_count = bswap ? byteswap4(page_count) : page_count;
  ------------------
  |  Branch (302:29): [True: 332, False: 798]
  ------------------
  303|  1.13k|    }
  304|  1.55k|    if (hinfo->page_count > (1<<24)) {
  ------------------
  |  Branch (304:9): [True: 16, False: 1.53k]
  ------------------
  305|     16|        retval = READSTAT_ERROR_PARSE;
  306|     16|        goto cleanup;
  307|     16|    }
  308|       |    
  309|  1.53k|    if (io->seek(8, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (309:9): [True: 72, False: 1.46k]
  ------------------
  310|     72|        retval = READSTAT_ERROR_SEEK;
  311|     72|        if (error_handler) {
  ------------------
  |  Branch (311:13): [True: 0, False: 72]
  ------------------
  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|     72|        goto cleanup;
  316|     72|    }
  317|  1.46k|    if (io->read(&header_end, sizeof(sas_header_end_t), io->io_ctx) < sizeof(sas_header_end_t)) {
  ------------------
  |  Branch (317:9): [True: 16, False: 1.44k]
  ------------------
  318|     16|        retval = READSTAT_ERROR_READ;
  319|     16|        goto cleanup;
  320|     16|    }
  321|  1.44k|    char major, revision_tag, revision;
  322|  1.44k|    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.44k|    char release[sizeof(header_end.release)+1];
  326|  1.44k|    memcpy(release, header_end.release, sizeof(header_end.release));
  327|  1.44k|    release[sizeof(header_end.release)] = '\0';
  328|  1.44k|    if (sscanf(release, "%c.%04d%c%c", &major, &minor, &revision_tag, &revision) != 4) {
  ------------------
  |  Branch (328:9): [True: 4, False: 1.44k]
  ------------------
  329|      4|        retval = READSTAT_ERROR_PARSE;
  330|      4|        goto cleanup;
  331|      4|    }
  332|       |
  333|  1.44k|    if (major >= '1' && major <= '9') {
  ------------------
  |  Branch (333:9): [True: 1.43k, False: 10]
  |  Branch (333:25): [True: 1.42k, False: 12]
  ------------------
  334|  1.42k|        hinfo->major_version = major - '0';
  335|  1.42k|    } else if (major == 'V') {
  ------------------
  |  Branch (335:16): [True: 5, False: 17]
  ------------------
  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|      5|        hinfo->major_version = 9;
  339|     17|    } else {
  340|     17|        retval = READSTAT_ERROR_PARSE;
  341|     17|        goto cleanup;
  342|     17|    }
  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.42k|    if ((major == '8' || major == '9') && revision_tag != 'M' && revision_tag != 'J' && revision_tag != 'T') {
  ------------------
  |  Branch (345:10): [True: 141, False: 1.28k]
  |  Branch (345:26): [True: 293, False: 994]
  |  Branch (345:43): [True: 39, False: 395]
  |  Branch (345:66): [True: 34, False: 5]
  |  Branch (345:89): [True: 16, False: 18]
  ------------------
  346|     16|        retval = READSTAT_ERROR_PARSE;
  347|     16|        goto cleanup;
  348|     16|    }
  349|  1.41k|    hinfo->minor_version = minor;
  350|  1.41k|    if (revision >= '0' && revision <= '9') {
  ------------------
  |  Branch (350:9): [True: 1.40k, False: 12]
  |  Branch (350:28): [True: 1.38k, False: 17]
  ------------------
  351|  1.38k|        hinfo->revision = revision - '0';
  352|  1.38k|    } else if (revision_tag == 'T' && revision == 'K') {
  ------------------
  |  Branch (352:16): [True: 17, False: 12]
  |  Branch (352:39): [True: 6, False: 11]
  ------------------
  353|       |        // files have been observed with TK in place of the revision details.
  354|      6|        hinfo->revision = 0;
  355|     23|    } else {
  356|     23|        retval = READSTAT_ERROR_PARSE;
  357|     23|        goto cleanup;
  358|     23|    }
  359|       |
  360|  1.38k|    if ((major == '8' || major == '9') && minor == 0 && hinfo->revision == 0) {
  ------------------
  |  Branch (360:10): [True: 128, False: 1.26k]
  |  Branch (360:26): [True: 279, False: 982]
  |  Branch (360:43): [True: 18, False: 389]
  |  Branch (360:57): [True: 6, False: 12]
  ------------------
  361|       |        /* A bit of a hack, but most SAS installations are running a minor update */
  362|      6|        hinfo->vendor = READSTAT_VENDOR_STAT_TRANSFER;
  363|  1.38k|    } else {
  364|  1.38k|        hinfo->vendor = READSTAT_VENDOR_SAS;
  365|  1.38k|    }
  366|  1.38k|    if (io->seek(hinfo->header_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (366:9): [True: 47, False: 1.34k]
  ------------------
  367|     47|        retval = READSTAT_ERROR_SEEK;
  368|     47|        if (error_handler) {
  ------------------
  |  Branch (368:13): [True: 0, False: 47]
  ------------------
  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|     47|        goto cleanup;
  374|     47|    }
  375|       |
  376|  1.86k|cleanup:
  377|  1.86k|    return retval;
  378|  1.38k|}
sas_validate_tag:
  599|    134|readstat_error_t sas_validate_tag(char tag) {
  600|    134|    if (tag == '_' || (tag >= 'A' && tag <= 'Z'))
  ------------------
  |  Branch (600:9): [True: 24, False: 110]
  |  Branch (600:24): [True: 49, False: 61]
  |  Branch (600:38): [True: 31, False: 18]
  ------------------
  601|     55|        return READSTAT_OK;
  602|       |
  603|     79|    return READSTAT_ERROR_TAGGED_VALUE_IS_OUT_OF_RANGE;
  604|    134|}
sas_assign_tag:
  639|    134|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|    134|    if (tag == 0) {
  ------------------
  |  Branch (646:9): [True: 23, False: 111]
  ------------------
  647|     23|        tag = '_';
  648|    111|    } else if (tag >= 2 && tag < 28) {
  ------------------
  |  Branch (648:16): [True: 100, False: 11]
  |  Branch (648:28): [True: 26, False: 74]
  ------------------
  649|     26|        tag = 'A' + (tag - 2);
  650|     26|    }
  651|    134|    if (sas_validate_tag(tag) == READSTAT_OK) {
  ------------------
  |  Branch (651:9): [True: 55, False: 79]
  ------------------
  652|     55|        value->tag = tag;
  653|     55|        value->is_tagged_missing = 1;
  654|     79|    } else {
  655|     79|        value->tag = 0;
  656|     79|        value->is_system_missing = 1;
  657|     79|    }
  658|    134|}
readstat_sas.c:sas_epoch:
  131|  1.86k|static time_t sas_epoch(void) {
  132|  1.86k|    return - 3653 * 86400; // seconds between 01-01-1960 and 01-01-1970
  133|  1.86k|}
readstat_sas.c:sas_convert_time:
  135|  3.32k|static time_t sas_convert_time(double time, double time_diff, time_t epoch) {
  136|  3.32k|    time -= time_diff;
  137|  3.32k|    time += epoch;
  138|  3.32k|    if (isnan(time))
  ------------------
  |  Branch (138:9): [True: 122, False: 3.20k]
  ------------------
  139|    122|        return 0;
  140|  3.20k|    if (time > (double)LONG_MAX)
  ------------------
  |  Branch (140:9): [True: 449, False: 2.75k]
  ------------------
  141|    449|        return LONG_MAX;
  142|  2.75k|    if (time < (double)LONG_MIN)
  ------------------
  |  Branch (142:9): [True: 300, False: 2.45k]
  ------------------
  143|    300|        return LONG_MIN;
  144|  2.45k|    return time;
  145|  2.75k|}

readstat_parse_sas7bcat:
  432|  1.86k|readstat_error_t readstat_parse_sas7bcat(readstat_parser_t *parser, const char *path, void *user_ctx) {
  433|  1.86k|    readstat_error_t retval = READSTAT_OK;
  434|  1.86k|    readstat_io_t *io = parser->io;
  435|  1.86k|    int64_t i;
  436|  1.86k|    char *page = NULL;
  437|  1.86k|    char *buffer = NULL;
  438|       |
  439|  1.86k|    sas7bcat_ctx_t *ctx = calloc(1, sizeof(sas7bcat_ctx_t));
  440|  1.86k|    sas_header_info_t *hinfo = calloc(1, sizeof(sas_header_info_t));
  441|       |
  442|  1.86k|    ctx->block_pointers = malloc((ctx->block_pointers_capacity = 200) * sizeof(uint64_t));
  443|       |
  444|  1.86k|    ctx->value_label_handler = parser->handlers.value_label;
  445|  1.86k|    ctx->metadata_handler = parser->handlers.metadata;
  446|  1.86k|    ctx->input_encoding = parser->input_encoding;
  447|  1.86k|    ctx->output_encoding = parser->output_encoding;
  448|  1.86k|    ctx->user_ctx = user_ctx;
  449|  1.86k|    ctx->io = io;
  450|       |
  451|  1.86k|    if (io->open(path, io->io_ctx) == -1) {
  ------------------
  |  Branch (451:9): [True: 0, False: 1.86k]
  ------------------
  452|      0|        retval = READSTAT_ERROR_OPEN;
  453|      0|        goto cleanup;
  454|      0|    }
  455|       |
  456|  1.86k|    if ((retval = sas_read_header(io, hinfo, parser->handlers.error, user_ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (456:9): [True: 521, False: 1.34k]
  ------------------
  457|    521|        goto cleanup;
  458|    521|    }
  459|       |
  460|  1.34k|    ctx->u64 = hinfo->u64;
  461|  1.34k|    ctx->pad1 = hinfo->pad1;
  462|  1.34k|    ctx->bswap = machine_is_little_endian() ^ hinfo->little_endian;
  463|  1.34k|    ctx->header_size = hinfo->header_size;
  464|  1.34k|    ctx->page_count = hinfo->page_count;
  465|  1.34k|    ctx->page_size = hinfo->page_size;
  466|  1.34k|    if (ctx->input_encoding == NULL) {
  ------------------
  |  Branch (466:9): [True: 1.34k, False: 0]
  ------------------
  467|  1.34k|        ctx->input_encoding = hinfo->encoding;
  468|  1.34k|    }
  469|       |
  470|  1.34k|    ctx->xlsr_size = 212 + ctx->pad1;
  471|  1.34k|    ctx->xlsr_offset = 856 + 2 * ctx->pad1;
  472|  1.34k|    ctx->xlsr_O_offset = 50 + ctx->pad1;
  473|  1.34k|    if (ctx->u64) {
  ------------------
  |  Branch (473:9): [True: 375, False: 967]
  ------------------
  474|    375|        ctx->xlsr_offset += 144;
  475|    375|        ctx->xlsr_size += 72;
  476|    375|        ctx->xlsr_O_offset += 24;
  477|    375|    }
  478|       |
  479|  1.34k|    const char *input_encoding = ctx->input_encoding;
  480|  1.34k|    if (input_encoding == NULL) {
  ------------------
  |  Branch (480:9): [True: 336, False: 1.00k]
  ------------------
  481|    336|        input_encoding = SAS_DEFAULT_STRING_ENCODING;
  ------------------
  |  |  141|    336|#define SAS_DEFAULT_STRING_ENCODING "WINDOWS-1252"
  ------------------
  482|    336|    }
  483|  1.34k|    if (ctx->output_encoding && strcmp(input_encoding, ctx->output_encoding) != 0) {
  ------------------
  |  Branch (483:9): [True: 1.34k, False: 0]
  |  Branch (483:33): [True: 933, False: 409]
  ------------------
  484|    933|        iconv_t converter = iconv_open(ctx->output_encoding, input_encoding);
  485|    933|        if (converter == (iconv_t)-1) {
  ------------------
  |  Branch (485:13): [True: 2, False: 931]
  ------------------
  486|      2|            retval = READSTAT_ERROR_UNSUPPORTED_CHARSET;
  487|      2|            goto cleanup;
  488|      2|        }
  489|    931|        ctx->converter = converter;
  490|    931|    }
  491|       |
  492|  1.34k|    if (ctx->metadata_handler) {
  ------------------
  |  Branch (492:9): [True: 1.34k, False: 0]
  ------------------
  493|  1.34k|        char table_name[4*32+1];
  494|  1.34k|        readstat_metadata_t metadata = { 
  495|  1.34k|            .file_encoding = ctx->input_encoding, /* orig encoding? */
  496|  1.34k|            .modified_time = hinfo->modification_time,
  497|  1.34k|            .creation_time = hinfo->creation_time,
  498|  1.34k|            .file_format_version = hinfo->major_version,
  499|  1.34k|            .endianness = hinfo->little_endian ? READSTAT_ENDIAN_LITTLE : READSTAT_ENDIAN_BIG,
  ------------------
  |  Branch (499:27): [True: 1.07k, False: 264]
  ------------------
  500|  1.34k|            .is64bit = ctx->u64
  501|  1.34k|        };
  502|  1.34k|        retval = readstat_convert(table_name, sizeof(table_name),
  503|  1.34k|                hinfo->table_name, sizeof(hinfo->table_name), ctx->converter);
  504|  1.34k|        if (retval != READSTAT_OK)
  ------------------
  |  Branch (504:13): [True: 5, False: 1.33k]
  ------------------
  505|      5|            goto cleanup;
  506|       |
  507|  1.33k|        metadata.table_name = table_name;
  508|       |
  509|  1.33k|        if (ctx->metadata_handler(&metadata, ctx->user_ctx) != READSTAT_HANDLER_OK) {
  ------------------
  |  |   22|  1.33k|#define READSTAT_HANDLER_OK             0
  ------------------
  |  Branch (509:13): [True: 0, False: 1.33k]
  ------------------
  510|      0|            retval = READSTAT_ERROR_USER_ABORT;
  511|      0|            goto cleanup;
  512|      0|        }
  513|  1.33k|    }
  514|       |
  515|  1.33k|    if ((page = readstat_malloc(ctx->page_size)) == NULL) {
  ------------------
  |  Branch (515:9): [True: 0, False: 1.33k]
  ------------------
  516|      0|        retval = READSTAT_ERROR_MALLOC;
  517|      0|        goto cleanup;
  518|      0|    }
  519|  1.33k|    if (io->seek(ctx->header_size+SAS_CATALOG_FIRST_INDEX_PAGE*ctx->page_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  |   11|  1.33k|#define SAS_CATALOG_FIRST_INDEX_PAGE 1
  ------------------
  |  Branch (519:9): [True: 60, False: 1.27k]
  ------------------
  520|     60|        retval = READSTAT_ERROR_SEEK;
  521|     60|        goto cleanup;
  522|     60|    }
  523|  1.27k|    if (io->read(page, ctx->page_size, io->io_ctx) < ctx->page_size) {
  ------------------
  |  Branch (523:9): [True: 41, False: 1.23k]
  ------------------
  524|     41|        retval = READSTAT_ERROR_READ;
  525|     41|        goto cleanup;
  526|     41|    }
  527|       |
  528|  1.23k|    retval = sas7bcat_augment_index(&page[ctx->xlsr_offset], ctx->page_size - ctx->xlsr_offset, ctx);
  529|  1.23k|    if (retval != READSTAT_OK)
  ------------------
  |  Branch (529:9): [True: 0, False: 1.23k]
  ------------------
  530|      0|        goto cleanup;
  531|       |
  532|       |    // Pass 1 -- find the XLSR entries
  533|  22.4k|    for (i=SAS_CATALOG_USELESS_PAGES; i<ctx->page_count; i++) {
  ------------------
  |  |   12|  1.23k|#define SAS_CATALOG_USELESS_PAGES    3
  ------------------
  |  Branch (533:39): [True: 21.3k, False: 1.13k]
  ------------------
  534|  21.3k|        if (io->seek(ctx->header_size+i*ctx->page_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (534:13): [True: 21, False: 21.3k]
  ------------------
  535|     21|            retval = READSTAT_ERROR_SEEK;
  536|     21|            goto cleanup;
  537|     21|        }
  538|  21.3k|        if (io->read(page, ctx->page_size, io->io_ctx) < ctx->page_size) {
  ------------------
  |  Branch (538:13): [True: 77, False: 21.2k]
  ------------------
  539|     77|            retval = READSTAT_ERROR_READ;
  540|     77|            goto cleanup;
  541|     77|        }
  542|  21.2k|        int offset = ctx->u64 ? 32 : 16;
  ------------------
  |  Branch (542:22): [True: 1.26k, False: 19.9k]
  ------------------
  543|  21.2k|        if (memcmp(&page[offset], "XLSR", sizeof("XLSR")-1) == 0) {
  ------------------
  |  Branch (543:13): [True: 5.87k, False: 15.3k]
  ------------------
  544|  5.87k|            retval = sas7bcat_augment_index(&page[offset], ctx->page_size - offset, ctx);
  545|  5.87k|            if (retval != READSTAT_OK)
  ------------------
  |  Branch (545:17): [True: 0, False: 5.87k]
  ------------------
  546|      0|                goto cleanup;
  547|  5.87k|        }
  548|  21.2k|    }
  549|       |
  550|  1.13k|    sas7bcat_sort_index(ctx);
  551|  1.13k|    sas7bcat_uniq_index(ctx);
  552|       |
  553|       |    // Pass 2 -- look up the individual block pointers
  554|  2.33k|    for (i=0; i<ctx->block_pointers_used; i++) {
  ------------------
  |  Branch (554:15): [True: 1.97k, False: 366]
  ------------------
  555|  1.97k|        int start_page = ctx->block_pointers[i] >> 32;
  556|  1.97k|        int start_page_pos = (ctx->block_pointers[i]) & 0xFFFF;
  557|       |
  558|  1.97k|        int buffer_len = sas7bcat_block_size(start_page, start_page_pos, ctx, &retval);
  559|  1.97k|        if (buffer_len == -1) {
  ------------------
  |  Branch (559:13): [True: 26, False: 1.94k]
  ------------------
  560|     26|            goto cleanup;
  561|  1.94k|        } else if (buffer_len == 0) {
  ------------------
  |  Branch (561:20): [True: 869, False: 1.07k]
  ------------------
  562|    869|            continue;
  563|    869|        }
  564|  1.07k|        if ((buffer = readstat_realloc(buffer, buffer_len)) == NULL) {
  ------------------
  |  Branch (564:13): [True: 4, False: 1.07k]
  ------------------
  565|      4|            retval = READSTAT_ERROR_MALLOC;
  566|      4|            goto cleanup;
  567|      4|        }
  568|  1.07k|        if ((retval = sas7bcat_read_block(buffer, buffer_len, start_page, start_page_pos, ctx)) != READSTAT_OK)
  ------------------
  |  Branch (568:13): [True: 46, False: 1.02k]
  ------------------
  569|     46|            goto cleanup;
  570|  1.02k|        if ((retval = sas7bcat_parse_block(buffer, buffer_len, ctx)) != READSTAT_OK)
  ------------------
  |  Branch (570:13): [True: 694, False: 333]
  ------------------
  571|    694|            goto cleanup;
  572|  1.02k|    }
  573|       |
  574|  1.86k|cleanup:
  575|  1.86k|    io->close(io->io_ctx);
  576|  1.86k|    if (page)
  ------------------
  |  Branch (576:9): [True: 1.33k, False: 528]
  ------------------
  577|  1.33k|        free(page);
  578|  1.86k|    if (buffer)
  ------------------
  |  Branch (578:9): [True: 866, False: 997]
  ------------------
  579|    866|        free(buffer);
  580|  1.86k|    if (ctx)
  ------------------
  |  Branch (580:9): [True: 1.86k, False: 0]
  ------------------
  581|  1.86k|        sas7bcat_ctx_free(ctx);
  582|  1.86k|    if (hinfo)
  ------------------
  |  Branch (582:9): [True: 1.86k, False: 0]
  ------------------
  583|  1.86k|        free(hinfo);
  584|       |
  585|  1.86k|    return retval;
  586|  1.13k|}
readstat_sas7bcat_read.c:sas7bcat_augment_index:
  261|  7.11k|static readstat_error_t sas7bcat_augment_index(const char *index, size_t len, sas7bcat_ctx_t *ctx) {
  262|  7.11k|    const char *xlsr = index;
  263|  7.11k|    readstat_error_t retval = READSTAT_OK;
  264|  29.2k|    while (xlsr + ctx->xlsr_size <= index + len) {
  ------------------
  |  Branch (264:12): [True: 23.2k, False: 5.97k]
  ------------------
  265|  23.2k|        if (memcmp(xlsr, "XLSR", 4) != 0) // some block pointers seem to have 8 bytes of extra padding
  ------------------
  |  Branch (265:13): [True: 11.4k, False: 11.7k]
  ------------------
  266|  11.4k|            xlsr += 8;
  267|  23.2k|        if (memcmp(xlsr, "XLSR", 4) != 0)
  ------------------
  |  Branch (267:13): [True: 1.14k, False: 22.1k]
  ------------------
  268|  1.14k|            break;
  269|       |
  270|  22.1k|        if (xlsr[ctx->xlsr_O_offset] == 'O') {
  ------------------
  |  Branch (270:13): [True: 20.7k, False: 1.32k]
  ------------------
  271|  20.7k|            uint64_t page = 0, pos = 0;
  272|  20.7k|            if (ctx->u64) {
  ------------------
  |  Branch (272:17): [True: 594, False: 20.1k]
  ------------------
  273|    594|                page = sas_read8(&xlsr[8], ctx->bswap);
  274|    594|                pos = sas_read2(&xlsr[16], ctx->bswap);
  275|  20.1k|            } else {
  276|  20.1k|                page = sas_read4(&xlsr[4], ctx->bswap);
  277|  20.1k|                pos = sas_read2(&xlsr[8], ctx->bswap);
  278|  20.1k|            }
  279|  20.7k|            ctx->block_pointers[ctx->block_pointers_used++] = (page << 32) + pos;
  280|  20.7k|        }
  281|       |
  282|  22.1k|        if (ctx->block_pointers_used == ctx->block_pointers_capacity) {
  ------------------
  |  Branch (282:13): [True: 49, False: 22.0k]
  ------------------
  283|     49|            ctx->block_pointers = readstat_realloc(ctx->block_pointers, (ctx->block_pointers_capacity *= 2) * sizeof(uint64_t));
  284|     49|            if (ctx->block_pointers == NULL) {
  ------------------
  |  Branch (284:17): [True: 0, False: 49]
  ------------------
  285|      0|                retval = READSTAT_ERROR_MALLOC;
  286|      0|                goto cleanup;
  287|      0|            }
  288|     49|        }
  289|       |
  290|  22.1k|        xlsr += ctx->xlsr_size;
  291|  22.1k|    }
  292|  7.11k|cleanup:
  293|  7.11k|    return retval;
  294|  7.11k|}
readstat_sas7bcat_read.c:sas7bcat_sort_index:
  305|  1.13k|static void sas7bcat_sort_index(sas7bcat_ctx_t *ctx) {
  306|  1.13k|    if (ctx->block_pointers_used == 0)
  ------------------
  |  Branch (306:9): [True: 107, False: 1.02k]
  ------------------
  307|    107|        return;
  308|       |
  309|  1.02k|    int i;
  310|  1.45k|    for (i=1; i<ctx->block_pointers_used; i++) {
  ------------------
  |  Branch (310:15): [True: 640, False: 811]
  ------------------
  311|    640|        if (ctx->block_pointers[i] < ctx->block_pointers[i-1]) {
  ------------------
  |  Branch (311:13): [True: 218, False: 422]
  ------------------
  312|    218|            qsort(ctx->block_pointers, ctx->block_pointers_used, sizeof(uint64_t), &compare_block_pointers);
  313|    218|            break;
  314|    218|        }
  315|    640|    }
  316|  1.02k|}
readstat_sas7bcat_read.c:compare_block_pointers:
  296|  78.8k|static int compare_block_pointers(const void *elem1, const void *elem2) {
  297|  78.8k|    uint64_t v1 = *(const uint64_t *)elem1;
  298|  78.8k|    uint64_t v2 = *(const uint64_t *)elem2;
  299|       |    /* The values pack a page number into the high 32 bits, so a plain
  300|       |     * subtraction truncated to int loses that half and misorders (or reports
  301|       |     * as equal) pointers on different pages. Compare three-way instead. */
  302|  78.8k|    return (v1 > v2) - (v1 < v2);
  303|  78.8k|}
readstat_sas7bcat_read.c:sas7bcat_uniq_index:
  318|  1.13k|static void sas7bcat_uniq_index(sas7bcat_ctx_t *ctx) {
  319|  1.13k|    if (ctx->block_pointers_used == 0)
  ------------------
  |  Branch (319:9): [True: 107, False: 1.02k]
  ------------------
  320|    107|        return;
  321|       |
  322|  1.02k|    int i;
  323|  1.02k|    int out_i = 1;
  324|  12.1k|    for (i=1; i<ctx->block_pointers_used; i++) {
  ------------------
  |  Branch (324:15): [True: 11.1k, False: 1.02k]
  ------------------
  325|  11.1k|        if (ctx->block_pointers[i] != ctx->block_pointers[i-1]) {
  ------------------
  |  Branch (325:13): [True: 1.35k, False: 9.80k]
  ------------------
  326|  1.35k|            if (out_i != i) {
  ------------------
  |  Branch (326:17): [True: 945, False: 408]
  ------------------
  327|    945|                ctx->block_pointers[out_i] = ctx->block_pointers[i];
  328|    945|            }
  329|  1.35k|            out_i++;
  330|  1.35k|        }
  331|  11.1k|    }
  332|  1.02k|    ctx->block_pointers_used = out_i;
  333|  1.02k|}
readstat_sas7bcat_read.c:sas7bcat_block_size:
  335|  1.97k|static int sas7bcat_block_size(int start_page, int start_page_pos, sas7bcat_ctx_t *ctx, readstat_error_t *outError) {
  336|  1.97k|    readstat_error_t retval = READSTAT_OK;
  337|  1.97k|    readstat_io_t *io = ctx->io;
  338|  1.97k|    int next_page = start_page;
  339|  1.97k|    int next_page_pos = start_page_pos;
  340|  1.97k|    int link_count = 0;
  341|       |
  342|  1.97k|    int buffer_len = 0;
  343|  1.97k|    int chain_link_len = 0;
  344|       |
  345|  1.97k|    char chain_link[32];
  346|  1.97k|    int chain_link_header_len = 16;
  347|  1.97k|    if (ctx->u64) {
  ------------------
  |  Branch (347:9): [True: 309, False: 1.66k]
  ------------------
  348|    309|        chain_link_header_len = 32;
  349|    309|    }
  350|       |
  351|       |    // calculate buffer size needed
  352|  15.3k|    while (next_page > 0 && next_page_pos > 0 && next_page <= ctx->page_count && link_count++ < ctx->page_count) {
  ------------------
  |  Branch (352:12): [True: 14.6k, False: 711]
  |  Branch (352:29): [True: 14.4k, False: 193]
  |  Branch (352:50): [True: 13.6k, False: 816]
  |  Branch (352:82): [True: 13.4k, False: 226]
  ------------------
  353|  13.4k|        if (io->seek(ctx->header_size+(next_page-1)*ctx->page_size+next_page_pos, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (353:13): [True: 13, False: 13.4k]
  ------------------
  354|     13|            retval = READSTAT_ERROR_SEEK;
  355|     13|            goto cleanup;
  356|     13|        }
  357|  13.4k|        if (io->read(chain_link, chain_link_header_len, io->io_ctx) < chain_link_header_len) {
  ------------------
  |  Branch (357:13): [True: 13, False: 13.4k]
  ------------------
  358|     13|            retval = READSTAT_ERROR_READ;
  359|     13|            goto cleanup;
  360|     13|        }
  361|       |
  362|  13.4k|        if (ctx->u64) {
  ------------------
  |  Branch (362:13): [True: 1.17k, False: 12.2k]
  ------------------
  363|  1.17k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  364|  1.17k|            next_page_pos = sas_read2(&chain_link[8], ctx->bswap);
  365|  1.17k|            chain_link_len = sas_read2(&chain_link[10], ctx->bswap);
  366|  12.2k|        } else {
  367|  12.2k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  368|  12.2k|            next_page_pos = sas_read2(&chain_link[4], ctx->bswap);
  369|  12.2k|            chain_link_len = sas_read2(&chain_link[6], ctx->bswap);
  370|  12.2k|        }
  371|       |
  372|  13.4k|        buffer_len += chain_link_len;
  373|  13.4k|    }
  374|       |
  375|  1.97k|cleanup:
  376|  1.97k|    if (outError)
  ------------------
  |  Branch (376:9): [True: 1.97k, False: 0]
  ------------------
  377|  1.97k|        *outError = retval;
  378|       |
  379|  1.97k|    return retval == READSTAT_OK ? buffer_len : -1;
  ------------------
  |  Branch (379:12): [True: 1.94k, False: 26]
  ------------------
  380|  1.97k|}
readstat_sas7bcat_read.c:sas7bcat_read_block:
  383|  1.07k|        int start_page, int start_page_pos, sas7bcat_ctx_t *ctx) {
  384|  1.07k|    readstat_error_t retval = READSTAT_OK;
  385|  1.07k|    readstat_io_t *io = ctx->io;
  386|  1.07k|    int next_page = start_page;
  387|  1.07k|    int next_page_pos = start_page_pos;
  388|  1.07k|    int link_count = 0;
  389|       |
  390|  1.07k|    int chain_link_len = 0;
  391|  1.07k|    int buffer_offset = 0;
  392|       |
  393|  1.07k|    char chain_link[32];
  394|  1.07k|    int chain_link_header_len = 16;
  395|  1.07k|    if (ctx->u64) {
  ------------------
  |  Branch (395:9): [True: 255, False: 818]
  ------------------
  396|    255|        chain_link_header_len = 32;
  397|    255|    }
  398|       |
  399|  11.7k|    while (next_page > 0 && next_page_pos > 0 && next_page <= ctx->page_count && link_count++ < ctx->page_count) {
  ------------------
  |  Branch (399:12): [True: 11.3k, False: 398]
  |  Branch (399:29): [True: 11.2k, False: 106]
  |  Branch (399:50): [True: 10.9k, False: 324]
  |  Branch (399:82): [True: 10.7k, False: 199]
  ------------------
  400|  10.7k|        if (io->seek(ctx->header_size+(next_page-1)*ctx->page_size+next_page_pos, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (400:13): [True: 0, False: 10.7k]
  ------------------
  401|      0|            retval = READSTAT_ERROR_SEEK;
  402|      0|            goto cleanup;
  403|      0|        }
  404|  10.7k|        if (io->read(chain_link, chain_link_header_len, io->io_ctx) < chain_link_header_len) {
  ------------------
  |  Branch (404:13): [True: 0, False: 10.7k]
  ------------------
  405|      0|            retval = READSTAT_ERROR_READ;
  406|      0|            goto cleanup;
  407|      0|        }
  408|  10.7k|        if (ctx->u64) {
  ------------------
  |  Branch (408:13): [True: 1.15k, False: 9.59k]
  ------------------
  409|  1.15k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  410|  1.15k|            next_page_pos = sas_read2(&chain_link[8], ctx->bswap);
  411|  1.15k|            chain_link_len = sas_read2(&chain_link[10], ctx->bswap);
  412|  9.59k|        } else {
  413|  9.59k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  414|  9.59k|            next_page_pos = sas_read2(&chain_link[4], ctx->bswap);
  415|  9.59k|            chain_link_len = sas_read2(&chain_link[6], ctx->bswap);
  416|  9.59k|        }
  417|  10.7k|        if (buffer_offset + chain_link_len > buffer_len) {
  ------------------
  |  Branch (417:13): [True: 0, False: 10.7k]
  ------------------
  418|      0|            retval = READSTAT_ERROR_PARSE;
  419|      0|            goto cleanup;
  420|      0|        }
  421|  10.7k|        if (io->read(buffer + buffer_offset, chain_link_len, io->io_ctx) < chain_link_len) {
  ------------------
  |  Branch (421:13): [True: 46, False: 10.6k]
  ------------------
  422|     46|            retval = READSTAT_ERROR_READ;
  423|     46|            goto cleanup;
  424|     46|        }
  425|  10.6k|        buffer_offset += chain_link_len;
  426|  10.6k|    }
  427|  1.07k|cleanup:
  428|       |
  429|  1.07k|    return retval;
  430|  1.07k|}
readstat_sas7bcat_read.c:sas7bcat_parse_block:
  188|  1.02k|static readstat_error_t sas7bcat_parse_block(const char *data, size_t data_size, sas7bcat_ctx_t *ctx) {
  189|  1.02k|    readstat_error_t retval = READSTAT_OK;
  190|       |
  191|  1.02k|    size_t pad = 0;
  192|  1.02k|    uint64_t label_count_capacity = 0;
  193|  1.02k|    uint64_t label_count_used = 0;
  194|  1.02k|    uint64_t default_label_pos = 0;
  195|  1.02k|    int string_offset = 0;
  196|  1.02k|    int payload_offset = 106;
  197|  1.02k|    uint16_t flags = 0;
  198|  1.02k|    char name[4*32+1];
  199|       |
  200|  1.02k|    if (data_size < payload_offset)
  ------------------
  |  Branch (200:9): [True: 47, False: 980]
  ------------------
  201|     47|        goto cleanup;
  202|       |
  203|    980|    flags = sas_read2(&data[2], ctx->bswap);
  204|    980|    pad = (flags & 0x08) ? 4 : 0; // might be 0x10, not sure
  ------------------
  |  Branch (204:11): [True: 393, False: 587]
  ------------------
  205|    980|    if (ctx->u64) {
  ------------------
  |  Branch (205:9): [True: 236, False: 744]
  ------------------
  206|    236|        if (data_size < 128 + pad)
  ------------------
  |  Branch (206:13): [True: 12, False: 224]
  ------------------
  207|     12|            goto cleanup;
  208|    224|        label_count_capacity = sas_read8(&data[42+pad], ctx->bswap);
  209|    224|        label_count_used = sas_read8(&data[50+pad], ctx->bswap);
  210|    224|        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|    224|        string_offset = sas_read4(&data[124+pad], ctx->bswap);
  214|       |
  215|    224|        payload_offset += 32;
  216|    744|    } else {
  217|    744|        if (data_size < 106 + pad)
  ------------------
  |  Branch (217:13): [True: 15, False: 729]
  ------------------
  218|     15|            goto cleanup;
  219|    729|        label_count_capacity = sas_read4(&data[38+pad], ctx->bswap);
  220|    729|        label_count_used = sas_read4(&data[42+pad], ctx->bswap);
  221|    729|        default_label_pos = sas_read4(&data[66+pad], ctx->bswap);
  222|    729|        string_offset = sas_read2(&data[104+pad], ctx->bswap);
  223|    729|    }
  224|       |
  225|    953|    if (string_offset < 0 || default_label_pos > label_count_used) {
  ------------------
  |  Branch (225:9): [True: 34, False: 919]
  |  Branch (225:30): [True: 65, False: 854]
  ------------------
  226|     99|        retval = READSTAT_ERROR_PARSE;
  227|     99|        goto cleanup;
  228|     99|    }
  229|       |
  230|    854|    if ((retval = readstat_convert(name, sizeof(name), &data[8], 8, ctx->converter)) != READSTAT_OK)
  ------------------
  |  Branch (230:9): [True: 3, False: 851]
  ------------------
  231|      3|        goto cleanup;
  232|       |
  233|    851|    if (pad) {
  ------------------
  |  Branch (233:9): [True: 330, False: 521]
  ------------------
  234|    330|        pad += 16;
  235|    330|    }
  236|       |
  237|    851|    if (((flags & 0x80) && !ctx->u64) || ((flags & 0x20) && ctx->u64)) { // has long name
  ------------------
  |  Branch (237:10): [True: 429, False: 422]
  |  Branch (237:28): [True: 346, False: 83]
  |  Branch (237:43): [True: 185, False: 320]
  |  Branch (237:61): [True: 85, False: 100]
  ------------------
  238|    431|        if (data_size < payload_offset + pad + 32)
  ------------------
  |  Branch (238:13): [True: 20, False: 411]
  ------------------
  239|     20|            goto cleanup;
  240|       |
  241|    411|        retval = readstat_convert(name, sizeof(name), &data[payload_offset+pad], 32, ctx->converter);
  242|    411|        if (retval != READSTAT_OK)
  ------------------
  |  Branch (242:13): [True: 2, False: 409]
  ------------------
  243|      2|            goto cleanup;
  244|    409|        pad += 32;
  245|    409|    }
  246|       |
  247|    829|    if (data_size < payload_offset + pad)
  ------------------
  |  Branch (247:9): [True: 18, False: 811]
  ------------------
  248|     18|        goto cleanup;
  249|       |
  250|    811|    if (label_count_used == 0)
  ------------------
  |  Branch (250:9): [True: 76, False: 735]
  ------------------
  251|     76|        goto cleanup;
  252|       |
  253|    735|    if ((retval = sas7bcat_parse_value_labels(&data[payload_offset+pad], data_size - payload_offset - pad,
  ------------------
  |  Branch (253:9): [True: 590, False: 145]
  ------------------
  254|    735|            label_count_used, label_count_capacity, default_label_pos, string_offset, name, ctx)) != READSTAT_OK)
  255|    590|        goto cleanup;
  256|       |
  257|  1.02k|cleanup:
  258|  1.02k|    return retval;
  259|    735|}
readstat_sas7bcat_read.c:sas7bcat_parse_value_labels:
   47|    735|        const char *name, sas7bcat_ctx_t *ctx) {
   48|    735|    readstat_error_t retval = READSTAT_OK;
   49|    735|    int i;
   50|    735|    const char *lbp1 = value_start;
   51|    735|    uint32_t *value_offset = NULL;
   52|       |    /* Doubles appear to be stored as big-endian, always */
   53|    735|    int bswap_doubles = machine_is_little_endian();
   54|    735|    int is_string = 0;
   55|    735|    char *label = NULL;
   56|    735|    char *string_val = NULL;
   57|       |
   58|    735|    if (name[0] == '$') {
  ------------------
  |  Branch (58:9): [True: 106, False: 629]
  ------------------
   59|    106|        is_string = 1;
   60|    629|    } else if (name[0] == '@') {
  ------------------
  |  Branch (60:16): [True: 29, False: 600]
  ------------------
   61|     29|        goto cleanup; // Skip informats
   62|     29|    }
   63|       |
   64|    706|    value_offset = readstat_calloc(label_count_used, sizeof(uint32_t));
   65|    706|    if (value_offset == NULL) {
  ------------------
  |  Branch (65:9): [True: 158, False: 548]
  ------------------
   66|    158|        retval = READSTAT_ERROR_MALLOC;
   67|    158|        goto cleanup;
   68|    158|    }
   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|   515M|    for (i=0; i<label_count_used; i++) {
  ------------------
  |  Branch (71:15): [True: 515M, False: 548]
  ------------------
   72|   515M|        value_offset[i] = UINT32_MAX;
   73|   515M|    }
   74|       |
   75|       |    /* Pass 1 -- find out the offset of the labels */
   76|  3.36M|    for (i=0; i<label_count_capacity; i++) {
  ------------------
  |  Branch (76:15): [True: 3.36M, False: 413]
  ------------------
   77|  3.36M|        if (&lbp1[4] - value_start > value_labels_len || sas_read2(&lbp1[2], ctx->bswap) < 0) {
  ------------------
  |  Branch (77:13): [True: 54, False: 3.36M]
  |  Branch (77:58): [True: 0, False: 3.36M]
  ------------------
   78|     54|            retval = READSTAT_ERROR_PARSE;
   79|     54|            goto cleanup;
   80|     54|        }
   81|  3.36M|        if (i<label_count_used) {
  ------------------
  |  Branch (81:13): [True: 85.7k, False: 3.28M]
  ------------------
   82|  85.7k|            if (&lbp1[10+ctx->pad1+4] - value_start > value_labels_len) {
  ------------------
  |  Branch (82:17): [True: 7, False: 85.7k]
  ------------------
   83|      7|                retval = READSTAT_ERROR_PARSE;
   84|      7|                goto cleanup;
   85|      7|            }
   86|  85.7k|            uint32_t label_pos = sas_read4(&lbp1[10+ctx->pad1], ctx->bswap);
   87|  85.7k|            if (label_pos >= label_count_used || (default_label_pos > 0 && label_pos == default_label_pos - 1)) {
  ------------------
  |  Branch (87:17): [True: 73, False: 85.6k]
  |  Branch (87:51): [True: 7.07k, False: 78.5k]
  |  Branch (87:76): [True: 1, False: 7.07k]
  ------------------
   88|     74|                retval = READSTAT_ERROR_PARSE;
   89|     74|                goto cleanup;
   90|     74|            }
   91|  85.6k|            value_offset[label_pos] = lbp1 - value_start;
   92|  85.6k|        }
   93|  3.36M|        lbp1 += 6 + sas_read2(&lbp1[2], ctx->bswap);
   94|  3.36M|    }
   95|       |
   96|    413|    const char *lbp2 = lbp1;
   97|       |
   98|       |    /* Pass 2 -- parse pairs of values & labels */
   99|  1.19k|    for (i=0; i<label_count_used; i++) {
  ------------------
  |  Branch (99:15): [True: 1.08k, False: 116]
  ------------------
  100|  1.08k|        readstat_value_t value = { 0 };
  101|  1.08k|        int is_default_label = default_label_pos > 0 && i == default_label_pos - 1;
  ------------------
  |  Branch (101:32): [True: 450, False: 631]
  |  Branch (101:57): [True: 40, False: 410]
  ------------------
  102|  1.08k|        if (!is_default_label) {
  ------------------
  |  Branch (102:13): [True: 1.04k, False: 40]
  ------------------
  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.04k|            if (value_offset[i] == UINT32_MAX) {
  ------------------
  |  Branch (105:17): [True: 188, False: 853]
  ------------------
  106|    188|                retval = READSTAT_ERROR_PARSE;
  107|    188|                goto cleanup;
  108|    188|            }
  109|    853|            lbp1 = value_start + value_offset[i];
  110|       |
  111|    853|            if (&lbp1[30] - value_start > value_labels_len) {
  ------------------
  |  Branch (111:17): [True: 6, False: 847]
  ------------------
  112|      6|                retval = READSTAT_ERROR_PARSE;
  113|      6|                goto cleanup;
  114|      6|            }
  115|    847|            value.type = is_string ? READSTAT_TYPE_STRING : READSTAT_TYPE_DOUBLE;
  ------------------
  |  Branch (115:26): [True: 139, False: 708]
  ------------------
  116|    847|            if (is_string) {
  ------------------
  |  Branch (116:17): [True: 139, False: 708]
  ------------------
  117|    139|                size_t value_entry_len = 6 + sas_read2(&lbp1[2], ctx->bswap);
  118|    139|                size_t string_start = 22 + string_offset;
  119|    139|                if (string_start > value_entry_len ||
  ------------------
  |  Branch (119:21): [True: 10, False: 129]
  ------------------
  120|    129|                        &lbp1[value_entry_len] - value_start > value_labels_len) {
  ------------------
  |  Branch (120:25): [True: 25, False: 104]
  ------------------
  121|     35|                    retval = READSTAT_ERROR_PARSE;
  122|     35|                    goto cleanup;
  123|     35|                }
  124|    104|                size_t string_len = value_entry_len - string_start;
  125|    104|                char *new_string_val = realloc(string_val, 4 * string_len + 1);
  126|    104|                if (new_string_val == NULL) {
  ------------------
  |  Branch (126:21): [True: 0, False: 104]
  ------------------
  127|      0|                    retval = READSTAT_ERROR_MALLOC;
  128|      0|                    goto cleanup;
  129|      0|                }
  130|    104|                string_val = new_string_val;
  131|    104|                retval = readstat_convert(string_val, 4 * string_len + 1,
  132|    104|                        &lbp1[string_start], string_len, ctx->converter);
  133|    104|                if (retval != READSTAT_OK)
  ------------------
  |  Branch (133:21): [True: 2, False: 102]
  ------------------
  134|      2|                    goto cleanup;
  135|       |
  136|    102|                value.v.string_value = string_val;
  137|    708|            } else {
  138|    708|                uint64_t val = sas_read8(&lbp1[22], bswap_doubles);
  139|    708|                double dval = NAN;
  140|    708|                if ((val | 0xFF0000000000) == 0xFFFFFFFFFFFF) {
  ------------------
  |  Branch (140:21): [True: 134, False: 574]
  ------------------
  141|    134|                    sas_assign_tag(&value, (val >> 40));
  142|    574|                } else {
  143|    574|                    memcpy(&dval, &val, 8);
  144|    574|                    if (dval > 0.0) {
  ------------------
  |  Branch (144:25): [True: 272, False: 302]
  ------------------
  145|    272|                        val = ~val;
  146|    272|                        memcpy(&dval, &val, 8);
  147|    302|                    } else {
  148|    302|                        dval *= -1.0;
  149|    302|                    }
  150|    574|                }
  151|       |
  152|    708|                value.v.double_value = dval;
  153|    708|            }
  154|    847|        }
  155|    850|        if (&lbp2[10] > value_start + value_labels_len) {
  ------------------
  |  Branch (155:13): [True: 47, False: 803]
  ------------------
  156|     47|            retval = READSTAT_ERROR_PARSE;
  157|     47|            goto cleanup;
  158|     47|        }
  159|    803|        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|    803|        if (label_len > value_start + value_labels_len - &lbp2[10]) {
  ------------------
  |  Branch (162:13): [True: 114, False: 689]
  ------------------
  163|    114|            label_len = value_start + value_labels_len - &lbp2[10];
  164|    114|        }
  165|    803|        if (ctx->value_label_handler && !is_default_label) {
  ------------------
  |  Branch (165:13): [True: 803, False: 0]
  |  Branch (165:41): [True: 766, False: 37]
  ------------------
  166|    766|            label = realloc(label, 4 * label_len + 1);
  167|    766|            retval = readstat_convert(label, 4 * label_len + 1,
  168|    766|                    &lbp2[10], label_len, ctx->converter);
  169|    766|            if (retval != READSTAT_OK)
  ------------------
  |  Branch (169:17): [True: 19, False: 747]
  ------------------
  170|     19|                goto cleanup;
  171|       |
  172|    747|            if (ctx->value_label_handler(name, value, label, ctx->user_ctx) != READSTAT_HANDLER_OK) {
  ------------------
  |  |   22|    747|#define READSTAT_HANDLER_OK             0
  ------------------
  |  Branch (172:17): [True: 0, False: 747]
  ------------------
  173|      0|                retval = READSTAT_ERROR_USER_ABORT;
  174|      0|                goto cleanup;
  175|      0|            }
  176|    747|        }
  177|       |
  178|    784|        lbp2 += 8 + 2 + label_len + 1;
  179|    784|    }
  180|       |
  181|    735|cleanup:
  182|    735|    free(label);
  183|    735|    free(string_val);
  184|    735|    free(value_offset);
  185|    735|    return retval;
  186|    413|}
readstat_sas7bcat_read.c:sas7bcat_ctx_free:
   36|  1.86k|static void sas7bcat_ctx_free(sas7bcat_ctx_t *ctx) {
   37|  1.86k|    if (ctx->converter)
  ------------------
  |  Branch (37:9): [True: 931, False: 932]
  ------------------
   38|    931|        iconv_close(ctx->converter);
   39|  1.86k|    if (ctx->block_pointers)
  ------------------
  |  Branch (39:9): [True: 1.86k, False: 0]
  ------------------
   40|  1.86k|        free(ctx->block_pointers);
   41|       |
   42|  1.86k|    free(ctx);
   43|  1.86k|}

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

