|
1 /* compress.c -- compress a memory buffer |
|
2 * Copyright (C) 1995-1998 Jean-loup Gailly. |
|
3 * For conditions of distribution and use, see copyright notice in zlib.h |
|
4 */ |
|
5 |
|
6 /* @(#) $Id$ */ |
|
7 |
|
8 #include "zlib.h" |
|
9 |
|
10 /* =========================================================================== |
|
11 Compresses the source buffer into the destination buffer. The level |
|
12 parameter has the same meaning as in deflateInit. sourceLen is the byte |
|
13 length of the source buffer. Upon entry, destLen is the total size of the |
|
14 destination buffer, which must be at least 0.1% larger than sourceLen plus |
|
15 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. |
|
16 |
|
17 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough |
|
18 memory, Z_BUF_ERROR if there was not enough room in the output buffer, |
|
19 Z_STREAM_ERROR if the level parameter is invalid. |
|
20 */ |
|
21 int ZEXPORT compress2 (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level) |
|
22 { |
|
23 z_stream stream; |
|
24 int err; |
|
25 |
|
26 stream.next_in = (Bytef*)source; |
|
27 stream.avail_in = (uInt)sourceLen; |
|
28 #ifdef MAXSEG_64K |
|
29 /* Check for source > 64K on 16-bit machine: */ |
|
30 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; |
|
31 #endif |
|
32 stream.next_out = dest; |
|
33 stream.avail_out = (uInt)*destLen; |
|
34 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; |
|
35 |
|
36 stream.zalloc = (alloc_func)0; |
|
37 stream.zfree = (free_func)0; |
|
38 stream.opaque = (voidpf)0; |
|
39 |
|
40 err = deflateInit(&stream, level); |
|
41 if (err != Z_OK) return err; |
|
42 |
|
43 err = deflate(&stream, Z_FINISH); |
|
44 if (err != Z_STREAM_END) { |
|
45 deflateEnd(&stream); |
|
46 return err == Z_OK ? Z_BUF_ERROR : err; |
|
47 } |
|
48 *destLen = stream.total_out; |
|
49 |
|
50 err = deflateEnd(&stream); |
|
51 return err; |
|
52 } |
|
53 |
|
54 /* =========================================================================== |
|
55 */ |
|
56 int ZEXPORT compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen) |
|
57 { |
|
58 return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); |
|
59 } |