test/py: serial# cannot be overwritten on some devices
[platform/kernel/u-boot.git] / lib / lz4_wrapper.c
1 // SPDX-License-Identifier: GPL 2.0+ OR BSD-3-Clause
2 /*
3  * Copyright 2015 Google Inc.
4  */
5
6 #include <common.h>
7 #include <compiler.h>
8 #include <image.h>
9 #include <lz4.h>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <asm/unaligned.h>
13
14 static u16 LZ4_readLE16(const void *src) { return le16_to_cpu(*(u16 *)src); }
15 static void LZ4_copy4(void *dst, const void *src) { *(u32 *)dst = *(u32 *)src; }
16 static void LZ4_copy8(void *dst, const void *src) { *(u64 *)dst = *(u64 *)src; }
17
18 typedef  uint8_t BYTE;
19 typedef uint16_t U16;
20 typedef uint32_t U32;
21 typedef  int32_t S32;
22 typedef uint64_t U64;
23
24 #define FORCE_INLINE static inline __attribute__((always_inline))
25
26 /* lz4.c is unaltered (except removing unrelated code) from github.com/Cyan4973/lz4. */
27 #include "lz4.c"        /* #include for inlining, do not link! */
28
29 #define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U
30
31 int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn)
32 {
33         const void *end = dst + *dstn;
34         const void *in = src;
35         void *out = dst;
36         int has_block_checksum;
37         int ret;
38         *dstn = 0;
39
40         { /* With in-place decompression the header may become invalid later. */
41                 u32 magic;
42                 u8 flags, version, independent_blocks, has_content_size;
43                 u8 block_desc;
44
45                 if (srcn < sizeof(u32) + 3*sizeof(u8))
46                         return -EINVAL; /* input overrun */
47
48                 magic = get_unaligned_le32(in);
49                 in += sizeof(u32);
50                 flags = *(u8 *)in;
51                 in += sizeof(u8);
52                 block_desc = *(u8 *)in;
53                 in += sizeof(u8);
54
55                 version = (flags >> 6) & 0x3;
56                 independent_blocks = (flags >> 5) & 0x1;
57                 has_block_checksum = (flags >> 4) & 0x1;
58                 has_content_size = (flags >> 3) & 0x1;
59
60                 /* We assume there's always only a single, standard frame. */
61                 if (magic != LZ4F_MAGIC || version != 1)
62                         return -EPROTONOSUPPORT;        /* unknown format */
63                 if ((flags & 0x03) || (block_desc & 0x8f))
64                         return -EINVAL; /* reserved bits must be zero */
65                 if (!independent_blocks)
66                         return -EPROTONOSUPPORT; /* we can't support this yet */
67
68                 if (has_content_size) {
69                         if (srcn < sizeof(u32) + 3*sizeof(u8) + sizeof(u64))
70                                 return -EINVAL; /* input overrun */
71                         in += sizeof(u64);
72                 }
73                 /* Header checksum byte */
74                 in += sizeof(u8);
75         }
76
77         while (1) {
78                 u32 block_header, block_size;
79
80                 block_header = get_unaligned_le32(in);
81                 in += sizeof(u32);
82                 block_size = block_header & ~LZ4F_BLOCKUNCOMPRESSED_FLAG;
83
84                 if (in - src + block_size > srcn) {
85                         ret = -EINVAL;          /* input overrun */
86                         break;
87                 }
88
89                 if (!block_size) {
90                         ret = 0;        /* decompression successful */
91                         break;
92                 }
93
94                 if (block_header & LZ4F_BLOCKUNCOMPRESSED_FLAG) {
95                         size_t size = min((ptrdiff_t)block_size, end - out);
96                         memcpy(out, in, size);
97                         out += size;
98                         if (size < block_size) {
99                                 ret = -ENOBUFS; /* output overrun */
100                                 break;
101                         }
102                 } else {
103                         /* constant folding essential, do not touch params! */
104                         ret = LZ4_decompress_generic(in, out, block_size,
105                                         end - out, endOnInputSize,
106                                         full, 0, noDict, out, NULL, 0);
107                         if (ret < 0) {
108                                 ret = -EPROTO;  /* decompression error */
109                                 break;
110                         }
111                         out += ret;
112                 }
113
114                 in += block_size;
115                 if (has_block_checksum)
116                         in += sizeof(u32);
117         }
118
119         *dstn = out - dst;
120         return ret;
121 }