blob: 566c2bf7278efad9fca26631da820ea93f1cb26f [file] [log] [blame]
Julius Werner98eeb962019-12-11 15:47:42 -08001/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only */
Julius Werner09f29212015-09-29 13:51:35 -07002
Julius Werner98eeb962019-12-11 15:47:42 -08003#include <commonlib/bsd/compression.h>
4#include <commonlib/bsd/helpers.h>
Idwer Vollering3c5b8032020-09-11 22:32:51 +02005#include <commonlib/bsd/sysincludes.h>
Julius Werner09f29212015-09-29 13:51:35 -07006#include <stdint.h>
7#include <string.h>
Maximilian Brune99bed462024-04-15 18:17:54 +02008#include <endian.h>
Julius Werner09f29212015-09-29 13:51:35 -07009
Maximilian Brune99bed462024-04-15 18:17:54 +020010/*
11 * RISC-V and older ARM architectures do not mandate support for misaligned access.
12 * Our le16toh and friends functions assume misaligned access support. Writing the access
13 * like this causes the compiler to generate instructions using misaligned access (or not)
14 * depending on the architecture. So there is no performance penalty for platforms supporting
15 * misaligned access.
16 */
Julius Werner09f29212015-09-29 13:51:35 -070017static uint16_t LZ4_readLE16(const void *src)
18{
Maximilian Brune99bed462024-04-15 18:17:54 +020019 return *((const uint8_t *)src + 1) << 8
20 | *(const uint8_t *)src;
Julius Werner09f29212015-09-29 13:51:35 -070021}
Maximilian Brune99bed462024-04-15 18:17:54 +020022
23static uint32_t LZ4_readLE32(const void *src)
24{
25 return *((const uint8_t *)src + 3) << 24
26 | *((const uint8_t *)src + 2) << 16
27 | *((const uint8_t *)src + 1) << 8
28 | *(const uint8_t *)src;
29}
30
Julius Werner09f29212015-09-29 13:51:35 -070031static void LZ4_copy8(void *dst, const void *src)
32{
33/* ARM32 needs to be a special snowflake to prevent GCC from coalescing the
34 * access into LDRD/STRD (which don't support unaligned accesses). */
35#ifdef __arm__ /* ARMv < 6 doesn't support unaligned accesses at all. */
36 #if defined(__COREBOOT_ARM_ARCH__) && __COREBOOT_ARM_ARCH__ < 6
37 int i;
38 for (i = 0; i < 8; i++)
39 ((uint8_t *)dst)[i] = ((uint8_t *)src)[i];
40 #else
41 uint32_t x0, x1;
Benjamin Barenblat82ef8ad2016-06-17 09:49:24 -070042 __asm__ ("ldr %[x0], [%[src]]"
43 : [x0]"=r"(x0)
44 : [src]"r"(src), "m"(*(const uint32_t *)src));
45 __asm__ ("ldr %[x1], [%[src], #4]"
46 : [x1]"=r"(x1)
47 : [src]"r"(src), "m"(*(const uint32_t *)(src + 4)));
48 __asm__ ("str %[x0], [%[dst]]"
49 : "=m"(*(uint32_t *)dst)
50 : [x0]"r"(x0), [dst]"r"(dst));
51 __asm__ ("str %[x1], [%[dst], #4]"
52 : "=m"(*(uint32_t *)(dst + 4))
53 : [x1]"r"(x1), [dst]"r"(dst));
Julius Werner09f29212015-09-29 13:51:35 -070054 #endif
wxjstz05782382017-06-09 17:10:32 +080055#elif defined(__riscv)
Jonathan Neuschäfer4acb0e72016-05-27 09:05:02 +020056 /* RISC-V implementations may trap on any unaligned access. */
57 int i;
58 for (i = 0; i < 8; i++)
59 ((uint8_t *)dst)[i] = ((uint8_t *)src)[i];
Julius Werner09f29212015-09-29 13:51:35 -070060#else
61 *(uint64_t *)dst = *(const uint64_t *)src;
62#endif
63}
64
65typedef uint8_t BYTE;
66typedef uint16_t U16;
67typedef uint32_t U32;
68typedef int32_t S32;
69typedef uint64_t U64;
70
Aaron Durbin75a62e72018-09-13 02:10:45 -060071#define FORCE_INLINE static __always_inline
Julius Werner09f29212015-09-29 13:51:35 -070072#define likely(expr) __builtin_expect((expr) != 0, 1)
73#define unlikely(expr) __builtin_expect((expr) != 0, 0)
74
75/* Unaltered (just removed unrelated code) from github.com/Cyan4973/lz4/dev. */
76#include "lz4.c.inc" /* #include for inlining, do not link! */
77
78#define LZ4F_MAGICNUMBER 0x184D2204
79
Krystian Hebel3c75a8d2020-10-08 19:19:42 +020080/* Bit field endianness is implementation-defined. Use masks instead.
81 * https://stackoverflow.com/a/6044223 */
82#define RESERVED0 0x03
83#define HAS_CONTENT_CHECKSUM 0x04
84#define HAS_CONTENT_SIZE 0x08
85#define HAS_BLOCK_CHECKSUM 0x10
86#define INDEPENDENT_BLOCKS 0x20
87#define VERSION 0xC0
88#define VERSION_SHIFT 6
89
90#define RESERVED1_2 0x8F
91#define MAX_BLOCK_SIZE 0x70
92
Julius Werner09f29212015-09-29 13:51:35 -070093struct lz4_frame_header {
94 uint32_t magic;
Krystian Hebel3c75a8d2020-10-08 19:19:42 +020095 uint8_t flags;
96 uint8_t block_descriptor;
Julius Werner09f29212015-09-29 13:51:35 -070097 /* + uint64_t content_size iff has_content_size is set */
98 /* + uint8_t header_checksum */
Stefan Reinauer6a001132017-07-13 02:20:27 +020099} __packed;
Julius Werner09f29212015-09-29 13:51:35 -0700100
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200101#define BH_SIZE 0x7FFFFFFF
102#define NOT_COMPRESSED 0x80000000
103
Julius Werner09f29212015-09-29 13:51:35 -0700104struct lz4_block_header {
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200105 uint32_t raw;
Julius Werner09f29212015-09-29 13:51:35 -0700106 /* + size bytes of data */
107 /* + uint32_t block_checksum iff has_block_checksum is set */
Stefan Reinauer6a001132017-07-13 02:20:27 +0200108} __packed;
Julius Werner09f29212015-09-29 13:51:35 -0700109
110size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn)
111{
112 const void *in = src;
113 void *out = dst;
114 size_t out_size = 0;
115 int has_block_checksum;
116
117 { /* With in-place decompression the header may become invalid later. */
118 const struct lz4_frame_header *h = in;
119
120 if (srcn < sizeof(*h) + sizeof(uint64_t) + sizeof(uint8_t))
121 return 0; /* input overrun */
122
123 /* We assume there's always only a single, standard frame. */
Maximilian Brune99bed462024-04-15 18:17:54 +0200124 if (LZ4_readLE32(&h->magic) != LZ4F_MAGICNUMBER
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200125 || (h->flags & VERSION) != (1 << VERSION_SHIFT))
Julius Werner09f29212015-09-29 13:51:35 -0700126 return 0; /* unknown format */
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200127 if ((h->flags & RESERVED0) || (h->block_descriptor & RESERVED1_2))
Julius Werner09f29212015-09-29 13:51:35 -0700128 return 0; /* reserved must be zero */
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200129 if (!(h->flags & INDEPENDENT_BLOCKS))
Julius Werner09f29212015-09-29 13:51:35 -0700130 return 0; /* we don't support block dependency */
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200131 has_block_checksum = h->flags & HAS_BLOCK_CHECKSUM;
Julius Werner09f29212015-09-29 13:51:35 -0700132
133 in += sizeof(*h);
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200134 if (h->flags & HAS_CONTENT_SIZE)
Julius Werner09f29212015-09-29 13:51:35 -0700135 in += sizeof(uint64_t);
136 in += sizeof(uint8_t);
137 }
138
139 while (1) {
Alex Rebert70282ae2020-02-29 17:36:08 -0500140 if ((size_t)(in - src) + sizeof(struct lz4_block_header) > srcn)
141 break; /* input overrun */
142
Julius Werner98eeb962019-12-11 15:47:42 -0800143 struct lz4_block_header b = {
Maximilian Brune99bed462024-04-15 18:17:54 +0200144 .raw = LZ4_readLE32((const uint32_t *)in)
Julius Werner98eeb962019-12-11 15:47:42 -0800145 };
Julius Werner09f29212015-09-29 13:51:35 -0700146 in += sizeof(struct lz4_block_header);
147
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200148 if ((size_t)(in - src) + (b.raw & BH_SIZE) > srcn)
Julius Werner09f29212015-09-29 13:51:35 -0700149 break; /* input overrun */
150
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200151 if (!(b.raw & BH_SIZE)) {
Julius Werner09f29212015-09-29 13:51:35 -0700152 out_size = out - dst;
153 break; /* decompression successful */
154 }
155
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200156 if (b.raw & NOT_COMPRESSED) {
157 size_t size = MIN((uintptr_t)(b.raw & BH_SIZE), (uintptr_t)dst
Lee Leahy49fd42d2017-03-10 10:57:00 -0800158 + dstn - (uintptr_t)out);
Julius Werner09f29212015-09-29 13:51:35 -0700159 memcpy(out, in, size);
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200160 if (size < (b.raw & BH_SIZE))
Julius Werner09f29212015-09-29 13:51:35 -0700161 break; /* output overrun */
Lee Leahy72c60a42017-03-10 10:53:36 -0800162 out += size;
Julius Werner09f29212015-09-29 13:51:35 -0700163 } else {
164 /* constant folding essential, do not touch params! */
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200165 int ret = LZ4_decompress_generic(in, out, (b.raw & BH_SIZE),
Julius Werner09f29212015-09-29 13:51:35 -0700166 dst + dstn - out, endOnInputSize,
167 full, 0, noDict, out, NULL, 0);
168 if (ret < 0)
169 break; /* decompression error */
Lee Leahy72c60a42017-03-10 10:53:36 -0800170 out += ret;
Julius Werner09f29212015-09-29 13:51:35 -0700171 }
172
Krystian Hebel3c75a8d2020-10-08 19:19:42 +0200173 in += (b.raw & BH_SIZE);
Julius Werner09f29212015-09-29 13:51:35 -0700174 if (has_block_checksum)
175 in += sizeof(uint32_t);
176 }
177
178 return out_size;
179}
180
181size_t ulz4f(const void *src, void *dst)
182{
183 /* LZ4 uses signed size parameters, so can't just use ((u32)-1) here. */
184 return ulz4fn(src, 1*GiB, dst, 1*GiB);
185}