1 /* compression handling for cbfstool */
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; version 2 of the License.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
17 #include "lz4/lib/lz4frame.h"
18 #include <commonlib/bsd/compression.h>
20 static int lz4_compress(char *in
, int in_len
, char *out
, int *out_len
)
22 LZ4F_preferences_t prefs
= {
23 .compressionLevel
= 20,
25 .blockSizeID
= max4MB
,
26 .blockMode
= blockIndependent
,
27 .contentChecksumFlag
= noContentChecksum
,
30 size_t worst_size
= LZ4F_compressFrameBound(in_len
, &prefs
);
31 void *bounce
= malloc(worst_size
);
34 *out_len
= LZ4F_compressFrame(bounce
, worst_size
, in
, in_len
, &prefs
);
35 if (LZ4F_isError(*out_len
) || *out_len
>= in_len
)
37 memcpy(out
, bounce
, *out_len
);
41 static int lz4_decompress(char *in
, int in_len
, char *out
, int out_len
,
44 size_t result
= ulz4fn(in
, in_len
, out
, out_len
);
47 if (actual_size
!= NULL
)
48 *actual_size
= result
;
52 static int lzma_compress(char *in
, int in_len
, char *out
, int *out_len
)
54 return do_lzma_compress(in
, in_len
, out
, out_len
);
57 static int lzma_decompress(char *in
, int in_len
, char *out
, unused
int out_len
,
60 return do_lzma_uncompress(out
, out_len
, in
, in_len
, actual_size
);
62 static int none_compress(char *in
, int in_len
, char *out
, int *out_len
)
64 memcpy(out
, in
, in_len
);
69 static int none_decompress(char *in
, int in_len
, char *out
, unused
int out_len
,
72 memcpy(out
, in
, in_len
);
73 if (actual_size
!= NULL
)
74 *actual_size
= in_len
;
78 comp_func_ptr
compression_function(enum comp_algo algo
)
80 comp_func_ptr compress
;
82 case CBFS_COMPRESS_NONE
:
83 compress
= none_compress
;
85 case CBFS_COMPRESS_LZMA
:
86 compress
= lzma_compress
;
88 case CBFS_COMPRESS_LZ4
:
89 compress
= lz4_compress
;
92 ERROR("Unknown compression algorithm %d!\n", algo
);
98 decomp_func_ptr
decompression_function(enum comp_algo algo
)
100 decomp_func_ptr decompress
;
102 case CBFS_COMPRESS_NONE
:
103 decompress
= none_decompress
;
105 case CBFS_COMPRESS_LZMA
:
106 decompress
= lzma_decompress
;
108 case CBFS_COMPRESS_LZ4
:
109 decompress
= lz4_decompress
;
112 ERROR("Unknown compression algorithm %d!\n", algo
);