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 Jean-loup Gailly. |
|
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 |
|
56 stream.next_in = (z_const Bytef *)source; |
|
57 stream.avail_in = (uInt)sourceLen; |
|
58 #ifdef MAXSEG_64K |
|
59 /* Check for source > 64K on 16-bit machine: */ |
|
60 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; |
|
61 #endif |
|
62 stream.next_out = dest; |
|
63 stream.avail_out = (uInt)*destLen; |
|
64 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; |
|
65 |
|
66 stream.zalloc = (alloc_func)0; |
|
67 stream.zfree = (free_func)0; |
|
68 stream.opaque = (voidpf)0; |
|
69 |
|
70 err = deflateInit(&stream, level); |
|
71 if (err != Z_OK) return err; |
|
72 |
|
73 err = deflate(&stream, Z_FINISH); |
|
74 if (err != Z_STREAM_END) { |
|
75 deflateEnd(&stream); |
|
76 return err == Z_OK ? Z_BUF_ERROR : err; |
|
77 } |
|
78 *destLen = stream.total_out; |
|
79 |
|
80 err = deflateEnd(&stream); |
|
81 return err; |
|
82 } |
|
83 |
|
84 /* =========================================================================== |
|
85 */ |
|
86 int ZEXPORT compress (dest, destLen, source, sourceLen) |
|
87 Bytef *dest; |
|
88 uLongf *destLen; |
|
89 const Bytef *source; |
|
90 uLong sourceLen; |
|
91 { |
|
92 return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); |
|
93 } |
|
94 |
|
95 /* =========================================================================== |
|
96 If the default memLevel or windowBits for deflateInit() is changed, then |
|
97 this function needs to be updated. |
|
98 */ |
|
99 uLong ZEXPORT compressBound (sourceLen) |
|
100 uLong sourceLen; |
|
101 { |
|
102 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + |
|
103 (sourceLen >> 25) + 13; |
|
104 } |
|