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