compress.c revision 5d6f46b6bf7804af8c1ba780f72a37dc37193428
/*-*- 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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_XZ
#include <lzma.h>
#endif
#ifdef HAVE_LZ4
#include <lz4.h>
#include <lz4frame.h>
#endif
#include "alloc-util.h"
#include "compress.h"
#include "fd-util.h"
#include "io-util.h"
#include "journal-def.h"
#include "macro.h"
#include "sparse-endian.h"
#include "string-table.h"
#include "string-util.h"
#include "util.h"
#ifdef HAVE_LZ4
#endif
static const char* const object_compressed_table[_OBJECT_COMPRESSED_MAX] = {
[OBJECT_COMPRESSED_XZ] = "XZ",
[OBJECT_COMPRESSED_LZ4] = "LZ4",
};
#ifdef HAVE_XZ
static const lzma_options_lzma opt = {
};
static const lzma_filter filters[] = {
{ LZMA_VLI_UNKNOWN, NULL }
};
assert(dst_alloc_size > 0);
/* Returns < 0 if we couldn't compress the data or the
* compressed result is longer than the original */
if (src_size < 80)
return -ENOBUFS;
return -ENOBUFS;
return 0;
#else
return -EPROTONOSUPPORT;
#endif
}
#ifdef HAVE_LZ4
int r;
assert(dst_alloc_size > 0);
/* 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 -ENOMEM;
for (;;) {
if (ret == LZMA_STREAM_END)
break;
return -ENOMEM;
break;
return -ENOBUFS;
return -ENOMEM;
}
return 0;
#else
return -EPROTONOSUPPORT;
#endif
}
#ifdef HAVE_LZ4
char* out;
int r, size; /* LZ4 uses int for size */
if (src_size <= 8)
return -EBADMSG;
return -EFBIG;
if (!out)
return -ENOMEM;
*dst_alloc_size = size;
} else
if (r < 0 || r != size)
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)
size = (unsigned) r;
else {
/* lz4 always tries to decode full "sequence", so in
* pathological cases might need to decompress the
* full field. */
if (r < 0)
return r;
}
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;
}
#ifdef HAVE_XZ
return -EINVAL;
}
for (;;) {
ssize_t n;
if (n < 0)
return -errno;
if (n == 0)
else {
s.avail_in = n;
max_bytes -= n;
}
}
}
if (s.avail_out == 0) {
}
return -EBADMSG;
}
ssize_t n, k;
if (k < 0)
return k;
if (ret == LZMA_STREAM_END) {
return 0;
}
}
}
#else
return -EPROTONOSUPPORT;
#endif
}
#ifdef HAVE_LZ4
int r;
static const LZ4F_compressOptions_t options = {
.stableSrc = 1,
};
static const LZ4F_preferences_t preferences = {
};
if (LZ4F_isError(c))
return -ENOMEM;
if (!buf)
return -ENOMEM;
if (LZ4F_isError(n))
return -EINVAL;
if (src == MAP_FAILED)
return -errno;
ssize_t k;
if (LZ4F_isError(n)) {
r = -ENOTRECOVERABLE;
goto cleanup;
}
total_in += k;
offset += n;
total_out += n;
return -EFBIG;
}
if (k < 0) {
r = k;
goto cleanup;
}
offset = 0;
}
}
if (LZ4F_isError(n)) {
r = -ENOTRECOVERABLE;
goto cleanup;
}
offset += n;
total_out += n;
if (r < 0)
goto cleanup;
log_debug("LZ4 compression finished (%zu -> %zu bytes, %.1f%%)",
return r;
#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;
return -EFBIG;
max_bytes -= n;
}
if (k < 0)
return k;
if (ret == LZMA_STREAM_END) {
return 0;
}
}
}
#else
log_debug("Cannot decompress file. Compiled without XZ support.");
return -EPROTONOSUPPORT;
#endif
}
#ifdef HAVE_LZ4
LZ4_streamDecode_t lz4_data = {};
if (!out)
return -ENOMEM;
for (;;) {
ssize_t m;
int r;
if (r < 0)
return r;
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_debug("Compressed stream block too big: %zd bytes", m);
return -ENOBUFS;
}
return -ENOMEM;
if (r < 0)
return r;
if (r <= 0) {
log_debug("LZ4 decompression failed (legacy format).");
return -EBADMSG;
}
total_out += r;
return -EFBIG;
}
if (r < 0)
return r;
}
log_debug("LZ4 decompression finished (legacy format, %zu -> %zu bytes, %.1f%%)",
return 0;
}
size_t c;
char *src;
int r = 0;
if (LZ4F_isError(c))
return -ENOMEM;
if (!buf)
return -ENOMEM;
if (src == MAP_FAILED)
return -errno;
if (LZ4F_isError(c)) {
r = -EBADMSG;
goto cleanup;
}
r = -EFBIG;
goto cleanup;
}
if (r < 0)
goto cleanup;
}
log_debug("LZ4 decompression finished (%zu -> %zu bytes, %.1f%%)",
return r;
}
#endif
#ifdef HAVE_LZ4
int r;
if (r == -EBADMSG)
return r;
#else
log_debug("Cannot decompress file. Compiled without LZ4 support.");
return -EPROTONOSUPPORT;
#endif
}
else
return -EPROTONOSUPPORT;
}