compress.c revision 347272731e15d3c4a70fad7ccd7185e8e8059d01
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek/***
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek This file is part of systemd.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek Copyright 2011 Lennart Poettering
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
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
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
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-Szmek***/
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <assert.h>
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#include <stdlib.h>
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#include <string.h>
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include <unistd.h>
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include <lzma.h>
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#include "compress.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include "macro.h"
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "util.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
a0f29c767a3bb3d621c658fa5b87063e1f44e24aLennart Poetteringbool compress_blob(const void *src, uint64_t src_size, void *dst, uint64_t *dst_size) {
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering lzma_ret ret;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek size_t out_pos = 0;
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
7d50b32a129e781401cf897475f388f682de1368Lennart Poettering assert(src);
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering assert(src_size > 0);
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering assert(dst);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek assert(dst_size);
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering
2cf4172a71860c6e44edd27a3b68047ae062d7fcLennart Poettering /* Returns false if we couldn't compress the data or the
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering * compressed result is longer than the original */
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
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);
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmek if (ret != LZMA_OK)
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmek return false;
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmek
50a0b0717563d08c027a16a896bff8d7754eab9eZbigniew Jędrzejewski-Szmek /* Is it actually shorter? */
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek if (out_pos == src_size)
2cf4172a71860c6e44edd27a3b68047ae062d7fcLennart Poettering return false;
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek *dst_size = out_pos;
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek return true;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek}
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
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
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek lzma_stream s = LZMA_STREAM_INIT;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek lzma_ret ret;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek uint64_t space;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek bool b = false;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
2cf4172a71860c6e44edd27a3b68047ae062d7fcLennart Poettering assert(src);
2cf4172a71860c6e44edd27a3b68047ae062d7fcLennart Poettering assert(src_size > 0);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert(dst);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert(dst_alloc_size);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert(dst_size);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert(*dst_alloc_size == 0 || *dst);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
8847551bcbfa8265bae04f567bb1aadc7b480325Zbigniew Jędrzejewski-Szmek ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (ret != LZMA_OK)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek return false;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (*dst_alloc_size <= src_size) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek void *p;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek p = realloc(*dst, src_size*2);
8847551bcbfa8265bae04f567bb1aadc7b480325Zbigniew Jędrzejewski-Szmek if (!p)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek return false;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek *dst = p;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek *dst_alloc_size = src_size*2;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek }
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek s.next_in = src;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek s.avail_in = src_size;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek s.next_out = *dst;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek space = dst_max > 0 ? MIN(*dst_alloc_size, dst_max) : *dst_alloc_size;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek s.avail_out = space;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek for (;;) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek void *p;
c33b329709ebe2755181980a050d02ec7c81ed87Michal Schmidt
c33b329709ebe2755181980a050d02ec7c81ed87Michal Schmidt ret = lzma_code(&s, LZMA_FINISH);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek if (ret == LZMA_STREAM_END)
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek break;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek if (ret != LZMA_OK)
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek goto fail;
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek if (dst_max > 0 && (space - s.avail_out) >= dst_max)
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek break;
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek p = realloc(*dst, space*2);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek if (!p)
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek goto fail;
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *dst);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt s.avail_out += space;
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek space *= 2;
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt *dst = p;
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt *dst_alloc_size = space;
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt }
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek *dst_size = space - s.avail_out;
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek b = true;
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmekfail:
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek lzma_end(&s);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek return b;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek}
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
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 uint8_t extra) {
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering lzma_stream s = LZMA_STREAM_INIT;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek lzma_ret ret;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek bool b = false;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
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 * prefix */
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering assert(src);
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering assert(src_size > 0);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert(buffer);
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering assert(buffer_size);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert(prefix);
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering assert(*buffer_size == 0 || *buffer);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering if (ret != LZMA_OK)
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering return false;
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering if (*buffer_size <= prefix_len) {
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering void *p;
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering p = realloc(*buffer, prefix_len*2);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek if (!p)
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering return false;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek *buffer = p;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek *buffer_size = prefix_len*2;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek }
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek s.next_in = src;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek s.avail_in = src_size;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek s.next_out = *buffer;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek s.avail_out = *buffer_size;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek for (;;) {
36d4739a68c3edafe4d145d525a26de4ef0b8e5aZbigniew Jędrzejewski-Szmek void *p;
36d4739a68c3edafe4d145d525a26de4ef0b8e5aZbigniew Jędrzejewski-Szmek
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt ret = lzma_code(&s, LZMA_FINISH);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt if (ret != LZMA_STREAM_END && ret != LZMA_OK)
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt goto fail;
36d4739a68c3edafe4d145d525a26de4ef0b8e5aZbigniew Jędrzejewski-Szmek
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)
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek break;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek if (ret == LZMA_STREAM_END)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek goto fail;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek p = realloc(*buffer, *buffer_size*2);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (!p)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek goto fail;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *buffer);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek s.avail_out += *buffer_size;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek *buffer = p;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek *buffer_size *= 2;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek b = true;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmekfail:
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek lzma_end(&s);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek return b;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek}
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
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 lzma_ret ret;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek uint8_t buf[BUFSIZ], out[BUFSIZ];
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek lzma_action action = LZMA_RUN;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert(fdf >= 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert(fdt >= 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ret = lzma_easy_encoder(&s, preset, LZMA_CHECK_CRC64);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (ret != LZMA_OK) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error("Failed to initialize XZ encoder: code %d", ret);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek return -EINVAL;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek for (;;) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (s.avail_in == 0 && action == LZMA_RUN) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek size_t m = sizeof(buf);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ssize_t n;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (max_bytes != -1 && m > (size_t) max_bytes)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek m = max_bytes;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek n = read(fdf, buf, m);
b88a40a7e592e0a4a2e6e8eb1ed3721989ba5d0ePhilippe De Swert if (n < 0)
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek return -errno;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek if (n == 0)
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek action = LZMA_FINISH;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek else {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek s.next_in = buf;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek s.avail_in = n;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek if (max_bytes != -1) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert(max_bytes >= n);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek max_bytes -= n;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (s.avail_out == 0) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek s.next_out = out;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek s.avail_out = sizeof(out);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ret = lzma_code(&s, action);
5dabb1e04e4521abf040e77a8322b70e7acdc7b0Zbigniew Jędrzejewski-Szmek if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
5dabb1e04e4521abf040e77a8322b70e7acdc7b0Zbigniew Jędrzejewski-Szmek log_error("Compression failed: code %d", ret);
5dabb1e04e4521abf040e77a8322b70e7acdc7b0Zbigniew Jędrzejewski-Szmek return -EBADMSG;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ssize_t n, k;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek n = sizeof(out) - s.avail_out;
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmek
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek errno = 0;
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek k = loop_write(fdt, out, n, false);
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek if (k < 0)
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek return k;
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek if (k != n)
8847551bcbfa8265bae04f567bb1aadc7b480325Zbigniew Jędrzejewski-Szmek return errno ? -errno : -EIO;
8847551bcbfa8265bae04f567bb1aadc7b480325Zbigniew Jędrzejewski-Szmek
8847551bcbfa8265bae04f567bb1aadc7b480325Zbigniew Jędrzejewski-Szmek if (ret == LZMA_STREAM_END) {
8847551bcbfa8265bae04f567bb1aadc7b480325Zbigniew Jędrzejewski-Szmek log_debug("Compression finished (%zu -> %zu bytes, %.1f%%)",
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek s.total_in, s.total_out,
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek (double) s.total_out / s.total_in * 100);
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek return 0;
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek }
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek }
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek}
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
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 lzma_ret ret;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek uint8_t buf[BUFSIZ], out[BUFSIZ];
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek lzma_action action = LZMA_RUN;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert(fdf >= 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert(fdt >= 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (ret != LZMA_OK) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_error("Failed to initialize XZ decoder: code %d", ret);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek return -EINVAL;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek for (;;) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (s.avail_in == 0 && action == LZMA_RUN) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ssize_t n;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek n = read(fdf, buf, sizeof(buf));
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (n < 0)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek return -errno;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (n == 0)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek action = LZMA_FINISH;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek else {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek s.next_in = buf;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek s.avail_in = n;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (s.avail_out == 0) {
1fa2f38f0f011010bf57522b42fcc168856a7003Zbigniew Jędrzejewski-Szmek s.next_out = out;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek s.avail_out = sizeof(out);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek ret = lzma_code(&s, action);
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 return -EBADMSG;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (s.avail_out == 0 || ret == LZMA_STREAM_END) {
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt ssize_t n, k;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek n = sizeof(out) - s.avail_out;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (max_bytes != -1) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (max_bytes < n)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek return -E2BIG;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek max_bytes -= n;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek }
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek errno = 0;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek k = loop_write(fdt, out, n, false);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (k < 0)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek return k;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (k != n)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek return errno ? -errno : -EIO;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek if (ret == LZMA_STREAM_END) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_debug("Decompression finished (%zu -> %zu bytes, %.1f%%)",
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek s.total_in, s.total_out,
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek (double) s.total_out / s.total_in * 100);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
8201af08fa09c2bd0f005fbe262f27e2c5bd2d86Zbigniew Jędrzejewski-Szmek return 0;
8201af08fa09c2bd0f005fbe262f27e2c5bd2d86Zbigniew Jędrzejewski-Szmek }
8201af08fa09c2bd0f005fbe262f27e2c5bd2d86Zbigniew Jędrzejewski-Szmek }
8201af08fa09c2bd0f005fbe262f27e2c5bd2d86Zbigniew Jędrzejewski-Szmek }
8201af08fa09c2bd0f005fbe262f27e2c5bd2d86Zbigniew Jędrzejewski-Szmek}
8201af08fa09c2bd0f005fbe262f27e2c5bd2d86Zbigniew Jędrzejewski-Szmek