*/
/*
- * Copyright (C) 2013 Eric Biggers
+ * Copyright (C) 2013, 2014 Eric Biggers
*
- * This file is part of wimlib, a library for working with WIM files.
+ * This file is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option) any
+ * later version.
*
- * wimlib is free software; you can redistribute it and/or modify it under the
- * terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option)
- * any later version.
- *
- * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- * A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * This file is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
- * You should have received a copy of the GNU General Public License
- * along with wimlib; if not, see http://www.gnu.org/licenses/.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this file; if not, see http://www.gnu.org/licenses/.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
+#include <stdlib.h>
+#include <string.h>
+
#include "wimlib.h"
+#include "wimlib/error.h"
#include "wimlib/compressor_ops.h"
#include "wimlib/util.h"
struct wimlib_compressor {
const struct compressor_ops *ops;
void *private;
+ enum wimlib_compression_type ctype;
+ size_t max_block_size;
};
static const struct compressor_ops *compressor_ops[] = {
- [WIMLIB_COMPRESSION_TYPE_LZX] = &lzx_compressor_ops,
[WIMLIB_COMPRESSION_TYPE_XPRESS] = &xpress_compressor_ops,
+ [WIMLIB_COMPRESSION_TYPE_LZX] = &lzx_compressor_ops,
[WIMLIB_COMPRESSION_TYPE_LZMS] = &lzms_compressor_ops,
};
-static struct wimlib_compressor_params_header *
-compressor_default_params[ARRAY_LEN(compressor_ops)] = {
-};
+/* Scale: 10 = low, 50 = medium, 100 = high */
+
+#define DEFAULT_COMPRESSION_LEVEL 50
+
+static unsigned int default_compression_levels[ARRAY_LEN(compressor_ops)];
static bool
compressor_ctype_valid(int ctype)
}
WIMLIBAPI int
-wimlib_set_default_compressor_params(enum wimlib_compression_type ctype,
- const struct wimlib_compressor_params_header *params)
+wimlib_set_default_compression_level(int ctype, unsigned int compression_level)
{
- struct wimlib_compressor_params_header *dup;
-
- if (!compressor_ctype_valid(ctype))
- return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
-
- if (params != NULL &&
- compressor_ops[ctype]->params_valid != NULL &&
- !compressor_ops[ctype]->params_valid(params))
- return WIMLIB_ERR_INVALID_PARAM;
+ if (ctype == -1) {
+ for (int i = 0; i < ARRAY_LEN(default_compression_levels); i++)
+ default_compression_levels[i] = compression_level;
+ } else {
+ if (!compressor_ctype_valid(ctype))
+ return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
- dup = NULL;
- if (params) {
- dup = memdup(params, params->size);
- if (dup == NULL)
- return WIMLIB_ERR_NOMEM;
+ default_compression_levels[ctype] = compression_level;
}
-
- FREE(compressor_default_params[ctype]);
- compressor_default_params[ctype] = dup;
return 0;
}
-void
-cleanup_compressor_params(void)
-{
- for (size_t i = 0; i < ARRAY_LEN(compressor_default_params); i++) {
- FREE(compressor_default_params[i]);
- compressor_default_params[i] = NULL;
- }
-}
-
WIMLIBAPI u64
wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype,
size_t max_block_size,
- const struct wimlib_compressor_params_header *extra_params)
+ unsigned int compression_level)
{
const struct compressor_ops *ops;
- const struct wimlib_compressor_params_header *params;
+ u64 size;
if (!compressor_ctype_valid(ctype))
return 0;
- ops = compressor_ops[ctype];
- if (ops->get_needed_memory == NULL)
+ if (max_block_size == 0)
return 0;
- if (extra_params) {
- params = extra_params;
- if (ops->params_valid && !ops->params_valid(params))
+ ops = compressor_ops[ctype];
+
+ if (compression_level == 0)
+ compression_level = default_compression_levels[ctype];
+ if (compression_level == 0)
+ compression_level = DEFAULT_COMPRESSION_LEVEL;
+
+ if (ops->get_needed_memory) {
+ size = ops->get_needed_memory(max_block_size, compression_level);
+
+ /* 0 is never valid and indicates an invalid max_block_size. */
+ if (size == 0)
return 0;
} else {
- params = compressor_default_params[ctype];
+ size = 0;
}
-
- return ops->get_needed_memory(max_block_size, params);
+ return size + sizeof(struct wimlib_compressor);
}
-
WIMLIBAPI int
wimlib_create_compressor(enum wimlib_compression_type ctype,
size_t max_block_size,
- const struct wimlib_compressor_params_header *extra_params,
+ unsigned int compression_level,
struct wimlib_compressor **c_ret)
{
struct wimlib_compressor *c;
+ if (!compressor_ctype_valid(ctype))
+ return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
+
if (c_ret == NULL)
return WIMLIB_ERR_INVALID_PARAM;
- if (!compressor_ctype_valid(ctype))
- return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
+ if (max_block_size == 0)
+ return WIMLIB_ERR_INVALID_PARAM;
c = MALLOC(sizeof(*c));
if (c == NULL)
return WIMLIB_ERR_NOMEM;
c->ops = compressor_ops[ctype];
c->private = NULL;
+ c->ctype = ctype;
+ c->max_block_size = max_block_size;
if (c->ops->create_compressor) {
- const struct wimlib_compressor_params_header *params;
int ret;
- if (extra_params) {
- params = extra_params;
- if (c->ops->params_valid && !c->ops->params_valid(params)) {
- FREE(c);
- return WIMLIB_ERR_INVALID_PARAM;
- }
- } else {
- params = compressor_default_params[ctype];
- }
+ if (compression_level == 0)
+ compression_level = default_compression_levels[ctype];
+ if (compression_level == 0)
+ compression_level = DEFAULT_COMPRESSION_LEVEL;
+
ret = c->ops->create_compressor(max_block_size,
- params, &c->private);
+ compression_level,
+ &c->private);
if (ret) {
FREE(c);
return ret;
void *compressed_data, size_t compressed_size_avail,
struct wimlib_compressor *c)
{
- return c->ops->compress(uncompressed_data, uncompressed_size,
- compressed_data, compressed_size_avail,
- c->private);
+ size_t compressed_size;
+
+ if (unlikely(uncompressed_size == 0 || uncompressed_size > c->max_block_size))
+ return 0;
+
+ compressed_size = c->ops->compress(uncompressed_data,
+ uncompressed_size,
+ compressed_data,
+ compressed_size_avail,
+ c->private);
+
+ /* (Optional) Verify that we really get the same thing back when
+ * decompressing. Should always be the case, unless there's a bug. */
+#ifdef ENABLE_VERIFY_COMPRESSION
+ if (compressed_size != 0) {
+ struct wimlib_decompressor *d;
+ int res;
+ u8 *buf;
+
+ buf = MALLOC(uncompressed_size);
+ if (!buf) {
+ WARNING("Unable to verify results of %s compression "
+ "(can't allocate buffer)",
+ wimlib_get_compression_type_string(c->ctype));
+ return 0;
+ }
+
+ res = wimlib_create_decompressor(c->ctype,
+ c->max_block_size, &d);
+ if (res) {
+ WARNING("Unable to verify results of %s compression "
+ "(can't create decompressor)",
+ wimlib_get_compression_type_string(c->ctype));
+ FREE(buf);
+ return 0;
+ }
+
+ res = wimlib_decompress(compressed_data, compressed_size,
+ buf, uncompressed_size, d);
+ wimlib_free_decompressor(d);
+ if (res) {
+ ERROR("Failed to decompress our %s-compressed data",
+ wimlib_get_compression_type_string(c->ctype));
+ FREE(buf);
+ abort();
+ }
+
+ res = memcmp(uncompressed_data, buf, uncompressed_size);
+ FREE(buf);
+
+ if (res) {
+ ERROR("Our %s-compressed data did not decompress "
+ "to original",
+ wimlib_get_compression_type_string(c->ctype));
+ abort();
+ }
+ }
+#endif /* ENABLE_VERIFY_COMPRESSION */
+
+ return compressed_size;
}
WIMLIBAPI void