xref: /OK3568_Linux_fs/kernel/lib/zstd/fse_decompress.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * FSE : Finite State Entropy decoder
3*4882a593Smuzhiyun  * Copyright (C) 2013-2015, 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 *  Compiler specifics
42*4882a593Smuzhiyun ****************************************************************/
43*4882a593Smuzhiyun #define FORCE_INLINE static __always_inline
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /* **************************************************************
46*4882a593Smuzhiyun *  Includes
47*4882a593Smuzhiyun ****************************************************************/
48*4882a593Smuzhiyun #include "bitstream.h"
49*4882a593Smuzhiyun #include "fse.h"
50*4882a593Smuzhiyun #include "zstd_internal.h"
51*4882a593Smuzhiyun #include <linux/compiler.h>
52*4882a593Smuzhiyun #include <linux/kernel.h>
53*4882a593Smuzhiyun #include <linux/string.h> /* memcpy, memset */
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /* **************************************************************
56*4882a593Smuzhiyun *  Error Management
57*4882a593Smuzhiyun ****************************************************************/
58*4882a593Smuzhiyun #define FSE_isError ERR_isError
59*4882a593Smuzhiyun #define FSE_STATIC_ASSERT(c)                                   \
60*4882a593Smuzhiyun 	{                                                      \
61*4882a593Smuzhiyun 		enum { FSE_static_assert = 1 / (int)(!!(c)) }; \
62*4882a593Smuzhiyun 	} /* use only *after* variable declarations */
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /* **************************************************************
65*4882a593Smuzhiyun *  Templates
66*4882a593Smuzhiyun ****************************************************************/
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun   designed to be included
69*4882a593Smuzhiyun   for type-specific functions (template emulation in C)
70*4882a593Smuzhiyun   Objective is to write these functions only once, for improved maintenance
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /* safety checks */
74*4882a593Smuzhiyun #ifndef FSE_FUNCTION_EXTENSION
75*4882a593Smuzhiyun #error "FSE_FUNCTION_EXTENSION must be defined"
76*4882a593Smuzhiyun #endif
77*4882a593Smuzhiyun #ifndef FSE_FUNCTION_TYPE
78*4882a593Smuzhiyun #error "FSE_FUNCTION_TYPE must be defined"
79*4882a593Smuzhiyun #endif
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /* Function names */
82*4882a593Smuzhiyun #define FSE_CAT(X, Y) X##Y
83*4882a593Smuzhiyun #define FSE_FUNCTION_NAME(X, Y) FSE_CAT(X, Y)
84*4882a593Smuzhiyun #define FSE_TYPE_NAME(X, Y) FSE_CAT(X, Y)
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /* Function templates */
87*4882a593Smuzhiyun 
FSE_buildDTable_wksp(FSE_DTable * dt,const short * normalizedCounter,unsigned maxSymbolValue,unsigned tableLog,void * workspace,size_t workspaceSize)88*4882a593Smuzhiyun size_t FSE_buildDTable_wksp(FSE_DTable *dt, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workspace, size_t workspaceSize)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	void *const tdPtr = dt + 1; /* because *dt is unsigned, 32-bits aligned on 32-bits */
91*4882a593Smuzhiyun 	FSE_DECODE_TYPE *const tableDecode = (FSE_DECODE_TYPE *)(tdPtr);
92*4882a593Smuzhiyun 	U16 *symbolNext = (U16 *)workspace;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	U32 const maxSV1 = maxSymbolValue + 1;
95*4882a593Smuzhiyun 	U32 const tableSize = 1 << tableLog;
96*4882a593Smuzhiyun 	U32 highThreshold = tableSize - 1;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	/* Sanity Checks */
99*4882a593Smuzhiyun 	if (workspaceSize < sizeof(U16) * (FSE_MAX_SYMBOL_VALUE + 1))
100*4882a593Smuzhiyun 		return ERROR(tableLog_tooLarge);
101*4882a593Smuzhiyun 	if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE)
102*4882a593Smuzhiyun 		return ERROR(maxSymbolValue_tooLarge);
103*4882a593Smuzhiyun 	if (tableLog > FSE_MAX_TABLELOG)
104*4882a593Smuzhiyun 		return ERROR(tableLog_tooLarge);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	/* Init, lay down lowprob symbols */
107*4882a593Smuzhiyun 	{
108*4882a593Smuzhiyun 		FSE_DTableHeader DTableH;
109*4882a593Smuzhiyun 		DTableH.tableLog = (U16)tableLog;
110*4882a593Smuzhiyun 		DTableH.fastMode = 1;
111*4882a593Smuzhiyun 		{
112*4882a593Smuzhiyun 			S16 const largeLimit = (S16)(1 << (tableLog - 1));
113*4882a593Smuzhiyun 			U32 s;
114*4882a593Smuzhiyun 			for (s = 0; s < maxSV1; s++) {
115*4882a593Smuzhiyun 				if (normalizedCounter[s] == -1) {
116*4882a593Smuzhiyun 					tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
117*4882a593Smuzhiyun 					symbolNext[s] = 1;
118*4882a593Smuzhiyun 				} else {
119*4882a593Smuzhiyun 					if (normalizedCounter[s] >= largeLimit)
120*4882a593Smuzhiyun 						DTableH.fastMode = 0;
121*4882a593Smuzhiyun 					symbolNext[s] = normalizedCounter[s];
122*4882a593Smuzhiyun 				}
123*4882a593Smuzhiyun 			}
124*4882a593Smuzhiyun 		}
125*4882a593Smuzhiyun 		memcpy(dt, &DTableH, sizeof(DTableH));
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	/* Spread symbols */
129*4882a593Smuzhiyun 	{
130*4882a593Smuzhiyun 		U32 const tableMask = tableSize - 1;
131*4882a593Smuzhiyun 		U32 const step = FSE_TABLESTEP(tableSize);
132*4882a593Smuzhiyun 		U32 s, position = 0;
133*4882a593Smuzhiyun 		for (s = 0; s < maxSV1; s++) {
134*4882a593Smuzhiyun 			int i;
135*4882a593Smuzhiyun 			for (i = 0; i < normalizedCounter[s]; i++) {
136*4882a593Smuzhiyun 				tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
137*4882a593Smuzhiyun 				position = (position + step) & tableMask;
138*4882a593Smuzhiyun 				while (position > highThreshold)
139*4882a593Smuzhiyun 					position = (position + step) & tableMask; /* lowprob area */
140*4882a593Smuzhiyun 			}
141*4882a593Smuzhiyun 		}
142*4882a593Smuzhiyun 		if (position != 0)
143*4882a593Smuzhiyun 			return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
144*4882a593Smuzhiyun 	}
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	/* Build Decoding table */
147*4882a593Smuzhiyun 	{
148*4882a593Smuzhiyun 		U32 u;
149*4882a593Smuzhiyun 		for (u = 0; u < tableSize; u++) {
150*4882a593Smuzhiyun 			FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
151*4882a593Smuzhiyun 			U16 nextState = symbolNext[symbol]++;
152*4882a593Smuzhiyun 			tableDecode[u].nbBits = (BYTE)(tableLog - BIT_highbit32((U32)nextState));
153*4882a593Smuzhiyun 			tableDecode[u].newState = (U16)((nextState << tableDecode[u].nbBits) - tableSize);
154*4882a593Smuzhiyun 		}
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	return 0;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /*-*******************************************************
161*4882a593Smuzhiyun *  Decompression (Byte symbols)
162*4882a593Smuzhiyun *********************************************************/
FSE_buildDTable_rle(FSE_DTable * dt,BYTE symbolValue)163*4882a593Smuzhiyun size_t FSE_buildDTable_rle(FSE_DTable *dt, BYTE symbolValue)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun 	void *ptr = dt;
166*4882a593Smuzhiyun 	FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr;
167*4882a593Smuzhiyun 	void *dPtr = dt + 1;
168*4882a593Smuzhiyun 	FSE_decode_t *const cell = (FSE_decode_t *)dPtr;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	DTableH->tableLog = 0;
171*4882a593Smuzhiyun 	DTableH->fastMode = 0;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	cell->newState = 0;
174*4882a593Smuzhiyun 	cell->symbol = symbolValue;
175*4882a593Smuzhiyun 	cell->nbBits = 0;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	return 0;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun 
FSE_buildDTable_raw(FSE_DTable * dt,unsigned nbBits)180*4882a593Smuzhiyun size_t FSE_buildDTable_raw(FSE_DTable *dt, unsigned nbBits)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	void *ptr = dt;
183*4882a593Smuzhiyun 	FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr;
184*4882a593Smuzhiyun 	void *dPtr = dt + 1;
185*4882a593Smuzhiyun 	FSE_decode_t *const dinfo = (FSE_decode_t *)dPtr;
186*4882a593Smuzhiyun 	const unsigned tableSize = 1 << nbBits;
187*4882a593Smuzhiyun 	const unsigned tableMask = tableSize - 1;
188*4882a593Smuzhiyun 	const unsigned maxSV1 = tableMask + 1;
189*4882a593Smuzhiyun 	unsigned s;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	/* Sanity checks */
192*4882a593Smuzhiyun 	if (nbBits < 1)
193*4882a593Smuzhiyun 		return ERROR(GENERIC); /* min size */
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	/* Build Decoding Table */
196*4882a593Smuzhiyun 	DTableH->tableLog = (U16)nbBits;
197*4882a593Smuzhiyun 	DTableH->fastMode = 1;
198*4882a593Smuzhiyun 	for (s = 0; s < maxSV1; s++) {
199*4882a593Smuzhiyun 		dinfo[s].newState = 0;
200*4882a593Smuzhiyun 		dinfo[s].symbol = (BYTE)s;
201*4882a593Smuzhiyun 		dinfo[s].nbBits = (BYTE)nbBits;
202*4882a593Smuzhiyun 	}
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	return 0;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun 
FSE_decompress_usingDTable_generic(void * dst,size_t maxDstSize,const void * cSrc,size_t cSrcSize,const FSE_DTable * dt,const unsigned fast)207*4882a593Smuzhiyun FORCE_INLINE size_t FSE_decompress_usingDTable_generic(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt,
208*4882a593Smuzhiyun 						       const unsigned fast)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun 	BYTE *const ostart = (BYTE *)dst;
211*4882a593Smuzhiyun 	BYTE *op = ostart;
212*4882a593Smuzhiyun 	BYTE *const omax = op + maxDstSize;
213*4882a593Smuzhiyun 	BYTE *const olimit = omax - 3;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	BIT_DStream_t bitD;
216*4882a593Smuzhiyun 	FSE_DState_t state1;
217*4882a593Smuzhiyun 	FSE_DState_t state2;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	/* Init */
220*4882a593Smuzhiyun 	CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	FSE_initDState(&state1, &bitD, dt);
223*4882a593Smuzhiyun 	FSE_initDState(&state2, &bitD, dt);
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	/* 4 symbols per loop */
228*4882a593Smuzhiyun 	for (; (BIT_reloadDStream(&bitD) == BIT_DStream_unfinished) & (op < olimit); op += 4) {
229*4882a593Smuzhiyun 		op[0] = FSE_GETSYMBOL(&state1);
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 		if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
232*4882a593Smuzhiyun 			BIT_reloadDStream(&bitD);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 		op[1] = FSE_GETSYMBOL(&state2);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 		if (FSE_MAX_TABLELOG * 4 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
237*4882a593Smuzhiyun 		{
238*4882a593Smuzhiyun 			if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) {
239*4882a593Smuzhiyun 				op += 2;
240*4882a593Smuzhiyun 				break;
241*4882a593Smuzhiyun 			}
242*4882a593Smuzhiyun 		}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 		op[2] = FSE_GETSYMBOL(&state1);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 		if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */
247*4882a593Smuzhiyun 			BIT_reloadDStream(&bitD);
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 		op[3] = FSE_GETSYMBOL(&state2);
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	/* tail */
253*4882a593Smuzhiyun 	/* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
254*4882a593Smuzhiyun 	while (1) {
255*4882a593Smuzhiyun 		if (op > (omax - 2))
256*4882a593Smuzhiyun 			return ERROR(dstSize_tooSmall);
257*4882a593Smuzhiyun 		*op++ = FSE_GETSYMBOL(&state1);
258*4882a593Smuzhiyun 		if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) {
259*4882a593Smuzhiyun 			*op++ = FSE_GETSYMBOL(&state2);
260*4882a593Smuzhiyun 			break;
261*4882a593Smuzhiyun 		}
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 		if (op > (omax - 2))
264*4882a593Smuzhiyun 			return ERROR(dstSize_tooSmall);
265*4882a593Smuzhiyun 		*op++ = FSE_GETSYMBOL(&state2);
266*4882a593Smuzhiyun 		if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) {
267*4882a593Smuzhiyun 			*op++ = FSE_GETSYMBOL(&state1);
268*4882a593Smuzhiyun 			break;
269*4882a593Smuzhiyun 		}
270*4882a593Smuzhiyun 	}
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	return op - ostart;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
FSE_decompress_usingDTable(void * dst,size_t originalSize,const void * cSrc,size_t cSrcSize,const FSE_DTable * dt)275*4882a593Smuzhiyun size_t FSE_decompress_usingDTable(void *dst, size_t originalSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun 	const void *ptr = dt;
278*4882a593Smuzhiyun 	const FSE_DTableHeader *DTableH = (const FSE_DTableHeader *)ptr;
279*4882a593Smuzhiyun 	const U32 fastMode = DTableH->fastMode;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	/* select fast mode (static) */
282*4882a593Smuzhiyun 	if (fastMode)
283*4882a593Smuzhiyun 		return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
284*4882a593Smuzhiyun 	return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun 
FSE_decompress_wksp(void * dst,size_t dstCapacity,const void * cSrc,size_t cSrcSize,unsigned maxLog,void * workspace,size_t workspaceSize)287*4882a593Smuzhiyun size_t FSE_decompress_wksp(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, unsigned maxLog, void *workspace, size_t workspaceSize)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun 	const BYTE *const istart = (const BYTE *)cSrc;
290*4882a593Smuzhiyun 	const BYTE *ip = istart;
291*4882a593Smuzhiyun 	unsigned tableLog;
292*4882a593Smuzhiyun 	unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
293*4882a593Smuzhiyun 	size_t NCountLength;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	FSE_DTable *dt;
296*4882a593Smuzhiyun 	short *counting;
297*4882a593Smuzhiyun 	size_t spaceUsed32 = 0;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	FSE_STATIC_ASSERT(sizeof(FSE_DTable) == sizeof(U32));
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	dt = (FSE_DTable *)((U32 *)workspace + spaceUsed32);
302*4882a593Smuzhiyun 	spaceUsed32 += FSE_DTABLE_SIZE_U32(maxLog);
303*4882a593Smuzhiyun 	counting = (short *)((U32 *)workspace + spaceUsed32);
304*4882a593Smuzhiyun 	spaceUsed32 += ALIGN(sizeof(short) * (FSE_MAX_SYMBOL_VALUE + 1), sizeof(U32)) >> 2;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	if ((spaceUsed32 << 2) > workspaceSize)
307*4882a593Smuzhiyun 		return ERROR(tableLog_tooLarge);
308*4882a593Smuzhiyun 	workspace = (U32 *)workspace + spaceUsed32;
309*4882a593Smuzhiyun 	workspaceSize -= (spaceUsed32 << 2);
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	/* normal FSE decoding mode */
312*4882a593Smuzhiyun 	NCountLength = FSE_readNCount(counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
313*4882a593Smuzhiyun 	if (FSE_isError(NCountLength))
314*4882a593Smuzhiyun 		return NCountLength;
315*4882a593Smuzhiyun 	// if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong);   /* too small input size; supposed to be already checked in NCountLength, only remaining
316*4882a593Smuzhiyun 	// case : NCountLength==cSrcSize */
317*4882a593Smuzhiyun 	if (tableLog > maxLog)
318*4882a593Smuzhiyun 		return ERROR(tableLog_tooLarge);
319*4882a593Smuzhiyun 	ip += NCountLength;
320*4882a593Smuzhiyun 	cSrcSize -= NCountLength;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	CHECK_F(FSE_buildDTable_wksp(dt, counting, maxSymbolValue, tableLog, workspace, workspaceSize));
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	return FSE_decompress_usingDTable(dst, dstCapacity, ip, cSrcSize, dt); /* always return, even if it is an error code */
325*4882a593Smuzhiyun }
326