32
32
#include <linux/interrupt.h>
33
33
#include <linux/mm.h>
34
34
#include <linux/net.h>
35
+ #include <crypto/internal/scompress.h>
35
36
36
37
#define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION
37
38
#define DEFLATE_DEF_WINBITS 11
@@ -101,9 +102,8 @@ static void deflate_decomp_exit(struct deflate_ctx *ctx)
101
102
vfree (ctx -> decomp_stream .workspace );
102
103
}
103
104
104
- static int deflate_init ( struct crypto_tfm * tfm )
105
+ static int __deflate_init ( void * ctx )
105
106
{
106
- struct deflate_ctx * ctx = crypto_tfm_ctx (tfm );
107
107
int ret ;
108
108
109
109
ret = deflate_comp_init (ctx );
@@ -116,19 +116,55 @@ static int deflate_init(struct crypto_tfm *tfm)
116
116
return ret ;
117
117
}
118
118
119
- static void deflate_exit (struct crypto_tfm * tfm )
119
+ static void * deflate_alloc_ctx (struct crypto_scomp * tfm )
120
+ {
121
+ struct deflate_ctx * ctx ;
122
+ int ret ;
123
+
124
+ ctx = kzalloc (sizeof (* ctx ), GFP_KERNEL );
125
+ if (!ctx )
126
+ return ERR_PTR (- ENOMEM );
127
+
128
+ ret = __deflate_init (ctx );
129
+ if (ret ) {
130
+ kfree (ctx );
131
+ return ERR_PTR (ret );
132
+ }
133
+
134
+ return ctx ;
135
+ }
136
+
137
+ static int deflate_init (struct crypto_tfm * tfm )
120
138
{
121
139
struct deflate_ctx * ctx = crypto_tfm_ctx (tfm );
122
140
141
+ return __deflate_init (ctx );
142
+ }
143
+
144
+ static void __deflate_exit (void * ctx )
145
+ {
123
146
deflate_comp_exit (ctx );
124
147
deflate_decomp_exit (ctx );
125
148
}
126
149
127
- static int deflate_compress (struct crypto_tfm * tfm , const u8 * src ,
128
- unsigned int slen , u8 * dst , unsigned int * dlen )
150
+ static void deflate_free_ctx (struct crypto_scomp * tfm , void * ctx )
151
+ {
152
+ __deflate_exit (ctx );
153
+ kzfree (ctx );
154
+ }
155
+
156
+ static void deflate_exit (struct crypto_tfm * tfm )
157
+ {
158
+ struct deflate_ctx * ctx = crypto_tfm_ctx (tfm );
159
+
160
+ __deflate_exit (ctx );
161
+ }
162
+
163
+ static int __deflate_compress (const u8 * src , unsigned int slen ,
164
+ u8 * dst , unsigned int * dlen , void * ctx )
129
165
{
130
166
int ret = 0 ;
131
- struct deflate_ctx * dctx = crypto_tfm_ctx ( tfm ) ;
167
+ struct deflate_ctx * dctx = ctx ;
132
168
struct z_stream_s * stream = & dctx -> comp_stream ;
133
169
134
170
ret = zlib_deflateReset (stream );
@@ -153,12 +189,27 @@ static int deflate_compress(struct crypto_tfm *tfm, const u8 *src,
153
189
return ret ;
154
190
}
155
191
156
- static int deflate_decompress (struct crypto_tfm * tfm , const u8 * src ,
157
- unsigned int slen , u8 * dst , unsigned int * dlen )
192
+ static int deflate_compress (struct crypto_tfm * tfm , const u8 * src ,
193
+ unsigned int slen , u8 * dst , unsigned int * dlen )
194
+ {
195
+ struct deflate_ctx * dctx = crypto_tfm_ctx (tfm );
196
+
197
+ return __deflate_compress (src , slen , dst , dlen , dctx );
198
+ }
199
+
200
+ static int deflate_scompress (struct crypto_scomp * tfm , const u8 * src ,
201
+ unsigned int slen , u8 * dst , unsigned int * dlen ,
202
+ void * ctx )
203
+ {
204
+ return __deflate_compress (src , slen , dst , dlen , ctx );
205
+ }
206
+
207
+ static int __deflate_decompress (const u8 * src , unsigned int slen ,
208
+ u8 * dst , unsigned int * dlen , void * ctx )
158
209
{
159
210
160
211
int ret = 0 ;
161
- struct deflate_ctx * dctx = crypto_tfm_ctx ( tfm ) ;
212
+ struct deflate_ctx * dctx = ctx ;
162
213
struct z_stream_s * stream = & dctx -> decomp_stream ;
163
214
164
215
ret = zlib_inflateReset (stream );
@@ -194,6 +245,21 @@ static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src,
194
245
return ret ;
195
246
}
196
247
248
+ static int deflate_decompress (struct crypto_tfm * tfm , const u8 * src ,
249
+ unsigned int slen , u8 * dst , unsigned int * dlen )
250
+ {
251
+ struct deflate_ctx * dctx = crypto_tfm_ctx (tfm );
252
+
253
+ return __deflate_decompress (src , slen , dst , dlen , dctx );
254
+ }
255
+
256
+ static int deflate_sdecompress (struct crypto_scomp * tfm , const u8 * src ,
257
+ unsigned int slen , u8 * dst , unsigned int * dlen ,
258
+ void * ctx )
259
+ {
260
+ return __deflate_decompress (src , slen , dst , dlen , ctx );
261
+ }
262
+
197
263
static struct crypto_alg alg = {
198
264
.cra_name = "deflate" ,
199
265
.cra_flags = CRYPTO_ALG_TYPE_COMPRESS ,
@@ -206,14 +272,39 @@ static struct crypto_alg alg = {
206
272
.coa_decompress = deflate_decompress } }
207
273
};
208
274
275
+ static struct scomp_alg scomp = {
276
+ .alloc_ctx = deflate_alloc_ctx ,
277
+ .free_ctx = deflate_free_ctx ,
278
+ .compress = deflate_scompress ,
279
+ .decompress = deflate_sdecompress ,
280
+ .base = {
281
+ .cra_name = "deflate" ,
282
+ .cra_driver_name = "deflate-scomp" ,
283
+ .cra_module = THIS_MODULE ,
284
+ }
285
+ };
286
+
209
287
static int __init deflate_mod_init (void )
210
288
{
211
- return crypto_register_alg (& alg );
289
+ int ret ;
290
+
291
+ ret = crypto_register_alg (& alg );
292
+ if (ret )
293
+ return ret ;
294
+
295
+ ret = crypto_register_scomp (& scomp );
296
+ if (ret ) {
297
+ crypto_unregister_alg (& alg );
298
+ return ret ;
299
+ }
300
+
301
+ return ret ;
212
302
}
213
303
214
304
static void __exit deflate_mod_fini (void )
215
305
{
216
306
crypto_unregister_alg (& alg );
307
+ crypto_unregister_scomp (& scomp );
217
308
}
218
309
219
310
module_init (deflate_mod_init );
0 commit comments