1 #include <linux/kernel.h>
3 #include <linux/slab.h>
4 #include <linux/vmalloc.h>
6 #include "backend_lz4hc.h"
11 LZ4_streamDecode_t
*dstrm
;
12 LZ4_streamHC_t
*cstrm
;
15 static void lz4hc_release_params(struct zcomp_params
*params
)
19 static int lz4hc_setup_params(struct zcomp_params
*params
)
21 if (params
->level
== ZCOMP_PARAM_NO_LEVEL
)
22 params
->level
= LZ4HC_DEFAULT_CLEVEL
;
27 static void lz4hc_destroy(struct zcomp_ctx
*ctx
)
29 struct lz4hc_ctx
*zctx
= ctx
->context
;
40 static int lz4hc_create(struct zcomp_params
*params
, struct zcomp_ctx
*ctx
)
42 struct lz4hc_ctx
*zctx
;
44 zctx
= kzalloc(sizeof(*zctx
), GFP_KERNEL
);
49 if (params
->dict_sz
== 0) {
50 zctx
->mem
= vmalloc(LZ4HC_MEM_COMPRESS
);
54 zctx
->dstrm
= kzalloc(sizeof(*zctx
->dstrm
), GFP_KERNEL
);
58 zctx
->cstrm
= kzalloc(sizeof(*zctx
->cstrm
), GFP_KERNEL
);
70 static int lz4hc_compress(struct zcomp_params
*params
, struct zcomp_ctx
*ctx
,
71 struct zcomp_req
*req
)
73 struct lz4hc_ctx
*zctx
= ctx
->context
;
77 ret
= LZ4_compress_HC(req
->src
, req
->dst
, req
->src_len
,
78 req
->dst_len
, params
->level
,
81 /* Cstrm needs to be reset */
82 LZ4_resetStreamHC(zctx
->cstrm
, params
->level
);
83 ret
= LZ4_loadDictHC(zctx
->cstrm
, params
->dict
,
85 if (ret
!= params
->dict_sz
)
87 ret
= LZ4_compress_HC_continue(zctx
->cstrm
, req
->src
, req
->dst
,
88 req
->src_len
, req
->dst_len
);
96 static int lz4hc_decompress(struct zcomp_params
*params
, struct zcomp_ctx
*ctx
,
97 struct zcomp_req
*req
)
99 struct lz4hc_ctx
*zctx
= ctx
->context
;
103 ret
= LZ4_decompress_safe(req
->src
, req
->dst
, req
->src_len
,
106 /* Dstrm needs to be reset */
107 ret
= LZ4_setStreamDecode(zctx
->dstrm
, params
->dict
,
111 ret
= LZ4_decompress_safe_continue(zctx
->dstrm
, req
->src
,
112 req
->dst
, req
->src_len
,
120 const struct zcomp_ops backend_lz4hc
= {
121 .compress
= lz4hc_compress
,
122 .decompress
= lz4hc_decompress
,
123 .create_ctx
= lz4hc_create
,
124 .destroy_ctx
= lz4hc_destroy
,
125 .setup_params
= lz4hc_setup_params
,
126 .release_params
= lz4hc_release_params
,