1 // SPDX-License-Identifier: GPL-2.0+
2 #include "decompress.h"
4 #if IS_ENABLED(CONFIG_LZ4)
5 #include <u-boot/lz4.h>
6 static int z_erofs_decompress_lz4(struct z_erofs_decompress_req
*rq
)
12 bool support_0padding
= false;
13 unsigned int inputmargin
= 0;
15 if (erofs_sb_has_lz4_0padding()) {
16 support_0padding
= true;
18 while (!src
[inputmargin
& (erofs_blksiz() - 1)])
19 if (!(++inputmargin
& (erofs_blksiz() - 1)))
22 if (inputmargin
>= rq
->inputsize
)
26 if (rq
->decodedskip
) {
27 buff
= malloc(rq
->decodedlength
);
33 if (rq
->partial_decoding
|| !support_0padding
)
34 ret
= LZ4_decompress_safe_partial(src
+ inputmargin
, dest
,
35 rq
->inputsize
- inputmargin
,
36 rq
->decodedlength
, rq
->decodedlength
);
38 ret
= LZ4_decompress_safe(src
+ inputmargin
, dest
,
39 rq
->inputsize
- inputmargin
,
42 if (ret
!= (int)rq
->decodedlength
) {
43 erofs_err("failed to %s decompress %d in[%u, %u] out[%u]",
44 rq
->partial_decoding
? "partial" : "full",
45 ret
, rq
->inputsize
, inputmargin
, rq
->decodedlength
);
51 memcpy(rq
->out
, dest
+ rq
->decodedskip
,
52 rq
->decodedlength
- rq
->decodedskip
);
62 int z_erofs_decompress(struct z_erofs_decompress_req
*rq
)
64 if (rq
->alg
== Z_EROFS_COMPRESSION_INTERLACED
) {
65 unsigned int count
, rightpart
, skip
;
67 /* XXX: should support inputsize >= erofs_blksiz() later */
68 if (rq
->inputsize
> erofs_blksiz())
71 if (rq
->decodedlength
> erofs_blksiz())
74 if (rq
->decodedlength
< rq
->decodedskip
)
77 count
= rq
->decodedlength
- rq
->decodedskip
;
78 skip
= erofs_blkoff(rq
->interlaced_offset
+ rq
->decodedskip
);
79 rightpart
= min(erofs_blksiz() - skip
, count
);
80 memcpy(rq
->out
, rq
->in
+ skip
, rightpart
);
81 memcpy(rq
->out
+ rightpart
, rq
->in
, count
- rightpart
);
83 } else if (rq
->alg
== Z_EROFS_COMPRESSION_SHIFTED
) {
84 if (rq
->decodedlength
> rq
->inputsize
)
87 DBG_BUGON(rq
->decodedlength
< rq
->decodedskip
);
88 memcpy(rq
->out
, rq
->in
+ rq
->decodedskip
,
89 rq
->decodedlength
- rq
->decodedskip
);
93 #if IS_ENABLED(CONFIG_LZ4)
94 if (rq
->alg
== Z_EROFS_COMPRESSION_LZ4
)
95 return z_erofs_decompress_lz4(rq
);