backend_deflate.c (52c7b4e2ba508a924c991e681db534e66a851adf) backend_deflate.c (6a81bdfeb35094c3097650306a5fda9a990d8a97)
1// SPDX-License-Identifier: GPL-2.0-or-later
2
3#include <linux/kernel.h>
4#include <linux/slab.h>
5#include <linux/vmalloc.h>
6#include <linux/zlib.h>
7
8#include "backend_deflate.h"
9
10/* Use the same value as crypto API */
11#define DEFLATE_DEF_WINBITS 11
12#define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
13
14struct deflate_ctx {
15 struct z_stream_s cctx;
16 struct z_stream_s dctx;
17 s32 level;
18};
19
1// SPDX-License-Identifier: GPL-2.0-or-later
2
3#include <linux/kernel.h>
4#include <linux/slab.h>
5#include <linux/vmalloc.h>
6#include <linux/zlib.h>
7
8#include "backend_deflate.h"
9
10/* Use the same value as crypto API */
11#define DEFLATE_DEF_WINBITS 11
12#define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
13
14struct deflate_ctx {
15 struct z_stream_s cctx;
16 struct z_stream_s dctx;
17 s32 level;
18};
19
20static void deflate_destroy(void *ctx)
20static void deflate_destroy(struct zcomp_ctx *ctx)
21{
21{
22 struct deflate_ctx *zctx = ctx;
22 struct deflate_ctx *zctx = ctx->context;
23
23
24 if (!zctx)
25 return;
26
24 if (zctx->cctx.workspace) {
25 zlib_deflateEnd(&zctx->cctx);
26 vfree(zctx->cctx.workspace);
27 }
28 if (zctx->dctx.workspace) {
29 zlib_inflateEnd(&zctx->dctx);
30 vfree(zctx->dctx.workspace);
31 }
32 kfree(zctx);
33}
34
27 if (zctx->cctx.workspace) {
28 zlib_deflateEnd(&zctx->cctx);
29 vfree(zctx->cctx.workspace);
30 }
31 if (zctx->dctx.workspace) {
32 zlib_inflateEnd(&zctx->dctx);
33 vfree(zctx->dctx.workspace);
34 }
35 kfree(zctx);
36}
37
35static void *deflate_create(struct zcomp_params *params)
38static int deflate_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
36{
39{
37 struct deflate_ctx *ctx;
40 struct deflate_ctx *zctx;
38 size_t sz;
39 int ret;
40
41 size_t sz;
42 int ret;
43
41 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
42 if (!ctx)
43 return NULL;
44 zctx = kzalloc(sizeof(*zctx), GFP_KERNEL);
45 if (!zctx)
46 return -ENOMEM;
44
47
48 ctx->context = zctx;
45 if (params->level != ZCOMP_PARAM_NO_LEVEL)
49 if (params->level != ZCOMP_PARAM_NO_LEVEL)
46 ctx->level = params->level;
50 zctx->level = params->level;
47 else
51 else
48 ctx->level = Z_DEFAULT_COMPRESSION;
52 zctx->level = Z_DEFAULT_COMPRESSION;
49
50 sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL);
53
54 sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL);
51 ctx->cctx.workspace = vzalloc(sz);
52 if (!ctx->cctx.workspace)
55 zctx->cctx.workspace = vzalloc(sz);
56 if (!zctx->cctx.workspace)
53 goto error;
54
57 goto error;
58
55 ret = zlib_deflateInit2(&ctx->cctx, ctx->level, Z_DEFLATED,
59 ret = zlib_deflateInit2(&zctx->cctx, zctx->level, Z_DEFLATED,
56 -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
57 Z_DEFAULT_STRATEGY);
58 if (ret != Z_OK)
59 goto error;
60
61 sz = zlib_inflate_workspacesize();
60 -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
61 Z_DEFAULT_STRATEGY);
62 if (ret != Z_OK)
63 goto error;
64
65 sz = zlib_inflate_workspacesize();
62 ctx->dctx.workspace = vzalloc(sz);
63 if (!ctx->dctx.workspace)
66 zctx->dctx.workspace = vzalloc(sz);
67 if (!zctx->dctx.workspace)
64 goto error;
65
68 goto error;
69
66 ret = zlib_inflateInit2(&ctx->dctx, -DEFLATE_DEF_WINBITS);
70 ret = zlib_inflateInit2(&zctx->dctx, -DEFLATE_DEF_WINBITS);
67 if (ret != Z_OK)
68 goto error;
69
71 if (ret != Z_OK)
72 goto error;
73
70 return ctx;
74 return 0;
71
72error:
73 deflate_destroy(ctx);
75
76error:
77 deflate_destroy(ctx);
74 return NULL;
78 return -EINVAL;
75}
76
79}
80
77static int deflate_compress(void *ctx, struct zcomp_req *req)
81static int deflate_compress(struct zcomp_ctx *ctx, struct zcomp_req *req)
78{
82{
79 struct deflate_ctx *zctx = ctx;
83 struct deflate_ctx *zctx = ctx->context;
80 struct z_stream_s *deflate;
81 int ret;
82
83 deflate = &zctx->cctx;
84 ret = zlib_deflateReset(deflate);
85 if (ret != Z_OK)
86 return -EINVAL;
87

--- 5 unchanged lines hidden (view full) ---

93 ret = zlib_deflate(deflate, Z_FINISH);
94 if (ret != Z_STREAM_END)
95 return -EINVAL;
96
97 req->dst_len = deflate->total_out;
98 return 0;
99}
100
84 struct z_stream_s *deflate;
85 int ret;
86
87 deflate = &zctx->cctx;
88 ret = zlib_deflateReset(deflate);
89 if (ret != Z_OK)
90 return -EINVAL;
91

--- 5 unchanged lines hidden (view full) ---

97 ret = zlib_deflate(deflate, Z_FINISH);
98 if (ret != Z_STREAM_END)
99 return -EINVAL;
100
101 req->dst_len = deflate->total_out;
102 return 0;
103}
104
101static int deflate_decompress(void *ctx, struct zcomp_req *req)
105static int deflate_decompress(struct zcomp_ctx *ctx, struct zcomp_req *req)
102{
106{
103 struct deflate_ctx *zctx = ctx;
107 struct deflate_ctx *zctx = ctx->context;
104 struct z_stream_s *inflate;
105 int ret;
106
107 inflate = &zctx->dctx;
108
109 ret = zlib_inflateReset(inflate);
110 if (ret != Z_OK)
111 return -EINVAL;

--- 20 unchanged lines hidden ---
108 struct z_stream_s *inflate;
109 int ret;
110
111 inflate = &zctx->dctx;
112
113 ret = zlib_inflateReset(inflate);
114 if (ret != Z_OK)
115 return -EINVAL;

--- 20 unchanged lines hidden ---