]> wimlib.net Git - wimlib/blob - src/compress.c
414fad0f1e7dece566a2938668d57d61041a4843
[wimlib] / src / compress.c
1 /*
2  * compress.c
3  *
4  * Generic functions for compression, wrapping around actual compression
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/assert.h"
31 #include "wimlib/error.h"
32 #include "wimlib/compressor_ops.h"
33 #include "wimlib/util.h"
34
35 #include <stdlib.h>
36 #include <string.h>
37
38 struct wimlib_compressor {
39         const struct compressor_ops *ops;
40         void *private;
41         enum wimlib_compression_type ctype;
42         size_t max_block_size;
43 };
44
45 static const struct compressor_ops *compressor_ops[] = {
46         [WIMLIB_COMPRESSION_TYPE_XPRESS] = &xpress_compressor_ops,
47         [WIMLIB_COMPRESSION_TYPE_LZX]    = &lzx_compressor_ops,
48         [WIMLIB_COMPRESSION_TYPE_LZMS]   = &lzms_compressor_ops,
49 };
50
51 /* Scale: 10 = low, 50 = medium, 100 = high */
52
53 #define DEFAULT_COMPRESSION_LEVEL 50
54
55 static unsigned int default_compression_levels[ARRAY_LEN(compressor_ops)];
56
57 static bool
58 compressor_ctype_valid(int ctype)
59 {
60         return (ctype >= 0 &&
61                 ctype < ARRAY_LEN(compressor_ops) &&
62                 compressor_ops[ctype] != NULL);
63 }
64
65 WIMLIBAPI int
66 wimlib_set_default_compression_level(int ctype, unsigned int compression_level)
67 {
68         if (ctype == -1) {
69                 for (int i = 0; i < ARRAY_LEN(default_compression_levels); i++)
70                         default_compression_levels[i] = compression_level;
71         } else {
72                 if (!compressor_ctype_valid(ctype))
73                         return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
74
75                 default_compression_levels[ctype] = compression_level;
76         }
77         return 0;
78 }
79
80 WIMLIBAPI u64
81 wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype,
82                                     size_t max_block_size,
83                                     unsigned int compression_level)
84 {
85         const struct compressor_ops *ops;
86         u64 size;
87
88         if (!compressor_ctype_valid(ctype))
89                 return 0;
90
91         if (max_block_size == 0)
92                 return 0;
93
94         ops = compressor_ops[ctype];
95
96         if (compression_level == 0)
97                 compression_level = default_compression_levels[ctype];
98         if (compression_level == 0)
99                 compression_level = DEFAULT_COMPRESSION_LEVEL;
100
101         if (ops->get_needed_memory) {
102                 size = ops->get_needed_memory(max_block_size, compression_level);
103
104                 /* 0 is never valid and indicates an invalid max_block_size.  */
105                 if (size == 0)
106                         return 0;
107         } else {
108                 size = 0;
109         }
110         return size + sizeof(struct wimlib_compressor);
111 }
112
113 WIMLIBAPI int
114 wimlib_create_compressor(enum wimlib_compression_type ctype,
115                          size_t max_block_size,
116                          unsigned int compression_level,
117                          struct wimlib_compressor **c_ret)
118 {
119         struct wimlib_compressor *c;
120
121         if (!compressor_ctype_valid(ctype))
122                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
123
124         if (c_ret == NULL)
125                 return WIMLIB_ERR_INVALID_PARAM;
126
127         if (max_block_size == 0)
128                 return WIMLIB_ERR_INVALID_PARAM;
129
130         c = MALLOC(sizeof(*c));
131         if (c == NULL)
132                 return WIMLIB_ERR_NOMEM;
133         c->ops = compressor_ops[ctype];
134         c->private = NULL;
135         c->ctype = ctype;
136         c->max_block_size = max_block_size;
137         if (c->ops->create_compressor) {
138                 int ret;
139
140                 if (compression_level == 0)
141                         compression_level = default_compression_levels[ctype];
142                 if (compression_level == 0)
143                         compression_level = DEFAULT_COMPRESSION_LEVEL;
144
145                 ret = c->ops->create_compressor(max_block_size,
146                                                 compression_level,
147                                                 &c->private);
148                 if (ret) {
149                         FREE(c);
150                         return ret;
151                 }
152         }
153         *c_ret = c;
154         return 0;
155 }
156
157 WIMLIBAPI size_t
158 wimlib_compress(const void *uncompressed_data, size_t uncompressed_size,
159                 void *compressed_data, size_t compressed_size_avail,
160                 struct wimlib_compressor *c)
161 {
162         size_t compressed_size;
163
164         if (unlikely(uncompressed_size == 0 || uncompressed_size > c->max_block_size))
165                 return 0;
166
167         compressed_size = c->ops->compress(uncompressed_data,
168                                            uncompressed_size,
169                                            compressed_data,
170                                            compressed_size_avail,
171                                            c->private);
172
173         /* (Optional) Verify that we really get the same thing back when
174          * decompressing.  Should always be the case, unless there's a bug.  */
175 #ifdef ENABLE_VERIFY_COMPRESSION
176         if (compressed_size != 0) {
177                 struct wimlib_decompressor *d;
178                 int res;
179                 u8 *buf;
180
181                 buf = MALLOC(uncompressed_size);
182                 if (!buf) {
183                         WARNING("Unable to verify results of %s compression "
184                                 "(can't allocate buffer)",
185                                 wimlib_get_compression_type_string(c->ctype));
186                         return 0;
187                 }
188
189                 res = wimlib_create_decompressor(c->ctype,
190                                                  c->max_block_size, &d);
191                 if (res) {
192                         WARNING("Unable to verify results of %s compression "
193                                 "(can't create decompressor)",
194                                 wimlib_get_compression_type_string(c->ctype));
195                         FREE(buf);
196                         return 0;
197                 }
198
199                 res = wimlib_decompress(compressed_data, compressed_size,
200                                         buf, uncompressed_size, d);
201                 wimlib_free_decompressor(d);
202                 if (res) {
203                         ERROR("Failed to decompress our %s-compressed data",
204                               wimlib_get_compression_type_string(c->ctype));
205                         FREE(buf);
206                         abort();
207                 }
208
209                 res = memcmp(uncompressed_data, buf, uncompressed_size);
210                 FREE(buf);
211
212                 if (res) {
213                         ERROR("Our %s-compressed data did not decompress "
214                               "to original",
215                               wimlib_get_compression_type_string(c->ctype));
216                         abort();
217                 }
218         }
219 #endif /* ENABLE_VERIFY_COMPRESSION */
220
221         return compressed_size;
222 }
223
224 WIMLIBAPI void
225 wimlib_free_compressor(struct wimlib_compressor *c)
226 {
227         if (c) {
228                 if (c->ops->free_compressor)
229                         c->ops->free_compressor(c->private);
230                 FREE(c);
231         }
232 }