4 * Generic functions for compression, wrapping around actual compression
9 * Copyright (C) 2013 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
32 #include "wimlib/compressor_ops.h"
33 #include "wimlib/util.h"
35 struct wimlib_compressor {
36 const struct compressor_ops *ops;
40 static const struct compressor_ops *compressor_ops[] = {
41 [WIMLIB_COMPRESSION_TYPE_XPRESS] = &xpress_compressor_ops,
42 [WIMLIB_COMPRESSION_TYPE_LZX] = &lzx_compressor_ops,
43 [WIMLIB_COMPRESSION_TYPE_LZMS] = &lzms_compressor_ops,
46 static struct wimlib_compressor_params_header *
47 compressor_default_params[ARRAY_LEN(compressor_ops)] = {
51 compressor_ctype_valid(int ctype)
54 ctype < ARRAY_LEN(compressor_ops) &&
55 compressor_ops[ctype] != NULL);
59 wimlib_set_default_compressor_params(enum wimlib_compression_type ctype,
60 const struct wimlib_compressor_params_header *params)
62 struct wimlib_compressor_params_header *dup;
64 if (!compressor_ctype_valid(ctype))
65 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
68 compressor_ops[ctype]->params_valid != NULL &&
69 !compressor_ops[ctype]->params_valid(params))
70 return WIMLIB_ERR_INVALID_PARAM;
74 dup = memdup(params, params->size);
76 return WIMLIB_ERR_NOMEM;
79 FREE(compressor_default_params[ctype]);
80 compressor_default_params[ctype] = dup;
85 cleanup_compressor_params(void)
87 for (size_t i = 0; i < ARRAY_LEN(compressor_default_params); i++) {
88 FREE(compressor_default_params[i]);
89 compressor_default_params[i] = NULL;
94 wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype,
95 size_t max_block_size,
96 const struct wimlib_compressor_params_header *extra_params)
98 const struct compressor_ops *ops;
99 const struct wimlib_compressor_params_header *params;
102 if (!compressor_ctype_valid(ctype))
105 ops = compressor_ops[ctype];
108 params = extra_params;
109 if (ops->params_valid && !ops->params_valid(params))
112 params = compressor_default_params[ctype];
115 size = sizeof(struct wimlib_compressor);
116 if (ops->get_needed_memory)
117 size += ops->get_needed_memory(max_block_size, params);
123 wimlib_create_compressor(enum wimlib_compression_type ctype,
124 size_t max_block_size,
125 const struct wimlib_compressor_params_header *extra_params,
126 struct wimlib_compressor **c_ret)
128 struct wimlib_compressor *c;
131 return WIMLIB_ERR_INVALID_PARAM;
133 if (!compressor_ctype_valid(ctype))
134 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
136 c = MALLOC(sizeof(*c));
138 return WIMLIB_ERR_NOMEM;
139 c->ops = compressor_ops[ctype];
141 if (c->ops->create_compressor) {
142 const struct wimlib_compressor_params_header *params;
146 params = extra_params;
147 if (c->ops->params_valid && !c->ops->params_valid(params)) {
149 return WIMLIB_ERR_INVALID_PARAM;
152 params = compressor_default_params[ctype];
154 ret = c->ops->create_compressor(max_block_size,
155 params, &c->private);
166 wimlib_compress(const void *uncompressed_data, size_t uncompressed_size,
167 void *compressed_data, size_t compressed_size_avail,
168 struct wimlib_compressor *c)
170 return c->ops->compress(uncompressed_data, uncompressed_size,
171 compressed_data, compressed_size_avail,
176 wimlib_free_compressor(struct wimlib_compressor *c)
179 if (c->ops->free_compressor)
180 c->ops->free_compressor(c->private);