fuzzer_parser_init:
   33|  3.66k|readstat_parser_t *fuzzer_parser_init(const uint8_t *Data, size_t Size) {
   34|  3.66k|    readstat_parser_t *parser = readstat_parser_init();
   35|  3.66k|    readstat_set_open_handler(parser, rt_open_handler);
   36|  3.66k|    readstat_set_close_handler(parser, rt_close_handler);
   37|  3.66k|    readstat_set_seek_handler(parser, rt_seek_handler);
   38|  3.66k|    readstat_set_read_handler(parser, rt_read_handler);
   39|  3.66k|    readstat_set_update_handler(parser, rt_update_handler);
   40|       |
   41|  3.66k|    readstat_set_metadata_handler(parser, &handle_metadata);
   42|  3.66k|    readstat_set_note_handler(parser, &handle_note);
   43|  3.66k|    readstat_set_variable_handler(parser, &handle_variable);
   44|  3.66k|    readstat_set_fweight_handler(parser, &handle_fweight);
   45|  3.66k|    readstat_set_value_handler(parser, &handle_value);
   46|  3.66k|    readstat_set_value_label_handler(parser, &handle_value_label);
   47|       |
   48|  3.66k|    return parser;
   49|  3.66k|}
fuzz_format.c:handle_metadata:
    8|  1.45k|static int handle_metadata(readstat_metadata_t *metadata, void *ctx) {
    9|  1.45k|    return READSTAT_HANDLER_OK;
   10|  1.45k|}
fuzz_format.c:handle_variable:
   21|  1.86M|                           const char *val_labels, void *ctx) {
   22|  1.86M|    return READSTAT_HANDLER_OK;
   23|  1.86M|}
fuzz_format.c:handle_value:
   25|  7.96M|static int handle_value(int obs_index, readstat_variable_t *variable, readstat_value_t value, void *ctx) {
   26|  7.96M|    return READSTAT_HANDLER_OK;
   27|  7.96M|}

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

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

readstat_convert:
    7|  7.83M|readstat_error_t readstat_convert(char *dst, size_t dst_len, const char *src, size_t src_len, iconv_t converter) {
    8|       |    /* strip off spaces from the input because the programs use ASCII space
    9|       |     * padding even with non-ASCII encoding. */
   10|  7.88M|    while (src_len && (src[src_len-1] == ' ' || src[src_len-1] == '\0')) {
  ------------------
  |  Branch (10:12): [True: 66.4k, False: 7.81M]
  |  Branch (10:24): [True: 3.88k, False: 62.6k]
  |  Branch (10:49): [True: 41.8k, False: 20.7k]
  ------------------
   11|  45.7k|        src_len--;
   12|  45.7k|    }
   13|  7.83M|    if (dst_len == 0) {
  ------------------
  |  Branch (13:9): [True: 0, False: 7.83M]
  ------------------
   14|      0|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   15|  7.83M|    } else if (converter) {
  ------------------
  |  Branch (15:16): [True: 1.41M, False: 6.41M]
  ------------------
   16|  1.41M|        size_t dst_left = dst_len - 1;
   17|  1.41M|        char *dst_end = dst;
   18|  1.41M|        size_t status = iconv(converter, (readstat_iconv_inbuf_t)&src, &src_len, &dst_end, &dst_left);
   19|  1.41M|        if (status == (size_t)-1) {
  ------------------
  |  Branch (19:13): [True: 769, False: 1.41M]
  ------------------
   20|    769|            if (errno == E2BIG) {
  ------------------
  |  Branch (20:17): [True: 4, False: 765]
  ------------------
   21|      4|                return READSTAT_ERROR_CONVERT_LONG_STRING;
   22|    765|            } else if (errno == EILSEQ) {
  ------------------
  |  Branch (22:24): [True: 36, False: 729]
  ------------------
   23|     36|                return READSTAT_ERROR_CONVERT_BAD_STRING;
   24|    729|            } else if (errno != EINVAL) { /* EINVAL indicates improper truncation; accept it */
  ------------------
  |  Branch (24:24): [True: 0, False: 729]
  ------------------
   25|      0|                return READSTAT_ERROR_CONVERT;
   26|      0|            }
   27|    769|        }
   28|  1.41M|        dst[dst_len - dst_left - 1] = '\0';
   29|  6.41M|    } else if (src_len + 1 > dst_len) {
  ------------------
  |  Branch (29:16): [True: 26, False: 6.41M]
  ------------------
   30|     26|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   31|  6.41M|    } else {
   32|  6.41M|        memcpy(dst, src, src_len);
   33|  6.41M|        dst[src_len] = '\0';
   34|  6.41M|    }
   35|  7.83M|    return READSTAT_OK;
   36|  7.83M|}

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

readstat_malloc:
   10|  5.51k|void *readstat_malloc(size_t len) {
   11|  5.51k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|  11.0k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (11:9): [True: 0, False: 5.51k]
  |  Branch (11:34): [True: 0, False: 5.51k]
  ------------------
   12|      0|        return NULL;
   13|      0|    }
   14|  5.51k|    return malloc(len);
   15|  5.51k|}
readstat_calloc:
   17|  1.86M|void *readstat_calloc(size_t count, size_t size) {
   18|  1.86M|    if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  3.72M|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  3.72M|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  1.86M|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (18:9): [True: 0, False: 1.86M]
  |  Branch (18:36): [True: 0, False: 1.86M]
  |  Branch (18:62): [True: 0, False: 1.86M]
  ------------------
   19|      0|        return NULL;
   20|      0|    }
   21|  1.86M|    if (count == 0 || size == 0) {
  ------------------
  |  Branch (21:9): [True: 0, False: 1.86M]
  |  Branch (21:23): [True: 0, False: 1.86M]
  ------------------
   22|      0|        return NULL;
   23|      0|    }
   24|  1.86M|    return calloc(count, size);
   25|  1.86M|}
readstat_realloc:
   27|   265k|void *readstat_realloc(void *ptr, size_t len) {
   28|   265k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|   531k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (28:9): [True: 91, False: 265k]
  |  Branch (28:34): [True: 1, False: 265k]
  ------------------
   29|     92|        if (ptr)
  ------------------
  |  Branch (29:13): [True: 42, False: 50]
  ------------------
   30|     42|            free(ptr);
   31|     92|        return NULL;
   32|     92|    }
   33|   265k|    return realloc(ptr, len);
   34|   265k|}

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

readstat_variable_set_informat:
  441|    556|void readstat_variable_set_informat(readstat_variable_t *variable, const char *informat) {
  442|    556|    if (informat) {
  ------------------
  |  Branch (442:9): [True: 556, False: 0]
  ------------------
  443|    556|        snprintf(variable->informat, sizeof(variable->informat), "%s", informat);
  444|    556|    } else {
  445|      0|        memset(variable->informat, '\0', sizeof(variable->informat));
  446|      0|    }
  447|    556|}

sas_read8:
  139|  60.8k|uint64_t sas_read8(const char *data, int bswap) {
  140|  60.8k|    uint64_t tmp;
  141|  60.8k|    memcpy(&tmp, data, 8);
  142|  60.8k|    return bswap ? byteswap8(tmp) : tmp;
  ------------------
  |  Branch (142:12): [True: 56.1k, False: 4.75k]
  ------------------
  143|  60.8k|}
sas_read4:
  145|   217k|uint32_t sas_read4(const char *data, int bswap) {
  146|   217k|    uint32_t tmp;
  147|   217k|    memcpy(&tmp, data, 4);
  148|   217k|    return bswap ? byteswap4(tmp) : tmp;
  ------------------
  |  Branch (148:12): [True: 155k, False: 62.5k]
  ------------------
  149|   217k|}
sas_read2:
  151|   162k|uint16_t sas_read2(const char *data, int bswap) {
  152|   162k|    uint16_t tmp;
  153|   162k|    memcpy(&tmp, data, 2);
  154|   162k|    return bswap ? byteswap2(tmp) : tmp;
  ------------------
  |  Branch (154:12): [True: 77.6k, False: 85.3k]
  ------------------
  155|   162k|}
sas_subheader_remainder:
  157|  7.10k|size_t sas_subheader_remainder(size_t len, size_t signature_len) {
  158|  7.10k|    return len - (4+2*signature_len);
  159|  7.10k|}
