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