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 */
compress2(Bytef * dest,uLongf * destLen,const Bytef * source,uLong sourceLen,int level)22 int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source,
23 uLong sourceLen, int level) {
24 z_stream stream;
25 int err;
26 const uInt max = (uInt)-1;
27 uLong left;
28
29 left = *destLen;
30 *destLen = 0;
31
32 stream.zalloc = (alloc_func)0;
33 stream.zfree = (free_func)0;
34 stream.opaque = (voidpf)0;
35
36 err = deflateInit(&stream, level);
37 if (err != Z_OK) return err;
38
39 stream.next_out = dest;
40 stream.avail_out = 0;
41 stream.next_in = (z_const Bytef *)source;
42 stream.avail_in = 0;
43
44 do {
45 if (stream.avail_out == 0) {
46 stream.avail_out = left > (uLong)max ? max : (uInt)left;
47 left -= stream.avail_out;
48 }
49 if (stream.avail_in == 0) {
50 stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
51 sourceLen -= stream.avail_in;
52 }
53 err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
54 } while (err == Z_OK);
55
56 *destLen = stream.total_out;
57 deflateEnd(&stream);
58 return err == Z_STREAM_END ? Z_OK : err;
59 }
60
61 /* ===========================================================================
62 */
compress(Bytef * dest,uLongf * destLen,const Bytef * source,uLong sourceLen)63 int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source,
64 uLong sourceLen) {
65 return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
66 }
67
68 /* ===========================================================================
69 If the default memLevel or windowBits for deflateInit() is changed, then
70 this function needs to be updated.
71 */
compressBound(uLong sourceLen)72 uLong ZEXPORT compressBound(uLong sourceLen) {
73 sourceLen = sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
74 (sourceLen >> 25) + 13;
75 /* FIXME(cavalcantii): usage of CRC32 Castagnoli as a hash function
76 * for the hash table of symbols used for compression has a side effect
77 * where for compression level [4, 5] it will increase the output buffer size
78 * by 0.1% (i.e. less than 1%) for a high entropy input (i.e. random data).
79 * To avoid a scenario where client code would fail, for safety we increase
80 * the expected output size by 0.8% (i.e. 8x more than the worst scenario).
81 * See: http://crbug.com/990489
82 */
83 sourceLen += sourceLen >> 7; // Equivalent to 1.0078125
84 return sourceLen;
85 }
86