Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ Lesser General Public License for more details.
- You should have received a copy of the GNU General Public License
+ You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <string.h>
#include <lzma.h>
+#include "macro.h"
#include "compress.h"
bool compress_blob(const void *src, uint64_t src_size, void *dst, uint64_t *dst_size) {
}
bool uncompress_blob(const void *src, uint64_t src_size,
- void **dst, uint64_t *dst_alloc_size, uint64_t* dst_size) {
+ void **dst, uint64_t *dst_alloc_size, uint64_t* dst_size, uint64_t dst_max) {
lzma_stream s = LZMA_STREAM_INIT;
lzma_ret ret;
+ uint64_t space;
bool b = false;
assert(src);
s.avail_in = src_size;
s.next_out = *dst;
- s.avail_out = *dst_alloc_size;
+ space = dst_max > 0 ? MIN(*dst_alloc_size, dst_max) : *dst_alloc_size;
+ s.avail_out = space;
for (;;) {
void *p;
if (ret != LZMA_OK)
goto fail;
- p = realloc(*dst, *dst_alloc_size*2);
+ if (dst_max > 0 && (space - s.avail_out) >= dst_max)
+ break;
+
+ p = realloc(*dst, space*2);
if (!p)
goto fail;
s.next_out = (uint8_t*) p + ((uint8_t*) s.next_out - (uint8_t*) *dst);
- s.avail_out += *dst_alloc_size;
+ s.avail_out += space;
+
+ space *= 2;
*dst = p;
- *dst_alloc_size *= 2;
+ *dst_alloc_size = space;
}
- *dst_size = *dst_alloc_size - s.avail_out;
+ *dst_size = space - s.avail_out;
b = true;
fail: