Commit | Line | Data |
---|---|---|
0b77abb3 ZS |
1 | /* |
2 | * Cryptographic API. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License version 2 as published by | |
6 | * the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | * | |
17 | */ | |
18 | ||
19 | #include <linux/init.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/crypto.h> | |
22 | #include <linux/vmalloc.h> | |
42614b05 | 23 | #include <linux/mm.h> |
0b77abb3 ZS |
24 | #include <linux/lzo.h> |
25 | ||
26 | struct lzo_ctx { | |
27 | void *lzo_comp_mem; | |
28 | }; | |
29 | ||
30 | static int lzo_init(struct crypto_tfm *tfm) | |
31 | { | |
32 | struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); | |
33 | ||
42614b05 ED |
34 | ctx->lzo_comp_mem = kmalloc(LZO1X_MEM_COMPRESS, |
35 | GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT); | |
36 | if (!ctx->lzo_comp_mem) | |
37 | ctx->lzo_comp_mem = vmalloc(LZO1X_MEM_COMPRESS); | |
0b77abb3 ZS |
38 | if (!ctx->lzo_comp_mem) |
39 | return -ENOMEM; | |
40 | ||
41 | return 0; | |
42 | } | |
43 | ||
44 | static void lzo_exit(struct crypto_tfm *tfm) | |
45 | { | |
46 | struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); | |
47 | ||
42614b05 ED |
48 | if (is_vmalloc_addr(ctx->lzo_comp_mem)) |
49 | vfree(ctx->lzo_comp_mem); | |
50 | else | |
51 | kfree(ctx->lzo_comp_mem); | |
0b77abb3 ZS |
52 | } |
53 | ||
54 | static int lzo_compress(struct crypto_tfm *tfm, const u8 *src, | |
55 | unsigned int slen, u8 *dst, unsigned int *dlen) | |
56 | { | |
57 | struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); | |
58 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ | |
59 | int err; | |
60 | ||
61 | err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx->lzo_comp_mem); | |
62 | ||
63 | if (err != LZO_E_OK) | |
64 | return -EINVAL; | |
65 | ||
66 | *dlen = tmp_len; | |
67 | return 0; | |
68 | } | |
69 | ||
70 | static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src, | |
71 | unsigned int slen, u8 *dst, unsigned int *dlen) | |
72 | { | |
73 | int err; | |
74 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ | |
75 | ||
76 | err = lzo1x_decompress_safe(src, slen, dst, &tmp_len); | |
77 | ||
78 | if (err != LZO_E_OK) | |
79 | return -EINVAL; | |
80 | ||
81 | *dlen = tmp_len; | |
82 | return 0; | |
83 | ||
84 | } | |
85 | ||
86 | static struct crypto_alg alg = { | |
87 | .cra_name = "lzo", | |
88 | .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, | |
89 | .cra_ctxsize = sizeof(struct lzo_ctx), | |
90 | .cra_module = THIS_MODULE, | |
0b77abb3 ZS |
91 | .cra_init = lzo_init, |
92 | .cra_exit = lzo_exit, | |
93 | .cra_u = { .compress = { | |
94 | .coa_compress = lzo_compress, | |
95 | .coa_decompress = lzo_decompress } } | |
96 | }; | |
97 | ||
3af5b90b | 98 | static int __init lzo_mod_init(void) |
0b77abb3 ZS |
99 | { |
100 | return crypto_register_alg(&alg); | |
101 | } | |
102 | ||
3af5b90b | 103 | static void __exit lzo_mod_fini(void) |
0b77abb3 ZS |
104 | { |
105 | crypto_unregister_alg(&alg); | |
106 | } | |
107 | ||
3af5b90b KB |
108 | module_init(lzo_mod_init); |
109 | module_exit(lzo_mod_fini); | |
0b77abb3 ZS |
110 | |
111 | MODULE_LICENSE("GPL"); | |
112 | MODULE_DESCRIPTION("LZO Compression Algorithm"); |