lzo.c 2.6 KB

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