Coverage Report

Created: 2026-07-30 07:17

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
0
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
0
    zip_uint64_t compressed_size = uncompressed_size + (uncompressed_size + 16383) / 16384 * 5 + 6;
54
55
0
    if (compressed_size < uncompressed_size) {
56
0
        return ZIP_UINT64_MAX;
57
0
    }
58
0
    return compressed_size;
59
0
}
60
61
62
1.12k
static void *allocate(bool compress, zip_uint32_t compression_flags, zip_error_t *error) {
63
1.12k
    struct ctx *ctx;
64
65
1.12k
    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
1.12k
    ctx->error = error;
71
1.12k
    ctx->compress = compress;
72
1.12k
    if (compression_flags >= 1 && compression_flags <= 9) {
73
0
        ctx->level = (int)compression_flags;
74
0
    }
75
1.12k
    else {
76
1.12k
        ctx->level = Z_BEST_COMPRESSION;
77
1.12k
    }
78
1.12k
    ctx->mem_level = compression_flags == TORRENTZIP_COMPRESSION_FLAGS ? TORRENTZIP_MEM_LEVEL : MAX_MEM_LEVEL;
79
1.12k
    ctx->end_of_input = false;
80
81
1.12k
    ctx->zstr.zalloc = Z_NULL;
82
1.12k
    ctx->zstr.zfree = Z_NULL;
83
1.12k
    ctx->zstr.opaque = NULL;
84
85
1.12k
    return ctx;
86
1.12k
}
87
88
89
0
static void *compress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) {
90
0
    (void)method;
91
0
    return allocate(true, compression_flags, error);
92
0
}
93
94
95
1.12k
static void *decompress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) {
96
1.12k
    (void)method;
97
1.12k
    return allocate(false, compression_flags, error);
98
1.12k
}
99
100
101
1.12k
static void deallocate(void *ud) {
102
1.12k
    struct ctx *ctx = (struct ctx *)ud;
103
104
1.12k
    free(ctx);
105
1.12k
}
106
107
108
0
static zip_uint16_t general_purpose_bit_flags(void *ud) {
109
0
    struct ctx *ctx = (struct ctx *)ud;
110
111
0
    if (!ctx->compress) {
112
0
        return 0;
113
0
    }
114
115
0
    if (ctx->level < 3) {
116
0
        return 2 << 1;
117
0
    }
118
0
    else if (ctx->level > 7) {
119
0
        return 1 << 1;
120
0
    }
121
0
    return 0;
122
0
}
123
124
125
1.11k
static bool start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes) {
126
1.11k
    struct ctx *ctx = (struct ctx *)ud;
127
1.11k
    int ret;
128
129
1.11k
    (void)st;
130
1.11k
    (void)attributes;
131
132
1.11k
    ctx->zstr.avail_in = 0;
133
1.11k
    ctx->zstr.next_in = NULL;
134
1.11k
    ctx->zstr.avail_out = 0;
135
1.11k
    ctx->zstr.next_out = NULL;
136
137
1.11k
    if (ctx->compress) {
138
        /* negative value to tell zlib not to write a header */
139
0
        ret = deflateInit2(&ctx->zstr, ctx->level, Z_DEFLATED, -MAX_WBITS, ctx->mem_level, Z_DEFAULT_STRATEGY);
140
0
    }
141
1.11k
    else {
142
1.11k
        ret = inflateInit2(&ctx->zstr, -MAX_WBITS);
143
1.11k
    }
144
145
1.11k
    if (ret != Z_OK) {
146
0
        zip_error_set(ctx->error, ZIP_ER_ZLIB, ret);
147
0
        return false;
148
0
    }
149
150
151
1.11k
    return true;
152
1.11k
}
153
154
155
1.11k
static bool end(void *ud) {
156
1.11k
    struct ctx *ctx = (struct ctx *)ud;
157
1.11k
    int err;
158
159
1.11k
    if (ctx->compress) {
160
0
        err = deflateEnd(&ctx->zstr);
161
0
    }
162
1.11k
    else {
163
1.11k
        err = inflateEnd(&ctx->zstr);
164
1.11k
    }
165
166
1.11k
    if (err != Z_OK) {
167
0
        zip_error_set(ctx->error, ZIP_ER_ZLIB, err);
168
0
        return false;
169
0
    }
170
171
1.11k
    return true;
172
1.11k
}
173
174
175
1.10k
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length) {
176
1.10k
    struct ctx *ctx = (struct ctx *)ud;
177
178
1.10k
    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
1.10k
    ctx->zstr.avail_in = (uInt)length;
184
1.10k
    ctx->zstr.next_in = (Bytef *)data;
185
186
1.10k
    return true;
187
1.10k
}
188
189
190
743
static bool end_of_input(void *ud) {
191
743
    struct ctx *ctx = (struct ctx *)ud;
192
193
743
    ctx->end_of_input = true;
194
743
    return ctx->zstr.avail_in != 0;
195
743
}
196
197
198
3.11k
static zip_compression_status_t process(void *ud, zip_uint8_t *data, zip_uint64_t *length) {
199
3.11k
    struct ctx *ctx = (struct ctx *)ud;
200
3.11k
    uInt avail_out;
201
202
3.11k
    int ret;
203
204
3.11k
    avail_out = (uInt)ZIP_MIN(UINT_MAX, *length);
205
3.11k
    ctx->zstr.avail_out = avail_out;
206
3.11k
    ctx->zstr.next_out = (Bytef *)data;
207
208
3.11k
    if (ctx->compress) {
209
0
        ret = deflate(&ctx->zstr, ctx->end_of_input ? Z_FINISH : 0);
210
0
    }
211
3.11k
    else {
212
3.11k
        ret = inflate(&ctx->zstr, Z_SYNC_FLUSH);
213
3.11k
    }
214
215
3.11k
    *length = avail_out - ctx->zstr.avail_out;
216
217
3.11k
    switch (ret) {
218
704
    case Z_OK:
219
704
        return ZIP_COMPRESSION_OK;
220
221
532
    case Z_STREAM_END:
222
532
        return ZIP_COMPRESSION_END;
223
224
1.76k
    case Z_BUF_ERROR:
225
1.76k
        if (ctx->zstr.avail_in == 0) {
226
1.76k
            return ZIP_COMPRESSION_NEED_DATA;
227
1.76k
        }
228
229
        /* fallthrough */
230
231
114
    default:
232
114
        zip_error_set(ctx->error, ZIP_ER_ZLIB, ret);
233
114
        return ZIP_COMPRESSION_ERROR;
234
3.11k
    }
235
3.11k
}
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 */