AUTHORS, util/: Drop individual copyright notices
[coreboot.git] / util / cbfstool / compress.c
blobb92da0eac3e97e397a23a14999d4f0bbd39d65d4
1 /* compression handling for cbfstool */
2 /*
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.
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include "common.h"
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,
24 .frameInfo = {
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);
32 if (!bounce)
33 return -1;
34 *out_len = LZ4F_compressFrame(bounce, worst_size, in, in_len, &prefs);
35 if (LZ4F_isError(*out_len) || *out_len >= in_len)
36 return -1;
37 memcpy(out, bounce, *out_len);
38 return 0;
41 static int lz4_decompress(char *in, int in_len, char *out, int out_len,
42 size_t *actual_size)
44 size_t result = ulz4fn(in, in_len, out, out_len);
45 if (result == 0)
46 return -1;
47 if (actual_size != NULL)
48 *actual_size = result;
49 return 0;
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,
58 size_t *actual_size)
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);
65 *out_len = in_len;
66 return 0;
69 static int none_decompress(char *in, int in_len, char *out, unused int out_len,
70 size_t *actual_size)
72 memcpy(out, in, in_len);
73 if (actual_size != NULL)
74 *actual_size = in_len;
75 return 0;
78 comp_func_ptr compression_function(enum comp_algo algo)
80 comp_func_ptr compress;
81 switch (algo) {
82 case CBFS_COMPRESS_NONE:
83 compress = none_compress;
84 break;
85 case CBFS_COMPRESS_LZMA:
86 compress = lzma_compress;
87 break;
88 case CBFS_COMPRESS_LZ4:
89 compress = lz4_compress;
90 break;
91 default:
92 ERROR("Unknown compression algorithm %d!\n", algo);
93 return NULL;
95 return compress;
98 decomp_func_ptr decompression_function(enum comp_algo algo)
100 decomp_func_ptr decompress;
101 switch (algo) {
102 case CBFS_COMPRESS_NONE:
103 decompress = none_decompress;
104 break;
105 case CBFS_COMPRESS_LZMA:
106 decompress = lzma_decompress;
107 break;
108 case CBFS_COMPRESS_LZ4:
109 decompress = lz4_decompress;
110 break;
111 default:
112 ERROR("Unknown compression algorithm %d!\n", algo);
113 return NULL;
115 return decompress;