1 /* 2 * Copyright 2015 Google Inc. 3 * 4 * SPDX-License-Identifier: GPL 2.0+ BSD-3-Clause 5 */ 6 7 #include <common.h> 8 #include <compiler.h> 9 #include <linux/kernel.h> 10 #include <linux/types.h> 11 12 static u16 LZ4_readLE16(const void *src) { return le16_to_cpu(*(u16 *)src); } 13 static void LZ4_copy4(void *dst, const void *src) { *(u32 *)dst = *(u32 *)src; } 14 static void LZ4_copy8(void *dst, const void *src) { *(u64 *)dst = *(u64 *)src; } 15 16 typedef uint8_t BYTE; 17 typedef uint16_t U16; 18 typedef uint32_t U32; 19 typedef int32_t S32; 20 typedef uint64_t U64; 21 22 #define FORCE_INLINE static inline __attribute__((always_inline)) 23 24 /* Unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */ 25 #include "lz4.c" /* #include for inlining, do not link! */ 26 27 #define LZ4F_MAGIC 0x184D2204 28 29 struct lz4_frame_header { 30 u32 magic; 31 union { 32 u8 flags; 33 struct { 34 u8 reserved0:2; 35 u8 has_content_checksum:1; 36 u8 has_content_size:1; 37 u8 has_block_checksum:1; 38 u8 independent_blocks:1; 39 u8 version:2; 40 }; 41 }; 42 union { 43 u8 block_descriptor; 44 struct { 45 u8 reserved1:4; 46 u8 max_block_size:3; 47 u8 reserved2:1; 48 }; 49 }; 50 /* + u64 content_size iff has_content_size is set */ 51 /* + u8 header_checksum */ 52 } __packed; 53 54 struct lz4_block_header { 55 union { 56 u32 raw; 57 struct { 58 u32 size:31; 59 u32 not_compressed:1; 60 }; 61 }; 62 /* + size bytes of data */ 63 /* + u32 block_checksum iff has_block_checksum is set */ 64 } __packed; 65 66 bool lz4_is_valid_header(const unsigned char *h) 67 { 68 const struct lz4_frame_header *hdr = (const struct lz4_frame_header *)h; 69 /* We assume there's always only a single, standard frame. */ 70 if (le32_to_cpu(hdr->magic) != LZ4F_MAGIC || hdr->version != 1) 71 return false; /* unknown format */ 72 if (hdr->reserved0 || hdr->reserved1 || hdr->reserved2) 73 return false; /* reserved must be zero */ 74 if (!hdr->independent_blocks) 75 return false; /* we can't support this yet */ 76 77 return true; 78 } 79 80 int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn) 81 { 82 const void *end = dst + *dstn; 83 const void *in = src; 84 void *out = dst; 85 int has_block_checksum; 86 int ret; 87 *dstn = 0; 88 89 { /* With in-place decompression the header may become invalid later. */ 90 const struct lz4_frame_header *h = in; 91 92 if (srcn < sizeof(*h) + sizeof(u64) + sizeof(u8)) 93 return -EINVAL; /* input overrun */ 94 95 /* We assume there's always only a single, standard frame. */ 96 if (le32_to_cpu(h->magic) != LZ4F_MAGIC || h->version != 1) 97 return -EPROTONOSUPPORT; /* unknown format */ 98 if (h->reserved0 || h->reserved1 || h->reserved2) 99 return -EINVAL; /* reserved must be zero */ 100 if (!h->independent_blocks) 101 return -EPROTONOSUPPORT; /* we can't support this yet */ 102 has_block_checksum = h->has_block_checksum; 103 104 in += sizeof(*h); 105 if (h->has_content_size) 106 in += sizeof(u64); 107 in += sizeof(u8); 108 } 109 110 while (1) { 111 struct lz4_block_header b; 112 113 b.raw = le32_to_cpu(*(u32 *)in); 114 in += sizeof(struct lz4_block_header); 115 116 if (in - src + b.size > srcn) { 117 ret = -EINVAL; /* input overrun */ 118 break; 119 } 120 121 if (!b.size) { 122 ret = 0; /* decompression successful */ 123 break; 124 } 125 126 if (b.not_compressed) { 127 size_t size = min((ptrdiff_t)b.size, end - out); 128 memcpy(out, in, size); 129 out += size; 130 if (size < b.size) { 131 ret = -ENOBUFS; /* output overrun */ 132 break; 133 } 134 } else { 135 /* constant folding essential, do not touch params! */ 136 ret = LZ4_decompress_generic(in, out, b.size, 137 end - out, endOnInputSize, 138 full, 0, noDict, out, NULL, 0); 139 if (ret < 0) { 140 ret = -EPROTO; /* decompression error */ 141 break; 142 } 143 out += ret; 144 } 145 146 in += b.size; 147 if (has_block_checksum) 148 in += sizeof(u32); 149 } 150 151 *dstn = out - dst; 152 return ret; 153 } 154