]>
Commit | Line | Data |
---|---|---|
e4e61fdb LP |
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 | |
5430f7f2 LP |
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 | |
e4e61fdb LP |
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 | |
5430f7f2 | 14 | Lesser General Public License for more details. |
e4e61fdb | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
e4e61fdb LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
82e24b00 | 20 | #include <inttypes.h> |
e4e61fdb LP |
21 | #include <stdlib.h> |
22 | #include <string.h> | |
4b5bc539 | 23 | #include <sys/mman.h> |
07630cea | 24 | #include <unistd.h> |
d89c8fdf | 25 | |
349cc4a5 | 26 | #if HAVE_XZ |
3ffd4af2 | 27 | #include <lzma.h> |
d89c8fdf ZJS |
28 | #endif |
29 | ||
349cc4a5 | 30 | #if HAVE_LZ4 |
3ffd4af2 LP |
31 | #include <lz4.h> |
32 | #include <lz4frame.h> | |
d89c8fdf | 33 | #endif |
e4e61fdb | 34 | |
b5efdb8a | 35 | #include "alloc-util.h" |
e4e61fdb | 36 | #include "compress.h" |
3ffd4af2 | 37 | #include "fd-util.h" |
c004493c | 38 | #include "io-util.h" |
07630cea | 39 | #include "journal-def.h" |
355b59e2 | 40 | #include "macro.h" |
d89c8fdf | 41 | #include "sparse-endian.h" |
8b43440b | 42 | #include "string-table.h" |
07630cea LP |
43 | #include "string-util.h" |
44 | #include "util.h" | |
d89c8fdf | 45 | |
349cc4a5 | 46 | #if HAVE_LZ4 |
4b5bc539 ZJS |
47 | DEFINE_TRIVIAL_CLEANUP_FUNC(LZ4F_compressionContext_t, LZ4F_freeCompressionContext); |
48 | DEFINE_TRIVIAL_CLEANUP_FUNC(LZ4F_decompressionContext_t, LZ4F_freeDecompressionContext); | |
49 | #endif | |
50 | ||
d89c8fdf ZJS |
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 | }; | |
e4e61fdb | 57 | |
d89c8fdf ZJS |
58 | DEFINE_STRING_TABLE_LOOKUP(object_compressed, int); |
59 | ||
5d6f46b6 ZJS |
60 | int compress_blob_xz(const void *src, uint64_t src_size, |
61 | void *dst, size_t dst_alloc_size, size_t *dst_size) { | |
349cc4a5 | 62 | #if HAVE_XZ |
1930eed2 JS |
63 | static const lzma_options_lzma opt = { |
64 | 1u << 20u, NULL, 0, LZMA_LC_DEFAULT, LZMA_LP_DEFAULT, | |
0240c603 LP |
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 } | |
1930eed2 | 70 | }; |
e4e61fdb | 71 | lzma_ret ret; |
76cc0bf6 | 72 | size_t out_pos = 0; |
e4e61fdb LP |
73 | |
74 | assert(src); | |
75 | assert(src_size > 0); | |
76 | assert(dst); | |
5d6f46b6 | 77 | assert(dst_alloc_size > 0); |
e4e61fdb LP |
78 | assert(dst_size); |
79 | ||
d89c8fdf | 80 | /* Returns < 0 if we couldn't compress the data or the |
e4e61fdb LP |
81 | * compressed result is longer than the original */ |
82 | ||
1930eed2 JS |
83 | if (src_size < 80) |
84 | return -ENOBUFS; | |
85 | ||
86 | ret = lzma_stream_buffer_encode((lzma_filter*) filters, LZMA_CHECK_NONE, NULL, | |
5d6f46b6 | 87 | src, src_size, dst, &out_pos, dst_alloc_size); |
e4e61fdb | 88 | if (ret != LZMA_OK) |
d89c8fdf | 89 | return -ENOBUFS; |
e4e61fdb | 90 | |
76cc0bf6 | 91 | *dst_size = out_pos; |
d89c8fdf ZJS |
92 | return 0; |
93 | #else | |
94 | return -EPROTONOSUPPORT; | |
95 | #endif | |
e4e61fdb LP |
96 | } |
97 | ||
5d6f46b6 ZJS |
98 | int compress_blob_lz4(const void *src, uint64_t src_size, |
99 | void *dst, size_t dst_alloc_size, size_t *dst_size) { | |
349cc4a5 | 100 | #if HAVE_LZ4 |
d89c8fdf ZJS |
101 | int r; |
102 | ||
103 | assert(src); | |
104 | assert(src_size > 0); | |
105 | assert(dst); | |
5d6f46b6 | 106 | assert(dst_alloc_size > 0); |
d89c8fdf ZJS |
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; | |
e4e61fdb | 114 | |
691b90d4 ZJS |
115 | #if LZ4_VERSION_NUMBER >= 10700 |
116 | r = LZ4_compress_default(src, (char*)dst + 8, src_size, (int) dst_alloc_size - 8); | |
117 | #else | |
8e170d29 | 118 | r = LZ4_compress_limitedOutput(src, (char*)dst + 8, src_size, (int) dst_alloc_size - 8); |
691b90d4 | 119 | #endif |
d89c8fdf ZJS |
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, | |
fa1c4b51 | 134 | void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) { |
d89c8fdf | 135 | |
349cc4a5 | 136 | #if HAVE_XZ |
5e592c66 | 137 | _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT; |
e4e61fdb | 138 | lzma_ret ret; |
fa1c4b51 | 139 | size_t space; |
e4e61fdb LP |
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) | |
d89c8fdf | 150 | return -ENOMEM; |
e4e61fdb | 151 | |
fa1c4b51 | 152 | space = MIN(src_size * 2, dst_max ?: (size_t) -1); |
5e592c66 | 153 | if (!greedy_realloc(dst, dst_alloc_size, space, 1)) |
01c3322e | 154 | return -ENOMEM; |
e4e61fdb LP |
155 | |
156 | s.next_in = src; | |
157 | s.avail_in = src_size; | |
158 | ||
159 | s.next_out = *dst; | |
93b73b06 | 160 | s.avail_out = space; |
e4e61fdb LP |
161 | |
162 | for (;;) { | |
fa1c4b51 | 163 | size_t used; |
e4e61fdb LP |
164 | |
165 | ret = lzma_code(&s, LZMA_FINISH); | |
166 | ||
167 | if (ret == LZMA_STREAM_END) | |
168 | break; | |
d89c8fdf ZJS |
169 | else if (ret != LZMA_OK) |
170 | return -ENOMEM; | |
e4e61fdb | 171 | |
93b73b06 LP |
172 | if (dst_max > 0 && (space - s.avail_out) >= dst_max) |
173 | break; | |
d89c8fdf ZJS |
174 | else if (dst_max > 0 && space == dst_max) |
175 | return -ENOBUFS; | |
93b73b06 | 176 | |
5e592c66 | 177 | used = space - s.avail_out; |
fa1c4b51 | 178 | space = MIN(2 * space, dst_max ?: (size_t) -1); |
5e592c66 | 179 | if (!greedy_realloc(dst, dst_alloc_size, space, 1)) |
01c3322e | 180 | return -ENOMEM; |
e4e61fdb | 181 | |
5e592c66 | 182 | s.avail_out = space - used; |
8e170d29 | 183 | s.next_out = *(uint8_t**)dst + used; |
e4e61fdb LP |
184 | } |
185 | ||
93b73b06 | 186 | *dst_size = space - s.avail_out; |
d89c8fdf ZJS |
187 | return 0; |
188 | #else | |
189 | return -EPROTONOSUPPORT; | |
190 | #endif | |
e4e61fdb LP |
191 | } |
192 | ||
d89c8fdf | 193 | int decompress_blob_lz4(const void *src, uint64_t src_size, |
fa1c4b51 | 194 | void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) { |
d89c8fdf | 195 | |
349cc4a5 | 196 | #if HAVE_LZ4 |
d89c8fdf | 197 | char* out; |
fa1c4b51 | 198 | int r, size; /* LZ4 uses int for size */ |
e4e61fdb | 199 | |
d89c8fdf ZJS |
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 ); | |
b3aa6229 | 211 | if (size < 0 || (unsigned) size != le64toh(*(le64_t*)src)) |
fa1c4b51 ZJS |
212 | return -EFBIG; |
213 | if ((size_t) size > *dst_alloc_size) { | |
d89c8fdf ZJS |
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 | ||
8e170d29 | 222 | r = LZ4_decompress_safe((char*)src + 8, out, src_size - 8, size); |
fa1c4b51 | 223 | if (r < 0 || r != size) |
d89c8fdf ZJS |
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, | |
fa1c4b51 | 235 | void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) { |
d89c8fdf ZJS |
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, | |
fa1c4b51 ZJS |
248 | void **buffer, size_t *buffer_size, |
249 | const void *prefix, size_t prefix_len, | |
d89c8fdf ZJS |
250 | uint8_t extra) { |
251 | ||
349cc4a5 | 252 | #if HAVE_XZ |
5e592c66 | 253 | _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT; |
e4e61fdb | 254 | lzma_ret ret; |
e4e61fdb | 255 | |
d89c8fdf | 256 | /* Checks whether the decompressed blob starts with the |
e4e61fdb LP |
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) | |
d89c8fdf | 269 | return -EBADMSG; |
e4e61fdb | 270 | |
d89c8fdf ZJS |
271 | if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1))) |
272 | return -ENOMEM; | |
e4e61fdb LP |
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 (;;) { | |
e4e61fdb LP |
281 | ret = lzma_code(&s, LZMA_FINISH); |
282 | ||
283 | if (ret != LZMA_STREAM_END && ret != LZMA_OK) | |
d89c8fdf | 284 | return -EBADMSG; |
e4e61fdb | 285 | |
5e592c66 ZJS |
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; | |
e4e61fdb LP |
289 | |
290 | if (ret == LZMA_STREAM_END) | |
d89c8fdf | 291 | return 0; |
e4e61fdb | 292 | |
e4e61fdb LP |
293 | s.avail_out += *buffer_size; |
294 | ||
5e592c66 | 295 | if (!(greedy_realloc(buffer, buffer_size, *buffer_size * 2, 1))) |
d89c8fdf | 296 | return -ENOMEM; |
e4e61fdb | 297 | |
8e170d29 | 298 | s.next_out = *(uint8_t**)buffer + *buffer_size - s.avail_out; |
5e592c66 | 299 | } |
d89c8fdf ZJS |
300 | |
301 | #else | |
302 | return -EPROTONOSUPPORT; | |
303 | #endif | |
304 | } | |
305 | ||
306 | int decompress_startswith_lz4(const void *src, uint64_t src_size, | |
fa1c4b51 ZJS |
307 | void **buffer, size_t *buffer_size, |
308 | const void *prefix, size_t prefix_len, | |
d89c8fdf | 309 | uint8_t extra) { |
349cc4a5 | 310 | #if HAVE_LZ4 |
d89c8fdf ZJS |
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; | |
1f4b467d | 316 | size_t size; |
d89c8fdf ZJS |
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 | ||
8e170d29 | 331 | r = LZ4_decompress_safe_partial((char*)src + 8, *buffer, src_size - 8, |
d89c8fdf | 332 | prefix_len + 1, *buffer_size); |
1f4b467d ZJS |
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 | } | |
d89c8fdf | 343 | |
1f4b467d | 344 | if (size >= prefix_len + 1) |
d89c8fdf ZJS |
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 | |
e4e61fdb | 353 | } |
355b59e2 | 354 | |
d89c8fdf ZJS |
355 | int decompress_startswith(int compression, |
356 | const void *src, uint64_t src_size, | |
fa1c4b51 ZJS |
357 | void **buffer, size_t *buffer_size, |
358 | const void *prefix, size_t prefix_len, | |
d89c8fdf ZJS |
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 | ||
59f448cf | 374 | int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes) { |
349cc4a5 | 375 | #if HAVE_XZ |
355b59e2 ZJS |
376 | _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT; |
377 | lzma_ret ret; | |
355b59e2 ZJS |
378 | uint8_t buf[BUFSIZ], out[BUFSIZ]; |
379 | lzma_action action = LZMA_RUN; | |
380 | ||
381 | assert(fdf >= 0); | |
382 | assert(fdt >= 0); | |
383 | ||
d89c8fdf | 384 | ret = lzma_easy_encoder(&s, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64); |
355b59e2 | 385 | if (ret != LZMA_OK) { |
1fa2f38f | 386 | log_error("Failed to initialize XZ encoder: code %u", ret); |
355b59e2 ZJS |
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 | ||
59f448cf LP |
395 | if (max_bytes != (uint64_t) -1 && (uint64_t) m > max_bytes) |
396 | m = (size_t) max_bytes; | |
355b59e2 ZJS |
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 | ||
59f448cf LP |
407 | if (max_bytes != (uint64_t) -1) { |
408 | assert(max_bytes >= (uint64_t) n); | |
355b59e2 ZJS |
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) { | |
1fa2f38f | 421 | log_error("Compression failed: code %u", ret); |
355b59e2 ZJS |
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 | ||
355b59e2 ZJS |
430 | k = loop_write(fdt, out, n, false); |
431 | if (k < 0) | |
432 | return k; | |
355b59e2 ZJS |
433 | |
434 | if (ret == LZMA_STREAM_END) { | |
fa1c4b51 | 435 | log_debug("XZ compression finished (%"PRIu64" -> %"PRIu64" bytes, %.1f%%)", |
355b59e2 ZJS |
436 | s.total_in, s.total_out, |
437 | (double) s.total_out / s.total_in * 100); | |
438 | ||
439 | return 0; | |
440 | } | |
441 | } | |
442 | } | |
3b1a55e1 ZJS |
443 | #else |
444 | return -EPROTONOSUPPORT; | |
445 | #endif | |
355b59e2 ZJS |
446 | } |
447 | ||
4b5bc539 | 448 | #define LZ4_BUFSIZE (512*1024u) |
d89c8fdf | 449 | |
59f448cf | 450 | int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes) { |
d89c8fdf | 451 | |
349cc4a5 | 452 | #if HAVE_LZ4 |
4b5bc539 ZJS |
453 | LZ4F_errorCode_t c; |
454 | _cleanup_(LZ4F_freeCompressionContextp) LZ4F_compressionContext_t ctx = NULL; | |
455 | _cleanup_free_ char *buf = NULL; | |
456 | char *src = NULL; | |
d487b815 | 457 | size_t size, n, total_in = 0, total_out, offset = 0, frame_size; |
4b5bc539 ZJS |
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 | }; | |
d89c8fdf | 466 | |
4b5bc539 ZJS |
467 | c = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION); |
468 | if (LZ4F_isError(c)) | |
469 | return -ENOMEM; | |
d89c8fdf | 470 | |
4b5bc539 | 471 | if (fstat(fdf, &st) < 0) |
5146f9f0 | 472 | return log_debug_errno(errno, "fstat() failed: %m"); |
d89c8fdf | 473 | |
4b5bc539 ZJS |
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; | |
d89c8fdf | 479 | |
d487b815 | 480 | n = offset = total_out = LZ4F_compressBegin(ctx, buf, size, &preferences); |
4b5bc539 ZJS |
481 | if (LZ4F_isError(n)) |
482 | return -EINVAL; | |
d89c8fdf | 483 | |
4b5bc539 | 484 | src = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fdf, 0); |
e0685172 | 485 | if (src == MAP_FAILED) |
4b5bc539 | 486 | return -errno; |
d89c8fdf | 487 | |
4b5bc539 | 488 | log_debug("Buffer size is %zu bytes, header size %zu bytes.", size, n); |
d89c8fdf | 489 | |
4b5bc539 ZJS |
490 | while (total_in < (size_t) st.st_size) { |
491 | ssize_t k; | |
d89c8fdf | 492 | |
4b5bc539 ZJS |
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; | |
d89c8fdf ZJS |
499 | } |
500 | ||
4b5bc539 ZJS |
501 | total_in += k; |
502 | offset += n; | |
503 | total_out += n; | |
d89c8fdf | 504 | |
4b5bc539 | 505 | if (max_bytes != (uint64_t) -1 && total_out > (size_t) max_bytes) { |
82e24b00 | 506 | log_debug("Compressed stream longer than %"PRIu64" bytes", max_bytes); |
4b5bc539 ZJS |
507 | return -EFBIG; |
508 | } | |
d89c8fdf | 509 | |
4b5bc539 | 510 | if (size - offset < frame_size + 4) { |
4b5bc539 ZJS |
511 | k = loop_write(fdt, buf, offset, false); |
512 | if (k < 0) { | |
513 | r = k; | |
514 | goto cleanup; | |
515 | } | |
516 | offset = 0; | |
517 | } | |
518 | } | |
d89c8fdf | 519 | |
4b5bc539 ZJS |
520 | n = LZ4F_compressEnd(ctx, buf + offset, size - offset, &options); |
521 | if (LZ4F_isError(n)) { | |
522 | r = -ENOTRECOVERABLE; | |
523 | goto cleanup; | |
d89c8fdf ZJS |
524 | } |
525 | ||
4b5bc539 ZJS |
526 | offset += n; |
527 | total_out += n; | |
4b5bc539 ZJS |
528 | r = loop_write(fdt, buf, offset, false); |
529 | if (r < 0) | |
530 | goto cleanup; | |
d89c8fdf ZJS |
531 | |
532 | log_debug("LZ4 compression finished (%zu -> %zu bytes, %.1f%%)", | |
533 | total_in, total_out, | |
534 | (double) total_out / total_in * 100); | |
4b5bc539 ZJS |
535 | cleanup: |
536 | munmap(src, st.st_size); | |
537 | return r; | |
d89c8fdf ZJS |
538 | #else |
539 | return -EPROTONOSUPPORT; | |
540 | #endif | |
541 | } | |
542 | ||
59f448cf | 543 | int decompress_stream_xz(int fdf, int fdt, uint64_t max_bytes) { |
d89c8fdf | 544 | |
349cc4a5 | 545 | #if HAVE_XZ |
355b59e2 ZJS |
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) { | |
5146f9f0 | 557 | log_debug("Failed to initialize XZ decoder: code %u", ret); |
d89c8fdf | 558 | return -ENOMEM; |
355b59e2 ZJS |
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) { | |
5146f9f0 | 583 | log_debug("Decompression failed: code %u", ret); |
355b59e2 ZJS |
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 | ||
59f448cf LP |
592 | if (max_bytes != (uint64_t) -1) { |
593 | if (max_bytes < (uint64_t) n) | |
d89c8fdf | 594 | return -EFBIG; |
355b59e2 ZJS |
595 | |
596 | max_bytes -= n; | |
597 | } | |
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 decompression 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 | } | |
d89c8fdf | 612 | #else |
5146f9f0 | 613 | log_debug("Cannot decompress file. Compiled without XZ support."); |
d89c8fdf ZJS |
614 | return -EPROTONOSUPPORT; |
615 | #endif | |
616 | } | |
617 | ||
8e64dd1e | 618 | int decompress_stream_lz4(int in, int out, uint64_t max_bytes) { |
349cc4a5 | 619 | #if HAVE_LZ4 |
4b5bc539 ZJS |
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) | |
5146f9f0 | 633 | return log_debug_errno(errno, "fstat() failed: %m"); |
4b5bc539 ZJS |
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); | |
e0685172 | 640 | if (src == MAP_FAILED) |
4b5bc539 ZJS |
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) { | |
82e24b00 | 657 | log_debug("Decompressed stream longer than %"PRIu64" bytes", max_bytes); |
5146f9f0 | 658 | r = -EFBIG; |
4b5bc539 ZJS |
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; | |
d89c8fdf | 673 | #else |
5146f9f0 | 674 | log_debug("Cannot decompress file. Compiled without LZ4 support."); |
d89c8fdf ZJS |
675 | return -EPROTONOSUPPORT; |
676 | #endif | |
677 | } | |
678 | ||
59f448cf | 679 | int decompress_stream(const char *filename, int fdf, int fdt, uint64_t max_bytes) { |
d89c8fdf ZJS |
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; | |
355b59e2 | 687 | } |