1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/kernel.h>
4 #include <linux/slab.h>
5 #include <linux/vmalloc.h>
6 #include <linux/zlib.h>
8 #include "backend_deflate.h"
10 /* Use the same value as crypto API */
11 #define DEFLATE_DEF_WINBITS 11
12 #define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
15 struct z_stream_s cctx
;
16 struct z_stream_s dctx
;
19 static void deflate_release_params(struct zcomp_params
*params
)
23 static int deflate_setup_params(struct zcomp_params
*params
)
25 if (params
->level
== ZCOMP_PARAM_NO_LEVEL
)
26 params
->level
= Z_DEFAULT_COMPRESSION
;
31 static void deflate_destroy(struct zcomp_ctx
*ctx
)
33 struct deflate_ctx
*zctx
= ctx
->context
;
38 if (zctx
->cctx
.workspace
) {
39 zlib_deflateEnd(&zctx
->cctx
);
40 vfree(zctx
->cctx
.workspace
);
42 if (zctx
->dctx
.workspace
) {
43 zlib_inflateEnd(&zctx
->dctx
);
44 vfree(zctx
->dctx
.workspace
);
49 static int deflate_create(struct zcomp_params
*params
, struct zcomp_ctx
*ctx
)
51 struct deflate_ctx
*zctx
;
55 zctx
= kzalloc(sizeof(*zctx
), GFP_KERNEL
);
60 sz
= zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS
, MAX_MEM_LEVEL
);
61 zctx
->cctx
.workspace
= vzalloc(sz
);
62 if (!zctx
->cctx
.workspace
)
65 ret
= zlib_deflateInit2(&zctx
->cctx
, params
->level
, Z_DEFLATED
,
66 -DEFLATE_DEF_WINBITS
, DEFLATE_DEF_MEMLEVEL
,
71 sz
= zlib_inflate_workspacesize();
72 zctx
->dctx
.workspace
= vzalloc(sz
);
73 if (!zctx
->dctx
.workspace
)
76 ret
= zlib_inflateInit2(&zctx
->dctx
, -DEFLATE_DEF_WINBITS
);
87 static int deflate_compress(struct zcomp_params
*params
, struct zcomp_ctx
*ctx
,
88 struct zcomp_req
*req
)
90 struct deflate_ctx
*zctx
= ctx
->context
;
91 struct z_stream_s
*deflate
;
94 deflate
= &zctx
->cctx
;
95 ret
= zlib_deflateReset(deflate
);
99 deflate
->next_in
= (u8
*)req
->src
;
100 deflate
->avail_in
= req
->src_len
;
101 deflate
->next_out
= (u8
*)req
->dst
;
102 deflate
->avail_out
= req
->dst_len
;
104 ret
= zlib_deflate(deflate
, Z_FINISH
);
105 if (ret
!= Z_STREAM_END
)
108 req
->dst_len
= deflate
->total_out
;
112 static int deflate_decompress(struct zcomp_params
*params
,
113 struct zcomp_ctx
*ctx
,
114 struct zcomp_req
*req
)
116 struct deflate_ctx
*zctx
= ctx
->context
;
117 struct z_stream_s
*inflate
;
120 inflate
= &zctx
->dctx
;
122 ret
= zlib_inflateReset(inflate
);
126 inflate
->next_in
= (u8
*)req
->src
;
127 inflate
->avail_in
= req
->src_len
;
128 inflate
->next_out
= (u8
*)req
->dst
;
129 inflate
->avail_out
= req
->dst_len
;
131 ret
= zlib_inflate(inflate
, Z_SYNC_FLUSH
);
132 if (ret
!= Z_STREAM_END
)
138 const 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
,