/* compress.c -- compress a memory buffer1* Copyright (C) 1995-2003 Jean-loup Gailly.2* For conditions of distribution and use, see copyright notice in zlib.h3*/45/* @(#) $Id$ */67#define ZLIB_INTERNAL8#include "zlib.h"910/* ===========================================================================11Compresses the source buffer into the destination buffer. The level12parameter has the same meaning as in deflateInit. sourceLen is the byte13length of the source buffer. Upon entry, destLen is the total size of the14destination buffer, which must be at least 0.1% larger than sourceLen plus1512 bytes. Upon exit, destLen is the actual size of the compressed buffer.1617compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough18memory, Z_BUF_ERROR if there was not enough room in the output buffer,19Z_STREAM_ERROR if the level parameter is invalid.20*/21int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)22Bytef *dest;23uLongf *destLen;24const Bytef *source;25uLong sourceLen;26int level;27{28z_stream stream;29int err;3031stream.next_in = (Bytef*)source;32stream.avail_in = (uInt)sourceLen;33#ifdef MAXSEG_64K34/* Check for source > 64K on 16-bit machine: */35if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;36#endif37stream.next_out = dest;38stream.avail_out = (uInt)*destLen;39if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;4041stream.zalloc = (alloc_func)0;42stream.zfree = (free_func)0;43stream.opaque = (voidpf)0;4445err = deflateInit(&stream, level);46if (err != Z_OK) return err;4748err = deflate(&stream, Z_FINISH);49if (err != Z_STREAM_END) {50deflateEnd(&stream);51return err == Z_OK ? Z_BUF_ERROR : err;52}53*destLen = stream.total_out;5455err = deflateEnd(&stream);56return err;57}5859/* ===========================================================================60*/61int ZEXPORT compress (dest, destLen, source, sourceLen)62Bytef *dest;63uLongf *destLen;64const Bytef *source;65uLong sourceLen;66{67return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);68}6970/* ===========================================================================71If the default memLevel or windowBits for deflateInit() is changed, then72this function needs to be updated.73*/74uLong ZEXPORT compressBound (sourceLen)75uLong sourceLen;76{77return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;78}798081