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