Coverage Report

Created: 2026-08-14 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_algorithm_deflate.c
Line
Count
Source
1
/*
2
  zip_algorithm_deflate.c -- deflate (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 <limits.h>
37
#include <stdlib.h>
38
#include <zlib.h>
39
40
struct ctx {
41
    zip_error_t *error;
42
    bool compress;
43
    int level;
44
    int mem_level;
45
    bool end_of_input;
46
    z_stream zstr;
47
};
48
49
50
10.8k
static zip_uint64_t maximum_compressed_size(zip_uint64_t uncompressed_size) {
51
    /* max deflate size increase: size + ceil(size/16k)*5+6 */
52
53
10.8k
    zip_uint64_t compressed_size = uncompressed_size + (uncompressed_size + 16383) / 16384 * 5 + 6;
54
55
10.8k
    if (compressed_size < uncompressed_size) {
56
0
        return ZIP_UINT64_MAX;
57
0
    }
58
10.8k
    return compressed_size;
59
10.8k
}
60
61
62
33.3k
static void *allocate(bool compress, zip_uint32_t compression_flags, zip_error_t *error) {
63
33.3k
    struct ctx *ctx;
64
65
33.3k
    if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) {
66
0
        zip_error_set(error, ZIP_ET_SYS, errno);
67
0
        return NULL;
68
0
    }
69
70
33.3k
    ctx->error = error;
71
33.3k
    ctx->compress = compress;
72
33.3k
    if (compression_flags >= 1 && compression_flags <= 9) {
73
3.74k
        ctx->level = (int)compression_flags;
74
3.74k
    }
75
29.6k
    else {
76
29.6k
        ctx->level = Z_BEST_COMPRESSION;
77
29.6k
    }
78
33.3k
    ctx->mem_level = compression_flags == TORRENTZIP_COMPRESSION_FLAGS ? TORRENTZIP_MEM_LEVEL : MAX_MEM_LEVEL;
79
33.3k
    ctx->end_of_input = false;
80
81
33.3k
    ctx->zstr.zalloc = Z_NULL;
82
33.3k
    ctx->zstr.zfree = Z_NULL;
83
33.3k
    ctx->zstr.opaque = NULL;
84
85
33.3k
    return ctx;
