1 /*
  2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  3  *
  4  * This code is free software; you can redistribute it and/or modify it
  5  * under the terms of the GNU General Public License version 2 only, as
  6  * published by the Free Software Foundation.  Oracle designates this
  7  * particular file as subject to the "Classpath" exception as provided
  8  * by Oracle in the LICENSE file that accompanied this code.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 /* uncompr.c -- decompress a memory buffer
 26  * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
 27  * For conditions of distribution and use, see copyright notice in zlib.h
 28  */
 29 
 30 /* @(#) $Id$ */
 31 
 32 #define ZLIB_INTERNAL
 33 #include "zlib.h"
 34 
 35 /* ===========================================================================
 36      Decompresses the source buffer into the destination buffer.  *sourceLen is
 37    the byte length of the source buffer. Upon entry, *destLen is the total size
 38    of the destination buffer, which must be large enough to hold the entire
 39    uncompressed data. (The size of the uncompressed data must have been saved
 40    previously by the compressor and transmitted to the decompressor by some
 41    mechanism outside the scope of this compression library.) Upon exit,
 42    *destLen is the size of the decompressed data and *sourceLen is the number
 43    of source bytes consumed. Upon return, source + *sourceLen points to the
 44    first unused input byte.
 45 
 46      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
 47    memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
 48    Z_DATA_ERROR if the input data was corrupted, including if the input data is
 49    an incomplete zlib stream.
 50 */
 51 int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
 52                         uLong *sourceLen) {
 53     z_stream stream;
 54     int err;
 55     const uInt max = (uInt)-1;
 56     uLong len, left;
 57     Byte buf[1];    /* for detection of incomplete stream when *destLen == 0 */
 58 
 59     len = *sourceLen;
 60     if (*destLen) {
 61         left = *destLen;
 62         *destLen = 0;
 63     }
 64     else {
 65         left = 1;
 66         dest = buf;
 67     }
 68 
 69     stream.next_in = (z_const Bytef *)source;
 70     stream.avail_in = 0;
 71     stream.zalloc = (alloc_func)0;
 72     stream.zfree = (free_func)0;
 73     stream.opaque = (voidpf)0;
 74 
 75     err = inflateInit(&stream);
 76     if (err != Z_OK) return err;
 77 
 78     stream.next_out = dest;
 79     stream.avail_out = 0;
 80 
 81     do {
 82         if (stream.avail_out == 0) {
 83             stream.avail_out = left > (uLong)max ? max : (uInt)left;
 84             left -= stream.avail_out;
 85         }
 86         if (stream.avail_in == 0) {
 87             stream.avail_in = len > (uLong)max ? max : (uInt)len;
 88             len -= stream.avail_in;
 89         }
 90         err = inflate(&stream, Z_NO_FLUSH);
 91     } while (err == Z_OK);
 92 
 93     *sourceLen -= len + stream.avail_in;
 94     if (dest != buf)
 95         *destLen = stream.total_out;
 96     else if (stream.total_out && err == Z_BUF_ERROR)
 97         left = 1;
 98 
 99     inflateEnd(&stream);
100     return err == Z_STREAM_END ? Z_OK :
101            err == Z_NEED_DICT ? Z_DATA_ERROR  :
102            err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
103            err;
104 }
105 
106 int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
107                        uLong sourceLen) {
108     return uncompress2(dest, destLen, source, &sourceLen);
109 }