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.
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
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
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/crypto.h>
22 #include <linux/vmalloc.h>
24 #include <linux/lzo.h>
30 static int lzo_init(struct crypto_tfm
*tfm
)
32 struct lzo_ctx
*ctx
= crypto_tfm_ctx(tfm
);
34 ctx
->lzo_comp_mem
= kmalloc(LZO1X_MEM_COMPRESS
,
35 GFP_KERNEL
| __GFP_NOWARN
| __GFP_REPEAT
);
36 if (!ctx
->lzo_comp_mem
)
37 ctx
->lzo_comp_mem
= vmalloc(LZO1X_MEM_COMPRESS
);
38 if (!ctx
->lzo_comp_mem
)
44 static void lzo_exit(struct crypto_tfm
*tfm
)
46 struct lzo_ctx
*ctx
= crypto_tfm_ctx(tfm
);
48 kvfree(ctx
->lzo_comp_mem
);
51 static int lzo_compress(struct crypto_tfm
*tfm
, const u8
*src
,
52 unsigned int slen
, u8
*dst
, unsigned int *dlen
)
54 struct lzo_ctx
*ctx
= crypto_tfm_ctx(tfm
);
55 size_t tmp_len
= *dlen
; /* size_t(ulong) <-> uint on 64 bit */
58 err
= lzo1x_1_compress(src
, slen
, dst
, &tmp_len
, ctx
->lzo_comp_mem
);
67 static int lzo_decompress(struct crypto_tfm
*tfm
, const u8
*src
,
68 unsigned int slen
, u8
*dst
, unsigned int *dlen
)
71 size_t tmp_len
= *dlen
; /* size_t(ulong) <-> uint on 64 bit */
73 err
= lzo1x_decompress_safe(src
, slen
, dst
, &tmp_len
);
83 static struct crypto_alg alg
= {
85 .cra_flags
= CRYPTO_ALG_TYPE_COMPRESS
,
86 .cra_ctxsize
= sizeof(struct lzo_ctx
),
87 .cra_module
= THIS_MODULE
,
90 .cra_u
= { .compress
= {
91 .coa_compress
= lzo_compress
,
92 .coa_decompress
= lzo_decompress
} }
95 static int __init
lzo_mod_init(void)
97 return crypto_register_alg(&alg
);
100 static void __exit
lzo_mod_fini(void)
102 crypto_unregister_alg(&alg
);
105 module_init(lzo_mod_init
);
106 module_exit(lzo_mod_fini
);
108 MODULE_LICENSE("GPL");
109 MODULE_DESCRIPTION("LZO Compression Algorithm");