Skip to content
Browse files

Clean up warnings and check byte order

  • Loading branch information...
1 parent c2f4a1d commit 1b5ee226fcc8a6f0b36768828b462e9e210085d1 @b4winckler committed Feb 16, 2012
Showing with 25 additions and 6 deletions.
  1. +25 −6 src/lxbread.c
View
31 src/lxbread.c
@@ -93,12 +93,12 @@ int parse_header(const char *data, long size, fcs_header *hdr)
if (!hdr) return FALSE;
if (size < 58) {
- fprintf(stderr, "data too small (%lu)\n", size);
+ fprintf(stderr, "Bad LXB: header data is too small (%lu)\n", size);
return FALSE;
}
if (0 != strncmp(data, "FCS3.0 ", 10)) {
- fprintf(stderr, "bad magic\n");
+ fprintf(stderr, "Bad LXB: magic bytes do not match\n");
return FALSE;
}
@@ -111,14 +111,15 @@ int parse_header(const char *data, long size, fcs_header *hdr)
ok &= sscanf(&data[50], "%8d", &hdr->end_analysis);
if (!ok)
- fprintf(stderr, "segment offset parse failed\n");
+ fprintf(stderr, "Bad LXB: failed to parse segment offsets\n");
return ok;
}
map_t parse_text(const char *text, long size)
{
- assert(size > 1);
+ if (size < 2)
+ return NULL;
map_t m = map_create();
char *sep = strndup(text, 1);
@@ -160,6 +161,13 @@ int check_par_format(map_t txt)
return FALSE;
}
+ const char *byteord = map_get(txt, "$BYTEORD");
+ if (strcmp("1,2,3,4", byteord) != 0) {
+ fprintf(stderr, "Data not in little endian format ($BYTEORD=%s)\n",
+ byteord);
+ return FALSE;
+ }
+
init_parameter_mask(txt);
for (int i = 0; i < npar; ++i) {
@@ -213,14 +221,20 @@ int main(int argc, char *argv[])
long size;
char *buf = read_file(argv[1], &size);
- assert(buf != NULL);
+ if (!buf) {
+ fprintf(stderr, "Could not read file: %s\n", argv[1]);
+ return -1;
+ }
fcs_header hdr;
int ok = parse_header(buf, size, &hdr);
if (!ok) return -1;
long txt_size = hdr.end_text - hdr.begin_text;
- assert(txt_size > 0 && hdr.begin_text > 0 && hdr.end_text <= size);
+ if (!(txt_size > 0 && hdr.begin_text > 0 && hdr.end_text <= size)) {
+ fprintf(stderr, "Bad LXB: could not locate TEXT segment\n");
+ return -1;
+ }
map_t txt = parse_text(buf + hdr.begin_text, txt_size);
@@ -230,6 +244,11 @@ int main(int argc, char *argv[])
print_header(txt);
long data_size = hdr.end_data - hdr.begin_data;
+ if (!(data_size > 0 && hdr.begin_data > 0 && hdr.end_data <= size)) {
+ fprintf(stderr, "Bad LXB: could not locate DATA segment\n");
+ return -1;
+ }
+
print_data((int32_t*)(buf + hdr.begin_data), data_size, txt);
map_free(txt);

0 comments on commit 1b5ee22

Please sign in to comment.
Something went wrong with that request. Please try again.