xref: /rk3399_rockchip-uboot/lib/lz4_wrapper.c (revision b3df74d022cabe1eb83c0ddb00affb500f0dd3fe)
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 bool lz4_is_valid_header(const unsigned char *h)
38 {
39 	const struct lz4_frame_header *hdr  = (const struct lz4_frame_header *)h;
40 	/* We assume there's always only a single, standard frame. */
41 	if (le32_to_cpu(hdr->magic) != LZ4F_MAGIC || hdr->version != 1)
42 		return false;        /* unknown format */
43 	if (hdr->reserved0 || hdr->reserved1 || hdr->reserved2)
44 		return false; /* reserved must be zero */
45 	if (!hdr->independent_blocks)
46 		return false; /* we can't support this yet */
47 
48 	return true;
49 }
50 
51 int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn)
52 {
53 	const void *end = dst + *dstn;
54 	const void *in = src;
55 	void *out = dst;
56 	int has_block_checksum;
57 	int ret;
58 	*dstn = 0;
59 
60 	{ /* With in-place decompression the header may become invalid later. */
61 		const struct lz4_frame_header *h = in;
62 
63 		if (srcn < sizeof(*h) + sizeof(u64) + sizeof(u8))
64 			return -EINVAL;	/* input overrun */
65 
66 		/* We assume there's always only a single, standard frame. */
67 		if (le32_to_cpu(h->magic) != LZ4F_MAGIC || h->version != 1)
68 			return -EPROTONOSUPPORT;	/* unknown format */
69 		if (h->reserved0 || h->reserved1 || h->reserved2)
70 			return -EINVAL;	/* reserved must be zero */
71 		if (!h->independent_blocks)
72 			return -EPROTONOSUPPORT; /* we can't support this yet */
73 		has_block_checksum = h->has_block_checksum;
74 
75 		in += sizeof(*h);
76 		if (h->has_content_size)
77 			in += sizeof(u64);
78 		in += sizeof(u8);
79 	}
80 
81 	while (1) {
82 		struct lz4_block_header b;
83 
84 		b.raw = le32_to_cpu(*(u32 *)in);
85 		in += sizeof(struct lz4_block_header);
86 
87 		if (in - src + b.size > srcn) {
88 			ret = -EINVAL;		/* input overrun */
89 			break;
90 		}
91 
92 		if (!b.size) {
93 			ret = 0;	/* decompression successful */
94 			break;
95 		}
96 
97 		if (b.not_compressed) {
98 			size_t size = min((ptrdiff_t)b.size, end - out);
99 			memcpy(out, in, size);
100 			out += size;
101 			if (size < b.size) {
102 				ret = -ENOBUFS;	/* output overrun */
103 				break;
104 			}
105 		} else {
106 			/* constant folding essential, do not touch params! */
107 			ret = LZ4_decompress_generic(in, out, b.size,
108 					end - out, endOnInputSize,
109 					full, 0, noDict, out, NULL, 0);
110 			if (ret < 0) {
111 				ret = -EPROTO;	/* decompression error */
112 				break;
113 			}
114 			out += ret;
115 		}
116 
117 		in += b.size;
118 		if (has_block_checksum)
119 			in += sizeof(u32);
120 	}
121 
122 	*dstn = out - dst;
123 	return ret;
124 }
125