Coverage Report

Created: 2026-04-01 07:49

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