blob: 6de140e403ceae9e269561f2fbf617f27c99bf4d [file] [log] [blame]
Julius Wernerbf273912015-06-30 10:30:30 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * Alternatively, this software may be distributed under the terms of the
16 * GNU General Public License ("GPL") version 2 as published by the Free
17 * Software Foundation.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
Julius Wernerbf273912015-06-30 10:30:30 -070032#include <endian.h>
33#include <libpayload.h>
34#include <lz4.h>
35
36/* LZ4 comes with its own supposedly portable memory access functions, but they
37 * seem to be very inefficient in practice (at least on ARM64). Since libpayload
38 * knows about endinaness and allows some basic assumptions (such as unaligned
39 * access support), we can easily write the ones we need ourselves. */
Julius Werner09f29212015-09-29 13:51:35 -070040static uint16_t LZ4_readLE16(const void *src)
41{
42 return le16toh(*(uint16_t *)src);
43}
44static void LZ4_copy8(void *dst, const void *src)
45{
46/* ARM32 needs to be a special snowflake to prevent GCC from coalescing the
47 * access into LDRD/STRD (which don't support unaligned accesses). */
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}
Julius Wernerbf273912015-06-30 10:30:30 -070062
63typedef uint8_t BYTE;
64typedef uint16_t U16;
65typedef uint32_t U32;
66typedef int32_t S32;
67typedef 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
Julius Werner09f29212015-09-29 13:51:35 -070073/* Unaltered (just removed unrelated code) from github.com/Cyan4973/lz4/dev. */
74#include "lz4.c.inc" /* #include for inlining, do not link! */
Julius Wernerbf273912015-06-30 10:30:30 -070075
76#define LZ4F_MAGICNUMBER 0x184D2204
77
78struct lz4_frame_header {
Julius Werner09f29212015-09-29 13:51:35 -070079 uint32_t magic;
Julius Wernerbf273912015-06-30 10:30:30 -070080 union {
Julius Werner09f29212015-09-29 13:51:35 -070081 uint8_t flags;
Julius Wernerbf273912015-06-30 10:30:30 -070082 struct {
Julius Werner09f29212015-09-29 13:51:35 -070083 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;
Julius Wernerbf273912015-06-30 10:30:30 -070089 };
90 };
91 union {
Julius Werner09f29212015-09-29 13:51:35 -070092 uint8_t block_descriptor;
Julius Wernerbf273912015-06-30 10:30:30 -070093 struct {
Julius Werner09f29212015-09-29 13:51:35 -070094 uint8_t reserved1 : 4;
95 uint8_t max_block_size : 3;
96 uint8_t reserved2 : 1;
Julius Wernerbf273912015-06-30 10:30:30 -070097 };
98 };
Julius Werner09f29212015-09-29 13:51:35 -070099 /* + uint64_t content_size iff has_content_size is set */
100 /* + uint8_t header_checksum */
Julius Wernerbf273912015-06-30 10:30:30 -0700101} __attribute__((packed));
102
103struct lz4_block_header {
104 union {
Julius Werner09f29212015-09-29 13:51:35 -0700105 uint32_t raw;
Julius Wernerbf273912015-06-30 10:30:30 -0700106 struct {
Julius Werner09f29212015-09-29 13:51:35 -0700107 uint32_t size : 31;
108 uint32_t not_compressed : 1;
Julius Wernerbf273912015-06-30 10:30:30 -0700109 };
110 };
111 /* + size bytes of data */
Julius Werner09f29212015-09-29 13:51:35 -0700112 /* + uint32_t block_checksum iff has_block_checksum is set */
Julius Wernerbf273912015-06-30 10:30:30 -0700113} __attribute__((packed));
114
115size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn)
116{
117 const void *in = src;
118 void *out = dst;
Julius Werner09f29212015-09-29 13:51:35 -0700119 size_t out_size = 0;
Julius Wernerbf273912015-06-30 10:30:30 -0700120 int has_block_checksum;
121
122 { /* With in-place decompression the header may become invalid later. */
123 const struct lz4_frame_header *h = in;
124
Julius Werner09f29212015-09-29 13:51:35 -0700125 if (srcn < sizeof(*h) + sizeof(uint64_t) + sizeof(uint8_t))
Julius Wernerbf273912015-06-30 10:30:30 -0700126 return 0; /* input overrun */
127
128 /* We assume there's always only a single, standard frame. */
129 if (le32toh(h->magic) != LZ4F_MAGICNUMBER || h->version != 1)
130 return 0; /* unknown format */
131 if (h->reserved0 || h->reserved1 || h->reserved2)
132 return 0; /* reserved must be zero */
133 if (!h->independent_blocks)
134 return 0; /* we don't support block dependency */
135 has_block_checksum = h->has_block_checksum;
136
137 in += sizeof(*h);
138 if (h->has_content_size)
Julius Werner09f29212015-09-29 13:51:35 -0700139 in += sizeof(uint64_t);
140 in += sizeof(uint8_t);
Julius Wernerbf273912015-06-30 10:30:30 -0700141 }
142
143 while (1) {
Julius Werner09f29212015-09-29 13:51:35 -0700144 struct lz4_block_header b = { .raw = le32toh(*(uint32_t *)in) };
Julius Wernerbf273912015-06-30 10:30:30 -0700145 in += sizeof(struct lz4_block_header);
146
Julius Werner09f29212015-09-29 13:51:35 -0700147 if ((size_t)(in - src) + b.size > srcn)
148 break; /* input overrun */
Julius Wernerbf273912015-06-30 10:30:30 -0700149
Julius Werner09f29212015-09-29 13:51:35 -0700150 if (!b.size) {
151 out_size = out - dst;
152 break; /* decompression successful */
153 }
Julius Wernerbf273912015-06-30 10:30:30 -0700154
155 if (b.not_compressed) {
Julius Werner09f29212015-09-29 13:51:35 -0700156 size_t size = MIN((uint32_t)b.size, dst + dstn - out);
Julius Wernerbf697562015-07-16 13:59:57 -0700157 memcpy(out, in, size);
158 if (size < b.size)
Julius Werner09f29212015-09-29 13:51:35 -0700159 break; /* output overrun */
Julius Wernerbf697562015-07-16 13:59:57 -0700160 else
161 out += size;
Julius Wernerbf273912015-06-30 10:30:30 -0700162 } else {
163 /* constant folding essential, do not touch params! */
164 int ret = LZ4_decompress_generic(in, out, b.size,
165 dst + dstn - out, endOnInputSize,
166 full, 0, noDict, out, NULL, 0);
167 if (ret < 0)
Julius Werner09f29212015-09-29 13:51:35 -0700168 break; /* decompression error */
Julius Wernerbf273912015-06-30 10:30:30 -0700169 else
170 out += ret;
171 }
172
173 in += b.size;
174 if (has_block_checksum)
Julius Werner09f29212015-09-29 13:51:35 -0700175 in += sizeof(uint32_t);
Julius Wernerbf273912015-06-30 10:30:30 -0700176 }
Julius Werner09f29212015-09-29 13:51:35 -0700177
178 return out_size;
Julius Wernerbf273912015-06-30 10:30:30 -0700179}
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}