2 * compression handling for cbfstool
4 * Copyright (C) 2009 coresystems GmbH
5 * written by Patrick Georgi <patrick.georgi@coresystems.de>
8 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>, released
9 * under identical license terms
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.
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.
25 #include "lz4/lib/lz4frame.h"
26 #include <commonlib/compression.h>
28 static int lz4_compress(char *in
, int in_len
, char *out
, int *out_len
)
30 LZ4F_preferences_t prefs
= {
31 .compressionLevel
= 20,
33 .blockSizeID
= max4MB
,
34 .blockMode
= blockIndependent
,
35 .contentChecksumFlag
= noContentChecksum
,
38 size_t worst_size
= LZ4F_compressFrameBound(in_len
, &prefs
);
39 void *bounce
= malloc(worst_size
);
42 *out_len
= LZ4F_compressFrame(bounce
, worst_size
, in
, in_len
, &prefs
);
43 if (LZ4F_isError(*out_len
) || *out_len
>= in_len
)
45 memcpy(out
, bounce
, *out_len
);
49 static int lz4_decompress(char *in
, int in_len
, char *out
, int out_len
,
52 size_t result
= ulz4fn(in
, in_len
, out
, out_len
);
55 if (actual_size
!= NULL
)
56 *actual_size
= result
;
60 static int lzma_compress(char *in
, int in_len
, char *out
, int *out_len
)
62 return do_lzma_compress(in
, in_len
, out
, out_len
);
65 static int lzma_decompress(char *in
, int in_len
, char *out
, unused
int out_len
,
68 return do_lzma_uncompress(out
, out_len
, in
, in_len
, actual_size
);
70 static int none_compress(char *in
, int in_len
, char *out
, int *out_len
)
72 memcpy(out
, in
, in_len
);
77 static int none_decompress(char *in
, int in_len
, char *out
, unused
int out_len
,
80 memcpy(out
, in
, in_len
);
81 if (actual_size
!= NULL
)
82 *actual_size
= in_len
;
86 comp_func_ptr
compression_function(enum comp_algo algo
)
88 comp_func_ptr compress
;
90 case CBFS_COMPRESS_NONE
:
91 compress
= none_compress
;
93 case CBFS_COMPRESS_LZMA
:
94 compress
= lzma_compress
;
96 case CBFS_COMPRESS_LZ4
:
97 compress
= lz4_compress
;
100 ERROR("Unknown compression algorithm %d!\n", algo
);
106 decomp_func_ptr
decompression_function(enum comp_algo algo
)
108 decomp_func_ptr decompress
;
110 case CBFS_COMPRESS_NONE
:
111 decompress
= none_decompress
;
113 case CBFS_COMPRESS_LZMA
:
114 decompress
= lzma_decompress
;
116 case CBFS_COMPRESS_LZ4
:
117 decompress
= lz4_decompress
;
120 ERROR("Unknown compression algorithm %d!\n", algo
);