xref: /rk3399_rockchip-uboot/lib/lz4_wrapper.c (revision b87ef1c11aa5a22e06a78af78f8827fc4cef6d2e)
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * SPDX-License-Identifier: GPL 2.0+ BSD-3-Clause
5  */
6 
7 #include <common.h>
8 #include <compiler.h>
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <asm/unaligned.h>
12 
13 static u16 LZ4_readLE16(const void *src)
14 {
15 	return get_unaligned_le16(src);
16 }
17 static void LZ4_copy4(void *dst, const void *src)
18 {
19 	put_unaligned(get_unaligned((const u32 *)src), (u32 *)dst);
20 }
21 static void LZ4_copy8(void *dst, const void *src)
22 {
23 	put_unaligned(get_unaligned((const u64 *)src), (u64 *)dst);
24 }
25 
26 typedef  uint8_t BYTE;
27 typedef uint16_t U16;
28 typedef uint32_t U32;
29 typedef  int32_t S32;
30 typedef uint64_t U64;
31 
32 #define FORCE_INLINE static inline __attribute__((always_inline))
33 
34 /* Unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */
35 #include "lz4.c"	/* #include for inlining, do not link! */
36 
37 #define LZ4F_MAGIC 0x184D2204
38 
39 struct lz4_frame_header {
40 	u32 magic;
41 	union {
42 		u8 flags;
43 		struct {
44 			u8 reserved0:2;
45 			u8 has_content_checksum:1;
46 			u8 has_content_size:1;
47 			u8 has_block_checksum:1;
48 			u8 independent_blocks:1;
49 			u8 version:2;
50 		};
51 	};
52 	union {
53 		u8 block_descriptor;
54 		struct {
55 			u8 reserved1:4;
56 			u8 max_block_size:3;
57 			u8 reserved2:1;
58 		};
59 	};
60 	/* + u64 content_size iff has_content_size is set */
61 	/* + u8 header_checksum */
62 } __packed;
63 
64 struct lz4_block_header {
65 	union {
66 		u32 raw;
67 		struct {
68 			u32 size:31;
69 			u32 not_compressed:1;
70 		};
71 	};
72 	/* + size bytes of data */
73 	/* + u32 block_checksum iff has_block_checksum is set */
74 } __packed;
75 
76 bool lz4_is_valid_header(const unsigned char *h)
77 {
78 	const struct lz4_frame_header *hdr  = (const struct lz4_frame_header *)h;
79 	/* We assume there's always only a single, standard frame. */
80 	if (le32_to_cpu(hdr->magic) != LZ4F_MAGIC || hdr->version != 1)
81 		return false;        /* unknown format */
82 	if (hdr->reserved0 || hdr->reserved1 || hdr->reserved2)
83 		return false; /* reserved must be zero */
84 	if (!hdr->independent_blocks)
85 		return false; /* we can't support this yet */
86 
87 	return true;
88 }
89 
90 int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn)
91 {
92 	const void *end = dst + *dstn;
93 	const void *in = src;
94 	void *out = dst;
95 	int has_block_checksum;
96 	int ret;
97 	*dstn = 0;
98 
99 	{ /* With in-place decompression the header may become invalid later. */
100 		const struct lz4_frame_header *h = in;
101 
102 		if (srcn < sizeof(*h) + sizeof(u64) + sizeof(u8))
103 			return -EINVAL;	/* input overrun */
104 
105 		/* We assume there's always only a single, standard frame. */
106 		if (le32_to_cpu(h->magic) != LZ4F_MAGIC || h->version != 1)
107 			return -EPROTONOSUPPORT;	/* unknown format */
108 		if (h->reserved0 || h->reserved1 || h->reserved2)
109 			return -EINVAL;	/* reserved must be zero */
110 		if (!h->independent_blocks)
111 			return -EPROTONOSUPPORT; /* we can't support this yet */
112 		has_block_checksum = h->has_block_checksum;
113 
114 		in += sizeof(*h);
115 		if (h->has_content_size)
116 			in += sizeof(u64);
117 		in += sizeof(u8);
118 	}
119 
120 	while (1) {
121 		struct lz4_block_header b;
122 
123 		b.raw = le32_to_cpu(*(u32 *)in);
124 		in += sizeof(struct lz4_block_header);
125 
126 		if (in - src + b.size > srcn) {
127 			ret = -EINVAL;		/* input overrun */
128 			break;
129 		}
130 
131 		if (!b.size) {
132 			ret = 0;	/* decompression successful */
133 			break;
134 		}
135 
136 		if (b.not_compressed) {
137 			size_t size = min((ptrdiff_t)b.size, end - out);
138 			memcpy(out, in, size);
139 			out += size;
140 			if (size < b.size) {
141 				ret = -ENOBUFS;	/* output overrun */
142 				break;
143 			}
144 		} else {
145 			/* constant folding essential, do not touch params! */
146 			ret = LZ4_decompress_generic(in, out, b.size,
147 					end - out, endOnInputSize,
148 					full, 0, noDict, out, NULL, 0);
149 			if (ret < 0) {
150 				ret = -EPROTO;	/* decompression error */
151 				break;
152 			}
153 			out += ret;
154 		}
155 
156 		in += b.size;
157 		if (has_block_checksum)
158 			in += sizeof(u32);
159 	}
160 
161 	*dstn = out - dst;
162 	return ret;
163 }
164