1*c03c5b1cSMartin Matuska /* ******************************************************************
2*c03c5b1cSMartin Matuska * FSE : Finite State Entropy decoder
3*c03c5b1cSMartin Matuska * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
4*c03c5b1cSMartin Matuska *
5*c03c5b1cSMartin Matuska * You can contact the author at :
6*c03c5b1cSMartin Matuska * - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
7*c03c5b1cSMartin Matuska * - Public forum : https://groups.google.com/forum/#!forum/lz4c
8*c03c5b1cSMartin Matuska *
9*c03c5b1cSMartin Matuska * This source code is licensed under both the BSD-style license (found in the
10*c03c5b1cSMartin Matuska * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11*c03c5b1cSMartin Matuska * in the COPYING file in the root directory of this source tree).
12*c03c5b1cSMartin Matuska * You may select, at your option, one of the above-listed licenses.
13*c03c5b1cSMartin Matuska ****************************************************************** */
14*c03c5b1cSMartin Matuska
15*c03c5b1cSMartin Matuska
16*c03c5b1cSMartin Matuska /* **************************************************************
17*c03c5b1cSMartin Matuska * Includes
18*c03c5b1cSMartin Matuska ****************************************************************/
19*c03c5b1cSMartin Matuska #include <stdlib.h> /* malloc, free, qsort */
20*c03c5b1cSMartin Matuska #include <string.h> /* memcpy, memset */
21*c03c5b1cSMartin Matuska #include "bitstream.h"
22*c03c5b1cSMartin Matuska #include "compiler.h"
23*c03c5b1cSMartin Matuska #define FSE_STATIC_LINKING_ONLY
24*c03c5b1cSMartin Matuska #include "fse.h"
25*c03c5b1cSMartin Matuska #include "error_private.h"
26*c03c5b1cSMartin Matuska
27*c03c5b1cSMartin Matuska
28*c03c5b1cSMartin Matuska /* **************************************************************
29*c03c5b1cSMartin Matuska * Error Management
30*c03c5b1cSMartin Matuska ****************************************************************/
31*c03c5b1cSMartin Matuska #define FSE_isError ERR_isError
32*c03c5b1cSMartin Matuska #define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */
33*c03c5b1cSMartin Matuska
34*c03c5b1cSMartin Matuska
35*c03c5b1cSMartin Matuska /* **************************************************************
36*c03c5b1cSMartin Matuska * Templates
37*c03c5b1cSMartin Matuska ****************************************************************/
38*c03c5b1cSMartin Matuska /*
39*c03c5b1cSMartin Matuska designed to be included
40*c03c5b1cSMartin Matuska for type-specific functions (template emulation in C)
41*c03c5b1cSMartin Matuska Objective is to write these functions only once, for improved maintenance
42*c03c5b1cSMartin Matuska */
43*c03c5b1cSMartin Matuska
44*c03c5b1cSMartin Matuska /* safety checks */
45*c03c5b1cSMartin Matuska #ifndef FSE_FUNCTION_EXTENSION
46*c03c5b1cSMartin Matuska # error "FSE_FUNCTION_EXTENSION must be defined"
47*c03c5b1cSMartin Matuska #endif
48*c03c5b1cSMartin Matuska #ifndef FSE_FUNCTION_TYPE
49*c03c5b1cSMartin Matuska # error "FSE_FUNCTION_TYPE must be defined"
50*c03c5b1cSMartin Matuska #endif
51*c03c5b1cSMartin Matuska
52*c03c5b1cSMartin Matuska /* Function names */
53*c03c5b1cSMartin Matuska #define FSE_CAT(X,Y) X##Y
54*c03c5b1cSMartin Matuska #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
55*c03c5b1cSMartin Matuska #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)
56*c03c5b1cSMartin Matuska
57*c03c5b1cSMartin Matuska
58*c03c5b1cSMartin Matuska /* Function templates */
FSE_buildDTable(FSE_DTable * dt,const short * normalizedCounter,unsigned maxSymbolValue,unsigned tableLog)59*c03c5b1cSMartin Matuska size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
60*c03c5b1cSMartin Matuska {
61*c03c5b1cSMartin Matuska void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */
62*c03c5b1cSMartin Matuska FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr);
63*c03c5b1cSMartin Matuska U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];
64*c03c5b1cSMartin Matuska
65*c03c5b1cSMartin Matuska U32 const maxSV1 = maxSymbolValue + 1;
66*c03c5b1cSMartin Matuska U32 const tableSize = 1 << tableLog;
67*c03c5b1cSMartin Matuska U32 highThreshold = tableSize-1;
68*c03c5b1cSMartin Matuska
69*c03c5b1cSMartin Matuska /* Sanity Checks */
70*c03c5b1cSMartin Matuska if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
71*c03c5b1cSMartin Matuska if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
72*c03c5b1cSMartin Matuska
73*c03c5b1cSMartin Matuska /* Init, lay down lowprob symbols */
74*c03c5b1cSMartin Matuska { FSE_DTableHeader DTableH;
75*c03c5b1cSMartin Matuska DTableH.tableLog = (U16)tableLog;
76*c03c5b1cSMartin Matuska DTableH.fastMode = 1;
77*c03c5b1cSMartin Matuska { S16 const largeLimit= (S16)(1 << (tableLog-1));
78*c03c5b1cSMartin Matuska U32 s;
79*c03c5b1cSMartin Matuska for (s=0; s<maxSV1; s++) {
80*c03c5b1cSMartin Matuska if (normalizedCounter[s]==-1) {
81*c03c5b1cSMartin Matuska tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;
82*c03c5b1cSMartin Matuska symbolNext[s] = 1;
83*c03c5b1cSMartin Matuska } else {
84*c03c5b1cSMartin Matuska if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;
85*c03c5b1cSMartin Matuska symbolNext[s] = normalizedCounter[s];
86*c03c5b1cSMartin Matuska } } }
87*c03c5b1cSMartin Matuska memcpy(dt, &DTableH, sizeof(DTableH));
88*c03c5b1cSMartin Matuska }
89*c03c5b1cSMartin Matuska
90*c03c5b1cSMartin Matuska /* Spread symbols */
91*c03c5b1cSMartin Matuska { U32 const tableMask = tableSize-1;
92*c03c5b1cSMartin Matuska U32 const step = FSE_TABLESTEP(tableSize);
93*c03c5b1cSMartin Matuska U32 s, position = 0;
94*c03c5b1cSMartin Matuska for (s=0; s<maxSV1; s++) {
95*c03c5b1cSMartin Matuska int i;
96*c03c5b1cSMartin Matuska for (i=0; i<normalizedCounter[s]; i++) {
97*c03c5b1cSMartin Matuska tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
98*c03c5b1cSMartin Matuska position = (position + step) & tableMask;
99*c03c5b1cSMartin Matuska while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
100*c03c5b1cSMartin Matuska } }
101*c03c5b1cSMartin Matuska if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
102*c03c5b1cSMartin Matuska }
103*c03c5b1cSMartin Matuska
104*c03c5b1cSMartin Matuska /* Build Decoding table */
105*c03c5b1cSMartin Matuska { U32 u;
106*c03c5b1cSMartin Matuska for (u=0; u<tableSize; u++) {
107*c03c5b1cSMartin Matuska FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
108*c03c5b1cSMartin Matuska U32 const nextState = symbolNext[symbol]++;
109*c03c5b1cSMartin Matuska tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) );
110*c03c5b1cSMartin Matuska tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
111*c03c5b1cSMartin Matuska } }
112*c03c5b1cSMartin Matuska
113*c03c5b1cSMartin Matuska return 0;
114*c03c5b1cSMartin Matuska }
115*c03c5b1cSMartin Matuska
116*c03c5b1cSMartin Matuska
117*c03c5b1cSMartin Matuska #ifndef FSE_COMMONDEFS_ONLY
118*c03c5b1cSMartin Matuska
119*c03c5b1cSMartin Matuska /*-*******************************************************
120*c03c5b1cSMartin Matuska * Decompression (Byte symbols)
121*c03c5b1cSMartin Matuska *********************************************************/
FSE_buildDTable_rle(FSE_DTable * dt,BYTE symbolValue)122*c03c5b1cSMartin Matuska size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue)
123*c03c5b1cSMartin Matuska {
124*c03c5b1cSMartin Matuska void* ptr = dt;
125*c03c5b1cSMartin Matuska FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
126*c03c5b1cSMartin Matuska void* dPtr = dt + 1;
127*c03c5b1cSMartin Matuska FSE_decode_t* const cell = (FSE_decode_t*)dPtr;
128*c03c5b1cSMartin Matuska
129*c03c5b1cSMartin Matuska DTableH->tableLog = 0;
130*c03c5b1cSMartin Matuska DTableH->fastMode = 0;
131*c03c5b1cSMartin Matuska
132*c03c5b1cSMartin Matuska cell->newState = 0;
133*c03c5b1cSMartin Matuska cell->symbol = symbolValue;
134*c03c5b1cSMartin Matuska cell->nbBits = 0;
135*c03c5b1cSMartin Matuska
136*c03c5b1cSMartin Matuska return 0;
137*c03c5b1cSMartin Matuska }
138*c03c5b1cSMartin Matuska
139*c03c5b1cSMartin Matuska
FSE_buildDTable_raw(FSE_DTable * dt,unsigned nbBits)140*c03c5b1cSMartin Matuska size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)
141*c03c5b1cSMartin Matuska {
142*c03c5b1cSMartin Matuska void* ptr = dt;
143*c03c5b1cSMartin Matuska FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;
144*c03c5b1cSMartin Matuska void* dPtr = dt + 1;
145*c03c5b1cSMartin Matuska FSE_decode_t* const dinfo = (FSE_decode_t*)dPtr;
146*c03c5b1cSMartin Matuska const unsigned tableSize = 1 << nbBits;
147*c03c5b1cSMartin Matuska const unsigned tableMask = tableSize - 1;
148*c03c5b1cSMartin Matuska const unsigned maxSV1 = tableMask+1;
149*c03c5b1cSMartin Matuska unsigned s;
150*c03c5b1cSMartin Matuska
151*c03c5b1cSMartin Matuska /* Sanity checks */
152*c03c5b1cSMartin Matuska if (nbBits < 1) return ERROR(GENERIC); /* min size */
153*c03c5b1cSMartin Matuska
154*c03c5b1cSMartin Matuska /* Build Decoding Table */
155*c03c5b1cSMartin Matuska DTableH->tableLog = (U16)nbBits;
156*c03c5b1cSMartin Matuska DTableH->fastMode = 1;
157*c03c5b1cSMartin Matuska for (s=0; s<maxSV1; s++) {
158*c03c5b1cSMartin Matuska dinfo[s].newState = 0;
159*c03c5b1cSMartin Matuska dinfo[s].symbol = (BYTE)s;
160*c03c5b1cSMartin Matuska dinfo[s].nbBits = (BYTE)nbBits;
161*c03c5b1cSMartin Matuska }
162*c03c5b1cSMartin Matuska
163*c03c5b1cSMartin Matuska return 0;
164*c03c5b1cSMartin Matuska }
165*c03c5b1cSMartin Matuska
FSE_decompress_usingDTable_generic(void * dst,size_t maxDstSize,const void * cSrc,size_t cSrcSize,const FSE_DTable * dt,const unsigned fast)166*c03c5b1cSMartin Matuska FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic(
167*c03c5b1cSMartin Matuska void* dst, size_t maxDstSize,
168*c03c5b1cSMartin Matuska const void* cSrc, size_t cSrcSize,
169*c03c5b1cSMartin Matuska const FSE_DTable* dt, const unsigned fast)
170*c03c5b1cSMartin Matuska {
171*c03c5b1cSMartin Matuska BYTE* const ostart = (BYTE*) dst;
172*c03c5b1cSMartin Matuska BYTE* op = ostart;
173*c03c5b1cSMartin Matuska BYTE* const omax = op + maxDstSize;
174*c03c5b1cSMartin Matuska BYTE* const olimit = omax-3;
175*c03c5b1cSMartin Matuska
176*c03c5b1cSMartin Matuska BIT_DStream_t bitD;
177*c03c5b1cSMartin Matuska FSE_DState_t state1;
178*c03c5b1cSMartin Matuska FSE_DState_t state2;
179*c03c5b1cSMartin Matuska
180*c03c5b1cSMartin Matuska /* Init */
181*c03c5b1cSMartin Matuska CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));
182*c03c5b1cSMartin Matuska
183*c03c5b1cSMartin Matuska FSE_initDState(&state1, &bitD, dt);
184*c03c5b1cSMartin Matuska FSE_initDState(&state2, &bitD, dt);
185*c03c5b1cSMartin Matuska
186*c03c5b1cSMartin Matuska #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
187*c03c5b1cSMartin Matuska
188*c03c5b1cSMartin Matuska /* 4 symbols per loop */
189*c03c5b1cSMartin Matuska for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) {
190*c03c5b1cSMartin Matuska op[0] = FSE_GETSYMBOL(&state1);
191*c03c5b1cSMartin Matuska
192*c03c5b1cSMartin Matuska if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
193*c03c5b1cSMartin Matuska BIT_reloadDStream(&bitD);
194*c03c5b1cSMartin Matuska
195*c03c5b1cSMartin Matuska op[1] = FSE_GETSYMBOL(&state2);
196*c03c5b1cSMartin Matuska
197*c03c5b1cSMartin Matuska if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
198*c03c5b1cSMartin Matuska { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } }
199*c03c5b1cSMartin Matuska
200*c03c5b1cSMartin Matuska op[2] = FSE_GETSYMBOL(&state1);
201*c03c5b1cSMartin Matuska
202*c03c5b1cSMartin Matuska if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
203*c03c5b1cSMartin Matuska BIT_reloadDStream(&bitD);
204*c03c5b1cSMartin Matuska
205*c03c5b1cSMartin Matuska op[3] = FSE_GETSYMBOL(&state2);
206*c03c5b1cSMartin Matuska }
207*c03c5b1cSMartin Matuska
208*c03c5b1cSMartin Matuska /* tail */
209*c03c5b1cSMartin Matuska /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
210*c03c5b1cSMartin Matuska while (1) {
211*c03c5b1cSMartin Matuska if (op>(omax-2)) return ERROR(dstSize_tooSmall);
212*c03c5b1cSMartin Matuska *op++ = FSE_GETSYMBOL(&state1);
213*c03c5b1cSMartin Matuska if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
214*c03c5b1cSMartin Matuska *op++ = FSE_GETSYMBOL(&state2);
215*c03c5b1cSMartin Matuska break;
216*c03c5b1cSMartin Matuska }
217*c03c5b1cSMartin Matuska
218*c03c5b1cSMartin Matuska if (op>(omax-2)) return ERROR(dstSize_tooSmall);
219*c03c5b1cSMartin Matuska *op++ = FSE_GETSYMBOL(&state2);
220*c03c5b1cSMartin Matuska if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
221*c03c5b1cSMartin Matuska *op++ = FSE_GETSYMBOL(&state1);
222*c03c5b1cSMartin Matuska break;
223*c03c5b1cSMartin Matuska } }
224*c03c5b1cSMartin Matuska
225*c03c5b1cSMartin Matuska return op-ostart;
226*c03c5b1cSMartin Matuska }
227*c03c5b1cSMartin Matuska
228*c03c5b1cSMartin Matuska
FSE_decompress_usingDTable(void * dst,size_t originalSize,const void * cSrc,size_t cSrcSize,const FSE_DTable * dt)229*c03c5b1cSMartin Matuska size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,
230*c03c5b1cSMartin Matuska const void* cSrc, size_t cSrcSize,
231*c03c5b1cSMartin Matuska const FSE_DTable* dt)
232*c03c5b1cSMartin Matuska {
233*c03c5b1cSMartin Matuska const void* ptr = dt;
234*c03c5b1cSMartin Matuska const FSE_DTableHeader* DTableH = (const FSE_DTableHeader*)ptr;
235*c03c5b1cSMartin Matuska const U32 fastMode = DTableH->fastMode;
236*c03c5b1cSMartin Matuska
237*c03c5b1cSMartin Matuska /* select fast mode (static) */
238*c03c5b1cSMartin Matuska if (fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
239*c03c5b1cSMartin Matuska return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
240*c03c5b1cSMartin Matuska }
241*c03c5b1cSMartin Matuska
242*c03c5b1cSMartin Matuska
FSE_decompress_wksp(void * dst,size_t dstCapacity,const void * cSrc,size_t cSrcSize,FSE_DTable * workSpace,unsigned maxLog)243*c03c5b1cSMartin Matuska size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog)
244*c03c5b1cSMartin Matuska {
245*c03c5b1cSMartin Matuska const BYTE* const istart = (const BYTE*)cSrc;
246*c03c5b1cSMartin Matuska const BYTE* ip = istart;
247*c03c5b1cSMartin Matuska short counting[FSE_MAX_SYMBOL_VALUE+1];
248*c03c5b1cSMartin Matuska unsigned tableLog;
249*c03c5b1cSMartin Matuska unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
250*c03c5b1cSMartin Matuska
251*c03c5b1cSMartin Matuska /* normal FSE decoding mode */
252*c03c5b1cSMartin Matuska size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
253*c03c5b1cSMartin Matuska if (FSE_isError(NCountLength)) return NCountLength;
254*c03c5b1cSMartin Matuska /* if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); */ /* too small input size; supposed to be already checked in NCountLength, only remaining case : NCountLength==cSrcSize */
255*c03c5b1cSMartin Matuska if (tableLog > maxLog) return ERROR(tableLog_tooLarge);
256*c03c5b1cSMartin Matuska ip += NCountLength;
257*c03c5b1cSMartin Matuska cSrcSize -= NCountLength;
258*c03c5b1cSMartin Matuska
259*c03c5b1cSMartin Matuska CHECK_F( FSE_buildDTable (workSpace, counting, maxSymbolValue, tableLog) );
260*c03c5b1cSMartin Matuska
261*c03c5b1cSMartin Matuska return FSE_decompress_usingDTable (dst, dstCapacity, ip, cSrcSize, workSpace); /* always return, even if it is an error code */
262*c03c5b1cSMartin Matuska }
263*c03c5b1cSMartin Matuska
264*c03c5b1cSMartin Matuska
265*c03c5b1cSMartin Matuska typedef FSE_DTable DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
266*c03c5b1cSMartin Matuska
FSE_decompress(void * dst,size_t dstCapacity,const void * cSrc,size_t cSrcSize)267*c03c5b1cSMartin Matuska size_t FSE_decompress(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize)
268*c03c5b1cSMartin Matuska {
269*c03c5b1cSMartin Matuska DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
270*c03c5b1cSMartin Matuska return FSE_decompress_wksp(dst, dstCapacity, cSrc, cSrcSize, dt, FSE_MAX_TABLELOG);
271*c03c5b1cSMartin Matuska }
272*c03c5b1cSMartin Matuska
273*c03c5b1cSMartin Matuska
274*c03c5b1cSMartin Matuska
275*c03c5b1cSMartin Matuska #endif /* FSE_COMMONDEFS_ONLY */
276