4 * Copyright (c) 2013 Chanho Min <chanho.min@lge.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/crypto.h>
24 #include <linux/vmalloc.h>
25 #include <linux/lz4.h>
26 #include <crypto/internal/scompress.h>
32 static void *lz4_alloc_ctx(struct crypto_scomp
*tfm
)
36 ctx
= vmalloc(LZ4_MEM_COMPRESS
);
38 return ERR_PTR(-ENOMEM
);
43 static int lz4_init(struct crypto_tfm
*tfm
)
45 struct lz4_ctx
*ctx
= crypto_tfm_ctx(tfm
);
47 ctx
->lz4_comp_mem
= lz4_alloc_ctx(NULL
);
48 if (IS_ERR(ctx
->lz4_comp_mem
))
54 static void lz4_free_ctx(struct crypto_scomp
*tfm
, void *ctx
)
59 static void lz4_exit(struct crypto_tfm
*tfm
)
61 struct lz4_ctx
*ctx
= crypto_tfm_ctx(tfm
);
63 lz4_free_ctx(NULL
, ctx
->lz4_comp_mem
);
66 static int __lz4_compress_crypto(const u8
*src
, unsigned int slen
,
67 u8
*dst
, unsigned int *dlen
, void *ctx
)
69 size_t tmp_len
= *dlen
;
72 err
= lz4_compress(src
, slen
, dst
, &tmp_len
, ctx
);
81 static int lz4_scompress(struct crypto_scomp
*tfm
, const u8
*src
,
82 unsigned int slen
, u8
*dst
, unsigned int *dlen
,
85 return __lz4_compress_crypto(src
, slen
, dst
, dlen
, ctx
);
88 static int lz4_compress_crypto(struct crypto_tfm
*tfm
, const u8
*src
,
89 unsigned int slen
, u8
*dst
, unsigned int *dlen
)
91 struct lz4_ctx
*ctx
= crypto_tfm_ctx(tfm
);
93 return __lz4_compress_crypto(src
, slen
, dst
, dlen
, ctx
->lz4_comp_mem
);
96 static int __lz4_decompress_crypto(const u8
*src
, unsigned int slen
,
97 u8
*dst
, unsigned int *dlen
, void *ctx
)
100 size_t tmp_len
= *dlen
;
101 size_t __slen
= slen
;
103 err
= lz4_decompress_unknownoutputsize(src
, __slen
, dst
, &tmp_len
);
111 static int lz4_sdecompress(struct crypto_scomp
*tfm
, const u8
*src
,
112 unsigned int slen
, u8
*dst
, unsigned int *dlen
,
115 return __lz4_decompress_crypto(src
, slen
, dst
, dlen
, NULL
);
118 static int lz4_decompress_crypto(struct crypto_tfm
*tfm
, const u8
*src
,
119 unsigned int slen
, u8
*dst
,
122 return __lz4_decompress_crypto(src
, slen
, dst
, dlen
, NULL
);
125 static struct crypto_alg alg_lz4
= {
127 .cra_flags
= CRYPTO_ALG_TYPE_COMPRESS
,
128 .cra_ctxsize
= sizeof(struct lz4_ctx
),
129 .cra_module
= THIS_MODULE
,
130 .cra_list
= LIST_HEAD_INIT(alg_lz4
.cra_list
),
131 .cra_init
= lz4_init
,
132 .cra_exit
= lz4_exit
,
133 .cra_u
= { .compress
= {
134 .coa_compress
= lz4_compress_crypto
,
135 .coa_decompress
= lz4_decompress_crypto
} }
138 static struct scomp_alg scomp
= {
139 .alloc_ctx
= lz4_alloc_ctx
,
140 .free_ctx
= lz4_free_ctx
,
141 .compress
= lz4_scompress
,
142 .decompress
= lz4_sdecompress
,
145 .cra_driver_name
= "lz4-scomp",
146 .cra_module
= THIS_MODULE
,
150 static int __init
lz4_mod_init(void)
154 ret
= crypto_register_alg(&alg_lz4
);
158 ret
= crypto_register_scomp(&scomp
);
160 crypto_unregister_alg(&alg_lz4
);
167 static void __exit
lz4_mod_fini(void)
169 crypto_unregister_alg(&alg_lz4
);
170 crypto_unregister_scomp(&scomp
);
173 module_init(lz4_mod_init
);
174 module_exit(lz4_mod_fini
);
176 MODULE_LICENSE("GPL");
177 MODULE_DESCRIPTION("LZ4 Compression Algorithm");
178 MODULE_ALIAS_CRYPTO("lz4");