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(Bytef *dest, uLongf *destLen, const Bytef *source,
47                       uLong sourceLen, int level) {
48     z_stream stream;
49     int err;
50     const uInt max = (uInt)-1;
51     uLong left;
52 
53     left = *destLen;
54     *destLen = 0;
55 
56     stream.zalloc = (alloc_func)0;
57     stream.zfree = (free_func)0;
58     stream.opaque = (voidpf)0;
59 
60     err = deflateInit(&stream, level);
61     if (err != Z_OK) return err;
62 
63     stream.next_out = dest;
64     stream.avail_out = 0;
65     stream.next_in = (z_const Bytef *)source;
66     stream.avail_in = 0;
67 
68     do {
69         if (stream.avail_out == 0) {
70             stream.avail_out = left > (uLong)max ? max : (uInt)left;
71             left -= stream.avail_out;
72         }
73         if (stream.avail_in == 0) {
74             stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
75             sourceLen -= stream.avail_in;
76         }
77         err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
78     } while (err == Z_OK);
79 
80     *destLen = stream.total_out;
81     deflateEnd(&stream);
82     return err == Z_STREAM_END ? Z_OK : err;
83 }
84 
85 /* ===========================================================================
86  */
87 int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source,
88                      uLong sourceLen) {
89     return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
90 }
91 
92 /* ===========================================================================
93      If the default memLevel or windowBits for deflateInit() is changed, then
94    this function needs to be updated.
95  */
96 uLong ZEXPORT compressBound(uLong sourceLen) {
97     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
98            (sourceLen >> 25) + 13;
99 }