Coverage Report

Created: 2025-08-03 07:00

/src/libzip/lib/zip_algorithm_zstd.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
  zip_algorithm_zstd.c -- zstd (de)compression routines
3
  Copyright (C) 2020-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 <stdlib.h>
37
#include <zstd.h>
38
#include <zstd_errors.h>
39
40
struct ctx {
41
    zip_error_t *error;
42
    bool compress;
43
    int compression_flags;
44
    bool end_of_input;
45
    ZSTD_DStream *zdstream;
46
    ZSTD_CStream *zcstream;
47
    ZSTD_outBuffer out;
48
    ZSTD_inBuffer in;
49
};
50
51
static zip_uint64_t
52
0
maximum_compressed_size(zip_uint64_t uncompressed_size) {
53
0
    return ZSTD_compressBound(uncompressed_size);
54
0
}
55
56
57
static void *
58
2.51k
allocate(bool compress, zip_uint32_t compression_flags, zip_error_t *error) {
59
2.51k
    struct ctx *ctx;
60
61
2.51k
    if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) {
62
0
        return NULL;
63
0
    }
64
65
2.51k
    ctx->compression_flags = (zip_int32_t)compression_flags;
66
2.51k
    if (ctx->compression_flags < ZSTD_minCLevel() || ctx->compression_flags > ZSTD_maxCLevel()) {
67
0
        ctx->compression_flags = 0; /* let zstd choose */
68
0
    }
69
70
2.51k
    ctx->error = error;
71
2.51k
    ctx->compress = compress;
72
2.51k
    ctx->end_of_input = false;
73
74
2.51k
    ctx->zdstream = NULL;
75
2.51k
    ctx->zcstream = NULL;
76
2.51k
    ctx->in.src = NULL;
77
2.51k
    ctx->in.pos = 0;
78
2.51k
    ctx->in.size = 0;
79
2.51k
    ctx->out.dst = NULL;
80
2.51k
    ctx->out.pos = 0;
81
2.51k
    ctx->out.size = 0;
82
83
2.51k
    return ctx;
