|
kusano |
fc6ab3 |
/* compress.c -- compress a memory buffer
|
|
kusano |
fc6ab3 |
* Copyright (C) 1995-2005 Jean-loup Gailly.
|
|
kusano |
fc6ab3 |
* For conditions of distribution and use, see copyright notice in zlib.h
|
|
kusano |
fc6ab3 |
*/
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* @(#) $Id$ */
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
#define ZLIB_INTERNAL
|
|
kusano |
fc6ab3 |
#include "zlib.h"
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* ===========================================================================
|
|
kusano |
fc6ab3 |
Compresses the source buffer into the destination buffer. The level
|
|
kusano |
fc6ab3 |
parameter has the same meaning as in deflateInit. sourceLen is the byte
|
|
kusano |
fc6ab3 |
length of the source buffer. Upon entry, destLen is the total size of the
|
|
kusano |
fc6ab3 |
destination buffer, which must be at least 0.1% larger than sourceLen plus
|
|
kusano |
fc6ab3 |
12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
|
|
kusano |
fc6ab3 |
memory, Z_BUF_ERROR if there was not enough room in the output buffer,
|
|
kusano |
fc6ab3 |
Z_STREAM_ERROR if the level parameter is invalid.
|
|
kusano |
fc6ab3 |
*/
|
|
kusano |
fc6ab3 |
int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
|
|
kusano |
fc6ab3 |
Bytef *dest;
|
|
kusano |
fc6ab3 |
uLongf *destLen;
|
|
kusano |
fc6ab3 |
const Bytef *source;
|
|
kusano |
fc6ab3 |
uLong sourceLen;
|
|
kusano |
fc6ab3 |
int level;
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
z_stream stream;
|
|
kusano |
fc6ab3 |
int err;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
stream.next_in = (z_const Bytef *)source;
|
|
kusano |
fc6ab3 |
stream.avail_in = (uInt)sourceLen;
|
|
kusano |
fc6ab3 |
#ifdef MAXSEG_64K
|
|
kusano |
fc6ab3 |
/* Check for source > 64K on 16-bit machine: */
|
|
kusano |
fc6ab3 |
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
|
|
kusano |
fc6ab3 |
#endif
|
|
kusano |
fc6ab3 |
stream.next_out = dest;
|
|
kusano |
fc6ab3 |
stream.avail_out = (uInt)*destLen;
|
|
kusano |
fc6ab3 |
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
stream.zalloc = (alloc_func)0;
|
|
kusano |
fc6ab3 |
stream.zfree = (free_func)0;
|
|
kusano |
fc6ab3 |
stream.opaque = (voidpf)0;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
err = deflateInit(&stream, level);
|
|
kusano |
fc6ab3 |
if (err != Z_OK) return err;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
err = deflate(&stream, Z_FINISH);
|
|
kusano |
fc6ab3 |
if (err != Z_STREAM_END) {
|
|
kusano |
fc6ab3 |
deflateEnd(&stream);
|
|
kusano |
fc6ab3 |
return err == Z_OK ? Z_BUF_ERROR : err;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
*destLen = stream.total_out;
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
err = deflateEnd(&stream);
|
|
kusano |
fc6ab3 |
return err;
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* ===========================================================================
|
|
kusano |
fc6ab3 |
*/
|
|
kusano |
fc6ab3 |
int ZEXPORT compress (dest, destLen, source, sourceLen)
|
|
kusano |
fc6ab3 |
Bytef *dest;
|
|
kusano |
fc6ab3 |
uLongf *destLen;
|
|
kusano |
fc6ab3 |
const Bytef *source;
|
|
kusano |
fc6ab3 |
uLong sourceLen;
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
|
|
kusano |
fc6ab3 |
}
|
|
kusano |
fc6ab3 |
|
|
kusano |
fc6ab3 |
/* ===========================================================================
|
|
kusano |
fc6ab3 |
If the default memLevel or windowBits for deflateInit() is changed, then
|
|
kusano |
fc6ab3 |
this function needs to be updated.
|
|
kusano |
fc6ab3 |
*/
|
|
kusano |
fc6ab3 |
uLong ZEXPORT compressBound (sourceLen)
|
|
kusano |
fc6ab3 |
uLong sourceLen;
|
|
kusano |
fc6ab3 |
{
|
|
kusano |
fc6ab3 |
return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
|
|
kusano |
fc6ab3 |
(sourceLen >> 25) + 13;
|
|
kusano |
fc6ab3 |
}
|