Coverage Report

Created: 2026-09-01 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_algorithm_xz.c
Line
Count
Source
1
/*
2
  zip_algorithm_xz.c -- LZMA/XZ (de)compression routines
3
  Bazed on zip_algorithm_deflate.c -- deflate (de)compression routines
4
  Copyright (C) 2017-2025 Dieter Baron and Thomas Klausner
5
6
  This file is part of libzip, a library to manipulate ZIP archives.
7
  The authors can be contacted at <info@libzip.org>
8
9
  Redistribution and use in source and binary forms, with or without
10
  modification, are permitted provided that the following conditions
11
  are met:
12
  1. Redistributions of source code must retain the above copyright
13
     notice, this list of conditions and the following disclaimer.
14
  2. Redistributions in binary form must reproduce the above copyright
15
     notice, this list of conditions and the following disclaimer in
16
     the documentation and/or other materials provided with the
17
     distribution.
18
  3. The names of the authors may not be used to endorse or promote
19
     products derived from this software without specific prior
20
     written permission.
21
22
  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
23
  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
26
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28
  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
30
  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32
  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
*/
34
35
#include "zipint.h"
36
37
#include <limits.h>
38
#include <lzma.h>
39
#include <stdlib.h>
40
#include <zlib.h>
41
42
enum header_state {
43
    INCOMPLETE,
44
    OUTPUT,
45
    DONE
46
};
47
48
8.30k
#define HEADER_BYTES_ZIP 9
49
2.01k
#define HEADER_MAGIC_LENGTH 4
50
#define HEADER_MAGIC1_OFFSET 0
51
12.7k
#define HEADER_MAGIC2_OFFSET 2
52
2.01k
#define HEADER_SIZE_OFFSET 9
53
4.03k
#define HEADER_SIZE_LENGTH 8
54
2.01k
#define HEADER_PARAMETERS_LENGTH 5
55
2.01k
#define HEADER_LZMA_ALONE_LENGTH (HEADER_PARAMETERS_LENGTH + HEADER_SIZE_LENGTH)
56
57
struct ctx {
58
    zip_error_t *error;
59
    bool compress;
60
    zip_uint32_t compression_flags;
61
    bool end_of_input;
62
    lzma_stream zstr;
63
    zip_uint16_t method;
64
    /* header member is used for converting from zip to "lzma alone"
65
     * format
66
     *
67
     * "lzma alone" file format starts with:
68
     * 5 bytes lzma parameters
69
     * 8 bytes uncompressed size
70
     * compressed data
71
     *
72
     * zip archive on-disk format starts with
73
     * 4 bytes magic (first two bytes vary, e.g. 0x0914 or 0x1002, next bytes are 0x0500)
74
     * 5 bytes lzma parameters
75
     * compressed data
76
     *
77
     * we read the data into a header of the form
78
     * 4 bytes magic
79
     * 5 bytes lzma parameters
80
     * 8 bytes uncompressed size
81
     */
82
    zip_uint8_t header[HEADER_MAGIC_LENGTH + HEADER_LZMA_ALONE_LENGTH];
83
    zip_uint8_t header_bytes_offset;
84
    enum header_state header_state;
85
    zip_uint64_t uncompresssed_size;
86
};
87
88
89
0
static zip_uint64_t maximum_compressed_size(zip_uint64_t uncompressed_size) {
90
    /*
91
     According to https://sourceforge.net/p/sevenzip/discussion/45797/thread/b6bd62f8/
92
93
     1) you can use
94
     outSize = 1.10 * originalSize + 64 KB.
95
     in most cases outSize is less then 1.02 from originalSize.
96
     2) You can try LZMA2, where
97
     outSize can be = 1.001 * originalSize + 1 KB.
98
     */
99
    /* 13 bytes added for lzma alone header */
100
0
    zip_uint64_t compressed_size = (zip_uint64_t)((double)uncompressed_size * 1.1) + 64 * 1024 + 13;
101
102
0
    if (compressed_size < uncompressed_size) {
103
0
        return ZIP_UINT64_MAX;
104
0
    }
105
0
    return compressed_size;
106
0
}
107
108
109
12.5k
static void *allocate(bool compress, zip_uint32_t compression_flags, zip_error_t *error, zip_uint16_t method) {
110
12.5k
    struct ctx *ctx;
111
112
12.5k
    if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) {
113
0
        zip_error_set(error, ZIP_ER_MEMORY, 0);
114
0
        return NULL;
115
0
    }
116
117
12.5k
    ctx->error = error;
118
12.5k
    ctx->compress = compress;