84
2.51k
}
85
86
87
static void *
88
0
compress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) {
89
0
    (void)method;
90
0
    return allocate(true, compression_flags, error);
91
0
}
92
93
94
static void *
95
2.51k
decompress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) {
96
2.51k
    (void)method;
97
2.51k
    return allocate(false, compression_flags, error);
98
2.51k
}
99
100
101
static void
102
2.51k
deallocate(void *ud) {
103
2.51k
    struct ctx *ctx = (struct ctx *)ud;
104
2.51k
    free(ctx);
105
2.51k
}
106
107
108
static zip_uint16_t
109
0
general_purpose_bit_flags(void *ud) {
110
0
    (void)ud;
111
0
    return 0;
112
0
}
113
114
static int
115
1.00k
map_error(size_t ret) {
116
1.00k
    switch (ret) {
117
0
    case ZSTD_error_no_error:
118
0
        return ZIP_ER_OK;
119
120
0
    case ZSTD_error_corruption_detected:
121
0
    case ZSTD_error_checksum_wrong:
122
0
    case ZSTD_error_dictionary_corrupted:
123
0
    case ZSTD_error_dictionary_wrong:
124
0
        return ZIP_ER_COMPRESSED_DATA;
125
126
0
    case ZSTD_error_memory_allocation:
127
0
        return ZIP_ER_MEMORY;
128
129
0
    case ZSTD_error_parameter_unsupported:
130
0
    case ZSTD_error_parameter_outOfBound:
131
0
        return ZIP_ER_INVAL;
132
133
1.00k
    default:
134
1.00k
        return ZIP_ER_INTERNAL;
135
1.00k
    }
136
1.00k
}
137
138
139
static bool
140
2.04k
start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes) {
141
2.04k
    struct ctx *ctx = (struct ctx *)ud;
142
143
2.04k
    (void)st;
144
2.04k
    (void)attributes;
145
146
2.04k
    ctx->in.src = NULL;
147
2.04k
    ctx->in.pos = 0;
148
2.04k
    ctx->in.size = 0;
149
2.04k
    ctx->out.dst = NULL;
150
2.04k
    ctx->out.pos = 0;
151
2.04k
    ctx->out.size = 0;
152
2.04k
    if (ctx->compress) {
153
0
        size_t ret;
154
0
        ctx->zcstream = ZSTD_createCStream();
155
0
        if (ctx->zcstream == NULL) {
156
0
            zip_error_set(ctx->error, ZIP_ER_MEMORY, 0);
157
0
            return false;
158
0
        }
159
0
        ret = ZSTD_initCStream(ctx->zcstream, ctx->compression_flags);
160
0
        if (ZSTD_isError(ret)) {
161
0
            zip_error_set(ctx->error, ZIP_ER_ZLIB, map_error(ret));
162
0
            return false;
163
0
        }
164
0
    }
165
2.04k
    else {
166
2.04k
        ctx->zdstream = ZSTD_createDStream();
167
2.04k
        if (ctx->zdstream == NULL) {
168
0
            zip_error_set(ctx->error, ZIP_ER_MEMORY, 0);
169
0
            return false;
170
0
        }
171
2.04k
    }
172
173
2.04k
    return true;
174
2.04k
}
175
176
177
static bool
178
2.04k
end(void *ud) {
179
2.04k
    struct ctx *ctx = (struct ctx *)ud;
180
2.04k
    size_t ret;
181
182
2.04k
    if (ctx->compress) {
183
0
        ret = ZSTD_freeCStream(ctx->zcstream);
184
0
        ctx->zcstream = NULL;
185
0
    }
186
2.04k
    else {
187
2.04k
        ret = ZSTD_freeDStream(ctx->zdstream);
188
2.04k
        ctx->zdstream = NULL;
189
2.04k
    }
190
191
2.04k
    if (ZSTD_isError(ret)) {
192
0
        zip_error_set(ctx->error, map_error(ret), 0);
193
0
        return false;
194
0
    }
195
196
2.04k
    return true;
197
2.04k
}
198
199
200
static bool
201
1.58k
input(void *ud, zip_uint8_t *data, zip_uint64_t length) {
202
1.58k
    struct ctx *ctx = (struct ctx *)ud;
203
1.58k
    if (length > SIZE_MAX || ctx->in.pos != ctx->in.size) {
204
0
        zip_error_set(ctx->error, ZIP_ER_INVAL, 0);
205
0
        return false;
206
0
    }
207
1.58k
    ctx->in.src = (const void *)data;
208
1.58k
    ctx->in.size = (size_t)length;
209
1.58k
    ctx->in.pos = 0;
210
1.58k
    return true;
211
1.58k
}
212
213
214
585
static bool end_of_input(void *ud) {
215
585
    struct ctx *ctx = (struct ctx *)ud;
216
217
585
    ctx->end_of_input = true;
218
585
    return ctx->in.pos != ctx->in.size;
219
585
}
220
221
222
static zip_compression_status_t
223
6.83k
process(void *ud, zip_uint8_t *data, zip_uint64_t *length) {
224
6.83k
    struct ctx *ctx = (struct ctx *)ud;
225
226
6.83k
    size_t ret;
227
228
6.83k
    if (ctx->in.pos == ctx->in.size && !ctx->end_of_input) {
229
2.04k
        *length = 0;
230
2.04k
        return ZIP_COMPRESSION_NEED_DATA;
231
2.04k
    }
232
233
4.79k
    ctx->out.dst = data;
234
4.79k
    ctx->out.pos = 0;
235
4.79k
    ctx->out.size = ZIP_MIN(SIZE_MAX, *length);
236
237
4.79k
    if (ctx->compress) {
238
0
        if (ctx->in.pos == ctx->in.size && ctx->end_of_input) {
239
0
            ret = ZSTD_endStream(ctx->zcstream, &ctx->out);
240
0
            if (ret == 0) {
241
0
                *length = ctx->out.pos;
242
0
                return ZIP_COMPRESSION_END;
243
0
            }
244
0
        }
245
0
        else {
246
0
            ret = ZSTD_compressStream(ctx->zcstream, &ctx->out, &ctx->in);
247
0
        }
248
0
    }
249
4.79k
    else {
250
4.79k
        ret = ZSTD_decompressStream(ctx->zdstream, &ctx->out, &ctx->in);
251
4.79k
    }
252
4.79k
    if (ZSTD_isError(ret)) {
253
1.00k
        zip_error_set(ctx->error, map_error(ret), 0);
254
1.00k
        return ZIP_COMPRESSION_ERROR;
255
1.00k
    }
256
257
3.79k
    *length = ctx->out.pos;
258
3.79k
    if (ctx->in.pos == ctx->in.size) {
259
1.17k
        return ZIP_COMPRESSION_NEED_DATA;
260
1.17k
    }
261
262
2.61k
    return ZIP_COMPRESSION_OK;
263
3.79k
}
264
265
/* Version Required should be set to 63 (6.3) because this compression
266
   method was only defined in appnote.txt version 6.3.7, but Winzip
267
   does not unpack it if the value is not 20. */
268
269
/* clang-format off */
270
271
zip_compression_algorithm_t zip_algorithm_zstd_compress = {
272
    maximum_compressed_size,
273
    compress_allocate,
274
    deallocate,
275
    general_purpose_bit_flags,
276
    20,
277
    start,
278
    end,
279
    input,
280
    end_of_input,
281
    process
282
};
283
284
285
zip_compression_algorithm_t zip_algorithm_zstd_decompress = {
286
    maximum_compressed_size,
287
    decompress_allocate,
288
    deallocate,
289
    general_purpose_bit_flags,
290
    20,
291
    start,
292
    end,
293
    input,
294
    end_of_input,
295
    process
296
};
297
298
/* clang-format on */