]> wimlib.net Git - wimlib/blobdiff - src/compress.c
Use --enable-ssse3-sha1 for x86_64 Windows builds
[wimlib] / src / compress.c
index 75f92f13441029ae80a1400d502208659471797d..1976579289dd416d76344cf7e0fc99c9e939a4e3 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
@@ -29,7 +27,6 @@
 #endif
 
 #include "wimlib.h"
-#include "wimlib/assert.h"
 #include "wimlib/error.h"
 #include "wimlib/compressor_ops.h"
 #include "wimlib/util.h"
@@ -90,6 +87,9 @@ wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype,
        if (!compressor_ctype_valid(ctype))
                return 0;
 
+       if (max_block_size == 0)
+               return 0;
+
        ops = compressor_ops[ctype];
 
        if (compression_level == 0)
@@ -97,10 +97,16 @@ 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);
+
+               /* 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
@@ -111,15 +117,15 @@ wimlib_create_compressor(enum wimlib_compression_type ctype,
 {
        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 (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;
@@ -154,7 +160,8 @@ wimlib_compress(const void *uncompressed_data, size_t uncompressed_size,
 {
        size_t compressed_size;
 
-       wimlib_assert(uncompressed_size <= c->max_block_size);
+       if (unlikely(uncompressed_size == 0 || uncompressed_size > c->max_block_size))
+               return 0;
 
        compressed_size = c->ops->compress(uncompressed_data,
                                           uncompressed_size,