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