Joao Marcos Costa | 81014f7 | 2020-07-30 15:33:49 +0200 | [diff] [blame] | 1 | /* uncompr.c -- decompress a memory buffer |
| 2 | * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler |
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 | */ |
| 5 | |
| 6 | /* @(#) $Id$ */ |
| 7 | |
| 8 | #define ZLIB_INTERNAL |
| 9 | #include "zlib.h" |
| 10 | |
| 11 | /* =========================================================================== |
| 12 | Decompresses the source buffer into the destination buffer. *sourceLen is |
| 13 | the byte length of the source buffer. Upon entry, *destLen is the total size |
| 14 | of the destination buffer, which must be large enough to hold the entire |
| 15 | uncompressed data. (The size of the uncompressed data must have been saved |
| 16 | previously by the compressor and transmitted to the decompressor by some |
| 17 | mechanism outside the scope of this compression library.) Upon exit, |
| 18 | *destLen is the size of the decompressed data and *sourceLen is the number |
| 19 | of source bytes consumed. Upon return, source + *sourceLen points to the |
| 20 | first unused input byte. |
| 21 | |
| 22 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough |
| 23 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, or |
| 24 | Z_DATA_ERROR if the input data was corrupted, including if the input data is |
| 25 | an incomplete zlib stream. |
| 26 | */ |
| 27 | int ZEXPORT uncompress2(dest, destLen, source, sourceLen) |
| 28 | Bytef *dest; |
| 29 | uLongf *destLen; |
| 30 | const Bytef *source; |
| 31 | uLong *sourceLen; |
| 32 | |
| 33 | { |
| 34 | z_stream stream; |
| 35 | int err; |
| 36 | const uInt max = (uInt)-1; |
| 37 | uLong len, left; |
| 38 | /* for detection of incomplete stream when *destLen == 0 */ |
| 39 | Byte buf[1]; |
| 40 | |
| 41 | len = *sourceLen; |
| 42 | if (*destLen) { |
| 43 | left = *destLen; |
| 44 | *destLen = 0; |
| 45 | } else { |
| 46 | left = 1; |
| 47 | dest = buf; |
| 48 | } |
| 49 | |
| 50 | stream.next_in = (z_const Bytef *)source; |
| 51 | stream.avail_in = 0; |
| 52 | stream.zalloc = (alloc_func)0; |
| 53 | stream.zfree = (free_func)0; |
| 54 | stream.opaque = (voidpf)0; |
| 55 | |
| 56 | err = inflateInit(&stream); |
| 57 | if (err != Z_OK) |
| 58 | return err; |
| 59 | |
| 60 | stream.next_out = dest; |
| 61 | stream.avail_out = 0; |
| 62 | |
| 63 | do { |
| 64 | if (stream.avail_out == 0) { |
| 65 | stream.avail_out = left > (uLong)max ? max : (uInt)left; |
| 66 | left -= stream.avail_out; |
| 67 | } |
| 68 | |
| 69 | if (stream.avail_in == 0) { |
| 70 | stream.avail_in = len > (uLong)max ? max : (uInt)len; |
| 71 | len -= stream.avail_in; |
| 72 | } |
| 73 | |
| 74 | err = inflate(&stream, Z_NO_FLUSH); |
| 75 | } while (err == Z_OK); |
| 76 | |
| 77 | *sourceLen -= len + stream.avail_in; |
| 78 | if (dest != buf) |
| 79 | *destLen = stream.total_out; |
| 80 | else if (stream.total_out && err == Z_BUF_ERROR) |
| 81 | left = 1; |
| 82 | |
| 83 | inflateEnd(&stream); |
| 84 | return err == Z_STREAM_END ? Z_OK : |
| 85 | err == Z_NEED_DICT ? Z_DATA_ERROR : |
| 86 | err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR : |
| 87 | err; |
| 88 | } |
| 89 | |
| 90 | int ZEXPORT uncompress(dest, destLen, source, sourceLen) |
| 91 | Bytef *dest; |
| 92 | uLongf *destLen; |
| 93 | const Bytef *source; |
| 94 | uLong sourceLen; |
| 95 | { |
| 96 | return uncompress2(dest, destLen, source, &sourceLen); |
| 97 | } |