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