From: Mark Adler Date: Sat, 26 Apr 2014 04:52:19 +0000 (-0700) Subject: Fix compress() to work on lengths more than a maximum unsigned. X-Git-Tag: 1.9.9-b1~793^2~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6aa9399790e266d431e658d06509e781b5cc83d1;p=thirdparty%2Fzlib-ng.git Fix compress() to work on lengths more than a maximum unsigned. Conflicts: compress.c --- diff --git a/compress.c b/compress.c index 0bf1b0fc2..abae9b3ae 100644 --- a/compress.c +++ b/compress.c @@ -1,5 +1,5 @@ /* compress.c -- compress a memory buffer - * Copyright (C) 1995-2005 Jean-loup Gailly. + * Copyright (C) 1995-2005, 2014 Jean-loup Gailly, Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -23,13 +23,11 @@ int ZEXPORT compress2(unsigned char *dest, uLong *destLen, const unsigned char * uLong sourceLen, int level) { z_stream stream; int err; + const uInt max = -1; + uLong left; - stream.next_in = (const unsigned char *)source; - stream.avail_in = (uInt)sourceLen; - stream.next_out = dest; - stream.avail_out = (uInt)*destLen; - if ((uLong)stream.avail_out != *destLen) - return Z_BUF_ERROR; + left = *destLen; + *destLen = 0; stream.zalloc = (alloc_func)0; stream.zfree = (free_func)0; @@ -39,15 +37,26 @@ int ZEXPORT compress2(unsigned char *dest, uLong *destLen, const unsigned char * if (err != Z_OK) return err; - err = deflate(&stream, Z_FINISH); - if (err != Z_STREAM_END) { - deflateEnd(&stream); - return err == Z_OK ? Z_BUF_ERROR : err; - } - *destLen = stream.total_out; + stream.next_out = dest; + stream.avail_out = 0; + stream.next_in = (z_const Bytef *)source; + stream.avail_in = 0; - err = deflateEnd(&stream); - return err; + do { + if (stream.avail_out == 0) { + stream.avail_out = left > (uLong)max ? max : (uInt)left; + left -= stream.avail_out; + } + if (stream.avail_in == 0) { + stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen; + sourceLen -= stream.avail_in; + } + err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH); + } while (err == Z_OK); + + *destLen = stream.total_out; + deflateEnd(&stream); + return err == Z_STREAM_END ? Z_OK : err; } /* ===========================================================================