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>
25 #include <crypto/internal/scompress.h>
31 static void *lzo_alloc_ctx(struct crypto_scomp
*tfm
)
35 ctx
= kmalloc(LZO1X_MEM_COMPRESS
, GFP_KERNEL
| __GFP_NOWARN
);
37 ctx
= vmalloc(LZO1X_MEM_COMPRESS
);
39 return ERR_PTR(-ENOMEM
);
44 static int lzo_init(struct crypto_tfm
*tfm
)
46 struct lzo_ctx
*ctx
= crypto_tfm_ctx(tfm
);
48 ctx
->lzo_comp_mem
= lzo_alloc_ctx(NULL
);
49 if (IS_ERR(ctx
->lzo_comp_mem
))
55 static void lzo_free_ctx(struct crypto_scomp
*tfm
, void *ctx
)
60 static void lzo_exit(struct crypto_tfm
*tfm
)
62 struct lzo_ctx
*ctx
= crypto_tfm_ctx(tfm
);
64 lzo_free_ctx(NULL
, ctx
->lzo_comp_mem
);
67 static int __lzo_compress(const u8
*src
, unsigned int slen
,
68 u8
*dst
, unsigned int *dlen
, void *ctx
)
70 size_t tmp_len
= *dlen
; /* size_t(ulong) <-> uint on 64 bit */
73 err
= lzo1x_1_compress(src
, slen
, dst
, &tmp_len
, ctx
);
82 static int lzo_compress(struct crypto_tfm
*tfm
, const u8
*src
,
83 unsigned int slen
, u8
*dst
, unsigned int *dlen
)
85 struct lzo_ctx
*ctx
= crypto_tfm_ctx(tfm
);
87 return __lzo_compress(src
, slen
, dst
, dlen
, ctx
->lzo_comp_mem
);
90 static int lzo_scompress(struct crypto_scomp
*tfm
, const u8
*src
,
91 unsigned int slen
, u8
*dst
, unsigned int *dlen
,
94 return __lzo_compress(src
, slen
, dst
, dlen
, ctx
);
97 static int __lzo_decompress(const u8
*src
, unsigned int slen
,
98 u8
*dst
, unsigned int *dlen
)
101 size_t tmp_len
= *dlen
; /* size_t(ulong) <-> uint on 64 bit */
103 err
= lzo1x_decompress_safe(src
, slen
, dst
, &tmp_len
);
112 static int lzo_decompress(struct crypto_tfm
*tfm
, const u8
*src
,
113 unsigned int slen
, u8
*dst
, unsigned int *dlen
)
115 return __lzo_decompress(src
, slen
, dst
, dlen
);
118 static int lzo_sdecompress(struct crypto_scomp
*tfm
, const u8
*src
,
119 unsigned int slen
, u8
*dst
, unsigned int *dlen
,
122 return __lzo_decompress(src
, slen
, dst
, dlen
);
125 static struct crypto_alg alg
= {
127 .cra_flags
= CRYPTO_ALG_TYPE_COMPRESS
,
128 .cra_ctxsize
= sizeof(struct lzo_ctx
),
129 .cra_module
= THIS_MODULE
,
130 .cra_init
= lzo_init
,
131 .cra_exit
= lzo_exit
,
132 .cra_u
= { .compress
= {
133 .coa_compress
= lzo_compress
,
134 .coa_decompress
= lzo_decompress
} }
137 static struct scomp_alg scomp
= {
138 .alloc_ctx
= lzo_alloc_ctx
,
139 .free_ctx
= lzo_free_ctx
,
140 .compress
= lzo_scompress
,
141 .decompress
= lzo_sdecompress
,
144 .cra_driver_name
= "lzo-scomp",
145 .cra_module
= THIS_MODULE
,
149 static int __init
lzo_mod_init(void)
153 ret
= crypto_register_alg(&alg
);
157 ret
= crypto_register_scomp(&scomp
);
159 crypto_unregister_alg(&alg
);
166 static void __exit
lzo_mod_fini(void)
168 crypto_unregister_alg(&alg
);
169 crypto_unregister_scomp(&scomp
);
172 module_init(lzo_mod_init
);
173 module_exit(lzo_mod_fini
);
175 MODULE_LICENSE("GPL");
176 MODULE_DESCRIPTION("LZO Compression Algorithm");
177 MODULE_ALIAS_CRYPTO("lzo");