86
33.3k
}
87
88
89
10.8k
static void *compress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) {
90
10.8k
    (void)method;
91
10.8k
    return allocate(true, compression_flags, error);
92
10.8k
}
93
94
95
22.5k
static void *decompress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) {
96
22.5k
    (void)method;
97
22.5k
    return allocate(false, compression_flags, error);
98
22.5k
}
99
100
101
33.3k
static void deallocate(void *ud) {
102
33.3k
    struct ctx *ctx = (struct ctx *)ud;
103
104
33.3k
    free(ctx);
105
33.3k
}
106
107
108
14.5k
static zip_uint16_t general_purpose_bit_flags(void *ud) {
109
14.5k
    struct ctx *ctx = (struct ctx *)ud;
110
111
14.5k
    if (!ctx->compress) {
112
0
        return 0;
113
0
    }
114
115
14.5k
    if (ctx->level < 3) {
116
2.31k
        return 2 << 1;
117
2.31k
    }
118
12.2k
    else if (ctx->level > 7) {
119
8.60k
        return 1 << 1;
120
8.60k
    }
121
3.63k
    return 0;
122
14.5k
}
123
124
125
19.8k
static bool start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes) {
126
19.8k
    struct ctx *ctx = (struct ctx *)ud;
127
19.8k
    int ret;
128
129
19.8k
    (void)st;
130
19.8k
    (void)attributes;
131
132
19.8k
    ctx->zstr.avail_in = 0;
133
19.8k
    ctx->zstr.next_in = NULL;
134
19.8k
    ctx->zstr.avail_out = 0;
135
19.8k
    ctx->zstr.next_out = NULL;
136
137
19.8k
    if (ctx->compress) {
138
        /* negative value to tell zlib not to write a header */
139
10.8k
        ret = deflateInit2(&ctx->zstr, ctx->level, Z_DEFLATED, -MAX_WBITS, ctx->mem_level, Z_DEFAULT_STRATEGY);
140
10.8k
    }
141
8.98k
    else {
142
8.98k
        ret = inflateInit2(&ctx->zstr, -MAX_WBITS);
143
8.98k
    }
144
145
19.8k
    if (ret != Z_OK) {
146
0
        zip_error_set(ctx->error, ZIP_ER_ZLIB, ret);
147
0
        return false;
148
0
    }
149
150
151
19.8k
    return true;
152
19.8k
}
153
154
155
19.8k
static bool end(void *ud) {
156
19.8k
    struct ctx *ctx = (struct ctx *)ud;
157
19.8k
    int err;
158
159
19.8k
    if (ctx->compress) {
160
10.8k
        err = deflateEnd(&ctx->zstr);
161
10.8k
    }
162
8.98k
    else {
163
8.98k
        err = inflateEnd(&ctx->zstr);
164
8.98k
    }
165
166
19.8k
    if (err != Z_OK) {
167
0
        zip_error_set(ctx->error, ZIP_ER_ZLIB, err);
168
0
        return false;
169
0
    }
170
171
19.8k
    return true;
172
19.8k
}
173
174
175
10.3k
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length) {
176
10.3k
    struct ctx *ctx = (struct ctx *)ud;
177
178
10.3k
    if (length > UINT_MAX || ctx->zstr.avail_in > 0) {
179
0
        zip_error_set(ctx->error, ZIP_ER_INVAL, 0);
180
0
        return false;
181
0
    }
182
183
10.3k
    ctx->zstr.avail_in = (uInt)length;
184
10.3k
    ctx->zstr.next_in = (Bytef *)data;
185
186
10.3k
    return true;
187
10.3k
}
188
189
190
16.9k
static bool end_of_input(void *ud) {
191
16.9k
    struct ctx *ctx = (struct ctx *)ud;
192
193
16.9k
    ctx->end_of_input = true;
194
16.9k
    return ctx->zstr.avail_in != 0;
195
16.9k
}
196
197
198
57.1k
static zip_compression_status_t process(void *ud, zip_uint8_t *data, zip_uint64_t *length) {
199
57.1k
    struct ctx *ctx = (struct ctx *)ud;
200
57.1k
    uInt avail_out;
201
202
57.1k
    int ret;
203
204
57.1k
    avail_out = (uInt)ZIP_MIN(UINT_MAX, *length);
205
57.1k
    ctx->zstr.avail_out = avail_out;
206
57.1k
    ctx->zstr.next_out = (Bytef *)data;
207
208
57.1k
    if (ctx->compress) {
209
37.3k
        ret = deflate(&ctx->zstr, ctx->end_of_input ? Z_FINISH : 0);
210
37.3k
    }
211
19.8k
    else {
212
19.8k
        ret = inflate(&ctx->zstr, Z_SYNC_FLUSH);
213
19.8k
    }
214
215
57.1k
    *length = avail_out - ctx->zstr.avail_out;
216
217
57.1k
    switch (ret) {
218
14.9k
    case Z_OK:
219
14.9k
        return ZIP_COMPRESSION_OK;
220
221
16.6k
    case Z_STREAM_END:
222
16.6k
        return ZIP_COMPRESSION_END;
223
224
24.6k
    case Z_BUF_ERROR:
225
24.6k
        if (ctx->zstr.avail_in == 0) {
226
24.6k
            return ZIP_COMPRESSION_NEED_DATA;
227
24.6k
        }
228
229
        /* fallthrough */
230
231
923
    default:
232
923
        zip_error_set(ctx->error, ZIP_ER_ZLIB, ret);
233
923
        return ZIP_COMPRESSION_ERROR;
234
57.1k
    }
235
57.1k
}
236
237
/* clang-format off */
238
239
zip_compression_algorithm_t zip_algorithm_deflate_compress = {
240
    maximum_compressed_size,
241
    compress_allocate,
242
    deallocate,
243
    general_purpose_bit_flags,
244
    20,
245
    start,
246
    end,
247
    input,
248
    end_of_input,
249
    process
250
};
251
252
253
zip_compression_algorithm_t zip_algorithm_deflate_decompress = {
254
    maximum_compressed_size,
255
    decompress_allocate,
256
    deallocate,
257
    general_purpose_bit_flags,
258
    20,
259
    start,
260
    end,
261
    input,
262
    end_of_input,
263
    process
264
};
265
266
/* clang-format on */