]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/compress.c
tree-wide: drop string.h when string-util.h or friends are included
[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 #include "alloc-util.h"
20 #include "compress.h"
21 #include "fd-util.h"
22 #include "io-util.h"
23 #include "journal-def.h"
24 #include "macro.h"
25 #include "sparse-endian.h"
26 #include "string-table.h"
27 #include "string-util.h"
28 #include "unaligned.h"
29 #include "util.h"
30
31 #if HAVE_LZ4
32 DEFINE_TRIVIAL_CLEANUP_FUNC(LZ4F_compressionContext_t, LZ4F_freeCompressionContext);
33 DEFINE_TRIVIAL_CLEANUP_FUNC(LZ4F_decompressionContext_t, LZ4F_freeDecompressionContext);
34 #endif
35
36 #define ALIGN_8(l) ALIGN_TO(l, sizeof(size_t))
37
38 static const char* const object_compressed_table[_OBJECT_COMPRESSED_MAX] = {
39 [OBJECT_COMPRESSED_XZ] = "XZ",
40 [OBJECT_COMPRESSED_LZ4] = "LZ4",
41 };
42
43 DEFINE_STRING_TABLE_LOOKUP(object_compressed, int);
44
45 int compress_blob_xz(const void *src, uint64_t src_size,
46 void *dst, size_t dst_alloc_size, size_t *dst_size) {
47 #if HAVE_XZ
48 static const lzma_options_lzma opt = {
49 1u << 20u, NULL, 0, LZMA_LC_DEFAULT, LZMA_LP_DEFAULT,
50 LZMA_PB_DEFAULT, LZMA_MODE_FAST, 128, LZMA_MF_HC3, 4
51 };
52 static const lzma_filter filters[] = {
53 { LZMA_FILTER_LZMA2, (lzma_options_lzma*) &opt },
54 { LZMA_VLI_UNKNOWN, NULL }
55 };
56 lzma_ret ret;
57 size_t out_pos = 0;
58
59 assert(src);
60 assert(src_size > 0);
61 assert(dst);
62 assert(dst_alloc_size > 0);
63 assert(dst_size);
64
65 /* Returns < 0 if we couldn't compress the data or the
66 * compressed result is longer than the original */
67
68 if (src_size < 80)
69 return -ENOBUFS;
70
71 ret = lzma_stream_buffer_encode((lzma_filter*) filters, LZMA_CHECK_NONE, NULL,
72 src, src_size, dst, &out_pos, dst_alloc_size);
73 if (ret != LZMA_OK)
74 return -ENOBUFS;
75
76 *dst_size = out_pos;
77 return 0;
78 #else
79 return -EPROTONOSUPPORT;
80 #endif
81 }
82
83 int compress_blob_lz4(const void *src, uint64_t src_size,
84 void *dst, size_t dst_alloc_size, size_t *dst_size) {
85 #if HAVE_LZ4
86 int r;
87
88 assert(src);
89 assert(src_size > 0);
90 assert(dst);
91 assert(dst_alloc_size > 0);
92 assert(dst_size);
93
94 /* Returns < 0 if we couldn't compress the data or the
95 * compressed result is longer than the original */
96
97 if (src_size < 9)
98 return -ENOBUFS;
99
100 r = LZ4_compress_default(src, (char*)dst + 8, src_size, (int) dst_alloc_size - 8);
101 if (r <= 0)
102 return -ENOBUFS;
103
104 unaligned_write_le64(dst, src_size);
105 *dst_size = r + 8;
106
107 return 0;
108 #else
109 return -EPROTONOSUPPORT;
110 #endif
111 }
112
113 int decompress_blob_xz(const void *src, uint64_t src_size,
114 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
115
116 #if HAVE_XZ
117 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
118 lzma_ret ret;
119 size_t space;
120
121 assert(src);
122 assert(src_size > 0);
123 assert(dst);
124 assert(dst_alloc_size);
125 assert(dst_size);
126 assert(*dst_alloc_size == 0 || *dst);
127
128 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
129 if (ret != LZMA_OK)
130 return -ENOMEM;
131
132 space = MIN(src_size * 2, dst_max ?: (size_t) -1);
133 if (!greedy_realloc(dst, dst_alloc_size, space, 1))
134 return -ENOMEM;
135
136 s.next_in = src;
137 s.avail_in = src_size;
138
139 s.next_out = *dst;
140 s.avail_out = space;
141
142 for (;;) {
143 size_t used;
144
145 ret = lzma_code(&s, LZMA_FINISH);
146
147 if (ret == LZMA_STREAM_END)
148 break;
149 else if (ret != LZMA_OK)
150 return -ENOMEM;
151
152 if (dst_max > 0 && (space - s.avail_out) >= dst_max)
153 break;
154 else if (dst_max > 0 && space == dst_max)
155 return -ENOBUFS;
156
157 used = space - s.avail_out;
158 space = MIN(2 * space, dst_max ?: (size_t) -1);
159 if (!greedy_realloc(dst, dst_alloc_size, space, 1))
160 return -ENOMEM;
161
162 s.avail_out = space - used;
163 s.next_out = *(uint8_t**)dst + used;
164 }
165
166 *dst_size = space - s.avail_out;
167 return 0;
168 #else
169 return -EPROTONOSUPPORT;
170 #endif
171 }
172
173 int decompress_blob_lz4(const void *src, uint64_t src_size,
174 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
175
176 #if HAVE_LZ4
177 char* out;
178 int r, size; /* LZ4 uses int for size */
179
180 assert(src);
181 assert(src_size > 0);
182 assert(dst);
183 assert(dst_alloc_size);
184 assert(dst_size);
185 assert(*dst_alloc_size == 0 || *dst);
186
187 if (src_size <= 8)
188 return -EBADMSG;
189
190 size = unaligned_read_le64(src);
191 if (size < 0 || (unsigned) size != unaligned_read_le64(src))
192 return -EFBIG;
193 if ((size_t) size > *dst_alloc_size) {
194 out = realloc(*dst, size);
195 if (!out)
196 return -ENOMEM;
197 *dst = out;
198 *dst_alloc_size = size;
199 } else
200 out = *dst;
201
202 r = LZ4_decompress_safe((char*)src + 8, out, src_size - 8, size);
203 if (r < 0 || r != size)
204 return -EBADMSG;
205
206 *dst_size = size;
207 return 0;
208 #else
209 return -EPROTONOSUPPORT;
210 #endif
211 }
212
213 int decompress_blob(int compression,
214 const void *src, uint64_t src_size,
215 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {
216 if (compression == OBJECT_COMPRESSED_XZ)
217 return decompress_blob_xz(src, src_size,
218 dst, dst_alloc_size, dst_size, dst_max);
219 else if (compression == OBJECT_COMPRESSED_LZ4)
220 return decompress_blob_lz4(src, src_size,
221 dst, dst_alloc_size, dst_size, dst_max);
222 else
223 return -EBADMSG;
224 }
225
226 int decompress_startswith_xz(const void *src, uint64_t src_size,
227 void **buffer, size_t *buffer_size,
228 const void *prefix, size_t prefix_len,
229 uint8_t extra) {
230
231 #if HAVE_XZ
232 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
233 lzma_ret ret;
234
235 /* Checks whether the decompressed blob starts with the
236 * mentioned prefix. The byte extra needs to follow the
237 * prefix */
238
239 assert(src);
240 assert(src_size > 0);
241 assert(buffer);
242 assert(buffer_size);
243 assert(prefix);
244 assert(*buffer_size == 0 || *buffer);
245
246 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
247 if (ret != LZMA_OK)
248 return -EBADMSG;
249
250 if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1)))
251 return -ENOMEM;
252
253 s.next_in = src;
254 s.avail_in = src_size;
255
256 s.next_out = *buffer;
257 s.avail_out = *buffer_size;
258
259 for (;;) {
260 ret = lzma_code(&s, LZMA_FINISH);
261
262 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END))
263 return -EBADMSG;
264
265 if (*buffer_size - s.avail_out >= prefix_len + 1)
266 return memcmp(*buffer, prefix, prefix_len) == 0 &&
267 ((const uint8_t*) *buffer)[prefix_len] == extra;
268
269 if (ret == LZMA_STREAM_END)
270 return 0;
271
272 s.avail_out += *buffer_size;
273
274 if (!(greedy_realloc(buffer, buffer_size, *buffer_size * 2, 1)))
275 return -ENOMEM;
276
277 s.next_out = *(uint8_t**)buffer + *buffer_size - s.avail_out;
278 }
279
280 #else
281 return -EPROTONOSUPPORT;
282 #endif
283 }
284
285 int decompress_startswith_lz4(const void *src, uint64_t src_size,
286 void **buffer, size_t *buffer_size,
287 const void *prefix, size_t prefix_len,
288 uint8_t extra) {
289 #if HAVE_LZ4
290 /* Checks whether the decompressed blob starts with the
291 * mentioned prefix. The byte extra needs to follow the
292 * prefix */
293
294 int r;
295
296 assert(src);
297 assert(src_size > 0);
298 assert(buffer);
299 assert(buffer_size);
300 assert(prefix);
301 assert(*buffer_size == 0 || *buffer);
302
303 if (src_size <= 8)
304 return -EBADMSG;
305
306 if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1)))
307 return -ENOMEM;
308
309 r = LZ4_decompress_safe_partial((char*)src + 8, *buffer, src_size - 8,
310 prefix_len + 1, *buffer_size);
311 /* One lz4 < 1.8.3, we might get "failure" (r < 0), or "success" where
312 * just a part of the buffer is decompressed. But if we get a smaller
313 * amount of bytes than requested, we don't know whether there isn't enough
314 * data to fill the requested size or whether we just got a partial answer.
315 */
316 if (r < 0 || (size_t) r < prefix_len + 1) {
317 size_t size;
318
319 if (LZ4_versionNumber() >= 10803)
320 /* We trust that the newer lz4 decompresses the number of bytes we
321 * requested if available in the compressed string. */
322 return 0;
323
324 if (r > 0)
325 /* Compare what we have first, in case of mismatch we can
326 * shortcut the full comparison. */
327 if (memcmp(*buffer, prefix, r) != 0)
328 return 0;
329
330 /* Before version 1.8.3, lz4 always tries to decode full a "sequence",
331 * so in pathological cases might need to decompress the full field. */
332 r = decompress_blob_lz4(src, src_size, buffer, buffer_size, &size, 0);
333 if (r < 0)
334 return r;
335
336 if (size < prefix_len + 1)
337 return 0;
338 }
339
340 return memcmp(*buffer, prefix, prefix_len) == 0 &&
341 ((const uint8_t*) *buffer)[prefix_len] == extra;
342 #else
343 return -EPROTONOSUPPORT;
344 #endif
345 }
346
347 int decompress_startswith(int compression,
348 const void *src, uint64_t src_size,
349 void **buffer, size_t *buffer_size,
350 const void *prefix, size_t prefix_len,
351 uint8_t extra) {
352 if (compression == OBJECT_COMPRESSED_XZ)
353 return decompress_startswith_xz(src, src_size,
354 buffer, buffer_size,
355 prefix, prefix_len,
356 extra);
357 else if (compression == OBJECT_COMPRESSED_LZ4)
358 return decompress_startswith_lz4(src, src_size,
359 buffer, buffer_size,
360 prefix, prefix_len,
361 extra);
362 else
363 return -EBADMSG;
364 }
365
366 int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes) {
367 #if HAVE_XZ
368 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
369 lzma_ret ret;
370 uint8_t buf[BUFSIZ], out[BUFSIZ];
371 lzma_action action = LZMA_RUN;
372
373 assert(fdf >= 0);
374 assert(fdt >= 0);
375
376 ret = lzma_easy_encoder(&s, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64);
377 if (ret != LZMA_OK) {
378 log_error("Failed to initialize XZ encoder: code %u", ret);
379 return -EINVAL;
380 }
381
382 for (;;) {
383 if (s.avail_in == 0 && action == LZMA_RUN) {
384 size_t m = sizeof(buf);
385 ssize_t n;
386
387 if (max_bytes != (uint64_t) -1 && (uint64_t) m > max_bytes)
388 m = (size_t) max_bytes;
389
390 n = read(fdf, buf, m);
391 if (n < 0)
392 return -errno;
393 if (n == 0)
394 action = LZMA_FINISH;
395 else {
396 s.next_in = buf;
397 s.avail_in = n;
398
399 if (max_bytes != (uint64_t) -1) {
400 assert(max_bytes >= (uint64_t) n);
401 max_bytes -= n;
402 }
403 }
404 }
405
406 if (s.avail_out == 0) {
407 s.next_out = out;
408 s.avail_out = sizeof(out);
409 }
410
411 ret = lzma_code(&s, action);
412 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END)) {
413 log_error("Compression failed: code %u", ret);
414 return -EBADMSG;
415 }
416
417 if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
418 ssize_t n, k;
419
420 n = sizeof(out) - s.avail_out;
421
422 k = loop_write(fdt, out, n, false);
423 if (k < 0)
424 return k;
425
426 if (ret == LZMA_STREAM_END) {
427 log_debug("XZ compression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
428 s.total_in, s.total_out,
429 (double) s.total_out / s.total_in * 100);
430
431 return 0;
432 }
433 }
434 }
435 #else
436 return -EPROTONOSUPPORT;
437 #endif
438 }
439
440 #define LZ4_BUFSIZE (512*1024u)
441
442 int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes) {
443
444 #if HAVE_LZ4
445 LZ4F_errorCode_t c;
446 _cleanup_(LZ4F_freeCompressionContextp) LZ4F_compressionContext_t ctx = NULL;
447 _cleanup_free_ char *buf = NULL;
448 char *src = NULL;
449 size_t size, n, total_in = 0, total_out, offset = 0, frame_size;
450 struct stat st;
451 int r;
452 static const LZ4F_compressOptions_t options = {
453 .stableSrc = 1,
454 };
455 static const LZ4F_preferences_t preferences = {
456 .frameInfo.blockSizeID = 5,
457 };
458
459 c = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
460 if (LZ4F_isError(c))
461 return -ENOMEM;
462
463 if (fstat(fdf, &st) < 0)
464 return log_debug_errno(errno, "fstat() failed: %m");
465
466 frame_size = LZ4F_compressBound(LZ4_BUFSIZE, &preferences);
467 size = frame_size + 64*1024; /* add some space for header and trailer */
468 buf = malloc(size);
469 if (!buf)
470 return -ENOMEM;
471
472 n = offset = total_out = LZ4F_compressBegin(ctx, buf, size, &preferences);
473 if (LZ4F_isError(n))
474 return -EINVAL;
475
476 src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fdf, 0);
477 if (src == MAP_FAILED)
478 return -errno;
479
480 log_debug("Buffer size is %zu bytes, header size %zu bytes.", size, n);
481
482 while (total_in < (size_t) st.st_size) {
483 ssize_t k;
484
485 k = MIN(LZ4_BUFSIZE, st.st_size - total_in);
486 n = LZ4F_compressUpdate(ctx, buf + offset, size - offset,
487 src + total_in, k, &options);
488 if (LZ4F_isError(n)) {
489 r = -ENOTRECOVERABLE;
490 goto cleanup;
491 }
492
493 total_in += k;
494 offset += n;
495 total_out += n;
496
497 if (max_bytes != (uint64_t) -1 && total_out > (size_t) max_bytes) {
498 log_debug("Compressed stream longer than %"PRIu64" bytes", max_bytes);
499 return -EFBIG;
500 }
501
502 if (size - offset < frame_size + 4) {
503 k = loop_write(fdt, buf, offset, false);
504 if (k < 0) {
505 r = k;
506 goto cleanup;
507 }
508 offset = 0;
509 }
510 }
511
512 n = LZ4F_compressEnd(ctx, buf + offset, size - offset, &options);
513 if (LZ4F_isError(n)) {
514 r = -ENOTRECOVERABLE;
515 goto cleanup;
516 }
517
518 offset += n;
519 total_out += n;
520 r = loop_write(fdt, buf, offset, false);
521 if (r < 0)
522 goto cleanup;
523
524 log_debug("LZ4 compression finished (%zu -> %zu bytes, %.1f%%)",
525 total_in, total_out,
526 (double) total_out / total_in * 100);
527 cleanup:
528 munmap(src, st.st_size);
529 return r;
530 #else
531 return -EPROTONOSUPPORT;
532 #endif
533 }
534
535 int decompress_stream_xz(int fdf, int fdt, uint64_t max_bytes) {
536
537 #if HAVE_XZ
538 _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
539 lzma_ret ret;
540
541 uint8_t buf[BUFSIZ], out[BUFSIZ];
542 lzma_action action = LZMA_RUN;
543
544 assert(fdf >= 0);
545 assert(fdt >= 0);
546
547 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
548 if (ret != LZMA_OK) {
549 log_debug("Failed to initialize XZ decoder: code %u", ret);
550 return -ENOMEM;
551 }
552
553 for (;;) {
554 if (s.avail_in == 0 && action == LZMA_RUN) {
555 ssize_t n;
556
557 n = read(fdf, buf, sizeof(buf));
558 if (n < 0)
559 return -errno;
560 if (n == 0)
561 action = LZMA_FINISH;
562 else {
563 s.next_in = buf;
564 s.avail_in = n;
565 }
566 }
567
568 if (s.avail_out == 0) {
569 s.next_out = out;
570 s.avail_out = sizeof(out);
571 }
572
573 ret = lzma_code(&s, action);
574 if (!IN_SET(ret, LZMA_OK, LZMA_STREAM_END)) {
575 log_debug("Decompression failed: code %u", ret);
576 return -EBADMSG;
577 }
578
579 if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
580 ssize_t n, k;
581
582 n = sizeof(out) - s.avail_out;
583
584 if (max_bytes != (uint64_t) -1) {
585 if (max_bytes < (uint64_t) n)
586 return -EFBIG;
587
588 max_bytes -= n;
589 }
590
591 k = loop_write(fdt, out, n, false);
592 if (k < 0)
593 return k;
594
595 if (ret == LZMA_STREAM_END) {
596 log_debug("XZ decompression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)",
597 s.total_in, s.total_out,
598 (double) s.total_out / s.total_in * 100);
599
600 return 0;
601 }
602 }
603 }
604 #else
605 log_debug("Cannot decompress file. Compiled without XZ support.");
606 return -EPROTONOSUPPORT;
607 #endif
608 }
609
610 int decompress_stream_lz4(int in, int out, uint64_t max_bytes) {
611 #if HAVE_LZ4
612 size_t c;
613 _cleanup_(LZ4F_freeDecompressionContextp) LZ4F_decompressionContext_t ctx = NULL;
614 _cleanup_free_ char *buf = NULL;
615 char *src;
616 struct stat st;
617 int r = 0;
618 size_t total_in = 0, total_out = 0;
619
620 c = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
621 if (LZ4F_isError(c))
622 return -ENOMEM;
623
624 if (fstat(in, &st) < 0)
625 return log_debug_errno(errno, "fstat() failed: %m");
626
627 buf = malloc(LZ4_BUFSIZE);
628 if (!buf)
629 return -ENOMEM;
630
631 src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, in, 0);
632 if (src == MAP_FAILED)
633 return -errno;
634
635 while (total_in < (size_t) st.st_size) {
636 size_t produced = LZ4_BUFSIZE;
637 size_t used = st.st_size - total_in;
638
639 c = LZ4F_decompress(ctx, buf, &produced, src + total_in, &used, NULL);
640 if (LZ4F_isError(c)) {
641 r = -EBADMSG;
642 goto cleanup;
643 }
644
645 total_in += used;
646 total_out += produced;
647
648 if (max_bytes != (uint64_t) -1 && total_out > (size_t) max_bytes) {
649 log_debug("Decompressed stream longer than %"PRIu64" bytes", max_bytes);
650 r = -EFBIG;
651 goto cleanup;
652 }
653
654 r = loop_write(out, buf, produced, false);
655 if (r < 0)
656 goto cleanup;
657 }
658
659 log_debug("LZ4 decompression finished (%zu -> %zu bytes, %.1f%%)",
660 total_in, total_out,
661 total_in > 0 ? (double) total_out / total_in * 100 : 0.0);
662 cleanup:
663 munmap(src, st.st_size);
664 return r;
665 #else
666 log_debug("Cannot decompress file. Compiled without LZ4 support.");
667 return -EPROTONOSUPPORT;
668 #endif
669 }
670
671 int decompress_stream(const char *filename, int fdf, int fdt, uint64_t max_bytes) {
672
673 if (endswith(filename, ".lz4"))
674 return decompress_stream_lz4(fdf, fdt, max_bytes);
675 else if (endswith(filename, ".xz"))
676 return decompress_stream_xz(fdf, fdt, max_bytes);
677 else
678 return -EPROTONOSUPPORT;
679 }