1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * erofs-utils/lib/compressor-lz4hc.c
4  *
5  * Copyright (C) 2018-2019 HUAWEI, Inc.
6  *             http://www.huawei.com/
7  * Created by Gao Xiang <gaoxiang25@huawei.com>
8  */
9 #define LZ4_HC_STATIC_LINKING_ONLY (1)
10 #include <lz4hc.h>
11 #include "erofs/internal.h"
12 #include "compressor.h"
13 
lz4hc_compress_destsize(struct erofs_compress * c,int compression_level,void * src,unsigned int * srcsize,void * dst,unsigned int dstsize)14 static int lz4hc_compress_destsize(struct erofs_compress *c,
15 				   int compression_level,
16 				   void *src,
17 				   unsigned int *srcsize,
18 				   void *dst,
19 				   unsigned int dstsize)
20 {
21 	int srcSize = (int)*srcsize;
22 	int rc = LZ4_compress_HC_destSize(c->private_data, src, dst,
23 					  &srcSize, (int)dstsize,
24 					  compression_level);
25 	if (!rc)
26 		return -EFAULT;
27 	*srcsize = srcSize;
28 	return rc;
29 }
30 
compressor_lz4hc_exit(struct erofs_compress * c)31 static int compressor_lz4hc_exit(struct erofs_compress *c)
32 {
33 	if (!c->private_data)
34 		return -EINVAL;
35 
36 	LZ4_freeStreamHC(c->private_data);
37 	return 0;
38 }
39 
compressor_lz4hc_init(struct erofs_compress * c)40 static int compressor_lz4hc_init(struct erofs_compress *c)
41 {
42 	c->alg = &erofs_compressor_lz4hc;
43 
44 	c->private_data = LZ4_createStreamHC();
45 	if (!c->private_data)
46 		return -ENOMEM;
47 	return 0;
48 }
49 
50 struct erofs_compressor erofs_compressor_lz4hc = {
51 	.name = "lz4hc",
52 	.default_level = LZ4HC_CLEVEL_DEFAULT,
53 	.best_level = LZ4HC_CLEVEL_MAX,
54 	.init = compressor_lz4hc_init,
55 	.exit = compressor_lz4hc_exit,
56 	.compress_destsize = lz4hc_compress_destsize,
57 };
58 
59