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