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 struct lzorle_ctx { 13 void *lzorle_comp_mem; 14 }; 15 16 static void *lzorle_alloc_ctx(void) 17 { 18 void *ctx; 19 20 ctx = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); 21 if (!ctx) 22 return ERR_PTR(-ENOMEM); 23 24 return ctx; 25 } 26 27 static void lzorle_free_ctx(void *ctx) 28 { 29 kvfree(ctx); 30 } 31 32 static int __lzorle_compress(const u8 *src, unsigned int slen, 33 u8 *dst, unsigned int *dlen, void *ctx) 34 { 35 size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ 36 int err; 37 38 err = lzorle1x_1_compress_safe(src, slen, dst, &tmp_len, ctx); 39 40 if (err != LZO_E_OK) 41 return -EINVAL; 42 43 *dlen = tmp_len; 44 return 0; 45 } 46 47 static int lzorle_scompress(struct crypto_scomp *tfm, const u8 *src, 48 unsigned int slen, u8 *dst, unsigned int *dlen, 49 void *ctx) 50 { 51 return __lzorle_compress(src, slen, dst, dlen, ctx); 52 } 53 54 static int __lzorle_decompress(const u8 *src, unsigned int slen, 55 u8 *dst, unsigned int *dlen) 56 { 57 int err; 58 size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ 59 60 err = lzo1x_decompress_safe(src, slen, dst, &tmp_len); 61 62 if (err != LZO_E_OK) 63 return -EINVAL; 64 65 *dlen = tmp_len; 66 return 0; 67 } 68 69 static int lzorle_sdecompress(struct crypto_scomp *tfm, const u8 *src, 70 unsigned int slen, u8 *dst, unsigned int *dlen, 71 void *ctx) 72 { 73 return __lzorle_decompress(src, slen, dst, dlen); 74 } 75 76 static struct scomp_alg scomp = { 77 .alloc_ctx = lzorle_alloc_ctx, 78 .free_ctx = lzorle_free_ctx, 79 .compress = lzorle_scompress, 80 .decompress = lzorle_sdecompress, 81 .base = { 82 .cra_name = "lzo-rle", 83 .cra_driver_name = "lzo-rle-scomp", 84 .cra_module = THIS_MODULE, 85 } 86 }; 87 88 static int __init lzorle_mod_init(void) 89 { 90 return crypto_register_scomp(&scomp); 91 } 92 93 static void __exit lzorle_mod_fini(void) 94 { 95 crypto_unregister_scomp(&scomp); 96 } 97 98 subsys_initcall(lzorle_mod_init); 99 module_exit(lzorle_mod_fini); 100 101 MODULE_LICENSE("GPL"); 102 MODULE_DESCRIPTION("LZO-RLE Compression Algorithm"); 103 MODULE_ALIAS_CRYPTO("lzo-rle"); 104