}
-int read_ihex_chunks(const char * fname, struct ihex_chunk_t * chunks, int max_chunks)
+int
+read_ihex_chunks(
+ const char * fname,
+ ihex_chunk_p * chunks )
{
- if (!fname || !chunks || !max_chunks)
+ if (!fname || !chunks)
return -1;
- memset((void*)chunks, 0, sizeof(chunks[0]) * max_chunks);
FILE * f = fopen(fname, "r");
if (!f) {
perror(fname);
return -1;
}
uint32_t segment = 0; // segment address
- int chunk = 0;
- chunks[0].baseaddr = ~0;
+ int chunk = 0, max_chunks = 0;
+ *chunks = NULL;
while (!feof(f)) {
char line[128];
fprintf(stderr, "%s: %s, unsupported check type %02x\n", __FUNCTION__, fname, bline[3]);
continue;
}
- if (addr != chunks[chunk].baseaddr + chunks[chunk].size) {
- if (chunks[chunk].size)
+ if (chunk < max_chunks && addr != ((*chunks)[chunk].baseaddr + (*chunks)[chunk].size)) {
+ if ((*chunks)[chunk].size)
chunk++;
- chunks[chunk].baseaddr = addr;
}
- chunks[chunk].data = realloc(chunks[chunk].data, chunks[chunk].size + bline[0]);
- memcpy(chunks[chunk].data + chunks[chunk].size, bline + 4, bline[0]);
- chunks[chunk].size += bline[0];
+ if (chunk >= max_chunks) {
+ max_chunks++;
+ *chunks = realloc(*chunks, max_chunks * sizeof(ihex_chunk_t));
+ memset(*chunks + chunk, 0, (max_chunks - chunk) * sizeof(ihex_chunk_t));
+ (*chunks)[chunk].baseaddr = addr;
+ }
+ (*chunks)[chunk].data = realloc((*chunks)[chunk].data, (*chunks)[chunk].size + bline[0]);
+ memcpy((*chunks)[chunk].data + (*chunks)[chunk].size, bline + 4, bline[0]);
+ (*chunks)[chunk].size += bline[0];
}
- if (chunks[chunk].size)
- chunk++;
fclose(f);
- return chunk;
+ return max_chunks;
}
#endif
// parses a hex text string 'src' of at max 'maxlen' characters, decodes it into 'buffer'
-int read_hex_string(const char * src, uint8_t * buffer, int maxlen);
+int
+read_hex_string(
+ const char * src,
+ uint8_t * buffer,
+ int maxlen);
// a .hex file chunk (base address + size)
-struct ihex_chunk_t {
+typedef struct ihex_chunk_t {
uint32_t baseaddr; // offset it started at in the .hex file
uint8_t * data; // read data
uint32_t size; // read data size
-};
+} ihex_chunk_t, *ihex_chunk_p;
/*
* Read a .hex file, detects the various different chunks in it from their starting
- * addresses and fills up the chunks passed as arguments up to max_chunks.
+ * addresses and allocate an array of ihex_chunk_t returned in 'chunks'.
* Returns the number of chunks found, or -1 if an error occurs.
*/
-int read_ihex_chunks(const char * fname, struct ihex_chunk_t * chunks, int max_chunks);
+int
+read_ihex_chunks(
+ const char * fname,
+ ihex_chunk_p * chunks );
// reads IHEX file 'fname', puts it's decoded size in *'dsize' and returns
// a newly allocated buffer with the binary data (or NULL, if error)
-uint8_t * read_ihex_file(const char * fname, uint32_t * dsize, uint32_t * start);
+uint8_t *
+read_ihex_file(
+ const char * fname,
+ uint32_t * dsize,
+ uint32_t * start);
// hex dump from pointer 'b' for 'l' bytes with string prefix 'w'
-void hdump(const char *w, uint8_t *b, size_t l);
+void
+hdump(
+ const char *w,
+ uint8_t *b, size_t l);
#ifdef __cplusplus
};