Coverage Report

Created: 2025-07-12 06:27

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