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 | |
| 32 | #include <assert.h> |
| 33 | #include <endian.h> |
| 34 | #include <libpayload.h> |
| 35 | #include <lz4.h> |
| 36 | |
| 37 | /* LZ4 comes with its own supposedly portable memory access functions, but they |
| 38 | * seem to be very inefficient in practice (at least on ARM64). Since libpayload |
| 39 | * knows about endinaness and allows some basic assumptions (such as unaligned |
| 40 | * access support), we can easily write the ones we need ourselves. */ |
| 41 | static u16 LZ4_readLE16(const void *src) { return le16toh(*(u16 *)src); } |
| 42 | static void LZ4_copy4(void *dst, const void *src) { *(u32 *)dst = *(u32 *)src; } |
| 43 | static void LZ4_copy8(void *dst, const void *src) { *(u64 *)dst = *(u64 *)src; } |
| 44 | |
| 45 | typedef uint8_t BYTE; |
| 46 | typedef uint16_t U16; |
| 47 | typedef uint32_t U32; |
| 48 | typedef int32_t S32; |
| 49 | typedef uint64_t U64; |
| 50 | |
| 51 | #define FORCE_INLINE static inline __attribute__((always_inline)) |
| 52 | #define likely(expr) __builtin_expect((expr) != 0, 1) |
| 53 | #define unlikely(expr) __builtin_expect((expr) != 0, 0) |
| 54 | |
| 55 | /* Unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */ |
| 56 | #include "lz4.c" /* #include for inlining, do not link! */ |
| 57 | |
| 58 | #define LZ4F_MAGICNUMBER 0x184D2204 |
| 59 | |
| 60 | struct lz4_frame_header { |
| 61 | u32 magic; |
| 62 | union { |
| 63 | u8 flags; |
| 64 | struct { |
| 65 | u8 reserved0 : 2; |
| 66 | u8 has_content_checksum : 1; |
| 67 | u8 has_content_size : 1; |
| 68 | u8 has_block_checksum : 1; |
| 69 | u8 independent_blocks : 1; |
| 70 | u8 version : 2; |
| 71 | }; |
| 72 | }; |
| 73 | union { |
| 74 | u8 block_descriptor; |
| 75 | struct { |
| 76 | u8 reserved1 : 4; |
| 77 | u8 max_block_size : 3; |
| 78 | u8 reserved2 : 1; |
| 79 | }; |
| 80 | }; |
| 81 | /* + u64 content_size iff has_content_size is set */ |
| 82 | /* + u8 header_checksum */ |
| 83 | } __attribute__((packed)); |
| 84 | |
| 85 | struct lz4_block_header { |
| 86 | union { |
| 87 | u32 raw; |
| 88 | struct { |
| 89 | u32 size : 31; |
| 90 | u32 not_compressed : 1; |
| 91 | }; |
| 92 | }; |
| 93 | /* + size bytes of data */ |
| 94 | /* + u32 block_checksum iff has_block_checksum is set */ |
| 95 | } __attribute__((packed)); |
| 96 | |
| 97 | size_t ulz4fn(const void *src, size_t srcn, void *dst, size_t dstn) |
| 98 | { |
| 99 | const void *in = src; |
| 100 | void *out = dst; |
| 101 | int has_block_checksum; |
| 102 | |
| 103 | { /* With in-place decompression the header may become invalid later. */ |
| 104 | const struct lz4_frame_header *h = in; |
| 105 | |
| 106 | if (srcn < sizeof(*h) + sizeof(u64) + sizeof(u8)) |
| 107 | return 0; /* input overrun */ |
| 108 | |
| 109 | /* We assume there's always only a single, standard frame. */ |
| 110 | if (le32toh(h->magic) != LZ4F_MAGICNUMBER || h->version != 1) |
| 111 | return 0; /* unknown format */ |
| 112 | if (h->reserved0 || h->reserved1 || h->reserved2) |
| 113 | return 0; /* reserved must be zero */ |
| 114 | if (!h->independent_blocks) |
| 115 | return 0; /* we don't support block dependency */ |
| 116 | has_block_checksum = h->has_block_checksum; |
| 117 | |
| 118 | in += sizeof(*h); |
| 119 | if (h->has_content_size) |
| 120 | in += sizeof(u64); |
| 121 | in += sizeof(u8); |
| 122 | } |
| 123 | |
| 124 | while (1) { |
| 125 | struct lz4_block_header b = { .raw = le32toh(*(u32 *)in) }; |
| 126 | in += sizeof(struct lz4_block_header); |
| 127 | |
| 128 | if (in - src + b.size > srcn) |
| 129 | return 0; /* input overrun */ |
| 130 | |
| 131 | if (!b.size) |
| 132 | return out - dst; /* decompression successful */ |
| 133 | |
| 134 | if (b.not_compressed) { |
Julius Werner | bf69756 | 2015-07-16 13:59:57 -0700 | [diff] [blame] | 135 | size_t size = MIN((u32)b.size, dst + dstn - out); |
| 136 | memcpy(out, in, size); |
| 137 | if (size < b.size) |
| 138 | return 0; /* output overrun */ |
| 139 | else |
| 140 | out += size; |
Julius Werner | bf27391 | 2015-06-30 10:30:30 -0700 | [diff] [blame] | 141 | } else { |
| 142 | /* constant folding essential, do not touch params! */ |
| 143 | int ret = LZ4_decompress_generic(in, out, b.size, |
| 144 | dst + dstn - out, endOnInputSize, |
| 145 | full, 0, noDict, out, NULL, 0); |
| 146 | if (ret < 0) |
| 147 | return 0; /* decompression error */ |
| 148 | else |
| 149 | out += ret; |
| 150 | } |
| 151 | |
| 152 | in += b.size; |
| 153 | if (has_block_checksum) |
| 154 | in += sizeof(u32); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | size_t ulz4f(const void *src, void *dst) |
| 159 | { |
| 160 | /* LZ4 uses signed size parameters, so can't just use ((u32)-1) here. */ |
| 161 | return ulz4fn(src, 1*GiB, dst, 1*GiB); |
| 162 | } |