xref: /openbmc/linux/lib/lz4/lz4_decompress.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1cffb78b0SKyungsik Lee /*
2cffb78b0SKyungsik Lee  * LZ4 - Fast LZ compression algorithm
34e1a33b1SSven Schmidt  * Copyright (C) 2011 - 2016, Yann Collet.
4cffb78b0SKyungsik Lee  * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php)
5cffb78b0SKyungsik Lee  * Redistribution and use in source and binary forms, with or without
6cffb78b0SKyungsik Lee  * modification, are permitted provided that the following conditions are
7cffb78b0SKyungsik Lee  * met:
8cffb78b0SKyungsik Lee  *	* Redistributions of source code must retain the above copyright
9cffb78b0SKyungsik Lee  *	  notice, this list of conditions and the following disclaimer.
10cffb78b0SKyungsik Lee  *	* Redistributions in binary form must reproduce the above
11cffb78b0SKyungsik Lee  * copyright notice, this list of conditions and the following disclaimer
12cffb78b0SKyungsik Lee  * in the documentation and/or other materials provided with the
13cffb78b0SKyungsik Lee  * distribution.
14cffb78b0SKyungsik Lee  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15cffb78b0SKyungsik Lee  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16cffb78b0SKyungsik Lee  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17cffb78b0SKyungsik Lee  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18cffb78b0SKyungsik Lee  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19cffb78b0SKyungsik Lee  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20cffb78b0SKyungsik Lee  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21cffb78b0SKyungsik Lee  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22cffb78b0SKyungsik Lee  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23cffb78b0SKyungsik Lee  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24cffb78b0SKyungsik Lee  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25cffb78b0SKyungsik Lee  * You can contact the author at :
264e1a33b1SSven Schmidt  *	- LZ4 homepage : http://www.lz4.org
274e1a33b1SSven Schmidt  *	- LZ4 source repository : https://github.com/lz4/lz4
284e1a33b1SSven Schmidt  *
294e1a33b1SSven Schmidt  *	Changed for kernel usage by:
304e1a33b1SSven Schmidt  *	Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
31cffb78b0SKyungsik Lee  */
32cffb78b0SKyungsik Lee 
334e1a33b1SSven Schmidt /*-************************************
344e1a33b1SSven Schmidt  *	Dependencies
354e1a33b1SSven Schmidt  **************************************/
364e1a33b1SSven Schmidt #include <linux/lz4.h>
374e1a33b1SSven Schmidt #include "lz4defs.h"
384e1a33b1SSven Schmidt #include <linux/init.h>
39cffb78b0SKyungsik Lee #include <linux/module.h>
40cffb78b0SKyungsik Lee #include <linux/kernel.h>
41cffb78b0SKyungsik Lee #include <asm/unaligned.h>
42cffb78b0SKyungsik Lee 
434e1a33b1SSven Schmidt /*-*****************************
444e1a33b1SSven Schmidt  *	Decompression functions
454e1a33b1SSven Schmidt  *******************************/
462209fda3SGao Xiang 
472209fda3SGao Xiang #define DEBUGLOG(l, ...) {}	/* disabled */
482209fda3SGao Xiang 
492209fda3SGao Xiang #ifndef assert
502209fda3SGao Xiang #define assert(condition) ((void)0)
512209fda3SGao Xiang #endif
522209fda3SGao Xiang 
532209fda3SGao Xiang /*
542209fda3SGao Xiang  * LZ4_decompress_generic() :
552209fda3SGao Xiang  * This generic decompression function covers all use cases.
562209fda3SGao Xiang  * It shall be instantiated several times, using different sets of directives.
572209fda3SGao Xiang  * Note that it is important for performance that this function really get inlined,
584e1a33b1SSven Schmidt  * in order to remove useless branches during compilation optimization.
594e1a33b1SSven Schmidt  */
LZ4_decompress_generic(const char * const src,char * const dst,int srcSize,int outputSize,endCondition_directive endOnInput,earlyEnd_directive partialDecoding,dict_directive dict,const BYTE * const lowPrefix,const BYTE * const dictStart,const size_t dictSize)604e1a33b1SSven Schmidt static FORCE_INLINE int LZ4_decompress_generic(
612209fda3SGao Xiang 	 const char * const src,
622209fda3SGao Xiang 	 char * const dst,
632209fda3SGao Xiang 	 int srcSize,
644e1a33b1SSven Schmidt 		/*
654e1a33b1SSven Schmidt 		 * If endOnInput == endOnInputSize,
662209fda3SGao Xiang 		 * this value is `dstCapacity`
674e1a33b1SSven Schmidt 		 */
684e1a33b1SSven Schmidt 	 int outputSize,
694e1a33b1SSven Schmidt 	 /* endOnOutputSize, endOnInputSize */
702209fda3SGao Xiang 	 endCondition_directive endOnInput,
714e1a33b1SSven Schmidt 	 /* full, partial */
722209fda3SGao Xiang 	 earlyEnd_directive partialDecoding,
734e1a33b1SSven Schmidt 	 /* noDict, withPrefix64k, usingExtDict */
742209fda3SGao Xiang 	 dict_directive dict,
752209fda3SGao Xiang 	 /* always <= dst, == dst when no prefix */
764e1a33b1SSven Schmidt 	 const BYTE * const lowPrefix,
774e1a33b1SSven Schmidt 	 /* only if dict == usingExtDict */
784e1a33b1SSven Schmidt 	 const BYTE * const dictStart,
794e1a33b1SSven Schmidt 	 /* note : = 0 if noDict */
804e1a33b1SSven Schmidt 	 const size_t dictSize
814e1a33b1SSven Schmidt 	 )
82cffb78b0SKyungsik Lee {
832209fda3SGao Xiang 	const BYTE *ip = (const BYTE *) src;
842209fda3SGao Xiang 	const BYTE * const iend = ip + srcSize;
854e1a33b1SSven Schmidt 
862209fda3SGao Xiang 	BYTE *op = (BYTE *) dst;
874e1a33b1SSven Schmidt 	BYTE * const oend = op + outputSize;
88cffb78b0SKyungsik Lee 	BYTE *cpy;
89cffb78b0SKyungsik Lee 
904e1a33b1SSven Schmidt 	const BYTE * const dictEnd = (const BYTE *)dictStart + dictSize;
912209fda3SGao Xiang 	static const unsigned int inc32table[8] = {0, 1, 2, 1, 0, 4, 4, 4};
922209fda3SGao Xiang 	static const int dec64table[8] = {0, 0, 0, -1, -4, 1, 2, 3};
934e1a33b1SSven Schmidt 
944e1a33b1SSven Schmidt 	const int safeDecode = (endOnInput == endOnInputSize);
954e1a33b1SSven Schmidt 	const int checkOffset = ((safeDecode) && (dictSize < (int)(64 * KB)));
964e1a33b1SSven Schmidt 
972209fda3SGao Xiang 	/* Set up the "end" pointers for the shortcut. */
982209fda3SGao Xiang 	const BYTE *const shortiend = iend -
992209fda3SGao Xiang 		(endOnInput ? 14 : 8) /*maxLL*/ - 2 /*offset*/;
1002209fda3SGao Xiang 	const BYTE *const shortoend = oend -
1012209fda3SGao Xiang 		(endOnInput ? 14 : 8) /*maxLL*/ - 18 /*maxML*/;
1022209fda3SGao Xiang 
1032209fda3SGao Xiang 	DEBUGLOG(5, "%s (srcSize:%i, dstSize:%i)", __func__,
1042209fda3SGao Xiang 		 srcSize, outputSize);
1052209fda3SGao Xiang 
1064e1a33b1SSven Schmidt 	/* Special cases */
1072209fda3SGao Xiang 	assert(lowPrefix <= op);
1082209fda3SGao Xiang 	assert(src != NULL);
1094e1a33b1SSven Schmidt 
1104e1a33b1SSven Schmidt 	/* Empty output buffer */
1114e1a33b1SSven Schmidt 	if ((endOnInput) && (unlikely(outputSize == 0)))
1122209fda3SGao Xiang 		return ((srcSize == 1) && (*ip == 0)) ? 0 : -1;
1134e1a33b1SSven Schmidt 
1144e1a33b1SSven Schmidt 	if ((!endOnInput) && (unlikely(outputSize == 0)))
1154e1a33b1SSven Schmidt 		return (*ip == 0 ? 1 : -1);
1164e1a33b1SSven Schmidt 
1172209fda3SGao Xiang 	if ((endOnInput) && unlikely(srcSize == 0))
1182209fda3SGao Xiang 		return -1;
1192209fda3SGao Xiang 
1204e1a33b1SSven Schmidt 	/* Main Loop : decode sequences */
121cffb78b0SKyungsik Lee 	while (1) {
1224e1a33b1SSven Schmidt 		size_t length;
1234e1a33b1SSven Schmidt 		const BYTE *match;
1244e1a33b1SSven Schmidt 		size_t offset;
125cffb78b0SKyungsik Lee 
1264e1a33b1SSven Schmidt 		/* get literal length */
1274e1a33b1SSven Schmidt 		unsigned int const token = *ip++;
1284e1a33b1SSven Schmidt 		length = token>>ML_BITS;
1294e1a33b1SSven Schmidt 
1302209fda3SGao Xiang 		/* ip < iend before the increment */
1312209fda3SGao Xiang 		assert(!endOnInput || ip <= iend);
1322209fda3SGao Xiang 
1332209fda3SGao Xiang 		/*
1342209fda3SGao Xiang 		 * A two-stage shortcut for the most common case:
1352209fda3SGao Xiang 		 * 1) If the literal length is 0..14, and there is enough
1362209fda3SGao Xiang 		 * space, enter the shortcut and copy 16 bytes on behalf
1372209fda3SGao Xiang 		 * of the literals (in the fast mode, only 8 bytes can be
1382209fda3SGao Xiang 		 * safely copied this way).
1392209fda3SGao Xiang 		 * 2) Further if the match length is 4..18, copy 18 bytes
1402209fda3SGao Xiang 		 * in a similar manner; but we ensure that there's enough
1412209fda3SGao Xiang 		 * space in the output for those 18 bytes earlier, upon
1422209fda3SGao Xiang 		 * entering the shortcut (in other words, there is a
1432209fda3SGao Xiang 		 * combined check for both stages).
144e8ec0493SJoe Perches 		 *
145e8ec0493SJoe Perches 		 * The & in the likely() below is intentionally not && so that
146e8ec0493SJoe Perches 		 * some compilers can produce better parallelized runtime code
1472209fda3SGao Xiang 		 */
1482209fda3SGao Xiang 		if ((endOnInput ? length != RUN_MASK : length <= 8)
1492209fda3SGao Xiang 		   /*
1502209fda3SGao Xiang 		    * strictly "less than" on input, to re-enter
1512209fda3SGao Xiang 		    * the loop with at least one byte
1522209fda3SGao Xiang 		    */
1532209fda3SGao Xiang 		   && likely((endOnInput ? ip < shortiend : 1) &
1542209fda3SGao Xiang 			     (op <= shortoend))) {
1552209fda3SGao Xiang 			/* Copy the literals */
156b1a3e75eSNick Terrell 			LZ4_memcpy(op, ip, endOnInput ? 16 : 8);
1572209fda3SGao Xiang 			op += length; ip += length;
1582209fda3SGao Xiang 
1592209fda3SGao Xiang 			/*
1602209fda3SGao Xiang 			 * The second stage:
1612209fda3SGao Xiang 			 * prepare for match copying, decode full info.
1622209fda3SGao Xiang 			 * If it doesn't work out, the info won't be wasted.
1632209fda3SGao Xiang 			 */
1642209fda3SGao Xiang 			length = token & ML_MASK; /* match length */
1652209fda3SGao Xiang 			offset = LZ4_readLE16(ip);
1662209fda3SGao Xiang 			ip += 2;
1672209fda3SGao Xiang 			match = op - offset;
1682209fda3SGao Xiang 			assert(match <= op); /* check overflow */
1692209fda3SGao Xiang 
1702209fda3SGao Xiang 			/* Do not deal with overlapping matches. */
1712209fda3SGao Xiang 			if ((length != ML_MASK) &&
1722209fda3SGao Xiang 			    (offset >= 8) &&
1732209fda3SGao Xiang 			    (dict == withPrefix64k || match >= lowPrefix)) {
1742209fda3SGao Xiang 				/* Copy the match. */
175b1a3e75eSNick Terrell 				LZ4_memcpy(op + 0, match + 0, 8);
176b1a3e75eSNick Terrell 				LZ4_memcpy(op + 8, match + 8, 8);
177b1a3e75eSNick Terrell 				LZ4_memcpy(op + 16, match + 16, 2);
1782209fda3SGao Xiang 				op += length + MINMATCH;
1792209fda3SGao Xiang 				/* Both stages worked, load the next token. */
1802209fda3SGao Xiang 				continue;
1812209fda3SGao Xiang 			}
1822209fda3SGao Xiang 
1832209fda3SGao Xiang 			/*
1842209fda3SGao Xiang 			 * The second stage didn't work out, but the info
1852209fda3SGao Xiang 			 * is ready. Propel it right to the point of match
1862209fda3SGao Xiang 			 * copying.
1872209fda3SGao Xiang 			 */
1882209fda3SGao Xiang 			goto _copy_match;
1892209fda3SGao Xiang 		}
1902209fda3SGao Xiang 
1912209fda3SGao Xiang 		/* decode literal length */
192cffb78b0SKyungsik Lee 		if (length == RUN_MASK) {
1934e1a33b1SSven Schmidt 			unsigned int s;
194cffb78b0SKyungsik Lee 
1952209fda3SGao Xiang 			if (unlikely(endOnInput ? ip >= iend - RUN_MASK : 0)) {
1962209fda3SGao Xiang 				/* overflow detection */
1972209fda3SGao Xiang 				goto _output_error;
1982209fda3SGao Xiang 			}
1994e1a33b1SSven Schmidt 			do {
2004e1a33b1SSven Schmidt 				s = *ip++;
2014e1a33b1SSven Schmidt 				length += s;
2024e1a33b1SSven Schmidt 			} while (likely(endOnInput
2034e1a33b1SSven Schmidt 				? ip < iend - RUN_MASK
2044e1a33b1SSven Schmidt 				: 1) & (s == 255));
2054e1a33b1SSven Schmidt 
2064e1a33b1SSven Schmidt 			if ((safeDecode)
2072209fda3SGao Xiang 			    && unlikely((uptrval)(op) +
2082209fda3SGao Xiang 					length < (uptrval)(op))) {
2094e1a33b1SSven Schmidt 				/* overflow detection */
210206204a1SGreg Kroah-Hartman 				goto _output_error;
2114e1a33b1SSven Schmidt 			}
2124e1a33b1SSven Schmidt 			if ((safeDecode)
2132209fda3SGao Xiang 			    && unlikely((uptrval)(ip) +
2142209fda3SGao Xiang 					length < (uptrval)(ip))) {
2154e1a33b1SSven Schmidt 				/* overflow detection */
2164e1a33b1SSven Schmidt 				goto _output_error;
2174e1a33b1SSven Schmidt 			}
218cffb78b0SKyungsik Lee 		}
219cffb78b0SKyungsik Lee 
220cffb78b0SKyungsik Lee 		/* copy literals */
221cffb78b0SKyungsik Lee 		cpy = op + length;
2222209fda3SGao Xiang 		LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH);
2232209fda3SGao Xiang 
2242209fda3SGao Xiang 		if (((endOnInput) && ((cpy > oend - MFLIMIT)
2254e1a33b1SSven Schmidt 			|| (ip + length > iend - (2 + 1 + LASTLITERALS))))
2264e1a33b1SSven Schmidt 			|| ((!endOnInput) && (cpy > oend - WILDCOPYLENGTH))) {
2274e1a33b1SSven Schmidt 			if (partialDecoding) {
2284e1a33b1SSven Schmidt 				if (cpy > oend) {
229cffb78b0SKyungsik Lee 					/*
2302209fda3SGao Xiang 					 * Partial decoding :
2312209fda3SGao Xiang 					 * stop in the middle of literal segment
232cffb78b0SKyungsik Lee 					 */
2332209fda3SGao Xiang 					cpy = oend;
2342209fda3SGao Xiang 					length = oend - op;
2354e1a33b1SSven Schmidt 				}
2364e1a33b1SSven Schmidt 				if ((endOnInput)
2374e1a33b1SSven Schmidt 					&& (ip + length > iend)) {
2384e1a33b1SSven Schmidt 					/*
2394e1a33b1SSven Schmidt 					 * Error :
2404e1a33b1SSven Schmidt 					 * read attempt beyond
2414e1a33b1SSven Schmidt 					 * end of input buffer
2424e1a33b1SSven Schmidt 					 */
2434e1a33b1SSven Schmidt 					goto _output_error;
2444e1a33b1SSven Schmidt 				}
2454e1a33b1SSven Schmidt 			} else {
2464e1a33b1SSven Schmidt 				if ((!endOnInput)
2474e1a33b1SSven Schmidt 					&& (cpy != oend)) {
2484e1a33b1SSven Schmidt 					/*
2494e1a33b1SSven Schmidt 					 * Error :
2504e1a33b1SSven Schmidt 					 * block decoding must
2514e1a33b1SSven Schmidt 					 * stop exactly there
2524e1a33b1SSven Schmidt 					 */
2534e1a33b1SSven Schmidt 					goto _output_error;
2544e1a33b1SSven Schmidt 				}
2554e1a33b1SSven Schmidt 				if ((endOnInput)
2564e1a33b1SSven Schmidt 					&& ((ip + length != iend)
2574e1a33b1SSven Schmidt 					|| (cpy > oend))) {
2584e1a33b1SSven Schmidt 					/*
2594e1a33b1SSven Schmidt 					 * Error :
2604e1a33b1SSven Schmidt 					 * input must be consumed
2614e1a33b1SSven Schmidt 					 */
2624e1a33b1SSven Schmidt 					goto _output_error;
2634e1a33b1SSven Schmidt 				}
2644e1a33b1SSven Schmidt 			}
265cffb78b0SKyungsik Lee 
26689b15863SGao Xiang 			/*
26789b15863SGao Xiang 			 * supports overlapping memory regions; only matters
26889b15863SGao Xiang 			 * for in-place decompression scenarios
26989b15863SGao Xiang 			 */
27089b15863SGao Xiang 			LZ4_memmove(op, ip, length);
271cffb78b0SKyungsik Lee 			ip += length;
2724e1a33b1SSven Schmidt 			op += length;
2734e1a33b1SSven Schmidt 
274eafc0a02SGuo Xuenan 			/* Necessarily EOF when !partialDecoding.
275eafc0a02SGuo Xuenan 			 * When partialDecoding, it is EOF if we've either
276eafc0a02SGuo Xuenan 			 * filled the output buffer or
277eafc0a02SGuo Xuenan 			 * can't proceed with reading an offset for following match.
278eafc0a02SGuo Xuenan 			 */
279eafc0a02SGuo Xuenan 			if (!partialDecoding || (cpy == oend) || (ip >= (iend - 2)))
2802209fda3SGao Xiang 				break;
2812209fda3SGao Xiang 		} else {
2822209fda3SGao Xiang 			/* may overwrite up to WILDCOPYLENGTH beyond cpy */
2834e1a33b1SSven Schmidt 			LZ4_wildCopy(op, ip, cpy);
2844e1a33b1SSven Schmidt 			ip += length;
285cffb78b0SKyungsik Lee 			op = cpy;
2862209fda3SGao Xiang 		}
287cffb78b0SKyungsik Lee 
288cffb78b0SKyungsik Lee 		/* get offset */
2894e1a33b1SSven Schmidt 		offset = LZ4_readLE16(ip);
290cffb78b0SKyungsik Lee 		ip += 2;
2914e1a33b1SSven Schmidt 		match = op - offset;
292cffb78b0SKyungsik Lee 
2932209fda3SGao Xiang 		/* get matchlength */
2942209fda3SGao Xiang 		length = token & ML_MASK;
2952209fda3SGao Xiang 
2962209fda3SGao Xiang _copy_match:
2972209fda3SGao Xiang 		if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) {
2984e1a33b1SSven Schmidt 			/* Error : offset outside buffers */
299cffb78b0SKyungsik Lee 			goto _output_error;
3004e1a33b1SSven Schmidt 		}
3014e1a33b1SSven Schmidt 
3024e1a33b1SSven Schmidt 		/* costs ~1%; silence an msan warning when offset == 0 */
3032209fda3SGao Xiang 		/*
3042209fda3SGao Xiang 		 * note : when partialDecoding, there is no guarantee that
3052209fda3SGao Xiang 		 * at least 4 bytes remain available in output buffer
3062209fda3SGao Xiang 		 */
3072209fda3SGao Xiang 		if (!partialDecoding) {
3082209fda3SGao Xiang 			assert(oend > op);
3092209fda3SGao Xiang 			assert(oend - op >= 4);
310cffb78b0SKyungsik Lee 
3112209fda3SGao Xiang 			LZ4_write32(op, (U32)offset);
3122209fda3SGao Xiang 		}
3132209fda3SGao Xiang 
314cffb78b0SKyungsik Lee 		if (length == ML_MASK) {
3154e1a33b1SSven Schmidt 			unsigned int s;
3164e1a33b1SSven Schmidt 
3174e1a33b1SSven Schmidt 			do {
3184e1a33b1SSven Schmidt 				s = *ip++;
3194e1a33b1SSven Schmidt 
3204e1a33b1SSven Schmidt 				if ((endOnInput) && (ip > iend - LASTLITERALS))
3214148c1f6SGreg Kroah-Hartman 					goto _output_error;
3224e1a33b1SSven Schmidt 
3234e1a33b1SSven Schmidt 				length += s;
3244e1a33b1SSven Schmidt 			} while (s == 255);
3254e1a33b1SSven Schmidt 
3264e1a33b1SSven Schmidt 			if ((safeDecode)
3274e1a33b1SSven Schmidt 				&& unlikely(
3282209fda3SGao Xiang 					(uptrval)(op) + length < (uptrval)op)) {
3294e1a33b1SSven Schmidt 				/* overflow detection */
3304e1a33b1SSven Schmidt 				goto _output_error;
3314e1a33b1SSven Schmidt 			}
332cffb78b0SKyungsik Lee 		}
333cffb78b0SKyungsik Lee 
3344e1a33b1SSven Schmidt 		length += MINMATCH;
335cffb78b0SKyungsik Lee 
3362209fda3SGao Xiang 		/* match starting within external dictionary */
3374e1a33b1SSven Schmidt 		if ((dict == usingExtDict) && (match < lowPrefix)) {
3384e1a33b1SSven Schmidt 			if (unlikely(op + length > oend - LASTLITERALS)) {
3394e1a33b1SSven Schmidt 				/* doesn't respect parsing restriction */
3402209fda3SGao Xiang 				if (!partialDecoding)
341cffb78b0SKyungsik Lee 					goto _output_error;
3422209fda3SGao Xiang 				length = min(length, (size_t)(oend - op));
3434e1a33b1SSven Schmidt 			}
3444e1a33b1SSven Schmidt 
3454e1a33b1SSven Schmidt 			if (length <= (size_t)(lowPrefix - match)) {
346cffb78b0SKyungsik Lee 				/*
3472209fda3SGao Xiang 				 * match fits entirely within external
3482209fda3SGao Xiang 				 * dictionary : just copy
349cffb78b0SKyungsik Lee 				 */
3504e1a33b1SSven Schmidt 				memmove(op, dictEnd - (lowPrefix - match),
3514e1a33b1SSven Schmidt 					length);
3524e1a33b1SSven Schmidt 				op += length;
3534e1a33b1SSven Schmidt 			} else {
3544e1a33b1SSven Schmidt 				/*
3552209fda3SGao Xiang 				 * match stretches into both external
3564e1a33b1SSven Schmidt 				 * dictionary and current block
3574e1a33b1SSven Schmidt 				 */
3584e1a33b1SSven Schmidt 				size_t const copySize = (size_t)(lowPrefix - match);
3594e1a33b1SSven Schmidt 				size_t const restSize = length - copySize;
3604e1a33b1SSven Schmidt 
361b1a3e75eSNick Terrell 				LZ4_memcpy(op, dictEnd - copySize, copySize);
3624e1a33b1SSven Schmidt 				op += copySize;
3634e1a33b1SSven Schmidt 				if (restSize > (size_t)(op - lowPrefix)) {
3644e1a33b1SSven Schmidt 					/* overlap copy */
3654e1a33b1SSven Schmidt 					BYTE * const endOfMatch = op + restSize;
3664e1a33b1SSven Schmidt 					const BYTE *copyFrom = lowPrefix;
3674e1a33b1SSven Schmidt 
3684e1a33b1SSven Schmidt 					while (op < endOfMatch)
3694e1a33b1SSven Schmidt 						*op++ = *copyFrom++;
3704e1a33b1SSven Schmidt 				} else {
371b1a3e75eSNick Terrell 					LZ4_memcpy(op, lowPrefix, restSize);
3724e1a33b1SSven Schmidt 					op += restSize;
3734e1a33b1SSven Schmidt 				}
3744e1a33b1SSven Schmidt 			}
375cffb78b0SKyungsik Lee 			continue;
376cffb78b0SKyungsik Lee 		}
3774e1a33b1SSven Schmidt 
3784e1a33b1SSven Schmidt 		/* copy match within block */
3794e1a33b1SSven Schmidt 		cpy = op + length;
3804e1a33b1SSven Schmidt 
3812209fda3SGao Xiang 		/*
3822209fda3SGao Xiang 		 * partialDecoding :
3832209fda3SGao Xiang 		 * may not respect endBlock parsing restrictions
3842209fda3SGao Xiang 		 */
3852209fda3SGao Xiang 		assert(op <= oend);
3862209fda3SGao Xiang 		if (partialDecoding &&
3872209fda3SGao Xiang 		    (cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
3882209fda3SGao Xiang 			size_t const mlen = min(length, (size_t)(oend - op));
3892209fda3SGao Xiang 			const BYTE * const matchEnd = match + mlen;
3902209fda3SGao Xiang 			BYTE * const copyEnd = op + mlen;
3914e1a33b1SSven Schmidt 
3922209fda3SGao Xiang 			if (matchEnd > op) {
3932209fda3SGao Xiang 				/* overlap copy */
3942209fda3SGao Xiang 				while (op < copyEnd)
3952209fda3SGao Xiang 					*op++ = *match++;
3962209fda3SGao Xiang 			} else {
397b1a3e75eSNick Terrell 				LZ4_memcpy(op, match, mlen);
3982209fda3SGao Xiang 			}
3992209fda3SGao Xiang 			op = copyEnd;
4002209fda3SGao Xiang 			if (op == oend)
4012209fda3SGao Xiang 				break;
4022209fda3SGao Xiang 			continue;
4032209fda3SGao Xiang 		}
4042209fda3SGao Xiang 
4052209fda3SGao Xiang 		if (unlikely(offset < 8)) {
4064e1a33b1SSven Schmidt 			op[0] = match[0];
4074e1a33b1SSven Schmidt 			op[1] = match[1];
4084e1a33b1SSven Schmidt 			op[2] = match[2];
4094e1a33b1SSven Schmidt 			op[3] = match[3];
4102209fda3SGao Xiang 			match += inc32table[offset];
411b1a3e75eSNick Terrell 			LZ4_memcpy(op + 4, match, 4);
4122209fda3SGao Xiang 			match -= dec64table[offset];
4134e1a33b1SSven Schmidt 		} else {
4144e1a33b1SSven Schmidt 			LZ4_copy8(op, match);
4154e1a33b1SSven Schmidt 			match += 8;
4164e1a33b1SSven Schmidt 		}
4174e1a33b1SSven Schmidt 
4184e1a33b1SSven Schmidt 		op += 8;
4194e1a33b1SSven Schmidt 
4202209fda3SGao Xiang 		if (unlikely(cpy > oend - MATCH_SAFEGUARD_DISTANCE)) {
4214e1a33b1SSven Schmidt 			BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
4224e1a33b1SSven Schmidt 
4234e1a33b1SSven Schmidt 			if (cpy > oend - LASTLITERALS) {
4244e1a33b1SSven Schmidt 				/*
4254e1a33b1SSven Schmidt 				 * Error : last LASTLITERALS bytes
4264e1a33b1SSven Schmidt 				 * must be literals (uncompressed)
4274e1a33b1SSven Schmidt 				 */
4284e1a33b1SSven Schmidt 				goto _output_error;
4294e1a33b1SSven Schmidt 			}
4304e1a33b1SSven Schmidt 
4314e1a33b1SSven Schmidt 			if (op < oCopyLimit) {
4324e1a33b1SSven Schmidt 				LZ4_wildCopy(op, match, oCopyLimit);
4334e1a33b1SSven Schmidt 				match += oCopyLimit - op;
4344e1a33b1SSven Schmidt 				op = oCopyLimit;
4354e1a33b1SSven Schmidt 			}
4364e1a33b1SSven Schmidt 			while (op < cpy)
4374e1a33b1SSven Schmidt 				*op++ = *match++;
4384e1a33b1SSven Schmidt 		} else {
4394e1a33b1SSven Schmidt 			LZ4_copy8(op, match);
4404e1a33b1SSven Schmidt 			if (length > 16)
4414e1a33b1SSven Schmidt 				LZ4_wildCopy(op + 8, match + 8, cpy);
4424e1a33b1SSven Schmidt 		}
4432209fda3SGao Xiang 		op = cpy; /* wildcopy correction */
444cffb78b0SKyungsik Lee 	}
445cffb78b0SKyungsik Lee 
4464e1a33b1SSven Schmidt 	/* end of decoding */
4474e1a33b1SSven Schmidt 	if (endOnInput) {
4484e1a33b1SSven Schmidt 		/* Nb of output bytes decoded */
4492209fda3SGao Xiang 		return (int) (((char *)op) - dst);
4504e1a33b1SSven Schmidt 	} else {
4514e1a33b1SSven Schmidt 		/* Nb of input bytes read */
4522209fda3SGao Xiang 		return (int) (((const char *)ip) - src);
4534e1a33b1SSven Schmidt 	}
4544e1a33b1SSven Schmidt 
4554e1a33b1SSven Schmidt 	/* Overflow error detected */
456cffb78b0SKyungsik Lee _output_error:
4572209fda3SGao Xiang 	return (int) (-(((const char *)ip) - src)) - 1;
458cffb78b0SKyungsik Lee }
459cffb78b0SKyungsik Lee 
LZ4_decompress_safe(const char * source,char * dest,int compressedSize,int maxDecompressedSize)4604e1a33b1SSven Schmidt int LZ4_decompress_safe(const char *source, char *dest,
4614e1a33b1SSven Schmidt 	int compressedSize, int maxDecompressedSize)
462cffb78b0SKyungsik Lee {
4632209fda3SGao Xiang 	return LZ4_decompress_generic(source, dest,
4642209fda3SGao Xiang 				      compressedSize, maxDecompressedSize,
4652209fda3SGao Xiang 				      endOnInputSize, decode_full_block,
4664e1a33b1SSven Schmidt 				      noDict, (BYTE *)dest, NULL, 0);
467cffb78b0SKyungsik Lee }
4684e1a33b1SSven Schmidt 
LZ4_decompress_safe_partial(const char * src,char * dst,int compressedSize,int targetOutputSize,int dstCapacity)4692209fda3SGao Xiang int LZ4_decompress_safe_partial(const char *src, char *dst,
4702209fda3SGao Xiang 	int compressedSize, int targetOutputSize, int dstCapacity)
4714e1a33b1SSven Schmidt {
4722209fda3SGao Xiang 	dstCapacity = min(targetOutputSize, dstCapacity);
4732209fda3SGao Xiang 	return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity,
4742209fda3SGao Xiang 				      endOnInputSize, partial_decode,
4752209fda3SGao Xiang 				      noDict, (BYTE *)dst, NULL, 0);
476cffb78b0SKyungsik Lee }
477cffb78b0SKyungsik Lee 
LZ4_decompress_fast(const char * source,char * dest,int originalSize)4784e1a33b1SSven Schmidt int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
4794e1a33b1SSven Schmidt {
4804e1a33b1SSven Schmidt 	return LZ4_decompress_generic(source, dest, 0, originalSize,
4812209fda3SGao Xiang 				      endOnOutputSize, decode_full_block,
4822209fda3SGao Xiang 				      withPrefix64k,
4832209fda3SGao Xiang 				      (BYTE *)dest - 64 * KB, NULL, 0);
484cffb78b0SKyungsik Lee }
485cffb78b0SKyungsik Lee 
4862209fda3SGao Xiang /* ===== Instantiate a few more decoding cases, used more than once. ===== */
4872209fda3SGao Xiang 
LZ4_decompress_safe_withPrefix64k(const char * source,char * dest,int compressedSize,int maxOutputSize)4887fde9d6eSRajat Asthana static int LZ4_decompress_safe_withPrefix64k(const char *source, char *dest,
4892209fda3SGao Xiang 				      int compressedSize, int maxOutputSize)
4902209fda3SGao Xiang {
4912209fda3SGao Xiang 	return LZ4_decompress_generic(source, dest,
4922209fda3SGao Xiang 				      compressedSize, maxOutputSize,
4932209fda3SGao Xiang 				      endOnInputSize, decode_full_block,
4942209fda3SGao Xiang 				      withPrefix64k,
4952209fda3SGao Xiang 				      (BYTE *)dest - 64 * KB, NULL, 0);
4962209fda3SGao Xiang }
4972209fda3SGao Xiang 
LZ4_decompress_safe_withSmallPrefix(const char * source,char * dest,int compressedSize,int maxOutputSize,size_t prefixSize)4982209fda3SGao Xiang static int LZ4_decompress_safe_withSmallPrefix(const char *source, char *dest,
4992209fda3SGao Xiang 					       int compressedSize,
5002209fda3SGao Xiang 					       int maxOutputSize,
5012209fda3SGao Xiang 					       size_t prefixSize)
5022209fda3SGao Xiang {
5032209fda3SGao Xiang 	return LZ4_decompress_generic(source, dest,
5042209fda3SGao Xiang 				      compressedSize, maxOutputSize,
5052209fda3SGao Xiang 				      endOnInputSize, decode_full_block,
5062209fda3SGao Xiang 				      noDict,
5072209fda3SGao Xiang 				      (BYTE *)dest - prefixSize, NULL, 0);
5082209fda3SGao Xiang }
5092209fda3SGao Xiang 
LZ4_decompress_safe_forceExtDict(const char * source,char * dest,int compressedSize,int maxOutputSize,const void * dictStart,size_t dictSize)510*2d8867f3STiezhu Yang static int LZ4_decompress_safe_forceExtDict(const char *source, char *dest,
5112209fda3SGao Xiang 					    int compressedSize, int maxOutputSize,
5122209fda3SGao Xiang 					    const void *dictStart, size_t dictSize)
5132209fda3SGao Xiang {
5142209fda3SGao Xiang 	return LZ4_decompress_generic(source, dest,
5152209fda3SGao Xiang 				      compressedSize, maxOutputSize,
5162209fda3SGao Xiang 				      endOnInputSize, decode_full_block,
5172209fda3SGao Xiang 				      usingExtDict, (BYTE *)dest,
5182209fda3SGao Xiang 				      (const BYTE *)dictStart, dictSize);
5192209fda3SGao Xiang }
5202209fda3SGao Xiang 
LZ4_decompress_fast_extDict(const char * source,char * dest,int originalSize,const void * dictStart,size_t dictSize)5212209fda3SGao Xiang static int LZ4_decompress_fast_extDict(const char *source, char *dest,
5222209fda3SGao Xiang 				       int originalSize,
5232209fda3SGao Xiang 				       const void *dictStart, size_t dictSize)
5242209fda3SGao Xiang {
5252209fda3SGao Xiang 	return LZ4_decompress_generic(source, dest,
5262209fda3SGao Xiang 				      0, originalSize,
5272209fda3SGao Xiang 				      endOnOutputSize, decode_full_block,
5282209fda3SGao Xiang 				      usingExtDict, (BYTE *)dest,
5292209fda3SGao Xiang 				      (const BYTE *)dictStart, dictSize);
5302209fda3SGao Xiang }
5312209fda3SGao Xiang 
5322209fda3SGao Xiang /*
5332209fda3SGao Xiang  * The "double dictionary" mode, for use with e.g. ring buffers: the first part
5342209fda3SGao Xiang  * of the dictionary is passed as prefix, and the second via dictStart + dictSize.
5352209fda3SGao Xiang  * These routines are used only once, in LZ4_decompress_*_continue().
5362209fda3SGao Xiang  */
5372209fda3SGao Xiang static FORCE_INLINE
LZ4_decompress_safe_doubleDict(const char * source,char * dest,int compressedSize,int maxOutputSize,size_t prefixSize,const void * dictStart,size_t dictSize)5382209fda3SGao Xiang int LZ4_decompress_safe_doubleDict(const char *source, char *dest,
5392209fda3SGao Xiang 				   int compressedSize, int maxOutputSize,
5402209fda3SGao Xiang 				   size_t prefixSize,
5412209fda3SGao Xiang 				   const void *dictStart, size_t dictSize)
5422209fda3SGao Xiang {
5432209fda3SGao Xiang 	return LZ4_decompress_generic(source, dest,
5442209fda3SGao Xiang 				      compressedSize, maxOutputSize,
5452209fda3SGao Xiang 				      endOnInputSize, decode_full_block,
5462209fda3SGao Xiang 				      usingExtDict, (BYTE *)dest - prefixSize,
5472209fda3SGao Xiang 				      (const BYTE *)dictStart, dictSize);
5482209fda3SGao Xiang }
5492209fda3SGao Xiang 
5502209fda3SGao Xiang static FORCE_INLINE
LZ4_decompress_fast_doubleDict(const char * source,char * dest,int originalSize,size_t prefixSize,const void * dictStart,size_t dictSize)5512209fda3SGao Xiang int LZ4_decompress_fast_doubleDict(const char *source, char *dest,
5522209fda3SGao Xiang 				   int originalSize, size_t prefixSize,
5532209fda3SGao Xiang 				   const void *dictStart, size_t dictSize)
5542209fda3SGao Xiang {
5552209fda3SGao Xiang 	return LZ4_decompress_generic(source, dest,
5562209fda3SGao Xiang 				      0, originalSize,
5572209fda3SGao Xiang 				      endOnOutputSize, decode_full_block,
5582209fda3SGao Xiang 				      usingExtDict, (BYTE *)dest - prefixSize,
5592209fda3SGao Xiang 				      (const BYTE *)dictStart, dictSize);
5602209fda3SGao Xiang }
5612209fda3SGao Xiang 
5622209fda3SGao Xiang /* ===== streaming decompression functions ===== */
5632209fda3SGao Xiang 
LZ4_setStreamDecode(LZ4_streamDecode_t * LZ4_streamDecode,const char * dictionary,int dictSize)5644e1a33b1SSven Schmidt int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
5654e1a33b1SSven Schmidt 	const char *dictionary, int dictSize)
5664e1a33b1SSven Schmidt {
5672209fda3SGao Xiang 	LZ4_streamDecode_t_internal *lz4sd =
5682209fda3SGao Xiang 		&LZ4_streamDecode->internal_donotuse;
5694e1a33b1SSven Schmidt 
5704e1a33b1SSven Schmidt 	lz4sd->prefixSize = (size_t) dictSize;
5714e1a33b1SSven Schmidt 	lz4sd->prefixEnd = (const BYTE *) dictionary + dictSize;
5724e1a33b1SSven Schmidt 	lz4sd->externalDict = NULL;
5734e1a33b1SSven Schmidt 	lz4sd->extDictSize	= 0;
5744e1a33b1SSven Schmidt 	return 1;
5754e1a33b1SSven Schmidt }
5764e1a33b1SSven Schmidt 
577cffb78b0SKyungsik Lee /*
5784e1a33b1SSven Schmidt  * *_continue() :
5794e1a33b1SSven Schmidt  * These decoding functions allow decompression of multiple blocks
5804e1a33b1SSven Schmidt  * in "streaming" mode.
5814e1a33b1SSven Schmidt  * Previously decoded blocks must still be available at the memory
5824e1a33b1SSven Schmidt  * position where they were decoded.
5834e1a33b1SSven Schmidt  * If it's not possible, save the relevant part of
5844e1a33b1SSven Schmidt  * decoded data into a safe buffer,
5854e1a33b1SSven Schmidt  * and indicate where it stands using LZ4_setStreamDecode()
586cffb78b0SKyungsik Lee  */
LZ4_decompress_safe_continue(LZ4_streamDecode_t * LZ4_streamDecode,const char * source,char * dest,int compressedSize,int maxOutputSize)5874e1a33b1SSven Schmidt int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
5884e1a33b1SSven Schmidt 	const char *source, char *dest, int compressedSize, int maxOutputSize)
5894e1a33b1SSven Schmidt {
5902209fda3SGao Xiang 	LZ4_streamDecode_t_internal *lz4sd =
5912209fda3SGao Xiang 		&LZ4_streamDecode->internal_donotuse;
5924e1a33b1SSven Schmidt 	int result;
593cffb78b0SKyungsik Lee 
5942209fda3SGao Xiang 	if (lz4sd->prefixSize == 0) {
5952209fda3SGao Xiang 		/* The first call, no dictionary yet. */
5962209fda3SGao Xiang 		assert(lz4sd->extDictSize == 0);
5972209fda3SGao Xiang 		result = LZ4_decompress_safe(source, dest,
5982209fda3SGao Xiang 			compressedSize, maxOutputSize);
5994e1a33b1SSven Schmidt 		if (result <= 0)
6004e1a33b1SSven Schmidt 			return result;
6012209fda3SGao Xiang 		lz4sd->prefixSize = result;
6022209fda3SGao Xiang 		lz4sd->prefixEnd = (BYTE *)dest + result;
6032209fda3SGao Xiang 	} else if (lz4sd->prefixEnd == (BYTE *)dest) {
6042209fda3SGao Xiang 		/* They're rolling the current segment. */
6052209fda3SGao Xiang 		if (lz4sd->prefixSize >= 64 * KB - 1)
6062209fda3SGao Xiang 			result = LZ4_decompress_safe_withPrefix64k(source, dest,
6072209fda3SGao Xiang 				compressedSize, maxOutputSize);
6082209fda3SGao Xiang 		else if (lz4sd->extDictSize == 0)
6092209fda3SGao Xiang 			result = LZ4_decompress_safe_withSmallPrefix(source,
6102209fda3SGao Xiang 				dest, compressedSize, maxOutputSize,
6112209fda3SGao Xiang 				lz4sd->prefixSize);
6122209fda3SGao Xiang 		else
6132209fda3SGao Xiang 			result = LZ4_decompress_safe_doubleDict(source, dest,
6142209fda3SGao Xiang 				compressedSize, maxOutputSize,
6152209fda3SGao Xiang 				lz4sd->prefixSize,
6162209fda3SGao Xiang 				lz4sd->externalDict, lz4sd->extDictSize);
6172209fda3SGao Xiang 		if (result <= 0)
6182209fda3SGao Xiang 			return result;
6194e1a33b1SSven Schmidt 		lz4sd->prefixSize += result;
6204e1a33b1SSven Schmidt 		lz4sd->prefixEnd  += result;
621cffb78b0SKyungsik Lee 	} else {
6222209fda3SGao Xiang 		/*
6232209fda3SGao Xiang 		 * The buffer wraps around, or they're
6242209fda3SGao Xiang 		 * switching to another buffer.
6252209fda3SGao Xiang 		 */
6264e1a33b1SSven Schmidt 		lz4sd->extDictSize = lz4sd->prefixSize;
6274e1a33b1SSven Schmidt 		lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
6282209fda3SGao Xiang 		result = LZ4_decompress_safe_forceExtDict(source, dest,
6294e1a33b1SSven Schmidt 			compressedSize, maxOutputSize,
6304e1a33b1SSven Schmidt 			lz4sd->externalDict, lz4sd->extDictSize);
6314e1a33b1SSven Schmidt 		if (result <= 0)
6324e1a33b1SSven Schmidt 			return result;
6334e1a33b1SSven Schmidt 		lz4sd->prefixSize = result;
6344e1a33b1SSven Schmidt 		lz4sd->prefixEnd  = (BYTE *)dest + result;
635cffb78b0SKyungsik Lee 	}
636cffb78b0SKyungsik Lee 
6374e1a33b1SSven Schmidt 	return result;
6384e1a33b1SSven Schmidt }
6394e1a33b1SSven Schmidt 
LZ4_decompress_fast_continue(LZ4_streamDecode_t * LZ4_streamDecode,const char * source,char * dest,int originalSize)6404e1a33b1SSven Schmidt int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
6414e1a33b1SSven Schmidt 	const char *source, char *dest, int originalSize)
6424e1a33b1SSven Schmidt {
6434e1a33b1SSven Schmidt 	LZ4_streamDecode_t_internal *lz4sd = &LZ4_streamDecode->internal_donotuse;
6444e1a33b1SSven Schmidt 	int result;
6454e1a33b1SSven Schmidt 
6462209fda3SGao Xiang 	if (lz4sd->prefixSize == 0) {
6472209fda3SGao Xiang 		assert(lz4sd->extDictSize == 0);
6482209fda3SGao Xiang 		result = LZ4_decompress_fast(source, dest, originalSize);
6494e1a33b1SSven Schmidt 		if (result <= 0)
6504e1a33b1SSven Schmidt 			return result;
6512209fda3SGao Xiang 		lz4sd->prefixSize = originalSize;
6522209fda3SGao Xiang 		lz4sd->prefixEnd = (BYTE *)dest + originalSize;
6532209fda3SGao Xiang 	} else if (lz4sd->prefixEnd == (BYTE *)dest) {
6542209fda3SGao Xiang 		if (lz4sd->prefixSize >= 64 * KB - 1 ||
6552209fda3SGao Xiang 		    lz4sd->extDictSize == 0)
6562209fda3SGao Xiang 			result = LZ4_decompress_fast(source, dest,
6572209fda3SGao Xiang 						     originalSize);
6582209fda3SGao Xiang 		else
6592209fda3SGao Xiang 			result = LZ4_decompress_fast_doubleDict(source, dest,
6602209fda3SGao Xiang 				originalSize, lz4sd->prefixSize,
6612209fda3SGao Xiang 				lz4sd->externalDict, lz4sd->extDictSize);
6622209fda3SGao Xiang 		if (result <= 0)
6632209fda3SGao Xiang 			return result;
6644e1a33b1SSven Schmidt 		lz4sd->prefixSize += originalSize;
6654e1a33b1SSven Schmidt 		lz4sd->prefixEnd  += originalSize;
6664e1a33b1SSven Schmidt 	} else {
6674e1a33b1SSven Schmidt 		lz4sd->extDictSize = lz4sd->prefixSize;
6684e1a33b1SSven Schmidt 		lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize;
6692209fda3SGao Xiang 		result = LZ4_decompress_fast_extDict(source, dest,
6702209fda3SGao Xiang 			originalSize, lz4sd->externalDict, lz4sd->extDictSize);
6714e1a33b1SSven Schmidt 		if (result <= 0)
6724e1a33b1SSven Schmidt 			return result;
6734e1a33b1SSven Schmidt 		lz4sd->prefixSize = originalSize;
6744e1a33b1SSven Schmidt 		lz4sd->prefixEnd = (BYTE *)dest + originalSize;
6754e1a33b1SSven Schmidt 	}
6764e1a33b1SSven Schmidt 	return result;
6774e1a33b1SSven Schmidt }
6784e1a33b1SSven Schmidt 
LZ4_decompress_safe_usingDict(const char * source,char * dest,int compressedSize,int maxOutputSize,const char * dictStart,int dictSize)6794e1a33b1SSven Schmidt int LZ4_decompress_safe_usingDict(const char *source, char *dest,
6804e1a33b1SSven Schmidt 				  int compressedSize, int maxOutputSize,
6814e1a33b1SSven Schmidt 				  const char *dictStart, int dictSize)
6824e1a33b1SSven Schmidt {
6832209fda3SGao Xiang 	if (dictSize == 0)
6842209fda3SGao Xiang 		return LZ4_decompress_safe(source, dest,
6852209fda3SGao Xiang 					   compressedSize, maxOutputSize);
6862209fda3SGao Xiang 	if (dictStart+dictSize == dest) {
6872209fda3SGao Xiang 		if (dictSize >= 64 * KB - 1)
6882209fda3SGao Xiang 			return LZ4_decompress_safe_withPrefix64k(source, dest,
6892209fda3SGao Xiang 				compressedSize, maxOutputSize);
6902209fda3SGao Xiang 		return LZ4_decompress_safe_withSmallPrefix(source, dest,
6912209fda3SGao Xiang 			compressedSize, maxOutputSize, dictSize);
6922209fda3SGao Xiang 	}
6932209fda3SGao Xiang 	return LZ4_decompress_safe_forceExtDict(source, dest,
6942209fda3SGao Xiang 		compressedSize, maxOutputSize, dictStart, dictSize);
6954e1a33b1SSven Schmidt }
6964e1a33b1SSven Schmidt 
LZ4_decompress_fast_usingDict(const char * source,char * dest,int originalSize,const char * dictStart,int dictSize)6974e1a33b1SSven Schmidt int LZ4_decompress_fast_usingDict(const char *source, char *dest,
6982209fda3SGao Xiang 				  int originalSize,
6992209fda3SGao Xiang 				  const char *dictStart, int dictSize)
7004e1a33b1SSven Schmidt {
7012209fda3SGao Xiang 	if (dictSize == 0 || dictStart + dictSize == dest)
7022209fda3SGao Xiang 		return LZ4_decompress_fast(source, dest, originalSize);
7032209fda3SGao Xiang 
7042209fda3SGao Xiang 	return LZ4_decompress_fast_extDict(source, dest, originalSize,
7052209fda3SGao Xiang 		dictStart, dictSize);
7064e1a33b1SSven Schmidt }
7074e1a33b1SSven Schmidt 
708cffb78b0SKyungsik Lee #ifndef STATIC
7094e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_decompress_safe);
7104e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_decompress_safe_partial);
7114e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_decompress_fast);
7124e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_setStreamDecode);
7134e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_decompress_safe_continue);
7144e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_decompress_fast_continue);
7154e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_decompress_safe_usingDict);
7164e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_decompress_fast_usingDict);
717cffb78b0SKyungsik Lee 
718ee8a99bdSRichard Laager MODULE_LICENSE("Dual BSD/GPL");
7194e1a33b1SSven Schmidt MODULE_DESCRIPTION("LZ4 decompressor");
720cffb78b0SKyungsik Lee #endif
721