]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | /* compress.c -- compress a memory buffer |
41faf807 | 2 | * Copyright (C) 1995-2003 Jean-loup Gailly. |
e6ebb514 | 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
c801d85f KB |
4 | */ |
5 | ||
c801d85f | 6 | |
51dbdf87 VS |
7 | #define ZLIB_INTERNAL |
8 | #include "zlib.h" | |
c801d85f KB |
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 (dest, destLen, source, sourceLen, level) | |
22 | Bytef *dest; | |
23 | uLongf *destLen; | |
24 | const Bytef *source; | |
25 | uLong sourceLen; | |
26 | int level; | |
27 | { | |
28 | z_stream stream; | |
29 | int err; | |
30 | ||
31 | stream.next_in = (Bytef*)source; | |
32 | stream.avail_in = (uInt)sourceLen; | |
33 | #ifdef MAXSEG_64K | |
34 | /* Check for source > 64K on 16-bit machine: */ | |
35 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; | |
36 | #endif | |
37 | stream.next_out = dest; | |
38 | stream.avail_out = (uInt)*destLen; | |
39 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; | |
40 | ||
41 | stream.zalloc = (alloc_func)0; | |
42 | stream.zfree = (free_func)0; | |
43 | stream.opaque = (voidpf)0; | |
44 | ||
45 | err = deflateInit(&stream, level); | |
46 | if (err != Z_OK) return err; | |
47 | ||
48 | err = deflate(&stream, Z_FINISH); | |
49 | if (err != Z_STREAM_END) { | |
50 | deflateEnd(&stream); | |
51 | return err == Z_OK ? Z_BUF_ERROR : err; | |
52 | } | |
53 | *destLen = stream.total_out; | |
54 | ||
55 | err = deflateEnd(&stream); | |
56 | return err; | |
57 | } | |
58 | ||
59 | /* =========================================================================== | |
60 | */ | |
61 | int ZEXPORT compress (dest, destLen, source, sourceLen) | |
62 | Bytef *dest; | |
63 | uLongf *destLen; | |
64 | const Bytef *source; | |
65 | uLong sourceLen; | |
66 | { | |
67 | return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); | |
68 | } | |
51dbdf87 VS |
69 | |
70 | /* =========================================================================== | |
71 | If the default memLevel or windowBits for deflateInit() is changed, then | |
72 | this function needs to be updated. | |
73 | */ | |
74 | uLong ZEXPORT compressBound (sourceLen) | |
75 | uLong sourceLen; | |
76 | { | |
77 | return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11; | |
78 | } |