Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 32 | #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 Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 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 | /* 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 Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 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 | |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 73 | /* Unaltered (just removed unrelated code) from github.com/Cyan4973/lz4/dev. */ |
| 74 | #include "lz4.c.inc" /* #include for inlining, do not link! */ |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 75 | |
| 76 | #define LZ4F_MAGICNUMBER 0x184D2204 |
| 77 | |
| 78 | struct lz4_frame_header { |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 79 | uint32_t magic; |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 80 | union { |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 81 | uint8_t flags; |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 82 | struct { |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 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; |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 89 | }; |
| 90 | }; |
| 91 | union { |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 92 | uint8_t block_descriptor; |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 93 | struct { |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 94 | uint8_t reserved1 : 4; |
| 95 | uint8_t max_block_size : 3; |
| 96 | uint8_t reserved2 : 1; |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 97 | }; |
| 98 | }; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 99 | /* + uint64_t content_size iff has_content_size is set */ |
| 100 | /* + uint8_t header_checksum */ |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 101 | } __attribute__((packed)); |
| 102 | |
| 103 | struct lz4_block_header { |
| 104 | union { |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 105 | uint32_t raw; |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 106 | struct { |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 107 | uint32_t size : 31; |
| 108 | uint32_t not_compressed : 1; |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 109 | }; |
| 110 | }; |
| 111 | /* + size bytes of data */ |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 112 | /* + uint32_t block_checksum iff has_block_checksum is set */ |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 113 | } __attribute__((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; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 119 | size_t out_size = 0; |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 120 | 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 Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 125 | if (srcn < sizeof(*h) + sizeof(uint64_t) + sizeof(uint8_t)) |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 126 | 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 Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 139 | in += sizeof(uint64_t); |
| 140 | in += sizeof(uint8_t); |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | while (1) { |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 144 | struct lz4_block_header b = { .raw = le32toh(*(uint32_t *)in) }; |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 145 | in += sizeof(struct lz4_block_header); |
| 146 | |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 147 | if ((size_t)(in - src) + b.size > srcn) |
| 148 | break; /* input overrun */ |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 149 | |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 150 | if (!b.size) { |
| 151 | out_size = out - dst; |
| 152 | break; /* decompression successful */ |
| 153 | } |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 154 | |
| 155 | if (b.not_compressed) { |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 156 | size_t size = MIN((uint32_t)b.size, dst + dstn - out); |
Julius Werner | bf69756 | 2015-07-16 13:59:57 -0700 | [diff] [blame] | 157 | memcpy(out, in, size); |
| 158 | if (size < b.size) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 159 | break; /* output overrun */ |
Julius Werner | bf69756 | 2015-07-16 13:59:57 -0700 | [diff] [blame] | 160 | else |
| 161 | out += size; |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 162 | } 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 Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 168 | break; /* decompression error */ |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 169 | else |
| 170 | out += ret; |
| 171 | } |
| 172 | |
| 173 | in += b.size; |
| 174 | if (has_block_checksum) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 175 | in += sizeof(uint32_t); |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 176 | } |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 177 | |
| 178 | return out_size; |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | size_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 | } |