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