Coverage Report

Created: 2026-04-01 07:49

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
0
static void *allocate(bool compress, zip_uint32_t compression_flags, zip_error_t *error) {
57
0
    struct ctx *ctx;
58
59
0
    if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) {
60
0
        return NULL;
61
0
    }
62
63
0
    ctx->compression_flags = (zip_int32_t)compression_flags;
64
0
    if (ctx->compression_flags < ZSTD_minCLevel() || ctx->compression_flags > ZSTD_maxCLevel()) {
65
0
        ctx->compression_flags = 0; /* let zstd choose */
66
0
    }
67
68
0
    ctx->error = error;
69
0
    ctx->compress = compress;
70
0
    ctx->end_of_input = false;
71
72
0
    ctx->zdstream = NULL;
73
0
    ctx->zcstream = NULL;
74
0
    ctx->in.src = NULL;
75
0
    ctx->in.pos = 0;
76
0
    ctx->in.size = 0;
77
0
    ctx->out.dst = NULL;
78
0
    ctx->out.pos = 0;
79
0
    ctx->out.size = 0;
80
81
0
    return ctx;
82
0
}
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
0
static void *decompress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) {
92
0
    (void)method;
93
0
    return allocate(false, compression_flags, error);
94
0
}
95
96
97
0
static void deallocate(void *ud) {
98
0
    struct ctx *ctx = (struct ctx *)ud;
99
0
    free(ctx);
100
0
}
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
0
static int map_error(size_t ret) {
109
0
    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
0
    default:
127
0
        return ZIP_ER_INTERNAL;
128
0
    }
129
0
}
130
131
132
0
static bool start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes) {
133
0
    struct ctx *ctx = (struct ctx *)ud;
134
135
0
    (void)st;
136
0
    (void)attributes;
137
138
0
    ctx->in.src = NULL;
139
0
    ctx->in.pos = 0;
140
0
    ctx->in.size = 0;
141
0
    ctx->out.dst = NULL;
142
0
    ctx->out.pos = 0;
143
0
    ctx->out.size = 0;
144
0
    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
0
    else {
158
0
        ctx->zdstream = ZSTD_createDStream();
159
0
        if (ctx->zdstream == NULL) {
160
0
            zip_error_set(ctx->error, ZIP_ER_MEMORY, 0);
161
0
            return false;
162
0
        }
163
0
    }
164
165
0
    return true;
166
0
}
167
168
169
0
static bool end(void *ud) {
170
0
    struct ctx *ctx = (struct ctx *)ud;
171
0
    size_t ret;
172
173
0
    if (ctx->compress) {
174
0
        ret = ZSTD_freeCStream(ctx->zcstream);
175
0
        ctx->zcstream = NULL;
176
0
    }
177
0
    else {
178
0
        ret = ZSTD_freeDStream(ctx->zdstream);
179
0
        ctx->zdstream = NULL;
180
0
    }
181
182
0
    if (ZSTD_isError(ret)) {
183
0
        zip_error_set(ctx->error, map_error(ret), 0);
184
0
        return false;
185
0
    }
186
187
0
    return true;
188
0
}
189
190
191
0
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length) {
192
0
    struct ctx *ctx = (struct ctx *)ud;
193
0
    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
0
    ctx->in.src = (const void *)data;
198
0
    ctx->in.size = (size_t)length;
199
0
    ctx->in.pos = 0;
200
0
    return true;
201
0
}
202
203
204
0
static bool end_of_input(void *ud) {
205
0
    struct ctx *ctx = (struct ctx *)ud;
206
207
0
    ctx->end_of_input = true;
208
0
    return ctx->in.pos != ctx->in.size;
209
0
}
210
211
212
0
static zip_compression_status_t process(void *ud, zip_uint8_t *data, zip_uint64_t *length) {
213
0
    struct ctx *ctx = (struct ctx *)ud;
214
215
0
    size_t ret;
216
217
0
    if (ctx->in.pos == ctx->in.size && !ctx->end_of_input) {
218
0
        *length = 0;
219
0
        return ZIP_COMPRESSION_NEED_DATA;
220
0
    }
221
222
0
    ctx->out.dst = data;
223
0
    ctx->out.pos = 0;
224
0
    ctx->out.size = ZIP_MIN(SIZE_MAX, *length);
225
226
0
    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
0
    else {
239
0
        ret = ZSTD_decompressStream(ctx->zdstream, &ctx->out, &ctx->in);
240
0
    }
241
0
    if (ZSTD_isError(ret)) {
242
0
        zip_error_set(ctx->error, map_error(ret), 0);
243
0
        return ZIP_COMPRESSION_ERROR;
244
0
    }
245
246
0
    *length = ctx->out.pos;
247
0
    if (ctx->in.pos == ctx->in.size) {
248
0
        return ZIP_COMPRESSION_NEED_DATA;
249
0
    }
250
251
0
    return ZIP_COMPRESSION_OK;
252
0
}
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 */