119
12.5k
    if (compression_flags <= 9) {
120
12.5k
        ctx->compression_flags = compression_flags;
121
12.5k
    }
122
0
    else {
123
0
        ctx->compression_flags = 6; /* default value */
124
0
    }
125
12.5k
    ctx->compression_flags |= LZMA_PRESET_EXTREME;
126
12.5k
    ctx->end_of_input = false;
127
12.5k
    memset(ctx->header, 0, sizeof(ctx->header));
128
12.5k
    ctx->header_bytes_offset = 0;
129
12.5k
    if (method == ZIP_CM_LZMA) {
130
10.4k
        ctx->header_state = INCOMPLETE;
131
10.4k
    }
132
2.10k
    else {
133
2.10k
        ctx->header_state = DONE;
134
2.10k
    }
135
12.5k
    memset(&ctx->zstr, 0, sizeof(ctx->zstr));
136
12.5k
    ctx->method = method;
137
12.5k
    return ctx;
138
12.5k
}
139
140
141
0
static void *compress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) {
142
0
    return allocate(true, compression_flags, error, method);
143
0
}
144
145
146
12.5k
static void *decompress_allocate(zip_uint16_t method, zip_uint32_t compression_flags, zip_error_t *error) {
147
12.5k
    return allocate(false, compression_flags, error, method);
148
12.5k
}
149
150
151
12.5k
static void deallocate(void *ud) {
152
12.5k
    struct ctx *ctx = (struct ctx *)ud;
153
12.5k
    free(ctx);
154
12.5k
}
155
156
157
0
static zip_uint16_t general_purpose_bit_flags(void *ud) {
158
0
    struct ctx *ctx = (struct ctx *)ud;
159
160
0
    if (!ctx->compress) {
161
0
        return 0;
162
0
    }
163
164
0
    if (ctx->method == ZIP_CM_LZMA) {
165
        /* liblzma always returns an EOS/EOPM marker, see
166
         * https://sourceforge.net/p/lzmautils/discussion/708858/thread/84c5dbb9/#a5e4/3764 */
167
0
        return 1 << 1;
168
0
    }
169
0
    return 0;
170
0
}
171
172
1.60k
static int map_error(lzma_ret ret) {
173
1.60k
    switch (ret) {
174
776
    case LZMA_DATA_ERROR:
175
776
    case LZMA_UNSUPPORTED_CHECK:
176
776
        return ZIP_ER_COMPRESSED_DATA;
177
178
0
    case LZMA_MEM_ERROR:
179
19
    case LZMA_MEMLIMIT_ERROR:
180
19
        return ZIP_ER_MEMORY;
181
182
0
    case LZMA_OPTIONS_ERROR:
183
0
        return ZIP_ER_INVAL;
184
185
807
    default:
186
807
        return ZIP_ER_INTERNAL;
187
1.60k
    }
188
1.60k
}
189
190
191
6.86k
static bool start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes) {
192
6.86k
    struct ctx *ctx = (struct ctx *)ud;
193
6.86k
    lzma_ret ret;
194
195
6.86k
    lzma_options_lzma opt_lzma;
196
6.86k
    lzma_lzma_preset(&opt_lzma, ctx->compression_flags);
197
6.86k
    lzma_filter filters[] = {
198
6.86k
        {.id = (ctx->method == ZIP_CM_LZMA ? LZMA_FILTER_LZMA1 : LZMA_FILTER_LZMA2), .options = &opt_lzma},
199
6.86k
        {.id = LZMA_VLI_UNKNOWN, .options = NULL},
200
6.86k
    };
201
202
6.86k
    ctx->zstr.avail_in = 0;
203
6.86k
    ctx->zstr.next_in = NULL;
204
6.86k
    ctx->zstr.avail_out = 0;
205
6.86k
    ctx->zstr.next_out = NULL;
206
207
6.86k
    if (ctx->compress) {
208
0
        if (ctx->method == ZIP_CM_LZMA) {
209
0
            ret = lzma_alone_encoder(&ctx->zstr, filters[0].options);
210
0
        }
211
0
        else {
212
0
            ret = lzma_stream_encoder(&ctx->zstr, filters, LZMA_CHECK_CRC64);
213
0
        }
214
0
    }
215
6.86k
    else {
216
6.86k
        if (ctx->method == ZIP_CM_LZMA) {
217
5.83k
            ret = lzma_alone_decoder(&ctx->zstr, LZMA_WINDOW_LIMIT);
218
5.83k
        }
219
1.03k
        else {
220
1.03k
            ret = lzma_stream_decoder(&ctx->zstr, LZMA_WINDOW_LIMIT, LZMA_CONCATENATED);
221
1.03k
        }
222
6.86k
    }
223
224
6.86k
    if (ret != LZMA_OK) {
225
0
        zip_error_set(ctx->error, map_error(ret), 0);
226
0
        return false;
227
0
    }
228
229
    /* If general purpose bits 1 & 2 are both zero, write real uncompressed size in header. */
230
6.86k
    if ((attributes->valid & ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS) && (attributes->general_purpose_bit_mask & 0x6) == 0x6 && (attributes->general_purpose_bit_flags & 0x06) == 0 && (st->valid & ZIP_STAT_SIZE)) {
231
4.79k
        ctx->uncompresssed_size = st->size;
232
4.79k
    }
233
2.07k
    else {
234
2.07k
        ctx->uncompresssed_size = ZIP_UINT64_MAX;
235
2.07k
    }
236
237
6.86k
    return true;
238
6.86k
}
239
240
241
6.86k
static bool end(void *ud) {
242
6.86k
    struct ctx *ctx = (struct ctx *)ud;
243
244
6.86k
    lzma_end(&ctx->zstr);
245
6.86k
    return true;
246
6.86k
}
247
248
249
9.54k
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length) {
250
9.54k
    struct ctx *ctx = (struct ctx *)ud;
251
252
9.54k
    if (length > UINT_MAX || ctx->zstr.avail_in > 0) {
253
0
        zip_error_set(ctx->error, ZIP_ER_INVAL, 0);
254
0
        return false;
255
0
    }
256
257
    /* For decompression of LZMA1: Have we read the full "lzma alone" header yet? */
258
9.54k
    if (ctx->method == ZIP_CM_LZMA && !ctx->compress && ctx->header_state == INCOMPLETE) {
259
        /* if not, get more of the data */
260
8.30k
        zip_uint8_t got = (zip_uint8_t)ZIP_MIN(HEADER_BYTES_ZIP - ctx->header_bytes_offset, length);
261
8.30k
        (void)memcpy_s(ctx->header + ctx->header_bytes_offset, sizeof(ctx->header) - ctx->header_bytes_offset, data, got);
262
8.30k
        ctx->header_bytes_offset += got;
263
8.30k
        length -= got;
264
8.30k
        data += got;
265
        /* Do we have a complete header now? */
266
8.30k
        if (ctx->header_bytes_offset == HEADER_BYTES_ZIP) {
267
7.85k
            Bytef empty_buffer[1];
268
7.85k
            zip_buffer_t *buffer;
269
            /* check magic */
270
7.85k
            if (ctx->header[HEADER_MAGIC2_OFFSET] != 0x05 || ctx->header[HEADER_MAGIC2_OFFSET + 1] != 0x00) {
271
                /* magic does not match */
272
5.83k
                zip_error_set(ctx->error, ZIP_ER_COMPRESSED_DATA, 0);
273
5.83k
                return false;
274
5.83k
            }
275
            /* set size of uncompressed data in "lzma alone" header to "unknown" */
276
2.01k
            if ((buffer = _zip_buffer_new(ctx->header + HEADER_SIZE_OFFSET, HEADER_SIZE_LENGTH)) == NULL) {
277
0
                zip_error_set(ctx->error, ZIP_ER_MEMORY, 0);
278
0
                return false;
279
0
            }
280
2.01k
            _zip_buffer_put_64(buffer, ctx->uncompresssed_size);
281
2.01k
            _zip_buffer_free(buffer);
282
            /* Feed header into "lzma alone" decoder, for
283
             * initialization; this should not produce output. */
284
2.01k
            ctx->zstr.next_in = (void *)(ctx->header + HEADER_MAGIC_LENGTH);
285
2.01k
            ctx->zstr.avail_in = HEADER_LZMA_ALONE_LENGTH;
286
2.01k
            ctx->zstr.total_in = 0;
287
2.01k
            ctx->zstr.next_out = empty_buffer;
288
2.01k
            ctx->zstr.avail_out = sizeof(*empty_buffer);
289
2.01k
            ctx->zstr.total_out = 0;
290
            /* this just initializes the decoder and does not produce output, so it consumes the complete header */
291
2.01k
            if (lzma_code(&ctx->zstr, LZMA_RUN) != LZMA_OK || ctx->zstr.total_out > 0) {
292
445
                zip_error_set(ctx->error, ZIP_ER_COMPRESSED_DATA, 0);
293
445
                return false;
294
445
            }
295
1.57k
            ctx->header_state = DONE;
296
1.57k
        }
297
8.30k
    }
298
3.26k
    ctx->zstr.avail_in = (uInt)length;
299
3.26k
    ctx->zstr.next_in = (Bytef *)data;
300
301
3.26k
    return true;
302
9.54k
}
303
304
305
1.96k
static bool end_of_input(void *ud) {
306
1.96k
    struct ctx *ctx = (struct ctx *)ud;
307
308
1.96k
    ctx->end_of_input = true;
309
1.96k
    return ctx->zstr.avail_in != 0;
310
1.96k
}
311
312
313
33.0k
static zip_compression_status_t process(void *ud, zip_uint8_t *data, zip_uint64_t *length) {
314
33.0k
    struct ctx *ctx = (struct ctx *)ud;
315
33.0k
    uInt avail_out;
316
33.0k
    lzma_ret ret;
317
    /* for compression of LZMA1 */
318
33.0k
    if (ctx->method == ZIP_CM_LZMA && ctx->compress) {
319
0
        if (ctx->header_state == INCOMPLETE) {
320
            /* write magic to output buffer */
321
0
            ctx->header[0] = 0x09;
322
0
            ctx->header[1] = 0x14;
323
0
            ctx->header[2] = 0x05;
324
0
            ctx->header[3] = 0x00;
325
            /* generate lzma parameters into output buffer */
326
0
            ctx->zstr.avail_out = HEADER_LZMA_ALONE_LENGTH;
327
0
            ctx->zstr.next_out = ctx->header + HEADER_MAGIC_LENGTH;
328
0
            ret = lzma_code(&ctx->zstr, LZMA_RUN);
329
0
            if (ret != LZMA_OK || ctx->zstr.avail_out != 0) {
330
                /* assume that the whole header will be provided with the first call to lzma_code */
331
0
                return ZIP_COMPRESSION_ERROR;
332
0
            }
333
0
            ctx->header_state = OUTPUT;
334
0
        }
335
0
        if (ctx->header_state == OUTPUT) {
336
            /* write header */
337
0
            zip_uint8_t write_len = (zip_uint8_t)ZIP_MIN(HEADER_BYTES_ZIP - ctx->header_bytes_offset, *length);
338
0
            (void)memcpy_s(data, *length, ctx->header + ctx->header_bytes_offset, write_len);
339
0
            ctx->header_bytes_offset += write_len;
340
0
            *length = write_len;
341
0
            if (ctx->header_bytes_offset == HEADER_BYTES_ZIP) {
342
0
                ctx->header_state = DONE;
343
0
            }
344
0
            return ZIP_COMPRESSION_OK;
345
0
        }
346
0
    }
347
348
33.0k
    avail_out = (uInt)ZIP_MIN(UINT_MAX, *length);
349
33.0k
    ctx->zstr.avail_out = avail_out;
350
33.0k
    ctx->zstr.next_out = (Bytef *)data;
351
352
33.0k
    ret = lzma_code(&ctx->zstr, ctx->end_of_input ? LZMA_FINISH : LZMA_RUN);
353
33.0k
    *length = avail_out - ctx->zstr.avail_out;
354
355
33.0k
    switch (ret) {
356
14.7k
    case LZMA_OK:
357
14.7k
        return ZIP_COMPRESSION_OK;
358
359
696
    case LZMA_STREAM_END:
360
696
        return ZIP_COMPRESSION_END;
361
362
16.0k
    case LZMA_BUF_ERROR:
363
16.0k
        if (ctx->zstr.avail_in == 0) {
364
16.0k
            return ZIP_COMPRESSION_NEED_DATA;
365
16.0k
        }
366
367
        /* fallthrough */
368
1.60k
    default:
369
1.60k
        zip_error_set(ctx->error, map_error(ret), 0);
370
1.60k
        return ZIP_COMPRESSION_ERROR;
371
33.0k
    }
372
33.0k
}
373
374
/* Version Required should be set to 63 (6.3) because this compression
375
   method was only defined in appnote.txt version 6.3.8, but Winzip
376
   does not unpack it if the value is not 20. */
377
378
/* clang-format off */
379
380
zip_compression_algorithm_t zip_algorithm_xz_compress = {
381
    maximum_compressed_size,
382
    compress_allocate,
383
    deallocate,
384
    general_purpose_bit_flags,
385
    20,
386
    start,
387
    end,
388
    input,
389
    end_of_input,
390
    process
391
};
392
393
394
zip_compression_algorithm_t zip_algorithm_xz_decompress = {
395
    maximum_compressed_size,
396
    decompress_allocate,
397
    deallocate,
398
    general_purpose_bit_flags,
399
    20,
400
    start,
401
    end,
402
    input,
403
    end_of_input,
404
    process
405
};
406
407
/* clang-format on */