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

systemd / systemd / 17688773592

12 Sep 2025 11:20PM UTC coverage: 72.201% (-0.06%) from 72.257%
17688773592

push

github

bluca
TEST-55-OOMD: Verify that ExecStopPost= runs on oom-kill

302423 of 418862 relevant lines covered (72.2%)

1071140.14 hits per line

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

90.06
/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);
21✔
79
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(ZSTD_DCtx*, sym_ZSTD_freeDCtx, NULL);
4,263,934✔
80

81
static int zstd_ret_to_errno(size_t ret) {
4✔
82
        switch (sym_ZSTD_getErrorCode(ret)) {
4✔
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,838✔
111
        sym_lzma_end(ls);
2,838✔
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);
486✔
132
DEFINE_STRING_TABLE_LOOKUP(compression_lowercase, Compression);
85✔
133

134
bool compression_supported(Compression c) {
2,683✔
135
        static const unsigned supported =
2,683✔
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,683✔
142
        assert(c < _COMPRESSION_MAX);
2,683✔
143

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

147
#if HAVE_XZ
148
int dlopen_lzma(void) {
2,867✔
149
        ELF_NOTE_DLOPEN("lzma",
2,867✔
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,867✔
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,
28✔
167
                     void *dst, size_t dst_alloc_size, size_t *dst_size, int level) {
168

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

175
#if HAVE_XZ
176
        lzma_options_lzma opt = {
28✔
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[] = {
28✔
181
                { LZMA_FILTER_LZMA2, &opt },
182
                { LZMA_VLI_UNKNOWN, NULL }
183
        };
184
        lzma_ret ret;
28✔
185
        size_t out_pos = 0;
28✔
186
        int r;
28✔
187

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

192
        if (level >= 0) {
28✔
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)
28✔
202
                return -ENOBUFS;
203

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

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

216
#if HAVE_LZ4
217
int dlopen_lz4(void) {
3,114✔
218
        ELF_NOTE_DLOPEN("lz4",
3,114✔
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,114✔
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,
163✔
245
                      void *dst, size_t dst_alloc_size, size_t *dst_size, int level) {
246

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

253
#if HAVE_LZ4
254
        int r;
163✔
255

256
        r = dlopen_lz4();
163✔
257
        if (r < 0)
163✔
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)
163✔
263
                return -ENOBUFS;
264

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

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

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

281
#if HAVE_ZSTD
282
int dlopen_zstd(void) {
4,264,193✔
283
        ELF_NOTE_DLOPEN("zstd",
4,264,193✔
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(
4,264,193✔
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(
231✔
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);
231✔
315
        assert(src_size > 0);
231✔
316
        assert(dst);
231✔
317
        assert(dst_alloc_size > 0);
231✔
318
        assert(dst_size);
231✔
319

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

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

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

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

339
int decompress_blob_xz(
2,564✔
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,564✔
347
        assert(src_size > 0);
2,564✔
348
        assert(dst);
2,564✔
349
        assert(dst_size);
2,564✔
350

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

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

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

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

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

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

375
        for (;;) {
2,930✔
376
                size_t used;
2,747✔
377

378
                ret = sym_lzma_code(&s, LZMA_FINISH);
2,747✔
379
                if (ret == LZMA_STREAM_END)
2,747✔
380
                        break;
381
                if (ret != LZMA_OK)
189✔
382
                        return -ENOMEM;
383

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

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

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

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

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

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

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

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

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

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

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

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

446
int decompress_blob_zstd(
208,599✔
447
                const void *src,
448
                uint64_t src_size,
449
                void **dst,
450
                size_t *dst_size,
451
                size_t dst_max) {
452

453
        assert(src);
208,599✔
454
        assert(src_size > 0);
208,599✔
455
        assert(dst);
208,599✔
456
        assert(dst_size);
208,599✔
457

458
#if HAVE_ZSTD
459
        uint64_t size;
208,599✔
460
        int r;
208,599✔
461

462
        r = dlopen_zstd();
208,599✔
463
        if (r < 0)
208,599✔
464
                return r;
208,599✔
465

466
        size = sym_ZSTD_getFrameContentSize(src, src_size);
208,599✔
467
        if (IN_SET(size, ZSTD_CONTENTSIZE_ERROR, ZSTD_CONTENTSIZE_UNKNOWN))
208,599✔
468
                return -EBADMSG;
469

470
        if (dst_max > 0 && size > dst_max)
208,593✔
471
                size = dst_max;
×
472
        if (size > SIZE_MAX)
208,593✔
473
                return -E2BIG;
474

475
        if (!(greedy_realloc(dst, MAX(sym_ZSTD_DStreamOutSize(), size), 1)))
208,593✔
476
                return -ENOMEM;
477

478
        _cleanup_(sym_ZSTD_freeDCtxp) ZSTD_DCtx *dctx = sym_ZSTD_createDCtx();
417,186✔
479
        if (!dctx)
208,593✔
480
                return -ENOMEM;
481

482
        ZSTD_inBuffer input = {
208,593✔
483
                .src = src,
484
                .size = src_size,
485
        };
486
        ZSTD_outBuffer output = {
417,186✔
487
                .dst = *dst,
208,593✔
488
                .size = MALLOC_SIZEOF_SAFE(*dst),
208,593✔
489
        };
490

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

497
        *dst_size = size;
208,593✔
498
        return 0;
208,593✔
499
#else
500
        return -EPROTONOSUPPORT;
501
#endif
502
}
503

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

512
        switch (compression) {
213,561✔
513
        case COMPRESSION_XZ:
2,543✔
514
                return decompress_blob_xz(
2,543✔
515
                                src, src_size,
516
                                dst, dst_size, dst_max);
517
        case COMPRESSION_LZ4:
2,525✔
518
                return decompress_blob_lz4(
2,525✔
519
                                src, src_size,
520
                                dst, dst_size, dst_max);
521
        case COMPRESSION_ZSTD:
208,493✔
522
                return decompress_blob_zstd(
208,493✔
523
                                src, src_size,
524
                                dst, dst_size, dst_max);
525
        default:
526
                return -EPROTONOSUPPORT;
527
        }
528
}
529

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

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

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

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

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

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

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

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

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

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

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

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

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

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

584
                s.avail_out += allocated;
×
585

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

671
int decompress_startswith_zstd(
4,055,328✔
672
                const void *src,
673
                uint64_t src_size,
674
                void **buffer,
675
                const void *prefix,
676
                size_t prefix_len,
677
                uint8_t extra) {
678

679
        assert(src);
4,055,328✔
680
        assert(src_size > 0);
4,055,328✔
681
        assert(buffer);
4,055,328✔
682
        assert(prefix);
4,055,328✔
683

684
#if HAVE_ZSTD
685
        int r;
4,055,328✔
686

687
        r = dlopen_zstd();
4,055,328✔
688
        if (r < 0)
4,055,328✔
689
                return r;
4,055,328✔
690

691
        uint64_t size = sym_ZSTD_getFrameContentSize(src, src_size);
4,055,328✔
692
        if (IN_SET(size, ZSTD_CONTENTSIZE_ERROR, ZSTD_CONTENTSIZE_UNKNOWN))
4,055,328✔
693
                return -EBADMSG;
694

695
        if (size < prefix_len + 1)
4,055,328✔
696
                return 0; /* Decompressed text too short to match the prefix and extra */
697

698
        _cleanup_(sym_ZSTD_freeDCtxp) ZSTD_DCtx *dctx = sym_ZSTD_createDCtx();
8,110,656✔
699
        if (!dctx)
4,055,328✔
700
                return -ENOMEM;
701

702
        if (!(greedy_realloc(buffer, MAX(sym_ZSTD_DStreamOutSize(), prefix_len + 1), 1)))
4,055,328✔
703
                return -ENOMEM;
704

705
        ZSTD_inBuffer input = {
4,055,328✔
706
                .src = src,
707
                .size = src_size,
708
        };
709
        ZSTD_outBuffer output = {
8,110,656✔
710
                .dst = *buffer,
4,055,328✔
711
                .size = MALLOC_SIZEOF_SAFE(*buffer),
4,055,328✔
712
        };
713
        size_t k;
4,055,328✔
714

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

722
        return memcmp(*buffer, prefix, prefix_len) == 0 &&
4,055,328✔
723
                ((const uint8_t*) *buffer)[prefix_len] == extra;
13,194✔
724
#else
725
        return -EPROTONOSUPPORT;
726
#endif
727
}
728

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

738
        switch (compression) {
4,055,070✔
739

740
        case COMPRESSION_XZ:
4✔
741
                return decompress_startswith_xz(
4✔
742
                                src, src_size,
743
                                buffer,
744
                                prefix, prefix_len,
745
                                extra);
746

747
        case COMPRESSION_LZ4:
4✔
748
                return decompress_startswith_lz4(
4✔
749
                                src, src_size,
750
                                buffer,
751
                                prefix, prefix_len,
752
                                extra);
753
        case COMPRESSION_ZSTD:
4,055,062✔
754
                return decompress_startswith_zstd(
4,055,062✔
755
                                src, src_size,
756
                                buffer,
757
                                prefix, prefix_len,
758
                                extra);
759
        default:
760
                return -EBADMSG;
761
        }
762
}
763

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

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

775
        r = dlopen_lzma();
1✔
776
        if (r < 0)
1✔
777
                return r;
778

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

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

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

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

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

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

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

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

823
                        n = sizeof(out) - s.avail_out;
2✔
824

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

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

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

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

849
#define LZ4_BUFSIZE (512*1024u)
850

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

938
        return 0;
939
#else
940
        return -EPROTONOSUPPORT;
941
#endif
942
}
943

944
int decompress_stream_xz(int fdf, int fdt, uint64_t max_bytes) {
3✔
945
        assert(fdf >= 0);
3✔
946
        assert(fdt >= 0);
3✔
947

948
#if HAVE_XZ
949
        _cleanup_(lzma_end_wrapper) lzma_stream s = LZMA_STREAM_INIT;
3✔
950
        lzma_ret ret;
3✔
951

952
        uint8_t buf[BUFSIZ], out[BUFSIZ];
3✔
953
        lzma_action action = LZMA_RUN;
3✔
954
        int r;
3✔
955

956
        r = dlopen_lzma();
3✔
957
        if (r < 0)
3✔
958
                return r;
959

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

966
        for (;;) {
15✔
967
                if (s.avail_in == 0 && action == LZMA_RUN) {
15✔
968
                        ssize_t n;
5✔
969

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

981
                if (s.avail_out == 0) {
15✔
982
                        s.next_out = out;
13✔
983
                        s.avail_out = sizeof(out);
13✔
984
                }
985

986
                ret = sym_lzma_code(&s, action);
15✔
987
                if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
15✔
988
                        return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
1✔
989
                                               "Decompression failed: code %u",
990
                                               ret);
991

992
                if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
14✔
993
                        ssize_t n, k;
12✔
994

995
                        n = sizeof(out) - s.avail_out;
12✔
996

997
                        if (max_bytes != UINT64_MAX) {
12✔
998
                                if (max_bytes < (uint64_t) n)
12✔
999
                                        return -EFBIG;
1000

1001
                                max_bytes -= n;
11✔
1002
                        }
1003

1004
                        k = loop_write(fdt, out, n);
11✔
1005
                        if (k < 0)
11✔
1006
                                return k;
1007

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

1016
                                return 0;
1✔
1017
                        }
1018
                }
1019
        }
1020
#else
1021
        return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT),
1022
                               "Cannot decompress file. Compiled without XZ support.");
1023
#endif
1024
}
1025

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

1036
        r = dlopen_lz4();
3✔
1037
        if (r < 0)
3✔
1038
                return r;
1039

1040
        c = sym_LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
3✔
1041
        if (sym_LZ4F_isError(c))
3✔
1042
                return -ENOMEM;
1043

1044
        if (fstat(in, &st) < 0)
3✔
1045
                return log_debug_errno(errno, "fstat() failed: %m");
×
1046

1047
        if (file_offset_beyond_memory_size(st.st_size))
3✔
1048
                return -EFBIG;
1049

1050
        buf = malloc(LZ4_BUFSIZE);
3✔
1051
        if (!buf)
3✔
1052
                return -ENOMEM;
1053

1054
        src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, in, 0);
3✔
1055
        if (src == MAP_FAILED)
3✔
1056
                return -errno;
×
1057

1058
        while (total_in < (size_t) st.st_size) {
5✔
1059
                size_t produced = LZ4_BUFSIZE;
3✔
1060
                size_t used = st.st_size - total_in;
3✔
1061

1062
                c = sym_LZ4F_decompress(ctx, buf, &produced, src + total_in, &used, NULL);
3✔
1063
                if (sym_LZ4F_isError(c)) {
3✔
1064
                        r = -EBADMSG;
×
1065
                        goto cleanup;
1✔
1066
                }
1067

1068
                total_in += used;
3✔
1069
                total_out += produced;
3✔
1070

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

1077
                r = loop_write(out, buf, produced);
2✔
1078
                if (r < 0)
2✔
1079
                        goto cleanup;
×
1080
        }
1081

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

1098
int compress_stream_zstd(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size) {
21✔
1099
        assert(fdf >= 0);
21✔
1100
        assert(fdt >= 0);
21✔
1101

1102
#if HAVE_ZSTD
1103
        _cleanup_(sym_ZSTD_freeCCtxp) ZSTD_CCtx *cctx = NULL;
×
1104
        _cleanup_free_ void *in_buff = NULL, *out_buff = NULL;
21✔
1105
        size_t in_allocsize, out_allocsize;
21✔
1106
        size_t z;
21✔
1107
        uint64_t left = max_bytes, in_bytes = 0;
21✔
1108
        int r;
21✔
1109

1110
        r = dlopen_zstd();
21✔
1111
        if (r < 0)
21✔
1112
                return r;
1113

1114
        /* Create the context and buffers */
1115
        in_allocsize = sym_ZSTD_CStreamInSize();
21✔
1116
        out_allocsize = sym_ZSTD_CStreamOutSize();
21✔
1117
        in_buff = malloc(in_allocsize);
21✔
1118
        out_buff = malloc(out_allocsize);
21✔
1119
        cctx = sym_ZSTD_createCCtx();
21✔
1120
        if (!cctx || !out_buff || !in_buff)
21✔
1121
                return -ENOMEM;
1122

1123
        z = sym_ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1);
21✔
1124
        if (sym_ZSTD_isError(z))
21✔
1125
                log_debug("Failed to enable ZSTD checksum, ignoring: %s", sym_ZSTD_getErrorName(z));
×
1126

1127
        /* This loop read from the input file, compresses that entire chunk,
1128
         * and writes all output produced to the output file.
1129
         */
1130
        for (;;) {
179✔
1131
                bool is_last_chunk;
200✔
1132
                ZSTD_inBuffer input = {
200✔
1133
                        .src = in_buff,
1134
                        .size = 0,
1135
                        .pos = 0
1136
                };
1137
                ssize_t red;
200✔
1138

1139
                red = loop_read(fdf, in_buff, in_allocsize, true);
200✔
1140
                if (red < 0)
200✔
1141
                        return red;
×
1142
                is_last_chunk = red == 0;
200✔
1143

1144
                in_bytes += (size_t) red;
200✔
1145
                input.size = (size_t) red;
200✔
1146

1147
                for (bool finished = false; !finished;) {
400✔
1148
                        ZSTD_outBuffer output = {
200✔
1149
                                .dst = out_buff,
1150
                                .size = out_allocsize,
1151
                                .pos = 0
1152
                        };
1153
                        size_t remaining;
200✔
1154
                        ssize_t wrote;
200✔
1155

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

1164
                        if (sym_ZSTD_isError(remaining)) {
200✔
1165
                                log_debug("ZSTD encoder failed: %s", sym_ZSTD_getErrorName(remaining));
×
1166
                                return zstd_ret_to_errno(remaining);
×
1167
                        }
1168

1169
                        if (left < output.pos)
200✔
1170
                                return -EFBIG;
1171

1172
                        wrote = loop_write_full(fdt, output.dst, output.pos, USEC_INFINITY);
200✔
1173
                        if (wrote < 0)
200✔
1174
                                return wrote;
1175

1176
                        left -= output.pos;
200✔
1177

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

1186
                /* zstd only returns 0 when the input is completely consumed */
1187
                assert(input.pos == input.size);
200✔
1188
                if (is_last_chunk)
200✔
1189
                        break;
1190
        }
1191

1192
        if (ret_uncompressed_size)
21✔
1193
                *ret_uncompressed_size = in_bytes;
21✔
1194

1195
        if (in_bytes == 0)
21✔
1196
                log_debug("ZSTD compression finished (no input data)");
×
1197
        else
1198
                log_debug("ZSTD compression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
21✔
1199
                          in_bytes, max_bytes - left, (double) (max_bytes - left) / in_bytes * 100);
1200

1201
        return 0;
1202
#else
1203
        return -EPROTONOSUPPORT;
1204
#endif
1205
}
1206

1207
int decompress_stream_zstd(int fdf, int fdt, uint64_t max_bytes) {
13✔
1208
        assert(fdf >= 0);
13✔
1209
        assert(fdt >= 0);
13✔
1210

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

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

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

1246
                red = loop_read(fdf, in_buff, in_allocsize, true);
23✔
1247
                if (red < 0)
23✔
1248
                        return red;
2✔
1249
                if (red == 0)
23✔
1250
                        break;
1251

1252
                in_bytes += (size_t) red;
13✔
1253
                input.size = (size_t) red;
13✔
1254
                input.pos = 0;
13✔
1255

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

1282
                        if (left < output.pos)
48✔
1283
                                return -EFBIG;
2✔
1284

1285
                        wrote = loop_write_full(fdt, output.dst, output.pos, USEC_INFINITY);
47✔
1286
                        if (wrote < 0)
47✔
1287
                                return wrote;
1288

1289
                        left -= output.pos;
46✔
1290
                }
1291
                if (has_error)
11✔
1292
                        break;
1293
        }
1294

1295
        if (in_bytes == 0)
11✔
1296
                return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "ZSTD decoder failed: no data read");
×
1297

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

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

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

1323
        if (endswith(filename, ".lz4"))
10✔
1324
                return decompress_stream_lz4(fdf, fdt, max_bytes);
×
1325
        if (endswith(filename, ".xz"))
10✔
1326
                return decompress_stream_xz(fdf, fdt, max_bytes);
×
1327
        if (endswith(filename, ".zst"))
10✔
1328
                return decompress_stream_zstd(fdf, fdt, max_bytes);
10✔
1329

1330
        return -EPROTONOSUPPORT;
1331
}
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