]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/compress.c
journal/compress: drop "future" code in zstd compression
[thirdparty/systemd.git] / src / journal / compress.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
e4e61fdb 2
82e24b00 3#include <inttypes.h>
e4e61fdb 4#include <stdlib.h>
4b5bc539 5#include <sys/mman.h>
ca78ad1d
ZJS
6#include <sys/types.h>
7#include <sys/stat.h>
07630cea 8#include <unistd.h>
d89c8fdf 9
349cc4a5 10#if HAVE_XZ
3ffd4af2 11#include <lzma.h>
d89c8fdf
ZJS
12#endif
13
349cc4a5 14#if HAVE_LZ4
3ffd4af2
LP
15#include <lz4.h>
16#include <lz4frame.h>
d89c8fdf 17#endif
e4e61fdb 18
ef5924aa
NL
19#if HAVE_ZSTD
20#include <zstd.h>
21#include <zstd_errors.h>
22#endif
23
b5efdb8a 24#include "alloc-util.h"
e4e61fdb 25#include "compress.h"
3ffd4af2 26#include "fd-util.h"
c004493c 27#include "io-util.h"
07630cea 28#include "journal-def.h"
355b59e2 29#include "macro.h"
d89c8fdf 30#include "sparse-endian.h"
8b43440b 31#include "string-table.h"
07630cea 32#include "string-util.h"
4094c4bf 33#include "unaligned.h"
07630cea 34#include "util.h"
d89c8fdf 35
349cc4a5 36#if HAVE_LZ4
4b5bc539
ZJS
37DEFINE_TRIVIAL_CLEANUP_FUNC(LZ4F_compressionContext_t, LZ4F_freeCompressionContext);
38DEFINE_TRIVIAL_CLEANUP_FUNC(LZ4F_decompressionContext_t, LZ4F_freeDecompressionContext);
39#endif
40
ef5924aa
NL
41#if HAVE_ZSTD
42DEFINE_TRIVIAL_CLEANUP_FUNC(ZSTD_CCtx *, ZSTD_freeCCtx);
43DEFINE_TRIVIAL_CLEANUP_FUNC(ZSTD_DCtx *, ZSTD_freeDCtx);
44
45static 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
d89c8fdf
ZJS
57#define ALIGN_8(l) ALIGN_TO(l, sizeof(size_t))
58
59static const char* const object_compressed_table[_OBJECT_COMPRESSED_MAX] = {
e9ece6a0
LP
60 [OBJECT_COMPRESSED_XZ] = "XZ",
61 [OBJECT_COMPRESSED_LZ4] = "LZ4",
8653185a 62 [OBJECT_COMPRESSED_ZSTD] = "ZSTD",
e9ece6a0
LP
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 */
d89c8fdf 65};
e4e61fdb 66
d89c8fdf
ZJS
67DEFINE_STRING_TABLE_LOOKUP(object_compressed, int);
68
5d6f46b6
ZJS
69int compress_blob_xz(const void *src, uint64_t src_size,
70 void *dst, size_t dst_alloc_size, size_t *dst_size) {
349cc4a5 71#if HAVE_XZ
1930eed2
JS
72 static const lzma_options_lzma opt = {
73 1u << 20u, NULL, 0, LZMA_LC_DEFAULT, LZMA_LP_DEFAULT,
0240c603
LP
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 }
1930eed2 79 };
e4e61fdb 80 lzma_ret ret;
76cc0bf6 81 size_t out_pos = 0;
e4e61fdb
LP
82
83 assert(src);
84 assert(src_size > 0);
85 assert(dst);
5d6f46b6 86 assert(dst_alloc_size > 0);
e4e61fdb
LP
87 assert(dst_size);
88
d89c8fdf 89 /* Returns < 0 if we couldn't compress the data or the
e4e61fdb
LP
90 * compressed result is longer than the original */
91
1930eed2
JS
92 if (src_size < 80)
93 return -ENOBUFS;
94
95 ret = lzma_stream_buffer_encode((lzma_filter*) filters, LZMA_CHECK_NONE, NULL,
5d6f46b6 96 src, src_size, dst, &out_pos, dst_alloc_size);
e4e61fdb 97 if (ret != LZMA_OK)
d89c8fdf 98 return -ENOBUFS;
e4e61fdb 99
76cc0bf6 100 *dst_size = out_pos;
d89c8fdf
ZJS
101 return 0;
102#else
103 return -EPROTONOSUPPORT;
104#endif
e4e61fdb
LP
105}
106
5d6f46b6
ZJS
107int compress_blob_lz4(const void *src, uint64_t src_size,
108 void *dst, size_t dst_alloc_size, size_t *dst_size) {
349cc4a5 109#if HAVE_LZ4
d89c8fdf
ZJS
110 int r;
111
112 assert(src);
113 assert(src_size > 0);
114 assert(dst);
5d6f46b6 115 assert(dst_alloc_size > 0);
d89c8fdf
ZJS
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;
e4e61fdb 123
691b90d4 124 r = LZ4_compress_default(src, (char*)dst + 8, src_size, (int) dst_alloc_size - 8);
d89c8fdf
ZJS
125 if (r <= 0)
126 return -ENOBUFS;
127
4094c4bf 128 unaligned_write_le64(dst, src_size);
d89c8fdf
ZJS
129 *dst_size = r + 8;
130
131 return 0;
132#else
133 return -EPROTONOSUPPORT;
134#endif
135}
136
8653185a
LP
137int 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
d89c8fdf 160int decompress_blob_xz(const void *src, uint64_t src_size,
fa1c4b51 161 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
d89c8fdf 162
349cc4a5 163#if HAVE_XZ
5e592c66 164 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
e4e61fdb 165 lzma_ret ret;
fa1c4b51 166 size_t space;
e4e61fdb
LP
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)
d89c8fdf 177 return -ENOMEM;
e4e61fdb 178
fa1c4b51 179 space = MIN(src_size * 2, dst_max ?: (size_t) -1);
5e592c66 180 if (!greedy_realloc(dst, dst_alloc_size, space, 1))
01c3322e 181 return -ENOMEM;
e4e61fdb
LP
182
183 s.next_in = src;
184 s.avail_in = src_size;
185
186 s.next_out = *dst;
93b73b06 187 s.avail_out = space;
e4e61fdb
LP
188
189 for (;;) {
fa1c4b51 190 size_t used;
e4e61fdb
LP
191
192 ret = lzma_code(&s, LZMA_FINISH);
193
194 if (ret == LZMA_STREAM_END)
195 break;
d89c8fdf
ZJS
196 else if (ret != LZMA_OK)
197 return -ENOMEM;
e4e61fdb 198
93b73b06
LP
199 if (dst_max > 0 && (space - s.avail_out) >= dst_max)
200 break;
d89c8fdf
ZJS
201 else if (dst_max > 0 && space == dst_max)
202 return -ENOBUFS;
93b73b06 203
5e592c66 204 used = space - s.avail_out;
fa1c4b51 205 space = MIN(2 * space, dst_max ?: (size_t) -1);
5e592c66 206 if (!greedy_realloc(dst, dst_alloc_size, space, 1))
01c3322e 207 return -ENOMEM;
e4e61fdb 208
5e592c66 209 s.avail_out = space - used;
8e170d29 210 s.next_out = *(uint8_t**)dst + used;
e4e61fdb
LP
211 }
212
93b73b06 213 *dst_size = space - s.avail_out;
d89c8fdf
ZJS
214 return 0;
215#else
216 return -EPROTONOSUPPORT;
217#endif
e4e61fdb
LP
218}
219
d89c8fdf 220int decompress_blob_lz4(const void *src, uint64_t src_size,
fa1c4b51 221 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
d89c8fdf 222
349cc4a5 223#if HAVE_LZ4
d89c8fdf 224 char* out;
fa1c4b51 225 int r, size; /* LZ4 uses int for size */
e4e61fdb 226
d89c8fdf
ZJS
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
4094c4bf
LP
237 size = unaligned_read_le64(src);
238 if (size < 0 || (unsigned) size != unaligned_read_le64(src))
fa1c4b51
ZJS
239 return -EFBIG;
240 if ((size_t) size > *dst_alloc_size) {
d89c8fdf
ZJS
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
8e170d29 249 r = LZ4_decompress_safe((char*)src + 8, out, src_size - 8, size);
fa1c4b51 250 if (r < 0 || r != size)
d89c8fdf
ZJS
251 return -EBADMSG;
252
253 *dst_size = size;
254 return 0;
255#else
256 return -EPROTONOSUPPORT;
257#endif
258}
259
8653185a
LP
260int 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
311int 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
d89c8fdf 316 if (compression == OBJECT_COMPRESSED_XZ)
8653185a
LP
317 return decompress_blob_xz(
318 src, src_size,
319 dst, dst_alloc_size, dst_size, dst_max);
d89c8fdf 320 else if (compression == OBJECT_COMPRESSED_LZ4)
8653185a
LP
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);
d89c8fdf
ZJS
328 else
329 return -EBADMSG;
330}
331
d89c8fdf 332int decompress_startswith_xz(const void *src, uint64_t src_size,
fa1c4b51
ZJS
333 void **buffer, size_t *buffer_size,
334 const void *prefix, size_t prefix_len,
d89c8fdf
ZJS
335 uint8_t extra) {
336
349cc4a5 337#if HAVE_XZ
5e592c66 338 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
e4e61fdb 339 lzma_ret ret;
e4e61fdb 340
d89c8fdf 341 /* Checks whether the decompressed blob starts with the
e4e61fdb
LP
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)
d89c8fdf 354 return -EBADMSG;
e4e61fdb 355
d89c8fdf
ZJS
356 if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1)))
357 return -ENOMEM;
e4e61fdb
LP
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 (;;) {
e4e61fdb
LP
366 ret = lzma_code(&s, LZMA_FINISH);
367
4c701096 368 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
d89c8fdf 369 return -EBADMSG;
e4e61fdb 370
5e592c66
ZJS
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;
e4e61fdb
LP
374
375 if (ret == LZMA_STREAM_END)
d89c8fdf 376 return 0;
e4e61fdb 377
e4e61fdb
LP
378 s.avail_out += *buffer_size;
379
5e592c66 380 if (!(greedy_realloc(buffer, buffer_size, *buffer_size * 2, 1)))
d89c8fdf 381 return -ENOMEM;
e4e61fdb 382
8e170d29 383 s.next_out = *(uint8_t**)buffer + *buffer_size - s.avail_out;
5e592c66 384 }
d89c8fdf
ZJS
385
386#else
387 return -EPROTONOSUPPORT;
388#endif
389}
390
391int decompress_startswith_lz4(const void *src, uint64_t src_size,
fa1c4b51
ZJS
392 void **buffer, size_t *buffer_size,
393 const void *prefix, size_t prefix_len,
d89c8fdf 394 uint8_t extra) {
349cc4a5 395#if HAVE_LZ4
d89c8fdf
ZJS
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
8e170d29 415 r = LZ4_decompress_safe_partial((char*)src + 8, *buffer, src_size - 8,
d89c8fdf 416 prefix_len + 1, *buffer_size);
e41ef6fd
ZJS
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. */
1f4b467d
ZJS
438 r = decompress_blob_lz4(src, src_size, buffer, buffer_size, &size, 0);
439 if (r < 0)
440 return r;
d89c8fdf 441
e41ef6fd
ZJS
442 if (size < prefix_len + 1)
443 return 0;
444 }
d89c8fdf 445
e41ef6fd
ZJS
446 return memcmp(*buffer, prefix, prefix_len) == 0 &&
447 ((const uint8_t*) *buffer)[prefix_len] == extra;
d89c8fdf
ZJS
448#else
449 return -EPROTONOSUPPORT;
450#endif
e4e61fdb 451}
355b59e2 452
8653185a
LP
453int 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
513int 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
d89c8fdf 520 if (compression == OBJECT_COMPRESSED_XZ)
8653185a
LP
521 return decompress_startswith_xz(
522 src, src_size,
523 buffer, buffer_size,
524 prefix, prefix_len,
525 extra);
526
d89c8fdf 527 else if (compression == OBJECT_COMPRESSED_LZ4)
8653185a
LP
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);
d89c8fdf
ZJS
539 else
540 return -EBADMSG;
541}
542
59f448cf 543int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes) {
349cc4a5 544#if HAVE_XZ
355b59e2
ZJS
545 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
546 lzma_ret ret;
355b59e2
ZJS
547 uint8_t buf[BUFSIZ], out[BUFSIZ];
548 lzma_action action = LZMA_RUN;
549
550 assert(fdf >= 0);
551 assert(fdt >= 0);
552
d89c8fdf 553 ret = lzma_easy_encoder(&s, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64);
355b59e2 554 if (ret != LZMA_OK) {
1fa2f38f 555 log_error("Failed to initialize XZ encoder: code %u", ret);
355b59e2
ZJS
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
59f448cf
LP
564 if (max_bytes != (uint64_t) -1 && (uint64_t) m > max_bytes)
565 m = (size_t) max_bytes;
355b59e2
ZJS
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
59f448cf
LP
576 if (max_bytes != (uint64_t) -1) {
577 assert(max_bytes >= (uint64_t) n);
355b59e2
ZJS
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);
4c701096 589 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END)) {
1fa2f38f 590 log_error("Compression failed: code %u", ret);
355b59e2
ZJS
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
355b59e2
ZJS
599 k = loop_write(fdt, out, n, false);
600 if (k < 0)
601 return k;
355b59e2
ZJS
602
603 if (ret == LZMA_STREAM_END) {
fa1c4b51 604 log_debug("XZ compression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
355b59e2
ZJS
605 s.total_in, s.total_out,
606 (double) s.total_out / s.total_in * 100);
607
608 return 0;
609 }
610 }
611 }
3b1a55e1
ZJS
612#else
613 return -EPROTONOSUPPORT;
614#endif
355b59e2
ZJS
615}
616
4b5bc539 617#define LZ4_BUFSIZE (512*1024u)
d89c8fdf 618
59f448cf 619int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes) {
d89c8fdf 620
349cc4a5 621#if HAVE_LZ4
4b5bc539
ZJS
622 LZ4F_errorCode_t c;
623 _cleanup_(LZ4F_freeCompressionContextp) LZ4F_compressionContext_t ctx = NULL;
624 _cleanup_free_ char *buf = NULL;
625 char *src = NULL;
d487b815 626 size_t size, n, total_in = 0, total_out, offset = 0, frame_size;
4b5bc539
ZJS
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 };
d89c8fdf 635
4b5bc539
ZJS
636 c = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
637 if (LZ4F_isError(c))
638 return -ENOMEM;
d89c8fdf 639
4b5bc539 640 if (fstat(fdf, &st) < 0)
5146f9f0 641 return log_debug_errno(errno, "fstat() failed: %m");
d89c8fdf 642
4b5bc539
ZJS
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;
d89c8fdf 648
d487b815 649 n = offset = total_out = LZ4F_compressBegin(ctx, buf, size, &preferences);
4b5bc539
ZJS
650 if (LZ4F_isError(n))
651 return -EINVAL;
d89c8fdf 652
4b5bc539 653 src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fdf, 0);
e0685172 654 if (src == MAP_FAILED)
4b5bc539 655 return -errno;
d89c8fdf 656
4b5bc539 657 log_debug("Buffer size is %zu bytes, header size %zu bytes.", size, n);
d89c8fdf 658
4b5bc539
ZJS
659 while (total_in < (size_t) st.st_size) {
660 ssize_t k;
d89c8fdf 661
4b5bc539
ZJS
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;
d89c8fdf
ZJS
668 }
669
4b5bc539
ZJS
670 total_in += k;
671 offset += n;
672 total_out += n;
d89c8fdf 673
4b5bc539 674 if (max_bytes != (uint64_t) -1 && total_out > (size_t) max_bytes) {
82e24b00 675 log_debug("Compressed stream longer than %"PRIu64" bytes", max_bytes);
4b5bc539
ZJS
676 return -EFBIG;
677 }
d89c8fdf 678
4b5bc539 679 if (size - offset < frame_size + 4) {
4b5bc539
ZJS
680 k = loop_write(fdt, buf, offset, false);
681 if (k < 0) {
682 r = k;
683 goto cleanup;
684 }
685 offset = 0;
686 }
687 }
d89c8fdf 688
4b5bc539
ZJS
689 n = LZ4F_compressEnd(ctx, buf + offset, size - offset, &options);
690 if (LZ4F_isError(n)) {
691 r = -ENOTRECOVERABLE;
692 goto cleanup;
d89c8fdf
ZJS
693 }
694
4b5bc539
ZJS
695 offset += n;
696 total_out += n;
4b5bc539
ZJS
697 r = loop_write(fdt, buf, offset, false);
698 if (r < 0)
699 goto cleanup;
d89c8fdf
ZJS
700
701 log_debug("LZ4 compression finished (%zu -> %zu bytes, %.1f%%)",
702 total_in, total_out,
703 (double) total_out / total_in * 100);
4b5bc539
ZJS
704 cleanup:
705 munmap(src, st.st_size);
706 return r;
d89c8fdf
ZJS
707#else
708 return -EPROTONOSUPPORT;
709#endif
710}
711
59f448cf 712int decompress_stream_xz(int fdf, int fdt, uint64_t max_bytes) {
d89c8fdf 713
349cc4a5 714#if HAVE_XZ
355b59e2
ZJS
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) {
5146f9f0 726 log_debug("Failed to initialize XZ decoder: code %u", ret);
d89c8fdf 727 return -ENOMEM;
355b59e2
ZJS
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);
4c701096 751 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END)) {
5146f9f0 752 log_debug("Decompression failed: code %u", ret);
355b59e2
ZJS
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
59f448cf
LP
761 if (max_bytes != (uint64_t) -1) {
762 if (max_bytes < (uint64_t) n)
d89c8fdf 763 return -EFBIG;
355b59e2
ZJS
764
765 max_bytes -= n;
766 }
767
355b59e2
ZJS
768 k = loop_write(fdt, out, n, false);
769 if (k < 0)
770 return k;
355b59e2
ZJS
771
772 if (ret == LZMA_STREAM_END) {
fa1c4b51 773 log_debug("XZ decompression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
355b59e2
ZJS
774 s.total_in, s.total_out,
775 (double) s.total_out / s.total_in * 100);
776
777 return 0;
778 }
779 }
780 }
d89c8fdf 781#else
5146f9f0 782 log_debug("Cannot decompress file. Compiled without XZ support.");
d89c8fdf
ZJS
783 return -EPROTONOSUPPORT;
784#endif
785}
786
8e64dd1e 787int decompress_stream_lz4(int in, int out, uint64_t max_bytes) {
349cc4a5 788#if HAVE_LZ4
4b5bc539
ZJS
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)
5146f9f0 802 return log_debug_errno(errno, "fstat() failed: %m");
4b5bc539
ZJS
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);
e0685172 809 if (src == MAP_FAILED)
4b5bc539
ZJS
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) {
82e24b00 826 log_debug("Decompressed stream longer than %"PRIu64" bytes", max_bytes);
5146f9f0 827 r = -EFBIG;
4b5bc539
ZJS
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,
25048348 838 total_in > 0 ? (double) total_out / total_in * 100 : 0.0);
4b5bc539
ZJS
839 cleanup:
840 munmap(src, st.st_size);
841 return r;
d89c8fdf 842#else
5146f9f0 843 log_debug("Cannot decompress file. Compiled without LZ4 support.");
d89c8fdf
ZJS
844 return -EPROTONOSUPPORT;
845#endif
846}
847
ef5924aa
NL
848int 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;
ef5924aa
NL
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
ef5924aa
NL
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
949int 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
59f448cf 1057int decompress_stream(const char *filename, int fdf, int fdt, uint64_t max_bytes) {
d89c8fdf
ZJS
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);
ef5924aa
NL
1063 else if (endswith(filename, ".zst"))
1064 return decompress_stream_zstd(fdf, fdt, max_bytes);
d89c8fdf
ZJS
1065 else
1066 return -EPROTONOSUPPORT;
355b59e2 1067}