int compress_model,
int compress_level)
{
- return compress_modules_cmds[compress_model].ops->val_level(knet_h, compress_level);
+ if (compress_modules_cmds[compress_model].ops->val_level != NULL) {
+ return compress_modules_cmds[compress_model].ops->val_level(knet_h, compress_level);
+ }
+ return 0;
}
/*
return 0;
}
+static int compress_lib_test(knet_handle_t knet_h)
+{
+ int savederrno = 0;
+ unsigned char src[KNET_DATABUFSIZE];
+ unsigned char dst[KNET_DATABUFSIZE_COMPRESS];
+ ssize_t dst_comp_len = KNET_DATABUFSIZE_COMPRESS, dst_decomp_len = KNET_DATABUFSIZE;
+
+ memset(src, 0, KNET_DATABUFSIZE);
+ memset(dst, 0, KNET_DATABUFSIZE_COMPRESS);
+
+ /*
+ * NOTE: we cannot use compress and decompress API calls due to locking
+ * so we need to call directly into the modules
+ */
+
+ if (compress_modules_cmds[knet_h->compress_model].ops->compress(knet_h, src, KNET_DATABUFSIZE, dst, &dst_comp_len) < 0) {
+ savederrno = errno;
+ log_err(knet_h, KNET_SUB_COMPRESS, "Unable to compress test buffer. Please check your compression settings: %s", strerror(savederrno));
+ errno = savederrno;
+ return -1;
+ }
+
+ if (compress_modules_cmds[knet_h->compress_model].ops->decompress(knet_h, dst, dst_comp_len, src, &dst_decomp_len) < 0) {
+ savederrno = errno;
+ log_err(knet_h, KNET_SUB_COMPRESS, "Unable to decompress test buffer. Please check your compression settings: %s", strerror(savederrno));
+ errno = savederrno;
+ return -1;
+ }
+
+ return 0;
+}
+
int compress_init(
knet_handle_t knet_h)
{
goto out_unlock;
}
+ knet_h->compress_model = cmp_model;
+ knet_h->compress_level = knet_handle_compress_cfg->compress_level;
+
+ if (compress_lib_test(knet_h) < 0) {
+ savederrno = errno;
+ err = -1;
+ goto out_unlock;
+ }
+
out_unlock:
pthread_rwlock_unlock(&shlib_rwlock);
}
- if (!err) {
- knet_h->compress_model = cmp_model;
- knet_h->compress_level = knet_handle_compress_cfg->compress_level;
- } else {
+ if (err) {
knet_h->compress_model = 0;
+ knet_h->compress_level = 0;
}
errno = savederrno;
#include "logging.h"
#include "compress_model.h"
-static 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;
-}
-
static int bzip2_compress(
knet_handle_t knet_h,
const unsigned char *buf_in,
NULL,
NULL,
NULL,
- bzip2_val_level,
+ NULL,
bzip2_compress,
bzip2_decompress
};
#include "logging.h"
#include "compress_model.h"
-static 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");
- }
-
- return 0;
-}
-
static int lz4_compress(
knet_handle_t knet_h,
const unsigned char *buf_in,
NULL,
NULL,
NULL,
- lz4_val_level,
+ NULL,
lz4_compress,
lz4_decompress
};
#define KNET_LZ4HC_MAX 16
#endif
-static 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;
-}
-
static int lz4hc_compress(
knet_handle_t knet_h,
const unsigned char *buf_in,
NULL,
NULL,
NULL,
- lz4hc_val_level,
+ NULL,
lz4hc_compress,
lz4_decompress
};
#include "logging.h"
#include "compress_model.h"
-static 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;
-}
-
static int lzma_compress(
knet_handle_t knet_h,
const unsigned char *buf_in,
NULL,
NULL,
NULL,
- lzma_val_level,
+ NULL,
lzma_compress,
lzma_decompress
};
*/
/*
- * required functions
- *
* val_level is called upon compress configuration changes
* to make sure that the requested compress_level is valid
* within the context of a given module.
int compress_level);
/*
+ * required functions
+ *
* hopefully those 2 don't require any explanation....
*/
int (*compress) (knet_handle_t knet_h,
#include "logging.h"
#include "compress_model.h"
-static int zlib_val_level(
- knet_handle_t knet_h,
- int compress_level)
-{
- if (compress_level < 0) {
- log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib does not support negative compression level %d",
- compress_level);
- return -1;
- }
- if (compress_level > 9) {
- log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib does not support compression level higher than 9");
- return -1;
- }
- if (compress_level == 0) {
- log_warn(knet_h, KNET_SUB_ZLIBCOMP, "zlib compress level 0 does NOT perform any compression");
- }
- return 0;
-}
-
static int zlib_compress(
knet_handle_t knet_h,
const unsigned char *buf_in,
NULL,
NULL,
NULL,
- zlib_val_level,
+ NULL,
zlib_compress,
zlib_decompress
};
flush_logs(logfds[0], stdout);
- printf("Test knet_handle_compress with zlib compress and negative level\n");
+ printf("Test knet_handle_compress with zlib compress and negative level (-2)\n");
memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg));
strncpy(knet_handle_compress_cfg.compress_model, "zlib", sizeof(knet_handle_compress_cfg.compress_model) - 1);
- knet_handle_compress_cfg.compress_level = -1;
+ knet_handle_compress_cfg.compress_level = -2;
if ((!knet_handle_compress(knet_h, &knet_handle_compress_cfg)) || (errno != EINVAL)) {
- printf("knet_handle_compress accepted invalid (-1) compress level for zlib\n");
+ printf("knet_handle_compress accepted invalid (-2) compress level for zlib\n");
knet_handle_free(knet_h);
flush_logs(logfds[0], stdout);
close_logpipes(logfds);