Re: [PATCH 2/2] crypto: acomp - add support for deflate rfc1950 (zlib)

2017-04-21 Thread Herbert Xu
On Wed, Apr 19, 2017 at 03:11:42PM +0100, Giovanni Cabiddu wrote:
>
> +}, {
> + .alloc_ctx  = zlib_deflate_alloc_ctx,
> + .free_ctx   = deflate_free_ctx,
> + .compress   = deflate_scompress,
> + .decompress = deflate_sdecompress,
> + .base   = {
> + .cra_name   = "zlib(deflate)",

Please avoid using parentheses as they are used for templates.
Perhaps zlib-deflate or deflate-rfc1950.

Cheers,
-- 
Email: Herbert Xu 
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt


[PATCH 2/2] crypto: acomp - add support for deflate rfc1950 (zlib)

2017-04-19 Thread Giovanni Cabiddu
Add scomp backend for zlib-deflate compression algorithm.
This backend outputs data using the format defined in rfc1950
(raw deflate surrounded by zlib header and footer).

Signed-off-by: Giovanni Cabiddu 
---
 crypto/deflate.c | 61 -
 crypto/testmgr.c | 10 
 crypto/testmgr.h | 75 
 3 files changed, 129 insertions(+), 17 deletions(-)

diff --git a/crypto/deflate.c b/crypto/deflate.c
index f942cb3..647f2e5 100644
--- a/crypto/deflate.c
+++ b/crypto/deflate.c
@@ -43,20 +43,24 @@ struct deflate_ctx {
struct z_stream_s decomp_stream;
 };
 
-static int deflate_comp_init(struct deflate_ctx *ctx)
+static int deflate_comp_init(struct deflate_ctx *ctx, int format)
 {
int ret = 0;
struct z_stream_s *stream = >comp_stream;
 
stream->workspace = vzalloc(zlib_deflate_workspacesize(
-   -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL));
+   MAX_WBITS, MAX_MEM_LEVEL));
if (!stream->workspace) {
ret = -ENOMEM;
goto out;
}
-   ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
-   -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
-   Z_DEFAULT_STRATEGY);
+   if (format)
+   ret = zlib_deflateInit(stream, 3);
+   else
+   ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
+   -DEFLATE_DEF_WINBITS,
+   DEFLATE_DEF_MEMLEVEL,
+   Z_DEFAULT_STRATEGY);
if (ret != Z_OK) {
ret = -EINVAL;
goto out_free;
@@ -68,7 +72,7 @@ static int deflate_comp_init(struct deflate_ctx *ctx)
goto out;
 }
 
-static int deflate_decomp_init(struct deflate_ctx *ctx)
+static int deflate_decomp_init(struct deflate_ctx *ctx, int format)
 {
int ret = 0;
struct z_stream_s *stream = >decomp_stream;
@@ -78,7 +82,10 @@ static int deflate_decomp_init(struct deflate_ctx *ctx)
ret = -ENOMEM;
goto out;
}
-   ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
+   if (format)
+   ret = zlib_inflateInit(stream);
+   else
+   ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
if (ret != Z_OK) {
ret = -EINVAL;
goto out_free;
@@ -102,21 +109,21 @@ static void deflate_decomp_exit(struct deflate_ctx *ctx)
vfree(ctx->decomp_stream.workspace);
 }
 
-static int __deflate_init(void *ctx)
+static int __deflate_init(void *ctx, int format)
 {
int ret;
 
-   ret = deflate_comp_init(ctx);
+   ret = deflate_comp_init(ctx, format);
if (ret)
goto out;
-   ret = deflate_decomp_init(ctx);
+   ret = deflate_decomp_init(ctx, format);
if (ret)
deflate_comp_exit(ctx);
 out:
return ret;
 }
 
-static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
+static void *gen_deflate_alloc_ctx(struct crypto_scomp *tfm, int format)
 {
struct deflate_ctx *ctx;
int ret;
@@ -125,7 +132,7 @@ static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
if (!ctx)
return ERR_PTR(-ENOMEM);
 
-   ret = __deflate_init(ctx);
+   ret = __deflate_init(ctx, format);
if (ret) {
kfree(ctx);
return ERR_PTR(ret);
@@ -134,11 +141,21 @@ static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
return ctx;
 }
 
+static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
+{
+   return gen_deflate_alloc_ctx(tfm, 0);
+}
+
+static void *zlib_deflate_alloc_ctx(struct crypto_scomp *tfm)
+{
+   return gen_deflate_alloc_ctx(tfm, 1);
+}
+
 static int deflate_init(struct crypto_tfm *tfm)
 {
struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
 
-   return __deflate_init(ctx);
+   return __deflate_init(ctx, 0);
 }
 
 static void __deflate_exit(void *ctx)
@@ -272,7 +289,7 @@ static struct crypto_alg alg = {
.coa_decompress = deflate_decompress } }
 };
 
-static struct scomp_alg scomp = {
+static struct scomp_alg scomp[] = { {
.alloc_ctx  = deflate_alloc_ctx,
.free_ctx   = deflate_free_ctx,
.compress   = deflate_scompress,
@@ -282,7 +299,17 @@ static struct scomp_alg scomp = {
.cra_driver_name = "deflate-scomp",
.cra_module  = THIS_MODULE,
}
-};
+}, {
+   .alloc_ctx  = zlib_deflate_alloc_ctx,
+   .free_ctx   = deflate_free_ctx,
+   .compress   = deflate_scompress,
+   .decompress = deflate_sdecompress,
+   .base   = {
+   .cra_name   = "zlib(deflate)",
+