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

systemd / systemd / 14950366571

10 May 2025 12:04PM UTC coverage: 72.229% (-0.02%) from 72.251%
14950366571

push

github

keszybz
test: fix root check for test-bpf-foreign-programs

This test requires root, but the check was mistakenly dropped, causing it
to fail with an assert when running without root:

src/test/test-bpf-foreign-programs.c:308: Assertion failed: expected "test_bpf_cgroup_programs(m, "single_prog.service", single_prog, ELEMENTSOF(single_prog))" to succeed, but got error: Operation not permitted

Restore the uid check

Follow-up for 22e2f0642

0 of 3 new or added lines in 1 file covered. (0.0%)

176 existing lines in 40 files now uncovered.

297455 of 411822 relevant lines covered (72.23%)

704153.97 hits per line

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

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

3
#include <inttypes.h>
4
#include <malloc.h>
5
#include <stdlib.h>
6
#include <sys/mman.h>
7
#include <sys/stat.h>
8
#include <sys/types.h>
9
#include <unistd.h>
10

11
#if HAVE_LZ4
12
#include <lz4.h>
13
#include <lz4hc.h>
14
#include <lz4frame.h>
15
#endif
16

17
#if HAVE_XZ
18
#include <lzma.h>
19
#endif
20

21
#if HAVE_ZSTD
22
#include <zstd.h>
23
#include <zstd_errors.h>
24
#endif
25

26
#include "alloc-util.h"
27
#include "bitfield.h"
28
#include "compress.h"
29
#include "fd-util.h"
30
#include "fileio.h"
31
#include "io-util.h"
32
#include "log.h"
33
#include "macro.h"
34
#include "sparse-endian.h"
35
#include "string-table.h"
36
#include "string-util.h"
37
#include "unaligned.h"
38

39
#if HAVE_LZ4
40
static void *lz4_dl = NULL;
41

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

59
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(LZ4F_compressionContext_t, sym_LZ4F_freeCompressionContext, NULL);
1✔
60
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(LZ4F_decompressionContext_t, sym_LZ4F_freeDecompressionContext, NULL);
3✔
61
#endif
62

63
#if HAVE_ZSTD
64
static void *zstd_dl = NULL;
65

66
static DLSYM_PROTOTYPE(ZSTD_CCtx_setParameter) = NULL;
67
static DLSYM_PROTOTYPE(ZSTD_compress) = NULL;
68
static DLSYM_PROTOTYPE(ZSTD_compressStream2) = NULL;
69
static DLSYM_PROTOTYPE(ZSTD_createCCtx) = NULL;
70
static DLSYM_PROTOTYPE(ZSTD_createDCtx) = NULL;
71
static DLSYM_PROTOTYPE(ZSTD_CStreamInSize) = NULL;
72
static DLSYM_PROTOTYPE(ZSTD_CStreamOutSize) = NULL;
73
static DLSYM_PROTOTYPE(ZSTD_decompressStream) = NULL;
74
static DLSYM_PROTOTYPE(ZSTD_DStreamInSize) = NULL;
75
static DLSYM_PROTOTYPE(ZSTD_DStreamOutSize) = NULL;
76
static DLSYM_PROTOTYPE(ZSTD_freeCCtx) = NULL;
77
static DLSYM_PROTOTYPE(ZSTD_freeDCtx) = NULL;
78
static DLSYM_PROTOTYPE(ZSTD_getErrorCode) = NULL;
79
static DLSYM_PROTOTYPE(ZSTD_getErrorName) = NULL;
80
static DLSYM_PROTOTYPE(ZSTD_getFrameContentSize) = NULL;
81
static DLSYM_PROTOTYPE(ZSTD_isError) = NULL;
82

83
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(ZSTD_CCtx*, sym_ZSTD_freeCCtx, NULL);
25✔
84
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(ZSTD_DCtx*, sym_ZSTD_freeDCtx, NULL);
246,908✔
85

86
static int zstd_ret_to_errno(size_t ret) {
7✔
87
        switch (sym_ZSTD_getErrorCode(ret)) {
7✔
88
        case ZSTD_error_dstSize_tooSmall:
89
                return -ENOBUFS;
90
        case ZSTD_error_memory_allocation:
×
91
                return -ENOMEM;
×
92
        default:
1✔
93
                return -EBADMSG;
1✔
94
        }
95
}
96
#endif
97

98
#if HAVE_XZ
99
static void *lzma_dl = NULL;
100

101
static DLSYM_PROTOTYPE(lzma_code) = NULL;
102
static DLSYM_PROTOTYPE(lzma_easy_encoder) = NULL;
103
static DLSYM_PROTOTYPE(lzma_end) = NULL;
104
static DLSYM_PROTOTYPE(lzma_stream_buffer_encode) = NULL;
105
static DLSYM_PROTOTYPE(lzma_stream_decoder) = NULL;
106
static DLSYM_PROTOTYPE(lzma_lzma_preset) = NULL;
107

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

120
#define ALIGN_8(l) ALIGN_TO(l, sizeof(size_t))
121

122
static const char* const compression_table[_COMPRESSION_MAX] = {
123
        [COMPRESSION_NONE] = "NONE",
124
        [COMPRESSION_XZ]   = "XZ",
125
        [COMPRESSION_LZ4]  = "LZ4",
126
        [COMPRESSION_ZSTD] = "ZSTD",
127
};
128

