xref: /linux/lib/lz4/lz4_compress.c (revision b1a3e75e)
1c72ac7a1SChanho Min /*
2c72ac7a1SChanho Min  * LZ4 - Fast LZ compression algorithm
34e1a33b1SSven Schmidt  * Copyright (C) 2011 - 2016, Yann Collet.
4c72ac7a1SChanho Min  * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php)
5c72ac7a1SChanho Min  * Redistribution and use in source and binary forms, with or without
6c72ac7a1SChanho Min  * modification, are permitted provided that the following conditions are
7c72ac7a1SChanho Min  * met:
8c72ac7a1SChanho Min  *	* Redistributions of source code must retain the above copyright
9c72ac7a1SChanho Min  *	  notice, this list of conditions and the following disclaimer.
10c72ac7a1SChanho Min  *	* Redistributions in binary form must reproduce the above
11c72ac7a1SChanho Min  * copyright notice, this list of conditions and the following disclaimer
12c72ac7a1SChanho Min  * in the documentation and/or other materials provided with the
13c72ac7a1SChanho Min  * distribution.
14c72ac7a1SChanho Min  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15c72ac7a1SChanho Min  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16c72ac7a1SChanho Min  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17c72ac7a1SChanho Min  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18c72ac7a1SChanho Min  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19c72ac7a1SChanho Min  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20c72ac7a1SChanho Min  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21c72ac7a1SChanho Min  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22c72ac7a1SChanho Min  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23c72ac7a1SChanho Min  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24c72ac7a1SChanho Min  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25c72ac7a1SChanho Min  * You can contact the author at :
264e1a33b1SSven Schmidt  *	- LZ4 homepage : http://www.lz4.org
274e1a33b1SSven Schmidt  *	- LZ4 source repository : https://github.com/lz4/lz4
28c72ac7a1SChanho Min  *
294e1a33b1SSven Schmidt  *	Changed for kernel usage by:
304e1a33b1SSven Schmidt  *	Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
31c72ac7a1SChanho Min  */
32c72ac7a1SChanho Min 
334e1a33b1SSven Schmidt /*-************************************
344e1a33b1SSven Schmidt  *	Dependencies
354e1a33b1SSven Schmidt  **************************************/
364e1a33b1SSven Schmidt #include <linux/lz4.h>
374e1a33b1SSven Schmidt #include "lz4defs.h"
38c72ac7a1SChanho Min #include <linux/module.h>
39c72ac7a1SChanho Min #include <linux/kernel.h>
40c72ac7a1SChanho Min #include <asm/unaligned.h>
414e1a33b1SSven Schmidt 
424e1a33b1SSven Schmidt static const int LZ4_minLength = (MFLIMIT + 1);
434e1a33b1SSven Schmidt static const int LZ4_64Klimit = ((64 * KB) + (MFLIMIT - 1));
444e1a33b1SSven Schmidt 
454e1a33b1SSven Schmidt /*-******************************
464e1a33b1SSven Schmidt  *	Compression functions
474e1a33b1SSven Schmidt  ********************************/
LZ4_hash4(U32 sequence,tableType_t const tableType)484e1a33b1SSven Schmidt static FORCE_INLINE U32 LZ4_hash4(
494e1a33b1SSven Schmidt 	U32 sequence,
504e1a33b1SSven Schmidt 	tableType_t const tableType)
514e1a33b1SSven Schmidt {
524e1a33b1SSven Schmidt 	if (tableType == byU16)
534e1a33b1SSven Schmidt 		return ((sequence * 2654435761U)
544e1a33b1SSven Schmidt 			>> ((MINMATCH * 8) - (LZ4_HASHLOG + 1)));
554e1a33b1SSven Schmidt 	else
564e1a33b1SSven Schmidt 		return ((sequence * 2654435761U)
574e1a33b1SSven Schmidt 			>> ((MINMATCH * 8) - LZ4_HASHLOG));
584e1a33b1SSven Schmidt }
594e1a33b1SSven Schmidt 
LZ4_hash5(U64 sequence,tableType_t const tableType)604e1a33b1SSven Schmidt static FORCE_INLINE U32 LZ4_hash5(
614e1a33b1SSven Schmidt 	U64 sequence,
624e1a33b1SSven Schmidt 	tableType_t const tableType)
634e1a33b1SSven Schmidt {
644e1a33b1SSven Schmidt 	const U32 hashLog = (tableType == byU16)
654e1a33b1SSven Schmidt 		? LZ4_HASHLOG + 1
664e1a33b1SSven Schmidt 		: LZ4_HASHLOG;
674e1a33b1SSven Schmidt 
684e1a33b1SSven Schmidt #if LZ4_LITTLE_ENDIAN
694e1a33b1SSven Schmidt 	static const U64 prime5bytes = 889523592379ULL;
704e1a33b1SSven Schmidt 
714e1a33b1SSven Schmidt 	return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog));
724e1a33b1SSven Schmidt #else
734e1a33b1SSven Schmidt 	static const U64 prime8bytes = 11400714785074694791ULL;
744e1a33b1SSven Schmidt 
754e1a33b1SSven Schmidt 	return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog));
764e1a33b1SSven Schmidt #endif
774e1a33b1SSven Schmidt }
784e1a33b1SSven Schmidt 
LZ4_hashPosition(const void * p,tableType_t const tableType)794e1a33b1SSven Schmidt static FORCE_INLINE U32 LZ4_hashPosition(
804e1a33b1SSven Schmidt 	const void *p,
814e1a33b1SSven Schmidt 	tableType_t const tableType)
824e1a33b1SSven Schmidt {
834e1a33b1SSven Schmidt #if LZ4_ARCH64
844e1a33b1SSven Schmidt 	if (tableType == byU32)
854e1a33b1SSven Schmidt 		return LZ4_hash5(LZ4_read_ARCH(p), tableType);
864e1a33b1SSven Schmidt #endif
874e1a33b1SSven Schmidt 
884e1a33b1SSven Schmidt 	return LZ4_hash4(LZ4_read32(p), tableType);
894e1a33b1SSven Schmidt }
904e1a33b1SSven Schmidt 
LZ4_putPositionOnHash(const BYTE * p,U32 h,void * tableBase,tableType_t const tableType,const BYTE * srcBase)914e1a33b1SSven Schmidt static void LZ4_putPositionOnHash(
924e1a33b1SSven Schmidt 	const BYTE *p,
934e1a33b1SSven Schmidt 	U32 h,
944e1a33b1SSven Schmidt 	void *tableBase,
954e1a33b1SSven Schmidt 	tableType_t const tableType,
964e1a33b1SSven Schmidt 	const BYTE *srcBase)
974e1a33b1SSven Schmidt {
984e1a33b1SSven Schmidt 	switch (tableType) {
994e1a33b1SSven Schmidt 	case byPtr:
1004e1a33b1SSven Schmidt 	{
1014e1a33b1SSven Schmidt 		const BYTE **hashTable = (const BYTE **)tableBase;
1024e1a33b1SSven Schmidt 
1034e1a33b1SSven Schmidt 		hashTable[h] = p;
1044e1a33b1SSven Schmidt 		return;
1054e1a33b1SSven Schmidt 	}
1064e1a33b1SSven Schmidt 	case byU32:
1074e1a33b1SSven Schmidt 	{
1084e1a33b1SSven Schmidt 		U32 *hashTable = (U32 *) tableBase;
1094e1a33b1SSven Schmidt 
1104e1a33b1SSven Schmidt 		hashTable[h] = (U32)(p - srcBase);
1114e1a33b1SSven Schmidt 		return;
1124e1a33b1SSven Schmidt 	}
1134e1a33b1SSven Schmidt 	case byU16:
1144e1a33b1SSven Schmidt 	{
1154e1a33b1SSven Schmidt 		U16 *hashTable = (U16 *) tableBase;
1164e1a33b1SSven Schmidt 
1174e1a33b1SSven Schmidt 		hashTable[h] = (U16)(p - srcBase);
1184e1a33b1SSven Schmidt 		return;
1194e1a33b1SSven Schmidt 	}
1204e1a33b1SSven Schmidt 	}
1214e1a33b1SSven Schmidt }
1224e1a33b1SSven Schmidt 
LZ4_putPosition(const BYTE * p,void * tableBase,tableType_t tableType,const BYTE * srcBase)1234e1a33b1SSven Schmidt static FORCE_INLINE void LZ4_putPosition(
1244e1a33b1SSven Schmidt 	const BYTE *p,
1254e1a33b1SSven Schmidt 	void *tableBase,
1264e1a33b1SSven Schmidt 	tableType_t tableType,
1274e1a33b1SSven Schmidt 	const BYTE *srcBase)
1284e1a33b1SSven Schmidt {
1294e1a33b1SSven Schmidt 	U32 const h = LZ4_hashPosition(p, tableType);
1304e1a33b1SSven Schmidt 
1314e1a33b1SSven Schmidt 	LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
1324e1a33b1SSven Schmidt }
1334e1a33b1SSven Schmidt 
LZ4_getPositionOnHash(U32 h,void * tableBase,tableType_t tableType,const BYTE * srcBase)1344e1a33b1SSven Schmidt static const BYTE *LZ4_getPositionOnHash(
1354e1a33b1SSven Schmidt 	U32 h,
1364e1a33b1SSven Schmidt 	void *tableBase,
1374e1a33b1SSven Schmidt 	tableType_t tableType,
1384e1a33b1SSven Schmidt 	const BYTE *srcBase)
1394e1a33b1SSven Schmidt {
1404e1a33b1SSven Schmidt 	if (tableType == byPtr) {
1414e1a33b1SSven Schmidt 		const BYTE **hashTable = (const BYTE **) tableBase;
1424e1a33b1SSven Schmidt 
1434e1a33b1SSven Schmidt 		return hashTable[h];
1444e1a33b1SSven Schmidt 	}
1454e1a33b1SSven Schmidt 
1464e1a33b1SSven Schmidt 	if (tableType == byU32) {
1474e1a33b1SSven Schmidt 		const U32 * const hashTable = (U32 *) tableBase;
1484e1a33b1SSven Schmidt 
1494e1a33b1SSven Schmidt 		return hashTable[h] + srcBase;
1504e1a33b1SSven Schmidt 	}
1514e1a33b1SSven Schmidt 
1524e1a33b1SSven Schmidt 	{
1534e1a33b1SSven Schmidt 		/* default, to ensure a return */
1544e1a33b1SSven Schmidt 		const U16 * const hashTable = (U16 *) tableBase;
1554e1a33b1SSven Schmidt 
1564e1a33b1SSven Schmidt 		return hashTable[h] + srcBase;
1574e1a33b1SSven Schmidt 	}
1584e1a33b1SSven Schmidt }
1594e1a33b1SSven Schmidt 
LZ4_getPosition(const BYTE * p,void * tableBase,tableType_t tableType,const BYTE * srcBase)1604e1a33b1SSven Schmidt static FORCE_INLINE const BYTE *LZ4_getPosition(
1614e1a33b1SSven Schmidt 	const BYTE *p,
1624e1a33b1SSven Schmidt 	void *tableBase,
1634e1a33b1SSven Schmidt 	tableType_t tableType,
1644e1a33b1SSven Schmidt 	const BYTE *srcBase)
1654e1a33b1SSven Schmidt {
1664e1a33b1SSven Schmidt 	U32 const h = LZ4_hashPosition(p, tableType);
1674e1a33b1SSven Schmidt 
1684e1a33b1SSven Schmidt 	return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
1694e1a33b1SSven Schmidt }
1704e1a33b1SSven Schmidt 
171c72ac7a1SChanho Min 
172c72ac7a1SChanho Min /*
1734e1a33b1SSven Schmidt  * LZ4_compress_generic() :
1744e1a33b1SSven Schmidt  * inlined, to ensure branches are decided at compilation time
175c72ac7a1SChanho Min  */
LZ4_compress_generic(LZ4_stream_t_internal * const dictPtr,const char * const source,char * const dest,const int inputSize,const int maxOutputSize,const limitedOutput_directive outputLimited,const tableType_t tableType,const dict_directive dict,const dictIssue_directive dictIssue,const U32 acceleration)1764e1a33b1SSven Schmidt static FORCE_INLINE int LZ4_compress_generic(
1774e1a33b1SSven Schmidt 	LZ4_stream_t_internal * const dictPtr,
1784e1a33b1SSven Schmidt 	const char * const source,
1794e1a33b1SSven Schmidt 	char * const dest,
1804e1a33b1SSven Schmidt 	const int inputSize,
1814e1a33b1SSven Schmidt 	const int maxOutputSize,
1824e1a33b1SSven Schmidt 	const limitedOutput_directive outputLimited,
1834e1a33b1SSven Schmidt 	const tableType_t tableType,
1844e1a33b1SSven Schmidt 	const dict_directive dict,
1854e1a33b1SSven Schmidt 	const dictIssue_directive dictIssue,
1864e1a33b1SSven Schmidt 	const U32 acceleration)
187c72ac7a1SChanho Min {
1884e1a33b1SSven Schmidt 	const BYTE *ip = (const BYTE *) source;
1894e1a33b1SSven Schmidt 	const BYTE *base;
1904e1a33b1SSven Schmidt 	const BYTE *lowLimit;
1914e1a33b1SSven Schmidt 	const BYTE * const lowRefLimit = ip - dictPtr->dictSize;
1924e1a33b1SSven Schmidt 	const BYTE * const dictionary = dictPtr->dictionary;
1934e1a33b1SSven Schmidt 	const BYTE * const dictEnd = dictionary + dictPtr->dictSize;
1944e1a33b1SSven Schmidt 	const size_t dictDelta = dictEnd - (const BYTE *)source;
1954e1a33b1SSven Schmidt 	const BYTE *anchor = (const BYTE *) source;
1964e1a33b1SSven Schmidt 	const BYTE * const iend = ip + inputSize;
1974e1a33b1SSven Schmidt 	const BYTE * const mflimit = iend - MFLIMIT;
1984e1a33b1SSven Schmidt 	const BYTE * const matchlimit = iend - LASTLITERALS;
199c72ac7a1SChanho Min 
2004e1a33b1SSven Schmidt 	BYTE *op = (BYTE *) dest;
2014e1a33b1SSven Schmidt 	BYTE * const olimit = op + maxOutputSize;
202c72ac7a1SChanho Min 
2034e1a33b1SSven Schmidt 	U32 forwardH;
2044e1a33b1SSven Schmidt 	size_t refDelta = 0;
205c72ac7a1SChanho Min 
2064e1a33b1SSven Schmidt 	/* Init conditions */
2074e1a33b1SSven Schmidt 	if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) {
2084e1a33b1SSven Schmidt 		/* Unsupported inputSize, too large (or negative) */
209c72ac7a1SChanho Min 		return 0;
2104e1a33b1SSven Schmidt 	}
211c72ac7a1SChanho Min 
2124e1a33b1SSven Schmidt 	switch (dict) {
2134e1a33b1SSven Schmidt 	case noDict:
2144e1a33b1SSven Schmidt 	default:
2154e1a33b1SSven Schmidt 		base = (const BYTE *)source;
2164e1a33b1SSven Schmidt 		lowLimit = (const BYTE *)source;
2174e1a33b1SSven Schmidt 		break;
2184e1a33b1SSven Schmidt 	case withPrefix64k:
2194e1a33b1SSven Schmidt 		base = (const BYTE *)source - dictPtr->currentOffset;
2204e1a33b1SSven Schmidt 		lowLimit = (const BYTE *)source - dictPtr->dictSize;
2214e1a33b1SSven Schmidt 		break;
2224e1a33b1SSven Schmidt 	case usingExtDict:
2234e1a33b1SSven Schmidt 		base = (const BYTE *)source - dictPtr->currentOffset;
2244e1a33b1SSven Schmidt 		lowLimit = (const BYTE *)source;
225c72ac7a1SChanho Min 		break;
226c72ac7a1SChanho Min 	}
227c72ac7a1SChanho Min 
2284e1a33b1SSven Schmidt 	if ((tableType == byU16)
2294e1a33b1SSven Schmidt 		&& (inputSize >= LZ4_64Klimit)) {
2304e1a33b1SSven Schmidt 		/* Size too large (not within 64K limit) */
2314e1a33b1SSven Schmidt 		return 0;
2324e1a33b1SSven Schmidt 	}
2334e1a33b1SSven Schmidt 
2344e1a33b1SSven Schmidt 	if (inputSize < LZ4_minLength) {
2354e1a33b1SSven Schmidt 		/* Input too small, no compression (all literals) */
2364e1a33b1SSven Schmidt 		goto _last_literals;
2374e1a33b1SSven Schmidt 	}
2384e1a33b1SSven Schmidt 
2394e1a33b1SSven Schmidt 	/* First Byte */
2404e1a33b1SSven Schmidt 	LZ4_putPosition(ip, dictPtr->hashTable, tableType, base);
2414e1a33b1SSven Schmidt 	ip++;
2424e1a33b1SSven Schmidt 	forwardH = LZ4_hashPosition(ip, tableType);
2434e1a33b1SSven Schmidt 
2444e1a33b1SSven Schmidt 	/* Main Loop */
2454e1a33b1SSven Schmidt 	for ( ; ; ) {
2464e1a33b1SSven Schmidt 		const BYTE *match;
2474e1a33b1SSven Schmidt 		BYTE *token;
2484e1a33b1SSven Schmidt 
2494e1a33b1SSven Schmidt 		/* Find a match */
2504e1a33b1SSven Schmidt 		{
2514e1a33b1SSven Schmidt 			const BYTE *forwardIp = ip;
2524e1a33b1SSven Schmidt 			unsigned int step = 1;
2534e1a33b1SSven Schmidt 			unsigned int searchMatchNb = acceleration << LZ4_SKIPTRIGGER;
2544e1a33b1SSven Schmidt 
2554e1a33b1SSven Schmidt 			do {
2564e1a33b1SSven Schmidt 				U32 const h = forwardH;
2574e1a33b1SSven Schmidt 
2584e1a33b1SSven Schmidt 				ip = forwardIp;
2594e1a33b1SSven Schmidt 				forwardIp += step;
2604e1a33b1SSven Schmidt 				step = (searchMatchNb++ >> LZ4_SKIPTRIGGER);
2614e1a33b1SSven Schmidt 
2624e1a33b1SSven Schmidt 				if (unlikely(forwardIp > mflimit))
2634e1a33b1SSven Schmidt 					goto _last_literals;
2644e1a33b1SSven Schmidt 
2654e1a33b1SSven Schmidt 				match = LZ4_getPositionOnHash(h,
2664e1a33b1SSven Schmidt 					dictPtr->hashTable,
2674e1a33b1SSven Schmidt 					tableType, base);
2684e1a33b1SSven Schmidt 
2694e1a33b1SSven Schmidt 				if (dict == usingExtDict) {
2704e1a33b1SSven Schmidt 					if (match < (const BYTE *)source) {
2714e1a33b1SSven Schmidt 						refDelta = dictDelta;
2724e1a33b1SSven Schmidt 						lowLimit = dictionary;
2734e1a33b1SSven Schmidt 					} else {
2744e1a33b1SSven Schmidt 						refDelta = 0;
2754e1a33b1SSven Schmidt 						lowLimit = (const BYTE *)source;
2764e1a33b1SSven Schmidt 				}	 }
2774e1a33b1SSven Schmidt 
2784e1a33b1SSven Schmidt 				forwardH = LZ4_hashPosition(forwardIp,
2794e1a33b1SSven Schmidt 					tableType);
2804e1a33b1SSven Schmidt 
2814e1a33b1SSven Schmidt 				LZ4_putPositionOnHash(ip, h, dictPtr->hashTable,
2824e1a33b1SSven Schmidt 					tableType, base);
2834e1a33b1SSven Schmidt 			} while (((dictIssue == dictSmall)
2844e1a33b1SSven Schmidt 					? (match < lowRefLimit)
2854e1a33b1SSven Schmidt 					: 0)
2864e1a33b1SSven Schmidt 				|| ((tableType == byU16)
2874e1a33b1SSven Schmidt 					? 0
2884e1a33b1SSven Schmidt 					: (match + MAX_DISTANCE < ip))
2894e1a33b1SSven Schmidt 				|| (LZ4_read32(match + refDelta)
2904e1a33b1SSven Schmidt 					!= LZ4_read32(ip)));
2914e1a33b1SSven Schmidt 		}
2924e1a33b1SSven Schmidt 
2934e1a33b1SSven Schmidt 		/* Catch up */
2944e1a33b1SSven Schmidt 		while (((ip > anchor) & (match + refDelta > lowLimit))
2954e1a33b1SSven Schmidt 				&& (unlikely(ip[-1] == match[refDelta - 1]))) {
2964e1a33b1SSven Schmidt 			ip--;
2974e1a33b1SSven Schmidt 			match--;
2984e1a33b1SSven Schmidt 		}
2994e1a33b1SSven Schmidt 
3004e1a33b1SSven Schmidt 		/* Encode Literals */
3014e1a33b1SSven Schmidt 		{
3024e1a33b1SSven Schmidt 			unsigned const int litLength = (unsigned int)(ip - anchor);
3034e1a33b1SSven Schmidt 
3044e1a33b1SSven Schmidt 			token = op++;
3054e1a33b1SSven Schmidt 
3064e1a33b1SSven Schmidt 			if ((outputLimited) &&
3074e1a33b1SSven Schmidt 				/* Check output buffer overflow */
3084e1a33b1SSven Schmidt 				(unlikely(op + litLength +
3094e1a33b1SSven Schmidt 					(2 + 1 + LASTLITERALS) +
3104e1a33b1SSven Schmidt 					(litLength / 255) > olimit)))
3114e1a33b1SSven Schmidt 				return 0;
3124e1a33b1SSven Schmidt 
3134e1a33b1SSven Schmidt 			if (litLength >= RUN_MASK) {
3144e1a33b1SSven Schmidt 				int len = (int)litLength - RUN_MASK;
3154e1a33b1SSven Schmidt 
3164e1a33b1SSven Schmidt 				*token = (RUN_MASK << ML_BITS);
3174e1a33b1SSven Schmidt 
3184e1a33b1SSven Schmidt 				for (; len >= 255; len -= 255)
3194e1a33b1SSven Schmidt 					*op++ = 255;
3204e1a33b1SSven Schmidt 				*op++ = (BYTE)len;
3214e1a33b1SSven Schmidt 			} else
3224e1a33b1SSven Schmidt 				*token = (BYTE)(litLength << ML_BITS);
3234e1a33b1SSven Schmidt 
3244e1a33b1SSven Schmidt 			/* Copy Literals */
3254e1a33b1SSven Schmidt 			LZ4_wildCopy(op, anchor, op + litLength);
3264e1a33b1SSven Schmidt 			op += litLength;
3274e1a33b1SSven Schmidt 		}
3284e1a33b1SSven Schmidt 
3294e1a33b1SSven Schmidt _next_match:
3304e1a33b1SSven Schmidt 		/* Encode Offset */
3314e1a33b1SSven Schmidt 		LZ4_writeLE16(op, (U16)(ip - match));
3324e1a33b1SSven Schmidt 		op += 2;
3334e1a33b1SSven Schmidt 
3344e1a33b1SSven Schmidt 		/* Encode MatchLength */
3354e1a33b1SSven Schmidt 		{
3364e1a33b1SSven Schmidt 			unsigned int matchCode;
3374e1a33b1SSven Schmidt 
3384e1a33b1SSven Schmidt 			if ((dict == usingExtDict)
3394e1a33b1SSven Schmidt 				&& (lowLimit == dictionary)) {
3404e1a33b1SSven Schmidt 				const BYTE *limit;
3414e1a33b1SSven Schmidt 
3424e1a33b1SSven Schmidt 				match += refDelta;
3434e1a33b1SSven Schmidt 				limit = ip + (dictEnd - match);
3444e1a33b1SSven Schmidt 
3454e1a33b1SSven Schmidt 				if (limit > matchlimit)
3464e1a33b1SSven Schmidt 					limit = matchlimit;
3474e1a33b1SSven Schmidt 
3484e1a33b1SSven Schmidt 				matchCode = LZ4_count(ip + MINMATCH,
3494e1a33b1SSven Schmidt 					match + MINMATCH, limit);
3504e1a33b1SSven Schmidt 
3514e1a33b1SSven Schmidt 				ip += MINMATCH + matchCode;
3524e1a33b1SSven Schmidt 
3534e1a33b1SSven Schmidt 				if (ip == limit) {
3544e1a33b1SSven Schmidt 					unsigned const int more = LZ4_count(ip,
3554e1a33b1SSven Schmidt 						(const BYTE *)source,
3564e1a33b1SSven Schmidt 						matchlimit);
3574e1a33b1SSven Schmidt 
3584e1a33b1SSven Schmidt 					matchCode += more;
3594e1a33b1SSven Schmidt 					ip += more;
3604e1a33b1SSven Schmidt 				}
3614e1a33b1SSven Schmidt 			} else {
3624e1a33b1SSven Schmidt 				matchCode = LZ4_count(ip + MINMATCH,
3634e1a33b1SSven Schmidt 					match + MINMATCH, matchlimit);
3644e1a33b1SSven Schmidt 				ip += MINMATCH + matchCode;
3654e1a33b1SSven Schmidt 			}
3664e1a33b1SSven Schmidt 
3674e1a33b1SSven Schmidt 			if (outputLimited &&
3684e1a33b1SSven Schmidt 				/* Check output buffer overflow */
3694e1a33b1SSven Schmidt 				(unlikely(op +
3704e1a33b1SSven Schmidt 					(1 + LASTLITERALS) +
3714e1a33b1SSven Schmidt 					(matchCode >> 8) > olimit)))
3724e1a33b1SSven Schmidt 				return 0;
3734e1a33b1SSven Schmidt 
3744e1a33b1SSven Schmidt 			if (matchCode >= ML_MASK) {
3754e1a33b1SSven Schmidt 				*token += ML_MASK;
3764e1a33b1SSven Schmidt 				matchCode -= ML_MASK;
3774e1a33b1SSven Schmidt 				LZ4_write32(op, 0xFFFFFFFF);
3784e1a33b1SSven Schmidt 
3794e1a33b1SSven Schmidt 				while (matchCode >= 4 * 255) {
3804e1a33b1SSven Schmidt 					op += 4;
3814e1a33b1SSven Schmidt 					LZ4_write32(op, 0xFFFFFFFF);
3824e1a33b1SSven Schmidt 					matchCode -= 4 * 255;
3834e1a33b1SSven Schmidt 				}
3844e1a33b1SSven Schmidt 
3854e1a33b1SSven Schmidt 				op += matchCode / 255;
3864e1a33b1SSven Schmidt 				*op++ = (BYTE)(matchCode % 255);
3874e1a33b1SSven Schmidt 			} else
3884e1a33b1SSven Schmidt 				*token += (BYTE)(matchCode);
3894e1a33b1SSven Schmidt 		}
3904e1a33b1SSven Schmidt 
3914e1a33b1SSven Schmidt 		anchor = ip;
3924e1a33b1SSven Schmidt 
3934e1a33b1SSven Schmidt 		/* Test end of chunk */
3944e1a33b1SSven Schmidt 		if (ip > mflimit)
3954e1a33b1SSven Schmidt 			break;
3964e1a33b1SSven Schmidt 
397c72ac7a1SChanho Min 		/* Fill table */
3984e1a33b1SSven Schmidt 		LZ4_putPosition(ip - 2, dictPtr->hashTable, tableType, base);
399c72ac7a1SChanho Min 
400c72ac7a1SChanho Min 		/* Test next position */
4014e1a33b1SSven Schmidt 		match = LZ4_getPosition(ip, dictPtr->hashTable,
4024e1a33b1SSven Schmidt 			tableType, base);
4034e1a33b1SSven Schmidt 
4044e1a33b1SSven Schmidt 		if (dict == usingExtDict) {
4054e1a33b1SSven Schmidt 			if (match < (const BYTE *)source) {
4064e1a33b1SSven Schmidt 				refDelta = dictDelta;
4074e1a33b1SSven Schmidt 				lowLimit = dictionary;
4084e1a33b1SSven Schmidt 			} else {
4094e1a33b1SSven Schmidt 				refDelta = 0;
4104e1a33b1SSven Schmidt 				lowLimit = (const BYTE *)source;
4114e1a33b1SSven Schmidt 			}
4124e1a33b1SSven Schmidt 		}
4134e1a33b1SSven Schmidt 
4144e1a33b1SSven Schmidt 		LZ4_putPosition(ip, dictPtr->hashTable, tableType, base);
4154e1a33b1SSven Schmidt 
4164e1a33b1SSven Schmidt 		if (((dictIssue == dictSmall) ? (match >= lowRefLimit) : 1)
4174e1a33b1SSven Schmidt 			&& (match + MAX_DISTANCE >= ip)
4184e1a33b1SSven Schmidt 			&& (LZ4_read32(match + refDelta) == LZ4_read32(ip))) {
419c72ac7a1SChanho Min 			token = op++;
420c72ac7a1SChanho Min 			*token = 0;
421c72ac7a1SChanho Min 			goto _next_match;
422c72ac7a1SChanho Min 		}
423c72ac7a1SChanho Min 
424c72ac7a1SChanho Min 		/* Prepare next loop */
4254e1a33b1SSven Schmidt 		forwardH = LZ4_hashPosition(++ip, tableType);
426c72ac7a1SChanho Min 	}
427c72ac7a1SChanho Min 
428c72ac7a1SChanho Min _last_literals:
429c72ac7a1SChanho Min 	/* Encode Last Literals */
4304e1a33b1SSven Schmidt 	{
4314e1a33b1SSven Schmidt 		size_t const lastRun = (size_t)(iend - anchor);
4324e1a33b1SSven Schmidt 
4334e1a33b1SSven Schmidt 		if ((outputLimited) &&
4344e1a33b1SSven Schmidt 			/* Check output buffer overflow */
4354e1a33b1SSven Schmidt 			((op - (BYTE *)dest) + lastRun + 1 +
4364e1a33b1SSven Schmidt 			((lastRun + 255 - RUN_MASK) / 255) > (U32)maxOutputSize))
437c72ac7a1SChanho Min 			return 0;
438c72ac7a1SChanho Min 
4394e1a33b1SSven Schmidt 		if (lastRun >= RUN_MASK) {
4404e1a33b1SSven Schmidt 			size_t accumulator = lastRun - RUN_MASK;
4414e1a33b1SSven Schmidt 			*op++ = RUN_MASK << ML_BITS;
4424e1a33b1SSven Schmidt 			for (; accumulator >= 255; accumulator -= 255)
443c72ac7a1SChanho Min 				*op++ = 255;
4444e1a33b1SSven Schmidt 			*op++ = (BYTE) accumulator;
4454e1a33b1SSven Schmidt 		} else {
4464e1a33b1SSven Schmidt 			*op++ = (BYTE)(lastRun << ML_BITS);
4474e1a33b1SSven Schmidt 		}
4484e1a33b1SSven Schmidt 
449*b1a3e75eSNick Terrell 		LZ4_memcpy(op, anchor, lastRun);
4504e1a33b1SSven Schmidt 
4514e1a33b1SSven Schmidt 		op += lastRun;
4524e1a33b1SSven Schmidt 	}
453c72ac7a1SChanho Min 
454c72ac7a1SChanho Min 	/* End */
455c72ac7a1SChanho Min 	return (int) (((char *)op) - dest);
456c72ac7a1SChanho Min }
457c72ac7a1SChanho Min 
LZ4_compress_fast_extState(void * state,const char * source,char * dest,int inputSize,int maxOutputSize,int acceleration)4584e1a33b1SSven Schmidt static int LZ4_compress_fast_extState(
4594e1a33b1SSven Schmidt 	void *state,
460c72ac7a1SChanho Min 	const char *source,
461c72ac7a1SChanho Min 	char *dest,
4624e1a33b1SSven Schmidt 	int inputSize,
4634e1a33b1SSven Schmidt 	int maxOutputSize,
4644e1a33b1SSven Schmidt 	int acceleration)
465c72ac7a1SChanho Min {
4664e1a33b1SSven Schmidt 	LZ4_stream_t_internal *ctx = &((LZ4_stream_t *)state)->internal_donotuse;
467c72ac7a1SChanho Min #if LZ4_ARCH64
4684e1a33b1SSven Schmidt 	const tableType_t tableType = byU32;
469c72ac7a1SChanho Min #else
4704e1a33b1SSven Schmidt 	const tableType_t tableType = byPtr;
471c72ac7a1SChanho Min #endif
472c72ac7a1SChanho Min 
4734e1a33b1SSven Schmidt 	LZ4_resetStream((LZ4_stream_t *)state);
474c72ac7a1SChanho Min 
4754e1a33b1SSven Schmidt 	if (acceleration < 1)
4764e1a33b1SSven Schmidt 		acceleration = LZ4_ACCELERATION_DEFAULT;
477c72ac7a1SChanho Min 
4784e1a33b1SSven Schmidt 	if (maxOutputSize >= LZ4_COMPRESSBOUND(inputSize)) {
4794e1a33b1SSven Schmidt 		if (inputSize < LZ4_64Klimit)
4804e1a33b1SSven Schmidt 			return LZ4_compress_generic(ctx, source,
4814e1a33b1SSven Schmidt 				dest, inputSize, 0,
4824e1a33b1SSven Schmidt 				noLimit, byU16, noDict,
4834e1a33b1SSven Schmidt 				noDictIssue, acceleration);
484c72ac7a1SChanho Min 		else
4854e1a33b1SSven Schmidt 			return LZ4_compress_generic(ctx, source,
4864e1a33b1SSven Schmidt 				dest, inputSize, 0,
4874e1a33b1SSven Schmidt 				noLimit, tableType, noDict,
4884e1a33b1SSven Schmidt 				noDictIssue, acceleration);
4894e1a33b1SSven Schmidt 	} else {
4904e1a33b1SSven Schmidt 		if (inputSize < LZ4_64Klimit)
4914e1a33b1SSven Schmidt 			return LZ4_compress_generic(ctx, source,
4924e1a33b1SSven Schmidt 				dest, inputSize,
4934e1a33b1SSven Schmidt 				maxOutputSize, limitedOutput, byU16, noDict,
4944e1a33b1SSven Schmidt 				noDictIssue, acceleration);
4954e1a33b1SSven Schmidt 		else
4964e1a33b1SSven Schmidt 			return LZ4_compress_generic(ctx, source,
4974e1a33b1SSven Schmidt 				dest, inputSize,
4984e1a33b1SSven Schmidt 				maxOutputSize, limitedOutput, tableType, noDict,
4994e1a33b1SSven Schmidt 				noDictIssue, acceleration);
5004e1a33b1SSven Schmidt 	}
5014e1a33b1SSven Schmidt }
502c72ac7a1SChanho Min 
LZ4_compress_fast(const char * source,char * dest,int inputSize,int maxOutputSize,int acceleration,void * wrkmem)5034e1a33b1SSven Schmidt int LZ4_compress_fast(const char *source, char *dest, int inputSize,
5044e1a33b1SSven Schmidt 	int maxOutputSize, int acceleration, void *wrkmem)
5054e1a33b1SSven Schmidt {
5064e1a33b1SSven Schmidt 	return LZ4_compress_fast_extState(wrkmem, source, dest, inputSize,
5074e1a33b1SSven Schmidt 		maxOutputSize, acceleration);
5084e1a33b1SSven Schmidt }
5094e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_compress_fast);
510c72ac7a1SChanho Min 
LZ4_compress_default(const char * source,char * dest,int inputSize,int maxOutputSize,void * wrkmem)5114e1a33b1SSven Schmidt int LZ4_compress_default(const char *source, char *dest, int inputSize,
5124e1a33b1SSven Schmidt 	int maxOutputSize, void *wrkmem)
5134e1a33b1SSven Schmidt {
5144e1a33b1SSven Schmidt 	return LZ4_compress_fast(source, dest, inputSize,
5154e1a33b1SSven Schmidt 		maxOutputSize, LZ4_ACCELERATION_DEFAULT, wrkmem);
5164e1a33b1SSven Schmidt }
5174e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_compress_default);
518c72ac7a1SChanho Min 
5194e1a33b1SSven Schmidt /*-******************************
5204e1a33b1SSven Schmidt  *	*_destSize() variant
5214e1a33b1SSven Schmidt  ********************************/
LZ4_compress_destSize_generic(LZ4_stream_t_internal * const ctx,const char * const src,char * const dst,int * const srcSizePtr,const int targetDstSize,const tableType_t tableType)5224e1a33b1SSven Schmidt static int LZ4_compress_destSize_generic(
5234e1a33b1SSven Schmidt 	LZ4_stream_t_internal * const ctx,
5244e1a33b1SSven Schmidt 	const char * const src,
5254e1a33b1SSven Schmidt 	char * const dst,
5264e1a33b1SSven Schmidt 	int * const srcSizePtr,
5274e1a33b1SSven Schmidt 	const int targetDstSize,
5284e1a33b1SSven Schmidt 	const tableType_t tableType)
5294e1a33b1SSven Schmidt {
5304e1a33b1SSven Schmidt 	const BYTE *ip = (const BYTE *) src;
5314e1a33b1SSven Schmidt 	const BYTE *base = (const BYTE *) src;
5324e1a33b1SSven Schmidt 	const BYTE *lowLimit = (const BYTE *) src;
5334e1a33b1SSven Schmidt 	const BYTE *anchor = ip;
5344e1a33b1SSven Schmidt 	const BYTE * const iend = ip + *srcSizePtr;
5354e1a33b1SSven Schmidt 	const BYTE * const mflimit = iend - MFLIMIT;
5364e1a33b1SSven Schmidt 	const BYTE * const matchlimit = iend - LASTLITERALS;
5374e1a33b1SSven Schmidt 
5384e1a33b1SSven Schmidt 	BYTE *op = (BYTE *) dst;
5394e1a33b1SSven Schmidt 	BYTE * const oend = op + targetDstSize;
5404e1a33b1SSven Schmidt 	BYTE * const oMaxLit = op + targetDstSize - 2 /* offset */
5414e1a33b1SSven Schmidt 		- 8 /* because 8 + MINMATCH == MFLIMIT */ - 1 /* token */;
5424e1a33b1SSven Schmidt 	BYTE * const oMaxMatch = op + targetDstSize
5434e1a33b1SSven Schmidt 		- (LASTLITERALS + 1 /* token */);
5444e1a33b1SSven Schmidt 	BYTE * const oMaxSeq = oMaxLit - 1 /* token */;
5454e1a33b1SSven Schmidt 
5464e1a33b1SSven Schmidt 	U32 forwardH;
5474e1a33b1SSven Schmidt 
5484e1a33b1SSven Schmidt 	/* Init conditions */
5494e1a33b1SSven Schmidt 	/* Impossible to store anything */
5504e1a33b1SSven Schmidt 	if (targetDstSize < 1)
551c72ac7a1SChanho Min 		return 0;
5524e1a33b1SSven Schmidt 	/* Unsupported input size, too large (or negative) */
5534e1a33b1SSven Schmidt 	if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE)
5544e1a33b1SSven Schmidt 		return 0;
5554e1a33b1SSven Schmidt 	/* Size too large (not within 64K limit) */
5564e1a33b1SSven Schmidt 	if ((tableType == byU16) && (*srcSizePtr >= LZ4_64Klimit))
5574e1a33b1SSven Schmidt 		return 0;
5584e1a33b1SSven Schmidt 	/* Input too small, no compression (all literals) */
5594e1a33b1SSven Schmidt 	if (*srcSizePtr < LZ4_minLength)
5604e1a33b1SSven Schmidt 		goto _last_literals;
5614e1a33b1SSven Schmidt 
5624e1a33b1SSven Schmidt 	/* First Byte */
5634e1a33b1SSven Schmidt 	*srcSizePtr = 0;
5644e1a33b1SSven Schmidt 	LZ4_putPosition(ip, ctx->hashTable, tableType, base);
5654e1a33b1SSven Schmidt 	ip++; forwardH = LZ4_hashPosition(ip, tableType);
5664e1a33b1SSven Schmidt 
5674e1a33b1SSven Schmidt 	/* Main Loop */
5684e1a33b1SSven Schmidt 	for ( ; ; ) {
5694e1a33b1SSven Schmidt 		const BYTE *match;
5704e1a33b1SSven Schmidt 		BYTE *token;
5714e1a33b1SSven Schmidt 
5724e1a33b1SSven Schmidt 		/* Find a match */
5734e1a33b1SSven Schmidt 		{
5744e1a33b1SSven Schmidt 			const BYTE *forwardIp = ip;
5754e1a33b1SSven Schmidt 			unsigned int step = 1;
5764e1a33b1SSven Schmidt 			unsigned int searchMatchNb = 1 << LZ4_SKIPTRIGGER;
5774e1a33b1SSven Schmidt 
5784e1a33b1SSven Schmidt 			do {
5794e1a33b1SSven Schmidt 				U32 h = forwardH;
5804e1a33b1SSven Schmidt 
5814e1a33b1SSven Schmidt 				ip = forwardIp;
5824e1a33b1SSven Schmidt 				forwardIp += step;
5834e1a33b1SSven Schmidt 				step = (searchMatchNb++ >> LZ4_SKIPTRIGGER);
5844e1a33b1SSven Schmidt 
5854e1a33b1SSven Schmidt 				if (unlikely(forwardIp > mflimit))
5864e1a33b1SSven Schmidt 					goto _last_literals;
5874e1a33b1SSven Schmidt 
5884e1a33b1SSven Schmidt 				match = LZ4_getPositionOnHash(h, ctx->hashTable,
5894e1a33b1SSven Schmidt 					tableType, base);
5904e1a33b1SSven Schmidt 				forwardH = LZ4_hashPosition(forwardIp,
5914e1a33b1SSven Schmidt 					tableType);
5924e1a33b1SSven Schmidt 				LZ4_putPositionOnHash(ip, h,
5934e1a33b1SSven Schmidt 					ctx->hashTable, tableType,
5944e1a33b1SSven Schmidt 					base);
5954e1a33b1SSven Schmidt 
5964e1a33b1SSven Schmidt 			} while (((tableType == byU16)
5974e1a33b1SSven Schmidt 				? 0
5984e1a33b1SSven Schmidt 				: (match + MAX_DISTANCE < ip))
5994e1a33b1SSven Schmidt 				|| (LZ4_read32(match) != LZ4_read32(ip)));
6004e1a33b1SSven Schmidt 		}
6014e1a33b1SSven Schmidt 
6024e1a33b1SSven Schmidt 		/* Catch up */
6034e1a33b1SSven Schmidt 		while ((ip > anchor)
6044e1a33b1SSven Schmidt 			&& (match > lowLimit)
6054e1a33b1SSven Schmidt 			&& (unlikely(ip[-1] == match[-1]))) {
6064e1a33b1SSven Schmidt 			ip--;
6074e1a33b1SSven Schmidt 			match--;
6084e1a33b1SSven Schmidt 		}
6094e1a33b1SSven Schmidt 
6104e1a33b1SSven Schmidt 		/* Encode Literal length */
6114e1a33b1SSven Schmidt 		{
6124e1a33b1SSven Schmidt 			unsigned int litLength = (unsigned int)(ip - anchor);
6134e1a33b1SSven Schmidt 
6144e1a33b1SSven Schmidt 			token = op++;
6154e1a33b1SSven Schmidt 			if (op + ((litLength + 240) / 255)
6164e1a33b1SSven Schmidt 				+ litLength > oMaxLit) {
6174e1a33b1SSven Schmidt 				/* Not enough space for a last match */
6184e1a33b1SSven Schmidt 				op--;
6194e1a33b1SSven Schmidt 				goto _last_literals;
6204e1a33b1SSven Schmidt 			}
6214e1a33b1SSven Schmidt 			if (litLength >= RUN_MASK) {
6224e1a33b1SSven Schmidt 				unsigned int len = litLength - RUN_MASK;
6234e1a33b1SSven Schmidt 				*token = (RUN_MASK<<ML_BITS);
6244e1a33b1SSven Schmidt 				for (; len >= 255; len -= 255)
6254e1a33b1SSven Schmidt 					*op++ = 255;
6264e1a33b1SSven Schmidt 				*op++ = (BYTE)len;
6274e1a33b1SSven Schmidt 			} else
6284e1a33b1SSven Schmidt 				*token = (BYTE)(litLength << ML_BITS);
6294e1a33b1SSven Schmidt 
6304e1a33b1SSven Schmidt 			/* Copy Literals */
6314e1a33b1SSven Schmidt 			LZ4_wildCopy(op, anchor, op + litLength);
6324e1a33b1SSven Schmidt 			op += litLength;
6334e1a33b1SSven Schmidt 		}
6344e1a33b1SSven Schmidt 
6354e1a33b1SSven Schmidt _next_match:
6364e1a33b1SSven Schmidt 		/* Encode Offset */
6374e1a33b1SSven Schmidt 		LZ4_writeLE16(op, (U16)(ip - match)); op += 2;
6384e1a33b1SSven Schmidt 
6394e1a33b1SSven Schmidt 		/* Encode MatchLength */
6404e1a33b1SSven Schmidt 		{
6414e1a33b1SSven Schmidt 			size_t matchLength = LZ4_count(ip + MINMATCH,
6424e1a33b1SSven Schmidt 			match + MINMATCH, matchlimit);
6434e1a33b1SSven Schmidt 
6444e1a33b1SSven Schmidt 			if (op + ((matchLength + 240)/255) > oMaxMatch) {
6454e1a33b1SSven Schmidt 				/* Match description too long : reduce it */
6464e1a33b1SSven Schmidt 				matchLength = (15 - 1) + (oMaxMatch - op) * 255;
6474e1a33b1SSven Schmidt 			}
6484e1a33b1SSven Schmidt 			ip += MINMATCH + matchLength;
6494e1a33b1SSven Schmidt 
6504e1a33b1SSven Schmidt 			if (matchLength >= ML_MASK) {
6514e1a33b1SSven Schmidt 				*token += ML_MASK;
6524e1a33b1SSven Schmidt 				matchLength -= ML_MASK;
6534e1a33b1SSven Schmidt 				while (matchLength >= 255) {
6544e1a33b1SSven Schmidt 					matchLength -= 255;
6554e1a33b1SSven Schmidt 					*op++ = 255;
6564e1a33b1SSven Schmidt 				}
6574e1a33b1SSven Schmidt 				*op++ = (BYTE)matchLength;
6584e1a33b1SSven Schmidt 			} else
6594e1a33b1SSven Schmidt 				*token += (BYTE)(matchLength);
6604e1a33b1SSven Schmidt 		}
6614e1a33b1SSven Schmidt 
6624e1a33b1SSven Schmidt 		anchor = ip;
6634e1a33b1SSven Schmidt 
6644e1a33b1SSven Schmidt 		/* Test end of block */
6654e1a33b1SSven Schmidt 		if (ip > mflimit)
6664e1a33b1SSven Schmidt 			break;
6674e1a33b1SSven Schmidt 		if (op > oMaxSeq)
6684e1a33b1SSven Schmidt 			break;
6694e1a33b1SSven Schmidt 
6704e1a33b1SSven Schmidt 		/* Fill table */
6714e1a33b1SSven Schmidt 		LZ4_putPosition(ip - 2, ctx->hashTable, tableType, base);
6724e1a33b1SSven Schmidt 
6734e1a33b1SSven Schmidt 		/* Test next position */
6744e1a33b1SSven Schmidt 		match = LZ4_getPosition(ip, ctx->hashTable, tableType, base);
6754e1a33b1SSven Schmidt 		LZ4_putPosition(ip, ctx->hashTable, tableType, base);
6764e1a33b1SSven Schmidt 
6774e1a33b1SSven Schmidt 		if ((match + MAX_DISTANCE >= ip)
6784e1a33b1SSven Schmidt 			&& (LZ4_read32(match) == LZ4_read32(ip))) {
6794e1a33b1SSven Schmidt 			token = op++; *token = 0;
6804e1a33b1SSven Schmidt 			goto _next_match;
6814e1a33b1SSven Schmidt 		}
6824e1a33b1SSven Schmidt 
6834e1a33b1SSven Schmidt 		/* Prepare next loop */
6844e1a33b1SSven Schmidt 		forwardH = LZ4_hashPosition(++ip, tableType);
6854e1a33b1SSven Schmidt 	}
6864e1a33b1SSven Schmidt 
6874e1a33b1SSven Schmidt _last_literals:
6884e1a33b1SSven Schmidt 	/* Encode Last Literals */
6894e1a33b1SSven Schmidt 	{
6904e1a33b1SSven Schmidt 		size_t lastRunSize = (size_t)(iend - anchor);
6914e1a33b1SSven Schmidt 
6924e1a33b1SSven Schmidt 		if (op + 1 /* token */
6934e1a33b1SSven Schmidt 			+ ((lastRunSize + 240) / 255) /* litLength */
6944e1a33b1SSven Schmidt 			+ lastRunSize /* literals */ > oend) {
6954e1a33b1SSven Schmidt 			/* adapt lastRunSize to fill 'dst' */
6964e1a33b1SSven Schmidt 			lastRunSize	= (oend - op) - 1;
6974e1a33b1SSven Schmidt 			lastRunSize -= (lastRunSize + 240) / 255;
6984e1a33b1SSven Schmidt 		}
6994e1a33b1SSven Schmidt 		ip = anchor + lastRunSize;
7004e1a33b1SSven Schmidt 
7014e1a33b1SSven Schmidt 		if (lastRunSize >= RUN_MASK) {
7024e1a33b1SSven Schmidt 			size_t accumulator = lastRunSize - RUN_MASK;
7034e1a33b1SSven Schmidt 
7044e1a33b1SSven Schmidt 			*op++ = RUN_MASK << ML_BITS;
7054e1a33b1SSven Schmidt 			for (; accumulator >= 255; accumulator -= 255)
7064e1a33b1SSven Schmidt 				*op++ = 255;
7074e1a33b1SSven Schmidt 			*op++ = (BYTE) accumulator;
7084e1a33b1SSven Schmidt 		} else {
7094e1a33b1SSven Schmidt 			*op++ = (BYTE)(lastRunSize<<ML_BITS);
7104e1a33b1SSven Schmidt 		}
711*b1a3e75eSNick Terrell 		LZ4_memcpy(op, anchor, lastRunSize);
7124e1a33b1SSven Schmidt 		op += lastRunSize;
7134e1a33b1SSven Schmidt 	}
7144e1a33b1SSven Schmidt 
7154e1a33b1SSven Schmidt 	/* End */
7164e1a33b1SSven Schmidt 	*srcSizePtr = (int) (((const char *)ip) - src);
7174e1a33b1SSven Schmidt 	return (int) (((char *)op) - dst);
7184e1a33b1SSven Schmidt }
7194e1a33b1SSven Schmidt 
LZ4_compress_destSize_extState(LZ4_stream_t * state,const char * src,char * dst,int * srcSizePtr,int targetDstSize)7204e1a33b1SSven Schmidt static int LZ4_compress_destSize_extState(
7214e1a33b1SSven Schmidt 	LZ4_stream_t *state,
7224e1a33b1SSven Schmidt 	const char *src,
7234e1a33b1SSven Schmidt 	char *dst,
7244e1a33b1SSven Schmidt 	int *srcSizePtr,
7254e1a33b1SSven Schmidt 	int targetDstSize)
7264e1a33b1SSven Schmidt {
7274e1a33b1SSven Schmidt #if LZ4_ARCH64
7284e1a33b1SSven Schmidt 	const tableType_t tableType = byU32;
7294e1a33b1SSven Schmidt #else
7304e1a33b1SSven Schmidt 	const tableType_t tableType = byPtr;
7314e1a33b1SSven Schmidt #endif
7324e1a33b1SSven Schmidt 
7334e1a33b1SSven Schmidt 	LZ4_resetStream(state);
7344e1a33b1SSven Schmidt 
7354e1a33b1SSven Schmidt 	if (targetDstSize >= LZ4_COMPRESSBOUND(*srcSizePtr)) {
7364e1a33b1SSven Schmidt 		/* compression success is guaranteed */
7374e1a33b1SSven Schmidt 		return LZ4_compress_fast_extState(
7384e1a33b1SSven Schmidt 			state, src, dst, *srcSizePtr,
7394e1a33b1SSven Schmidt 			targetDstSize, 1);
7404e1a33b1SSven Schmidt 	} else {
7414e1a33b1SSven Schmidt 		if (*srcSizePtr < LZ4_64Klimit)
7424e1a33b1SSven Schmidt 			return LZ4_compress_destSize_generic(
7434e1a33b1SSven Schmidt 				&state->internal_donotuse,
7444e1a33b1SSven Schmidt 				src, dst, srcSizePtr,
7454e1a33b1SSven Schmidt 				targetDstSize, byU16);
7464e1a33b1SSven Schmidt 		else
7474e1a33b1SSven Schmidt 			return LZ4_compress_destSize_generic(
7484e1a33b1SSven Schmidt 				&state->internal_donotuse,
7494e1a33b1SSven Schmidt 				src, dst, srcSizePtr,
7504e1a33b1SSven Schmidt 				targetDstSize, tableType);
7514e1a33b1SSven Schmidt 	}
7524e1a33b1SSven Schmidt }
7534e1a33b1SSven Schmidt 
7544e1a33b1SSven Schmidt 
LZ4_compress_destSize(const char * src,char * dst,int * srcSizePtr,int targetDstSize,void * wrkmem)7554e1a33b1SSven Schmidt int LZ4_compress_destSize(
7564e1a33b1SSven Schmidt 	const char *src,
7574e1a33b1SSven Schmidt 	char *dst,
7584e1a33b1SSven Schmidt 	int *srcSizePtr,
7594e1a33b1SSven Schmidt 	int targetDstSize,
7604e1a33b1SSven Schmidt 	void *wrkmem)
7614e1a33b1SSven Schmidt {
7624e1a33b1SSven Schmidt 	return LZ4_compress_destSize_extState(wrkmem, src, dst, srcSizePtr,
7634e1a33b1SSven Schmidt 		targetDstSize);
7644e1a33b1SSven Schmidt }
7654e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_compress_destSize);
7664e1a33b1SSven Schmidt 
7674e1a33b1SSven Schmidt /*-******************************
7684e1a33b1SSven Schmidt  *	Streaming functions
7694e1a33b1SSven Schmidt  ********************************/
LZ4_resetStream(LZ4_stream_t * LZ4_stream)7704e1a33b1SSven Schmidt void LZ4_resetStream(LZ4_stream_t *LZ4_stream)
7714e1a33b1SSven Schmidt {
7724e1a33b1SSven Schmidt 	memset(LZ4_stream, 0, sizeof(LZ4_stream_t));
7734e1a33b1SSven Schmidt }
7744e1a33b1SSven Schmidt 
LZ4_loadDict(LZ4_stream_t * LZ4_dict,const char * dictionary,int dictSize)7754e1a33b1SSven Schmidt int LZ4_loadDict(LZ4_stream_t *LZ4_dict,
7764e1a33b1SSven Schmidt 	const char *dictionary, int dictSize)
7774e1a33b1SSven Schmidt {
7784e1a33b1SSven Schmidt 	LZ4_stream_t_internal *dict = &LZ4_dict->internal_donotuse;
7794e1a33b1SSven Schmidt 	const BYTE *p = (const BYTE *)dictionary;
7804e1a33b1SSven Schmidt 	const BYTE * const dictEnd = p + dictSize;
7814e1a33b1SSven Schmidt 	const BYTE *base;
7824e1a33b1SSven Schmidt 
7834e1a33b1SSven Schmidt 	if ((dict->initCheck)
7844e1a33b1SSven Schmidt 		|| (dict->currentOffset > 1 * GB)) {
7854e1a33b1SSven Schmidt 		/* Uninitialized structure, or reuse overflow */
7864e1a33b1SSven Schmidt 		LZ4_resetStream(LZ4_dict);
7874e1a33b1SSven Schmidt 	}
7884e1a33b1SSven Schmidt 
7894e1a33b1SSven Schmidt 	if (dictSize < (int)HASH_UNIT) {
7904e1a33b1SSven Schmidt 		dict->dictionary = NULL;
7914e1a33b1SSven Schmidt 		dict->dictSize = 0;
7924e1a33b1SSven Schmidt 		return 0;
7934e1a33b1SSven Schmidt 	}
7944e1a33b1SSven Schmidt 
7954e1a33b1SSven Schmidt 	if ((dictEnd - p) > 64 * KB)
7964e1a33b1SSven Schmidt 		p = dictEnd - 64 * KB;
7974e1a33b1SSven Schmidt 	dict->currentOffset += 64 * KB;
7984e1a33b1SSven Schmidt 	base = p - dict->currentOffset;
7994e1a33b1SSven Schmidt 	dict->dictionary = p;
8004e1a33b1SSven Schmidt 	dict->dictSize = (U32)(dictEnd - p);
8014e1a33b1SSven Schmidt 	dict->currentOffset += dict->dictSize;
8024e1a33b1SSven Schmidt 
8034e1a33b1SSven Schmidt 	while (p <= dictEnd - HASH_UNIT) {
8044e1a33b1SSven Schmidt 		LZ4_putPosition(p, dict->hashTable, byU32, base);
8054e1a33b1SSven Schmidt 		p += 3;
8064e1a33b1SSven Schmidt 	}
8074e1a33b1SSven Schmidt 
8084e1a33b1SSven Schmidt 	return dict->dictSize;
8094e1a33b1SSven Schmidt }
8104e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_loadDict);
8114e1a33b1SSven Schmidt 
LZ4_renormDictT(LZ4_stream_t_internal * LZ4_dict,const BYTE * src)8124e1a33b1SSven Schmidt static void LZ4_renormDictT(LZ4_stream_t_internal *LZ4_dict,
8134e1a33b1SSven Schmidt 	const BYTE *src)
8144e1a33b1SSven Schmidt {
8154e1a33b1SSven Schmidt 	if ((LZ4_dict->currentOffset > 0x80000000) ||
8164e1a33b1SSven Schmidt 		((uptrval)LZ4_dict->currentOffset > (uptrval)src)) {
8174e1a33b1SSven Schmidt 		/* address space overflow */
8184e1a33b1SSven Schmidt 		/* rescale hash table */
8194e1a33b1SSven Schmidt 		U32 const delta = LZ4_dict->currentOffset - 64 * KB;
8204e1a33b1SSven Schmidt 		const BYTE *dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize;
8214e1a33b1SSven Schmidt 		int i;
8224e1a33b1SSven Schmidt 
8234e1a33b1SSven Schmidt 		for (i = 0; i < LZ4_HASH_SIZE_U32; i++) {
8244e1a33b1SSven Schmidt 			if (LZ4_dict->hashTable[i] < delta)
8254e1a33b1SSven Schmidt 				LZ4_dict->hashTable[i] = 0;
8264e1a33b1SSven Schmidt 			else
8274e1a33b1SSven Schmidt 				LZ4_dict->hashTable[i] -= delta;
8284e1a33b1SSven Schmidt 		}
8294e1a33b1SSven Schmidt 		LZ4_dict->currentOffset = 64 * KB;
8304e1a33b1SSven Schmidt 		if (LZ4_dict->dictSize > 64 * KB)
8314e1a33b1SSven Schmidt 			LZ4_dict->dictSize = 64 * KB;
8324e1a33b1SSven Schmidt 		LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize;
8334e1a33b1SSven Schmidt 	}
8344e1a33b1SSven Schmidt }
8354e1a33b1SSven Schmidt 
LZ4_saveDict(LZ4_stream_t * LZ4_dict,char * safeBuffer,int dictSize)8364e1a33b1SSven Schmidt int LZ4_saveDict(LZ4_stream_t *LZ4_dict, char *safeBuffer, int dictSize)
8374e1a33b1SSven Schmidt {
8384e1a33b1SSven Schmidt 	LZ4_stream_t_internal * const dict = &LZ4_dict->internal_donotuse;
8394e1a33b1SSven Schmidt 	const BYTE * const previousDictEnd = dict->dictionary + dict->dictSize;
8404e1a33b1SSven Schmidt 
8414e1a33b1SSven Schmidt 	if ((U32)dictSize > 64 * KB) {
8424e1a33b1SSven Schmidt 		/* useless to define a dictionary > 64 * KB */
8434e1a33b1SSven Schmidt 		dictSize = 64 * KB;
8444e1a33b1SSven Schmidt 	}
8454e1a33b1SSven Schmidt 	if ((U32)dictSize > dict->dictSize)
8464e1a33b1SSven Schmidt 		dictSize = dict->dictSize;
8474e1a33b1SSven Schmidt 
8484e1a33b1SSven Schmidt 	memmove(safeBuffer, previousDictEnd - dictSize, dictSize);
8494e1a33b1SSven Schmidt 
8504e1a33b1SSven Schmidt 	dict->dictionary = (const BYTE *)safeBuffer;
8514e1a33b1SSven Schmidt 	dict->dictSize = (U32)dictSize;
8524e1a33b1SSven Schmidt 
8534e1a33b1SSven Schmidt 	return dictSize;
8544e1a33b1SSven Schmidt }
8554e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_saveDict);
8564e1a33b1SSven Schmidt 
LZ4_compress_fast_continue(LZ4_stream_t * LZ4_stream,const char * source,char * dest,int inputSize,int maxOutputSize,int acceleration)8574e1a33b1SSven Schmidt int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream, const char *source,
8584e1a33b1SSven Schmidt 	char *dest, int inputSize, int maxOutputSize, int acceleration)
8594e1a33b1SSven Schmidt {
8604e1a33b1SSven Schmidt 	LZ4_stream_t_internal *streamPtr = &LZ4_stream->internal_donotuse;
8614e1a33b1SSven Schmidt 	const BYTE * const dictEnd = streamPtr->dictionary
8624e1a33b1SSven Schmidt 		+ streamPtr->dictSize;
8634e1a33b1SSven Schmidt 
8644e1a33b1SSven Schmidt 	const BYTE *smallest = (const BYTE *) source;
8654e1a33b1SSven Schmidt 
8664e1a33b1SSven Schmidt 	if (streamPtr->initCheck) {
8674e1a33b1SSven Schmidt 		/* Uninitialized structure detected */
8684e1a33b1SSven Schmidt 		return 0;
8694e1a33b1SSven Schmidt 	}
8704e1a33b1SSven Schmidt 
8714e1a33b1SSven Schmidt 	if ((streamPtr->dictSize > 0) && (smallest > dictEnd))
8724e1a33b1SSven Schmidt 		smallest = dictEnd;
8734e1a33b1SSven Schmidt 
8744e1a33b1SSven Schmidt 	LZ4_renormDictT(streamPtr, smallest);
8754e1a33b1SSven Schmidt 
8764e1a33b1SSven Schmidt 	if (acceleration < 1)
8774e1a33b1SSven Schmidt 		acceleration = LZ4_ACCELERATION_DEFAULT;
8784e1a33b1SSven Schmidt 
8794e1a33b1SSven Schmidt 	/* Check overlapping input/dictionary space */
8804e1a33b1SSven Schmidt 	{
8814e1a33b1SSven Schmidt 		const BYTE *sourceEnd = (const BYTE *) source + inputSize;
8824e1a33b1SSven Schmidt 
8834e1a33b1SSven Schmidt 		if ((sourceEnd > streamPtr->dictionary)
8844e1a33b1SSven Schmidt 			&& (sourceEnd < dictEnd)) {
8854e1a33b1SSven Schmidt 			streamPtr->dictSize = (U32)(dictEnd - sourceEnd);
8864e1a33b1SSven Schmidt 			if (streamPtr->dictSize > 64 * KB)
8874e1a33b1SSven Schmidt 				streamPtr->dictSize = 64 * KB;
8884e1a33b1SSven Schmidt 			if (streamPtr->dictSize < 4)
8894e1a33b1SSven Schmidt 				streamPtr->dictSize = 0;
8904e1a33b1SSven Schmidt 			streamPtr->dictionary = dictEnd - streamPtr->dictSize;
8914e1a33b1SSven Schmidt 		}
8924e1a33b1SSven Schmidt 	}
8934e1a33b1SSven Schmidt 
8944e1a33b1SSven Schmidt 	/* prefix mode : source data follows dictionary */
8954e1a33b1SSven Schmidt 	if (dictEnd == (const BYTE *)source) {
8964e1a33b1SSven Schmidt 		int result;
8974e1a33b1SSven Schmidt 
8984e1a33b1SSven Schmidt 		if ((streamPtr->dictSize < 64 * KB) &&
8994e1a33b1SSven Schmidt 			(streamPtr->dictSize < streamPtr->currentOffset)) {
9004e1a33b1SSven Schmidt 			result = LZ4_compress_generic(
9014e1a33b1SSven Schmidt 				streamPtr, source, dest, inputSize,
9024e1a33b1SSven Schmidt 				maxOutputSize, limitedOutput, byU32,
9034e1a33b1SSven Schmidt 				withPrefix64k, dictSmall, acceleration);
9044e1a33b1SSven Schmidt 		} else {
9054e1a33b1SSven Schmidt 			result = LZ4_compress_generic(
9064e1a33b1SSven Schmidt 				streamPtr, source, dest, inputSize,
9074e1a33b1SSven Schmidt 				maxOutputSize, limitedOutput, byU32,
9084e1a33b1SSven Schmidt 				withPrefix64k, noDictIssue, acceleration);
9094e1a33b1SSven Schmidt 		}
9104e1a33b1SSven Schmidt 		streamPtr->dictSize += (U32)inputSize;
9114e1a33b1SSven Schmidt 		streamPtr->currentOffset += (U32)inputSize;
9124e1a33b1SSven Schmidt 		return result;
9134e1a33b1SSven Schmidt 	}
9144e1a33b1SSven Schmidt 
9154e1a33b1SSven Schmidt 	/* external dictionary mode */
9164e1a33b1SSven Schmidt 	{
9174e1a33b1SSven Schmidt 		int result;
9184e1a33b1SSven Schmidt 
9194e1a33b1SSven Schmidt 		if ((streamPtr->dictSize < 64 * KB) &&
9204e1a33b1SSven Schmidt 			(streamPtr->dictSize < streamPtr->currentOffset)) {
9214e1a33b1SSven Schmidt 			result = LZ4_compress_generic(
9224e1a33b1SSven Schmidt 				streamPtr, source, dest, inputSize,
9234e1a33b1SSven Schmidt 				maxOutputSize, limitedOutput, byU32,
9244e1a33b1SSven Schmidt 				usingExtDict, dictSmall, acceleration);
9254e1a33b1SSven Schmidt 		} else {
9264e1a33b1SSven Schmidt 			result = LZ4_compress_generic(
9274e1a33b1SSven Schmidt 				streamPtr, source, dest, inputSize,
9284e1a33b1SSven Schmidt 				maxOutputSize, limitedOutput, byU32,
9294e1a33b1SSven Schmidt 				usingExtDict, noDictIssue, acceleration);
9304e1a33b1SSven Schmidt 		}
9314e1a33b1SSven Schmidt 		streamPtr->dictionary = (const BYTE *)source;
9324e1a33b1SSven Schmidt 		streamPtr->dictSize = (U32)inputSize;
9334e1a33b1SSven Schmidt 		streamPtr->currentOffset += (U32)inputSize;
9344e1a33b1SSven Schmidt 		return result;
9354e1a33b1SSven Schmidt 	}
9364e1a33b1SSven Schmidt }
9374e1a33b1SSven Schmidt EXPORT_SYMBOL(LZ4_compress_fast_continue);
9384e1a33b1SSven Schmidt 
939ee8a99bdSRichard Laager MODULE_LICENSE("Dual BSD/GPL");
940c72ac7a1SChanho Min MODULE_DESCRIPTION("LZ4 compressor");
941