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*/ 51int ZEXPORT uncompress2 (dest, destLen, source, sourceLen) 52 Bytef *dest; 53 uLongf *destLen; 54 const Bytef *source; 55 uLong *sourceLen; 56{ 57 z_stream stream; 58 int err; 59 const uInt max = (uInt)-1; 60 uLong len, left; 61 Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */ 62 63 len = *sourceLen; 64 if (*destLen) { 65 left = *destLen; 66 *destLen = 0; 67 } 68 else { 69 left = 1; 70 dest = buf; 71 } 72 73 stream.next_in = (z_const Bytef *)source; 74 stream.avail_in = 0; 75 stream.zalloc = (alloc_func)0; 76 stream.zfree = (free_func)0; 77 stream.opaque = (voidpf)0; 78 79 err = inflateInit(&stream); 80 if (err != Z_OK) return err; 81 82 stream.next_out = dest; 83 stream.avail_out = 0; 84 85 do { 86 if (stream.avail_out == 0) { 87 stream.avail_out = left > (uLong)max ? max : (uInt)left; 88 left -= stream.avail_out; 89 } 90 if (stream.avail_in == 0) { 91 stream.avail_in = len > (uLong)max ? max : (uInt)len; 92 len -= stream.avail_in; 93 } 94 err = inflate(&stream, Z_NO_FLUSH); 95 } while (err == Z_OK); 96 97 *sourceLen -= len + stream.avail_in; 98 if (dest != buf) 99 *destLen = stream.total_out; 100 else if (stream.total_out && err == Z_BUF_ERROR) 101 left = 1; 102 103 inflateEnd(&stream); 104 return err == Z_STREAM_END ? Z_OK : 105 err == Z_NEED_DICT ? Z_DATA_ERROR : 106 err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR : 107 err; 108} 109 110int ZEXPORT uncompress (dest, destLen, source, sourceLen) 111 Bytef *dest; 112 uLongf *destLen; 113 const Bytef *source; 114 uLong sourceLen; 115{ 116 return uncompress2(dest, destLen, source, &sourceLen); 117} 118