Home | History | Annotate | Line # | Download | only in zlib
uncompr.c revision 1.1
      1  1.1  christos /*	$NetBSD: uncompr.c,v 1.1 2006/01/14 20:10:31 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /* uncompr.c -- decompress a memory buffer
      4  1.1  christos  * Copyright (C) 1995-2003 Jean-loup Gailly.
      5  1.1  christos  * For conditions of distribution and use, see copyright notice in zlib.h
      6  1.1  christos  */
      7  1.1  christos 
      8  1.1  christos /* @(#) Id */
      9  1.1  christos 
     10  1.1  christos #define ZLIB_INTERNAL
     11  1.1  christos #include "zlib.h"
     12  1.1  christos 
     13  1.1  christos /* ===========================================================================
     14  1.1  christos      Decompresses the source buffer into the destination buffer.  sourceLen is
     15  1.1  christos    the byte length of the source buffer. Upon entry, destLen is the total
     16  1.1  christos    size of the destination buffer, which must be large enough to hold the
     17  1.1  christos    entire uncompressed data. (The size of the uncompressed data must have
     18  1.1  christos    been saved previously by the compressor and transmitted to the decompressor
     19  1.1  christos    by some mechanism outside the scope of this compression library.)
     20  1.1  christos    Upon exit, destLen is the actual size of the compressed buffer.
     21  1.1  christos      This function can be used to decompress a whole file at once if the
     22  1.1  christos    input file is mmap'ed.
     23  1.1  christos 
     24  1.1  christos      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
     25  1.1  christos    enough memory, Z_BUF_ERROR if there was not enough room in the output
     26  1.1  christos    buffer, or Z_DATA_ERROR if the input data was corrupted.
     27  1.1  christos */
     28  1.1  christos int ZEXPORT uncompress (dest, destLen, source, sourceLen)
     29  1.1  christos     Bytef *dest;
     30  1.1  christos     uLongf *destLen;
     31  1.1  christos     const Bytef *source;
     32  1.1  christos     uLong sourceLen;
     33  1.1  christos {
     34  1.1  christos     z_stream stream;
     35  1.1  christos     int err;
     36  1.1  christos 
     37  1.1  christos     stream.next_in = (Bytef*)source;
     38  1.1  christos     stream.avail_in = (uInt)sourceLen;
     39  1.1  christos     /* Check for source > 64K on 16-bit machine: */
     40  1.1  christos     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
     41  1.1  christos 
     42  1.1  christos     stream.next_out = dest;
     43  1.1  christos     stream.avail_out = (uInt)*destLen;
     44  1.1  christos     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
     45  1.1  christos 
     46  1.1  christos     stream.zalloc = (alloc_func)0;
     47  1.1  christos     stream.zfree = (free_func)0;
     48  1.1  christos 
     49  1.1  christos     err = inflateInit(&stream);
     50  1.1  christos     if (err != Z_OK) return err;
     51  1.1  christos 
     52  1.1  christos     err = inflate(&stream, Z_FINISH);
     53  1.1  christos     if (err != Z_STREAM_END) {
     54  1.1  christos         inflateEnd(&stream);
     55  1.1  christos         if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
     56  1.1  christos             return Z_DATA_ERROR;
     57  1.1  christos         return err;
     58  1.1  christos     }
     59  1.1  christos     *destLen = stream.total_out;
     60  1.1  christos 
     61  1.1  christos     err = inflateEnd(&stream);
     62  1.1  christos     return err;
     63  1.1  christos }
     64