Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 1 | /* |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 2 | * compression handling for cbfstool |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 3 | * |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009 coresystems GmbH |
| 5 | * written by Patrick Georgi <patrick.georgi@coresystems.de> |
| 6 | * |
| 7 | * Adapted from code |
| 8 | * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>, released |
| 9 | * under identical license terms |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; version 2 of the License. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <string.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 23 | #include <stdlib.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 24 | #include "common.h" |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 25 | #include "lz4/lib/lz4frame.h" |
| 26 | #include <commonlib/compression.h> |
| 27 | |
| 28 | static int lz4_compress(char *in, int in_len, char *out, int *out_len) |
| 29 | { |
| 30 | LZ4F_preferences_t prefs = { |
| 31 | .compressionLevel = 20, |
| 32 | .frameInfo = { |
| 33 | .blockSizeID = max4MB, |
| 34 | .blockMode = blockIndependent, |
| 35 | .contentChecksumFlag = noContentChecksum, |
| 36 | }, |
| 37 | }; |
| 38 | size_t worst_size = LZ4F_compressFrameBound(in_len, &prefs); |
| 39 | void *bounce = malloc(worst_size); |
| 40 | if (!bounce) |
| 41 | return -1; |
| 42 | *out_len = LZ4F_compressFrame(bounce, worst_size, in, in_len, &prefs); |
| 43 | if (LZ4F_isError(*out_len) || *out_len >= in_len) |
| 44 | return -1; |
| 45 | memcpy(out, bounce, *out_len); |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int lz4_decompress(char *in, int in_len, char *out, int out_len, |
| 50 | size_t *actual_size) |
| 51 | { |
| 52 | size_t result = ulz4fn(in, in_len, out, out_len); |
| 53 | if (result == 0) |
| 54 | return -1; |
| 55 | if (actual_size != NULL) |
| 56 | *actual_size = result; |
| 57 | return 0; |
| 58 | } |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 59 | |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 60 | 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] | 61 | { |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 62 | return do_lzma_compress(in, in_len, out, out_len); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Aaron Durbin | 5213c53 | 2015-10-23 17:38:40 -0500 | [diff] [blame] | 65 | static int lzma_decompress(char *in, int in_len, char *out, unused int out_len, |
| 66 | size_t *actual_size) |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 67 | { |
Aaron Durbin | 5213c53 | 2015-10-23 17:38:40 -0500 | [diff] [blame] | 68 | return do_lzma_uncompress(out, out_len, in, in_len, actual_size); |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 69 | } |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 70 | 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] | 71 | { |
| 72 | memcpy(out, in, in_len); |
| 73 | *out_len = in_len; |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 74 | return 0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 75 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 76 | |
Aaron Durbin | 5213c53 | 2015-10-23 17:38:40 -0500 | [diff] [blame] | 77 | static int none_decompress(char *in, int in_len, char *out, unused int out_len, |
| 78 | size_t *actual_size) |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 79 | { |
| 80 | memcpy(out, in, in_len); |
Aaron Durbin | 5213c53 | 2015-10-23 17:38:40 -0500 | [diff] [blame] | 81 | if (actual_size != NULL) |
| 82 | *actual_size = in_len; |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 86 | comp_func_ptr compression_function(enum comp_algo algo) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 87 | { |
| 88 | comp_func_ptr compress; |
| 89 | switch (algo) { |
| 90 | case CBFS_COMPRESS_NONE: |
| 91 | compress = none_compress; |
| 92 | break; |
| 93 | case CBFS_COMPRESS_LZMA: |
| 94 | compress = lzma_compress; |
| 95 | break; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 96 | case CBFS_COMPRESS_LZ4: |
| 97 | compress = lz4_compress; |
| 98 | break; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 99 | default: |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 100 | ERROR("Unknown compression algorithm %d!\n", algo); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
| 103 | return compress; |
| 104 | } |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 105 | |
| 106 | decomp_func_ptr decompression_function(enum comp_algo algo) |
| 107 | { |
| 108 | decomp_func_ptr decompress; |
| 109 | switch (algo) { |
| 110 | case CBFS_COMPRESS_NONE: |
| 111 | decompress = none_decompress; |
| 112 | break; |
| 113 | case CBFS_COMPRESS_LZMA: |
| 114 | decompress = lzma_decompress; |
| 115 | break; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 116 | case CBFS_COMPRESS_LZ4: |
| 117 | decompress = lz4_decompress; |
| 118 | break; |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame] | 119 | default: |
| 120 | ERROR("Unknown compression algorithm %d!\n", algo); |
| 121 | return NULL; |
| 122 | } |
| 123 | return decompress; |
| 124 | } |