• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 15430420074

03 Jun 2025 10:29PM UTC coverage: 72.013% (-0.03%) from 72.041%
15430420074

push

github

yuwata
doc: fix integration tests guide reference

299598 of 416033 relevant lines covered (72.01%)

700977.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.83
/src/basic/compress.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdio.h>
4
#include <sys/mman.h>
5
#include <sys/stat.h>
6
#include <unistd.h>
7

8
#if HAVE_LZ4
9
#include <lz4.h>
10
#include <lz4hc.h>
11
#include <lz4frame.h>
12
#endif
13

14
#if HAVE_XZ
15
#include <lzma.h>
16
#endif
17

18
#if HAVE_ZSTD
19
#include <zstd.h>
20
#include <zstd_errors.h>
21
#endif
22

23
#include "alloc-util.h"
24
#include "bitfield.h"
25
#include "compress.h"
26
#include "dlfcn-util.h"
27
#include "fileio.h"
28
#include "io-util.h"
29
#include "log.h"
30
#include "string-table.h"
31
#include "string-util.h"
32
#include "unaligned.h"
33

34
#if HAVE_LZ4
35
static void *lz4_dl = NULL;
36

37
static DLSYM_PROTOTYPE(LZ4F_compressBegin) = NULL;
38
static DLSYM_PROTOTYPE(LZ4F_compressBound) = NULL;
39
static DLSYM_PROTOTYPE(LZ4F_compressEnd) = NULL;
40
static DLSYM_PROTOTYPE(LZ4F_compressUpdate) = NULL;
41
static DLSYM_PROTOTYPE(LZ4F_createCompressionContext) = NULL;
42
static DLSYM_PROTOTYPE(LZ4F_createDecompressionContext) = NULL;
43
static DLSYM_PROTOTYPE(LZ4F_decompress) = NULL;
44
static DLSYM_PROTOTYPE(LZ4F_freeCompressionContext) = NULL;
45
static DLSYM_PROTOTYPE(LZ4F_freeDecompressionContext) = NULL;
46
static DLSYM_PROTOTYPE(LZ4F_isError) = NULL;
47
static DLSYM_PROTOTYPE(LZ4_compress_HC) = NULL;
48
/* These are used in test-compress.c so we don't make them static. */
49
DLSYM_PROTOTYPE(LZ4_compress_default) = NULL;
50
DLSYM_PROTOTYPE(LZ4_decompress_safe) = NULL;
51
DLSYM_PROTOTYPE(LZ4_decompress_safe_partial) = NULL;
52
DLSYM_PROTOTYPE(LZ4_versionNumber) = NULL;
53

54
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(LZ4F_compressionContext_t, sym_LZ4F_freeCompressionContext, NULL);
1✔
55
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(LZ4F_decompressionContext_t, sym_LZ4F_freeDecompressionContext, NULL);
3✔
56
#endif
57

58
#if HAVE_ZSTD
59
static void *zstd_dl = NULL;
60

61
static DLSYM_PROTOTYPE(ZSTD_CCtx_setParameter) = NULL;
62
static DLSYM_PROTOTYPE(ZSTD_compress) = NULL;
63
static DLSYM_PROTOTYPE(ZSTD_compressStream2) = NULL;
64
static DLSYM_PROTOTYPE(ZSTD_createCCtx) = NULL;
65
static DLSYM_PROTOTYPE(ZSTD_createDCtx) = NULL;
66
static DLSYM_PROTOTYPE(ZSTD_CStreamInSize) = NULL;
67
static DLSYM_PROTOTYPE(ZSTD_CStreamOutSize) = NULL;
68
static DLSYM_PROTOTYPE(ZSTD_decompressStream) = NULL;
69
static DLSYM_PROTOTYPE(ZSTD_DStreamInSize) = NULL;
70
static DLSYM_PROTOTYPE(ZSTD_DStreamOutSize) = NULL;
71
static DLSYM_PROTOTYPE(ZSTD_freeCCtx) = NULL;
72
static DLSYM_PROTOTYPE(ZSTD_freeDCtx) = NULL;
73
static DLSYM_PROTOTYPE(ZSTD_getErrorCode) = NULL;
74
static DLSYM_PROTOTYPE(ZSTD_getErrorName) = NULL;
75
static DLSYM_PROTOTYPE(ZSTD_getFrameContentSize) = NULL;
76
static DLSYM_PROTOTYPE(ZSTD_isError) = NULL;
77

78
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(ZSTD_CCtx*, sym_ZSTD_freeCCtx, NULL);
25✔
79
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(ZSTD_DCtx*, sym_ZSTD_freeDCtx, NULL);
245,750✔
80

81
static int zstd_ret_to_errno(size_t ret) {
5✔
82
        switch (sym_ZSTD_getErrorCode(ret)) {
5✔
83
        case ZSTD_error_dstSize_tooSmall:
84
                return -ENOBUFS;
85
        case ZSTD_error_memory_allocation:
×
86
                return -ENOMEM;
×
87
        default:
1✔
88
                return -EBADMSG;
1✔
89
        }
90
}
91
#endif
92

93
#if HAVE_XZ
94
static void *lzma_dl = NULL;
95

96
static DLSYM_PROTOTYPE(lzma_code) = NULL;
97
static DLSYM_PROTOTYPE(lzma_easy_encoder) = NULL;
98
static DLSYM_PROTOTYPE(lzma_end) = NULL;
99
static DLSYM_PROTOTYPE(lzma_stream_buffer_encode) = NULL;
100
static DLSYM_PROTOTYPE(lzma_stream_decoder) = NULL;
101
static DLSYM_PROTOTYPE(lzma_lzma_preset) = NULL;
102

103
/* We can't just do _cleanup_(sym_lzma_end) because a compiler bug makes
104
 * this fail with:
105
 * ../src/basic/compress.c: In function ‘decompress_blob_xz’:
106
 * ../src/basic/compress.c:304:9: error: cleanup argument not a function
107
 *   304 |         _cleanup_(sym_lzma_end) lzma_stream s = LZMA_STREAM_INIT;
108
 *       |         ^~~~~~~~~
109
 */
110
static inline void lzma_end_wrapper(lzma_stream *ls) {
2,807✔
111
        sym_lzma_end(ls);
2,807✔
112
}
113
#endif
114

115
#define ALIGN_8(l) ALIGN_TO(l, sizeof(size_t))
116

117
static const char* const compression_table[_COMPRESSION_MAX] = {
118
        [COMPRESSION_NONE] = "NONE",
119
        [COMPRESSION_XZ]   = "XZ",
120
        [COMPRESSION_LZ4]  = "LZ4",
121
        [COMPRESSION_ZSTD] = "ZSTD",
122
};
123

124
static const char* const compression_lowercase_table[_COMPRESSION_MAX] = {
125
        [COMPRESSION_NONE] = "none",
126
        [COMPRESSION_XZ]   = "xz",
127
        [COMPRESSION_LZ4]  = "lz4",
128
        [COMPRESSION_ZSTD] = "zstd",
129
};
130