129
static const char* const compression_lowercase_table[_COMPRESSION_MAX] = {
130
        [COMPRESSION_NONE] = "none",
131
        [COMPRESSION_XZ]   = "xz",
132
        [COMPRESSION_LZ4]  = "lz4",
133
        [COMPRESSION_ZSTD] = "zstd",
134
};
135

136
DEFINE_STRING_TABLE_LOOKUP(compression, Compression);
537✔
137
DEFINE_STRING_TABLE_LOOKUP(compression_lowercase, Compression);
74✔
138

139
bool compression_supported(Compression c) {
2,675✔
140
        static const unsigned supported =
2,675✔
141
                (1U << COMPRESSION_NONE) |
142
                (1U << COMPRESSION_XZ) * HAVE_XZ |
143
                (1U << COMPRESSION_LZ4) * HAVE_LZ4 |
144
                (1U << COMPRESSION_ZSTD) * HAVE_ZSTD;
145

146
        assert(c >= 0);
2,675✔
147
        assert(c < _COMPRESSION_MAX);
2,675✔
148

149
        return BIT_SET(supported, c);
2,675✔
150
}
151

152
#if HAVE_XZ
153
int dlopen_lzma(void) {
2,839✔
154
        ELF_NOTE_DLOPEN("lzma",
2,839✔
155
                        "Support lzma compression in journal and coredump files",
156
                        COMPRESSION_PRIORITY_XZ,
157
                        "liblzma.so.5");
158

159
        return dlopen_many_sym_or_warn(
2,839✔
160
                        &lzma_dl,
161
                        "liblzma.so.5", LOG_DEBUG,
162
                        DLSYM_ARG(lzma_code),
163
                        DLSYM_ARG(lzma_easy_encoder),
164
                        DLSYM_ARG(lzma_end),
165
                        DLSYM_ARG(lzma_stream_buffer_encode),
166
                        DLSYM_ARG(lzma_lzma_preset),
167
                        DLSYM_ARG(lzma_stream_decoder));
168
}
169
#endif
170

171
int compress_blob_xz(const void *src, uint64_t src_size,
21✔
172
                     void *dst, size_t dst_alloc_size, size_t *dst_size, int level) {
173

174
        assert(src);
21✔
175
        assert(src_size > 0);
21✔
176
        assert(dst);
21✔
177
        assert(dst_alloc_size > 0);
21✔
178
        assert(dst_size);
21✔
179

180
#if HAVE_XZ
181
        lzma_options_lzma opt = {
21✔
182
                1u << 20u, NULL, 0, LZMA_LC_DEFAULT, LZMA_LP_DEFAULT,
183
                LZMA_PB_DEFAULT, LZMA_MODE_FAST, 128, LZMA_MF_HC3, 4
184
        };
185
        lzma_filter filters[] = {
21✔
186
                { LZMA_FILTER_LZMA2, &opt },
187
                { LZMA_VLI_UNKNOWN, NULL }
188
        };
189
        lzma_ret ret;
21✔
190
        size_t out_pos = 0;
21✔
191
        int r;
21✔
192

193
        r = dlopen_lzma();
21✔
194
        if (r < 0)
21✔
195
                return r;
21✔
196

197
        if (level >= 0) {
21✔
198
                r = sym_lzma_lzma_preset(&opt, (uint32_t) level);
×
199
                if (r < 0)
200
                        return r;
201
        }
202

203
        /* Returns < 0 if we couldn't compress the data or the
204
         * compressed result is longer than the original */
205

206
        if (src_size < 80)
21✔
207
                return -ENOBUFS;
208

209
        ret = sym_lzma_stream_buffer_encode(filters, LZMA_CHECK_NONE, NULL,
21✔
210
                                        src, src_size, dst, &out_pos, dst_alloc_size);
211
        if (ret != LZMA_OK)
21✔
212
                return -ENOBUFS;
213

214
        *dst_size = out_pos;
17✔
215
        return 0;
17✔
216
#else
217
        return -EPROTONOSUPPORT;
218
#endif
219
}
220

