lzo.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Cryptographic API.
  3. *
  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.
  7. *
  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
  11. * more details.
  12. *
  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
  16. *
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/crypto.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/lzo.h>
  23. struct lzo_ctx {
  24. void *lzo_comp_mem;
  25. };
  26. static int lzo_init(struct crypto_tfm *tfm)
  27. {
  28. struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
  29. ctx->lzo_comp_mem = vmalloc(LZO1X_MEM_COMPRESS);
  30. if (!ctx->lzo_comp_mem)
  31. return -ENOMEM;
  32. return 0;
  33. }
  34. static void lzo_exit(struct crypto_tfm *tfm)
  35. {
  36. struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
  37. vfree(ctx->lzo_comp_mem);
  38. }
  39. static int lzo_compress(struct crypto_tfm *tfm, const u8 *src,
  40. unsigned int slen, u8 *dst, unsigned int *dlen)
  41. {
  42. struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
  43. size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  44. int err;
  45. err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx->lzo_comp_mem);
  46. if (err != LZO_E_OK)
  47. return -EINVAL;
  48. *dlen = tmp_len;
  49. return 0;
  50. }
  51. static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src,
  52. unsigned int slen, u8 *dst, unsigned int *dlen)
  53. {
  54. int err;
  55. size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  56. err = lzo1x_decompress_safe(src, slen, dst, &tmp_len);
  57. if (err != LZO_E_OK)
  58. return -EINVAL;
  59. *dlen = tmp_len;
  60. return 0;
  61. }
  62. static struct crypto_alg alg = {
  63. .cra_name = "lzo",
  64. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  65. .cra_ctxsize = sizeof(struct lzo_ctx),
  66. .cra_module = THIS_MODULE,
  67. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  68. .cra_init = lzo_init,
  69. .cra_exit = lzo_exit,
  70. .cra_u = { .compress = {
  71. .coa_compress = lzo_compress,
  72. .coa_decompress = lzo_decompress } }
  73. };
  74. static int __init lzo_mod_init(void)
  75. {
  76. return crypto_register_alg(&alg);
  77. }
  78. static void __exit lzo_mod_fini(void)
  79. {
  80. crypto_unregister_alg(&alg);
  81. }
  82. module_init(lzo_mod_init);
  83. module_exit(lzo_mod_fini);
  84. MODULE_LICENSE("GPL");
  85. MODULE_DESCRIPTION("LZO Compression Algorithm");