3232#include <linux/interrupt.h>
3333#include <linux/mm.h>
3434#include <linux/net.h>
35+ #include <crypto/internal/scompress.h>
3536
3637#define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION
3738#define DEFLATE_DEF_WINBITS 11
@@ -101,9 +102,8 @@ static void deflate_decomp_exit(struct deflate_ctx *ctx)
101102 vfree (ctx -> decomp_stream .workspace );
102103}
103104
104- static int deflate_init ( struct crypto_tfm * tfm )
105+ static int __deflate_init ( void * ctx )
105106{
106- struct deflate_ctx * ctx = crypto_tfm_ctx (tfm );
107107 int ret ;
108108
109109 ret = deflate_comp_init (ctx );
@@ -116,19 +116,55 @@ static int deflate_init(struct crypto_tfm *tfm)
116116 return ret ;
117117}
118118
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 )
120138{
121139 struct deflate_ctx * ctx = crypto_tfm_ctx (tfm );
122140
141+ return __deflate_init (ctx );
142+ }
143+
144+ static void __deflate_exit (void * ctx )
145+ {
123146 deflate_comp_exit (ctx );
124147 deflate_decomp_exit (ctx );
125148}
126149
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 )
129165{
130166 int ret = 0 ;
131- struct deflate_ctx * dctx = crypto_tfm_ctx ( tfm ) ;
167+ struct deflate_ctx * dctx = ctx ;
132168 struct z_stream_s * stream = & dctx -> comp_stream ;
133169
134170 ret = zlib_deflateReset (stream );
@@ -153,12 +189,27 @@ static int deflate_compress(struct crypto_tfm *tfm, const u8 *src,
153189 return ret ;
154190}
155191
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 )
158209{
159210
160211 int ret = 0 ;
161- struct deflate_ctx * dctx = crypto_tfm_ctx ( tfm ) ;
212+ struct deflate_ctx * dctx = ctx ;
162213 struct z_stream_s * stream = & dctx -> decomp_stream ;
163214
164215 ret = zlib_inflateReset (stream );
@@ -194,6 +245,21 @@ static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src,
194245 return ret ;
195246}
196247
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+
197263static struct crypto_alg alg = {
198264 .cra_name = "deflate" ,
199265 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS ,
@@ -206,14 +272,39 @@ static struct crypto_alg alg = {
206272 .coa_decompress = deflate_decompress } }
207273};
208274
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+
209287static int __init deflate_mod_init (void )
210288{
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 ;
212302}
213303
214304static void __exit deflate_mod_fini (void )
215305{
216306 crypto_unregister_alg (& alg );
307+ crypto_unregister_scomp (& scomp );
217308}
218309
219310module_init (deflate_mod_init );
0 commit comments