/* compress.c -- compress a memory buffer1* Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler2* 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(Bytef *dest, uLongf *destLen, const Bytef *source,22uLong sourceLen, int level) {23z_stream stream;24int err;25const uInt max = (uInt)-1;26uLong left;2728left = *destLen;29*destLen = 0;3031stream.zalloc = (alloc_func)0;32stream.zfree = (free_func)0;33stream.opaque = (voidpf)0;3435err = deflateInit(&stream, level);36if (err != Z_OK) return err;3738stream.next_out = dest;39stream.avail_out = 0;40stream.next_in = (z_const Bytef *)source;41stream.avail_in = 0;4243do {44if (stream.avail_out == 0) {45stream.avail_out = left > (uLong)max ? max : (uInt)left;46left -= stream.avail_out;47}48if (stream.avail_in == 0) {49stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;50sourceLen -= stream.avail_in;51}52err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);53} while (err == Z_OK);5455*destLen = stream.total_out;56deflateEnd(&stream);57return err == Z_STREAM_END ? Z_OK : err;58}5960/* ===========================================================================61*/62int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source,63uLong sourceLen) {64return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);65}6667/* ===========================================================================68If the default memLevel or windowBits for deflateInit() is changed, then69this function needs to be updated.70*/71uLong ZEXPORT compressBound(uLong sourceLen) {72return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +73(sourceLen >> 25) + 13;74}757677