diff --git a/libknet/Makefile.am b/libknet/Makefile.am index 747af0bc..31ffb0c3 100644 --- a/libknet/Makefile.am +++ b/libknet/Makefile.am @@ -1,124 +1,128 @@ # # Copyright (C) 2010-2017 Red Hat, Inc. All rights reserved. # # Authors: Fabio M. Di Nitto # Federico Simoncelli # # This software licensed under GPL-2.0+, LGPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk SYMFILE = libknet_exported_syms EXTRA_DIST = $(SYMFILE) SUBDIRS = . tests man libversion = 1:0:0 # override global LIBS that pulls in lots of craft we don't need here LIBS = sources = \ common.c \ compat.c \ compress.c \ compress_zlib.c \ compress_lz4.c \ compress_lzo2.c \ compress_lzma.c \ compress_bzip2.c \ crypto.c \ crypto_nss.c \ crypto_openssl.c \ handle.c \ host.c \ links.c \ logging.c \ netutils.c \ threads_common.c \ threads_dsthandler.c \ threads_heartbeat.c \ threads_pmtud.c \ threads_rx.c \ threads_tx.c \ transport_udp.c \ transport_sctp.c \ transport_loopback.c \ transport_common.c include_HEADERS = libknet.h pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libknet.pc noinst_HEADERS = \ common.h \ compat.h \ compress.h \ + compress_bzip2.h \ + compress_bzip2_remap.h \ compress_lz4.h \ - compress_lzo2.h \ + compress_lz4_remap.h \ compress_lzma.h \ - compress_bzip2.h \ + compress_lzma_remap.h \ + compress_lzo2.h \ + compress_lzo2_remap.h \ compress_zlib.h \ compress_zlib_remap.h \ crypto.h \ crypto_nss.h \ crypto_openssl.h \ host.h \ internals.h \ links.h \ logging.h \ netutils.h \ onwire.h \ remap.h \ threads_common.h \ threads_dsthandler.h \ threads_heartbeat.h \ threads_pmtud.h \ threads_rx.h \ threads_tx.h \ transports.h lib_LTLIBRARIES = libknet.la libknet_la_SOURCES = $(sources) libknet_la_CFLAGS = $(nss_CFLAGS) $(openssl_CFLAGS) \ $(zlib_CFLAGS) $(liblz4_CFLAGS) $(lzo2_CFLAGS) $(liblzma_CFLAGS) $(bzip2_CFLAGS) EXTRA_libknet_la_DEPENDENCIES = $(SYMFILE) libknet_la_LDFLAGS = -Wl,--version-script=$(srcdir)/$(SYMFILE) \ --export-dynamic \ -version-number $(libversion) libknet_la_LIBADD = $(dl_LIBS) $(pthread_LIBS) $(rt_LIBS) $(m_LIBS) noinst_PROGRAMS = crypto_canary compress_canary crypto_canary_SOURCES = crypto_canary.c crypto_canary_CFLAGS = $(nss_CFLAGS) $(openssl_CFLAGS) crypto_canary_LDADD = $(nss_LIBS) $(openssl_LIBS) compress_canary_SOURCES = compress_canary.c compress_canary_CFLAGS = $(zlib_CFLAGS) $(liblz4_CFLAGS) $(lzo2_CFLAGS) $(liblzma_CFLAGS) $(bzip2_CFLAGS) compress_canary_LDADD = $(zlib_LIBS) $(liblz4_LIBS) $(lzo2_LIBS) $(liblzma_LIBS) $(bzip2_LIBS) dist_man_MANS = man update-man-pages: doxyfile.stamp doxyfile.stamp: Doxyfile if MANPAGEUPDATES $(DOXYGEN) Doxyfile $(DOXY2MAN) -o $(abs_srcdir)/man -s 3 --short-pkg @PACKAGE_NAME@ --pkg "Kronosnet Programmer's Manual" $(builddir)/xml/libknet_8h.xml else @echo this system does not have doxy2man or doxygen installed. Unable to update man pages automatically. endif touch doxyfile.stamp clean-local: rm -rf doxyfile.stamp xml diff --git a/libknet/compress_bzip2.c b/libknet/compress_bzip2.c index f65f7e40..b276530a 100644 --- a/libknet/compress_bzip2.c +++ b/libknet/compress_bzip2.c @@ -1,190 +1,167 @@ /* * Copyright (C) 2017 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #include "config.h" #include #include #include #include #ifdef BUILDCOMPBZIP2 #include #include "internals.h" #include "compress_bzip2.h" #include "logging.h" #include "common.h" #define LIBBZ2_1 "libbz2.so.1" /* * global vars for dlopen */ static void *bzip2_lib; -/* - * symbols remapping - */ -int (*_int_BZ2_bzBuffToBuffCompress)(char* dest, unsigned int* destLen, - char* source, unsigned int sourceLen, - int blockSize100k, int verbosity, - int workFactor); -int (*_int_BZ2_bzBuffToBuffDecompress)(char* dest, unsigned int* destLen, - char* source, unsigned int sourceLen, - int samll, int verbosity); +#include "compress_bzip2_remap.h" static int bzip2_remap_symbols(knet_handle_t knet_h) { - int err = 0; - char *error = NULL; - - _int_BZ2_bzBuffToBuffCompress = dlsym(bzip2_lib, "BZ2_bzBuffToBuffCompress"); - if (!_int_BZ2_bzBuffToBuffCompress) { - error = dlerror(); - log_err(knet_h, KNET_SUB_BZIP2COMP, "unable to map BZ2_bzBuffToBuffCompress: %s", error); - err = -1; - goto out; - } +#define REMAP_WITH(name) remap_symbol (knet_h, KNET_SUB_BZIP2COMP, bzip2_lib, name) +#include "compress_bzip2_remap.h" + return 0; - _int_BZ2_bzBuffToBuffDecompress = dlsym(bzip2_lib, "BZ2_bzBuffToBuffDecompress"); - if (!_int_BZ2_bzBuffToBuffDecompress) { - error = dlerror(); - log_err(knet_h, KNET_SUB_BZIP2COMP, "unable to map BZ2_bzBuffToBuffDecompress: %s", error); - err = -1; - goto out; - } -out: - if (err) { - errno = EINVAL; - } - return err; + fail: +#define REMAP_FAIL +#include "compress_bzip2_remap.h" + errno = EINVAL; + return -1; } int bzip2_load_lib( knet_handle_t knet_h) { int err = 0, savederrno = 0; if (!bzip2_lib) { bzip2_lib = open_lib(knet_h, LIBBZ2_1, 0); if (!bzip2_lib) { savederrno = errno; err = -1; goto out; } } if (bzip2_remap_symbols(knet_h) < 0) { savederrno = errno; err = -1; } out: errno = savederrno; return err; } int bzip2_val_level( knet_handle_t knet_h, int compress_level) { if ((compress_level < 1) || (compress_level > 9)) { log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 unsupported compression level %d (accepted values from 1 to 9)", compress_level); errno = EINVAL; return -1; } return 0; } int bzip2_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int err = 0; int savederrno = 0; unsigned int destLen = KNET_DATABUFSIZE_COMPRESS; err = (*_int_BZ2_bzBuffToBuffCompress)((char *)buf_out, &destLen, (char *)buf_in, buf_in_len, knet_h->compress_level, 0, 0); switch(err) { case BZ_OK: *buf_out_len = destLen; break; case BZ_MEM_ERROR: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 compress has not enough memory"); savederrno = ENOMEM; err = -1; break; case BZ_OUTBUFF_FULL: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 unable to compress source in destination buffer"); savederrno = E2BIG; err = -1; break; default: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 compress unknown error %d", err); savederrno = EINVAL; err = -1; break; } errno = savederrno; return err; } int bzip2_decompress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int err = 0; int savederrno = 0; unsigned int destLen = KNET_DATABUFSIZE_COMPRESS; err = (*_int_BZ2_bzBuffToBuffDecompress)((char *)buf_out, &destLen, (char *)buf_in, buf_in_len, 0, 0); switch(err) { case BZ_OK: *buf_out_len = destLen; break; case BZ_MEM_ERROR: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 decompress has not enough memory"); savederrno = ENOMEM; err = -1; break; case BZ_OUTBUFF_FULL: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 unable to decompress source in destination buffer"); savederrno = E2BIG; err = -1; break; case BZ_DATA_ERROR: case BZ_DATA_ERROR_MAGIC: case BZ_UNEXPECTED_EOF: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 decompress detected input data corruption"); savederrno = EINVAL; err = -1; break; default: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 decompress unknown error %d", err); savederrno = EINVAL; err = -1; break; } errno = savederrno; return err; } #endif diff --git a/libknet/compress_bzip2_remap.h b/libknet/compress_bzip2_remap.h new file mode 100644 index 00000000..917e7ee1 --- /dev/null +++ b/libknet/compress_bzip2_remap.h @@ -0,0 +1,11 @@ +#include "remap.h" + +REMAP_PROTO(int,BZ2_bzBuffToBuffCompress, + (char* dest, unsigned int* destLen, + char* source, unsigned int sourceLen, + int blockSize100k, int verbosity, + int workFactor)) +REMAP_PROTO(int,BZ2_bzBuffToBuffDecompress, + (char* dest, unsigned int* destLen, + char* source, unsigned int sourceLen, + int samll, int verbosity)) diff --git a/libknet/compress_canary.c b/libknet/compress_canary.c index a9088c61..fe010fa2 100644 --- a/libknet/compress_canary.c +++ b/libknet/compress_canary.c @@ -1,36 +1,36 @@ /* Transform the binary into dependencies like: * dpkg-shlibdeps -pcompress -dSuggests -xlibc6 -elibknet/compress_canary */ #include "config.h" #define CANARY -char BZ2_bzBuffToBuffCompress(void); -char LZ4_compress_HC(void); -char lzma_easy_buffer_encode(void); -char lzo1x_1_compress(void); +#include "compress_bzip2_remap.h" +#include "compress_lz4_remap.h" +#include "compress_lzma_remap.h" +#include "compress_lzo2_remap.h" #include "compress_zlib_remap.h" #define CANARY_CALL int main (void) { return #ifdef BUILDCOMPBZIP2 - BZ2_bzBuffToBuffCompress() + +#include "compress_bzip2_remap.h" #endif #ifdef BUILDCOMPLZ4 - LZ4_compress_HC() + +#include "compress_lz4_remap.h" #endif #ifdef BUILDCOMPLZMA - lzma_easy_buffer_encode() + +#include "compress_lzma_remap.h" #endif #ifdef BUILDCOMPLZO2 - lzo1x_1_compress() + +#include "compress_lzo2_remap.h" #endif #ifdef BUILDCOMPZLIB #include "compress_zlib_remap.h" #endif 0; } diff --git a/libknet/compress_lz4.c b/libknet/compress_lz4.c index f9da3553..e491ee12 100644 --- a/libknet/compress_lz4.c +++ b/libknet/compress_lz4.c @@ -1,243 +1,207 @@ /* * Copyright (C) 2017 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #include "config.h" #include #include #include #include #ifdef BUILDCOMPLZ4 #include #include "internals.h" #include "compress_lz4.h" #include "logging.h" #include "common.h" #define LIBLZ4_1 "liblz4.so.1" /* * global vars for dlopen */ static void *lz4_lib; -/* - * symbols remapping - */ -int (*_int_LZ4_compress_HC)(const char* src, char* dst, - int srcSize, int dstCapacity, - int compressionLevel); -int (*_int_LZ4_compress_fast)(const char* source, char* dest, - int sourceSize, int maxDestSize, - int acceleration); -int (*_int_LZ4_decompress_safe)(const char* source, char* dest, - int compressedSize, int maxDecompressedSize); +#include "compress_lz4_remap.h" static int lz4_remap_symbols(knet_handle_t knet_h) { - int err = 0; - char *error = NULL; - - _int_LZ4_compress_HC = dlsym(lz4_lib, "LZ4_compress_HC"); - if (!_int_LZ4_compress_HC) { - error = dlerror(); - log_err(knet_h, KNET_SUB_LZ4COMP, "unable to map LZ4_compress_HC: %s", error); - err = -1; - goto out; - } - - _int_LZ4_compress_fast = dlsym(lz4_lib, "LZ4_compress_fast"); - if (!_int_LZ4_compress_fast) { - error = dlerror(); - log_err(knet_h, KNET_SUB_LZ4COMP, "unable to map LZ4_compress_fast: %s", error); - err = -1; - goto out; - } - - _int_LZ4_decompress_safe = dlsym(lz4_lib, "LZ4_decompress_safe"); - if (!_int_LZ4_decompress_safe) { - error = dlerror(); - log_err(knet_h, KNET_SUB_LZ4COMP, "unable to map LZ4_decompress_safe: %s", error); - err = -1; - goto out; - } +#define REMAP_WITH(name) remap_symbol (knet_h, KNET_SUB_LZ4COMP, lz4_lib, name) +#include "compress_lz4_remap.h" + return 0; -out: - if (err) { - _int_LZ4_compress_HC = NULL; - _int_LZ4_compress_fast = NULL; - _int_LZ4_decompress_safe = NULL; - errno = EINVAL; - } - return err; + fail: +#define REMAP_FAIL +#include "compress_lz4_remap.h" + errno = EINVAL; + return -1; } int lz4_load_lib( knet_handle_t knet_h) { int err = 0, savederrno = 0; if (!lz4_lib) { lz4_lib = open_lib(knet_h, LIBLZ4_1, 0); if (!lz4_lib) { savederrno = EAGAIN; err = -1; goto out; } } if (lz4_remap_symbols(knet_h) < 0) { savederrno = errno; err = -1; } out: errno = savederrno; return err; } int lz4_val_level( knet_handle_t knet_h, int compress_level) { if (compress_level <= 0) { log_info(knet_h, KNET_SUB_LZ4COMP, "lz4 acceleration level 0 (or negatives) are automatically remapped to 1 by %s", LIBLZ4_1); } return 0; } int lz4_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int lzerr = 0, err = 0; int savederrno = 0; lzerr = (*_int_LZ4_compress_fast)((const char *)buf_in, (char *)buf_out, buf_in_len, KNET_DATABUFSIZE_COMPRESS, knet_h->compress_level); /* * data compressed */ if (lzerr > 0) { *buf_out_len = lzerr; } /* * unable to compress */ if (lzerr == 0) { *buf_out_len = buf_in_len; } /* * lz4 internal error */ if (lzerr < 0) { log_err(knet_h, KNET_SUB_LZ4COMP, "lz4 compression error: %d", lzerr); savederrno = EINVAL; err = -1; } errno = savederrno; return err; } #ifdef LZ4HC_CLEVEL_MAX #define KNET_LZ4HC_MAX LZ4HC_CLEVEL_MAX #endif #ifdef LZ4HC_MAX_CLEVEL #define KNET_LZ4HC_MAX LZ4HC_MAX_CLEVEL #endif #ifndef KNET_LZ4HC_MAX #define KNET_LZ4HC_MAX 0 #error Please check lz4hc.h for missing LZ4HC_CLEVEL_MAX or LZ4HC_MAX_CLEVEL variants #endif int lz4hc_val_level( knet_handle_t knet_h, int compress_level) { if (compress_level < 1) { log_err(knet_h, KNET_SUB_LZ4HCCOMP, "lz4hc supports only 1+ values for compression level"); errno = EINVAL; return -1; } if (compress_level < 4) { log_info(knet_h, KNET_SUB_LZ4HCCOMP, "lz4hc recommends 4+ compression level for better results"); } if (compress_level > KNET_LZ4HC_MAX) { log_warn(knet_h, KNET_SUB_LZ4HCCOMP, "lz4hc installed on this system supports up to compression level %d. Higher values behaves as %d", KNET_LZ4HC_MAX, KNET_LZ4HC_MAX); } return 0; } int lz4hc_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int lzerr = 0, err = 0; int savederrno = 0; lzerr = (*_int_LZ4_compress_HC)((const char *)buf_in, (char *)buf_out, buf_in_len, KNET_DATABUFSIZE_COMPRESS, knet_h->compress_level); /* * data compressed */ if (lzerr > 0) { *buf_out_len = lzerr; } /* * unable to compress */ if (lzerr <= 0) { log_err(knet_h, KNET_SUB_LZ4HCCOMP, "lz4hc compression error: %d", lzerr); savederrno = EINVAL; err = -1; } errno = savederrno; return err; } int lz4_decompress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int lzerr = 0, err = 0; int savederrno = 0; lzerr = (*_int_LZ4_decompress_safe)((const char *)buf_in, (char *)buf_out, buf_in_len, KNET_DATABUFSIZE); if (lzerr < 0) { log_err(knet_h, KNET_SUB_LZ4COMP, "lz4 decompression error: %d", lzerr); savederrno = EINVAL; err = -1; } if (lzerr > 0) { *buf_out_len = lzerr; } errno = savederrno; return err; } #endif diff --git a/libknet/compress_lz4_remap.h b/libknet/compress_lz4_remap.h new file mode 100644 index 00000000..039cc9ad --- /dev/null +++ b/libknet/compress_lz4_remap.h @@ -0,0 +1,13 @@ +#include "remap.h" + +REMAP_PROTO(int,LZ4_compress_HC, + (const char* src, char* dst, + int srcSize, int dstCapacity, + int compressionLevel)) +REMAP_PROTO(int,LZ4_compress_fast, + (const char* source, char* dest, + int sourceSize, int maxDestSize, + int acceleration)) +REMAP_PROTO(int,LZ4_decompress_safe, + (const char* source, char* dest, + int compressedSize, int maxDecompressedSize)) diff --git a/libknet/compress_lzma.c b/libknet/compress_lzma.c index 79a0acb9..359382ca 100644 --- a/libknet/compress_lzma.c +++ b/libknet/compress_lzma.c @@ -1,205 +1,179 @@ /* * Copyright (C) 2017 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #include "config.h" #include #include #include #include #ifdef BUILDCOMPLZMA #include #include "internals.h" #include "compress_lzma.h" #include "logging.h" #include "common.h" #define LIBLZMA_5 "liblzma.so.5" /* * global vars for dlopen */ static void *lzma_lib; -/* - * symbols remapping - */ -int (*_int_lzma_easy_buffer_encode)( - uint32_t preset, lzma_check check, - const lzma_allocator *allocator, - const uint8_t *in, size_t in_size, - uint8_t *out, size_t *out_pos, size_t out_size); -int (*_int_lzma_stream_buffer_decode)( - uint64_t *memlimit, uint32_t flags, - const lzma_allocator *allocator, - const uint8_t *in, size_t *in_pos, size_t in_size, - uint8_t *out, size_t *out_pos, size_t out_size); +#include "compress_lzma_remap.h" static int lzma_remap_symbols(knet_handle_t knet_h) { - int err = 0; - char *error = NULL; - - _int_lzma_easy_buffer_encode = dlsym(lzma_lib, "lzma_easy_buffer_encode"); - if (!_int_lzma_easy_buffer_encode) { - error = dlerror(); - log_err(knet_h, KNET_SUB_LZMACOMP, "unable to map lzma_easy_buffer_encode: %s", error); - err = -1; - goto out; - } +#define REMAP_WITH(name) remap_symbol (knet_h, KNET_SUB_LZMACOMP, lzma_lib, name) +#include "compress_lzma_remap.h" + return 0; - _int_lzma_stream_buffer_decode = dlsym(lzma_lib, "lzma_stream_buffer_decode"); - if (!_int_lzma_stream_buffer_decode) { - error = dlerror(); - log_err(knet_h, KNET_SUB_LZMACOMP, "unable to map lzma_stream_buffer_decode: %s", error); - err = -1; - goto out; - } -out: - if (err) { - errno = EINVAL; - } - return err; + fail: +#define REMAP_FAIL +#include "compress_lzma_remap.h" + errno = EINVAL; + return -1; } int lzma_load_lib( knet_handle_t knet_h) { int err = 0, savederrno = 0; if (!lzma_lib) { lzma_lib = open_lib(knet_h, LIBLZMA_5, 0); if (!lzma_lib) { savederrno = EAGAIN; err = -1; goto out; } } if (lzma_remap_symbols(knet_h) < 0) { savederrno = errno; err = -1; } out: errno = savederrno; return err; } int lzma_val_level( knet_handle_t knet_h, int compress_level) { if ((compress_level < 0) || (compress_level > 9)) { log_err(knet_h, KNET_SUB_LZMACOMP, "lzma unsupported compression preset %d (accepted values from 0 to 9)", compress_level); errno = EINVAL; return -1; } return 0; } int lzma_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int err = 0; int savederrno = 0; size_t out_pos = 0; lzma_ret ret = 0; ret = (*_int_lzma_easy_buffer_encode)(knet_h->compress_level, LZMA_CHECK_NONE, NULL, (const uint8_t *)buf_in, buf_in_len, (uint8_t *)buf_out, &out_pos, KNET_DATABUFSIZE_COMPRESS); switch(ret) { case LZMA_OK: *buf_out_len = out_pos; break; case LZMA_MEM_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma compress memory allocation failed"); savederrno = ENOMEM; err = -1; break; case LZMA_MEMLIMIT_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma compress requires higher memory boundaries (see lzma_memlimit_set)"); savederrno = ENOMEM; err = -1; break; case LZMA_PROG_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma compress has been called with incorrect options"); savederrno = EINVAL; err = -1; break; default: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma compress unknown error %u", ret); savederrno = EINVAL; err = -1; break; } errno = savederrno; return err; } int lzma_decompress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int err = 0; int savederrno = 0; uint64_t memlimit = UINT64_MAX; /* disable lzma internal memlimit check */ size_t out_pos = 0, in_pos = 0; lzma_ret ret = 0; ret = (*_int_lzma_stream_buffer_decode)(&memlimit, 0, NULL, (const uint8_t *)buf_in, &in_pos, buf_in_len, (uint8_t *)buf_out, &out_pos, KNET_DATABUFSIZE_COMPRESS); switch(ret) { case LZMA_OK: *buf_out_len = out_pos; break; case LZMA_MEM_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma decompress memory allocation failed"); savederrno = ENOMEM; err = -1; break; case LZMA_MEMLIMIT_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma decompress requires higher memory boundaries (see lzma_memlimit_set)"); savederrno = ENOMEM; err = -1; break; case LZMA_DATA_ERROR: case LZMA_FORMAT_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma decompress invalid data received"); savederrno = EINVAL; err = -1; break; case LZMA_PROG_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma decompress has been called with incorrect options"); savederrno = EINVAL; err = -1; break; default: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma decompress unknown error %u", ret); savederrno = EINVAL; err = -1; break; } errno = savederrno; return err; } #endif diff --git a/libknet/compress_lzma_remap.h b/libknet/compress_lzma_remap.h new file mode 100644 index 00000000..9a13e7c7 --- /dev/null +++ b/libknet/compress_lzma_remap.h @@ -0,0 +1,12 @@ +#include "remap.h" + +REMAP_PROTO(int,lzma_easy_buffer_encode, + (uint32_t preset, lzma_check check, + const lzma_allocator *allocator, + const uint8_t *in, size_t in_size, + uint8_t *out, size_t *out_pos, size_t out_size)) +REMAP_PROTO(int,lzma_stream_buffer_decode, + (uint64_t *memlimit, uint32_t flags, + const lzma_allocator *allocator, + const uint8_t *in, size_t *in_pos, size_t in_size, + uint8_t *out, size_t *out_pos, size_t out_size)) diff --git a/libknet/compress_lzo2.c b/libknet/compress_lzo2.c index 792ddd28..37ac8f2c 100644 --- a/libknet/compress_lzo2.c +++ b/libknet/compress_lzo2.c @@ -1,278 +1,204 @@ /* * Copyright (C) 2017 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #include "config.h" #include #include #include #include #ifdef BUILDCOMPLZO2 #include #include "internals.h" #include "compress_lzo2.h" #include "logging.h" #include "common.h" #define LIBLZO2_2 "liblzo2.so.2" /* * global vars for dlopen */ static void *lzo2_lib; -/* - * symbols remapping - */ -int (*_int_lzo1x_decompress)(const lzo_bytep src, lzo_uint src_len, - lzo_bytep dst, lzo_uintp dst_len, - lzo_voidp wrkmem /* NOT USED */ ); -int (*_int_lzo1x_1_compress)(const lzo_bytep src, lzo_uint src_len, - lzo_bytep dst, lzo_uintp dst_len, - lzo_voidp wrkmem); -int (*_int_lzo1x_1_11_compress)(const lzo_bytep src, lzo_uint src_len, - lzo_bytep dst, lzo_uintp dst_len, - lzo_voidp wrkmem); -int (*_int_lzo1x_1_12_compress)(const lzo_bytep src, lzo_uint src_len, - lzo_bytep dst, lzo_uintp dst_len, - lzo_voidp wrkmem); -int (*_int_lzo1x_1_15_compress)(const lzo_bytep src, lzo_uint src_len, - lzo_bytep dst, lzo_uintp dst_len, - lzo_voidp wrkmem); - -int (*_int_lzo1x_999_compress)(const lzo_bytep src, lzo_uint src_len, - lzo_bytep dst, lzo_uintp dst_len, - lzo_voidp wrkmem); +#include "compress_lzo2_remap.h" static int lzo2_remap_symbols(knet_handle_t knet_h) { - int err = 0; - char *error = NULL; - - _int_lzo1x_decompress = dlsym(lzo2_lib, "lzo1x_decompress"); - if (!_int_lzo1x_decompress) { - error = dlerror(); - log_err(knet_h, KNET_SUB_LZO2COMP, "unable to map lzo1x_decompress: %s", error); - err = -1; - goto out; - } - - _int_lzo1x_1_compress = dlsym(lzo2_lib, "lzo1x_1_compress"); - if (!_int_lzo1x_1_compress) { - error = dlerror(); - log_err(knet_h, KNET_SUB_LZO2COMP, "unable to map lzo1x_1_compress: %s", error); - err = -1; - goto out; - } - - _int_lzo1x_1_11_compress = dlsym(lzo2_lib, "lzo1x_1_11_compress"); - if (!_int_lzo1x_1_11_compress) { - error = dlerror(); - log_err(knet_h, KNET_SUB_LZO2COMP, "unable to map lzo1x_1_11_compress: %s", error); - err = -1; - goto out; - } - - _int_lzo1x_1_12_compress = dlsym(lzo2_lib, "lzo1x_1_12_compress"); - if (!_int_lzo1x_1_12_compress) { - error = dlerror(); - log_err(knet_h, KNET_SUB_LZO2COMP, "unable to map lzo1x_1_12_compress: %s", error); - err = -1; - goto out; - } - - _int_lzo1x_1_15_compress = dlsym(lzo2_lib, "lzo1x_1_15_compress"); - if (!_int_lzo1x_1_15_compress) { - error = dlerror(); - log_err(knet_h, KNET_SUB_LZO2COMP, "unable to map lzo1x_1_15_compress: %s", error); - err = -1; - goto out; - } - - _int_lzo1x_999_compress = dlsym(lzo2_lib, "lzo1x_999_compress"); - if (!_int_lzo1x_999_compress) { - error = dlerror(); - log_err(knet_h, KNET_SUB_LZO2COMP, "unable to map lzo1x_999_compress: %s", error); - err = -1; - goto out; - } +#define REMAP_WITH(name) remap_symbol (knet_h, KNET_SUB_LZO2COMP, lzo2_lib, name) +#include "compress_lzo2_remap.h" + return 0; -out: - if (err) { - _int_lzo1x_decompress = NULL; - _int_lzo1x_1_compress = NULL; - _int_lzo1x_1_11_compress = NULL; - _int_lzo1x_1_12_compress = NULL; - _int_lzo1x_1_15_compress = NULL; - _int_lzo1x_999_compress = NULL; - errno = EINVAL; - } - return err; + fail: +#define REMAP_FAIL +#include "compress_lzo2_remap.h" + errno = EINVAL; + return -1; } int lzo2_load_lib( knet_handle_t knet_h) { int err = 0, savederrno = 0; if (!lzo2_lib) { lzo2_lib = open_lib(knet_h, LIBLZO2_2, 0); if (!lzo2_lib) { savederrno = EAGAIN; err = -1; goto out; } } if (lzo2_remap_symbols(knet_h) < 0) { savederrno = errno; err = -1; } out: errno = savederrno; return err; } int lzo2_is_init( knet_handle_t knet_h, int method_idx) { if (knet_h->compress_int_data[method_idx]) { return 1; } return 0; } int lzo2_init( knet_handle_t knet_h, int method_idx) { /* * LZO1X_999_MEM_COMPRESS is the highest amount of memory lzo2 can use */ if (!knet_h->compress_int_data[method_idx]) { knet_h->compress_int_data[method_idx] = malloc(LZO1X_999_MEM_COMPRESS); if (!knet_h->compress_int_data[method_idx]) { log_err(knet_h, KNET_SUB_LZO2COMP, "lzo2 unable to allocate work memory"); errno = ENOMEM; return -1; } memset(knet_h->compress_int_data[method_idx], 0, LZO1X_999_MEM_COMPRESS); } return 0; } void lzo2_fini( knet_handle_t knet_h, int method_idx) { if (knet_h->compress_int_data[method_idx]) { free(knet_h->compress_int_data[method_idx]); knet_h->compress_int_data[method_idx] = NULL; } return; } int lzo2_val_level( knet_handle_t knet_h, int compress_level) { switch(compress_level) { case 1: log_debug(knet_h, KNET_SUB_LZO2COMP, "lzo2 will use lzo1x_1_compress internal compress method"); break; case 11: log_debug(knet_h, KNET_SUB_LZO2COMP, "lzo2 will use lzo1x_1_11_compress internal compress method"); break; case 12: log_debug(knet_h, KNET_SUB_LZO2COMP, "lzo2 will use lzo1x_1_12_compress internal compress method"); break; case 15: log_debug(knet_h, KNET_SUB_LZO2COMP, "lzo2 will use lzo1x_1_15_compress internal compress method"); break; case 999: log_debug(knet_h, KNET_SUB_LZO2COMP, "lzo2 will use lzo1x_999_compress internal compress method"); break; default: log_warn(knet_h, KNET_SUB_LZO2COMP, "Unknown lzo2 internal compress method. lzo1x_1_compress will be used as default fallback"); break; } return 0; } int lzo2_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int savederrno = 0, lzerr = 0, err = 0; lzo_uint cmp_len; switch(knet_h->compress_level) { case 1: lzerr = (*_int_lzo1x_1_compress)(buf_in, buf_in_len, buf_out, &cmp_len, knet_h->compress_int_data[knet_h->compress_model]); break; case 11: lzerr = (*_int_lzo1x_1_11_compress)(buf_in, buf_in_len, buf_out, &cmp_len, knet_h->compress_int_data[knet_h->compress_model]); break; case 12: lzerr = (*_int_lzo1x_1_12_compress)(buf_in, buf_in_len, buf_out, &cmp_len, knet_h->compress_int_data[knet_h->compress_model]); break; case 15: lzerr = (*_int_lzo1x_1_15_compress)(buf_in, buf_in_len, buf_out, &cmp_len, knet_h->compress_int_data[knet_h->compress_model]); break; case 999: lzerr = (*_int_lzo1x_999_compress)(buf_in, buf_in_len, buf_out, &cmp_len, knet_h->compress_int_data[knet_h->compress_model]); break; default: lzerr = (*_int_lzo1x_1_compress)(buf_in, buf_in_len, buf_out, &cmp_len, knet_h->compress_int_data[knet_h->compress_model]); break; } if (lzerr != LZO_E_OK) { log_err(knet_h, KNET_SUB_LZO2COMP, "lzo2 internal compression error"); savederrno = EAGAIN; err = -1; } else { *buf_out_len = cmp_len; } errno = savederrno; return err; } int lzo2_decompress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int lzerr = 0, err = 0; int savederrno = 0; lzo_uint decmp_len; lzerr = (*_int_lzo1x_decompress)(buf_in, buf_in_len, buf_out, &decmp_len, NULL); if (lzerr != LZO_E_OK) { log_err(knet_h, KNET_SUB_LZO2COMP, "lzo2 internal decompression error"); savederrno = EAGAIN; err = -1; } else { *buf_out_len = decmp_len; } errno = savederrno; return err; } #endif diff --git a/libknet/compress_lzo2_remap.h b/libknet/compress_lzo2_remap.h new file mode 100644 index 00000000..c0c7d2b7 --- /dev/null +++ b/libknet/compress_lzo2_remap.h @@ -0,0 +1,26 @@ +#include "remap.h" + +REMAP_PROTO(int,lzo1x_decompress, + (const lzo_bytep src, lzo_uint src_len, + lzo_bytep dst, lzo_uintp dst_len, + lzo_voidp wrkmem /* NOT USED */ )) +REMAP_PROTO(int,lzo1x_1_compress, + (const lzo_bytep src, lzo_uint src_len, + lzo_bytep dst, lzo_uintp dst_len, + lzo_voidp wrkmem)) +REMAP_PROTO(int,lzo1x_1_11_compress, + (const lzo_bytep src, lzo_uint src_len, + lzo_bytep dst, lzo_uintp dst_len, + lzo_voidp wrkmem)) +REMAP_PROTO(int,lzo1x_1_12_compress, + (const lzo_bytep src, lzo_uint src_len, + lzo_bytep dst, lzo_uintp dst_len, + lzo_voidp wrkmem)) +REMAP_PROTO(int,lzo1x_1_15_compress, + (const lzo_bytep src, lzo_uint src_len, + lzo_bytep dst, lzo_uintp dst_len, + lzo_voidp wrkmem)) +REMAP_PROTO(int,lzo1x_999_compress, + (const lzo_bytep src, lzo_uint src_len, + lzo_bytep dst, lzo_uintp dst_len, + lzo_voidp wrkmem))