From e7a0826cfe7ca292b626c98d6828eff4ef412cbd Mon Sep 17 00:00:00 2001 From: Yucheng Du <256645790+yucheng-du@users.noreply.github.com> Date: Tue, 1 Dec 2026 07:01:57 -0800 Subject: [PATCH 1/2] Stop reading XPORT rows at the next dataset --- src/sas/readstat_xport_read.c | 55 ++++++++++++++++++++++++++++++++++- src/test/test_list.h | 34 ++++++++++++++++++++++ src/test/test_readstat.c | 20 +++++++++++++ src/test/test_types.h | 1 + 4 files changed, 109 insertions(+), 1 deletion(-) diff --git a/src/sas/readstat_xport_read.c b/src/sas/readstat_xport_read.c index d3959619..a14d4258 100644 --- a/src/sas/readstat_xport_read.c +++ b/src/sas/readstat_xport_read.c @@ -119,7 +119,10 @@ static readstat_error_t xport_read_header_record(xport_ctx_t *ctx, xport_header_ &xrecord->num4, &xrecord->num5, &xrecord->num6); if (matches < 2) { - return READSTAT_ERROR_PARSE; + const char blank_obsv8[] = "HEADER RECORD*******OBSV8 HEADER RECORD!!!!!!!" + " "; + if (memcmp(line, blank_obsv8, sizeof(blank_obsv8)-1) != 0) + return READSTAT_ERROR_PARSE; } /* V8 files store the observation count as a 15-character field @@ -660,6 +663,47 @@ static readstat_error_t xport_process_row(xport_ctx_t *ctx, const char *row, siz return retval; } +static int xport_matches_header(const char *record, const char *name, int descriptor_size, int namestr_size) { + char expected[LINE_LEN + 1]; + snprintf(expected, sizeof(expected), "HEADER RECORD*******%-8.8sHEADER RECORD!!!!!!!%020d%010d ", + name, descriptor_size, namestr_size); + return memcmp(record, expected, LINE_LEN) == 0; +} + +/* A new member follows the last observation and its padding to an 80-byte record. */ +static readstat_error_t xport_check_next_member(xport_ctx_t *ctx, const char *row, size_t remaining, + int *is_member) { + const char prefix[] = "HEADER RECORD*******"; + size_t prefix_len = remaining < sizeof(prefix)-1 ? remaining : sizeof(prefix)-1; + if (memcmp(row, prefix, prefix_len) != 0) + return READSTAT_OK; + + readstat_io_t *io = ctx->io; + readstat_off_t row_end = io->seek(0, READSTAT_SEEK_CUR, io->io_ctx); + if (row_end == -1) + return READSTAT_ERROR_SEEK; + readstat_off_t start = row_end - (readstat_off_t)remaining; + char records[3 * LINE_LEN]; + if (start % LINE_LEN || ctx->file_size - start < sizeof(records)) + return READSTAT_OK; + if (io->seek(start, READSTAT_SEEK_SET, io->io_ctx) == -1) + return READSTAT_ERROR_SEEK; + if (read_bytes(ctx, records, sizeof(records)) != sizeof(records)) + return READSTAT_ERROR_READ; + if (io->seek(row_end, READSTAT_SEEK_SET, io->io_ctx) == -1) + return READSTAT_ERROR_SEEK; + + const char *member = ctx->version == 5 ? "MEMBER" : "MEMBV8"; + const char *descriptor = ctx->version == 5 ? "DSCRPTR" : "DSCPTV8"; + /* VAX/VMS used a 136-byte NAMESTR instead of the usual 140 bytes. */ + *is_member = (xport_matches_header(records, member, 160, 140) || + xport_matches_header(records, member, 160, 136)) && + xport_matches_header(records + LINE_LEN, descriptor, 0, 0) && + memcmp(records + 2 * LINE_LEN, "SAS ", 8) == 0 && + memcmp(records + 2 * LINE_LEN + (ctx->version == 5 ? 16 : 40), "SASDATA ", 8) == 0; + return READSTAT_OK; +} + static readstat_error_t xport_read_data(xport_ctx_t *ctx) { if (!ctx->row_length) return READSTAT_OK; @@ -710,6 +754,15 @@ static readstat_error_t xport_read_data(xport_ctx_t *ctx) { continue; } + if (ctx->obs_count == 0 && pos < LINE_LEN && row[pos] == 'H') { + int is_member = 0; + retval = xport_check_next_member(ctx, row + pos, ctx->row_length - pos, &is_member); + if (retval != READSTAT_OK) + goto cleanup; + if (is_member) + break; + } + while (num_blank_rows) { retval = xport_process_row(ctx, blank_row, ctx->row_length); if (retval != READSTAT_OK) diff --git a/src/test/test_list.h b/src/test/test_list.h index 0c466509..ef06ad93 100644 --- a/src/test/test_list.h +++ b/src/test/test_list.h @@ -82,6 +82,40 @@ static rt_test_group_t _test_groups[] = { } }, + { + .label = "XPORT multiple members", + .tests = { + { + .label = "First member ends in a partial record", + .test_formats = RT_FORMAT_XPORT, + .append_xport_member = 1, + .rows = 3, + .columns = {{ + .name = "TEXT", .type = READSTAT_TYPE_STRING, .user_width = 9, + .values = { + { .type = READSTAT_TYPE_STRING, .v = { .string_value = "a" } }, + { .type = READSTAT_TYPE_STRING, .v = { .string_value = "" } }, + { .type = READSTAT_TYPE_STRING, .v = { .string_value = "b" } } + } + }} + }, + { + .label = "Header text in a character variable", + .test_formats = RT_FORMAT_XPORT_5, + .append_xport_member = 1, + .rows = 3, + .columns = {{ + .name = "TEXT", .type = READSTAT_TYPE_STRING, .user_width = 80, + .values = { + { .type = READSTAT_TYPE_STRING, .v = { .string_value = + "HEADER RECORD*******MEMBER HEADER RECORD!!!!!!!000000000000000001600000000140" } }, + { .type = READSTAT_TYPE_STRING, .v = { .string_value = "another observation" } }, + { .type = READSTAT_TYPE_STRING, .v = { .string_value = "" } } + } + }} + } + } + }, { .label = "SAV unknown row count", .tests = { diff --git a/src/test/test_readstat.c b/src/test/test_readstat.c index 1b25cd50..7a25a982 100644 --- a/src/test/test_readstat.c +++ b/src/test/test_readstat.c @@ -278,6 +278,26 @@ int main(int argc, char *argv[]) { int32_t unknown = -1; memcpy(&buffer->bytes[80], &unknown, sizeof(unknown)); } + if (file->append_xport_member) { + /* Older V8 writers leave the observation count blank. */ + if (f == RT_FORMAT_XPORT_8) { + for (size_t offset=0; offsetused; offset+=80) { + if (memcmp(buffer->bytes + offset + 20, "OBSV8 ", 8) == 0) { + memset(buffer->bytes + offset + 48, ' ', 15); + break; + } + } + } + /* Repeat the member, leaving out the three library records. */ + size_t member_size = buffer->used - 3 * 80; + buffer_grow(buffer, member_size); + if (!buffer->bytes) { + error = READSTAT_ERROR_MALLOC; + goto cleanup; + } + memcpy(buffer->bytes + buffer->used, buffer->bytes + 3 * 80, member_size); + buffer->used += member_size; + } } else { error = buffer_read_from_resource(buffer, file->resource_name); if (error != READSTAT_OK) diff --git a/src/test/test_types.h b/src/test/test_types.h index d41311e3..7b006012 100644 --- a/src/test/test_types.h +++ b/src/test/test_types.h @@ -73,6 +73,7 @@ typedef struct rt_test_file_s { /* SAV only: after writing, overwrite the header's case count with -1 * (unknown) so the reader must find the rows by itself */ int unknown_row_count; + int append_xport_member; char resource_name[80]; } rt_test_file_t; From ea60b9318c0a2a2f1bd599a0d2a5fe3251facb72 Mon Sep 17 00:00:00 2001 From: Yucheng Du <256645790+yucheng-du@users.noreply.github.com> Date: Wed, 2 Dec 2026 01:35:29 -0800 Subject: [PATCH 2/2] Document the width of a blank OBSV8 count --- src/sas/readstat_xport_read.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sas/readstat_xport_read.c b/src/sas/readstat_xport_read.c index a14d4258..1645e0da 100644 --- a/src/sas/readstat_xport_read.c +++ b/src/sas/readstat_xport_read.c @@ -120,7 +120,7 @@ static readstat_error_t xport_read_header_record(xport_ctx_t *ctx, xport_header_ if (matches < 2) { const char blank_obsv8[] = "HEADER RECORD*******OBSV8 HEADER RECORD!!!!!!!" - " "; + " "; /* 15 spaces for the blank observation count */ if (memcmp(line, blank_obsv8, sizeof(blank_obsv8)-1) != 0) return READSTAT_ERROR_PARSE; }