Source
36
36
37
37
#define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION
38
38
#define DEFLATE_DEF_WINBITS 11
39
39
#define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
40
40
41
41
struct deflate_ctx {
42
42
struct z_stream_s comp_stream;
43
43
struct z_stream_s decomp_stream;
44
44
};
45
45
46
-
static int deflate_comp_init(struct deflate_ctx *ctx)
46
+
static int deflate_comp_init(struct deflate_ctx *ctx, int format)
47
47
{
48
48
int ret = 0;
49
49
struct z_stream_s *stream = &ctx->comp_stream;
50
50
51
51
stream->workspace = vzalloc(zlib_deflate_workspacesize(
52
-
-DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL));
52
+
MAX_WBITS, MAX_MEM_LEVEL));
53
53
if (!stream->workspace) {
54
54
ret = -ENOMEM;
55
55
goto out;
56
56
}
57
-
ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
58
-
-DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
59
-
Z_DEFAULT_STRATEGY);
57
+
if (format)
58
+
ret = zlib_deflateInit(stream, 3);
59
+
else
60
+
ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
61
+
-DEFLATE_DEF_WINBITS,
62
+
DEFLATE_DEF_MEMLEVEL,
63
+
Z_DEFAULT_STRATEGY);
60
64
if (ret != Z_OK) {
61
65
ret = -EINVAL;
62
66
goto out_free;
63
67
}
64
68
out:
65
69
return ret;
66
70
out_free:
67
71
vfree(stream->workspace);
68
72
goto out;
69
73
}
70
74
71
-
static int deflate_decomp_init(struct deflate_ctx *ctx)
75
+
static int deflate_decomp_init(struct deflate_ctx *ctx, int format)
72
76
{
73
77
int ret = 0;
74
78
struct z_stream_s *stream = &ctx->decomp_stream;
75
79
76
80
stream->workspace = vzalloc(zlib_inflate_workspacesize());
77
81
if (!stream->workspace) {
78
82
ret = -ENOMEM;
79
83
goto out;
80
84
}
81
-
ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
85
+
if (format)
86
+
ret = zlib_inflateInit(stream);
87
+
else
88
+
ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
82
89
if (ret != Z_OK) {
83
90
ret = -EINVAL;
84
91
goto out_free;
85
92
}
86
93
out:
87
94
return ret;
88
95
out_free:
89
96
vfree(stream->workspace);
90
97
goto out;
91
98
}
95
102
zlib_deflateEnd(&ctx->comp_stream);
96
103
vfree(ctx->comp_stream.workspace);
97
104
}
98
105
99
106
static void deflate_decomp_exit(struct deflate_ctx *ctx)
100
107
{
101
108
zlib_inflateEnd(&ctx->decomp_stream);
102
109
vfree(ctx->decomp_stream.workspace);
103
110
}
104
111
105
-
static int __deflate_init(void *ctx)
112
+
static int __deflate_init(void *ctx, int format)
106
113
{
107
114
int ret;
108
115
109
-
ret = deflate_comp_init(ctx);
116
+
ret = deflate_comp_init(ctx, format);
110
117
if (ret)
111
118
goto out;
112
-
ret = deflate_decomp_init(ctx);
119
+
ret = deflate_decomp_init(ctx, format);
113
120
if (ret)
114
121
deflate_comp_exit(ctx);
115
122
out:
116
123
return ret;
117
124
}
118
125
119
-
static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
126
+
static void *gen_deflate_alloc_ctx(struct crypto_scomp *tfm, int format)
120
127
{
121
128
struct deflate_ctx *ctx;
122
129
int ret;
123
130
124
131
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
125
132
if (!ctx)
126
133
return ERR_PTR(-ENOMEM);
127
134
128
-
ret = __deflate_init(ctx);
135
+
ret = __deflate_init(ctx, format);
129
136
if (ret) {
130
137
kfree(ctx);
131
138
return ERR_PTR(ret);
132
139
}
133
140
134
141
return ctx;
135
142
}
136
143
144
+
static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
145
+
{
146
+
return gen_deflate_alloc_ctx(tfm, 0);
147
+
}
148
+
149
+
static void *zlib_deflate_alloc_ctx(struct crypto_scomp *tfm)
150
+
{
151
+
return gen_deflate_alloc_ctx(tfm, 1);
152
+
}
153
+
137
154
static int deflate_init(struct crypto_tfm *tfm)
138
155
{
139
156
struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
140
157
141
-
return __deflate_init(ctx);
158
+
return __deflate_init(ctx, 0);
142
159
}
143
160
144
161
static void __deflate_exit(void *ctx)
145
162
{
146
163
deflate_comp_exit(ctx);
147
164
deflate_decomp_exit(ctx);
148
165
}
149
166
150
167
static void deflate_free_ctx(struct crypto_scomp *tfm, void *ctx)
151
168
{
265
282
.cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
266
283
.cra_ctxsize = sizeof(struct deflate_ctx),
267
284
.cra_module = THIS_MODULE,
268
285
.cra_init = deflate_init,
269
286
.cra_exit = deflate_exit,
270
287
.cra_u = { .compress = {
271
288
.coa_compress = deflate_compress,
272
289
.coa_decompress = deflate_decompress } }
273
290
};
274
291
275
-
static struct scomp_alg scomp = {
292
+
static struct scomp_alg scomp[] = { {
276
293
.alloc_ctx = deflate_alloc_ctx,
277
294
.free_ctx = deflate_free_ctx,
278
295
.compress = deflate_scompress,
279
296
.decompress = deflate_sdecompress,
280
297
.base = {
281
298
.cra_name = "deflate",
282
299
.cra_driver_name = "deflate-scomp",
283
300
.cra_module = THIS_MODULE,
284
301
}
285
-
};
302
+
}, {
303
+
.alloc_ctx = zlib_deflate_alloc_ctx,
304
+
.free_ctx = deflate_free_ctx,
305
+
.compress = deflate_scompress,
306
+
.decompress = deflate_sdecompress,
307
+
.base = {
308
+
.cra_name = "zlib-deflate",
309
+
.cra_driver_name = "zlib-deflate-scomp",
310
+
.cra_module = THIS_MODULE,
311
+
}
312
+
} };
286
313
287
314
static int __init deflate_mod_init(void)
288
315
{
289
316
int ret;
290
317
291
318
ret = crypto_register_alg(&alg);
292
319
if (ret)
293
320
return ret;
294
321
295
-
ret = crypto_register_scomp(&scomp);
322
+
ret = crypto_register_scomps(scomp, ARRAY_SIZE(scomp));
296
323
if (ret) {
297
324
crypto_unregister_alg(&alg);
298
325
return ret;
299
326
}
300
327
301
328
return ret;
302
329
}
303
330
304
331
static void __exit deflate_mod_fini(void)
305
332
{
306
333
crypto_unregister_alg(&alg);
307
-
crypto_unregister_scomp(&scomp);
334
+
crypto_unregister_scomps(scomp, ARRAY_SIZE(scomp));
308
335
}
309
336
310
337
module_init(deflate_mod_init);
311
338
module_exit(deflate_mod_fini);
312
339
313
340
MODULE_LICENSE("GPL");
314
341
MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
315
342
MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
316
343
MODULE_ALIAS_CRYPTO("deflate");