Blame view
crypto/lzo.c
3.93 KB
0b77abb3b
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/* * Cryptographic API. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * */ #include <linux/init.h> #include <linux/module.h> #include <linux/crypto.h> #include <linux/vmalloc.h> |
42614b058
|
23 |
#include <linux/mm.h> |
0b77abb3b
|
24 |
#include <linux/lzo.h> |
ac9d2c4b3
|
25 |
#include <crypto/internal/scompress.h> |
0b77abb3b
|
26 27 28 29 |
struct lzo_ctx { void *lzo_comp_mem; }; |
ac9d2c4b3
|
30 31 32 33 34 35 36 37 38 39 40 41 |
static void *lzo_alloc_ctx(struct crypto_scomp *tfm) { void *ctx; ctx = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL | __GFP_NOWARN); if (!ctx) ctx = vmalloc(LZO1X_MEM_COMPRESS); if (!ctx) return ERR_PTR(-ENOMEM); return ctx; } |
0b77abb3b
|
42 43 44 |
static int lzo_init(struct crypto_tfm *tfm) { struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); |
ac9d2c4b3
|
45 46 |
ctx->lzo_comp_mem = lzo_alloc_ctx(NULL); if (IS_ERR(ctx->lzo_comp_mem)) |
0b77abb3b
|
47 48 49 50 |
return -ENOMEM; return 0; } |
ac9d2c4b3
|
51 52 53 54 |
static void lzo_free_ctx(struct crypto_scomp *tfm, void *ctx) { kvfree(ctx); } |
0b77abb3b
|
55 56 57 |
static void lzo_exit(struct crypto_tfm *tfm) { struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); |
ac9d2c4b3
|
58 |
lzo_free_ctx(NULL, ctx->lzo_comp_mem); |
0b77abb3b
|
59 |
} |
ac9d2c4b3
|
60 61 |
static int __lzo_compress(const u8 *src, unsigned int slen, u8 *dst, unsigned int *dlen, void *ctx) |
0b77abb3b
|
62 |
{ |
0b77abb3b
|
63 64 |
size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ int err; |
ac9d2c4b3
|
65 |
err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx); |
0b77abb3b
|
66 67 68 69 70 71 72 |
if (err != LZO_E_OK) return -EINVAL; *dlen = tmp_len; return 0; } |
ac9d2c4b3
|
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
static int lzo_compress(struct crypto_tfm *tfm, const u8 *src, unsigned int slen, u8 *dst, unsigned int *dlen) { struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); return __lzo_compress(src, slen, dst, dlen, ctx->lzo_comp_mem); } static int lzo_scompress(struct crypto_scomp *tfm, const u8 *src, unsigned int slen, u8 *dst, unsigned int *dlen, void *ctx) { return __lzo_compress(src, slen, dst, dlen, ctx); } static int __lzo_decompress(const u8 *src, unsigned int slen, u8 *dst, unsigned int *dlen) |
0b77abb3b
|
90 91 92 93 94 95 96 97 98 99 100 |
{ int err; size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ err = lzo1x_decompress_safe(src, slen, dst, &tmp_len); if (err != LZO_E_OK) return -EINVAL; *dlen = tmp_len; return 0; |
ac9d2c4b3
|
101 |
} |
0b77abb3b
|
102 |
|
ac9d2c4b3
|
103 104 105 106 107 108 109 110 111 112 113 |
static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src, unsigned int slen, u8 *dst, unsigned int *dlen) { return __lzo_decompress(src, slen, dst, dlen); } static int lzo_sdecompress(struct crypto_scomp *tfm, const u8 *src, unsigned int slen, u8 *dst, unsigned int *dlen, void *ctx) { return __lzo_decompress(src, slen, dst, dlen); |
0b77abb3b
|
114 115 116 117 118 119 120 |
} static struct crypto_alg alg = { .cra_name = "lzo", .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, .cra_ctxsize = sizeof(struct lzo_ctx), .cra_module = THIS_MODULE, |
0b77abb3b
|
121 122 123 |
.cra_init = lzo_init, .cra_exit = lzo_exit, .cra_u = { .compress = { |
ac9d2c4b3
|
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
.coa_compress = lzo_compress, .coa_decompress = lzo_decompress } } }; static struct scomp_alg scomp = { .alloc_ctx = lzo_alloc_ctx, .free_ctx = lzo_free_ctx, .compress = lzo_scompress, .decompress = lzo_sdecompress, .base = { .cra_name = "lzo", .cra_driver_name = "lzo-scomp", .cra_module = THIS_MODULE, } |
0b77abb3b
|
138 |
}; |
3af5b90bd
|
139 |
static int __init lzo_mod_init(void) |
0b77abb3b
|
140 |
{ |
ac9d2c4b3
|
141 142 143 144 145 146 147 148 149 150 151 152 153 |
int ret; ret = crypto_register_alg(&alg); if (ret) return ret; ret = crypto_register_scomp(&scomp); if (ret) { crypto_unregister_alg(&alg); return ret; } return ret; |
0b77abb3b
|
154 |
} |
3af5b90bd
|
155 |
static void __exit lzo_mod_fini(void) |
0b77abb3b
|
156 157 |
{ crypto_unregister_alg(&alg); |
ac9d2c4b3
|
158 |
crypto_unregister_scomp(&scomp); |
0b77abb3b
|
159 |
} |
3af5b90bd
|
160 161 |
module_init(lzo_mod_init); module_exit(lzo_mod_fini); |
0b77abb3b
|
162 163 164 |
MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("LZO Compression Algorithm"); |
5d26a105b
|
165 |
MODULE_ALIAS_CRYPTO("lzo"); |