sas_read_header:
  162|  3.66k|        readstat_error_handler error_handler, void *user_ctx) {
  163|  3.66k|    sas_header_start_t  header_start;
  164|  3.66k|    sas_header_end_t    header_end;
  165|  3.66k|    int retval = READSTAT_OK;
  166|  3.66k|    char error_buf[1024];
  167|  3.66k|    time_t epoch = sas_epoch();
  168|       |
  169|  3.66k|    if (io->read(&header_start, sizeof(sas_header_start_t), io->io_ctx) < sizeof(sas_header_start_t)) {
  ------------------
  |  Branch (169:9): [True: 19, False: 3.64k]
  ------------------
  170|     19|        retval = READSTAT_ERROR_READ;
  171|     19|        goto cleanup;
  172|     19|    }
  173|  3.64k|    if (memcmp(header_start.magic, sas7bdat_magic_number, sizeof(sas7bdat_magic_number)) != 0 &&
  ------------------
  |  Branch (173:9): [True: 1.63k, False: 2.00k]
  ------------------
  174|  1.63k|            memcmp(header_start.magic, sas7bcat_magic_number, sizeof(sas7bcat_magic_number)) != 0) {
  ------------------
  |  Branch (174:13): [True: 130, False: 1.50k]
  ------------------
  175|    130|        retval = READSTAT_ERROR_PARSE;
  176|    130|        goto cleanup;
  177|    130|    }
  178|  3.51k|    if (header_start.a1 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   87|  3.51k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (178:9): [True: 6, False: 3.50k]
  ------------------
  179|      6|        hinfo->pad1 = 4;
  180|      6|    }
  181|  3.51k|    if (header_start.a2 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   87|  3.51k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (181:9): [True: 1.69k, False: 1.82k]
  ------------------
  182|  1.69k|        hinfo->u64 = 1;
  183|  1.69k|    }
  184|  3.51k|    int bswap = 0;
  185|  3.51k|    if (header_start.endian == SAS_ENDIAN_BIG) {
  ------------------
  |  |   80|  3.51k|#define SAS_ENDIAN_BIG       0x00
  ------------------
  |  Branch (185:9): [True: 2.69k, False: 822]
  ------------------
  186|  2.69k|        bswap = machine_is_little_endian();
  187|  2.69k|        hinfo->little_endian = 0;
  188|  2.69k|    } else if (header_start.endian == SAS_ENDIAN_LITTLE) {
  ------------------
  |  |   81|    822|#define SAS_ENDIAN_LITTLE    0x01
  ------------------
  |  Branch (188:16): [True: 809, False: 13]
  ------------------
  189|    809|        bswap = !machine_is_little_endian();
  190|    809|        hinfo->little_endian = 1;
  191|    809|    } else {
  192|     13|        retval = READSTAT_ERROR_PARSE;
  193|     13|        goto cleanup;
  194|     13|    }
  195|  3.50k|    int i;
  196|  19.9k|    for (i=0; i<sizeof(_charset_table)/sizeof(_charset_table[0]); i++) {
  ------------------
  |  Branch (196:15): [True: 19.8k, False: 3]
  ------------------
  197|  19.8k|        if (header_start.encoding == _charset_table[i].code) {
  ------------------
  |  Branch (197:13): [True: 3.49k, False: 16.4k]
  ------------------
  198|  3.49k|            hinfo->encoding = _charset_table[i].name;
  199|  3.49k|            break;
  200|  3.49k|        }
  201|  19.8k|    }
  202|  3.50k|    if (hinfo->encoding == NULL) {
  ------------------
  |  Branch (202:9): [True: 3, False: 3.49k]
  ------------------
  203|      3|        if (error_handler) {
  ------------------
  |  Branch (203:13): [True: 0, False: 3]
  ------------------
  204|      0|            snprintf(error_buf, sizeof(error_buf), "Unsupported character set code: %d", header_start.encoding);
  205|      0|            error_handler(error_buf, user_ctx);
  206|      0|        }
  207|      3|        retval = READSTAT_ERROR_UNSUPPORTED_CHARSET;
  208|      3|        goto cleanup;
  209|      3|    }
  210|  3.49k|    memcpy(hinfo->table_name, header_start.table_name, sizeof(header_start.table_name));
  211|  3.49k|    if (io->seek(hinfo->pad1, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (211:9): [True: 5, False: 3.49k]
  ------------------
  212|      5|        retval = READSTAT_ERROR_SEEK;
  213|      5|        goto cleanup;
  214|      5|    }
  215|       |
  216|  3.49k|    double creation_time, modification_time, creation_time_diff, modification_time_diff;
  217|       |
  218|  3.49k|    if (io->read(&creation_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (218:9): [True: 10, False: 3.48k]
  ------------------
  219|     10|        retval = READSTAT_ERROR_READ;
  220|     10|        goto cleanup;
  221|     10|    }
  222|  3.48k|    if (bswap)
  ------------------
  |  Branch (222:9): [True: 2.67k, False: 806]
  ------------------
  223|  2.67k|        creation_time = byteswap_double(creation_time);
  224|       |
  225|  3.48k|    if (io->read(&modification_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (225:9): [True: 6, False: 3.47k]
  ------------------
  226|      6|        retval = READSTAT_ERROR_READ;
  227|      6|        goto cleanup;
  228|      6|    }
  229|  3.47k|    if (bswap)
  ------------------
  |  Branch (229:9): [True: 2.67k, False: 803]
  ------------------
  230|  2.67k|        modification_time = byteswap_double(modification_time);
  231|       |
  232|  3.47k|    if (io->read(&creation_time_diff, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (232:9): [True: 6, False: 3.47k]
  ------------------
  233|      6|        retval = READSTAT_ERROR_READ;
  234|      6|        goto cleanup;
  235|      6|    }
  236|  3.47k|    if (bswap)
  ------------------
  |  Branch (236:9): [True: 2.66k, False: 802]
  ------------------
  237|  2.66k|        creation_time_diff = byteswap_double(creation_time_diff);
  238|       |    
  239|  3.47k|    if (io->read(&modification_time_diff, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (239:9): [True: 6, False: 3.46k]
  ------------------
  240|      6|        retval = READSTAT_ERROR_READ;
  241|      6|        goto cleanup;
  242|      6|    }
  243|  3.46k|    if (bswap)
  ------------------
  |  Branch (243:9): [True: 2.66k, False: 801]
  ------------------
  244|  2.66k|        modification_time_diff = byteswap_double(modification_time_diff);
  245|       |    
  246|  3.46k|    hinfo->creation_time = sas_convert_time(creation_time, creation_time_diff, epoch);
  247|  3.46k|    hinfo->modification_time = sas_convert_time(modification_time, modification_time_diff, epoch);
  248|       |
  249|  3.46k|    uint32_t header_size, page_size;
  250|       |
  251|  3.46k|    if (io->read(&header_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (251:9): [True: 8, False: 3.45k]
  ------------------
  252|      8|        retval = READSTAT_ERROR_READ;
  253|      8|        goto cleanup;
  254|      8|    }
  255|  3.45k|    if (io->read(&page_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (255:9): [True: 3, False: 3.45k]
  ------------------
  256|      3|        retval = READSTAT_ERROR_READ;
  257|      3|        goto cleanup;
  258|      3|    }
  259|       |
  260|  3.45k|    hinfo->header_size = bswap ? byteswap4(header_size) : header_size;
  ------------------
  |  Branch (260:26): [True: 2.65k, False: 797]
  ------------------
  261|  3.45k|    hinfo->page_size = bswap ? byteswap4(page_size) : page_size;
  ------------------
  |  Branch (261:24): [True: 2.65k, False: 797]
  ------------------
  262|       |
  263|  3.45k|    if (hinfo->header_size < 1024 || hinfo->page_size < 1024) {
  ------------------
  |  Branch (263:9): [True: 11, False: 3.44k]
  |  Branch (263:38): [True: 12, False: 3.43k]
  ------------------
  264|     23|        retval = READSTAT_ERROR_PARSE;
  265|     23|        goto cleanup;
  266|     23|    }
  267|  3.43k|    if (hinfo->header_size > (1<<24) || hinfo->page_size > (1<<24)) {
  ------------------
  |  Branch (267:9): [True: 21, False: 3.41k]
  |  Branch (267:41): [True: 22, False: 3.38k]
  ------------------
  268|     43|        retval = READSTAT_ERROR_PARSE;
  269|     43|        goto cleanup;
  270|     43|    }
  271|       |
  272|  3.38k|    if (hinfo->u64) {
  ------------------
  |  Branch (272:9): [True: 1.65k, False: 1.73k]
  ------------------
  273|  1.65k|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_64BIT;
  ------------------
  |  |  122|  1.65k|#define SAS_PAGE_HEADER_SIZE_64BIT  40
  ------------------
  274|  1.65k|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_64BIT;
  ------------------
  |  |  119|  1.65k|#define SAS_SUBHEADER_POINTER_SIZE_64BIT    24
  ------------------
  275|  1.73k|    } else {
  276|  1.73k|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_32BIT;
  ------------------
  |  |  121|  1.73k|#define SAS_PAGE_HEADER_SIZE_32BIT  24
  ------------------
  277|  1.73k|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_32BIT;
  ------------------
  |  |  118|  1.73k|#define SAS_SUBHEADER_POINTER_SIZE_32BIT    12
  ------------------
  278|  1.73k|    }
  279|       |
  280|  3.38k|    if (hinfo->u64) {
  ------------------
  |  Branch (280:9): [True: 1.65k, False: 1.73k]
  ------------------
  281|  1.65k|        uint64_t page_count;
  282|  1.65k|        if (io->read(&page_count, sizeof(uint64_t), io->io_ctx) < sizeof(uint64_t)) {
  ------------------
  |  Branch (282:13): [True: 17, False: 1.63k]
  ------------------
  283|     17|            retval = READSTAT_ERROR_READ;
  284|     17|            goto cleanup;
  285|     17|        }
  286|  1.63k|        hinfo->page_count = bswap ? byteswap8(page_count) : page_count;
  ------------------
  |  Branch (286:29): [True: 1.52k, False: 113]
  ------------------
  287|  1.73k|    } else {
  288|  1.73k|        uint32_t page_count;
  289|  1.73k|        if (io->read(&page_count, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (289:13): [True: 17, False: 1.71k]
  ------------------
  290|     17|            retval = READSTAT_ERROR_READ;
  291|     17|            goto cleanup;
  292|     17|        }
  293|  1.71k|        hinfo->page_count = bswap ? byteswap4(page_count) : page_count;
  ------------------
  |  Branch (293:29): [True: 1.04k, False: 672]
  ------------------
  294|  1.71k|    }
  295|  3.35k|    if (hinfo->page_count > (1<<24)) {
  ------------------
  |  Branch (295:9): [True: 15, False: 3.33k]
  ------------------
  296|     15|        retval = READSTAT_ERROR_PARSE;
  297|     15|        goto cleanup;
  298|     15|    }
  299|       |    
  300|  3.33k|    if (io->seek(8, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (300:9): [True: 72, False: 3.26k]
  ------------------
  301|     72|        retval = READSTAT_ERROR_SEEK;
  302|     72|        if (error_handler) {
  ------------------
  |  Branch (302:13): [True: 0, False: 72]
  ------------------
  303|      0|            snprintf(error_buf, sizeof(error_buf), "ReadStat: Failed to seek forward by %d", 8);
  304|      0|            error_handler(error_buf, user_ctx);
  305|      0|        }
  306|     72|        goto cleanup;
  307|     72|    }
  308|  3.26k|    if (io->read(&header_end, sizeof(sas_header_end_t), io->io_ctx) < sizeof(sas_header_end_t)) {
  ------------------
  |  Branch (308:9): [True: 18, False: 3.24k]
  ------------------
  309|     18|        retval = READSTAT_ERROR_READ;
  310|     18|        goto cleanup;
  311|     18|    }
  312|  3.24k|    char major, revision_tag;
  313|  3.24k|    int minor, revision;
  314|  3.24k|    if (sscanf(header_end.release, "%c.%04d%c%1d", &major, &minor, &revision_tag, &revision) != 4) {
  ------------------
  |  Branch (314:9): [True: 5, False: 3.24k]
  ------------------
  315|      5|        retval = READSTAT_ERROR_PARSE;
  316|      5|        goto cleanup;
  317|      5|    }
  318|       |
  319|  3.24k|    if (major >= '1' && major <= '9') {
  ------------------
  |  Branch (319:9): [True: 3.23k, False: 13]
  |  Branch (319:25): [True: 3.22k, False: 7]
  ------------------
  320|  3.22k|        hinfo->major_version = major - '0';
  321|  3.22k|    } else if (major == 'V') {
  ------------------
  |  Branch (321:16): [True: 1, False: 19]
  ------------------
  322|       |        // It appears that SAS Visual Forecaster reports the major version as "V"
  323|       |        // Treat it as version 9 for all intents and purposes
  324|      1|        hinfo->major_version = 9;
  325|     19|    } else {
  326|     19|        retval = READSTAT_ERROR_PARSE;
  327|     19|        goto cleanup;
  328|     19|    }
  329|       |    // revision_tag is usually M, but J has been observed in the wild (not created with SAS?)
  330|  3.22k|    if (revision_tag != 'M' && revision_tag != 'J') {
  ------------------
  |  Branch (330:9): [True: 25, False: 3.20k]
  |  Branch (330:32): [True: 13, False: 12]
  ------------------
  331|     13|        retval = READSTAT_ERROR_PARSE;
  332|     13|        goto cleanup;
  333|     13|    }
  334|  3.21k|    hinfo->minor_version = minor;
  335|  3.21k|    hinfo->revision = revision;
  336|       |
  337|  3.21k|    if ((major == '8' || major == '9') && minor == 0 && revision == 0) {
  ------------------
  |  Branch (337:10): [True: 306, False: 2.90k]
  |  Branch (337:26): [True: 193, False: 2.71k]
  |  Branch (337:43): [True: 86, False: 413]
  |  Branch (337:57): [True: 69, False: 17]
  ------------------
  338|       |        /* A bit of a hack, but most SAS installations are running a minor update */
  339|     69|        hinfo->vendor = READSTAT_VENDOR_STAT_TRANSFER;
  340|  3.14k|    } else {
  341|  3.14k|        hinfo->vendor = READSTAT_VENDOR_SAS;
  342|  3.14k|    }
  343|  3.21k|    if (io->seek(hinfo->header_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (343:9): [True: 50, False: 3.16k]
  ------------------
  344|     50|        retval = READSTAT_ERROR_SEEK;
  345|     50|        if (error_handler) {
  ------------------
  |  Branch (345:13): [True: 0, False: 50]
  ------------------
  346|      0|            snprintf(error_buf, sizeof(error_buf), 
  347|      0|                    "ReadStat: Failed to seek to position %" PRId64, hinfo->header_size);
  348|      0|            error_handler(error_buf, user_ctx);
  349|      0|        }
  350|     50|        goto cleanup;
  351|     50|    }
  352|       |
  353|  3.66k|cleanup:
  354|  3.66k|    return retval;
  355|  3.21k|}
sas_validate_tag:
  507|  8.35k|readstat_error_t sas_validate_tag(char tag) {
  508|  8.35k|    if (tag == '_' || (tag >= 'A' && tag <= 'Z'))
  ------------------
  |  Branch (508:9): [True: 4.01k, False: 4.33k]
  |  Branch (508:24): [True: 1.75k, False: 2.58k]
  |  Branch (508:38): [True: 1.12k, False: 632]
  ------------------
  509|  5.13k|        return READSTAT_OK;
  510|       |
  511|  3.21k|    return READSTAT_ERROR_TAGGED_VALUE_IS_OUT_OF_RANGE;
  512|  8.35k|}
sas_assign_tag:
  514|  8.35k|void sas_assign_tag(readstat_value_t *value, uint8_t tag) {
  515|       |    /* We accommodate two tag schemes. In the first, the tag is an ASCII code
  516|       |     * given by uint8_t tag above. System missing is represented by an ASCII
  517|       |     * period. In the second scheme, (tag-2) is an offset from 'A', except when
  518|       |     * tag == 0, in which case it represents an underscore, or tag == 1, in
  519|       |     * which case it represents system-missing.
  520|       |     */
  521|  8.35k|    if (tag == 0) {
  ------------------
  |  Branch (521:9): [True: 4.01k, False: 4.33k]
  ------------------
  522|  4.01k|        tag = '_';
  523|  4.33k|    } else if (tag >= 2 && tag < 28) {
  ------------------
  |  Branch (523:16): [True: 3.82k, False: 509]
  |  Branch (523:28): [True: 1.06k, False: 2.76k]
  ------------------
  524|  1.06k|        tag = 'A' + (tag - 2);
  525|  1.06k|    }
  526|  8.35k|    if (sas_validate_tag(tag) == READSTAT_OK) {
  ------------------
  |  Branch (526:9): [True: 5.13k, False: 3.21k]
  ------------------
  527|  5.13k|        value->tag = tag;
  528|  5.13k|        value->is_tagged_missing = 1;
  529|  5.13k|    } else {
  530|  3.21k|        value->tag = 0;
  531|  3.21k|        value->is_system_missing = 1;
  532|  3.21k|    }
  533|  8.35k|}
readstat_sas.c:sas_epoch:
  123|  3.66k|static time_t sas_epoch(void) {
  124|  3.66k|    return - 3653 * 86400; // seconds between 01-01-1960 and 01-01-1970
  125|  3.66k|}
readstat_sas.c:sas_convert_time:
  127|  6.93k|static time_t sas_convert_time(double time, double time_diff, time_t epoch) {
  128|  6.93k|    time -= time_diff;
  129|  6.93k|    time += epoch;
  130|  6.93k|    if (isnan(time))
  ------------------
  |  Branch (130:9): [True: 899, False: 6.03k]
  ------------------
  131|    899|        return 0;
  132|  6.03k|    if (time > (double)LONG_MAX)
  ------------------
  |  Branch (132:9): [True: 1.70k, False: 4.32k]
  ------------------
  133|  1.70k|        return LONG_MAX;
  134|  4.32k|    if (time < (double)LONG_MIN)
  ------------------
  |  Branch (134:9): [True: 1.08k, False: 3.23k]
  ------------------
  135|  1.08k|        return LONG_MIN;
  136|  3.23k|    return time;
  137|  4.32k|}

readstat_parse_sas7bdat:
 1282|  3.66k|readstat_error_t readstat_parse_sas7bdat(readstat_parser_t *parser, const char *path, void *user_ctx) {
 1283|  3.66k|    int64_t last_examined_page_pass1 = 0;
 1284|  3.66k|    readstat_error_t retval = READSTAT_OK;
 1285|  3.66k|    readstat_io_t *io = parser->io;
 1286|       |
 1287|  3.66k|    sas7bdat_ctx_t  *ctx = calloc(1, sizeof(sas7bdat_ctx_t));
 1288|  3.66k|    sas_header_info_t  *hinfo = calloc(1, sizeof(sas_header_info_t));
 1289|       |
 1290|  3.66k|    if (ctx == NULL || hinfo == NULL) {
  ------------------
  |  Branch (1290:9): [True: 0, False: 3.66k]
  |  Branch (1290:24): [True: 0, False: 3.66k]
  ------------------
 1291|      0|        retval = READSTAT_ERROR_MALLOC;
 1292|      0|        goto cleanup;
 1293|      0|    }
 1294|       |
 1295|  3.66k|    ctx->handle = parser->handlers;
 1296|  3.66k|    ctx->input_encoding = parser->input_encoding;
 1297|  3.66k|    ctx->output_encoding = parser->output_encoding;
 1298|  3.66k|    ctx->user_ctx = user_ctx;
 1299|  3.66k|    ctx->io = parser->io;
 1300|  3.66k|    ctx->row_limit = parser->row_limit;
 1301|  3.66k|    if (parser->row_offset > 0)
  ------------------
  |  Branch (1301:9): [True: 0, False: 3.66k]
  ------------------
 1302|      0|        ctx->row_offset = parser->row_offset;
 1303|       |
 1304|  3.66k|    if (io->open(path, io->io_ctx) == -1) {
  ------------------
  |  Branch (1304:9): [True: 0, False: 3.66k]
  ------------------
 1305|      0|        retval = READSTAT_ERROR_OPEN;
 1306|      0|        goto cleanup;
 1307|      0|    }
 1308|       |
 1309|  3.66k|    if ((ctx->file_size = io->seek(0, READSTAT_SEEK_END, io->io_ctx)) == -1) {
  ------------------
  |  Branch (1309:9): [True: 0, False: 3.66k]
  ------------------
 1310|      0|        retval = READSTAT_ERROR_SEEK;
 1311|      0|        if (ctx->handle.error) {
  ------------------
  |  Branch (1311:13): [True: 0, False: 0]
  ------------------
 1312|      0|            snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: Failed to seek to end of file");
 1313|      0|            ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1314|      0|        }
 1315|      0|        goto cleanup;
 1316|      0|    }
 1317|       |
 1318|  3.66k|    if (io->seek(0, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (1318:9): [True: 0, False: 3.66k]
  ------------------
 1319|      0|        retval = READSTAT_ERROR_SEEK;
 1320|      0|        if (ctx->handle.error) {
  ------------------
  |  Branch (1320:13): [True: 0, False: 0]
  ------------------
 1321|      0|            snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: Failed to seek to beginning of file");
 1322|      0|            ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1323|      0|        }
 1324|      0|        goto cleanup;
 1325|      0|    }
 1326|       |
 1327|  3.66k|    if ((retval = sas_read_header(io, hinfo, ctx->handle.error, user_ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1327:9): [True: 501, False: 3.16k]
  ------------------
 1328|    501|        goto cleanup;
 1329|    501|    }
 1330|       |
 1331|  3.16k|    ctx->u64 = hinfo->u64;
 1332|  3.16k|    ctx->little_endian = hinfo->little_endian;
 1333|  3.16k|    ctx->vendor = hinfo->vendor;
 1334|  3.16k|    ctx->bswap = machine_is_little_endian() ^ hinfo->little_endian;
 1335|  3.16k|    ctx->header_size = hinfo->header_size;
 1336|  3.16k|    ctx->page_count = hinfo->page_count;
 1337|  3.16k|    ctx->page_size = hinfo->page_size;
 1338|  3.16k|    ctx->page_header_size = hinfo->page_header_size;
 1339|  3.16k|    ctx->subheader_pointer_size = hinfo->subheader_pointer_size;
 1340|  3.16k|    ctx->subheader_signature_size = ctx->u64 ? 8 : 4;
  ------------------
  |  Branch (1340:37): [True: 1.57k, False: 1.58k]
  ------------------
 1341|  3.16k|    ctx->ctime = hinfo->creation_time;
 1342|  3.16k|    ctx->mtime = hinfo->modification_time;
 1343|  3.16k|    ctx->version = hinfo->major_version;
 1344|  3.16k|    if (ctx->input_encoding == NULL) {
  ------------------
  |  Branch (1344:9): [True: 3.16k, False: 0]
  ------------------
 1345|  3.16k|        ctx->input_encoding = hinfo->encoding;
 1346|  3.16k|    }
 1347|  3.16k|    if ((ctx->page = readstat_malloc(ctx->page_size)) == NULL) {
  ------------------
  |  Branch (1347:9): [True: 0, False: 3.16k]
  ------------------
 1348|      0|        retval = READSTAT_ERROR_MALLOC;
 1349|      0|        goto cleanup;
 1350|      0|    }
 1351|       |
 1352|  3.16k|    if (ctx->input_encoding && ctx->output_encoding && strcmp(ctx->input_encoding, ctx->output_encoding) != 0) {
  ------------------
  |  Branch (1352:9): [True: 3.16k, False: 0]
  |  Branch (1352:32): [True: 3.16k, False: 0]
  |  Branch (1352:56): [True: 2.27k, False: 886]
  ------------------
 1353|  2.27k|        iconv_t converter = iconv_open(ctx->output_encoding, ctx->input_encoding);
 1354|  2.27k|        if (converter == (iconv_t)-1) {
  ------------------
  |  Branch (1354:13): [True: 1, False: 2.27k]
  ------------------
 1355|      1|            retval = READSTAT_ERROR_UNSUPPORTED_CHARSET;
 1356|      1|            goto cleanup;
 1357|      1|        }
 1358|  2.27k|        ctx->converter = converter;
 1359|  2.27k|    }
 1360|       |
 1361|  3.16k|    if ((retval = readstat_convert(ctx->table_name, sizeof(ctx->table_name),
  ------------------
  |  Branch (1361:9): [True: 1, False: 3.16k]
  ------------------
 1362|  3.16k|                hinfo->table_name, sizeof(hinfo->table_name), ctx->converter)) != READSTAT_OK) {
 1363|      1|        goto cleanup;
 1364|      1|    }
 1365|       |
 1366|  3.16k|    if ((retval = sas7bdat_parse_meta_pages_pass1(ctx, &last_examined_page_pass1)) != READSTAT_OK) {
  ------------------
  |  Branch (1366:9): [True: 252, False: 2.90k]
  ------------------
 1367|    252|        goto cleanup;
 1368|    252|    }
 1369|       |
 1370|  2.90k|    if ((retval = sas7bdat_parse_amd_pages_pass1(last_examined_page_pass1, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1370:9): [True: 885, False: 2.02k]
  ------------------
 1371|    885|        goto cleanup;
 1372|    885|    }
 1373|       |
 1374|  2.02k|    if (io->seek(ctx->header_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (1374:9): [True: 0, False: 2.02k]
  ------------------
 1375|      0|        retval = READSTAT_ERROR_SEEK;
 1376|      0|        if (ctx->handle.error) {
  ------------------
  |  Branch (1376:13): [True: 0, False: 0]
  ------------------
 1377|      0|            snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: Failed to seek to position %" PRId64, 
 1378|      0|                    ctx->header_size);
 1379|      0|            ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1380|      0|        }
 1381|      0|        goto cleanup;
 1382|      0|    }
 1383|       |
 1384|  2.02k|    if ((retval = sas7bdat_parse_all_pages_pass2(ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1384:9): [True: 1.71k, False: 307]
  ------------------
 1385|  1.71k|        goto cleanup;
 1386|  1.71k|    }
 1387|       |    
 1388|    307|    if ((retval = sas7bdat_submit_columns_if_needed(ctx, 0)) != READSTAT_OK) {
  ------------------
  |  Branch (1388:9): [True: 75, False: 232]
  ------------------
 1389|     75|        goto cleanup;
 1390|     75|    }
 1391|       |
 1392|    232|    if (ctx->handle.value && ctx->parsed_row_count != ctx->row_limit) {
  ------------------
  |  Branch (1392:9): [True: 232, False: 0]
  |  Branch (1392:30): [True: 93, False: 139]
  ------------------
 1393|     93|        retval = READSTAT_ERROR_ROW_COUNT_MISMATCH;
 1394|     93|        if (ctx->handle.error) {
  ------------------
  |  Branch (1394:13): [True: 0, False: 93]
  ------------------
 1395|      0|            snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: Expected %d rows in file, found %d",
 1396|      0|                    ctx->row_limit, ctx->parsed_row_count);
 1397|      0|            ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1398|      0|        }
 1399|     93|        goto cleanup;
 1400|     93|    }
 1401|       |
 1402|    139|    if ((retval = sas7bdat_update_progress(ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1402:9): [True: 0, False: 139]
  ------------------
 1403|      0|        goto cleanup;
 1404|      0|    }
 1405|       |
 1406|  3.66k|cleanup:
 1407|  3.66k|    io->close(io->io_ctx);
 1408|       |
 1409|  3.66k|    if (retval == READSTAT_ERROR_OPEN ||
  ------------------
  |  Branch (1409:9): [True: 0, False: 3.66k]
  ------------------
 1410|  3.66k|            retval == READSTAT_ERROR_READ ||
  ------------------
  |  Branch (1410:13): [True: 502, False: 3.16k]
  ------------------
 1411|  3.16k|            retval == READSTAT_ERROR_SEEK) {
  ------------------
  |  Branch (1411:13): [True: 351, False: 2.81k]
  ------------------
 1412|    853|        if (ctx->handle.error) {
  ------------------
  |  Branch (1412:13): [True: 0, False: 853]
  ------------------
 1413|      0|            snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: %s (retval = %d): %s (errno = %d)", 
 1414|      0|                    readstat_error_message(retval), retval, strerror(errno), errno);
 1415|      0|            ctx->handle.error(ctx->error_buf, user_ctx);
 1416|      0|        }
 1417|    853|    }
 1418|       |
 1419|  3.66k|    if (ctx)
  ------------------
  |  Branch (1419:9): [True: 3.66k, False: 0]
  ------------------
 1420|  3.66k|        sas7bdat_ctx_free(ctx);
 1421|  3.66k|    if (hinfo)
  ------------------
  |  Branch (1421:9): [True: 3.66k, False: 0]
  ------------------
 1422|  3.66k|        free(hinfo);
 1423|       |
 1424|  3.66k|    return retval;
 1425|    139|}
readstat_sas7bdat_read.c:sas7bdat_parse_meta_pages_pass1:
 1123|  3.16k|static readstat_error_t sas7bdat_parse_meta_pages_pass1(sas7bdat_ctx_t *ctx, int64_t *outLastExaminedPage) {
 1124|  3.16k|    readstat_error_t retval = READSTAT_OK;
 1125|  3.16k|    readstat_io_t *io = ctx->io;
 1126|  3.16k|    int64_t i;
 1127|       |
 1128|       |    /* look for META and MIX pages at beginning... */
 1129|  8.88k|    for (i=0; i<ctx->page_count; i++) {
  ------------------
  |  Branch (1129:15): [True: 6.78k, False: 2.09k]
  ------------------
 1130|  6.78k|        if (io->seek(ctx->header_size + i*ctx->page_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (1130:13): [True: 11, False: 6.77k]
  ------------------
 1131|     11|            retval = READSTAT_ERROR_SEEK;
 1132|     11|            if (ctx->handle.error) {
  ------------------
  |  Branch (1132:17): [True: 0, False: 11]
  ------------------
 1133|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: Failed to seek to position %" PRId64 
 1134|      0|                        " (= %" PRId64 " + %" PRId64 "*%" PRId64 ")",
 1135|      0|                        ctx->header_size + i*ctx->page_size, ctx->header_size, i, ctx->page_size);
 1136|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1137|      0|            }
 1138|     11|            goto cleanup;
 1139|     11|        }
 1140|       |
 1141|  6.77k|        readstat_off_t off = 0;
 1142|  6.77k|        if (ctx->u64)
  ------------------
  |  Branch (1142:13): [True: 4.35k, False: 2.42k]
  ------------------
 1143|  4.35k|            off = 16;
 1144|       |
 1145|  6.77k|        size_t head_len = off + 16 + 2;
 1146|  6.77k|        size_t tail_len = ctx->page_size - head_len;
 1147|       |
 1148|  6.77k|        if (io->read(ctx->page, head_len, io->io_ctx) < head_len) {
  ------------------
  |  Branch (1148:13): [True: 141, False: 6.63k]
  ------------------
 1149|    141|            retval = READSTAT_ERROR_READ;
 1150|    141|            goto cleanup;
 1151|    141|        }
 1152|       |
 1153|  6.63k|        uint16_t page_type = sas_read2(&ctx->page[off+16], ctx->bswap);
 1154|       |
 1155|  6.63k|        if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_DATA)
  ------------------
  |  |  112|  6.63k|#define SAS_PAGE_TYPE_MASK   0x0F00
  ------------------
                      if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_DATA)
  ------------------
  |  |  109|  6.63k|#define SAS_PAGE_TYPE_DATA   0x0100
  ------------------
  |  Branch (1155:13): [True: 809, False: 5.82k]
  ------------------
 1156|    809|            break;
 1157|  5.82k|        if ((page_type & SAS_PAGE_TYPE_COMP))
  ------------------
  |  |  116|  5.82k|#define SAS_PAGE_TYPE_COMP          0x9000
  ------------------
  |  Branch (1157:13): [True: 1.37k, False: 4.44k]
  ------------------
 1158|  1.37k|            continue;
 1159|       |
 1160|  4.44k|        if (io->read(ctx->page + head_len, tail_len, io->io_ctx) < tail_len) {
  ------------------
  |  Branch (1160:13): [True: 40, False: 4.40k]
  ------------------
 1161|     40|            retval = READSTAT_ERROR_READ;
 1162|     40|            goto cleanup;
 1163|     40|        }
 1164|       |
 1165|  4.40k|        if ((retval = sas7bdat_parse_page_pass1(ctx->page, ctx->page_size, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1165:13): [True: 60, False: 4.34k]
  ------------------
 1166|     60|            if (ctx->handle.error && retval != READSTAT_ERROR_USER_ABORT) {
  ------------------
  |  Branch (1166:17): [True: 0, False: 60]
  |  Branch (1166:38): [True: 0, False: 0]
  ------------------
 1167|      0|                int64_t pos = io->seek(0, READSTAT_SEEK_CUR, io->io_ctx);
 1168|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf), 
 1169|      0|                        "ReadStat: Error parsing page %" PRId64 ", bytes %" PRId64 "-%" PRId64, 
 1170|      0|                        i, pos - ctx->page_size, pos-1);
 1171|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1172|      0|            }
 1173|     60|            goto cleanup;
 1174|     60|        }
 1175|  4.40k|    }
 1176|       |
 1177|  3.16k|cleanup:
 1178|  3.16k|    if (outLastExaminedPage)
  ------------------
  |  Branch (1178:9): [True: 3.16k, False: 0]
  ------------------
 1179|  3.16k|        *outLastExaminedPage = i;
 1180|       |
 1181|  3.16k|    return retval;
 1182|  3.16k|}
readstat_sas7bdat_read.c:sas7bdat_parse_page_pass1:
  949|  7.12k|static readstat_error_t sas7bdat_parse_page_pass1(const char *page, size_t page_size, sas7bdat_ctx_t *ctx) {
  950|  7.12k|    readstat_error_t retval = READSTAT_OK;
  951|       |
  952|  7.12k|    uint16_t subheader_count = sas_read2(&page[ctx->page_header_size-4], ctx->bswap);
  953|       |
  954|  7.12k|    int i;
  955|  7.12k|    const char *shp = &page[ctx->page_header_size];
  956|  7.12k|    int lshp = ctx->subheader_pointer_size;
  957|       |
  958|  7.12k|    if (ctx->page_header_size + subheader_count*lshp > page_size) {
  ------------------
  |  Branch (958:9): [True: 38, False: 7.08k]
  ------------------
  959|     38|        retval = READSTAT_ERROR_PARSE;
  960|     38|        goto cleanup;
  961|     38|    }
  962|       |
  963|  68.9k|    for (i=0; i<subheader_count; i++) {
  ------------------
  |  Branch (963:15): [True: 62.5k, False: 6.41k]
  ------------------
  964|  62.5k|        subheader_pointer_t shp_info = { 0 };
  965|  62.5k|        if ((retval = sas7bdat_parse_subheader_pointer(shp, page + page_size - shp, &shp_info, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (965:13): [True: 0, False: 62.5k]
  ------------------
  966|      0|            goto cleanup;
  967|      0|        }
  968|  62.5k|        if (shp_info.len > 0 && shp_info.compression != SAS_COMPRESSION_TRUNC) {
  ------------------
  |  |  125|  40.8k|#define SAS_COMPRESSION_TRUNC        0x01
  ------------------
  |  Branch (968:13): [True: 40.8k, False: 21.7k]
  |  Branch (968:33): [True: 34.5k, False: 6.27k]
  ------------------
  969|  34.5k|            if ((retval = sas7bdat_validate_subheader_pointer(&shp_info, page_size, subheader_count, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (969:17): [True: 589, False: 33.9k]
  ------------------
  970|    589|                goto cleanup;
  971|    589|            }
  972|  33.9k|            if (shp_info.compression == SAS_COMPRESSION_NONE) {
  ------------------
  |  |  124|  33.9k|#define SAS_COMPRESSION_NONE         0x00
  ------------------
  |  Branch (972:17): [True: 30.5k, False: 3.41k]
  ------------------
  973|  30.5k|                sas_subheader_type_t subheader_type = sas7bdat_parse_subheader_type(page + shp_info.offset, ctx);
  974|  30.5k|                if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_TEXT) {
  ------------------
  |  Branch (974:21): [True: 2.41k, False: 28.1k]
  ------------------
  975|  2.41k|                    if ((retval = sas7bdat_parse_subheader(subheader_type, page + shp_info.offset, shp_info.len, ctx))
  ------------------
  |  Branch (975:25): [True: 60, False: 2.35k]
  ------------------
  976|  2.41k|                            != READSTAT_OK) {
  977|     60|                        goto cleanup;
  978|     60|                    }
  979|  2.41k|                }
  980|  30.5k|            } else if (shp_info.compression == SAS_COMPRESSION_ROW || shp_info.compression == SAS_COMPRESSION_DELETED_ROW) {
  ------------------
  |  |  126|  6.82k|#define SAS_COMPRESSION_ROW          0x04
  ------------------
                          } else if (shp_info.compression == SAS_COMPRESSION_ROW || shp_info.compression == SAS_COMPRESSION_DELETED_ROW) {
  ------------------
  |  |  127|    294|#define SAS_COMPRESSION_DELETED_ROW  0x05
  ------------------
  |  Branch (980:24): [True: 3.11k, False: 294]
  |  Branch (980:71): [True: 272, False: 22]
  ------------------
  981|       |                /* void */
  982|  3.39k|            } else {
  983|     22|                retval = READSTAT_ERROR_UNSUPPORTED_COMPRESSION;
  984|     22|                goto cleanup;
  985|     22|            }
  986|  33.9k|        }
  987|       |
  988|  61.8k|        shp += lshp;
  989|  61.8k|    }
  990|       |
  991|  7.12k|cleanup:
  992|       |
  993|  7.12k|    return retval;
  994|  7.08k|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader_pointer:
  903|   100k|        subheader_pointer_t *info, sas7bdat_ctx_t *ctx) {
  904|   100k|    readstat_error_t retval = READSTAT_OK;
  905|   100k|    if (ctx->u64) {
  ------------------
  |  Branch (905:9): [True: 20.9k, False: 79.1k]
  ------------------
  906|  20.9k|        if (shp_size <= 17) {
  ------------------
  |  Branch (906:13): [True: 0, False: 20.9k]
  ------------------
  907|      0|            retval = READSTAT_ERROR_PARSE;
  908|      0|            goto cleanup;
  909|      0|        }
  910|  20.9k|        info->offset = sas_read8(&shp[0], ctx->bswap);
  911|  20.9k|        info->len = sas_read8(&shp[8], ctx->bswap);
  912|  20.9k|        info->compression = shp[16];
  913|  20.9k|        info->is_compressed_data = shp[17];
  914|  79.1k|    } else {
  915|  79.1k|        if (shp_size <= 9) {
  ------------------
  |  Branch (915:13): [True: 0, False: 79.1k]
  ------------------
  916|      0|            retval = READSTAT_ERROR_PARSE;
  917|      0|            goto cleanup;
  918|      0|        }
  919|  79.1k|        info->offset = sas_read4(&shp[0], ctx->bswap);
  920|  79.1k|        info->len = sas_read4(&shp[4], ctx->bswap);
  921|  79.1k|        info->compression = shp[8];
  922|  79.1k|        info->is_compressed_data = shp[9];
  923|  79.1k|    }
  924|   100k|cleanup:
  925|   100k|    return retval;
  926|   100k|}
readstat_sas7bdat_read.c:sas7bdat_validate_subheader_pointer:
  929|  54.0k|        uint16_t subheader_count, sas7bdat_ctx_t *ctx) {
  930|  54.0k|    if (shp_info->offset > page_size)
  ------------------
  |  Branch (930:9): [True: 471, False: 53.5k]
  ------------------
  931|    471|        return READSTAT_ERROR_PARSE;
  932|  53.5k|    if (shp_info->len > page_size)
  ------------------
  |  Branch (932:9): [True: 274, False: 53.3k]
  ------------------
  933|    274|        return READSTAT_ERROR_PARSE;
  934|  53.3k|    if (shp_info->offset + shp_info->len > page_size)
  ------------------
  |  Branch (934:9): [True: 34, False: 53.2k]
  ------------------
  935|     34|        return READSTAT_ERROR_PARSE;
  936|  53.2k|    if (shp_info->offset < ctx->page_header_size + subheader_count*ctx->subheader_pointer_size)
  ------------------
  |  Branch (936:9): [True: 14, False: 53.2k]
  ------------------
  937|     14|        return READSTAT_ERROR_PARSE;
  938|  53.2k|    if (shp_info->compression == SAS_COMPRESSION_NONE) {
  ------------------
  |  |  124|  53.2k|#define SAS_COMPRESSION_NONE         0x00
  ------------------
  |  Branch (938:9): [True: 47.9k, False: 5.28k]
  ------------------
  939|  47.9k|        if (shp_info->len < ctx->subheader_signature_size)
  ------------------
  |  Branch (939:13): [True: 6, False: 47.9k]
  ------------------
  940|      6|            return READSTAT_ERROR_PARSE;
  941|  47.9k|        if (shp_info->offset + ctx->subheader_signature_size > page_size)
  ------------------
  |  Branch (941:13): [True: 0, False: 47.9k]
  ------------------
  942|      0|            return READSTAT_ERROR_PARSE;
  943|  47.9k|    }
  944|       |    
  945|  53.2k|    return READSTAT_OK;
  946|  53.2k|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader_type:
  883|  47.9k|static sas_subheader_type_t sas7bdat_parse_subheader_type(const char* subheader, sas7bdat_ctx_t* ctx) {
  884|  47.9k|    if (!ctx->u64) {
  ------------------
  |  Branch (884:9): [True: 34.4k, False: 13.5k]
  ------------------
  885|  34.4k|        uint32_t signature_32 = sas_read4(subheader, ctx->bswap);
  886|  34.4k|        return sas7bdat_parse_subheader_type_32(signature_32);
  887|  34.4k|    }
  888|       |
  889|  13.5k|    uint64_t signature = sas_read8(subheader, ctx->bswap);
  890|  13.5k|    if (signature == SAS_SUBHEADER_SIGNATURE_ROW_SIZE) {
  ------------------
  |  |   92|  13.5k|#define SAS_SUBHEADER_SIGNATURE_ROW_SIZE       0xF7F7F7F7
  ------------------
  |  Branch (890:9): [True: 2.10k, False: 11.3k]
  ------------------
  891|  2.10k|        return SAS_SUBHEADER_TYPE_ROW_SIZE;
  892|  11.3k|    } else if (signature == SAS_SUBHEADER_SIGNATURE_COLUMN_SIZE) {
  ------------------
  |  |   93|  11.3k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_SIZE    0xF6F6F6F6
  ------------------
  |  Branch (892:16): [True: 523, False: 10.8k]
  ------------------
  893|    523|        return SAS_SUBHEADER_TYPE_COLUMN_SIZE;
  894|  10.8k|    } else if ((signature & SAS_SUBHEADER_SIGNATURE_64BIT_MASK) != SAS_SUBHEADER_SIGNATURE_64BIT_MASK) {
  ------------------
  |  |  105|  10.8k|#define SAS_SUBHEADER_SIGNATURE_64BIT_MASK     0xFFFFFFFF00000000
  ------------------
                  } else if ((signature & SAS_SUBHEADER_SIGNATURE_64BIT_MASK) != SAS_SUBHEADER_SIGNATURE_64BIT_MASK) {
  ------------------
  |  |  105|  10.8k|#define SAS_SUBHEADER_SIGNATURE_64BIT_MASK     0xFFFFFFFF00000000
  ------------------
  |  Branch (894:16): [True: 1.41k, False: 9.46k]
  ------------------
  895|  1.41k|        return SAS_SUBHEADER_TYPE_DATA;
  896|  1.41k|    }
  897|       |
  898|  9.46k|    uint32_t lower_bytes = (uint32_t)(signature & SAS_SUBHEADER_SIGNATURE_32BIT_MASK);
  ------------------
  |  |  106|  9.46k|#define SAS_SUBHEADER_SIGNATURE_32BIT_MASK     0x00000000FFFFFFFF
  ------------------
  899|  9.46k|    return sas7bdat_parse_subheader_type_32(lower_bytes);
  900|  13.5k|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader_type_32:
  857|  43.9k|static sas_subheader_type_t sas7bdat_parse_subheader_type_32(uint32_t signature) {
  858|  43.9k|    switch (signature) {
  859|  7.32k|        case SAS_SUBHEADER_SIGNATURE_ROW_SIZE:
  ------------------
  |  |   92|  7.32k|#define SAS_SUBHEADER_SIGNATURE_ROW_SIZE       0xF7F7F7F7
  ------------------
  |  Branch (859:9): [True: 7.32k, False: 36.6k]
  ------------------
  860|  7.32k|            return SAS_SUBHEADER_TYPE_ROW_SIZE;
  861|  2.99k|        case SAS_SUBHEADER_SIGNATURE_COLUMN_SIZE:
  ------------------
  |  |   93|  2.99k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_SIZE    0xF6F6F6F6
  ------------------
  |  Branch (861:9): [True: 2.99k, False: 40.9k]
  ------------------
  862|  2.99k|            return SAS_SUBHEADER_TYPE_COLUMN_SIZE;
  863|  2.67k|        case SAS_SUBHEADER_SIGNATURE_COUNTS:
  ------------------
  |  |   94|  2.67k|#define SAS_SUBHEADER_SIGNATURE_COUNTS         0xFFFFFC00
  ------------------
  |  Branch (863:9): [True: 2.67k, False: 41.2k]
  ------------------
  864|  2.67k|            return SAS_SUBHEADER_TYPE_COUNTS;
  865|  7.26k|        case SAS_SUBHEADER_SIGNATURE_COLUMN_FORMAT:
  ------------------
  |  |   95|  7.26k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_FORMAT  0xFFFFFBFE
  ------------------
  |  Branch (865:9): [True: 7.26k, False: 36.6k]
  ------------------
  866|  7.26k|            return SAS_SUBHEADER_TYPE_COLUMN_FORMAT;
  867|  4.83k|        case SAS_SUBHEADER_SIGNATURE_COLUMN_ATTRS:
  ------------------
  |  |  100|  4.83k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_ATTRS   0xFFFFFFFC
  ------------------
  |  Branch (867:9): [True: 4.83k, False: 39.0k]
  ------------------
  868|  4.83k|            return SAS_SUBHEADER_TYPE_COLUMN_ATTRS;
  869|  3.82k|        case SAS_SUBHEADER_SIGNATURE_COLUMN_TEXT:
  ------------------
  |  |  101|  3.82k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_TEXT    0xFFFFFFFD
  ------------------
  |  Branch (869:9): [True: 3.82k, False: 40.1k]
  ------------------
  870|  3.82k|            return SAS_SUBHEADER_TYPE_COLUMN_TEXT;
  871|  2.76k|        case SAS_SUBHEADER_SIGNATURE_COLUMN_LIST:
  ------------------
  |  |  102|  2.76k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_LIST    0xFFFFFFFE
  ------------------
  |  Branch (871:9): [True: 2.76k, False: 41.1k]
  ------------------
  872|  2.76k|            return SAS_SUBHEADER_TYPE_COLUMN_LIST;
  873|  7.02k|        case SAS_SUBHEADER_SIGNATURE_COLUMN_NAME:
  ------------------
  |  |  103|  7.02k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_NAME    0xFFFFFFFF
  ------------------
  |  Branch (873:9): [True: 7.02k, False: 36.9k]
  ------------------
  874|  7.02k|            return SAS_SUBHEADER_TYPE_COLUMN_NAME;
  875|  5.21k|        default:
  ------------------
  |  Branch (875:9): [True: 5.21k, False: 38.7k]
  ------------------
  876|  5.21k|            if ((signature & SAS_SUBHEADER_SIGNATURE_COLUMN_MASK) == SAS_SUBHEADER_SIGNATURE_COLUMN_MASK) {
  ------------------
  |  |   97|  5.21k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_MASK    0xFFFFFFF8
  ------------------
                          if ((signature & SAS_SUBHEADER_SIGNATURE_COLUMN_MASK) == SAS_SUBHEADER_SIGNATURE_COLUMN_MASK) {
  ------------------
  |  |   97|  5.21k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_MASK    0xFFFFFFF8
  ------------------
  |  Branch (876:17): [True: 1.73k, False: 3.48k]
  ------------------
  877|  1.73k|                return SAS_SUBHEADER_TYPE_UNKNOWN;
  878|  1.73k|            }
  879|  3.48k|            return SAS_SUBHEADER_TYPE_DATA;
  880|  43.9k|    }
  881|  43.9k|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader:
  676|  17.8k|        size_t len, sas7bdat_ctx_t *ctx) {
  677|  17.8k|    readstat_error_t retval = READSTAT_OK;
  678|       |
  679|  17.8k|    if (len < 2 + ctx->subheader_signature_size) {
  ------------------
  |  Branch (679:9): [True: 5, False: 17.7k]
  ------------------
  680|      5|        retval = READSTAT_ERROR_PARSE;
  681|      5|        goto cleanup;
  682|      5|    }
  683|  17.7k|    if (subheader_type == SAS_SUBHEADER_TYPE_ROW_SIZE) {
  ------------------
  |  Branch (683:9): [True: 3.54k, False: 14.2k]
  ------------------
  684|  3.54k|        retval = sas7bdat_parse_row_size_subheader(subheader, len, ctx);
  685|  14.2k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_SIZE) {
  ------------------
  |  Branch (685:16): [True: 1.25k, False: 12.9k]
  ------------------
  686|  1.25k|        retval = sas7bdat_parse_column_size_subheader(subheader, len, ctx);
  687|  12.9k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COUNTS) {
  ------------------
  |  Branch (687:16): [True: 1.15k, False: 11.8k]
  ------------------
  688|       |        /* void */
  689|  11.8k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_TEXT) {
  ------------------
  |  Branch (689:16): [True: 2.41k, False: 9.42k]
  ------------------
  690|  2.41k|        retval = sas7bdat_parse_column_text_subheader(subheader, len, ctx);
  691|  9.42k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_NAME) {
  ------------------
  |  Branch (691:16): [True: 2.71k, False: 6.71k]
  ------------------
  692|  2.71k|        retval = sas7bdat_parse_column_name_subheader(subheader, len, ctx);
  693|  6.71k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_ATTRS) {
  ------------------
  |  Branch (693:16): [True: 1.97k, False: 4.73k]
  ------------------
  694|  1.97k|        retval = sas7bdat_parse_column_attributes_subheader(subheader, len, ctx);
  695|  4.73k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_FORMAT) {
  ------------------
  |  Branch (695:16): [True: 3.17k, False: 1.56k]
  ------------------
  696|  3.17k|        retval = sas7bdat_parse_column_format_subheader(subheader, len, ctx);
  697|  3.17k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_LIST) {
  ------------------
  |  Branch (697:16): [True: 797, False: 763]
  ------------------
  698|       |        /* void */
  699|    797|    } else if (subheader_type == SAS_SUBHEADER_TYPE_UNKNOWN) {
  ------------------
  |  Branch (699:16): [True: 726, False: 37]
  ------------------
  700|       |        /* void */
  701|    726|    } else {
  702|     37|        retval = READSTAT_ERROR_PARSE;
  703|     37|    }
  704|       |
  705|  17.8k|cleanup:
  706|       |
  707|  17.8k|    return retval;
  708|  17.7k|}
readstat_sas7bdat_read.c:sas7bdat_parse_row_size_subheader:
  236|  3.54k|static readstat_error_t sas7bdat_parse_row_size_subheader(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  237|  3.54k|    readstat_error_t retval = READSTAT_OK;
  238|  3.54k|    uint64_t total_row_count;
  239|  3.54k|    uint64_t row_length, deleted_row_count, page_row_count;
  240|       |
  241|  3.54k|    if (len < (ctx->u64 ? 250: 190)) {
  ------------------
  |  Branch (241:9): [True: 5, False: 3.54k]
  |  Branch (241:16): [True: 1.02k, False: 2.52k]
  ------------------
  242|      5|        retval = READSTAT_ERROR_PARSE;
  243|      5|        goto cleanup;
  244|      5|    }
  245|       |
  246|  3.54k|    if (ctx->u64) {
  ------------------
  |  Branch (246:9): [True: 1.02k, False: 2.52k]
  ------------------
  247|  1.02k|        row_length = sas_read8(&subheader[40], ctx->bswap);
  248|  1.02k|        total_row_count = sas_read8(&subheader[48], ctx->bswap);
  249|  1.02k|        deleted_row_count = sas_read8(&subheader[56], ctx->bswap);
  250|  1.02k|        page_row_count = sas_read8(&subheader[120], ctx->bswap);
  251|  2.52k|    } else {
  252|  2.52k|        row_length = sas_read4(&subheader[20], ctx->bswap);
  253|  2.52k|        total_row_count = sas_read4(&subheader[24], ctx->bswap);
  254|  2.52k|        deleted_row_count = sas_read4(&subheader[28], ctx->bswap);
  255|  2.52k|        page_row_count = sas_read4(&subheader[60], ctx->bswap);
  256|  2.52k|    }
  257|       |
  258|  3.54k|    if (deleted_row_count > total_row_count) {
  ------------------
  |  Branch (258:9): [True: 77, False: 3.46k]
  ------------------
  259|     77|        retval = READSTAT_ERROR_PARSE;
  260|     77|        goto cleanup;
  261|     77|    }
  262|  3.46k|    ctx->total_row_count = total_row_count;
  263|  3.46k|    ctx->deleted_row_count = deleted_row_count;
  264|       |
  265|  3.46k|    sas_text_ref_t file_label_ref = sas7bdat_parse_text_ref(&subheader[len-130], ctx);
  266|  3.46k|    if (file_label_ref.length) {
  ------------------
  |  Branch (266:9): [True: 961, False: 2.50k]
  ------------------
  267|    961|        if ((retval = sas7bdat_copy_text_ref(ctx->file_label, sizeof(ctx->file_label),
  ------------------
  |  Branch (267:13): [True: 42, False: 919]
  ------------------
  268|    961|                        file_label_ref, ctx)) != READSTAT_OK) {
  269|     42|            goto cleanup;
  270|     42|        }
  271|    961|    }
  272|       |
  273|  3.42k|    sas_text_ref_t compression_ref = sas7bdat_parse_text_ref(&subheader[len-118], ctx);
  274|  3.42k|    if (compression_ref.length) {
  ------------------
  |  Branch (274:9): [True: 1.30k, False: 2.11k]
  ------------------
  275|  1.30k|        char compression[9];
  276|  1.30k|        if ((retval = sas7bdat_copy_text_ref(compression, sizeof(compression),
  ------------------
  |  Branch (276:13): [True: 57, False: 1.25k]
  ------------------
  277|  1.30k|                        compression_ref, ctx)) != READSTAT_OK) {
  278|     57|            goto cleanup;
  279|     57|        }
  280|  1.25k|        ctx->rdc_compression = (memcmp(compression, SAS_COMPRESSION_SIGNATURE_RDC, 8) == 0);
  ------------------
  |  |  130|  1.25k|#define SAS_COMPRESSION_SIGNATURE_RDC  "SASYZCR2"
  ------------------
  281|  1.25k|    }
  282|       |
  283|  3.36k|    ctx->row_length = row_length;
  284|  3.36k|    ctx->row = readstat_realloc(ctx->row, ctx->row_length);
  285|  3.36k|    if (ctx->row == NULL) {
  ------------------
  |  Branch (285:9): [True: 8, False: 3.35k]
  ------------------
  286|      8|        retval = READSTAT_ERROR_MALLOC;
  287|      8|        goto cleanup;
  288|      8|    }
  289|       |
  290|  3.35k|    ctx->page_row_count = page_row_count;
  291|  3.35k|    uint64_t live_row_count = total_row_count - deleted_row_count;
  292|  3.35k|    uint64_t total_row_count_after_skipping = live_row_count;
  293|  3.35k|    if (live_row_count > ctx->row_offset) {
  ------------------
  |  Branch (293:9): [True: 3.00k, False: 356]
  ------------------
  294|  3.00k|        total_row_count_after_skipping -= ctx->row_offset;
  295|  3.00k|    } else {
  296|    356|        total_row_count_after_skipping = 0;
  297|    356|        ctx->row_offset = live_row_count;
  298|    356|    }
  299|  3.35k|    if (ctx->row_limit == 0 || total_row_count_after_skipping < ctx->row_limit)
  ------------------
  |  Branch (299:9): [True: 1.43k, False: 1.92k]
  |  Branch (299:32): [True: 98, False: 1.82k]
  ------------------
  300|  1.53k|        ctx->row_limit = total_row_count_after_skipping;
  301|       |
  302|  3.54k|cleanup:
  303|  3.54k|    return retval;
  304|  3.35k|}
readstat_sas7bdat_read.c:sas7bdat_parse_text_ref:
  137|  39.7k|static sas_text_ref_t sas7bdat_parse_text_ref(const char *data, sas7bdat_ctx_t *ctx) {
  138|  39.7k|    sas_text_ref_t  ref;
  139|       |
  140|  39.7k|    ref.index = sas_read2(&data[0], ctx->bswap);
  141|  39.7k|    ref.offset = sas_read2(&data[2], ctx->bswap);
  142|  39.7k|    ref.length = sas_read2(&data[4], ctx->bswap);
  143|       |
  144|  39.7k|    return ref;
  145|  39.7k|}
readstat_sas7bdat_read.c:sas7bdat_copy_text_ref:
  147|  5.59M|static readstat_error_t sas7bdat_copy_text_ref(char *out_buffer, size_t out_buffer_len, sas_text_ref_t text_ref, sas7bdat_ctx_t *ctx) {
  148|  5.59M|    if (text_ref.index >= ctx->text_blob_count)
  ------------------
  |  Branch (148:9): [True: 136, False: 5.59M]
  ------------------
  149|    136|        return READSTAT_ERROR_PARSE;
  150|       |    
  151|  5.59M|    if (text_ref.length == 0) {
  ------------------
  |  Branch (151:9): [True: 5.58M, False: 3.90k]
  ------------------
  152|  5.58M|        out_buffer[0] = '\0';
  153|  5.58M|        return READSTAT_OK;
  154|  5.58M|    }
  155|       |
  156|  3.90k|    char *blob = ctx->text_blobs[text_ref.index];
  157|       |
  158|  3.90k|    if (text_ref.offset + text_ref.length > ctx->text_blob_lengths[text_ref.index])
  ------------------
  |  Branch (158:9): [True: 81, False: 3.82k]
  ------------------
  159|     81|        return READSTAT_ERROR_PARSE;
  160|       |
  161|  3.82k|    return readstat_convert(out_buffer, out_buffer_len, &blob[text_ref.offset], text_ref.length,
  162|  3.82k|            ctx->converter);
  163|  3.90k|}
readstat_sas7bdat_read.c:sas7bdat_parse_column_size_subheader:
  208|  1.25k|static readstat_error_t sas7bdat_parse_column_size_subheader(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  209|  1.25k|    uint64_t col_count;
  210|  1.25k|    readstat_error_t retval = READSTAT_OK;
  211|       |
  212|  1.25k|    if (ctx->column_count || ctx->did_submit_columns) {
  ------------------
  |  Branch (212:9): [True: 3, False: 1.25k]
  |  Branch (212:30): [True: 1, False: 1.25k]
  ------------------
  213|      4|        retval = READSTAT_ERROR_PARSE;
  214|      4|        goto cleanup;
  215|      4|    }
  216|       |
  217|  1.25k|    if (len < (ctx->u64 ? 16 : 8)) {
  ------------------
  |  Branch (217:9): [True: 3, False: 1.25k]
  |  Branch (217:16): [True: 330, False: 924]
  ------------------
  218|      3|        retval = READSTAT_ERROR_PARSE;
  219|      3|        goto cleanup;
  220|      3|    }
  221|       |
  222|  1.25k|    if (ctx->u64) {
  ------------------
  |  Branch (222:9): [True: 329, False: 922]
  ------------------
  223|    329|        col_count = sas_read8(&subheader[8], ctx->bswap);
  224|    922|    } else {
  225|    922|        col_count = sas_read4(&subheader[4], ctx->bswap);
  226|    922|    }
  227|       |
  228|  1.25k|    ctx->column_count = col_count;
  229|       |
  230|  1.25k|    retval = sas7bdat_realloc_col_info(ctx, ctx->column_count);
  231|       |
  232|  1.25k|cleanup:
  233|  1.25k|    return retval;
  234|  1.25k|}
readstat_sas7bdat_read.c:sas7bdat_realloc_col_info:
  195|  9.01k|static readstat_error_t sas7bdat_realloc_col_info(sas7bdat_ctx_t *ctx, size_t count) {
  196|  9.01k|    if (ctx->col_info_count < count) {
  ------------------
  |  Branch (196:9): [True: 3.57k, False: 5.43k]
  ------------------
  197|  3.57k|        size_t old_count = ctx->col_info_count;
  198|  3.57k|        ctx->col_info_count = count;
  199|  3.57k|        ctx->col_info = readstat_realloc(ctx->col_info, ctx->col_info_count * sizeof(col_info_t));
  200|  3.57k|        if (ctx->col_info == NULL) {
  ------------------
  |  Branch (200:13): [True: 81, False: 3.49k]
  ------------------
  201|     81|            return READSTAT_ERROR_MALLOC;
  202|     81|        }
  203|  3.49k|        memset(ctx->col_info + old_count, 0, (count - old_count) * sizeof(col_info_t));
  204|  3.49k|    }
  205|  8.92k|    return READSTAT_OK;
  206|  9.01k|}
readstat_sas7bdat_read.c:sas7bdat_parse_column_text_subheader:
  165|  2.41k|static readstat_error_t sas7bdat_parse_column_text_subheader(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  166|  2.41k|    readstat_error_t retval = READSTAT_OK;
  167|  2.41k|    size_t signature_len = ctx->subheader_signature_size;
  168|  2.41k|    uint16_t remainder = sas_read2(&subheader[signature_len], ctx->bswap);
  169|  2.41k|    char *blob = NULL;
  170|  2.41k|    if (remainder != sas_subheader_remainder(len, signature_len)) {
  ------------------
  |  Branch (170:9): [True: 58, False: 2.35k]
  ------------------
  171|     58|        retval = READSTAT_ERROR_PARSE;
  172|     58|        goto cleanup;
  173|     58|    }
  174|  2.35k|    ctx->text_blob_count++;
  175|  2.35k|    ctx->text_blobs = readstat_realloc(ctx->text_blobs, ctx->text_blob_count * sizeof(char *));
  176|  2.35k|    ctx->text_blob_lengths = readstat_realloc(ctx->text_blob_lengths,
  177|  2.35k|            ctx->text_blob_count * sizeof(ctx->text_blob_lengths[0]));
  178|  2.35k|    if (ctx->text_blobs == NULL || ctx->text_blob_lengths == NULL) {
  ------------------
  |  Branch (178:9): [True: 0, False: 2.35k]
  |  Branch (178:36): [True: 0, False: 2.35k]
  ------------------
  179|      0|        retval = READSTAT_ERROR_MALLOC;
  180|      0|        goto cleanup;
  181|      0|    }
  182|       |
  183|  2.35k|    if ((blob = readstat_malloc(len-signature_len)) == NULL) {
  ------------------
  |  Branch (183:9): [True: 0, False: 2.35k]
  ------------------
  184|      0|        retval = READSTAT_ERROR_MALLOC;
  185|      0|        goto cleanup;
  186|      0|    }
  187|  2.35k|    memcpy(blob, subheader+signature_len, len-signature_len);
  188|  2.35k|    ctx->text_blob_lengths[ctx->text_blob_count-1] = len-signature_len;
  189|  2.35k|    ctx->text_blobs[ctx->text_blob_count-1] = blob;
  190|       |
  191|  2.41k|cleanup:
  192|  2.41k|    return retval;
  193|  2.35k|}
readstat_sas7bdat_read.c:sas7bdat_parse_column_name_subheader:
  306|  2.71k|static readstat_error_t sas7bdat_parse_column_name_subheader(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  307|  2.71k|    readstat_error_t retval = READSTAT_OK;
  308|  2.71k|    size_t signature_len = ctx->subheader_signature_size;
  309|  2.71k|    int cmax = ctx->u64 ? (len-28)/8 : (len-20)/8;
  ------------------
  |  Branch (309:16): [True: 1.70k, False: 1.00k]
  ------------------
  310|  2.71k|    int i;
  311|  2.71k|    const char *cnp = &subheader[signature_len+8];
  312|  2.71k|    uint16_t remainder = sas_read2(&subheader[signature_len], ctx->bswap);
  313|       |
  314|  2.71k|    if (remainder != sas_subheader_remainder(len, signature_len)) {
  ------------------
  |  Branch (314:9): [True: 57, False: 2.65k]
  ------------------
  315|     57|        retval = READSTAT_ERROR_PARSE;
  316|     57|        goto cleanup;
  317|     57|    }
  318|       |
  319|  2.65k|    ctx->col_names_count += cmax;
  320|       |
  321|  2.65k|    if ((retval = sas7bdat_realloc_col_info(ctx, ctx->col_names_count)) != READSTAT_OK)
  ------------------
  |  Branch (321:9): [True: 13, False: 2.64k]
  ------------------
  322|     13|        goto cleanup;
  323|       |
  324|  29.1k|    for (i=ctx->col_names_count-cmax; i<ctx->col_names_count; i++) {
  ------------------
  |  Branch (324:39): [True: 26.5k, False: 2.64k]
  ------------------
  325|  26.5k|        ctx->col_info[i].name_ref = sas7bdat_parse_text_ref(cnp, ctx);
  326|  26.5k|        cnp += 8;
  327|  26.5k|    }
  328|       |
  329|  2.71k|cleanup:
  330|       |
  331|  2.71k|    return retval;
  332|  2.64k|}
readstat_sas7bdat_read.c:sas7bdat_parse_column_attributes_subheader:
  334|  1.97k|static readstat_error_t sas7bdat_parse_column_attributes_subheader(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  335|  1.97k|    readstat_error_t retval = READSTAT_OK;
  336|  1.97k|    size_t signature_len = ctx->subheader_signature_size;
  337|  1.97k|    int cmax = ctx->u64 ? (len-28)/16 : (len-20)/12;
  ------------------
  |  Branch (337:16): [True: 375, False: 1.60k]
  ------------------
  338|  1.97k|    int i;
  339|  1.97k|    const char *cap = &subheader[signature_len+8];
  340|  1.97k|    uint16_t remainder = sas_read2(&subheader[signature_len], ctx->bswap);
  341|       |
  342|  1.97k|    if (remainder != sas_subheader_remainder(len, signature_len)) {
  ------------------
  |  Branch (342:9): [True: 46, False: 1.93k]
  ------------------
  343|     46|        retval = READSTAT_ERROR_PARSE;
  344|     46|        goto cleanup;
  345|     46|    }
  346|  1.93k|    ctx->col_attrs_count += cmax;
  347|  1.93k|    if ((retval = sas7bdat_realloc_col_info(ctx, ctx->col_attrs_count)) != READSTAT_OK)
  ------------------
  |  Branch (347:9): [True: 11, False: 1.92k]
  ------------------
  348|     11|        goto cleanup;
  349|       |
  350|  9.16k|    for (i=ctx->col_attrs_count-cmax; i<ctx->col_attrs_count; i++) {
  ------------------
  |  Branch (350:39): [True: 7.26k, False: 1.90k]
  ------------------
  351|  7.26k|        if (ctx->u64) {
  ------------------
  |  Branch (351:13): [True: 600, False: 6.66k]
  ------------------
  352|    600|            ctx->col_info[i].offset = sas_read8(&cap[0], ctx->bswap);
  353|  6.66k|        } else {
  354|  6.66k|            ctx->col_info[i].offset = sas_read4(&cap[0], ctx->bswap);
  355|  6.66k|        }
  356|       |
  357|  7.26k|        readstat_off_t off=4;
  358|  7.26k|        if (ctx->u64)
  ------------------
  |  Branch (358:13): [True: 600, False: 6.66k]
  ------------------
  359|    600|            off=8;
  360|       |
  361|  7.26k|        ctx->col_info[i].width = sas_read4(&cap[off], ctx->bswap);
  362|  7.26k|        if (ctx->col_info[i].width > ctx->max_col_width)
  ------------------
  |  Branch (362:13): [True: 911, False: 6.35k]
  ------------------
  363|    911|            ctx->max_col_width = ctx->col_info[i].width;
  364|       |
  365|  7.26k|        if (cap[off+6] == SAS_COLUMN_TYPE_NUM) {
  ------------------
  |  |   89|  7.26k|#define SAS_COLUMN_TYPE_NUM  0x01
  ------------------
  |  Branch (365:13): [True: 4.47k, False: 2.78k]
  ------------------
  366|  4.47k|            ctx->col_info[i].type = READSTAT_TYPE_DOUBLE;
  367|  4.47k|        } else if (cap[off+6] == SAS_COLUMN_TYPE_CHR) {
  ------------------
  |  |   90|  2.78k|#define SAS_COLUMN_TYPE_CHR  0x02
  ------------------
  |  Branch (367:20): [True: 2.76k, False: 18]
  ------------------
  368|  2.76k|            ctx->col_info[i].type = READSTAT_TYPE_STRING;
  369|  2.76k|        } else {
  370|     18|            retval = READSTAT_ERROR_PARSE;
  371|     18|            goto cleanup;
  372|     18|        }
  373|  7.24k|        ctx->col_info[i].index = i;
  374|  7.24k|        cap += off+8;
  375|  7.24k|    }
  376|       |
  377|  1.97k|cleanup:
  378|       |
  379|  1.97k|    return retval;
  380|  1.92k|}
readstat_sas7bdat_read.c:sas7bdat_parse_column_format_subheader:
  382|  3.17k|static readstat_error_t sas7bdat_parse_column_format_subheader(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  383|  3.17k|    readstat_error_t retval = READSTAT_OK;
  384|       |
  385|  3.17k|    if (len < (ctx->u64 ? 58 : 46)) {
  ------------------
  |  Branch (385:9): [True: 5, False: 3.17k]
  |  Branch (385:16): [True: 301, False: 2.87k]
  ------------------
  386|      5|        retval = READSTAT_ERROR_PARSE;
  387|      5|        goto cleanup;
  388|      5|    }
  389|       |
  390|  3.17k|    ctx->col_formats_count++;
  391|  3.17k|    if ((retval = sas7bdat_realloc_col_info(ctx, ctx->col_formats_count)) != READSTAT_OK)
  ------------------
  |  Branch (391:9): [True: 0, False: 3.17k]
  ------------------
  392|      0|        goto cleanup;
  393|       |
  394|  3.17k|    if (ctx->u64) {
  ------------------
  |  Branch (394:9): [True: 300, False: 2.87k]
  ------------------
  395|    300|        ctx->col_info[ctx->col_formats_count-1].format_width = sas_read2(&subheader[24], ctx->bswap);
  396|    300|        ctx->col_info[ctx->col_formats_count-1].format_digits = sas_read2(&subheader[26], ctx->bswap);
  397|  2.87k|    } else {
  398|  2.87k|        ctx->col_info[ctx->col_formats_count-1].format_width = sas_read2(&subheader[12], ctx->bswap);
  399|  2.87k|        ctx->col_info[ctx->col_formats_count-1].format_digits = sas_read2(&subheader[14], ctx->bswap);
  400|  2.87k|    }
  401|  3.17k|    ctx->col_info[ctx->col_formats_count-1].format_ref = sas7bdat_parse_text_ref(
  402|  3.17k|            ctx->u64 ? &subheader[46] : &subheader[34], ctx);
  ------------------
  |  Branch (402:13): [True: 300, False: 2.87k]
  ------------------
  403|  3.17k|    ctx->col_info[ctx->col_formats_count-1].label_ref = sas7bdat_parse_text_ref(
  404|  3.17k|            ctx->u64 ? &subheader[52] : &subheader[40], ctx);
  ------------------
  |  Branch (404:13): [True: 300, False: 2.87k]
  ------------------
  405|       |
  406|  3.17k|cleanup:
  407|  3.17k|    return retval;
  408|  3.17k|}
readstat_sas7bdat_read.c:sas7bdat_parse_amd_pages_pass1:
 1184|  2.90k|static readstat_error_t sas7bdat_parse_amd_pages_pass1(int64_t last_examined_page_pass1, sas7bdat_ctx_t *ctx) {
 1185|  2.90k|    readstat_error_t retval = READSTAT_OK;
 1186|  2.90k|    readstat_io_t *io = ctx->io;
 1187|  2.90k|    uint64_t i;
 1188|  2.90k|    uint64_t amd_page_count = 0;
 1189|       |
 1190|       |    /* ...then AMD pages at the end */
 1191|  6.82k|    for (i=ctx->page_count-1; i>last_examined_page_pass1; i--) {
  ------------------
  |  Branch (1191:31): [True: 5.32k, False: 1.49k]
  ------------------
 1192|  5.32k|        if (io->seek(ctx->header_size + i*ctx->page_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (1192:13): [True: 213, False: 5.11k]
  ------------------
 1193|    213|            retval = READSTAT_ERROR_SEEK;
 1194|    213|            if (ctx->handle.error) {
  ------------------
  |  Branch (1194:17): [True: 0, False: 213]
  ------------------
 1195|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: Failed to seek to position %" PRId64 
 1196|      0|                        " (= %" PRId64 " + %" PRId64 "*%" PRId64 ")",
 1197|      0|                        ctx->header_size + i*ctx->page_size, ctx->header_size, i, ctx->page_size);
 1198|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1199|      0|            }
 1200|    213|            goto cleanup;
 1201|    213|        }
 1202|       |
 1203|  5.11k|        readstat_off_t off = 0;
 1204|  5.11k|        if (ctx->u64)
  ------------------
  |  Branch (1204:13): [True: 4.03k, False: 1.07k]
  ------------------
 1205|  4.03k|            off = 16;
 1206|       |
 1207|  5.11k|        size_t head_len = off + 16 + 2;
 1208|  5.11k|        size_t tail_len = ctx->page_size - head_len;
 1209|       |
 1210|  5.11k|        if (io->read(ctx->page, head_len, io->io_ctx) < head_len) {
  ------------------
  |  Branch (1210:13): [True: 6, False: 5.10k]
  ------------------
 1211|      6|            retval = READSTAT_ERROR_READ;
 1212|      6|            goto cleanup;
 1213|      6|        }
 1214|       |
 1215|  5.10k|        uint16_t page_type = sas_read2(&ctx->page[off+16], ctx->bswap);
 1216|       |
 1217|  5.10k|        if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_DATA) {
  ------------------
  |  |  112|  5.10k|#define SAS_PAGE_TYPE_MASK   0x0F00
  ------------------
                      if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_DATA) {
  ------------------
  |  |  109|  5.10k|#define SAS_PAGE_TYPE_DATA   0x0100
  ------------------
  |  Branch (1217:13): [True: 1.40k, False: 3.69k]
  ------------------
 1218|       |            /* Usually AMD pages are at the end but sometimes data pages appear after them */
 1219|  1.40k|            if (amd_page_count > 0)
  ------------------
  |  Branch (1219:17): [True: 527, False: 882]
  ------------------
 1220|    527|                break;
 1221|    882|            continue;
 1222|  1.40k|        }
 1223|  3.69k|        if ((page_type & SAS_PAGE_TYPE_COMP))
  ------------------
  |  |  116|  3.69k|#define SAS_PAGE_TYPE_COMP          0x9000
  ------------------
  |  Branch (1223:13): [True: 959, False: 2.73k]
  ------------------
 1224|    959|            continue;
 1225|       |
 1226|  2.73k|        if (io->read(ctx->page + head_len, tail_len, io->io_ctx) < tail_len) {
  ------------------
  |  Branch (1226:13): [True: 17, False: 2.72k]
  ------------------
 1227|     17|            retval = READSTAT_ERROR_READ;
 1228|     17|            goto cleanup;
 1229|     17|        }
 1230|       |
 1231|  2.72k|        if ((retval = sas7bdat_parse_page_pass1(ctx->page, ctx->page_size, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1231:13): [True: 649, False: 2.07k]
  ------------------
 1232|    649|            if (ctx->handle.error && retval != READSTAT_ERROR_USER_ABORT) {
  ------------------
  |  Branch (1232:17): [True: 0, False: 649]
  |  Branch (1232:38): [True: 0, False: 0]
  ------------------
 1233|      0|                int64_t pos = io->seek(0, READSTAT_SEEK_CUR, io->io_ctx);
 1234|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf), 
 1235|      0|                        "ReadStat: Error parsing page %" PRId64 ", bytes %" PRId64 "-%" PRId64, 
 1236|      0|                        i, pos - ctx->page_size, pos-1);
 1237|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1238|      0|            }
 1239|    649|            goto cleanup;
 1240|    649|        }
 1241|       |
 1242|  2.07k|        amd_page_count++;
 1243|  2.07k|    }
 1244|       |
 1245|  2.90k|cleanup:
 1246|       |
 1247|  2.90k|    return retval;
 1248|  2.90k|}
readstat_sas7bdat_read.c:sas7bdat_parse_all_pages_pass2:
 1250|  2.02k|static readstat_error_t sas7bdat_parse_all_pages_pass2(sas7bdat_ctx_t *ctx) {
 1251|  2.02k|    readstat_error_t retval = READSTAT_OK;
 1252|  2.02k|    readstat_io_t *io = ctx->io;
 1253|  2.02k|    int64_t i;
 1254|       |
 1255|  6.28k|    for (i=0; i<ctx->page_count; i++) {
  ------------------
  |  Branch (1255:15): [True: 6.17k, False: 106]
  ------------------
 1256|  6.17k|        if ((retval = sas7bdat_update_progress(ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1256:13): [True: 0, False: 6.17k]
  ------------------
 1257|      0|            goto cleanup;
 1258|      0|        }
 1259|  6.17k|        if (io->read(ctx->page, ctx->page_size, io->io_ctx) < ctx->page_size) {
  ------------------
  |  Branch (1259:13): [True: 188, False: 5.98k]
  ------------------
 1260|    188|            retval = READSTAT_ERROR_READ;
 1261|    188|            goto cleanup;
 1262|    188|        }
 1263|       |
 1264|  5.98k|        if ((retval = sas7bdat_parse_page_pass2(ctx->page, ctx->page_size, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1264:13): [True: 1.52k, False: 4.45k]
  ------------------
 1265|  1.52k|            if (ctx->handle.error && retval != READSTAT_ERROR_USER_ABORT) {
  ------------------
  |  Branch (1265:17): [True: 0, False: 1.52k]
  |  Branch (1265:38): [True: 0, False: 0]
  ------------------
 1266|      0|                int64_t pos = io->seek(0, READSTAT_SEEK_CUR, io->io_ctx);
 1267|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf), 
 1268|      0|                        "ReadStat: Error parsing page %" PRId64 ", bytes %" PRId64 "-%" PRId64, 
 1269|      0|                        i, pos - ctx->page_size, pos-1);
 1270|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1271|      0|            }
 1272|  1.52k|            goto cleanup;
 1273|  1.52k|        }
 1274|  4.45k|        if (ctx->parsed_row_count == ctx->row_limit)
  ------------------
  |  Branch (1274:13): [True: 201, False: 4.25k]
  ------------------
 1275|    201|            break;
 1276|  4.45k|    }
 1277|  2.02k|cleanup:
 1278|       |
 1279|  2.02k|    return retval;
 1280|  2.02k|}
readstat_sas7bdat_read.c:sas7bdat_parse_page_pass2:
 1015|  5.98k|static readstat_error_t sas7bdat_parse_page_pass2(const char *page, size_t page_size, sas7bdat_ctx_t *ctx) {
 1016|  5.98k|    uint16_t page_type;
 1017|       |
 1018|  5.98k|    readstat_error_t retval = READSTAT_OK;
 1019|       |
 1020|  5.98k|    page_type = sas_read2(&page[ctx->page_header_size-8], ctx->bswap);
 1021|       |
 1022|  5.98k|    const char *data = NULL;
 1023|       |
 1024|  5.98k|    if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_DATA) {
  ------------------
  |  |  112|  5.98k|#define SAS_PAGE_TYPE_MASK   0x0F00
  ------------------
                  if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_DATA) {
  ------------------
  |  |  109|  5.98k|#define SAS_PAGE_TYPE_DATA   0x0100
  ------------------
  |  Branch (1024:9): [True: 1.14k, False: 4.83k]
  ------------------
 1025|  1.14k|        ctx->page_row_count = sas_read2(&page[ctx->page_header_size-6], ctx->bswap);
 1026|  1.14k|        data = &page[ctx->page_header_size];
 1027|  4.83k|    } else if (!(page_type & SAS_PAGE_TYPE_COMP)) {
  ------------------
  |  |  116|  4.83k|#define SAS_PAGE_TYPE_COMP          0x9000
  ------------------
  |  Branch (1027:16): [True: 4.22k, False: 610]
  ------------------
 1028|  4.22k|        uint16_t subheader_count = sas_read2(&page[ctx->page_header_size-4], ctx->bswap);
 1029|       |
 1030|  4.22k|        int i;
 1031|  4.22k|        const char *shp = &page[ctx->page_header_size];
 1032|  4.22k|        int lshp = ctx->subheader_pointer_size;
 1033|       |
 1034|  4.22k|        if (ctx->page_header_size + subheader_count*lshp > page_size) {
  ------------------
  |  Branch (1034:13): [True: 13, False: 4.21k]
  ------------------
 1035|     13|            retval = READSTAT_ERROR_PARSE;
 1036|     13|            goto cleanup;
 1037|     13|        }
 1038|       |
 1039|  40.7k|        for (i=0; i<subheader_count; i++) {
  ------------------
  |  Branch (1039:19): [True: 37.5k, False: 3.17k]
  ------------------
 1040|  37.5k|            subheader_pointer_t shp_info = { 0 };
 1041|  37.5k|            if ((retval = sas7bdat_parse_subheader_pointer(shp, page + page_size - shp, &shp_info, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1041:17): [True: 0, False: 37.5k]
  ------------------
 1042|      0|                goto cleanup;
 1043|      0|            }
 1044|  37.5k|            if (shp_info.len > 0 && shp_info.compression != SAS_COMPRESSION_TRUNC) {
  ------------------
  |  |  125|  23.5k|#define SAS_COMPRESSION_TRUNC        0x01
  ------------------
  |  Branch (1044:17): [True: 23.5k, False: 14.0k]
  |  Branch (1044:37): [True: 19.4k, False: 4.04k]
  ------------------
 1045|  19.4k|                if ((retval = sas7bdat_validate_subheader_pointer(&shp_info, page_size, subheader_count, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1045:21): [True: 210, False: 19.2k]
  ------------------
 1046|    210|                    goto cleanup;
 1047|    210|                }
 1048|  19.2k|                if (shp_info.compression == SAS_COMPRESSION_NONE) {
  ------------------
  |  |  124|  19.2k|#define SAS_COMPRESSION_NONE         0x00
  ------------------
  |  Branch (1048:21): [True: 17.4k, False: 1.87k]
  ------------------
 1049|  17.4k|                    sas_subheader_type_t subheader_type = sas7bdat_parse_subheader_type(page + shp_info.offset, ctx);
 1050|  17.4k|                    if (shp_info.is_compressed_data && subheader_type == SAS_SUBHEADER_TYPE_DATA) {
  ------------------
  |  Branch (1050:25): [True: 12.4k, False: 4.95k]
  |  Branch (1050:56): [True: 602, False: 11.8k]
  ------------------
 1051|    602|                        if (shp_info.len != ctx->row_length) {
  ------------------
  |  Branch (1051:29): [True: 119, False: 483]
  ------------------
 1052|    119|                            retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
 1053|    119|                            goto cleanup;
 1054|    119|                        }
 1055|    483|                        if ((retval = sas7bdat_submit_columns_if_needed(ctx, 1)) != READSTAT_OK) {
  ------------------
  |  Branch (1055:29): [True: 6, False: 477]
  ------------------
 1056|      6|                            goto cleanup;
 1057|      6|                        }
 1058|    477|                        if ((retval = sas7bdat_parse_single_row(page + shp_info.offset, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1058:29): [True: 4, False: 473]
  ------------------
 1059|      4|                            goto cleanup;
 1060|      4|                        }
 1061|  16.7k|                    } else {
 1062|  16.7k|                        if (subheader_type != SAS_SUBHEADER_TYPE_COLUMN_TEXT) {
  ------------------
  |  Branch (1062:29): [True: 15.3k, False: 1.41k]
  ------------------
 1063|  15.3k|                            if ((retval = sas7bdat_parse_subheader(subheader_type, page + shp_info.offset, shp_info.len, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1063:33): [True: 443, False: 14.9k]
  ------------------
 1064|    443|                                goto cleanup;
 1065|    443|                            }
 1066|  15.3k|                        }
 1067|  16.7k|                    }
 1068|  17.4k|                } else if (shp_info.compression == SAS_COMPRESSION_ROW) {
  ------------------
  |  |  126|  1.87k|#define SAS_COMPRESSION_ROW          0x04
  ------------------
  |  Branch (1068:28): [True: 1.67k, False: 199]
  ------------------
 1069|  1.67k|                    if ((retval = sas7bdat_submit_columns_if_needed(ctx, 1)) != READSTAT_OK) {
  ------------------
  |  Branch (1069:25): [True: 26, False: 1.64k]
  ------------------
 1070|     26|                        goto cleanup;
 1071|     26|                    }
 1072|  1.64k|                    if ((retval = sas7bdat_parse_subheader_compressed(page + shp_info.offset, shp_info.len, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1072:25): [True: 225, False: 1.42k]
  ------------------
 1073|    225|                        goto cleanup;
 1074|    225|                    }
 1075|  1.64k|                } else if (shp_info.compression == SAS_COMPRESSION_DELETED_ROW) {
  ------------------
  |  |  127|    199|#define SAS_COMPRESSION_DELETED_ROW  0x05
  ------------------
  |  Branch (1075:28): [True: 193, False: 6]
  ------------------
 1076|    193|                    if ((retval = sas7bdat_register_deleted_row(ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1076:25): [True: 2, False: 191]
  ------------------
 1077|      2|                        goto cleanup;
 1078|      2|                    }
 1079|    193|                } else {
 1080|      6|                    retval = READSTAT_ERROR_UNSUPPORTED_COMPRESSION;
 1081|      6|                    goto cleanup;
 1082|      6|                }
 1083|  19.2k|            }
 1084|       |
 1085|  36.5k|            shp += lshp;
 1086|  36.5k|        }
 1087|       |
 1088|  3.17k|        if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_MIX) {
  ------------------
  |  |  112|  3.17k|#define SAS_PAGE_TYPE_MASK   0x0F00
  ------------------
                      if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_MIX) {
  ------------------
  |  |  110|  3.17k|#define SAS_PAGE_TYPE_MIX    0x0200
  ------------------
  |  Branch (1088:13): [True: 1.58k, False: 1.58k]
  ------------------
 1089|       |            /* HACK - this is supposed to obey 8-byte boundaries but
 1090|       |             * some files created by Stat/Transfer don't. So verify that the
 1091|       |             * padding is { 0, 0, 0, 0 } or { ' ', ' ', ' ', ' ' } (or that
 1092|       |             * the file is not from Stat/Transfer) before skipping it */
 1093|  1.58k|            if ((shp-page)%8 == 4 && shp + 4 <= page + page_size &&
  ------------------
  |  Branch (1093:17): [True: 517, False: 1.06k]
  |  Branch (1093:38): [True: 514, False: 3]
  ------------------
 1094|    514|                    (*(uint32_t *)shp == 0x00000000 ||
  ------------------
  |  Branch (1094:22): [True: 132, False: 382]
  ------------------
 1095|    382|                     *(uint32_t *)shp == 0x20202020 ||
  ------------------
  |  Branch (1095:22): [True: 124, False: 258]
  ------------------
 1096|    488|                     ctx->vendor != READSTAT_VENDOR_STAT_TRANSFER)) {
  ------------------
  |  Branch (1096:22): [True: 232, False: 26]
  ------------------
 1097|    488|                data = shp + 4;
 1098|  1.09k|            } else {
 1099|  1.09k|                data = shp;
 1100|  1.09k|            }
 1101|  1.58k|        }
 1102|  3.17k|    }
 1103|  4.93k|    if (data) {
  ------------------
  |  Branch (1103:9): [True: 2.73k, False: 2.19k]
  ------------------
 1104|  2.73k|        if ((retval = sas7bdat_submit_columns_if_needed(ctx, 0)) != READSTAT_OK) {
  ------------------
  |  Branch (1104:13): [True: 130, False: 2.60k]
  ------------------
 1105|    130|            goto cleanup;
 1106|    130|        }
 1107|  2.60k|        if (ctx->handle.value) {
  ------------------
  |  Branch (1107:13): [True: 2.60k, False: 0]
  ------------------
 1108|  2.60k|            const unsigned char *deleted_row_bitmap = NULL;
 1109|  2.60k|            if (page_type & SAS_PAGE_TYPE_DELETED_ROWS) {
  ------------------
  |  |  114|  2.60k|#define SAS_PAGE_TYPE_DELETED_ROWS  0x0080
  ------------------
  |  Branch (1109:17): [True: 592, False: 2.01k]
  ------------------
 1110|    592|                if ((retval = sas7bdat_parse_deleted_row_bitmap(page, data, page_size,
  ------------------
  |  Branch (1110:21): [True: 137, False: 455]
  ------------------
 1111|    592|                        &deleted_row_bitmap, ctx)) != READSTAT_OK) {
 1112|    137|                    goto cleanup;
 1113|    137|                }
 1114|    592|            }
 1115|  2.46k|            retval = sas7bdat_parse_rows(data, page + page_size - data, deleted_row_bitmap, ctx);
 1116|  2.46k|        }
 1117|  2.60k|    } 
 1118|  5.98k|cleanup:
 1119|       |
 1120|  5.98k|    return retval;
 1121|  4.93k|}
readstat_sas7bdat_read.c:sas7bdat_parse_single_row:
  482|   254k|static readstat_error_t sas7bdat_parse_single_row(const char *data, sas7bdat_ctx_t *ctx) {
  483|   254k|    if (ctx->parsed_row_count == ctx->row_limit)
  ------------------
  |  Branch (483:9): [True: 259, False: 254k]
  ------------------
  484|    259|        return READSTAT_OK;
  485|   254k|    if (ctx->row_offset) {
  ------------------
  |  Branch (485:9): [True: 0, False: 254k]
  ------------------
  486|      0|        ctx->row_offset--;
  487|      0|        return READSTAT_OK;
  488|      0|    }
  489|       |
  490|   254k|    readstat_error_t retval = READSTAT_OK;
  491|   254k|    int j;
  492|   254k|    if (ctx->handle.value) {
  ------------------
  |  Branch (492:9): [True: 254k, False: 0]
  ------------------
  493|   254k|        ctx->scratch_buffer_len = 4*ctx->max_col_width+1;
  494|   254k|        ctx->scratch_buffer = readstat_realloc(ctx->scratch_buffer, ctx->scratch_buffer_len);
  495|   254k|        if (ctx->scratch_buffer == NULL) {
  ------------------
  |  Branch (495:13): [True: 3, False: 254k]
  ------------------
  496|      3|            retval = READSTAT_ERROR_MALLOC;
  497|      3|            goto cleanup;
  498|      3|        }
  499|       |
  500|  8.22M|        for (j=0; j<ctx->column_count; j++) {
  ------------------
  |  Branch (500:19): [True: 7.96M, False: 254k]
  ------------------
  501|  7.96M|            col_info_t *col_info = &ctx->col_info[j];
  502|  7.96M|            readstat_variable_t *variable = ctx->variables[j];
  503|  7.96M|            if (variable->skip)
  ------------------
  |  Branch (503:17): [True: 0, False: 7.96M]
  ------------------
  504|      0|                continue;
  505|       |
  506|  7.96M|            if (col_info->offset > ctx->row_length || col_info->offset + col_info->width > ctx->row_length) {
  ------------------
  |  Branch (506:17): [True: 84, False: 7.96M]
  |  Branch (506:55): [True: 23, False: 7.96M]
  ------------------
  507|    107|                retval = READSTAT_ERROR_PARSE;
  508|    107|                goto cleanup;
  509|    107|            }
  510|  7.96M|            retval = sas7bdat_handle_data_value(variable, col_info, &data[col_info->offset], ctx);
  511|  7.96M|            if (retval != READSTAT_OK) {
  ------------------
  |  Branch (511:17): [True: 29, False: 7.96M]
  ------------------
  512|     29|                goto cleanup;
  513|     29|            }
  514|  7.96M|        }
  515|   254k|    }
  516|   254k|    ctx->parsed_row_count++;
  517|       |
  518|   254k|cleanup:
  519|   254k|    return retval;
  520|   254k|}
readstat_sas7bdat_read.c:sas7bdat_handle_data_value:
  419|  7.96M|        col_info_t *col_info, const char *col_data, sas7bdat_ctx_t *ctx) {
  420|  7.96M|    readstat_error_t retval = READSTAT_OK;
  421|  7.96M|    int cb_retval = 0;
  422|  7.96M|    readstat_value_t value;
  423|  7.96M|    memset(&value, 0, sizeof(readstat_value_t));
  424|       |
  425|  7.96M|    value.type = col_info->type;
  426|       |
  427|  7.96M|    if (col_info->type == READSTAT_TYPE_STRING) {
  ------------------
  |  Branch (427:9): [True: 7.82M, False: 139k]
  ------------------
  428|  7.82M|        retval = readstat_convert(ctx->scratch_buffer, ctx->scratch_buffer_len,
  429|  7.82M|                col_data, col_info->width, ctx->converter);
  430|  7.82M|        if (retval != READSTAT_OK) {
  ------------------
  |  Branch (430:13): [True: 27, False: 7.82M]
  ------------------
  431|     27|            if (ctx->handle.error) {
  ------------------
  |  Branch (431:17): [True: 0, False: 27]
  ------------------
  432|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf),
  433|      0|                        "ReadStat: Error converting string (row=%u, col=%u) to specified encoding: %.*s",
  434|      0|                        ctx->parsed_row_count+1, col_info->index+1, col_info->width, col_data);
  435|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
  436|      0|            }
  437|     27|            goto cleanup;
  438|     27|        }
  439|       |
  440|  7.82M|        value.v.string_value = ctx->scratch_buffer;
  441|  7.82M|    } else if (col_info->type == READSTAT_TYPE_DOUBLE) {
  ------------------
  |  Branch (441:16): [True: 139k, False: 0]
  ------------------
  442|   139k|        uint64_t  val = 0;
  443|   139k|        double dval = NAN;
  444|   139k|        if (col_info->width < 1 || col_info->width > 8) {
  ------------------
  |  Branch (444:13): [True: 1, False: 139k]
  |  Branch (444:36): [True: 1, False: 139k]
  ------------------
  445|       |            /* A DOUBLE is 3-8 bytes on disk, but col_info can be mutated by a
  446|       |             * COLUMN_ATTRS subheader after the columns were validated. Guard
  447|       |             * here so a corrupt width can't produce an undefined shift below. */
  448|      2|            retval = READSTAT_ERROR_PARSE;
  449|      2|            goto cleanup;
  450|      2|        }
  451|   139k|        if (ctx->little_endian) {
  ------------------
  |  Branch (451:13): [True: 139k, False: 0]
  ------------------
  452|   139k|            int k;
  453|   999k|            for (k=0; k<col_info->width; k++) {
  ------------------
  |  Branch (453:23): [True: 860k, False: 139k]
  ------------------
  454|   860k|                val = (val << 8) | (unsigned char)col_data[col_info->width-1-k];
  455|   860k|            }
  456|   139k|        } else {
  457|      0|            int k;
  458|      0|            for (k=0; k<col_info->width; k++) {
  ------------------
  |  Branch (458:23): [True: 0, False: 0]
  ------------------
  459|      0|                val = (val << 8) | (unsigned char)col_data[k];
  460|      0|            }
  461|      0|        }
  462|   139k|        val <<= (8-col_info->width)*8;
  463|       |
  464|   139k|        memcpy(&dval, &val, 8);
  465|       |
  466|   139k|        if (isnan(dval)) {
  ------------------
  |  Branch (466:13): [True: 8.35k, False: 130k]
  ------------------
  467|  8.35k|            value.v.double_value = NAN;
  468|  8.35k|            sas_assign_tag(&value, ~((val >> 40) & 0xFF));
  469|   130k|        } else {
  470|   130k|            value.v.double_value = dval;
  471|   130k|        }
  472|   139k|    }
  473|  7.96M|    cb_retval = ctx->handle.value(ctx->parsed_row_count, variable, value, ctx->user_ctx);
  474|       |
  475|  7.96M|    if (cb_retval != READSTAT_HANDLER_OK)
  ------------------
  |  Branch (475:9): [True: 0, False: 7.96M]
  ------------------
  476|      0|        retval = READSTAT_ERROR_USER_ABORT;
  477|       |
  478|  7.96M|cleanup:
  479|  7.96M|    return retval;
  480|  7.96M|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader_compressed:
  668|  1.64k|static readstat_error_t sas7bdat_parse_subheader_compressed(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  669|  1.64k|    if (ctx->rdc_compression)
  ------------------
  |  Branch (669:9): [True: 404, False: 1.24k]
  ------------------
  670|    404|        return sas7bdat_parse_subheader_rdc(subheader, len, ctx);
  671|       |
  672|  1.24k|    return sas7bdat_parse_subheader_rle(subheader, len, ctx);
  673|  1.64k|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader_rdc:
  554|    404|static readstat_error_t sas7bdat_parse_subheader_rdc(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  555|    404|    readstat_error_t retval = READSTAT_OK;
  556|    404|    const unsigned char *input = (const unsigned char *)subheader;
  557|    404|    char *buffer = malloc(ctx->row_length);
  558|    404|    if (buffer == NULL) return READSTAT_ERROR_MALLOC;
  ------------------
  |  Branch (558:9): [True: 0, False: 404]
  ------------------
  559|    404|    char *output = buffer;
  560|  1.81k|    while (input + 2 <= (const unsigned char *)subheader + len) {
  ------------------
  |  Branch (560:12): [True: 1.45k, False: 364]
  ------------------
  561|  1.45k|        int i;
  562|  1.45k|        unsigned short prefix = (input[0] << 8) + input[1];
  563|  1.45k|        input += 2;
  564|  20.0k|        for (i=0; i<16; i++) {
  ------------------
  |  Branch (564:19): [True: 19.0k, False: 1.05k]
  ------------------
  565|  19.0k|            if ((prefix & (1 << (15 - i))) == 0) {
  ------------------
  |  Branch (565:17): [True: 14.5k, False: 4.41k]
  ------------------
  566|  14.5k|                if (input + 1 > (const unsigned char *)subheader + len) {
  ------------------
  |  Branch (566:21): [True: 359, False: 14.2k]
  ------------------
  567|    359|                    break;
  568|    359|                }
  569|  14.2k|                if (output + 1 > buffer + ctx->row_length) {
  ------------------
  |  Branch (569:21): [True: 3, False: 14.2k]
  ------------------
  570|      3|                    retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  571|      3|                    goto cleanup;
  572|      3|                }
  573|  14.2k|                *output++ = *input++;
  574|  14.2k|                continue;
  575|  14.2k|            }
  576|       |
  577|  4.41k|            if (input + 2 > (const unsigned char *)subheader + len) {
  ------------------
  |  Branch (577:17): [True: 3, False: 4.41k]
  ------------------
  578|      3|                retval = READSTAT_ERROR_PARSE;
  579|      3|                goto cleanup;
  580|      3|            }
  581|       |
  582|  4.41k|            unsigned char marker_byte = *input++;
  583|  4.41k|            unsigned char next_byte = *input++;
  584|  4.41k|            size_t insert_len = 0, copy_len = 0;
  585|  4.41k|            unsigned char insert_byte = 0x00;
  586|  4.41k|            size_t back_offset = 0;
  587|       |
  588|  4.41k|            if (marker_byte <= 0x0F) {
  ------------------
  |  Branch (588:17): [True: 1.96k, False: 2.44k]
  ------------------
  589|  1.96k|                insert_len = 3 + marker_byte;
  590|  1.96k|                insert_byte = next_byte;
  591|  2.44k|            } else if ((marker_byte >> 4) == 1) {
  ------------------
  |  Branch (591:24): [True: 486, False: 1.96k]
  ------------------
  592|    486|                if (input + 1 > (const unsigned char *)subheader + len) {
  ------------------
  |  Branch (592:21): [True: 3, False: 483]
  ------------------
  593|      3|                    retval = READSTAT_ERROR_PARSE;
  594|      3|                    goto cleanup;
  595|      3|                }
  596|    483|                insert_len = 19 + (marker_byte & 0x0F) + next_byte * 16;
  597|    483|                insert_byte = *input++;
  598|  1.96k|            } else if ((marker_byte >> 4) == 2) {
  ------------------
  |  Branch (598:24): [True: 318, False: 1.64k]
  ------------------
  599|    318|                if (input + 1 > (const unsigned char *)subheader + len) {
  ------------------
  |  Branch (599:21): [True: 1, False: 317]
  ------------------
  600|      1|                    retval = READSTAT_ERROR_PARSE;
  601|      1|                    goto cleanup;
  602|      1|                }
  603|    317|                copy_len = 16 + (*input++);
  604|    317|                back_offset = 3 + (marker_byte & 0x0F) + next_byte * 16;
  605|  1.64k|            } else {
  606|  1.64k|                copy_len = (marker_byte >> 4);
  607|  1.64k|                back_offset = 3 + (marker_byte & 0x0F) + next_byte * 16;
  608|  1.64k|            }
  609|       |
  610|  4.40k|            if (insert_len) {
  ------------------
  |  Branch (610:17): [True: 2.44k, False: 1.96k]
  ------------------
  611|  2.44k|                if (output + insert_len > buffer + ctx->row_length) {
  ------------------
  |  Branch (611:21): [True: 2, False: 2.44k]
  ------------------
  612|      2|                    retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  613|      2|                    goto cleanup;
  614|      2|                }
  615|  2.44k|                memset(output, insert_byte, insert_len);
  616|  2.44k|                output += insert_len;
  617|  2.44k|            } else if (copy_len) {
  ------------------
  |  Branch (617:24): [True: 1.96k, False: 0]
  ------------------
  618|  1.96k|                if (output - buffer < back_offset || copy_len > back_offset) {
  ------------------
  |  Branch (618:21): [True: 12, False: 1.94k]
  |  Branch (618:54): [True: 13, False: 1.93k]
  ------------------
  619|     25|                    retval = READSTAT_ERROR_PARSE;
  620|     25|                    goto cleanup;
  621|     25|                }
  622|  1.93k|                if (output + copy_len > buffer + ctx->row_length) {
  ------------------
  |  Branch (622:21): [True: 3, False: 1.93k]
  ------------------
  623|      3|                    retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  624|      3|                    goto cleanup;
  625|      3|                }
  626|  1.93k|                memcpy(output, output - back_offset, copy_len);
  627|  1.93k|                output += copy_len;
  628|  1.93k|            }
  629|  4.40k|        }
  630|  1.45k|    }
  631|       |
  632|    364|    if (output - buffer != ctx->row_length) {
  ------------------
  |  Branch (632:9): [True: 12, False: 352]
  ------------------
  633|     12|        retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  634|     12|        goto cleanup;
  635|     12|    }
  636|    352|    retval = sas7bdat_parse_single_row(buffer, ctx);
  637|    404|cleanup:
  638|    404|    free(buffer);
  639|       |
  640|    404|    return retval;
  641|    352|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader_rle:
  643|  1.24k|static readstat_error_t sas7bdat_parse_subheader_rle(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  644|  1.24k|    if (ctx->row_limit == ctx->parsed_row_count)
  ------------------
  |  Branch (644:9): [True: 769, False: 475]
  ------------------
  645|    769|        return READSTAT_OK;
  646|       |
  647|    475|    readstat_error_t retval = READSTAT_OK;
  648|    475|    ssize_t bytes_decompressed = 0;
  649|       |
  650|    475|    bytes_decompressed = sas_rle_decompress(ctx->row, ctx->row_length, subheader, len);
  651|       |
  652|    475|    if (bytes_decompressed != ctx->row_length) {
  ------------------
  |  Branch (652:9): [True: 137, False: 338]
  ------------------
  653|    137|        retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  654|    137|        if (ctx->handle.error) {
  ------------------
  |  Branch (654:13): [True: 0, False: 137]
  ------------------
  655|      0|            snprintf(ctx->error_buf, sizeof(ctx->error_buf), 
  656|      0|                    "ReadStat: Row #%d decompressed to %ld bytes (expected %d bytes)",
  657|      0|                    ctx->parsed_row_count, (long)(bytes_decompressed), ctx->row_length);
  658|      0|            ctx->handle.error(ctx->error_buf, ctx->user_ctx);
  659|      0|        }
  660|    137|        goto cleanup;
  661|    137|    }
  662|    338|    retval = sas7bdat_parse_single_row(ctx->row, ctx);
  663|       |
  664|    475|cleanup:
  665|    475|    return retval;
  666|    338|}
readstat_sas7bdat_read.c:sas7bdat_register_deleted_row:
  410|  25.2k|static readstat_error_t sas7bdat_register_deleted_row(sas7bdat_ctx_t *ctx) {
  411|  25.2k|    if (ctx->parsed_deleted_row_count >= ctx->deleted_row_count) {
  ------------------
  |  Branch (411:9): [True: 6, False: 25.2k]
  ------------------
  412|      6|        return READSTAT_ERROR_PARSE;
  413|      6|    }
  414|  25.2k|    ctx->parsed_deleted_row_count++;
  415|  25.2k|    return READSTAT_OK;
  416|  25.2k|}
readstat_sas7bdat_read.c:sas7bdat_parse_deleted_row_bitmap:
  997|    592|        size_t page_size, const unsigned char **deleted_row_bitmap, sas7bdat_ctx_t *ctx) {
  998|    592|    uint64_t page_unused_bytes;
  999|    592|    if (ctx->u64) {
  ------------------
  |  Branch (999:9): [True: 453, False: 139]
  ------------------
 1000|    453|        page_unused_bytes = sas_read8(&page[24], ctx->bswap);
 1001|    453|    } else {
 1002|    139|        page_unused_bytes = sas_read4(&page[12], ctx->bswap);
 1003|    139|    }
 1004|    592|    uint32_t row_count = ctx->page_row_count < ctx->total_row_count ? ctx->page_row_count : ctx->total_row_count;
  ------------------
  |  Branch (1004:26): [True: 416, False: 176]
  ------------------
 1005|    592|    uint64_t deleted_row_bitmap_offset = (uint64_t)row_count * ctx->row_length + page_unused_bytes;
 1006|    592|    uint32_t required_bytes = row_count / CHAR_BIT + (row_count % CHAR_BIT == 0 ? 0 : 1);
  ------------------
  |  Branch (1006:55): [True: 476, False: 116]
  ------------------
 1007|       |
 1008|    592|    if ((data - page) + deleted_row_bitmap_offset + required_bytes > page_size) {
  ------------------
  |  Branch (1008:9): [True: 137, False: 455]
  ------------------
 1009|    137|        return READSTAT_ERROR_PARSE;
 1010|    137|    }
 1011|    455|    *deleted_row_bitmap = (const unsigned char *)data + deleted_row_bitmap_offset;
 1012|    455|    return READSTAT_OK;
 1013|    592|}
readstat_sas7bdat_read.c:sas7bdat_parse_rows:
  530|  2.46k|        const unsigned char *deleted_bitmap, sas7bdat_ctx_t *ctx) {
  531|  2.46k|    readstat_error_t retval = READSTAT_OK;
  532|  2.46k|    int i;
  533|  2.46k|    size_t row_offset=0;
  534|   280k|    for (i=0; i<ctx->page_row_count && ctx->parsed_row_count < ctx->row_limit; i++) {
  ------------------
  |  Branch (534:15): [True: 278k, False: 2.19k]
  |  Branch (534:40): [True: 278k, False: 68]
  ------------------
  535|   278k|        if (row_offset + ctx->row_length > len) {
  ------------------
  |  Branch (535:13): [True: 104, False: 278k]
  ------------------
  536|    104|            retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  537|    104|            goto cleanup;
  538|    104|        }
  539|   278k|        if (deleted_bitmap != NULL && sas7bdat_read_bitmap(deleted_bitmap, i)) {
  ------------------
  |  Branch (539:13): [True: 85.4k, False: 192k]
  |  Branch (539:39): [True: 25.0k, False: 60.4k]
  ------------------
  540|  25.0k|            if ((retval = sas7bdat_register_deleted_row(ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (540:17): [True: 4, False: 25.0k]
  ------------------
  541|      4|                goto cleanup;
  542|      4|            }
  543|   253k|        } else if ((retval = sas7bdat_parse_single_row(&data[row_offset], ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (543:20): [True: 99, False: 253k]
  ------------------
  544|     99|            goto cleanup;
  545|     99|        }
  546|       |
  547|   278k|        row_offset += ctx->row_length;
  548|   278k|    }
  549|       |
  550|  2.46k|cleanup:
  551|  2.46k|    return retval;
  552|  2.46k|}
readstat_sas7bdat_read.c:sas7bdat_read_bitmap:
  522|  85.4k|static unsigned char sas7bdat_read_bitmap(const unsigned char *bitmap, int index) {
  523|  85.4k|    unsigned char current_byte = bitmap[index / CHAR_BIT];
  524|  85.4k|    unsigned char mask = 1 << (CHAR_BIT - 1 - index % CHAR_BIT);
  525|       |
  526|  85.4k|    return current_byte & mask;
  527|  85.4k|}
readstat_sas7bdat_read.c:sas7bdat_submit_columns_if_needed:
  845|  5.19k|static readstat_error_t sas7bdat_submit_columns_if_needed(sas7bdat_ctx_t *ctx, int compressed) {
  846|  5.19k|    readstat_error_t retval = READSTAT_OK;
  847|  5.19k|    if (!ctx->did_submit_columns) {
  ------------------
  |  Branch (847:9): [True: 1.45k, False: 3.74k]
  ------------------
  848|  1.45k|        if ((retval = sas7bdat_submit_columns(ctx, compressed)) != READSTAT_OK) {
  ------------------
  |  Branch (848:13): [True: 237, False: 1.22k]
  ------------------
  849|    237|            goto cleanup;
  850|    237|        }
  851|  1.22k|        ctx->did_submit_columns = 1;
  852|  1.22k|    }
  853|  5.19k|cleanup:
  854|  5.19k|    return retval;
  855|  5.19k|}
readstat_sas7bdat_read.c:sas7bdat_submit_columns:
  784|  1.45k|static readstat_error_t sas7bdat_submit_columns(sas7bdat_ctx_t *ctx, int compressed) {
  785|  1.45k|    readstat_error_t retval = READSTAT_OK;
  786|  1.45k|    if (ctx->handle.metadata) {
  ------------------
  |  Branch (786:9): [True: 1.45k, False: 0]
  ------------------
  787|  1.45k|        readstat_metadata_t metadata = {
  788|  1.45k|            .row_count = ctx->row_limit,
  789|  1.45k|            .var_count = ctx->column_count,
  790|  1.45k|            .table_name = ctx->table_name,
  791|  1.45k|            .file_label = ctx->file_label,
  792|  1.45k|            .file_encoding = ctx->input_encoding, /* orig encoding? */
  793|  1.45k|            .creation_time = ctx->ctime,
  794|  1.45k|            .modified_time = ctx->mtime,
  795|  1.45k|            .file_format_version = ctx->version,
  796|  1.45k|            .compression = READSTAT_COMPRESS_NONE,
  797|  1.45k|            .endianness = ctx->little_endian ? READSTAT_ENDIAN_LITTLE : READSTAT_ENDIAN_BIG,
  ------------------
  |  Branch (797:27): [True: 319, False: 1.13k]
  ------------------
  798|  1.45k|            .is64bit = ctx->u64
  799|  1.45k|        };
  800|  1.45k|        if (compressed) {
  ------------------
  |  Branch (800:13): [True: 421, False: 1.03k]
  ------------------
  801|    421|            if (ctx->rdc_compression) {
  ------------------
  |  Branch (801:17): [True: 52, False: 369]
  ------------------
  802|     52|                metadata.compression = READSTAT_COMPRESS_BINARY;
  803|    369|            } else {
  804|    369|                metadata.compression = READSTAT_COMPRESS_ROWS;
  805|    369|            }
  806|    421|        }
  807|  1.45k|        if (ctx->handle.metadata(&metadata, ctx->user_ctx) != READSTAT_HANDLER_OK) {
  ------------------
  |  Branch (807:13): [True: 0, False: 1.45k]
  ------------------
  808|      0|            retval = READSTAT_ERROR_USER_ABORT;
  809|      0|            goto cleanup;
  810|      0|        }
  811|  1.45k|    }
  812|  1.45k|    if (ctx->column_count == 0)
  ------------------
  |  Branch (812:9): [True: 985, False: 472]
  ------------------
  813|    985|        goto cleanup;
  814|       |
  815|    472|    if ((ctx->variables = readstat_calloc(ctx->column_count, sizeof(readstat_variable_t *))) == NULL) {
  ------------------
  |  Branch (815:9): [True: 0, False: 472]
  ------------------
  816|      0|        retval = READSTAT_ERROR_MALLOC;
  817|      0|        goto cleanup;
  818|      0|    }
  819|    472|    int i;
  820|    472|    int index_after_skipping = 0;
  821|  1.86M|    for (i=0; i<ctx->column_count; i++) {
  ------------------
  |  Branch (821:15): [True: 1.86M, False: 235]
  ------------------
  822|  1.86M|        ctx->variables[i] = sas7bdat_init_variable(ctx, i, index_after_skipping, &retval);
  823|  1.86M|        if (ctx->variables[i] == NULL)
  ------------------
  |  Branch (823:13): [True: 237, False: 1.86M]
  ------------------
  824|    237|            break;
  825|       |
  826|  1.86M|        int cb_retval = READSTAT_HANDLER_OK;
  827|  1.86M|        if (ctx->handle.variable) {
  ------------------
  |  Branch (827:13): [True: 1.86M, False: 0]
  ------------------
  828|  1.86M|            cb_retval = ctx->handle.variable(i, ctx->variables[i], ctx->variables[i]->format, ctx->user_ctx);
  829|  1.86M|        }
  830|  1.86M|        if (cb_retval == READSTAT_HANDLER_ABORT) {
  ------------------
  |  Branch (830:13): [True: 0, False: 1.86M]
  ------------------
  831|      0|            retval = READSTAT_ERROR_USER_ABORT;
  832|      0|            goto cleanup;
  833|      0|        }
  834|       |
  835|  1.86M|        if (cb_retval == READSTAT_HANDLER_SKIP_VARIABLE) {
  ------------------
  |  Branch (835:13): [True: 0, False: 1.86M]
  ------------------
  836|      0|            ctx->variables[i]->skip = 1;
  837|  1.86M|        } else {
  838|  1.86M|            index_after_skipping++;
  839|  1.86M|        }
  840|  1.86M|    }
  841|  1.45k|cleanup:
  842|  1.45k|    return retval;
  843|    472|}
readstat_sas7bdat_read.c:sas7bdat_init_variable:
  725|  1.86M|        int index_after_skipping, readstat_error_t *out_retval) {
  726|  1.86M|    readstat_error_t retval = READSTAT_OK;
  727|  1.86M|    readstat_variable_t *variable = readstat_calloc(1, sizeof(readstat_variable_t));
  728|       |
  729|  1.86M|    variable->index = i;
  730|  1.86M|    variable->index_after_skipping = index_after_skipping;
  731|  1.86M|    variable->type = ctx->col_info[i].type;
  732|  1.86M|    variable->storage_width = ctx->col_info[i].width;
  733|       |
  734|  1.86M|    if ((retval = sas7bdat_validate_column(&ctx->col_info[i])) != READSTAT_OK) {
  ------------------
  |  Branch (734:9): [True: 81, False: 1.86M]
  ------------------
  735|     81|        goto cleanup;
  736|     81|    }
  737|  1.86M|    if ((retval = sas7bdat_copy_text_ref(variable->name, sizeof(variable->name), 
  ------------------
  |  Branch (737:9): [True: 52, False: 1.86M]
  ------------------
  738|  1.86M|                    ctx->col_info[i].name_ref, ctx)) != READSTAT_OK) {
  739|     52|        goto cleanup;
  740|     52|    }
  741|  1.86M|    if ((retval = sas7bdat_copy_text_ref(variable->format, sizeof(variable->format),
  ------------------
  |  Branch (741:9): [True: 53, False: 1.86M]
  ------------------
  742|  1.86M|                    ctx->col_info[i].format_ref, ctx)) != READSTAT_OK) {
  743|     53|        goto cleanup;
  744|     53|    }
  745|  1.86M|    size_t len = strlen(variable->format);
  746|  1.86M|    if (ctx->col_info[i].format_width) {
  ------------------
  |  Branch (746:9): [True: 531, False: 1.86M]
  ------------------
  747|    531|        len += snprintf(variable->format + len, sizeof(variable->format) - len,
  748|    531|                "%d", ctx->col_info[i].format_width);
  749|    531|    }
  750|  1.86M|    if (len && ctx->col_info[i].format_digits) {
  ------------------
  |  Branch (750:9): [True: 556, False: 1.86M]
  |  Branch (750:16): [True: 347, False: 209]
  ------------------
  751|    347|        len += snprintf(variable->format + len, sizeof(variable->format) - len,
  752|    347|                ".%d", ctx->col_info[i].format_digits);
  753|    347|    }
  754|  1.86M|    if (len) { // TODO where is the informat saved?
  ------------------
  |  Branch (754:9): [True: 556, False: 1.86M]
  ------------------
  755|    556|        readstat_variable_set_informat(variable, variable->format);
  756|    556|    }
  757|  1.86M|    if ((retval = sas7bdat_copy_text_ref(variable->label, sizeof(variable->label), 
  ------------------
  |  Branch (757:9): [True: 51, False: 1.86M]
  ------------------
  758|  1.86M|                    ctx->col_info[i].label_ref, ctx)) != READSTAT_OK) {
  759|     51|        goto cleanup;
  760|     51|    }
  761|       |
  762|  1.86M|cleanup:
  763|  1.86M|    if (retval != READSTAT_OK) {
  ------------------
  |  Branch (763:9): [True: 237, False: 1.86M]
  ------------------
  764|    237|        if (out_retval)
  ------------------
  |  Branch (764:13): [True: 237, False: 0]
  ------------------
  765|    237|            *out_retval = retval;
  766|       |
  767|    237|        if (retval == READSTAT_ERROR_CONVERT_BAD_STRING) {
  ------------------
  |  Branch (767:13): [True: 7, False: 230]
  ------------------
  768|      7|            if (ctx->handle.error) {
  ------------------
  |  Branch (768:17): [True: 0, False: 7]
  ------------------
  769|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf),
  770|      0|                        "ReadStat: Error converting variable #%d info to specified encoding: %s %s (%s)",
  771|      0|                        i, variable->name, variable->format, variable->label);
  772|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
  773|      0|            }
  774|      7|        }
  775|       |
  776|    237|        free(variable);
  777|       |
  778|    237|        return NULL;
  779|    237|    }
  780|       |
  781|  1.86M|    return variable;
  782|  1.86M|}
readstat_sas7bdat_read.c:sas7bdat_validate_column:
  710|  1.86M|static readstat_error_t sas7bdat_validate_column(col_info_t *col_info) {
  711|  1.86M|    if (col_info->type == READSTAT_TYPE_DOUBLE) {
  ------------------
  |  Branch (711:9): [True: 1.14k, False: 1.86M]
  ------------------
  712|  1.14k|        if (col_info->width > 8 || col_info->width < 3) {
  ------------------
  |  Branch (712:13): [True: 48, False: 1.09k]
  |  Branch (712:36): [True: 3, False: 1.09k]
  ------------------
  713|     51|            return READSTAT_ERROR_PARSE;
  714|     51|        }
  715|  1.14k|    }
  716|  1.86M|    if (col_info->type == READSTAT_TYPE_STRING) {
  ------------------
  |  Branch (716:9): [True: 1.86M, False: 1.09k]
  ------------------
  717|  1.86M|        if (col_info->width > INT16_MAX) {
  ------------------
  |  Branch (717:13): [True: 30, False: 1.86M]
  ------------------
  718|     30|            return READSTAT_ERROR_PARSE;
  719|     30|        }
  720|  1.86M|    }
  721|  1.86M|    return READSTAT_OK;
  722|  1.86M|}
readstat_sas7bdat_read.c:sas7bdat_update_progress:
  132|  6.31k|static readstat_error_t sas7bdat_update_progress(sas7bdat_ctx_t *ctx) {
  133|  6.31k|    readstat_io_t *io = ctx->io;
  134|  6.31k|    return io->update(ctx->file_size, ctx->handle.progress, ctx->user_ctx, io->io_ctx);
  135|  6.31k|}
readstat_sas7bdat_read.c:sas7bdat_ctx_free:
   98|  3.66k|static void sas7bdat_ctx_free(sas7bdat_ctx_t *ctx) {
   99|  3.66k|    int i;
  100|  3.66k|    if (ctx->text_blobs) {
  ------------------
  |  Branch (100:9): [True: 856, False: 2.80k]
  ------------------
  101|  3.21k|        for (i=0; i<ctx->text_blob_count; i++) {
  ------------------
  |  Branch (101:19): [True: 2.35k, False: 856]
  ------------------
  102|  2.35k|            free(ctx->text_blobs[i]);
  103|  2.35k|        }
  104|    856|        free(ctx->text_blobs);
  105|    856|        free(ctx->text_blob_lengths);
  106|    856|    }
  107|  3.66k|    if (ctx->variables) {
  ------------------
  |  Branch (107:9): [True: 472, False: 3.19k]
  ------------------
  108|  14.8M|        for (i=0; i<ctx->column_count; i++) {
  ------------------
  |  Branch (108:19): [True: 14.8M, False: 472]
  ------------------
  109|  14.8M|            if (ctx->variables[i])
  ------------------
  |  Branch (109:17): [True: 1.86M, False: 13.0M]
  ------------------
  110|  1.86M|                free(ctx->variables[i]);
  111|  14.8M|        }
  112|    472|        free(ctx->variables);
  113|    472|    }
  114|  3.66k|    if (ctx->col_info)
  ------------------
  |  Branch (114:9): [True: 619, False: 3.04k]
  ------------------
  115|    619|        free(ctx->col_info);
  116|       |
  117|  3.66k|    if (ctx->scratch_buffer)
  ------------------
  |  Branch (117:9): [True: 633, False: 3.03k]
  ------------------
  118|    633|        free(ctx->scratch_buffer);
  119|       |
  120|  3.66k|    if (ctx->page)
  ------------------
  |  Branch (120:9): [True: 3.16k, False: 501]
  ------------------
  121|  3.16k|        free(ctx->page);
  122|       |
  123|  3.66k|    if (ctx->row)
  ------------------
  |  Branch (123:9): [True: 1.10k, False: 2.56k]
  ------------------
  124|  1.10k|        free(ctx->row);
  125|       |
  126|  3.66k|    if (ctx->converter)
  ------------------
  |  Branch (126:9): [True: 2.27k, False: 1.38k]
  ------------------
  127|  2.27k|        iconv_close(ctx->converter);
  128|       |
  129|  3.66k|    free(ctx);
  130|  3.66k|}

sas_rle_decompress:
   47|    475|        const void *input_buf, size_t input_len) {
   48|    475|    unsigned char *buffer = (unsigned char *)output_buf;
   49|    475|    unsigned char *output = buffer;
   50|    475|    size_t output_written = 0;
   51|       |
   52|    475|    const unsigned char *input = (const unsigned char *)input_buf;
   53|       |
   54|  43.8k|    while (input < (const unsigned char *)input_buf + input_len) {
  ------------------
  |  Branch (54:12): [True: 43.5k, False: 355]
  ------------------
   55|  43.5k|        unsigned char control = *input++;
   56|  43.5k|        unsigned char command = (control & 0xF0) >> 4;
   57|  43.5k|        unsigned char length = (control & 0x0F);
   58|  43.5k|        int copy_len = 0;
   59|  43.5k|        int insert_len = 0;
   60|  43.5k|        unsigned char insert_byte = '\0';
   61|  43.5k|        if (input + command_lengths[command] > (const unsigned char *)input_buf + input_len) {
  ------------------
  |  Branch (61:13): [True: 4, False: 43.5k]
  ------------------
   62|      4|            return -1;
   63|      4|        }
   64|  43.5k|        switch (command) {
   65|  5.08k|            case SAS_RLE_COMMAND_COPY64:
  ------------------
  |  |   13|  5.08k|#define SAS_RLE_COMMAND_COPY64          0
  ------------------
  |  Branch (65:13): [True: 5.08k, False: 38.4k]
  ------------------
   66|  5.08k|                copy_len = (*input++) + 64 + length * 256;
   67|  5.08k|                break;
   68|    176|            case SAS_RLE_COMMAND_COPY64_PLUS_4096:
  ------------------
  |  |   14|    176|#define SAS_RLE_COMMAND_COPY64_PLUS_4096 1
  ------------------
  |  Branch (68:13): [True: 176, False: 43.3k]
  ------------------
   69|    176|                copy_len = (*input++) + 64 + length * 256 + 4096;
   70|    176|                break;
   71|    438|            case SAS_RLE_COMMAND_COPY96: copy_len = length + 96; break;
  ------------------
  |  |   15|    438|#define SAS_RLE_COMMAND_COPY96          2
  ------------------
  |  Branch (71:13): [True: 438, False: 43.1k]
  ------------------
   72|    718|            case SAS_RLE_COMMAND_INSERT_BYTE18:
  ------------------
  |  |   16|    718|#define SAS_RLE_COMMAND_INSERT_BYTE18   4
  ------------------
  |  Branch (72:13): [True: 718, False: 42.8k]
  ------------------
   73|    718|                insert_len = (*input++) + 18 + length * 256;
   74|    718|                insert_byte = *input++;
   75|    718|                break;
   76|    961|            case SAS_RLE_COMMAND_INSERT_AT17:
  ------------------
  |  |   17|    961|#define SAS_RLE_COMMAND_INSERT_AT17     5
  ------------------
  |  Branch (76:13): [True: 961, False: 42.5k]
  ------------------
   77|    961|                insert_len = (*input++) + 17 + length * 256;
   78|    961|                insert_byte = '@';
   79|    961|                break;
   80|  1.53k|            case SAS_RLE_COMMAND_INSERT_BLANK17:
  ------------------
  |  |   18|  1.53k|#define SAS_RLE_COMMAND_INSERT_BLANK17  6
  ------------------
  |  Branch (80:13): [True: 1.53k, False: 42.0k]
  ------------------
   81|  1.53k|                insert_len = (*input++) + 17 + length * 256;
   82|  1.53k|                insert_byte = ' ';
   83|  1.53k|                break;
   84|  1.90k|            case SAS_RLE_COMMAND_INSERT_ZERO17:
  ------------------
  |  |   19|  1.90k|#define SAS_RLE_COMMAND_INSERT_ZERO17   7
  ------------------
  |  Branch (84:13): [True: 1.90k, False: 41.6k]
  ------------------
   85|  1.90k|                insert_len = (*input++) + 17 + length * 256;
   86|  1.90k|                insert_byte = '\0';
   87|  1.90k|                break;
   88|  1.25k|            case SAS_RLE_COMMAND_COPY1:  copy_len = length + 1; break;
  ------------------
  |  |   20|  1.25k|#define SAS_RLE_COMMAND_COPY1           8
  ------------------
  |  Branch (88:13): [True: 1.25k, False: 42.2k]
  ------------------
   89|  9.90k|            case SAS_RLE_COMMAND_COPY17: copy_len = length + 17; break;
  ------------------
  |  |   21|  9.90k|#define SAS_RLE_COMMAND_COPY17          9
  ------------------
  |  Branch (89:13): [True: 9.90k, False: 33.6k]
  ------------------
   90|    470|            case SAS_RLE_COMMAND_COPY33: copy_len = length + 33; break;
  ------------------
  |  |   22|    470|#define SAS_RLE_COMMAND_COPY33         10
  ------------------
  |  Branch (90:13): [True: 470, False: 43.0k]
  ------------------
   91|    346|            case SAS_RLE_COMMAND_COPY49: copy_len = length + 49; break;
  ------------------
  |  |   23|    346|#define SAS_RLE_COMMAND_COPY49         11
  ------------------
  |  Branch (91:13): [True: 346, False: 43.1k]
  ------------------
   92|  1.13k|            case SAS_RLE_COMMAND_INSERT_BYTE3:
  ------------------
  |  |   24|  1.13k|#define SAS_RLE_COMMAND_INSERT_BYTE3   12
  ------------------
  |  Branch (92:13): [True: 1.13k, False: 42.4k]
  ------------------
   93|  1.13k|                insert_byte = *input++;
   94|  1.13k|                insert_len = length + 3;
   95|  1.13k|                break;
   96|  1.18k|            case SAS_RLE_COMMAND_INSERT_AT2:
  ------------------
  |  |   25|  1.18k|#define SAS_RLE_COMMAND_INSERT_AT2     13
  ------------------
  |  Branch (96:13): [True: 1.18k, False: 42.3k]
  ------------------
   97|  1.18k|                insert_byte = '@';
   98|  1.18k|                insert_len = length + 2;
   99|  1.18k|                break;
  100|  7.84k|            case SAS_RLE_COMMAND_INSERT_BLANK2:
  ------------------
  |  |   26|  7.84k|#define SAS_RLE_COMMAND_INSERT_BLANK2  14
  ------------------
  |  Branch (100:13): [True: 7.84k, False: 35.6k]
  ------------------
  101|  7.84k|                insert_byte = ' ';
  102|  7.84k|                insert_len = length + 2;
  103|  7.84k|                break;
  104|  7.49k|            case SAS_RLE_COMMAND_INSERT_ZERO2:
  ------------------
  |  |   27|  7.49k|#define SAS_RLE_COMMAND_INSERT_ZERO2   15
  ------------------
  |  Branch (104:13): [True: 7.49k, False: 36.0k]
  ------------------
  105|  7.49k|                insert_byte = '\0';
  106|  7.49k|                insert_len = length + 2;
  107|  7.49k|                break;
  108|  3.08k|            default:
  ------------------
  |  Branch (108:13): [True: 3.08k, False: 40.4k]
  ------------------
  109|       |                /* error out here? */
  110|  3.08k|                break;
  111|  43.5k|        }
  112|  43.5k|        if (copy_len) {
  ------------------
  |  Branch (112:13): [True: 17.6k, False: 25.8k]
  ------------------
  113|  17.6k|            if (output_written + copy_len > output_len) {
  ------------------
  |  Branch (113:17): [True: 20, False: 17.6k]
  ------------------
  114|     20|                return -1;
  115|     20|            }
  116|  17.6k|            if (input + copy_len > (const unsigned char *)input_buf + input_len) {
  ------------------
  |  Branch (116:17): [True: 84, False: 17.5k]
  ------------------
  117|     84|                return -1;
  118|     84|            }
  119|  17.5k|            if (output) {
  ------------------
  |  Branch (119:17): [True: 17.5k, False: 0]
  ------------------
  120|  17.5k|                memcpy(&output[output_written], input, copy_len);
  121|  17.5k|            }
  122|  17.5k|            input += copy_len;
  123|  17.5k|            output_written += copy_len;
  124|  17.5k|        }
  125|  43.4k|        if (insert_len) {
  ------------------
  |  Branch (125:13): [True: 22.7k, False: 20.6k]
  ------------------
  126|  22.7k|            if (output_written + insert_len > output_len) {
  ------------------
  |  Branch (126:17): [True: 12, False: 22.7k]
  ------------------
  127|     12|                return -1;
  128|     12|            }
  129|  22.7k|            if (output) {
  ------------------
  |  Branch (129:17): [True: 22.7k, False: 0]
  ------------------
  130|  22.7k|                memset(&output[output_written], insert_byte, insert_len);
  131|  22.7k|            }
  132|  22.7k|            output_written += insert_len;
  133|  22.7k|        }
  134|  43.4k|    }
  135|       |
  136|    355|    return output_written;
  137|    475|}

rt_open_handler:
    8|  3.66k|int rt_open_handler(const char *path, void *io_ctx) {
    9|  3.66k|    return 0;
   10|  3.66k|}
rt_close_handler:
   12|  3.66k|int rt_close_handler(void *io_ctx) {
   13|  3.66k|    return 0;
   14|  3.66k|}
rt_seek_handler:
   17|  31.5k|        readstat_io_flags_t whence, void *io_ctx) {
   18|  31.5k|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   19|  31.5k|    readstat_off_t newpos = -1;
   20|  31.5k|    if (whence == READSTAT_SEEK_SET) {
  ------------------
  |  Branch (20:9): [True: 21.0k, False: 10.5k]
  ------------------
   21|  21.0k|        newpos = offset;
   22|  21.0k|    } else if (whence == READSTAT_SEEK_CUR) {
  ------------------
  |  Branch (22:16): [True: 6.83k, False: 3.66k]
  ------------------
   23|  6.83k|        newpos = buffer_ctx->pos + offset;
   24|  6.83k|    } else if (whence == READSTAT_SEEK_END) {
  ------------------
  |  Branch (24:16): [True: 3.66k, False: 0]
  ------------------
   25|  3.66k|        newpos = buffer_ctx->buffer->used + offset;
   26|  3.66k|    }
   27|       |
   28|  31.5k|    if (newpos < 0)
  ------------------
  |  Branch (28:9): [True: 127, False: 31.3k]
  ------------------
   29|    127|        return -1;
   30|       |
   31|  31.3k|    if (newpos > buffer_ctx->buffer->used)
  ------------------
  |  Branch (31:9): [True: 224, False: 31.1k]
  ------------------
   32|    224|        return -1;
   33|       |
   34|  31.1k|    buffer_ctx->pos = newpos;
   35|  31.1k|    return newpos;
   36|  31.3k|}
rt_read_handler:
   38|  56.4k|ssize_t rt_read_handler(void *buf, size_t nbytes, void *io_ctx) {
   39|  56.4k|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   40|  56.4k|    ssize_t bytes_copied = 0;
   41|  56.4k|    ssize_t bytes_left = buffer_ctx->buffer->used - buffer_ctx->pos;
   42|  56.4k|    if (nbytes <= bytes_left) {
  ------------------
  |  Branch (42:9): [True: 55.9k, False: 502]
  ------------------
   43|  55.9k|        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, nbytes);
   44|  55.9k|        bytes_copied = nbytes;
   45|  55.9k|    } else if (bytes_left > 0) {
  ------------------
  |  Branch (45:16): [True: 307, False: 195]
  ------------------
   46|    307|        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, bytes_left);
   47|    307|        bytes_copied = bytes_left;
   48|    307|    }
   49|  56.4k|    buffer_ctx->pos += bytes_copied;
   50|  56.4k|    return bytes_copied;
   51|  56.4k|}
rt_update_handler:
   54|  6.31k|        void *user_ctx, void *io_ctx) {
   55|  6.31k|    if (!progress_handler)
  ------------------
  |  Branch (55:9): [True: 6.31k, False: 0]
  ------------------
   56|  6.31k|        return READSTAT_OK;
   57|       |
   58|      0|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   59|       |
   60|      0|    if (progress_handler(1.0 * buffer_ctx->pos / buffer_ctx->buffer->used, user_ctx))
  ------------------
  |  Branch (60:9): [True: 0, False: 0]
  ------------------
   61|      0|        return READSTAT_ERROR_USER_ABORT;
   62|       |
   63|      0|    return READSTAT_OK;
   64|      0|}

