lzo.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Cryptographic API.
  4. */
  5. #include <crypto/internal/scompress.h>
  6. #include <linux/init.h>
  7. #include <linux/lzo.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. static void *lzo_alloc_ctx(void)
  11. {
  12. void *ctx;
  13. ctx = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
  14. if (!ctx)
  15. return ERR_PTR(-ENOMEM);
  16. return ctx;
  17. }
  18. static void lzo_free_ctx(void *ctx)
  19. {
  20. kvfree(ctx);
  21. }
  22. static int __lzo_compress(const u8 *src, unsigned int slen,
  23. u8 *dst, unsigned int *dlen, void *ctx)
  24. {
  25. size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  26. int err;
  27. err = lzo1x_1_compress_safe(src, slen, dst, &tmp_len, ctx);
  28. if (err != LZO_E_OK)
  29. return -EINVAL;
  30. *dlen = tmp_len;
  31. return 0;
  32. }
  33. static int lzo_scompress(struct crypto_scomp *tfm, const u8 *src,
  34. unsigned int slen, u8 *dst, unsigned int *dlen,
  35. void *ctx)
  36. {
  37. return __lzo_compress(src, slen, dst, dlen, ctx);
  38. }
  39. static int __lzo_decompress(const u8 *src, unsigned int slen,
  40. u8 *dst, unsigned int *dlen)
  41. {
  42. int err;
  43. size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  44. err = lzo1x_decompress_safe(src, slen, dst, &tmp_len);
  45. if (err != LZO_E_OK)
  46. return -EINVAL;
  47. *dlen = tmp_len;
  48. return 0;
  49. }
  50. static int lzo_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  51. unsigned int slen, u8 *dst, unsigned int *dlen,
  52. void *ctx)
  53. {
  54. return __lzo_decompress(src, slen, dst, dlen);
  55. }
  56. static struct scomp_alg scomp = {
  57. .streams = {
  58. .alloc_ctx = lzo_alloc_ctx,
  59. .free_ctx = lzo_free_ctx,
  60. },
  61. .compress = lzo_scompress,
  62. .decompress = lzo_sdecompress,
  63. .base = {
  64. .cra_name = "lzo",
  65. .cra_driver_name = "lzo-scomp",
  66. .cra_module = THIS_MODULE,
  67. }
  68. };
  69. static int __init lzo_mod_init(void)
  70. {
  71. return crypto_register_scomp(&scomp);
  72. }
  73. static void __exit lzo_mod_fini(void)
  74. {
  75. crypto_unregister_scomp(&scomp);
  76. }
  77. module_init(lzo_mod_init);
  78. module_exit(lzo_mod_fini);
  79. MODULE_LICENSE("GPL");
  80. MODULE_DESCRIPTION("LZO Compression Algorithm");
  81. MODULE_ALIAS_CRYPTO("lzo");