Make create_decompressor() checks of max_block_size consistent
authorEric Biggers <ebiggers3@gmail.com>
Wed, 20 Aug 2014 02:17:29 +0000 (21:17 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Wed, 20 Aug 2014 02:30:37 +0000 (21:30 -0500)
src/lzms-compress.c
src/lzms-decompress.c
src/xpress-compress.c
src/xpress-decompress.c

index b4391c2..21c82ea 100644 (file)
@@ -1335,10 +1335,12 @@ static u64
 lzms_get_needed_memory(size_t max_block_size, unsigned int compression_level)
 {
        struct lzms_compressor_params params;
+       u64 size = 0;
 
-       lzms_build_params(compression_level, &params);
+       if (max_block_size >= INT32_MAX)
+               return 0;
 
-       u64 size = 0;
+       lzms_build_params(compression_level, &params);
 
        size += sizeof(struct lzms_compressor);
        size += max_block_size;
index f8bfcdb..7254091 100644 (file)
@@ -1012,16 +1012,6 @@ lzms_decompress(const void *compressed_data, size_t compressed_size,
        if (uncompressed_size == 0)
                return 0;
 
-       /* The x86 post-processor requires that the uncompressed length fit into
-        * a signed 32-bit integer.  Also, the position slot table cannot be
-        * searched for a position of INT32_MAX or greater.  */
-       if (uncompressed_size >= INT32_MAX) {
-               LZMS_DEBUG("Uncompressed length too large "
-                          "(got %zu, expected < INT32_MAX)",
-                          uncompressed_size);
-               return -1;
-       }
-
        /* Decode the literals and matches.  */
        if (lzms_decode_items(compressed_data, compressed_size,
                              uncompressed_data, uncompressed_size, ctx))
@@ -1048,6 +1038,12 @@ lzms_create_decompressor(size_t max_block_size, void **ctx_ret)
 {
        struct lzms_decompressor *ctx;
 
+       /* The x86 post-processor requires that the uncompressed length fit into
+        * a signed 32-bit integer.  Also, the position slot table cannot be
+        * searched for a position of INT32_MAX or greater.  */
+       if (max_block_size >= INT32_MAX)
+               return WIMLIB_ERR_INVALID_PARAM;
+
        ctx = ALIGNED_MALLOC(sizeof(struct lzms_decompressor),
                             DECODE_TABLE_ALIGNMENT);
        if (ctx == NULL)
index e188497..5ff23e0 100644 (file)
@@ -1068,12 +1068,6 @@ xpress_build_mf_params(const struct xpress_compressor_params *xpress_params,
        mf_params->nice_match_len = xpress_params->nice_match_length;
 }
 
-static inline bool
-xpress_window_size_valid(size_t window_size)
-{
-       return (window_size > 0 && window_size <= XPRESS_MAX_OFFSET + 1);
-}
-
 static void
 xpress_free_compressor(void *_c);
 
@@ -1083,7 +1077,7 @@ xpress_get_needed_memory(size_t max_window_size, unsigned int compression_level)
        u64 size = 0;
        struct xpress_compressor_params params;
 
-       if (!xpress_window_size_valid(max_window_size))
+       if (max_window_size > XPRESS_MAX_OFFSET + 1)
                return 0;
 
        xpress_build_params(compression_level, max_window_size, &params);
@@ -1117,7 +1111,7 @@ xpress_create_compressor(size_t max_window_size, unsigned int compression_level,
        struct xpress_compressor_params params;
        struct lz_mf_params mf_params;
 
-       if (!xpress_window_size_valid(max_window_size))
+       if (max_window_size > XPRESS_MAX_OFFSET + 1)
                return WIMLIB_ERR_INVALID_PARAM;
 
        xpress_build_params(compression_level, max_window_size, &params);
index f060d0b..8d77410 100644 (file)
@@ -71,6 +71,7 @@
 
 #include "wimlib/decompressor_ops.h"
 #include "wimlib/decompress_common.h"
+#include "wimlib/error.h"
 #include "wimlib/xpress.h"
 
 /*
@@ -198,6 +199,16 @@ xpress_decompress(const void *compressed_data, size_t compressed_size,
                                uncompressed_size, decode_table);
 }
 
+static int
+xpress_create_decompressor(size_t max_block_size, void **dec_ret)
+{
+       if (max_block_size > XPRESS_MAX_OFFSET + 1)
+               return WIMLIB_ERR_INVALID_PARAM;
+
+       return 0;
+}
+
 const struct decompressor_ops xpress_decompressor_ops = {
-       .decompress = xpress_decompress,
+       .create_decompressor = xpress_create_decompressor,
+       .decompress          = xpress_decompress,
 };