Commit | Line | Data |
---|---|---|
4b9e9796 S |
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> | |
23 | #include <linux/lzo.h> | |
24 | #include <linux/lz4k.h> | |
25 | ||
26 | #define malloc(a) kmalloc(a, GFP_KERNEL) | |
27 | #define free(a) kfree(a) | |
28 | ||
29 | #if 0 | |
30 | ||
31 | extern int lz4k_compress_ubifs(const unsigned char *in, size_t in_len, unsigned char *out, | |
32 | size_t *out_len, void *wrkmem); | |
33 | ||
34 | extern int lz4k_decompress_ubifs(const unsigned char *in, size_t in_len, | |
35 | unsigned char *out, size_t *out_len); | |
36 | ||
37 | extern int lz4k_compress(const unsigned char *in, size_t in_len, unsigned char *out, | |
38 | size_t *out_len, void *wrkmem); | |
39 | #endif | |
40 | struct lz4k_ctx { | |
41 | void *lz4k_comp_mem; | |
42 | }; | |
43 | ||
44 | static int lz4kc_init(struct crypto_tfm *tfm) | |
45 | { | |
46 | struct lz4k_ctx *ctx = crypto_tfm_ctx(tfm); | |
47 | ||
48 | ctx->lz4k_comp_mem = vmalloc(LZO1X_MEM_COMPRESS); | |
49 | if (!ctx->lz4k_comp_mem) | |
50 | return -ENOMEM; | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
55 | static void lz4kc_exit(struct crypto_tfm *tfm) | |
56 | { | |
57 | struct lz4k_ctx *ctx = crypto_tfm_ctx(tfm); | |
58 | ||
59 | vfree(ctx->lz4k_comp_mem); | |
60 | } | |
61 | ||
62 | static int lz4kc_compress(struct crypto_tfm *tfm, const u8 *src, | |
63 | unsigned int slen, u8 *dst, unsigned int *dlen) | |
64 | { | |
65 | ||
66 | struct lz4k_ctx *ctx = crypto_tfm_ctx(tfm); | |
67 | //static size_t in_size = 0, out_size = 0; | |
68 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ | |
69 | int err; | |
70 | ||
71 | static int count = 0; | |
72 | ||
73 | //printk("lz4k_compress 2 count = %d\r\n", count); | |
74 | ||
75 | count++; | |
76 | ||
77 | ||
78 | err = lz4k_compress(src, slen, dst, &tmp_len, ctx->lz4k_comp_mem); | |
79 | //err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx->lz4k_comp_mem); | |
80 | ||
81 | ||
82 | if (err != LZO_E_OK) | |
83 | return -EINVAL; | |
84 | #if 0 | |
85 | if (count%10 == 0) | |
86 | { | |
87 | in_size += slen; | |
88 | out_size += tmp_len; | |
89 | printk("lz4k_compress_ubifs result in_size = %d, out_size = %d \n", in_size,out_size); | |
90 | } | |
91 | #endif | |
92 | //printk("lz4k_compress result in_size = %d, out_size = %d \n", in_size,out_size); | |
93 | //printk("lz4k_compress result slen = %d, tmp_len = %d \n", slen,tmp_len); | |
94 | ||
95 | *dlen = tmp_len; | |
96 | return 0; | |
97 | ||
98 | } | |
99 | ||
100 | static int lz4kc_decompress(struct crypto_tfm *tfm, const u8 *src, | |
101 | unsigned int slen, u8 *dst, unsigned int *dlen) | |
102 | { | |
103 | static int count = 0; | |
104 | int err; | |
105 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ | |
106 | ||
107 | ||
108 | //printk("lz4k_decompress 2count = %d, *dlen = %d", count,*dlen); | |
109 | ||
110 | err = lz4k_decompress_ubifs(src, slen, dst, &tmp_len); | |
111 | //err = lzo1x_decompress_safe(src, slen, dst, &tmp_len); | |
112 | ||
113 | ||
114 | count++; | |
115 | ||
116 | if (err != LZO_E_OK) | |
117 | return -EINVAL; | |
118 | ||
119 | *dlen = tmp_len; | |
120 | return 0; | |
121 | ||
122 | } | |
123 | ||
124 | static struct crypto_alg alg = { | |
125 | .cra_name = "lz4k", | |
126 | .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, | |
127 | .cra_ctxsize = sizeof(struct lz4k_ctx), | |
128 | .cra_module = THIS_MODULE, | |
129 | .cra_list = LIST_HEAD_INIT(alg.cra_list), | |
130 | .cra_init = lz4kc_init, | |
131 | .cra_exit = lz4kc_exit, | |
132 | .cra_u = { .compress = { | |
133 | .coa_compress = lz4kc_compress, | |
134 | .coa_decompress = lz4kc_decompress } } | |
135 | }; | |
136 | ||
137 | static int __init lz4k_mod_init(void) | |
138 | { | |
139 | return crypto_register_alg(&alg); | |
140 | } | |
141 | ||
142 | static void __exit lz4k_mod_fini(void) | |
143 | { | |
144 | crypto_unregister_alg(&alg); | |
145 | } | |
146 | ||
147 | module_init(lz4k_mod_init); | |
148 | module_exit(lz4k_mod_fini); | |
149 | ||
150 | MODULE_LICENSE("GPL"); | |
151 | MODULE_DESCRIPTION("LZ77 with 4K Compression Algorithm"); |