Coverage Report

Created: 2025-07-23 08:18

/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
0
allocate(bool compress, zip_uint32_t compression_flags, zip_error_t *error) {
59
0
    struct ctx *ctx;
60
61
0
    if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) {
62
0
        return NULL;
63
0
    }
64
65
0
    ctx->compression_flags = (zip_int32_t)compression_flags;
66
0
    if (ctx->compression_flags < ZSTD_minCLevel() || ctx->compression_flags > ZSTD_maxCLevel()) {
67
0
        ctx->compression_flags = 0; /* let zstd choose */
68
0
    }
69
70
0
    ctx->error = error;
71
0
    ctx->compress = compress;
72
0
    ctx->end_of_input = false;
73
74
0
    ctx->zdstream = NULL;
75
0
    ctx->zcstream = NULL;
76
0
    ctx->in.src = NULL;
77
0
    ctx->in.pos = 0;
78
0
    ctx->in.size = 0;
79
0
    ctx->out.dst = NULL;
80
0
    ctx->out.pos = 0;
81
0
    ctx->out.size = 0;
82
83
0
    return ctx;
84
0
}
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
0
decompress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) {
96
0
    (void)method;
97
0
    return allocate(false, compression_flags, error);
98
0
}
99
100
101
static void
102
0
deallocate(void *ud) {
103
0
    struct ctx *ctx = (struct ctx *)ud;
104
0
    free(ctx);
105
0
}
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
0
map_error(size_t ret) {
116
0
    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
0
    default:
134
0
        return ZIP_ER_INTERNAL;
135
0
    }
136
0
}
137
138
139
static bool
140
0
start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes) {
141
0
    struct ctx *ctx = (struct ctx *)ud;
142
143
0
    (void)st;
144
0
    (void)attributes;
145
146
0
    ctx->in.src = NULL;
147
0
    ctx->in.pos = 0;
148
0
    ctx->in.size = 0;
149
0
    ctx->out.dst = NULL;
150
0
    ctx->out.pos = 0;
151
0
    ctx->out.size = 0;
152
0
    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
0
    else {
166
0
        ctx->zdstream = ZSTD_createDStream();
167
0
        if (ctx->zdstream == NULL) {
168
0
            zip_error_set(ctx->error, ZIP_ER_MEMORY, 0);
169
0
            return false;
170
0
        }
171
0
    }
172
173
0
    return true;
174
0
}
175
176
177
static bool
178
0
end(void *ud) {
179
0
    struct ctx *ctx = (struct ctx *)ud;
180
0
    size_t ret;
181
182
0
    if (ctx->compress) {
183
0
        ret = ZSTD_freeCStream(ctx->zcstream);
184
0
        ctx->zcstream = NULL;
185
0
    }
186
0
    else {
187
0
        ret = ZSTD_freeDStream(ctx->zdstream);
188
0
        ctx->zdstream = NULL;
189
0
    }
190
191
0
    if (ZSTD_isError(ret)) {
192
0
        zip_error_set(ctx->error, map_error(ret), 0);
193
0
        return false;
194
0
    }
195
196
0
    return true;
197
0
}
198
199
200
static bool
201
0
input(void *ud, zip_uint8_t *data, zip_uint64_t length) {
202
0
    struct ctx *ctx = (struct ctx *)ud;
203
0
    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
0
    ctx->in.src = (const void *)data;
208
0
    ctx->in.size = (size_t)length;
209
0
    ctx->in.pos = 0;
210
0
    return true;
211
0
}
212
213
214
0
static bool end_of_input(void *ud) {
215
0
    struct ctx *ctx = (struct ctx *)ud;
216
217
0
    ctx->end_of_input = true;
218
0
    return ctx->in.pos != ctx->in.size;
219
0
}
220
221
222
static zip_compression_status_t
223
0
process(void *ud, zip_uint8_t *data, zip_uint64_t *length) {
224
0
    struct ctx *ctx = (struct ctx *)ud;
225
226
0
    size_t ret;
227
228
0
    if (ctx->in.pos == ctx->in.size && !ctx->end_of_input) {
229
0
        *length = 0;
230
0
        return ZIP_COMPRESSION_NEED_DATA;
231
0
    }
232
233
0
    ctx->out.dst = data;
234
0
    ctx->out.pos = 0;
235
0
    ctx->out.size = ZIP_MIN(SIZE_MAX, *length);
236
237
0
    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
0
    else {
250
0
        ret = ZSTD_decompressStream(ctx->zdstream, &ctx->out, &ctx->in);
251
0
    }
252
0
    if (ZSTD_isError(ret)) {
253
0
        zip_error_set(ctx->error, map_error(ret), 0);
254
0
        return ZIP_COMPRESSION_ERROR;
255
0
    }
256
257
0
    *length = ctx->out.pos;
258
0
    if (ctx->in.pos == ctx->in.size) {
259
0
        return ZIP_COMPRESSION_NEED_DATA;
260
0
    }
261
262
0
    return ZIP_COMPRESSION_OK;
263
0
}
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 */