1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 bool compress_blob(const void *src, uint64_t src_size, void *dst, uint64_t *dst_size) {
31 lzma_stream s = LZMA_STREAM_INIT;
40 /* Returns false if we couldn't compress the data or the
41 * compressed result is longer than the original */
43 ret = lzma_easy_encoder(&s, LZMA_PRESET_DEFAULT, LZMA_CHECK_NONE);
48 s.avail_in = src_size;
50 s.avail_out = src_size;
53 if (lzma_code(&s, LZMA_FINISH) != LZMA_STREAM_END)
56 /* Is it actually shorter? */
60 *dst_size = src_size - s.avail_out;
69 bool uncompress_blob(const void *src, uint64_t src_size,
70 void **dst, uint64_t *dst_alloc_size, uint64_t* dst_size, uint64_t dst_max) {
72 lzma_stream s = LZMA_STREAM_INIT;
80 assert(dst_alloc_size);
82 assert(*dst_alloc_size == 0 || *dst);
84 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
88 if (*dst_alloc_size <= src_size) {
91 p = realloc(*dst, src_size*2);
96 *dst_alloc_size = src_size*2;
100 s.avail_in = src_size;
103 space = dst_max > 0 ? MIN(*dst_alloc_size, dst_max) : *dst_alloc_size;
109 ret = lzma_code(&s, LZMA_FINISH);
111 if (ret == LZMA_STREAM_END)
117 if (dst_max > 0 && (space - s.avail_out) >= dst_max)
120 p = realloc(*dst, space*2);
124 s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *dst);
125 s.avail_out += space;
130 *dst_alloc_size = space;
133 *dst_size = space - s.avail_out;
142 bool uncompress_startswith(const void *src, uint64_t src_size,
143 void **buffer, uint64_t *buffer_size,
144 const void *prefix, uint64_t prefix_len,
147 lzma_stream s = LZMA_STREAM_INIT;
151 /* Checks whether the uncompressed blob starts with the
152 * mentioned prefix. The byte extra needs to follow the
156 assert(src_size > 0);
160 assert(*buffer_size == 0 || *buffer);
162 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
166 if (*buffer_size <= prefix_len) {
169 p = realloc(*buffer, prefix_len*2);
174 *buffer_size = prefix_len*2;
178 s.avail_in = src_size;
180 s.next_out = *buffer;
181 s.avail_out = *buffer_size;
186 ret = lzma_code(&s, LZMA_FINISH);
188 if (ret != LZMA_STREAM_END && ret != LZMA_OK)
191 if ((*buffer_size - s.avail_out > prefix_len) &&
192 memcmp(*buffer, prefix, prefix_len) == 0 &&
193 ((const uint8_t*) *buffer)[prefix_len] == extra)
196 if (ret == LZMA_STREAM_END)
199 p = realloc(*buffer, *buffer_size*2);
203 s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *buffer);
204 s.avail_out += *buffer_size;