1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2018-2019 HUAWEI, Inc.
4 * http://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
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(struct erofs_compress * c,void * src,unsigned int * srcsize,void * dst,unsigned int dstsize)15 static int lz4_compress_destsize(struct erofs_compress *c,
16 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->alg = &erofs_compressor_lz4;
36 sbi.lz4_max_distance = LZ4_DISTANCE_MAX;
37 return 0;
38 }
39
40 struct erofs_compressor erofs_compressor_lz4 = {
41 .name = "lz4",
42 .default_level = 0,
43 .best_level = 0,
44 .init = compressor_lz4_init,
45 .exit = compressor_lz4_exit,
46 .compress_destsize = lz4_compress_destsize,
47 };
48