]> wimlib.net Git - wimlib/blob - src/compress.c
Windows: improved error messages
[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         const struct compressor_ops *ops;
85         u64 size;
86
87         if (!compressor_ctype_valid(ctype))
88                 return 0;
89
90         if (max_block_size == 0)
91                 return 0;
92
93         ops = compressor_ops[ctype];
94
95         if (compression_level == 0)
96                 compression_level = default_compression_levels[ctype];
97         if (compression_level == 0)
98                 compression_level = DEFAULT_COMPRESSION_LEVEL;
99
100         if (ops->get_needed_memory) {
101                 size = ops->get_needed_memory(max_block_size, compression_level);
102
103                 /* 0 is never valid and indicates an invalid max_block_size.  */
104                 if (size == 0)
105                         return 0;
106         } else {
107                 size = 0;
108         }
109         return size + sizeof(struct wimlib_compressor);
110 }
111
112 WIMLIBAPI int
113 wimlib_create_compressor(enum wimlib_compression_type ctype,
114                          size_t max_block_size,
115                          unsigned int compression_level,
116                          struct wimlib_compressor **c_ret)
117 {
118         struct wimlib_compressor *c;
119
120         if (!compressor_ctype_valid(ctype))
121                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
122
123         if (c_ret == NULL)
124                 return WIMLIB_ERR_INVALID_PARAM;
125
126         if (max_block_size == 0)
127                 return WIMLIB_ERR_INVALID_PARAM;
128
129         c = MALLOC(sizeof(*c));
130         if (c == NULL)
131                 return WIMLIB_ERR_NOMEM;
132         c->ops = compressor_ops[ctype];
133         c->private = NULL;
134         c->ctype = ctype;
135         c->max_block_size = max_block_size;
136         if (c->ops->create_compressor) {
137                 int ret;
138
139                 if (compression_level == 0)
140                         compression_level = default_compression_levels[ctype];
141                 if (compression_level == 0)
142                         compression_level = DEFAULT_COMPRESSION_LEVEL;
143
144                 ret = c->ops->create_compressor(max_block_size,
145                                                 compression_level,
146                                                 &c->private);
147                 if (ret) {
148                         FREE(c);
149                         return ret;
150                 }
151         }
152         *c_ret = c;
153         return 0;
154 }
155
156 WIMLIBAPI size_t
157 wimlib_compress(const void *uncompressed_data, size_t uncompressed_size,
158                 void *compressed_data, size_t compressed_size_avail,
159                 struct wimlib_compressor *c)
160 {
161         size_t compressed_size;
162
163         if (unlikely(uncompressed_size == 0 || uncompressed_size > c->max_block_size))
164                 return 0;
165
166         compressed_size = c->ops->compress(uncompressed_data,
167                                            uncompressed_size,
168                                            compressed_data,
169                                            compressed_size_avail,
170                                            c->private);
171
172         /* (Optional) Verify that we really get the same thing back when
173          * decompressing.  Should always be the case, unless there's a bug.  */
174 #ifdef ENABLE_VERIFY_COMPRESSION
175         if (compressed_size != 0) {
176                 struct wimlib_decompressor *d;
177                 int res;
178                 u8 *buf;
179
180                 buf = MALLOC(uncompressed_size);
181                 if (!buf) {
182                         WARNING("Unable to verify results of %s compression "
183                                 "(can't allocate buffer)",
184                                 wimlib_get_compression_type_string(c->ctype));
185                         return 0;
186                 }
187
188                 res = wimlib_create_decompressor(c->ctype,
189                                                  c->max_block_size, &d);
190                 if (res) {
191                         WARNING("Unable to verify results of %s compression "
192                                 "(can't create decompressor)",
193                                 wimlib_get_compression_type_string(c->ctype));
194                         FREE(buf);
195                         return 0;
196                 }
197
198                 res = wimlib_decompress(compressed_data, compressed_size,
199                                         buf, uncompressed_size, d);
200                 wimlib_free_decompressor(d);
201                 if (res) {
202                         ERROR("Failed to decompress our %s-compressed data",
203                               wimlib_get_compression_type_string(c->ctype));
204                         FREE(buf);
205                         abort();
206                 }
207
208                 res = memcmp(uncompressed_data, buf, uncompressed_size);
209                 FREE(buf);
210
211                 if (res) {
212                         ERROR("Our %s-compressed data did not decompress "
213                               "to original",
214                               wimlib_get_compression_type_string(c->ctype));
215                         abort();
216                 }
217         }
218 #endif /* ENABLE_VERIFY_COMPRESSION */
219
220         return compressed_size;
221 }
222
223 WIMLIBAPI void
224 wimlib_free_compressor(struct wimlib_compressor *c)
225 {
226         if (c) {
227                 if (c->ops->free_compressor)
228                         c->ops->free_compressor(c->private);
229                 FREE(c);
230         }
231 }