1 /* compression handling for cbfstool */
2 /* SPDX-License-Identifier: GPL-2.0-only */
8 #include "lz4/lib/lz4frame.h"
9 #include <commonlib/bsd/compression.h>
11 static int lz4_compress(char *in
, int in_len
, char *out
, int *out_len
)
13 LZ4F_preferences_t prefs
= {
14 .compressionLevel
= 20,
16 .blockSizeID
= max4MB
,
17 .blockMode
= blockIndependent
,
18 .contentChecksumFlag
= noContentChecksum
,
21 size_t worst_size
= LZ4F_compressFrameBound(in_len
, &prefs
);
22 void *bounce
= malloc(worst_size
);
25 *out_len
= LZ4F_compressFrame(bounce
, worst_size
, in
, in_len
, &prefs
);
26 if (LZ4F_isError(*out_len
) || *out_len
>= in_len
) {
30 memcpy(out
, bounce
, *out_len
);
35 static int lz4_decompress(char *in
, int in_len
, char *out
, int out_len
,
38 size_t result
= ulz4fn(in
, in_len
, out
, out_len
);
41 if (actual_size
!= NULL
)
42 *actual_size
= result
;
46 static int lzma_compress(char *in
, int in_len
, char *out
, int *out_len
)
48 return do_lzma_compress(in
, in_len
, out
, out_len
);
51 static int lzma_decompress(char *in
, int in_len
, char *out
, unused
int out_len
,
54 return do_lzma_uncompress(out
, out_len
, in
, in_len
, actual_size
);
56 static int none_compress(char *in
, int in_len
, char *out
, int *out_len
)
58 memcpy(out
, in
, in_len
);
63 static int none_decompress(char *in
, int in_len
, char *out
, unused
int out_len
,
66 memcpy(out
, in
, in_len
);
67 if (actual_size
!= NULL
)
68 *actual_size
= in_len
;
72 comp_func_ptr
compression_function(enum cbfs_compression algo
)
74 comp_func_ptr compress
;
76 case CBFS_COMPRESS_NONE
:
77 compress
= none_compress
;
79 case CBFS_COMPRESS_LZMA
:
80 compress
= lzma_compress
;
82 case CBFS_COMPRESS_LZ4
:
83 compress
= lz4_compress
;
86 ERROR("Unknown compression algorithm %d!\n", algo
);
92 decomp_func_ptr
decompression_function(enum cbfs_compression algo
)
94 decomp_func_ptr decompress
;
96 case CBFS_COMPRESS_NONE
:
97 decompress
= none_decompress
;
99 case CBFS_COMPRESS_LZMA
:
100 decompress
= lzma_decompress
;
102 case CBFS_COMPRESS_LZ4
:
103 decompress
= lz4_decompress
;
106 ERROR("Unknown compression algorithm %d!\n", algo
);