backend_deflate.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/kernel.h>
  3. #include <linux/slab.h>
  4. #include <linux/vmalloc.h>
  5. #include <linux/zlib.h>
  6. #include "backend_deflate.h"
  7. /* Use the same value as crypto API */
  8. #define DEFLATE_DEF_WINBITS 11
  9. #define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
  10. struct deflate_ctx {
  11. struct z_stream_s cctx;
  12. struct z_stream_s dctx;
  13. };
  14. static void deflate_release_params(struct zcomp_params *params)
  15. {
  16. }
  17. static int deflate_setup_params(struct zcomp_params *params)
  18. {
  19. if (params->level == ZCOMP_PARAM_NO_LEVEL)
  20. params->level = Z_DEFAULT_COMPRESSION;
  21. return 0;
  22. }
  23. static void deflate_destroy(struct zcomp_ctx *ctx)
  24. {
  25. struct deflate_ctx *zctx = ctx->context;
  26. if (!zctx)
  27. return;
  28. if (zctx->cctx.workspace) {
  29. zlib_deflateEnd(&zctx->cctx);
  30. vfree(zctx->cctx.workspace);
  31. }
  32. if (zctx->dctx.workspace) {
  33. zlib_inflateEnd(&zctx->dctx);
  34. vfree(zctx->dctx.workspace);
  35. }
  36. kfree(zctx);
  37. }
  38. static int deflate_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
  39. {
  40. struct deflate_ctx *zctx;
  41. size_t sz;
  42. int ret;
  43. zctx = kzalloc(sizeof(*zctx), GFP_KERNEL);
  44. if (!zctx)
  45. return -ENOMEM;
  46. ctx->context = zctx;
  47. sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL);
  48. zctx->cctx.workspace = vzalloc(sz);
  49. if (!zctx->cctx.workspace)
  50. goto error;
  51. ret = zlib_deflateInit2(&zctx->cctx, params->level, Z_DEFLATED,
  52. -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
  53. Z_DEFAULT_STRATEGY);
  54. if (ret != Z_OK)
  55. goto error;
  56. sz = zlib_inflate_workspacesize();
  57. zctx->dctx.workspace = vzalloc(sz);
  58. if (!zctx->dctx.workspace)
  59. goto error;
  60. ret = zlib_inflateInit2(&zctx->dctx, -DEFLATE_DEF_WINBITS);
  61. if (ret != Z_OK)
  62. goto error;
  63. return 0;
  64. error:
  65. deflate_destroy(ctx);
  66. return -EINVAL;
  67. }
  68. static int deflate_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
  69. struct zcomp_req *req)
  70. {
  71. struct deflate_ctx *zctx = ctx->context;
  72. struct z_stream_s *deflate;
  73. int ret;
  74. deflate = &zctx->cctx;
  75. ret = zlib_deflateReset(deflate);
  76. if (ret != Z_OK)
  77. return -EINVAL;
  78. deflate->next_in = (u8 *)req->src;
  79. deflate->avail_in = req->src_len;
  80. deflate->next_out = (u8 *)req->dst;
  81. deflate->avail_out = req->dst_len;
  82. ret = zlib_deflate(deflate, Z_FINISH);
  83. if (ret != Z_STREAM_END)
  84. return -EINVAL;
  85. req->dst_len = deflate->total_out;
  86. return 0;
  87. }
  88. static int deflate_decompress(struct zcomp_params *params,
  89. struct zcomp_ctx *ctx,
  90. struct zcomp_req *req)
  91. {
  92. struct deflate_ctx *zctx = ctx->context;
  93. struct z_stream_s *inflate;
  94. int ret;
  95. inflate = &zctx->dctx;
  96. ret = zlib_inflateReset(inflate);
  97. if (ret != Z_OK)
  98. return -EINVAL;
  99. inflate->next_in = (u8 *)req->src;
  100. inflate->avail_in = req->src_len;
  101. inflate->next_out = (u8 *)req->dst;
  102. inflate->avail_out = req->dst_len;
  103. ret = zlib_inflate(inflate, Z_SYNC_FLUSH);
  104. if (ret != Z_STREAM_END)
  105. return -EINVAL;
  106. return 0;
  107. }
  108. const struct zcomp_ops backend_deflate = {
  109. .compress = deflate_compress,
  110. .decompress = deflate_decompress,
  111. .create_ctx = deflate_create,
  112. .destroy_ctx = deflate_destroy,
  113. .setup_params = deflate_setup_params,
  114. .release_params = deflate_release_params,
  115. .name = "deflate",
  116. };