Coverage Report

Created: 2025-11-09 06:16

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