diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-05-04 23:20:28 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-05-04 23:20:28 +0200 |
commit | a2750dee0e4c374ae51f83eead7eb7df3c018d95 (patch) | |
tree | eb02eafaecd5a966d216cb2d97b272ab2731f121 /unpack/extract_file.c | |
parent | 09bdcdb1f3f2d7810ea956842bc7551d9ca26f92 (diff) |
rdsquashfs: malloc extraction buffers ahead of time
Instead of malloc/freeing the buffers for every file, allocate them
once, ahead of time.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'unpack/extract_file.c')
-rw-r--r-- | unpack/extract_file.c | 43 |
1 files changed, 17 insertions, 26 deletions
diff --git a/unpack/extract_file.c b/unpack/extract_file.c index 4d5997a..62d1086 100644 --- a/unpack/extract_file.c +++ b/unpack/extract_file.c @@ -3,19 +3,12 @@ int extract_file(file_info_t *fi, unsqfs_info_t *info, int outfd) { - void *buffer, *scratch, *ptr; size_t i, count, fragsz; bool compressed; uint32_t bs; ssize_t ret; + void *ptr; - buffer = malloc(info->block_size * 2); - if (buffer == NULL) { - perror("allocating scratch buffer"); - return -1; - } - - scratch = (char *)buffer + info->block_size; count = fi->size / info->block_size; if (count > 0) { @@ -31,7 +24,7 @@ int extract_file(file_info_t *fi, unsqfs_info_t *info, int outfd) if (bs > info->block_size) goto fail_bs; - ret = read_retry(info->sqfsfd, buffer, bs); + ret = read_retry(info->sqfsfd, info->buffer, bs); if (ret < 0) goto fail_rd; @@ -39,16 +32,17 @@ int extract_file(file_info_t *fi, unsqfs_info_t *info, int outfd) goto fail_trunc; if (compressed) { - ret = info->cmp->do_block(info->cmp, buffer, bs, - scratch, + ret = info->cmp->do_block(info->cmp, + info->buffer, bs, + info->scratch, info->block_size); if (ret <= 0) - goto fail; + return -1; bs = ret; - ptr = scratch; + ptr = info->scratch; } else { - ptr = buffer; + ptr = info->buffer; } ret = write_retry(outfd, ptr, bs); @@ -64,11 +58,12 @@ int extract_file(file_info_t *fi, unsqfs_info_t *info, int outfd) if (fragsz > 0) { if (frag_reader_read(info->frag, fi->fragment, - fi->fragment_offset, buffer, fragsz)) { - goto fail; + fi->fragment_offset, info->buffer, + fragsz)) { + return -1; } - ret = write_retry(outfd, buffer, fragsz); + ret = write_retry(outfd, info->buffer, fragsz); if (ret < 0) goto fail_wr; @@ -76,27 +71,23 @@ int extract_file(file_info_t *fi, unsqfs_info_t *info, int outfd) goto fail_wr_trunc; } - free(buffer); return 0; fail_seek: perror("seek on squashfs"); - goto fail; + return -1; fail_wr: perror("writing uncompressed block"); - goto fail; + return -1; fail_wr_trunc: fputs("writing uncompressed block: truncated write\n", stderr); - goto fail; + return -1; fail_rd: perror("reading from squashfs"); - goto fail; + return -1; fail_trunc: fputs("reading from squashfs: unexpected end of file\n", stderr); - goto fail; + return -1; fail_bs: fputs("found compressed block larger than block size\n", stderr); - goto fail; -fail: - free(buffer); return -1; } |