131
DEFINE_STRING_TABLE_LOOKUP(compression, Compression);
538✔
132
DEFINE_STRING_TABLE_LOOKUP(compression_lowercase, Compression);
83✔
133

134
bool compression_supported(Compression c) {
2,687✔
135
        static const unsigned supported =
2,687✔
136
                (1U << COMPRESSION_NONE) |
137
                (1U << COMPRESSION_XZ) * HAVE_XZ |
138
                (1U << COMPRESSION_LZ4) * HAVE_LZ4 |
139
                (1U << COMPRESSION_ZSTD) * HAVE_ZSTD;
140

141
        assert(c >= 0);
2,687✔
142
        assert(c < _COMPRESSION_MAX);
2,687✔
143

144
        return BIT_SET(supported, c);
2,687✔
145
}
146

147
#if HAVE_XZ
148
int dlopen_lzma(void) {
2,825✔
149
        ELF_NOTE_DLOPEN("lzma",
2,825✔
150
                        "Support lzma compression in journal and coredump files",
151
                        COMPRESSION_PRIORITY_XZ,
152
                        "liblzma.so.5");
153

154
        return dlopen_many_sym_or_warn(
2,825✔
155
                        &lzma_dl,
156
                        "liblzma.so.5", LOG_DEBUG,
157
                        DLSYM_ARG(lzma_code),
158
                        DLSYM_ARG(lzma_easy_encoder),
159
                        DLSYM_ARG(lzma_end),
160
                        DLSYM_ARG(lzma_stream_buffer_encode),
161
                        DLSYM_ARG(lzma_lzma_preset),
162
                        DLSYM_ARG(lzma_stream_decoder));
163
}
164
#endif
165

166
int compress_blob_xz(const void *src, uint64_t src_size,
17✔
167
                     void *dst, size_t dst_alloc_size, size_t *dst_size, int level) {
168

169
        assert(src);
17✔
170
        assert(src_size > 0);
17✔
171
        assert(dst);
17✔
172
        assert(dst_alloc_size > 0);
17✔
173
        assert(dst_size);
17✔
174

175
#if HAVE_XZ
176
        lzma_options_lzma opt = {
17✔
177
                1u << 20u, NULL, 0, LZMA_LC_DEFAULT, LZMA_LP_DEFAULT,
178
                LZMA_PB_DEFAULT, LZMA_MODE_FAST, 128, LZMA_MF_HC3, 4
179
        };
180
        lzma_filter filters[] = {
17✔
181
                { LZMA_FILTER_LZMA2, &opt },
182
                { LZMA_VLI_UNKNOWN, NULL }
183
        };
184
        lzma_ret ret;
17✔
185
        size_t out_pos = 0;
17✔
186
        int r;
17✔
187

188
        r = dlopen_lzma();
17✔
189
        if (r < 0)
17✔
190
                return r;
17✔
191

192
        if (level >= 0) {
17✔
193
                r = sym_lzma_lzma_preset(&opt, (uint32_t) level);
×
194
                if (r < 0)
195
                        return r;
196
        }
197

198
        /* Returns < 0 if we couldn't compress the data or the
199
         * compressed result is longer than the original */
200

201
        if (src_size < 80)
17✔
202
                return -ENOBUFS;
203

204
        ret = sym_lzma_stream_buffer_encode(filters, LZMA_CHECK_NONE, NULL,
17✔
205
                                        src, src_size, dst, &out_pos, dst_alloc_size);
206
        if (ret != LZMA_OK)
17✔
207
                return -ENOBUFS;
208

209
        *dst_size = out_pos;
14✔
210
        return 0;
14✔
211
#else
212
        return -EPROTONOSUPPORT;
213
#endif
214
}
215

216
#if HAVE_LZ4
217
int dlopen_lz4(void) {
3,034✔
218
        ELF_NOTE_DLOPEN("lz4",
3,034✔
219
                        "Support lz4 compression in journal and coredump files",
220
                        COMPRESSION_PRIORITY_LZ4,
221
                        "liblz4.so.1");
222

223
        return dlopen_many_sym_or_warn(
3,034✔
224
                        &lz4_dl,
225
                        "liblz4.so.1", LOG_DEBUG,
226
                        DLSYM_ARG(LZ4F_compressBegin),
227
                        DLSYM_ARG(LZ4F_compressBound),
228
                        DLSYM_ARG(LZ4F_compressEnd),
229
                        DLSYM_ARG(LZ4F_compressUpdate),
230
                        DLSYM_ARG(LZ4F_createCompressionContext),
231
                        DLSYM_ARG(LZ4F_createDecompressionContext),
232
                        DLSYM_ARG(LZ4F_decompress),
233
                        DLSYM_ARG(LZ4F_freeCompressionContext),
234
                        DLSYM_ARG(LZ4F_freeDecompressionContext),
235
                        DLSYM_ARG(LZ4F_isError),
236
                        DLSYM_ARG(LZ4_compress_default),
237
                        DLSYM_ARG(LZ4_compress_HC),
238
                        DLSYM_ARG(LZ4_decompress_safe),
239
                        DLSYM_ARG(LZ4_decompress_safe_partial),
240
                        DLSYM_ARG(LZ4_versionNumber));
241
}
242
#endif
243

244
int compress_blob_lz4(const void *src, uint64_t src_size,
123✔
245
                      void *dst, size_t dst_alloc_size, size_t *dst_size, int level) {
246

247
        assert(src);
123✔
248
        assert(src_size > 0);
123✔
249
        assert(dst);
123✔
250
        assert(dst_alloc_size > 0);
123✔
251
        assert(dst_size);
123✔
252

253
#if HAVE_LZ4
254
        int r;
123✔
255

256
        r = dlopen_lz4();
123✔
257
        if (r < 0)
123✔
258
                return r;
259
        /* Returns < 0 if we couldn't compress the data or the
260
         * compressed result is longer than the original */
261

262
        if (src_size < 9)
123✔
263
                return -ENOBUFS;
264

265
        if (level <= 0)
123✔
266
                r = sym_LZ4_compress_default(src, (char*)dst + 8, src_size, (int) dst_alloc_size - 8);
123✔
267
        else
268
                r = sym_LZ4_compress_HC(src, (char*)dst + 8, src_size, (int) dst_alloc_size - 8, level);
×
269
        if (r <= 0)
123✔
270
                return -ENOBUFS;
271

272
        unaligned_write_le64(dst, src_size);
112✔
273
        *dst_size = r + 8;
112✔
274

275
        return 0;
112✔
276
#else
277
        return -EPROTONOSUPPORT;
278
#endif
279
}
280

