compress.c revision 347272731e15d3c4a70fad7ccd7185e8e8059d01
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek This file is part of systemd.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek Copyright 2011 Lennart Poettering
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek (at your option) any later version.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
a0f29c767a3bb3d621c658fa5b87063e1f44e24aLennart Poetteringbool compress_blob(const void *src, uint64_t src_size, void *dst, uint64_t *dst_size) {
2cf4172a71860c6e44edd27a3b68047ae062d7fcLennart Poettering /* Returns false if we couldn't compress the data or the
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering * compressed result is longer than the original */
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering ret = lzma_easy_buffer_encode(LZMA_PRESET_DEFAULT, LZMA_CHECK_NONE, NULL,
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek src, src_size, dst, &out_pos, src_size);
50a0b0717563d08c027a16a896bff8d7754eab9eZbigniew Jędrzejewski-Szmek /* Is it actually shorter? */
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmekbool uncompress_blob(const void *src, uint64_t src_size,
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek void **dst, uint64_t *dst_alloc_size, uint64_t* dst_size, uint64_t dst_max) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek lzma_stream s = LZMA_STREAM_INIT;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert(*dst_alloc_size == 0 || *dst);
8847551bcbfa8265bae04f567bb1aadc7b480325Zbigniew Jędrzejewski-Szmek ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (*dst_alloc_size <= src_size) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek space = dst_max > 0 ? MIN(*dst_alloc_size, dst_max) : *dst_alloc_size;
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek if (dst_max > 0 && (space - s.avail_out) >= dst_max)
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *dst);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek *dst_size = space - s.avail_out;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmekbool uncompress_startswith(const void *src, uint64_t src_size,
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek void **buffer, uint64_t *buffer_size,
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek const void *prefix, uint64_t prefix_len,
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek /* Checks whether the uncompressed blob starts with the
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek * mentioned prefix. The byte extra needs to follow the
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt if (ret != LZMA_STREAM_END && ret != LZMA_OK)
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek if ((*buffer_size - s.avail_out > prefix_len) &&
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek memcmp(*buffer, prefix, prefix_len) == 0 &&
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek ((const uint8_t*) *buffer)[prefix_len] == extra)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek p = realloc(*buffer, *buffer_size*2);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *buffer);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmekint compress_stream(int fdf, int fdt, uint32_t preset, off_t max_bytes) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek uint8_t buf[BUFSIZ], out[BUFSIZ];
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ret = lzma_easy_encoder(&s, preset, LZMA_CHECK_CRC64);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error("Failed to initialize XZ encoder: code %d", ret);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (s.avail_in == 0 && action == LZMA_RUN) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (max_bytes != -1 && m > (size_t) max_bytes)
5dabb1e04e4521abf040e77a8322b70e7acdc7b0Zbigniew Jędrzejewski-Szmek if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
5dabb1e04e4521abf040e77a8322b70e7acdc7b0Zbigniew Jędrzejewski-Szmek log_error("Compression failed: code %d", ret);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek k = loop_write(fdt, out, n, false);
8847551bcbfa8265bae04f567bb1aadc7b480325Zbigniew Jędrzejewski-Szmek log_debug("Compression finished (%zu -> %zu bytes, %.1f%%)",
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek (double) s.total_out / s.total_in * 100);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmekint decompress_stream(int fdf, int fdt, off_t max_bytes) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek uint8_t buf[BUFSIZ], out[BUFSIZ];
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error("Failed to initialize XZ decoder: code %d", ret);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (s.avail_in == 0 && action == LZMA_RUN) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek n = read(fdf, buf, sizeof(buf));
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error("Decompression failed: code %d", ret);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek k = loop_write(fdt, out, n, false);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_debug("Decompression finished (%zu -> %zu bytes, %.1f%%)",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek (double) s.total_out / s.total_in * 100);