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