281
#if HAVE_ZSTD
282
int dlopen_zstd(void) {
246,000✔
283
        ELF_NOTE_DLOPEN("zstd",
246,000✔
284
                        "Support zstd compression in journal and coredump files",
285
                        COMPRESSION_PRIORITY_ZSTD,
286
                        "libzstd.so.1");
287

288
        return dlopen_many_sym_or_warn(
246,000✔
289
                        &zstd_dl,
290
                        "libzstd.so.1", LOG_DEBUG,
291
                        DLSYM_ARG(ZSTD_getErrorCode),
292
                        DLSYM_ARG(ZSTD_compress),
293
                        DLSYM_ARG(ZSTD_getFrameContentSize),
294
                        DLSYM_ARG(ZSTD_decompressStream),
295
                        DLSYM_ARG(ZSTD_getErrorName),
296
                        DLSYM_ARG(ZSTD_DStreamOutSize),
297
                        DLSYM_ARG(ZSTD_CStreamInSize),
298
                        DLSYM_ARG(ZSTD_CStreamOutSize),
299
                        DLSYM_ARG(ZSTD_CCtx_setParameter),
300
                        DLSYM_ARG(ZSTD_compressStream2),
301
                        DLSYM_ARG(ZSTD_DStreamInSize),
302
                        DLSYM_ARG(ZSTD_freeCCtx),
303
                        DLSYM_ARG(ZSTD_freeDCtx),
304
                        DLSYM_ARG(ZSTD_isError),
305
                        DLSYM_ARG(ZSTD_createDCtx),
306
                        DLSYM_ARG(ZSTD_createCCtx));
307
}
308
#endif
309

310
int compress_blob_zstd(
218✔
311
                const void *src, uint64_t src_size,
312
                void *dst, size_t dst_alloc_size, size_t *dst_size, int level) {
313

314
        assert(src);
218✔
315
        assert(src_size > 0);
218✔
316
        assert(dst);
218✔
317
        assert(dst_alloc_size > 0);
218✔
318
        assert(dst_size);
218✔
319

320
#if HAVE_ZSTD
321
        size_t k;
218✔
322
        int r;
218✔
323

324
        r = dlopen_zstd();
218✔
325
        if (r < 0)
218✔
326
                return r;
327

328
        k = sym_ZSTD_compress(dst, dst_alloc_size, src, src_size, level < 0 ? 0 : level);
218✔
329
        if (sym_ZSTD_isError(k))
218✔
330
                return zstd_ret_to_errno(k);
4✔
331

332
        *dst_size = k;
214✔
333
        return 0;
214✔
334
#else
335
        return -EPROTONOSUPPORT;
336
#endif
337
}
338

339
int decompress_blob_xz(
2,533✔
340
                const void *src,
341
                uint64_t src_size,
342
                void **dst,
343
                size_t* dst_size,
344
                size_t dst_max) {
345

346
        assert(src);
2,533✔
347
        assert(src_size > 0);
2,533✔
348
        assert(dst);
2,533✔
349
        assert(dst_size);
2,533✔
350

351
#if HAVE_XZ
352
        _cleanup_(lzma_end_wrapper) lzma_stream s = LZMA_STREAM_INIT;
2,533✔
353
        lzma_ret ret;
2,533✔
354
        size_t space;
2,533✔
355
        int r;
2,533✔
356

357
        r = dlopen_lzma();
2,533✔
358
        if (r < 0)
2,533✔
359
                return r;
360

361
        ret = sym_lzma_stream_decoder(&s, UINT64_MAX, 0);
2,533✔
362
        if (ret != LZMA_OK)
2,533✔
363
                return -ENOMEM;
364

365
        space = MIN(src_size * 2, dst_max ?: SIZE_MAX);
2,533✔
366
        if (!greedy_realloc(dst, space, 1))
2,533✔
367
                return -ENOMEM;
368

369
        s.next_in = src;
2,533✔
370
        s.avail_in = src_size;
2,533✔
371

372
        s.next_out = *dst;
2,533✔
373
        s.avail_out = space;
2,533✔
374

375
        for (;;) {
2,697✔
376
                size_t used;
2,615✔
377

378
                ret = sym_lzma_code(&s, LZMA_FINISH);
2,615✔
379

380
                if (ret == LZMA_STREAM_END)
2,615✔
381
                        break;
382
                else if (ret != LZMA_OK)
88✔
383
                        return -ENOMEM;
384

385
                if (dst_max > 0 && (space - s.avail_out) >= dst_max)
82✔
386
                        break;
387
                else if (dst_max > 0 && space == dst_max)
82✔
388
                        return -ENOBUFS;
389

390
                used = space - s.avail_out;
82✔
391
                space = MIN(2 * space, dst_max ?: SIZE_MAX);
82✔
392
                if (!greedy_realloc(dst, space, 1))
82✔
393
                        return -ENOMEM;
394

395
                s.avail_out = space - used;
82✔
396
                s.next_out = *(uint8_t**)dst + used;
82✔
397
        }
398

399
        *dst_size = space - s.avail_out;
2,527✔
400
        return 0;
2,527✔
401
#else
402
        return -EPROTONOSUPPORT;
403
#endif
404
}
405

406
int decompress_blob_lz4(
2,635✔
407
                const void *src,
408
                uint64_t src_size,
409
                void **dst,
410
                size_t* dst_size,
411
                size_t dst_max) {
412

413
        assert(src);
2,635✔
414
        assert(src_size > 0);
2,635✔
415
        assert(dst);
2,635✔
416
        assert(dst_size);
2,635✔
417

418
#if HAVE_LZ4
419
        char* out;
2,635✔
420
        int r, size; /* LZ4 uses int for size */
2,635✔
421

422
        r = dlopen_lz4();
2,635✔
423
        if (r < 0)
2,635✔
424
                return r;
425

426
        if (src_size <= 8)
2,635✔
427
                return -EBADMSG;
428

429
        size = unaligned_read_le64(src);
2,633✔
430
        if (size < 0 || (unsigned) size != unaligned_read_le64(src))
2,633✔
431
                return -EFBIG;
432
        out = greedy_realloc(dst, size, 1);
2,629✔
433
        if (!out)
2,629✔
434
                return -ENOMEM;
435

436
        r = sym_LZ4_decompress_safe((char*)src + 8, out, src_size - 8, size);
2,629✔
437
        if (r < 0 || r != size)
2,629✔
438
                return -EBADMSG;
439

440
        *dst_size = size;
2,629✔
441
        return 0;
2,629✔
442
#else
443
        return -EPROTONOSUPPORT;
444
#endif
445
}
446

