From e113595e0bd845bbd3346806c7f10a17fb09927c Mon Sep 17 00:00:00 2001 From: Konstantin Tokarev Date: Sun, 6 Oct 2013 19:24:27 +0400 Subject: [PATCH] LZ4HC/LZO hybrid compression --- mkfs.ubifs/compr.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 51 insertions(+), 1 deletions(-) diff --git a/mkfs.ubifs/compr.c b/mkfs.ubifs/compr.c index 0900653..8f6e06e 100644 --- a/mkfs.ubifs/compr.c +++ b/mkfs.ubifs/compr.c @@ -182,6 +182,54 @@ select_zlib: return 0; } +static int lz4hc_lzo_compress(void *in_buf, size_t in_len, void *out_buf, + size_t *out_len, int *type) +{ + int lz4hc_ret, lzo_ret; + size_t lz4hc_len, lzo_len; + + lz4hc_len = lzo_len = *out_len; + lzo_ret = lzo_compress(in_buf, in_len, zlib_buf, &lzo_len); + lz4hc_ret = lz4hc_compress(in_buf, in_len, out_buf, &lz4hc_len); + + if (lz4hc_ret && lzo_ret) + /* Both compressors failed */ + return -1; + + if (!lz4hc_ret && !lzo_ret) { + double percent; + + /* Both compressors succeeded */ + if (lz4hc_len <= lzo_len ) + goto select_lz4hc; + + /*percent = (double)lzo_len / (double)lz4hc_len; + percent *= 100; + if (percent > 100 - c->favor_percent) + goto select_lz4hc;*/ + goto select_lzo; + } + + if (lz4hc_ret) + /* Only lzo compressor succeeded */ + goto select_lzo; + + /* Only lz4hc compressor succeeded */ + +select_lz4hc: + printf("lz4hc\n"); + *out_len = lz4hc_len; + *type = MKFS_UBIFS_COMPR_LZ4HC; + return 0; + +select_lzo: + printf("lzo\n"); + *out_len = lzo_len; + *type = MKFS_UBIFS_COMPR_LZO; + memcpy(out_buf, zlib_buf, lzo_len); + return 0; +} + int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len, int type) { @@ -192,8 +240,10 @@ int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len, return MKFS_UBIFS_COMPR_NONE; } - if (c->favor_lzo || c->favor_lz4hc) + if (c->favor_lzo) ret = favor_alt_compress(in_buf, in_len, out_buf, out_len, &type, c->favor_lzo); + else if (c->favor_lz4hc) + ret = lz4hc_lzo_compress(in_buf, in_len, out_buf, out_len, &type); else { switch (type) { case MKFS_UBIFS_COMPR_LZ4HC: -- 1.7.2.5