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