lz4kc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/crypto.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/lzo.h>
  19. #include <linux/lz4k.h>
  20. #define malloc(a) kmalloc(a, GFP_KERNEL)
  21. #define free(a) kfree(a)
  22. struct lz4k_ctx {
  23. void *lz4k_comp_mem;
  24. };
  25. static int lz4kc_init(struct crypto_tfm *tfm)
  26. {
  27. struct lz4k_ctx *ctx = crypto_tfm_ctx(tfm);
  28. ctx->lz4k_comp_mem = vmalloc(LZO1X_MEM_COMPRESS);
  29. if (!ctx->lz4k_comp_mem)
  30. return -ENOMEM;
  31. return 0;
  32. }
  33. static void lz4kc_exit(struct crypto_tfm *tfm)
  34. {
  35. struct lz4k_ctx *ctx = crypto_tfm_ctx(tfm);
  36. vfree(ctx->lz4k_comp_mem);
  37. }
  38. static int lz4kc_compress(struct crypto_tfm *tfm, const u8 *src,
  39. unsigned int slen, u8 *dst, unsigned int *dlen)
  40. {
  41. struct lz4k_ctx *ctx = crypto_tfm_ctx(tfm);
  42. /* static size_t in_size = 0, out_size = 0; */
  43. size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  44. int err;
  45. static int count; /*= 0*/
  46. /* printk("lz4k_compress 2 count = %d\r\n", count); */
  47. count++;
  48. err = lz4k_compress(src, slen, dst, &tmp_len, ctx->lz4k_comp_mem);
  49. /* err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx->lz4k_comp_mem); */
  50. if (err != LZO_E_OK)
  51. return -EINVAL;
  52. #if 0
  53. if (count%10 == 0) {
  54. in_size += slen;
  55. out_size += tmp_len;
  56. /* printk("lz4k_compress_ubifs result in_size = %d, out_size = %d\n", in_size, out_size); */
  57. }
  58. #endif
  59. /* printk("lz4k_compress result in_size = %d, out_size = %d\n", in_size, out_size); */
  60. /* printk("lz4k_compress result slen = %d, tmp_len = %d\n", slen, tmp_len); */
  61. *dlen = tmp_len;
  62. return 0;
  63. }
  64. static int lz4kc_decompress(struct crypto_tfm *tfm, const u8 *src,
  65. unsigned int slen, u8 *dst, unsigned int *dlen)
  66. {
  67. static int count; /* = 0 */
  68. int err;
  69. size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  70. /* printk("lz4k_decompress 2count = %d, *dlen = %d", count, *dlen); */
  71. err = lz4k_decompress_ubifs(src, slen, dst, &tmp_len);
  72. /* err = lzo1x_decompress_safe(src, slen, dst, &tmp_len); */
  73. count++;
  74. if (err != LZO_E_OK)
  75. return -EINVAL;
  76. *dlen = tmp_len;
  77. return 0;
  78. }
  79. static struct crypto_alg alg = {
  80. .cra_name = "lz4k",
  81. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  82. .cra_ctxsize = sizeof(struct lz4k_ctx),
  83. .cra_module = THIS_MODULE,
  84. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  85. .cra_init = lz4kc_init,
  86. .cra_exit = lz4kc_exit,
  87. .cra_u = { .compress = {
  88. .coa_compress = lz4kc_compress,
  89. .coa_decompress = lz4kc_decompress }
  90. }
  91. };
  92. static int __init lz4k_mod_init(void)
  93. {
  94. return crypto_register_alg(&alg);
  95. }
  96. static void __exit lz4k_mod_fini(void)
  97. {
  98. crypto_unregister_alg(&alg);
  99. }
  100. module_init(lz4k_mod_init);
  101. module_exit(lz4k_mod_fini);
  102. MODULE_LICENSE("GPL");
  103. MODULE_DESCRIPTION("LZ77 with 4K Compression Algorithm");