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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 bool compress_blob(const void *src, uint64_t src_size, void *dst, uint64_t *dst_size) {
30 lzma_stream s = LZMA_STREAM_INIT;
39 /* Returns false if we couldn't compress the data or the
40 * compressed result is longer than the original */
42 ret = lzma_easy_encoder(&s, LZMA_PRESET_DEFAULT, LZMA_CHECK_NONE);
47 s.avail_in = src_size;
49 s.avail_out = src_size;
52 if (lzma_code(&s, LZMA_FINISH) != LZMA_STREAM_END)
55 /* Is it actually shorter? */
59 *dst_size = src_size - s.avail_out;
68 bool uncompress_blob(const void *src, uint64_t src_size,
69 void **dst, uint64_t *dst_alloc_size, uint64_t* dst_size) {
71 lzma_stream s = LZMA_STREAM_INIT;
78 assert(dst_alloc_size);
80 assert(*dst_alloc_size == 0 || *dst);
82 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
86 if (*dst_alloc_size <= src_size) {
89 p = realloc(*dst, src_size*2);
94 *dst_alloc_size = src_size*2;
98 s.avail_in = src_size;
101 s.avail_out = *dst_alloc_size;
106 ret = lzma_code(&s, LZMA_FINISH);
108 if (ret == LZMA_STREAM_END)
114 p = realloc(*dst, *dst_alloc_size*2);
118 s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *dst);
119 s.avail_out += *dst_alloc_size;
122 *dst_alloc_size *= 2;
125 *dst_size = *dst_alloc_size - s.avail_out;
134 bool uncompress_startswith(const void *src, uint64_t src_size,
135 void **buffer, uint64_t *buffer_size,
136 const void *prefix, uint64_t prefix_len,
139 lzma_stream s = LZMA_STREAM_INIT;
143 /* Checks whether the uncompressed blob starts with the
144 * mentioned prefix. The byte extra needs to follow the
148 assert(src_size > 0);
152 assert(*buffer_size == 0 || *buffer);
154 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
158 if (*buffer_size <= prefix_len) {
161 p = realloc(*buffer, prefix_len*2);
166 *buffer_size = prefix_len*2;
170 s.avail_in = src_size;
172 s.next_out = *buffer;
173 s.avail_out = *buffer_size;
178 ret = lzma_code(&s, LZMA_FINISH);
180 if (ret != LZMA_STREAM_END && ret != LZMA_OK)
183 if ((*buffer_size - s.avail_out > prefix_len) &&
184 memcmp(*buffer, prefix, prefix_len) == 0 &&
185 ((const uint8_t*) *buffer)[prefix_len] == extra)
188 if (ret == LZMA_STREAM_END)
191 p = realloc(*buffer, *buffer_size*2);
195 s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *buffer);
196 s.avail_out += *buffer_size;