X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fcompress.c;h=1976579289dd416d76344cf7e0fc99c9e939a4e3;hb=3e8aa757aaa63297f0d54007adf46411778fb6a8;hp=75f92f13441029ae80a1400d502208659471797d;hpb=91b0f909393476f39ed5865dfe4b27c244ba3c48;p=wimlib diff --git a/src/compress.c b/src/compress.c index 75f92f13..19765792 100644 --- a/src/compress.c +++ b/src/compress.c @@ -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,