xref: /aosp_15_r20/external/erofs-utils/lib/compressor_lz4.c (revision 33b1fccf6a0fada2c2875d400ed01119b7676ee5)
1*33b1fccfSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0
2*33b1fccfSAndroid Build Coastguard Worker /*
3*33b1fccfSAndroid Build Coastguard Worker  * Copyright (C) 2018-2019 HUAWEI, Inc.
4*33b1fccfSAndroid Build Coastguard Worker  *             http://www.huawei.com/
5*33b1fccfSAndroid Build Coastguard Worker  * Created by Gao Xiang <[email protected]>
6*33b1fccfSAndroid Build Coastguard Worker  */
7*33b1fccfSAndroid Build Coastguard Worker #include <lz4.h>
8*33b1fccfSAndroid Build Coastguard Worker #include "erofs/internal.h"
9*33b1fccfSAndroid Build Coastguard Worker #include "compressor.h"
10*33b1fccfSAndroid Build Coastguard Worker 
11*33b1fccfSAndroid Build Coastguard Worker #ifndef LZ4_DISTANCE_MAX	/* history window size */
12*33b1fccfSAndroid Build Coastguard Worker #define LZ4_DISTANCE_MAX 65535	/* set to maximum value by default */
13*33b1fccfSAndroid Build Coastguard Worker #endif
14*33b1fccfSAndroid Build Coastguard Worker 
lz4_compress_destsize(const struct erofs_compress * c,const void * src,unsigned int * srcsize,void * dst,unsigned int dstsize)15*33b1fccfSAndroid Build Coastguard Worker static int lz4_compress_destsize(const struct erofs_compress *c,
16*33b1fccfSAndroid Build Coastguard Worker 				 const void *src, unsigned int *srcsize,
17*33b1fccfSAndroid Build Coastguard Worker 				 void *dst, unsigned int dstsize)
18*33b1fccfSAndroid Build Coastguard Worker {
19*33b1fccfSAndroid Build Coastguard Worker 	int srcSize = (int)*srcsize;
20*33b1fccfSAndroid Build Coastguard Worker 	int rc = LZ4_compress_destSize(src, dst, &srcSize, (int)dstsize);
21*33b1fccfSAndroid Build Coastguard Worker 
22*33b1fccfSAndroid Build Coastguard Worker 	if (!rc)
23*33b1fccfSAndroid Build Coastguard Worker 		return -EFAULT;
24*33b1fccfSAndroid Build Coastguard Worker 	*srcsize = srcSize;
25*33b1fccfSAndroid Build Coastguard Worker 	return rc;
26*33b1fccfSAndroid Build Coastguard Worker }
27*33b1fccfSAndroid Build Coastguard Worker 
compressor_lz4_exit(struct erofs_compress * c)28*33b1fccfSAndroid Build Coastguard Worker static int compressor_lz4_exit(struct erofs_compress *c)
29*33b1fccfSAndroid Build Coastguard Worker {
30*33b1fccfSAndroid Build Coastguard Worker 	return 0;
31*33b1fccfSAndroid Build Coastguard Worker }
32*33b1fccfSAndroid Build Coastguard Worker 
compressor_lz4_init(struct erofs_compress * c)33*33b1fccfSAndroid Build Coastguard Worker static int compressor_lz4_init(struct erofs_compress *c)
34*33b1fccfSAndroid Build Coastguard Worker {
35*33b1fccfSAndroid Build Coastguard Worker 	c->sbi->lz4.max_distance = max_t(u16, c->sbi->lz4.max_distance,
36*33b1fccfSAndroid Build Coastguard Worker 					 LZ4_DISTANCE_MAX);
37*33b1fccfSAndroid Build Coastguard Worker 	return 0;
38*33b1fccfSAndroid Build Coastguard Worker }
39*33b1fccfSAndroid Build Coastguard Worker 
40*33b1fccfSAndroid Build Coastguard Worker const struct erofs_compressor erofs_compressor_lz4 = {
41*33b1fccfSAndroid Build Coastguard Worker 	.init = compressor_lz4_init,
42*33b1fccfSAndroid Build Coastguard Worker 	.exit = compressor_lz4_exit,
43*33b1fccfSAndroid Build Coastguard Worker 	.compress_destsize = lz4_compress_destsize,
44*33b1fccfSAndroid Build Coastguard Worker };
45