Skip to content

Commit

Permalink
Fix nfreader sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
phaag committed Mar 30, 2024
1 parent ea6322e commit 06475c9
Showing 1 changed file with 17 additions and 33 deletions.
50 changes: 17 additions & 33 deletions src/nfreader/nfreader.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,14 @@ static void print_record(recordHandle_t *recordHandle) {
" src addr = %16s\n"
" dst addr = %16s\n",
as, ds);

printf("\n");
}

static void process_data(void) {
// Get the first file handle
nffile_t *nffile = GetNextFile(NULL);
if (!nffile) {
LogError("GetNextFile() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
return;
}
if (nffile == EMPTY_LIST) {
if (nffile == NULL) {
LogError("Empty file list. No files to process\n");
return;
}
Expand All @@ -181,44 +179,30 @@ static void process_data(void) {
return;
}

dataBlock_t *dataBlock = NULL;
int done = 0;
while (!done) {
// get next data block from file
int ret = ReadBlock(nffile);

switch (ret) {
case NF_CORRUPT:
case NF_ERROR:
if (ret == NF_CORRUPT)
LogError("Skip corrupt data file '%s'", nffile->fileName);
else
LogError("Read error in file '%s': %s", nffile->fileName, strerror(errno));
// fall through - get next file in chain
case NF_EOF: {
nffile_t *next = GetNextFile(nffile);
if (next == EMPTY_LIST) {
done = 1;
}
if (next == NULL) {
done = 1;
LogError("Unexpected end of file list\n");
}
// else continue with next file
continue;
dataBlock = ReadBlock(nffile, dataBlock);

} break; // not really needed
if (dataBlock == NULL) {
if (GetNextFile(nffile) == NULL) {
done = 1;
printf("\nDone\n");
continue;
}
}

if (nffile->block_header->type != DATA_BLOCK_TYPE_2 && nffile->block_header->type != DATA_BLOCK_TYPE_3) {
LogError("Unknown block type %u. Skip block", nffile->block_header->type);
if (dataBlock->type != DATA_BLOCK_TYPE_2 && dataBlock->type != DATA_BLOCK_TYPE_3) {
LogError("Skip block type %u. Write block unmodified", dataBlock->type);
continue;
}

record_header_t *record_ptr = nffile->buff_ptr;
record_header_t *record_ptr = GetCursor(dataBlock);
uint32_t sumSize = 0;
uint32_t processed = 0;
for (int i = 0; i < nffile->block_header->NumRecords; i++) {
if ((sumSize + record_ptr->size) > ret || (record_ptr->size < sizeof(record_header_t))) {
for (int i = 0; i < dataBlock->NumRecords; i++) {
if ((sumSize + record_ptr->size) > dataBlock->size || (record_ptr->size < sizeof(record_header_t))) {
LogError("Corrupt data file. Inconsistent block size in %s line %d\n", __FILE__, __LINE__);
exit(255);
}
Expand Down Expand Up @@ -248,7 +232,7 @@ static void process_data(void) {

} // while

CloseFile(nffile);
FreeDataBlock(dataBlock);
DisposeFile(nffile);

} // End of process_data
Expand Down

0 comments on commit 06475c9

Please sign in to comment.