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)
85 const struct compressor_ops *ops;
88 destructive = (compression_level & WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE);
89 compression_level &= ~WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE;
91 if (!compressor_ctype_valid(ctype))
94 if (compression_level > 0xFFFFFF)
97 if (max_block_size == 0)
100 ops = compressor_ops[ctype];
102 if (compression_level == 0)
103 compression_level = default_compression_levels[ctype];
104 if (compression_level == 0)
105 compression_level = DEFAULT_COMPRESSION_LEVEL;
107 if (ops->get_needed_memory) {
108 size = ops->get_needed_memory(max_block_size, compression_level,
111 /* 0 is never valid and indicates an invalid max_block_size. */
117 return size + sizeof(struct wimlib_compressor);
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)
127 struct wimlib_compressor *c;
129 destructive = (compression_level & WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE);
130 compression_level &= ~WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE;
132 if (!compressor_ctype_valid(ctype))
133 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
135 if (compression_level > 0xFFFFFF)
136 return WIMLIB_ERR_INVALID_PARAM;
139 return WIMLIB_ERR_INVALID_PARAM;
141 if (max_block_size == 0)
142 return WIMLIB_ERR_INVALID_PARAM;
144 c = MALLOC(sizeof(*c));
146 return WIMLIB_ERR_NOMEM;
147 c->ops = compressor_ops[ctype];
150 c->max_block_size = max_block_size;
151 if (c->ops->create_compressor) {
154 if (compression_level == 0)
155 compression_level = default_compression_levels[ctype];
156 if (compression_level == 0)
157 compression_level = DEFAULT_COMPRESSION_LEVEL;
159 ret = c->ops->create_compressor(max_block_size,
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)
177 size_t compressed_size;
179 if (unlikely(uncompressed_size == 0 || uncompressed_size > c->max_block_size))
182 compressed_size = c->ops->compress(uncompressed_data,
185 compressed_size_avail,
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;
196 buf = MALLOC(uncompressed_size);
198 WARNING("Unable to verify results of %s compression "
199 "(can't allocate buffer)",
200 wimlib_get_compression_type_string(c->ctype));
204 res = wimlib_create_decompressor(c->ctype,
205 c->max_block_size, &d);
207 WARNING("Unable to verify results of %s compression "
208 "(can't create decompressor)",
209 wimlib_get_compression_type_string(c->ctype));
214 res = wimlib_decompress(compressed_data, compressed_size,
215 buf, uncompressed_size, d);
216 wimlib_free_decompressor(d);
218 ERROR("Failed to decompress our %s-compressed data",
219 wimlib_get_compression_type_string(c->ctype));
224 res = memcmp(uncompressed_data, buf, uncompressed_size);
228 ERROR("Our %s-compressed data did not decompress "
230 wimlib_get_compression_type_string(c->ctype));
234 #endif /* ENABLE_VERIFY_COMPRESSION */
236 return compressed_size;
240 wimlib_free_compressor(struct wimlib_compressor *c)
243 if (c->ops->free_compressor)
244 c->ops->free_compressor(c->private);