soc/cavium: Enable MMU
[coreboot.git] / util / cbfstool / compress.c
bloba6a0df4dd1d4615e938523fddadea7e9901289f6
1 /*
2 * compression handling for cbfstool
4 * Copyright (C) 2009 coresystems GmbH
5 * written by Patrick Georgi <patrick.georgi@coresystems.de>
7 * Adapted from code
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.
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "common.h"
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,
32 .frameInfo = {
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);
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;
49 static int lz4_decompress(char *in, int in_len, char *out, int out_len,
50 size_t *actual_size)
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;
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,
66 size_t *actual_size)
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);
73 *out_len = in_len;
74 return 0;
77 static int none_decompress(char *in, int in_len, char *out, unused int out_len,
78 size_t *actual_size)
80 memcpy(out, in, in_len);
81 if (actual_size != NULL)
82 *actual_size = in_len;
83 return 0;
86 comp_func_ptr compression_function(enum comp_algo algo)
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;
96 case CBFS_COMPRESS_LZ4:
97 compress = lz4_compress;
98 break;
99 default:
100 ERROR("Unknown compression algorithm %d!\n", algo);
101 return NULL;
103 return compress;
106 decomp_func_ptr decompression_function(enum comp_algo algo)
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;
116 case CBFS_COMPRESS_LZ4:
117 decompress = lz4_decompress;
118 break;
119 default:
120 ERROR("Unknown compression algorithm %d!\n", algo);
121 return NULL;
123 return decompress;