/src/libzip/lib/zip_algorithm_bzip2.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | zip_algorithm_bzip2.c -- bzip2 (de)compression routines |
3 | | Copyright (C) 2017-2023 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 <bzlib.h> |
37 | | #include <limits.h> |
38 | | #include <stdlib.h> |
39 | | |
40 | | struct ctx { |
41 | | zip_error_t *error; |
42 | | bool compress; |
43 | | int compression_flags; |
44 | | bool end_of_input; |
45 | | bz_stream zstr; |
46 | | }; |
47 | | |
48 | | |
49 | | static zip_uint64_t |
50 | 0 | maximum_compressed_size(zip_uint64_t uncompressed_size) { |
51 | 0 | zip_uint64_t compressed_size = (zip_uint64_t)((double)uncompressed_size * 1.006); |
52 | |
|
53 | 0 | if (compressed_size < uncompressed_size) { |
54 | 0 | return ZIP_UINT64_MAX; |
55 | 0 | } |
56 | 0 | return compressed_size; |
57 | 0 | } |
58 | | |
59 | | |
60 | | static void * |
61 | 0 | allocate(bool compress, zip_uint32_t compression_flags, zip_error_t *error) { |
62 | 0 | struct ctx *ctx; |
63 | |
|
64 | 0 | if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) { |
65 | 0 | return NULL; |
66 | 0 | } |
67 | | |
68 | 0 | ctx->error = error; |
69 | 0 | ctx->compress = compress; |
70 | 0 | if (compression_flags >= 1 && compression_flags <= 9) { |
71 | 0 | ctx->compression_flags = (int)compression_flags; |
72 | 0 | } |
73 | 0 | else { |
74 | 0 | ctx->compression_flags = 9; |
75 | 0 | } |
76 | 0 | ctx->end_of_input = false; |
77 | |
|
78 | 0 | ctx->zstr.bzalloc = NULL; |
79 | 0 | ctx->zstr.bzfree = NULL; |
80 | 0 | ctx->zstr.opaque = NULL; |
81 | |
|
82 | 0 | return ctx; |
83 | 0 | } |
84 | | |
85 | | |
86 | | static void * |
87 | 0 | compress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) { |
88 | 0 | (void)method; |
89 | 0 | return allocate(true, compression_flags, error); |
90 | 0 | } |
91 | | |
92 | | |
93 | | static void * |
94 | 0 | decompress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) { |
95 | 0 | (void)method; |
96 | 0 | return allocate(false, compression_flags, error); |
97 | 0 | } |
98 | | |
99 | | |
100 | | static void |
101 | 0 | deallocate(void *ud) { |
102 | 0 | struct ctx *ctx = (struct ctx *)ud; |
103 | |
|
104 | 0 | free(ctx); |
105 | 0 | } |
106 | | |
107 | | |
108 | | static zip_uint16_t |
109 | 0 | general_purpose_bit_flags(void *ud) { |
110 | 0 | (void)ud; |
111 | 0 | return 0; |
112 | 0 | } |
113 | | |
114 | | |
115 | | static int |
116 | 0 | map_error(int ret) { |
117 | 0 | switch (ret) { |
118 | 0 | case BZ_FINISH_OK: |
119 | 0 | case BZ_FLUSH_OK: |
120 | 0 | case BZ_OK: |
121 | 0 | case BZ_RUN_OK: |
122 | 0 | case BZ_STREAM_END: |
123 | 0 | return ZIP_ER_OK; |
124 | | |
125 | 0 | case BZ_DATA_ERROR: |
126 | 0 | case BZ_DATA_ERROR_MAGIC: |
127 | 0 | case BZ_UNEXPECTED_EOF: |
128 | 0 | return ZIP_ER_COMPRESSED_DATA; |
129 | | |
130 | 0 | case BZ_MEM_ERROR: |
131 | 0 | return ZIP_ER_MEMORY; |
132 | | |
133 | 0 | case BZ_PARAM_ERROR: |
134 | 0 | return ZIP_ER_INVAL; |
135 | | |
136 | 0 | case BZ_CONFIG_ERROR: /* actually, bzip2 miscompiled */ |
137 | 0 | case BZ_IO_ERROR: |
138 | 0 | case BZ_OUTBUFF_FULL: |
139 | 0 | case BZ_SEQUENCE_ERROR: |
140 | 0 | default: |
141 | 0 | return ZIP_ER_INTERNAL; |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | | static bool |
146 | 0 | start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes) { |
147 | 0 | struct ctx *ctx = (struct ctx *)ud; |
148 | 0 | int ret; |
149 | |
|
150 | 0 | (void)st; |
151 | 0 | (void)attributes; |
152 | |
|
153 | 0 | ctx->zstr.avail_in = 0; |
154 | 0 | ctx->zstr.next_in = NULL; |
155 | 0 | ctx->zstr.avail_out = 0; |
156 | 0 | ctx->zstr.next_out = NULL; |
157 | |
|
158 | 0 | if (ctx->compress) { |
159 | 0 | ret = BZ2_bzCompressInit(&ctx->zstr, ctx->compression_flags, 0, 30); |
160 | 0 | } |
161 | 0 | else { |
162 | 0 | ret = BZ2_bzDecompressInit(&ctx->zstr, 0, 0); |
163 | 0 | } |
164 | |
|
165 | 0 | if (ret != BZ_OK) { |
166 | 0 | zip_error_set(ctx->error, map_error(ret), 0); |
167 | 0 | return false; |
168 | 0 | } |
169 | | |
170 | 0 | return true; |
171 | 0 | } |
172 | | |
173 | | |
174 | | static bool |
175 | 0 | end(void *ud) { |
176 | 0 | struct ctx *ctx = (struct ctx *)ud; |
177 | 0 | int err; |
178 | |
|
179 | 0 | if (ctx->compress) { |
180 | 0 | err = BZ2_bzCompressEnd(&ctx->zstr); |
181 | 0 | } |
182 | 0 | else { |
183 | 0 | err = BZ2_bzDecompressEnd(&ctx->zstr); |
184 | 0 | } |
185 | |
|
186 | 0 | if (err != BZ_OK) { |
187 | 0 | zip_error_set(ctx->error, map_error(err), 0); |
188 | 0 | return false; |
189 | 0 | } |
190 | | |
191 | 0 | return true; |
192 | 0 | } |
193 | | |
194 | | |
195 | | static bool |
196 | 0 | input(void *ud, zip_uint8_t *data, zip_uint64_t length) { |
197 | 0 | struct ctx *ctx = (struct ctx *)ud; |
198 | |
|
199 | 0 | if (length > UINT_MAX || ctx->zstr.avail_in > 0) { |
200 | 0 | zip_error_set(ctx->error, ZIP_ER_INVAL, 0); |
201 | 0 | return false; |
202 | 0 | } |
203 | | |
204 | 0 | ctx->zstr.avail_in = (unsigned int)length; |
205 | 0 | ctx->zstr.next_in = (char *)data; |
206 | |
|
207 | 0 | return true; |
208 | 0 | } |
209 | | |
210 | | |
211 | 0 | static bool end_of_input(void *ud) { |
212 | 0 | struct ctx *ctx = (struct ctx *)ud; |
213 | |
|
214 | 0 | ctx->end_of_input = true; |
215 | 0 | return ctx->zstr.avail_in != 0; |
216 | 0 | } |
217 | | |
218 | | |
219 | | static zip_compression_status_t |
220 | 0 | process(void *ud, zip_uint8_t *data, zip_uint64_t *length) { |
221 | 0 | struct ctx *ctx = (struct ctx *)ud; |
222 | 0 | unsigned int avail_out; |
223 | |
|
224 | 0 | int ret; |
225 | |
|
226 | 0 | if (ctx->zstr.avail_in == 0 && !ctx->end_of_input) { |
227 | 0 | *length = 0; |
228 | 0 | return ZIP_COMPRESSION_NEED_DATA; |
229 | 0 | } |
230 | | |
231 | 0 | avail_out = (unsigned int)ZIP_MIN(UINT_MAX, *length); |
232 | 0 | ctx->zstr.avail_out = avail_out; |
233 | 0 | ctx->zstr.next_out = (char *)data; |
234 | |
|
235 | 0 | if (ctx->compress) { |
236 | 0 | ret = BZ2_bzCompress(&ctx->zstr, ctx->end_of_input ? BZ_FINISH : BZ_RUN); |
237 | 0 | } |
238 | 0 | else { |
239 | 0 | ret = BZ2_bzDecompress(&ctx->zstr); |
240 | 0 | } |
241 | |
|
242 | 0 | *length = avail_out - ctx->zstr.avail_out; |
243 | |
|
244 | 0 | switch (ret) { |
245 | 0 | case BZ_FINISH_OK: /* compression */ |
246 | 0 | return ZIP_COMPRESSION_OK; |
247 | | |
248 | 0 | case BZ_OK: /* decompression */ |
249 | 0 | case BZ_RUN_OK: /* compression */ |
250 | 0 | if (ctx->zstr.avail_in == 0) { |
251 | 0 | return ZIP_COMPRESSION_NEED_DATA; |
252 | 0 | } |
253 | 0 | return ZIP_COMPRESSION_OK; |
254 | | |
255 | 0 | case BZ_STREAM_END: |
256 | 0 | return ZIP_COMPRESSION_END; |
257 | | |
258 | 0 | default: |
259 | 0 | zip_error_set(ctx->error, map_error(ret), 0); |
260 | 0 | return ZIP_COMPRESSION_ERROR; |
261 | 0 | } |
262 | 0 | } |
263 | | |
264 | | /* clang-format off */ |
265 | | |
266 | | zip_compression_algorithm_t zip_algorithm_bzip2_compress = { |
267 | | maximum_compressed_size, |
268 | | compress_allocate, |
269 | | deallocate, |
270 | | general_purpose_bit_flags, |
271 | | 46, |
272 | | start, |
273 | | end, |
274 | | input, |
275 | | end_of_input, |
276 | | process |
277 | | }; |
278 | | |
279 | | |
280 | | zip_compression_algorithm_t zip_algorithm_bzip2_decompress = { |
281 | | maximum_compressed_size, |
282 | | decompress_allocate, |
283 | | deallocate, |
284 | | general_purpose_bit_flags, |
285 | | 46, |
286 | | start, |
287 | | end, |
288 | | input, |
289 | | end_of_input, |
290 | | process |
291 | | }; |
292 | | |
293 | | /* clang-format on */ |