/* 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 (dest, destLen, source, sourceLen, level)22Bytef *dest;23uLongf *destLen;24const Bytef *source;25uLong sourceLen;26int level;27{28z_stream stream;29int err;30const uInt max = (uInt)-1;31uLong left;3233left = *destLen;34*destLen = 0;3536stream.zalloc = (alloc_func)0;37stream.zfree = (free_func)0;38stream.opaque = (voidpf)0;3940err = deflateInit(&stream, level);41if (err != Z_OK) return err;4243stream.next_out = dest;44stream.avail_out = 0;45stream.next_in = (z_const Bytef *)source;46stream.avail_in = 0;4748do {49if (stream.avail_out == 0) {50stream.avail_out = left > (uLong)max ? max : (uInt)left;51left -= stream.avail_out;52}53if (stream.avail_in == 0) {54stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;55sourceLen -= stream.avail_in;56}57err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);58} while (err == Z_OK);5960*destLen = stream.total_out;61deflateEnd(&stream);62return err == Z_STREAM_END ? Z_OK : err;63}6465/* ===========================================================================66*/67int ZEXPORT compress (dest, destLen, source, sourceLen)68Bytef *dest;69uLongf *destLen;70const Bytef *source;71uLong sourceLen;72{73return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);74}7576/* ===========================================================================77If the default memLevel or windowBits for deflateInit() is changed, then78this function needs to be updated.79*/80uLong ZEXPORT compressBound (sourceLen)81uLong sourceLen;82{83return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +84(sourceLen >> 25) + 13;85}868788