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) {
39 /* Returns false if we couldn't compress the data or the
40 * compressed result is longer than the original */
42 ret = lzma_easy_buffer_encode(LZMA_PRESET_DEFAULT, LZMA_CHECK_NONE, NULL,
43 src, src_size, dst, &out_pos, *dst_size);
47 /* Is it actually shorter? */
48 if (out_pos == *dst_size)
55 bool uncompress_blob(const void *src, uint64_t src_size,
56 void **dst, uint64_t *dst_alloc_size, uint64_t* dst_size, uint64_t dst_max) {
58 lzma_stream s = LZMA_STREAM_INIT;
66 assert(dst_alloc_size);
68 assert(*dst_alloc_size == 0 || *dst);
70 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
74 if (*dst_alloc_size <= src_size) {
77 p = realloc(*dst, src_size*2);
82 *dst_alloc_size = src_size*2;
86 s.avail_in = src_size;
89 space = dst_max > 0 ? MIN(*dst_alloc_size, dst_max) : *dst_alloc_size;
95 ret = lzma_code(&s, LZMA_FINISH);
97 if (ret == LZMA_STREAM_END)
103 if (dst_max > 0 && (space - s.avail_out) >= dst_max)
106 p = realloc(*dst, space*2);
110 s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *dst);
111 s.avail_out += space;
116 *dst_alloc_size = space;
119 *dst_size = space - s.avail_out;
128 bool uncompress_startswith(const void *src, uint64_t src_size,
129 void **buffer, uint64_t *buffer_size,
130 const void *prefix, uint64_t prefix_len,
133 lzma_stream s = LZMA_STREAM_INIT;
137 /* Checks whether the uncompressed blob starts with the
138 * mentioned prefix. The byte extra needs to follow the
142 assert(src_size > 0);
146 assert(*buffer_size == 0 || *buffer);
148 ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
152 if (*buffer_size <= prefix_len) {
155 p = realloc(*buffer, prefix_len*2);
160 *buffer_size = prefix_len*2;
164 s.avail_in = src_size;
166 s.next_out = *buffer;
167 s.avail_out = *buffer_size;
172 ret = lzma_code(&s, LZMA_FINISH);
174 if (ret != LZMA_STREAM_END && ret != LZMA_OK)
177 if ((*buffer_size - s.avail_out > prefix_len) &&
178 memcmp(*buffer, prefix, prefix_len) == 0 &&
179 ((const uint8_t*) *buffer)[prefix_len] == extra)
182 if (ret == LZMA_STREAM_END)
185 p = realloc(*buffer, *buffer_size*2);
189 s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *buffer);
190 s.avail_out += *buffer_size;