Chromium Code Reviews| Index: utility/bmpblk_util.c |
| diff --git a/utility/bmpblk_util.c b/utility/bmpblk_util.c |
| index 214beda4b0040d2b2a1b6b88c283665e3b47c2d4..9da9d6bcdd9f3cea9718aa24a0209a74b357ceb0 100644 |
| --- a/utility/bmpblk_util.c |
| +++ b/utility/bmpblk_util.c |
| @@ -5,6 +5,7 @@ |
| #include <errno.h> |
| #include <fcntl.h> |
| #include <limits.h> |
| +#include <lzma.h> |
| #include <stdio.h> |
| #include <string.h> |
| #include <sys/mman.h> |
| @@ -139,6 +140,51 @@ static void *do_efi_decompress(ImageInfo *img) { |
| } |
| + |
| +static void *do_lzma_decompress(ImageInfo *img) { |
| + void *ibuf; |
| + void *obuf; |
| + uint32_t isize; |
| + uint32_t osize; |
| + lzma_stream stream = LZMA_STREAM_INIT; |
| + lzma_ret result; |
| + |
| + ibuf = ((void *)img) + sizeof(ImageInfo); |
|
Hung-Te
2011/02/15 07:55:10
We should not perform arithmetic operation to void
Tom Wai-Hong Tam
2011/02/16 10:58:03
Done.
|
| + isize = img->compressed_size; |
| + osize = img->original_size; |
| + obuf = malloc(osize); |
|
Hung-Te
2011/02/15 07:55:10
Why not lzma_stream_buffer_bound?
Tom Wai-Hong Tam
2011/02/16 10:58:03
Since the exact uncompressed size is filled in whe
|
| + if (!obuf) { |
| + fprintf(stderr, "Can't allocate %d bytes: %s\n", |
| + osize, |
| + strerror(errno)); |
| + return 0; |
| + } |
| + |
| + result = lzma_auto_decoder(&stream, -1, 0); |
| + if (result != LZMA_OK) { |
| + fprintf(stderr, "Unalbe to initialize auto decoder (error: %d)!\n", result); |
|
Hung-Te
2011/02/15 07:55:10
Unable, typo
Tom Wai-Hong Tam
2011/02/16 10:58:03
Done.
|
| + free(obuf); |
| + return 0; |
| + } |
| + |
| + stream.next_in = ibuf; |
| + stream.avail_in = isize; |
| + do { |
| + stream.next_out = obuf; |
| + stream.avail_out = osize; |
| + result = lzma_code(&stream, LZMA_FINISH); |
| + if (result != LZMA_OK && result != LZMA_STREAM_END) { |
| + fprintf(stderr, "Unalbe to decode data (error: %d)!\n", result); |
| + free(obuf); |
| + return 0; |
| + } |
| + } while (result == LZMA_OK); |
| + lzma_end(&stream); |
| + return obuf; |
| +} |
| + |
| + |
| + |
| // Show what's inside. If todir is NULL, just print. Otherwise unpack. |
| int dump_bmpblock(const char *infile, int show_as_yaml, |
| const char *todir, int overwrite) { |
| @@ -266,6 +312,16 @@ int dump_bmpblock(const char *infile, int show_as_yaml, |
| } |
| free_data = 1; |
| break; |
| + case COMPRESS_LZMA: |
| + data_ptr = do_lzma_decompress(img); |
| + if (!data_ptr) { |
| + fclose(bfp); |
| + fclose(yfp); |
| + discard_file(ptr, length); |
| + return 1; |
| + } |
| + free_data = 1; |
| + break; |
| default: |
| fprintf(stderr, "Unsupported compression method encountered.\n"); |
| fclose(bfp); |