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

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

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

readstat_convert:
    7|  6.11M|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|  6.16M|    while (src_len && (src[src_len-1] == ' ' || src[src_len-1] == '\0')) {
  ------------------
  |  Branch (10:12): [True: 61.0k, False: 6.09M]
  |  Branch (10:24): [True: 3.72k, False: 57.3k]
  |  Branch (10:49): [True: 38.5k, False: 18.8k]
  ------------------
   11|  42.2k|        src_len--;
   12|  42.2k|    }
   13|  6.11M|    if (dst_len == 0) {
  ------------------
  |  Branch (13:9): [True: 0, False: 6.11M]
  ------------------
   14|      0|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   15|  6.11M|    } else if (converter) {
  ------------------
  |  Branch (15:16): [True: 3.29M, False: 2.81M]
  ------------------
   16|  3.29M|        size_t dst_left = dst_len - 1;
   17|  3.29M|        char *dst_end = dst;
   18|  3.29M|        size_t status = iconv(converter, (readstat_iconv_inbuf_t)&src, &src_len, &dst_end, &dst_left);
   19|  3.29M|        if (status == (size_t)-1) {
  ------------------
  |  Branch (19:13): [True: 1.42k, False: 3.29M]
  ------------------
   20|  1.42k|            if (errno == E2BIG) {
  ------------------
  |  Branch (20:17): [True: 6, False: 1.41k]
  ------------------
   21|      6|                return READSTAT_ERROR_CONVERT_LONG_STRING;
   22|  1.41k|            } else if (errno == EILSEQ) {
  ------------------
  |  Branch (22:24): [True: 26, False: 1.39k]
  ------------------
   23|     26|                return READSTAT_ERROR_CONVERT_BAD_STRING;
   24|  1.39k|            } else if (errno != EINVAL) { /* EINVAL indicates improper truncation; accept it */
  ------------------
  |  Branch (24:24): [True: 0, False: 1.39k]
  ------------------
   25|      0|                return READSTAT_ERROR_CONVERT;
   26|      0|            }
   27|  1.42k|        }
   28|  3.29M|        dst[dst_len - dst_left - 1] = '\0';
   29|  3.29M|    } else if (src_len + 1 > dst_len) {
  ------------------
  |  Branch (29:16): [True: 18, False: 2.81M]
  ------------------
   30|     18|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   31|  2.81M|    } else {
   32|  2.81M|        memcpy(dst, src, src_len);
   33|  2.81M|        dst[src_len] = '\0';
   34|  2.81M|    }
   35|  6.11M|    return READSTAT_OK;
   36|  6.11M|}

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

readstat_malloc:
   10|  5.56k|void *readstat_malloc(size_t len) {
   11|  5.56k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|  11.1k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (11:9): [True: 0, False: 5.56k]
  |  Branch (11:34): [True: 0, False: 5.56k]
  ------------------
   12|      0|        return NULL;
   13|      0|    }
   14|  5.56k|    return malloc(len);
   15|  5.56k|}
readstat_calloc:
   17|  3.11M|void *readstat_calloc(size_t count, size_t size) {
   18|  3.11M|    if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  6.23M|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  6.23M|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  3.11M|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (18:9): [True: 0, False: 3.11M]
  |  Branch (18:36): [True: 0, False: 3.11M]
  |  Branch (18:62): [True: 0, False: 3.11M]
  ------------------
   19|      0|        return NULL;
   20|      0|    }
   21|  3.11M|    if (count == 0 || size == 0) {
  ------------------
  |  Branch (21:9): [True: 0, False: 3.11M]
  |  Branch (21:23): [True: 0, False: 3.11M]
  ------------------
   22|      0|        return NULL;
   23|      0|    }
   24|  3.11M|    return calloc(count, size);
   25|  3.11M|}
readstat_realloc:
   27|   235k|void *readstat_realloc(void *ptr, size_t len) {
   28|   235k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|   471k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (28:9): [True: 101, False: 235k]
  |  Branch (28:34): [True: 2, False: 235k]
  ------------------
   29|    103|        if (ptr)
  ------------------
  |  Branch (29:13): [True: 54, False: 49]
  ------------------
   30|     54|            free(ptr);
   31|    103|        return NULL;
   32|    103|    }
   33|   235k|    return realloc(ptr, len);
   34|   235k|}

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

sas_read8:
  139|  50.7k|uint64_t sas_read8(const char *data, int bswap) {
  140|  50.7k|    uint64_t tmp;
  141|  50.7k|    memcpy(&tmp, data, 8);
  142|  50.7k|    return bswap ? byteswap8(tmp) : tmp;
  ------------------
  |  Branch (142:12): [True: 45.3k, False: 5.47k]
  ------------------
  143|  50.7k|}
sas_read4:
  145|   227k|uint32_t sas_read4(const char *data, int bswap) {
  146|   227k|    uint32_t tmp;
  147|   227k|    memcpy(&tmp, data, 4);
  148|   227k|    return bswap ? byteswap4(tmp) : tmp;
  ------------------
  |  Branch (148:12): [True: 174k, False: 53.5k]
  ------------------
  149|   227k|}
sas_read2:
  151|   220k|uint16_t sas_read2(const char *data, int bswap) {
  152|   220k|    uint16_t tmp;
  153|   220k|    memcpy(&tmp, data, 2);
  154|   220k|    return bswap ? byteswap2(tmp) : tmp;
  ------------------
  |  Branch (154:12): [True: 82.8k, False: 137k]
  ------------------
  155|   220k|}
sas_subheader_remainder:
  157|  6.13k|size_t sas_subheader_remainder(size_t len, size_t signature_len) {
  158|  6.13k|    return len - (4+2*signature_len);
  159|  6.13k|}
