]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/compress.c
Merge pull request #17549 from yuwata/tiny-fixes
[thirdparty/systemd.git] / src / journal / compress.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 uint64_t size;
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 size = ZSTD_getFrameContentSize(src, src_size);
275 if (IN_SET(size, ZSTD_CONTENTSIZE_ERROR, ZSTD_CONTENTSIZE_UNKNOWN))
276 return -EBADMSG;
277
278 if (dst_max > 0 && size > dst_max)
279 size = dst_max;
280 if (size > SIZE_MAX)
281 return -E2BIG;
282
283 if (!(greedy_realloc(dst, dst_alloc_size, MAX(ZSTD_DStreamOutSize(), size), 1)))
284 return -ENOMEM;
285
286 _cleanup_(ZSTD_freeDCtxp) ZSTD_DCtx *dctx = ZSTD_createDCtx();
287 if (!dctx)
288 return -ENOMEM;
289
290 ZSTD_inBuffer input = {
291 .src = src,
292 .size = src_size,
293 };
294 ZSTD_outBuffer output = {
295 .dst = *dst,
296 .size = *dst_alloc_size,
297 };
298
299 size_t k = ZSTD_decompressStream(dctx, &output, &input);
300 if (ZSTD_isError(k)) {
301 log_debug("ZSTD decoder failed: %s", ZSTD_getErrorName(k));
302 return zstd_ret_to_errno(k);
303 }
304 assert(output.pos >= size);
305
306 *dst_size = size;
307 return 0;
308 #else
309 return -EPROTONOSUPPORT;
310 #endif
311 }
312
313 int decompress_blob(
314 int compression,
315 const void *src, uint64_t src_size,
316 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
317
318 if (compression == OBJECT_COMPRESSED_XZ)
319 return decompress_blob_xz(
320 src, src_size,
321 dst, dst_alloc_size, dst_size, dst_max);
322 else if (compression == OBJECT_COMPRESSED_LZ4)
323 return decompress_blob_lz4(
324 src, src_size,
325 dst, dst_alloc_size, dst_size, dst_max);
326 else if (compression == OBJECT_COMPRESSED_ZSTD)
327 return decompress_blob_zstd(
328 src, src_size,
329 dst, dst_alloc_size, dst_size, dst_max);
330 else
331 return -EPROTONOSUPPORT;
332 }
333
334 int decompress_startswith_xz(const void *src, uint64_t src_size,
335 void **buffer, size_t *buffer_size,
336 const void *prefix, size_t prefix_len,
337 uint8_t extra) {
338
339 #if HAVE_XZ
340 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
341 lzma_ret ret;
342
343 /* Checks whether the decompressed blob starts with the
344 * mentioned prefix. The byte extra needs to follow the
345 * prefix */
346
347 assert(src);
348 assert(src_size > 0);
349 assert(buffer);
350 assert(buffer_size);
351 assert(prefix);
352 assert(*buffer_size == 0 || *buffer);
353
354 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
355 if (ret != LZMA_OK)
356 return -EBADMSG;
357
358 if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1)))
359 return -ENOMEM;
360
361 s.next_in = src;
362 s.avail_in = src_size;
363
364 s.next_out = *buffer;
365 s.avail_out = *buffer_size;
366
367 for (;;) {
368 ret = lzma_code(&s, LZMA_FINISH);
369
370 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
371 return -EBADMSG;
372
373 if (*buffer_size - s.avail_out >= prefix_len + 1)
374 return memcmp(*buffer, prefix, prefix_len) == 0 &&
375 ((const uint8_t*) *buffer)[prefix_len] == extra;
376
377 if (ret == LZMA_STREAM_END)
378 return 0;
379
380 s.avail_out += *buffer_size;
381
382 if (!(greedy_realloc(buffer, buffer_size, *buffer_size * 2, 1)))
383 return -ENOMEM;
384
385 s.next_out = *(uint8_t**)buffer + *buffer_size - s.avail_out;
386 }
387
388 #else
389 return -EPROTONOSUPPORT;
390 #endif
391 }
392
393 int decompress_startswith_lz4(const void *src, uint64_t src_size,
394 void **buffer, size_t *buffer_size,
395 const void *prefix, size_t prefix_len,
396 uint8_t extra) {
397 #if HAVE_LZ4
398 /* Checks whether the decompressed blob starts with the
399 * mentioned prefix. The byte extra needs to follow the
400 * prefix */
401
402 int r;
403
404 assert(src);
405 assert(src_size > 0);
406 assert(buffer);
407 assert(buffer_size);
408 assert(prefix);
409 assert(*buffer_size == 0 || *buffer);
410
411 if (src_size <= 8)
412 return -EBADMSG;
413
414 if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1)))
415 return -ENOMEM;
416
417 r = LZ4_decompress_safe_partial((char*)src + 8, *buffer, src_size - 8,
418 prefix_len + 1, *buffer_size);
419 /* One lz4 < 1.8.3, we might get "failure" (r < 0), or "success" where
420 * just a part of the buffer is decompressed. But if we get a smaller
421 * amount of bytes than requested, we don't know whether there isn't enough
422 * data to fill the requested size or whether we just got a partial answer.
423 */
424 if (r < 0 || (size_t) r < prefix_len + 1) {
425 size_t size;
426
427 if (LZ4_versionNumber() >= 10803)
428 /* We trust that the newer lz4 decompresses the number of bytes we
429 * requested if available in the compressed string. */
430 return 0;
431
432 if (r > 0)
433 /* Compare what we have first, in case of mismatch we can
434 * shortcut the full comparison. */
435 if (memcmp(*buffer, prefix, r) != 0)
436 return 0;
437
438 /* Before version 1.8.3, lz4 always tries to decode full a "sequence",
439 * so in pathological cases might need to decompress the full field. */
440 r = decompress_blob_lz4(src, src_size, buffer, buffer_size, &size, 0);
441 if (r < 0)
442 return r;
443
444 if (size < prefix_len + 1)
445 return 0;
446 }
447
448 return memcmp(*buffer, prefix, prefix_len) == 0 &&
449 ((const uint8_t*) *buffer)[prefix_len] == extra;
450 #else
451 return -EPROTONOSUPPORT;
452 #endif
453 }
454
455 int decompress_startswith_zstd(
456 const void *src, uint64_t src_size,
457 void **buffer, size_t *buffer_size,
458 const void *prefix, size_t prefix_len,
459 uint8_t extra) {
460 #if HAVE_ZSTD
461 assert(src);
462 assert(src_size > 0);
463 assert(buffer);
464 assert(buffer_size);
465 assert(prefix);
466 assert(*buffer_size == 0 || *buffer);
467
468 uint64_t size = ZSTD_getFrameContentSize(src, src_size);
469 if (IN_SET(size, ZSTD_CONTENTSIZE_ERROR, ZSTD_CONTENTSIZE_UNKNOWN))
470 return -EBADMSG;
471
472 if (size < prefix_len + 1)
473 return 0; /* Decompressed text too short to match the prefix and extra */
474
475 _cleanup_(ZSTD_freeDCtxp) ZSTD_DCtx *dctx = ZSTD_createDCtx();
476 if (!dctx)
477 return -ENOMEM;
478
479 if (!(greedy_realloc(buffer, buffer_size, MAX(ZSTD_DStreamOutSize(), prefix_len + 1), 1)))
480 return -ENOMEM;
481
482 ZSTD_inBuffer input = {
483 .src = src,
484 .size = src_size,
485 };
486 ZSTD_outBuffer output = {
487 .dst = *buffer,
488 .size = *buffer_size,
489 };
490 size_t k;
491
492 k = ZSTD_decompressStream(dctx, &output, &input);
493 if (ZSTD_isError(k)) {
494 log_debug("ZSTD decoder failed: %s", ZSTD_getErrorName(k));
495 return zstd_ret_to_errno(k);
496 }
497 assert(output.pos >= prefix_len + 1);
498
499 return memcmp(*buffer, prefix, prefix_len) == 0 &&
500 ((const uint8_t*) *buffer)[prefix_len] == extra;
501 #else
502 return -EPROTONOSUPPORT;
503 #endif
504 }
505
506 int decompress_startswith(
507 int compression,
508 const void *src, uint64_t src_size,
509 void **buffer, size_t *buffer_size,
510 const void *prefix, size_t prefix_len,
511 uint8_t extra) {
512
513 if (compression == OBJECT_COMPRESSED_XZ)
514 return decompress_startswith_xz(
515 src, src_size,
516 buffer, buffer_size,
517 prefix, prefix_len,
518 extra);
519
520 else if (compression == OBJECT_COMPRESSED_LZ4)
521 return decompress_startswith_lz4(
522 src, src_size,
523 buffer, buffer_size,
524 prefix, prefix_len,
525 extra);
526 else if (compression == OBJECT_COMPRESSED_ZSTD)
527 return decompress_startswith_zstd(
528 src, src_size,
529 buffer, buffer_size,
530 prefix, prefix_len,
531 extra);
532 else
533 return -EBADMSG;
534 }
535
536 int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes) {
537 #if HAVE_XZ
538 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
539 lzma_ret ret;
540 uint8_t buf[BUFSIZ], out[BUFSIZ];
541 lzma_action action = LZMA_RUN;
542
543 assert(fdf >= 0);
544 assert(fdt >= 0);
545
546 ret = lzma_easy_encoder(&s, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64);
547 if (ret != LZMA_OK)
548 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
549 "Failed to initialize XZ encoder: code %u",
550 ret);
551
552 for (;;) {
553 if (s.avail_in == 0 && action == LZMA_RUN) {
554 size_t m = sizeof(buf);
555 ssize_t n;
556
557 if (max_bytes != (uint64_t) -1 && (uint64_t) m > max_bytes)
558 m = (size_t) max_bytes;
559
560 n = read(fdf, buf, m);
561 if (n < 0)
562 return -errno;
563 if (n == 0)
564 action = LZMA_FINISH;
565 else {
566 s.next_in = buf;
567 s.avail_in = n;
568
569 if (max_bytes != (uint64_t) -1) {
570 assert(max_bytes >= (uint64_t) n);
571 max_bytes -= n;
572 }
573 }
574 }
575
576 if (s.avail_out == 0) {
577 s.next_out = out;
578 s.avail_out = sizeof(out);
579 }
580
581 ret = lzma_code(&s, action);
582 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
583 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
584 "Compression failed: code %u",
585 ret);
586
587 if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
588 ssize_t n, k;
589
590 n = sizeof(out) - s.avail_out;
591
592 k = loop_write(fdt, out, n, false);
593 if (k < 0)
594 return k;
595
596 if (ret == LZMA_STREAM_END) {
597 log_debug("XZ compression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
598 s.total_in, s.total_out,
599 (double) s.total_out / s.total_in * 100);
600
601 return 0;
602 }
603 }
604 }
605 #else
606 return -EPROTONOSUPPORT;
607 #endif
608 }
609
610 #define LZ4_BUFSIZE (512*1024u)
611
612 int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes) {
613
614 #if HAVE_LZ4
615 LZ4F_errorCode_t c;
616 _cleanup_(LZ4F_freeCompressionContextp) LZ4F_compressionContext_t ctx = NULL;
617 _cleanup_free_ char *buf = NULL;
618 char *src = NULL;
619 size_t size, n, total_in = 0, total_out, offset = 0, frame_size;
620 struct stat st;
621 int r;
622 static const LZ4F_compressOptions_t options = {
623 .stableSrc = 1,
624 };
625 static const LZ4F_preferences_t preferences = {
626 .frameInfo.blockSizeID = 5,
627 };
628
629 c = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
630 if (LZ4F_isError(c))
631 return -ENOMEM;
632
633 if (fstat(fdf, &st) < 0)
634 return log_debug_errno(errno, "fstat() failed: %m");
635
636 frame_size = LZ4F_compressBound(LZ4_BUFSIZE, &preferences);
637 size = frame_size + 64*1024; /* add some space for header and trailer */
638 buf = malloc(size);
639 if (!buf)
640 return -ENOMEM;
641
642 n = offset = total_out = LZ4F_compressBegin(ctx, buf, size, &preferences);
643 if (LZ4F_isError(n))
644 return -EINVAL;
645
646 src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fdf, 0);
647 if (src == MAP_FAILED)
648 return -errno;
649
650 log_debug("Buffer size is %zu bytes, header size %zu bytes.", size, n);
651
652 while (total_in < (size_t) st.st_size) {
653 ssize_t k;
654
655 k = MIN(LZ4_BUFSIZE, st.st_size - total_in);
656 n = LZ4F_compressUpdate(ctx, buf + offset, size - offset,
657 src + total_in, k, &options);
658 if (LZ4F_isError(n)) {
659 r = -ENOTRECOVERABLE;
660 goto cleanup;
661 }
662
663 total_in += k;
664 offset += n;
665 total_out += n;
666
667 if (max_bytes != (uint64_t) -1 && total_out > (size_t) max_bytes)
668 return log_debug_errno(SYNTHETIC_ERRNO(EFBIG),
669 "Compressed stream longer than %" PRIu64 " bytes",
670 max_bytes);
671
672 if (size - offset < frame_size + 4) {
673 k = loop_write(fdt, buf, offset, false);
674 if (k < 0) {
675 r = k;
676 goto cleanup;
677 }
678 offset = 0;
679 }
680 }
681
682 n = LZ4F_compressEnd(ctx, buf + offset, size - offset, &options);
683 if (LZ4F_isError(n)) {
684 r = -ENOTRECOVERABLE;
685 goto cleanup;
686 }
687
688 offset += n;
689 total_out += n;
690 r = loop_write(fdt, buf, offset, false);
691 if (r < 0)
692 goto cleanup;
693
694 log_debug("LZ4 compression finished (%zu -> %zu bytes, %.1f%%)",
695 total_in, total_out,
696 (double) total_out / total_in * 100);
697 cleanup:
698 munmap(src, st.st_size);
699 return r;
700 #else
701 return -EPROTONOSUPPORT;
702 #endif
703 }
704
705 int decompress_stream_xz(int fdf, int fdt, uint64_t max_bytes) {
706
707 #if HAVE_XZ
708 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
709 lzma_ret ret;
710
711 uint8_t buf[BUFSIZ], out[BUFSIZ];
712 lzma_action action = LZMA_RUN;
713
714 assert(fdf >= 0);
715 assert(fdt >= 0);
716
717 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
718 if (ret != LZMA_OK)
719 return log_debug_errno(SYNTHETIC_ERRNO(ENOMEM),
720 "Failed to initialize XZ decoder: code %u",
721 ret);
722
723 for (;;) {
724 if (s.avail_in == 0 && action == LZMA_RUN) {
725 ssize_t n;
726
727 n = read(fdf, buf, sizeof(buf));
728 if (n < 0)
729 return -errno;
730 if (n == 0)
731 action = LZMA_FINISH;
732 else {
733 s.next_in = buf;
734 s.avail_in = n;
735 }
736 }
737
738 if (s.avail_out == 0) {
739 s.next_out = out;
740 s.avail_out = sizeof(out);
741 }
742
743 ret = lzma_code(&s, action);
744 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
745 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
746 "Decompression failed: code %u",
747 ret);
748
749 if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
750 ssize_t n, k;
751
752 n = sizeof(out) - s.avail_out;
753
754 if (max_bytes != (uint64_t) -1) {
755 if (max_bytes < (uint64_t) n)
756 return -EFBIG;
757
758 max_bytes -= n;
759 }
760
761 k = loop_write(fdt, out, n, false);
762 if (k < 0)
763 return k;
764
765 if (ret == LZMA_STREAM_END) {
766 log_debug("XZ decompression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
767 s.total_in, s.total_out,
768 (double) s.total_out / s.total_in * 100);
769
770 return 0;
771 }
772 }
773 }
774 #else
775 return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT),
776 "Cannot decompress file. Compiled without XZ support.");
777 #endif
778 }
779
780 int decompress_stream_lz4(int in, int out, uint64_t max_bytes) {
781 #if HAVE_LZ4
782 size_t c;
783 _cleanup_(LZ4F_freeDecompressionContextp) LZ4F_decompressionContext_t ctx = NULL;
784 _cleanup_free_ char *buf = NULL;
785 char *src;
786 struct stat st;
787 int r = 0;
788 size_t total_in = 0, total_out = 0;
789
790 c = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
791 if (LZ4F_isError(c))
792 return -ENOMEM;
793
794 if (fstat(in, &st) < 0)
795 return log_debug_errno(errno, "fstat() failed: %m");
796
797 buf = malloc(LZ4_BUFSIZE);
798 if (!buf)
799 return -ENOMEM;
800
801 src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, in, 0);
802 if (src == MAP_FAILED)
803 return -errno;
804
805 while (total_in < (size_t) st.st_size) {
806 size_t produced = LZ4_BUFSIZE;
807 size_t used = st.st_size - total_in;
808
809 c = LZ4F_decompress(ctx, buf, &produced, src + total_in, &used, NULL);
810 if (LZ4F_isError(c)) {
811 r = -EBADMSG;
812 goto cleanup;
813 }
814
815 total_in += used;
816 total_out += produced;
817
818 if (max_bytes != (uint64_t) -1 && total_out > (size_t) max_bytes) {
819 log_debug("Decompressed stream longer than %"PRIu64" bytes", max_bytes);
820 r = -EFBIG;
821 goto cleanup;
822 }
823
824 r = loop_write(out, buf, produced, false);
825 if (r < 0)
826 goto cleanup;
827 }
828
829 log_debug("LZ4 decompression finished (%zu -> %zu bytes, %.1f%%)",
830 total_in, total_out,
831 total_in > 0 ? (double) total_out / total_in * 100 : 0.0);
832 cleanup:
833 munmap(src, st.st_size);
834 return r;
835 #else
836 return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT),
837 "Cannot decompress file. Compiled without LZ4 support.");
838 #endif
839 }
840
841 int compress_stream_zstd(int fdf, int fdt, uint64_t max_bytes) {
842 #if HAVE_ZSTD
843 _cleanup_(ZSTD_freeCCtxp) ZSTD_CCtx *cctx = NULL;
844 _cleanup_free_ void *in_buff = NULL, *out_buff = NULL;
845 size_t in_allocsize, out_allocsize;
846 size_t z;
847 uint64_t left = max_bytes, in_bytes = 0;
848
849 assert(fdf >= 0);
850 assert(fdt >= 0);
851
852 /* Create the context and buffers */
853 in_allocsize = ZSTD_CStreamInSize();
854 out_allocsize = ZSTD_CStreamOutSize();
855 in_buff = malloc(in_allocsize);
856 out_buff = malloc(out_allocsize);
857 cctx = ZSTD_createCCtx();
858 if (!cctx || !out_buff || !in_buff)
859 return -ENOMEM;
860
861 z = ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1);
862 if (ZSTD_isError(z))
863 log_debug("Failed to enable ZSTD checksum, ignoring: %s", ZSTD_getErrorName(z));
864
865 /* This loop read from the input file, compresses that entire chunk,
866 * and writes all output produced to the output file.
867 */
868 for (;;) {
869 bool is_last_chunk;
870 ZSTD_inBuffer input = {
871 .src = in_buff,
872 .size = 0,
873 .pos = 0
874 };
875 ssize_t red;
876
877 red = loop_read(fdf, in_buff, in_allocsize, true);
878 if (red < 0)
879 return red;
880 is_last_chunk = red == 0;
881
882 in_bytes += (size_t) red;
883 input.size = (size_t) red;
884
885 for (bool finished = false; !finished;) {
886 ZSTD_outBuffer output = {
887 .dst = out_buff,
888 .size = out_allocsize,
889 .pos = 0
890 };
891 size_t remaining;
892 ssize_t wrote;
893
894 /* Compress into the output buffer and write all of the
895 * output to the file so we can reuse the buffer next
896 * iteration.
897 */
898 remaining = ZSTD_compressStream2(
899 cctx, &output, &input,
900 is_last_chunk ? ZSTD_e_end : ZSTD_e_continue);
901
902 if (ZSTD_isError(remaining)) {
903 log_debug("ZSTD encoder failed: %s", ZSTD_getErrorName(remaining));
904 return zstd_ret_to_errno(remaining);
905 }
906
907 if (left < output.pos)
908 return -EFBIG;
909
910 wrote = loop_write(fdt, output.dst, output.pos, 1);
911 if (wrote < 0)
912 return wrote;
913
914 left -= output.pos;
915
916 /* If we're on the last chunk we're finished when zstd
917 * returns 0, which means its consumed all the input AND
918 * finished the frame. Otherwise, we're finished when
919 * we've consumed all the input.
920 */
921 finished = is_last_chunk ? (remaining == 0) : (input.pos == input.size);
922 }
923
924 /* zstd only returns 0 when the input is completely consumed */
925 assert(input.pos == input.size);
926 if (is_last_chunk)
927 break;
928 }
929
930 if (in_bytes > 0)
931 log_debug("ZSTD compression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
932 in_bytes, max_bytes - left, (double) (max_bytes - left) / in_bytes * 100);
933 else
934 log_debug("ZSTD compression finished (%" PRIu64 " -> %" PRIu64 " bytes)",
935 in_bytes, max_bytes - left);
936
937 return 0;
938 #else
939 return -EPROTONOSUPPORT;
940 #endif
941 }
942
943 int decompress_stream_zstd(int fdf, int fdt, uint64_t max_bytes) {
944 #if HAVE_ZSTD
945 _cleanup_(ZSTD_freeDCtxp) ZSTD_DCtx *dctx = NULL;
946 _cleanup_free_ void *in_buff = NULL, *out_buff = NULL;
947 size_t in_allocsize, out_allocsize;
948 size_t last_result = 0;
949 uint64_t left = max_bytes, in_bytes = 0;
950
951 assert(fdf >= 0);
952 assert(fdt >= 0);
953
954 /* Create the context and buffers */
955 in_allocsize = ZSTD_DStreamInSize();
956 out_allocsize = ZSTD_DStreamOutSize();
957 in_buff = malloc(in_allocsize);
958 out_buff = malloc(out_allocsize);
959 dctx = ZSTD_createDCtx();
960 if (!dctx || !out_buff || !in_buff)
961 return -ENOMEM;
962
963 /* This loop assumes that the input file is one or more concatenated
964 * zstd streams. This example won't work if there is trailing non-zstd
965 * data at the end, but streaming decompression in general handles this
966 * case. ZSTD_decompressStream() returns 0 exactly when the frame is
967 * completed, and doesn't consume input after the frame.
968 */
969 for (;;) {
970 bool has_error = false;
971 ZSTD_inBuffer input = {
972 .src = in_buff,
973 .size = 0,
974 .pos = 0
975 };
976 ssize_t red;
977
978 red = loop_read(fdf, in_buff, in_allocsize, true);
979 if (red < 0)
980 return red;
981 if (red == 0)
982 break;
983
984 in_bytes += (size_t) red;
985 input.size = (size_t) red;
986 input.pos = 0;
987
988 /* Given a valid frame, zstd won't consume the last byte of the
989 * frame until it has flushed all of the decompressed data of
990 * the frame. So input.pos < input.size means frame is not done
991 * or there is still output available.
992 */
993 while (input.pos < input.size) {
994 ZSTD_outBuffer output = {
995 .dst = out_buff,
996 .size = out_allocsize,
997 .pos = 0
998 };
999 ssize_t wrote;
1000 /* The return code is zero if the frame is complete, but
1001 * there may be multiple frames concatenated together.
1002 * Zstd will automatically reset the context when a
1003 * frame is complete. Still, calling ZSTD_DCtx_reset()
1004 * can be useful to reset the context to a clean state,
1005 * for instance if the last decompression call returned
1006 * an error.
1007 */
1008 last_result = ZSTD_decompressStream(dctx, &output, &input);
1009 if (ZSTD_isError(last_result)) {
1010 has_error = true;
1011 break;
1012 }
1013
1014 if (left < output.pos)
1015 return -EFBIG;
1016
1017 wrote = loop_write(fdt, output.dst, output.pos, 1);
1018 if (wrote < 0)
1019 return wrote;
1020
1021 left -= output.pos;
1022 }
1023 if (has_error)
1024 break;
1025 }
1026
1027 if (in_bytes == 0)
1028 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "ZSTD decoder failed: no data read");
1029
1030 if (last_result != 0) {
1031 /* The last return value from ZSTD_decompressStream did not end
1032 * on a frame, but we reached the end of the file! We assume
1033 * this is an error, and the input was truncated.
1034 */
1035 log_debug("ZSTD decoder failed: %s", ZSTD_getErrorName(last_result));
1036 return zstd_ret_to_errno(last_result);
1037 }
1038
1039 log_debug(
1040 "ZSTD decompression finished (%" PRIu64 " -> %" PRIu64 " bytes, %.1f%%)",
1041 in_bytes,
1042 max_bytes - left,
1043 (double) (max_bytes - left) / in_bytes * 100);
1044 return 0;
1045 #else
1046 return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT),
1047 "Cannot decompress file. Compiled without ZSTD support.");
1048 #endif
1049 }
1050
1051 int decompress_stream(const char *filename, int fdf, int fdt, uint64_t max_bytes) {
1052
1053 if (endswith(filename, ".lz4"))
1054 return decompress_stream_lz4(fdf, fdt, max_bytes);
1055 else if (endswith(filename, ".xz"))
1056 return decompress_stream_xz(fdf, fdt, max_bytes);
1057 else if (endswith(filename, ".zst"))
1058 return decompress_stream_zstd(fdf, fdt, max_bytes);
1059 else
1060 return -EPROTONOSUPPORT;
1061 }