uncompr.c 1.99 KB
Newer Older
Mark Adler's avatar
Mark Adler committed
1
/* uncompr.c -- decompress a memory buffer
Mark Adler's avatar
Mark Adler committed
2
 * Copyright (C) 1995-2003, 2010 Jean-loup Gailly.
Mark Adler's avatar
Mark Adler committed
3
 * For conditions of distribution and use, see copyright notice in zlib.h
Mark Adler's avatar
Mark Adler committed
4 5
 */

Mark Adler's avatar
Mark Adler committed
6
/* @(#) $Id$ */
Mark Adler's avatar
Mark Adler committed
7

Mark Adler's avatar
Mark Adler committed
8
#define ZLIB_INTERNAL
Mark Adler's avatar
Mark Adler committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include "zlib.h"

/* ===========================================================================
     Decompresses the source buffer into the destination buffer.  sourceLen is
   the byte length of the source buffer. Upon entry, destLen is the total
   size of the destination buffer, which must be large enough to hold the
   entire uncompressed data. (The size of the uncompressed data must have
   been saved previously by the compressor and transmitted to the decompressor
   by some mechanism outside the scope of this compression library.)
   Upon exit, destLen is the actual size of the compressed buffer.

     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
   enough memory, Z_BUF_ERROR if there was not enough room in the output
   buffer, or Z_DATA_ERROR if the input data was corrupted.
*/
24 25 26 27
int ZEXPORT uncompress(unsigned char *dest,
                       unsigned long *destLen,
                       const unsigned char *source,
                       unsigned long sourceLen)
Mark Adler's avatar
Mark Adler committed
28 29 30 31
{
    z_stream stream;
    int err;

32 33 34
    stream.next_in = (const unsigned char *)source;
    stream.avail_in = (unsigned int)sourceLen;
    if ((unsigned long)stream.avail_in != sourceLen) return Z_BUF_ERROR;
Mark Adler's avatar
Mark Adler committed
35 36

    stream.next_out = dest;
37 38
    stream.avail_out = (unsigned int)*destLen;
    if ((unsigned long)stream.avail_out != *destLen) return Z_BUF_ERROR;
Mark Adler's avatar
Mark Adler committed
39 40 41 42 43 44 45 46 47

    stream.zalloc = (alloc_func)0;
    stream.zfree = (free_func)0;

    err = inflateInit(&stream);
    if (err != Z_OK) return err;

    err = inflate(&stream, Z_FINISH);
    if (err != Z_STREAM_END) {
Mark Adler's avatar
Mark Adler committed
48
        inflateEnd(&stream);
Mark Adler's avatar
Mark Adler committed
49 50 51
        if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
            return Z_DATA_ERROR;
        return err;
Mark Adler's avatar
Mark Adler committed
52 53 54 55 56 57
    }
    *destLen = stream.total_out;

    err = inflateEnd(&stream);
    return err;
}