221
#if HAVE_LZ4
222
int dlopen_lz4(void) {
3,080✔
223
        ELF_NOTE_DLOPEN("lz4",
3,080✔
224
                        "Support lz4 compression in journal and coredump files",
225
                        COMPRESSION_PRIORITY_LZ4,
226
                        "liblz4.so.1");
227

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

249
int compress_blob_lz4(const void *src, uint64_t src_size,
146✔
250
                      void *dst, size_t dst_alloc_size, size_t *dst_size, int level) {
251

252
        assert(src);
146✔
253
        assert(src_size > 0);
146✔
254
        assert(dst);
146✔
255
        assert(dst_alloc_size > 0);
146✔
256
        assert(dst_size);
146✔
257

258
#if HAVE_LZ4
259
        int r;
146✔
260

261
        r = dlopen_lz4();
146✔
262
        if (r < 0)
146✔
263
                return r;
264
        /* Returns < 0 if we couldn't compress the data or the
265
         * compressed result is longer than the original */
266

267
        if (src_size < 9)
146✔
268
                return -ENOBUFS;
269

270
        if (level <= 0)
146✔
271
                r = sym_LZ4_compress_default(src, (char*)dst + 8, src_size, (int) dst_alloc_size - 8);
146✔
272
        else
UNCOV
273
                r = sym_LZ4_compress_HC(src, (char*)dst + 8, src_size, (int) dst_alloc_size - 8, level);
×
274
        if (r <= 0)
146✔
275
                return -ENOBUFS;
276

277
        unaligned_write_le64(dst, src_size);
134✔
278
        *dst_size = r + 8;
134✔
279

280
        return 0;
134✔
281
#else
282
        return -EPROTONOSUPPORT;
283
#endif
284
}
285

286
#if HAVE_ZSTD
287
int dlopen_zstd(void) {
247,156✔
288
        ELF_NOTE_DLOPEN("zstd",
247,156✔
289
                        "Support zstd compression in journal and coredump files",
290
                        COMPRESSION_PRIORITY_ZSTD,
291
                        "libzstd.so.1");
292

293
        return dlopen_many_sym_or_warn(
247,156✔
294
                        &zstd_dl,
295
                        "libzstd.so.1", LOG_DEBUG,
296
                        DLSYM_ARG(ZSTD_getErrorCode),
297
                        DLSYM_ARG(ZSTD_compress),
298
                        DLSYM_ARG(ZSTD_getFrameContentSize),
299
                        DLSYM_ARG(ZSTD_decompressStream),
300
                        DLSYM_ARG(ZSTD_getErrorName),
301
                        DLSYM_ARG(ZSTD_DStreamOutSize),
302
                        DLSYM_ARG(ZSTD_CStreamInSize),
303
                        DLSYM_ARG(ZSTD_CStreamOutSize),
304
                        DLSYM_ARG(ZSTD_CCtx_setParameter),
305
                        DLSYM_ARG(ZSTD_compressStream2),
306
                        DLSYM_ARG(ZSTD_DStreamInSize),
307
                        DLSYM_ARG(ZSTD_freeCCtx),
308
                        DLSYM_ARG(ZSTD_freeDCtx),
309
                        DLSYM_ARG(ZSTD_isError),
310
                        DLSYM_ARG(ZSTD_createDCtx),
311
                        DLSYM_ARG(ZSTD_createCCtx));
312
}
313
#endif
314

315
int compress_blob_zstd(
216✔
316
                const void *src, uint64_t src_size,
317
                void *dst, size_t dst_alloc_size, size_t *dst_size, int level) {
318

319
        assert(src);
216✔
320
        assert(src_size > 0);
216✔
321
        assert(dst);
216✔
322
        assert(dst_alloc_size > 0);
216✔
323
        assert(dst_size);
216✔
324

325
#if HAVE_ZSTD
326
        size_t k;
216✔
327
        int r;
216✔
328

329
        r = dlopen_zstd();
216✔
330
        if (r < 0)
216✔
331
                return r;
332

333
        k = sym_ZSTD_compress(dst, dst_alloc_size, src, src_size, level < 0 ? 0 : level);
216✔
334
        if (sym_ZSTD_isError(k))
216✔
335
                return zstd_ret_to_errno(k);
6✔
336

337
        *dst_size = k;
210✔
338
        return 0;
210✔
339
#else
340
        return -EPROTONOSUPPORT;
341
#endif
342
}
343

344
int decompress_blob_xz(
2,543✔
345
                const void *src,
346
                uint64_t src_size,
347
                void **dst,
348
                size_t* dst_size,
349
                size_t dst_max) {
350

351
        assert(src);
2,543✔
352
        assert(src_size > 0);
2,543✔
353
        assert(dst);
2,543✔
354
        assert(dst_size);
2,543✔
355

356
#if HAVE_XZ
357
        _cleanup_(lzma_end_wrapper) lzma_stream s = LZMA_STREAM_INIT;
2,543✔
358
        lzma_ret ret;
2,543✔
359
        size_t space;
2,543✔
360
        int r;
2,543✔
361

362
        r = dlopen_lzma();
2,543✔
363
        if (r < 0)
2,543✔
364
                return r;
365

366
        ret = sym_lzma_stream_decoder(&s, UINT64_MAX, 0);
2,543✔
367
        if (ret != LZMA_OK)
2,543✔
368
                return -ENOMEM;
369

370
        space = MIN(src_size * 2, dst_max ?: SIZE_MAX);
2,543✔
371
        if (!greedy_realloc(dst, space, 1))
2,543✔
372
                return -ENOMEM;
373

374
        s.next_in = src;
2,543✔
375
        s.avail_in = src_size;
2,543✔
376

377
        s.next_out = *dst;
2,543✔
378
        s.avail_out = space;
2,543✔
379

380
        for (;;) {
2,743✔
381
                size_t used;
2,643✔
382

383
                ret = sym_lzma_code(&s, LZMA_FINISH);
2,643✔
384

385
                if (ret == LZMA_STREAM_END)
2,643✔
386
                        break;
387
                else if (ret != LZMA_OK)
106✔
388
                        return -ENOMEM;
389

390
                if (dst_max > 0 && (space - s.avail_out) >= dst_max)
100✔
391
                        break;
392
                else if (dst_max > 0 && space == dst_max)
100✔
393
                        return -ENOBUFS;
394

395
                used = space - s.avail_out;
100✔
396
                space = MIN(2 * space, dst_max ?: SIZE_MAX);
100✔
397
                if (!greedy_realloc(dst, space, 1))
100✔
398
                        return -ENOMEM;
399

400
                s.avail_out = space - used;
100✔
401
                s.next_out = *(uint8_t**)dst + used;
100✔
402
        }
403

404
        *dst_size = space - s.avail_out;
2,537✔
405
        return 0;
2,537✔
406
#else
407
        return -EPROTONOSUPPORT;
408
#endif
409
}
410

411
int decompress_blob_lz4(
2,658✔
412
                const void *src,
413
                uint64_t src_size,
414
                void **dst,
415
                size_t* dst_size,
416
                size_t dst_max) {
417

418
        assert(src);
2,658✔
419
        assert(src_size > 0);
2,658✔
420
        assert(dst);
2,658✔
421
        assert(dst_size);
2,658✔
422

423
#if HAVE_LZ4
424
        char* out;
2,658✔
425
        int r, size; /* LZ4 uses int for size */
2,658✔
426

427
        r = dlopen_lz4();
2,658✔
428
        if (r < 0)
2,658✔
429
                return r;
430

431
        if (src_size <= 8)
2,658✔
432
                return -EBADMSG;
433

434
        size = unaligned_read_le64(src);
2,656✔
435
        if (size < 0 || (unsigned) size != unaligned_read_le64(src))
2,656✔
436
                return -EFBIG;
437
        out = greedy_realloc(dst, size, 1);
2,652✔
438
        if (!out)
2,652✔
439
                return -ENOMEM;
440

441
        r = sym_LZ4_decompress_safe((char*)src + 8, out, src_size - 8, size);
2,652✔
442
        if (r < 0 || r != size)
2,652✔
443
                return -EBADMSG;
444

445
        *dst_size = size;
2,652✔
446
        return 0;
2,652✔
447
#else
448
        return -EPROTONOSUPPORT;
449
#endif
450
}
451

452
int decompress_blob_zstd(
197,574✔
453
                const void *src,
454
                uint64_t src_size,
455
                void **dst,
456
                size_t *dst_size,
457
                size_t dst_max) {
458

459
        assert(src);
197,574✔
460
        assert(src_size > 0);
197,574✔
461
        assert(dst);
197,574✔
462
        assert(dst_size);
197,574✔
463

464
#if HAVE_ZSTD
465
        uint64_t size;
197,574✔
466
        int r;
197,574✔
467

468
        r = dlopen_zstd();
197,574✔
469
        if (r < 0)
197,574✔
470
                return r;
197,574✔
471

472
        size = sym_ZSTD_getFrameContentSize(src, src_size);
197,574✔
473
        if (IN_SET(size, ZSTD_CONTENTSIZE_ERROR, ZSTD_CONTENTSIZE_UNKNOWN))
197,574✔
474
                return -EBADMSG;
475

476
        if (dst_max > 0 && size > dst_max)
197,568✔
477
                size = dst_max;
×
478
        if (size > SIZE_MAX)
197,568✔
479
                return -E2BIG;
480

481
        if (!(greedy_realloc(dst, MAX(sym_ZSTD_DStreamOutSize(), size), 1)))
197,568✔
482
                return -ENOMEM;
483

484
        _cleanup_(sym_ZSTD_freeDCtxp) ZSTD_DCtx *dctx = sym_ZSTD_createDCtx();
395,136✔
485
        if (!dctx)
197,568✔
486
                return -ENOMEM;
487

488
        ZSTD_inBuffer input = {
197,568✔
489
                .src = src,
490
                .size = src_size,
491
        };
492
        ZSTD_outBuffer output = {
395,136✔
493
                .dst = *dst,
197,568✔
494
                .size = MALLOC_SIZEOF_SAFE(*dst),
197,568✔
495
        };
496

497
        size_t k = sym_ZSTD_decompressStream(dctx, &output, &input);
197,568✔
498
        if (sym_ZSTD_isError(k)) {
197,568✔
499
                log_debug("ZSTD decoder failed: %s", sym_ZSTD_getErrorName(k));
×
500
                return zstd_ret_to_errno(k);
×
501
        }
502
        assert(output.pos >= size);
197,568✔
503

504
        *dst_size = size;
197,568✔
505
        return 0;
197,568✔
506
#else
507
        return -EPROTONOSUPPORT;
508
#endif
509
}
510

511
int decompress_blob(
202,549✔
512
                Compression compression,
513
                const void *src,
514
                uint64_t src_size,
515
                void **dst,
516
                size_t* dst_size,
517
                size_t dst_max) {
518

519
        if (compression == COMPRESSION_XZ)
202,549✔
520
                return decompress_blob_xz(
2,530✔
521
                                src, src_size,
522
                                dst, dst_size, dst_max);
523
        else if (compression == COMPRESSION_LZ4)
200,019✔
524
                return decompress_blob_lz4(
2,528✔
525
                                src, src_size,
526
                                dst, dst_size, dst_max);
527
        else if (compression == COMPRESSION_ZSTD)
197,491✔
528
                return decompress_blob_zstd(
197,491✔
529
                                src, src_size,
530
                                dst, dst_size, dst_max);
531
        else
532
                return -EPROTONOSUPPORT;
533
}
534

535
int decompress_startswith_xz(
270✔
536
                const void *src,
537
                uint64_t src_size,
538
                void **buffer,
539
                const void *prefix,
540
                size_t prefix_len,
541
                uint8_t extra) {
542

543
        /* Checks whether the decompressed blob starts with the mentioned prefix. The byte extra needs to
544
         * follow the prefix */
545

546
        assert(src);
270✔
547
        assert(src_size > 0);
270✔
548
        assert(buffer);
270✔
549
        assert(prefix);
270✔
550

551
#if HAVE_XZ
552
        _cleanup_(lzma_end_wrapper) lzma_stream s = LZMA_STREAM_INIT;
270✔
553
        size_t allocated;
270✔
554
        lzma_ret ret;
270✔
555
        int r;
270✔
556

557
        r = dlopen_lzma();
270✔
558
        if (r < 0)
270✔
559
                return r;
560

561
        ret = sym_lzma_stream_decoder(&s, UINT64_MAX, 0);
270✔
562
        if (ret != LZMA_OK)
270✔
563
                return -EBADMSG;
564

565
        if (!(greedy_realloc(buffer, ALIGN_8(prefix_len + 1), 1)))
270✔
566
                return -ENOMEM;
567

568
        allocated = MALLOC_SIZEOF_SAFE(*buffer);
270✔
569

570
        s.next_in = src;
270✔
571
        s.avail_in = src_size;
270✔
572

573
        s.next_out = *buffer;
270✔
574
        s.avail_out = allocated;
270✔
575

576
        for (;;) {
270✔
577
                ret = sym_lzma_code(&s, LZMA_FINISH);
270✔
578

579
                if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
270✔
580
                        return -EBADMSG;
581

582
                if (allocated - s.avail_out >= prefix_len + 1)
270✔
583
                        return memcmp(*buffer, prefix, prefix_len) == 0 &&
540✔
584
                                ((const uint8_t*) *buffer)[prefix_len] == extra;
267✔
585

586
                if (ret == LZMA_STREAM_END)
×
587
                        return 0;
588

589
                s.avail_out += allocated;
×
590

591
                if (!(greedy_realloc(buffer, allocated * 2, 1)))
×
592
                        return -ENOMEM;
593

594
                allocated = MALLOC_SIZEOF_SAFE(*buffer);
×
595
                s.next_out = *(uint8_t**)buffer + allocated - s.avail_out;
×
596
        }
597

598
#else
599
        return -EPROTONOSUPPORT;
600
#endif
601
}
602

603
int decompress_startswith_lz4(
270✔
604
                const void *src,
605
                uint64_t src_size,
606
                void **buffer,
607
                const void *prefix,
608
                size_t prefix_len,
609
                uint8_t extra) {
610

611
        /* Checks whether the decompressed blob starts with the mentioned prefix. The byte extra needs to
612
         * follow the prefix */
613

614
        assert(src);
270✔
615
        assert(src_size > 0);
270✔
616
        assert(buffer);
270✔
617
        assert(prefix);
270✔
618

619
#if HAVE_LZ4
620
        size_t allocated;
270✔
621
        int r;
270✔
622

623
        r = dlopen_lz4();
270✔
624
        if (r < 0)
270✔
625
                return r;
626

627
        if (src_size <= 8)
270✔
628
                return -EBADMSG;
629

630
        if (!(greedy_realloc(buffer, ALIGN_8(prefix_len + 1), 1)))
270✔
631
                return -ENOMEM;
632
        allocated = MALLOC_SIZEOF_SAFE(*buffer);
270✔
633

634
        r = sym_LZ4_decompress_safe_partial(
540✔
635
                        (char*)src + 8,
636
                        *buffer,
637
                        src_size - 8,
270✔
638
                        prefix_len + 1,
270✔
639
                        allocated);
640

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

648
                if (sym_LZ4_versionNumber() >= 10803)
×
649
                        /* We trust that the newer lz4 decompresses the number of bytes we
650
                         * requested if available in the compressed string. */
651
                        return 0;
×
652

653
                if (r > 0)
×
654
                        /* Compare what we have first, in case of mismatch we can
655
                         * shortcut the full comparison. */
656
                        if (memcmp(*buffer, prefix, r) != 0)
×
657
                                return 0;
658

659
                /* Before version 1.8.3, lz4 always tries to decode full a "sequence",
660
                 * so in pathological cases might need to decompress the full field. */
661
                r = decompress_blob_lz4(src, src_size, buffer, &size, 0);
×
662
                if (r < 0)
×
663
                        return r;
664

665
                if (size < prefix_len + 1)
×
666
                        return 0;
667
        }
668

669
        return memcmp(*buffer, prefix, prefix_len) == 0 &&
270✔
670
                ((const uint8_t*) *buffer)[prefix_len] == extra;
267✔
671
#else
672
        return -EPROTONOSUPPORT;
673
#endif
674
}
675

