Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 1 | /* |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 2 | * compression handling for cbfstool |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 3 | * |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 4 | * 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. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 10 | * |
| 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. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 22 | * Foundation, Inc. |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #include <string.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include "common.h" |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 28 | |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 29 | static int lzma_compress(char *in, int in_len, char *out, int *out_len) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 30 | { |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 31 | return do_lzma_compress(in, in_len, out, out_len); |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame^] | 34 | static int lzma_decompress(char *in, int in_len, char *out, unused int out_len) |
| 35 | { |
| 36 | return do_lzma_uncompress(out, out_len, in, in_len); |
| 37 | } |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 38 | static int none_compress(char *in, int in_len, char *out, int *out_len) |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 39 | { |
| 40 | memcpy(out, in, in_len); |
| 41 | *out_len = in_len; |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame] | 42 | return 0; |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 43 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 44 | |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame^] | 45 | static int none_decompress(char *in, int in_len, char *out, unused int out_len) |
| 46 | { |
| 47 | memcpy(out, in, in_len); |
| 48 | return 0; |
| 49 | } |
| 50 | |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 51 | comp_func_ptr compression_function(enum comp_algo algo) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 52 | { |
| 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 Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 62 | ERROR("Unknown compression algorithm %d!\n", algo); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
| 65 | return compress; |
| 66 | } |
Patrick Georgi | 61c8229 | 2015-08-26 12:53:41 +0200 | [diff] [blame^] | 67 | |
| 68 | decomp_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 | } |