Coverage Report

Created: 2026-06-02 06:56

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