blob: 51353317f4b3cb43cbfd6a596b2c1cae8aeb3ef9 [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>
23#include "common.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000024
Gabe Blackdbd006b2014-02-20 23:38:49 -080025static int lzma_compress(char *in, int in_len, char *out, int *out_len)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000026{
Gabe Blackdbd006b2014-02-20 23:38:49 -080027 return do_lzma_compress(in, in_len, out, out_len);
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000028}
29
Aaron Durbin5213c532015-10-23 17:38:40 -050030static int lzma_decompress(char *in, int in_len, char *out, unused int out_len,
31 size_t *actual_size)
Patrick Georgi61c82292015-08-26 12:53:41 +020032{
Aaron Durbin5213c532015-10-23 17:38:40 -050033 return do_lzma_uncompress(out, out_len, in, in_len, actual_size);
Patrick Georgi61c82292015-08-26 12:53:41 +020034}
Gabe Blackdbd006b2014-02-20 23:38:49 -080035static int none_compress(char *in, int in_len, char *out, int *out_len)
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000036{
37 memcpy(out, in, in_len);
38 *out_len = in_len;
Gabe Blackdbd006b2014-02-20 23:38:49 -080039 return 0;
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000040}
Patrick Georgib7b56dd82009-09-14 13:29:27 +000041
Aaron Durbin5213c532015-10-23 17:38:40 -050042static int none_decompress(char *in, int in_len, char *out, unused int out_len,
43 size_t *actual_size)
Patrick Georgi61c82292015-08-26 12:53:41 +020044{
45 memcpy(out, in, in_len);
Aaron Durbin5213c532015-10-23 17:38:40 -050046 if (actual_size != NULL)
47 *actual_size = in_len;
Patrick Georgi61c82292015-08-26 12:53:41 +020048 return 0;
49}
50
Sol Boucher6310ccc2015-05-07 21:12:28 -070051comp_func_ptr compression_function(enum comp_algo algo)
Patrick Georgib7b56dd82009-09-14 13:29:27 +000052{
53 comp_func_ptr compress;
54 switch (algo) {
55 case CBFS_COMPRESS_NONE:
56 compress = none_compress;
57 break;
58 case CBFS_COMPRESS_LZMA:
59 compress = lzma_compress;
60 break;
61 default:
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +080062 ERROR("Unknown compression algorithm %d!\n", algo);
Patrick Georgib7b56dd82009-09-14 13:29:27 +000063 return NULL;
64 }
65 return compress;
66}
Patrick Georgi61c82292015-08-26 12:53:41 +020067
68decomp_func_ptr decompression_function(enum comp_algo algo)
69{
70 decomp_func_ptr decompress;
71 switch (algo) {
72 case CBFS_COMPRESS_NONE:
73 decompress = none_decompress;
74 break;
75 case CBFS_COMPRESS_LZMA:
76 decompress = lzma_decompress;
77 break;
78 default:
79 ERROR("Unknown compression algorithm %d!\n", algo);
80 return NULL;
81 }
82 return decompress;
83}