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/error.h"
31 #include "wimlib/compressor_ops.h"
32 #include "wimlib/util.h"
34 struct wimlib_compressor {
35 const struct compressor_ops *ops;
37 enum wimlib_compression_type ctype;
38 size_t max_block_size;
41 static const struct compressor_ops * const compressor_ops[] = {
42 [WIMLIB_COMPRESSION_TYPE_XPRESS] = &xpress_compressor_ops,
43 [WIMLIB_COMPRESSION_TYPE_LZX] = &lzx_compressor_ops,
44 [WIMLIB_COMPRESSION_TYPE_LZMS] = &lzms_compressor_ops,
47 /* Scale: 10 = low, 50 = medium, 100 = high */
49 #define DEFAULT_COMPRESSION_LEVEL 50
51 static unsigned int default_compression_levels[ARRAY_LEN(compressor_ops)];
54 compressor_ctype_valid(int ctype)
57 ctype < ARRAY_LEN(compressor_ops) &&
58 compressor_ops[ctype] != NULL);
62 wimlib_set_default_compression_level(int ctype, unsigned int compression_level)
65 for (int i = 0; i < ARRAY_LEN(default_compression_levels); i++)
66 default_compression_levels[i] = compression_level;
68 if (!compressor_ctype_valid(ctype))
69 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
71 default_compression_levels[ctype] = compression_level;
77 wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype,
78 size_t max_block_size,
79 unsigned int compression_level)
82 const struct compressor_ops *ops;
85 destructive = (compression_level & WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE);
86 compression_level &= ~WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE;
88 if (!compressor_ctype_valid(ctype))
91 if (compression_level > 0xFFFFFF)
94 if (max_block_size == 0)
97 ops = compressor_ops[ctype];
99 if (compression_level == 0)
100 compression_level = default_compression_levels[ctype];
101 if (compression_level == 0)
102 compression_level = DEFAULT_COMPRESSION_LEVEL;
104 if (ops->get_needed_memory) {
105 size = ops->get_needed_memory(max_block_size, compression_level,
108 /* 0 is never valid and indicates an invalid max_block_size. */
114 return size + sizeof(struct wimlib_compressor);
118 wimlib_create_compressor(enum wimlib_compression_type ctype,
119 size_t max_block_size,
120 unsigned int compression_level,
121 struct wimlib_compressor **c_ret)
124 struct wimlib_compressor *c;
126 destructive = (compression_level & WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE);
127 compression_level &= ~WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE;
129 if (!compressor_ctype_valid(ctype))
130 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
132 if (compression_level > 0xFFFFFF)
133 return WIMLIB_ERR_INVALID_PARAM;
136 return WIMLIB_ERR_INVALID_PARAM;
138 if (max_block_size == 0)
139 return WIMLIB_ERR_INVALID_PARAM;
141 c = MALLOC(sizeof(*c));
143 return WIMLIB_ERR_NOMEM;
144 c->ops = compressor_ops[ctype];
147 c->max_block_size = max_block_size;
148 if (c->ops->create_compressor) {
151 if (compression_level == 0)
152 compression_level = default_compression_levels[ctype];
153 if (compression_level == 0)
154 compression_level = DEFAULT_COMPRESSION_LEVEL;
156 ret = c->ops->create_compressor(max_block_size,
170 wimlib_compress(const void *uncompressed_data, size_t uncompressed_size,
171 void *compressed_data, size_t compressed_size_avail,
172 struct wimlib_compressor *c)
174 if (unlikely(uncompressed_size == 0 || uncompressed_size > c->max_block_size))
177 return c->ops->compress(uncompressed_data, uncompressed_size,
178 compressed_data, compressed_size_avail,
183 wimlib_free_compressor(struct wimlib_compressor *c)
186 if (c->ops->free_compressor)
187 c->ops->free_compressor(c->private);