Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* uncompr.c -- decompress a memory buffer |
michael@0 | 2 | * Copyright (C) 1995-2003, 2010 Jean-loup Gailly. |
michael@0 | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | /* @(#) $Id$ */ |
michael@0 | 7 | |
michael@0 | 8 | #define ZLIB_INTERNAL |
michael@0 | 9 | #include "zlib.h" |
michael@0 | 10 | |
michael@0 | 11 | /* =========================================================================== |
michael@0 | 12 | Decompresses the source buffer into the destination buffer. sourceLen is |
michael@0 | 13 | the byte length of the source buffer. Upon entry, destLen is the total |
michael@0 | 14 | size of the destination buffer, which must be large enough to hold the |
michael@0 | 15 | entire uncompressed data. (The size of the uncompressed data must have |
michael@0 | 16 | been saved previously by the compressor and transmitted to the decompressor |
michael@0 | 17 | by some mechanism outside the scope of this compression library.) |
michael@0 | 18 | Upon exit, destLen is the actual size of the compressed buffer. |
michael@0 | 19 | |
michael@0 | 20 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not |
michael@0 | 21 | enough memory, Z_BUF_ERROR if there was not enough room in the output |
michael@0 | 22 | buffer, or Z_DATA_ERROR if the input data was corrupted. |
michael@0 | 23 | */ |
michael@0 | 24 | int ZEXPORT uncompress (dest, destLen, source, sourceLen) |
michael@0 | 25 | Bytef *dest; |
michael@0 | 26 | uLongf *destLen; |
michael@0 | 27 | const Bytef *source; |
michael@0 | 28 | uLong sourceLen; |
michael@0 | 29 | { |
michael@0 | 30 | z_stream stream; |
michael@0 | 31 | int err; |
michael@0 | 32 | |
michael@0 | 33 | stream.next_in = (Bytef*)source; |
michael@0 | 34 | stream.avail_in = (uInt)sourceLen; |
michael@0 | 35 | /* Check for source > 64K on 16-bit machine: */ |
michael@0 | 36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; |
michael@0 | 37 | |
michael@0 | 38 | stream.next_out = dest; |
michael@0 | 39 | stream.avail_out = (uInt)*destLen; |
michael@0 | 40 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; |
michael@0 | 41 | |
michael@0 | 42 | stream.zalloc = (alloc_func)0; |
michael@0 | 43 | stream.zfree = (free_func)0; |
michael@0 | 44 | |
michael@0 | 45 | err = inflateInit(&stream); |
michael@0 | 46 | if (err != Z_OK) return err; |
michael@0 | 47 | |
michael@0 | 48 | err = inflate(&stream, Z_FINISH); |
michael@0 | 49 | if (err != Z_STREAM_END) { |
michael@0 | 50 | inflateEnd(&stream); |
michael@0 | 51 | if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) |
michael@0 | 52 | return Z_DATA_ERROR; |
michael@0 | 53 | return err; |
michael@0 | 54 | } |
michael@0 | 55 | *destLen = stream.total_out; |
michael@0 | 56 | |
michael@0 | 57 | err = inflateEnd(&stream); |
michael@0 | 58 | return err; |
michael@0 | 59 | } |