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