]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/compress.c
Merge pull request #15891 from bluca/host_os_release
[thirdparty/systemd.git] / src / journal / compress.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <inttypes.h>
4 #include <stdlib.h>
5 #include <sys/mman.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9
10 #if HAVE_XZ
11 #include <lzma.h>
12 #endif
13
14 #if HAVE_LZ4
15 #include <lz4.h>
16 #include <lz4frame.h>
17 #endif
18
19 #if HAVE_ZSTD
20 #include <zstd.h>
21 #include <zstd_errors.h>
22 #endif
23
24 #include "alloc-util.h"
25 #include "compress.h"
26 #include "fd-util.h"
27 #include "io-util.h"
28 #include "journal-def.h"
29 #include "macro.h"
30 #include "sparse-endian.h"
31 #include "string-table.h"
32 #include "string-util.h"
33 #include "unaligned.h"
34 #include "util.h"
35
36 #if HAVE_LZ4
37 DEFINE_TRIVIAL_CLEANUP_FUNC(LZ4F_compressionContext_t, LZ4F_freeCompressionContext);
38 DEFINE_TRIVIAL_CLEANUP_FUNC(LZ4F_decompressionContext_t, LZ4F_freeDecompressionContext);
39 #endif
40
41 #if HAVE_ZSTD
42 DEFINE_TRIVIAL_CLEANUP_FUNC(ZSTD_CCtx *, ZSTD_freeCCtx);
43 DEFINE_TRIVIAL_CLEANUP_FUNC(ZSTD_DCtx *, ZSTD_freeDCtx);
44
45 static int zstd_ret_to_errno(size_t ret) {
46 switch (ZSTD_getErrorCode(ret)) {
47 case ZSTD_error_dstSize_tooSmall:
48 return -ENOBUFS;
49 case ZSTD_error_memory_allocation:
50 return -ENOMEM;
51 default:
52 return -EBADMSG;
53 }
54 }
55 #endif
56
57 #define ALIGN_8(l) ALIGN_TO(l, sizeof(size_t))
58
59 static const char* const object_compressed_table[_OBJECT_COMPRESSED_MAX] = {
60 [OBJECT_COMPRESSED_XZ] = "XZ",
61 [OBJECT_COMPRESSED_LZ4] = "LZ4",
62 [OBJECT_COMPRESSED_ZSTD] = "ZSTD",
63 /* If we add too many more entries here, it's going to grow quite large (and be mostly sparse), since
64 * the array key is actually a bitmask, not a plain enum */
65 };
66
67 DEFINE_STRING_TABLE_LOOKUP(object_compressed, int);
68
69 int compress_blob_xz(const void *src, uint64_t src_size,
70 void *dst, size_t dst_alloc_size, size_t *dst_size) {
71 #if HAVE_XZ
72 static const lzma_options_lzma opt = {
73 1u << 20u, NULL, 0, LZMA_LC_DEFAULT, LZMA_LP_DEFAULT,
74 LZMA_PB_DEFAULT, LZMA_MODE_FAST, 128, LZMA_MF_HC3, 4
75 };
76 static const lzma_filter filters[] = {
77 { LZMA_FILTER_LZMA2, (lzma_options_lzma*) &opt },
78 { LZMA_VLI_UNKNOWN, NULL }
79 };
80 lzma_ret ret;
81 size_t out_pos = 0;
82
83 assert(src);
84 assert(src_size > 0);
85 assert(dst);
86 assert(dst_alloc_size > 0);
87 assert(dst_size);
88
89 /* Returns < 0 if we couldn't compress the data or the
90 * compressed result is longer than the original */
91
92 if (src_size < 80)
93 return -ENOBUFS;
94
95 ret = lzma_stream_buffer_encode((lzma_filter*) filters, LZMA_CHECK_NONE, NULL,
96 src, src_size, dst, &out_pos, dst_alloc_size);
97 if (ret != LZMA_OK)
98 return -ENOBUFS;
99
100 *dst_size = out_pos;
101 return 0;
102 #else
103 return -EPROTONOSUPPORT;
104 #endif
105 }
106
107 int compress_blob_lz4(const void *src, uint64_t src_size,
108 void *dst, size_t dst_alloc_size, size_t *dst_size) {
109 #if HAVE_LZ4
110 int r;
111
112 assert(src);
113 assert(src_size > 0);
114 assert(dst);
115 assert(dst_alloc_size > 0);
116 assert(dst_size);
117
118 /* Returns < 0 if we couldn't compress the data or the
119 * compressed result is longer than the original */
120
121 if (src_size < 9)
122 return -ENOBUFS;
123
124 r = LZ4_compress_default(src, (char*)dst + 8, src_size, (int) dst_alloc_size - 8);
125 if (r <= 0)
126 return -ENOBUFS;
127
128 unaligned_write_le64(dst, src_size);
129 *dst_size = r + 8;
130
131 return 0;
132 #else
133 return -EPROTONOSUPPORT;
134 #endif
135 }
136
137 int compress_blob_zstd(
138 const void *src, uint64_t src_size,
139 void *dst, size_t dst_alloc_size, size_t *dst_size) {
140 #if HAVE_ZSTD
141 size_t k;
142
143 assert(src);
144 assert(src_size > 0);
145 assert(dst);
146 assert(dst_alloc_size > 0);
147 assert(dst_size);
148
149 k = ZSTD_compress(dst, dst_alloc_size, src, src_size, 0);
150 if (ZSTD_isError(k))
151 return zstd_ret_to_errno(k);
152
153 *dst_size = k;
154 return 0;
155 #else
156 return -EPROTONOSUPPORT;
157 #endif
158 }
159
160 int decompress_blob_xz(const void *src, uint64_t src_size,
161 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
162
163 #if HAVE_XZ
164 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
165 lzma_ret ret;
166 size_t space;
167
168 assert(src);
169 assert(src_size > 0);
170 assert(dst);
171 assert(dst_alloc_size);
172 assert(dst_size);
173 assert(*dst_alloc_size == 0 || *dst);
174
175 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
176 if (ret != LZMA_OK)
177 return -ENOMEM;
178
179 space = MIN(src_size * 2, dst_max ?: (size_t) -1);
180 if (!greedy_realloc(dst, dst_alloc_size, space, 1))
181 return -ENOMEM;
182
183 s.next_in = src;
184 s.avail_in = src_size;
185
186 s.next_out = *dst;
187 s.avail_out = space;
188
189 for (;;) {
190 size_t used;
191
192 ret = lzma_code(&s, LZMA_FINISH);
193
194 if (ret == LZMA_STREAM_END)
195 break;
196 else if (ret != LZMA_OK)
197 return -ENOMEM;
198
199 if (dst_max > 0 && (space - s.avail_out) >= dst_max)
200 break;
201 else if (dst_max > 0 && space == dst_max)
202 return -ENOBUFS;
203
204 used = space - s.avail_out;
205 space = MIN(2 * space, dst_max ?: (size_t) -1);
206 if (!greedy_realloc(dst, dst_alloc_size, space, 1))
207 return -ENOMEM;
208
209 s.avail_out = space - used;
210 s.next_out = *(uint8_t**)dst + used;
211 }
212
213 *dst_size = space - s.avail_out;
214 return 0;
215 #else
216 return -EPROTONOSUPPORT;
217 #endif
218 }
219
220 int decompress_blob_lz4(const void *src, uint64_t src_size,
221 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
222
223 #if HAVE_LZ4
224 char* out;
225 int r, size; /* LZ4 uses int for size */
226
227 assert(src);
228 assert(src_size > 0);
229 assert(dst);
230 assert(dst_alloc_size);
231 assert(dst_size);
232 assert(*dst_alloc_size == 0 || *dst);
233
234 if (src_size <= 8)
235 return -EBADMSG;
236
237 size = unaligned_read_le64(src);
238 if (size < 0 || (unsigned) size != unaligned_read_le64(src))
239 return -EFBIG;
240 if ((size_t) size > *dst_alloc_size) {
241 out = realloc(*dst, size);
242 if (!out)
243 return -ENOMEM;
244 *dst = out;
245 *dst_alloc_size = size;
246 } else
247 out = *dst;
248
249 r = LZ4_decompress_safe((char*)src + 8, out, src_size - 8, size);
250 if (r < 0 || r != size)
251 return -EBADMSG;
252
253 *dst_size = size;
254 return 0;
255 #else
256 return -EPROTONOSUPPORT;
257 #endif
258 }
259
260 int decompress_blob_zstd(
261 const void *src, uint64_t src_size,
262 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
263
264 #if HAVE_ZSTD
265 size_t space;
266
267 assert(src);
268 assert(src_size > 0);
269 assert(dst);
270 assert(dst_alloc_size);
271 assert(dst_size);
272 assert(*dst_alloc_size == 0 || *dst);
273
274 if (src_size > SIZE_MAX/2) /* Overflow? */
275 return -ENOBUFS;
276 space = src_size * 2;
277 if (dst_max > 0 && space > dst_max)
278 space = dst_max;
279
280 if (!greedy_realloc(dst, dst_alloc_size, space, 1))
281 return -ENOMEM;
282
283 for (;;) {
284 size_t k;
285
286 k = ZSTD_decompress(*dst, *dst_alloc_size, src, src_size);
287 if (!ZSTD_isError(k)) {
288 *dst_size = k;
289 return 0;
290 }
291 if (ZSTD_getErrorCode(k) != ZSTD_error_dstSize_tooSmall)
292 return zstd_ret_to_errno(k);
293
294 if (dst_max > 0 && space >= dst_max) /* Already at max? */
295 return -ENOBUFS;
296 if (space > SIZE_MAX / 2) /* Overflow? */
297 return -ENOBUFS;
298
299 space *= 2;
300 if (dst_max > 0 && space > dst_max)
301 space = dst_max;
302
303 if (!greedy_realloc(dst, dst_alloc_size, space, 1))
304 return -ENOMEM;
305 }
306 #else
307 return -EPROTONOSUPPORT;
308 #endif
309 }
310
311 int decompress_blob(
312 int compression,
313 const void *src, uint64_t src_size,
314 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
315
316 if (compression == OBJECT_COMPRESSED_XZ)
317 return decompress_blob_xz(
318 src, src_size,
319 dst, dst_alloc_size, dst_size, dst_max);
320 else if (compression == OBJECT_COMPRESSED_LZ4)
321 return decompress_blob_lz4(
322 src, src_size,
323 dst, dst_alloc_size, dst_size, dst_max);
324 else if (compression == OBJECT_COMPRESSED_ZSTD)
325 return decompress_blob_zstd(
326 src, src_size,
327 dst, dst_alloc_size, dst_size, dst_max);
328 else
329 return -EBADMSG;
330 }
331
332 int decompress_startswith_xz(const void *src, uint64_t src_size,
333 void **buffer, size_t *buffer_size,
334 const void *prefix, size_t prefix_len,
335 uint8_t extra) {
336
337 #if HAVE_XZ
338 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
339 lzma_ret ret;
340
341 /* Checks whether the decompressed blob starts with the
342 * mentioned prefix. The byte extra needs to follow the
343 * prefix */
344
345 assert(src);
346 assert(src_size > 0);
347 assert(buffer);
348 assert(buffer_size);
349 assert(prefix);
350 assert(*buffer_size == 0 || *buffer);
351
352 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
353 if (ret != LZMA_OK)
354 return -EBADMSG;
355
356 if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1)))
357 return -ENOMEM;
358
359 s.next_in = src;
360 s.avail_in = src_size;
361
362 s.next_out = *buffer;
363 s.avail_out = *buffer_size;
364
365 for (;;) {
366 ret = lzma_code(&s, LZMA_FINISH);
367
368 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
369 return -EBADMSG;
370
371 if (*buffer_size - s.avail_out >= prefix_len + 1)
372 return memcmp(*buffer, prefix, prefix_len) == 0 &&
373 ((const uint8_t*) *buffer)[prefix_len] == extra;
374
375 if (ret == LZMA_STREAM_END)
376 return 0;
377
378 s.avail_out += *buffer_size;
379
380 if (!(greedy_realloc(buffer, buffer_size, *buffer_size * 2, 1)))
381 return -ENOMEM;
382
383 s.next_out = *(uint8_t**)buffer + *buffer_size - s.avail_out;
384 }
385
386 #else
387 return -EPROTONOSUPPORT;
388 #endif
389 }
390
391 int decompress_startswith_lz4(const void *src, uint64_t src_size,
392 void **buffer, size_t *buffer_size,
393 const void *prefix, size_t prefix_len,
394 uint8_t extra) {
395 #if HAVE_LZ4
396 /* Checks whether the decompressed blob starts with the
397 * mentioned prefix. The byte extra needs to follow the
398 * prefix */
399
400 int r;
401
402 assert(src);
403 assert(src_size > 0);
404 assert(buffer);
405 assert(buffer_size);
406 assert(prefix);
407 assert(*buffer_size == 0 || *buffer);
408
409 if (src_size <= 8)
410 return -EBADMSG;
411
412 if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1)))
413 return -ENOMEM;
414
415 r = LZ4_decompress_safe_partial((char*)src + 8, *buffer, src_size - 8,
416 prefix_len + 1, *buffer_size);
417 /* One lz4 < 1.8.3, we might get "failure" (r < 0), or "success" where
418 * just a part of the buffer is decompressed. But if we get a smaller
419 * amount of bytes than requested, we don't know whether there isn't enough
420 * data to fill the requested size or whether we just got a partial answer.
421 */
422 if (r < 0 || (size_t) r < prefix_len + 1) {
423 size_t size;
424
425 if (LZ4_versionNumber() >= 10803)
426 /* We trust that the newer lz4 decompresses the number of bytes we
427 * requested if available in the compressed string. */
428 return 0;
429
430 if (r > 0)
431 /* Compare what we have first, in case of mismatch we can
432 * shortcut the full comparison. */
433 if (memcmp(*buffer, prefix, r) != 0)
434 return 0;
435
436 /* Before version 1.8.3, lz4 always tries to decode full a "sequence",
437 * so in pathological cases might need to decompress the full field. */
438 r = decompress_blob_lz4(src, src_size, buffer, buffer_size, &size, 0);
439 if (r < 0)
440 return r;
441
442 if (size < prefix_len + 1)
443 return 0;
444 }
445
446 return memcmp(*buffer, prefix, prefix_len) == 0 &&
447 ((const uint8_t*) *buffer)[prefix_len] == extra;
448 #else
449 return -EPROTONOSUPPORT;
450 #endif
451 }
452
453 int decompress_startswith_zstd(
454 const void *src, uint64_t src_size,
455 void **buffer, size_t *buffer_size,
456 const void *prefix, size_t prefix_len,
457 uint8_t extra) {
458 #if HAVE_ZSTD
459 _cleanup_(ZSTD_freeDCtxp) ZSTD_DCtx *dctx = NULL;
460 size_t k;
461
462 assert(src);
463 assert(src_size > 0);
464 assert(buffer);
465 assert(buffer_size);
466 assert(prefix);
467 assert(*buffer_size == 0 || *buffer);
468
469 dctx = ZSTD_createDCtx();
470 if (!dctx)
471 return -ENOMEM;
472
473 if (!(greedy_realloc(buffer, buffer_size, MAX(ZSTD_DStreamOutSize(), prefix_len + 1), 1)))
474 return -ENOMEM;
475
476 ZSTD_inBuffer input = {
477 .src = src,
478 .size = src_size,
479 };
480 ZSTD_outBuffer output = {
481 .dst = *buffer,
482 .size = *buffer_size,
483 };
484
485 for (;;) {
486 k = ZSTD_decompressStream(dctx, &output, &input);
487 if (ZSTD_isError(k)) {
488 log_debug("ZSTD decoder failed: %s", ZSTD_getErrorName(k));
489 return zstd_ret_to_errno(k);
490 }
491
492 if (output.pos >= prefix_len + 1)
493 return memcmp(*buffer, prefix, prefix_len) == 0 &&
494 ((const uint8_t*) *buffer)[prefix_len] == extra;
495
496 if (input.pos >= input.size)
497 return 0;
498
499 if (*buffer_size > SIZE_MAX/2)
500 return -ENOBUFS;
501
502 if (!(greedy_realloc(buffer, buffer_size, *buffer_size * 2, 1)))
503 return -ENOMEM;
504
505 output.dst = *buffer;
506 output.size = *buffer_size;
507 }
508 #else
509 return -EPROTONOSUPPORT;
510 #endif
511 }
512
513 int decompress_startswith(
514 int compression,
515 const void *src, uint64_t src_size,
516 void **buffer, size_t *buffer_size,
517 const void *prefix, size_t prefix_len,
518 uint8_t extra) {
519
520 if (compression == OBJECT_COMPRESSED_XZ)
521 return decompress_startswith_xz(
522 src, src_size,
523 buffer, buffer_size,
524 prefix, prefix_len,
525 extra);
526
527 else if (compression == OBJECT_COMPRESSED_LZ4)
528 return decompress_startswith_lz4(
529 src, src_size,
530 buffer, buffer_size,
531 prefix, prefix_len,
532 extra);
533 else if (compression == OBJECT_COMPRESSED_ZSTD)
534 return decompress_startswith_zstd(
535 src, src_size,
536 buffer, buffer_size,
537 prefix, prefix_len,
538 extra);
539 else
540 return -EBADMSG;
541 }
542
543 int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes) {
544 #if HAVE_XZ
545 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
546 lzma_ret ret;
547 uint8_t buf[BUFSIZ], out[BUFSIZ];
548 lzma_action action = LZMA_RUN;
549
550 assert(fdf >= 0);
551 assert(fdt >= 0);
552
553 ret = lzma_easy_encoder(&s, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64);
554 if (ret != LZMA_OK) {
555 log_error("Failed to initialize XZ encoder: code %u", ret);
556 return -EINVAL;
557 }
558
559 for (;;) {
560 if (s.avail_in == 0 && action == LZMA_RUN) {
561 size_t m = sizeof(buf);
562 ssize_t n;
563
564 if (max_bytes != (uint64_t) -1 && (uint64_t) m > max_bytes)
565 m = (size_t) max_bytes;
566
567 n = read(fdf, buf, m);
568 if (n < 0)
569 return -errno;
570 if (n == 0)
571 action = LZMA_FINISH;
572 else {
573 s.next_in = buf;
574 s.avail_in = n;
575
576 if (max_bytes != (uint64_t) -1) {
577 assert(max_bytes >= (uint64_t) n);
578 max_bytes -= n;
579 }
580 }
581 }
582
583 if (s.avail_out == 0) {
584 s.next_out = out;
585 s.avail_out = sizeof(out);
586 }
587
588 ret = lzma_code(&s, action);
589 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END)) {
590 log_error("Compression failed: code %u", ret);
591 return -EBADMSG;
592 }
593
594 if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
595 ssize_t n, k;
596
597 n = sizeof(out) - s.avail_out;
598
599 k = loop_write(fdt, out, n, false);
600 if (k < 0)
601 return k;
602
603 if (ret == LZMA_STREAM_END) {
604 log_debug("XZ compression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
605 s.total_in, s.total_out,
606 (double) s.total_out / s.total_in * 100);
607
608 return 0;
609 }
610 }
611 }
612 #else
613 return -EPROTONOSUPPORT;
614 #endif
615 }
616
617 #define LZ4_BUFSIZE (512*1024u)
618
619 int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes) {
620
621 #if HAVE_LZ4
622 LZ4F_errorCode_t c;
623 _cleanup_(LZ4F_freeCompressionContextp) LZ4F_compressionContext_t ctx = NULL;
624 _cleanup_free_ char *buf = NULL;
625 char *src = NULL;
626 size_t size, n, total_in = 0, total_out, offset = 0, frame_size;
627 struct stat st;
628 int r;
629 static const LZ4F_compressOptions_t options = {
630 .stableSrc = 1,
631 };
632 static const LZ4F_preferences_t preferences = {
633 .frameInfo.blockSizeID = 5,
634 };
635
636 c = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
637 if (LZ4F_isError(c))
638 return -ENOMEM;
639
640 if (fstat(fdf, &st) < 0)
641 return log_debug_errno(errno, "fstat() failed: %m");
642
643 frame_size = LZ4F_compressBound(LZ4_BUFSIZE, &preferences);
644 size = frame_size + 64*1024; /* add some space for header and trailer */
645 buf = malloc(size);
646 if (!buf)
647 return -ENOMEM;
648
649 n = offset = total_out = LZ4F_compressBegin(ctx, buf, size, &preferences);
650 if (LZ4F_isError(n))
651 return -EINVAL;
652
653 src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fdf, 0);
654 if (src == MAP_FAILED)
655 return -errno;
656
657 log_debug("Buffer size is %zu bytes, header size %zu bytes.", size, n);
658
659 while (total_in < (size_t) st.st_size) {
660 ssize_t k;
661
662 k = MIN(LZ4_BUFSIZE, st.st_size - total_in);
663 n = LZ4F_compressUpdate(ctx, buf + offset, size - offset,
664 src + total_in, k, &options);
665 if (LZ4F_isError(n)) {
666 r = -ENOTRECOVERABLE;
667 goto cleanup;
668 }
669
670 total_in += k;
671 offset += n;
672 total_out += n;
673
674 if (max_bytes != (uint64_t) -1 && total_out > (size_t) max_bytes) {
675 log_debug("Compressed stream longer than %"PRIu64" bytes", max_bytes);
676 return -EFBIG;
677 }
678
679 if (size - offset < frame_size + 4) {
680 k = loop_write(fdt, buf, offset, false);
681 if (k < 0) {
682 r = k;
683 goto cleanup;
684 }
685 offset = 0;
686 }
687 }
688
689 n = LZ4F_compressEnd(ctx, buf + offset, size - offset, &options);
690 if (LZ4F_isError(n)) {
691 r = -ENOTRECOVERABLE;
692 goto cleanup;
693 }
694
695 offset += n;
696 total_out += n;
697 r = loop_write(fdt, buf, offset, false);
698 if (r < 0)
699 goto cleanup;
700
701 log_debug("LZ4 compression finished (%zu -> %zu bytes, %.1f%%)",
702 total_in, total_out,
703 (double) total_out / total_in * 100);
704 cleanup:
705 munmap(src, st.st_size);
706 return r;
707 #else
708 return -EPROTONOSUPPORT;
709 #endif
710 }
711
712 int decompress_stream_xz(int fdf, int fdt, uint64_t max_bytes) {
713
714 #if HAVE_XZ
715 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
716 lzma_ret ret;
717
718 uint8_t buf[BUFSIZ], out[BUFSIZ];
719 lzma_action action = LZMA_RUN;
720
721 assert(fdf >= 0);
722 assert(fdt >= 0);
723
724 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
725 if (ret != LZMA_OK) {
726 log_debug("Failed to initialize XZ decoder: code %u", ret);
727 return -ENOMEM;
728 }
729
730 for (;;) {
731 if (s.avail_in == 0 && action == LZMA_RUN) {
732 ssize_t n;
733
734 n = read(fdf, buf, sizeof(buf));
735 if (n < 0)
736 return -errno;
737 if (n == 0)
738 action = LZMA_FINISH;
739 else {
740 s.next_in = buf;
741 s.avail_in = n;
742 }
743 }
744
745 if (s.avail_out == 0) {
746 s.next_out = out;
747 s.avail_out = sizeof(out);
748 }
749
750 ret = lzma_code(&s, action);
751 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END)) {
752 log_debug("Decompression failed: code %u", ret);
753 return -EBADMSG;
754 }
755
756 if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
757 ssize_t n, k;
758
759 n = sizeof(out) - s.avail_out;
760
761 if (max_bytes != (uint64_t) -1) {
762 if (max_bytes < (uint64_t) n)
763 return -EFBIG;
764
765 max_bytes -= n;
766 }
767
768 k = loop_write(fdt, out, n, false);
769 if (k < 0)
770 return k;
771
772 if (ret == LZMA_STREAM_END) {
773 log_debug("XZ decompression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
774 s.total_in, s.total_out,
775 (double) s.total_out / s.total_in * 100);
776
777 return 0;
778 }
779 }
780 }
781 #else
782 log_debug("Cannot decompress file. Compiled without XZ support.");
783 return -EPROTONOSUPPORT;
784 #endif
785 }
786
787 int decompress_stream_lz4(int in, int out, uint64_t max_bytes) {
788 #if HAVE_LZ4
789 size_t c;
790 _cleanup_(LZ4F_freeDecompressionContextp) LZ4F_decompressionContext_t ctx = NULL;
791 _cleanup_free_ char *buf = NULL;
792 char *src;
793 struct stat st;
794 int r = 0;
795 size_t total_in = 0, total_out = 0;
796
797 c = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
798 if (LZ4F_isError(c))
799 return -ENOMEM;
800
801 if (fstat(in, &st) < 0)
802 return log_debug_errno(errno, "fstat() failed: %m");
803
804 buf = malloc(LZ4_BUFSIZE);
805 if (!buf)
806 return -ENOMEM;
807
808 src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, in, 0);
809 if (src == MAP_FAILED)
810 return -errno;
811
812 while (total_in < (size_t) st.st_size) {
813 size_t produced = LZ4_BUFSIZE;
814 size_t used = st.st_size - total_in;
815
816 c = LZ4F_decompress(ctx, buf, &produced, src + total_in, &used, NULL);
817 if (LZ4F_isError(c)) {
818 r = -EBADMSG;
819 goto cleanup;
820 }
821
822 total_in += used;
823 total_out += produced;
824
825 if (max_bytes != (uint64_t) -1 && total_out > (size_t) max_bytes) {
826 log_debug("Decompressed stream longer than %"PRIu64" bytes", max_bytes);
827 r = -EFBIG;
828 goto cleanup;
829 }
830
831 r = loop_write(out, buf, produced, false);
832 if (r < 0)
833 goto cleanup;
834 }
835
836 log_debug("LZ4 decompression finished (%zu -> %zu bytes, %.1f%%)",
837 total_in, total_out,
838 total_in > 0 ? (double) total_out / total_in * 100 : 0.0);
839 cleanup:
840 munmap(src, st.st_size);
841 return r;
842 #else
843 log_debug("Cannot decompress file. Compiled without LZ4 support.");
844 return -EPROTONOSUPPORT;
845 #endif
846 }
847
848 int compress_stream_zstd(int fdf, int fdt, uint64_t max_bytes) {
849 #if HAVE_ZSTD
850 _cleanup_(ZSTD_freeCCtxp) ZSTD_CCtx *cctx = NULL;
851 _cleanup_free_ void *in_buff = NULL, *out_buff = NULL;
852 size_t in_allocsize, out_allocsize;
853 size_t z;
854 uint64_t left = max_bytes, in_bytes = 0;
855
856 assert(fdf >= 0);
857 assert(fdt >= 0);
858
859 /* Create the context and buffers */
860 in_allocsize = ZSTD_CStreamInSize();
861 out_allocsize = ZSTD_CStreamOutSize();
862 in_buff = malloc(in_allocsize);
863 out_buff = malloc(out_allocsize);
864 cctx = ZSTD_createCCtx();
865 if (!cctx || !out_buff || !in_buff)
866 return -ENOMEM;
867
868 z = ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1);
869 if (ZSTD_isError(z))
870 log_debug("Failed to enable ZSTD checksum, ignoring: %s", ZSTD_getErrorName(z));
871
872 /* This loop read from the input file, compresses that entire chunk,
873 * and writes all output produced to the output file.
874 */
875 for (;;) {
876 bool is_last_chunk;
877 ZSTD_inBuffer input = {
878 .src = in_buff,
879 .size = 0,
880 .pos = 0
881 };
882 ssize_t red;
883
884 red = loop_read(fdf, in_buff, in_allocsize, true);
885 if (red < 0)
886 return red;
887 is_last_chunk = red == 0;
888
889 in_bytes += (size_t) red;
890 input.size = (size_t) red;
891
892 for (bool finished = false; !finished;) {
893 ZSTD_outBuffer output = {
894 .dst = out_buff,
895 .size = out_allocsize,
896 .pos = 0
897 };
898 size_t remaining;
899 ssize_t wrote;
900
901 /* Compress into the output buffer and write all of the
902 * output to the file so we can reuse the buffer next
903 * iteration.
904 */
905 remaining = ZSTD_compressStream2(
906 cctx, &output, &input,
907 is_last_chunk ? ZSTD_e_end : ZSTD_e_continue);
908
909 if (ZSTD_isError(remaining)) {
910 log_debug("ZSTD encoder failed: %s", ZSTD_getErrorName(remaining));
911 return zstd_ret_to_errno(remaining);
912 }
913
914 if (left < output.pos)
915 return -EFBIG;
916
917 wrote = loop_write(fdt, output.dst, output.pos, 1);
918 if (wrote < 0)
919 return wrote;
920
921 left -= output.pos;
922
923 /* If we're on the last chunk we're finished when zstd
924 * returns 0, which means its consumed all the input AND
925 * finished the frame. Otherwise, we're finished when
926 * we've consumed all the input.
927 */
928 finished = is_last_chunk ? (remaining == 0) : (input.pos == input.size);
929 }
930
931 /* zstd only returns 0 when the input is completely consumed */
932 assert(input.pos == input.size);
933 if (is_last_chunk)
934 break;
935 }
936
937 log_debug(
938 "ZSTD compression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
939 in_bytes,
940 max_bytes - left,
941 (double) (max_bytes - left) / in_bytes * 100);
942
943 return 0;
944 #else
945 return -EPROTONOSUPPORT;
946 #endif
947 }
948
949 int decompress_stream_zstd(int fdf, int fdt, uint64_t max_bytes) {
950 #if HAVE_ZSTD
951 _cleanup_(ZSTD_freeDCtxp) ZSTD_DCtx *dctx = NULL;
952 _cleanup_free_ void *in_buff = NULL, *out_buff = NULL;
953 size_t in_allocsize, out_allocsize;
954 size_t last_result = 0;
955 uint64_t left = max_bytes, in_bytes = 0;
956
957 assert(fdf >= 0);
958 assert(fdt >= 0);
959
960 /* Create the context and buffers */
961 in_allocsize = ZSTD_DStreamInSize();
962 out_allocsize = ZSTD_DStreamOutSize();
963 in_buff = malloc(in_allocsize);
964 out_buff = malloc(out_allocsize);
965 dctx = ZSTD_createDCtx();
966 if (!dctx || !out_buff || !in_buff)
967 return -ENOMEM;
968
969 /* This loop assumes that the input file is one or more concatenated
970 * zstd streams. This example won't work if there is trailing non-zstd
971 * data at the end, but streaming decompression in general handles this
972 * case. ZSTD_decompressStream() returns 0 exactly when the frame is
973 * completed, and doesn't consume input after the frame.
974 */
975 for (;;) {
976 bool has_error = false;
977 ZSTD_inBuffer input = {
978 .src = in_buff,
979 .size = 0,
980 .pos = 0
981 };
982 ssize_t red;
983
984 red = loop_read(fdf, in_buff, in_allocsize, true);
985 if (red < 0)
986 return red;
987 if (red == 0)
988 break;
989
990 in_bytes += (size_t) red;
991 input.size = (size_t) red;
992 input.pos = 0;
993
994 /* Given a valid frame, zstd won't consume the last byte of the
995 * frame until it has flushed all of the decompressed data of
996 * the frame. So input.pos < input.size means frame is not done
997 * or there is still output available.
998 */
999 while (input.pos < input.size) {
1000 ZSTD_outBuffer output = {
1001 .dst = out_buff,
1002 .size = out_allocsize,
1003 .pos = 0
1004 };
1005 ssize_t wrote;
1006 /* The return code is zero if the frame is complete, but
1007 * there may be multiple frames concatenated together.
1008 * Zstd will automatically reset the context when a
1009 * frame is complete. Still, calling ZSTD_DCtx_reset()
1010 * can be useful to reset the context to a clean state,
1011 * for instance if the last decompression call returned
1012 * an error.
1013 */
1014 last_result = ZSTD_decompressStream(dctx, &output, &input);
1015 if (ZSTD_isError(last_result)) {
1016 has_error = true;
1017 break;
1018 }
1019
1020 if (left < output.pos)
1021 return -EFBIG;
1022
1023 wrote = loop_write(fdt, output.dst, output.pos, 1);
1024 if (wrote < 0)
1025 return wrote;
1026
1027 left -= output.pos;
1028 }
1029 if (has_error)
1030 break;
1031 }
1032
1033 if (in_bytes == 0)
1034 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "ZSTD decoder failed: no data read");
1035
1036 if (last_result != 0) {
1037 /* The last return value from ZSTD_decompressStream did not end
1038 * on a frame, but we reached the end of the file! We assume
1039 * this is an error, and the input was truncated.
1040 */
1041 log_debug("ZSTD decoder failed: %s", ZSTD_getErrorName(last_result));
1042 return zstd_ret_to_errno(last_result);
1043 }
1044
1045 log_debug(
1046 "ZSTD decompression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
1047 in_bytes,
1048 max_bytes - left,
1049 (double) (max_bytes - left) / in_bytes * 100);
1050 return 0;
1051 #else
1052 log_debug("Cannot decompress file. Compiled without ZSTD support.");
1053 return -EPROTONOSUPPORT;
1054 #endif
1055 }
1056
1057 int decompress_stream(const char *filename, int fdf, int fdt, uint64_t max_bytes) {
1058
1059 if (endswith(filename, ".lz4"))
1060 return decompress_stream_lz4(fdf, fdt, max_bytes);
1061 else if (endswith(filename, ".xz"))
1062 return decompress_stream_xz(fdf, fdt, max_bytes);
1063 else if (endswith(filename, ".zst"))
1064 return decompress_stream_zstd(fdf, fdt, max_bytes);
1065 else
1066 return -EPROTONOSUPPORT;
1067 }