| 1 |
|
| /* uncompr.c -- decompress a memory buffer |
| 2 |
|
| * Copyright (C) 1995-2026 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 |
|
| The _z versions of the functions take size_t length arguments. |
| 28 |
|
| */ |
| 29 |
0 |
| int ZEXPORT uncompress2_z(Bytef *dest, z_size_t *destLen, const Bytef *source, |
| 30 |
|
| z_size_t *sourceLen) { |
| 31 |
|
| z_stream stream; |
| 32 |
|
| int err; |
| 33 |
0 |
| const uInt max = (uInt)-1; |
| 34 |
|
| z_size_t len, left; |
| 35 |
|
| |
| 36 |
0 |
▶ | if (sourceLen == Z_NULL || (*sourceLen > 0 && source == Z_NULL) || |
Branch: 4 branches
| Branch | True | False | Total |
| if (sourceLen == Z_NULL || (*sourceLen > 0 && source == Z_NULL) || |
0 |
0 |
0 |
| if (sourceLen == Z_NULL || (*sourceLen > 0 && source == Z_NULL) || |
0 |
0 |
0 |
| if (sourceLen == Z_NULL || (*sourceLen > 0 && source == Z_NULL) || |
0 |
0 |
0 |
| if (sourceLen == Z_NULL || (*sourceLen > 0 && source == Z_NULL) || |
0 |
0 |
0 |
|
| 37 |
0 |
▶ | destLen == Z_NULL || (*destLen > 0 && dest == Z_NULL)) |
Branch: 2 branches
| Branch | True | False | Total |
| destLen == Z_NULL || (*destLen > 0 && dest == Z_NULL)) |
0 |
0 |
0 |
| destLen == Z_NULL || (*destLen > 0 && dest == Z_NULL)) |
0 |
0 |
0 |
|
| 38 |
0 |
| return Z_STREAM_ERROR; |
| 39 |
|
| |
| 40 |
0 |
| len = *sourceLen; |
| 41 |
0 |
| left = *destLen; |
| 42 |
0 |
▶ | if (left == 0 && dest == Z_NULL) |
Branch: 2 branches
| Branch | True | False | Total |
| if (left == 0 && dest == Z_NULL) |
0 |
0 |
0 |
| if (left == 0 && dest == Z_NULL) |
0 |
0 |
0 |
|
| 43 |
0 |
| dest = (Bytef *)&stream.reserved; /* next_out cannot be NULL */ |
| 44 |
|
| |
| 45 |
0 |
| stream.next_in = (z_const Bytef *)source; |
| 46 |
0 |
| stream.avail_in = 0; |
| 47 |
0 |
| stream.zalloc = (alloc_func)0; |
| 48 |
0 |
| stream.zfree = (free_func)0; |
| 49 |
0 |
| stream.opaque = (voidpf)0; |
| 50 |
|
| |
| 51 |
0 |
| err = inflateInit(&stream); |
| 52 |
0 |
▶ | if (err != Z_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != Z_OK) return err; |
0 |
0 |
0 |
|
| 53 |
|
| |
| 54 |
0 |
| stream.next_out = dest; |
| 55 |
0 |
| stream.avail_out = 0; |
| 56 |
|
| |
| 57 |
|
| do { |
| 58 |
0 |
▶ | if (stream.avail_out == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stream.avail_out == 0) { |
0 |
0 |
0 |
|
| 59 |
0 |
▶ | stream.avail_out = left > (z_size_t)max ? max : (uInt)left; |
Branch: 1 branch
| Branch | True | False | Total |
| stream.avail_out = left > (z_size_t)max ? max : (uInt)left; |
0 |
0 |
0 |
|
| 60 |
0 |
| left -= stream.avail_out; |
| 61 |
|
| } |
| 62 |
0 |
▶ | if (stream.avail_in == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stream.avail_in == 0) { |
0 |
0 |
0 |
|
| 63 |
0 |
▶ | stream.avail_in = len > (z_size_t)max ? max : (uInt)len; |
Branch: 1 branch
| Branch | True | False | Total |
| stream.avail_in = len > (z_size_t)max ? max : (uInt)len; |
0 |
0 |
0 |
|
| 64 |
0 |
| len -= stream.avail_in; |
| 65 |
|
| } |
| 66 |
0 |
| err = inflate(&stream, Z_NO_FLUSH); |
| 67 |
0 |
▶ | } while (err == Z_OK); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (err == Z_OK); |
0 |
0 |
0 |
|
| 68 |
|
| |
| 69 |
|
| /* Set len and left to the unused input data and unused output space. Set |
| 70 |
|
| *sourceLen to the amount of input consumed. Set *destLen to the amount |
| 71 |
|
| of data produced. */ |
| 72 |
0 |
| len += stream.avail_in; |
| 73 |
0 |
| left += stream.avail_out; |
| 74 |
0 |
| *sourceLen -= len; |
| 75 |
0 |
| *destLen -= left; |
| 76 |
|
| |
| 77 |
0 |
| inflateEnd(&stream); |
| 78 |
0 |
▶ | return err == Z_STREAM_END ? Z_OK : |
Branch: 1 branch
| Branch | True | False | Total |
| return err == Z_STREAM_END ? Z_OK : |
0 |
0 |
0 |
|
| 79 |
0 |
▶ | err == Z_NEED_DICT ? Z_DATA_ERROR : |
Branch: 1 branch
| Branch | True | False | Total |
| err == Z_NEED_DICT ? Z_DATA_ERROR : |
0 |
0 |
0 |
|
| 80 |
0 |
▶ | err == Z_BUF_ERROR && len == 0 ? Z_DATA_ERROR : |
Branch: 2 branches
| Branch | True | False | Total |
| err == Z_BUF_ERROR && len == 0 ? Z_DATA_ERROR : |
0 |
0 |
0 |
| err == Z_BUF_ERROR && len == 0 ? Z_DATA_ERROR : |
0 |
0 |
0 |
|
| 81 |
|
| err; |
| 82 |
|
| } |
| 83 |
0 |
| int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source, |
| 84 |
|
| uLong *sourceLen) { |
| 85 |
|
| int ret; |
| 86 |
0 |
| z_size_t got = *destLen, used = *sourceLen; |
| 87 |
0 |
| ret = uncompress2_z(dest, &got, source, &used); |
| 88 |
0 |
| *sourceLen = (uLong)used; |
| 89 |
0 |
| *destLen = (uLong)got; |
| 90 |
0 |
| return ret; |
| 91 |
|
| } |
| 92 |
0 |
| int ZEXPORT uncompress_z(Bytef *dest, z_size_t *destLen, const Bytef *source, |
| 93 |
|
| z_size_t sourceLen) { |
| 94 |
0 |
| z_size_t used = sourceLen; |
| 95 |
0 |
| return uncompress2_z(dest, destLen, source, &used); |
| 96 |
|
| } |
| 97 |
0 |
| int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, |
| 98 |
|
| uLong sourceLen) { |
| 99 |
0 |
| uLong used = sourceLen; |
| 100 |
0 |
| return uncompress2(dest, destLen, source, &used); |
| 101 |
|
| } |