xref: /netbsd-src/common/dist/zlib/uncompr.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: uncompr.c,v 1.3 2017/01/10 01:27:41 christos Exp $	*/
2 
3 /* uncompr.c -- decompress a memory buffer
4  * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
5  * For conditions of distribution and use, see copyright notice in zlib.h
6  */
7 
8 /* @(#) $Id: uncompr.c,v 1.3 2017/01/10 01:27:41 christos Exp $ */
9 
10 #define ZLIB_INTERNAL
11 #include "zlib.h"
12 
13 /* ===========================================================================
14      Decompresses the source buffer into the destination buffer.  *sourceLen is
15    the byte length of the source buffer. Upon entry, *destLen is the total size
16    of the destination buffer, which must be large enough to hold the entire
17    uncompressed data. (The size of the uncompressed data must have been saved
18    previously by the compressor and transmitted to the decompressor by some
19    mechanism outside the scope of this compression library.) Upon exit,
20    *destLen is the size of the decompressed data and *sourceLen is the number
21    of source bytes consumed. Upon return, source + *sourceLen points to the
22    first unused input byte.
23 
24      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
25    memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
26    Z_DATA_ERROR if the input data was corrupted, including if the input data is
27    an incomplete zlib stream.
28 */
29 int ZEXPORT uncompress2 (dest, destLen, source, sourceLen)
30     Bytef *dest;
31     uLongf *destLen;
32     const Bytef *source;
33     uLong *sourceLen;
34 {
35     z_stream stream;
36     int err;
37     const uInt max = (uInt)-1;
38     uLong len, left;
39     Byte buf[1];    /* for detection of incomplete stream when *destLen == 0 */
40 
41     len = *sourceLen;
42     if (*destLen) {
43         left = *destLen;
44         *destLen = 0;
45     }
46     else {
47         left = 1;
48         dest = buf;
49     }
50 
51     stream.next_in = __UNCONST(source);
52     stream.avail_in = 0;
53     stream.zalloc = (alloc_func)0;
54     stream.zfree = (free_func)0;
55     stream.opaque = (voidpf)0;
56 
57     err = inflateInit(&stream);
58     if (err != Z_OK) return err;
59 
60     stream.next_out = dest;
61     stream.avail_out = 0;
62 
63     do {
64         if (stream.avail_out == 0) {
65             stream.avail_out = left > (uLong)max ? max : (uInt)left;
66             left -= stream.avail_out;
67         }
68         if (stream.avail_in == 0) {
69             stream.avail_in = len > (uLong)max ? max : (uInt)len;
70             len -= stream.avail_in;
71         }
72         err = inflate(&stream, Z_NO_FLUSH);
73     } while (err == Z_OK);
74 
75     *sourceLen -= len + stream.avail_in;
76     if (dest != buf)
77         *destLen = stream.total_out;
78     else if (stream.total_out && err == Z_BUF_ERROR)
79         left = 1;
80 
81     inflateEnd(&stream);
82     return err == Z_STREAM_END ? Z_OK :
83            err == Z_NEED_DICT ? Z_DATA_ERROR  :
84            err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
85            err;
86 }
87 
88 int ZEXPORT uncompress (dest, destLen, source, sourceLen)
89     Bytef *dest;
90     uLongf *destLen;
91     const Bytef *source;
92     uLong sourceLen;
93 {
94     return uncompress2(dest, destLen, source, &sourceLen);
95 }
96