676
int decompress_startswith_zstd(
49,327✔
677
                const void *src,
678
                uint64_t src_size,
679
                void **buffer,
680
                const void *prefix,
681
                size_t prefix_len,
682
                uint8_t extra) {
683

684
        assert(src);
49,327✔
685
        assert(src_size > 0);
49,327✔
686
        assert(buffer);
49,327✔
687
        assert(prefix);
49,327✔
688

689
#if HAVE_ZSTD
690
        int r;
49,327✔
691

692
        r = dlopen_zstd();
49,327✔
693
        if (r < 0)
49,327✔
694
                return r;
49,327✔
695

696
        uint64_t size = sym_ZSTD_getFrameContentSize(src, src_size);
49,327✔
697
        if (IN_SET(size, ZSTD_CONTENTSIZE_ERROR, ZSTD_CONTENTSIZE_UNKNOWN))
49,327✔
698
                return -EBADMSG;
699

700
        if (size < prefix_len + 1)
49,327✔
701
                return 0; /* Decompressed text too short to match the prefix and extra */
702

703
        _cleanup_(sym_ZSTD_freeDCtxp) ZSTD_DCtx *dctx = sym_ZSTD_createDCtx();
98,654✔
704
        if (!dctx)
49,327✔
705
                return -ENOMEM;
706

707
        if (!(greedy_realloc(buffer, MAX(sym_ZSTD_DStreamOutSize(), prefix_len + 1), 1)))
49,327✔
708
                return -ENOMEM;
709

710
        ZSTD_inBuffer input = {
49,327✔
711
                .src = src,
712
                .size = src_size,
713
        };
714
        ZSTD_outBuffer output = {
98,654✔
715
                .dst = *buffer,
49,327✔
716
                .size = MALLOC_SIZEOF_SAFE(*buffer),
49,327✔
717
        };
718
        size_t k;
49,327✔
719

720
        k = sym_ZSTD_decompressStream(dctx, &output, &input);
49,327✔
721
        if (sym_ZSTD_isError(k)) {
49,327✔
722
                log_debug("ZSTD decoder failed: %s", sym_ZSTD_getErrorName(k));
×
723
                return zstd_ret_to_errno(k);
×
724
        }
725
        assert(output.pos >= prefix_len + 1);
49,327✔
726

727
        return memcmp(*buffer, prefix, prefix_len) == 0 &&
49,327✔
728
                ((const uint8_t*) *buffer)[prefix_len] == extra;
371✔
729
#else
730
        return -EPROTONOSUPPORT;
731
#endif
732
}
733

