summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2016-11-15 20:45:01 -0700
committerMark Adler <madler@alumni.caltech.edu>2016-12-04 07:48:47 -0800
commit37281ac222ee7ceb8cc3253f13c8fa26a88dd566 (patch)
tree0ac0add0b56a769f7714e3a2af5a8b0210803f75
parent001300d0d91f75f03eed5ec97eca160452f62d61 (diff)
downloadzlib-37281ac222ee7ceb8cc3253f13c8fa26a88dd566.tar.gz
zlib-37281ac222ee7ceb8cc3253f13c8fa26a88dd566.tar.bz2
zlib-37281ac222ee7ceb8cc3253f13c8fa26a88dd566.zip
Add uncompress2() function, which returns the input size used.
-rw-r--r--uncompr.c47
-rw-r--r--zlib.h10
2 files changed, 39 insertions, 18 deletions
diff --git a/uncompr.c b/uncompr.c
index f3f883a..2fbfcc0 100644
--- a/uncompr.c
+++ b/uncompr.c
@@ -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*/
25int ZEXPORT uncompress (dest, destLen, source, sourceLen) 27int 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
86int 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}
diff --git a/zlib.h b/zlib.h
index 5c99039..4544e57 100644
--- a/zlib.h
+++ b/zlib.h
@@ -1246,7 +1246,7 @@ ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
1246 uncompressed data. (The size of the uncompressed data must have been saved 1246 uncompressed data. (The size of the uncompressed data must have been saved
1247 previously by the compressor and transmitted to the decompressor by some 1247 previously by the compressor and transmitted to the decompressor by some
1248 mechanism outside the scope of this compression library.) Upon exit, destLen 1248 mechanism outside the scope of this compression library.) Upon exit, destLen
1249 is the actual size of the uncompressed buffer. 1249 is the actual size of the uncompressed data.
1250 1250
1251 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not 1251 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
1252 enough memory, Z_BUF_ERROR if there was not enough room in the output 1252 enough memory, Z_BUF_ERROR if there was not enough room in the output
@@ -1255,6 +1255,14 @@ ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
1255 buffer with the uncompressed data up to that point. 1255 buffer with the uncompressed data up to that point.
1256*/ 1256*/
1257 1257
1258ZEXTERN int ZEXPORT uncompress2 OF((Bytef *dest, uLongf *destLen,
1259 const Bytef *source, uLong *sourceLen));
1260/*
1261 Same as uncompress, except that sourceLen is a pointer, where the
1262 length of the source is *sourceLen. On return, *sourceLen is the number of
1263 source bytes consumed.
1264*/
1265
1258 /* gzip file access functions */ 1266 /* gzip file access functions */
1259 1267
1260/* 1268/*