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/module.h>
398c2ecf20Sopenharmony_ci#include <linux/kernel.h>
408c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic const int LZ4_minLength = (MFLIMIT + 1);
438c2ecf20Sopenharmony_cistatic const int LZ4_64Klimit = ((64 * KB) + (MFLIMIT - 1));
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/*-******************************
468c2ecf20Sopenharmony_ci *	Compression functions
478c2ecf20Sopenharmony_ci ********************************/
488c2ecf20Sopenharmony_cistatic FORCE_INLINE U32 LZ4_hash4(
498c2ecf20Sopenharmony_ci	U32 sequence,
508c2ecf20Sopenharmony_ci	tableType_t const tableType)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	if (tableType == byU16)
538c2ecf20Sopenharmony_ci		return ((sequence * 2654435761U)
548c2ecf20Sopenharmony_ci			>> ((MINMATCH * 8) - (LZ4_HASHLOG + 1)));
558c2ecf20Sopenharmony_ci	else
568c2ecf20Sopenharmony_ci		return ((sequence * 2654435761U)
578c2ecf20Sopenharmony_ci			>> ((MINMATCH * 8) - LZ4_HASHLOG));
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic FORCE_INLINE U32 LZ4_hash5(
618c2ecf20Sopenharmony_ci	U64 sequence,
628c2ecf20Sopenharmony_ci	tableType_t const tableType)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	const U32 hashLog = (tableType == byU16)
658c2ecf20Sopenharmony_ci		? LZ4_HASHLOG + 1
668c2ecf20Sopenharmony_ci		: LZ4_HASHLOG;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#if LZ4_LITTLE_ENDIAN
698c2ecf20Sopenharmony_ci	static const U64 prime5bytes = 889523592379ULL;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog));
728c2ecf20Sopenharmony_ci#else
738c2ecf20Sopenharmony_ci	static const U64 prime8bytes = 11400714785074694791ULL;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog));
768c2ecf20Sopenharmony_ci#endif
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic FORCE_INLINE U32 LZ4_hashPosition(
808c2ecf20Sopenharmony_ci	const void *p,
818c2ecf20Sopenharmony_ci	tableType_t const tableType)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci#if LZ4_ARCH64
848c2ecf20Sopenharmony_ci	if (tableType == byU32)
858c2ecf20Sopenharmony_ci		return LZ4_hash5(LZ4_read_ARCH(p), tableType);
868c2ecf20Sopenharmony_ci#endif
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	return LZ4_hash4(LZ4_read32(p), tableType);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic void LZ4_putPositionOnHash(
928c2ecf20Sopenharmony_ci	const BYTE *p,
938c2ecf20Sopenharmony_ci	U32 h,
948c2ecf20Sopenharmony_ci	void *tableBase,
958c2ecf20Sopenharmony_ci	tableType_t const tableType,
968c2ecf20Sopenharmony_ci	const BYTE *srcBase)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	switch (tableType) {
998c2ecf20Sopenharmony_ci	case byPtr:
1008c2ecf20Sopenharmony_ci	{
1018c2ecf20Sopenharmony_ci		const BYTE **hashTable = (const BYTE **)tableBase;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci		hashTable[h] = p;
1048c2ecf20Sopenharmony_ci		return;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci	case byU32:
1078c2ecf20Sopenharmony_ci	{
1088c2ecf20Sopenharmony_ci		U32 *hashTable = (U32 *) tableBase;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci		hashTable[h] = (U32)(p - srcBase);
1118c2ecf20Sopenharmony_ci		return;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci	case byU16:
1148c2ecf20Sopenharmony_ci	{
1158c2ecf20Sopenharmony_ci		U16 *hashTable = (U16 *) tableBase;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci		hashTable[h] = (U16)(p - srcBase);
1188c2ecf20Sopenharmony_ci		return;
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci	}
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic FORCE_INLINE void LZ4_putPosition(
1248c2ecf20Sopenharmony_ci	const BYTE *p,
1258c2ecf20Sopenharmony_ci	void *tableBase,
1268c2ecf20Sopenharmony_ci	tableType_t tableType,
1278c2ecf20Sopenharmony_ci	const BYTE *srcBase)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	U32 const h = LZ4_hashPosition(p, tableType);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic const BYTE *LZ4_getPositionOnHash(
1358c2ecf20Sopenharmony_ci	U32 h,
1368c2ecf20Sopenharmony_ci	void *tableBase,
1378c2ecf20Sopenharmony_ci	tableType_t tableType,
1388c2ecf20Sopenharmony_ci	const BYTE *srcBase)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	if (tableType == byPtr) {
1418c2ecf20Sopenharmony_ci		const BYTE **hashTable = (const BYTE **) tableBase;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci		return hashTable[h];
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	if (tableType == byU32) {
1478c2ecf20Sopenharmony_ci		const U32 * const hashTable = (U32 *) tableBase;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci		return hashTable[h] + srcBase;
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	{
1538c2ecf20Sopenharmony_ci		/* default, to ensure a return */
1548c2ecf20Sopenharmony_ci		const U16 * const hashTable = (U16 *) tableBase;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci		return hashTable[h] + srcBase;
1578c2ecf20Sopenharmony_ci	}
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic FORCE_INLINE const BYTE *LZ4_getPosition(
1618c2ecf20Sopenharmony_ci	const BYTE *p,
1628c2ecf20Sopenharmony_ci	void *tableBase,
1638c2ecf20Sopenharmony_ci	tableType_t tableType,
1648c2ecf20Sopenharmony_ci	const BYTE *srcBase)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	U32 const h = LZ4_hashPosition(p, tableType);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci/*
1738c2ecf20Sopenharmony_ci * LZ4_compress_generic() :
1748c2ecf20Sopenharmony_ci * inlined, to ensure branches are decided at compilation time
1758c2ecf20Sopenharmony_ci */
1768c2ecf20Sopenharmony_cistatic FORCE_INLINE int LZ4_compress_generic(
1778c2ecf20Sopenharmony_ci	LZ4_stream_t_internal * const dictPtr,
1788c2ecf20Sopenharmony_ci	const char * const source,
1798c2ecf20Sopenharmony_ci	char * const dest,
1808c2ecf20Sopenharmony_ci	const int inputSize,
1818c2ecf20Sopenharmony_ci	const int maxOutputSize,
1828c2ecf20Sopenharmony_ci	const limitedOutput_directive outputLimited,
1838c2ecf20Sopenharmony_ci	const tableType_t tableType,
1848c2ecf20Sopenharmony_ci	const dict_directive dict,
1858c2ecf20Sopenharmony_ci	const dictIssue_directive dictIssue,
1868c2ecf20Sopenharmony_ci	const U32 acceleration)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	const BYTE *ip = (const BYTE *) source;
1898c2ecf20Sopenharmony_ci	const BYTE *base;
1908c2ecf20Sopenharmony_ci	const BYTE *lowLimit;
1918c2ecf20Sopenharmony_ci	const BYTE * const lowRefLimit = ip - dictPtr->dictSize;
1928c2ecf20Sopenharmony_ci	const BYTE * const dictionary = dictPtr->dictionary;
1938c2ecf20Sopenharmony_ci	const BYTE * const dictEnd = dictionary + dictPtr->dictSize;
1948c2ecf20Sopenharmony_ci	const size_t dictDelta = dictEnd - (const BYTE *)source;
1958c2ecf20Sopenharmony_ci	const BYTE *anchor = (const BYTE *) source;
1968c2ecf20Sopenharmony_ci	const BYTE * const iend = ip + inputSize;
1978c2ecf20Sopenharmony_ci	const BYTE * const mflimit = iend - MFLIMIT;
1988c2ecf20Sopenharmony_ci	const BYTE * const matchlimit = iend - LASTLITERALS;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	BYTE *op = (BYTE *) dest;
2018c2ecf20Sopenharmony_ci	BYTE * const olimit = op + maxOutputSize;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	U32 forwardH;
2048c2ecf20Sopenharmony_ci	size_t refDelta = 0;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	/* Init conditions */
2078c2ecf20Sopenharmony_ci	if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) {
2088c2ecf20Sopenharmony_ci		/* Unsupported inputSize, too large (or negative) */
2098c2ecf20Sopenharmony_ci		return 0;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	switch (dict) {
2138c2ecf20Sopenharmony_ci	case noDict:
2148c2ecf20Sopenharmony_ci	default:
2158c2ecf20Sopenharmony_ci		base = (const BYTE *)source;
2168c2ecf20Sopenharmony_ci		lowLimit = (const BYTE *)source;
2178c2ecf20Sopenharmony_ci		break;
2188c2ecf20Sopenharmony_ci	case withPrefix64k:
2198c2ecf20Sopenharmony_ci		base = (const BYTE *)source - dictPtr->currentOffset;
2208c2ecf20Sopenharmony_ci		lowLimit = (const BYTE *)source - dictPtr->dictSize;
2218c2ecf20Sopenharmony_ci		break;
2228c2ecf20Sopenharmony_ci	case usingExtDict:
2238c2ecf20Sopenharmony_ci		base = (const BYTE *)source - dictPtr->currentOffset;
2248c2ecf20Sopenharmony_ci		lowLimit = (const BYTE *)source;
2258c2ecf20Sopenharmony_ci		break;
2268c2ecf20Sopenharmony_ci	}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	if ((tableType == byU16)
2298c2ecf20Sopenharmony_ci		&& (inputSize >= LZ4_64Klimit)) {
2308c2ecf20Sopenharmony_ci		/* Size too large (not within 64K limit) */
2318c2ecf20Sopenharmony_ci		return 0;
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	if (inputSize < LZ4_minLength) {
2358c2ecf20Sopenharmony_ci		/* Input too small, no compression (all literals) */
2368c2ecf20Sopenharmony_ci		goto _last_literals;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* First Byte */
2408c2ecf20Sopenharmony_ci	LZ4_putPosition(ip, dictPtr->hashTable, tableType, base);
2418c2ecf20Sopenharmony_ci	ip++;
2428c2ecf20Sopenharmony_ci	forwardH = LZ4_hashPosition(ip, tableType);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	/* Main Loop */
2458c2ecf20Sopenharmony_ci	for ( ; ; ) {
2468c2ecf20Sopenharmony_ci		const BYTE *match;
2478c2ecf20Sopenharmony_ci		BYTE *token;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci		/* Find a match */
2508c2ecf20Sopenharmony_ci		{
2518c2ecf20Sopenharmony_ci			const BYTE *forwardIp = ip;
2528c2ecf20Sopenharmony_ci			unsigned int step = 1;
2538c2ecf20Sopenharmony_ci			unsigned int searchMatchNb = acceleration << LZ4_SKIPTRIGGER;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci			do {
2568c2ecf20Sopenharmony_ci				U32 const h = forwardH;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci				ip = forwardIp;
2598c2ecf20Sopenharmony_ci				forwardIp += step;
2608c2ecf20Sopenharmony_ci				step = (searchMatchNb++ >> LZ4_SKIPTRIGGER);
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci				if (unlikely(forwardIp > mflimit))
2638c2ecf20Sopenharmony_ci					goto _last_literals;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci				match = LZ4_getPositionOnHash(h,
2668c2ecf20Sopenharmony_ci					dictPtr->hashTable,
2678c2ecf20Sopenharmony_ci					tableType, base);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci				if (dict == usingExtDict) {
2708c2ecf20Sopenharmony_ci					if (match < (const BYTE *)source) {
2718c2ecf20Sopenharmony_ci						refDelta = dictDelta;
2728c2ecf20Sopenharmony_ci						lowLimit = dictionary;
2738c2ecf20Sopenharmony_ci					} else {
2748c2ecf20Sopenharmony_ci						refDelta = 0;
2758c2ecf20Sopenharmony_ci						lowLimit = (const BYTE *)source;
2768c2ecf20Sopenharmony_ci				}	 }
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci				forwardH = LZ4_hashPosition(forwardIp,
2798c2ecf20Sopenharmony_ci					tableType);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci				LZ4_putPositionOnHash(ip, h, dictPtr->hashTable,
2828c2ecf20Sopenharmony_ci					tableType, base);
2838c2ecf20Sopenharmony_ci			} while (((dictIssue == dictSmall)
2848c2ecf20Sopenharmony_ci					? (match < lowRefLimit)
2858c2ecf20Sopenharmony_ci					: 0)
2868c2ecf20Sopenharmony_ci				|| ((tableType == byU16)
2878c2ecf20Sopenharmony_ci					? 0
2888c2ecf20Sopenharmony_ci					: (match + MAX_DISTANCE < ip))
2898c2ecf20Sopenharmony_ci				|| (LZ4_read32(match + refDelta)
2908c2ecf20Sopenharmony_ci					!= LZ4_read32(ip)));
2918c2ecf20Sopenharmony_ci		}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci		/* Catch up */
2948c2ecf20Sopenharmony_ci		while (((ip > anchor) & (match + refDelta > lowLimit))
2958c2ecf20Sopenharmony_ci				&& (unlikely(ip[-1] == match[refDelta - 1]))) {
2968c2ecf20Sopenharmony_ci			ip--;
2978c2ecf20Sopenharmony_ci			match--;
2988c2ecf20Sopenharmony_ci		}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci		/* Encode Literals */
3018c2ecf20Sopenharmony_ci		{
3028c2ecf20Sopenharmony_ci			unsigned const int litLength = (unsigned int)(ip - anchor);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci			token = op++;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci			if ((outputLimited) &&
3078c2ecf20Sopenharmony_ci				/* Check output buffer overflow */
3088c2ecf20Sopenharmony_ci				(unlikely(op + litLength +
3098c2ecf20Sopenharmony_ci					(2 + 1 + LASTLITERALS) +
3108c2ecf20Sopenharmony_ci					(litLength / 255) > olimit)))
3118c2ecf20Sopenharmony_ci				return 0;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci			if (litLength >= RUN_MASK) {
3148c2ecf20Sopenharmony_ci				int len = (int)litLength - RUN_MASK;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci				*token = (RUN_MASK << ML_BITS);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci				for (; len >= 255; len -= 255)
3198c2ecf20Sopenharmony_ci					*op++ = 255;
3208c2ecf20Sopenharmony_ci				*op++ = (BYTE)len;
3218c2ecf20Sopenharmony_ci			} else
3228c2ecf20Sopenharmony_ci				*token = (BYTE)(litLength << ML_BITS);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci			/* Copy Literals */
3258c2ecf20Sopenharmony_ci			LZ4_wildCopy(op, anchor, op + litLength);
3268c2ecf20Sopenharmony_ci			op += litLength;
3278c2ecf20Sopenharmony_ci		}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci_next_match:
3308c2ecf20Sopenharmony_ci		/* Encode Offset */
3318c2ecf20Sopenharmony_ci		LZ4_writeLE16(op, (U16)(ip - match));
3328c2ecf20Sopenharmony_ci		op += 2;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci		/* Encode MatchLength */
3358c2ecf20Sopenharmony_ci		{
3368c2ecf20Sopenharmony_ci			unsigned int matchCode;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci			if ((dict == usingExtDict)
3398c2ecf20Sopenharmony_ci				&& (lowLimit == dictionary)) {
3408c2ecf20Sopenharmony_ci				const BYTE *limit;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci				match += refDelta;
3438c2ecf20Sopenharmony_ci				limit = ip + (dictEnd - match);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci				if (limit > matchlimit)
3468c2ecf20Sopenharmony_ci					limit = matchlimit;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci				matchCode = LZ4_count(ip + MINMATCH,
3498c2ecf20Sopenharmony_ci					match + MINMATCH, limit);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci				ip += MINMATCH + matchCode;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci				if (ip == limit) {
3548c2ecf20Sopenharmony_ci					unsigned const int more = LZ4_count(ip,
3558c2ecf20Sopenharmony_ci						(const BYTE *)source,
3568c2ecf20Sopenharmony_ci						matchlimit);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci					matchCode += more;
3598c2ecf20Sopenharmony_ci					ip += more;
3608c2ecf20Sopenharmony_ci				}
3618c2ecf20Sopenharmony_ci			} else {
3628c2ecf20Sopenharmony_ci				matchCode = LZ4_count(ip + MINMATCH,
3638c2ecf20Sopenharmony_ci					match + MINMATCH, matchlimit);
3648c2ecf20Sopenharmony_ci				ip += MINMATCH + matchCode;
3658c2ecf20Sopenharmony_ci			}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci			if (outputLimited &&
3688c2ecf20Sopenharmony_ci				/* Check output buffer overflow */
3698c2ecf20Sopenharmony_ci				(unlikely(op +
3708c2ecf20Sopenharmony_ci					(1 + LASTLITERALS) +
3718c2ecf20Sopenharmony_ci					(matchCode >> 8) > olimit)))
3728c2ecf20Sopenharmony_ci				return 0;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci			if (matchCode >= ML_MASK) {
3758c2ecf20Sopenharmony_ci				*token += ML_MASK;
3768c2ecf20Sopenharmony_ci				matchCode -= ML_MASK;
3778c2ecf20Sopenharmony_ci				LZ4_write32(op, 0xFFFFFFFF);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci				while (matchCode >= 4 * 255) {
3808c2ecf20Sopenharmony_ci					op += 4;
3818c2ecf20Sopenharmony_ci					LZ4_write32(op, 0xFFFFFFFF);
3828c2ecf20Sopenharmony_ci					matchCode -= 4 * 255;
3838c2ecf20Sopenharmony_ci				}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci				op += matchCode / 255;
3868c2ecf20Sopenharmony_ci				*op++ = (BYTE)(matchCode % 255);
3878c2ecf20Sopenharmony_ci			} else
3888c2ecf20Sopenharmony_ci				*token += (BYTE)(matchCode);
3898c2ecf20Sopenharmony_ci		}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci		anchor = ip;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci		/* Test end of chunk */
3948c2ecf20Sopenharmony_ci		if (ip > mflimit)
3958c2ecf20Sopenharmony_ci			break;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci		/* Fill table */
3988c2ecf20Sopenharmony_ci		LZ4_putPosition(ip - 2, dictPtr->hashTable, tableType, base);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci		/* Test next position */
4018c2ecf20Sopenharmony_ci		match = LZ4_getPosition(ip, dictPtr->hashTable,
4028c2ecf20Sopenharmony_ci			tableType, base);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci		if (dict == usingExtDict) {
4058c2ecf20Sopenharmony_ci			if (match < (const BYTE *)source) {
4068c2ecf20Sopenharmony_ci				refDelta = dictDelta;
4078c2ecf20Sopenharmony_ci				lowLimit = dictionary;
4088c2ecf20Sopenharmony_ci			} else {
4098c2ecf20Sopenharmony_ci				refDelta = 0;
4108c2ecf20Sopenharmony_ci				lowLimit = (const BYTE *)source;
4118c2ecf20Sopenharmony_ci			}
4128c2ecf20Sopenharmony_ci		}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci		LZ4_putPosition(ip, dictPtr->hashTable, tableType, base);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci		if (((dictIssue == dictSmall) ? (match >= lowRefLimit) : 1)
4178c2ecf20Sopenharmony_ci			&& (match + MAX_DISTANCE >= ip)
4188c2ecf20Sopenharmony_ci			&& (LZ4_read32(match + refDelta) == LZ4_read32(ip))) {
4198c2ecf20Sopenharmony_ci			token = op++;
4208c2ecf20Sopenharmony_ci			*token = 0;
4218c2ecf20Sopenharmony_ci			goto _next_match;
4228c2ecf20Sopenharmony_ci		}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci		/* Prepare next loop */
4258c2ecf20Sopenharmony_ci		forwardH = LZ4_hashPosition(++ip, tableType);
4268c2ecf20Sopenharmony_ci	}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci_last_literals:
4298c2ecf20Sopenharmony_ci	/* Encode Last Literals */
4308c2ecf20Sopenharmony_ci	{
4318c2ecf20Sopenharmony_ci		size_t const lastRun = (size_t)(iend - anchor);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci		if ((outputLimited) &&
4348c2ecf20Sopenharmony_ci			/* Check output buffer overflow */
4358c2ecf20Sopenharmony_ci			((op - (BYTE *)dest) + lastRun + 1 +
4368c2ecf20Sopenharmony_ci			((lastRun + 255 - RUN_MASK) / 255) > (U32)maxOutputSize))
4378c2ecf20Sopenharmony_ci			return 0;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci		if (lastRun >= RUN_MASK) {
4408c2ecf20Sopenharmony_ci			size_t accumulator = lastRun - RUN_MASK;
4418c2ecf20Sopenharmony_ci			*op++ = RUN_MASK << ML_BITS;
4428c2ecf20Sopenharmony_ci			for (; accumulator >= 255; accumulator -= 255)
4438c2ecf20Sopenharmony_ci				*op++ = 255;
4448c2ecf20Sopenharmony_ci			*op++ = (BYTE) accumulator;
4458c2ecf20Sopenharmony_ci		} else {
4468c2ecf20Sopenharmony_ci			*op++ = (BYTE)(lastRun << ML_BITS);
4478c2ecf20Sopenharmony_ci		}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci		LZ4_memcpy(op, anchor, lastRun);
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci		op += lastRun;
4528c2ecf20Sopenharmony_ci	}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	/* End */
4558c2ecf20Sopenharmony_ci	return (int) (((char *)op) - dest);
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_cistatic int LZ4_compress_fast_extState(
4598c2ecf20Sopenharmony_ci	void *state,
4608c2ecf20Sopenharmony_ci	const char *source,
4618c2ecf20Sopenharmony_ci	char *dest,
4628c2ecf20Sopenharmony_ci	int inputSize,
4638c2ecf20Sopenharmony_ci	int maxOutputSize,
4648c2ecf20Sopenharmony_ci	int acceleration)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	LZ4_stream_t_internal *ctx = &((LZ4_stream_t *)state)->internal_donotuse;
4678c2ecf20Sopenharmony_ci#if LZ4_ARCH64
4688c2ecf20Sopenharmony_ci	const tableType_t tableType = byU32;
4698c2ecf20Sopenharmony_ci#else
4708c2ecf20Sopenharmony_ci	const tableType_t tableType = byPtr;
4718c2ecf20Sopenharmony_ci#endif
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	LZ4_resetStream((LZ4_stream_t *)state);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	if (acceleration < 1)
4768c2ecf20Sopenharmony_ci		acceleration = LZ4_ACCELERATION_DEFAULT;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	if (maxOutputSize >= LZ4_COMPRESSBOUND(inputSize)) {
4798c2ecf20Sopenharmony_ci		if (inputSize < LZ4_64Klimit)
4808c2ecf20Sopenharmony_ci			return LZ4_compress_generic(ctx, source,
4818c2ecf20Sopenharmony_ci				dest, inputSize, 0,
4828c2ecf20Sopenharmony_ci				noLimit, byU16, noDict,
4838c2ecf20Sopenharmony_ci				noDictIssue, acceleration);
4848c2ecf20Sopenharmony_ci		else
4858c2ecf20Sopenharmony_ci			return LZ4_compress_generic(ctx, source,
4868c2ecf20Sopenharmony_ci				dest, inputSize, 0,
4878c2ecf20Sopenharmony_ci				noLimit, tableType, noDict,
4888c2ecf20Sopenharmony_ci				noDictIssue, acceleration);
4898c2ecf20Sopenharmony_ci	} else {
4908c2ecf20Sopenharmony_ci		if (inputSize < LZ4_64Klimit)
4918c2ecf20Sopenharmony_ci			return LZ4_compress_generic(ctx, source,
4928c2ecf20Sopenharmony_ci				dest, inputSize,
4938c2ecf20Sopenharmony_ci				maxOutputSize, limitedOutput, byU16, noDict,
4948c2ecf20Sopenharmony_ci				noDictIssue, acceleration);
4958c2ecf20Sopenharmony_ci		else
4968c2ecf20Sopenharmony_ci			return LZ4_compress_generic(ctx, source,
4978c2ecf20Sopenharmony_ci				dest, inputSize,
4988c2ecf20Sopenharmony_ci				maxOutputSize, limitedOutput, tableType, noDict,
4998c2ecf20Sopenharmony_ci				noDictIssue, acceleration);
5008c2ecf20Sopenharmony_ci	}
5018c2ecf20Sopenharmony_ci}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ciint LZ4_compress_fast(const char *source, char *dest, int inputSize,
5048c2ecf20Sopenharmony_ci	int maxOutputSize, int acceleration, void *wrkmem)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	return LZ4_compress_fast_extState(wrkmem, source, dest, inputSize,
5078c2ecf20Sopenharmony_ci		maxOutputSize, acceleration);
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_compress_fast);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ciint LZ4_compress_default(const char *source, char *dest, int inputSize,
5128c2ecf20Sopenharmony_ci	int maxOutputSize, void *wrkmem)
5138c2ecf20Sopenharmony_ci{
5148c2ecf20Sopenharmony_ci	return LZ4_compress_fast(source, dest, inputSize,
5158c2ecf20Sopenharmony_ci		maxOutputSize, LZ4_ACCELERATION_DEFAULT, wrkmem);
5168c2ecf20Sopenharmony_ci}
5178c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_compress_default);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci/*-******************************
5208c2ecf20Sopenharmony_ci *	*_destSize() variant
5218c2ecf20Sopenharmony_ci ********************************/
5228c2ecf20Sopenharmony_cistatic int LZ4_compress_destSize_generic(
5238c2ecf20Sopenharmony_ci	LZ4_stream_t_internal * const ctx,
5248c2ecf20Sopenharmony_ci	const char * const src,
5258c2ecf20Sopenharmony_ci	char * const dst,
5268c2ecf20Sopenharmony_ci	int * const srcSizePtr,
5278c2ecf20Sopenharmony_ci	const int targetDstSize,
5288c2ecf20Sopenharmony_ci	const tableType_t tableType)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	const BYTE *ip = (const BYTE *) src;
5318c2ecf20Sopenharmony_ci	const BYTE *base = (const BYTE *) src;
5328c2ecf20Sopenharmony_ci	const BYTE *lowLimit = (const BYTE *) src;
5338c2ecf20Sopenharmony_ci	const BYTE *anchor = ip;
5348c2ecf20Sopenharmony_ci	const BYTE * const iend = ip + *srcSizePtr;
5358c2ecf20Sopenharmony_ci	const BYTE * const mflimit = iend - MFLIMIT;
5368c2ecf20Sopenharmony_ci	const BYTE * const matchlimit = iend - LASTLITERALS;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	BYTE *op = (BYTE *) dst;
5398c2ecf20Sopenharmony_ci	BYTE * const oend = op + targetDstSize;
5408c2ecf20Sopenharmony_ci	BYTE * const oMaxLit = op + targetDstSize - 2 /* offset */
5418c2ecf20Sopenharmony_ci		- 8 /* because 8 + MINMATCH == MFLIMIT */ - 1 /* token */;
5428c2ecf20Sopenharmony_ci	BYTE * const oMaxMatch = op + targetDstSize
5438c2ecf20Sopenharmony_ci		- (LASTLITERALS + 1 /* token */);
5448c2ecf20Sopenharmony_ci	BYTE * const oMaxSeq = oMaxLit - 1 /* token */;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	U32 forwardH;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	/* Init conditions */
5498c2ecf20Sopenharmony_ci	/* Impossible to store anything */
5508c2ecf20Sopenharmony_ci	if (targetDstSize < 1)
5518c2ecf20Sopenharmony_ci		return 0;
5528c2ecf20Sopenharmony_ci	/* Unsupported input size, too large (or negative) */
5538c2ecf20Sopenharmony_ci	if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE)
5548c2ecf20Sopenharmony_ci		return 0;
5558c2ecf20Sopenharmony_ci	/* Size too large (not within 64K limit) */
5568c2ecf20Sopenharmony_ci	if ((tableType == byU16) && (*srcSizePtr >= LZ4_64Klimit))
5578c2ecf20Sopenharmony_ci		return 0;
5588c2ecf20Sopenharmony_ci	/* Input too small, no compression (all literals) */
5598c2ecf20Sopenharmony_ci	if (*srcSizePtr < LZ4_minLength)
5608c2ecf20Sopenharmony_ci		goto _last_literals;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	/* First Byte */
5638c2ecf20Sopenharmony_ci	*srcSizePtr = 0;
5648c2ecf20Sopenharmony_ci	LZ4_putPosition(ip, ctx->hashTable, tableType, base);
5658c2ecf20Sopenharmony_ci	ip++; forwardH = LZ4_hashPosition(ip, tableType);
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	/* Main Loop */
5688c2ecf20Sopenharmony_ci	for ( ; ; ) {
5698c2ecf20Sopenharmony_ci		const BYTE *match;
5708c2ecf20Sopenharmony_ci		BYTE *token;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci		/* Find a match */
5738c2ecf20Sopenharmony_ci		{
5748c2ecf20Sopenharmony_ci			const BYTE *forwardIp = ip;
5758c2ecf20Sopenharmony_ci			unsigned int step = 1;
5768c2ecf20Sopenharmony_ci			unsigned int searchMatchNb = 1 << LZ4_SKIPTRIGGER;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci			do {
5798c2ecf20Sopenharmony_ci				U32 h = forwardH;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci				ip = forwardIp;
5828c2ecf20Sopenharmony_ci				forwardIp += step;
5838c2ecf20Sopenharmony_ci				step = (searchMatchNb++ >> LZ4_SKIPTRIGGER);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci				if (unlikely(forwardIp > mflimit))
5868c2ecf20Sopenharmony_ci					goto _last_literals;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci				match = LZ4_getPositionOnHash(h, ctx->hashTable,
5898c2ecf20Sopenharmony_ci					tableType, base);
5908c2ecf20Sopenharmony_ci				forwardH = LZ4_hashPosition(forwardIp,
5918c2ecf20Sopenharmony_ci					tableType);
5928c2ecf20Sopenharmony_ci				LZ4_putPositionOnHash(ip, h,
5938c2ecf20Sopenharmony_ci					ctx->hashTable, tableType,
5948c2ecf20Sopenharmony_ci					base);
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci			} while (((tableType == byU16)
5978c2ecf20Sopenharmony_ci				? 0
5988c2ecf20Sopenharmony_ci				: (match + MAX_DISTANCE < ip))
5998c2ecf20Sopenharmony_ci				|| (LZ4_read32(match) != LZ4_read32(ip)));
6008c2ecf20Sopenharmony_ci		}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci		/* Catch up */
6038c2ecf20Sopenharmony_ci		while ((ip > anchor)
6048c2ecf20Sopenharmony_ci			&& (match > lowLimit)
6058c2ecf20Sopenharmony_ci			&& (unlikely(ip[-1] == match[-1]))) {
6068c2ecf20Sopenharmony_ci			ip--;
6078c2ecf20Sopenharmony_ci			match--;
6088c2ecf20Sopenharmony_ci		}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci		/* Encode Literal length */
6118c2ecf20Sopenharmony_ci		{
6128c2ecf20Sopenharmony_ci			unsigned int litLength = (unsigned int)(ip - anchor);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci			token = op++;
6158c2ecf20Sopenharmony_ci			if (op + ((litLength + 240) / 255)
6168c2ecf20Sopenharmony_ci				+ litLength > oMaxLit) {
6178c2ecf20Sopenharmony_ci				/* Not enough space for a last match */
6188c2ecf20Sopenharmony_ci				op--;
6198c2ecf20Sopenharmony_ci				goto _last_literals;
6208c2ecf20Sopenharmony_ci			}
6218c2ecf20Sopenharmony_ci			if (litLength >= RUN_MASK) {
6228c2ecf20Sopenharmony_ci				unsigned int len = litLength - RUN_MASK;
6238c2ecf20Sopenharmony_ci				*token = (RUN_MASK<<ML_BITS);
6248c2ecf20Sopenharmony_ci				for (; len >= 255; len -= 255)
6258c2ecf20Sopenharmony_ci					*op++ = 255;
6268c2ecf20Sopenharmony_ci				*op++ = (BYTE)len;
6278c2ecf20Sopenharmony_ci			} else
6288c2ecf20Sopenharmony_ci				*token = (BYTE)(litLength << ML_BITS);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci			/* Copy Literals */
6318c2ecf20Sopenharmony_ci			LZ4_wildCopy(op, anchor, op + litLength);
6328c2ecf20Sopenharmony_ci			op += litLength;
6338c2ecf20Sopenharmony_ci		}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci_next_match:
6368c2ecf20Sopenharmony_ci		/* Encode Offset */
6378c2ecf20Sopenharmony_ci		LZ4_writeLE16(op, (U16)(ip - match)); op += 2;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci		/* Encode MatchLength */
6408c2ecf20Sopenharmony_ci		{
6418c2ecf20Sopenharmony_ci			size_t matchLength = LZ4_count(ip + MINMATCH,
6428c2ecf20Sopenharmony_ci			match + MINMATCH, matchlimit);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci			if (op + ((matchLength + 240)/255) > oMaxMatch) {
6458c2ecf20Sopenharmony_ci				/* Match description too long : reduce it */
6468c2ecf20Sopenharmony_ci				matchLength = (15 - 1) + (oMaxMatch - op) * 255;
6478c2ecf20Sopenharmony_ci			}
6488c2ecf20Sopenharmony_ci			ip += MINMATCH + matchLength;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci			if (matchLength >= ML_MASK) {
6518c2ecf20Sopenharmony_ci				*token += ML_MASK;
6528c2ecf20Sopenharmony_ci				matchLength -= ML_MASK;
6538c2ecf20Sopenharmony_ci				while (matchLength >= 255) {
6548c2ecf20Sopenharmony_ci					matchLength -= 255;
6558c2ecf20Sopenharmony_ci					*op++ = 255;
6568c2ecf20Sopenharmony_ci				}
6578c2ecf20Sopenharmony_ci				*op++ = (BYTE)matchLength;
6588c2ecf20Sopenharmony_ci			} else
6598c2ecf20Sopenharmony_ci				*token += (BYTE)(matchLength);
6608c2ecf20Sopenharmony_ci		}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci		anchor = ip;
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci		/* Test end of block */
6658c2ecf20Sopenharmony_ci		if (ip > mflimit)
6668c2ecf20Sopenharmony_ci			break;
6678c2ecf20Sopenharmony_ci		if (op > oMaxSeq)
6688c2ecf20Sopenharmony_ci			break;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci		/* Fill table */
6718c2ecf20Sopenharmony_ci		LZ4_putPosition(ip - 2, ctx->hashTable, tableType, base);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci		/* Test next position */
6748c2ecf20Sopenharmony_ci		match = LZ4_getPosition(ip, ctx->hashTable, tableType, base);
6758c2ecf20Sopenharmony_ci		LZ4_putPosition(ip, ctx->hashTable, tableType, base);
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci		if ((match + MAX_DISTANCE >= ip)
6788c2ecf20Sopenharmony_ci			&& (LZ4_read32(match) == LZ4_read32(ip))) {
6798c2ecf20Sopenharmony_ci			token = op++; *token = 0;
6808c2ecf20Sopenharmony_ci			goto _next_match;
6818c2ecf20Sopenharmony_ci		}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci		/* Prepare next loop */
6848c2ecf20Sopenharmony_ci		forwardH = LZ4_hashPosition(++ip, tableType);
6858c2ecf20Sopenharmony_ci	}
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci_last_literals:
6888c2ecf20Sopenharmony_ci	/* Encode Last Literals */
6898c2ecf20Sopenharmony_ci	{
6908c2ecf20Sopenharmony_ci		size_t lastRunSize = (size_t)(iend - anchor);
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci		if (op + 1 /* token */
6938c2ecf20Sopenharmony_ci			+ ((lastRunSize + 240) / 255) /* litLength */
6948c2ecf20Sopenharmony_ci			+ lastRunSize /* literals */ > oend) {
6958c2ecf20Sopenharmony_ci			/* adapt lastRunSize to fill 'dst' */
6968c2ecf20Sopenharmony_ci			lastRunSize	= (oend - op) - 1;
6978c2ecf20Sopenharmony_ci			lastRunSize -= (lastRunSize + 240) / 255;
6988c2ecf20Sopenharmony_ci		}
6998c2ecf20Sopenharmony_ci		ip = anchor + lastRunSize;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci		if (lastRunSize >= RUN_MASK) {
7028c2ecf20Sopenharmony_ci			size_t accumulator = lastRunSize - RUN_MASK;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci			*op++ = RUN_MASK << ML_BITS;
7058c2ecf20Sopenharmony_ci			for (; accumulator >= 255; accumulator -= 255)
7068c2ecf20Sopenharmony_ci				*op++ = 255;
7078c2ecf20Sopenharmony_ci			*op++ = (BYTE) accumulator;
7088c2ecf20Sopenharmony_ci		} else {
7098c2ecf20Sopenharmony_ci			*op++ = (BYTE)(lastRunSize<<ML_BITS);
7108c2ecf20Sopenharmony_ci		}
7118c2ecf20Sopenharmony_ci		LZ4_memcpy(op, anchor, lastRunSize);
7128c2ecf20Sopenharmony_ci		op += lastRunSize;
7138c2ecf20Sopenharmony_ci	}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	/* End */
7168c2ecf20Sopenharmony_ci	*srcSizePtr = (int) (((const char *)ip) - src);
7178c2ecf20Sopenharmony_ci	return (int) (((char *)op) - dst);
7188c2ecf20Sopenharmony_ci}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_cistatic int LZ4_compress_destSize_extState(
7218c2ecf20Sopenharmony_ci	LZ4_stream_t *state,
7228c2ecf20Sopenharmony_ci	const char *src,
7238c2ecf20Sopenharmony_ci	char *dst,
7248c2ecf20Sopenharmony_ci	int *srcSizePtr,
7258c2ecf20Sopenharmony_ci	int targetDstSize)
7268c2ecf20Sopenharmony_ci{
7278c2ecf20Sopenharmony_ci#if LZ4_ARCH64
7288c2ecf20Sopenharmony_ci	const tableType_t tableType = byU32;
7298c2ecf20Sopenharmony_ci#else
7308c2ecf20Sopenharmony_ci	const tableType_t tableType = byPtr;
7318c2ecf20Sopenharmony_ci#endif
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	LZ4_resetStream(state);
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	if (targetDstSize >= LZ4_COMPRESSBOUND(*srcSizePtr)) {
7368c2ecf20Sopenharmony_ci		/* compression success is guaranteed */
7378c2ecf20Sopenharmony_ci		return LZ4_compress_fast_extState(
7388c2ecf20Sopenharmony_ci			state, src, dst, *srcSizePtr,
7398c2ecf20Sopenharmony_ci			targetDstSize, 1);
7408c2ecf20Sopenharmony_ci	} else {
7418c2ecf20Sopenharmony_ci		if (*srcSizePtr < LZ4_64Klimit)
7428c2ecf20Sopenharmony_ci			return LZ4_compress_destSize_generic(
7438c2ecf20Sopenharmony_ci				&state->internal_donotuse,
7448c2ecf20Sopenharmony_ci				src, dst, srcSizePtr,
7458c2ecf20Sopenharmony_ci				targetDstSize, byU16);
7468c2ecf20Sopenharmony_ci		else
7478c2ecf20Sopenharmony_ci			return LZ4_compress_destSize_generic(
7488c2ecf20Sopenharmony_ci				&state->internal_donotuse,
7498c2ecf20Sopenharmony_ci				src, dst, srcSizePtr,
7508c2ecf20Sopenharmony_ci				targetDstSize, tableType);
7518c2ecf20Sopenharmony_ci	}
7528c2ecf20Sopenharmony_ci}
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ciint LZ4_compress_destSize(
7568c2ecf20Sopenharmony_ci	const char *src,
7578c2ecf20Sopenharmony_ci	char *dst,
7588c2ecf20Sopenharmony_ci	int *srcSizePtr,
7598c2ecf20Sopenharmony_ci	int targetDstSize,
7608c2ecf20Sopenharmony_ci	void *wrkmem)
7618c2ecf20Sopenharmony_ci{
7628c2ecf20Sopenharmony_ci	return LZ4_compress_destSize_extState(wrkmem, src, dst, srcSizePtr,
7638c2ecf20Sopenharmony_ci		targetDstSize);
7648c2ecf20Sopenharmony_ci}
7658c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_compress_destSize);
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci/*-******************************
7688c2ecf20Sopenharmony_ci *	Streaming functions
7698c2ecf20Sopenharmony_ci ********************************/
7708c2ecf20Sopenharmony_civoid LZ4_resetStream(LZ4_stream_t *LZ4_stream)
7718c2ecf20Sopenharmony_ci{
7728c2ecf20Sopenharmony_ci	memset(LZ4_stream, 0, sizeof(LZ4_stream_t));
7738c2ecf20Sopenharmony_ci}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ciint LZ4_loadDict(LZ4_stream_t *LZ4_dict,
7768c2ecf20Sopenharmony_ci	const char *dictionary, int dictSize)
7778c2ecf20Sopenharmony_ci{
7788c2ecf20Sopenharmony_ci	LZ4_stream_t_internal *dict = &LZ4_dict->internal_donotuse;
7798c2ecf20Sopenharmony_ci	const BYTE *p = (const BYTE *)dictionary;
7808c2ecf20Sopenharmony_ci	const BYTE * const dictEnd = p + dictSize;
7818c2ecf20Sopenharmony_ci	const BYTE *base;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	if ((dict->initCheck)
7848c2ecf20Sopenharmony_ci		|| (dict->currentOffset > 1 * GB)) {
7858c2ecf20Sopenharmony_ci		/* Uninitialized structure, or reuse overflow */
7868c2ecf20Sopenharmony_ci		LZ4_resetStream(LZ4_dict);
7878c2ecf20Sopenharmony_ci	}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	if (dictSize < (int)HASH_UNIT) {
7908c2ecf20Sopenharmony_ci		dict->dictionary = NULL;
7918c2ecf20Sopenharmony_ci		dict->dictSize = 0;
7928c2ecf20Sopenharmony_ci		return 0;
7938c2ecf20Sopenharmony_ci	}
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	if ((dictEnd - p) > 64 * KB)
7968c2ecf20Sopenharmony_ci		p = dictEnd - 64 * KB;
7978c2ecf20Sopenharmony_ci	dict->currentOffset += 64 * KB;
7988c2ecf20Sopenharmony_ci	base = p - dict->currentOffset;
7998c2ecf20Sopenharmony_ci	dict->dictionary = p;
8008c2ecf20Sopenharmony_ci	dict->dictSize = (U32)(dictEnd - p);
8018c2ecf20Sopenharmony_ci	dict->currentOffset += dict->dictSize;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	while (p <= dictEnd - HASH_UNIT) {
8048c2ecf20Sopenharmony_ci		LZ4_putPosition(p, dict->hashTable, byU32, base);
8058c2ecf20Sopenharmony_ci		p += 3;
8068c2ecf20Sopenharmony_ci	}
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	return dict->dictSize;
8098c2ecf20Sopenharmony_ci}
8108c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_loadDict);
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_cistatic void LZ4_renormDictT(LZ4_stream_t_internal *LZ4_dict,
8138c2ecf20Sopenharmony_ci	const BYTE *src)
8148c2ecf20Sopenharmony_ci{
8158c2ecf20Sopenharmony_ci	if ((LZ4_dict->currentOffset > 0x80000000) ||
8168c2ecf20Sopenharmony_ci		((uptrval)LZ4_dict->currentOffset > (uptrval)src)) {
8178c2ecf20Sopenharmony_ci		/* address space overflow */
8188c2ecf20Sopenharmony_ci		/* rescale hash table */
8198c2ecf20Sopenharmony_ci		U32 const delta = LZ4_dict->currentOffset - 64 * KB;
8208c2ecf20Sopenharmony_ci		const BYTE *dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize;
8218c2ecf20Sopenharmony_ci		int i;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci		for (i = 0; i < LZ4_HASH_SIZE_U32; i++) {
8248c2ecf20Sopenharmony_ci			if (LZ4_dict->hashTable[i] < delta)
8258c2ecf20Sopenharmony_ci				LZ4_dict->hashTable[i] = 0;
8268c2ecf20Sopenharmony_ci			else
8278c2ecf20Sopenharmony_ci				LZ4_dict->hashTable[i] -= delta;
8288c2ecf20Sopenharmony_ci		}
8298c2ecf20Sopenharmony_ci		LZ4_dict->currentOffset = 64 * KB;
8308c2ecf20Sopenharmony_ci		if (LZ4_dict->dictSize > 64 * KB)
8318c2ecf20Sopenharmony_ci			LZ4_dict->dictSize = 64 * KB;
8328c2ecf20Sopenharmony_ci		LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize;
8338c2ecf20Sopenharmony_ci	}
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ciint LZ4_saveDict(LZ4_stream_t *LZ4_dict, char *safeBuffer, int dictSize)
8378c2ecf20Sopenharmony_ci{
8388c2ecf20Sopenharmony_ci	LZ4_stream_t_internal * const dict = &LZ4_dict->internal_donotuse;
8398c2ecf20Sopenharmony_ci	const BYTE * const previousDictEnd = dict->dictionary + dict->dictSize;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	if ((U32)dictSize > 64 * KB) {
8428c2ecf20Sopenharmony_ci		/* useless to define a dictionary > 64 * KB */
8438c2ecf20Sopenharmony_ci		dictSize = 64 * KB;
8448c2ecf20Sopenharmony_ci	}
8458c2ecf20Sopenharmony_ci	if ((U32)dictSize > dict->dictSize)
8468c2ecf20Sopenharmony_ci		dictSize = dict->dictSize;
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	memmove(safeBuffer, previousDictEnd - dictSize, dictSize);
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	dict->dictionary = (const BYTE *)safeBuffer;
8518c2ecf20Sopenharmony_ci	dict->dictSize = (U32)dictSize;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	return dictSize;
8548c2ecf20Sopenharmony_ci}
8558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_saveDict);
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ciint LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream, const char *source,
8588c2ecf20Sopenharmony_ci	char *dest, int inputSize, int maxOutputSize, int acceleration)
8598c2ecf20Sopenharmony_ci{
8608c2ecf20Sopenharmony_ci	LZ4_stream_t_internal *streamPtr = &LZ4_stream->internal_donotuse;
8618c2ecf20Sopenharmony_ci	const BYTE * const dictEnd = streamPtr->dictionary
8628c2ecf20Sopenharmony_ci		+ streamPtr->dictSize;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	const BYTE *smallest = (const BYTE *) source;
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	if (streamPtr->initCheck) {
8678c2ecf20Sopenharmony_ci		/* Uninitialized structure detected */
8688c2ecf20Sopenharmony_ci		return 0;
8698c2ecf20Sopenharmony_ci	}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	if ((streamPtr->dictSize > 0) && (smallest > dictEnd))
8728c2ecf20Sopenharmony_ci		smallest = dictEnd;
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	LZ4_renormDictT(streamPtr, smallest);
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	if (acceleration < 1)
8778c2ecf20Sopenharmony_ci		acceleration = LZ4_ACCELERATION_DEFAULT;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	/* Check overlapping input/dictionary space */
8808c2ecf20Sopenharmony_ci	{
8818c2ecf20Sopenharmony_ci		const BYTE *sourceEnd = (const BYTE *) source + inputSize;
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci		if ((sourceEnd > streamPtr->dictionary)
8848c2ecf20Sopenharmony_ci			&& (sourceEnd < dictEnd)) {
8858c2ecf20Sopenharmony_ci			streamPtr->dictSize = (U32)(dictEnd - sourceEnd);
8868c2ecf20Sopenharmony_ci			if (streamPtr->dictSize > 64 * KB)
8878c2ecf20Sopenharmony_ci				streamPtr->dictSize = 64 * KB;
8888c2ecf20Sopenharmony_ci			if (streamPtr->dictSize < 4)
8898c2ecf20Sopenharmony_ci				streamPtr->dictSize = 0;
8908c2ecf20Sopenharmony_ci			streamPtr->dictionary = dictEnd - streamPtr->dictSize;
8918c2ecf20Sopenharmony_ci		}
8928c2ecf20Sopenharmony_ci	}
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	/* prefix mode : source data follows dictionary */
8958c2ecf20Sopenharmony_ci	if (dictEnd == (const BYTE *)source) {
8968c2ecf20Sopenharmony_ci		int result;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci		if ((streamPtr->dictSize < 64 * KB) &&
8998c2ecf20Sopenharmony_ci			(streamPtr->dictSize < streamPtr->currentOffset)) {
9008c2ecf20Sopenharmony_ci			result = LZ4_compress_generic(
9018c2ecf20Sopenharmony_ci				streamPtr, source, dest, inputSize,
9028c2ecf20Sopenharmony_ci				maxOutputSize, limitedOutput, byU32,
9038c2ecf20Sopenharmony_ci				withPrefix64k, dictSmall, acceleration);
9048c2ecf20Sopenharmony_ci		} else {
9058c2ecf20Sopenharmony_ci			result = LZ4_compress_generic(
9068c2ecf20Sopenharmony_ci				streamPtr, source, dest, inputSize,
9078c2ecf20Sopenharmony_ci				maxOutputSize, limitedOutput, byU32,
9088c2ecf20Sopenharmony_ci				withPrefix64k, noDictIssue, acceleration);
9098c2ecf20Sopenharmony_ci		}
9108c2ecf20Sopenharmony_ci		streamPtr->dictSize += (U32)inputSize;
9118c2ecf20Sopenharmony_ci		streamPtr->currentOffset += (U32)inputSize;
9128c2ecf20Sopenharmony_ci		return result;
9138c2ecf20Sopenharmony_ci	}
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	/* external dictionary mode */
9168c2ecf20Sopenharmony_ci	{
9178c2ecf20Sopenharmony_ci		int result;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci		if ((streamPtr->dictSize < 64 * KB) &&
9208c2ecf20Sopenharmony_ci			(streamPtr->dictSize < streamPtr->currentOffset)) {
9218c2ecf20Sopenharmony_ci			result = LZ4_compress_generic(
9228c2ecf20Sopenharmony_ci				streamPtr, source, dest, inputSize,
9238c2ecf20Sopenharmony_ci				maxOutputSize, limitedOutput, byU32,
9248c2ecf20Sopenharmony_ci				usingExtDict, dictSmall, acceleration);
9258c2ecf20Sopenharmony_ci		} else {
9268c2ecf20Sopenharmony_ci			result = LZ4_compress_generic(
9278c2ecf20Sopenharmony_ci				streamPtr, source, dest, inputSize,
9288c2ecf20Sopenharmony_ci				maxOutputSize, limitedOutput, byU32,
9298c2ecf20Sopenharmony_ci				usingExtDict, noDictIssue, acceleration);
9308c2ecf20Sopenharmony_ci		}
9318c2ecf20Sopenharmony_ci		streamPtr->dictionary = (const BYTE *)source;
9328c2ecf20Sopenharmony_ci		streamPtr->dictSize = (U32)inputSize;
9338c2ecf20Sopenharmony_ci		streamPtr->currentOffset += (U32)inputSize;
9348c2ecf20Sopenharmony_ci		return result;
9358c2ecf20Sopenharmony_ci	}
9368c2ecf20Sopenharmony_ci}
9378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(LZ4_compress_fast_continue);
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
9408c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("LZ4 compressor");
941