| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Cryptographic API. |
| 4 | */ |
| 5 | |
| 6 | #include <crypto/internal/scompress.h> |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/lzo.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/slab.h> |
| 11 | |
| 12 | static void *lzorle_alloc_ctx(void) |
| 13 | { |
| 14 | void *ctx; |
| 15 | |
| 16 | ctx = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
| 17 | if (!ctx) |
| 18 | return ERR_PTR(error: -ENOMEM); |
| 19 | |
| 20 | return ctx; |
| 21 | } |
| 22 | |
| 23 | static void lzorle_free_ctx(void *ctx) |
| 24 | { |
| 25 | kvfree(addr: ctx); |
| 26 | } |
| 27 | |
| 28 | static int __lzorle_compress(const u8 *src, unsigned int slen, |
| 29 | u8 *dst, unsigned int *dlen, void *ctx) |
| 30 | { |
| 31 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ |
| 32 | int err; |
| 33 | |
| 34 | err = lzorle1x_1_compress_safe(src, src_len: slen, dst, dst_len: &tmp_len, wrkmem: ctx); |
| 35 | |
| 36 | if (err != LZO_E_OK) |
| 37 | return -EINVAL; |
| 38 | |
| 39 | *dlen = tmp_len; |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int lzorle_scompress(struct crypto_scomp *tfm, const u8 *src, |
| 44 | unsigned int slen, u8 *dst, unsigned int *dlen, |
| 45 | void *ctx) |
| 46 | { |
| 47 | return __lzorle_compress(src, slen, dst, dlen, ctx); |
| 48 | } |
| 49 | |
| 50 | static int __lzorle_decompress(const u8 *src, unsigned int slen, |
| 51 | u8 *dst, unsigned int *dlen) |
| 52 | { |
| 53 | int err; |
| 54 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ |
| 55 | |
| 56 | err = lzo1x_decompress_safe(src, src_len: slen, dst, dst_len: &tmp_len); |
| 57 | |
| 58 | if (err != LZO_E_OK) |
| 59 | return -EINVAL; |
| 60 | |
| 61 | *dlen = tmp_len; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int lzorle_sdecompress(struct crypto_scomp *tfm, const u8 *src, |
| 66 | unsigned int slen, u8 *dst, unsigned int *dlen, |
| 67 | void *ctx) |
| 68 | { |
| 69 | return __lzorle_decompress(src, slen, dst, dlen); |
| 70 | } |
| 71 | |
| 72 | static struct scomp_alg scomp = { |
| 73 | .streams = { |
| 74 | .alloc_ctx = lzorle_alloc_ctx, |
| 75 | .free_ctx = lzorle_free_ctx, |
| 76 | }, |
| 77 | .compress = lzorle_scompress, |
| 78 | .decompress = lzorle_sdecompress, |
| 79 | .base = { |
| 80 | .cra_name = "lzo-rle" , |
| 81 | .cra_driver_name = "lzo-rle-scomp" , |
| 82 | .cra_module = THIS_MODULE, |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | static int __init lzorle_mod_init(void) |
| 87 | { |
| 88 | return crypto_register_scomp(alg: &scomp); |
| 89 | } |
| 90 | |
| 91 | static void __exit lzorle_mod_fini(void) |
| 92 | { |
| 93 | crypto_unregister_scomp(alg: &scomp); |
| 94 | } |
| 95 | |
| 96 | module_init(lzorle_mod_init); |
| 97 | module_exit(lzorle_mod_fini); |
| 98 | |
| 99 | MODULE_LICENSE("GPL" ); |
| 100 | MODULE_DESCRIPTION("LZO-RLE Compression Algorithm" ); |
| 101 | MODULE_ALIAS_CRYPTO("lzo-rle" ); |
| 102 | |