1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/crypto.h>
30#include <linux/zlib.h>
31#include <linux/vmalloc.h>
32#include <linux/interrupt.h>
33#include <linux/mm.h>
34#include <linux/net.h>
35#include <linux/slab.h>
36
37#define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION
38#define DEFLATE_DEF_WINBITS 11
39#define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
40
41struct deflate_ctx {
42 int comp_initialized;
43 int decomp_initialized;
44 struct z_stream_s comp_stream;
45 struct z_stream_s decomp_stream;
46};
47
48static inline int deflate_gfp(void)
49{
50 return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
51}
52
53static int deflate_init(void *ctx)
54{
55 return 0;
56}
57
58static void deflate_exit(void *ctx)
59{
60 struct deflate_ctx *dctx = ctx;
61
62 if (dctx->comp_initialized)
63 vfree(dctx->comp_stream.workspace);
64 if (dctx->decomp_initialized)
65 kfree(dctx->decomp_stream.workspace);
66}
67
68
69
70
71
72static int deflate_comp_init(struct deflate_ctx *ctx)
73{
74 int ret = 0;
75 struct z_stream_s *stream = &ctx->comp_stream;
76
77 stream->workspace = __vmalloc(zlib_deflate_workspacesize(),
78 deflate_gfp()|__GFP_HIGHMEM,
79 PAGE_KERNEL);
80 if (!stream->workspace ) {
81 ret = -ENOMEM;
82 goto out;
83 }
84 memset(stream->workspace, 0, zlib_deflate_workspacesize());
85 ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
86 -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
87 Z_DEFAULT_STRATEGY);
88 if (ret != Z_OK) {
89 ret = -EINVAL;
90 goto out_free;
91 }
92 ctx->comp_initialized = 1;
93out:
94 return ret;
95out_free:
96 vfree(stream->workspace);
97 goto out;
98}
99
100static int deflate_decomp_init(struct deflate_ctx *ctx)
101{
102 int ret = 0;
103 struct z_stream_s *stream = &ctx->decomp_stream;
104
105 stream->workspace = kmalloc(zlib_inflate_workspacesize(),
106 deflate_gfp());
107 if (!stream->workspace ) {
108 ret = -ENOMEM;
109 goto out;
110 }
111 memset(stream->workspace, 0, zlib_inflate_workspacesize());
112 ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
113 if (ret != Z_OK) {
114 ret = -EINVAL;
115 goto out_free;
116 }
117 ctx->decomp_initialized = 1;
118out:
119 return ret;
120out_free:
121 kfree(stream->workspace);
122 goto out;
123}
124
125static int deflate_compress(void *ctx, const u8 *src, unsigned int slen,
126 u8 *dst, unsigned int *dlen)
127{
128 int ret = 0;
129 struct deflate_ctx *dctx = ctx;
130 struct z_stream_s *stream = &dctx->comp_stream;
131
132 if (!dctx->comp_initialized) {
133 ret = deflate_comp_init(dctx);
134 if (ret)
135 goto out;
136 }
137
138 ret = zlib_deflateReset(stream);
139 if (ret != Z_OK) {
140 ret = -EINVAL;
141 goto out;
142 }
143
144 stream->next_in = (u8 *)src;
145 stream->avail_in = slen;
146 stream->next_out = (u8 *)dst;
147 stream->avail_out = *dlen;
148
149 ret = zlib_deflate(stream, Z_FINISH);
150 if (ret != Z_STREAM_END) {
151 ret = -EINVAL;
152 goto out;
153 }
154 ret = 0;
155 *dlen = stream->total_out;
156out:
157 return ret;
158}
159
160static int deflate_decompress(void *ctx, const u8 *src, unsigned int slen,
161 u8 *dst, unsigned int *dlen)
162{
163
164 int ret = 0;
165 struct deflate_ctx *dctx = ctx;
166 struct z_stream_s *stream = &dctx->decomp_stream;
167
168 if (!dctx->decomp_initialized) {
169 ret = deflate_decomp_init(dctx);
170 if (ret)
171 goto out;
172 }
173
174 ret = zlib_inflateReset(stream);
175 if (ret != Z_OK) {
176 ret = -EINVAL;
177 goto out;
178 }
179
180 stream->next_in = (u8 *)src;
181 stream->avail_in = slen;
182 stream->next_out = (u8 *)dst;
183 stream->avail_out = *dlen;
184
185 ret = zlib_inflate(stream, Z_SYNC_FLUSH);
186
187
188
189
190
191 if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
192 u8 zerostuff = 0;
193 stream->next_in = &zerostuff;
194 stream->avail_in = 1;
195 ret = zlib_inflate(stream, Z_FINISH);
196 }
197 if (ret != Z_STREAM_END) {
198 ret = -EINVAL;
199 goto out;
200 }
201 ret = 0;
202 *dlen = stream->total_out;
203out:
204 return ret;
205}
206
207static struct crypto_alg alg = {
208 .cra_name = "deflate",
209 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
210 .cra_ctxsize = sizeof(struct deflate_ctx),
211 .cra_module = THIS_MODULE,
212 .cra_list = LIST_HEAD_INIT(alg.cra_list),
213 .cra_u = { .compress = {
214 .coa_init = deflate_init,
215 .coa_exit = deflate_exit,
216 .coa_compress = deflate_compress,
217 .coa_decompress = deflate_decompress } }
218};
219
220static int __init init(void)
221{
222 return crypto_register_alg(&alg);
223}
224
225static void __exit fini(void)
226{
227 crypto_unregister_alg(&alg);
228}
229
230module_init(init);
231module_exit(fini);
232
233MODULE_LICENSE("GPL");
234MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
235MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
236
237