Linux-EROFS Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Gao Xiang <xiang@kernel.org>
To: linux-erofs@lists.ozlabs.org
Cc: Lasse Collin <lasse.collin@tukaani.org>
Subject: [PATCH 2/3] erofs-utils: fuse: add LZMA algorithm support
Date: Sat, 30 Oct 2021 10:01:17 +0800	[thread overview]
Message-ID: <20211030020118.13898-2-xiang@kernel.org> (raw)
In-Reply-To: <20211030020118.13898-1-xiang@kernel.org>

This patch adds LZMA compression algorithm support to erofsfuse
to test if LZMA fixed-sized output compression works as expected.

Cc: Lasse Collin <lasse.collin@tukaani.org>
Signed-off-by: Gao Xiang <xiang@kernel.org>
---
 include/erofs_fs.h |  5 +++-
 lib/data.c         |  7 ++---
 lib/decompress.c   | 66 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+), 4 deletions(-)

diff --git a/include/erofs_fs.h b/include/erofs_fs.h
index 66a68e3b2065..86ad6f5fd86c 100644
--- a/include/erofs_fs.h
+++ b/include/erofs_fs.h
@@ -248,7 +248,8 @@ struct erofs_inode_chunk_index {
 
 /* available compression algorithm types (for h_algorithmtype) */
 enum {
-	Z_EROFS_COMPRESSION_LZ4	= 0,
+	Z_EROFS_COMPRESSION_LZ4		= 0,
+	Z_EROFS_COMPRESSION_LZMA	= 1,
 	Z_EROFS_COMPRESSION_MAX
 };
 #define Z_EROFS_ALL_COMPR_ALGS		(1 << (Z_EROFS_COMPRESSION_MAX - 1))
@@ -260,6 +261,8 @@ struct z_erofs_lz4_cfgs {
 	u8 reserved[10];
 } __packed;
 
+#define Z_EROFS_LZMA_MAX_DICT_SIZE	(8 * Z_EROFS_PCLUSTER_MAX_SIZE)
+
 /*
  * bit 0 : COMPACTED_2B indexes (0 - off; 1 - on)
  *  e.g. for 4k logical cluster size,      4B        if compacted 2B is off;
diff --git a/lib/data.c b/lib/data.c
index 641d8408b54f..82bf6ba85592 100644
--- a/lib/data.c
+++ b/lib/data.c
@@ -244,9 +244,10 @@ static int z_erofs_read_data(struct erofs_inode *inode, char *buffer,
 		if (ret < 0)
 			break;
 
-		algorithmformat = map.m_flags & EROFS_MAP_ZIPPED ?
-						Z_EROFS_COMPRESSION_LZ4 :
-						Z_EROFS_COMPRESSION_SHIFTED;
+		if (map.m_flags & EROFS_MAP_ZIPPED)
+			algorithmformat = inode->z_algorithmtype[0];
+		else
+			algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
 
 		ret = z_erofs_decompress(&(struct z_erofs_decompress_req) {
 					.in = raw,
diff --git a/lib/decompress.c b/lib/decompress.c
index 2ee1439d9bfa..f313e41d780b 100644
--- a/lib/decompress.c
+++ b/lib/decompress.c
@@ -7,6 +7,68 @@
 
 #include "erofs/decompress.h"
 #include "erofs/err.h"
+#include "erofs/print.h"
+
+#ifdef HAVE_LIBLZMA
+#include <lzma.h>
+
+static int z_erofs_decompress_lzma(struct z_erofs_decompress_req *rq)
+{
+	int ret = 0;
+	u8 *dest = (u8 *)rq->out;
+	u8 *src = (u8 *)rq->in;
+	u8 *buff = NULL;
+	unsigned int inputmargin = 0;
+	lzma_stream strm;
+	lzma_ret ret2;
+
+	while (!src[inputmargin & ~PAGE_MASK])
+		if (!(++inputmargin & ~PAGE_MASK))
+			break;
+
+	if (inputmargin >= rq->inputsize)
+		return -EFSCORRUPTED;
+
+	if (rq->decodedskip) {
+		buff = malloc(rq->decodedlength);
+		if (!buff)
+			return -ENOMEM;
+		dest = buff;
+	}
+
+	strm = (lzma_stream)LZMA_STREAM_INIT;
+	strm.next_in = src + inputmargin;
+	strm.avail_in = rq->inputsize - inputmargin;
+	strm.next_out = dest;
+	strm.avail_out = rq->decodedlength;
+
+	ret2 = lzma_microlzma_decoder(&strm, strm.avail_in, rq->decodedlength,
+				      !rq->partial_decoding,
+				      Z_EROFS_LZMA_MAX_DICT_SIZE);
+	if (ret2 != LZMA_OK) {
+		erofs_err("fail to initialize lzma decoder %u", ret2 | 0U);
+		ret = -EFAULT;
+		goto out;
+	}
+
+	ret2 = lzma_code(&strm, LZMA_FINISH);
+	if (ret2 != LZMA_STREAM_END) {
+		ret = -EFSCORRUPTED;
+		goto out_lzma_end;
+	}
+
+	if (rq->decodedskip)
+		memcpy(rq->out, dest + rq->decodedskip,
+		       rq->decodedlength - rq->decodedskip);
+
+out_lzma_end:
+	lzma_end(&strm);
+out:
+	if (buff)
+		free(buff);
+	return ret;
+}
+#endif
 
 #ifdef LZ4_ENABLED
 #include <lz4.h>
@@ -81,6 +143,10 @@ int z_erofs_decompress(struct z_erofs_decompress_req *rq)
 #ifdef LZ4_ENABLED
 	if (rq->alg == Z_EROFS_COMPRESSION_LZ4)
 		return z_erofs_decompress_lz4(rq);
+#endif
+#ifdef HAVE_LIBLZMA
+	if (rq->alg == Z_EROFS_COMPRESSION_LZMA)
+		return z_erofs_decompress_lzma(rq);
 #endif
 	return -EOPNOTSUPP;
 }
-- 
2.20.1


  reply	other threads:[~2021-10-30  2:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-30  2:01 [PATCH 1/3] erofs-utils: add liblzma dependency Gao Xiang
2021-10-30  2:01 ` Gao Xiang [this message]
2021-10-30  2:01 ` [PATCH 3/3] erofs-utils: mkfs: add LZMA algorithm support Gao Xiang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211030020118.13898-2-xiang@kernel.org \
    --to=xiang@kernel.org \
    --cc=lasse.collin@tukaani.org \
    --cc=linux-erofs@lists.ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox