Coverage Report

Created: 2026-03-31 07:05

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