|
Packit Service |
4a2782 |
/* compress.c -- compress a memory buffer
|
|
Packit Service |
4a2782 |
* Copyright (C) 1995-2005, 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 |
Compresses the source buffer into the destination buffer. The level
|
|
Packit Service |
4a2782 |
parameter has the same meaning as in deflateInit. sourceLen is the byte
|
|
Packit Service |
4a2782 |
length of the source buffer. Upon entry, destLen is the total size of the
|
|
Packit Service |
4a2782 |
destination buffer, which must be at least 0.1% larger than sourceLen plus
|
|
Packit Service |
4a2782 |
12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
|
|
Packit Service |
4a2782 |
|
|
Packit Service |
4a2782 |
compress2 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,
|
|
Packit Service |
4a2782 |
Z_STREAM_ERROR if the level parameter is invalid.
|
|
Packit Service |
4a2782 |
*/
|
|
Packit Service |
4a2782 |
int ZEXPORT compress2 (
|
|
Packit Service |
4a2782 |
Bytef *dest,
|
|
Packit Service |
4a2782 |
uLongf *destLen,
|
|
Packit Service |
4a2782 |
const Bytef *source,
|
|
Packit Service |
4a2782 |
uLong sourceLen,
|
|
Packit Service |
4a2782 |
int level)
|
|
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 left;
|
|
Packit Service |
4a2782 |
|
|
Packit Service |
4a2782 |
left = *destLen;
|
|
Packit Service |
4a2782 |
*destLen = 0;
|
|
Packit Service |
4a2782 |
|
|
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 = deflateInit(&stream, level);
|
|
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 |
stream.next_in = (z_const Bytef *)source;
|
|
Packit Service |
4a2782 |
stream.avail_in = 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 = sourceLen > (uLong)max ? max : (uInt)sourceLen;
|
|
Packit Service |
4a2782 |
sourceLen -= stream.avail_in;
|
|
Packit Service |
4a2782 |
}
|
|
Packit Service |
4a2782 |
err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
|
|
Packit Service |
4a2782 |
} while (err == Z_OK);
|
|
Packit Service |
4a2782 |
|
|
Packit Service |
4a2782 |
*destLen = stream.total_out;
|
|
Packit Service |
4a2782 |
deflateEnd(&stream);
|
|
Packit Service |
4a2782 |
return err == Z_STREAM_END ? Z_OK : err;
|
|
Packit Service |
4a2782 |
}
|
|
Packit Service |
4a2782 |
|
|
Packit Service |
4a2782 |
/* ===========================================================================
|
|
Packit Service |
4a2782 |
*/
|
|
Packit Service |
4a2782 |
int ZEXPORT compress (
|
|
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 compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
|
|
Packit Service |
4a2782 |
}
|
|
Packit Service |
4a2782 |
|
|
Packit Service |
4a2782 |
/* ===========================================================================
|
|
Packit Service |
4a2782 |
If the default memLevel or windowBits for deflateInit() is changed, then
|
|
Packit Service |
4a2782 |
this function needs to be updated.
|
|
Packit Service |
4a2782 |
*/
|
|
Packit Service |
4a2782 |
uLong ZEXPORT compressBound (
|
|
Packit Service |
4a2782 |
uLong sourceLen)
|
|
Packit Service |
4a2782 |
{
|
|
Packit Service |
4a2782 |
return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
|
|
Packit Service |
4a2782 |
(sourceLen >> 25) + 13;
|
|
Packit Service |
4a2782 |
}
|