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