blob: a6a0df4dd1d4615e938523fddadea7e9901289f6 [file] [log] [blame]
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00001/*
Patrick Georgib7b56dd82009-09-14 13:29:27 +00002 * compression handling for cbfstool
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00003 *
Patrick Georgib7b56dd82009-09-14 13:29:27 +00004 * Copyright (C) 2009 coresystems GmbH
5 * written by Patrick Georgi <patrick.georgi@coresystems.de>
6 *
7 * Adapted from code
8 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>, released
9 * under identical license terms
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000010 *
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.
14 *
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.
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000019 */
20
21#include <string.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +000022#include <stdio.h>
Julius Werner09f29212015-09-29 13:51:35 -070023#include <stdlib.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +000024#include "common.h"
Julius Werner09f29212015-09-29 13:51:35 -070025#include "lz4/lib/lz4frame.h"
26#include <commonlib/compression.h>
27
28static int lz4_compress(char *in, int in_len, char *out, int *out_len)
29{
30 LZ4F_preferences_t prefs = {
31 .compressionLevel = 20,
32 .frameInfo = {
33 .blockSizeID = max4MB,
34 .blockMode = blockIndependent,
35 .contentChecksumFlag = noContentChecksum,
36 },
37 };
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;
47}
48
49static int lz4_decompress(char *in, int in_len, char *out, int out_len,
50 size_t *actual_size)
51{
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;
58}
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000059
Gabe Blackdbd006b2014-02-20 23:38:49 -080060static int lzma_compress(char *in, int in_len, char *out, int *out_len)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000061{
Gabe Blackdbd006b2014-02-20 23:38:49 -080062 return do_lzma_compress(in, in_len, out, out_len);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000063}
64
Aaron Durbin5213c532015-10-23 17:38:40 -050065static int lzma_decompress(char *in, int in_len, char *out, unused int out_len,
66 size_t *actual_size)
Patrick Georgi61c82292015-08-26 12:53:41 +020067{
Aaron Durbin5213c532015-10-23 17:38:40 -050068 return do_lzma_uncompress(out, out_len, in, in_len, actual_size);
Patrick Georgi61c82292015-08-26 12:53:41 +020069}
Gabe Blackdbd006b2014-02-20 23:38:49 -080070static int none_compress(char *in, int in_len, char *out, int *out_len)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000071{
72 memcpy(out, in, in_len);
73 *out_len = in_len;
Gabe Blackdbd006b2014-02-20 23:38:49 -080074 return 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000075}
Patrick Georgib7b56dd82009-09-14 13:29:27 +000076
Aaron Durbin5213c532015-10-23 17:38:40 -050077static int none_decompress(char *in, int in_len, char *out, unused int out_len,
78 size_t *actual_size)
Patrick Georgi61c82292015-08-26 12:53:41 +020079{
80 memcpy(out, in, in_len);
Aaron Durbin5213c532015-10-23 17:38:40 -050081 if (actual_size != NULL)
82 *actual_size = in_len;
Patrick Georgi61c82292015-08-26 12:53:41 +020083 return 0;
84}
85
Sol Boucher6310ccc2015-05-07 21:12:28 -070086comp_func_ptr compression_function(enum comp_algo algo)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000087{
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;
Julius Werner09f29212015-09-29 13:51:35 -070096 case CBFS_COMPRESS_LZ4:
97 compress = lz4_compress;
98 break;
Patrick Georgib7b56dd82009-09-14 13:29:27 +000099 default:
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800100 ERROR("Unknown compression algorithm %d!\n", algo);
Patrick Georgib7b56dd82009-09-14 13:29:27 +0000101 return NULL;
102 }
103 return compress;
104}
Patrick Georgi61c82292015-08-26 12:53:41 +0200105
106decomp_func_ptr decompression_function(enum comp_algo algo)
107{
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;
Julius Werner09f29212015-09-29 13:51:35 -0700116 case CBFS_COMPRESS_LZ4:
117 decompress = lz4_decompress;
118 break;
Patrick Georgi61c82292015-08-26 12:53:41 +0200119 default:
120 ERROR("Unknown compression algorithm %d!\n", algo);
121 return NULL;
122 }
123 return decompress;
124}