734
int decompress_startswith(
49,069✔
735
                Compression compression,
736
                const void *src,
737
                uint64_t src_size,
738
                void **buffer,
739
                const void *prefix,
740
                size_t prefix_len,
741
                uint8_t extra) {
742

743
        if (compression == COMPRESSION_XZ)
49,069✔
744
                return decompress_startswith_xz(
4✔
745
                                src, src_size,
746
                                buffer,
747
                                prefix, prefix_len,
748
                                extra);
749

750
        else if (compression == COMPRESSION_LZ4)
49,065✔
751
                return decompress_startswith_lz4(
4✔
752
                                src, src_size,
753
                                buffer,
754
                                prefix, prefix_len,
755
                                extra);
756
        else if (compression == COMPRESSION_ZSTD)
49,061✔
757
                return decompress_startswith_zstd(
49,061✔
758
                                src, src_size,
759
                                buffer,
760
                                prefix, prefix_len,
761
                                extra);
762
        else
763
                return -EBADMSG;
764
}
765

766
int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size) {
1✔
767
        assert(fdf >= 0);
1✔
768
        assert(fdt >= 0);
1✔
769

770
#if HAVE_XZ
771
        _cleanup_(lzma_end_wrapper) lzma_stream s = LZMA_STREAM_INIT;
1✔
772
        lzma_ret ret;
1✔
773
        uint8_t buf[BUFSIZ], out[BUFSIZ];
1✔
774
        lzma_action action = LZMA_RUN;
1✔
775
        int r;
1✔
776

777
        r = dlopen_lzma();
1✔
778
        if (r < 0)
1✔
779
                return r;
780

781
        ret = sym_lzma_easy_encoder(&s, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64);
1✔
782
        if (ret != LZMA_OK)
1✔
783
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
784
                                       "Failed to initialize XZ encoder: code %u",
785
                                       ret);
