summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPali Rohár <pali.rohar@gmail.com>2016-12-24 18:06:57 +0100
committerPali Rohár <pali.rohar@gmail.com>2016-12-24 18:06:57 +0100
commit117d0bb2d04df5a6cb0289f00afe61671376f0ec (patch)
treeca323c42998596c2a9dbf1504b75b6224a9016fe
parentf7bad18a565515421329931acf2669c5e75a1fde (diff)
download0xFFFF-117d0bb2d04df5a6cb0289f00afe61671376f0ec.tar.bz2
cal: cal_read_block allocate memory
-rw-r--r--src/cal.c13
-rw-r--r--src/local.c2
2 files changed, 11 insertions, 4 deletions
diff --git a/src/cal.c b/src/cal.c
index 8fa7e9f..25bb9ee 100644
--- a/src/cal.c
+++ b/src/cal.c
@@ -264,6 +264,7 @@ int cal_read_block(struct cal * cal, const char * name, void ** ptr, unsigned lo
uint64_t filelen = cal->size;
uint8_t * data = cal->mem;
struct header * hdr;
+ void * offset;
find_offset = find_section(data, filelen, INDEX_LAST, name);
if ( find_offset < 0 )
@@ -277,12 +278,18 @@ int cal_read_block(struct cal * cal, const char * name, void ** ptr, unsigned lo
if ( crc32(0, hdr, sizeof(*hdr) - 4) != hdr->hdrsum )
return -1;
- *ptr = data + find_offset + sizeof(struct header);
- *len = hdr->length;
+ offset = data + find_offset + sizeof(struct header);
+
+ if ( crc32(0, offset, hdr->length) != hdr->datasum )
+ return -1;
- if ( crc32(0, *ptr, *len) != hdr->datasum )
+ *ptr = malloc(hdr->length);
+ if (!ptr)
return -1;
+ memcpy(*ptr, offset, hdr->length);
+ *len = hdr->length;
+
return 0;
}
diff --git a/src/local.c b/src/local.c
index 628c49e..3813910 100644
--- a/src/local.c
+++ b/src/local.c
@@ -53,7 +53,7 @@ static int root_device = -1;
#define min(a, b) (a < b ? a : b)
#define local_cal_copy(dest, from, len) strncpy(dest, from, min(len, sizeof(dest)-1))
#define local_cal_read(cal, str, ptr, len) ( cal_read_block(cal, str, &ptr, &len, 0) == 0 && ptr )
-#define local_cal_readcopy(cal, str, dest) do { void * ptr; unsigned long int len; if ( local_cal_read(cal, str, ptr, len) ) local_cal_copy(dest, ptr, len); } while ( 0 )
+#define local_cal_readcopy(cal, str, dest) do { void * ptr; unsigned long int len; if ( local_cal_read(cal, str, ptr, len) ) { local_cal_copy(dest, ptr, len); free(ptr); } } while ( 0 )
#if defined(__linux__) && defined(__arm__)