...
 
Commits (2)
......@@ -62,6 +62,7 @@ int read_hex(char *filename, char **result, int *dest)
short int addr_pre = 0;
int len_pre = 256;
int diff;
int typ;
*result = NULL;
......@@ -99,18 +100,36 @@ int read_hex(char *filename, char **result, int *dest)
fseek(f, 1, SEEK_CUR); // :
fgets(buffer, 2+1, f); // length
if (feof(f)) break;
if (feof(f)) {
debug_print("Missing EOF record\n");
fclose(f);
return result_size;
}
strncpy(tmp, buffer, 2+1);
sscanf(tmp, "%x", &len);
fgets(buffer, 4+1, f); // address
if (feof(f)) break;
strncpy(tmp, buffer, 4+1);
sscanf(tmp, "%x", (unsigned int *)&addr);
sscanf(tmp, "%hx", &addr);
fgets(buffer, 2+1, f); // record type
if (feof(f)) break;
strncpy(tmp, buffer, 2+1);
sscanf(tmp, "%x", &typ);
if (typ == 1) { // EOF
fclose(f);
return result_size;
}
else if (typ) { // no data
break;
}
diff = (addr - addr_pre) - len_pre;
if (diff > 0) {
debug_print("Warning: Prev line at 0x%x shorter then expected, filling %d bytes with 0xff\n", addr_pre, diff);
if (diff > 1 || g_debug)
printf("Warning: Prev line at 0x%x shorter then expected, filling %d bytes with 0xff\n", addr_pre, diff);
result_len+=diff;
while(result_len >= result_size) {
......@@ -124,8 +143,6 @@ int read_hex(char *filename, char **result, int *dest)
memset(*result+(result_len-diff), 0xff, diff); // append 0xff
}
fseek(f, 2, SEEK_CUR); // record type
fgets(buffer, 1000, f); // data + checksum
if (feof(f)) break;
......@@ -145,8 +162,8 @@ int read_hex(char *filename, char **result, int *dest)
len_pre = len;
}
fclose(f);
return result_size;
printf("Bad record type or premature EOF\n");
return 0;
}
......