Line | Count | Source (jump to first uncovered line) |
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 | | #include "zbuild.h" |
7 | | #include "zutil.h" |
8 | | |
9 | | /* =========================================================================== |
10 | | * Architecture-specific hooks. |
11 | | */ |
12 | | #ifdef S390_DFLTCC_DEFLATE |
13 | | # include "arch/s390/dfltcc_common.h" |
14 | | #else |
15 | | /* Returns the upper bound on compressed data length based on uncompressed data length, assuming default settings. |
16 | | * Zero means that arch-specific deflation code behaves identically to the regular zlib-ng algorithms. */ |
17 | 4.73k | # define DEFLATE_BOUND_COMPLEN(source_len) 0 |
18 | | #endif |
19 | | |
20 | | /* =========================================================================== |
21 | | Compresses the source buffer into the destination buffer. The level |
22 | | parameter has the same meaning as in deflateInit. sourceLen is the byte |
23 | | length of the source buffer. Upon entry, destLen is the total size of the |
24 | | destination buffer, which must be at least 0.1% larger than sourceLen plus |
25 | | 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. |
26 | | |
27 | | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough |
28 | | memory, Z_BUF_ERROR if there was not enough room in the output buffer, |
29 | | Z_STREAM_ERROR if the level parameter is invalid. |
30 | | */ |
31 | | int Z_EXPORT PREFIX(compress2)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, |
32 | 18.9k | z_uintmax_t sourceLen, int level) { |
33 | 18.9k | PREFIX3(stream) stream; |
34 | 18.9k | int err; |
35 | 18.9k | const unsigned int max = (unsigned int)-1; |
36 | 18.9k | z_size_t left; |
37 | | |
38 | 18.9k | left = *destLen; |
39 | 18.9k | *destLen = 0; |
40 | | |
41 | 18.9k | stream.zalloc = NULL; |
42 | 18.9k | stream.zfree = NULL; |
43 | 18.9k | stream.opaque = NULL; |
44 | | |
45 | 18.9k | err = PREFIX(deflateInit)(&stream, level); |
46 | 18.9k | if (err != Z_OK) |
47 | 0 | return err; |
48 | | |
49 | 18.9k | stream.next_out = dest; |
50 | 18.9k | stream.avail_out = 0; |
51 | 18.9k | stream.next_in = (z_const unsigned char *)source; |
52 | 18.9k | stream.avail_in = 0; |
53 | | |
54 | 18.9k | do { |
55 | 18.9k | if (stream.avail_out == 0) { |
56 | 18.9k | stream.avail_out = left > (unsigned long)max ? max : (unsigned int)left; |
57 | 18.9k | left -= stream.avail_out; |
58 | 18.9k | } |
59 | 18.9k | if (stream.avail_in == 0) { |
60 | 18.9k | stream.avail_in = sourceLen > (unsigned long)max ? max : (unsigned int)sourceLen; |
61 | 18.9k | sourceLen -= stream.avail_in; |
62 | 18.9k | } |
63 | 18.9k | err = PREFIX(deflate)(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH); |
64 | 18.9k | } while (err == Z_OK); |
65 | | |
66 | 18.9k | *destLen = stream.total_out; |
67 | 18.9k | PREFIX(deflateEnd)(&stream); |
68 | 18.9k | return err == Z_STREAM_END ? Z_OK : err; |
69 | 18.9k | } |
70 | | |
71 | | /* =========================================================================== |
72 | | */ |
73 | 0 | int Z_EXPORT PREFIX(compress)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t sourceLen) { |
74 | 0 | return PREFIX(compress2)(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); |
75 | 0 | } |
76 | | |
77 | | /* =========================================================================== |
78 | | If the default memLevel or windowBits for deflateInit() is changed, then |
79 | | this function needs to be updated. |
80 | | */ |
81 | 4.73k | z_uintmax_t Z_EXPORT PREFIX(compressBound)(z_uintmax_t sourceLen) { |
82 | 4.73k | z_uintmax_t complen = DEFLATE_BOUND_COMPLEN(sourceLen); |
83 | | |
84 | 4.73k | if (complen > 0) |
85 | | /* Architecture-specific code provided an upper bound. */ |
86 | 0 | return complen + ZLIB_WRAPLEN; |
87 | | |
88 | 4.73k | #ifndef NO_QUICK_STRATEGY |
89 | 4.73k | return sourceLen /* The source size itself */ |
90 | 4.73k | + (sourceLen == 0 ? 1 : 0) /* Always at least one byte for any input */ |
91 | 4.73k | + (sourceLen < 9 ? 1 : 0) /* One extra byte for lengths less than 9 */ |
92 | 4.73k | + DEFLATE_QUICK_OVERHEAD(sourceLen) /* Source encoding overhead, padded to next full byte */ |
93 | 4.73k | + DEFLATE_BLOCK_OVERHEAD /* Deflate block overhead bytes */ |
94 | 4.73k | + ZLIB_WRAPLEN; /* zlib wrapper */ |
95 | | #else |
96 | | return sourceLen + (sourceLen >> 4) + 7 + ZLIB_WRAPLEN; |
97 | | #endif |
98 | 4.73k | } |