Blame zlib-src/uncompr.c

Packit Service 4a2782
/* uncompr.c -- decompress a memory buffer
Packit Service 4a2782
 * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
Packit Service 4a2782
 * For conditions of distribution and use, see copyright notice in zlib.h
Packit Service 4a2782
 */
Packit Service 4a2782
Packit Service 4a2782
/* @(#) $Id$ */
Packit Service 4a2782
Packit Service 4a2782
#define ZLIB_INTERNAL
Packit Service 4a2782
#include "zlib.h"
Packit Service 4a2782
Packit Service 4a2782
/* ===========================================================================
Packit Service 4a2782
     Decompresses the source buffer into the destination buffer.  *sourceLen is
Packit Service 4a2782
   the byte length of the source buffer. Upon entry, *destLen is the total size
Packit Service 4a2782
   of the destination buffer, which must be large enough to hold the entire
Packit Service 4a2782
   uncompressed data. (The size of the uncompressed data must have been saved
Packit Service 4a2782
   previously by the compressor and transmitted to the decompressor by some
Packit Service 4a2782
   mechanism outside the scope of this compression library.) Upon exit,
Packit Service 4a2782
   *destLen is the size of the decompressed data and *sourceLen is the number
Packit Service 4a2782
   of source bytes consumed. Upon return, source + *sourceLen points to the
Packit Service 4a2782
   first unused input byte.
Packit Service 4a2782
Packit Service 4a2782
     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
Packit Service 4a2782
   memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
Packit Service 4a2782
   Z_DATA_ERROR if the input data was corrupted, including if the input data is
Packit Service 4a2782
   an incomplete zlib stream.
Packit Service 4a2782
*/
Packit Service 4a2782
int ZEXPORT uncompress2 (
Packit Service 4a2782
    Bytef *dest,
Packit Service 4a2782
    uLongf *destLen,
Packit Service 4a2782
    const Bytef *source,
Packit Service 4a2782
    uLong *sourceLen)
Packit Service 4a2782
{
Packit Service 4a2782
    z_stream stream;
Packit Service 4a2782
    int err;
Packit Service 4a2782
    const uInt max = (uInt)-1;
Packit Service 4a2782
    uLong len, left;
Packit Service 4a2782
    Byte buf[1];    /* for detection of incomplete stream when *destLen == 0 */
Packit Service 4a2782
Packit Service 4a2782
    len = *sourceLen;
Packit Service 4a2782
    if (*destLen) {
Packit Service 4a2782
        left = *destLen;
Packit Service 4a2782
        *destLen = 0;
Packit Service 4a2782
    }
Packit Service 4a2782
    else {
Packit Service 4a2782
        left = 1;
Packit Service 4a2782
        dest = buf;
Packit Service 4a2782
    }
Packit Service 4a2782
Packit Service 4a2782
    stream.next_in = (z_const Bytef *)source;
Packit Service 4a2782
    stream.avail_in = 0;
Packit Service 4a2782
    stream.zalloc = (alloc_func)0;
Packit Service 4a2782
    stream.zfree = (free_func)0;
Packit Service 4a2782
    stream.opaque = (voidpf)0;
Packit Service 4a2782
Packit Service 4a2782
    err = inflateInit(&stream);
Packit Service 4a2782
    if (err != Z_OK) return err;
Packit Service 4a2782
Packit Service 4a2782
    stream.next_out = dest;
Packit Service 4a2782
    stream.avail_out = 0;
Packit Service 4a2782
Packit Service 4a2782
    do {
Packit Service 4a2782
        if (stream.avail_out == 0) {
Packit Service 4a2782
            stream.avail_out = left > (uLong)max ? max : (uInt)left;
Packit Service 4a2782
            left -= stream.avail_out;
Packit Service 4a2782
        }
Packit Service 4a2782
        if (stream.avail_in == 0) {
Packit Service 4a2782
            stream.avail_in = len > (uLong)max ? max : (uInt)len;
Packit Service 4a2782
            len -= stream.avail_in;
Packit Service 4a2782
        }
Packit Service 4a2782
        err = inflate(&stream, Z_NO_FLUSH);
Packit Service 4a2782
    } while (err == Z_OK);
Packit Service 4a2782
Packit Service 4a2782
    *sourceLen -= len + stream.avail_in;
Packit Service 4a2782
    if (dest != buf)
Packit Service 4a2782
        *destLen = stream.total_out;
Packit Service 4a2782
    else if (stream.total_out && err == Z_BUF_ERROR)
Packit Service 4a2782
        left = 1;
Packit Service 4a2782
Packit Service 4a2782
    inflateEnd(&stream);
Packit Service 4a2782
    return err == Z_STREAM_END ? Z_OK :
Packit Service 4a2782
           err == Z_NEED_DICT ? Z_DATA_ERROR  :
Packit Service 4a2782
           err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
Packit Service 4a2782
           err;
Packit Service 4a2782
}
Packit Service 4a2782
Packit Service 4a2782
int ZEXPORT uncompress (
Packit Service 4a2782
    Bytef *dest,
Packit Service 4a2782
    uLongf *destLen,
Packit Service 4a2782
    const Bytef *source,
Packit Service 4a2782
    uLong sourceLen)
Packit Service 4a2782
{
Packit Service 4a2782
    return uncompress2(dest, destLen, source, &sourceLen);
Packit Service 4a2782
}