447
int decompress_blob_zstd(
196,899✔
448
                const void *src,
449
                uint64_t src_size,
450
                void **dst,
451
                size_t *dst_size,
452
                size_t dst_max) {
453

454
        assert(src);
196,899✔
455
        assert(src_size > 0);
196,899✔
456
        assert(dst);
196,899✔
457
        assert(dst_size);
196,899✔
458

459
#if HAVE_ZSTD
460
        uint64_t size;
196,899✔
461
        int r;
196,899✔
462

463
        r = dlopen_zstd();
196,899✔
464
        if (r < 0)
196,899✔
465
                return r;
196,899✔
466

467
        size = sym_ZSTD_getFrameContentSize(src, src_size);
196,899✔
468
        if (IN_SET(size, ZSTD_CONTENTSIZE_ERROR, ZSTD_CONTENTSIZE_UNKNOWN))
196,899✔
469
                return -EBADMSG;
470

471
        if (dst_max > 0 && size > dst_max)
196,893✔
472
                size = dst_max;
×
473
        if (size > SIZE_MAX)
196,893✔
474
                return -E2BIG;
475

476
        if (!(greedy_realloc(dst, MAX(sym_ZSTD_DStreamOutSize(), size), 1)))
196,893✔
477
                return -ENOMEM;
478

479
        _cleanup_(sym_ZSTD_freeDCtxp) ZSTD_DCtx *dctx = sym_ZSTD_createDCtx();
393,786✔
480
        if (!dctx)
196,893✔
481
                return -ENOMEM;
482

483
        ZSTD_inBuffer input = {
196,893✔
484
                .src = src,
485
                .size = src_size,
486
        };
487
        ZSTD_outBuffer output = {
393,786✔
488
                .dst = *dst,
196,893✔
489
                .size = MALLOC_SIZEOF_SAFE(*dst),
196,893✔
490
        };
491

492
        size_t k = sym_ZSTD_decompressStream(dctx, &output, &input);
196,893✔
493
        if (sym_ZSTD_isError(k))
196,893✔
494
                return log_debug_errno(zstd_ret_to_errno(k), "ZSTD decoder failed: %s", sym_ZSTD_getErrorName(k));
×
495
        if (output.pos < size)
196,893✔
496
                return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "ZSTD decoded less data than indicated, probably corrupted stream.");
×
497

498
        *dst_size = size;
196,893✔
499
        return 0;
196,893✔
500
#else
501
        return -EPROTONOSUPPORT;
502
#endif
503
}
504

505
int decompress_blob(
201,878✔
506
                Compression compression,
507
                const void *src,
508
                uint64_t src_size,
509
                void **dst,
510
                size_t* dst_size,
511
                size_t dst_max) {
512

513
        if (compression == COMPRESSION_XZ)
201,878✔
514
                return decompress_blob_xz(
2,523✔
515
                                src, src_size,
516
                                dst, dst_size, dst_max);
517
        else if (compression == COMPRESSION_LZ4)
199,355✔
518
                return decompress_blob_lz4(
2,527✔
519
                                src, src_size,
520
                                dst, dst_size, dst_max);
521
        else if (compression == COMPRESSION_ZSTD)
196,828✔
522
                return decompress_blob_zstd(
196,828✔
523
                                src, src_size,
524
                                dst, dst_size, dst_max);
525
        else
526
                return -EPROTONOSUPPORT;
527
}
528

529
int decompress_startswith_xz(
270✔
530
                const void *src,
531
                uint64_t src_size,
532
                void **buffer,
533
                const void *prefix,
534
                size_t prefix_len,
535
                uint8_t extra) {
536

537
        /* Checks whether the decompressed blob starts with the mentioned prefix. The byte extra needs to
538
         * follow the prefix */
539

540
        assert(src);
270✔
541
        assert(src_size > 0);
270✔
542
        assert(buffer);
270✔
543
        assert(prefix);
270✔
544

545
#if HAVE_XZ
546
        _cleanup_(lzma_end_wrapper) lzma_stream s = LZMA_STREAM_INIT;
270✔
547
        size_t allocated;
270✔
548
        lzma_ret ret;
270✔
549
        int r;
270✔
550

551
        r = dlopen_lzma();
270✔
552
        if (r < 0)
270✔
553
                return r;
554

555
        ret = sym_lzma_stream_decoder(&s, UINT64_MAX, 0);
270✔
556
        if (ret != LZMA_OK)
270✔
557
                return -EBADMSG;
558

559
        if (!(greedy_realloc(buffer, ALIGN_8(prefix_len + 1), 1)))
270✔
560
                return -ENOMEM;
561

562
        allocated = MALLOC_SIZEOF_SAFE(*buffer);
270✔
563

564
        s.next_in = src;
270✔
565
        s.avail_in = src_size;
270✔
566

567
        s.next_out = *buffer;
270✔
568
        s.avail_out = allocated;
270✔
569

570
        for (;;) {
270✔
571
                ret = sym_lzma_code(&s, LZMA_FINISH);
270✔
572

573
                if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
270✔
574
                        return -EBADMSG;
575

576
                if (allocated - s.avail_out >= prefix_len + 1)
270✔
577
                        return memcmp(*buffer, prefix, prefix_len) == 0 &&
540✔
578
                                ((const uint8_t*) *buffer)[prefix_len] == extra;
267✔
579

580
                if (ret == LZMA_STREAM_END)
×
581
                        return 0;
582

583
                s.avail_out += allocated;
×
584

585
                if (!(greedy_realloc(buffer, allocated * 2, 1)))
×
586
                        return -ENOMEM;
587

588
                allocated = MALLOC_SIZEOF_SAFE(*buffer);
×
589
                s.next_out = *(uint8_t**)buffer + allocated - s.avail_out;
×
590
        }
591

592
#else
593
        return -EPROTONOSUPPORT;
594
#endif
595
}
596

597
int decompress_startswith_lz4(
270✔
598
                const void *src,
599
                uint64_t src_size,
600
                void **buffer,
601
                const void *prefix,
602
                size_t prefix_len,
603
                uint8_t extra) {
604

605
        /* Checks whether the decompressed blob starts with the mentioned prefix. The byte extra needs to
606
         * follow the prefix */
607

608
        assert(src);
270✔
609
        assert(src_size > 0);
270✔
610
        assert(buffer);
270✔
611
        assert(prefix);
270✔
612

613
#if HAVE_LZ4
614
        size_t allocated;
270✔
615
        int r;
270✔
616

617
        r = dlopen_lz4();
270✔
618
        if (r < 0)
270✔
619
                return r;
620

621
        if (src_size <= 8)
270✔
622
                return -EBADMSG;
623

624
        if (!(greedy_realloc(buffer, ALIGN_8(prefix_len + 1), 1)))
270✔
625
                return -ENOMEM;
626
        allocated = MALLOC_SIZEOF_SAFE(*buffer);
270✔
627

628
        r = sym_LZ4_decompress_safe_partial(
540✔
629
                        (char*)src + 8,
630
                        *buffer,
631
                        src_size - 8,
270✔
632
                        prefix_len + 1,
270✔
633
                        allocated);
634

635
        /* One lz4 < 1.8.3, we might get "failure" (r < 0), or "success" where just a part of the buffer is
636
         * decompressed. But if we get a smaller amount of bytes than requested, we don't know whether there
637
         * isn't enough data to fill the requested size or whether we just got a partial answer.
638
         */
639
        if (r < 0 || (size_t) r < prefix_len + 1) {
270✔
640
                size_t size;
×
641

642
                if (sym_LZ4_versionNumber() >= 10803)
×
643
                        /* We trust that the newer lz4 decompresses the number of bytes we
644
                         * requested if available in the compressed string. */
645
                        return 0;
×
646

647
                if (r > 0)
×
648
                        /* Compare what we have first, in case of mismatch we can
649
                         * shortcut the full comparison. */
650
                        if (memcmp(*buffer, prefix, r) != 0)
×
651
                                return 0;
652

653
                /* Before version 1.8.3, lz4 always tries to decode full a "sequence",
654
                 * so in pathological cases might need to decompress the full field. */
655
                r = decompress_blob_lz4(src, src_size, buffer, &size, 0);
×
656
                if (r < 0)
×
657
                        return r;
658

659
                if (size < prefix_len + 1)
×
660
                        return 0;
661
        }
662

663
        return memcmp(*buffer, prefix, prefix_len) == 0 &&
270✔
664
                ((const uint8_t*) *buffer)[prefix_len] == extra;
267✔
665
#else
666
        return -EPROTONOSUPPORT;
667
#endif
668
}
669

