Coverage Report

Created: 2025-08-03 07:00

/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
4.85k
allocate(bool compress, zip_uint32_t compression_flags, zip_error_t *error) {
62
4.85k
    struct ctx *ctx;
63
64
4.85k
    if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) {
65
0
        return NULL;
66
0
    }
67
68
4.85k
    ctx->error = error;
69
4.85k
    ctx->compress = compress;
70
4.85k
    if (compression_flags >= 1 && compression_flags <= 9) {
71
0
        ctx->compression_flags = (int)compression_flags;
72
0
    }
73
4.85k
    else {
74
4.85k
        ctx->compression_flags = 9;
75
4.85k
    }
76
4.85k
    ctx->end_of_input = false;
77
78
4.85k
    ctx->zstr.bzalloc = NULL;
79
4.85k
    ctx->zstr.bzfree = NULL;
80
4.85k
    ctx->zstr.opaque = NULL;
81
82
4.85k
    return ctx;
83
4.85k
}
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
4.85k
decompress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) {
95
4.85k
    (void)method;
96
4.85k
    return allocate(false, compression_flags, error);
97
4.85k
}
98
99
100
static void
101
4.85k
deallocate(void *ud) {
102
4.85k
    struct ctx *ctx = (struct ctx *)ud;
103
104
4.85k
    free(ctx);
105
4.85k
}
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
986
map_error(int ret) {
117
986
    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
60
    case BZ_DATA_ERROR:
126
986
    case BZ_DATA_ERROR_MAGIC:
127
986
    case BZ_UNEXPECTED_EOF:
128
986
        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
986
    }
143
986
}
144
145
static bool
146
3.74k
start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes) {
147
3.74k
    struct ctx *ctx = (struct ctx *)ud;
148
3.74k
    int ret;
149
150
3.74k
    (void)st;
151
3.74k
    (void)attributes;
152
153
3.74k
    ctx->zstr.avail_in = 0;
154
3.74k
    ctx->zstr.next_in = NULL;
155
3.74k
    ctx->zstr.avail_out = 0;
156
3.74k
    ctx->zstr.next_out = NULL;
157
158
3.74k
    if (ctx->compress) {
159
0
        ret = BZ2_bzCompressInit(&ctx->zstr, ctx->compression_flags, 0, 30);
160
0
    }
161
3.74k
    else {
162
3.74k
        ret = BZ2_bzDecompressInit(&ctx->zstr, 0, 0);
163
3.74k
    }
164
165
3.74k
    if (ret != BZ_OK) {
166
0
        zip_error_set(ctx->error, map_error(ret), 0);
167
0
        return false;
168
0
    }
169
170
3.74k
    return true;
171
3.74k
}
172
173
174
static bool
175
3.74k
end(void *ud) {
176
3.74k
    struct ctx *ctx = (struct ctx *)ud;
177
3.74k
    int err;
178
179
3.74k
    if (ctx->compress) {
180
0
        err = BZ2_bzCompressEnd(&ctx->zstr);
181
0
    }
182
3.74k
    else {
183
3.74k
        err = BZ2_bzDecompressEnd(&ctx->zstr);
184
3.74k
    }
185
186
3.74k
    if (err != BZ_OK) {
187
0
        zip_error_set(ctx->error, map_error(err), 0);
188
0
        return false;
189
0
    }
190
191
3.74k
    return true;
192
3.74k
}
193
194
195
static bool
196
1.16k
input(void *ud, zip_uint8_t *data, zip_uint64_t length) {
197
1.16k
    struct ctx *ctx = (struct ctx *)ud;
198
199
1.16k
    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
1.16k
    ctx->zstr.avail_in = (unsigned int)length;
205
1.16k
    ctx->zstr.next_in = (char *)data;
206
207
1.16k
    return true;
208
1.16k
}
209
210
211
1.59k
static bool end_of_input(void *ud) {
212
1.59k
    struct ctx *ctx = (struct ctx *)ud;
213
214
1.59k
    ctx->end_of_input = true;
215
1.59k
    return ctx->zstr.avail_in != 0;
216
1.59k
}
217
218
219
static zip_compression_status_t
220
10.2k
process(void *ud, zip_uint8_t *data, zip_uint64_t *length) {
221
10.2k
    struct ctx *ctx = (struct ctx *)ud;
222
10.2k
    unsigned int avail_out;
223
224
10.2k
    int ret;
225
226
10.2k
    if (ctx->zstr.avail_in == 0 && !ctx->end_of_input) {
227
3.74k
        *length = 0;
228
3.74k
        return ZIP_COMPRESSION_NEED_DATA;
229
3.74k
    }
230
231
6.50k
    avail_out = (unsigned int)ZIP_MIN(UINT_MAX, *length);
232
6.50k
    ctx->zstr.avail_out = avail_out;
233
6.50k
    ctx->zstr.next_out = (char *)data;
234
235
6.50k
    if (ctx->compress) {
236
0
        ret = BZ2_bzCompress(&ctx->zstr, ctx->end_of_input ? BZ_FINISH : BZ_RUN);
237
0
    }
238
6.50k
    else {
239
6.50k
        ret = BZ2_bzDecompress(&ctx->zstr);
240
6.50k
    }
241
242
6.50k
    *length = avail_out - ctx->zstr.avail_out;
243
244
6.50k
    switch (ret) {
245
0
    case BZ_FINISH_OK: /* compression */
246
0
        return ZIP_COMPRESSION_OK;
247
248
5.34k
    case BZ_OK:     /* decompression */
249
5.34k
    case BZ_RUN_OK: /* compression */
250
5.34k
        if (ctx->zstr.avail_in == 0) {
251
2.78k
            return ZIP_COMPRESSION_NEED_DATA;
252
2.78k
        }
253
2.56k
        return ZIP_COMPRESSION_OK;
254
255
166
    case BZ_STREAM_END:
256
166
        return ZIP_COMPRESSION_END;
257
258
986
    default:
259
986
        zip_error_set(ctx->error, map_error(ret), 0);
260
986
        return ZIP_COMPRESSION_ERROR;
261
6.50k
    }
262
6.50k
}
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 */