1 // SPDX-License-Identifier: GPL 2.0+ OR BSD-2-Clause
3 * LZ4 - Fast LZ compression algorithm
4 * Copyright (C) 2011 - 2016, Yann Collet.
5 * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * You can contact the author at :
27 * - LZ4 homepage : http://www.lz4.org
28 * - LZ4 source repository : https://github.com/lz4/lz4
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/bug.h>
34 #include <asm/unaligned.h>
35 #include <u-boot/lz4.h>
37 #define FORCE_INLINE inline __attribute__((always_inline))
39 static FORCE_INLINE u16 LZ4_readLE16(const void *src)
41 return get_unaligned_le16(src);
44 static FORCE_INLINE void LZ4_copy8(void *dst, const void *src)
46 put_unaligned(get_unaligned((const u64 *)src), (u64 *)dst);
54 typedef uintptr_t uptrval;
56 static FORCE_INLINE void LZ4_write32(void *memPtr, U32 value)
58 put_unaligned(value, (U32 *)memPtr);
61 /**************************************
62 * Reading and writing into memory
63 **************************************/
65 /* customized version of memcpy, which may overwrite up to 7 bytes beyond dstEnd */
66 static void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd)
68 BYTE* d = (BYTE*)dstPtr;
69 const BYTE* s = (const BYTE*)srcPtr;
70 BYTE* e = (BYTE*)dstEnd;
71 do { LZ4_copy8(d,s); d+=8; s+=8; } while (d<e);
74 /**************************************
76 **************************************/
79 #define WILDCOPYLENGTH 8
80 #define LASTLITERALS 5
81 #define MFLIMIT (WILDCOPYLENGTH + MINMATCH)
84 * ensure it's possible to write 2 x wildcopyLength
85 * without overflowing output buffer
87 #define MATCH_SAFEGUARD_DISTANCE ((2 * WILDCOPYLENGTH) - MINMATCH)
92 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
95 #define ML_MASK ((1U<<ML_BITS)-1)
96 #define RUN_BITS (8-ML_BITS)
97 #define RUN_MASK ((1U<<RUN_BITS)-1)
99 #define LZ4_STATIC_ASSERT(c) BUILD_BUG_ON(!(c))
101 /**************************************
102 * Local Structures and types
103 **************************************/
104 typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive;
105 typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
106 typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive;
108 #define DEBUGLOG(l, ...) {} /* disabled */
111 #define assert(condition) ((void)0)
115 * LZ4_decompress_generic() :
116 * This generic decompression function covers all use cases.
117 * It shall be instantiated several times, using different sets of directives.
118 * Note that it is important for performance that this function really get inlined,
119 * in order to remove useless branches during compilation optimization.
121 static FORCE_INLINE int LZ4_decompress_generic(
122 const char * const src,
126 * If endOnInput == endOnInputSize,
127 * this value is `dstCapacity`
130 /* endOnOutputSize, endOnInputSize */
131 endCondition_directive endOnInput,
133 earlyEnd_directive partialDecoding,
134 /* noDict, withPrefix64k, usingExtDict */
136 /* always <= dst, == dst when no prefix */
137 const BYTE * const lowPrefix,
138 /* only if dict == usingExtDict */
139 const BYTE * const dictStart,
140 /* note : = 0 if noDict */
141 const size_t dictSize
144 const BYTE *ip = (const BYTE *) src;
145 const BYTE * const iend = ip + srcSize;
147 BYTE *op = (BYTE *) dst;
148 BYTE * const oend = op + outputSize;
151 const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize;
152 static const unsigned int inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4};
153 static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3};
155 const int safeDecode = (endOnInput == endOnInputSize);
156 const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB)));
158 /* Set up the "end" pointers for the shortcut. */
159 const BYTE *const shortiend = iend -
160 (endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/;
161 const BYTE *const shortoend = oend -
162 (endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/;
164 DEBUGLOG(5, "%s (srcSize:%i, dstSize:%i)", __func__,
165 srcSize, outputSize);
168 assert(lowPrefix <= op);
171 /* Empty output buffer */
172 if ((endOnInput) && (unlikely(outputSize == 0)))
173 return ((srcSize == 1) && (*ip == 0)) ? 0 : -1;
175 if ((!endOnInput) && (unlikely(outputSize == 0)))
176 return (*ip == 0 ? 1 : -1);
178 if ((endOnInput) && unlikely(srcSize == 0))
181 /* Main Loop : decode sequences */
187 /* get literal length */
188 unsigned int const token = *ip++;
189 length = token>>ML_BITS;
191 /* ip < iend before the increment */
192 assert(!endOnInput || ip <= iend);
195 * A two-stage shortcut for the most common case:
196 * 1) If the literal length is 0..14, and there is enough
197 * space, enter the shortcut and copy 16 bytes on behalf
198 * of the literals (in the fast mode, only 8 bytes can be
199 * safely copied this way).
200 * 2) Further if the match length is 4..18, copy 18 bytes
201 * in a similar manner; but we ensure that there's enough
202 * space in the output for those 18 bytes earlier, upon
203 * entering the shortcut (in other words, there is a
204 * combined check for both stages).
206 * The & in the likely() below is intentionally not && so that
207 * some compilers can produce better parallelized runtime code
209 if ((endOnInput ? length != RUN_MASK : length <= 8)
211 * strictly "less than" on input, to re-enter
212 * the loop with at least one byte
214 && likely((endOnInput ? ip < shortiend : 1) &
215 (op <= shortoend))) {
216 /* Copy the literals */
217 memcpy(op, ip, endOnInput ? 16 : 8);
218 op += length; ip += length;
222 * prepare for match copying, decode full info.
223 * If it doesn't work out, the info won't be wasted.
225 length = token & ML_MASK; /* match length */
226 offset = LZ4_readLE16(ip);
229 assert(match <= op); /* check overflow */
231 /* Do not deal with overlapping matches. */
232 if ((length != ML_MASK) &&
234 (dict == withPrefix64k || match >= lowPrefix)) {
235 /* Copy the match. */
236 memcpy(op + 0, match + 0, 8);
237 memcpy(op + 8, match + 8, 8);
238 memcpy(op + 16, match + 16, 2);
239 op += length + MINMATCH;
240 /* Both stages worked, load the next token. */
245 * The second stage didn't work out, but the info
246 * is ready. Propel it right to the point of match
252 /* decode literal length */
253 if (length == RUN_MASK) {
256 if (unlikely(endOnInput ? ip >= iend - RUN_MASK : 0)) {
257 /* overflow detection */
263 } while (likely(endOnInput
264 ? ip < iend - RUN_MASK
268 && unlikely((uptrval)(op) +
269 length < (uptrval)(op))) {
270 /* overflow detection */
274 && unlikely((uptrval)(ip) +
275 length < (uptrval)(ip))) {
276 /* overflow detection */
283 LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
285 if (((endOnInput) && ((cpy > oend - MFLIMIT)
286 || (ip + length > iend - (2 + 1 + LASTLITERALS))))
287 || ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
288 if (partialDecoding) {
292 * stop in the middle of literal segment
298 && (ip + length > iend)) {
301 * read attempt beyond
302 * end of input buffer
311 * block decoding must
317 && ((ip + length != iend)
321 * input must be consumed
328 * supports overlapping memory regions; only matters
329 * for in-place decompression scenarios
331 memmove(op, ip, length);
335 /* Necessarily EOF, due to parsing restrictions */
336 if (!partialDecoding || (cpy == oend))
339 /* may overwrite up to WILDCOPYLENGTH beyond cpy */
340 LZ4_wildCopy(op, ip, cpy);
346 offset = LZ4_readLE16(ip);
350 /* get matchlength */
351 length = token & ML_MASK;
354 if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) {
355 /* Error : offset outside buffers */
359 /* costs ~1%; silence an msan warning when offset == 0 */
361 * note : when partialDecoding, there is no guarantee that
362 * at least 4 bytes remain available in output buffer
364 if (!partialDecoding) {
366 assert(oend - op >= 4);
368 LZ4_write32(op, (U32)offset);
371 if (length == ML_MASK) {
377 if ((endOnInput) && (ip > iend - LASTLITERALS))
385 (uptrval)(op) + length < (uptrval)op)) {
386 /* overflow detection */
393 /* match starting within external dictionary */
394 if ((dict == usingExtDict) && (match < lowPrefix)) {
395 if (unlikely(op + length > oend - LASTLITERALS)) {
396 /* doesn't respect parsing restriction */
397 if (!partialDecoding)
399 length = min(length, (size_t)(oend - op));
402 if (length <= (size_t)(lowPrefix - match)) {
404 * match fits entirely within external
405 * dictionary : just copy
407 memmove(op, dictEnd - (lowPrefix - match),
412 * match stretches into both external
413 * dictionary and current block
415 size_t const copySize = (size_t)(lowPrefix - match);
416 size_t const restSize = length - copySize;
418 memcpy(op, dictEnd - copySize, copySize);
420 if (restSize > (size_t)(op - lowPrefix)) {
422 BYTE * const endOfMatch = op + restSize;
423 const BYTE *copyFrom = lowPrefix;
425 while (op < endOfMatch)
428 memcpy(op, lowPrefix, restSize);
435 /* copy match within block */
440 * may not respect endBlock parsing restrictions
443 if (partialDecoding &&
444 (cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
445 size_t const mlen = min(length, (size_t)(oend - op));
446 const BYTE * const matchEnd = match + mlen;
447 BYTE * const copyEnd = op + mlen;
454 memcpy(op, match, mlen);
462 if (unlikely(offset < 8)) {
467 match += inc32table[offset];
468 memcpy(op + 4, match, 4);
469 match -= dec64table[offset];
471 LZ4_copy8(op, match);
477 if (unlikely(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
478 BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
480 if (cpy > oend - LASTLITERALS) {
482 * Error : last LASTLITERALS bytes
483 * must be literals (uncompressed)
488 if (op < oCopyLimit) {
489 LZ4_wildCopy(op, match, oCopyLimit);
490 match += oCopyLimit - op;
496 LZ4_copy8(op, match);
498 LZ4_wildCopy(op + 8, match + 8, cpy);
500 op = cpy; /* wildcopy correction */
503 /* end of decoding */
505 /* Nb of output bytes decoded */
506 return (int) (((char *)op) - dst);
508 /* Nb of input bytes read */
509 return (int) (((const char *)ip) - src);
512 /* Overflow error detected */
514 return (int) (-(((const char *)ip) - src)) - 1;
517 int LZ4_decompress_safe(const char *source, char *dest,
518 int compressedSize, int maxDecompressedSize)
520 return LZ4_decompress_generic(source, dest,
521 compressedSize, maxDecompressedSize,
522 endOnInputSize, decode_full_block,
523 noDict, (BYTE *)dest, NULL, 0);
526 int LZ4_decompress_safe_partial(const char *src, char *dst,
527 int compressedSize, int targetOutputSize, int dstCapacity)
529 dstCapacity = min(targetOutputSize, dstCapacity);
530 return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
531 endOnInputSize, partial_decode,
532 noDict, (BYTE *)dst, NULL, 0);