diff options
author | Mark Adler <madler@alumni.caltech.edu> | 2016-11-15 20:45:01 -0700 |
---|---|---|
committer | Mark Adler <madler@alumni.caltech.edu> | 2016-12-04 07:48:47 -0800 |
commit | 37281ac222ee7ceb8cc3253f13c8fa26a88dd566 (patch) | |
tree | 0ac0add0b56a769f7714e3a2af5a8b0210803f75 /uncompr.c | |
parent | 001300d0d91f75f03eed5ec97eca160452f62d61 (diff) | |
download | zlib-37281ac222ee7ceb8cc3253f13c8fa26a88dd566.tar.gz zlib-37281ac222ee7ceb8cc3253f13c8fa26a88dd566.tar.bz2 zlib-37281ac222ee7ceb8cc3253f13c8fa26a88dd566.zip |
Add uncompress2() function, which returns the input size used.
Diffstat (limited to 'uncompr.c')
-rw-r--r-- | uncompr.c | 47 |
1 files changed, 30 insertions, 17 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* uncompr.c -- decompress a memory buffer | 1 | /* uncompr.c -- decompress a memory buffer |
2 | * Copyright (C) 1995-2003, 2010, 2014 Jean-loup Gailly, Mark Adler | 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 | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
4 | */ | 4 | */ |
5 | 5 | ||
@@ -9,31 +9,34 @@ | |||
9 | #include "zlib.h" | 9 | #include "zlib.h" |
10 | 10 | ||
11 | /* =========================================================================== | 11 | /* =========================================================================== |
12 | Decompresses the source buffer into the destination buffer. sourceLen is | 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 | 13 | the byte length of the source buffer. Upon entry, *destLen is the total size |
14 | size of the destination buffer, which must be large enough to hold the | 14 | of the destination buffer, which must be large enough to hold the entire |
15 | entire uncompressed data. (The size of the uncompressed data must have | 15 | uncompressed data. (The size of the uncompressed data must have been saved |
16 | been saved previously by the compressor and transmitted to the decompressor | 16 | previously by the compressor and transmitted to the decompressor by some |
17 | by some mechanism outside the scope of this compression library.) | 17 | mechanism outside the scope of this compression library.) Upon exit, |
18 | Upon exit, destLen is the actual size of the compressed buffer. | 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. | ||
19 | 21 | ||
20 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not | 22 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough |
21 | enough memory, Z_BUF_ERROR if there was not enough room in the output | 23 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, or |
22 | buffer, or Z_DATA_ERROR if the input data was corrupted, including if the | 24 | Z_DATA_ERROR if the input data was corrupted, including if the input data is |
23 | input data is an incomplete zlib stream. | 25 | an incomplete zlib stream. |
24 | */ | 26 | */ |
25 | int ZEXPORT uncompress (dest, destLen, source, sourceLen) | 27 | int ZEXPORT uncompress2 (dest, destLen, source, sourceLen) |
26 | Bytef *dest; | 28 | Bytef *dest; |
27 | uLongf *destLen; | 29 | uLongf *destLen; |
28 | const Bytef *source; | 30 | const Bytef *source; |
29 | uLong sourceLen; | 31 | uLong *sourceLen; |
30 | { | 32 | { |
31 | z_stream stream; | 33 | z_stream stream; |
32 | int err; | 34 | int err; |
33 | const uInt max = (uInt)0 - 1; | 35 | const uInt max = (uInt)0 - 1; |
34 | uLong left; | 36 | uLong len, left; |
35 | Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */ | 37 | Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */ |
36 | 38 | ||
39 | len = *sourceLen; | ||
37 | if (*destLen) { | 40 | if (*destLen) { |
38 | left = *destLen; | 41 | left = *destLen; |
39 | *destLen = 0; | 42 | *destLen = 0; |
@@ -61,12 +64,13 @@ int ZEXPORT uncompress (dest, destLen, source, sourceLen) | |||
61 | left -= stream.avail_out; | 64 | left -= stream.avail_out; |
62 | } | 65 | } |
63 | if (stream.avail_in == 0) { | 66 | if (stream.avail_in == 0) { |
64 | stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen; | 67 | stream.avail_in = len > (uLong)max ? max : (uInt)len; |
65 | sourceLen -= stream.avail_in; | 68 | len -= stream.avail_in; |
66 | } | 69 | } |
67 | err = inflate(&stream, Z_NO_FLUSH); | 70 | err = inflate(&stream, Z_NO_FLUSH); |
68 | } while (err == Z_OK); | 71 | } while (err == Z_OK); |
69 | 72 | ||
73 | *sourceLen -= len + stream.avail_in; | ||
70 | if (dest != buf) | 74 | if (dest != buf) |
71 | *destLen = stream.total_out; | 75 | *destLen = stream.total_out; |
72 | else if (stream.total_out && err == Z_BUF_ERROR) | 76 | else if (stream.total_out && err == Z_BUF_ERROR) |
@@ -78,3 +82,12 @@ int ZEXPORT uncompress (dest, destLen, source, sourceLen) | |||
78 | err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR : | 82 | err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR : |
79 | err; | 83 | err; |
80 | } | 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 | } | ||