12af503bcStholo /* compress.c -- compress a memory buffer
236f395ceStb * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
32af503bcStholo * For conditions of distribution and use, see copyright notice in zlib.h
42af503bcStholo */
52af503bcStholo
685c48e79Shenning #define ZLIB_INTERNAL
72af503bcStholo #include "zlib.h"
82af503bcStholo
92af503bcStholo /* ===========================================================================
1015ce0796Smillert Compresses the source buffer into the destination buffer. The level
1115ce0796Smillert parameter has the same meaning as in deflateInit. sourceLen is the byte
1215ce0796Smillert length of the source buffer. Upon entry, destLen is the total size of the
1315ce0796Smillert destination buffer, which must be at least 0.1% larger than sourceLen plus
1415ce0796Smillert 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
1515ce0796Smillert
1615ce0796Smillert compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
1715ce0796Smillert memory, Z_BUF_ERROR if there was not enough room in the output buffer,
1815ce0796Smillert Z_STREAM_ERROR if the level parameter is invalid.
192af503bcStholo */
compress2(Bytef * dest,uLongf * destLen,const Bytef * source,uLong sourceLen,int level)20*a04ea15dStb int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source,
21*a04ea15dStb uLong sourceLen, int level) {
222af503bcStholo z_stream stream;
232af503bcStholo int err;
2436f395ceStb const uInt max = (uInt)-1;
2536f395ceStb uLong left;
262af503bcStholo
2736f395ceStb left = *destLen;
2836f395ceStb *destLen = 0;
292af503bcStholo
302af503bcStholo stream.zalloc = (alloc_func)0;
312af503bcStholo stream.zfree = (free_func)0;
322af503bcStholo stream.opaque = (voidpf)0;
332af503bcStholo
3415ce0796Smillert err = deflateInit(&stream, level);
352af503bcStholo if (err != Z_OK) return err;
362af503bcStholo
3736f395ceStb stream.next_out = dest;
3836f395ceStb stream.avail_out = 0;
3936f395ceStb stream.next_in = (z_const Bytef *)source;
4036f395ceStb stream.avail_in = 0;
412af503bcStholo
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;
572af503bcStholo }
5815ce0796Smillert
5915ce0796Smillert /* ===========================================================================
6015ce0796Smillert */
compress(Bytef * dest,uLongf * destLen,const Bytef * source,uLong sourceLen)61*a04ea15dStb int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source,
62*a04ea15dStb uLong sourceLen) {
6315ce0796Smillert return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
6415ce0796Smillert }
6585c48e79Shenning
6685c48e79Shenning /* ===========================================================================
6785c48e79Shenning If the default memLevel or windowBits for deflateInit() is changed, then
6885c48e79Shenning this function needs to be updated.
6985c48e79Shenning */
compressBound(uLong sourceLen)70*a04ea15dStb uLong ZEXPORT compressBound(uLong sourceLen) {
7136f395ceStb return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
7236f395ceStb (sourceLen >> 25) + 13;
7385c48e79Shenning }
74