Coverage Report

Created: 2026-10-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_source_compress.c
Line
Count
Source
1
/*
2
  zip_source_compress.c -- (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 <stdlib.h>
37
#include <string.h>
38
39
struct context {
40
    zip_error_t error;
41
42
    bool end_of_input;
43
    bool end_of_stream;
44
    bool can_store;
45
    bool is_stored; /* only valid if end_of_stream is true */
46
    bool compress;
47
    bool check_consistency;
48
    zip_int32_t method;
49
50
    zip_uint64_t size;
51
    zip_int64_t first_read;
52
    zip_uint8_t buffer[BUFSIZE];
53
54
    zip_compression_algorithm_t *algorithm;
55
    void *ud;
56
};
57
58
59
struct implementation {
60
    zip_uint16_t method;
61
    zip_compression_algorithm_t *compress;
62
    zip_compression_algorithm_t *decompress;
63
};
64
65
static struct implementation implementations[] = {
66
    {ZIP_CM_DEFLATE, &zip_algorithm_deflate_compress, &zip_algorithm_deflate_decompress},
67
#if defined(HAVE_LIBBZ2)
68
    {ZIP_CM_BZIP2, &zip_algorithm_bzip2_compress, &zip_algorithm_bzip2_decompress},
69
#endif
70
#if defined(HAVE_LIBLZMA)
71
    {ZIP_CM_LZMA, &zip_algorithm_xz_compress, &zip_algorithm_xz_decompress},
72
    /*  Disabled - because 7z isn't able to unpack ZIP+LZMA2
73
        archives made this way - and vice versa.
74
75
        {ZIP_CM_LZMA2, &zip_algorithm_xz_compress, &zip_algorithm_xz_decompress},
76
    */
77
    {ZIP_CM_XZ, &zip_algorithm_xz_compress, &zip_algorithm_xz_decompress},
78
#endif
79
#if defined(HAVE_LIBZSTD)
80
    {ZIP_CM_ZSTD, &zip_algorithm_zstd_compress, &zip_algorithm_zstd_decompress},
81
#endif
82
83
};
84
85
static size_t implementations_size = sizeof(implementations) / sizeof(implementations[0]);
86
87
static zip_source_t *compression_source_new(zip_t *za, zip_source_t *src, zip_int32_t method, bool compress, zip_uint32_t compression_flags);
88
static zip_int64_t compress_callback(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t);
89
static void context_free(struct context *ctx);
90
static struct context *context_new(zip_int32_t method, bool compress, zip_uint32_t compression_flags, zip_compression_algorithm_t *algorithm, bool check_consistency);
91
static zip_int64_t compress_read(zip_source_t *, struct context *, void *, zip_uint64_t);
92
93
7.53k
zip_compression_algorithm_t *_zip_get_compression_algorithm(zip_int32_t method, bool compress) {
94
7.53k
    size_t i;
95
7.53k
    zip_uint16_t real_method = ZIP_CM_ACTUAL(method);
96
97
8.05k
    for (i = 0; i < implementations_size; i++) {
98
7.53k
        if (implementations[i].method == real_method) {
99
7.01k
            if (compress) {
100
0
                return implementations[i].compress;
101
0
            }
102
7.01k
            else {
103
7.01k
                return implementations[i].decompress;
104
7.01k
            }
105
7.01k
        }
106
7.53k
    }
107
108
522
    return NULL;
109
7.53k
}
110
111
0
ZIP_EXTERN int zip_compression_method_supported(zip_int32_t method, int compress) {
112
0
    if (method == ZIP_CM_STORE) {
113
0
        return 1;
114
0
    }
115
0
    return _zip_get_compression_algorithm(method, compress) != NULL;
116
0
}
117
118
0
zip_source_t *zip_source_compress(zip_t *za, zip_source_t *src, zip_int32_t method, zip_uint32_t compression_flags) {
119
0
    return compression_source_new(za, src, method, true, compression_flags);
120
0
}
121
122
7.53k
zip_source_t *zip_source_decompress(zip_t *za, zip_source_t *src, zip_int32_t method) {
123
7.53k
    return compression_source_new(za, src, method, false, 0);
124
7.53k
}
125
126
127
7.53k
static zip_source_t *compression_source_new(zip_t *za, zip_source_t *src, zip_int32_t method, bool compress, zip_uint32_t compression_flags) {
128
7.53k
    struct context *ctx;
129
7.53k
    zip_source_t *s2;
130
7.53k
    zip_compression_algorithm_t *algorithm = NULL;
131
132
7.53k
    if (src == NULL) {
133
0
        zip_error_set(&za->error, ZIP_ER_INVAL, 0);
134
0
        return NULL;
135
0
    }
136
137
7.53k
    if ((algorithm = _zip_get_compression_algorithm(method, compress)) == NULL) {
138
522
        zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0);
139
522
        return NULL;
140
522
    }
