/src/libzip/lib/zip_algorithm_deflate.c
Line | Count | Source |
1 | | /* |
2 | | zip_algorithm_deflate.c -- deflate (de)compression routines |
3 | | Copyright (C) 2017-2025 Dieter Baron and Thomas Klausner |
4 | | |
5 | | This file is part of libzip, a library to manipulate ZIP archives. |
6 | | The authors can be contacted at <info@libzip.org> |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | 1. Redistributions of source code must retain the above copyright |
12 | | notice, this list of conditions and the following disclaimer. |
13 | | 2. Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in |
15 | | the documentation and/or other materials provided with the |
16 | | distribution. |
17 | | 3. The names of the authors may not be used to endorse or promote |
18 | | products derived from this software without specific prior |
19 | | written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
22 | | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
25 | | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
27 | | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
29 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
30 | | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
31 | | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | #include "zipint.h" |
35 | | |
36 | | #include <limits.h> |
37 | | #include <stdlib.h> |
38 | | #include <zlib.h> |
39 | | |
40 | | struct ctx { |
41 | | zip_error_t *error; |
42 | | bool compress; |
43 | | int level; |
44 | | int mem_level; |
45 | | bool end_of_input; |
46 | | z_stream zstr; |
47 | | }; |
48 | | |
49 | | |
50 | 0 | static zip_uint64_t maximum_compressed_size(zip_uint64_t uncompressed_size) { |
51 | | /* max deflate size increase: size + ceil(size/16k)*5+6 */ |
52 | |
|
53 | 0 | zip_uint64_t compressed_size = uncompressed_size + (uncompressed_size + 16383) / 16384 * 5 + 6; |
54 | |
|
55 | 0 | if (compressed_size < uncompressed_size) { |
56 | 0 | return ZIP_UINT64_MAX; |
57 | 0 | } |
58 | 0 | return compressed_size; |
59 | 0 | } |
60 | | |
61 | | |
62 | 0 | static void *allocate(bool compress, zip_uint32_t compression_flags, zip_error_t *error) { |
63 | 0 | struct ctx *ctx; |
64 | |
|
65 | 0 | if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) { |
66 | 0 | zip_error_set(error, ZIP_ET_SYS, errno); |
67 | 0 | return NULL; |
68 | 0 | } |
69 | | |
70 | 0 | ctx->error = error; |
71 | 0 | ctx->compress = compress; |
72 | 0 | if (compression_flags >= 1 && compression_flags <= 9) { |
73 | 0 | ctx->level = (int)compression_flags; |
74 | 0 | } |
75 | 0 | else { |
76 | 0 | ctx->level = Z_BEST_COMPRESSION; |
77 | 0 | } |
78 | 0 | ctx->mem_level = compression_flags == TORRENTZIP_COMPRESSION_FLAGS ? TORRENTZIP_MEM_LEVEL : MAX_MEM_LEVEL; |
79 | 0 | ctx->end_of_input = false; |
80 | |
|
81 | 0 | ctx->zstr.zalloc = Z_NULL; |
82 | 0 | ctx->zstr.zfree = Z_NULL; |
83 | 0 | ctx->zstr.opaque = NULL; |
84 | |
|
85 | 0 | return ctx; |
86 | 0 | } |
87 | | |
88 | | |
89 | 0 | static void *compress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) { |
90 | 0 | (void)method; |
91 | 0 | return allocate(true, compression_flags, error); |
92 | 0 | } |
93 | | |
94 | | |
95 | 0 | static void *decompress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) { |
96 | 0 | (void)method; |
97 | 0 | return allocate(false, compression_flags, error); |
98 | 0 | } |
99 | | |
100 | | |
101 | 0 | static void deallocate(void *ud) { |
102 | 0 | struct ctx *ctx = (struct ctx *)ud; |
103 | |
|
104 | 0 | free(ctx); |
105 | 0 | } |
106 | | |
107 | | |
108 | 0 | static zip_uint16_t general_purpose_bit_flags(void *ud) { |
109 | 0 | struct ctx *ctx = (struct ctx *)ud; |
110 | |
|
111 | 0 | if (!ctx->compress) { |
112 | 0 | return 0; |
113 | 0 | } |
114 | | |
115 | 0 | if (ctx->level < 3) { |
116 | 0 | return 2 << 1; |
117 | 0 | } |
118 | 0 | else if (ctx->level > 7) { |
119 | 0 | return 1 << 1; |
120 | 0 | } |
121 | 0 | return 0; |
122 | 0 | } |
123 | | |
124 | | |
125 | 0 | static bool start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes) { |
126 | 0 | struct ctx *ctx = (struct ctx *)ud; |
127 | 0 | int ret; |
128 | |
|
129 | 0 | (void)st; |
130 | 0 | (void)attributes; |
131 | |
|
132 | 0 | ctx->zstr.avail_in = 0; |
133 | 0 | ctx->zstr.next_in = NULL; |
134 | 0 | ctx->zstr.avail_out = 0; |
135 | 0 | ctx->zstr.next_out = NULL; |
136 | |
|
137 | 0 | if (ctx->compress) { |
138 | | /* negative value to tell zlib not to write a header */ |
139 | 0 | ret = deflateInit2(&ctx->zstr, ctx->level, Z_DEFLATED, -MAX_WBITS, ctx->mem_level, Z_DEFAULT_STRATEGY); |
140 | 0 | } |
141 | 0 | else { |
142 | 0 | ret = inflateInit2(&ctx->zstr, -MAX_WBITS); |
143 | 0 | } |
144 | |
|
145 | 0 | if (ret != Z_OK) { |
146 | 0 | zip_error_set(ctx->error, ZIP_ER_ZLIB, ret); |
147 | 0 | return false; |
148 | 0 | } |
149 | | |
150 | | |
151 | 0 | return true; |
152 | 0 | } |
153 | | |
154 | | |
155 | 0 | static bool end(void *ud) { |
156 | 0 | struct ctx *ctx = (struct ctx *)ud; |
157 | 0 | int err; |
158 | |
|
159 | 0 | if (ctx->compress) { |
160 | 0 | err = deflateEnd(&ctx->zstr); |
161 | 0 | } |
162 | 0 | else { |
163 | 0 | err = inflateEnd(&ctx->zstr); |
164 | 0 | } |
165 | |
|
166 | 0 | if (err != Z_OK) { |
167 | 0 | zip_error_set(ctx->error, ZIP_ER_ZLIB, err); |
168 | 0 | return false; |
169 | 0 | } |
170 | | |
171 | 0 | return true; |
172 | 0 | } |
173 | | |
174 | | |
175 | 0 | static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length) { |
176 | 0 | struct ctx *ctx = (struct ctx *)ud; |
177 | |
|
178 | 0 | if (length > UINT_MAX || ctx->zstr.avail_in > 0) { |
179 | 0 | zip_error_set(ctx->error, ZIP_ER_INVAL, 0); |
180 | 0 | return false; |
181 | 0 | } |
182 | | |
183 | 0 | ctx->zstr.avail_in = (uInt)length; |
184 | 0 | ctx->zstr.next_in = (Bytef *)data; |
185 | |
|
186 | 0 | return true; |
187 | 0 | } |
188 | | |
189 | | |
190 | 0 | static bool end_of_input(void *ud) { |
191 | 0 | struct ctx *ctx = (struct ctx *)ud; |
192 | |
|
193 | 0 | ctx->end_of_input = true; |
194 | 0 | return ctx->zstr.avail_in != 0; |
195 | 0 | } |
196 | | |
197 | | |
198 | 0 | static zip_compression_status_t process(void *ud, zip_uint8_t *data, zip_uint64_t *length) { |
199 | 0 | struct ctx *ctx = (struct ctx *)ud; |
200 | 0 | uInt avail_out; |
201 | |
|
202 | 0 | int ret; |
203 | |
|
204 | 0 | avail_out = (uInt)ZIP_MIN(UINT_MAX, *length); |
205 | 0 | ctx->zstr.avail_out = avail_out; |
206 | 0 | ctx->zstr.next_out = (Bytef *)data; |
207 | |
|
208 | 0 | if (ctx->compress) { |
209 | 0 | ret = deflate(&ctx->zstr, ctx->end_of_input ? Z_FINISH : 0); |
210 | 0 | } |
211 | 0 | else { |
212 | 0 | ret = inflate(&ctx->zstr, Z_SYNC_FLUSH); |
213 | 0 | } |
214 | |
|
215 | 0 | *length = avail_out - ctx->zstr.avail_out; |
216 | |
|
217 | 0 | switch (ret) { |
218 | 0 | case Z_OK: |
219 | 0 | return ZIP_COMPRESSION_OK; |
220 | | |
221 | 0 | case Z_STREAM_END: |
222 | 0 | return ZIP_COMPRESSION_END; |
223 | | |
224 | 0 | case Z_BUF_ERROR: |
225 | 0 | if (ctx->zstr.avail_in == 0) { |
226 | 0 | return ZIP_COMPRESSION_NEED_DATA; |
227 | 0 | } |
228 | | |
229 | | /* fallthrough */ |
230 | | |
231 | 0 | default: |
232 | 0 | zip_error_set(ctx->error, ZIP_ER_ZLIB, ret); |
233 | 0 | return ZIP_COMPRESSION_ERROR; |
234 | 0 | } |
235 | 0 | } |
236 | | |
237 | | /* clang-format off */ |
238 | | |
239 | | zip_compression_algorithm_t zip_algorithm_deflate_compress = { |
240 | | maximum_compressed_size, |
241 | | compress_allocate, |
242 | | deallocate, |
243 | | general_purpose_bit_flags, |
244 | | 20, |
245 | | start, |
246 | | end, |
247 | | input, |
248 | | end_of_input, |
249 | | process |
250 | | }; |
251 | | |
252 | | |
253 | | zip_compression_algorithm_t zip_algorithm_deflate_decompress = { |
254 | | maximum_compressed_size, |
255 | | decompress_allocate, |
256 | | deallocate, |
257 | | general_purpose_bit_flags, |
258 | | 20, |
259 | | start, |
260 | | end, |
261 | | input, |
262 | | end_of_input, |
263 | | process |
264 | | }; |
265 | | |
266 | | /* clang-format on */ |