4 * Generic functions for compression, wrapping around actual compression
9 * Copyright (C) 2013, 2014 Eric Biggers
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
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
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/.
30 #include "wimlib/assert.h"
31 #include "wimlib/error.h"
32 #include "wimlib/compressor_ops.h"
33 #include "wimlib/util.h"
38 struct wimlib_compressor {
39 const struct compressor_ops *ops;
41 enum wimlib_compression_type ctype;
42 size_t max_block_size;
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,
51 /* Scale: 10 = low, 50 = medium, 100 = high */
53 #define DEFAULT_COMPRESSION_LEVEL 50
55 static unsigned int default_compression_levels[ARRAY_LEN(compressor_ops)];
58 compressor_ctype_valid(int ctype)
61 ctype < ARRAY_LEN(compressor_ops) &&
62 compressor_ops[ctype] != NULL);
66 wimlib_set_default_compression_level(int ctype, unsigned int compression_level)
69 for (int i = 0; i < ARRAY_LEN(default_compression_levels); i++)
70 default_compression_levels[i] = compression_level;
72 if (!compressor_ctype_valid(ctype))
73 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
75 default_compression_levels[ctype] = compression_level;
81 wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype,
82 size_t max_block_size,
83 unsigned int compression_level)
85 const struct compressor_ops *ops;
88 if (!compressor_ctype_valid(ctype))
91 if (max_block_size == 0)
94 ops = compressor_ops[ctype];
96 if (compression_level == 0)
97 compression_level = default_compression_levels[ctype];
98 if (compression_level == 0)
99 compression_level = DEFAULT_COMPRESSION_LEVEL;
101 if (ops->get_needed_memory) {
102 size = ops->get_needed_memory(max_block_size, compression_level);
104 /* 0 is never valid and indicates an invalid max_block_size. */
110 return size + sizeof(struct wimlib_compressor);
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)
119 struct wimlib_compressor *c;
121 if (!compressor_ctype_valid(ctype))
122 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
125 return WIMLIB_ERR_INVALID_PARAM;
127 if (max_block_size == 0)
128 return WIMLIB_ERR_INVALID_PARAM;
130 c = MALLOC(sizeof(*c));
132 return WIMLIB_ERR_NOMEM;
133 c->ops = compressor_ops[ctype];
136 c->max_block_size = max_block_size;
137 if (c->ops->create_compressor) {
140 if (compression_level == 0)
141 compression_level = default_compression_levels[ctype];
142 if (compression_level == 0)
143 compression_level = DEFAULT_COMPRESSION_LEVEL;
145 ret = c->ops->create_compressor(max_block_size,
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)
162 size_t compressed_size;
164 if (unlikely(uncompressed_size == 0 || uncompressed_size > c->max_block_size))
167 compressed_size = c->ops->compress(uncompressed_data,
170 compressed_size_avail,
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;
181 buf = MALLOC(uncompressed_size);
183 WARNING("Unable to verify results of %s compression "
184 "(can't allocate buffer)",
185 wimlib_get_compression_type_string(c->ctype));
189 res = wimlib_create_decompressor(c->ctype,
190 c->max_block_size, &d);
192 WARNING("Unable to verify results of %s compression "
193 "(can't create decompressor)",
194 wimlib_get_compression_type_string(c->ctype));
199 res = wimlib_decompress(compressed_data, compressed_size,
200 buf, uncompressed_size, d);
201 wimlib_free_decompressor(d);
203 ERROR("Failed to decompress our %s-compressed data",
204 wimlib_get_compression_type_string(c->ctype));
209 res = memcmp(uncompressed_data, buf, uncompressed_size);
213 ERROR("Our %s-compressed data did not decompress "
215 wimlib_get_compression_type_string(c->ctype));
219 #endif /* ENABLE_VERIFY_COMPRESSION */
221 return compressed_size;
225 wimlib_free_compressor(struct wimlib_compressor *c)
228 if (c->ops->free_compressor)
229 c->ops->free_compressor(c->private);