x86_cpu_features.c: cpuid fix for x86_32 PIC with old GCC versions
[wimlib] / src / decompress.c
1 /*
2  * decompress.c
3  *
4  * Generic functions for decompression, wrapping around actual decompression
5  * implementations.
6  */
7
8 /*
9  * Copyright (C) 2013, 2014 Eric Biggers
10  *
11  * This file is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU Lesser General Public License as published by the Free
13  * Software Foundation; either version 3 of the License, or (at your option) any
14  * later version.
15  *
16  * This file is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this file; if not, see http://www.gnu.org/licenses/.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h"
27 #endif
28
29 #include "wimlib.h"
30 #include "wimlib/decompressor_ops.h"
31 #include "wimlib/util.h"
32
33 struct wimlib_decompressor {
34         const struct decompressor_ops *ops;
35         size_t max_block_size;
36         void *private;
37 };
38
39 static const struct decompressor_ops *decompressor_ops[] = {
40         [WIMLIB_COMPRESSION_TYPE_XPRESS] = &xpress_decompressor_ops,
41         [WIMLIB_COMPRESSION_TYPE_LZX]    = &lzx_decompressor_ops,
42         [WIMLIB_COMPRESSION_TYPE_LZMS]   = &lzms_decompressor_ops,
43 };
44
45 static bool
46 decompressor_ctype_valid(int ctype)
47 {
48         return (ctype >= 0 &&
49                 ctype < ARRAY_LEN(decompressor_ops) &&
50                 decompressor_ops[ctype] != NULL);
51 }
52
53 WIMLIBAPI int
54 wimlib_create_decompressor(enum wimlib_compression_type ctype,
55                            size_t max_block_size,
56                            struct wimlib_decompressor **dec_ret)
57 {
58         struct wimlib_decompressor *dec;
59
60         if (!decompressor_ctype_valid(ctype))
61                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
62
63         if (dec_ret == NULL)
64                 return WIMLIB_ERR_INVALID_PARAM;
65
66         if (max_block_size == 0)
67                 return WIMLIB_ERR_INVALID_PARAM;
68
69         dec = MALLOC(sizeof(*dec));
70         if (dec == NULL)
71                 return WIMLIB_ERR_NOMEM;
72         dec->ops = decompressor_ops[ctype];
73         dec->max_block_size = max_block_size;
74         dec->private = NULL;
75         if (dec->ops->create_decompressor) {
76                 int ret;
77
78                 ret = dec->ops->create_decompressor(max_block_size,
79                                                     &dec->private);
80                 if (ret) {
81                         FREE(dec);
82                         return ret;
83                 }
84         }
85         *dec_ret = dec;
86         return 0;
87 }
88
89 WIMLIBAPI int
90 wimlib_decompress(const void *compressed_data, size_t compressed_size,
91                   void *uncompressed_data, size_t uncompressed_size,
92                   struct wimlib_decompressor *dec)
93 {
94         if (unlikely(uncompressed_size > dec->max_block_size))
95                 return -2;
96
97         return dec->ops->decompress(compressed_data, compressed_size,
98                                     uncompressed_data, uncompressed_size,
99                                     dec->private);
100 }
101
102 WIMLIBAPI void
103 wimlib_free_decompressor(struct wimlib_decompressor *dec)
104 {
105         if (dec) {
106                 if (dec->ops->free_decompressor)
107                         dec->ops->free_decompressor(dec->private);
108                 FREE(dec);
109         }
110 }