| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include <endian.h> |
| 33 |
#include <libpayload.h> |
| 34 |
#include <lz4.h> |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
static uint16_t LZ4_readLE16(const void *src) |
| 41 |
{ |
| 42 |
return le16toh(*(uint16_t *)src); |
| 43 |
} |
| 44 |
static void LZ4_copy8(void *dst, const void *src) |
| 45 |
{ |
| 46 |
|
| 47 |
|
| 48 |
#ifdef __arm__ |
| 49 |
uint32_t x0, x1; |
| 50 |
asm volatile ( |
| 51 |
"ldr %[x0], [%[src]]\n\t" |
| 52 |
"ldr %[x1], [%[src], #4]\n\t" |
| 53 |
"str %[x0], [%[dst]]\n\t" |
| 54 |
"str %[x1], [%[dst], #4]\n\t" |
| 55 |
: [x0]"=r"(x0), [x1]"=r"(x1) |
| 56 |
: [src]"r"(src), [dst]"r"(dst) |
| 57 |
: "memory" ); |
| 58 |
#else |
| 59 |
*(uint64_t *)dst = *(const uint64_t *)src; |
| 60 |
#endif |
| 61 |
} |
| 62 |
|
| 63 |
typedef uint8_t BYTE; |
| 64 |
typedef uint16_t U16; |
| 65 |
typedef uint32_t U32; |
| 66 |
typedef int32_t S32; |
| 67 |
typedef uint64_t U64; |
| 68 |
|
| 69 |
#define FORCE_INLINE static inline __attribute__((always_inline)) |
| 70 |
#define likely(expr) __builtin_expect((expr) != 0, 1) |
| 71 |
#define unlikely(expr) __builtin_expect((expr) != 0, 0) |
| 72 |
|
| 73 |
|
| 74 |
#include "lz4.c.inc" |
| 75 |
|
| 76 |
#define LZ4F_MAGICNUMBER 0x184D2204 |
| 77 |
|
| 78 |
struct lz4_frame_header { |
| 79 |
uint32_t magic; |
| 80 |
union { |
| 81 |
uint8_t flags; |
| 82 |
struct { |
| 83 |
uint8_t reserved0 : 2; |
| 84 |
uint8_t has_content_checksum : 1; |
| 85 |
uint8_t has_content_size : 1; |
| 86 |
uint8_t has_block_checksum : 1; |
| 87 |
uint8_t independent_blocks : 1; |
| 88 |
uint8_t version : 2; |
| 89 |
}; |
| 90 |
}; |
| 91 |
union { |
| 92 |
uint8_t block_descriptor; |
| 93 |
struct { |
| 94 |
uint8_t reserved1 : 4; |
| 95 |
uint8_t max_block_size : 3; |
| 96 |
uint8_t reserved2 : 1; |
| 97 |
}; |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
} __packed; |
| 102 |
|
| 103 |
struct lz4_block_header { |
| 104 |
union { |
| 105 |
uint32_t raw; |
| 106 |
struct { |
| 107 |
uint32_t size : 31; |
| 108 |
uint32_t not_compressed : 1; |
| 109 |
}; |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
} __packed; |
| 114 |
|
| 115 |
size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn) |
| 116 |
{ |
| 117 |
const void *in = src; |
| 118 |
void *out = dst; |
| 119 |
size_t out_size = 0; |
| 120 |
int has_block_checksum; |
| 121 |
|
| 122 |
{ |
| 123 |
const struct lz4_frame_header *h = in; |
| 124 |
|
| 125 |
if (srcn < sizeof(*h) + sizeof(uint64_t) + sizeof(uint8_t)) |
| 126 |
return 0; |
| 127 |
|
| 128 |
|
| 129 |
if (le32toh(h->magic) != LZ4F_MAGICNUMBER || h->version != 1) |
| 130 |
return 0; |
| 131 |
if (h->reserved0 || h->reserved1 || h->reserved2) |
| 132 |
return 0; |
| 133 |
if (!h->independent_blocks) |
| 134 |
return 0; |
| 135 |
has_block_checksum = h->has_block_checksum; |
| 136 |
|
| 137 |
in += sizeof(*h); |
| 138 |
if (h->has_content_size) |
| 139 |
in += sizeof(uint64_t); |
| 140 |
in += sizeof(uint8_t); |
| 141 |
} |
| 142 |
|
| 143 |
while (1) { |
| 144 |
if ((size_t)(in - src) + sizeof(struct lz4_block_header) > srcn) |
| 145 |
break; |
| 146 |
|
| 147 |
struct lz4_block_header b = { .raw = le32toh(*(uint32_t *)in) }; |
| 148 |
in += sizeof(struct lz4_block_header); |
| 149 |
|
| 150 |
if ((size_t)(in - src) + b.size > srcn) |
| 151 |
break; |
| 152 |
|
| 153 |
if (!b.size) { |
| 154 |
out_size = out - dst; |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
if (b.not_compressed) { |
| 159 |
size_t size = MIN((uint32_t)b.size, dst + dstn - out); |
| 160 |
memcpy(out, in, size); |
| 161 |
if (size < b.size) |
| 162 |
break; |
| 163 |
else |
| 164 |
out += size; |
| 165 |
} else { |
| 166 |
|
| 167 |
int ret = LZ4_decompress_generic(in, out, b.size, |
| 168 |
dst + dstn - out, endOnInputSize, |
| 169 |
full, 0, noDict, out, NULL, 0); |
| 170 |
if (ret < 0) |
| 171 |
break; |
| 172 |
else |
| 173 |
out += ret; |
| 174 |
} |
| 175 |
|
| 176 |
in += b.size; |
| 177 |
if (has_block_checksum) |
| 178 |
in += sizeof(uint32_t); |
| 179 |
} |
| 180 |
|
| 181 |
return out_size; |
| 182 |
} |
| 183 |
|
| 184 |
size_t ulz4f(const void *src, void *dst) |
| 185 |
{ |
| 186 |
|
| 187 |
return ulz4fn(src, 1*GiB, dst, 1*GiB); |
| 188 |
} |