backend_deflate.c (6a81bdfeb35094c3097650306a5fda9a990d8a97) backend_deflate.c (b8f03cb703a160e14f87a467a4443adc5d940087)
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;
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
17};
18
19static void deflate_release_params(struct zcomp_params *params)
20{
21}
22
23static int deflate_setup_params(struct zcomp_params *params)
24{
25 if (params->level == ZCOMP_PARAM_NO_LEVEL)
26 params->level = Z_DEFAULT_COMPRESSION;
27
28 return 0;
29}
30
20static void deflate_destroy(struct zcomp_ctx *ctx)
21{
22 struct deflate_ctx *zctx = ctx->context;
23
24 if (!zctx)
25 return;
26
27 if (zctx->cctx.workspace) {

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

41 size_t sz;
42 int ret;
43
44 zctx = kzalloc(sizeof(*zctx), GFP_KERNEL);
45 if (!zctx)
46 return -ENOMEM;
47
48 ctx->context = zctx;
31static void deflate_destroy(struct zcomp_ctx *ctx)
32{
33 struct deflate_ctx *zctx = ctx->context;
34
35 if (!zctx)
36 return;
37
38 if (zctx->cctx.workspace) {

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

52 size_t sz;
53 int ret;
54
55 zctx = kzalloc(sizeof(*zctx), GFP_KERNEL);
56 if (!zctx)
57 return -ENOMEM;
58
59 ctx->context = zctx;
49 if (params->level != ZCOMP_PARAM_NO_LEVEL)
50 zctx->level = params->level;
51 else
52 zctx->level = Z_DEFAULT_COMPRESSION;
53
54 sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL);
55 zctx->cctx.workspace = vzalloc(sz);
56 if (!zctx->cctx.workspace)
57 goto error;
58
60 sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL);
61 zctx->cctx.workspace = vzalloc(sz);
62 if (!zctx->cctx.workspace)
63 goto error;
64
59 ret = zlib_deflateInit2(&zctx->cctx, zctx->level, Z_DEFLATED,
65 ret = zlib_deflateInit2(&zctx->cctx, params->level, Z_DEFLATED,
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();
66 zctx->dctx.workspace = vzalloc(sz);
67 if (!zctx->dctx.workspace)

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

73
74 return 0;
75
76error:
77 deflate_destroy(ctx);
78 return -EINVAL;
79}
80
66 -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
67 Z_DEFAULT_STRATEGY);
68 if (ret != Z_OK)
69 goto error;
70
71 sz = zlib_inflate_workspacesize();
72 zctx->dctx.workspace = vzalloc(sz);
73 if (!zctx->dctx.workspace)

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

79
80 return 0;
81
82error:
83 deflate_destroy(ctx);
84 return -EINVAL;
85}
86
81static int deflate_compress(struct zcomp_ctx *ctx, struct zcomp_req *req)
87static int deflate_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
88 struct zcomp_req *req)
82{
83 struct deflate_ctx *zctx = ctx->context;
84 struct z_stream_s *deflate;
85 int ret;
86
87 deflate = &zctx->cctx;
88 ret = zlib_deflateReset(deflate);
89 if (ret != Z_OK)

--- 7 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
89{
90 struct deflate_ctx *zctx = ctx->context;
91 struct z_stream_s *deflate;
92 int ret;
93
94 deflate = &zctx->cctx;
95 ret = zlib_deflateReset(deflate);
96 if (ret != Z_OK)

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

104 ret = zlib_deflate(deflate, Z_FINISH);
105 if (ret != Z_STREAM_END)
106 return -EINVAL;
107
108 req->dst_len = deflate->total_out;
109 return 0;
110}
111
105static int deflate_decompress(struct zcomp_ctx *ctx, struct zcomp_req *req)
112static int deflate_decompress(struct zcomp_params *params,
113 struct zcomp_ctx *ctx,
114 struct zcomp_req *req)
106{
107 struct deflate_ctx *zctx = ctx->context;
108 struct z_stream_s *inflate;
109 int ret;
110
111 inflate = &zctx->dctx;
112
113 ret = zlib_inflateReset(inflate);

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

126 return 0;
127}
128
129const struct zcomp_ops backend_deflate = {
130 .compress = deflate_compress,
131 .decompress = deflate_decompress,
132 .create_ctx = deflate_create,
133 .destroy_ctx = deflate_destroy,
115{
116 struct deflate_ctx *zctx = ctx->context;
117 struct z_stream_s *inflate;
118 int ret;
119
120 inflate = &zctx->dctx;
121
122 ret = zlib_inflateReset(inflate);

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

135 return 0;
136}
137
138const struct zcomp_ops backend_deflate = {
139 .compress = deflate_compress,
140 .decompress = deflate_decompress,
141 .create_ctx = deflate_create,
142 .destroy_ctx = deflate_destroy,
143 .setup_params = deflate_setup_params,
144 .release_params = deflate_release_params,
134 .name = "deflate",
135};
145 .name = "deflate",
146};