670
int decompress_startswith_zstd(
48,844✔
671
                const void *src,
672
                uint64_t src_size,
673
                void **buffer,
674
                const void *prefix,
675
                size_t prefix_len,
676
                uint8_t extra) {
677

678
        assert(src);
48,844✔
679
        assert(src_size > 0);
48,844✔
680
        assert(buffer);
48,844✔
681
        assert(prefix);
48,844✔
682

683
#if HAVE_ZSTD
684
        int r;
48,844✔
685

686
        r = dlopen_zstd();
48,844✔
687
        if (r < 0)
48,844✔
688
                return r;
48,844✔
689

690
        uint64_t size = sym_ZSTD_getFrameContentSize(src, src_size);
48,844✔
691
        if (IN_SET(size, ZSTD_CONTENTSIZE_ERROR, ZSTD_CONTENTSIZE_UNKNOWN))
48,844✔
692
                return -EBADMSG;
693

694
        if (size < prefix_len + 1)
48,844✔
695
                return 0; /* Decompressed text too short to match the prefix and extra */
696

697
        _cleanup_(sym_ZSTD_freeDCtxp) ZSTD_DCtx *dctx = sym_ZSTD_createDCtx();
97,688✔
698
        if (!dctx)
48,844✔
699
                return -ENOMEM;
700

701
        if (!(greedy_realloc(buffer, MAX(sym_ZSTD_DStreamOutSize(), prefix_len + 1), 1)))
48,844✔
702
                return -ENOMEM;
703

704
        ZSTD_inBuffer input = {
48,844✔
705
                .src = src,
706
                .size = src_size,
707
        };
708
        ZSTD_outBuffer output = {
97,688✔
709
                .dst = *buffer,
48,844✔
710
                .size = MALLOC_SIZEOF_SAFE(*buffer),
48,844✔
711
        };
712
        size_t k;
48,844✔
713

714
        k = sym_ZSTD_decompressStream(dctx, &output, &input);
48,844✔
715
        if (sym_ZSTD_isError(k)) {
48,844✔
716
                log_debug("ZSTD decoder failed: %s", sym_ZSTD_getErrorName(k));
×
717
                return zstd_ret_to_errno(k);
×
718
        }
719
        assert(output.pos >= prefix_len + 1);
48,844✔
720

721
        return memcmp(*buffer, prefix, prefix_len) == 0 &&
48,844✔
722
                ((const uint8_t*) *buffer)[prefix_len] == extra;
382✔
723
#else
724
        return -EPROTONOSUPPORT;
725
#endif
726
}
727

728
int decompress_startswith(
48,586✔
729
                Compression compression,
730
                const void *src,
731
                uint64_t src_size,
732
                void **buffer,
733
                const void *prefix,
734
                size_t prefix_len,
735
                uint8_t extra) {
736

737
        if (compression == COMPRESSION_XZ)
48,586✔
738
                return decompress_startswith_xz(
4✔
739
                                src, src_size,
740
                                buffer,
741
                                prefix, prefix_len,
742
                                extra);
743

744
        else if (compression == COMPRESSION_LZ4)
48,582✔
745
                return decompress_startswith_lz4(
4✔
746
                                src, src_size,
747
                                buffer,
748
                                prefix, prefix_len,
749
                                extra);
750
        else if (compression == COMPRESSION_ZSTD)
48,578✔
751
                return decompress_startswith_zstd(
48,578✔
752
                                src, src_size,
753
                                buffer,
754
                                prefix, prefix_len,
755
                                extra);
756
        else
757
                return -EBADMSG;
758
}
759

760
int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size) {
1✔
761
        assert(fdf >= 0);
1✔
762
        assert(fdt >= 0);
1✔
763

764
#if HAVE_XZ
765
        _cleanup_(lzma_end_wrapper) lzma_stream s = LZMA_STREAM_INIT;
1✔
766
        lzma_ret ret;
1✔
767
        uint8_t buf[BUFSIZ], out[BUFSIZ];
1✔
768
        lzma_action action = LZMA_RUN;
1✔
769
        int r;
1✔
770

771
        r = dlopen_lzma();
1✔
772
        if (r < 0)
1✔
773
                return r;
774

775
        ret = sym_lzma_easy_encoder(&s, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64);
1✔
776
        if (ret != LZMA_OK)
1✔
777
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
778
                                       "Failed to initialize XZ encoder: code %u",
779
                                       ret);
780

781
        for (;;) {
8✔
782
                if (s.avail_in == 0 && action == LZMA_RUN) {
8✔
783
                        size_t m = sizeof(buf);
7✔
784
                        ssize_t n;
7✔
785

786
                        if (max_bytes != UINT64_MAX && (uint64_t) m > max_bytes)
7✔
787
                                m = (size_t) max_bytes;
×
788

789
                        n = read(fdf, buf, m);
7✔
790
                        if (n < 0)
7✔
791
                                return -errno;
×
792
                        if (n == 0)
7✔
793
                                action = LZMA_FINISH;
794
                        else {
795
                                s.next_in = buf;
6✔
796
                                s.avail_in = n;
6✔
797

798
                                if (max_bytes != UINT64_MAX) {
6✔
799
                                        assert(max_bytes >= (uint64_t) n);
×
800
                                        max_bytes -= n;
×
801
                                }
802
                        }
803
                }
804

805
                if (s.avail_out == 0) {
8✔
806
                        s.next_out = out;
2✔
807
                        s.avail_out = sizeof(out);
2✔
808
                }
809

810
                ret = sym_lzma_code(&s, action);
8✔
811
                if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
8✔
812
                        return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
×
813
                                               "Compression failed: code %u",
814
                                               ret);
815

816
                if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
8✔
817
                        ssize_t n, k;
2✔
818

819
                        n = sizeof(out) - s.avail_out;
2✔
820

821
                        k = loop_write(fdt, out, n);
2✔
822
                        if (k < 0)
2✔
823
                                return k;
824

