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