Path: blob/master/src/java.base/share/native/libzip/zlib/uncompr.c
41153 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/* uncompr.c -- decompress a memory buffer25* Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler26* For conditions of distribution and use, see copyright notice in zlib.h27*/2829/* @(#) $Id$ */3031#define ZLIB_INTERNAL32#include "zlib.h"3334/* ===========================================================================35Decompresses the source buffer into the destination buffer. *sourceLen is36the byte length of the source buffer. Upon entry, *destLen is the total size37of the destination buffer, which must be large enough to hold the entire38uncompressed data. (The size of the uncompressed data must have been saved39previously by the compressor and transmitted to the decompressor by some40mechanism outside the scope of this compression library.) Upon exit,41*destLen is the size of the decompressed data and *sourceLen is the number42of source bytes consumed. Upon return, source + *sourceLen points to the43first unused input byte.4445uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough46memory, Z_BUF_ERROR if there was not enough room in the output buffer, or47Z_DATA_ERROR if the input data was corrupted, including if the input data is48an incomplete zlib stream.49*/50int ZEXPORT uncompress2 (dest, destLen, source, sourceLen)51Bytef *dest;52uLongf *destLen;53const Bytef *source;54uLong *sourceLen;55{56z_stream stream;57int err;58const uInt max = (uInt)-1;59uLong len, left;60Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */6162len = *sourceLen;63if (*destLen) {64left = *destLen;65*destLen = 0;66}67else {68left = 1;69dest = buf;70}7172stream.next_in = (z_const Bytef *)source;73stream.avail_in = 0;74stream.zalloc = (alloc_func)0;75stream.zfree = (free_func)0;76stream.opaque = (voidpf)0;7778err = inflateInit(&stream);79if (err != Z_OK) return err;8081stream.next_out = dest;82stream.avail_out = 0;8384do {85if (stream.avail_out == 0) {86stream.avail_out = left > (uLong)max ? max : (uInt)left;87left -= stream.avail_out;88}89if (stream.avail_in == 0) {90stream.avail_in = len > (uLong)max ? max : (uInt)len;91len -= stream.avail_in;92}93err = inflate(&stream, Z_NO_FLUSH);94} while (err == Z_OK);9596*sourceLen -= len + stream.avail_in;97if (dest != buf)98*destLen = stream.total_out;99else if (stream.total_out && err == Z_BUF_ERROR)100left = 1;101102inflateEnd(&stream);103return err == Z_STREAM_END ? Z_OK :104err == Z_NEED_DICT ? Z_DATA_ERROR :105err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :106err;107}108109int ZEXPORT uncompress (dest, destLen, source, sourceLen)110Bytef *dest;111uLongf *destLen;112const Bytef *source;113uLong sourceLen;114{115return uncompress2(dest, destLen, source, &sourceLen);116}117118119