825
                        if (ret == LZMA_STREAM_END) {
2✔
826
                                if (ret_uncompressed_size)
1✔
827
                                        *ret_uncompressed_size = s.total_in;
1✔
828

829
                                if (s.total_in == 0)
1✔
830
                                        log_debug("XZ compression finished (no input data)");
×
831
                                else
832
                                        log_debug("XZ compression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
1✔
833
                                                  s.total_in, s.total_out,
834
                                                  (double) s.total_out / s.total_in * 100);
835

836
                                return 0;
1✔
837
                        }
838
                }
839
        }
840
#else
841
        return -EPROTONOSUPPORT;
842
#endif
843
}
844

845
#define LZ4_BUFSIZE (512*1024u)
846

847
int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size) {
1✔
848

849
#if HAVE_LZ4
850
        LZ4F_errorCode_t c;
1✔
851
        _cleanup_(sym_LZ4F_freeCompressionContextp) LZ4F_compressionContext_t ctx = NULL;
×
852
        _cleanup_free_ void *in_buff = NULL;
×
853
        _cleanup_free_ char *out_buff = NULL;
1✔
854
        size_t out_allocsize, n, offset = 0, frame_size;
1✔
855
        uint64_t total_in = 0, total_out;
1✔
856
        int r;
1✔
857
        static const LZ4F_preferences_t preferences = {
1✔
858
                .frameInfo.blockSizeID = 5,
859
        };
860

861
        r = dlopen_lz4();
1✔
862
        if (r < 0)
1✔
863
                return r;
864

865
        c = sym_LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
1✔
866
        if (sym_LZ4F_isError(c))
1✔
867
                return -ENOMEM;
868

869
        frame_size = sym_LZ4F_compressBound(LZ4_BUFSIZE, &preferences);
1✔
870
        out_allocsize = frame_size + 64*1024; /* add some space for header and trailer */
1✔
871
        out_buff = malloc(out_allocsize);
1✔
872
        if (!out_buff)
1✔
873
                return -ENOMEM;
874

875
        in_buff = malloc(LZ4_BUFSIZE);
1✔
876
        if (!in_buff)
1✔
877
                return -ENOMEM;
878

879
        n = offset = total_out = sym_LZ4F_compressBegin(ctx, out_buff, out_allocsize, &preferences);
1✔
880
        if (sym_LZ4F_isError(n))
1✔
881
                return -EINVAL;
882

883
        log_debug("Buffer size is %zu bytes, header size %zu bytes.", out_allocsize, n);
1✔
884

885
        for (;;) {
2✔
886
                ssize_t k;
2✔
887

888
                k = loop_read(fdf, in_buff, LZ4_BUFSIZE, true);
2✔
889
                if (k < 0)
2✔
890
                        return k;
×
891
                if (k == 0)
2✔
892
                        break;
893
                n = sym_LZ4F_compressUpdate(ctx, out_buff + offset, out_allocsize - offset,
1✔
894
                                        in_buff, k, NULL);
895
                if (sym_LZ4F_isError(n))
1✔
896
                        return -ENOTRECOVERABLE;
897

898
                total_in += k;
1✔
899
                offset += n;
1✔
900
                total_out += n;
1✔
901

902
                if (max_bytes != UINT64_MAX && total_out > (size_t) max_bytes)
1✔
903
                        return log_debug_errno(SYNTHETIC_ERRNO(EFBIG),
×
904
                                               "Compressed stream longer than %" PRIu64 " bytes", max_bytes);
905

906
                if (out_allocsize - offset < frame_size + 4) {
1✔
907
                        k = loop_write(fdt, out_buff, offset);
×
908
                        if (k < 0)
×
909
                                return k;
910
                        offset = 0;
911
                }
912
        }
913

914
        n = sym_LZ4F_compressEnd(ctx, out_buff + offset, out_allocsize - offset, NULL);
1✔
915
        if (sym_LZ4F_isError(n))
1✔
916
                return -ENOTRECOVERABLE;
917

918
        offset += n;
1✔
919
        total_out += n;
1✔
920
        r = loop_write(fdt, out_buff, offset);
1✔
921
        if (r < 0)
1✔
922
                return r;
923

924
        if (ret_uncompressed_size)
1✔
925
                *ret_uncompressed_size = total_in;
1✔
926

927
        if (total_in == 0)
1✔
928
                log_debug("LZ4 compression finished (no input data)");
×
929
        else
930
                log_debug("LZ4 compression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
1✔
931
                          total_in, total_out,
932
                          (double) total_out / total_in * 100);
933

934
        return 0;
935
#else
936
        return -EPROTONOSUPPORT;
937
#endif
938
}
939

940
int decompress_stream_xz(int fdf, int fdt, uint64_t max_bytes) {
3✔
941
        assert(fdf >= 0);
3✔
942
        assert(fdt >= 0);
3✔
943

944
#if HAVE_XZ
945
        _cleanup_(lzma_end_wrapper) lzma_stream s = LZMA_STREAM_INIT;
3✔
946
        lzma_ret ret;
3✔
947

948
        uint8_t buf[BUFSIZ], out[BUFSIZ];
3✔
949
        lzma_action action = LZMA_RUN;
3✔
950
        int r;
3✔
951

952
        r = dlopen_lzma();
3✔
953
        if (r < 0)
3✔
954
                return r;
955

956
        ret = sym_lzma_stream_decoder(&s, UINT64_MAX, 0);
3✔
957
        if (ret != LZMA_OK)
3✔
958
                return log_debug_errno(SYNTHETIC_ERRNO(ENOMEM),
×
959
                                       "Failed to initialize XZ decoder: code %u",
960
                                       ret);
961

962
        for (;;) {
15✔
963
                if (s.avail_in == 0 && action == LZMA_RUN) {
15✔
964
                        ssize_t n;
5✔
965

966
                        n = read(fdf, buf, sizeof(buf));
5✔
967
                        if (n < 0)
5✔
968
                                return -errno;
×
969
                        if (n == 0)
5✔
970
                                action = LZMA_FINISH;
971
                        else {
972
                                s.next_in = buf;
5✔
973
                                s.avail_in = n;
5✔
974
                        }
975
                }
976

977
                if (s.avail_out == 0) {
15✔
978
                        s.next_out = out;
13✔
979
                        s.avail_out = sizeof(out);
13✔
980
                }
981

982
                ret = sym_lzma_code(&s, action);
15✔
983
                if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
15✔
984
                        return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
1✔
985
                                               "Decompression failed: code %u",
986
                                               ret);
987

988
                if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
14✔
989
                        ssize_t n, k;
12✔
990

991
                        n = sizeof(out) - s.avail_out;
12✔
992

993
                        if (max_bytes != UINT64_MAX) {
12✔
994
                                if (max_bytes < (uint64_t) n)
12✔
995
                                        return -EFBIG;
996

997
                                max_bytes -= n;
11✔
998
                        }
999

1000
                        k = loop_write(fdt, out, n);
11✔
1001
                        if (k < 0)
11✔
1002
                                return k;
1003

1004
                        if (ret == LZMA_STREAM_END) {
11✔
1005
                                if (s.total_in == 0)
1✔
1006
                                        log_debug("XZ decompression finished (no input data)");
×
1007
                                else
1008
                                        log_debug("XZ decompression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
1✔
1009
                                                  s.total_in, s.total_out,
1010
                                                  (double) s.total_out / s.total_in * 100);
1011

1012
                                return 0;
1✔
1013
                        }
1014
                }
