X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Fcompress.c;h=259e7a4caca38aa3434d7f4ac3742346428144a0;hp=1976579289dd416d76344cf7e0fc99c9e939a4e3;hb=a8343baff22193be4651a3a63b07cb1520ced4e8;hpb=7251c7d0afac3b738dda1c4f45e6d3d3090f2622 diff --git a/src/compress.c b/src/compress.c index 19765792..259e7a4c 100644 --- a/src/compress.c +++ b/src/compress.c @@ -31,9 +31,6 @@ #include "wimlib/compressor_ops.h" #include "wimlib/util.h" -#include -#include - struct wimlib_compressor { const struct compressor_ops *ops; void *private; @@ -81,12 +78,19 @@ wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype, size_t max_block_size, unsigned int compression_level) { + bool destructive; const struct compressor_ops *ops; u64 size; + destructive = (compression_level & WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE); + compression_level &= ~WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE; + if (!compressor_ctype_valid(ctype)) return 0; + if (compression_level > 0xFFFFFF) + return 0; + if (max_block_size == 0) return 0; @@ -98,7 +102,8 @@ wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype, compression_level = DEFAULT_COMPRESSION_LEVEL; if (ops->get_needed_memory) { - size = ops->get_needed_memory(max_block_size, compression_level); + size = ops->get_needed_memory(max_block_size, compression_level, + destructive); /* 0 is never valid and indicates an invalid max_block_size. */ if (size == 0) @@ -115,11 +120,18 @@ wimlib_create_compressor(enum wimlib_compression_type ctype, unsigned int compression_level, struct wimlib_compressor **c_ret) { + bool destructive; struct wimlib_compressor *c; + destructive = (compression_level & WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE); + compression_level &= ~WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE; + if (!compressor_ctype_valid(ctype)) return WIMLIB_ERR_INVALID_COMPRESSION_TYPE; + if (compression_level > 0xFFFFFF) + return WIMLIB_ERR_INVALID_PARAM; + if (c_ret == NULL) return WIMLIB_ERR_INVALID_PARAM; @@ -143,6 +155,7 @@ wimlib_create_compressor(enum wimlib_compression_type ctype, ret = c->ops->create_compressor(max_block_size, compression_level, + destructive, &c->private); if (ret) { FREE(c); @@ -158,66 +171,12 @@ wimlib_compress(const void *uncompressed_data, size_t uncompressed_size, void *compressed_data, size_t compressed_size_avail, struct wimlib_compressor *c) { - 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; + return c->ops->compress(uncompressed_data, uncompressed_size, + compressed_data, compressed_size_avail, + c->private); } WIMLIBAPI void