compress.c revision d89c8fdf48c7bad5816b9f2e77e8361721f22517
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
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
Lesser General Public License for more details.
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 <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_XZ
# include <lzma.h>
#endif
#ifdef HAVE_LZ4
# include <lz4.h>
#endif
#include "compress.h"
#include "macro.h"
#include "util.h"
#include "sparse-endian.h"
#include "journal-def.h"
static const char* const object_compressed_table[_OBJECT_COMPRESSED_MAX] = {
[OBJECT_COMPRESSED_XZ] = "XZ",
[OBJECT_COMPRESSED_LZ4] = "LZ4",
};
#ifdef HAVE_XZ
/* Returns < 0 if we couldn't compress the data or the
* compressed result is longer than the original */
return -ENOBUFS;
return 0;
#else
return -EPROTONOSUPPORT;
#endif
}
#ifdef HAVE_LZ4
int r;
/* Returns < 0 if we couldn't compress the data or the
* compressed result is longer than the original */
if (src_size < 9)
return -ENOBUFS;
if (r <= 0)
return -ENOBUFS;
*dst_size = r + 8;
return 0;
#else
return -EPROTONOSUPPORT;
#endif
}
#ifdef HAVE_XZ
return -ENOMEM;
return false;
for (;;) {
if (ret == LZMA_STREAM_END)
break;
return -ENOMEM;
break;
return -ENOBUFS;
return false;
}
return 0;
#else
return -EPROTONOSUPPORT;
#endif
}
#ifdef HAVE_LZ4
char* out;
int r;
if (src_size <= 8)
return -EBADMSG;
if (size > *dst_alloc_size) {
if (!out)
return -ENOMEM;
*dst_alloc_size = size;
} else
return -EBADMSG;
return 0;
#else
return -EPROTONOSUPPORT;
#endif
}
int decompress_blob(int compression,
if (compression == OBJECT_COMPRESSED_XZ)
else if (compression == OBJECT_COMPRESSED_LZ4)
else
return -EBADMSG;
}
#ifdef HAVE_XZ
/* Checks whether the decompressed blob starts with the
* mentioned prefix. The byte extra needs to follow the
* prefix */
return -EBADMSG;
return -ENOMEM;
s.avail_out = *buffer_size;
for (;;) {
return -EBADMSG;
if (ret == LZMA_STREAM_END)
return 0;
s.avail_out += *buffer_size;
return -ENOMEM;
}
#else
return -EPROTONOSUPPORT;
#endif
}
#ifdef HAVE_LZ4
/* Checks whether the decompressed blob starts with the
* mentioned prefix. The byte extra needs to follow the
* prefix */
int r;
if (src_size <= 8)
return -EBADMSG;
return -ENOMEM;
if (r < 0)
return -EBADMSG;
if ((unsigned) r >= prefix_len + 1)
else
return 0;
#else
return -EPROTONOSUPPORT;
#endif
}
int decompress_startswith(int compression,
if (compression == OBJECT_COMPRESSED_XZ)
extra);
else if (compression == OBJECT_COMPRESSED_LZ4)
extra);
else
return -EBADMSG;
}
return -EINVAL;
}
for (;;) {
ssize_t n;
m = max_bytes;
if (n < 0)
return -errno;
if (n == 0)
else {
s.avail_in = n;
if (max_bytes != -1) {
max_bytes -= n;
}
}
}
if (s.avail_out == 0) {
}
return -EBADMSG;
}
ssize_t n, k;
errno = 0;
if (k < 0)
return k;
if (k != n)
if (ret == LZMA_STREAM_END) {
log_debug("XZ compression finished (%zu -> %zu bytes, %.1f%%)",
return 0;
}
}
}
}
#ifdef HAVE_LZ4
char *buf;
LZ4_stream_t lz4_data = {};
ssize_t n;
return log_oom();
for (;;) {
size_t m;
int r;
m = LZ4_BUFSIZE;
if (n < 0)
return -errno;
if (n == 0)
break;
total_in += n;
if (r == 0) {
log_debug("Compressed size exceeds original, aborting compression.");
return -ENOBUFS;
}
errno = 0;
if (n < 0)
return -errno;
if (n != sizeof(header))
if (n < 0)
return n;
if (n != r)
}
if (n < 0)
return -errno;
if (n != sizeof(header))
log_debug("LZ4 compression finished (%zu -> %zu bytes, %.1f%%)",
return 0;
#else
return -EPROTONOSUPPORT;
#endif
}
#ifdef HAVE_XZ
return -ENOMEM;
}
for (;;) {
ssize_t n;
if (n < 0)
return -errno;
if (n == 0)
else {
s.avail_in = n;
}
}
if (s.avail_out == 0) {
}
return -EBADMSG;
}
ssize_t n, k;
if (max_bytes != -1) {
if (max_bytes < n)
return -EFBIG;
max_bytes -= n;
}
errno = 0;
if (k < 0)
return k;
if (k != n)
if (ret == LZMA_STREAM_END) {
log_debug("XZ decompression finished (%zu -> %zu bytes, %.1f%%)",
return 0;
}
}
}
#else
log_error("Cannot decompress file. Compiled without XZ support.");
return -EPROTONOSUPPORT;
#endif
}
#ifdef HAVE_LZ4
LZ4_streamDecode_t lz4_data = {};
if (!out)
return log_oom();
for (;;) {
ssize_t n, m;
int r;
if (n < 0)
return -errno;
if (n != sizeof(header))
if (m == 0)
break;
/* We refuse to use a bigger decompression buffer than
* the one used for compression by 4 times. This means
* that compression buffer size can be enlarged 4
* times. This can be changed, but old binaries might
* not accept buffers compressed by newer binaries then.
*/
log_error("Compressed stream block too big: %zd bytes", m);
return -EBADMSG;
}
return log_oom();
errno = 0;
if (n < 0)
return n;
if (n != m)
if (r <= 0)
log_error("LZ4 decompression failed.");
total_out += r;
return -EFBIG;
}
errno = 0;
if (n < 0)
return n;
if (n != r)
}
log_debug("LZ4 decompression finished (%zu -> %zu bytes, %.1f%%)",
return 0;
#else
log_error("Cannot decompress file. Compiled without LZ4 support.");
return -EPROTONOSUPPORT;
#endif
}
else
return -EPROTONOSUPPORT;
}