Coverage Report

Created: 2026-08-11 07:29

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
7.04k
static void *allocate(bool compress, zip_uint32_t compression_flags, zip_error_t *error) {
63
7.04k
    struct ctx *ctx;
64
65
7.04k
    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
7.04k
    ctx->error = error;
71
7.04k
    ctx->compress = compress;
72
7.04k
    if (compression_flags >= 1 && compression_flags <= 9) {
73
0
        ctx->level = (int)compression_flags;
74
0
    }
75
7.04k
    else {
76
7.04k
        ctx->level = Z_BEST_COMPRESSION;
77
7.04k
    }
78
7.04k
    ctx->mem_level = compression_flags == TORRENTZIP_COMPRESSION_FLAGS ? TORRENTZIP_MEM_LEVEL : MAX_MEM_LEVEL;
79
7.04k
    ctx->end_of_input = false;
80
81
7.04k
    ctx->zstr.zalloc = Z_NULL;
82
7.04k
    ctx->zstr.zfree = Z_NULL;
83
7.04k
    ctx->zstr.opaque = NULL;
84
85
7.04k
    return ctx;
86
7.04k
}
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
7.04k
static void *decompress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) {
96
7.04k
    (void)method;
97
7.04k
    return allocate(false, compression_flags, error);
98
7.04k
}
99
100
101
7.04k
static void deallocate(void *ud) {
102
7.04k
    struct ctx *ctx = (struct ctx *)ud;
103
104
7.04k
    free(ctx);
105
7.04k
}
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
6.74k
static bool start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes) {
126
6.74k
    struct ctx *ctx = (struct ctx *)ud;
127
6.74k
    int ret;
128
129
6.74k
    (void)st;
130
6.74k
    (void)attributes;
131
132
6.74k
    ctx->zstr.avail_in = 0;
133
6.74k
    ctx->zstr.next_in = NULL;
134
6.74k
    ctx->zstr.avail_out = 0;
135
6.74k
    ctx->zstr.next_out = NULL;
136
137
6.74k
    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
6.74k
    else {
142
6.74k
        ret = inflateInit2(&ctx->zstr, -MAX_WBITS);
143
6.74k
    }
144
145
6.74k
    if (ret != Z_OK) {
146
0
        zip_error_set(ctx->error, ZIP_ER_ZLIB, ret);
147
0
        return false;
148
0
    }
149
150
151
6.74k
    return true;
152
6.74k
}
153
154
155
6.74k
static bool end(void *ud) {
156
6.74k
    struct ctx *ctx = (struct ctx *)ud;
157
6.74k
    int err;
158
159
6.74k
    if (ctx->compress) {
160
0
        err = deflateEnd(&ctx->zstr);
161
0
    }
162
6.74k
    else {
163
6.74k
        err = inflateEnd(&ctx->zstr);
164
6.74k
    }
165
166
6.74k
    if (err != Z_OK) {
167
0
        zip_error_set(ctx->error, ZIP_ER_ZLIB, err);
168
0
        return false;
169
0
    }
170
171
6.74k
    return true;
172
6.74k
}
173
174
175
6.66k
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length) {
176
6.66k
    struct ctx *ctx = (struct ctx *)ud;
177
178
6.66k
    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
6.66k
    ctx->zstr.avail_in = (uInt)length;
184
6.66k
    ctx->zstr.next_in = (Bytef *)data;
185
186
6.66k
    return true;
187
6.66k
}
188
189
190
3.98k
static bool end_of_input(void *ud) {
191
3.98k
    struct ctx *ctx = (struct ctx *)ud;
192
193
3.98k
    ctx->end_of_input = true;
194
3.98k
    return ctx->zstr.avail_in != 0;
195
3.98k
}
196
197
198
16.7k
static zip_compression_status_t process(void *ud, zip_uint8_t *data, zip_uint64_t *length) {
199
16.7k
    struct ctx *ctx = (struct ctx *)ud;
200
16.7k
    uInt avail_out;
201
202
16.7k
    int ret;
203
204
16.7k
    avail_out = (uInt)ZIP_MIN(UINT_MAX, *length);
205
16.7k
    ctx->zstr.avail_out = avail_out;
206
16.7k
    ctx->zstr.next_out = (Bytef *)data;
207
208
16.7k
    if (ctx->compress) {
209
0
        ret = deflate(&ctx->zstr, ctx->end_of_input ? Z_FINISH : 0);
210
0
    }
211
16.7k
    else {
212
16.7k
        ret = inflate(&ctx->zstr, Z_SYNC_FLUSH);
213
16.7k
    }
214
215
16.7k
    *length = avail_out - ctx->zstr.avail_out;
216
217
16.7k
    switch (ret) {
218
2.61k
    case Z_OK:
219
2.61k
        return ZIP_COMPRESSION_OK;
220
221
3.36k
    case Z_STREAM_END:
222
3.36k
        return ZIP_COMPRESSION_END;
223
224
9.12k
    case Z_BUF_ERROR:
225
9.12k
        if (ctx->zstr.avail_in == 0) {
226
9.12k
            return ZIP_COMPRESSION_NEED_DATA;
227
9.12k
        }
228
229
        /* fallthrough */
230
231
1.60k
    default:
232
1.60k
        zip_error_set(ctx->error, ZIP_ER_ZLIB, ret);
233
1.60k
        return ZIP_COMPRESSION_ERROR;
234
16.7k
    }
235
16.7k
}
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 */