From: Artem Bityutskiy <dedekind1@gmail.com>
To: linux-mtd@lists.infradead.org
Cc: Andi Kleen <ak@linux.intel.com>
Subject: [PATCH 3/3] Add snappy interface to crypto API
Date: Sun, 30 Oct 2011 14:38:43 +0200 [thread overview]
Message-ID: <1319978325.2126.35.camel@koala> (raw)
In-Reply-To: <1319153941-26234-1-git-send-email-andi@firstfloor.org>
From: Andi Kleen <ak@linux.intel.com>
Mainly so that ubifs can use it.
Snappy is a better compressor in the same niche as LZO.
Only lightly tested so far. Experiences welcome.
Cc: herbert@gondor.apana.org.au
Cc: dedekind1@gmail.com
Cc: adrian.hunter@intel.com
Signed-off-by: Andi Kleen <ak@linux.intel.com>
---
crypto/Kconfig | 9 +++++
crypto/Makefile | 1 +
crypto/snappy.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 109 insertions(+), 0 deletions(-)
create mode 100644 crypto/snappy.c
diff --git a/crypto/Kconfig b/crypto/Kconfig
index ae27b753..2c85991 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -823,6 +823,15 @@ config CRYPTO_LZO
help
This is the LZO algorithm.
+config CRYPTO_SNAPPY
+ tristate "Snappy compression algorithm"
+ select CRYPTO_ALGAPI
+ select SNAPPY
+ help
+ snappy is a faster alternative to the lzo compression algorithm
+ with comparable compression. It is very fast on 64bit systems, but also
+ good on 32bit systems. It especially excels at already compressed data.
+
comment "Random Number Generation"
config CRYPTO_ANSI_CPRNG
diff --git a/crypto/Makefile b/crypto/Makefile
index ce5a813..cf92f6f 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -80,6 +80,7 @@ obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o
obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o authencesn.o
obj-$(CONFIG_CRYPTO_LZO) += lzo.o
+obj-$(CONFIG_CRYPTO_SNAPPY) += snappy.o
obj-$(CONFIG_CRYPTO_RNG2) += rng.o
obj-$(CONFIG_CRYPTO_RNG2) += krng.o
obj-$(CONFIG_CRYPTO_ANSI_CPRNG) += ansi_cprng.o
diff --git a/crypto/snappy.c b/crypto/snappy.c
new file mode 100644
index 0000000..2f44d30
--- /dev/null
+++ b/crypto/snappy.c
@@ -0,0 +1,99 @@
+/*
+ * Cryptographic API.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ *
+ * This program 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 General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/crypto.h>
+#include <linux/vmalloc.h>
+#include <linux/snappy.h>
+
+struct snappy_ctx {
+ struct snappy_env env;
+};
+
+/* Only needed for compression actually */
+static int snappy_init(struct crypto_tfm *tfm)
+{
+ struct snappy_ctx *ctx = crypto_tfm_ctx(tfm);
+
+ return snappy_init_env(&ctx->env);
+}
+
+static void snappy_exit(struct crypto_tfm *tfm)
+{
+ struct snappy_ctx *ctx = crypto_tfm_ctx(tfm);
+
+ snappy_free_env(&ctx->env);
+}
+
+static int snp_compress(struct crypto_tfm *tfm, const u8 *src,
+ unsigned int slen, u8 *dst, unsigned int *dlen)
+{
+ struct snappy_ctx *ctx = crypto_tfm_ctx(tfm);
+ size_t olen;
+ int err;
+
+ /* XXXX very pessimistic. check in snappy? */
+ if (*dlen < snappy_max_compressed_length(*dlen))
+ return -EINVAL;
+ err = snappy_compress(&ctx->env, src, slen, dst, &olen);
+ *dlen = olen;
+ return err;
+}
+
+static int snp_decompress(struct crypto_tfm *tfm, const u8 *src,
+ unsigned int slen, u8 *dst, unsigned int *dlen)
+{
+ size_t ulen;
+
+ if (!snappy_uncompressed_length(src, slen, &ulen))
+ return -EIO;
+ if (*dlen < ulen)
+ return -EINVAL;
+ *dlen = ulen;
+ return snappy_uncompress(src, slen, dst) ? 0 : -EIO;
+}
+
+static struct crypto_alg alg = {
+ .cra_name = "snappy",
+ .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
+ .cra_ctxsize = sizeof(struct snappy_ctx),
+ .cra_module = THIS_MODULE,
+ .cra_list = LIST_HEAD_INIT(alg.cra_list),
+ .cra_init = snappy_init,
+ .cra_exit = snappy_exit,
+ .cra_u = { .compress = {
+ .coa_compress = snp_compress,
+ .coa_decompress = snp_decompress } }
+};
+
+static int __init snappy_mod_init(void)
+{
+ return crypto_register_alg(&alg);
+}
+
+static void __exit snappy_mod_fini(void)
+{
+ crypto_unregister_alg(&alg);
+}
+
+module_init(snappy_mod_init);
+module_exit(snappy_mod_fini);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Snappy Compression Algorithm");
--
1.7.4.4
next prev parent reply other threads:[~2011-10-30 12:38 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-10-20 23:38 [PATCH 1/3] Add the snappy-c compressor to lib Andi Kleen
2011-10-20 23:39 ` [PATCH 2/3] BTRFS: Add snappy support Andi Kleen
2011-10-20 23:39 ` [PATCH 3/3] Add snappy interface to crypto API Andi Kleen
2011-10-20 23:39 ` Andi Kleen
2011-10-21 13:24 ` Herbert Xu
2011-10-21 13:24 ` Herbert Xu
2011-10-30 12:38 ` [PATCH 2/3] BTRFS: Add snappy support Artem Bityutskiy
2011-10-30 12:38 ` Artem Bityutskiy [this message]
-- strict thread matches above, loose matches on Subject: below --
2012-01-13 0:28 Updated btrfs/crypto snappy interface ready for merging Andi Kleen
2012-01-13 0:28 ` [PATCH 3/3] Add snappy interface to crypto API Andi Kleen
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=1319978325.2126.35.camel@koala \
--to=dedekind1@gmail.com \
--cc=ak@linux.intel.com \
--cc=linux-mtd@lists.infradead.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.