1015
        }
1016
#else
1017
        return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT),
1018
                               "Cannot decompress file. Compiled without XZ support.");
1019
#endif
1020
}
1021

1022
int decompress_stream_lz4(int in, int out, uint64_t max_bytes) {
3✔
1023
#if HAVE_LZ4
1024
        size_t c;
3✔
1025
        _cleanup_(sym_LZ4F_freeDecompressionContextp) LZ4F_decompressionContext_t ctx = NULL;
×
1026
        _cleanup_free_ char *buf = NULL;
3✔
1027
        char *src;
3✔
1028
        struct stat st;
3✔
1029
        int r;
3✔
1030
        size_t total_in = 0, total_out = 0;
3✔
1031

1032
        r = dlopen_lz4();
3✔
1033
        if (r < 0)
3✔
1034
                return r;
1035

1036
        c = sym_LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
3✔
1037
        if (sym_LZ4F_isError(c))
3✔
1038
                return -ENOMEM;
1039

1040
        if (fstat(in, &st) < 0)
3✔
1041
                return log_debug_errno(errno, "fstat() failed: %m");
×
1042

1043
        if (file_offset_beyond_memory_size(st.st_size))
3✔
1044
                return -EFBIG;
1045

1046
        buf = malloc(LZ4_BUFSIZE);
3✔
1047
        if (!buf)
3✔
1048
                return -ENOMEM;
1049

1050
        src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, in, 0);
3✔
1051
        if (src == MAP_FAILED)
3✔
1052
                return -errno;
×
1053

1054
        while (total_in < (size_t) st.st_size) {
5✔
1055
                size_t produced = LZ4_BUFSIZE;
3✔
1056
                size_t used = st.st_size - total_in;
3✔
1057

1058
                c = sym_LZ4F_decompress(ctx, buf, &produced, src + total_in, &used, NULL);
3✔
1059
                if (sym_LZ4F_isError(c)) {
3✔
1060
                        r = -EBADMSG;
×
1061
                        goto cleanup;
1✔
1062
                }
1063

1064
                total_in += used;
3✔
1065
                total_out += produced;
3✔
1066

1067
                if (max_bytes != UINT64_MAX && total_out > (size_t) max_bytes) {
3✔
1068
                        log_debug("Decompressed stream longer than %"PRIu64" bytes", max_bytes);
1✔
1069
                        r = -EFBIG;
1✔
1070
                        goto cleanup;
1✔
1071
                }
1072

1073
                r = loop_write(out, buf, produced);
2✔
1074
                if (r < 0)
2✔
1075
                        goto cleanup;
×
1076
        }
1077

1078
        if (total_in == 0)
2✔
1079
                log_debug("LZ4 decompression finished (no input data)");
×
1080
        else
1081
                log_debug("LZ4 decompression finished (%zu -> %zu bytes, %.1f%%)",
2✔
1082
                          total_in, total_out,
1083
                          (double) total_out / total_in * 100);
1084
        r = 0;
1085
 cleanup:
3✔
1086
        munmap(src, st.st_size);
3✔
1087
        return r;
3✔
1088
#else
1089
        return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT),
1090
                               "Cannot decompress file. Compiled without LZ4 support.");
1091
#endif
1092
}
1093

1094
int compress_stream_zstd(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size) {
25✔
1095
        assert(fdf >= 0);
25✔
1096
        assert(fdt >= 0);
25✔
1097

1098
#if HAVE_ZSTD
1099
        _cleanup_(sym_ZSTD_freeCCtxp) ZSTD_CCtx *cctx = NULL;
×
1100
        _cleanup_free_ void *in_buff = NULL, *out_buff = NULL;
25✔
1101
        size_t in_allocsize, out_allocsize;
25✔
1102
        size_t z;
25✔
1103
        uint64_t left = max_bytes, in_bytes = 0;
25✔
1104
        int r;
25✔
1105

1106
        r = dlopen_zstd();
25✔
1107
        if (r < 0)
25✔
1108
                return r;
1109

1110
        /* Create the context and buffers */
1111
        in_allocsize = sym_ZSTD_CStreamInSize();
25✔
1112
        out_allocsize = sym_ZSTD_CStreamOutSize();
25✔
1113
        in_buff = malloc(in_allocsize);
25✔
1114
        out_buff = malloc(out_allocsize);
25✔
1115
        cctx = sym_ZSTD_createCCtx();
25✔
1116
        if (!cctx || !out_buff || !in_buff)
25✔
1117
                return -ENOMEM;
1118

1119
        z = sym_ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1);
25✔
1120
        if (sym_ZSTD_isError(z))
25✔
1121
                log_debug("Failed to enable ZSTD checksum, ignoring: %s", sym_ZSTD_getErrorName(z));
×
1122

1123
        /* This loop read from the input file, compresses that entire chunk,
1124
         * and writes all output produced to the output file.
1125
         */
1126
        for (;;) {
239✔
1127
                bool is_last_chunk;
264✔
1128
                ZSTD_inBuffer input = {
264✔
1129
                        .src = in_buff,
1130
                        .size = 0,
1131
                        .pos = 0
1132
                };
1133
                ssize_t red;
264✔
1134

1135
                red = loop_read(fdf, in_buff, in_allocsize, true);
264✔
1136
                if (red < 0)
264✔
1137
                        return red;
×
1138
                is_last_chunk = red == 0;
264✔
1139

1140
                in_bytes += (size_t) red;
264✔
1141
                input.size = (size_t) red;
264✔
1142

1143
                for (bool finished = false; !finished;) {
528✔
1144
                        ZSTD_outBuffer output = {
264✔
1145
                                .dst = out_buff,
1146
                                .size = out_allocsize,
1147
                                .pos = 0
1148
                        };
1149
                        size_t remaining;
264✔
1150
                        ssize_t wrote;
264✔
1151

1152
                        /* Compress into the output buffer and write all of the
1153
                         * output to the file so we can reuse the buffer next
1154
                         * iteration.
1155
                         */
1156
                        remaining = sym_ZSTD_compressStream2(
503✔
1157
                                cctx, &output, &input,
1158
                                is_last_chunk ? ZSTD_e_end : ZSTD_e_continue);
1159

1160
                        if (sym_ZSTD_isError(remaining)) {
264✔
1161
                                log_debug("ZSTD encoder failed: %s", sym_ZSTD_getErrorName(remaining));
×
1162
                                return zstd_ret_to_errno(remaining);
×
1163
                        }
1164

1165
                        if (left < output.pos)
264✔
1166
                                return -EFBIG;
1167

1168
                        wrote = loop_write_full(fdt, output.dst, output.pos, USEC_INFINITY);
264✔
1169
                        if (wrote < 0)
264✔
1170
                                return wrote;
1171

1172
                        left -= output.pos;
264✔
1173

1174
                        /* If we're on the last chunk we're finished when zstd
1175
                         * returns 0, which means its consumed all the input AND
1176
                         * finished the frame. Otherwise, we're finished when
1177
                         * we've consumed all the input.
1178
                         */
1179
                        finished = is_last_chunk ? (remaining == 0) : (input.pos == input.size);
264✔
1180
                }
1181

1182
                /* zstd only returns 0 when the input is completely consumed */
1183
                assert(input.pos == input.size);
264✔
1184
                if (is_last_chunk)
264✔
1185
                        break;
1186
        }
1187

1188
        if (ret_uncompressed_size)
25✔
1189
                *ret_uncompressed_size = in_bytes;
25✔
1190

1191
        if (in_bytes == 0)
25✔
1192
                log_debug("ZSTD compression finished (no input data)");
×
1193
        else
1194
                log_debug("ZSTD compression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
25✔
1195
                          in_bytes, max_bytes - left, (double) (max_bytes - left) / in_bytes * 100);
1196

1197
        return 0;
1198
#else
1199
        return -EPROTONOSUPPORT;
1200
#endif
1201
}
1202

1203
int decompress_stream_zstd(int fdf, int fdt, uint64_t max_bytes) {
13✔
1204
        assert(fdf >= 0);
13✔
1205
        assert(fdt >= 0);
13✔
1206

1207
#if HAVE_ZSTD
1208
        _cleanup_(sym_ZSTD_freeDCtxp) ZSTD_DCtx *dctx = NULL;
×
1209
        _cleanup_free_ void *in_buff = NULL, *out_buff = NULL;
13✔
1210
        size_t in_allocsize, out_allocsize;
13✔
1211
        size_t last_result = 0;
13✔
1212
        uint64_t left = max_bytes, in_bytes = 0;
13✔
1213
        int r;
13✔
1214

1215
        r = dlopen_zstd();
13✔
1216
        if (r < 0)
13✔
1217
                return r;
1218
        /* Create the context and buffers */
1219
        in_allocsize = sym_ZSTD_DStreamInSize();
13✔
1220
        out_allocsize = sym_ZSTD_DStreamOutSize();
13✔
1221
        in_buff = malloc(in_allocsize);
13✔
1222
        out_buff = malloc(out_allocsize);
13✔
1223
        dctx = sym_ZSTD_createDCtx();
13✔
1224
        if (!dctx || !out_buff || !in_buff)
13✔
1225
                return -ENOMEM;
1226

1227
        /* This loop assumes that the input file is one or more concatenated
1228
         * zstd streams. This example won't work if there is trailing non-zstd
1229
         * data at the end, but streaming decompression in general handles this
1230
         * case. ZSTD_decompressStream() returns 0 exactly when the frame is
1231
         * completed, and doesn't consume input after the frame.
1232
         */
1233
        for (;;) {
33✔
1234
                bool has_error = false;
23✔
1235
                ZSTD_inBuffer input = {
23✔
1236
                        .src = in_buff,
1237
                        .size = 0,
1238
                        .pos = 0
1239
                };
1240
                ssize_t red;
23✔
1241

1242
                red = loop_read(fdf, in_buff, in_allocsize, true);
23✔
1243
                if (red < 0)
23✔
1244
                        return red;
2✔
1245
                if (red == 0)
23✔
1246
                        break;
1247

1248
                in_bytes += (size_t) red;
13✔
1249
                input.size = (size_t) red;
13✔
1250
                input.pos = 0;
13✔
1251

1252
                /* Given a valid frame, zstd won't consume the last byte of the
1253
                 * frame until it has flushed all of the decompressed data of
1254
                 * the frame. So input.pos < input.size means frame is not done
1255
                 * or there is still output available.
1256
                 */
1257
                while (input.pos < input.size) {
59✔
1258
                        ZSTD_outBuffer output = {
49✔
1259
                                .dst = out_buff,
1260
                                .size = out_allocsize,
1261
                                .pos = 0
1262
                        };
1263
                        ssize_t wrote;
49✔
1264
                        /* The return code is zero if the frame is complete, but
1265
                         * there may be multiple frames concatenated together.
1266
                         * Zstd will automatically reset the context when a
1267
                         * frame is complete. Still, calling ZSTD_DCtx_reset()
1268
                         * can be useful to reset the context to a clean state,
1269
                         * for instance if the last decompression call returned
1270
                         * an error.
1271
                         */
1272
                        last_result = sym_ZSTD_decompressStream(dctx, &output, &input);
49✔
1273
                        if (sym_ZSTD_isError(last_result)) {
49✔
1274
                                has_error = true;
1✔
1275
                                break;
1✔
1276
                        }
1277

1278
                        if (left < output.pos)
48✔
1279
                                return -EFBIG;
2✔
1280

1281
                        wrote = loop_write_full(fdt, output.dst, output.pos, USEC_INFINITY);
47✔
1282
                        if (wrote < 0)
47✔
1283
                                return wrote;
1284

1285
                        left -= output.pos;
46✔
1286
                }
1287
                if (has_error)
11✔
1288
                        break;
1289
        }
1290

1291
        if (in_bytes == 0)
11✔
1292
                return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "ZSTD decoder failed: no data read");
×
1293

1294
        if (last_result != 0) {
11✔
1295
                /* The last return value from ZSTD_decompressStream did not end
1296
                 * on a frame, but we reached the end of the file! We assume
1297
                 * this is an error, and the input was truncated.
1298
                 */
1299
                log_debug("ZSTD decoder failed: %s", sym_ZSTD_getErrorName(last_result));
1✔
1300
                return zstd_ret_to_errno(last_result);
1✔
1301
        }
1302

1303
        if (in_bytes == 0)
10✔
1304
                log_debug("ZSTD decompression finished (no input data)");
1305
        else
1306
                log_debug("ZSTD decompression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
10✔
1307
                          in_bytes,
1308
                          max_bytes - left,
1309
                          (double) (max_bytes - left) / in_bytes * 100);
1310
        return 0;
1311
#else
1312
        return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT),
1313
                               "Cannot decompress file. Compiled without ZSTD support.");
1314
#endif
1315
}
1316

1317
int decompress_stream(const char *filename, int fdf, int fdt, uint64_t max_bytes) {
10✔
1318

1319
        if (endswith(filename, ".lz4"))
10✔
1320
                return decompress_stream_lz4(fdf, fdt, max_bytes);
×
1321
        else if (endswith(filename, ".xz"))
10✔
1322
                return decompress_stream_xz(fdf, fdt, max_bytes);
×
1323
        else if (endswith(filename, ".zst"))
10✔
1324
                return decompress_stream_zstd(fdf, fdt, max_bytes);
10✔
1325
        else
1326
                return -EPROTONOSUPPORT;
1327
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc