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/.
33 #include "wimlib/error.h"
34 #include "wimlib/compressor_ops.h"
35 #include "wimlib/util.h"
37 struct wimlib_compressor {
38 const struct compressor_ops *ops;
40 enum wimlib_compression_type ctype;
41 size_t max_block_size;
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,
50 /* Scale: 10 = low, 50 = medium, 100 = high */
52 #define DEFAULT_COMPRESSION_LEVEL 50
54 static unsigned int default_compression_levels[ARRAY_LEN(compressor_ops)];
57 compressor_ctype_valid(int ctype)
60 ctype < ARRAY_LEN(compressor_ops) &&
61 compressor_ops[ctype] != NULL);
65 wimlib_set_default_compression_level(int ctype, unsigned int compression_level)
68 for (int i = 0; i < ARRAY_LEN(default_compression_levels); i++)
69 default_compression_levels[i] = compression_level;
71 if (!compressor_ctype_valid(ctype))
72 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
74 default_compression_levels[ctype] = compression_level;
80 wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype,
81 size_t max_block_size,
82 unsigned int compression_level)
84 const struct compressor_ops *ops;
87 if (!compressor_ctype_valid(ctype))
90 if (max_block_size == 0)
93 ops = compressor_ops[ctype];
95 if (compression_level == 0)
96 compression_level = default_compression_levels[ctype];
97 if (compression_level == 0)
98 compression_level = DEFAULT_COMPRESSION_LEVEL;
100 if (ops->get_needed_memory) {
101 size = ops->get_needed_memory(max_block_size, compression_level);
103 /* 0 is never valid and indicates an invalid max_block_size. */
109 return size + sizeof(struct wimlib_compressor);
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)
118 struct wimlib_compressor *c;
120 if (!compressor_ctype_valid(ctype))
121 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
124 return WIMLIB_ERR_INVALID_PARAM;
126 if (max_block_size == 0)
127 return WIMLIB_ERR_INVALID_PARAM;
129 c = MALLOC(sizeof(*c));
131 return WIMLIB_ERR_NOMEM;
132 c->ops = compressor_ops[ctype];
135 c->max_block_size = max_block_size;
136 if (c->ops->create_compressor) {
139 if (compression_level == 0)
140 compression_level = default_compression_levels[ctype];
141 if (compression_level == 0)
142 compression_level = DEFAULT_COMPRESSION_LEVEL;
144 ret = c->ops->create_compressor(max_block_size,
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)
161 size_t compressed_size;
163 if (unlikely(uncompressed_size == 0 || uncompressed_size > c->max_block_size))
166 compressed_size = c->ops->compress(uncompressed_data,
169 compressed_size_avail,
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;
180 buf = MALLOC(uncompressed_size);
182 WARNING("Unable to verify results of %s compression "
183 "(can't allocate buffer)",
184 wimlib_get_compression_type_string(c->ctype));
188 res = wimlib_create_decompressor(c->ctype,
189 c->max_block_size, &d);
191 WARNING("Unable to verify results of %s compression "
192 "(can't create decompressor)",
193 wimlib_get_compression_type_string(c->ctype));
198 res = wimlib_decompress(compressed_data, compressed_size,
199 buf, uncompressed_size, d);
200 wimlib_free_decompressor(d);
202 ERROR("Failed to decompress our %s-compressed data",
203 wimlib_get_compression_type_string(c->ctype));
208 res = memcmp(uncompressed_data, buf, uncompressed_size);
212 ERROR("Our %s-compressed data did not decompress "
214 wimlib_get_compression_type_string(c->ctype));
218 #endif /* ENABLE_VERIFY_COMPRESSION */
220 return compressed_size;
224 wimlib_free_compressor(struct wimlib_compressor *c)
227 if (c->ops->free_compressor)
228 c->ops->free_compressor(c->private);