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 Jean-loup Gailly.
  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
  38    size of the destination buffer, which must be large enough to hold the
  39    entire uncompressed data. (The size of the uncompressed data must have
  40    been saved previously by the compressor and transmitted to the decompressor
  41    by some mechanism outside the scope of this compression library.)
  42    Upon exit, destLen is the actual size of the compressed buffer.
  43 
  44      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
  45    enough memory, Z_BUF_ERROR if there was not enough room in the output
  46    buffer, or Z_DATA_ERROR if the input data was corrupted.
  47 */
  48 int ZEXPORT uncompress (dest, destLen, source, sourceLen)
  49     Bytef *dest;
  50     uLongf *destLen;
  51     const Bytef *source;
  52     uLong sourceLen;
  53 {
  54     z_stream stream;
  55     int err;
  56 
  57     stream.next_in = (z_const Bytef *)source;
  58     stream.avail_in = (uInt)sourceLen;
  59     /* Check for source > 64K on 16-bit machine: */
  60     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
  61 
  62     stream.next_out = dest;
  63     stream.avail_out = (uInt)*destLen;
  64     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
  65 
  66     stream.zalloc = (alloc_func)0;
  67     stream.zfree = (free_func)0;
  68 
  69     err = inflateInit(&stream);
  70     if (err != Z_OK) return err;
  71 
  72     err = inflate(&stream, Z_FINISH);
  73     if (err != Z_STREAM_END) {
  74         inflateEnd(&stream);
  75         if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
  76             return Z_DATA_ERROR;
  77         return err;
  78     }
  79     *destLen = stream.total_out;
  80 
  81     err = inflateEnd(&stream);
  82     return err;
  83 }