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