annotate zlib/uncompr.c @ 144:8f4e72ab4e11

fix segmentation fault caused by nothing next cur_op to end
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 21:23:56 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
51
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 /* uncompr.c -- decompress a memory buffer
111
kono
parents: 51
diff changeset
2 * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
51
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 * For conditions of distribution and use, see copyright notice in zlib.h
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 */
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 /* @(#) $Id: uncompr.c,v 1.1.1.2 2002/03/11 21:53:27 tromey Exp $ */
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 #define ZLIB_INTERNAL
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include "zlib.h"
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 /* ===========================================================================
111
kono
parents: 51
diff changeset
12 Decompresses the source buffer into the destination buffer. *sourceLen is
kono
parents: 51
diff changeset
13 the byte length of the source buffer. Upon entry, *destLen is the total size
kono
parents: 51
diff changeset
14 of the destination buffer, which must be large enough to hold the entire
kono
parents: 51
diff changeset
15 uncompressed data. (The size of the uncompressed data must have been saved
kono
parents: 51
diff changeset
16 previously by the compressor and transmitted to the decompressor by some
kono
parents: 51
diff changeset
17 mechanism outside the scope of this compression library.) Upon exit,
kono
parents: 51
diff changeset
18 *destLen is the size of the decompressed data and *sourceLen is the number
kono
parents: 51
diff changeset
19 of source bytes consumed. Upon return, source + *sourceLen points to the
kono
parents: 51
diff changeset
20 first unused input byte.
kono
parents: 51
diff changeset
21
kono
parents: 51
diff changeset
22 uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
kono
parents: 51
diff changeset
23 memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
kono
parents: 51
diff changeset
24 Z_DATA_ERROR if the input data was corrupted, including if the input data is
kono
parents: 51
diff changeset
25 an incomplete zlib stream.
kono
parents: 51
diff changeset
26 */
kono
parents: 51
diff changeset
27 int ZEXPORT uncompress2 (dest, destLen, source, sourceLen)
kono
parents: 51
diff changeset
28 Bytef *dest;
kono
parents: 51
diff changeset
29 uLongf *destLen;
kono
parents: 51
diff changeset
30 const Bytef *source;
kono
parents: 51
diff changeset
31 uLong *sourceLen;
kono
parents: 51
diff changeset
32 {
kono
parents: 51
diff changeset
33 z_stream stream;
kono
parents: 51
diff changeset
34 int err;
kono
parents: 51
diff changeset
35 const uInt max = (uInt)-1;
kono
parents: 51
diff changeset
36 uLong len, left;
kono
parents: 51
diff changeset
37 Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */
kono
parents: 51
diff changeset
38
kono
parents: 51
diff changeset
39 len = *sourceLen;
kono
parents: 51
diff changeset
40 if (*destLen) {
kono
parents: 51
diff changeset
41 left = *destLen;
kono
parents: 51
diff changeset
42 *destLen = 0;
kono
parents: 51
diff changeset
43 }
kono
parents: 51
diff changeset
44 else {
kono
parents: 51
diff changeset
45 left = 1;
kono
parents: 51
diff changeset
46 dest = buf;
kono
parents: 51
diff changeset
47 }
51
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
111
kono
parents: 51
diff changeset
49 stream.next_in = (z_const Bytef *)source;
kono
parents: 51
diff changeset
50 stream.avail_in = 0;
kono
parents: 51
diff changeset
51 stream.zalloc = (alloc_func)0;
kono
parents: 51
diff changeset
52 stream.zfree = (free_func)0;
kono
parents: 51
diff changeset
53 stream.opaque = (voidpf)0;
kono
parents: 51
diff changeset
54
kono
parents: 51
diff changeset
55 err = inflateInit(&stream);
kono
parents: 51
diff changeset
56 if (err != Z_OK) return err;
kono
parents: 51
diff changeset
57
kono
parents: 51
diff changeset
58 stream.next_out = dest;
kono
parents: 51
diff changeset
59 stream.avail_out = 0;
kono
parents: 51
diff changeset
60
kono
parents: 51
diff changeset
61 do {
kono
parents: 51
diff changeset
62 if (stream.avail_out == 0) {
kono
parents: 51
diff changeset
63 stream.avail_out = left > (uLong)max ? max : (uInt)left;
kono
parents: 51
diff changeset
64 left -= stream.avail_out;
kono
parents: 51
diff changeset
65 }
kono
parents: 51
diff changeset
66 if (stream.avail_in == 0) {
kono
parents: 51
diff changeset
67 stream.avail_in = len > (uLong)max ? max : (uInt)len;
kono
parents: 51
diff changeset
68 len -= stream.avail_in;
kono
parents: 51
diff changeset
69 }
kono
parents: 51
diff changeset
70 err = inflate(&stream, Z_NO_FLUSH);
kono
parents: 51
diff changeset
71 } while (err == Z_OK);
kono
parents: 51
diff changeset
72
kono
parents: 51
diff changeset
73 *sourceLen -= len + stream.avail_in;
kono
parents: 51
diff changeset
74 if (dest != buf)
kono
parents: 51
diff changeset
75 *destLen = stream.total_out;
kono
parents: 51
diff changeset
76 else if (stream.total_out && err == Z_BUF_ERROR)
kono
parents: 51
diff changeset
77 left = 1;
kono
parents: 51
diff changeset
78
kono
parents: 51
diff changeset
79 inflateEnd(&stream);
kono
parents: 51
diff changeset
80 return err == Z_STREAM_END ? Z_OK :
kono
parents: 51
diff changeset
81 err == Z_NEED_DICT ? Z_DATA_ERROR :
kono
parents: 51
diff changeset
82 err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
kono
parents: 51
diff changeset
83 err;
kono
parents: 51
diff changeset
84 }
kono
parents: 51
diff changeset
85
51
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 int ZEXPORT uncompress (dest, destLen, source, sourceLen)
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 Bytef *dest;
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 uLongf *destLen;
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 const Bytef *source;
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 uLong sourceLen;
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 {
111
kono
parents: 51
diff changeset
92 return uncompress2(dest, destLen, source, &sourceLen);
51
ae3a4bfb450b add some files of version 4.4.3 that have been forgotten.
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 }