Patrick Georgi | ea063cb | 2020-05-08 19:28:13 +0200 | [diff] [blame] | 1 | /* compression handling for cbfstool */ |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 3 | |
| 4 | #include <string.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 5 | #include <stdio.h> |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 6 | #include <stdlib.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 7 | #include "common.h" |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 8 | #include "lz4/lib/lz4frame.h" |
Julius Werner | 98eeb96 | 2019-12-11 15:47:42 -0800 | [diff] [blame] | 9 | #include <commonlib/bsd/compression.h> |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 10 | |
| 11 | static int lz4_compress(char *in, int in_len, char *out, int *out_len) |
| 12 | { |
| 13 | LZ4F_preferences_t prefs = { |
| 14 | .compressionLevel = 20, |
| 15 | .frameInfo = { |
| 16 | .blockSizeID = max4MB, |
| 17 | .blockMode = blockIndependent, |
| 18 | .contentChecksumFlag = noContentChecksum, |
| 19 | }, |
| 20 | }; |
| 21 | size_t worst_size = LZ4F_compressFrameBound(in_len, &prefs); |
| 22 | void *bounce = malloc(worst_size); |
| 23 | if (!bounce) |
| 24 | return -1; |
| 25 | *out_len = LZ4F_compressFrame(bounce, worst_size, in, in_len, &prefs); |
| 26 | if (LZ4F_isError(*out_len) || *out_len >= in_len) |
| 27 | return -1; |
| 28 | memcpy(out, bounce, *out_len); |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | static int lz4_decompress(char *in, int in_len, char *out, int out_len, |
| 33 | size_t *actual_size) |
| 34 | { |
| 35 | size_t result = ulz4fn(in, in_len, out, out_len); |
| 36 | if (result == 0) |
| 37 | return -1; |
| 38 | if (actual_size != NULL) |
| 39 | *actual_size = result; |
| 40 | return 0; |
| 41 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 42 | |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 43 | static int lzma_compress(char *in, int in_len, char *out, int *out_len) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 44 | { |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 45 | return do_lzma_compress(in, in_len, out, out_len); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Aaron Durbin | 5213c53 | 2015-10-23 17:38:40 -0500 | [diff] [blame] | 48 | static int lzma_decompress(char *in, int in_len, char *out, unused int out_len, |
| 49 | size_t *actual_size) |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 50 | { |
Aaron Durbin | 5213c53 | 2015-10-23 17:38:40 -0500 | [diff] [blame] | 51 | return do_lzma_uncompress(out, out_len, in, in_len, actual_size); |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 52 | } |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 53 | static int none_compress(char *in, int in_len, char *out, int *out_len) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 54 | { |
| 55 | memcpy(out, in, in_len); |
| 56 | *out_len = in_len; |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 57 | return 0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 58 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 59 | |
Aaron Durbin | 5213c53 | 2015-10-23 17:38:40 -0500 | [diff] [blame] | 60 | static int none_decompress(char *in, int in_len, char *out, unused int out_len, |
| 61 | size_t *actual_size) |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 62 | { |
| 63 | memcpy(out, in, in_len); |
Aaron Durbin | 5213c53 | 2015-10-23 17:38:40 -0500 | [diff] [blame] | 64 | if (actual_size != NULL) |
| 65 | *actual_size = in_len; |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 69 | comp_func_ptr compression_function(enum comp_algo algo) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 70 | { |
| 71 | comp_func_ptr compress; |
| 72 | switch (algo) { |
| 73 | case CBFS_COMPRESS_NONE: |
| 74 | compress = none_compress; |
| 75 | break; |
| 76 | case CBFS_COMPRESS_LZMA: |
| 77 | compress = lzma_compress; |
| 78 | break; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 79 | case CBFS_COMPRESS_LZ4: |
| 80 | compress = lz4_compress; |
| 81 | break; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 82 | default: |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 83 | ERROR("Unknown compression algorithm %d!\n", algo); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 84 | return NULL; |
| 85 | } |
| 86 | return compress; |
| 87 | } |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 88 | |
| 89 | decomp_func_ptr decompression_function(enum comp_algo algo) |
| 90 | { |
| 91 | decomp_func_ptr decompress; |
| 92 | switch (algo) { |
| 93 | case CBFS_COMPRESS_NONE: |
| 94 | decompress = none_decompress; |
| 95 | break; |
| 96 | case CBFS_COMPRESS_LZMA: |
| 97 | decompress = lzma_decompress; |
| 98 | break; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 99 | case CBFS_COMPRESS_LZ4: |
| 100 | decompress = lz4_decompress; |
| 101 | break; |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 102 | default: |
| 103 | ERROR("Unknown compression algorithm %d!\n", algo); |
| 104 | return NULL; |
| 105 | } |
| 106 | return decompress; |
| 107 | } |