786

787
        for (;;) {
8✔
788
                if (s.avail_in == 0 && action == LZMA_RUN) {
8✔
789
                        size_t m = sizeof(buf);
7✔
790
                        ssize_t n;
7✔
791

792
                        if (max_bytes != UINT64_MAX && (uint64_t) m > max_bytes)
7✔
793
                                m = (size_t) max_bytes;
×
794

795
                        n = read(fdf, buf, m);
7✔
796
                        if (n < 0)
7✔
797
                                return -errno;
×
798
                        if (n == 0)
7✔
799
                                action = LZMA_FINISH;
800
                        else {
801
                                s.next_in = buf;
6✔
802
                                s.avail_in = n;
6✔
803

804
                                if (max_bytes != UINT64_MAX) {
6✔
805
                                        assert(max_bytes >= (uint64_t) n);
×
806
                                        max_bytes -= n;
×
807
                                }
808
                        }
809
                }
810

811
                if (s.avail_out == 0) {
8✔
812
                        s.next_out = out;
2✔
813
                        s.avail_out = sizeof(out);
2✔
814
                }
815

816
                ret = sym_lzma_code(&s, action);
8✔
817
                if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
8✔
818
                        return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
×
819
                                               "Compression failed: code %u",
820
                                               ret);
821

822
                if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
8✔
823
                        ssize_t n, k;
2✔
824

825
                        n = sizeof(out) - s.avail_out;
2✔
826

827
                        k = loop_write(fdt, out, n);
2✔
828
                        if (k < 0)
2✔
829
                                return k;
830

831
                        if (ret == LZMA_STREAM_END) {
2✔
832
                                if (ret_uncompressed_size)
1✔
833
                                        *ret_uncompressed_size = s.total_in;
1✔
834

835
                                log_debug("XZ compression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
1✔
836
                                          s.total_in, s.total_out,
837
                                          (double) s.total_out / s.total_in * 100);
838

839
                                return 0;
1✔
840
                        }
841
                }
842
        }
843
#else
844
        return -EPROTONOSUPPORT;
845
#endif
846
}
847

848
#define LZ4_BUFSIZE (512*1024u)
849

