16141cf33Sderaadt /* compress.c -- compress a memory buffer
236f395ceStb * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
36141cf33Sderaadt * For conditions of distribution and use, see copyright notice in zlib.h
46141cf33Sderaadt */
56141cf33Sderaadt
66141cf33Sderaadt #define ZLIB_INTERNAL
76141cf33Sderaadt #include "zlib.h"
86141cf33Sderaadt
96141cf33Sderaadt /* ===========================================================================
106141cf33Sderaadt Compresses the source buffer into the destination buffer. The level
116141cf33Sderaadt parameter has the same meaning as in deflateInit. sourceLen is the byte
126141cf33Sderaadt length of the source buffer. Upon entry, destLen is the total size of the
136141cf33Sderaadt destination buffer, which must be at least 0.1% larger than sourceLen plus
146141cf33Sderaadt 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
156141cf33Sderaadt
166141cf33Sderaadt compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
176141cf33Sderaadt memory, Z_BUF_ERROR if there was not enough room in the output buffer,
186141cf33Sderaadt Z_STREAM_ERROR if the level parameter is invalid.
196141cf33Sderaadt */
compress2(Bytef * dest,uLongf * destLen,const Bytef * source,uLong sourceLen,int level)20*cfac609cStb int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source,
21*cfac609cStb uLong sourceLen, int level) {
226141cf33Sderaadt z_stream stream;
236141cf33Sderaadt int err;
2436f395ceStb const uInt max = (uInt)-1;
2536f395ceStb uLong left;
266141cf33Sderaadt
2736f395ceStb left = *destLen;
2836f395ceStb *destLen = 0;
296141cf33Sderaadt
306141cf33Sderaadt stream.zalloc = (alloc_func)0;
316141cf33Sderaadt stream.zfree = (free_func)0;
326141cf33Sderaadt stream.opaque = (voidpf)0;
336141cf33Sderaadt
346141cf33Sderaadt err = deflateInit(&stream, level);
356141cf33Sderaadt if (err != Z_OK) return err;
366141cf33Sderaadt
3736f395ceStb stream.next_out = dest;
3836f395ceStb stream.avail_out = 0;
3936f395ceStb stream.next_in = (z_const Bytef *)source;
4036f395ceStb stream.avail_in = 0;
416141cf33Sderaadt
4236f395ceStb do {
4336f395ceStb if (stream.avail_out == 0) {
4436f395ceStb stream.avail_out = left > (uLong)max ? max : (uInt)left;
4536f395ceStb left -= stream.avail_out;
4636f395ceStb }
4736f395ceStb if (stream.avail_in == 0) {
4836f395ceStb stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
4936f395ceStb sourceLen -= stream.avail_in;
5036f395ceStb }
5136f395ceStb err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
5236f395ceStb } while (err == Z_OK);
5336f395ceStb
5436f395ceStb *destLen = stream.total_out;
5536f395ceStb deflateEnd(&stream);
5636f395ceStb return err == Z_STREAM_END ? Z_OK : err;
576141cf33Sderaadt }
586141cf33Sderaadt
596141cf33Sderaadt /* ===========================================================================
606141cf33Sderaadt */
compress(Bytef * dest,uLongf * destLen,const Bytef * source,uLong sourceLen)61*cfac609cStb int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source,
62*cfac609cStb uLong sourceLen) {
636141cf33Sderaadt return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
646141cf33Sderaadt }
656141cf33Sderaadt
666141cf33Sderaadt /* ===========================================================================
676141cf33Sderaadt If the default memLevel or windowBits for deflateInit() is changed, then
686141cf33Sderaadt this function needs to be updated.
696141cf33Sderaadt */
compressBound(uLong sourceLen)70*cfac609cStb uLong ZEXPORT compressBound(uLong sourceLen) {
7136f395ceStb return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
7236f395ceStb (sourceLen >> 25) + 13;
736141cf33Sderaadt }
74