diff options
author | Rafał Miłecki <rafal@milecki.pl> | 2017-11-13 23:07:46 +0100 |
---|---|---|
committer | Rafał Miłecki <rafal@milecki.pl> | 2017-11-14 22:33:19 +0100 |
commit | 6d283b8029c87590bd056617340dbdb110239415 (patch) | |
tree | 7856a184eb4578fc5697ce4d2a8f0b5e94b8729e /package/utils/otrx/src | |
parent | c6761e7c8e5d0d33e2f266c297b403f337f93c6b (diff) | |
download | upstream-6d283b8029c87590bd056617340dbdb110239415.tar.gz upstream-6d283b8029c87590bd056617340dbdb110239415.tar.bz2 upstream-6d283b8029c87590bd056617340dbdb110239415.zip |
otrx: optimize memory usage when creating TRX image
There is no need to allocate buffer as big as the whole image in order
to calculate CRC32. It's enough to use small buffer and just read file
content block by block.
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Diffstat (limited to 'package/utils/otrx/src')
-rw-r--r-- | package/utils/otrx/src/otrx.c | 21 |
1 files changed, 7 insertions, 14 deletions
diff --git a/package/utils/otrx/src/otrx.c b/package/utils/otrx/src/otrx.c index 7b02977abb..d08507f685 100644 --- a/package/utils/otrx/src/otrx.c +++ b/package/utils/otrx/src/otrx.c @@ -284,7 +284,7 @@ static ssize_t otrx_create_align(FILE *trx, size_t curr_offset, size_t alignment static int otrx_create_write_hdr(FILE *trx, struct trx_header *hdr) { size_t bytes, length; - uint8_t *buf; + uint8_t buf[1024]; uint32_t crc32; hdr->magic = cpu_to_le32(TRX_MAGIC); @@ -299,20 +299,13 @@ static int otrx_create_write_hdr(FILE *trx, struct trx_header *hdr) { length = le32_to_cpu(hdr->length); - buf = malloc(length); - if (!buf) { - fprintf(stderr, "Couldn't alloc %zu B buffer\n", length); - return -ENOMEM; - } - - fseek(trx, 0, SEEK_SET); - bytes = fread(buf, 1, length, trx); - if (bytes != length) { - fprintf(stderr, "Couldn't read %zu B of data from %s\n", length, trx_path); - return -ENOMEM; + crc32 = 0xffffffff; + fseek(trx, TRX_FLAGS_OFFSET, SEEK_SET); + length -= TRX_FLAGS_OFFSET; + while ((bytes = fread(buf, 1, otrx_min(sizeof(buf), length), trx)) > 0) { + crc32 = otrx_crc32(crc32, buf, bytes); + length -= bytes; } - - crc32 = otrx_crc32(0xffffffff, buf + TRX_FLAGS_OFFSET, length - TRX_FLAGS_OFFSET); hdr->crc32 = cpu_to_le32(crc32); fseek(trx, 0, SEEK_SET); |