| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#define ZLIB_INTERNAL |
| 9 |
#include "zlib.h" |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
int ZEXPORT uncompress2 (dest, destLen, source, sourceLen) |
| 28 |
Bytef *dest; |
| 29 |
uLongf *destLen; |
| 30 |
const Bytef *source; |
| 31 |
uLong *sourceLen; |
| 32 |
{ |
| 33 |
z_stream stream; |
| 34 |
int err; |
| 35 |
const uInt max = (uInt)-1; |
| 36 |
uLong len, left; |
| 37 |
Byte buf[1]; |
| 38 |
|
| 39 |
len = *sourceLen; |
| 40 |
if (*destLen) { |
| 41 |
left = *destLen; |
| 42 |
*destLen = 0; |
| 43 |
} |
| 44 |
else { |
| 45 |
left = 1; |
| 46 |
dest = buf; |
| 47 |
} |
| 48 |
|
| 49 |
stream.next_in = (z_const Bytef *)source; |
| 50 |
stream.avail_in = 0; |
| 51 |
stream.zalloc = (alloc_func)0; |
| 52 |
stream.zfree = (free_func)0; |
| 53 |
stream.opaque = (voidpf)0; |
| 54 |
|
| 55 |
err = inflateInit(&stream); |
| 56 |
if (err != Z_OK) return err; |
| 57 |
|
| 58 |
stream.next_out = dest; |
| 59 |
stream.avail_out = 0; |
| 60 |
|
| 61 |
do { |
| 62 |
if (stream.avail_out == 0) { |
| 63 |
stream.avail_out = left > (uLong)max ? max : (uInt)left; |
| 64 |
left -= stream.avail_out; |
| 65 |
} |
| 66 |
if (stream.avail_in == 0) { |
| 67 |
stream.avail_in = len > (uLong)max ? max : (uInt)len; |
| 68 |
len -= stream.avail_in; |
| 69 |
} |
| 70 |
err = inflate(&stream, Z_NO_FLUSH); |
| 71 |
} while (err == Z_OK); |
| 72 |
|
| 73 |
*sourceLen -= len + stream.avail_in; |
| 74 |
if (dest != buf) |
| 75 |
*destLen = stream.total_out; |
| 76 |
else if (stream.total_out && err == Z_BUF_ERROR) |
| 77 |
left = 1; |
| 78 |
|
| 79 |
inflateEnd(&stream); |
| 80 |
return err == Z_STREAM_END ? Z_OK : |
| 81 |
err == Z_NEED_DICT ? Z_DATA_ERROR : |
| 82 |
err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR : |
| 83 |
err; |
| 84 |
} |
| 85 |
|
| 86 |
int ZEXPORT uncompress (dest, destLen, source, sourceLen) |
| 87 |
Bytef *dest; |
| 88 |
uLongf *destLen; |
| 89 |
const Bytef *source; |
| 90 |
uLong sourceLen; |
| 91 |
{ |
| 92 |
return uncompress2(dest, destLen, source, &sourceLen); |
| 93 |
} |