850
int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size) {
1✔
851

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

864
        r = dlopen_lz4();
1✔
865
        if (r < 0)
1✔
866
                return r;
867

868
        c = sym_LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
1✔
869
        if (sym_LZ4F_isError(c))
1✔
870
                return -ENOMEM;
871

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

878
        in_buff = malloc(LZ4_BUFSIZE);
1✔
879
        if (!in_buff)
1✔
880
                return -ENOMEM;
881

882
        n = offset = total_out = sym_LZ4F_compressBegin(ctx, out_buff, out_allocsize, &preferences);
1✔
883
        if (sym_LZ4F_isError(n))
1✔
884
                return -EINVAL;
885

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

888
        for (;;) {
2✔
889
                ssize_t k;
2✔
890

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

901
                total_in += k;
1✔
902
                offset += n;
1✔
903
                total_out += n;
1✔
904

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

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

917
        n = sym_LZ4F_compressEnd(ctx, out_buff + offset, out_allocsize - offset, NULL);
1✔
918
        if (sym_LZ4F_isError(n))
1✔
919
                return -ENOTRECOVERABLE;
920

921
        offset += n;
1✔
922
        total_out += n;
1✔
923
        r = loop_write(fdt, out_buff, offset);
1✔
924
        if (r < 0)
1✔
925
                return r;
926

927
        if (ret_uncompressed_size)
1✔
928
                *ret_uncompressed_size = total_in;
1✔
929

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
                                log_debug("XZ decompression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
1✔
1006
                                          s.total_in, s.total_out,
1007
                                          (double) s.total_out / s.total_in * 100);
1008

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

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

1029
        r = dlopen_lz4();
3✔
1030
        if (r < 0)
3✔
1031
                return r;
1032

1033
        c = sym_LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
3✔
1034
        if (sym_LZ4F_isError(c))
3✔
1035
                return -ENOMEM;
1036

1037
        if (fstat(in, &st) < 0)
3✔
1038
                return log_debug_errno(errno, "fstat() failed: %m");
×
1039

1040
        if (file_offset_beyond_memory_size(st.st_size))
3✔
1041
                return -EFBIG;
1042

1043
        buf = malloc(LZ4_BUFSIZE);
3✔
1044
        if (!buf)
3✔
1045
                return -ENOMEM;
1046

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

1051
        while (total_in < (size_t) st.st_size) {
5✔
1052
                size_t produced = LZ4_BUFSIZE;
3✔
1053
                size_t used = st.st_size - total_in;
3✔
1054

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

1061
                total_in += used;
3✔
1062
                total_out += produced;
3✔
1063

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

1070
                r = loop_write(out, buf, produced);
2✔
1071
                if (r < 0)
2✔
1072
                        goto cleanup;
×
1073
        }
1074

1075
        log_debug("LZ4 decompression finished (%zu -> %zu bytes, %.1f%%)",
2✔
1076
                  total_in, total_out,
1077
                  total_in > 0 ? (double) total_out / total_in * 100 : 0.0);
1078
        r = 0;
1079
 cleanup:
3✔
1080
        munmap(src, st.st_size);
3✔
1081
        return r;
3✔
1082
#else
1083
        return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT),
1084
                               "Cannot decompress file. Compiled without LZ4 support.");
1085
#endif
1086
}
1087

1088
int compress_stream_zstd(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size) {
25✔
1089
        assert(fdf >= 0);
25✔
1090
        assert(fdt >= 0);
25✔
1091

1092
#if HAVE_ZSTD
1093
        _cleanup_(sym_ZSTD_freeCCtxp) ZSTD_CCtx *cctx = NULL;
×
1094
        _cleanup_free_ void *in_buff = NULL, *out_buff = NULL;
25✔
1095
        size_t in_allocsize, out_allocsize;
25✔
1096
        size_t z;
25✔
1097
        uint64_t left = max_bytes, in_bytes = 0;
25✔
1098
        int r;
25✔
1099

1100
        r = dlopen_zstd();
25✔
1101
        if (r < 0)
25✔
1102
                return r;
1103

1104
        /* Create the context and buffers */
1105
        in_allocsize = sym_ZSTD_CStreamInSize();
25✔
1106
        out_allocsize = sym_ZSTD_CStreamOutSize();
25✔
1107
        in_buff = malloc(in_allocsize);
25✔
1108
        out_buff = malloc(out_allocsize);
25✔
1109
        cctx = sym_ZSTD_createCCtx();
25✔
1110
        if (!cctx || !out_buff || !in_buff)
25✔
1111
                return -ENOMEM;
1112

1113
        z = sym_ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1);
25✔
1114
        if (sym_ZSTD_isError(z))
25✔
1115
                log_debug("Failed to enable ZSTD checksum, ignoring: %s", sym_ZSTD_getErrorName(z));
×
1116

1117
        /* This loop read from the input file, compresses that entire chunk,
1118
         * and writes all output produced to the output file.
1119
         */