sas_read_header:
  162|  3.56k|        readstat_error_handler error_handler, void *user_ctx) {
  163|  3.56k|    sas_header_start_t  header_start;
  164|  3.56k|    sas_header_end_t    header_end;
  165|  3.56k|    int retval = READSTAT_OK;
  166|  3.56k|    char error_buf[1024];
  167|  3.56k|    time_t epoch = sas_epoch();
  168|       |
  169|  3.56k|    if (io->read(&header_start, sizeof(sas_header_start_t), io->io_ctx) < sizeof(sas_header_start_t)) {
  ------------------
  |  Branch (169:9): [True: 20, False: 3.54k]
  ------------------
  170|     20|        retval = READSTAT_ERROR_READ;
  171|     20|        goto cleanup;
  172|     20|    }
  173|  3.54k|    if (memcmp(header_start.magic, sas7bdat_magic_number, sizeof(sas7bdat_magic_number)) != 0 &&
  ------------------
  |  Branch (173:9): [True: 1.65k, False: 1.88k]
  ------------------
  174|  1.65k|            memcmp(header_start.magic, sas7bcat_magic_number, sizeof(sas7bcat_magic_number)) != 0) {
  ------------------
  |  Branch (174:13): [True: 132, False: 1.52k]
  ------------------
  175|    132|        retval = READSTAT_ERROR_PARSE;
  176|    132|        goto cleanup;
  177|    132|    }
  178|  3.41k|    if (header_start.a1 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   87|  3.41k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (178:9): [True: 7, False: 3.40k]
  ------------------
  179|      7|        hinfo->pad1 = 4;
  180|      7|    }
  181|  3.41k|    if (header_start.a2 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   87|  3.41k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (181:9): [True: 1.58k, False: 1.83k]
  ------------------
  182|  1.58k|        hinfo->u64 = 1;
  183|  1.58k|    }
  184|  3.41k|    int bswap = 0;
  185|  3.41k|    if (header_start.endian == SAS_ENDIAN_BIG) {
  ------------------
  |  |   80|  3.41k|#define SAS_ENDIAN_BIG       0x00
  ------------------
  |  Branch (185:9): [True: 2.67k, False: 739]
  ------------------
  186|  2.67k|        bswap = machine_is_little_endian();
  187|  2.67k|        hinfo->little_endian = 0;
  188|  2.67k|    } else if (header_start.endian == SAS_ENDIAN_LITTLE) {
  ------------------
  |  |   81|    739|#define SAS_ENDIAN_LITTLE    0x01
  ------------------
  |  Branch (188:16): [True: 727, False: 12]
  ------------------
  189|    727|        bswap = !machine_is_little_endian();
  190|    727|        hinfo->little_endian = 1;
  191|    727|    } else {
  192|     12|        retval = READSTAT_ERROR_PARSE;
  193|     12|        goto cleanup;
  194|     12|    }
  195|  3.40k|    int i;
  196|  22.4k|    for (i=0; i<sizeof(_charset_table)/sizeof(_charset_table[0]); i++) {
  ------------------
  |  Branch (196:15): [True: 22.4k, False: 7]
  ------------------
  197|  22.4k|        if (header_start.encoding == _charset_table[i].code) {
  ------------------
  |  Branch (197:13): [True: 3.39k, False: 19.0k]
  ------------------
  198|  3.39k|            hinfo->encoding = _charset_table[i].name;
  199|  3.39k|            break;
  200|  3.39k|        }
  201|  22.4k|    }
  202|  3.40k|    if (hinfo->encoding == NULL) {
  ------------------
  |  Branch (202:9): [True: 7, False: 3.39k]
  ------------------
  203|      7|        if (error_handler) {
  ------------------
  |  Branch (203:13): [True: 0, False: 7]
  ------------------
  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|      7|        retval = READSTAT_ERROR_UNSUPPORTED_CHARSET;
  208|      7|        goto cleanup;
  209|      7|    }
  210|  3.39k|    memcpy(hinfo->table_name, header_start.table_name, sizeof(header_start.table_name));
  211|  3.39k|    if (io->seek(hinfo->pad1, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (211:9): [True: 5, False: 3.38k]
  ------------------
  212|      5|        retval = READSTAT_ERROR_SEEK;
  213|      5|        goto cleanup;
  214|      5|    }
  215|       |
  216|  3.38k|    double creation_time, modification_time, creation_time_diff, modification_time_diff;
  217|       |
  218|  3.38k|    if (io->read(&creation_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (218:9): [True: 12, False: 3.37k]
  ------------------
  219|     12|        retval = READSTAT_ERROR_READ;
  220|     12|        goto cleanup;
  221|     12|    }
  222|  3.37k|    if (bswap)
  ------------------
  |  Branch (222:9): [True: 2.65k, False: 721]
  ------------------
  223|  2.65k|        creation_time = byteswap_double(creation_time);
  224|       |
  225|  3.37k|    if (io->read(&modification_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (225:9): [True: 6, False: 3.37k]
  ------------------
  226|      6|        retval = READSTAT_ERROR_READ;
  227|      6|        goto cleanup;
  228|      6|    }
  229|  3.37k|    if (bswap)
  ------------------
  |  Branch (229:9): [True: 2.65k, False: 720]
  ------------------
  230|  2.65k|        modification_time = byteswap_double(modification_time);
  231|       |
  232|  3.37k|    if (io->read(&creation_time_diff, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (232:9): [True: 6, False: 3.36k]
  ------------------
  233|      6|        retval = READSTAT_ERROR_READ;
  234|      6|        goto cleanup;
  235|      6|    }
  236|  3.36k|    if (bswap)
  ------------------
  |  Branch (236:9): [True: 2.64k, False: 717]
  ------------------
  237|  2.64k|        creation_time_diff = byteswap_double(creation_time_diff);
  238|       |    
  239|  3.36k|    if (io->read(&modification_time_diff, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (239:9): [True: 6, False: 3.35k]
  ------------------
  240|      6|        retval = READSTAT_ERROR_READ;
  241|      6|        goto cleanup;
  242|      6|    }
  243|  3.35k|    if (bswap)
  ------------------
  |  Branch (243:9): [True: 2.64k, False: 716]
  ------------------
  244|  2.64k|        modification_time_diff = byteswap_double(modification_time_diff);
  245|       |    
  246|  3.35k|    hinfo->creation_time = sas_convert_time(creation_time, creation_time_diff, epoch);
  247|  3.35k|    hinfo->modification_time = sas_convert_time(modification_time, modification_time_diff, epoch);
  248|       |
  249|  3.35k|    uint32_t header_size, page_size;
  250|       |
  251|  3.35k|    if (io->read(&header_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (251:9): [True: 8, False: 3.35k]
  ------------------
  252|      8|        retval = READSTAT_ERROR_READ;
  253|      8|        goto cleanup;
  254|      8|    }
  255|  3.35k|    if (io->read(&page_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (255:9): [True: 3, False: 3.34k]
  ------------------
  256|      3|        retval = READSTAT_ERROR_READ;
  257|      3|        goto cleanup;
  258|      3|    }
  259|       |
  260|  3.34k|    hinfo->header_size = bswap ? byteswap4(header_size) : header_size;
  ------------------
  |  Branch (260:26): [True: 2.63k, False: 713]
  ------------------
  261|  3.34k|    hinfo->page_size = bswap ? byteswap4(page_size) : page_size;
  ------------------
  |  Branch (261:24): [True: 2.63k, False: 713]
  ------------------
  262|       |
  263|  3.34k|    if (hinfo->header_size < 1024 || hinfo->page_size < 1024) {
  ------------------
  |  Branch (263:9): [True: 11, False: 3.33k]
  |  Branch (263:38): [True: 13, False: 3.32k]
  ------------------
  264|     24|        retval = READSTAT_ERROR_PARSE;
  265|     24|        goto cleanup;
  266|     24|    }
  267|  3.32k|    if (hinfo->header_size > (1<<24) || hinfo->page_size > (1<<24)) {
  ------------------
  |  Branch (267:9): [True: 20, False: 3.30k]
  |  Branch (267:41): [True: 22, False: 3.28k]
  ------------------
  268|     42|        retval = READSTAT_ERROR_PARSE;
  269|     42|        goto cleanup;
  270|     42|    }
  271|       |
  272|  3.28k|    if (hinfo->u64) {
  ------------------
  |  Branch (272:9): [True: 1.53k, False: 1.75k]
  ------------------
  273|  1.53k|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_64BIT;
  ------------------
  |  |  121|  1.53k|#define SAS_PAGE_HEADER_SIZE_64BIT  40
  ------------------
  274|  1.53k|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_64BIT;
  ------------------
  |  |  118|  1.53k|#define SAS_SUBHEADER_POINTER_SIZE_64BIT    24
  ------------------
  275|  1.75k|    } else {
  276|  1.75k|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_32BIT;
  ------------------
  |  |  120|  1.75k|#define SAS_PAGE_HEADER_SIZE_32BIT  24
  ------------------
  277|  1.75k|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_32BIT;
  ------------------
  |  |  117|  1.75k|#define SAS_SUBHEADER_POINTER_SIZE_32BIT    12
  ------------------
  278|  1.75k|    }
  279|       |
  280|  3.28k|    if (hinfo->u64) {
  ------------------
  |  Branch (280:9): [True: 1.53k, False: 1.75k]
  ------------------
  281|  1.53k|        uint64_t page_count;
  282|  1.53k|        if (io->read(&page_count, sizeof(uint64_t), io->io_ctx) < sizeof(uint64_t)) {
  ------------------
  |  Branch (282:13): [True: 19, False: 1.51k]
  ------------------
  283|     19|            retval = READSTAT_ERROR_READ;
  284|     19|            goto cleanup;
  285|     19|        }
  286|  1.51k|        hinfo->page_count = bswap ? byteswap8(page_count) : page_count;
  ------------------
  |  Branch (286:29): [True: 1.40k, False: 103]
  ------------------
  287|  1.75k|    } else {
  288|  1.75k|        uint32_t page_count;
  289|  1.75k|        if (io->read(&page_count, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (289:13): [True: 17, False: 1.73k]
  ------------------
  290|     17|            retval = READSTAT_ERROR_READ;
  291|     17|            goto cleanup;
  292|     17|        }
  293|  1.73k|        hinfo->page_count = bswap ? byteswap4(page_count) : page_count;
  ------------------
  |  Branch (293:29): [True: 1.13k, False: 598]
  ------------------
  294|  1.73k|    }
  295|  3.24k|    if (hinfo->page_count > (1<<24)) {
  ------------------
  |  Branch (295:9): [True: 17, False: 3.22k]
  ------------------
  296|     17|        retval = READSTAT_ERROR_PARSE;
  297|     17|        goto cleanup;
  298|     17|    }
  299|       |    
  300|  3.22k|    if (io->seek(8, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (300:9): [True: 75, False: 3.15k]
  ------------------
  301|     75|        retval = READSTAT_ERROR_SEEK;
  302|     75|        if (error_handler) {
  ------------------
  |  Branch (302:13): [True: 0, False: 75]
  ------------------
  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|     75|        goto cleanup;
  307|     75|    }
  308|  3.15k|    if (io->read(&header_end, sizeof(sas_header_end_t), io->io_ctx) < sizeof(sas_header_end_t)) {
  ------------------
  |  Branch (308:9): [True: 16, False: 3.13k]
  ------------------
  309|     16|        retval = READSTAT_ERROR_READ;
  310|     16|        goto cleanup;
  311|     16|    }
  312|  3.13k|    char major, revision_tag;
  313|  3.13k|    int minor, revision;
  314|  3.13k|    if (sscanf(header_end.release, "%c.%04d%c%1d", &major, &minor, &revision_tag, &revision) != 4) {
  ------------------
  |  Branch (314:9): [True: 4, False: 3.13k]
  ------------------
  315|      4|        retval = READSTAT_ERROR_PARSE;
  316|      4|        goto cleanup;
  317|      4|    }
  318|       |
  319|  3.13k|    if (major >= '1' && major <= '9') {
  ------------------
  |  Branch (319:9): [True: 3.11k, False: 14]
  |  Branch (319:25): [True: 3.11k, False: 9]
  ------------------
  320|  3.11k|        hinfo->major_version = major - '0';
  321|  3.11k|    } else if (major == 'V') {
  ------------------
  |  Branch (321:16): [True: 3, False: 20]
  ------------------
  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|      3|        hinfo->major_version = 9;
  325|     20|    } else {
  326|     20|        retval = READSTAT_ERROR_PARSE;
  327|     20|        goto cleanup;
  328|     20|    }
  329|       |    // revision_tag is usually M, but J has been observed in the wild (not created with SAS?)
  330|  3.11k|    if (revision_tag != 'M' && revision_tag != 'J') {
  ------------------
  |  Branch (330:9): [True: 28, False: 3.08k]
  |  Branch (330:32): [True: 14, False: 14]
  ------------------
  331|     14|        retval = READSTAT_ERROR_PARSE;
  332|     14|        goto cleanup;
  333|     14|    }
  334|  3.09k|    hinfo->minor_version = minor;
  335|  3.09k|    hinfo->revision = revision;
  336|       |
  337|  3.09k|    if ((major == '8' || major == '9') && minor == 0 && revision == 0) {
  ------------------
  |  Branch (337:10): [True: 294, False: 2.80k]
  |  Branch (337:26): [True: 156, False: 2.64k]
  |  Branch (337:43): [True: 102, False: 348]
  |  Branch (337:57): [True: 81, False: 21]
  ------------------
  338|       |        /* A bit of a hack, but most SAS installations are running a minor update */
  339|     81|        hinfo->vendor = READSTAT_VENDOR_STAT_TRANSFER;
  340|  3.01k|    } else {
  341|  3.01k|        hinfo->vendor = READSTAT_VENDOR_SAS;
  342|  3.01k|    }
  343|  3.09k|    if (io->seek(hinfo->header_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (343:9): [True: 53, False: 3.04k]
  ------------------
  344|     53|        retval = READSTAT_ERROR_SEEK;
  345|     53|        if (error_handler) {
  ------------------
  |  Branch (345:13): [True: 0, False: 53]
  ------------------
  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|     53|        goto cleanup;
  351|     53|    }
  352|       |
  353|  3.56k|cleanup:
  354|  3.56k|    return retval;
  355|  3.09k|}
sas_validate_tag:
  507|  8.00k|readstat_error_t sas_validate_tag(char tag) {
  508|  8.00k|    if (tag == '_' || (tag >= 'A' && tag <= 'Z'))
  ------------------
  |  Branch (508:9): [True: 3.65k, False: 4.34k]
  |  Branch (508:24): [True: 1.55k, False: 2.79k]
  |  Branch (508:38): [True: 1.00k, False: 544]
  ------------------
  509|  4.65k|        return READSTAT_OK;
  510|       |
  511|  3.34k|    return READSTAT_ERROR_TAGGED_VALUE_IS_OUT_OF_RANGE;
  512|  8.00k|}
sas_assign_tag:
  514|  8.00k|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.00k|    if (tag == 0) {
  ------------------
  |  Branch (521:9): [True: 3.65k, False: 4.34k]
  ------------------
  522|  3.65k|        tag = '_';
  523|  4.34k|    } else if (tag >= 2 && tag < 28) {
  ------------------
  |  Branch (523:16): [True: 3.76k, False: 583]
  |  Branch (523:28): [True: 921, False: 2.84k]
  ------------------
  524|    921|        tag = 'A' + (tag - 2);
  525|    921|    }
  526|  8.00k|    if (sas_validate_tag(tag) == READSTAT_OK) {
  ------------------
  |  Branch (526:9): [True: 4.65k, False: 3.34k]
  ------------------
  527|  4.65k|        value->tag = tag;
  528|  4.65k|        value->is_tagged_missing = 1;
  529|  4.65k|    } else {
  530|  3.34k|        value->tag = 0;
  531|  3.34k|        value->is_system_missing = 1;
  532|  3.34k|    }
  533|  8.00k|}
readstat_sas.c:sas_epoch:
  123|  3.56k|static time_t sas_epoch(void) {
  124|  3.56k|    return - 3653 * 86400; // seconds between 01-01-1960 and 01-01-1970
  125|  3.56k|}
readstat_sas.c:sas_convert_time:
  127|  6.71k|static time_t sas_convert_time(double time, double time_diff, time_t epoch) {
  128|  6.71k|    time -= time_diff;
  129|  6.71k|    time += epoch;
  130|  6.71k|    if (isnan(time))
  ------------------
  |  Branch (130:9): [True: 777, False: 5.93k]
  ------------------
  131|    777|        return 0;
  132|  5.93k|    if (time > (double)LONG_MAX)
  ------------------
  |  Branch (132:9): [True: 1.58k, False: 4.35k]
  ------------------
  133|  1.58k|        return LONG_MAX;
  134|  4.35k|    if (time < (double)LONG_MIN)
  ------------------
  |  Branch (134:9): [True: 1.07k, False: 3.28k]
  ------------------
  135|  1.07k|        return LONG_MIN;
  136|  3.28k|    return time;
  137|  4.35k|}

readstat_parse_sas7bdat:
 1207|  3.56k|readstat_error_t readstat_parse_sas7bdat(readstat_parser_t *parser, const char *path, void *user_ctx) {
 1208|  3.56k|    int64_t last_examined_page_pass1 = 0;
 1209|  3.56k|    readstat_error_t retval = READSTAT_OK;
 1210|  3.56k|    readstat_io_t *io = parser->io;
 1211|       |
 1212|  3.56k|    sas7bdat_ctx_t  *ctx = calloc(1, sizeof(sas7bdat_ctx_t));
 1213|  3.56k|    sas_header_info_t  *hinfo = calloc(1, sizeof(sas_header_info_t));
 1214|       |
 1215|  3.56k|    if (ctx == NULL || hinfo == NULL) {
  ------------------
  |  Branch (1215:9): [True: 0, False: 3.56k]
  |  Branch (1215:24): [True: 0, False: 3.56k]
  ------------------
 1216|      0|        retval = READSTAT_ERROR_MALLOC;
 1217|      0|        goto cleanup;
 1218|      0|    }
 1219|       |
 1220|  3.56k|    ctx->handle = parser->handlers;
 1221|  3.56k|    ctx->input_encoding = parser->input_encoding;
 1222|  3.56k|    ctx->output_encoding = parser->output_encoding;
 1223|  3.56k|    ctx->user_ctx = user_ctx;
 1224|  3.56k|    ctx->io = parser->io;
 1225|  3.56k|    ctx->row_limit = parser->row_limit;
 1226|  3.56k|    if (parser->row_offset > 0)
  ------------------
  |  Branch (1226:9): [True: 0, False: 3.56k]
  ------------------
 1227|      0|        ctx->row_offset = parser->row_offset;
 1228|       |
 1229|  3.56k|    if (io->open(path, io->io_ctx) == -1) {
  ------------------
  |  Branch (1229:9): [True: 0, False: 3.56k]
  ------------------
 1230|      0|        retval = READSTAT_ERROR_OPEN;
 1231|      0|        goto cleanup;
 1232|      0|    }
 1233|       |
 1234|  3.56k|    if ((ctx->file_size = io->seek(0, READSTAT_SEEK_END, io->io_ctx)) == -1) {
  ------------------
  |  Branch (1234:9): [True: 0, False: 3.56k]
  ------------------
 1235|      0|        retval = READSTAT_ERROR_SEEK;
 1236|      0|        if (ctx->handle.error) {
  ------------------
  |  Branch (1236:13): [True: 0, False: 0]
  ------------------
 1237|      0|            snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: Failed to seek to end of file");
 1238|      0|            ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1239|      0|        }
 1240|      0|        goto cleanup;
 1241|      0|    }
 1242|       |
 1243|  3.56k|    if (io->seek(0, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (1243:9): [True: 0, False: 3.56k]
  ------------------
 1244|      0|        retval = READSTAT_ERROR_SEEK;
 1245|      0|        if (ctx->handle.error) {
  ------------------
  |  Branch (1245:13): [True: 0, False: 0]
  ------------------
 1246|      0|            snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: Failed to seek to beginning of file");
 1247|      0|            ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1248|      0|        }
 1249|      0|        goto cleanup;
 1250|      0|    }
 1251|       |
 1252|  3.56k|    if ((retval = sas_read_header(io, hinfo, ctx->handle.error, user_ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1252:9): [True: 518, False: 3.04k]
  ------------------
 1253|    518|        goto cleanup;
 1254|    518|    }
 1255|       |
 1256|  3.04k|    ctx->u64 = hinfo->u64;
 1257|  3.04k|    ctx->little_endian = hinfo->little_endian;
 1258|  3.04k|    ctx->vendor = hinfo->vendor;
 1259|  3.04k|    ctx->bswap = machine_is_little_endian() ^ hinfo->little_endian;
 1260|  3.04k|    ctx->header_size = hinfo->header_size;
 1261|  3.04k|    ctx->page_count = hinfo->page_count;
 1262|  3.04k|    ctx->page_size = hinfo->page_size;
 1263|  3.04k|    ctx->page_header_size = hinfo->page_header_size;
 1264|  3.04k|    ctx->subheader_pointer_size = hinfo->subheader_pointer_size;
 1265|  3.04k|    ctx->subheader_signature_size = ctx->u64 ? 8 : 4;
  ------------------
  |  Branch (1265:37): [True: 1.45k, False: 1.59k]
  ------------------
 1266|  3.04k|    ctx->ctime = hinfo->creation_time;
 1267|  3.04k|    ctx->mtime = hinfo->modification_time;
 1268|  3.04k|    ctx->version = hinfo->major_version;
 1269|  3.04k|    if (ctx->input_encoding == NULL) {
  ------------------
  |  Branch (1269:9): [True: 3.04k, False: 0]
  ------------------
 1270|  3.04k|        ctx->input_encoding = hinfo->encoding;
 1271|  3.04k|    }
 1272|  3.04k|    if ((ctx->page = readstat_malloc(ctx->page_size)) == NULL) {
  ------------------
  |  Branch (1272:9): [True: 0, False: 3.04k]
  ------------------
 1273|      0|        retval = READSTAT_ERROR_MALLOC;
 1274|      0|        goto cleanup;
 1275|      0|    }
 1276|       |
 1277|  3.04k|    if (ctx->input_encoding && ctx->output_encoding && strcmp(ctx->input_encoding, ctx->output_encoding) != 0) {
  ------------------
  |  Branch (1277:9): [True: 3.04k, False: 0]
  |  Branch (1277:32): [True: 3.04k, False: 0]
  |  Branch (1277:56): [True: 2.13k, False: 914]
  ------------------
 1278|  2.13k|        iconv_t converter = iconv_open(ctx->output_encoding, ctx->input_encoding);
 1279|  2.13k|        if (converter == (iconv_t)-1) {
  ------------------
  |  Branch (1279:13): [True: 1, False: 2.13k]
  ------------------
 1280|      1|            retval = READSTAT_ERROR_UNSUPPORTED_CHARSET;
 1281|      1|            goto cleanup;
 1282|      1|        }
 1283|  2.13k|        ctx->converter = converter;
 1284|  2.13k|    }
 1285|       |
 1286|  3.04k|    if ((retval = readstat_convert(ctx->table_name, sizeof(ctx->table_name),
  ------------------
  |  Branch (1286:9): [True: 2, False: 3.04k]
  ------------------
 1287|  3.04k|                hinfo->table_name, sizeof(hinfo->table_name), ctx->converter)) != READSTAT_OK) {
 1288|      2|        goto cleanup;
 1289|      2|    }
 1290|       |
 1291|  3.04k|    if ((retval = sas7bdat_parse_meta_pages_pass1(ctx, &last_examined_page_pass1)) != READSTAT_OK) {
  ------------------
  |  Branch (1291:9): [True: 238, False: 2.80k]
  ------------------
 1292|    238|        goto cleanup;
 1293|    238|    }
 1294|       |
 1295|  2.80k|    if ((retval = sas7bdat_parse_amd_pages_pass1(last_examined_page_pass1, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1295:9): [True: 974, False: 1.83k]
  ------------------
 1296|    974|        goto cleanup;
 1297|    974|    }
 1298|       |
 1299|  1.83k|    if (io->seek(ctx->header_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (1299:9): [True: 0, False: 1.83k]
  ------------------
 1300|      0|        retval = READSTAT_ERROR_SEEK;
 1301|      0|        if (ctx->handle.error) {
  ------------------
  |  Branch (1301:13): [True: 0, False: 0]
  ------------------
 1302|      0|            snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: Failed to seek to position %" PRId64, 
 1303|      0|                    ctx->header_size);
 1304|      0|            ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1305|      0|        }
 1306|      0|        goto cleanup;
 1307|      0|    }
 1308|       |
 1309|  1.83k|    if ((retval = sas7bdat_parse_all_pages_pass2(ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1309:9): [True: 1.49k, False: 332]
  ------------------
 1310|  1.49k|        goto cleanup;
 1311|  1.49k|    }
 1312|       |    
 1313|    332|    if ((retval = sas7bdat_submit_columns_if_needed(ctx, 0)) != READSTAT_OK) {
  ------------------
  |  Branch (1313:9): [True: 88, False: 244]
  ------------------
 1314|     88|        goto cleanup;
 1315|     88|    }
 1316|       |
 1317|    244|    if (ctx->handle.value && ctx->parsed_row_count != ctx->row_limit) {
  ------------------
  |  Branch (1317:9): [True: 244, False: 0]
  |  Branch (1317:30): [True: 99, False: 145]
  ------------------
 1318|     99|        retval = READSTAT_ERROR_ROW_COUNT_MISMATCH;
 1319|     99|        if (ctx->handle.error) {
  ------------------
  |  Branch (1319:13): [True: 0, False: 99]
  ------------------
 1320|      0|            snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: Expected %d rows in file, found %d",
 1321|      0|                    ctx->row_limit, ctx->parsed_row_count);
 1322|      0|            ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1323|      0|        }
 1324|     99|        goto cleanup;
 1325|     99|    }
 1326|       |
 1327|    145|    if ((retval = sas7bdat_update_progress(ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1327:9): [True: 0, False: 145]
  ------------------
 1328|      0|        goto cleanup;
 1329|      0|    }
 1330|       |
 1331|  3.56k|cleanup:
 1332|  3.56k|    io->close(io->io_ctx);
 1333|       |
 1334|  3.56k|    if (retval == READSTAT_ERROR_OPEN ||
  ------------------
  |  Branch (1334:9): [True: 0, False: 3.56k]
  ------------------
 1335|  3.56k|            retval == READSTAT_ERROR_READ ||
  ------------------
  |  Branch (1335:13): [True: 433, False: 3.13k]
  ------------------
 1336|  3.13k|            retval == READSTAT_ERROR_SEEK) {
  ------------------
  |  Branch (1336:13): [True: 371, False: 2.76k]
  ------------------
 1337|    804|        if (ctx->handle.error) {
  ------------------
  |  Branch (1337:13): [True: 0, False: 804]
  ------------------
 1338|      0|            snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: %s (retval = %d): %s (errno = %d)", 
 1339|      0|                    readstat_error_message(retval), retval, strerror(errno), errno);
 1340|      0|            ctx->handle.error(ctx->error_buf, user_ctx);
 1341|      0|        }
 1342|    804|    }
 1343|       |
 1344|  3.56k|    if (ctx)
  ------------------
  |  Branch (1344:9): [True: 3.56k, False: 0]
  ------------------
 1345|  3.56k|        sas7bdat_ctx_free(ctx);
 1346|  3.56k|    if (hinfo)
  ------------------
  |  Branch (1346:9): [True: 3.56k, False: 0]
  ------------------
 1347|  3.56k|        free(hinfo);
 1348|       |
 1349|  3.56k|    return retval;
 1350|    145|}
readstat_sas7bdat_read.c:sas7bdat_parse_meta_pages_pass1:
 1048|  3.04k|static readstat_error_t sas7bdat_parse_meta_pages_pass1(sas7bdat_ctx_t *ctx, int64_t *outLastExaminedPage) {
 1049|  3.04k|    readstat_error_t retval = READSTAT_OK;
 1050|  3.04k|    readstat_io_t *io = ctx->io;
 1051|  3.04k|    int64_t i;
 1052|       |
 1053|       |    /* look for META and MIX pages at beginning... */
 1054|  8.20k|    for (i=0; i<ctx->page_count; i++) {
  ------------------
  |  Branch (1054:15): [True: 5.97k, False: 2.23k]
  ------------------
 1055|  5.97k|        if (io->seek(ctx->header_size + i*ctx->page_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (1055:13): [True: 9, False: 5.96k]
  ------------------
 1056|      9|            retval = READSTAT_ERROR_SEEK;
 1057|      9|            if (ctx->handle.error) {
  ------------------
  |  Branch (1057:17): [True: 0, False: 9]
  ------------------
 1058|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: Failed to seek to position %" PRId64 
 1059|      0|                        " (= %" PRId64 " + %" PRId64 "*%" PRId64 ")",
 1060|      0|                        ctx->header_size + i*ctx->page_size, ctx->header_size, i, ctx->page_size);
 1061|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1062|      0|            }
 1063|      9|            goto cleanup;
 1064|      9|        }
 1065|       |
 1066|  5.96k|        readstat_off_t off = 0;
 1067|  5.96k|        if (ctx->u64)
  ------------------
  |  Branch (1067:13): [True: 3.89k, False: 2.06k]
  ------------------
 1068|  3.89k|            off = 16;
 1069|       |
 1070|  5.96k|        size_t head_len = off + 16 + 2;
 1071|  5.96k|        size_t tail_len = ctx->page_size - head_len;
 1072|       |
 1073|  5.96k|        if (io->read(ctx->page, head_len, io->io_ctx) < head_len) {
  ------------------
  |  Branch (1073:13): [True: 144, False: 5.82k]
  ------------------
 1074|    144|            retval = READSTAT_ERROR_READ;
 1075|    144|            goto cleanup;
 1076|    144|        }
 1077|       |
 1078|  5.82k|        uint16_t page_type = sas_read2(&ctx->page[off+16], ctx->bswap);
 1079|       |
 1080|  5.82k|        if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_DATA)
  ------------------
  |  |  112|  5.82k|#define SAS_PAGE_TYPE_MASK   0x0F00
  ------------------
                      if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_DATA)
  ------------------
  |  |  109|  5.82k|#define SAS_PAGE_TYPE_DATA   0x0100
  ------------------
  |  Branch (1080:13): [True: 572, False: 5.25k]
  ------------------
 1081|    572|            break;
 1082|  5.25k|        if ((page_type & SAS_PAGE_TYPE_COMP))
  ------------------
  |  |  115|  5.25k|#define SAS_PAGE_TYPE_COMP   0x9000
  ------------------
  |  Branch (1082:13): [True: 1.42k, False: 3.82k]
  ------------------
 1083|  1.42k|            continue;
 1084|       |
 1085|  3.82k|        if (io->read(ctx->page + head_len, tail_len, io->io_ctx) < tail_len) {
  ------------------
  |  Branch (1085:13): [True: 32, False: 3.79k]
  ------------------
 1086|     32|            retval = READSTAT_ERROR_READ;
 1087|     32|            goto cleanup;
 1088|     32|        }
 1089|       |
 1090|  3.79k|        if ((retval = sas7bdat_parse_page_pass1(ctx->page, ctx->page_size, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1090:13): [True: 53, False: 3.74k]
  ------------------
 1091|     53|            if (ctx->handle.error && retval != READSTAT_ERROR_USER_ABORT) {
  ------------------
  |  Branch (1091:17): [True: 0, False: 53]
  |  Branch (1091:38): [True: 0, False: 0]
  ------------------
 1092|      0|                int64_t pos = io->seek(0, READSTAT_SEEK_CUR, io->io_ctx);
 1093|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf), 
 1094|      0|                        "ReadStat: Error parsing page %" PRId64 ", bytes %" PRId64 "-%" PRId64, 
 1095|      0|                        i, pos - ctx->page_size, pos-1);
 1096|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1097|      0|            }
 1098|     53|            goto cleanup;
 1099|     53|        }
 1100|  3.79k|    }
 1101|       |
 1102|  3.04k|cleanup:
 1103|  3.04k|    if (outLastExaminedPage)
  ------------------
  |  Branch (1103:9): [True: 3.04k, False: 0]
  ------------------
 1104|  3.04k|        *outLastExaminedPage = i;
 1105|       |
 1106|  3.04k|    return retval;
 1107|  3.04k|}
readstat_sas7bdat_read.c:sas7bdat_parse_page_pass1:
  904|  6.37k|static readstat_error_t sas7bdat_parse_page_pass1(const char *page, size_t page_size, sas7bdat_ctx_t *ctx) {
  905|  6.37k|    readstat_error_t retval = READSTAT_OK;
  906|       |
  907|  6.37k|    uint16_t subheader_count = sas_read2(&page[ctx->page_header_size-4], ctx->bswap);
  908|       |
  909|  6.37k|    int i;
  910|  6.37k|    const char *shp = &page[ctx->page_header_size];
  911|  6.37k|    int lshp = ctx->subheader_pointer_size;
  912|       |
  913|  6.37k|    if (ctx->page_header_size + subheader_count*lshp > page_size) {
  ------------------
  |  Branch (913:9): [True: 35, False: 6.33k]
  ------------------
  914|     35|        retval = READSTAT_ERROR_PARSE;
  915|     35|        goto cleanup;
  916|     35|    }
  917|       |
  918|  68.0k|    for (i=0; i<subheader_count; i++) {
  ------------------
  |  Branch (918:15): [True: 62.4k, False: 5.60k]
  ------------------
  919|  62.4k|        subheader_pointer_t shp_info = { 0 };
  920|  62.4k|        if ((retval = sas7bdat_parse_subheader_pointer(shp, page + page_size - shp, &shp_info, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (920:13): [True: 0, False: 62.4k]
  ------------------
  921|      0|            goto cleanup;
  922|      0|        }
  923|  62.4k|        if (shp_info.len > 0 && shp_info.compression != SAS_COMPRESSION_TRUNC) {
  ------------------
  |  |  124|  40.6k|#define SAS_COMPRESSION_TRUNC  0x01
  ------------------
  |  Branch (923:13): [True: 40.6k, False: 21.8k]
  |  Branch (923:33): [True: 33.7k, False: 6.86k]
  ------------------
  924|  33.7k|            if ((retval = sas7bdat_validate_subheader_pointer(&shp_info, page_size, subheader_count, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (924:17): [True: 654, False: 33.1k]
  ------------------
  925|    654|                goto cleanup;
  926|    654|            }
  927|  33.1k|            if (shp_info.compression == SAS_COMPRESSION_NONE) {
  ------------------
  |  |  123|  33.1k|#define SAS_COMPRESSION_NONE   0x00
  ------------------
  |  Branch (927:17): [True: 30.3k, False: 2.75k]
  ------------------
  928|  30.3k|                sas_subheader_type_t subheader_type = sas7bdat_parse_subheader_type(page + shp_info.offset, ctx);
  929|  30.3k|                if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_TEXT) {
  ------------------
  |  Branch (929:21): [True: 2.58k, False: 27.7k]
  ------------------
  930|  2.58k|                    if ((retval = sas7bdat_parse_subheader(subheader_type, page + shp_info.offset, shp_info.len, ctx))
  ------------------
  |  Branch (930:25): [True: 63, False: 2.52k]
  ------------------
  931|  2.58k|                            != READSTAT_OK) {
  932|     63|                        goto cleanup;
  933|     63|                    }
  934|  2.58k|                }
  935|  30.3k|            } else if (shp_info.compression == SAS_COMPRESSION_ROW) {
  ------------------
  |  |  125|  2.75k|#define SAS_COMPRESSION_ROW    0x04
  ------------------
  |  Branch (935:24): [True: 2.73k, False: 17]
  ------------------
  936|       |                /* void */
  937|  2.73k|            } else {
  938|     17|                retval = READSTAT_ERROR_UNSUPPORTED_COMPRESSION;
  939|     17|                goto cleanup;
  940|     17|            }
  941|  33.1k|        }
  942|       |
  943|  61.7k|        shp += lshp;
  944|  61.7k|    }
  945|       |
  946|  6.37k|cleanup:
  947|       |
  948|  6.37k|    return retval;
  949|  6.33k|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader_pointer:
  858|   100k|        subheader_pointer_t *info, sas7bdat_ctx_t *ctx) {
  859|   100k|    readstat_error_t retval = READSTAT_OK;
  860|   100k|    if (ctx->u64) {
  ------------------
  |  Branch (860:9): [True: 18.0k, False: 82.8k]
  ------------------
  861|  18.0k|        if (shp_size <= 17) {
  ------------------
  |  Branch (861:13): [True: 0, False: 18.0k]
  ------------------
  862|      0|            retval = READSTAT_ERROR_PARSE;
  863|      0|            goto cleanup;
  864|      0|        }
  865|  18.0k|        info->offset = sas_read8(&shp[0], ctx->bswap);
  866|  18.0k|        info->len = sas_read8(&shp[8], ctx->bswap);
  867|  18.0k|        info->compression = shp[16];
  868|  18.0k|        info->is_compressed_data = shp[17];
  869|  82.8k|    } else {
  870|  82.8k|        if (shp_size <= 9) {
  ------------------
  |  Branch (870:13): [True: 0, False: 82.8k]
  ------------------
  871|      0|            retval = READSTAT_ERROR_PARSE;
  872|      0|            goto cleanup;
  873|      0|        }
  874|  82.8k|        info->offset = sas_read4(&shp[0], ctx->bswap);
  875|  82.8k|        info->len = sas_read4(&shp[4], ctx->bswap);
  876|  82.8k|        info->compression = shp[8];
  877|  82.8k|        info->is_compressed_data = shp[9];
  878|  82.8k|    }
  879|   100k|cleanup:
  880|   100k|    return retval;
  881|   100k|}
readstat_sas7bdat_read.c:sas7bdat_validate_subheader_pointer:
  884|  53.0k|        uint16_t subheader_count, sas7bdat_ctx_t *ctx) {
  885|  53.0k|    if (shp_info->offset > page_size)
  ------------------
  |  Branch (885:9): [True: 507, False: 52.5k]
  ------------------
  886|    507|        return READSTAT_ERROR_PARSE;
  887|  52.5k|    if (shp_info->len > page_size)
  ------------------
  |  Branch (887:9): [True: 300, False: 52.2k]
  ------------------
  888|    300|        return READSTAT_ERROR_PARSE;
  889|  52.2k|    if (shp_info->offset + shp_info->len > page_size)
  ------------------
  |  Branch (889:9): [True: 31, False: 52.1k]
  ------------------
  890|     31|        return READSTAT_ERROR_PARSE;
  891|  52.1k|    if (shp_info->offset < ctx->page_header_size + subheader_count*ctx->subheader_pointer_size)
  ------------------
  |  Branch (891:9): [True: 14, False: 52.1k]
  ------------------
  892|     14|        return READSTAT_ERROR_PARSE;
  893|  52.1k|    if (shp_info->compression == SAS_COMPRESSION_NONE) {
  ------------------
  |  |  123|  52.1k|#define SAS_COMPRESSION_NONE   0x00
  ------------------
  |  Branch (893:9): [True: 48.0k, False: 4.10k]
  ------------------
  894|  48.0k|        if (shp_info->len < ctx->subheader_signature_size)
  ------------------
  |  Branch (894:13): [True: 5, False: 48.0k]
  ------------------
  895|      5|            return READSTAT_ERROR_PARSE;
  896|  48.0k|        if (shp_info->offset + ctx->subheader_signature_size > page_size)
  ------------------
  |  Branch (896:13): [True: 0, False: 48.0k]
  ------------------
  897|      0|            return READSTAT_ERROR_PARSE;
  898|  48.0k|    }
  899|       |    
  900|  52.1k|    return READSTAT_OK;
  901|  52.1k|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader_type:
  838|  48.0k|static sas_subheader_type_t sas7bdat_parse_subheader_type(const char* subheader, sas7bdat_ctx_t* ctx) {
  839|  48.0k|    if (!ctx->u64) {
  ------------------
  |  Branch (839:9): [True: 37.5k, False: 10.4k]
  ------------------
  840|  37.5k|        uint32_t signature_32 = sas_read4(subheader, ctx->bswap);
  841|  37.5k|        return sas7bdat_parse_subheader_type_32(signature_32);
  842|  37.5k|    }
  843|       |
  844|  10.4k|    uint64_t signature = sas_read8(subheader, ctx->bswap);
  845|  10.4k|    if (signature == SAS_SUBHEADER_SIGNATURE_ROW_SIZE) {
  ------------------
  |  |   92|  10.4k|#define SAS_SUBHEADER_SIGNATURE_ROW_SIZE       0xF7F7F7F7
  ------------------
  |  Branch (845:9): [True: 1.74k, False: 8.71k]
  ------------------
  846|  1.74k|        return SAS_SUBHEADER_TYPE_ROW_SIZE;
  847|  8.71k|    } else if (signature == SAS_SUBHEADER_SIGNATURE_COLUMN_SIZE) {
  ------------------
  |  |   93|  8.71k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_SIZE    0xF6F6F6F6
  ------------------
  |  Branch (847:16): [True: 424, False: 8.29k]
  ------------------
  848|    424|        return SAS_SUBHEADER_TYPE_COLUMN_SIZE;
  849|  8.29k|    } else if ((signature & SAS_SUBHEADER_SIGNATURE_64BIT_MASK) != SAS_SUBHEADER_SIGNATURE_64BIT_MASK) {
  ------------------
  |  |  105|  8.29k|#define SAS_SUBHEADER_SIGNATURE_64BIT_MASK     0xFFFFFFFF00000000
  ------------------
                  } else if ((signature & SAS_SUBHEADER_SIGNATURE_64BIT_MASK) != SAS_SUBHEADER_SIGNATURE_64BIT_MASK) {
  ------------------
  |  |  105|  8.29k|#define SAS_SUBHEADER_SIGNATURE_64BIT_MASK     0xFFFFFFFF00000000
  ------------------
  |  Branch (849:16): [True: 1.59k, False: 6.70k]
  ------------------
  850|  1.59k|        return SAS_SUBHEADER_TYPE_DATA;
  851|  1.59k|    }
  852|       |
  853|  6.70k|    uint32_t lower_bytes = (uint32_t)(signature & SAS_SUBHEADER_SIGNATURE_32BIT_MASK);
  ------------------
  |  |  106|  6.70k|#define SAS_SUBHEADER_SIGNATURE_32BIT_MASK     0x00000000FFFFFFFF
  ------------------
  854|  6.70k|    return sas7bdat_parse_subheader_type_32(lower_bytes);
  855|  10.4k|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader_type_32:
  812|  44.2k|static sas_subheader_type_t sas7bdat_parse_subheader_type_32(uint32_t signature) {
  813|  44.2k|    switch (signature) {
  814|  9.04k|        case SAS_SUBHEADER_SIGNATURE_ROW_SIZE:
  ------------------
  |  |   92|  9.04k|#define SAS_SUBHEADER_SIGNATURE_ROW_SIZE       0xF7F7F7F7
  ------------------
  |  Branch (814:9): [True: 9.04k, False: 35.2k]
  ------------------
  815|  9.04k|            return SAS_SUBHEADER_TYPE_ROW_SIZE;
  816|  3.20k|        case SAS_SUBHEADER_SIGNATURE_COLUMN_SIZE:
  ------------------
  |  |   93|  3.20k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_SIZE    0xF6F6F6F6
  ------------------
  |  Branch (816:9): [True: 3.20k, False: 41.0k]
  ------------------
  817|  3.20k|            return SAS_SUBHEADER_TYPE_COLUMN_SIZE;
  818|  2.69k|        case SAS_SUBHEADER_SIGNATURE_COUNTS:
  ------------------
  |  |   94|  2.69k|#define SAS_SUBHEADER_SIGNATURE_COUNTS         0xFFFFFC00
  ------------------
  |  Branch (818:9): [True: 2.69k, False: 41.6k]
  ------------------
  819|  2.69k|            return SAS_SUBHEADER_TYPE_COUNTS;
  820|  8.79k|        case SAS_SUBHEADER_SIGNATURE_COLUMN_FORMAT:
  ------------------
  |  |   95|  8.79k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_FORMAT  0xFFFFFBFE
  ------------------
  |  Branch (820:9): [True: 8.79k, False: 35.5k]
  ------------------
  821|  8.79k|            return SAS_SUBHEADER_TYPE_COLUMN_FORMAT;
  822|  5.14k|        case SAS_SUBHEADER_SIGNATURE_COLUMN_ATTRS:
  ------------------
  |  |  100|  5.14k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_ATTRS   0xFFFFFFFC
  ------------------
  |  Branch (822:9): [True: 5.14k, False: 39.1k]
  ------------------
  823|  5.14k|            return SAS_SUBHEADER_TYPE_COLUMN_ATTRS;
  824|  4.00k|        case SAS_SUBHEADER_SIGNATURE_COLUMN_TEXT:
  ------------------
  |  |  101|  4.00k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_TEXT    0xFFFFFFFD
  ------------------
  |  Branch (824:9): [True: 4.00k, False: 40.2k]
  ------------------
  825|  4.00k|            return SAS_SUBHEADER_TYPE_COLUMN_TEXT;
  826|  2.67k|        case SAS_SUBHEADER_SIGNATURE_COLUMN_LIST:
  ------------------
  |  |  102|  2.67k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_LIST    0xFFFFFFFE
  ------------------
  |  Branch (826:9): [True: 2.67k, False: 41.6k]
  ------------------
  827|  2.67k|            return SAS_SUBHEADER_TYPE_COLUMN_LIST;
  828|  3.85k|        case SAS_SUBHEADER_SIGNATURE_COLUMN_NAME:
  ------------------
  |  |  103|  3.85k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_NAME    0xFFFFFFFF
  ------------------
  |  Branch (828:9): [True: 3.85k, False: 40.4k]
  ------------------
  829|  3.85k|            return SAS_SUBHEADER_TYPE_COLUMN_NAME;
  830|  4.88k|        default:
  ------------------
  |  Branch (830:9): [True: 4.88k, False: 39.4k]
  ------------------
  831|  4.88k|            if ((signature & SAS_SUBHEADER_SIGNATURE_COLUMN_MASK) == SAS_SUBHEADER_SIGNATURE_COLUMN_MASK) {
  ------------------
  |  |   97|  4.88k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_MASK    0xFFFFFFF8
  ------------------
                          if ((signature & SAS_SUBHEADER_SIGNATURE_COLUMN_MASK) == SAS_SUBHEADER_SIGNATURE_COLUMN_MASK) {
  ------------------
  |  |   97|  4.88k|#define SAS_SUBHEADER_SIGNATURE_COLUMN_MASK    0xFFFFFFF8
  ------------------
  |  Branch (831:17): [True: 1.59k, False: 3.28k]
  ------------------
  832|  1.59k|                return SAS_SUBHEADER_TYPE_UNKNOWN;
  833|  1.59k|            }
  834|  3.28k|            return SAS_SUBHEADER_TYPE_DATA;
  835|  44.2k|    }
  836|  44.2k|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader:
  634|  18.2k|        size_t len, sas7bdat_ctx_t *ctx) {
  635|  18.2k|    readstat_error_t retval = READSTAT_OK;
  636|       |
  637|  18.2k|    if (len < 2 + ctx->subheader_signature_size) {
  ------------------
  |  Branch (637:9): [True: 8, False: 18.2k]
  ------------------
  638|      8|        retval = READSTAT_ERROR_PARSE;
  639|      8|        goto cleanup;
  640|      8|    }
  641|  18.2k|    if (subheader_type == SAS_SUBHEADER_TYPE_ROW_SIZE) {
  ------------------
  |  Branch (641:9): [True: 4.28k, False: 13.9k]
  ------------------
  642|  4.28k|        retval = sas7bdat_parse_row_size_subheader(subheader, len, ctx);
  643|  13.9k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_SIZE) {
  ------------------
  |  Branch (643:16): [True: 1.25k, False: 12.7k]
  ------------------
  644|  1.25k|        retval = sas7bdat_parse_column_size_subheader(subheader, len, ctx);
  645|  12.7k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COUNTS) {
  ------------------
  |  Branch (645:16): [True: 1.16k, False: 11.5k]
  ------------------
  646|       |        /* void */
  647|  11.5k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_TEXT) {
  ------------------
  |  Branch (647:16): [True: 2.58k, False: 8.98k]
  ------------------
  648|  2.58k|        retval = sas7bdat_parse_column_text_subheader(subheader, len, ctx);
  649|  8.98k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_NAME) {
  ------------------
  |  Branch (649:16): [True: 1.34k, False: 7.64k]
  ------------------
  650|  1.34k|        retval = sas7bdat_parse_column_name_subheader(subheader, len, ctx);
  651|  7.64k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_ATTRS) {
  ------------------
  |  Branch (651:16): [True: 2.21k, False: 5.43k]
  ------------------
  652|  2.21k|        retval = sas7bdat_parse_column_attributes_subheader(subheader, len, ctx);
  653|  5.43k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_FORMAT) {
  ------------------
  |  Branch (653:16): [True: 3.84k, False: 1.58k]
  ------------------
  654|  3.84k|        retval = sas7bdat_parse_column_format_subheader(subheader, len, ctx);
  655|  3.84k|    } else if (subheader_type == SAS_SUBHEADER_TYPE_COLUMN_LIST) {
  ------------------
  |  Branch (655:16): [True: 891, False: 691]
  ------------------
  656|       |        /* void */
  657|    891|    } else if (subheader_type == SAS_SUBHEADER_TYPE_UNKNOWN) {
  ------------------
  |  Branch (657:16): [True: 654, False: 37]
  ------------------
  658|       |        /* void */
  659|    654|    } else {
  660|     37|        retval = READSTAT_ERROR_PARSE;
  661|     37|    }
  662|       |
  663|  18.2k|cleanup:
  664|       |
  665|  18.2k|    return retval;
  666|  18.2k|}
readstat_sas7bdat_read.c:sas7bdat_parse_row_size_subheader:
  232|  4.28k|static readstat_error_t sas7bdat_parse_row_size_subheader(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  233|  4.28k|    readstat_error_t retval = READSTAT_OK;
  234|  4.28k|    uint64_t total_row_count;
  235|  4.28k|    uint64_t row_length, page_row_count;
  236|       |
  237|  4.28k|    if (len < (ctx->u64 ? 250: 190)) {
  ------------------
  |  Branch (237:9): [True: 10, False: 4.27k]
  |  Branch (237:16): [True: 1.04k, False: 3.24k]
  ------------------
  238|     10|        retval = READSTAT_ERROR_PARSE;
  239|     10|        goto cleanup;
  240|     10|    }
  241|       |
  242|  4.27k|    if (ctx->u64) {
  ------------------
  |  Branch (242:9): [True: 1.04k, False: 3.23k]
  ------------------
  243|  1.04k|        row_length = sas_read8(&subheader[40], ctx->bswap);
  244|  1.04k|        total_row_count = sas_read8(&subheader[48], ctx->bswap);
  245|  1.04k|        page_row_count = sas_read8(&subheader[120], ctx->bswap);
  246|  3.23k|    } else {
  247|  3.23k|        row_length = sas_read4(&subheader[20], ctx->bswap);
  248|  3.23k|        total_row_count = sas_read4(&subheader[24], ctx->bswap);
  249|  3.23k|        page_row_count = sas_read4(&subheader[60], ctx->bswap);
  250|  3.23k|    }
  251|       |
  252|  4.27k|    sas_text_ref_t file_label_ref = sas7bdat_parse_text_ref(&subheader[len-130], ctx);
  253|  4.27k|    if (file_label_ref.length) {
  ------------------
  |  Branch (253:9): [True: 683, False: 3.59k]
  ------------------
  254|    683|        if ((retval = sas7bdat_copy_text_ref(ctx->file_label, sizeof(ctx->file_label),
  ------------------
  |  Branch (254:13): [True: 47, False: 636]
  ------------------
  255|    683|                        file_label_ref, ctx)) != READSTAT_OK) {
  256|     47|            goto cleanup;
  257|     47|        }
  258|    683|    }
  259|       |
  260|  4.23k|    sas_text_ref_t compression_ref = sas7bdat_parse_text_ref(&subheader[len-118], ctx);
  261|  4.23k|    if (compression_ref.length) {
  ------------------
  |  Branch (261:9): [True: 1.19k, False: 3.03k]
  ------------------
  262|  1.19k|        char compression[9];
  263|  1.19k|        if ((retval = sas7bdat_copy_text_ref(compression, sizeof(compression),
  ------------------
  |  Branch (263:13): [True: 38, False: 1.15k]
  ------------------
  264|  1.19k|                        compression_ref, ctx)) != READSTAT_OK) {
  265|     38|            goto cleanup;
  266|     38|        }
  267|  1.15k|        ctx->rdc_compression = (memcmp(compression, SAS_COMPRESSION_SIGNATURE_RDC, 8) == 0);
  ------------------
  |  |  128|  1.15k|#define SAS_COMPRESSION_SIGNATURE_RDC  "SASYZCR2"
  ------------------
  268|  1.15k|    }
  269|       |
  270|  4.19k|    ctx->row_length = row_length;
  271|  4.19k|    ctx->row = readstat_realloc(ctx->row, ctx->row_length);
  272|  4.19k|    if (ctx->row == NULL) {
  ------------------
  |  Branch (272:9): [True: 10, False: 4.18k]
  ------------------
  273|     10|        retval = READSTAT_ERROR_MALLOC;
  274|     10|        goto cleanup;
  275|     10|    }
  276|       |
  277|  4.18k|    ctx->page_row_count = page_row_count;
  278|  4.18k|    uint64_t total_row_count_after_skipping = total_row_count;
  279|  4.18k|    if (total_row_count > ctx->row_offset) {
  ------------------
  |  Branch (279:9): [True: 3.82k, False: 358]
  ------------------
  280|  3.82k|        total_row_count_after_skipping -= ctx->row_offset;
  281|  3.82k|    } else {
  282|    358|        total_row_count_after_skipping = 0;
  283|    358|        ctx->row_offset = total_row_count;
  284|    358|    }
  285|  4.18k|    if (ctx->row_limit == 0 || total_row_count_after_skipping < ctx->row_limit)
  ------------------
  |  Branch (285:9): [True: 1.35k, False: 2.83k]
  |  Branch (285:32): [True: 244, False: 2.58k]
  ------------------
  286|  1.59k|        ctx->row_limit = total_row_count_after_skipping;
  287|       |
  288|  4.28k|cleanup:
  289|  4.28k|    return retval;
  290|  4.18k|}
readstat_sas7bdat_read.c:sas7bdat_parse_text_ref:
  133|  60.5k|static sas_text_ref_t sas7bdat_parse_text_ref(const char *data, sas7bdat_ctx_t *ctx) {
  134|  60.5k|    sas_text_ref_t  ref;
  135|       |
  136|  60.5k|    ref.index = sas_read2(&data[0], ctx->bswap);
  137|  60.5k|    ref.offset = sas_read2(&data[2], ctx->bswap);
  138|  60.5k|    ref.length = sas_read2(&data[4], ctx->bswap);
  139|       |
  140|  60.5k|    return ref;
  141|  60.5k|}
readstat_sas7bdat_read.c:sas7bdat_copy_text_ref:
  143|  9.35M|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) {
  144|  9.35M|    if (text_ref.index >= ctx->text_blob_count)
  ------------------
  |  Branch (144:9): [True: 141, False: 9.35M]
  ------------------
  145|    141|        return READSTAT_ERROR_PARSE;
  146|       |    
  147|  9.35M|    if (text_ref.length == 0) {
  ------------------
  |  Branch (147:9): [True: 9.35M, False: 3.61k]
  ------------------
  148|  9.35M|        out_buffer[0] = '\0';
  149|  9.35M|        return READSTAT_OK;
  150|  9.35M|    }
  151|       |
  152|  3.61k|    char *blob = ctx->text_blobs[text_ref.index];
  153|       |
  154|  3.61k|    if (text_ref.offset + text_ref.length > ctx->text_blob_lengths[text_ref.index])
  ------------------
  |  Branch (154:9): [True: 80, False: 3.53k]
  ------------------
  155|     80|        return READSTAT_ERROR_PARSE;
  156|       |
  157|  3.53k|    return readstat_convert(out_buffer, out_buffer_len, &blob[text_ref.offset], text_ref.length,
  158|  3.53k|            ctx->converter);
  159|  3.61k|}
readstat_sas7bdat_read.c:sas7bdat_parse_column_size_subheader:
  204|  1.25k|static readstat_error_t sas7bdat_parse_column_size_subheader(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  205|  1.25k|    uint64_t col_count;
  206|  1.25k|    readstat_error_t retval = READSTAT_OK;
  207|       |
  208|  1.25k|    if (ctx->column_count || ctx->did_submit_columns) {
  ------------------
  |  Branch (208:9): [True: 22, False: 1.22k]
  |  Branch (208:30): [True: 1, False: 1.22k]
  ------------------
  209|     23|        retval = READSTAT_ERROR_PARSE;
  210|     23|        goto cleanup;
  211|     23|    }
  212|       |
  213|  1.22k|    if (len < (ctx->u64 ? 16 : 8)) {
  ------------------
  |  Branch (213:9): [True: 2, False: 1.22k]
  |  Branch (213:16): [True: 286, False: 941]
  ------------------
  214|      2|        retval = READSTAT_ERROR_PARSE;
  215|      2|        goto cleanup;
  216|      2|    }
  217|       |
  218|  1.22k|    if (ctx->u64) {
  ------------------
  |  Branch (218:9): [True: 286, False: 939]
  ------------------
  219|    286|        col_count = sas_read8(&subheader[8], ctx->bswap);
  220|    939|    } else {
  221|    939|        col_count = sas_read4(&subheader[4], ctx->bswap);
  222|    939|    }
  223|       |
  224|  1.22k|    ctx->column_count = col_count;
  225|       |
  226|  1.22k|    retval = sas7bdat_realloc_col_info(ctx, ctx->column_count);
  227|       |
  228|  1.25k|cleanup:
  229|  1.25k|    return retval;
  230|  1.22k|}
readstat_sas7bdat_read.c:sas7bdat_realloc_col_info:
  191|  8.51k|static readstat_error_t sas7bdat_realloc_col_info(sas7bdat_ctx_t *ctx, size_t count) {
  192|  8.51k|    if (ctx->col_info_count < count) {
  ------------------
  |  Branch (192:9): [True: 3.83k, False: 4.67k]
  ------------------
  193|  3.83k|        size_t old_count = ctx->col_info_count;
  194|  3.83k|        ctx->col_info_count = count;
  195|  3.83k|        ctx->col_info = readstat_realloc(ctx->col_info, ctx->col_info_count * sizeof(col_info_t));
  196|  3.83k|        if (ctx->col_info == NULL) {
  ------------------
  |  Branch (196:13): [True: 90, False: 3.74k]
  ------------------
  197|     90|            return READSTAT_ERROR_MALLOC;
  198|     90|        }
  199|  3.74k|        memset(ctx->col_info + old_count, 0, (count - old_count) * sizeof(col_info_t));
  200|  3.74k|    }
  201|  8.42k|    return READSTAT_OK;
  202|  8.51k|}
readstat_sas7bdat_read.c:sas7bdat_parse_column_text_subheader:
  161|  2.58k|static readstat_error_t sas7bdat_parse_column_text_subheader(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  162|  2.58k|    readstat_error_t retval = READSTAT_OK;
  163|  2.58k|    size_t signature_len = ctx->subheader_signature_size;
  164|  2.58k|    uint16_t remainder = sas_read2(&subheader[signature_len], ctx->bswap);
  165|  2.58k|    char *blob = NULL;
  166|  2.58k|    if (remainder != sas_subheader_remainder(len, signature_len)) {
  ------------------
  |  Branch (166:9): [True: 61, False: 2.52k]
  ------------------
  167|     61|        retval = READSTAT_ERROR_PARSE;
  168|     61|        goto cleanup;
  169|     61|    }
  170|  2.52k|    ctx->text_blob_count++;
  171|  2.52k|    ctx->text_blobs = readstat_realloc(ctx->text_blobs, ctx->text_blob_count * sizeof(char *));
  172|  2.52k|    ctx->text_blob_lengths = readstat_realloc(ctx->text_blob_lengths,
  173|  2.52k|            ctx->text_blob_count * sizeof(ctx->text_blob_lengths[0]));
  174|  2.52k|    if (ctx->text_blobs == NULL || ctx->text_blob_lengths == NULL) {
  ------------------
  |  Branch (174:9): [True: 0, False: 2.52k]
  |  Branch (174:36): [True: 0, False: 2.52k]
  ------------------
  175|      0|        retval = READSTAT_ERROR_MALLOC;
  176|      0|        goto cleanup;
  177|      0|    }
  178|       |
  179|  2.52k|    if ((blob = readstat_malloc(len-signature_len)) == NULL) {
  ------------------
  |  Branch (179:9): [True: 0, False: 2.52k]
  ------------------
  180|      0|        retval = READSTAT_ERROR_MALLOC;
  181|      0|        goto cleanup;
  182|      0|    }
  183|  2.52k|    memcpy(blob, subheader+signature_len, len-signature_len);
  184|  2.52k|    ctx->text_blob_lengths[ctx->text_blob_count-1] = len-signature_len;
  185|  2.52k|    ctx->text_blobs[ctx->text_blob_count-1] = blob;
  186|       |
  187|  2.58k|cleanup:
  188|  2.58k|    return retval;
  189|  2.52k|}
readstat_sas7bdat_read.c:sas7bdat_parse_column_name_subheader:
  292|  1.34k|static readstat_error_t sas7bdat_parse_column_name_subheader(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  293|  1.34k|    readstat_error_t retval = READSTAT_OK;
  294|  1.34k|    size_t signature_len = ctx->subheader_signature_size;
  295|  1.34k|    int cmax = ctx->u64 ? (len-28)/8 : (len-20)/8;
  ------------------
  |  Branch (295:16): [True: 354, False: 987]
  ------------------
  296|  1.34k|    int i;
  297|  1.34k|    const char *cnp = &subheader[signature_len+8];
  298|  1.34k|    uint16_t remainder = sas_read2(&subheader[signature_len], ctx->bswap);
  299|       |
  300|  1.34k|    if (remainder != sas_subheader_remainder(len, signature_len)) {
  ------------------
  |  Branch (300:9): [True: 63, False: 1.27k]
  ------------------
  301|     63|        retval = READSTAT_ERROR_PARSE;
  302|     63|        goto cleanup;
  303|     63|    }
  304|       |
  305|  1.27k|    ctx->col_names_count += cmax;
  306|       |
  307|  1.27k|    if ((retval = sas7bdat_realloc_col_info(ctx, ctx->col_names_count)) != READSTAT_OK)
  ------------------
  |  Branch (307:9): [True: 16, False: 1.26k]
  ------------------
  308|     16|        goto cleanup;
  309|       |
  310|  45.5k|    for (i=ctx->col_names_count-cmax; i<ctx->col_names_count; i++) {
  ------------------
  |  Branch (310:39): [True: 44.3k, False: 1.26k]
  ------------------
  311|  44.3k|        ctx->col_info[i].name_ref = sas7bdat_parse_text_ref(cnp, ctx);
  312|  44.3k|        cnp += 8;
  313|  44.3k|    }
  314|       |
  315|  1.34k|cleanup:
  316|       |
  317|  1.34k|    return retval;
  318|  1.26k|}
readstat_sas7bdat_read.c:sas7bdat_parse_column_attributes_subheader:
  320|  2.21k|static readstat_error_t sas7bdat_parse_column_attributes_subheader(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  321|  2.21k|    readstat_error_t retval = READSTAT_OK;
  322|  2.21k|    size_t signature_len = ctx->subheader_signature_size;
  323|  2.21k|    int cmax = ctx->u64 ? (len-28)/16 : (len-20)/12;
  ------------------
  |  Branch (323:16): [True: 479, False: 1.73k]
  ------------------
  324|  2.21k|    int i;
  325|  2.21k|    const char *cap = &subheader[signature_len+8];
  326|  2.21k|    uint16_t remainder = sas_read2(&subheader[signature_len], ctx->bswap);
  327|       |
  328|  2.21k|    if (remainder != sas_subheader_remainder(len, signature_len)) {
  ------------------
  |  Branch (328:9): [True: 52, False: 2.16k]
  ------------------
  329|     52|        retval = READSTAT_ERROR_PARSE;
  330|     52|        goto cleanup;
  331|     52|    }
  332|  2.16k|    ctx->col_attrs_count += cmax;
  333|  2.16k|    if ((retval = sas7bdat_realloc_col_info(ctx, ctx->col_attrs_count)) != READSTAT_OK)
  ------------------
  |  Branch (333:9): [True: 11, False: 2.15k]
  ------------------
  334|     11|        goto cleanup;
  335|       |
  336|  9.46k|    for (i=ctx->col_attrs_count-cmax; i<ctx->col_attrs_count; i++) {
  ------------------
  |  Branch (336:39): [True: 7.33k, False: 2.13k]
  ------------------
  337|  7.33k|        if (ctx->u64) {
  ------------------
  |  Branch (337:13): [True: 826, False: 6.50k]
  ------------------
  338|    826|            ctx->col_info[i].offset = sas_read8(&cap[0], ctx->bswap);
  339|  6.50k|        } else {
  340|  6.50k|            ctx->col_info[i].offset = sas_read4(&cap[0], ctx->bswap);
  341|  6.50k|        }
  342|       |
  343|  7.33k|        readstat_off_t off=4;
  344|  7.33k|        if (ctx->u64)
  ------------------
  |  Branch (344:13): [True: 826, False: 6.50k]
  ------------------
  345|    826|            off=8;
  346|       |
  347|  7.33k|        ctx->col_info[i].width = sas_read4(&cap[off], ctx->bswap);
  348|  7.33k|        if (ctx->col_info[i].width > ctx->max_col_width)
  ------------------
  |  Branch (348:13): [True: 886, False: 6.44k]
  ------------------
  349|    886|            ctx->max_col_width = ctx->col_info[i].width;
  350|       |
  351|  7.33k|        if (cap[off+6] == SAS_COLUMN_TYPE_NUM) {
  ------------------
  |  |   89|  7.33k|#define SAS_COLUMN_TYPE_NUM  0x01
  ------------------
  |  Branch (351:13): [True: 4.29k, False: 3.03k]
  ------------------
  352|  4.29k|            ctx->col_info[i].type = READSTAT_TYPE_DOUBLE;
  353|  4.29k|        } else if (cap[off+6] == SAS_COLUMN_TYPE_CHR) {
  ------------------
  |  |   90|  3.03k|#define SAS_COLUMN_TYPE_CHR  0x02
  ------------------
  |  Branch (353:20): [True: 3.01k, False: 20]
  ------------------
  354|  3.01k|            ctx->col_info[i].type = READSTAT_TYPE_STRING;
  355|  3.01k|        } else {
  356|     20|            retval = READSTAT_ERROR_PARSE;
  357|     20|            goto cleanup;
  358|     20|        }
  359|  7.31k|        ctx->col_info[i].index = i;
  360|  7.31k|        cap += off+8;
  361|  7.31k|    }
  362|       |
  363|  2.21k|cleanup:
  364|       |
  365|  2.21k|    return retval;
  366|  2.15k|}
readstat_sas7bdat_read.c:sas7bdat_parse_column_format_subheader:
  368|  3.84k|static readstat_error_t sas7bdat_parse_column_format_subheader(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  369|  3.84k|    readstat_error_t retval = READSTAT_OK;
  370|       |
  371|  3.84k|    if (len < (ctx->u64 ? 58 : 46)) {
  ------------------
  |  Branch (371:9): [True: 1, False: 3.84k]
  |  Branch (371:16): [True: 353, False: 3.49k]
  ------------------
  372|      1|        retval = READSTAT_ERROR_PARSE;
  373|      1|        goto cleanup;
  374|      1|    }
  375|       |
  376|  3.84k|    ctx->col_formats_count++;
  377|  3.84k|    if ((retval = sas7bdat_realloc_col_info(ctx, ctx->col_formats_count)) != READSTAT_OK)
  ------------------
  |  Branch (377:9): [True: 0, False: 3.84k]
  ------------------
  378|      0|        goto cleanup;
  379|       |
  380|  3.84k|    if (ctx->u64) {
  ------------------
  |  Branch (380:9): [True: 353, False: 3.49k]
  ------------------
  381|    353|        ctx->col_info[ctx->col_formats_count-1].format_width = sas_read2(&subheader[24], ctx->bswap);
  382|    353|        ctx->col_info[ctx->col_formats_count-1].format_digits = sas_read2(&subheader[26], ctx->bswap);
  383|  3.49k|    } else {
  384|  3.49k|        ctx->col_info[ctx->col_formats_count-1].format_width = sas_read2(&subheader[12], ctx->bswap);
  385|  3.49k|        ctx->col_info[ctx->col_formats_count-1].format_digits = sas_read2(&subheader[14], ctx->bswap);
  386|  3.49k|    }
  387|  3.84k|    ctx->col_info[ctx->col_formats_count-1].format_ref = sas7bdat_parse_text_ref(
  388|  3.84k|            ctx->u64 ? &subheader[46] : &subheader[34], ctx);
  ------------------
  |  Branch (388:13): [True: 353, False: 3.49k]
  ------------------
  389|  3.84k|    ctx->col_info[ctx->col_formats_count-1].label_ref = sas7bdat_parse_text_ref(
  390|  3.84k|            ctx->u64 ? &subheader[52] : &subheader[40], ctx);
  ------------------
  |  Branch (390:13): [True: 353, False: 3.49k]
  ------------------
  391|       |
  392|  3.84k|cleanup:
  393|  3.84k|    return retval;
  394|  3.84k|}
readstat_sas7bdat_read.c:sas7bdat_parse_amd_pages_pass1:
 1109|  2.80k|static readstat_error_t sas7bdat_parse_amd_pages_pass1(int64_t last_examined_page_pass1, sas7bdat_ctx_t *ctx) {
 1110|  2.80k|    readstat_error_t retval = READSTAT_OK;
 1111|  2.80k|    readstat_io_t *io = ctx->io;
 1112|  2.80k|    uint64_t i;
 1113|  2.80k|    uint64_t amd_page_count = 0;
 1114|       |
 1115|       |    /* ...then AMD pages at the end */
 1116|  6.22k|    for (i=ctx->page_count-1; i>last_examined_page_pass1; i--) {
  ------------------
  |  Branch (1116:31): [True: 4.74k, False: 1.47k]
  ------------------
 1117|  4.74k|        if (io->seek(ctx->header_size + i*ctx->page_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (1117:13): [True: 229, False: 4.51k]
  ------------------
 1118|    229|            retval = READSTAT_ERROR_SEEK;
 1119|    229|            if (ctx->handle.error) {
  ------------------
  |  Branch (1119:17): [True: 0, False: 229]
  ------------------
 1120|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf), "ReadStat: Failed to seek to position %" PRId64 
 1121|      0|                        " (= %" PRId64 " + %" PRId64 "*%" PRId64 ")",
 1122|      0|                        ctx->header_size + i*ctx->page_size, ctx->header_size, i, ctx->page_size);
 1123|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1124|      0|            }
 1125|    229|            goto cleanup;
 1126|    229|        }
 1127|       |
 1128|  4.51k|        readstat_off_t off = 0;
 1129|  4.51k|        if (ctx->u64)
  ------------------
  |  Branch (1129:13): [True: 3.59k, False: 918]
  ------------------
 1130|  3.59k|            off = 16;
 1131|       |
 1132|  4.51k|        size_t head_len = off + 16 + 2;
 1133|  4.51k|        size_t tail_len = ctx->page_size - head_len;
 1134|       |
 1135|  4.51k|        if (io->read(ctx->page, head_len, io->io_ctx) < head_len) {
  ------------------
  |  Branch (1135:13): [True: 6, False: 4.50k]
  ------------------
 1136|      6|            retval = READSTAT_ERROR_READ;
 1137|      6|            goto cleanup;
 1138|      6|        }
 1139|       |
 1140|  4.50k|        uint16_t page_type = sas_read2(&ctx->page[off+16], ctx->bswap);
 1141|       |
 1142|  4.50k|        if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_DATA) {
  ------------------
  |  |  112|  4.50k|#define SAS_PAGE_TYPE_MASK   0x0F00
  ------------------
                      if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_DATA) {
  ------------------
  |  |  109|  4.50k|#define SAS_PAGE_TYPE_DATA   0x0100
  ------------------
  |  Branch (1142:13): [True: 1.10k, False: 3.40k]
  ------------------
 1143|       |            /* Usually AMD pages are at the end but sometimes data pages appear after them */
 1144|  1.10k|            if (amd_page_count > 0)
  ------------------
  |  Branch (1144:17): [True: 353, False: 748]
  ------------------
 1145|    353|                break;
 1146|    748|            continue;
 1147|  1.10k|        }
 1148|  3.40k|        if ((page_type & SAS_PAGE_TYPE_COMP))
  ------------------
  |  |  115|  3.40k|#define SAS_PAGE_TYPE_COMP   0x9000
  ------------------
  |  Branch (1148:13): [True: 812, False: 2.59k]
  ------------------
 1149|    812|            continue;
 1150|       |
 1151|  2.59k|        if (io->read(ctx->page + head_len, tail_len, io->io_ctx) < tail_len) {
  ------------------
  |  Branch (1151:13): [True: 23, False: 2.57k]
  ------------------
 1152|     23|            retval = READSTAT_ERROR_READ;
 1153|     23|            goto cleanup;
 1154|     23|        }
 1155|       |
 1156|  2.57k|        if ((retval = sas7bdat_parse_page_pass1(ctx->page, ctx->page_size, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1156:13): [True: 716, False: 1.85k]
  ------------------
 1157|    716|            if (ctx->handle.error && retval != READSTAT_ERROR_USER_ABORT) {
  ------------------
  |  Branch (1157:17): [True: 0, False: 716]
  |  Branch (1157:38): [True: 0, False: 0]
  ------------------
 1158|      0|                int64_t pos = io->seek(0, READSTAT_SEEK_CUR, io->io_ctx);
 1159|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf), 
 1160|      0|                        "ReadStat: Error parsing page %" PRId64 ", bytes %" PRId64 "-%" PRId64, 
 1161|      0|                        i, pos - ctx->page_size, pos-1);
 1162|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1163|      0|            }
 1164|    716|            goto cleanup;
 1165|    716|        }
 1166|       |
 1167|  1.85k|        amd_page_count++;
 1168|  1.85k|    }
 1169|       |
 1170|  2.80k|cleanup:
 1171|       |
 1172|  2.80k|    return retval;
 1173|  2.80k|}
readstat_sas7bdat_read.c:sas7bdat_parse_all_pages_pass2:
 1175|  1.83k|static readstat_error_t sas7bdat_parse_all_pages_pass2(sas7bdat_ctx_t *ctx) {
 1176|  1.83k|    readstat_error_t retval = READSTAT_OK;
 1177|  1.83k|    readstat_io_t *io = ctx->io;
 1178|  1.83k|    int64_t i;
 1179|       |
 1180|  4.48k|    for (i=0; i<ctx->page_count; i++) {
  ------------------
  |  Branch (1180:15): [True: 4.37k, False: 111]
  ------------------
 1181|  4.37k|        if ((retval = sas7bdat_update_progress(ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1181:13): [True: 0, False: 4.37k]
  ------------------
 1182|      0|            goto cleanup;
 1183|      0|        }
 1184|  4.37k|        if (io->read(ctx->page, ctx->page_size, io->io_ctx) < ctx->page_size) {
  ------------------
  |  Branch (1184:13): [True: 115, False: 4.25k]
  ------------------
 1185|    115|            retval = READSTAT_ERROR_READ;
 1186|    115|            goto cleanup;
 1187|    115|        }
 1188|       |
 1189|  4.25k|        if ((retval = sas7bdat_parse_page_pass2(ctx->page, ctx->page_size, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1189:13): [True: 1.38k, False: 2.87k]
  ------------------
 1190|  1.38k|            if (ctx->handle.error && retval != READSTAT_ERROR_USER_ABORT) {
  ------------------
  |  Branch (1190:17): [True: 0, False: 1.38k]
  |  Branch (1190:38): [True: 0, False: 0]
  ------------------
 1191|      0|                int64_t pos = io->seek(0, READSTAT_SEEK_CUR, io->io_ctx);
 1192|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf), 
 1193|      0|                        "ReadStat: Error parsing page %" PRId64 ", bytes %" PRId64 "-%" PRId64, 
 1194|      0|                        i, pos - ctx->page_size, pos-1);
 1195|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
 1196|      0|            }
 1197|  1.38k|            goto cleanup;
 1198|  1.38k|        }
 1199|  2.87k|        if (ctx->parsed_row_count == ctx->row_limit)
  ------------------
  |  Branch (1199:13): [True: 221, False: 2.65k]
  ------------------
 1200|    221|            break;
 1201|  2.87k|    }
 1202|  1.83k|cleanup:
 1203|       |
 1204|  1.83k|    return retval;
 1205|  1.83k|}
readstat_sas7bdat_read.c:sas7bdat_parse_page_pass2:
  951|  4.25k|static readstat_error_t sas7bdat_parse_page_pass2(const char *page, size_t page_size, sas7bdat_ctx_t *ctx) {
  952|  4.25k|    uint16_t page_type;
  953|       |
  954|  4.25k|    readstat_error_t retval = READSTAT_OK;
  955|       |
  956|  4.25k|    page_type = sas_read2(&page[ctx->page_header_size-8], ctx->bswap);
  957|       |
  958|  4.25k|    const char *data = NULL;
  959|       |
  960|  4.25k|    if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_DATA) {
  ------------------
  |  |  112|  4.25k|#define SAS_PAGE_TYPE_MASK   0x0F00
  ------------------
                  if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_DATA) {
  ------------------
  |  |  109|  4.25k|#define SAS_PAGE_TYPE_DATA   0x0100
  ------------------
  |  Branch (960:9): [True: 792, False: 3.46k]
  ------------------
  961|    792|        ctx->page_row_count = sas_read2(&page[ctx->page_header_size-6], ctx->bswap);
  962|    792|        data = &page[ctx->page_header_size];
  963|  3.46k|    } else if (!(page_type & SAS_PAGE_TYPE_COMP)) {
  ------------------
  |  |  115|  3.46k|#define SAS_PAGE_TYPE_COMP   0x9000
  ------------------
  |  Branch (963:16): [True: 3.15k, False: 310]
  ------------------
  964|  3.15k|        uint16_t subheader_count = sas_read2(&page[ctx->page_header_size-4], ctx->bswap);
  965|       |
  966|  3.15k|        int i;
  967|  3.15k|        const char *shp = &page[ctx->page_header_size];
  968|  3.15k|        int lshp = ctx->subheader_pointer_size;
  969|       |
  970|  3.15k|        if (ctx->page_header_size + subheader_count*lshp > page_size) {
  ------------------
  |  Branch (970:13): [True: 22, False: 3.13k]
  ------------------
  971|     22|            retval = READSTAT_ERROR_PARSE;
  972|     22|            goto cleanup;
  973|     22|        }
  974|       |
  975|  40.5k|        for (i=0; i<subheader_count; i++) {
  ------------------
  |  Branch (975:19): [True: 38.4k, False: 2.07k]
  ------------------
  976|  38.4k|            subheader_pointer_t shp_info = { 0 };
  977|  38.4k|            if ((retval = sas7bdat_parse_subheader_pointer(shp, page + page_size - shp, &shp_info, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (977:17): [True: 0, False: 38.4k]
  ------------------
  978|      0|                goto cleanup;
  979|      0|            }
  980|  38.4k|            if (shp_info.len > 0 && shp_info.compression != SAS_COMPRESSION_TRUNC) {
  ------------------
  |  |  124|  23.8k|#define SAS_COMPRESSION_TRUNC  0x01
  ------------------
  |  Branch (980:17): [True: 23.8k, False: 14.6k]
  |  Branch (980:37): [True: 19.2k, False: 4.56k]
  ------------------
  981|  19.2k|                if ((retval = sas7bdat_validate_subheader_pointer(&shp_info, page_size, subheader_count, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (981:21): [True: 203, False: 19.0k]
  ------------------
  982|    203|                    goto cleanup;
  983|    203|                }
  984|  19.0k|                if (shp_info.compression == SAS_COMPRESSION_NONE) {
  ------------------
  |  |  123|  19.0k|#define SAS_COMPRESSION_NONE   0x00
  ------------------
  |  Branch (984:21): [True: 17.7k, False: 1.35k]
  ------------------
  985|  17.7k|                    sas_subheader_type_t subheader_type = sas7bdat_parse_subheader_type(page + shp_info.offset, ctx);
  986|  17.7k|                    if (shp_info.is_compressed_data && subheader_type == SAS_SUBHEADER_TYPE_DATA) {
  ------------------
  |  Branch (986:25): [True: 13.3k, False: 4.31k]
  |  Branch (986:56): [True: 589, False: 12.7k]
  ------------------
  987|    589|                        if (shp_info.len != ctx->row_length) {
  ------------------
  |  Branch (987:29): [True: 133, False: 456]
  ------------------
  988|    133|                            retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  989|    133|                            goto cleanup;
  990|    133|                        }
  991|    456|                        if ((retval = sas7bdat_submit_columns_if_needed(ctx, 1)) != READSTAT_OK) {
  ------------------
  |  Branch (991:29): [True: 10, False: 446]
  ------------------
  992|     10|                            goto cleanup;
  993|     10|                        }
  994|    446|                        if ((retval = sas7bdat_parse_single_row(page + shp_info.offset, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (994:29): [True: 10, False: 436]
  ------------------
  995|     10|                            goto cleanup;
  996|     10|                        }
  997|  17.1k|                    } else {
  998|  17.1k|                        if (subheader_type != SAS_SUBHEADER_TYPE_COLUMN_TEXT) {
  ------------------
  |  Branch (998:29): [True: 15.6k, False: 1.42k]
  ------------------
  999|  15.6k|                            if ((retval = sas7bdat_parse_subheader(subheader_type, page + shp_info.offset, shp_info.len, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (999:33): [True: 399, False: 15.2k]
  ------------------
 1000|    399|                                goto cleanup;
 1001|    399|                            }
 1002|  15.6k|                        }
 1003|  17.1k|                    }
 1004|  17.7k|                } else if (shp_info.compression == SAS_COMPRESSION_ROW) {
  ------------------
  |  |  125|  1.35k|#define SAS_COMPRESSION_ROW    0x04
  ------------------
  |  Branch (1004:28): [True: 1.34k, False: 9]
  ------------------
 1005|  1.34k|                    if ((retval = sas7bdat_submit_columns_if_needed(ctx, 1)) != READSTAT_OK) {
  ------------------
  |  Branch (1005:25): [True: 40, False: 1.30k]
  ------------------
 1006|     40|                        goto cleanup;
 1007|     40|                    }
 1008|  1.30k|                    if ((retval = sas7bdat_parse_subheader_compressed(page + shp_info.offset, shp_info.len, ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (1008:25): [True: 260, False: 1.04k]
  ------------------
 1009|    260|                        goto cleanup;
 1010|    260|                    }
 1011|  1.30k|                } else {
 1012|      9|                    retval = READSTAT_ERROR_UNSUPPORTED_COMPRESSION;
 1013|      9|                    goto cleanup;
 1014|      9|                }
 1015|  19.0k|            }
 1016|       |
 1017|  37.4k|            shp += lshp;
 1018|  37.4k|        }
 1019|       |
 1020|  2.07k|        if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_MIX) {
  ------------------
  |  |  112|  2.07k|#define SAS_PAGE_TYPE_MASK   0x0F00
  ------------------
                      if ((page_type & SAS_PAGE_TYPE_MASK) == SAS_PAGE_TYPE_MIX) {
  ------------------
  |  |  110|  2.07k|#define SAS_PAGE_TYPE_MIX    0x0200
  ------------------
  |  Branch (1020:13): [True: 1.12k, False: 943]
  ------------------
 1021|       |            /* HACK - this is supposed to obey 8-byte boundaries but
 1022|       |             * some files created by Stat/Transfer don't. So verify that the
 1023|       |             * padding is { 0, 0, 0, 0 } or { ' ', ' ', ' ', ' ' } (or that
 1024|       |             * the file is not from Stat/Transfer) before skipping it */
 1025|  1.12k|            if ((shp-page)%8 == 4 && shp + 4 <= page + page_size &&
  ------------------
  |  Branch (1025:17): [True: 395, False: 732]
  |  Branch (1025:38): [True: 392, False: 3]
  ------------------
 1026|    392|                    (*(uint32_t *)shp == 0x00000000 ||
  ------------------
  |  Branch (1026:22): [True: 94, False: 298]
  ------------------
 1027|    298|                     *(uint32_t *)shp == 0x20202020 ||
  ------------------
  |  Branch (1027:22): [True: 40, False: 258]
  ------------------
 1028|    349|                     ctx->vendor != READSTAT_VENDOR_STAT_TRANSFER)) {
  ------------------
  |  Branch (1028:22): [True: 215, False: 43]
  ------------------
 1029|    349|                data = shp + 4;
 1030|    778|            } else {
 1031|    778|                data = shp;
 1032|    778|            }
 1033|  1.12k|        }
 1034|  2.07k|    }
 1035|  3.17k|    if (data) {
  ------------------
  |  Branch (1035:9): [True: 1.91k, False: 1.25k]
  ------------------
 1036|  1.91k|        if ((retval = sas7bdat_submit_columns_if_needed(ctx, 0)) != READSTAT_OK) {
  ------------------
  |  Branch (1036:13): [True: 116, False: 1.80k]
  ------------------
 1037|    116|            goto cleanup;
 1038|    116|        }
 1039|  1.80k|        if (ctx->handle.value) {
  ------------------
  |  Branch (1039:13): [True: 1.80k, False: 0]
  ------------------
 1040|  1.80k|            retval = sas7bdat_parse_rows(data, page + page_size - data, ctx);
 1041|  1.80k|        }
 1042|  1.80k|    } 
 1043|  4.25k|cleanup:
 1044|       |
 1045|  4.25k|    return retval;
 1046|  3.17k|}
readstat_sas7bdat_read.c:sas7bdat_parse_single_row:
  453|   223k|static readstat_error_t sas7bdat_parse_single_row(const char *data, sas7bdat_ctx_t *ctx) {
  454|   223k|    if (ctx->parsed_row_count == ctx->row_limit)
  ------------------
  |  Branch (454:9): [True: 347, False: 222k]
  ------------------
  455|    347|        return READSTAT_OK;
  456|   222k|    if (ctx->row_offset) {
  ------------------
  |  Branch (456:9): [True: 0, False: 222k]
  ------------------
  457|      0|        ctx->row_offset--;
  458|      0|        return READSTAT_OK;
  459|      0|    }
  460|       |
  461|   222k|    readstat_error_t retval = READSTAT_OK;
  462|   222k|    int j;
  463|   222k|    if (ctx->handle.value) {
  ------------------
  |  Branch (463:9): [True: 222k, False: 0]
  ------------------
  464|   222k|        ctx->scratch_buffer_len = 4*ctx->max_col_width+1;
  465|   222k|        ctx->scratch_buffer = readstat_realloc(ctx->scratch_buffer, ctx->scratch_buffer_len);
  466|   222k|        if (ctx->scratch_buffer == NULL) {
  ------------------
  |  Branch (466:13): [True: 3, False: 222k]
  ------------------
  467|      3|            retval = READSTAT_ERROR_MALLOC;
  468|      3|            goto cleanup;
  469|      3|        }
  470|       |
  471|  6.45M|        for (j=0; j<ctx->column_count; j++) {
  ------------------
  |  Branch (471:19): [True: 6.23M, False: 222k]
  ------------------
  472|  6.23M|            col_info_t *col_info = &ctx->col_info[j];
  473|  6.23M|            readstat_variable_t *variable = ctx->variables[j];
  474|  6.23M|            if (variable->skip)
  ------------------
  |  Branch (474:17): [True: 0, False: 6.23M]
  ------------------
  475|      0|                continue;
  476|       |
  477|  6.23M|            if (col_info->offset > ctx->row_length || col_info->offset + col_info->width > ctx->row_length) {
  ------------------
  |  Branch (477:17): [True: 92, False: 6.23M]
  |  Branch (477:55): [True: 18, False: 6.23M]
  ------------------
  478|    110|                retval = READSTAT_ERROR_PARSE;
  479|    110|                goto cleanup;
  480|    110|            }
  481|  6.23M|            retval = sas7bdat_handle_data_value(variable, col_info, &data[col_info->offset], ctx);
  482|  6.23M|            if (retval != READSTAT_OK) {
  ------------------
  |  Branch (482:17): [True: 17, False: 6.23M]
  ------------------
  483|     17|                goto cleanup;
  484|     17|            }
  485|  6.23M|        }
  486|   222k|    }
  487|   222k|    ctx->parsed_row_count++;
  488|       |
  489|   222k|cleanup:
  490|   222k|    return retval;
  491|   222k|}
readstat_sas7bdat_read.c:sas7bdat_handle_data_value:
  397|  6.23M|        col_info_t *col_info, const char *col_data, sas7bdat_ctx_t *ctx) {
  398|  6.23M|    readstat_error_t retval = READSTAT_OK;
  399|  6.23M|    int cb_retval = 0;
  400|  6.23M|    readstat_value_t value;
  401|  6.23M|    memset(&value, 0, sizeof(readstat_value_t));
  402|       |
  403|  6.23M|    value.type = col_info->type;
  404|       |
  405|  6.23M|    if (col_info->type == READSTAT_TYPE_STRING) {
  ------------------
  |  Branch (405:9): [True: 6.11M, False: 123k]
  ------------------
  406|  6.11M|        retval = readstat_convert(ctx->scratch_buffer, ctx->scratch_buffer_len,
  407|  6.11M|                col_data, col_info->width, ctx->converter);
  408|  6.11M|        if (retval != READSTAT_OK) {
  ------------------
  |  Branch (408:13): [True: 17, False: 6.11M]
  ------------------
  409|     17|            if (ctx->handle.error) {
  ------------------
  |  Branch (409:17): [True: 0, False: 17]
  ------------------
  410|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf),
  411|      0|                        "ReadStat: Error converting string (row=%u, col=%u) to specified encoding: %.*s",
  412|      0|                        ctx->parsed_row_count+1, col_info->index+1, col_info->width, col_data);
  413|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
  414|      0|            }
  415|     17|            goto cleanup;
  416|     17|        }
  417|       |
  418|  6.11M|        value.v.string_value = ctx->scratch_buffer;
  419|  6.11M|    } else if (col_info->type == READSTAT_TYPE_DOUBLE) {
  ------------------
  |  Branch (419:16): [True: 123k, False: 0]
  ------------------
  420|   123k|        uint64_t  val = 0;
  421|   123k|        double dval = NAN;
  422|   123k|        if (ctx->little_endian) {
  ------------------
  |  Branch (422:13): [True: 123k, False: 0]
  ------------------
  423|   123k|            int k;
  424|   798k|            for (k=0; k<col_info->width; k++) {
  ------------------
  |  Branch (424:23): [True: 675k, False: 123k]
  ------------------
  425|   675k|                val = (val << 8) | (unsigned char)col_data[col_info->width-1-k];
  426|   675k|            }
  427|   123k|        } else {
  428|      0|            int k;
  429|      0|            for (k=0; k<col_info->width; k++) {
  ------------------
  |  Branch (429:23): [True: 0, False: 0]
  ------------------
  430|      0|                val = (val << 8) | (unsigned char)col_data[k];
  431|      0|            }
  432|      0|        }
  433|   123k|        val <<= (8-col_info->width)*8;
  434|       |
  435|   123k|        memcpy(&dval, &val, 8);
  436|       |
  437|   123k|        if (isnan(dval)) {
  ------------------
  |  Branch (437:13): [True: 8.00k, False: 115k]
  ------------------
  438|  8.00k|            value.v.double_value = NAN;
  439|  8.00k|            sas_assign_tag(&value, ~((val >> 40) & 0xFF));
  440|   115k|        } else {
  441|   115k|            value.v.double_value = dval;
  442|   115k|        }
  443|   123k|    }
  444|  6.23M|    cb_retval = ctx->handle.value(ctx->parsed_row_count, variable, value, ctx->user_ctx);
  445|       |
  446|  6.23M|    if (cb_retval != READSTAT_HANDLER_OK)
  ------------------
  |  Branch (446:9): [True: 0, False: 6.23M]
  ------------------
  447|      0|        retval = READSTAT_ERROR_USER_ABORT;
  448|       |
  449|  6.23M|cleanup:
  450|  6.23M|    return retval;
  451|  6.23M|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader_compressed:
  626|  1.30k|static readstat_error_t sas7bdat_parse_subheader_compressed(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  627|  1.30k|    if (ctx->rdc_compression)
  ------------------
  |  Branch (627:9): [True: 327, False: 978]
  ------------------
  628|    327|        return sas7bdat_parse_subheader_rdc(subheader, len, ctx);
  629|       |
  630|    978|    return sas7bdat_parse_subheader_rle(subheader, len, ctx);
  631|  1.30k|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader_rdc:
  512|    327|static readstat_error_t sas7bdat_parse_subheader_rdc(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  513|    327|    readstat_error_t retval = READSTAT_OK;
  514|    327|    const unsigned char *input = (const unsigned char *)subheader;
  515|    327|    char *buffer = malloc(ctx->row_length);
  516|    327|    if (buffer == NULL) return READSTAT_ERROR_MALLOC;
  ------------------
  |  Branch (516:9): [True: 0, False: 327]
  ------------------
  517|    327|    char *output = buffer;
  518|  1.99k|    while (input + 2 <= (const unsigned char *)subheader + len) {
  ------------------
  |  Branch (518:12): [True: 1.69k, False: 297]
  ------------------
  519|  1.69k|        int i;
  520|  1.69k|        unsigned short prefix = (input[0] << 8) + input[1];
  521|  1.69k|        input += 2;
  522|  25.2k|        for (i=0; i<16; i++) {
  ------------------
  |  Branch (522:19): [True: 23.8k, False: 1.38k]
  ------------------
  523|  23.8k|            if ((prefix & (1 << (15 - i))) == 0) {
  ------------------
  |  Branch (523:17): [True: 17.7k, False: 6.11k]
  ------------------
  524|  17.7k|                if (input + 1 > (const unsigned char *)subheader + len) {
  ------------------
  |  Branch (524:21): [True: 282, False: 17.4k]
  ------------------
  525|    282|                    break;
  526|    282|                }
  527|  17.4k|                if (output + 1 > buffer + ctx->row_length) {
  ------------------
  |  Branch (527:21): [True: 2, False: 17.4k]
  ------------------
  528|      2|                    retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  529|      2|                    goto cleanup;
  530|      2|                }
  531|  17.4k|                *output++ = *input++;
  532|  17.4k|                continue;
  533|  17.4k|            }
  534|       |
  535|  6.11k|            if (input + 2 > (const unsigned char *)subheader + len) {
  ------------------
  |  Branch (535:17): [True: 2, False: 6.11k]
  ------------------
  536|      2|                retval = READSTAT_ERROR_PARSE;
  537|      2|                goto cleanup;
  538|      2|            }
  539|       |
  540|  6.11k|            unsigned char marker_byte = *input++;
  541|  6.11k|            unsigned char next_byte = *input++;
  542|  6.11k|            size_t insert_len = 0, copy_len = 0;
  543|  6.11k|            unsigned char insert_byte = 0x00;
  544|  6.11k|            size_t back_offset = 0;
  545|       |
  546|  6.11k|            if (marker_byte <= 0x0F) {
  ------------------
  |  Branch (546:17): [True: 2.58k, False: 3.52k]
  ------------------
  547|  2.58k|                insert_len = 3 + marker_byte;
  548|  2.58k|                insert_byte = next_byte;
  549|  3.52k|            } else if ((marker_byte >> 4) == 1) {
  ------------------
  |  Branch (549:24): [True: 1.12k, False: 2.40k]
  ------------------
  550|  1.12k|                if (input + 1 > (const unsigned char *)subheader + len) {
  ------------------
  |  Branch (550:21): [True: 2, False: 1.12k]
  ------------------
  551|      2|                    retval = READSTAT_ERROR_PARSE;
  552|      2|                    goto cleanup;
  553|      2|                }
  554|  1.12k|                insert_len = 19 + (marker_byte & 0x0F) + next_byte * 16;
  555|  1.12k|                insert_byte = *input++;
  556|  2.40k|            } else if ((marker_byte >> 4) == 2) {
  ------------------
  |  Branch (556:24): [True: 526, False: 1.87k]
  ------------------
  557|    526|                if (input + 1 > (const unsigned char *)subheader + len) {
  ------------------
  |  Branch (557:21): [True: 1, False: 525]
  ------------------
  558|      1|                    retval = READSTAT_ERROR_PARSE;
  559|      1|                    goto cleanup;
  560|      1|                }
  561|    525|                copy_len = 16 + (*input++);
  562|    525|                back_offset = 3 + (marker_byte & 0x0F) + next_byte * 16;
  563|  1.87k|            } else {
  564|  1.87k|                copy_len = (marker_byte >> 4);
  565|  1.87k|                back_offset = 3 + (marker_byte & 0x0F) + next_byte * 16;
  566|  1.87k|            }
  567|       |
  568|  6.10k|            if (insert_len) {
  ------------------
  |  Branch (568:17): [True: 3.71k, False: 2.39k]
  ------------------
  569|  3.71k|                if (output + insert_len > buffer + ctx->row_length) {
  ------------------
  |  Branch (569:21): [True: 1, False: 3.70k]
  ------------------
  570|      1|                    retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  571|      1|                    goto cleanup;
  572|      1|                }
  573|  3.70k|                memset(output, insert_byte, insert_len);
  574|  3.70k|                output += insert_len;
  575|  3.70k|            } else if (copy_len) {
  ------------------
  |  Branch (575:24): [True: 2.39k, False: 0]
  ------------------
  576|  2.39k|                if (output - buffer < back_offset || copy_len > back_offset) {
  ------------------
  |  Branch (576:21): [True: 12, False: 2.38k]
  |  Branch (576:54): [True: 8, False: 2.37k]
  ------------------
  577|     20|                    retval = READSTAT_ERROR_PARSE;
  578|     20|                    goto cleanup;
  579|     20|                }
  580|  2.37k|                if (output + copy_len > buffer + ctx->row_length) {
  ------------------
  |  Branch (580:21): [True: 2, False: 2.37k]
  ------------------
  581|      2|                    retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  582|      2|                    goto cleanup;
  583|      2|                }
  584|  2.37k|                memcpy(output, output - back_offset, copy_len);
  585|  2.37k|                output += copy_len;
  586|  2.37k|            }
  587|  6.10k|        }
  588|  1.69k|    }
  589|       |
  590|    297|    if (output - buffer != ctx->row_length) {
  ------------------
  |  Branch (590:9): [True: 31, False: 266]
  ------------------
  591|     31|        retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  592|     31|        goto cleanup;
  593|     31|    }
  594|    266|    retval = sas7bdat_parse_single_row(buffer, ctx);
  595|    327|cleanup:
  596|    327|    free(buffer);
  597|       |
  598|    327|    return retval;
  599|    266|}
readstat_sas7bdat_read.c:sas7bdat_parse_subheader_rle:
  601|    978|static readstat_error_t sas7bdat_parse_subheader_rle(const char *subheader, size_t len, sas7bdat_ctx_t *ctx) {
  602|    978|    if (ctx->row_limit == ctx->parsed_row_count)
  ------------------
  |  Branch (602:9): [True: 517, False: 461]
  ------------------
  603|    517|        return READSTAT_OK;
  604|       |
  605|    461|    readstat_error_t retval = READSTAT_OK;
  606|    461|    ssize_t bytes_decompressed = 0;
  607|       |
  608|    461|    bytes_decompressed = sas_rle_decompress(ctx->row, ctx->row_length, subheader, len);
  609|       |
  610|    461|    if (bytes_decompressed != ctx->row_length) {
  ------------------
  |  Branch (610:9): [True: 157, False: 304]
  ------------------
  611|    157|        retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  612|    157|        if (ctx->handle.error) {
  ------------------
  |  Branch (612:13): [True: 0, False: 157]
  ------------------
  613|      0|            snprintf(ctx->error_buf, sizeof(ctx->error_buf), 
  614|      0|                    "ReadStat: Row #%d decompressed to %ld bytes (expected %d bytes)",
  615|      0|                    ctx->parsed_row_count, (long)(bytes_decompressed), ctx->row_length);
  616|      0|            ctx->handle.error(ctx->error_buf, ctx->user_ctx);
  617|      0|        }
  618|    157|        goto cleanup;
  619|    157|    }
  620|    304|    retval = sas7bdat_parse_single_row(ctx->row, ctx);
  621|       |
  622|    461|cleanup:
  623|    461|    return retval;
  624|    304|}
readstat_sas7bdat_read.c:sas7bdat_parse_rows:
  493|  1.80k|static readstat_error_t sas7bdat_parse_rows(const char *data, size_t len, sas7bdat_ctx_t *ctx) {
  494|  1.80k|    readstat_error_t retval = READSTAT_OK;
  495|  1.80k|    int i;
  496|  1.80k|    size_t row_offset=0;
  497|   223k|    for (i=0; i<ctx->page_row_count && ctx->parsed_row_count < ctx->row_limit; i++) {
  ------------------
  |  Branch (497:15): [True: 222k, False: 1.57k]
  |  Branch (497:40): [True: 222k, False: 45]
  ------------------
  498|   222k|        if (row_offset + ctx->row_length > len) {
  ------------------
  |  Branch (498:13): [True: 104, False: 222k]
  ------------------
  499|    104|            retval = READSTAT_ERROR_ROW_WIDTH_MISMATCH;
  500|    104|            goto cleanup;
  501|    104|        }
  502|   222k|        if ((retval = sas7bdat_parse_single_row(&data[row_offset], ctx)) != READSTAT_OK)
  ------------------
  |  Branch (502:13): [True: 78, False: 222k]
  ------------------
  503|     78|            goto cleanup;
  504|       |
  505|   222k|        row_offset += ctx->row_length;
  506|   222k|    }
  507|       |
  508|  1.80k|cleanup:
  509|  1.80k|    return retval;
  510|  1.80k|}
readstat_sas7bdat_read.c:sas7bdat_submit_columns_if_needed:
  800|  4.05k|static readstat_error_t sas7bdat_submit_columns_if_needed(sas7bdat_ctx_t *ctx, int compressed) {
  801|  4.05k|    readstat_error_t retval = READSTAT_OK;
  802|  4.05k|    if (!ctx->did_submit_columns) {
  ------------------
  |  Branch (802:9): [True: 1.31k, False: 2.74k]
  ------------------
  803|  1.31k|        if ((retval = sas7bdat_submit_columns(ctx, compressed)) != READSTAT_OK) {
  ------------------
  |  Branch (803:13): [True: 254, False: 1.05k]
  ------------------
  804|    254|            goto cleanup;
  805|    254|        }
  806|  1.05k|        ctx->did_submit_columns = 1;
  807|  1.05k|    }
  808|  4.05k|cleanup:
  809|  4.05k|    return retval;
  810|  4.05k|}
readstat_sas7bdat_read.c:sas7bdat_submit_columns:
  739|  1.31k|static readstat_error_t sas7bdat_submit_columns(sas7bdat_ctx_t *ctx, int compressed) {
  740|  1.31k|    readstat_error_t retval = READSTAT_OK;
  741|  1.31k|    if (ctx->handle.metadata) {
  ------------------
  |  Branch (741:9): [True: 1.31k, False: 0]
  ------------------
  742|  1.31k|        readstat_metadata_t metadata = {
  743|  1.31k|            .row_count = ctx->row_limit,
  744|  1.31k|            .var_count = ctx->column_count,
  745|  1.31k|            .table_name = ctx->table_name,
  746|  1.31k|            .file_label = ctx->file_label,
  747|  1.31k|            .file_encoding = ctx->input_encoding, /* orig encoding? */
  748|  1.31k|            .creation_time = ctx->ctime,
  749|  1.31k|            .modified_time = ctx->mtime,
  750|  1.31k|            .file_format_version = ctx->version,
  751|  1.31k|            .compression = READSTAT_COMPRESS_NONE,
  752|  1.31k|            .endianness = ctx->little_endian ? READSTAT_ENDIAN_LITTLE : READSTAT_ENDIAN_BIG,
  ------------------
  |  Branch (752:27): [True: 257, False: 1.05k]
  ------------------
  753|  1.31k|            .is64bit = ctx->u64
  754|  1.31k|        };
  755|  1.31k|        if (compressed) {
  ------------------
  |  Branch (755:13): [True: 527, False: 785]
  ------------------
  756|    527|            if (ctx->rdc_compression) {
  ------------------
  |  Branch (756:17): [True: 57, False: 470]
  ------------------
  757|     57|                metadata.compression = READSTAT_COMPRESS_BINARY;
  758|    470|            } else {
  759|    470|                metadata.compression = READSTAT_COMPRESS_ROWS;
  760|    470|            }
  761|    527|        }
  762|  1.31k|        if (ctx->handle.metadata(&metadata, ctx->user_ctx) != READSTAT_HANDLER_OK) {
  ------------------
  |  Branch (762:13): [True: 0, False: 1.31k]
  ------------------
  763|      0|            retval = READSTAT_ERROR_USER_ABORT;
  764|      0|            goto cleanup;
  765|      0|        }
  766|  1.31k|    }
  767|  1.31k|    if (ctx->column_count == 0)
  ------------------
  |  Branch (767:9): [True: 824, False: 488]
  ------------------
  768|    824|        goto cleanup;
  769|       |
  770|    488|    if ((ctx->variables = readstat_calloc(ctx->column_count, sizeof(readstat_variable_t *))) == NULL) {
  ------------------
  |  Branch (770:9): [True: 0, False: 488]
  ------------------
  771|      0|        retval = READSTAT_ERROR_MALLOC;
  772|      0|        goto cleanup;
  773|      0|    }
  774|    488|    int i;
  775|    488|    int index_after_skipping = 0;
  776|  3.11M|    for (i=0; i<ctx->column_count; i++) {
  ------------------
  |  Branch (776:15): [True: 3.11M, False: 234]
  ------------------
  777|  3.11M|        ctx->variables[i] = sas7bdat_init_variable(ctx, i, index_after_skipping, &retval);
  778|  3.11M|        if (ctx->variables[i] == NULL)
  ------------------
  |  Branch (778:13): [True: 254, False: 3.11M]
  ------------------
  779|    254|            break;
  780|       |
  781|  3.11M|        int cb_retval = READSTAT_HANDLER_OK;
  782|  3.11M|        if (ctx->handle.variable) {
  ------------------
  |  Branch (782:13): [True: 3.11M, False: 0]
  ------------------
  783|  3.11M|            cb_retval = ctx->handle.variable(i, ctx->variables[i], ctx->variables[i]->format, ctx->user_ctx);
  784|  3.11M|        }
  785|  3.11M|        if (cb_retval == READSTAT_HANDLER_ABORT) {
  ------------------
  |  Branch (785:13): [True: 0, False: 3.11M]
  ------------------
  786|      0|            retval = READSTAT_ERROR_USER_ABORT;
  787|      0|            goto cleanup;
  788|      0|        }
  789|       |
  790|  3.11M|        if (cb_retval == READSTAT_HANDLER_SKIP_VARIABLE) {
  ------------------
  |  Branch (790:13): [True: 0, False: 3.11M]
  ------------------
  791|      0|            ctx->variables[i]->skip = 1;
  792|  3.11M|        } else {
  793|  3.11M|            index_after_skipping++;
  794|  3.11M|        }
  795|  3.11M|    }
  796|  1.31k|cleanup:
  797|  1.31k|    return retval;
  798|    488|}
readstat_sas7bdat_read.c:sas7bdat_init_variable:
  683|  3.11M|        int index_after_skipping, readstat_error_t *out_retval) {
  684|  3.11M|    readstat_error_t retval = READSTAT_OK;
  685|  3.11M|    readstat_variable_t *variable = readstat_calloc(1, sizeof(readstat_variable_t));
  686|       |
  687|  3.11M|    variable->index = i;
  688|  3.11M|    variable->index_after_skipping = index_after_skipping;
  689|  3.11M|    variable->type = ctx->col_info[i].type;
  690|  3.11M|    variable->storage_width = ctx->col_info[i].width;
  691|       |
  692|  3.11M|    if ((retval = sas7bdat_validate_column(&ctx->col_info[i])) != READSTAT_OK) {
  ------------------
  |  Branch (692:9): [True: 87, False: 3.11M]
  ------------------
  693|     87|        goto cleanup;
  694|     87|    }
  695|  3.11M|    if ((retval = sas7bdat_copy_text_ref(variable->name, sizeof(variable->name), 
  ------------------
  |  Branch (695:9): [True: 54, False: 3.11M]
  ------------------
  696|  3.11M|                    ctx->col_info[i].name_ref, ctx)) != READSTAT_OK) {
  697|     54|        goto cleanup;
  698|     54|    }
  699|  3.11M|    if ((retval = sas7bdat_copy_text_ref(variable->format, sizeof(variable->format),
  ------------------
  |  Branch (699:9): [True: 54, False: 3.11M]
  ------------------
  700|  3.11M|                    ctx->col_info[i].format_ref, ctx)) != READSTAT_OK) {
  701|     54|        goto cleanup;
  702|     54|    }
  703|  3.11M|    size_t len = strlen(variable->format);
  704|  3.11M|    if (ctx->col_info[i].format_width) {
  ------------------
  |  Branch (704:9): [True: 536, False: 3.11M]
  ------------------
  705|    536|        len += snprintf(variable->format + len, sizeof(variable->format) - len,
  706|    536|                "%d", ctx->col_info[i].format_width);
  707|    536|    }
  708|  3.11M|    if (len && ctx->col_info[i].format_digits) {
  ------------------
  |  Branch (708:9): [True: 567, False: 3.11M]
  |  Branch (708:16): [True: 409, False: 158]
  ------------------
  709|    409|        len += snprintf(variable->format + len, sizeof(variable->format) - len,
  710|    409|                ".%d", ctx->col_info[i].format_digits);
  711|    409|    }
  712|  3.11M|    if ((retval = sas7bdat_copy_text_ref(variable->label, sizeof(variable->label), 
  ------------------
  |  Branch (712:9): [True: 59, False: 3.11M]
  ------------------
  713|  3.11M|                    ctx->col_info[i].label_ref, ctx)) != READSTAT_OK) {
  714|     59|        goto cleanup;
  715|     59|    }
  716|       |
  717|  3.11M|cleanup:
  718|  3.11M|    if (retval != READSTAT_OK) {
  ------------------
  |  Branch (718:9): [True: 254, False: 3.11M]
  ------------------
  719|    254|        if (out_retval)
  ------------------
  |  Branch (719:13): [True: 254, False: 0]
  ------------------
  720|    254|            *out_retval = retval;
  721|       |
  722|    254|        if (retval == READSTAT_ERROR_CONVERT_BAD_STRING) {
  ------------------
  |  Branch (722:13): [True: 7, False: 247]
  ------------------
  723|      7|            if (ctx->handle.error) {
  ------------------
  |  Branch (723:17): [True: 0, False: 7]
  ------------------
  724|      0|                snprintf(ctx->error_buf, sizeof(ctx->error_buf),
  725|      0|                        "ReadStat: Error converting variable #%d info to specified encoding: %s %s (%s)",
  726|      0|                        i, variable->name, variable->format, variable->label);
  727|      0|                ctx->handle.error(ctx->error_buf, ctx->user_ctx);
  728|      0|            }
  729|      7|        }
  730|       |
  731|    254|        free(variable);
  732|       |
  733|    254|        return NULL;
  734|    254|    }
  735|       |
  736|  3.11M|    return variable;
  737|  3.11M|}
readstat_sas7bdat_read.c:sas7bdat_validate_column:
  668|  3.11M|static readstat_error_t sas7bdat_validate_column(col_info_t *col_info) {
  669|  3.11M|    if (col_info->type == READSTAT_TYPE_DOUBLE) {
  ------------------
  |  Branch (669:9): [True: 886, False: 3.11M]
  ------------------
  670|    886|        if (col_info->width > 8 || col_info->width < 3) {
  ------------------
  |  Branch (670:13): [True: 48, False: 838]
  |  Branch (670:36): [True: 1, False: 837]
  ------------------
  671|     49|            return READSTAT_ERROR_PARSE;
  672|     49|        }
  673|    886|    }
  674|  3.11M|    if (col_info->type == READSTAT_TYPE_STRING) {
  ------------------
  |  Branch (674:9): [True: 3.11M, False: 837]
  ------------------
  675|  3.11M|        if (col_info->width > INT16_MAX) {
  ------------------
  |  Branch (675:13): [True: 38, False: 3.11M]
  ------------------
  676|     38|            return READSTAT_ERROR_PARSE;
  677|     38|        }
  678|  3.11M|    }
  679|  3.11M|    return READSTAT_OK;
  680|  3.11M|}
readstat_sas7bdat_read.c:sas7bdat_update_progress:
  128|  4.51k|static readstat_error_t sas7bdat_update_progress(sas7bdat_ctx_t *ctx) {
  129|  4.51k|    readstat_io_t *io = ctx->io;
  130|  4.51k|    return io->update(ctx->file_size, ctx->handle.progress, ctx->user_ctx, io->io_ctx);
  131|  4.51k|}
readstat_sas7bdat_read.c:sas7bdat_ctx_free:
   94|  3.56k|static void sas7bdat_ctx_free(sas7bdat_ctx_t *ctx) {
   95|  3.56k|    int i;
   96|  3.56k|    if (ctx->text_blobs) {
  ------------------
  |  Branch (96:9): [True: 855, False: 2.70k]
  ------------------
   97|  3.37k|        for (i=0; i<ctx->text_blob_count; i++) {
  ------------------
  |  Branch (97:19): [True: 2.52k, False: 855]
  ------------------
   98|  2.52k|            free(ctx->text_blobs[i]);
   99|  2.52k|        }
  100|    855|        free(ctx->text_blobs);
  101|    855|        free(ctx->text_blob_lengths);
  102|    855|    }
  103|  3.56k|    if (ctx->variables) {
  ------------------
  |  Branch (103:9): [True: 488, False: 3.07k]
  ------------------
  104|  16.1M|        for (i=0; i<ctx->column_count; i++) {
  ------------------
  |  Branch (104:19): [True: 16.1M, False: 488]
  ------------------
  105|  16.1M|            if (ctx->variables[i])
  ------------------
  |  Branch (105:17): [True: 3.11M, False: 13.0M]
  ------------------
  106|  3.11M|                free(ctx->variables[i]);
  107|  16.1M|        }
  108|    488|        free(ctx->variables);
  109|    488|    }
  110|  3.56k|    if (ctx->col_info)
  ------------------
  |  Branch (110:9): [True: 645, False: 2.91k]
  ------------------
  111|    645|        free(ctx->col_info);
  112|       |
  113|  3.56k|    if (ctx->scratch_buffer)
  ------------------
  |  Branch (113:9): [True: 533, False: 3.03k]
  ------------------
  114|    533|        free(ctx->scratch_buffer);
  115|       |
  116|  3.56k|    if (ctx->page)
  ------------------
  |  Branch (116:9): [True: 3.04k, False: 518]
  ------------------
  117|  3.04k|        free(ctx->page);
  118|       |
  119|  3.56k|    if (ctx->row)
  ------------------
  |  Branch (119:9): [True: 991, False: 2.57k]
  ------------------
  120|    991|        free(ctx->row);
  121|       |
  122|  3.56k|    if (ctx->converter)
  ------------------
  |  Branch (122:9): [True: 2.13k, False: 1.43k]
  ------------------
  123|  2.13k|        iconv_close(ctx->converter);
  124|       |
  125|  3.56k|    free(ctx);
  126|  3.56k|}

sas_rle_decompress:
   47|    461|        const void *input_buf, size_t input_len) {
   48|    461|    unsigned char *buffer = (unsigned char *)output_buf;
   49|    461|    unsigned char *output = buffer;
   50|    461|    size_t output_written = 0;
   51|       |
   52|    461|    const unsigned char *input = (const unsigned char *)input_buf;
   53|       |
   54|  24.1k|    while (input < (const unsigned char *)input_buf + input_len) {
  ------------------
  |  Branch (54:12): [True: 23.8k, False: 330]
  ------------------
   55|  23.8k|        unsigned char control = *input++;
   56|  23.8k|        unsigned char command = (control & 0xF0) >> 4;
   57|  23.8k|        unsigned char length = (control & 0x0F);
   58|  23.8k|        int copy_len = 0;
   59|  23.8k|        int insert_len = 0;
   60|  23.8k|        unsigned char insert_byte = '\0';
   61|  23.8k|        if (input + command_lengths[command] > (const unsigned char *)input_buf + input_len) {
  ------------------
  |  Branch (61:13): [True: 4, False: 23.8k]
  ------------------
   62|      4|            return -1;
   63|      4|        }
   64|  23.8k|        switch (command) {
   65|  2.82k|            case SAS_RLE_COMMAND_COPY64:
  ------------------
  |  |   13|  2.82k|#define SAS_RLE_COMMAND_COPY64          0
  ------------------
  |  Branch (65:13): [True: 2.82k, False: 20.9k]
  ------------------
   66|  2.82k|                copy_len = (*input++) + 64 + length * 256;
   67|  2.82k|                break;
   68|    129|            case SAS_RLE_COMMAND_COPY64_PLUS_4096:
  ------------------
  |  |   14|    129|#define SAS_RLE_COMMAND_COPY64_PLUS_4096 1
  ------------------
  |  Branch (68:13): [True: 129, False: 23.6k]
  ------------------
   69|    129|                copy_len = (*input++) + 64 + length * 256 + 4096;
   70|    129|                break;
   71|    591|            case SAS_RLE_COMMAND_COPY96: copy_len = length + 96; break;
  ------------------
  |  |   15|    591|#define SAS_RLE_COMMAND_COPY96          2
  ------------------
  |  Branch (71:13): [True: 591, False: 23.2k]
  ------------------
   72|    709|            case SAS_RLE_COMMAND_INSERT_BYTE18:
  ------------------
  |  |   16|    709|#define SAS_RLE_COMMAND_INSERT_BYTE18   4
  ------------------
  |  Branch (72:13): [True: 709, False: 23.1k]
  ------------------
   73|    709|                insert_len = (*input++) + 18 + length * 256;
   74|    709|                insert_byte = *input++;
   75|    709|                break;
   76|    993|            case SAS_RLE_COMMAND_INSERT_AT17:
  ------------------
  |  |   17|    993|#define SAS_RLE_COMMAND_INSERT_AT17     5
  ------------------
  |  Branch (76:13): [True: 993, False: 22.8k]
  ------------------
   77|    993|                insert_len = (*input++) + 17 + length * 256;
   78|    993|                insert_byte = '@';
   79|    993|                break;
   80|    868|            case SAS_RLE_COMMAND_INSERT_BLANK17:
  ------------------
  |  |   18|    868|#define SAS_RLE_COMMAND_INSERT_BLANK17  6
  ------------------
  |  Branch (80:13): [True: 868, False: 22.9k]
  ------------------
   81|    868|                insert_len = (*input++) + 17 + length * 256;
   82|    868|                insert_byte = ' ';
   83|    868|                break;
   84|  1.79k|            case SAS_RLE_COMMAND_INSERT_ZERO17:
  ------------------
  |  |   19|  1.79k|#define SAS_RLE_COMMAND_INSERT_ZERO17   7
  ------------------
  |  Branch (84:13): [True: 1.79k, False: 22.0k]
  ------------------
   85|  1.79k|                insert_len = (*input++) + 17 + length * 256;
   86|  1.79k|                insert_byte = '\0';
   87|  1.79k|                break;
   88|    937|            case SAS_RLE_COMMAND_COPY1:  copy_len = length + 1; break;
  ------------------
  |  |   20|    937|#define SAS_RLE_COMMAND_COPY1           8
  ------------------
  |  Branch (88:13): [True: 937, False: 22.8k]
  ------------------
   89|    363|            case SAS_RLE_COMMAND_COPY17: copy_len = length + 17; break;
  ------------------
  |  |   21|    363|#define SAS_RLE_COMMAND_COPY17          9
  ------------------
  |  Branch (89:13): [True: 363, False: 23.4k]
  ------------------
   90|    536|            case SAS_RLE_COMMAND_COPY33: copy_len = length + 33; break;
  ------------------
  |  |   22|    536|#define SAS_RLE_COMMAND_COPY33         10
  ------------------
  |  Branch (90:13): [True: 536, False: 23.2k]
  ------------------
   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: 23.4k]
  ------------------
   92|    777|            case SAS_RLE_COMMAND_INSERT_BYTE3:
  ------------------
  |  |   24|    777|#define SAS_RLE_COMMAND_INSERT_BYTE3   12
  ------------------
  |  Branch (92:13): [True: 777, False: 23.0k]
  ------------------
   93|    777|                insert_byte = *input++;
   94|    777|                insert_len = length + 3;
   95|    777|                break;
   96|    812|            case SAS_RLE_COMMAND_INSERT_AT2:
  ------------------
  |  |   25|    812|#define SAS_RLE_COMMAND_INSERT_AT2     13
  ------------------
  |  Branch (96:13): [True: 812, False: 22.9k]
  ------------------
   97|    812|                insert_byte = '@';
   98|    812|                insert_len = length + 2;
   99|    812|                break;
  100|  4.39k|            case SAS_RLE_COMMAND_INSERT_BLANK2:
  ------------------
  |  |   26|  4.39k|#define SAS_RLE_COMMAND_INSERT_BLANK2  14
  ------------------
  |  Branch (100:13): [True: 4.39k, False: 19.4k]
  ------------------
  101|  4.39k|                insert_byte = ' ';
  102|  4.39k|                insert_len = length + 2;
  103|  4.39k|                break;
  104|  5.18k|            case SAS_RLE_COMMAND_INSERT_ZERO2:
  ------------------
  |  |   27|  5.18k|#define SAS_RLE_COMMAND_INSERT_ZERO2   15
  ------------------
  |  Branch (104:13): [True: 5.18k, False: 18.6k]
  ------------------
  105|  5.18k|                insert_byte = '\0';
  106|  5.18k|                insert_len = length + 2;
  107|  5.18k|                break;
  108|  2.55k|            default:
  ------------------
  |  Branch (108:13): [True: 2.55k, False: 21.2k]
  ------------------
  109|       |                /* error out here? */
  110|  2.55k|                break;
  111|  23.8k|        }
  112|  23.8k|        if (copy_len) {
  ------------------
  |  Branch (112:13): [True: 5.72k, False: 18.0k]
  ------------------
  113|  5.72k|            if (output_written + copy_len > output_len) {
  ------------------
  |  Branch (113:17): [True: 27, False: 5.69k]
  ------------------
  114|     27|                return -1;
  115|     27|            }
  116|  5.69k|            if (input + copy_len > (const unsigned char *)input_buf + input_len) {
  ------------------
  |  Branch (116:17): [True: 86, False: 5.61k]
  ------------------
  117|     86|                return -1;
  118|     86|            }
  119|  5.61k|            if (output) {
  ------------------
  |  Branch (119:17): [True: 5.61k, False: 0]
  ------------------
  120|  5.61k|                memcpy(&output[output_written], input, copy_len);
  121|  5.61k|            }
  122|  5.61k|            input += copy_len;
  123|  5.61k|            output_written += copy_len;
  124|  5.61k|        }
  125|  23.6k|        if (insert_len) {
  ------------------
  |  Branch (125:13): [True: 15.5k, False: 8.16k]
  ------------------
  126|  15.5k|            if (output_written + insert_len > output_len) {
  ------------------
  |  Branch (126:17): [True: 14, False: 15.5k]
  ------------------
  127|     14|                return -1;
  128|     14|            }
  129|  15.5k|            if (output) {
  ------------------
  |  Branch (129:17): [True: 15.5k, False: 0]
  ------------------
  130|  15.5k|                memset(&output[output_written], insert_byte, insert_len);
  131|  15.5k|            }
  132|  15.5k|            output_written += insert_len;
  133|  15.5k|        }
  134|  23.6k|    }
  135|       |
  136|    330|    return output_written;
  137|    461|}

rt_open_handler:
    8|  3.56k|int rt_open_handler(const char *path, void *io_ctx) {
    9|  3.56k|    return 0;
   10|  3.56k|}
rt_close_handler:
   12|  3.56k|int rt_close_handler(void *io_ctx) {
   13|  3.56k|    return 0;
   14|  3.56k|}
rt_seek_handler:
   17|  29.3k|        readstat_io_flags_t whence, void *io_ctx) {
   18|  29.3k|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   19|  29.3k|    readstat_off_t newpos = -1;
   20|  29.3k|    if (whence == READSTAT_SEEK_SET) {
  ------------------
  |  Branch (20:9): [True: 19.2k, False: 10.1k]
  ------------------
   21|  19.2k|        newpos = offset;
   22|  19.2k|    } else if (whence == READSTAT_SEEK_CUR) {
  ------------------
  |  Branch (22:16): [True: 6.62k, False: 3.56k]
  ------------------
   23|  6.62k|        newpos = buffer_ctx->pos + offset;
   24|  6.62k|    } else if (whence == READSTAT_SEEK_END) {
  ------------------
  |  Branch (24:16): [True: 3.56k, False: 0]
  ------------------
   25|  3.56k|        newpos = buffer_ctx->buffer->used + offset;
   26|  3.56k|    }
   27|       |
   28|  29.3k|    if (newpos < 0)
  ------------------
  |  Branch (28:9): [True: 134, False: 29.2k]
  ------------------
   29|    134|        return -1;
   30|       |
   31|  29.2k|    if (newpos > buffer_ctx->buffer->used)
  ------------------
  |  Branch (31:9): [True: 237, False: 29.0k]
  ------------------
   32|    237|        return -1;
   33|       |
   34|  29.0k|    buffer_ctx->pos = newpos;
   35|  29.0k|    return newpos;
   36|  29.2k|}
rt_read_handler:
   38|  51.4k|ssize_t rt_read_handler(void *buf, size_t nbytes, void *io_ctx) {
   39|  51.4k|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   40|  51.4k|    ssize_t bytes_copied = 0;
   41|  51.4k|    ssize_t bytes_left = buffer_ctx->buffer->used - buffer_ctx->pos;
   42|  51.4k|    if (nbytes <= bytes_left) {
  ------------------
  |  Branch (42:9): [True: 51.0k, False: 433]
  ------------------
   43|  51.0k|        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, nbytes);
   44|  51.0k|        bytes_copied = nbytes;
   45|  51.0k|    } else if (bytes_left > 0) {
  ------------------
  |  Branch (45:16): [True: 213, False: 220]
  ------------------
   46|    213|        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, bytes_left);
   47|    213|        bytes_copied = bytes_left;
   48|    213|    }
   49|  51.4k|    buffer_ctx->pos += bytes_copied;
   50|  51.4k|    return bytes_copied;
   51|  51.4k|}
rt_update_handler:
   54|  4.51k|        void *user_ctx, void *io_ctx) {
   55|  4.51k|    if (!progress_handler)
  ------------------
  |  Branch (55:9): [True: 4.51k, False: 0]
  ------------------
   56|  4.51k|        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|}

