xref: /OK3568_Linux_fs/kernel/lib/zstd/entropy_common.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Common functions of New Generation Entropy library
3*4882a593Smuzhiyun  * Copyright (C) 2016, Yann Collet.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
8*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions are
9*4882a593Smuzhiyun  * met:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *   * Redistributions of source code must retain the above copyright
12*4882a593Smuzhiyun  * notice, this list of conditions and the following disclaimer.
13*4882a593Smuzhiyun  *   * Redistributions in binary form must reproduce the above
14*4882a593Smuzhiyun  * copyright notice, this list of conditions and the following disclaimer
15*4882a593Smuzhiyun  * in the documentation and/or other materials provided with the
16*4882a593Smuzhiyun  * distribution.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify it under
31*4882a593Smuzhiyun  * the terms of the GNU General Public License version 2 as published by the
32*4882a593Smuzhiyun  * Free Software Foundation. This program is dual-licensed; you may select
33*4882a593Smuzhiyun  * either version 2 of the GNU General Public License ("GPL") or BSD license
34*4882a593Smuzhiyun  * ("BSD").
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * You can contact the author at :
37*4882a593Smuzhiyun  * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
38*4882a593Smuzhiyun  */
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /* *************************************
41*4882a593Smuzhiyun *  Dependencies
42*4882a593Smuzhiyun ***************************************/
43*4882a593Smuzhiyun #include "error_private.h" /* ERR_*, ERROR */
44*4882a593Smuzhiyun #include "fse.h"
45*4882a593Smuzhiyun #include "huf.h"
46*4882a593Smuzhiyun #include "mem.h"
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /*===   Version   ===*/
FSE_versionNumber(void)49*4882a593Smuzhiyun unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; }
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /*===   Error Management   ===*/
FSE_isError(size_t code)52*4882a593Smuzhiyun unsigned FSE_isError(size_t code) { return ERR_isError(code); }
53*4882a593Smuzhiyun 
HUF_isError(size_t code)54*4882a593Smuzhiyun unsigned HUF_isError(size_t code) { return ERR_isError(code); }
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /*-**************************************************************
57*4882a593Smuzhiyun *  FSE NCount encoding-decoding
58*4882a593Smuzhiyun ****************************************************************/
FSE_readNCount(short * normalizedCounter,unsigned * maxSVPtr,unsigned * tableLogPtr,const void * headerBuffer,size_t hbSize)59*4882a593Smuzhiyun size_t FSE_readNCount(short *normalizedCounter, unsigned *maxSVPtr, unsigned *tableLogPtr, const void *headerBuffer, size_t hbSize)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	const BYTE *const istart = (const BYTE *)headerBuffer;
62*4882a593Smuzhiyun 	const BYTE *const iend = istart + hbSize;
63*4882a593Smuzhiyun 	const BYTE *ip = istart;
64*4882a593Smuzhiyun 	int nbBits;
65*4882a593Smuzhiyun 	int remaining;
66*4882a593Smuzhiyun 	int threshold;
67*4882a593Smuzhiyun 	U32 bitStream;
68*4882a593Smuzhiyun 	int bitCount;
69*4882a593Smuzhiyun 	unsigned charnum = 0;
70*4882a593Smuzhiyun 	int previous0 = 0;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	if (hbSize < 4)
73*4882a593Smuzhiyun 		return ERROR(srcSize_wrong);
74*4882a593Smuzhiyun 	bitStream = ZSTD_readLE32(ip);
75*4882a593Smuzhiyun 	nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
76*4882a593Smuzhiyun 	if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX)
77*4882a593Smuzhiyun 		return ERROR(tableLog_tooLarge);
78*4882a593Smuzhiyun 	bitStream >>= 4;
79*4882a593Smuzhiyun 	bitCount = 4;
80*4882a593Smuzhiyun 	*tableLogPtr = nbBits;
81*4882a593Smuzhiyun 	remaining = (1 << nbBits) + 1;
82*4882a593Smuzhiyun 	threshold = 1 << nbBits;
83*4882a593Smuzhiyun 	nbBits++;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	while ((remaining > 1) & (charnum <= *maxSVPtr)) {
86*4882a593Smuzhiyun 		if (previous0) {
87*4882a593Smuzhiyun 			unsigned n0 = charnum;
88*4882a593Smuzhiyun 			while ((bitStream & 0xFFFF) == 0xFFFF) {
89*4882a593Smuzhiyun 				n0 += 24;
90*4882a593Smuzhiyun 				if (ip < iend - 5) {
91*4882a593Smuzhiyun 					ip += 2;
92*4882a593Smuzhiyun 					bitStream = ZSTD_readLE32(ip) >> bitCount;
93*4882a593Smuzhiyun 				} else {
94*4882a593Smuzhiyun 					bitStream >>= 16;
95*4882a593Smuzhiyun 					bitCount += 16;
96*4882a593Smuzhiyun 				}
97*4882a593Smuzhiyun 			}
98*4882a593Smuzhiyun 			while ((bitStream & 3) == 3) {
99*4882a593Smuzhiyun 				n0 += 3;
100*4882a593Smuzhiyun 				bitStream >>= 2;
101*4882a593Smuzhiyun 				bitCount += 2;
102*4882a593Smuzhiyun 			}
103*4882a593Smuzhiyun 			n0 += bitStream & 3;
104*4882a593Smuzhiyun 			bitCount += 2;
105*4882a593Smuzhiyun 			if (n0 > *maxSVPtr)
106*4882a593Smuzhiyun 				return ERROR(maxSymbolValue_tooSmall);
107*4882a593Smuzhiyun 			while (charnum < n0)
108*4882a593Smuzhiyun 				normalizedCounter[charnum++] = 0;
109*4882a593Smuzhiyun 			if ((ip <= iend - 7) || (ip + (bitCount >> 3) <= iend - 4)) {
110*4882a593Smuzhiyun 				ip += bitCount >> 3;
111*4882a593Smuzhiyun 				bitCount &= 7;
112*4882a593Smuzhiyun 				bitStream = ZSTD_readLE32(ip) >> bitCount;
113*4882a593Smuzhiyun 			} else {
114*4882a593Smuzhiyun 				bitStream >>= 2;
115*4882a593Smuzhiyun 			}
116*4882a593Smuzhiyun 		}
117*4882a593Smuzhiyun 		{
118*4882a593Smuzhiyun 			int const max = (2 * threshold - 1) - remaining;
119*4882a593Smuzhiyun 			int count;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 			if ((bitStream & (threshold - 1)) < (U32)max) {
122*4882a593Smuzhiyun 				count = bitStream & (threshold - 1);
123*4882a593Smuzhiyun 				bitCount += nbBits - 1;
124*4882a593Smuzhiyun 			} else {
125*4882a593Smuzhiyun 				count = bitStream & (2 * threshold - 1);
126*4882a593Smuzhiyun 				if (count >= threshold)
127*4882a593Smuzhiyun 					count -= max;
128*4882a593Smuzhiyun 				bitCount += nbBits;
129*4882a593Smuzhiyun 			}
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 			count--;				 /* extra accuracy */
132*4882a593Smuzhiyun 			remaining -= count < 0 ? -count : count; /* -1 means +1 */
133*4882a593Smuzhiyun 			normalizedCounter[charnum++] = (short)count;
134*4882a593Smuzhiyun 			previous0 = !count;
135*4882a593Smuzhiyun 			while (remaining < threshold) {
136*4882a593Smuzhiyun 				nbBits--;
137*4882a593Smuzhiyun 				threshold >>= 1;
138*4882a593Smuzhiyun 			}
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 			if ((ip <= iend - 7) || (ip + (bitCount >> 3) <= iend - 4)) {
141*4882a593Smuzhiyun 				ip += bitCount >> 3;
142*4882a593Smuzhiyun 				bitCount &= 7;
143*4882a593Smuzhiyun 			} else {
144*4882a593Smuzhiyun 				bitCount -= (int)(8 * (iend - 4 - ip));
145*4882a593Smuzhiyun 				ip = iend - 4;
146*4882a593Smuzhiyun 			}
147*4882a593Smuzhiyun 			bitStream = ZSTD_readLE32(ip) >> (bitCount & 31);
148*4882a593Smuzhiyun 		}
149*4882a593Smuzhiyun 	} /* while ((remaining>1) & (charnum<=*maxSVPtr)) */
150*4882a593Smuzhiyun 	if (remaining != 1)
151*4882a593Smuzhiyun 		return ERROR(corruption_detected);
152*4882a593Smuzhiyun 	if (bitCount > 32)
153*4882a593Smuzhiyun 		return ERROR(corruption_detected);
154*4882a593Smuzhiyun 	*maxSVPtr = charnum - 1;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	ip += (bitCount + 7) >> 3;
157*4882a593Smuzhiyun 	return ip - istart;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /*! HUF_readStats() :
161*4882a593Smuzhiyun 	Read compact Huffman tree, saved by HUF_writeCTable().
162*4882a593Smuzhiyun 	`huffWeight` is destination buffer.
163*4882a593Smuzhiyun 	`rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.
164*4882a593Smuzhiyun 	@return : size read from `src` , or an error Code .
165*4882a593Smuzhiyun 	Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
166*4882a593Smuzhiyun */
HUF_readStats_wksp(BYTE * huffWeight,size_t hwSize,U32 * rankStats,U32 * nbSymbolsPtr,U32 * tableLogPtr,const void * src,size_t srcSize,void * workspace,size_t workspaceSize)167*4882a593Smuzhiyun size_t HUF_readStats_wksp(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize, void *workspace, size_t workspaceSize)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	U32 weightTotal;
170*4882a593Smuzhiyun 	const BYTE *ip = (const BYTE *)src;
171*4882a593Smuzhiyun 	size_t iSize;
172*4882a593Smuzhiyun 	size_t oSize;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	if (!srcSize)
175*4882a593Smuzhiyun 		return ERROR(srcSize_wrong);
176*4882a593Smuzhiyun 	iSize = ip[0];
177*4882a593Smuzhiyun 	/* memset(huffWeight, 0, hwSize);   */ /* is not necessary, even though some analyzer complain ... */
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	if (iSize >= 128) { /* special header */
180*4882a593Smuzhiyun 		oSize = iSize - 127;
181*4882a593Smuzhiyun 		iSize = ((oSize + 1) / 2);
182*4882a593Smuzhiyun 		if (iSize + 1 > srcSize)
183*4882a593Smuzhiyun 			return ERROR(srcSize_wrong);
184*4882a593Smuzhiyun 		if (oSize >= hwSize)
185*4882a593Smuzhiyun 			return ERROR(corruption_detected);
186*4882a593Smuzhiyun 		ip += 1;
187*4882a593Smuzhiyun 		{
188*4882a593Smuzhiyun 			U32 n;
189*4882a593Smuzhiyun 			for (n = 0; n < oSize; n += 2) {
190*4882a593Smuzhiyun 				huffWeight[n] = ip[n / 2] >> 4;
191*4882a593Smuzhiyun 				huffWeight[n + 1] = ip[n / 2] & 15;
192*4882a593Smuzhiyun 			}
193*4882a593Smuzhiyun 		}
194*4882a593Smuzhiyun 	} else {						 /* header compressed with FSE (normal case) */
195*4882a593Smuzhiyun 		if (iSize + 1 > srcSize)
196*4882a593Smuzhiyun 			return ERROR(srcSize_wrong);
197*4882a593Smuzhiyun 		oSize = FSE_decompress_wksp(huffWeight, hwSize - 1, ip + 1, iSize, 6, workspace, workspaceSize); /* max (hwSize-1) values decoded, as last one is implied */
198*4882a593Smuzhiyun 		if (FSE_isError(oSize))
199*4882a593Smuzhiyun 			return oSize;
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	/* collect weight stats */
203*4882a593Smuzhiyun 	memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
204*4882a593Smuzhiyun 	weightTotal = 0;
205*4882a593Smuzhiyun 	{
206*4882a593Smuzhiyun 		U32 n;
207*4882a593Smuzhiyun 		for (n = 0; n < oSize; n++) {
208*4882a593Smuzhiyun 			if (huffWeight[n] >= HUF_TABLELOG_MAX)
209*4882a593Smuzhiyun 				return ERROR(corruption_detected);
210*4882a593Smuzhiyun 			rankStats[huffWeight[n]]++;
211*4882a593Smuzhiyun 			weightTotal += (1 << huffWeight[n]) >> 1;
212*4882a593Smuzhiyun 		}
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 	if (weightTotal == 0)
215*4882a593Smuzhiyun 		return ERROR(corruption_detected);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	/* get last non-null symbol weight (implied, total must be 2^n) */
218*4882a593Smuzhiyun 	{
219*4882a593Smuzhiyun 		U32 const tableLog = BIT_highbit32(weightTotal) + 1;
220*4882a593Smuzhiyun 		if (tableLog > HUF_TABLELOG_MAX)
221*4882a593Smuzhiyun 			return ERROR(corruption_detected);
222*4882a593Smuzhiyun 		*tableLogPtr = tableLog;
223*4882a593Smuzhiyun 		/* determine last weight */
224*4882a593Smuzhiyun 		{
225*4882a593Smuzhiyun 			U32 const total = 1 << tableLog;
226*4882a593Smuzhiyun 			U32 const rest = total - weightTotal;
227*4882a593Smuzhiyun 			U32 const verif = 1 << BIT_highbit32(rest);
228*4882a593Smuzhiyun 			U32 const lastWeight = BIT_highbit32(rest) + 1;
229*4882a593Smuzhiyun 			if (verif != rest)
230*4882a593Smuzhiyun 				return ERROR(corruption_detected); /* last value must be a clean power of 2 */
231*4882a593Smuzhiyun 			huffWeight[oSize] = (BYTE)lastWeight;
232*4882a593Smuzhiyun 			rankStats[lastWeight]++;
233*4882a593Smuzhiyun 		}
234*4882a593Smuzhiyun 	}
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	/* check tree construction validity */
237*4882a593Smuzhiyun 	if ((rankStats[1] < 2) || (rankStats[1] & 1))
238*4882a593Smuzhiyun 		return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	/* results */
241*4882a593Smuzhiyun 	*nbSymbolsPtr = (U32)(oSize + 1);
242*4882a593Smuzhiyun 	return iSize + 1;
243*4882a593Smuzhiyun }
244