1275793eaSopenharmony_ci/* uncompr.c -- decompress a memory buffer 2275793eaSopenharmony_ci * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler 3275793eaSopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h 4275793eaSopenharmony_ci */ 5275793eaSopenharmony_ci 6275793eaSopenharmony_ci/* @(#) $Id$ */ 7275793eaSopenharmony_ci 8275793eaSopenharmony_ci#define ZLIB_INTERNAL 9275793eaSopenharmony_ci#include "zlib.h" 10275793eaSopenharmony_ci 11275793eaSopenharmony_ci/* =========================================================================== 12275793eaSopenharmony_ci Decompresses the source buffer into the destination buffer. *sourceLen is 13275793eaSopenharmony_ci the byte length of the source buffer. Upon entry, *destLen is the total size 14275793eaSopenharmony_ci of the destination buffer, which must be large enough to hold the entire 15275793eaSopenharmony_ci uncompressed data. (The size of the uncompressed data must have been saved 16275793eaSopenharmony_ci previously by the compressor and transmitted to the decompressor by some 17275793eaSopenharmony_ci mechanism outside the scope of this compression library.) Upon exit, 18275793eaSopenharmony_ci *destLen is the size of the decompressed data and *sourceLen is the number 19275793eaSopenharmony_ci of source bytes consumed. Upon return, source + *sourceLen points to the 20275793eaSopenharmony_ci first unused input byte. 21275793eaSopenharmony_ci 22275793eaSopenharmony_ci uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough 23275793eaSopenharmony_ci memory, Z_BUF_ERROR if there was not enough room in the output buffer, or 24275793eaSopenharmony_ci Z_DATA_ERROR if the input data was corrupted, including if the input data is 25275793eaSopenharmony_ci an incomplete zlib stream. 26275793eaSopenharmony_ci*/ 27275793eaSopenharmony_ciint ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source, 28275793eaSopenharmony_ci uLong *sourceLen) { 29275793eaSopenharmony_ci z_stream stream; 30275793eaSopenharmony_ci int err; 31275793eaSopenharmony_ci const uInt max = (uInt)-1; 32275793eaSopenharmony_ci uLong len, left; 33275793eaSopenharmony_ci Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */ 34275793eaSopenharmony_ci 35275793eaSopenharmony_ci len = *sourceLen; 36275793eaSopenharmony_ci if (*destLen) { 37275793eaSopenharmony_ci left = *destLen; 38275793eaSopenharmony_ci *destLen = 0; 39275793eaSopenharmony_ci } 40275793eaSopenharmony_ci else { 41275793eaSopenharmony_ci left = 1; 42275793eaSopenharmony_ci dest = buf; 43275793eaSopenharmony_ci } 44275793eaSopenharmony_ci 45275793eaSopenharmony_ci stream.next_in = (z_const Bytef *)source; 46275793eaSopenharmony_ci stream.avail_in = 0; 47275793eaSopenharmony_ci stream.zalloc = (alloc_func)0; 48275793eaSopenharmony_ci stream.zfree = (free_func)0; 49275793eaSopenharmony_ci stream.opaque = (voidpf)0; 50275793eaSopenharmony_ci 51275793eaSopenharmony_ci err = inflateInit(&stream); 52275793eaSopenharmony_ci if (err != Z_OK) return err; 53275793eaSopenharmony_ci 54275793eaSopenharmony_ci stream.next_out = dest; 55275793eaSopenharmony_ci stream.avail_out = 0; 56275793eaSopenharmony_ci 57275793eaSopenharmony_ci do { 58275793eaSopenharmony_ci if (stream.avail_out == 0) { 59275793eaSopenharmony_ci stream.avail_out = left > (uLong)max ? max : (uInt)left; 60275793eaSopenharmony_ci left -= stream.avail_out; 61275793eaSopenharmony_ci } 62275793eaSopenharmony_ci if (stream.avail_in == 0) { 63275793eaSopenharmony_ci stream.avail_in = len > (uLong)max ? max : (uInt)len; 64275793eaSopenharmony_ci len -= stream.avail_in; 65275793eaSopenharmony_ci } 66275793eaSopenharmony_ci err = inflate(&stream, Z_NO_FLUSH); 67275793eaSopenharmony_ci } while (err == Z_OK); 68275793eaSopenharmony_ci 69275793eaSopenharmony_ci *sourceLen -= len + stream.avail_in; 70275793eaSopenharmony_ci if (dest != buf) 71275793eaSopenharmony_ci *destLen = stream.total_out; 72275793eaSopenharmony_ci else if (stream.total_out && err == Z_BUF_ERROR) 73275793eaSopenharmony_ci left = 1; 74275793eaSopenharmony_ci 75275793eaSopenharmony_ci inflateEnd(&stream); 76275793eaSopenharmony_ci return err == Z_STREAM_END ? Z_OK : 77275793eaSopenharmony_ci err == Z_NEED_DICT ? Z_DATA_ERROR : 78275793eaSopenharmony_ci err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR : 79275793eaSopenharmony_ci err; 80275793eaSopenharmony_ci} 81275793eaSopenharmony_ci 82275793eaSopenharmony_ciint ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, 83275793eaSopenharmony_ci uLong sourceLen) { 84275793eaSopenharmony_ci return uncompress2(dest, destLen, source, &sourceLen); 85275793eaSopenharmony_ci} 86