18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * LZ4 - Fast LZ compression algorithm
38c2ecf20Sopenharmony_ci * Copyright (C) 2011 - 2016, Yann Collet.
48c2ecf20Sopenharmony_ci * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php)
58c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
68c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions are
78c2ecf20Sopenharmony_ci * met:
88c2ecf20Sopenharmony_ci *	* Redistributions of source code must retain the above copyright
98c2ecf20Sopenharmony_ci *	  notice, this list of conditions and the following disclaimer.
108c2ecf20Sopenharmony_ci *	* Redistributions in binary form must reproduce the above
118c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following disclaimer
128c2ecf20Sopenharmony_ci * in the documentation and/or other materials provided with the
138c2ecf20Sopenharmony_ci * distribution.
148c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
158c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
168c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
178c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
188c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
198c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
208c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
218c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
228c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
238c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
248c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258c2ecf20Sopenharmony_ci * You can contact the author at :
268c2ecf20Sopenharmony_ci *	- LZ4 homepage : http://www.lz4.org
278c2ecf20Sopenharmony_ci *	- LZ4 source repository : https://github.com/lz4/lz4
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci *	Changed for kernel usage by:
308c2ecf20Sopenharmony_ci *	Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/*-************************************
348c2ecf20Sopenharmony_ci *	Dependencies
358c2ecf20Sopenharmony_ci **************************************/
368c2ecf20Sopenharmony_ci#include <linux/lz4.h>
378c2ecf20Sopenharmony_ci#include "lz4defs.h"
388c2ecf20Sopenharmony_ci#include <linux/init.h>
398c2ecf20Sopenharmony_ci#include <linux/module.h>
408c2ecf20Sopenharmony_ci#include <linux/kernel.h>
418c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/*-*****************************
448c2ecf20Sopenharmony_ci *	Decompression functions
458c2ecf20Sopenharmony_ci *******************************/
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define DEBUGLOG(l, ...) {}	/* disabled */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#ifndef assert
508c2ecf20Sopenharmony_ci#define assert(condition) ((void)0)
518c2ecf20Sopenharmony_ci#endif
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/*
548c2ecf20Sopenharmony_ci * LZ4_decompress_generic() :
558c2ecf20Sopenharmony_ci * This generic decompression function covers all use cases.
568c2ecf20Sopenharmony_ci * It shall be instantiated several times, using different sets of directives.
578c2ecf20Sopenharmony_ci * Note that it is important for performance that this function really get inlined,
588c2ecf20Sopenharmony_ci * in order to remove useless branches during compilation optimization.
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_cistatic FORCE_INLINE int LZ4_decompress_generic(
618c2ecf20Sopenharmony_ci	 const char * const src,
628c2ecf20Sopenharmony_ci	 char * const dst,
638c2ecf20Sopenharmony_ci	 int srcSize,
648c2ecf20Sopenharmony_ci		/*
658c2ecf20Sopenharmony_ci		 * If endOnInput == endOnInputSize,
668c2ecf20Sopenharmony_ci		 * this value is `dstCapacity`
678c2ecf20Sopenharmony_ci		 */
688c2ecf20Sopenharmony_ci	 int outputSize,
698c2ecf20Sopenharmony_ci	 /* endOnOutputSize, endOnInputSize */
708c2ecf20Sopenharmony_ci	 endCondition_directive endOnInput,
718c2ecf20Sopenharmony_ci	 /* full, partial */
728c2ecf20Sopenharmony_ci	 earlyEnd_directive partialDecoding,
738c2ecf20Sopenharmony_ci	 /* noDict, withPrefix64k, usingExtDict */
748c2ecf20Sopenharmony_ci	 dict_directive dict,
758c2ecf20Sopenharmony_ci	 /* always <= dst, == dst when no prefix */
768c2ecf20Sopenharmony_ci	 const BYTE * const lowPrefix,
778c2ecf20Sopenharmony_ci	 /* only if dict == usingExtDict */
788c2ecf20Sopenharmony_ci	 const BYTE * const dictStart,
798c2ecf20Sopenharmony_ci	 /* note : = 0 if noDict */
808c2ecf20Sopenharmony_ci	 const size_t dictSize
818c2ecf20Sopenharmony_ci	 )
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	const BYTE *ip = (const BYTE *) src;
848c2ecf20Sopenharmony_ci	const BYTE * const iend = ip + srcSize;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	BYTE *op = (BYTE *) dst;
878c2ecf20Sopenharmony_ci	BYTE * const oend = op + outputSize;
888c2ecf20Sopenharmony_ci	BYTE *cpy;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize;
918c2ecf20Sopenharmony_ci	static const unsigned int inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4};
928c2ecf20Sopenharmony_ci	static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3};
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	const int safeDecode = (endOnInput == endOnInputSize);
958c2ecf20Sopenharmony_ci	const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB)));
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/* Set up the "end" pointers for the shortcut. */
988c2ecf20Sopenharmony_ci	const BYTE *const shortiend = iend -
998c2ecf20Sopenharmony_ci		(endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/;
1008c2ecf20Sopenharmony_ci	const BYTE *const shortoend = oend -
1018c2ecf20Sopenharmony_ci		(endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	DEBUGLOG(5, "%s (srcSize:%i, dstSize:%i)", __func__,
1048c2ecf20Sopenharmony_ci		 srcSize, outputSize);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	/* Special cases */
1078c2ecf20Sopenharmony_ci	assert(lowPrefix <= op);
1088c2ecf20Sopenharmony_ci	assert(src != NULL);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* Empty output buffer */
1118c2ecf20Sopenharmony_ci	if ((endOnInput) && (unlikely(outputSize == 0)))
1128c2ecf20Sopenharmony_ci		return ((srcSize == 1) && (*ip == 0)) ? 0 : -1;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	if ((!endOnInput) && (unlikely(outputSize == 0)))
1158c2ecf20Sopenharmony_ci		return (*ip == 0 ? 1 : -1);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if ((endOnInput) && unlikely(srcSize == 0))
1188c2ecf20Sopenharmony_ci		return -1;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* Main Loop : decode sequences */
1218c2ecf20Sopenharmony_ci	while (1) {
1228c2ecf20Sopenharmony_ci		size_t length;
1238c2ecf20Sopenharmony_ci		const BYTE *match;
1248c2ecf20Sopenharmony_ci		size_t offset;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci		/* get literal length */
1278c2ecf20Sopenharmony_ci		unsigned int const token = *ip++;
1288c2ecf20Sopenharmony_ci		length = token>>ML_BITS;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci		/* ip < iend before the increment */
1318c2ecf20Sopenharmony_ci		assert(!endOnInput || ip <= iend);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci		/*
1348c2ecf20Sopenharmony_ci		 * A two-stage shortcut for the most common case:
1358c2ecf20Sopenharmony_ci		 * 1) If the literal length is 0..14, and there is enough
1368c2ecf20Sopenharmony_ci		 * space, enter the shortcut and copy 16 bytes on behalf
1378c2ecf20Sopenharmony_ci		 * of the literals (in the fast mode, only 8 bytes can be
1388c2ecf20Sopenharmony_ci		 * safely copied this way).
1398c2ecf20Sopenharmony_ci		 * 2) Further if the match length is 4..18, copy 18 bytes
1408c2ecf20Sopenharmony_ci		 * in a similar manner; but we ensure that there's enough
1418c2ecf20Sopenharmony_ci		 * space in the output for those 18 bytes earlier, upon
1428c2ecf20Sopenharmony_ci		 * entering the shortcut (in other words, there is a
1438c2ecf20Sopenharmony_ci		 * combined check for both stages).
1448c2ecf20Sopenharmony_ci		 *
1458c2ecf20Sopenharmony_ci		 * The & in the likely() below is intentionally not && so that
1468c2ecf20Sopenharmony_ci		 * some compilers can produce better parallelized runtime code
1478c2ecf20Sopenharmony_ci		 */
1488c2ecf20Sopenharmony_ci		if ((endOnInput ? length != RUN_MASK : length <= 8)
1498c2ecf20Sopenharmony_ci		   /*
1508c2ecf20Sopenharmony_ci		    * strictly "less than" on input, to re-enter
1518c2ecf20Sopenharmony_ci		    * the loop with at least one byte
1528c2ecf20Sopenharmony_ci		    */
1538c2ecf20Sopenharmony_ci		   && likely((endOnInput ? ip < shortiend : 1) &
1548c2ecf20Sopenharmony_ci			     (op <= shortoend))) {
1558c2ecf20Sopenharmony_ci			/* Copy the literals */
1568c2ecf20Sopenharmony_ci			LZ4_memcpy(op, ip, endOnInput ? 16 : 8);
1578c2ecf20Sopenharmony_ci			op += length; ip += length;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci			/*
1608c2ecf20Sopenharmony_ci			 * The second stage:
1618c2ecf20Sopenharmony_ci			 * prepare for match copying, decode full info.
1628c2ecf20Sopenharmony_ci			 * If it doesn't work out, the info won't be wasted.
1638c2ecf20Sopenharmony_ci			 */
1648c2ecf20Sopenharmony_ci			length = token & ML_MASK; /* match length */
1658c2ecf20Sopenharmony_ci			offset = LZ4_readLE16(ip);
1668c2ecf20Sopenharmony_ci			ip += 2;
1678c2ecf20Sopenharmony_ci			match = op - offset;
1688c2ecf20Sopenharmony_ci			assert(match <= op); /* check overflow */
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci			/* Do not deal with overlapping matches. */
1718c2ecf20Sopenharmony_ci			if ((length != ML_MASK) &&
1728c2ecf20Sopenharmony_ci			    (offset >= 8) &&
1738c2ecf20Sopenharmony_ci			    (dict == withPrefix64k || match >= lowPrefix)) {
1748c2ecf20Sopenharmony_ci				/* Copy the match. */
1758c2ecf20Sopenharmony_ci				LZ4_memcpy(op + 0, match + 0, 8);
1768c2ecf20Sopenharmony_ci				LZ4_memcpy(op + 8, match + 8, 8);
1778c2ecf20Sopenharmony_ci				LZ4_memcpy(op + 16, match + 16, 2);
1788c2ecf20Sopenharmony_ci				op += length + MINMATCH;
1798c2ecf20Sopenharmony_ci				/* Both stages worked, load the next token. */
1808c2ecf20Sopenharmony_ci				continue;
1818c2ecf20Sopenharmony_ci			}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci			/*
1848c2ecf20Sopenharmony_ci			 * The second stage didn't work out, but the info
1858c2ecf20Sopenharmony_ci			 * is ready. Propel it right to the point of match
1868c2ecf20Sopenharmony_ci			 * copying.
1878c2ecf20Sopenharmony_ci			 */
1888c2ecf20Sopenharmony_ci			goto _copy_match;
1898c2ecf20Sopenharmony_ci		}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci		/* decode literal length */
1928c2ecf20Sopenharmony_ci		if (length == RUN_MASK) {
1938c2ecf20Sopenharmony_ci			unsigned int s;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci			if (unlikely(endOnInput ? ip >= iend - RUN_MASK : 0)) {
1968c2ecf20Sopenharmony_ci				/* overflow detection */
1978c2ecf20Sopenharmony_ci				goto _output_error;
1988c2ecf20Sopenharmony_ci			}
1998c2ecf20Sopenharmony_ci			do {
2008c2ecf20Sopenharmony_ci				s = *ip++;
2018c2ecf20Sopenharmony_ci				length += s;
2028c2ecf20Sopenharmony_ci			} while (likely(endOnInput
2038c2ecf20Sopenharmony_ci				? ip < iend - RUN_MASK
2048c2ecf20Sopenharmony_ci				: 1) & (s == 255));
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci			if ((safeDecode)
2078c2ecf20Sopenharmony_ci			    && unlikely((uptrval)(op) +
2088c2ecf20Sopenharmony_ci					length < (uptrval)(op))) {
2098c2ecf20Sopenharmony_ci				/* overflow detection */
2108c2ecf20Sopenharmony_ci				goto _output_error;
2118c2ecf20Sopenharmony_ci			}
2128c2ecf20Sopenharmony_ci			if ((safeDecode)
2138c2ecf20Sopenharmony_ci			    && unlikely((uptrval)(ip) +
2148c2ecf20Sopenharmony_ci					length < (uptrval)(ip))) {
2158c2ecf20Sopenharmony_ci				/* overflow detection */
2168c2ecf20Sopenharmony_ci				goto _output_error;
2178c2ecf20Sopenharmony_ci			}
2188c2ecf20Sopenharmony_ci		}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci		/* copy literals */
2218c2ecf20Sopenharmony_ci		cpy = op + length;
2228c2ecf20Sopenharmony_ci		LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci		if (((endOnInput) && ((cpy > oend - MFLIMIT)
2258c2ecf20Sopenharmony_ci			|| (ip + length > iend - (2 + 1 + LASTLITERALS))))
2268c2ecf20Sopenharmony_ci			|| ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
2278c2ecf20Sopenharmony_ci			if (partialDecoding) {
2288c2ecf20Sopenharmony_ci				if (cpy > oend) {
2298c2ecf20Sopenharmony_ci					/*
2308c2ecf20Sopenharmony_ci					 * Partial decoding :
2318c2ecf20Sopenharmony_ci					 * stop in the middle of literal segment
2328c2ecf20Sopenharmony_ci					 */
2338c2ecf20Sopenharmony_ci					cpy = oend;
2348c2ecf20Sopenharmony_ci					length = oend - op;
2358c2ecf20Sopenharmony_ci				}
2368c2ecf20Sopenharmony_ci				if ((endOnInput)
2378c2ecf20Sopenharmony_ci					&& (ip + length > iend)) {
2388c2ecf20Sopenharmony_ci					/*
2398c2ecf20Sopenharmony_ci					 * Error :
2408c2ecf20Sopenharmony_ci					 * read attempt beyond
2418c2ecf20Sopenharmony_ci					 * end of input buffer
2428c2ecf20Sopenharmony_ci					 */
2438c2ecf20Sopenharmony_ci					goto _output_error;
2448c2ecf20Sopenharmony_ci				}
2458c2ecf20Sopenharmony_ci			} else {
2468c2ecf20Sopenharmony_ci				if ((!endOnInput)
2478c2ecf20Sopenharmony_ci					&& (cpy != oend)) {
2488c2ecf20Sopenharmony_ci					/*
2498c2ecf20Sopenharmony_ci					 * Error :
2508c2ecf20Sopenharmony_ci					 * block decoding must
2518c2ecf20Sopenharmony_ci					 * stop exactly there
2528c2ecf20Sopenharmony_ci					 */
2538c2ecf20Sopenharmony_ci					goto _output_error;
2548c2ecf20Sopenharmony_ci				}
2558c2ecf20Sopenharmony_ci				if ((endOnInput)
2568c2ecf20Sopenharmony_ci					&& ((ip + length != iend)
2578c2ecf20Sopenharmony_ci					|| (cpy > oend))) {
2588c2ecf20Sopenharmony_ci					/*
2598c2ecf20Sopenharmony_ci					 * Error :
2608c2ecf20Sopenharmony_ci					 * input must be consumed
2618c2ecf20Sopenharmony_ci					 */
2628c2ecf20Sopenharmony_ci					goto _output_error;
2638c2ecf20Sopenharmony_ci				}
2648c2ecf20Sopenharmony_ci			}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci			/*
2678c2ecf20Sopenharmony_ci			 * supports overlapping memory regions; only matters
2688c2ecf20Sopenharmony_ci			 * for in-place decompression scenarios
2698c2ecf20Sopenharmony_ci			 */
2708c2ecf20Sopenharmony_ci			LZ4_memmove(op, ip, length);
2718c2ecf20Sopenharmony_ci			ip += length;
2728c2ecf20Sopenharmony_ci			op += length;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci			/* Necessarily EOF when !partialDecoding.
2758c2ecf20Sopenharmony_ci			 * When partialDecoding, it is EOF if we've either
2768c2ecf20Sopenharmony_ci			 * filled the output buffer or
2778c2ecf20Sopenharmony_ci			 * can't proceed with reading an offset for following match.
2788c2ecf20Sopenharmony_ci			 */
2798c2ecf20Sopenharmony_ci			if (!partialDecoding || (cpy == oend) || (ip >= (iend - 2)))
2808c2ecf20Sopenharmony_ci				break;
2818c2ecf20Sopenharmony_ci		} else {
2828c2ecf20Sopenharmony_ci			/* may overwrite up to WILDCOPYLENGTH beyond cpy */
2838c2ecf20Sopenharmony_ci			LZ4_wildCopy(op, ip, cpy);
2848c2ecf20Sopenharmony_ci			ip += length;
2858c2ecf20Sopenharmony_ci			op = cpy;
2868c2ecf20Sopenharmony_ci		}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci		/* get offset */
2898c2ecf20Sopenharmony_ci		offset = LZ4_readLE16(ip);
2908c2ecf20Sopenharmony_ci		ip += 2;
2918c2ecf20Sopenharmony_ci		match = op - offset;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci		/* get matchlength */
2948c2ecf20Sopenharmony_ci		length = token & ML_MASK;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci_copy_match:
2978c2ecf20Sopenharmony_ci		if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) {
2988c2ecf20Sopenharmony_ci			/* Error : offset outside buffers */
2998c2ecf20Sopenharmony_ci			goto _output_error;
3008c2ecf20Sopenharmony_ci		}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci		/* costs ~1%; silence an msan warning when offset == 0 */
3038c2ecf20Sopenharmony_ci		/*
3048c2ecf20Sopenharmony_ci		 * note : when partialDecoding, there is no guarantee that
3058c2ecf20Sopenharmony_ci		 * at least 4 bytes remain available in output buffer
3068c2ecf20Sopenharmony_ci		 */
3078c2ecf20Sopenharmony_ci		if (!partialDecoding) {
3088c2ecf20Sopenharmony_ci			assert(oend > op);
3098c2ecf20Sopenharmony_ci			assert(oend - op >= 4);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci			LZ4_write32(op, (U32)offset);
3128c2ecf20Sopenharmony_ci		}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci		if (length == ML_MASK) {
3158c2ecf20Sopenharmony_ci			unsigned int s;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci			do {
3188c2ecf20Sopenharmony_ci				s = *ip++;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci				if ((endOnInput) && (ip > iend - LASTLITERALS))
3218c2ecf20Sopenharmony_ci					goto _output_error;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci				length += s;
3248c2ecf20Sopenharmony_ci			} while (s == 255);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci			if ((safeDecode)
3278c2ecf20Sopenharmony_ci				&& unlikely(
3288c2ecf20Sopenharmony_ci					(uptrval)(op) + length < (uptrval)op)) {
3298c2ecf20Sopenharmony_ci				/* overflow detection */
3308c2ecf20Sopenharmony_ci				goto _output_error;
3318c2ecf20Sopenharmony_ci			}
3328c2ecf20Sopenharmony_ci		}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci		length += MINMATCH;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci		/* match starting within external dictionary */
3378c2ecf20Sopenharmony_ci		if ((dict == usingExtDict) && (match < lowPrefix)) {
3388c2ecf20Sopenharmony_ci			if (unlikely(op + length > oend - LASTLITERALS)) {
3398c2ecf20Sopenharmony_ci				/* doesn't respect parsing restriction */
3408c2ecf20Sopenharmony_ci				if (!partialDecoding)
3418c2ecf20Sopenharmony_ci					goto _output_error;
3428c2ecf20Sopenharmony_ci				length = min(length, (size_t)(oend - op));
3438c2ecf20Sopenharmony_ci			}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci			if (length <= (size_t)(lowPrefix - match)) {
3468c2ecf20Sopenharmony_ci				/*
3478c2ecf20Sopenharmony_ci				 * match fits entirely within external
3488c2ecf20Sopenharmony_ci				 * dictionary : just copy
3498c2ecf20Sopenharmony_ci				 */
3508c2ecf20Sopenharmony_ci				memmove(op, dictEnd - (lowPrefix - match),
3518c2ecf20Sopenharmony_ci					length);
3528c2ecf20Sopenharmony_ci				op += length;
3538c2ecf20Sopenharmony_ci			} else {
3548c2ecf20Sopenharmony_ci				/*
3558c2ecf20Sopenharmony_ci				 * match stretches into both external
3568c2ecf20Sopenharmony_ci				 * dictionary and current block
3578c2ecf20Sopenharmony_ci				 */
3588c2ecf20Sopenharmony_ci				size_t const copySize = (size_t)(lowPrefix - match);
3598c2ecf20Sopenharmony_ci				size_t const restSize = length - copySize;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci				LZ4_memcpy(op, dictEnd - copySize, copySize);
3628c2ecf20Sopenharmony_ci				op += copySize;
3638c2ecf20Sopenharmony_ci				if (restSize > (size_t)(op - lowPrefix)) {
3648c2ecf20Sopenharmony_ci					/* overlap copy */
3658c2ecf20Sopenharmony_ci					BYTE * const endOfMatch = op + restSize;
3668c2ecf20Sopenharmony_ci					const BYTE *copyFrom = lowPrefix;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci					while (op < endOfMatch)
3698c2ecf20Sopenharmony_ci						*op++ = *copyFrom++;
3708c2ecf20Sopenharmony_ci				} else {
3718c2ecf20Sopenharmony_ci					LZ4_memcpy(op, lowPrefix, restSize);
3728c2ecf20Sopenharmony_ci					op += restSize;
3738c2ecf20Sopenharmony_ci				}
3748c2ecf20Sopenharmony_ci			}
3758c2ecf20Sopenharmony_ci			continue;
3768c2ecf20Sopenharmony_ci		}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci		/* copy match within block */
3798c2ecf20Sopenharmony_ci		cpy = op + length;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci		/*
3828c2ecf20Sopenharmony_ci		 * partialDecoding :
3838c2ecf20Sopenharmony_ci		 * may not respect endBlock parsing restrictions
3848c2ecf20Sopenharmony_ci		 */
3858c2ecf20Sopenharmony_ci		assert(op <= oend);
3868c2ecf20Sopenharmony_ci		if (partialDecoding &&
3878c2ecf20Sopenharmony_ci		    (cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
3888c2ecf20Sopenharmony_ci			size_t const mlen = min(length, (size_t)(oend - op));
3898c2ecf20Sopenharmony_ci			const BYTE * const matchEnd = match + mlen;
3908c2ecf20Sopenharmony_ci			BYTE * const copyEnd = op + mlen;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci			if (matchEnd > op) {
3938c2ecf20Sopenharmony_ci				/* overlap copy */
3948c2ecf20Sopenharmony_ci				while (op < copyEnd)
3958c2ecf20Sopenharmony_ci					*op++ = *match++;
3968c2ecf20Sopenharmony_ci			} else {
3978c2ecf20Sopenharmony_ci				LZ4_memcpy(op, match, mlen);
3988c2ecf20Sopenharmony_ci			}
3998c2ecf20Sopenharmony_ci			op = copyEnd;
4008c2ecf20Sopenharmony_ci			if (op == oend)
4018c2ecf20Sopenharmony_ci				break;
4028c2ecf20Sopenharmony_ci			continue;
4038c2ecf20Sopenharmony_ci		}
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci		if (unlikely(offset < 8)) {
4068c2ecf20Sopenharmony_ci			op[0] = match[0];
4078c2ecf20Sopenharmony_ci			op[1] = match[1];
4088c2ecf20Sopenharmony_ci			op[2] = match[2];
4098c2ecf20Sopenharmony_ci			op[3] = match[3];
4108c2ecf20Sopenharmony_ci			match += inc32table[offset];
4118c2ecf20Sopenharmony_ci			LZ4_memcpy(op + 4, match, 4);
4128c2ecf20Sopenharmony_ci			match -= dec64table[offset];
4138c2ecf20Sopenharmony_ci		} else {
4148c2ecf20Sopenharmony_ci			LZ4_copy8(op, match);
4158c2ecf20Sopenharmony_ci			match += 8;
4168c2ecf20Sopenharmony_ci		}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci		op += 8;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci		if (unlikely(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
4218c2ecf20Sopenharmony_ci			BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci			if (cpy > oend - LASTLITERALS) {
4248c2ecf20Sopenharmony_ci				/*
4258c2ecf20Sopenharmony_ci				 * Error : last LASTLITERALS bytes
4268c2ecf20Sopenharmony_ci				 * must be literals (uncompressed)
4278c2ecf20Sopenharmony_ci				 */
4288c2ecf20Sopenharmony_ci				goto _output_error;
4298c2ecf20Sopenharmony_ci			}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci			if (op < oCopyLimit) {
4328c2ecf20Sopenharmony_ci				LZ4_wildCopy(op, match, oCopyLimit);
4338c2ecf20Sopenharmony_ci				match += oCopyLimit - op;
4348c2ecf20Sopenharmony_ci				op = oCopyLimit;
4358c2ecf20Sopenharmony_ci			}
4368c2ecf20Sopenharmony_ci			while (op < cpy)
4378c2ecf20Sopenharmony_ci				*op++ = *match++;
4388c2ecf20Sopenharmony_ci		} else {
4398c2ecf20Sopenharmony_ci			LZ4_copy8(op, match);
4408c2ecf20Sopenharmony_ci			if (length > 16)
4418c2ecf20Sopenharmony_ci				LZ4_wildCopy(op + 8, match + 8, cpy);
4428c2ecf20Sopenharmony_ci		}
4438c2ecf20Sopenharmony_ci		op = cpy; /* wildcopy correction */
4448c2ecf20Sopenharmony_ci	}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/* end of decoding */
4478c2ecf20Sopenharmony_ci	if (endOnInput) {
4488c2ecf20Sopenharmony_ci		/* Nb of output bytes decoded */
4498c2ecf20Sopenharmony_ci		return (int) (((char *)op) - dst);
4508c2ecf20Sopenharmony_ci	} else {
4518c2ecf20Sopenharmony_ci		/* Nb of input bytes read */
4528c2ecf20Sopenharmony_ci		return (int) (((const char *)ip) - src);
4538c2ecf20Sopenharmony_ci	}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	/* Overflow error detected */
4568c2ecf20Sopenharmony_ci_output_error:
4578c2ecf20Sopenharmony_ci	return (int) (-(((const char *)ip) - src)) - 1;
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ciint LZ4_decompress_safe(const char *source, char *dest,
4618c2ecf20Sopenharmony_ci	int compressedSize, int maxDecompressedSize)
4628c2ecf20Sopenharmony_ci{
4638c2ecf20Sopenharmony_ci	return LZ4_decompress_generic(source, dest,
4648c2ecf20Sopenharmony_ci				      compressedSize, maxDecompressedSize,
4658c2ecf20Sopenharmony_ci				      endOnInputSize, decode_full_block,
4668c2ecf20Sopenharmony_ci				      noDict, (BYTE *)dest, NULL, 0);
4678c2ecf20Sopenharmony_ci}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ciint LZ4_decompress_safe_partial(const char *src, char *dst,
4708c2ecf20Sopenharmony_ci	int compressedSize, int targetOutputSize, int dstCapacity)
4718c2ecf20Sopenharmony_ci{
4728c2ecf20Sopenharmony_ci	dstCapacity = min(targetOutputSize, dstCapacity);
4738c2ecf20Sopenharmony_ci	return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
4748c2ecf20Sopenharmony_ci				      endOnInputSize, partial_decode,
4758c2ecf20Sopenharmony_ci				      noDict, (BYTE *)dst, NULL, 0);
4768c2ecf20Sopenharmony_ci}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ciint LZ4_decompress_fast(const char *source, char *dest, int originalSize)
4798c2ecf20Sopenharmony_ci{
4808c2ecf20Sopenharmony_ci	return LZ4_decompress_generic(source, dest, 0, originalSize,
4818c2ecf20Sopenharmony_ci				      endOnOutputSize, decode_full_block,
4828c2ecf20Sopenharmony_ci				      withPrefix64k,
4838c2ecf20Sopenharmony_ci				      (BYTE *)dest - 64 * KB, NULL, 0);
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci/* ===== Instantiate a few more decoding cases, used more than once. ===== */
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ciint LZ4_decompress_safe_withPrefix64k(const char *source, char *dest,
4898c2ecf20Sopenharmony_ci				      int compressedSize, int maxOutputSize)
4908c2ecf20Sopenharmony_ci{
4918c2ecf20Sopenharmony_ci	return LZ4_decompress_generic(source, dest,
4928c2ecf20Sopenharmony_ci				      compressedSize, maxOutputSize,
4938c2ecf20Sopenharmony_ci				      endOnInputSize, decode_full_block,
4948c2ecf20Sopenharmony_ci				      withPrefix64k,
4958c2ecf20Sopenharmony_ci				      (BYTE *)dest - 64 * KB, NULL, 0);
4968c2ecf20Sopenharmony_ci}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_cistatic int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest,
4998c2ecf20Sopenharmony_ci					       int compressedSize,
5008c2ecf20Sopenharmony_ci					       int maxOutputSize,
5018c2ecf20Sopenharmony_ci					       size_t prefixSize)
5028c2ecf20Sopenharmony_ci{
5038c2ecf20Sopenharmony_ci	return LZ4_decompress_generic(source, dest,
5048c2ecf20Sopenharmony_ci				      compressedSize, maxOutputSize,
5058c2ecf20Sopenharmony_ci				      endOnInputSize, decode_full_block,
5068c2ecf20Sopenharmony_ci				      noDict,
5078c2ecf20Sopenharmony_ci				      (BYTE *)dest - prefixSize, NULL, 0);
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ciint LZ4_decompress_safe_forceExtDict(const char *source, char *dest,
5118c2ecf20Sopenharmony_ci				     int compressedSize, int maxOutputSize,
5128c2ecf20Sopenharmony_ci				     const void *dictStart, size_t dictSize)
5138c2ecf20Sopenharmony_ci{
5148c2ecf20Sopenharmony_ci	return LZ4_decompress_generic(source, dest,
5158c2ecf20Sopenharmony_ci				      compressedSize, maxOutputSize,
5168c2ecf20Sopenharmony_ci				      endOnInputSize, decode_full_block,
5178c2ecf20Sopenharmony_ci				      usingExtDict, (BYTE *)dest,
5188c2ecf20Sopenharmony_ci				      (const BYTE *)dictStart, dictSize);
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_cistatic int LZ4_decompress_fast_extDict(const char *source, char *dest,
5228c2ecf20Sopenharmony_ci				       int originalSize,
5238c2ecf20Sopenharmony_ci				       const void *dictStart, size_t dictSize)
5248c2ecf20Sopenharmony_ci{
5258c2ecf20Sopenharmony_ci	return LZ4_decompress_generic(source, dest,
5268c2ecf20Sopenharmony_ci				      0, originalSize,
5278c2ecf20Sopenharmony_ci				      endOnOutputSize, decode_full_block,
5288c2ecf20Sopenharmony_ci				      usingExtDict, (BYTE *)dest,
5298c2ecf20Sopenharmony_ci				      (const BYTE *)dictStart, dictSize);
5308c2ecf20Sopenharmony_ci}
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci/*
5338c2ecf20Sopenharmony_ci * The "double dictionary" mode, for use with e.g. ring buffers: the first part
5348c2ecf20Sopenharmony_ci * of the dictionary is passed as prefix, and the second via dictStart + dictSize.
5358c2ecf20Sopenharmony_ci * These routines are used only once, in LZ4_decompress_*_continue().
5368c2ecf20Sopenharmony_ci */
5378c2ecf20Sopenharmony_cistatic FORCE_INLINE
5388c2ecf20Sopenharmony_ciint LZ4_decompress_safe_doubleDict(const char *source, char *dest,
5398c2ecf20Sopenharmony_ci				   int compressedSize, int maxOutputSize,
5408c2ecf20Sopenharmony_ci				   size_t prefixSize,
5418c2ecf20Sopenharmony_ci				   const void *dictStart, size_t dictSize)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	return LZ4_decompress_generic(source, dest,
5448c2ecf20Sopenharmony_ci				      compressedSize, maxOutputSize,
5458c2ecf20Sopenharmony_ci				      endOnInputSize, decode_full_block,
5468c2ecf20Sopenharmony_ci				      usingExtDict, (BYTE *)dest - prefixSize,
5478c2ecf20Sopenharmony_ci				      (const BYTE *)dictStart, dictSize);
5488c2ecf20Sopenharmony_ci}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_cistatic FORCE_INLINE
5518c2ecf20Sopenharmony_ciint LZ4_decompress_fast_doubleDict(const char *source, char *dest,
5528c2ecf20Sopenharmony_ci				   int originalSize, size_t prefixSize,
5538c2ecf20Sopenharmony_ci				   const void *dictStart, size_t dictSize)
5548c2ecf20Sopenharmony_ci{
5558c2ecf20Sopenharmony_ci	return LZ4_decompress_generic(source, dest,
5568c2ecf20Sopenharmony_ci				      0, originalSize,
5578c2ecf20Sopenharmony_ci				      endOnOutputSize, decode_full_block,
5588c2ecf20Sopenharmony_ci				      usingExtDict, (BYTE *)dest - prefixSize,
5598c2ecf20Sopenharmony_ci				      (const BYTE *)dictStart, dictSize);
5608c2ecf20Sopenharmony_ci}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci/* ===== streaming decompression functions ===== */
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ciint LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
5658c2ecf20Sopenharmony_ci	const char *dictionary, int dictSize)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	LZ4_streamDecode_t_internal *lz4sd =
5688c2ecf20Sopenharmony_ci		&LZ4_streamDecode->internal_donotuse;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	lz4sd->prefixSize = (size_t) dictSize;
5718c2ecf20Sopenharmony_ci	lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize;
5728c2ecf20Sopenharmony_ci	lz4sd->externalDict = NULL;
5738c2ecf20Sopenharmony_ci	lz4sd->extDictSize	= 0;
5748c2ecf20Sopenharmony_ci	return 1;
5758c2ecf20Sopenharmony_ci}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci/*
5788c2ecf20Sopenharmony_ci * *_continue() :
5798c2ecf20Sopenharmony_ci * These decoding functions allow decompression of multiple blocks
5808c2ecf20Sopenharmony_ci * in "streaming" mode.
5818c2ecf20Sopenharmony_ci * Previously decoded blocks must still be available at the memory
5828c2ecf20Sopenharmony_ci * position where they were decoded.
5838c2ecf20Sopenharmony_ci * If it's not possible, save the relevant part of
5848c2ecf20Sopenharmony_ci * decoded data into a safe buffer,
5858c2ecf20Sopenharmony_ci * and indicate where it stands using LZ4_setStreamDecode()
5868c2ecf20Sopenharmony_ci */
5878c2ecf20Sopenharmony_ciint LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
5888c2ecf20Sopenharmony_ci	const char *source, char *dest, int compressedSize, int maxOutputSize)
5898c2ecf20Sopenharmony_ci{
5908c2ecf20Sopenharmony_ci	LZ4_streamDecode_t_internal *lz4sd =
5918c2ecf20Sopenharmony_ci		&LZ4_streamDecode->internal_donotuse;
5928c2ecf20Sopenharmony_ci	int result;
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	if (lz4sd->prefixSize == 0) {
5958c2ecf20Sopenharmony_ci		/* The first call, no dictionary yet. */
5968c2ecf20Sopenharmony_ci		assert(lz4sd->extDictSize == 0);
5978c2ecf20Sopenharmony_ci		result = LZ4_decompress_safe(source, dest,
5988c2ecf20Sopenharmony_ci			compressedSize, maxOutputSize);
5998c2ecf20Sopenharmony_ci		if (result <= 0)
6008c2ecf20Sopenharmony_ci			return result;
6018c2ecf20Sopenharmony_ci		lz4sd->prefixSize = result;
6028c2ecf20Sopenharmony_ci		lz4sd->prefixEnd = (BYTE *)dest + result;
6038c2ecf20Sopenharmony_ci	} else if (lz4sd->prefixEnd == (BYTE *)dest) {
6048c2ecf20Sopenharmony_ci		/* They're rolling the current segment. */
6058c2ecf20Sopenharmony_ci		if (lz4sd->prefixSize >= 64 * KB - 1)
6068c2ecf20Sopenharmony_ci			result = LZ4_decompress_safe_withPrefix64k(source, dest,
6078c2ecf20Sopenharmony_ci				compressedSize, maxOutputSize);
6088c2ecf20Sopenharmony_ci		else if (lz4sd->extDictSize == 0)
6098c2ecf20Sopenharmony_ci			result = LZ4_decompress_safe_withSmallPrefix(source,
6108c2ecf20Sopenharmony_ci				dest, compressedSize, maxOutputSize,
6118c2ecf20Sopenharmony_ci				lz4sd->prefixSize);
6128c2ecf20Sopenharmony_ci		else
6138c2ecf20Sopenharmony_ci			result = LZ4_decompress_safe_doubleDict(source, dest,
6148c2ecf20Sopenharmony_ci				compressedSize, maxOutputSize,
6158c2ecf20Sopenharmony_ci				lz4sd->prefixSize,
6168c2ecf20Sopenharmony_ci				lz4sd->externalDict, lz4sd->extDictSize);
6178c2ecf20Sopenharmony_ci		if (result <= 0)
6188c2ecf20Sopenharmony_ci			return result;
6198c2ecf20Sopenharmony_ci		lz4sd->prefixSize += result;
6208c2ecf20Sopenharmony_ci		lz4sd->prefixEnd  += result;
6218c2ecf20Sopenharmony_ci	} else {
6228c2ecf20Sopenharmony_ci		/*
6238c2ecf20Sopenharmony_ci		 * The buffer wraps around, or they're
6248c2ecf20Sopenharmony_ci		 * switching to another buffer.
6258c2ecf20Sopenharmony_ci		 */
6268c2ecf20Sopenharmony_ci		lz4sd->extDictSize = lz4sd->prefixSize;
6278c2ecf20Sopenharmony_ci		lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
6288c2ecf20Sopenharmony_ci		result = LZ4_decompress_safe_forceExtDict(source, dest,
6298c2ecf20Sopenharmony_ci			compressedSize, maxOutputSize,
6308c2ecf20Sopenharmony_ci			lz4sd->externalDict, lz4sd->extDictSize);
6318c2ecf20Sopenharmony_ci		if (result <= 0)
6328c2ecf20Sopenharmony_ci			return result;
6338c2ecf20Sopenharmony_ci		lz4sd->prefixSize = result;
6348c2ecf20Sopenharmony_ci		lz4sd->prefixEnd  = (BYTE *)dest + result;
6358c2ecf20Sopenharmony_ci	}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	return result;
6388c2ecf20Sopenharmony_ci}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ciint LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
6418c2ecf20Sopenharmony_ci	const char *source, char *dest, int originalSize)
6428c2ecf20Sopenharmony_ci{
6438c2ecf20Sopenharmony_ci	LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
6448c2ecf20Sopenharmony_ci	int result;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	if (lz4sd->prefixSize == 0) {
6478c2ecf20Sopenharmony_ci		assert(lz4sd->extDictSize == 0);
6488c2ecf20Sopenharmony_ci		result = LZ4_decompress_fast(source, dest, originalSize);
6498c2ecf20Sopenharmony_ci		if (result <= 0)
6508c2ecf20Sopenharmony_ci			return result;
6518c2ecf20Sopenharmony_ci		lz4sd->prefixSize = originalSize;
6528c2ecf20Sopenharmony_ci		lz4sd->prefixEnd = (BYTE *)dest + originalSize;
6538c2ecf20Sopenharmony_ci	} else if (lz4sd->prefixEnd == (BYTE *)dest) {
6548c2ecf20Sopenharmony_ci		if (lz4sd->prefixSize >= 64 * KB - 1 ||
6558c2ecf20Sopenharmony_ci		    lz4sd->extDictSize == 0)
6568c2ecf20Sopenharmony_ci			result = LZ4_decompress_fast(source, dest,
6578c2ecf20Sopenharmony_ci						     originalSize);
6588c2ecf20Sopenharmony_ci		else
6598c2ecf20Sopenharmony_ci			result = LZ4_decompress_fast_doubleDict(source, dest,
6608c2ecf20Sopenharmony_ci				originalSize, lz4sd->prefixSize,
6618c2ecf20Sopenharmony_ci				lz4sd->externalDict, lz4sd->extDictSize);
6628c2ecf20Sopenharmony_ci		if (result <= 0)
6638c2ecf20Sopenharmony_ci			return result;
6648c2ecf20Sopenharmony_ci		lz4sd->prefixSize += originalSize;
6658c2ecf20Sopenharmony_ci		lz4sd->prefixEnd  += originalSize;
6668c2ecf20Sopenharmony_ci	} else {
6678c2ecf20Sopenharmony_ci		lz4sd->extDictSize = lz4sd->prefixSize;
6688c2ecf20Sopenharmony_ci		lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
6698c2ecf20Sopenharmony_ci		result = LZ4_decompress_fast_extDict(source, dest,
6708c2ecf20Sopenharmony_ci			originalSize, lz4sd->externalDict, lz4sd->extDictSize);
6718c2ecf20Sopenharmony_ci		if (result <= 0)
6728c2ecf20Sopenharmony_ci			return result;
6738c2ecf20Sopenharmony_ci		lz4sd->prefixSize = originalSize;
6748c2ecf20Sopenharmony_ci		lz4sd->prefixEnd = (BYTE *)dest + originalSize;
6758c2ecf20Sopenharmony_ci	}
6768c2ecf20Sopenharmony_ci	return result;
6778c2ecf20Sopenharmony_ci}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ciint LZ4_decompress_safe_usingDict(const char *source, char *dest,
6808c2ecf20Sopenharmony_ci				  int compressedSize, int maxOutputSize,
6818c2ecf20Sopenharmony_ci				  const char *dictStart, int dictSize)
6828c2ecf20Sopenharmony_ci{
6838c2ecf20Sopenharmony_ci	if (dictSize == 0)
6848c2ecf20Sopenharmony_ci		return LZ4_decompress_safe(source, dest,
6858c2ecf20Sopenharmony_ci					   compressedSize, maxOutputSize);
6868c2ecf20Sopenharmony_ci	if (dictStart+dictSize == dest) {
6878c2ecf20Sopenharmony_ci		if (dictSize >= 64 * KB - 1)
6888c2ecf20Sopenharmony_ci			return LZ4_decompress_safe_withPrefix64k(source, dest,
6898c2ecf20Sopenharmony_ci				compressedSize, maxOutputSize);
6908c2ecf20Sopenharmony_ci		return LZ4_decompress_safe_withSmallPrefix(source, dest,
6918c2ecf20Sopenharmony_ci			compressedSize, maxOutputSize, dictSize);
6928c2ecf20Sopenharmony_ci	}
6938c2ecf20Sopenharmony_ci	return LZ4_decompress_safe_forceExtDict(source, dest,
6948c2ecf20Sopenharmony_ci		compressedSize, maxOutputSize, dictStart, dictSize);
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ciint LZ4_decompress_fast_usingDict(const char *source, char *dest,
6988c2ecf20Sopenharmony_ci				  int originalSize,
6998c2ecf20Sopenharmony_ci				  const char *dictStart, int dictSize)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	if (dictSize == 0 || dictStart + dictSize == dest)
7028c2ecf20Sopenharmony_ci		return LZ4_decompress_fast(source, dest, originalSize);
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	return LZ4_decompress_fast_extDict(source, dest, originalSize,
7058c2ecf20Sopenharmony_ci		dictStart, dictSize);
7068c2ecf20Sopenharmony_ci}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci#ifndef STATIC
7098c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_decompress_safe);
7108c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_decompress_safe_partial);
7118c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_decompress_fast);
7128c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_setStreamDecode);
7138c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_decompress_safe_continue);
7148c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_decompress_fast_continue);
7158c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_decompress_safe_usingDict);
7168c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_decompress_fast_usingDict);
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
7198c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("LZ4 decompressor");
7208c2ecf20Sopenharmony_ci#endif
721