2 * BTRFS filesystem implementation for U-Boot
4 * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
6 * SPDX-License-Identifier: GPL-2.0+
10 #include <linux/lzo.h>
11 #include <u-boot/zlib.h>
13 static u32 decompress_lzo(const u8 *cbuf, u32 clen, u8 *dbuf, u32 dlen)
15 u32 tot_len, in_len, res;
22 tot_len = le32_to_cpu(*(u32 *) cbuf);
27 if (tot_len == 0 && dlen)
35 in_len = le32_to_cpu(*(u32 *) cbuf);
39 if (in_len > clen || tot_len < 4 + in_len)
42 tot_len -= 4 + in_len;
45 ret = lzo1x_decompress_safe(cbuf, in_len, dbuf, &out_len);
61 #define PRESET_DICT 0x20
63 static u32 decompress_zlib(const u8 *_cbuf, u32 clen, u8 *dbuf, u32 dlen)
65 int wbits = MAX_WBITS, ret = -1;
70 memset(&stream, 0, sizeof(stream));
76 stream.next_out = dbuf;
77 stream.avail_out = dlen;
80 /* skip adler32 check if deflate and no dictionary */
81 if (clen > 2 && !(cbuf[1] & PRESET_DICT) &&
82 ((cbuf[0] & 0x0f) == Z_DEFLATED) &&
83 !(((cbuf[0] << 8) + cbuf[1]) % 31)) {
84 wbits = -((cbuf[0] >> 4) + 8);
89 if (Z_OK != inflateInit2(&stream, wbits))
92 while (stream.total_in < clen) {
93 stream.next_in = cbuf + stream.total_in;
94 stream.avail_in = min((u32) (clen - stream.total_in),
95 (u32) btrfs_info.sb.sectorsize);
97 ret = inflate(&stream, Z_NO_FLUSH);
102 res = stream.total_out;
105 if (ret != Z_STREAM_END)
111 u32 btrfs_decompress(u8 type, const char *c, u32 clen, char *d, u32 dlen)
117 cbuf = (const u8 *) c;
121 case BTRFS_COMPRESS_NONE:
122 res = dlen < clen ? dlen : clen;
123 memcpy(dbuf, cbuf, res);
125 case BTRFS_COMPRESS_ZLIB:
126 return decompress_zlib(cbuf, clen, dbuf, dlen);
127 case BTRFS_COMPRESS_LZO:
128 return decompress_lzo(cbuf, clen, dbuf, dlen);
130 printf("%s: Unsupported compression in extent: %i\n", __func__,