compress.c revision 355b59e252c9910e44a1ad95c045ba8db58a4f6a
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/>.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmekbool compress_blob(const void *src, uint64_t src_size, void *dst, uint64_t *dst_size) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek /* Returns false if we couldn't compress the data or the
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek * compressed result is longer than the original */
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ret = lzma_easy_buffer_encode(LZMA_PRESET_DEFAULT, LZMA_CHECK_NONE, NULL,
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek src, src_size, dst, &out_pos, src_size);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek /* Is it actually shorter? */
722b6795655149a68277b3cffeba711e1d440e5aZbigniew 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;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert(*dst_alloc_size == 0 || *dst);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew 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;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek ret = lzma_code(&s, LZMA_FINISH);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek if (dst_max > 0 && (space - s.avail_out) >= dst_max)
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *dst);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew 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 lzma_stream s = LZMA_STREAM_INIT;
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
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert(*buffer_size == 0 || *buffer);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (*buffer_size <= prefix_len) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek p = realloc(*buffer, prefix_len*2);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ret = lzma_code(&s, LZMA_FINISH);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (ret != LZMA_STREAM_END && ret != LZMA_OK)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if ((*buffer_size - s.avail_out > prefix_len) &&
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek memcmp(*buffer, prefix, prefix_len) == 0 &&
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew 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, 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, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error("Failed to initialize XZ encoder: code %d", ret);
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmek if (s.avail_in == 0 && action == LZMA_RUN) {
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek if (max_bytes != -1 && m > (size_t) max_bytes)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error("Compression 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("Compression finished (%zu -> %zu bytes, %.1f%%)",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek (double) s.total_out / s.total_in * 100);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmekint decompress_stream(int fdf, int fdt, 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_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);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek log_debug("Decompression finished (%zu -> %zu bytes, %.1f%%)",
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek (double) s.total_out / s.total_in * 100);