]> wimlib.net Git - wimlib/blobdiff - src/compress.c
configure.ac: generate version number from git commit and tags
[wimlib] / src / compress.c
index 4c99dab145db7b04c5f6fb80f6207d6537b95179..1b0d2c9f9aff5d0f04b1537bda5f264b3c9efd89 100644 (file)
@@ -8,20 +8,18 @@
 /*
  * 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
 #endif
 
 #include "wimlib.h"
-#include "wimlib/assert.h"
 #include "wimlib/error.h"
 #include "wimlib/compressor_ops.h"
 #include "wimlib/util.h"
 
-#include <stdlib.h>
-#include <string.h>
-
 struct wimlib_compressor {
        const struct compressor_ops *ops;
        void *private;
@@ -44,7 +38,7 @@ struct wimlib_compressor {
        size_t max_block_size;
 };
 
-static const struct compressor_ops *compressor_ops[] = {
+static const struct compressor_ops * const compressor_ops[] = {
        [WIMLIB_COMPRESSION_TYPE_XPRESS] = &xpress_compressor_ops,
        [WIMLIB_COMPRESSION_TYPE_LZX]    = &lzx_compressor_ops,
        [WIMLIB_COMPRESSION_TYPE_LZMS]   = &lzms_compressor_ops,
@@ -84,12 +78,22 @@ 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;
+
        ops = compressor_ops[ctype];
 
        if (compression_level == 0)
@@ -97,10 +101,17 @@ wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype,
        if (compression_level == 0)
                compression_level = DEFAULT_COMPRESSION_LEVEL;
 
-       size = sizeof(struct wimlib_compressor);
-       if (ops->get_needed_memory)
-               size += ops->get_needed_memory(max_block_size, compression_level);
-       return size;
+       if (ops->get_needed_memory) {
+               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)
+                       return 0;
+       } else {
+               size = 0;
+       }
+       return size + sizeof(struct wimlib_compressor);
 }
 
 WIMLIBAPI int
@@ -109,17 +120,24 @@ 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;
 
        if (max_block_size == 0)
                return WIMLIB_ERR_INVALID_PARAM;
 
-       if (!compressor_ctype_valid(ctype))
-               return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
-
        c = MALLOC(sizeof(*c));
        if (c == NULL)
                return WIMLIB_ERR_NOMEM;
@@ -137,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);
@@ -152,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