Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/sas/readstat_xport_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -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!!!!!!!"
" "; /* 15 spaces for the blank observation count */
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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions src/test/test_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
20 changes: 20 additions & 0 deletions src/test/test_readstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; offset<buffer->used; 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)
Expand Down
1 change: 1 addition & 0 deletions src/test/test_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down