141
142
7.01k
    if ((ctx = context_new(method, compress, compression_flags, algorithm, za->open_flags & ZIP_CHECKCONS)) == NULL) {
143
0
        zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
144
0
        return NULL;
145
0
    }
146
147
7.01k
    if ((s2 = zip_source_layered(za, src, compress_callback, ctx)) == NULL) {
148
0
        context_free(ctx);
149
0
        return NULL;
150
0
    }
151
152
7.01k
    return s2;
153
7.01k
}
154
155
156
7.01k
static struct context *context_new(zip_int32_t method, bool compress, zip_uint32_t compression_flags, zip_compression_algorithm_t *algorithm, bool check_consistency) {
157
7.01k
    struct context *ctx;
158
159
7.01k
    if ((ctx = (struct context *)malloc(sizeof(*ctx))) == NULL) {
160
0
        return NULL;
161
0
    }
162
7.01k
    zip_error_init(&ctx->error);
163
7.01k
    ctx->can_store = compress ? method == ZIP_CM_DEFAULT : false;
164
7.01k
    ctx->algorithm = algorithm;
165
7.01k
    ctx->method = method;
166
7.01k
    ctx->compress = compress;
167
7.01k
    ctx->end_of_input = false;
168
7.01k
    ctx->end_of_stream = false;
169
7.01k
    ctx->is_stored = false;
170
7.01k
    ctx->check_consistency = check_consistency;
171
172
7.01k
    if ((ctx->ud = ctx->algorithm->allocate(ZIP_CM_ACTUAL(method), compression_flags, &ctx->error)) == NULL) {
173
0
        zip_error_fini(&ctx->error);
174
0
        free(ctx);
175
0
        return NULL;
176
0
    }
177
178
7.01k
    return ctx;
179
7.01k
}
180
181
182
7.01k
static void context_free(struct context *ctx) {
183
7.01k
    if (ctx == NULL) {
184
0
        return;
185
0
    }
186
187
7.01k
    ctx->algorithm->deallocate(ctx->ud);
188
7.01k
    zip_error_fini(&ctx->error);
189
190
7.01k
    free(ctx);
191
7.01k
}
192
193
194
11.9k
static zip_int64_t compress_read(zip_source_t *src, struct context *ctx, void *data, zip_uint64_t len) {
195
11.9k
    zip_compression_status_t ret;
196
11.9k
    bool end;
197
11.9k
    zip_int64_t n;
198
11.9k
    zip_uint64_t out_offset;
199
11.9k
    zip_uint64_t out_len;
200
201
11.9k
    if (zip_error_code_zip(&ctx->error) != ZIP_ER_OK) {
202
100
        return -1;
203
100
    }
204
205
11.8k
    if (len == 0 || ctx->end_of_stream) {
206
3.16k
        return 0;
207
3.16k
    }
208
209
8.71k
    out_offset = 0;
210
211
8.71k
    end = false;
212
26.1k
    while (!end && out_offset < len) {
213
17.4k
        out_len = len - out_offset;
214
17.4k
        ret = ctx->algorithm->process(ctx->ud, (zip_uint8_t *)data + out_offset, &out_len);
215
216
17.4k
        if (ret != ZIP_COMPRESSION_ERROR) {
217
16.2k
            out_offset += out_len;
218
16.2k
        }
219
220
17.4k
        switch (ret) {
221
3.29k
        case ZIP_COMPRESSION_END:
222
3.29k
            ctx->end_of_stream = true;
223
224
3.29k
            if (!ctx->end_of_input) {
225
3.29k
                n = zip_source_read(src, ctx->buffer, 1);
226
3.29k
                if (n < 0) {
227
68
                    zip_error_set_from_source(&ctx->error, src);
228
68
                    end = true;
229
68
                    break;
230
68
                }
231
3.22k
                else if (n == 0) {
232
3.20k
                    ctx->end_of_input = true;
233
3.20k
                    n = ctx->algorithm->end_of_input(ctx->ud) ? 1 : 0;
234
3.20k
                }
235
236
3.22k
                if (n > 0 && ctx->check_consistency) {
237
                    /* garbage after stream, or compression ended before all data read */
238
0
                    zip_error_set(&ctx->error, ZIP_ER_INCONS, ZIP_ER_DETAIL_COMPRESSED_DATA_TRAILING_GARBAGE);
239
0
                    end = true;
240
0
                    break;
241
0
                }
242
3.22k
            }
243
244
3.22k
            if (ctx->first_read < 0) {
245
                /* we got end of processed stream before reading any input data */
246
0
                zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
247
0
                end = true;
248
0
                break;
249
0
            }
250
3.22k
            if (ctx->can_store && (zip_uint64_t)ctx->first_read <= out_offset) {
251
0
                ctx->is_stored = true;
252
0
                ctx->size = (zip_uint64_t)ctx->first_read;
253
0
                (void)memcpy_s(data, ctx->size, ctx->buffer, ctx->size);
254
0
                return (zip_int64_t)ctx->size;
255
0
            }
256
3.22k
            end = true;
257
3.22k
            break;
258
259
3.08k
        case ZIP_COMPRESSION_OK:
260
3.08k
            break;
261
262
9.86k
        case ZIP_COMPRESSION_NEED_DATA:
263
9.86k
            if (ctx->end_of_input) {
264
                /* TODO: error: stream not ended, but no more input */
265
2.04k
                end = true;
266
2.04k
                break;
267
2.04k
            }
268
269
7.81k
            if ((n = zip_source_read(src, ctx->buffer, sizeof(ctx->buffer))) < 0) {
270
118
                zip_error_set_from_source(&ctx->error, src);
271
118
                end = true;
272
118
                break;
273
118
            }
274
7.70k
            else if (n == 0) {
275
1.09k
                ctx->end_of_input = true;
276
1.09k
                ctx->algorithm->end_of_input(ctx->ud);
277
1.09k
                if (ctx->first_read < 0) {
278
33
                    ctx->first_read = 0;
279
33
                }
280
1.09k
            }
281
6.60k
            else {
282
6.60k
                if (ctx->first_read >= 0) {
283
                    /* we overwrote a previously filled ctx->buffer */
284
0
                    ctx->can_store = false;
285
0
                }
286
6.60k
                else {
287
6.60k
                    ctx->first_read = n;
288
6.60k
                }
289
290
6.60k
                ctx->algorithm->input(ctx->ud, ctx->buffer, (zip_uint64_t)n);
291
6.60k
            }
292
7.70k
            break;
293
294
7.70k
        case ZIP_COMPRESSION_ERROR:
295
            /* error set by algorithm */
296
1.24k
            if (zip_error_code_zip(&ctx->error) == ZIP_ER_OK) {
297
0
                zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
298
0
            }
299
1.24k
            end = true;
300
1.24k
            break;
301
17.4k
        }
302
17.4k
    }
303
304
8.71k
    if (out_offset > 0) {
305
6.28k
        ctx->can_store = false;
306
6.28k
        ctx->size += out_offset;
307
6.28k
        return (zip_int64_t)out_offset;
308
6.28k
    }
309
310
2.43k
    return (zip_error_code_zip(&ctx->error) == ZIP_ER_OK) ? 0 : -1;
311
8.71k
}
312
313
314
47.0k
static zip_int64_t compress_callback(zip_source_t *src, void *ud, void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
315
47.0k
    struct context *ctx;
316
317
47.0k
    ctx = (struct context *)ud;
318
319
47.0k
    switch (cmd) {
320
6.71k
    case ZIP_SOURCE_OPEN: {
321
6.71k
        zip_stat_t st;
322
6.71k
        zip_file_attributes_t attributes;
323
324
6.71k
        ctx->size = 0;
325
6.71k
        ctx->end_of_input = false;
326
6.71k
        ctx->end_of_stream = false;
327
6.71k
        ctx->is_stored = false;
328
6.71k
        ctx->first_read = -1;
329
330
6.71k
        if (zip_source_stat(src, &st) < 0 || zip_source_get_file_attributes(src, &attributes) < 0) {
331
0
            zip_error_set_from_source(&ctx->error, src);
332
0
            return -1;
333
0
        }
334
335
6.71k
        if (!ctx->algorithm->start(ctx->ud, &st, &attributes)) {
336
0
            return -1;
337
0
        }
338
339
6.71k
        return 0;
340
6.71k
    }
341
342
11.9k
    case ZIP_SOURCE_READ:
343
11.9k
        return compress_read(src, ctx, data, len);
344
345
6.71k
    case ZIP_SOURCE_CLOSE:
346
6.71k
        if (!ctx->algorithm->end(ctx->ud)) {
347
0
            return -1;
348
0
        }
349
6.71k
        return 0;
350
351
6.19k
    case ZIP_SOURCE_STAT: {
352
6.19k
        zip_stat_t *st;
353
354
6.19k
        st = (zip_stat_t *)data;
355
356
6.19k
        if (ctx->compress) {
357
0
            if (ctx->end_of_stream) {
358
0
                st->comp_method = ctx->is_stored ? ZIP_CM_STORE : ZIP_CM_ACTUAL(ctx->method);
359
0
                st->comp_size = ctx->size;
360
0
                st->valid |= ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD;
361
0
            }
362
0
            else {
363
0
                st->valid &= ~(ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD);
364
0
            }
365
0
        }
366
6.19k
        else {
367
6.19k
            st->comp_method = ZIP_CM_STORE;
368
6.19k
            st->valid |= ZIP_STAT_COMP_METHOD;
369
6.19k
            st->valid &= ~ZIP_STAT_COMP_SIZE;
370
6.19k
            if (ctx->end_of_stream) {
371
4.09k
                st->size = ctx->size;
372
4.09k
                st->valid |= ZIP_STAT_SIZE;
373
4.09k
            }
374
6.19k
        }
375
6.19k
    }
376
6.19k
        return 0;
377
378
1.43k
    case ZIP_SOURCE_ERROR:
379
1.43k
        return zip_error_to_data(&ctx->error, data, len);
380
381
7.01k
    case ZIP_SOURCE_FREE:
382
7.01k
        context_free(ctx);
383
7.01k
        return 0;
384
385
0
    case ZIP_SOURCE_GET_FILE_ATTRIBUTES: {
386
0
        zip_file_attributes_t *attributes = (zip_file_attributes_t *)data;
387
388
0
        if (len < sizeof(*attributes)) {
389
0
            zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
390
0
            return -1;
391
0
        }
392
393
0
        attributes->valid |= ZIP_FILE_ATTRIBUTES_VERSION_NEEDED | ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS;
394
0
        attributes->version_needed = ctx->algorithm->version_needed;
395
0
        attributes->general_purpose_bit_mask = ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS_ALLOWED_MASK;
396
0
        attributes->general_purpose_bit_flags = (ctx->is_stored ? 0 : ctx->algorithm->general_purpose_bit_flags(ctx->ud));
397
398
0
        return sizeof(*attributes);
399
0
    }
400
401
7.01k
    case ZIP_SOURCE_SUPPORTS:
402
7.01k
        return ZIP_SOURCE_SUPPORTS_READABLE | zip_source_make_command_bitmap(ZIP_SOURCE_GET_FILE_ATTRIBUTES, ZIP_SOURCE_SUPPORTS_REOPEN, -1);
403
404
0
    default:
405
0
        return zip_source_pass_to_lower_layer(src, data, len, cmd);
406
47.0k
    }
407
47.0k
}