1120
        for (;;) {
197✔
1121
                bool is_last_chunk;
222✔
1122
                ZSTD_inBuffer input = {
222✔
1123
                        .src = in_buff,
1124
                        .size = 0,
1125
                        .pos = 0
1126
                };
1127
                ssize_t red;
222✔
1128

1129
                red = loop_read(fdf, in_buff, in_allocsize, true);
222✔
1130
                if (red < 0)
222✔
1131
                        return red;
×
1132
                is_last_chunk = red == 0;
222✔
1133

1134
                in_bytes += (size_t) red;
222✔
1135
                input.size = (size_t) red;
222✔
1136

1137
                for (bool finished = false; !finished;) {
444✔
1138
                        ZSTD_outBuffer output = {
222✔
1139
                                .dst = out_buff,
1140
                                .size = out_allocsize,
1141
                                .pos = 0
1142
                        };
1143
                        size_t remaining;
222✔
1144
                        ssize_t wrote;
222✔
1145

1146
                        /* Compress into the output buffer and write all of the
1147
                         * output to the file so we can reuse the buffer next
1148
                         * iteration.
1149
                         */
1150
                        remaining = sym_ZSTD_compressStream2(
419✔
1151
                                cctx, &output, &input,
1152
                                is_last_chunk ? ZSTD_e_end : ZSTD_e_continue);
1153

1154
                        if (sym_ZSTD_isError(remaining)) {
222✔
1155
                                log_debug("ZSTD encoder failed: %s", sym_ZSTD_getErrorName(remaining));
×
1156
                                return zstd_ret_to_errno(remaining);
×
1157
                        }
1158

1159
                        if (left < output.pos)
222✔
1160
                                return -EFBIG;
1161

1162
                        wrote = loop_write_full(fdt, output.dst, output.pos, USEC_INFINITY);
222✔
1163
                        if (wrote < 0)
222✔
1164
                                return wrote;
1165

1166
                        left -= output.pos;
222✔
1167

1168
                        /* If we're on the last chunk we're finished when zstd
1169
                         * returns 0, which means its consumed all the input AND
1170
                         * finished the frame. Otherwise, we're finished when
1171
                         * we've consumed all the input.
1172
                         */
1173
                        finished = is_last_chunk ? (remaining == 0) : (input.pos == input.size);
222✔
1174
                }
1175

1176
                /* zstd only returns 0 when the input is completely consumed */
1177
                assert(input.pos == input.size);
222✔
1178
                if (is_last_chunk)
222✔
1179
                        break;
1180
        }
1181

1182
        if (ret_uncompressed_size)
25✔
1183
                *ret_uncompressed_size = in_bytes;
25✔
1184

1185
        if (in_bytes > 0)
25✔
1186
                log_debug("ZSTD compression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
25✔
1187
                          in_bytes, max_bytes - left, (double) (max_bytes - left) / in_bytes * 100);
1188
        else
1189
                log_debug("ZSTD compression finished (%" PRIu64 " -> %" PRIu64 " bytes)",
×
1190
                          in_bytes, max_bytes - left);
1191

1192
        return 0;
1193
#else
1194
        return -EPROTONOSUPPORT;
1195
#endif
1196
}
1197

1198
int decompress_stream_zstd(int fdf, int fdt, uint64_t max_bytes) {
13✔
1199
        assert(fdf >= 0);
13✔
1200
        assert(fdt >= 0);
13✔
1201

1202
#if HAVE_ZSTD
1203
        _cleanup_(sym_ZSTD_freeDCtxp) ZSTD_DCtx *dctx = NULL;
×
1204
        _cleanup_free_ void *in_buff = NULL, *out_buff = NULL;
13✔
1205
        size_t in_allocsize, out_allocsize;
13✔
1206
        size_t last_result = 0;
13✔
1207
        uint64_t left = max_bytes, in_bytes = 0;
13✔
1208
        int r;
13✔
1209

1210
        r = dlopen_zstd();
13✔
1211
        if (r < 0)
13✔
1212
                return r;
1213
        /* Create the context and buffers */
1214
        in_allocsize = sym_ZSTD_DStreamInSize();
13✔
1215
        out_allocsize = sym_ZSTD_DStreamOutSize();
13✔
1216
        in_buff = malloc(in_allocsize);
13✔
1217
        out_buff = malloc(out_allocsize);
13✔
1218
        dctx = sym_ZSTD_createDCtx();
13✔
1219
        if (!dctx || !out_buff || !in_buff)
13✔
1220
                return -ENOMEM;
1221

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

1237
                red = loop_read(fdf, in_buff, in_allocsize, true);
23✔
1238
                if (red < 0)
23✔
1239
                        return red;
2✔
1240
                if (red == 0)
23✔
1241
                        break;
1242

1243
                in_bytes += (size_t) red;
13✔
1244
                input.size = (size_t) red;
13✔
1245
                input.pos = 0;
13✔
1246

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

1273
                        if (left < output.pos)
48✔
1274
                                return -EFBIG;
2✔
1275

1276
                        wrote = loop_write_full(fdt, output.dst, output.pos, USEC_INFINITY);
47✔
1277
                        if (wrote < 0)
47✔
1278
                                return wrote;
1279

1280
                        left -= output.pos;
46✔
1281
                }
1282
                if (has_error)
11✔
1283
                        break;
1284
        }
1285

1286
        if (in_bytes == 0)
11✔
1287
                return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "ZSTD decoder failed: no data read");
×
1288

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

1298
        log_debug(
10✔
1299
                "ZSTD decompression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
1300
                in_bytes,
1301
                max_bytes - left,
1302
                (double) (max_bytes - left) / in_bytes * 100);
1303
        return 0;
1304
#else
1305
        return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT),
1306
                               "Cannot decompress file. Compiled without ZSTD support.");
1307
#endif
1308
}
1309

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

1312
        if (endswith(filename, ".lz4"))
10✔
1313
                return decompress_stream_lz4(fdf, fdt, max_bytes);
×
1314
        else if (endswith(filename, ".xz"))
10✔
1315
                return decompress_stream_xz(fdf, fdt, max_bytes);
×
1316
        else if (endswith(filename, ".zst"))
10✔
1317
                return decompress_stream_zstd(fdf, fdt, max_bytes);
10✔
1318
        else
1319
                return -EPROTONOSUPPORT;
1320
}
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