1 /*
  2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  3  *
  4  * This code is free software; you can redistribute it and/or modify it
  5  * under the terms of the GNU General Public License version 2 only, as
  6  * published by the Free Software Foundation.  Oracle designates this
  7  * particular file as subject to the "Classpath" exception as provided
  8  * by Oracle in the LICENSE file that accompanied this code.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 /* compress.c -- compress a memory buffer
 26  * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
 27  * For conditions of distribution and use, see copyright notice in zlib.h
 28  */
 29 
 30 /* @(#) $Id$ */
 31 
 32 #define ZLIB_INTERNAL
 33 #include "zlib.h"
 34 
 35 /* ===========================================================================
 36      Compresses the source buffer into the destination buffer. The level
 37    parameter has the same meaning as in deflateInit.  sourceLen is the byte
 38    length of the source buffer. Upon entry, destLen is the total size of the
 39    destination buffer, which must be at least 0.1% larger than sourceLen plus
 40    12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
 41 
 42      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
 43    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
 44    Z_STREAM_ERROR if the level parameter is invalid.
 45 */
 46 int ZEXPORT compress2(dest, destLen, source, sourceLen, level)
 47     Bytef *dest;
 48     uLongf *destLen;
 49     const Bytef *source;
 50     uLong sourceLen;
 51     int level;
 52 {
 53     z_stream stream;
 54     int err;
 55     const uInt max = (uInt)-1;
 56     uLong left;
 57 
 58     left = *destLen;
 59     *destLen = 0;
 60 
 61     stream.zalloc = (alloc_func)0;
 62     stream.zfree = (free_func)0;
 63     stream.opaque = (voidpf)0;
 64 
 65     err = deflateInit(&stream, level);
 66     if (err != Z_OK) return err;
 67 
 68     stream.next_out = dest;
 69     stream.avail_out = 0;
 70     stream.next_in = (z_const Bytef *)source;
 71     stream.avail_in = 0;
 72 
 73     do {
 74         if (stream.avail_out == 0) {
 75             stream.avail_out = left > (uLong)max ? max : (uInt)left;
 76             left -= stream.avail_out;
 77         }
 78         if (stream.avail_in == 0) {
 79             stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
 80             sourceLen -= stream.avail_in;
 81         }
 82         err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
 83     } while (err == Z_OK);
 84 
 85     *destLen = stream.total_out;
 86     deflateEnd(&stream);
 87     return err == Z_STREAM_END ? Z_OK : err;
 88 }
 89 
 90 /* ===========================================================================
 91  */
 92 int ZEXPORT compress(dest, destLen, source, sourceLen)
 93     Bytef *dest;
 94     uLongf *destLen;
 95     const Bytef *source;
 96     uLong sourceLen;
 97 {
 98     return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
 99 }
100 
101 /* ===========================================================================
102      If the default memLevel or windowBits for deflateInit() is changed, then
103    this function needs to be updated.
104  */
105 uLong ZEXPORT compressBound(sourceLen)
106     uLong sourceLen;
107 {
108     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
109            (sourceLen >> 25) + 13;
110 }