1*01826a49SYabin Cui /*
2*01826a49SYabin Cui * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui * All rights reserved.
4*01826a49SYabin Cui *
5*01826a49SYabin Cui * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui */
10*01826a49SYabin Cui
11*01826a49SYabin Cui
12*01826a49SYabin Cui /*- Dependencies -*/
13*01826a49SYabin Cui #include "zstd_v05.h"
14*01826a49SYabin Cui #include "../common/error_private.h"
15*01826a49SYabin Cui
16*01826a49SYabin Cui
17*01826a49SYabin Cui /* ******************************************************************
18*01826a49SYabin Cui mem.h
19*01826a49SYabin Cui low-level memory access routines
20*01826a49SYabin Cui Copyright (C) 2013-2015, Yann Collet.
21*01826a49SYabin Cui
22*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
23*01826a49SYabin Cui
24*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
25*01826a49SYabin Cui modification, are permitted provided that the following conditions are
26*01826a49SYabin Cui met:
27*01826a49SYabin Cui
28*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
29*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
30*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
31*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
32*01826a49SYabin Cui in the documentation and/or other materials provided with the
33*01826a49SYabin Cui distribution.
34*01826a49SYabin Cui
35*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46*01826a49SYabin Cui
47*01826a49SYabin Cui You can contact the author at :
48*01826a49SYabin Cui - FSEv05 source repository : https://github.com/Cyan4973/FiniteStateEntropy
49*01826a49SYabin Cui - Public forum : https://groups.google.com/forum/#!forum/lz4c
50*01826a49SYabin Cui ****************************************************************** */
51*01826a49SYabin Cui #ifndef MEM_H_MODULE
52*01826a49SYabin Cui #define MEM_H_MODULE
53*01826a49SYabin Cui
54*01826a49SYabin Cui #if defined (__cplusplus)
55*01826a49SYabin Cui extern "C" {
56*01826a49SYabin Cui #endif
57*01826a49SYabin Cui
58*01826a49SYabin Cui /*-****************************************
59*01826a49SYabin Cui * Dependencies
60*01826a49SYabin Cui ******************************************/
61*01826a49SYabin Cui #include <stddef.h> /* size_t, ptrdiff_t */
62*01826a49SYabin Cui #include <string.h> /* memcpy */
63*01826a49SYabin Cui
64*01826a49SYabin Cui
65*01826a49SYabin Cui /*-****************************************
66*01826a49SYabin Cui * Compiler specifics
67*01826a49SYabin Cui ******************************************/
68*01826a49SYabin Cui #if defined(__GNUC__)
69*01826a49SYabin Cui # define MEM_STATIC static __attribute__((unused))
70*01826a49SYabin Cui #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
71*01826a49SYabin Cui # define MEM_STATIC static inline
72*01826a49SYabin Cui #elif defined(_MSC_VER)
73*01826a49SYabin Cui # define MEM_STATIC static __inline
74*01826a49SYabin Cui #else
75*01826a49SYabin Cui # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
76*01826a49SYabin Cui #endif
77*01826a49SYabin Cui
78*01826a49SYabin Cui
79*01826a49SYabin Cui /*-**************************************************************
80*01826a49SYabin Cui * Basic Types
81*01826a49SYabin Cui *****************************************************************/
82*01826a49SYabin Cui #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
83*01826a49SYabin Cui # if defined(_AIX)
84*01826a49SYabin Cui # include <inttypes.h>
85*01826a49SYabin Cui # else
86*01826a49SYabin Cui # include <stdint.h> /* intptr_t */
87*01826a49SYabin Cui # endif
88*01826a49SYabin Cui typedef uint8_t BYTE;
89*01826a49SYabin Cui typedef uint16_t U16;
90*01826a49SYabin Cui typedef int16_t S16;
91*01826a49SYabin Cui typedef uint32_t U32;
92*01826a49SYabin Cui typedef int32_t S32;
93*01826a49SYabin Cui typedef uint64_t U64;
94*01826a49SYabin Cui typedef int64_t S64;
95*01826a49SYabin Cui #else
96*01826a49SYabin Cui typedef unsigned char BYTE;
97*01826a49SYabin Cui typedef unsigned short U16;
98*01826a49SYabin Cui typedef signed short S16;
99*01826a49SYabin Cui typedef unsigned int U32;
100*01826a49SYabin Cui typedef signed int S32;
101*01826a49SYabin Cui typedef unsigned long long U64;
102*01826a49SYabin Cui typedef signed long long S64;
103*01826a49SYabin Cui #endif
104*01826a49SYabin Cui
105*01826a49SYabin Cui
106*01826a49SYabin Cui /*-**************************************************************
107*01826a49SYabin Cui * Memory I/O
108*01826a49SYabin Cui *****************************************************************/
109*01826a49SYabin Cui
MEM_32bits(void)110*01826a49SYabin Cui MEM_STATIC unsigned MEM_32bits(void) { return sizeof(void*)==4; }
MEM_64bits(void)111*01826a49SYabin Cui MEM_STATIC unsigned MEM_64bits(void) { return sizeof(void*)==8; }
112*01826a49SYabin Cui
MEM_isLittleEndian(void)113*01826a49SYabin Cui MEM_STATIC unsigned MEM_isLittleEndian(void)
114*01826a49SYabin Cui {
115*01826a49SYabin Cui const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
116*01826a49SYabin Cui return one.c[0];
117*01826a49SYabin Cui }
118*01826a49SYabin Cui
MEM_read16(const void * memPtr)119*01826a49SYabin Cui MEM_STATIC U16 MEM_read16(const void* memPtr)
120*01826a49SYabin Cui {
121*01826a49SYabin Cui U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
122*01826a49SYabin Cui }
123*01826a49SYabin Cui
MEM_read32(const void * memPtr)124*01826a49SYabin Cui MEM_STATIC U32 MEM_read32(const void* memPtr)
125*01826a49SYabin Cui {
126*01826a49SYabin Cui U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
127*01826a49SYabin Cui }
128*01826a49SYabin Cui
MEM_read64(const void * memPtr)129*01826a49SYabin Cui MEM_STATIC U64 MEM_read64(const void* memPtr)
130*01826a49SYabin Cui {
131*01826a49SYabin Cui U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
132*01826a49SYabin Cui }
133*01826a49SYabin Cui
MEM_write16(void * memPtr,U16 value)134*01826a49SYabin Cui MEM_STATIC void MEM_write16(void* memPtr, U16 value)
135*01826a49SYabin Cui {
136*01826a49SYabin Cui memcpy(memPtr, &value, sizeof(value));
137*01826a49SYabin Cui }
138*01826a49SYabin Cui
MEM_write32(void * memPtr,U32 value)139*01826a49SYabin Cui MEM_STATIC void MEM_write32(void* memPtr, U32 value)
140*01826a49SYabin Cui {
141*01826a49SYabin Cui memcpy(memPtr, &value, sizeof(value));
142*01826a49SYabin Cui }
143*01826a49SYabin Cui
MEM_write64(void * memPtr,U64 value)144*01826a49SYabin Cui MEM_STATIC void MEM_write64(void* memPtr, U64 value)
145*01826a49SYabin Cui {
146*01826a49SYabin Cui memcpy(memPtr, &value, sizeof(value));
147*01826a49SYabin Cui }
148*01826a49SYabin Cui
MEM_readLE16(const void * memPtr)149*01826a49SYabin Cui MEM_STATIC U16 MEM_readLE16(const void* memPtr)
150*01826a49SYabin Cui {
151*01826a49SYabin Cui if (MEM_isLittleEndian())
152*01826a49SYabin Cui return MEM_read16(memPtr);
153*01826a49SYabin Cui else {
154*01826a49SYabin Cui const BYTE* p = (const BYTE*)memPtr;
155*01826a49SYabin Cui return (U16)(p[0] + (p[1]<<8));
156*01826a49SYabin Cui }
157*01826a49SYabin Cui }
158*01826a49SYabin Cui
MEM_writeLE16(void * memPtr,U16 val)159*01826a49SYabin Cui MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
160*01826a49SYabin Cui {
161*01826a49SYabin Cui if (MEM_isLittleEndian()) {
162*01826a49SYabin Cui MEM_write16(memPtr, val);
163*01826a49SYabin Cui } else {
164*01826a49SYabin Cui BYTE* p = (BYTE*)memPtr;
165*01826a49SYabin Cui p[0] = (BYTE)val;
166*01826a49SYabin Cui p[1] = (BYTE)(val>>8);
167*01826a49SYabin Cui }
168*01826a49SYabin Cui }
169*01826a49SYabin Cui
MEM_readLE32(const void * memPtr)170*01826a49SYabin Cui MEM_STATIC U32 MEM_readLE32(const void* memPtr)
171*01826a49SYabin Cui {
172*01826a49SYabin Cui if (MEM_isLittleEndian())
173*01826a49SYabin Cui return MEM_read32(memPtr);
174*01826a49SYabin Cui else {
175*01826a49SYabin Cui const BYTE* p = (const BYTE*)memPtr;
176*01826a49SYabin Cui return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24));
177*01826a49SYabin Cui }
178*01826a49SYabin Cui }
179*01826a49SYabin Cui
180*01826a49SYabin Cui
MEM_readLE64(const void * memPtr)181*01826a49SYabin Cui MEM_STATIC U64 MEM_readLE64(const void* memPtr)
182*01826a49SYabin Cui {
183*01826a49SYabin Cui if (MEM_isLittleEndian())
184*01826a49SYabin Cui return MEM_read64(memPtr);
185*01826a49SYabin Cui else {
186*01826a49SYabin Cui const BYTE* p = (const BYTE*)memPtr;
187*01826a49SYabin Cui return (U64)((U64)p[0] + ((U64)p[1]<<8) + ((U64)p[2]<<16) + ((U64)p[3]<<24)
188*01826a49SYabin Cui + ((U64)p[4]<<32) + ((U64)p[5]<<40) + ((U64)p[6]<<48) + ((U64)p[7]<<56));
189*01826a49SYabin Cui }
190*01826a49SYabin Cui }
191*01826a49SYabin Cui
192*01826a49SYabin Cui
MEM_readLEST(const void * memPtr)193*01826a49SYabin Cui MEM_STATIC size_t MEM_readLEST(const void* memPtr)
194*01826a49SYabin Cui {
195*01826a49SYabin Cui if (MEM_32bits())
196*01826a49SYabin Cui return (size_t)MEM_readLE32(memPtr);
197*01826a49SYabin Cui else
198*01826a49SYabin Cui return (size_t)MEM_readLE64(memPtr);
199*01826a49SYabin Cui }
200*01826a49SYabin Cui
201*01826a49SYabin Cui
202*01826a49SYabin Cui #if defined (__cplusplus)
203*01826a49SYabin Cui }
204*01826a49SYabin Cui #endif
205*01826a49SYabin Cui
206*01826a49SYabin Cui #endif /* MEM_H_MODULE */
207*01826a49SYabin Cui
208*01826a49SYabin Cui /*
209*01826a49SYabin Cui zstd - standard compression library
210*01826a49SYabin Cui Header File for static linking only
211*01826a49SYabin Cui Copyright (C) 2014-2016, Yann Collet.
212*01826a49SYabin Cui
213*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
214*01826a49SYabin Cui
215*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
216*01826a49SYabin Cui modification, are permitted provided that the following conditions are
217*01826a49SYabin Cui met:
218*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
219*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
220*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
221*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
222*01826a49SYabin Cui in the documentation and/or other materials provided with the
223*01826a49SYabin Cui distribution.
224*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
226*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
227*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
228*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
229*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
230*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
233*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
234*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
235*01826a49SYabin Cui
236*01826a49SYabin Cui You can contact the author at :
237*01826a49SYabin Cui - zstd homepage : https://facebook.github.io/zstd
238*01826a49SYabin Cui */
239*01826a49SYabin Cui #ifndef ZSTD_STATIC_H
240*01826a49SYabin Cui #define ZSTD_STATIC_H
241*01826a49SYabin Cui
242*01826a49SYabin Cui /* The prototypes defined within this file are considered experimental.
243*01826a49SYabin Cui * They should not be used in the context DLL as they may change in the future.
244*01826a49SYabin Cui * Prefer static linking if you need them, to control breaking version changes issues.
245*01826a49SYabin Cui */
246*01826a49SYabin Cui
247*01826a49SYabin Cui #if defined (__cplusplus)
248*01826a49SYabin Cui extern "C" {
249*01826a49SYabin Cui #endif
250*01826a49SYabin Cui
251*01826a49SYabin Cui
252*01826a49SYabin Cui
253*01826a49SYabin Cui /*-*************************************
254*01826a49SYabin Cui * Types
255*01826a49SYabin Cui ***************************************/
256*01826a49SYabin Cui #define ZSTDv05_WINDOWLOG_ABSOLUTEMIN 11
257*01826a49SYabin Cui
258*01826a49SYabin Cui
259*01826a49SYabin Cui /*-*************************************
260*01826a49SYabin Cui * Advanced functions
261*01826a49SYabin Cui ***************************************/
262*01826a49SYabin Cui /*- Advanced Decompression functions -*/
263*01826a49SYabin Cui
264*01826a49SYabin Cui /*! ZSTDv05_decompress_usingPreparedDCtx() :
265*01826a49SYabin Cui * Same as ZSTDv05_decompress_usingDict, but using a reference context `preparedDCtx`, where dictionary has been loaded.
266*01826a49SYabin Cui * It avoids reloading the dictionary each time.
267*01826a49SYabin Cui * `preparedDCtx` must have been properly initialized using ZSTDv05_decompressBegin_usingDict().
268*01826a49SYabin Cui * Requires 2 contexts : 1 for reference, which will not be modified, and 1 to run the decompression operation */
269*01826a49SYabin Cui size_t ZSTDv05_decompress_usingPreparedDCtx(
270*01826a49SYabin Cui ZSTDv05_DCtx* dctx, const ZSTDv05_DCtx* preparedDCtx,
271*01826a49SYabin Cui void* dst, size_t dstCapacity,
272*01826a49SYabin Cui const void* src, size_t srcSize);
273*01826a49SYabin Cui
274*01826a49SYabin Cui
275*01826a49SYabin Cui /* **************************************
276*01826a49SYabin Cui * Streaming functions (direct mode)
277*01826a49SYabin Cui ****************************************/
278*01826a49SYabin Cui size_t ZSTDv05_decompressBegin(ZSTDv05_DCtx* dctx);
279*01826a49SYabin Cui
280*01826a49SYabin Cui /*
281*01826a49SYabin Cui Streaming decompression, direct mode (bufferless)
282*01826a49SYabin Cui
283*01826a49SYabin Cui A ZSTDv05_DCtx object is required to track streaming operations.
284*01826a49SYabin Cui Use ZSTDv05_createDCtx() / ZSTDv05_freeDCtx() to manage it.
285*01826a49SYabin Cui A ZSTDv05_DCtx object can be re-used multiple times.
286*01826a49SYabin Cui
287*01826a49SYabin Cui First typical operation is to retrieve frame parameters, using ZSTDv05_getFrameParams().
288*01826a49SYabin Cui This operation is independent, and just needs enough input data to properly decode the frame header.
289*01826a49SYabin Cui Objective is to retrieve *params.windowlog, to know minimum amount of memory required during decoding.
290*01826a49SYabin Cui Result : 0 when successful, it means the ZSTDv05_parameters structure has been filled.
291*01826a49SYabin Cui >0 : means there is not enough data into src. Provides the expected size to successfully decode header.
292*01826a49SYabin Cui errorCode, which can be tested using ZSTDv05_isError()
293*01826a49SYabin Cui
294*01826a49SYabin Cui Start decompression, with ZSTDv05_decompressBegin() or ZSTDv05_decompressBegin_usingDict()
295*01826a49SYabin Cui Alternatively, you can copy a prepared context, using ZSTDv05_copyDCtx()
296*01826a49SYabin Cui
297*01826a49SYabin Cui Then use ZSTDv05_nextSrcSizeToDecompress() and ZSTDv05_decompressContinue() alternatively.
298*01826a49SYabin Cui ZSTDv05_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTDv05_decompressContinue().
299*01826a49SYabin Cui ZSTDv05_decompressContinue() requires this exact amount of bytes, or it will fail.
300*01826a49SYabin Cui ZSTDv05_decompressContinue() needs previous data blocks during decompression, up to (1 << windowlog).
301*01826a49SYabin Cui They should preferably be located contiguously, prior to current block. Alternatively, a round buffer is also possible.
302*01826a49SYabin Cui
303*01826a49SYabin Cui @result of ZSTDv05_decompressContinue() is the number of bytes regenerated within 'dst'.
304*01826a49SYabin Cui It can be zero, which is not an error; it just means ZSTDv05_decompressContinue() has decoded some header.
305*01826a49SYabin Cui
306*01826a49SYabin Cui A frame is fully decoded when ZSTDv05_nextSrcSizeToDecompress() returns zero.
307*01826a49SYabin Cui Context can then be reset to start a new decompression.
308*01826a49SYabin Cui */
309*01826a49SYabin Cui
310*01826a49SYabin Cui
311*01826a49SYabin Cui /* **************************************
312*01826a49SYabin Cui * Block functions
313*01826a49SYabin Cui ****************************************/
314*01826a49SYabin Cui /*! Block functions produce and decode raw zstd blocks, without frame metadata.
315*01826a49SYabin Cui User will have to take in charge required information to regenerate data, such as block sizes.
316*01826a49SYabin Cui
317*01826a49SYabin Cui A few rules to respect :
318*01826a49SYabin Cui - Uncompressed block size must be <= 128 KB
319*01826a49SYabin Cui - Compressing or decompressing requires a context structure
320*01826a49SYabin Cui + Use ZSTDv05_createCCtx() and ZSTDv05_createDCtx()
321*01826a49SYabin Cui - It is necessary to init context before starting
322*01826a49SYabin Cui + compression : ZSTDv05_compressBegin()
323*01826a49SYabin Cui + decompression : ZSTDv05_decompressBegin()
324*01826a49SYabin Cui + variants _usingDict() are also allowed
325*01826a49SYabin Cui + copyCCtx() and copyDCtx() work too
326*01826a49SYabin Cui - When a block is considered not compressible enough, ZSTDv05_compressBlock() result will be zero.
327*01826a49SYabin Cui In which case, nothing is produced into `dst`.
328*01826a49SYabin Cui + User must test for such outcome and deal directly with uncompressed data
329*01826a49SYabin Cui + ZSTDv05_decompressBlock() doesn't accept uncompressed data as input !!
330*01826a49SYabin Cui */
331*01826a49SYabin Cui
332*01826a49SYabin Cui size_t ZSTDv05_decompressBlock(ZSTDv05_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
333*01826a49SYabin Cui
334*01826a49SYabin Cui
335*01826a49SYabin Cui
336*01826a49SYabin Cui
337*01826a49SYabin Cui #if defined (__cplusplus)
338*01826a49SYabin Cui }
339*01826a49SYabin Cui #endif
340*01826a49SYabin Cui
341*01826a49SYabin Cui #endif /* ZSTDv05_STATIC_H */
342*01826a49SYabin Cui
343*01826a49SYabin Cui
344*01826a49SYabin Cui /*
345*01826a49SYabin Cui zstd_internal - common functions to include
346*01826a49SYabin Cui Header File for include
347*01826a49SYabin Cui Copyright (C) 2014-2016, Yann Collet.
348*01826a49SYabin Cui
349*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
350*01826a49SYabin Cui
351*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
352*01826a49SYabin Cui modification, are permitted provided that the following conditions are
353*01826a49SYabin Cui met:
354*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
355*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
356*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
357*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
358*01826a49SYabin Cui in the documentation and/or other materials provided with the
359*01826a49SYabin Cui distribution.
360*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
361*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
362*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
363*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
364*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
365*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
366*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
367*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
368*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
369*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
370*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
371*01826a49SYabin Cui
372*01826a49SYabin Cui You can contact the author at :
373*01826a49SYabin Cui - zstd source repository : https://github.com/Cyan4973/zstd
374*01826a49SYabin Cui */
375*01826a49SYabin Cui #ifndef ZSTD_CCOMMON_H_MODULE
376*01826a49SYabin Cui #define ZSTD_CCOMMON_H_MODULE
377*01826a49SYabin Cui
378*01826a49SYabin Cui
379*01826a49SYabin Cui
380*01826a49SYabin Cui /*-*************************************
381*01826a49SYabin Cui * Common macros
382*01826a49SYabin Cui ***************************************/
383*01826a49SYabin Cui #define MIN(a,b) ((a)<(b) ? (a) : (b))
384*01826a49SYabin Cui #define MAX(a,b) ((a)>(b) ? (a) : (b))
385*01826a49SYabin Cui
386*01826a49SYabin Cui
387*01826a49SYabin Cui /*-*************************************
388*01826a49SYabin Cui * Common constants
389*01826a49SYabin Cui ***************************************/
390*01826a49SYabin Cui #define ZSTDv05_DICT_MAGIC 0xEC30A435
391*01826a49SYabin Cui
392*01826a49SYabin Cui #define KB *(1 <<10)
393*01826a49SYabin Cui #define MB *(1 <<20)
394*01826a49SYabin Cui #define GB *(1U<<30)
395*01826a49SYabin Cui
396*01826a49SYabin Cui #define BLOCKSIZE (128 KB) /* define, for static allocation */
397*01826a49SYabin Cui
398*01826a49SYabin Cui static const size_t ZSTDv05_blockHeaderSize = 3;
399*01826a49SYabin Cui static const size_t ZSTDv05_frameHeaderSize_min = 5;
400*01826a49SYabin Cui #define ZSTDv05_frameHeaderSize_max 5 /* define, for static allocation */
401*01826a49SYabin Cui
402*01826a49SYabin Cui #define BITv057 128
403*01826a49SYabin Cui #define BITv056 64
404*01826a49SYabin Cui #define BITv055 32
405*01826a49SYabin Cui #define BITv054 16
406*01826a49SYabin Cui #define BITv051 2
407*01826a49SYabin Cui #define BITv050 1
408*01826a49SYabin Cui
409*01826a49SYabin Cui #define IS_HUFv05 0
410*01826a49SYabin Cui #define IS_PCH 1
411*01826a49SYabin Cui #define IS_RAW 2
412*01826a49SYabin Cui #define IS_RLE 3
413*01826a49SYabin Cui
414*01826a49SYabin Cui #define MINMATCH 4
415*01826a49SYabin Cui #define REPCODE_STARTVALUE 1
416*01826a49SYabin Cui
417*01826a49SYabin Cui #define Litbits 8
418*01826a49SYabin Cui #define MLbits 7
419*01826a49SYabin Cui #define LLbits 6
420*01826a49SYabin Cui #define Offbits 5
421*01826a49SYabin Cui #define MaxLit ((1<<Litbits) - 1)
422*01826a49SYabin Cui #define MaxML ((1<<MLbits) - 1)
423*01826a49SYabin Cui #define MaxLL ((1<<LLbits) - 1)
424*01826a49SYabin Cui #define MaxOff ((1<<Offbits)- 1)
425*01826a49SYabin Cui #define MLFSEv05Log 10
426*01826a49SYabin Cui #define LLFSEv05Log 10
427*01826a49SYabin Cui #define OffFSEv05Log 9
428*01826a49SYabin Cui #define MaxSeq MAX(MaxLL, MaxML)
429*01826a49SYabin Cui
430*01826a49SYabin Cui #define FSEv05_ENCODING_RAW 0
431*01826a49SYabin Cui #define FSEv05_ENCODING_RLE 1
432*01826a49SYabin Cui #define FSEv05_ENCODING_STATIC 2
433*01826a49SYabin Cui #define FSEv05_ENCODING_DYNAMIC 3
434*01826a49SYabin Cui
435*01826a49SYabin Cui
436*01826a49SYabin Cui #define ZSTD_HUFFDTABLE_CAPACITY_LOG 12
437*01826a49SYabin Cui
438*01826a49SYabin Cui #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
439*01826a49SYabin Cui #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
440*01826a49SYabin Cui
441*01826a49SYabin Cui #define WILDCOPY_OVERLENGTH 8
442*01826a49SYabin Cui
443*01826a49SYabin Cui #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
444*01826a49SYabin Cui
445*01826a49SYabin Cui typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
446*01826a49SYabin Cui
447*01826a49SYabin Cui
448*01826a49SYabin Cui /*-*******************************************
449*01826a49SYabin Cui * Shared functions to include for inlining
450*01826a49SYabin Cui *********************************************/
ZSTDv05_copy8(void * dst,const void * src)451*01826a49SYabin Cui static void ZSTDv05_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
452*01826a49SYabin Cui
453*01826a49SYabin Cui #define COPY8(d,s) { ZSTDv05_copy8(d,s); d+=8; s+=8; }
454*01826a49SYabin Cui
455*01826a49SYabin Cui /*! ZSTDv05_wildcopy() :
456*01826a49SYabin Cui * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
ZSTDv05_wildcopy(void * dst,const void * src,ptrdiff_t length)457*01826a49SYabin Cui MEM_STATIC void ZSTDv05_wildcopy(void* dst, const void* src, ptrdiff_t length)
458*01826a49SYabin Cui {
459*01826a49SYabin Cui const BYTE* ip = (const BYTE*)src;
460*01826a49SYabin Cui BYTE* op = (BYTE*)dst;
461*01826a49SYabin Cui BYTE* const oend = op + length;
462*01826a49SYabin Cui do
463*01826a49SYabin Cui COPY8(op, ip)
464*01826a49SYabin Cui while (op < oend);
465*01826a49SYabin Cui }
466*01826a49SYabin Cui
467*01826a49SYabin Cui
468*01826a49SYabin Cui /*-*******************************************
469*01826a49SYabin Cui * Private interfaces
470*01826a49SYabin Cui *********************************************/
471*01826a49SYabin Cui typedef struct {
472*01826a49SYabin Cui void* buffer;
473*01826a49SYabin Cui U32* offsetStart;
474*01826a49SYabin Cui U32* offset;
475*01826a49SYabin Cui BYTE* offCodeStart;
476*01826a49SYabin Cui BYTE* offCode;
477*01826a49SYabin Cui BYTE* litStart;
478*01826a49SYabin Cui BYTE* lit;
479*01826a49SYabin Cui BYTE* litLengthStart;
480*01826a49SYabin Cui BYTE* litLength;
481*01826a49SYabin Cui BYTE* matchLengthStart;
482*01826a49SYabin Cui BYTE* matchLength;
483*01826a49SYabin Cui BYTE* dumpsStart;
484*01826a49SYabin Cui BYTE* dumps;
485*01826a49SYabin Cui /* opt */
486*01826a49SYabin Cui U32* matchLengthFreq;
487*01826a49SYabin Cui U32* litLengthFreq;
488*01826a49SYabin Cui U32* litFreq;
489*01826a49SYabin Cui U32* offCodeFreq;
490*01826a49SYabin Cui U32 matchLengthSum;
491*01826a49SYabin Cui U32 litLengthSum;
492*01826a49SYabin Cui U32 litSum;
493*01826a49SYabin Cui U32 offCodeSum;
494*01826a49SYabin Cui } seqStore_t;
495*01826a49SYabin Cui
496*01826a49SYabin Cui
497*01826a49SYabin Cui
498*01826a49SYabin Cui #endif /* ZSTDv05_CCOMMON_H_MODULE */
499*01826a49SYabin Cui /* ******************************************************************
500*01826a49SYabin Cui FSEv05 : Finite State Entropy coder
501*01826a49SYabin Cui header file
502*01826a49SYabin Cui Copyright (C) 2013-2015, Yann Collet.
503*01826a49SYabin Cui
504*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
505*01826a49SYabin Cui
506*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
507*01826a49SYabin Cui modification, are permitted provided that the following conditions are
508*01826a49SYabin Cui met:
509*01826a49SYabin Cui
510*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
511*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
512*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
513*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
514*01826a49SYabin Cui in the documentation and/or other materials provided with the
515*01826a49SYabin Cui distribution.
516*01826a49SYabin Cui
517*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
518*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
519*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
520*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
521*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
522*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
523*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
524*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
525*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
526*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
527*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
528*01826a49SYabin Cui
529*01826a49SYabin Cui You can contact the author at :
530*01826a49SYabin Cui - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
531*01826a49SYabin Cui - Public forum : https://groups.google.com/forum/#!forum/lz4c
532*01826a49SYabin Cui ****************************************************************** */
533*01826a49SYabin Cui #ifndef FSEv05_H
534*01826a49SYabin Cui #define FSEv05_H
535*01826a49SYabin Cui
536*01826a49SYabin Cui #if defined (__cplusplus)
537*01826a49SYabin Cui extern "C" {
538*01826a49SYabin Cui #endif
539*01826a49SYabin Cui
540*01826a49SYabin Cui
541*01826a49SYabin Cui /* *****************************************
542*01826a49SYabin Cui * Includes
543*01826a49SYabin Cui ******************************************/
544*01826a49SYabin Cui #include <stddef.h> /* size_t, ptrdiff_t */
545*01826a49SYabin Cui
546*01826a49SYabin Cui
547*01826a49SYabin Cui /*-****************************************
548*01826a49SYabin Cui * FSEv05 simple functions
549*01826a49SYabin Cui ******************************************/
550*01826a49SYabin Cui size_t FSEv05_decompress(void* dst, size_t maxDstSize,
551*01826a49SYabin Cui const void* cSrc, size_t cSrcSize);
552*01826a49SYabin Cui /*!
553*01826a49SYabin Cui FSEv05_decompress():
554*01826a49SYabin Cui Decompress FSEv05 data from buffer 'cSrc', of size 'cSrcSize',
555*01826a49SYabin Cui into already allocated destination buffer 'dst', of size 'maxDstSize'.
556*01826a49SYabin Cui return : size of regenerated data (<= maxDstSize)
557*01826a49SYabin Cui or an error code, which can be tested using FSEv05_isError()
558*01826a49SYabin Cui
559*01826a49SYabin Cui ** Important ** : FSEv05_decompress() doesn't decompress non-compressible nor RLE data !!!
560*01826a49SYabin Cui Why ? : making this distinction requires a header.
561*01826a49SYabin Cui Header management is intentionally delegated to the user layer, which can better manage special cases.
562*01826a49SYabin Cui */
563*01826a49SYabin Cui
564*01826a49SYabin Cui
565*01826a49SYabin Cui /* *****************************************
566*01826a49SYabin Cui * Tool functions
567*01826a49SYabin Cui ******************************************/
568*01826a49SYabin Cui /* Error Management */
569*01826a49SYabin Cui unsigned FSEv05_isError(size_t code); /* tells if a return value is an error code */
570*01826a49SYabin Cui const char* FSEv05_getErrorName(size_t code); /* provides error code string (useful for debugging) */
571*01826a49SYabin Cui
572*01826a49SYabin Cui
573*01826a49SYabin Cui
574*01826a49SYabin Cui
575*01826a49SYabin Cui /* *****************************************
576*01826a49SYabin Cui * FSEv05 detailed API
577*01826a49SYabin Cui ******************************************/
578*01826a49SYabin Cui /* *** DECOMPRESSION *** */
579*01826a49SYabin Cui
580*01826a49SYabin Cui /*!
581*01826a49SYabin Cui FSEv05_readNCount():
582*01826a49SYabin Cui Read compactly saved 'normalizedCounter' from 'rBuffer'.
583*01826a49SYabin Cui return : size read from 'rBuffer'
584*01826a49SYabin Cui or an errorCode, which can be tested using FSEv05_isError()
585*01826a49SYabin Cui maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */
586*01826a49SYabin Cui size_t FSEv05_readNCount (short* normalizedCounter, unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, const void* rBuffer, size_t rBuffSize);
587*01826a49SYabin Cui
588*01826a49SYabin Cui /*!
589*01826a49SYabin Cui Constructor and Destructor of type FSEv05_DTable
590*01826a49SYabin Cui Note that its size depends on 'tableLog' */
591*01826a49SYabin Cui typedef unsigned FSEv05_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */
592*01826a49SYabin Cui FSEv05_DTable* FSEv05_createDTable(unsigned tableLog);
593*01826a49SYabin Cui void FSEv05_freeDTable(FSEv05_DTable* dt);
594*01826a49SYabin Cui
595*01826a49SYabin Cui /*!
596*01826a49SYabin Cui FSEv05_buildDTable():
597*01826a49SYabin Cui Builds 'dt', which must be already allocated, using FSEv05_createDTable()
598*01826a49SYabin Cui @return : 0,
599*01826a49SYabin Cui or an errorCode, which can be tested using FSEv05_isError() */
600*01826a49SYabin Cui size_t FSEv05_buildDTable (FSEv05_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
601*01826a49SYabin Cui
602*01826a49SYabin Cui /*!
603*01826a49SYabin Cui FSEv05_decompress_usingDTable():
604*01826a49SYabin Cui Decompress compressed source @cSrc of size @cSrcSize using `dt`
605*01826a49SYabin Cui into `dst` which must be already allocated.
606*01826a49SYabin Cui @return : size of regenerated data (necessarily <= @dstCapacity)
607*01826a49SYabin Cui or an errorCode, which can be tested using FSEv05_isError() */
608*01826a49SYabin Cui size_t FSEv05_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSEv05_DTable* dt);
609*01826a49SYabin Cui
610*01826a49SYabin Cui
611*01826a49SYabin Cui
612*01826a49SYabin Cui #if defined (__cplusplus)
613*01826a49SYabin Cui }
614*01826a49SYabin Cui #endif
615*01826a49SYabin Cui
616*01826a49SYabin Cui #endif /* FSEv05_H */
617*01826a49SYabin Cui /* ******************************************************************
618*01826a49SYabin Cui bitstream
619*01826a49SYabin Cui Part of FSEv05 library
620*01826a49SYabin Cui header file (to include)
621*01826a49SYabin Cui Copyright (C) 2013-2016, Yann Collet.
622*01826a49SYabin Cui
623*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
624*01826a49SYabin Cui
625*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
626*01826a49SYabin Cui modification, are permitted provided that the following conditions are
627*01826a49SYabin Cui met:
628*01826a49SYabin Cui
629*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
630*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
631*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
632*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
633*01826a49SYabin Cui in the documentation and/or other materials provided with the
634*01826a49SYabin Cui distribution.
635*01826a49SYabin Cui
636*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
637*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
638*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
639*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
640*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
641*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
642*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
643*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
644*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
645*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
646*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
647*01826a49SYabin Cui
648*01826a49SYabin Cui You can contact the author at :
649*01826a49SYabin Cui - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
650*01826a49SYabin Cui ****************************************************************** */
651*01826a49SYabin Cui #ifndef BITv05STREAM_H_MODULE
652*01826a49SYabin Cui #define BITv05STREAM_H_MODULE
653*01826a49SYabin Cui
654*01826a49SYabin Cui #if defined (__cplusplus)
655*01826a49SYabin Cui extern "C" {
656*01826a49SYabin Cui #endif
657*01826a49SYabin Cui
658*01826a49SYabin Cui
659*01826a49SYabin Cui /*
660*01826a49SYabin Cui * This API consists of small unitary functions, which highly benefit from being inlined.
661*01826a49SYabin Cui * Since link-time-optimization is not available for all compilers,
662*01826a49SYabin Cui * these functions are defined into a .h to be included.
663*01826a49SYabin Cui */
664*01826a49SYabin Cui
665*01826a49SYabin Cui
666*01826a49SYabin Cui
667*01826a49SYabin Cui /*-********************************************
668*01826a49SYabin Cui * bitStream decoding API (read backward)
669*01826a49SYabin Cui **********************************************/
670*01826a49SYabin Cui typedef struct
671*01826a49SYabin Cui {
672*01826a49SYabin Cui size_t bitContainer;
673*01826a49SYabin Cui unsigned bitsConsumed;
674*01826a49SYabin Cui const char* ptr;
675*01826a49SYabin Cui const char* start;
676*01826a49SYabin Cui } BITv05_DStream_t;
677*01826a49SYabin Cui
678*01826a49SYabin Cui typedef enum { BITv05_DStream_unfinished = 0,
679*01826a49SYabin Cui BITv05_DStream_endOfBuffer = 1,
680*01826a49SYabin Cui BITv05_DStream_completed = 2,
681*01826a49SYabin Cui BITv05_DStream_overflow = 3 } BITv05_DStream_status; /* result of BITv05_reloadDStream() */
682*01826a49SYabin Cui /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
683*01826a49SYabin Cui
684*01826a49SYabin Cui MEM_STATIC size_t BITv05_initDStream(BITv05_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
685*01826a49SYabin Cui MEM_STATIC size_t BITv05_readBits(BITv05_DStream_t* bitD, unsigned nbBits);
686*01826a49SYabin Cui MEM_STATIC BITv05_DStream_status BITv05_reloadDStream(BITv05_DStream_t* bitD);
687*01826a49SYabin Cui MEM_STATIC unsigned BITv05_endOfDStream(const BITv05_DStream_t* bitD);
688*01826a49SYabin Cui
689*01826a49SYabin Cui
690*01826a49SYabin Cui /*-****************************************
691*01826a49SYabin Cui * unsafe API
692*01826a49SYabin Cui ******************************************/
693*01826a49SYabin Cui MEM_STATIC size_t BITv05_readBitsFast(BITv05_DStream_t* bitD, unsigned nbBits);
694*01826a49SYabin Cui /* faster, but works only if nbBits >= 1 */
695*01826a49SYabin Cui
696*01826a49SYabin Cui
697*01826a49SYabin Cui
698*01826a49SYabin Cui /*-**************************************************************
699*01826a49SYabin Cui * Helper functions
700*01826a49SYabin Cui ****************************************************************/
BITv05_highbit32(U32 val)701*01826a49SYabin Cui MEM_STATIC unsigned BITv05_highbit32 (U32 val)
702*01826a49SYabin Cui {
703*01826a49SYabin Cui # if defined(_MSC_VER) /* Visual */
704*01826a49SYabin Cui unsigned long r;
705*01826a49SYabin Cui return _BitScanReverse(&r, val) ? (unsigned)r : 0;
706*01826a49SYabin Cui # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
707*01826a49SYabin Cui return __builtin_clz (val) ^ 31;
708*01826a49SYabin Cui # else /* Software version */
709*01826a49SYabin Cui static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
710*01826a49SYabin Cui U32 v = val;
711*01826a49SYabin Cui unsigned r;
712*01826a49SYabin Cui v |= v >> 1;
713*01826a49SYabin Cui v |= v >> 2;
714*01826a49SYabin Cui v |= v >> 4;
715*01826a49SYabin Cui v |= v >> 8;
716*01826a49SYabin Cui v |= v >> 16;
717*01826a49SYabin Cui r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
718*01826a49SYabin Cui return r;
719*01826a49SYabin Cui # endif
720*01826a49SYabin Cui }
721*01826a49SYabin Cui
722*01826a49SYabin Cui
723*01826a49SYabin Cui
724*01826a49SYabin Cui /*-********************************************************
725*01826a49SYabin Cui * bitStream decoding
726*01826a49SYabin Cui **********************************************************/
727*01826a49SYabin Cui /*!BITv05_initDStream
728*01826a49SYabin Cui * Initialize a BITv05_DStream_t.
729*01826a49SYabin Cui * @bitD : a pointer to an already allocated BITv05_DStream_t structure
730*01826a49SYabin Cui * @srcBuffer must point at the beginning of a bitStream
731*01826a49SYabin Cui * @srcSize must be the exact size of the bitStream
732*01826a49SYabin Cui * @result : size of stream (== srcSize) or an errorCode if a problem is detected
733*01826a49SYabin Cui */
BITv05_initDStream(BITv05_DStream_t * bitD,const void * srcBuffer,size_t srcSize)734*01826a49SYabin Cui MEM_STATIC size_t BITv05_initDStream(BITv05_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
735*01826a49SYabin Cui {
736*01826a49SYabin Cui if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
737*01826a49SYabin Cui
738*01826a49SYabin Cui if (srcSize >= sizeof(size_t)) { /* normal case */
739*01826a49SYabin Cui U32 contain32;
740*01826a49SYabin Cui bitD->start = (const char*)srcBuffer;
741*01826a49SYabin Cui bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t);
742*01826a49SYabin Cui bitD->bitContainer = MEM_readLEST(bitD->ptr);
743*01826a49SYabin Cui contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
744*01826a49SYabin Cui if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
745*01826a49SYabin Cui bitD->bitsConsumed = 8 - BITv05_highbit32(contain32);
746*01826a49SYabin Cui } else {
747*01826a49SYabin Cui U32 contain32;
748*01826a49SYabin Cui bitD->start = (const char*)srcBuffer;
749*01826a49SYabin Cui bitD->ptr = bitD->start;
750*01826a49SYabin Cui bitD->bitContainer = *(const BYTE*)(bitD->start);
751*01826a49SYabin Cui switch(srcSize)
752*01826a49SYabin Cui {
753*01826a49SYabin Cui case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);/* fall-through */
754*01826a49SYabin Cui case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);/* fall-through */
755*01826a49SYabin Cui case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);/* fall-through */
756*01826a49SYabin Cui case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24; /* fall-through */
757*01826a49SYabin Cui case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16; /* fall-through */
758*01826a49SYabin Cui case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8; /* fall-through */
759*01826a49SYabin Cui default: break;
760*01826a49SYabin Cui }
761*01826a49SYabin Cui contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
762*01826a49SYabin Cui if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
763*01826a49SYabin Cui bitD->bitsConsumed = 8 - BITv05_highbit32(contain32);
764*01826a49SYabin Cui bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8;
765*01826a49SYabin Cui }
766*01826a49SYabin Cui
767*01826a49SYabin Cui return srcSize;
768*01826a49SYabin Cui }
769*01826a49SYabin Cui
BITv05_lookBits(BITv05_DStream_t * bitD,U32 nbBits)770*01826a49SYabin Cui MEM_STATIC size_t BITv05_lookBits(BITv05_DStream_t* bitD, U32 nbBits)
771*01826a49SYabin Cui {
772*01826a49SYabin Cui const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
773*01826a49SYabin Cui return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
774*01826a49SYabin Cui }
775*01826a49SYabin Cui
776*01826a49SYabin Cui /*! BITv05_lookBitsFast :
777*01826a49SYabin Cui * unsafe version; only works if nbBits >= 1 */
BITv05_lookBitsFast(BITv05_DStream_t * bitD,U32 nbBits)778*01826a49SYabin Cui MEM_STATIC size_t BITv05_lookBitsFast(BITv05_DStream_t* bitD, U32 nbBits)
779*01826a49SYabin Cui {
780*01826a49SYabin Cui const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
781*01826a49SYabin Cui return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);
782*01826a49SYabin Cui }
783*01826a49SYabin Cui
BITv05_skipBits(BITv05_DStream_t * bitD,U32 nbBits)784*01826a49SYabin Cui MEM_STATIC void BITv05_skipBits(BITv05_DStream_t* bitD, U32 nbBits)
785*01826a49SYabin Cui {
786*01826a49SYabin Cui bitD->bitsConsumed += nbBits;
787*01826a49SYabin Cui }
788*01826a49SYabin Cui
BITv05_readBits(BITv05_DStream_t * bitD,unsigned nbBits)789*01826a49SYabin Cui MEM_STATIC size_t BITv05_readBits(BITv05_DStream_t* bitD, unsigned nbBits)
790*01826a49SYabin Cui {
791*01826a49SYabin Cui size_t value = BITv05_lookBits(bitD, nbBits);
792*01826a49SYabin Cui BITv05_skipBits(bitD, nbBits);
793*01826a49SYabin Cui return value;
794*01826a49SYabin Cui }
795*01826a49SYabin Cui
796*01826a49SYabin Cui /*!BITv05_readBitsFast :
797*01826a49SYabin Cui * unsafe version; only works if nbBits >= 1 */
BITv05_readBitsFast(BITv05_DStream_t * bitD,unsigned nbBits)798*01826a49SYabin Cui MEM_STATIC size_t BITv05_readBitsFast(BITv05_DStream_t* bitD, unsigned nbBits)
799*01826a49SYabin Cui {
800*01826a49SYabin Cui size_t value = BITv05_lookBitsFast(bitD, nbBits);
801*01826a49SYabin Cui BITv05_skipBits(bitD, nbBits);
802*01826a49SYabin Cui return value;
803*01826a49SYabin Cui }
804*01826a49SYabin Cui
BITv05_reloadDStream(BITv05_DStream_t * bitD)805*01826a49SYabin Cui MEM_STATIC BITv05_DStream_status BITv05_reloadDStream(BITv05_DStream_t* bitD)
806*01826a49SYabin Cui {
807*01826a49SYabin Cui if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
808*01826a49SYabin Cui return BITv05_DStream_overflow;
809*01826a49SYabin Cui
810*01826a49SYabin Cui if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) {
811*01826a49SYabin Cui bitD->ptr -= bitD->bitsConsumed >> 3;
812*01826a49SYabin Cui bitD->bitsConsumed &= 7;
813*01826a49SYabin Cui bitD->bitContainer = MEM_readLEST(bitD->ptr);
814*01826a49SYabin Cui return BITv05_DStream_unfinished;
815*01826a49SYabin Cui }
816*01826a49SYabin Cui if (bitD->ptr == bitD->start) {
817*01826a49SYabin Cui if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BITv05_DStream_endOfBuffer;
818*01826a49SYabin Cui return BITv05_DStream_completed;
819*01826a49SYabin Cui }
820*01826a49SYabin Cui {
821*01826a49SYabin Cui U32 nbBytes = bitD->bitsConsumed >> 3;
822*01826a49SYabin Cui BITv05_DStream_status result = BITv05_DStream_unfinished;
823*01826a49SYabin Cui if (bitD->ptr - nbBytes < bitD->start) {
824*01826a49SYabin Cui nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
825*01826a49SYabin Cui result = BITv05_DStream_endOfBuffer;
826*01826a49SYabin Cui }
827*01826a49SYabin Cui bitD->ptr -= nbBytes;
828*01826a49SYabin Cui bitD->bitsConsumed -= nbBytes*8;
829*01826a49SYabin Cui bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */
830*01826a49SYabin Cui return result;
831*01826a49SYabin Cui }
832*01826a49SYabin Cui }
833*01826a49SYabin Cui
834*01826a49SYabin Cui /*! BITv05_endOfDStream
835*01826a49SYabin Cui * @return Tells if DStream has reached its exact end
836*01826a49SYabin Cui */
BITv05_endOfDStream(const BITv05_DStream_t * DStream)837*01826a49SYabin Cui MEM_STATIC unsigned BITv05_endOfDStream(const BITv05_DStream_t* DStream)
838*01826a49SYabin Cui {
839*01826a49SYabin Cui return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
840*01826a49SYabin Cui }
841*01826a49SYabin Cui
842*01826a49SYabin Cui #if defined (__cplusplus)
843*01826a49SYabin Cui }
844*01826a49SYabin Cui #endif
845*01826a49SYabin Cui
846*01826a49SYabin Cui #endif /* BITv05STREAM_H_MODULE */
847*01826a49SYabin Cui /* ******************************************************************
848*01826a49SYabin Cui FSEv05 : Finite State Entropy coder
849*01826a49SYabin Cui header file for static linking (only)
850*01826a49SYabin Cui Copyright (C) 2013-2015, Yann Collet
851*01826a49SYabin Cui
852*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
853*01826a49SYabin Cui
854*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
855*01826a49SYabin Cui modification, are permitted provided that the following conditions are
856*01826a49SYabin Cui met:
857*01826a49SYabin Cui
858*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
859*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
860*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
861*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
862*01826a49SYabin Cui in the documentation and/or other materials provided with the
863*01826a49SYabin Cui distribution.
864*01826a49SYabin Cui
865*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
866*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
867*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
868*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
869*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
870*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
871*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
872*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
873*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
874*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
875*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
876*01826a49SYabin Cui
877*01826a49SYabin Cui You can contact the author at :
878*01826a49SYabin Cui - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
879*01826a49SYabin Cui - Public forum : https://groups.google.com/forum/#!forum/lz4c
880*01826a49SYabin Cui ****************************************************************** */
881*01826a49SYabin Cui #ifndef FSEv05_STATIC_H
882*01826a49SYabin Cui #define FSEv05_STATIC_H
883*01826a49SYabin Cui
884*01826a49SYabin Cui #if defined (__cplusplus)
885*01826a49SYabin Cui extern "C" {
886*01826a49SYabin Cui #endif
887*01826a49SYabin Cui
888*01826a49SYabin Cui
889*01826a49SYabin Cui
890*01826a49SYabin Cui /* *****************************************
891*01826a49SYabin Cui * Static allocation
892*01826a49SYabin Cui *******************************************/
893*01826a49SYabin Cui /* It is possible to statically allocate FSEv05 CTable/DTable as a table of unsigned using below macros */
894*01826a49SYabin Cui #define FSEv05_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog))
895*01826a49SYabin Cui
896*01826a49SYabin Cui
897*01826a49SYabin Cui /* *****************************************
898*01826a49SYabin Cui * FSEv05 advanced API
899*01826a49SYabin Cui *******************************************/
900*01826a49SYabin Cui size_t FSEv05_buildDTable_raw (FSEv05_DTable* dt, unsigned nbBits);
901*01826a49SYabin Cui /* build a fake FSEv05_DTable, designed to read an uncompressed bitstream where each symbol uses nbBits */
902*01826a49SYabin Cui
903*01826a49SYabin Cui size_t FSEv05_buildDTable_rle (FSEv05_DTable* dt, unsigned char symbolValue);
904*01826a49SYabin Cui /* build a fake FSEv05_DTable, designed to always generate the same symbolValue */
905*01826a49SYabin Cui
906*01826a49SYabin Cui
907*01826a49SYabin Cui
908*01826a49SYabin Cui /* *****************************************
909*01826a49SYabin Cui * FSEv05 symbol decompression API
910*01826a49SYabin Cui *******************************************/
911*01826a49SYabin Cui typedef struct
912*01826a49SYabin Cui {
913*01826a49SYabin Cui size_t state;
914*01826a49SYabin Cui const void* table; /* precise table may vary, depending on U16 */
915*01826a49SYabin Cui } FSEv05_DState_t;
916*01826a49SYabin Cui
917*01826a49SYabin Cui
918*01826a49SYabin Cui static void FSEv05_initDState(FSEv05_DState_t* DStatePtr, BITv05_DStream_t* bitD, const FSEv05_DTable* dt);
919*01826a49SYabin Cui
920*01826a49SYabin Cui static unsigned char FSEv05_decodeSymbol(FSEv05_DState_t* DStatePtr, BITv05_DStream_t* bitD);
921*01826a49SYabin Cui
922*01826a49SYabin Cui static unsigned FSEv05_endOfDState(const FSEv05_DState_t* DStatePtr);
923*01826a49SYabin Cui
924*01826a49SYabin Cui
925*01826a49SYabin Cui
926*01826a49SYabin Cui /* *****************************************
927*01826a49SYabin Cui * FSEv05 unsafe API
928*01826a49SYabin Cui *******************************************/
929*01826a49SYabin Cui static unsigned char FSEv05_decodeSymbolFast(FSEv05_DState_t* DStatePtr, BITv05_DStream_t* bitD);
930*01826a49SYabin Cui /* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */
931*01826a49SYabin Cui
932*01826a49SYabin Cui
933*01826a49SYabin Cui /* *****************************************
934*01826a49SYabin Cui * Implementation of inlined functions
935*01826a49SYabin Cui *******************************************/
936*01826a49SYabin Cui /* decompression */
937*01826a49SYabin Cui
938*01826a49SYabin Cui typedef struct {
939*01826a49SYabin Cui U16 tableLog;
940*01826a49SYabin Cui U16 fastMode;
941*01826a49SYabin Cui } FSEv05_DTableHeader; /* sizeof U32 */
942*01826a49SYabin Cui
943*01826a49SYabin Cui typedef struct
944*01826a49SYabin Cui {
945*01826a49SYabin Cui unsigned short newState;
946*01826a49SYabin Cui unsigned char symbol;
947*01826a49SYabin Cui unsigned char nbBits;
948*01826a49SYabin Cui } FSEv05_decode_t; /* size == U32 */
949*01826a49SYabin Cui
FSEv05_initDState(FSEv05_DState_t * DStatePtr,BITv05_DStream_t * bitD,const FSEv05_DTable * dt)950*01826a49SYabin Cui MEM_STATIC void FSEv05_initDState(FSEv05_DState_t* DStatePtr, BITv05_DStream_t* bitD, const FSEv05_DTable* dt)
951*01826a49SYabin Cui {
952*01826a49SYabin Cui const void* ptr = dt;
953*01826a49SYabin Cui const FSEv05_DTableHeader* const DTableH = (const FSEv05_DTableHeader*)ptr;
954*01826a49SYabin Cui DStatePtr->state = BITv05_readBits(bitD, DTableH->tableLog);
955*01826a49SYabin Cui BITv05_reloadDStream(bitD);
956*01826a49SYabin Cui DStatePtr->table = dt + 1;
957*01826a49SYabin Cui }
958*01826a49SYabin Cui
FSEv05_peakSymbol(FSEv05_DState_t * DStatePtr)959*01826a49SYabin Cui MEM_STATIC BYTE FSEv05_peakSymbol(FSEv05_DState_t* DStatePtr)
960*01826a49SYabin Cui {
961*01826a49SYabin Cui const FSEv05_decode_t DInfo = ((const FSEv05_decode_t*)(DStatePtr->table))[DStatePtr->state];
962*01826a49SYabin Cui return DInfo.symbol;
963*01826a49SYabin Cui }
964*01826a49SYabin Cui
FSEv05_decodeSymbol(FSEv05_DState_t * DStatePtr,BITv05_DStream_t * bitD)965*01826a49SYabin Cui MEM_STATIC BYTE FSEv05_decodeSymbol(FSEv05_DState_t* DStatePtr, BITv05_DStream_t* bitD)
966*01826a49SYabin Cui {
967*01826a49SYabin Cui const FSEv05_decode_t DInfo = ((const FSEv05_decode_t*)(DStatePtr->table))[DStatePtr->state];
968*01826a49SYabin Cui const U32 nbBits = DInfo.nbBits;
969*01826a49SYabin Cui BYTE symbol = DInfo.symbol;
970*01826a49SYabin Cui size_t lowBits = BITv05_readBits(bitD, nbBits);
971*01826a49SYabin Cui
972*01826a49SYabin Cui DStatePtr->state = DInfo.newState + lowBits;
973*01826a49SYabin Cui return symbol;
974*01826a49SYabin Cui }
975*01826a49SYabin Cui
FSEv05_decodeSymbolFast(FSEv05_DState_t * DStatePtr,BITv05_DStream_t * bitD)976*01826a49SYabin Cui MEM_STATIC BYTE FSEv05_decodeSymbolFast(FSEv05_DState_t* DStatePtr, BITv05_DStream_t* bitD)
977*01826a49SYabin Cui {
978*01826a49SYabin Cui const FSEv05_decode_t DInfo = ((const FSEv05_decode_t*)(DStatePtr->table))[DStatePtr->state];
979*01826a49SYabin Cui const U32 nbBits = DInfo.nbBits;
980*01826a49SYabin Cui BYTE symbol = DInfo.symbol;
981*01826a49SYabin Cui size_t lowBits = BITv05_readBitsFast(bitD, nbBits);
982*01826a49SYabin Cui
983*01826a49SYabin Cui DStatePtr->state = DInfo.newState + lowBits;
984*01826a49SYabin Cui return symbol;
985*01826a49SYabin Cui }
986*01826a49SYabin Cui
FSEv05_endOfDState(const FSEv05_DState_t * DStatePtr)987*01826a49SYabin Cui MEM_STATIC unsigned FSEv05_endOfDState(const FSEv05_DState_t* DStatePtr)
988*01826a49SYabin Cui {
989*01826a49SYabin Cui return DStatePtr->state == 0;
990*01826a49SYabin Cui }
991*01826a49SYabin Cui
992*01826a49SYabin Cui
993*01826a49SYabin Cui #if defined (__cplusplus)
994*01826a49SYabin Cui }
995*01826a49SYabin Cui #endif
996*01826a49SYabin Cui
997*01826a49SYabin Cui #endif /* FSEv05_STATIC_H */
998*01826a49SYabin Cui /* ******************************************************************
999*01826a49SYabin Cui FSEv05 : Finite State Entropy coder
1000*01826a49SYabin Cui Copyright (C) 2013-2015, Yann Collet.
1001*01826a49SYabin Cui
1002*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1003*01826a49SYabin Cui
1004*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
1005*01826a49SYabin Cui modification, are permitted provided that the following conditions are
1006*01826a49SYabin Cui met:
1007*01826a49SYabin Cui
1008*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
1009*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
1010*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
1011*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
1012*01826a49SYabin Cui in the documentation and/or other materials provided with the
1013*01826a49SYabin Cui distribution.
1014*01826a49SYabin Cui
1015*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1016*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1017*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1018*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1019*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1020*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1021*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1022*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1023*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1024*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1025*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1026*01826a49SYabin Cui
1027*01826a49SYabin Cui You can contact the author at :
1028*01826a49SYabin Cui - FSEv05 source repository : https://github.com/Cyan4973/FiniteStateEntropy
1029*01826a49SYabin Cui - Public forum : https://groups.google.com/forum/#!forum/lz4c
1030*01826a49SYabin Cui ****************************************************************** */
1031*01826a49SYabin Cui
1032*01826a49SYabin Cui #ifndef FSEv05_COMMONDEFS_ONLY
1033*01826a49SYabin Cui
1034*01826a49SYabin Cui /* **************************************************************
1035*01826a49SYabin Cui * Tuning parameters
1036*01826a49SYabin Cui ****************************************************************/
1037*01826a49SYabin Cui /*!MEMORY_USAGE :
1038*01826a49SYabin Cui * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
1039*01826a49SYabin Cui * Increasing memory usage improves compression ratio
1040*01826a49SYabin Cui * Reduced memory usage can improve speed, due to cache effect
1041*01826a49SYabin Cui * Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */
1042*01826a49SYabin Cui #define FSEv05_MAX_MEMORY_USAGE 14
1043*01826a49SYabin Cui #define FSEv05_DEFAULT_MEMORY_USAGE 13
1044*01826a49SYabin Cui
1045*01826a49SYabin Cui /*!FSEv05_MAX_SYMBOL_VALUE :
1046*01826a49SYabin Cui * Maximum symbol value authorized.
1047*01826a49SYabin Cui * Required for proper stack allocation */
1048*01826a49SYabin Cui #define FSEv05_MAX_SYMBOL_VALUE 255
1049*01826a49SYabin Cui
1050*01826a49SYabin Cui
1051*01826a49SYabin Cui /* **************************************************************
1052*01826a49SYabin Cui * template functions type & suffix
1053*01826a49SYabin Cui ****************************************************************/
1054*01826a49SYabin Cui #define FSEv05_FUNCTION_TYPE BYTE
1055*01826a49SYabin Cui #define FSEv05_FUNCTION_EXTENSION
1056*01826a49SYabin Cui #define FSEv05_DECODE_TYPE FSEv05_decode_t
1057*01826a49SYabin Cui
1058*01826a49SYabin Cui
1059*01826a49SYabin Cui #endif /* !FSEv05_COMMONDEFS_ONLY */
1060*01826a49SYabin Cui
1061*01826a49SYabin Cui /* **************************************************************
1062*01826a49SYabin Cui * Compiler specifics
1063*01826a49SYabin Cui ****************************************************************/
1064*01826a49SYabin Cui #ifdef _MSC_VER /* Visual Studio */
1065*01826a49SYabin Cui # define FORCE_INLINE static __forceinline
1066*01826a49SYabin Cui # include <intrin.h> /* For Visual 2005 */
1067*01826a49SYabin Cui # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1068*01826a49SYabin Cui # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
1069*01826a49SYabin Cui #else
1070*01826a49SYabin Cui # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
1071*01826a49SYabin Cui # ifdef __GNUC__
1072*01826a49SYabin Cui # define FORCE_INLINE static inline __attribute__((always_inline))
1073*01826a49SYabin Cui # else
1074*01826a49SYabin Cui # define FORCE_INLINE static inline
1075*01826a49SYabin Cui # endif
1076*01826a49SYabin Cui # else
1077*01826a49SYabin Cui # define FORCE_INLINE static
1078*01826a49SYabin Cui # endif /* __STDC_VERSION__ */
1079*01826a49SYabin Cui #endif
1080*01826a49SYabin Cui
1081*01826a49SYabin Cui
1082*01826a49SYabin Cui /* **************************************************************
1083*01826a49SYabin Cui * Includes
1084*01826a49SYabin Cui ****************************************************************/
1085*01826a49SYabin Cui #include <stdlib.h> /* malloc, free, qsort */
1086*01826a49SYabin Cui #include <string.h> /* memcpy, memset */
1087*01826a49SYabin Cui #include <stdio.h> /* printf (debug) */
1088*01826a49SYabin Cui
1089*01826a49SYabin Cui
1090*01826a49SYabin Cui
1091*01826a49SYabin Cui /* ***************************************************************
1092*01826a49SYabin Cui * Constants
1093*01826a49SYabin Cui *****************************************************************/
1094*01826a49SYabin Cui #define FSEv05_MAX_TABLELOG (FSEv05_MAX_MEMORY_USAGE-2)
1095*01826a49SYabin Cui #define FSEv05_MAX_TABLESIZE (1U<<FSEv05_MAX_TABLELOG)
1096*01826a49SYabin Cui #define FSEv05_MAXTABLESIZE_MASK (FSEv05_MAX_TABLESIZE-1)
1097*01826a49SYabin Cui #define FSEv05_DEFAULT_TABLELOG (FSEv05_DEFAULT_MEMORY_USAGE-2)
1098*01826a49SYabin Cui #define FSEv05_MIN_TABLELOG 5
1099*01826a49SYabin Cui
1100*01826a49SYabin Cui #define FSEv05_TABLELOG_ABSOLUTE_MAX 15
1101*01826a49SYabin Cui #if FSEv05_MAX_TABLELOG > FSEv05_TABLELOG_ABSOLUTE_MAX
1102*01826a49SYabin Cui #error "FSEv05_MAX_TABLELOG > FSEv05_TABLELOG_ABSOLUTE_MAX is not supported"
1103*01826a49SYabin Cui #endif
1104*01826a49SYabin Cui
1105*01826a49SYabin Cui
1106*01826a49SYabin Cui /* **************************************************************
1107*01826a49SYabin Cui * Error Management
1108*01826a49SYabin Cui ****************************************************************/
1109*01826a49SYabin Cui #define FSEv05_STATIC_ASSERT(c) { enum { FSEv05_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
1110*01826a49SYabin Cui
1111*01826a49SYabin Cui
1112*01826a49SYabin Cui /* **************************************************************
1113*01826a49SYabin Cui * Complex types
1114*01826a49SYabin Cui ****************************************************************/
1115*01826a49SYabin Cui typedef unsigned DTable_max_t[FSEv05_DTABLE_SIZE_U32(FSEv05_MAX_TABLELOG)];
1116*01826a49SYabin Cui
1117*01826a49SYabin Cui
1118*01826a49SYabin Cui /* **************************************************************
1119*01826a49SYabin Cui * Templates
1120*01826a49SYabin Cui ****************************************************************/
1121*01826a49SYabin Cui /*
1122*01826a49SYabin Cui designed to be included
1123*01826a49SYabin Cui for type-specific functions (template emulation in C)
1124*01826a49SYabin Cui Objective is to write these functions only once, for improved maintenance
1125*01826a49SYabin Cui */
1126*01826a49SYabin Cui
1127*01826a49SYabin Cui /* safety checks */
1128*01826a49SYabin Cui #ifndef FSEv05_FUNCTION_EXTENSION
1129*01826a49SYabin Cui # error "FSEv05_FUNCTION_EXTENSION must be defined"
1130*01826a49SYabin Cui #endif
1131*01826a49SYabin Cui #ifndef FSEv05_FUNCTION_TYPE
1132*01826a49SYabin Cui # error "FSEv05_FUNCTION_TYPE must be defined"
1133*01826a49SYabin Cui #endif
1134*01826a49SYabin Cui
1135*01826a49SYabin Cui /* Function names */
1136*01826a49SYabin Cui #define FSEv05_CAT(X,Y) X##Y
1137*01826a49SYabin Cui #define FSEv05_FUNCTION_NAME(X,Y) FSEv05_CAT(X,Y)
1138*01826a49SYabin Cui #define FSEv05_TYPE_NAME(X,Y) FSEv05_CAT(X,Y)
1139*01826a49SYabin Cui
1140*01826a49SYabin Cui
1141*01826a49SYabin Cui /* Function templates */
FSEv05_tableStep(U32 tableSize)1142*01826a49SYabin Cui static U32 FSEv05_tableStep(U32 tableSize) { return (tableSize>>1) + (tableSize>>3) + 3; }
1143*01826a49SYabin Cui
1144*01826a49SYabin Cui
1145*01826a49SYabin Cui
FSEv05_createDTable(unsigned tableLog)1146*01826a49SYabin Cui FSEv05_DTable* FSEv05_createDTable (unsigned tableLog)
1147*01826a49SYabin Cui {
1148*01826a49SYabin Cui if (tableLog > FSEv05_TABLELOG_ABSOLUTE_MAX) tableLog = FSEv05_TABLELOG_ABSOLUTE_MAX;
1149*01826a49SYabin Cui return (FSEv05_DTable*)malloc( FSEv05_DTABLE_SIZE_U32(tableLog) * sizeof (U32) );
1150*01826a49SYabin Cui }
1151*01826a49SYabin Cui
FSEv05_freeDTable(FSEv05_DTable * dt)1152*01826a49SYabin Cui void FSEv05_freeDTable (FSEv05_DTable* dt)
1153*01826a49SYabin Cui {
1154*01826a49SYabin Cui free(dt);
1155*01826a49SYabin Cui }
1156*01826a49SYabin Cui
FSEv05_buildDTable(FSEv05_DTable * dt,const short * normalizedCounter,unsigned maxSymbolValue,unsigned tableLog)1157*01826a49SYabin Cui size_t FSEv05_buildDTable(FSEv05_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
1158*01826a49SYabin Cui {
1159*01826a49SYabin Cui FSEv05_DTableHeader DTableH;
1160*01826a49SYabin Cui void* const tdPtr = dt+1; /* because dt is unsigned, 32-bits aligned on 32-bits */
1161*01826a49SYabin Cui FSEv05_DECODE_TYPE* const tableDecode = (FSEv05_DECODE_TYPE*) (tdPtr);
1162*01826a49SYabin Cui const U32 tableSize = 1 << tableLog;
1163*01826a49SYabin Cui const U32 tableMask = tableSize-1;
1164*01826a49SYabin Cui const U32 step = FSEv05_tableStep(tableSize);
1165*01826a49SYabin Cui U16 symbolNext[FSEv05_MAX_SYMBOL_VALUE+1];
1166*01826a49SYabin Cui U32 position = 0;
1167*01826a49SYabin Cui U32 highThreshold = tableSize-1;
1168*01826a49SYabin Cui const S16 largeLimit= (S16)(1 << (tableLog-1));
1169*01826a49SYabin Cui U32 noLarge = 1;
1170*01826a49SYabin Cui U32 s;
1171*01826a49SYabin Cui
1172*01826a49SYabin Cui /* Sanity Checks */
1173*01826a49SYabin Cui if (maxSymbolValue > FSEv05_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
1174*01826a49SYabin Cui if (tableLog > FSEv05_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
1175*01826a49SYabin Cui
1176*01826a49SYabin Cui /* Init, lay down lowprob symbols */
1177*01826a49SYabin Cui memset(tableDecode, 0, sizeof(FSEv05_FUNCTION_TYPE) * (maxSymbolValue+1) ); /* useless init, but keep static analyzer happy, and we don't need to performance optimize legacy decoders */
1178*01826a49SYabin Cui DTableH.tableLog = (U16)tableLog;
1179*01826a49SYabin Cui for (s=0; s<=maxSymbolValue; s++) {
1180*01826a49SYabin Cui if (normalizedCounter[s]==-1) {
1181*01826a49SYabin Cui tableDecode[highThreshold--].symbol = (FSEv05_FUNCTION_TYPE)s;
1182*01826a49SYabin Cui symbolNext[s] = 1;
1183*01826a49SYabin Cui } else {
1184*01826a49SYabin Cui if (normalizedCounter[s] >= largeLimit) noLarge=0;
1185*01826a49SYabin Cui symbolNext[s] = normalizedCounter[s];
1186*01826a49SYabin Cui } }
1187*01826a49SYabin Cui
1188*01826a49SYabin Cui /* Spread symbols */
1189*01826a49SYabin Cui for (s=0; s<=maxSymbolValue; s++) {
1190*01826a49SYabin Cui int i;
1191*01826a49SYabin Cui for (i=0; i<normalizedCounter[s]; i++) {
1192*01826a49SYabin Cui tableDecode[position].symbol = (FSEv05_FUNCTION_TYPE)s;
1193*01826a49SYabin Cui position = (position + step) & tableMask;
1194*01826a49SYabin Cui while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
1195*01826a49SYabin Cui } }
1196*01826a49SYabin Cui
1197*01826a49SYabin Cui if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
1198*01826a49SYabin Cui
1199*01826a49SYabin Cui /* Build Decoding table */
1200*01826a49SYabin Cui {
1201*01826a49SYabin Cui U32 i;
1202*01826a49SYabin Cui for (i=0; i<tableSize; i++) {
1203*01826a49SYabin Cui FSEv05_FUNCTION_TYPE symbol = (FSEv05_FUNCTION_TYPE)(tableDecode[i].symbol);
1204*01826a49SYabin Cui U16 nextState = symbolNext[symbol]++;
1205*01826a49SYabin Cui tableDecode[i].nbBits = (BYTE) (tableLog - BITv05_highbit32 ((U32)nextState) );
1206*01826a49SYabin Cui tableDecode[i].newState = (U16) ( (nextState << tableDecode[i].nbBits) - tableSize);
1207*01826a49SYabin Cui } }
1208*01826a49SYabin Cui
1209*01826a49SYabin Cui DTableH.fastMode = (U16)noLarge;
1210*01826a49SYabin Cui memcpy(dt, &DTableH, sizeof(DTableH));
1211*01826a49SYabin Cui return 0;
1212*01826a49SYabin Cui }
1213*01826a49SYabin Cui
1214*01826a49SYabin Cui
1215*01826a49SYabin Cui #ifndef FSEv05_COMMONDEFS_ONLY
1216*01826a49SYabin Cui /*-****************************************
1217*01826a49SYabin Cui * FSEv05 helper functions
1218*01826a49SYabin Cui ******************************************/
FSEv05_isError(size_t code)1219*01826a49SYabin Cui unsigned FSEv05_isError(size_t code) { return ERR_isError(code); }
1220*01826a49SYabin Cui
FSEv05_getErrorName(size_t code)1221*01826a49SYabin Cui const char* FSEv05_getErrorName(size_t code) { return ERR_getErrorName(code); }
1222*01826a49SYabin Cui
1223*01826a49SYabin Cui
1224*01826a49SYabin Cui /*-**************************************************************
1225*01826a49SYabin Cui * FSEv05 NCount encoding-decoding
1226*01826a49SYabin Cui ****************************************************************/
FSEv05_abs(short a)1227*01826a49SYabin Cui static short FSEv05_abs(short a) { return a<0 ? -a : a; }
1228*01826a49SYabin Cui
1229*01826a49SYabin Cui
FSEv05_readNCount(short * normalizedCounter,unsigned * maxSVPtr,unsigned * tableLogPtr,const void * headerBuffer,size_t hbSize)1230*01826a49SYabin Cui size_t FSEv05_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
1231*01826a49SYabin Cui const void* headerBuffer, size_t hbSize)
1232*01826a49SYabin Cui {
1233*01826a49SYabin Cui const BYTE* const istart = (const BYTE*) headerBuffer;
1234*01826a49SYabin Cui const BYTE* const iend = istart + hbSize;
1235*01826a49SYabin Cui const BYTE* ip = istart;
1236*01826a49SYabin Cui int nbBits;
1237*01826a49SYabin Cui int remaining;
1238*01826a49SYabin Cui int threshold;
1239*01826a49SYabin Cui U32 bitStream;
1240*01826a49SYabin Cui int bitCount;
1241*01826a49SYabin Cui unsigned charnum = 0;
1242*01826a49SYabin Cui int previous0 = 0;
1243*01826a49SYabin Cui
1244*01826a49SYabin Cui if (hbSize < 4) return ERROR(srcSize_wrong);
1245*01826a49SYabin Cui bitStream = MEM_readLE32(ip);
1246*01826a49SYabin Cui nbBits = (bitStream & 0xF) + FSEv05_MIN_TABLELOG; /* extract tableLog */
1247*01826a49SYabin Cui if (nbBits > FSEv05_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
1248*01826a49SYabin Cui bitStream >>= 4;
1249*01826a49SYabin Cui bitCount = 4;
1250*01826a49SYabin Cui *tableLogPtr = nbBits;
1251*01826a49SYabin Cui remaining = (1<<nbBits)+1;
1252*01826a49SYabin Cui threshold = 1<<nbBits;
1253*01826a49SYabin Cui nbBits++;
1254*01826a49SYabin Cui
1255*01826a49SYabin Cui while ((remaining>1) && (charnum<=*maxSVPtr)) {
1256*01826a49SYabin Cui if (previous0) {
1257*01826a49SYabin Cui unsigned n0 = charnum;
1258*01826a49SYabin Cui while ((bitStream & 0xFFFF) == 0xFFFF) {
1259*01826a49SYabin Cui n0+=24;
1260*01826a49SYabin Cui if (ip < iend-5) {
1261*01826a49SYabin Cui ip+=2;
1262*01826a49SYabin Cui bitStream = MEM_readLE32(ip) >> bitCount;
1263*01826a49SYabin Cui } else {
1264*01826a49SYabin Cui bitStream >>= 16;
1265*01826a49SYabin Cui bitCount+=16;
1266*01826a49SYabin Cui } }
1267*01826a49SYabin Cui while ((bitStream & 3) == 3) {
1268*01826a49SYabin Cui n0+=3;
1269*01826a49SYabin Cui bitStream>>=2;
1270*01826a49SYabin Cui bitCount+=2;
1271*01826a49SYabin Cui }
1272*01826a49SYabin Cui n0 += bitStream & 3;
1273*01826a49SYabin Cui bitCount += 2;
1274*01826a49SYabin Cui if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
1275*01826a49SYabin Cui while (charnum < n0) normalizedCounter[charnum++] = 0;
1276*01826a49SYabin Cui if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
1277*01826a49SYabin Cui ip += bitCount>>3;
1278*01826a49SYabin Cui bitCount &= 7;
1279*01826a49SYabin Cui bitStream = MEM_readLE32(ip) >> bitCount;
1280*01826a49SYabin Cui }
1281*01826a49SYabin Cui else
1282*01826a49SYabin Cui bitStream >>= 2;
1283*01826a49SYabin Cui }
1284*01826a49SYabin Cui {
1285*01826a49SYabin Cui const short max = (short)((2*threshold-1)-remaining);
1286*01826a49SYabin Cui short count;
1287*01826a49SYabin Cui
1288*01826a49SYabin Cui if ((bitStream & (threshold-1)) < (U32)max) {
1289*01826a49SYabin Cui count = (short)(bitStream & (threshold-1));
1290*01826a49SYabin Cui bitCount += nbBits-1;
1291*01826a49SYabin Cui } else {
1292*01826a49SYabin Cui count = (short)(bitStream & (2*threshold-1));
1293*01826a49SYabin Cui if (count >= threshold) count -= max;
1294*01826a49SYabin Cui bitCount += nbBits;
1295*01826a49SYabin Cui }
1296*01826a49SYabin Cui
1297*01826a49SYabin Cui count--; /* extra accuracy */
1298*01826a49SYabin Cui remaining -= FSEv05_abs(count);
1299*01826a49SYabin Cui normalizedCounter[charnum++] = count;
1300*01826a49SYabin Cui previous0 = !count;
1301*01826a49SYabin Cui while (remaining < threshold) {
1302*01826a49SYabin Cui nbBits--;
1303*01826a49SYabin Cui threshold >>= 1;
1304*01826a49SYabin Cui }
1305*01826a49SYabin Cui
1306*01826a49SYabin Cui if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
1307*01826a49SYabin Cui ip += bitCount>>3;
1308*01826a49SYabin Cui bitCount &= 7;
1309*01826a49SYabin Cui } else {
1310*01826a49SYabin Cui bitCount -= (int)(8 * (iend - 4 - ip));
1311*01826a49SYabin Cui ip = iend - 4;
1312*01826a49SYabin Cui }
1313*01826a49SYabin Cui bitStream = MEM_readLE32(ip) >> (bitCount & 31);
1314*01826a49SYabin Cui } }
1315*01826a49SYabin Cui if (remaining != 1) return ERROR(GENERIC);
1316*01826a49SYabin Cui *maxSVPtr = charnum-1;
1317*01826a49SYabin Cui
1318*01826a49SYabin Cui ip += (bitCount+7)>>3;
1319*01826a49SYabin Cui if ((size_t)(ip-istart) > hbSize) return ERROR(srcSize_wrong);
1320*01826a49SYabin Cui return ip-istart;
1321*01826a49SYabin Cui }
1322*01826a49SYabin Cui
1323*01826a49SYabin Cui
1324*01826a49SYabin Cui
1325*01826a49SYabin Cui /*-*******************************************************
1326*01826a49SYabin Cui * Decompression (Byte symbols)
1327*01826a49SYabin Cui *********************************************************/
FSEv05_buildDTable_rle(FSEv05_DTable * dt,BYTE symbolValue)1328*01826a49SYabin Cui size_t FSEv05_buildDTable_rle (FSEv05_DTable* dt, BYTE symbolValue)
1329*01826a49SYabin Cui {
1330*01826a49SYabin Cui void* ptr = dt;
1331*01826a49SYabin Cui FSEv05_DTableHeader* const DTableH = (FSEv05_DTableHeader*)ptr;
1332*01826a49SYabin Cui void* dPtr = dt + 1;
1333*01826a49SYabin Cui FSEv05_decode_t* const cell = (FSEv05_decode_t*)dPtr;
1334*01826a49SYabin Cui
1335*01826a49SYabin Cui DTableH->tableLog = 0;
1336*01826a49SYabin Cui DTableH->fastMode = 0;
1337*01826a49SYabin Cui
1338*01826a49SYabin Cui cell->newState = 0;
1339*01826a49SYabin Cui cell->symbol = symbolValue;
1340*01826a49SYabin Cui cell->nbBits = 0;
1341*01826a49SYabin Cui
1342*01826a49SYabin Cui return 0;
1343*01826a49SYabin Cui }
1344*01826a49SYabin Cui
1345*01826a49SYabin Cui
FSEv05_buildDTable_raw(FSEv05_DTable * dt,unsigned nbBits)1346*01826a49SYabin Cui size_t FSEv05_buildDTable_raw (FSEv05_DTable* dt, unsigned nbBits)
1347*01826a49SYabin Cui {
1348*01826a49SYabin Cui void* ptr = dt;
1349*01826a49SYabin Cui FSEv05_DTableHeader* const DTableH = (FSEv05_DTableHeader*)ptr;
1350*01826a49SYabin Cui void* dPtr = dt + 1;
1351*01826a49SYabin Cui FSEv05_decode_t* const dinfo = (FSEv05_decode_t*)dPtr;
1352*01826a49SYabin Cui const unsigned tableSize = 1 << nbBits;
1353*01826a49SYabin Cui const unsigned tableMask = tableSize - 1;
1354*01826a49SYabin Cui const unsigned maxSymbolValue = tableMask;
1355*01826a49SYabin Cui unsigned s;
1356*01826a49SYabin Cui
1357*01826a49SYabin Cui /* Sanity checks */
1358*01826a49SYabin Cui if (nbBits < 1) return ERROR(GENERIC); /* min size */
1359*01826a49SYabin Cui
1360*01826a49SYabin Cui /* Build Decoding Table */
1361*01826a49SYabin Cui DTableH->tableLog = (U16)nbBits;
1362*01826a49SYabin Cui DTableH->fastMode = 1;
1363*01826a49SYabin Cui for (s=0; s<=maxSymbolValue; s++) {
1364*01826a49SYabin Cui dinfo[s].newState = 0;
1365*01826a49SYabin Cui dinfo[s].symbol = (BYTE)s;
1366*01826a49SYabin Cui dinfo[s].nbBits = (BYTE)nbBits;
1367*01826a49SYabin Cui }
1368*01826a49SYabin Cui
1369*01826a49SYabin Cui return 0;
1370*01826a49SYabin Cui }
1371*01826a49SYabin Cui
FSEv05_decompress_usingDTable_generic(void * dst,size_t maxDstSize,const void * cSrc,size_t cSrcSize,const FSEv05_DTable * dt,const unsigned fast)1372*01826a49SYabin Cui FORCE_INLINE size_t FSEv05_decompress_usingDTable_generic(
1373*01826a49SYabin Cui void* dst, size_t maxDstSize,
1374*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1375*01826a49SYabin Cui const FSEv05_DTable* dt, const unsigned fast)
1376*01826a49SYabin Cui {
1377*01826a49SYabin Cui BYTE* const ostart = (BYTE*) dst;
1378*01826a49SYabin Cui BYTE* op = ostart;
1379*01826a49SYabin Cui BYTE* const omax = op + maxDstSize;
1380*01826a49SYabin Cui BYTE* const olimit = omax-3;
1381*01826a49SYabin Cui
1382*01826a49SYabin Cui BITv05_DStream_t bitD;
1383*01826a49SYabin Cui FSEv05_DState_t state1;
1384*01826a49SYabin Cui FSEv05_DState_t state2;
1385*01826a49SYabin Cui size_t errorCode;
1386*01826a49SYabin Cui
1387*01826a49SYabin Cui /* Init */
1388*01826a49SYabin Cui errorCode = BITv05_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */
1389*01826a49SYabin Cui if (FSEv05_isError(errorCode)) return errorCode;
1390*01826a49SYabin Cui
1391*01826a49SYabin Cui FSEv05_initDState(&state1, &bitD, dt);
1392*01826a49SYabin Cui FSEv05_initDState(&state2, &bitD, dt);
1393*01826a49SYabin Cui
1394*01826a49SYabin Cui #define FSEv05_GETSYMBOL(statePtr) fast ? FSEv05_decodeSymbolFast(statePtr, &bitD) : FSEv05_decodeSymbol(statePtr, &bitD)
1395*01826a49SYabin Cui
1396*01826a49SYabin Cui /* 4 symbols per loop */
1397*01826a49SYabin Cui for ( ; (BITv05_reloadDStream(&bitD)==BITv05_DStream_unfinished) && (op<olimit) ; op+=4) {
1398*01826a49SYabin Cui op[0] = FSEv05_GETSYMBOL(&state1);
1399*01826a49SYabin Cui
1400*01826a49SYabin Cui if (FSEv05_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1401*01826a49SYabin Cui BITv05_reloadDStream(&bitD);
1402*01826a49SYabin Cui
1403*01826a49SYabin Cui op[1] = FSEv05_GETSYMBOL(&state2);
1404*01826a49SYabin Cui
1405*01826a49SYabin Cui if (FSEv05_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1406*01826a49SYabin Cui { if (BITv05_reloadDStream(&bitD) > BITv05_DStream_unfinished) { op+=2; break; } }
1407*01826a49SYabin Cui
1408*01826a49SYabin Cui op[2] = FSEv05_GETSYMBOL(&state1);
1409*01826a49SYabin Cui
1410*01826a49SYabin Cui if (FSEv05_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1411*01826a49SYabin Cui BITv05_reloadDStream(&bitD);
1412*01826a49SYabin Cui
1413*01826a49SYabin Cui op[3] = FSEv05_GETSYMBOL(&state2);
1414*01826a49SYabin Cui }
1415*01826a49SYabin Cui
1416*01826a49SYabin Cui /* tail */
1417*01826a49SYabin Cui /* note : BITv05_reloadDStream(&bitD) >= FSEv05_DStream_partiallyFilled; Ends at exactly BITv05_DStream_completed */
1418*01826a49SYabin Cui while (1) {
1419*01826a49SYabin Cui if ( (BITv05_reloadDStream(&bitD)>BITv05_DStream_completed) || (op==omax) || (BITv05_endOfDStream(&bitD) && (fast || FSEv05_endOfDState(&state1))) )
1420*01826a49SYabin Cui break;
1421*01826a49SYabin Cui
1422*01826a49SYabin Cui *op++ = FSEv05_GETSYMBOL(&state1);
1423*01826a49SYabin Cui
1424*01826a49SYabin Cui if ( (BITv05_reloadDStream(&bitD)>BITv05_DStream_completed) || (op==omax) || (BITv05_endOfDStream(&bitD) && (fast || FSEv05_endOfDState(&state2))) )
1425*01826a49SYabin Cui break;
1426*01826a49SYabin Cui
1427*01826a49SYabin Cui *op++ = FSEv05_GETSYMBOL(&state2);
1428*01826a49SYabin Cui }
1429*01826a49SYabin Cui
1430*01826a49SYabin Cui /* end ? */
1431*01826a49SYabin Cui if (BITv05_endOfDStream(&bitD) && FSEv05_endOfDState(&state1) && FSEv05_endOfDState(&state2))
1432*01826a49SYabin Cui return op-ostart;
1433*01826a49SYabin Cui
1434*01826a49SYabin Cui if (op==omax) return ERROR(dstSize_tooSmall); /* dst buffer is full, but cSrc unfinished */
1435*01826a49SYabin Cui
1436*01826a49SYabin Cui return ERROR(corruption_detected);
1437*01826a49SYabin Cui }
1438*01826a49SYabin Cui
1439*01826a49SYabin Cui
FSEv05_decompress_usingDTable(void * dst,size_t originalSize,const void * cSrc,size_t cSrcSize,const FSEv05_DTable * dt)1440*01826a49SYabin Cui size_t FSEv05_decompress_usingDTable(void* dst, size_t originalSize,
1441*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1442*01826a49SYabin Cui const FSEv05_DTable* dt)
1443*01826a49SYabin Cui {
1444*01826a49SYabin Cui const void* ptr = dt;
1445*01826a49SYabin Cui const FSEv05_DTableHeader* DTableH = (const FSEv05_DTableHeader*)ptr;
1446*01826a49SYabin Cui const U32 fastMode = DTableH->fastMode;
1447*01826a49SYabin Cui
1448*01826a49SYabin Cui /* select fast mode (static) */
1449*01826a49SYabin Cui if (fastMode) return FSEv05_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
1450*01826a49SYabin Cui return FSEv05_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
1451*01826a49SYabin Cui }
1452*01826a49SYabin Cui
1453*01826a49SYabin Cui
FSEv05_decompress(void * dst,size_t maxDstSize,const void * cSrc,size_t cSrcSize)1454*01826a49SYabin Cui size_t FSEv05_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize)
1455*01826a49SYabin Cui {
1456*01826a49SYabin Cui const BYTE* const istart = (const BYTE*)cSrc;
1457*01826a49SYabin Cui const BYTE* ip = istart;
1458*01826a49SYabin Cui short counting[FSEv05_MAX_SYMBOL_VALUE+1];
1459*01826a49SYabin Cui DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
1460*01826a49SYabin Cui unsigned tableLog;
1461*01826a49SYabin Cui unsigned maxSymbolValue = FSEv05_MAX_SYMBOL_VALUE;
1462*01826a49SYabin Cui size_t errorCode;
1463*01826a49SYabin Cui
1464*01826a49SYabin Cui if (cSrcSize<2) return ERROR(srcSize_wrong); /* too small input size */
1465*01826a49SYabin Cui
1466*01826a49SYabin Cui /* normal FSEv05 decoding mode */
1467*01826a49SYabin Cui errorCode = FSEv05_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
1468*01826a49SYabin Cui if (FSEv05_isError(errorCode)) return errorCode;
1469*01826a49SYabin Cui if (errorCode >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */
1470*01826a49SYabin Cui ip += errorCode;
1471*01826a49SYabin Cui cSrcSize -= errorCode;
1472*01826a49SYabin Cui
1473*01826a49SYabin Cui errorCode = FSEv05_buildDTable (dt, counting, maxSymbolValue, tableLog);
1474*01826a49SYabin Cui if (FSEv05_isError(errorCode)) return errorCode;
1475*01826a49SYabin Cui
1476*01826a49SYabin Cui /* always return, even if it is an error code */
1477*01826a49SYabin Cui return FSEv05_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt);
1478*01826a49SYabin Cui }
1479*01826a49SYabin Cui
1480*01826a49SYabin Cui
1481*01826a49SYabin Cui
1482*01826a49SYabin Cui #endif /* FSEv05_COMMONDEFS_ONLY */
1483*01826a49SYabin Cui /* ******************************************************************
1484*01826a49SYabin Cui Huff0 : Huffman coder, part of New Generation Entropy library
1485*01826a49SYabin Cui header file
1486*01826a49SYabin Cui Copyright (C) 2013-2016, Yann Collet.
1487*01826a49SYabin Cui
1488*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1489*01826a49SYabin Cui
1490*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
1491*01826a49SYabin Cui modification, are permitted provided that the following conditions are
1492*01826a49SYabin Cui met:
1493*01826a49SYabin Cui
1494*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
1495*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
1496*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
1497*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
1498*01826a49SYabin Cui in the documentation and/or other materials provided with the
1499*01826a49SYabin Cui distribution.
1500*01826a49SYabin Cui
1501*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1502*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1503*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1504*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1505*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1506*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1507*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1508*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1509*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1510*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1511*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1512*01826a49SYabin Cui
1513*01826a49SYabin Cui You can contact the author at :
1514*01826a49SYabin Cui - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
1515*01826a49SYabin Cui ****************************************************************** */
1516*01826a49SYabin Cui #ifndef HUFF0_H
1517*01826a49SYabin Cui #define HUFF0_H
1518*01826a49SYabin Cui
1519*01826a49SYabin Cui #if defined (__cplusplus)
1520*01826a49SYabin Cui extern "C" {
1521*01826a49SYabin Cui #endif
1522*01826a49SYabin Cui
1523*01826a49SYabin Cui
1524*01826a49SYabin Cui
1525*01826a49SYabin Cui /* ****************************************
1526*01826a49SYabin Cui * Huff0 simple functions
1527*01826a49SYabin Cui ******************************************/
1528*01826a49SYabin Cui size_t HUFv05_decompress(void* dst, size_t dstSize,
1529*01826a49SYabin Cui const void* cSrc, size_t cSrcSize);
1530*01826a49SYabin Cui /*!
1531*01826a49SYabin Cui HUFv05_decompress():
1532*01826a49SYabin Cui Decompress Huff0 data from buffer 'cSrc', of size 'cSrcSize',
1533*01826a49SYabin Cui into already allocated destination buffer 'dst', of size 'dstSize'.
1534*01826a49SYabin Cui @dstSize : must be the **exact** size of original (uncompressed) data.
1535*01826a49SYabin Cui Note : in contrast with FSEv05, HUFv05_decompress can regenerate
1536*01826a49SYabin Cui RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data,
1537*01826a49SYabin Cui because it knows size to regenerate.
1538*01826a49SYabin Cui @return : size of regenerated data (== dstSize)
1539*01826a49SYabin Cui or an error code, which can be tested using HUFv05_isError()
1540*01826a49SYabin Cui */
1541*01826a49SYabin Cui
1542*01826a49SYabin Cui
1543*01826a49SYabin Cui /* ****************************************
1544*01826a49SYabin Cui * Tool functions
1545*01826a49SYabin Cui ******************************************/
1546*01826a49SYabin Cui /* Error Management */
1547*01826a49SYabin Cui unsigned HUFv05_isError(size_t code); /* tells if a return value is an error code */
1548*01826a49SYabin Cui const char* HUFv05_getErrorName(size_t code); /* provides error code string (useful for debugging) */
1549*01826a49SYabin Cui
1550*01826a49SYabin Cui
1551*01826a49SYabin Cui #if defined (__cplusplus)
1552*01826a49SYabin Cui }
1553*01826a49SYabin Cui #endif
1554*01826a49SYabin Cui
1555*01826a49SYabin Cui #endif /* HUF0_H */
1556*01826a49SYabin Cui /* ******************************************************************
1557*01826a49SYabin Cui Huff0 : Huffman codec, part of New Generation Entropy library
1558*01826a49SYabin Cui header file, for static linking only
1559*01826a49SYabin Cui Copyright (C) 2013-2016, Yann Collet
1560*01826a49SYabin Cui
1561*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1562*01826a49SYabin Cui
1563*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
1564*01826a49SYabin Cui modification, are permitted provided that the following conditions are
1565*01826a49SYabin Cui met:
1566*01826a49SYabin Cui
1567*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
1568*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
1569*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
1570*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
1571*01826a49SYabin Cui in the documentation and/or other materials provided with the
1572*01826a49SYabin Cui distribution.
1573*01826a49SYabin Cui
1574*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1575*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1576*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1577*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1578*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1579*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1580*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1581*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1582*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1583*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1584*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1585*01826a49SYabin Cui
1586*01826a49SYabin Cui You can contact the author at :
1587*01826a49SYabin Cui - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
1588*01826a49SYabin Cui ****************************************************************** */
1589*01826a49SYabin Cui #ifndef HUF0_STATIC_H
1590*01826a49SYabin Cui #define HUF0_STATIC_H
1591*01826a49SYabin Cui
1592*01826a49SYabin Cui #if defined (__cplusplus)
1593*01826a49SYabin Cui extern "C" {
1594*01826a49SYabin Cui #endif
1595*01826a49SYabin Cui
1596*01826a49SYabin Cui
1597*01826a49SYabin Cui
1598*01826a49SYabin Cui /* ****************************************
1599*01826a49SYabin Cui * Static allocation
1600*01826a49SYabin Cui ******************************************/
1601*01826a49SYabin Cui /* static allocation of Huff0's DTable */
1602*01826a49SYabin Cui #define HUFv05_DTABLE_SIZE(maxTableLog) (1 + (1<<maxTableLog))
1603*01826a49SYabin Cui #define HUFv05_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
1604*01826a49SYabin Cui unsigned short DTable[HUFv05_DTABLE_SIZE(maxTableLog)] = { maxTableLog }
1605*01826a49SYabin Cui #define HUFv05_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \
1606*01826a49SYabin Cui unsigned int DTable[HUFv05_DTABLE_SIZE(maxTableLog)] = { maxTableLog }
1607*01826a49SYabin Cui #define HUFv05_CREATE_STATIC_DTABLEX6(DTable, maxTableLog) \
1608*01826a49SYabin Cui unsigned int DTable[HUFv05_DTABLE_SIZE(maxTableLog) * 3 / 2] = { maxTableLog }
1609*01826a49SYabin Cui
1610*01826a49SYabin Cui
1611*01826a49SYabin Cui /* ****************************************
1612*01826a49SYabin Cui * Advanced decompression functions
1613*01826a49SYabin Cui ******************************************/
1614*01826a49SYabin Cui size_t HUFv05_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
1615*01826a49SYabin Cui size_t HUFv05_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbols decoder */
1616*01826a49SYabin Cui
1617*01826a49SYabin Cui
1618*01826a49SYabin Cui /* ****************************************
1619*01826a49SYabin Cui * Huff0 detailed API
1620*01826a49SYabin Cui ******************************************/
1621*01826a49SYabin Cui /*!
1622*01826a49SYabin Cui HUFv05_decompress() does the following:
1623*01826a49SYabin Cui 1. select the decompression algorithm (X2, X4, X6) based on pre-computed heuristics
1624*01826a49SYabin Cui 2. build Huffman table from save, using HUFv05_readDTableXn()
1625*01826a49SYabin Cui 3. decode 1 or 4 segments in parallel using HUFv05_decompressSXn_usingDTable
1626*01826a49SYabin Cui */
1627*01826a49SYabin Cui size_t HUFv05_readDTableX2 (unsigned short* DTable, const void* src, size_t srcSize);
1628*01826a49SYabin Cui size_t HUFv05_readDTableX4 (unsigned* DTable, const void* src, size_t srcSize);
1629*01826a49SYabin Cui
1630*01826a49SYabin Cui size_t HUFv05_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const unsigned short* DTable);
1631*01826a49SYabin Cui size_t HUFv05_decompress4X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const unsigned* DTable);
1632*01826a49SYabin Cui
1633*01826a49SYabin Cui
1634*01826a49SYabin Cui /* single stream variants */
1635*01826a49SYabin Cui
1636*01826a49SYabin Cui size_t HUFv05_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
1637*01826a49SYabin Cui size_t HUFv05_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */
1638*01826a49SYabin Cui
1639*01826a49SYabin Cui size_t HUFv05_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const unsigned short* DTable);
1640*01826a49SYabin Cui size_t HUFv05_decompress1X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const unsigned* DTable);
1641*01826a49SYabin Cui
1642*01826a49SYabin Cui
1643*01826a49SYabin Cui
1644*01826a49SYabin Cui #if defined (__cplusplus)
1645*01826a49SYabin Cui }
1646*01826a49SYabin Cui #endif
1647*01826a49SYabin Cui
1648*01826a49SYabin Cui #endif /* HUF0_STATIC_H */
1649*01826a49SYabin Cui /* ******************************************************************
1650*01826a49SYabin Cui Huff0 : Huffman coder, part of New Generation Entropy library
1651*01826a49SYabin Cui Copyright (C) 2013-2015, Yann Collet.
1652*01826a49SYabin Cui
1653*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1654*01826a49SYabin Cui
1655*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
1656*01826a49SYabin Cui modification, are permitted provided that the following conditions are
1657*01826a49SYabin Cui met:
1658*01826a49SYabin Cui
1659*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
1660*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
1661*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
1662*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
1663*01826a49SYabin Cui in the documentation and/or other materials provided with the
1664*01826a49SYabin Cui distribution.
1665*01826a49SYabin Cui
1666*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1667*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1668*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1669*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1670*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1671*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1672*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1673*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1674*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1675*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1676*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1677*01826a49SYabin Cui
1678*01826a49SYabin Cui You can contact the author at :
1679*01826a49SYabin Cui - FSEv05+Huff0 source repository : https://github.com/Cyan4973/FiniteStateEntropy
1680*01826a49SYabin Cui - Public forum : https://groups.google.com/forum/#!forum/lz4c
1681*01826a49SYabin Cui ****************************************************************** */
1682*01826a49SYabin Cui
1683*01826a49SYabin Cui /* **************************************************************
1684*01826a49SYabin Cui * Compiler specifics
1685*01826a49SYabin Cui ****************************************************************/
1686*01826a49SYabin Cui #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
1687*01826a49SYabin Cui /* inline is defined */
1688*01826a49SYabin Cui #elif defined(_MSC_VER)
1689*01826a49SYabin Cui # define inline __inline
1690*01826a49SYabin Cui #else
1691*01826a49SYabin Cui # define inline /* disable inline */
1692*01826a49SYabin Cui #endif
1693*01826a49SYabin Cui
1694*01826a49SYabin Cui
1695*01826a49SYabin Cui #ifdef _MSC_VER /* Visual Studio */
1696*01826a49SYabin Cui # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1697*01826a49SYabin Cui #endif
1698*01826a49SYabin Cui
1699*01826a49SYabin Cui
1700*01826a49SYabin Cui /* **************************************************************
1701*01826a49SYabin Cui * Includes
1702*01826a49SYabin Cui ****************************************************************/
1703*01826a49SYabin Cui #include <stdlib.h> /* malloc, free, qsort */
1704*01826a49SYabin Cui #include <string.h> /* memcpy, memset */
1705*01826a49SYabin Cui #include <stdio.h> /* printf (debug) */
1706*01826a49SYabin Cui
1707*01826a49SYabin Cui
1708*01826a49SYabin Cui /* **************************************************************
1709*01826a49SYabin Cui * Constants
1710*01826a49SYabin Cui ****************************************************************/
1711*01826a49SYabin Cui #define HUFv05_ABSOLUTEMAX_TABLELOG 16 /* absolute limit of HUFv05_MAX_TABLELOG. Beyond that value, code does not work */
1712*01826a49SYabin Cui #define HUFv05_MAX_TABLELOG 12 /* max configured tableLog (for static allocation); can be modified up to HUFv05_ABSOLUTEMAX_TABLELOG */
1713*01826a49SYabin Cui #define HUFv05_DEFAULT_TABLELOG HUFv05_MAX_TABLELOG /* tableLog by default, when not specified */
1714*01826a49SYabin Cui #define HUFv05_MAX_SYMBOL_VALUE 255
1715*01826a49SYabin Cui #if (HUFv05_MAX_TABLELOG > HUFv05_ABSOLUTEMAX_TABLELOG)
1716*01826a49SYabin Cui # error "HUFv05_MAX_TABLELOG is too large !"
1717*01826a49SYabin Cui #endif
1718*01826a49SYabin Cui
1719*01826a49SYabin Cui
1720*01826a49SYabin Cui /* **************************************************************
1721*01826a49SYabin Cui * Error Management
1722*01826a49SYabin Cui ****************************************************************/
HUFv05_isError(size_t code)1723*01826a49SYabin Cui unsigned HUFv05_isError(size_t code) { return ERR_isError(code); }
HUFv05_getErrorName(size_t code)1724*01826a49SYabin Cui const char* HUFv05_getErrorName(size_t code) { return ERR_getErrorName(code); }
1725*01826a49SYabin Cui #define HUFv05_STATIC_ASSERT(c) { enum { HUFv05_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
1726*01826a49SYabin Cui
1727*01826a49SYabin Cui
1728*01826a49SYabin Cui /* *******************************************************
1729*01826a49SYabin Cui * Huff0 : Huffman block decompression
1730*01826a49SYabin Cui *********************************************************/
1731*01826a49SYabin Cui typedef struct { BYTE byte; BYTE nbBits; } HUFv05_DEltX2; /* single-symbol decoding */
1732*01826a49SYabin Cui
1733*01826a49SYabin Cui typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUFv05_DEltX4; /* double-symbols decoding */
1734*01826a49SYabin Cui
1735*01826a49SYabin Cui typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t;
1736*01826a49SYabin Cui
1737*01826a49SYabin Cui /*! HUFv05_readStats
1738*01826a49SYabin Cui Read compact Huffman tree, saved by HUFv05_writeCTable
1739*01826a49SYabin Cui @huffWeight : destination buffer
1740*01826a49SYabin Cui @return : size read from `src`
1741*01826a49SYabin Cui */
HUFv05_readStats(BYTE * huffWeight,size_t hwSize,U32 * rankStats,U32 * nbSymbolsPtr,U32 * tableLogPtr,const void * src,size_t srcSize)1742*01826a49SYabin Cui static size_t HUFv05_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
1743*01826a49SYabin Cui U32* nbSymbolsPtr, U32* tableLogPtr,
1744*01826a49SYabin Cui const void* src, size_t srcSize)
1745*01826a49SYabin Cui {
1746*01826a49SYabin Cui U32 weightTotal;
1747*01826a49SYabin Cui U32 tableLog;
1748*01826a49SYabin Cui const BYTE* ip = (const BYTE*) src;
1749*01826a49SYabin Cui size_t iSize;
1750*01826a49SYabin Cui size_t oSize;
1751*01826a49SYabin Cui U32 n;
1752*01826a49SYabin Cui
1753*01826a49SYabin Cui if (!srcSize) return ERROR(srcSize_wrong);
1754*01826a49SYabin Cui iSize = ip[0];
1755*01826a49SYabin Cui /* memset(huffWeight, 0, hwSize); */ /* is not necessary, even though some analyzer complain ... */
1756*01826a49SYabin Cui
1757*01826a49SYabin Cui if (iSize >= 128) { /* special header */
1758*01826a49SYabin Cui if (iSize >= (242)) { /* RLE */
1759*01826a49SYabin Cui static int l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 };
1760*01826a49SYabin Cui oSize = l[iSize-242];
1761*01826a49SYabin Cui memset(huffWeight, 1, hwSize);
1762*01826a49SYabin Cui iSize = 0;
1763*01826a49SYabin Cui }
1764*01826a49SYabin Cui else { /* Incompressible */
1765*01826a49SYabin Cui oSize = iSize - 127;
1766*01826a49SYabin Cui iSize = ((oSize+1)/2);
1767*01826a49SYabin Cui if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
1768*01826a49SYabin Cui if (oSize >= hwSize) return ERROR(corruption_detected);
1769*01826a49SYabin Cui ip += 1;
1770*01826a49SYabin Cui for (n=0; n<oSize; n+=2) {
1771*01826a49SYabin Cui huffWeight[n] = ip[n/2] >> 4;
1772*01826a49SYabin Cui huffWeight[n+1] = ip[n/2] & 15;
1773*01826a49SYabin Cui } } }
1774*01826a49SYabin Cui else { /* header compressed with FSEv05 (normal case) */
1775*01826a49SYabin Cui if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
1776*01826a49SYabin Cui oSize = FSEv05_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */
1777*01826a49SYabin Cui if (FSEv05_isError(oSize)) return oSize;
1778*01826a49SYabin Cui }
1779*01826a49SYabin Cui
1780*01826a49SYabin Cui /* collect weight stats */
1781*01826a49SYabin Cui memset(rankStats, 0, (HUFv05_ABSOLUTEMAX_TABLELOG + 1) * sizeof(U32));
1782*01826a49SYabin Cui weightTotal = 0;
1783*01826a49SYabin Cui for (n=0; n<oSize; n++) {
1784*01826a49SYabin Cui if (huffWeight[n] >= HUFv05_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
1785*01826a49SYabin Cui rankStats[huffWeight[n]]++;
1786*01826a49SYabin Cui weightTotal += (1 << huffWeight[n]) >> 1;
1787*01826a49SYabin Cui }
1788*01826a49SYabin Cui if (weightTotal == 0) return ERROR(corruption_detected);
1789*01826a49SYabin Cui
1790*01826a49SYabin Cui /* get last non-null symbol weight (implied, total must be 2^n) */
1791*01826a49SYabin Cui tableLog = BITv05_highbit32(weightTotal) + 1;
1792*01826a49SYabin Cui if (tableLog > HUFv05_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
1793*01826a49SYabin Cui { /* determine last weight */
1794*01826a49SYabin Cui U32 total = 1 << tableLog;
1795*01826a49SYabin Cui U32 rest = total - weightTotal;
1796*01826a49SYabin Cui U32 verif = 1 << BITv05_highbit32(rest);
1797*01826a49SYabin Cui U32 lastWeight = BITv05_highbit32(rest) + 1;
1798*01826a49SYabin Cui if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
1799*01826a49SYabin Cui huffWeight[oSize] = (BYTE)lastWeight;
1800*01826a49SYabin Cui rankStats[lastWeight]++;
1801*01826a49SYabin Cui }
1802*01826a49SYabin Cui
1803*01826a49SYabin Cui /* check tree construction validity */
1804*01826a49SYabin Cui if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
1805*01826a49SYabin Cui
1806*01826a49SYabin Cui /* results */
1807*01826a49SYabin Cui *nbSymbolsPtr = (U32)(oSize+1);
1808*01826a49SYabin Cui *tableLogPtr = tableLog;
1809*01826a49SYabin Cui return iSize+1;
1810*01826a49SYabin Cui }
1811*01826a49SYabin Cui
1812*01826a49SYabin Cui
1813*01826a49SYabin Cui /*-***************************/
1814*01826a49SYabin Cui /* single-symbol decoding */
1815*01826a49SYabin Cui /*-***************************/
1816*01826a49SYabin Cui
HUFv05_readDTableX2(U16 * DTable,const void * src,size_t srcSize)1817*01826a49SYabin Cui size_t HUFv05_readDTableX2 (U16* DTable, const void* src, size_t srcSize)
1818*01826a49SYabin Cui {
1819*01826a49SYabin Cui BYTE huffWeight[HUFv05_MAX_SYMBOL_VALUE + 1];
1820*01826a49SYabin Cui U32 rankVal[HUFv05_ABSOLUTEMAX_TABLELOG + 1]; /* large enough for values from 0 to 16 */
1821*01826a49SYabin Cui U32 tableLog = 0;
1822*01826a49SYabin Cui size_t iSize;
1823*01826a49SYabin Cui U32 nbSymbols = 0;
1824*01826a49SYabin Cui U32 n;
1825*01826a49SYabin Cui U32 nextRankStart;
1826*01826a49SYabin Cui void* const dtPtr = DTable + 1;
1827*01826a49SYabin Cui HUFv05_DEltX2* const dt = (HUFv05_DEltX2*)dtPtr;
1828*01826a49SYabin Cui
1829*01826a49SYabin Cui HUFv05_STATIC_ASSERT(sizeof(HUFv05_DEltX2) == sizeof(U16)); /* if compilation fails here, assertion is false */
1830*01826a49SYabin Cui /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
1831*01826a49SYabin Cui
1832*01826a49SYabin Cui iSize = HUFv05_readStats(huffWeight, HUFv05_MAX_SYMBOL_VALUE + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
1833*01826a49SYabin Cui if (HUFv05_isError(iSize)) return iSize;
1834*01826a49SYabin Cui
1835*01826a49SYabin Cui /* check result */
1836*01826a49SYabin Cui if (tableLog > DTable[0]) return ERROR(tableLog_tooLarge); /* DTable is too small */
1837*01826a49SYabin Cui DTable[0] = (U16)tableLog; /* maybe should separate sizeof allocated DTable, from used size of DTable, in case of re-use */
1838*01826a49SYabin Cui
1839*01826a49SYabin Cui /* Prepare ranks */
1840*01826a49SYabin Cui nextRankStart = 0;
1841*01826a49SYabin Cui for (n=1; n<=tableLog; n++) {
1842*01826a49SYabin Cui U32 current = nextRankStart;
1843*01826a49SYabin Cui nextRankStart += (rankVal[n] << (n-1));
1844*01826a49SYabin Cui rankVal[n] = current;
1845*01826a49SYabin Cui }
1846*01826a49SYabin Cui
1847*01826a49SYabin Cui /* fill DTable */
1848*01826a49SYabin Cui for (n=0; n<nbSymbols; n++) {
1849*01826a49SYabin Cui const U32 w = huffWeight[n];
1850*01826a49SYabin Cui const U32 length = (1 << w) >> 1;
1851*01826a49SYabin Cui U32 i;
1852*01826a49SYabin Cui HUFv05_DEltX2 D;
1853*01826a49SYabin Cui D.byte = (BYTE)n; D.nbBits = (BYTE)(tableLog + 1 - w);
1854*01826a49SYabin Cui for (i = rankVal[w]; i < rankVal[w] + length; i++)
1855*01826a49SYabin Cui dt[i] = D;
1856*01826a49SYabin Cui rankVal[w] += length;
1857*01826a49SYabin Cui }
1858*01826a49SYabin Cui
1859*01826a49SYabin Cui return iSize;
1860*01826a49SYabin Cui }
1861*01826a49SYabin Cui
HUFv05_decodeSymbolX2(BITv05_DStream_t * Dstream,const HUFv05_DEltX2 * dt,const U32 dtLog)1862*01826a49SYabin Cui static BYTE HUFv05_decodeSymbolX2(BITv05_DStream_t* Dstream, const HUFv05_DEltX2* dt, const U32 dtLog)
1863*01826a49SYabin Cui {
1864*01826a49SYabin Cui const size_t val = BITv05_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
1865*01826a49SYabin Cui const BYTE c = dt[val].byte;
1866*01826a49SYabin Cui BITv05_skipBits(Dstream, dt[val].nbBits);
1867*01826a49SYabin Cui return c;
1868*01826a49SYabin Cui }
1869*01826a49SYabin Cui
1870*01826a49SYabin Cui #define HUFv05_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \
1871*01826a49SYabin Cui *ptr++ = HUFv05_decodeSymbolX2(DStreamPtr, dt, dtLog)
1872*01826a49SYabin Cui
1873*01826a49SYabin Cui #define HUFv05_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \
1874*01826a49SYabin Cui if (MEM_64bits() || (HUFv05_MAX_TABLELOG<=12)) \
1875*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
1876*01826a49SYabin Cui
1877*01826a49SYabin Cui #define HUFv05_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \
1878*01826a49SYabin Cui if (MEM_64bits()) \
1879*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
1880*01826a49SYabin Cui
HUFv05_decodeStreamX2(BYTE * p,BITv05_DStream_t * const bitDPtr,BYTE * const pEnd,const HUFv05_DEltX2 * const dt,const U32 dtLog)1881*01826a49SYabin Cui static inline size_t HUFv05_decodeStreamX2(BYTE* p, BITv05_DStream_t* const bitDPtr, BYTE* const pEnd, const HUFv05_DEltX2* const dt, const U32 dtLog)
1882*01826a49SYabin Cui {
1883*01826a49SYabin Cui BYTE* const pStart = p;
1884*01826a49SYabin Cui
1885*01826a49SYabin Cui /* up to 4 symbols at a time */
1886*01826a49SYabin Cui while ((BITv05_reloadDStream(bitDPtr) == BITv05_DStream_unfinished) && (p <= pEnd-4)) {
1887*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_2(p, bitDPtr);
1888*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_1(p, bitDPtr);
1889*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_2(p, bitDPtr);
1890*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_0(p, bitDPtr);
1891*01826a49SYabin Cui }
1892*01826a49SYabin Cui
1893*01826a49SYabin Cui /* closer to the end */
1894*01826a49SYabin Cui while ((BITv05_reloadDStream(bitDPtr) == BITv05_DStream_unfinished) && (p < pEnd))
1895*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_0(p, bitDPtr);
1896*01826a49SYabin Cui
1897*01826a49SYabin Cui /* no more data to retrieve from bitstream, hence no need to reload */
1898*01826a49SYabin Cui while (p < pEnd)
1899*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_0(p, bitDPtr);
1900*01826a49SYabin Cui
1901*01826a49SYabin Cui return pEnd-pStart;
1902*01826a49SYabin Cui }
1903*01826a49SYabin Cui
HUFv05_decompress1X2_usingDTable(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const U16 * DTable)1904*01826a49SYabin Cui size_t HUFv05_decompress1X2_usingDTable(
1905*01826a49SYabin Cui void* dst, size_t dstSize,
1906*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1907*01826a49SYabin Cui const U16* DTable)
1908*01826a49SYabin Cui {
1909*01826a49SYabin Cui BYTE* op = (BYTE*)dst;
1910*01826a49SYabin Cui BYTE* const oend = op + dstSize;
1911*01826a49SYabin Cui const U32 dtLog = DTable[0];
1912*01826a49SYabin Cui const void* dtPtr = DTable;
1913*01826a49SYabin Cui const HUFv05_DEltX2* const dt = ((const HUFv05_DEltX2*)dtPtr)+1;
1914*01826a49SYabin Cui BITv05_DStream_t bitD;
1915*01826a49SYabin Cui
1916*01826a49SYabin Cui if (dstSize <= cSrcSize) return ERROR(dstSize_tooSmall);
1917*01826a49SYabin Cui { size_t const errorCode = BITv05_initDStream(&bitD, cSrc, cSrcSize);
1918*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return errorCode; }
1919*01826a49SYabin Cui
1920*01826a49SYabin Cui HUFv05_decodeStreamX2(op, &bitD, oend, dt, dtLog);
1921*01826a49SYabin Cui
1922*01826a49SYabin Cui /* check */
1923*01826a49SYabin Cui if (!BITv05_endOfDStream(&bitD)) return ERROR(corruption_detected);
1924*01826a49SYabin Cui
1925*01826a49SYabin Cui return dstSize;
1926*01826a49SYabin Cui }
1927*01826a49SYabin Cui
HUFv05_decompress1X2(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)1928*01826a49SYabin Cui size_t HUFv05_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1929*01826a49SYabin Cui {
1930*01826a49SYabin Cui HUFv05_CREATE_STATIC_DTABLEX2(DTable, HUFv05_MAX_TABLELOG);
1931*01826a49SYabin Cui const BYTE* ip = (const BYTE*) cSrc;
1932*01826a49SYabin Cui size_t errorCode;
1933*01826a49SYabin Cui
1934*01826a49SYabin Cui errorCode = HUFv05_readDTableX2 (DTable, cSrc, cSrcSize);
1935*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return errorCode;
1936*01826a49SYabin Cui if (errorCode >= cSrcSize) return ERROR(srcSize_wrong);
1937*01826a49SYabin Cui ip += errorCode;
1938*01826a49SYabin Cui cSrcSize -= errorCode;
1939*01826a49SYabin Cui
1940*01826a49SYabin Cui return HUFv05_decompress1X2_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
1941*01826a49SYabin Cui }
1942*01826a49SYabin Cui
1943*01826a49SYabin Cui
HUFv05_decompress4X2_usingDTable(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const U16 * DTable)1944*01826a49SYabin Cui size_t HUFv05_decompress4X2_usingDTable(
1945*01826a49SYabin Cui void* dst, size_t dstSize,
1946*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1947*01826a49SYabin Cui const U16* DTable)
1948*01826a49SYabin Cui {
1949*01826a49SYabin Cui /* Check */
1950*01826a49SYabin Cui if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
1951*01826a49SYabin Cui {
1952*01826a49SYabin Cui const BYTE* const istart = (const BYTE*) cSrc;
1953*01826a49SYabin Cui BYTE* const ostart = (BYTE*) dst;
1954*01826a49SYabin Cui BYTE* const oend = ostart + dstSize;
1955*01826a49SYabin Cui const void* const dtPtr = DTable;
1956*01826a49SYabin Cui const HUFv05_DEltX2* const dt = ((const HUFv05_DEltX2*)dtPtr) +1;
1957*01826a49SYabin Cui const U32 dtLog = DTable[0];
1958*01826a49SYabin Cui size_t errorCode;
1959*01826a49SYabin Cui
1960*01826a49SYabin Cui /* Init */
1961*01826a49SYabin Cui BITv05_DStream_t bitD1;
1962*01826a49SYabin Cui BITv05_DStream_t bitD2;
1963*01826a49SYabin Cui BITv05_DStream_t bitD3;
1964*01826a49SYabin Cui BITv05_DStream_t bitD4;
1965*01826a49SYabin Cui const size_t length1 = MEM_readLE16(istart);
1966*01826a49SYabin Cui const size_t length2 = MEM_readLE16(istart+2);
1967*01826a49SYabin Cui const size_t length3 = MEM_readLE16(istart+4);
1968*01826a49SYabin Cui size_t length4;
1969*01826a49SYabin Cui const BYTE* const istart1 = istart + 6; /* jumpTable */
1970*01826a49SYabin Cui const BYTE* const istart2 = istart1 + length1;
1971*01826a49SYabin Cui const BYTE* const istart3 = istart2 + length2;
1972*01826a49SYabin Cui const BYTE* const istart4 = istart3 + length3;
1973*01826a49SYabin Cui const size_t segmentSize = (dstSize+3) / 4;
1974*01826a49SYabin Cui BYTE* const opStart2 = ostart + segmentSize;
1975*01826a49SYabin Cui BYTE* const opStart3 = opStart2 + segmentSize;
1976*01826a49SYabin Cui BYTE* const opStart4 = opStart3 + segmentSize;
1977*01826a49SYabin Cui BYTE* op1 = ostart;
1978*01826a49SYabin Cui BYTE* op2 = opStart2;
1979*01826a49SYabin Cui BYTE* op3 = opStart3;
1980*01826a49SYabin Cui BYTE* op4 = opStart4;
1981*01826a49SYabin Cui U32 endSignal;
1982*01826a49SYabin Cui
1983*01826a49SYabin Cui length4 = cSrcSize - (length1 + length2 + length3 + 6);
1984*01826a49SYabin Cui if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
1985*01826a49SYabin Cui errorCode = BITv05_initDStream(&bitD1, istart1, length1);
1986*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return errorCode;
1987*01826a49SYabin Cui errorCode = BITv05_initDStream(&bitD2, istart2, length2);
1988*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return errorCode;
1989*01826a49SYabin Cui errorCode = BITv05_initDStream(&bitD3, istart3, length3);
1990*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return errorCode;
1991*01826a49SYabin Cui errorCode = BITv05_initDStream(&bitD4, istart4, length4);
1992*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return errorCode;
1993*01826a49SYabin Cui
1994*01826a49SYabin Cui /* 16-32 symbols per loop (4-8 symbols per stream) */
1995*01826a49SYabin Cui endSignal = BITv05_reloadDStream(&bitD1) | BITv05_reloadDStream(&bitD2) | BITv05_reloadDStream(&bitD3) | BITv05_reloadDStream(&bitD4);
1996*01826a49SYabin Cui for ( ; (endSignal==BITv05_DStream_unfinished) && (op4<(oend-7)) ; ) {
1997*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_2(op1, &bitD1);
1998*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_2(op2, &bitD2);
1999*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_2(op3, &bitD3);
2000*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_2(op4, &bitD4);
2001*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_1(op1, &bitD1);
2002*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_1(op2, &bitD2);
2003*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_1(op3, &bitD3);
2004*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_1(op4, &bitD4);
2005*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_2(op1, &bitD1);
2006*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_2(op2, &bitD2);
2007*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_2(op3, &bitD3);
2008*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_2(op4, &bitD4);
2009*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_0(op1, &bitD1);
2010*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_0(op2, &bitD2);
2011*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_0(op3, &bitD3);
2012*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX2_0(op4, &bitD4);
2013*01826a49SYabin Cui endSignal = BITv05_reloadDStream(&bitD1) | BITv05_reloadDStream(&bitD2) | BITv05_reloadDStream(&bitD3) | BITv05_reloadDStream(&bitD4);
2014*01826a49SYabin Cui }
2015*01826a49SYabin Cui
2016*01826a49SYabin Cui /* check corruption */
2017*01826a49SYabin Cui if (op1 > opStart2) return ERROR(corruption_detected);
2018*01826a49SYabin Cui if (op2 > opStart3) return ERROR(corruption_detected);
2019*01826a49SYabin Cui if (op3 > opStart4) return ERROR(corruption_detected);
2020*01826a49SYabin Cui /* note : op4 supposed already verified within main loop */
2021*01826a49SYabin Cui
2022*01826a49SYabin Cui /* finish bitStreams one by one */
2023*01826a49SYabin Cui HUFv05_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
2024*01826a49SYabin Cui HUFv05_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
2025*01826a49SYabin Cui HUFv05_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
2026*01826a49SYabin Cui HUFv05_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
2027*01826a49SYabin Cui
2028*01826a49SYabin Cui /* check */
2029*01826a49SYabin Cui endSignal = BITv05_endOfDStream(&bitD1) & BITv05_endOfDStream(&bitD2) & BITv05_endOfDStream(&bitD3) & BITv05_endOfDStream(&bitD4);
2030*01826a49SYabin Cui if (!endSignal) return ERROR(corruption_detected);
2031*01826a49SYabin Cui
2032*01826a49SYabin Cui /* decoded size */
2033*01826a49SYabin Cui return dstSize;
2034*01826a49SYabin Cui }
2035*01826a49SYabin Cui }
2036*01826a49SYabin Cui
2037*01826a49SYabin Cui
HUFv05_decompress4X2(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)2038*01826a49SYabin Cui size_t HUFv05_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2039*01826a49SYabin Cui {
2040*01826a49SYabin Cui HUFv05_CREATE_STATIC_DTABLEX2(DTable, HUFv05_MAX_TABLELOG);
2041*01826a49SYabin Cui const BYTE* ip = (const BYTE*) cSrc;
2042*01826a49SYabin Cui size_t errorCode;
2043*01826a49SYabin Cui
2044*01826a49SYabin Cui errorCode = HUFv05_readDTableX2 (DTable, cSrc, cSrcSize);
2045*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return errorCode;
2046*01826a49SYabin Cui if (errorCode >= cSrcSize) return ERROR(srcSize_wrong);
2047*01826a49SYabin Cui ip += errorCode;
2048*01826a49SYabin Cui cSrcSize -= errorCode;
2049*01826a49SYabin Cui
2050*01826a49SYabin Cui return HUFv05_decompress4X2_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
2051*01826a49SYabin Cui }
2052*01826a49SYabin Cui
2053*01826a49SYabin Cui
2054*01826a49SYabin Cui /* *************************/
2055*01826a49SYabin Cui /* double-symbols decoding */
2056*01826a49SYabin Cui /* *************************/
2057*01826a49SYabin Cui
HUFv05_fillDTableX4Level2(HUFv05_DEltX4 * DTable,U32 sizeLog,const U32 consumed,const U32 * rankValOrigin,const int minWeight,const sortedSymbol_t * sortedSymbols,const U32 sortedListSize,U32 nbBitsBaseline,U16 baseSeq)2058*01826a49SYabin Cui static void HUFv05_fillDTableX4Level2(HUFv05_DEltX4* DTable, U32 sizeLog, const U32 consumed,
2059*01826a49SYabin Cui const U32* rankValOrigin, const int minWeight,
2060*01826a49SYabin Cui const sortedSymbol_t* sortedSymbols, const U32 sortedListSize,
2061*01826a49SYabin Cui U32 nbBitsBaseline, U16 baseSeq)
2062*01826a49SYabin Cui {
2063*01826a49SYabin Cui HUFv05_DEltX4 DElt;
2064*01826a49SYabin Cui U32 rankVal[HUFv05_ABSOLUTEMAX_TABLELOG + 1];
2065*01826a49SYabin Cui U32 s;
2066*01826a49SYabin Cui
2067*01826a49SYabin Cui /* get pre-calculated rankVal */
2068*01826a49SYabin Cui memcpy(rankVal, rankValOrigin, sizeof(rankVal));
2069*01826a49SYabin Cui
2070*01826a49SYabin Cui /* fill skipped values */
2071*01826a49SYabin Cui if (minWeight>1) {
2072*01826a49SYabin Cui U32 i, skipSize = rankVal[minWeight];
2073*01826a49SYabin Cui MEM_writeLE16(&(DElt.sequence), baseSeq);
2074*01826a49SYabin Cui DElt.nbBits = (BYTE)(consumed);
2075*01826a49SYabin Cui DElt.length = 1;
2076*01826a49SYabin Cui for (i = 0; i < skipSize; i++)
2077*01826a49SYabin Cui DTable[i] = DElt;
2078*01826a49SYabin Cui }
2079*01826a49SYabin Cui
2080*01826a49SYabin Cui /* fill DTable */
2081*01826a49SYabin Cui for (s=0; s<sortedListSize; s++) { /* note : sortedSymbols already skipped */
2082*01826a49SYabin Cui const U32 symbol = sortedSymbols[s].symbol;
2083*01826a49SYabin Cui const U32 weight = sortedSymbols[s].weight;
2084*01826a49SYabin Cui const U32 nbBits = nbBitsBaseline - weight;
2085*01826a49SYabin Cui const U32 length = 1 << (sizeLog-nbBits);
2086*01826a49SYabin Cui const U32 start = rankVal[weight];
2087*01826a49SYabin Cui U32 i = start;
2088*01826a49SYabin Cui const U32 end = start + length;
2089*01826a49SYabin Cui
2090*01826a49SYabin Cui MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
2091*01826a49SYabin Cui DElt.nbBits = (BYTE)(nbBits + consumed);
2092*01826a49SYabin Cui DElt.length = 2;
2093*01826a49SYabin Cui do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */
2094*01826a49SYabin Cui
2095*01826a49SYabin Cui rankVal[weight] += length;
2096*01826a49SYabin Cui }
2097*01826a49SYabin Cui }
2098*01826a49SYabin Cui
2099*01826a49SYabin Cui typedef U32 rankVal_t[HUFv05_ABSOLUTEMAX_TABLELOG][HUFv05_ABSOLUTEMAX_TABLELOG + 1];
2100*01826a49SYabin Cui
HUFv05_fillDTableX4(HUFv05_DEltX4 * DTable,const U32 targetLog,const sortedSymbol_t * sortedList,const U32 sortedListSize,const U32 * rankStart,rankVal_t rankValOrigin,const U32 maxWeight,const U32 nbBitsBaseline)2101*01826a49SYabin Cui static void HUFv05_fillDTableX4(HUFv05_DEltX4* DTable, const U32 targetLog,
2102*01826a49SYabin Cui const sortedSymbol_t* sortedList, const U32 sortedListSize,
2103*01826a49SYabin Cui const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight,
2104*01826a49SYabin Cui const U32 nbBitsBaseline)
2105*01826a49SYabin Cui {
2106*01826a49SYabin Cui U32 rankVal[HUFv05_ABSOLUTEMAX_TABLELOG + 1];
2107*01826a49SYabin Cui const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */
2108*01826a49SYabin Cui const U32 minBits = nbBitsBaseline - maxWeight;
2109*01826a49SYabin Cui U32 s;
2110*01826a49SYabin Cui
2111*01826a49SYabin Cui memcpy(rankVal, rankValOrigin, sizeof(rankVal));
2112*01826a49SYabin Cui
2113*01826a49SYabin Cui /* fill DTable */
2114*01826a49SYabin Cui for (s=0; s<sortedListSize; s++) {
2115*01826a49SYabin Cui const U16 symbol = sortedList[s].symbol;
2116*01826a49SYabin Cui const U32 weight = sortedList[s].weight;
2117*01826a49SYabin Cui const U32 nbBits = nbBitsBaseline - weight;
2118*01826a49SYabin Cui const U32 start = rankVal[weight];
2119*01826a49SYabin Cui const U32 length = 1 << (targetLog-nbBits);
2120*01826a49SYabin Cui
2121*01826a49SYabin Cui if (targetLog-nbBits >= minBits) { /* enough room for a second symbol */
2122*01826a49SYabin Cui U32 sortedRank;
2123*01826a49SYabin Cui int minWeight = nbBits + scaleLog;
2124*01826a49SYabin Cui if (minWeight < 1) minWeight = 1;
2125*01826a49SYabin Cui sortedRank = rankStart[minWeight];
2126*01826a49SYabin Cui HUFv05_fillDTableX4Level2(DTable+start, targetLog-nbBits, nbBits,
2127*01826a49SYabin Cui rankValOrigin[nbBits], minWeight,
2128*01826a49SYabin Cui sortedList+sortedRank, sortedListSize-sortedRank,
2129*01826a49SYabin Cui nbBitsBaseline, symbol);
2130*01826a49SYabin Cui } else {
2131*01826a49SYabin Cui U32 i;
2132*01826a49SYabin Cui const U32 end = start + length;
2133*01826a49SYabin Cui HUFv05_DEltX4 DElt;
2134*01826a49SYabin Cui
2135*01826a49SYabin Cui MEM_writeLE16(&(DElt.sequence), symbol);
2136*01826a49SYabin Cui DElt.nbBits = (BYTE)(nbBits);
2137*01826a49SYabin Cui DElt.length = 1;
2138*01826a49SYabin Cui for (i = start; i < end; i++)
2139*01826a49SYabin Cui DTable[i] = DElt;
2140*01826a49SYabin Cui }
2141*01826a49SYabin Cui rankVal[weight] += length;
2142*01826a49SYabin Cui }
2143*01826a49SYabin Cui }
2144*01826a49SYabin Cui
HUFv05_readDTableX4(unsigned * DTable,const void * src,size_t srcSize)2145*01826a49SYabin Cui size_t HUFv05_readDTableX4 (unsigned* DTable, const void* src, size_t srcSize)
2146*01826a49SYabin Cui {
2147*01826a49SYabin Cui BYTE weightList[HUFv05_MAX_SYMBOL_VALUE + 1];
2148*01826a49SYabin Cui sortedSymbol_t sortedSymbol[HUFv05_MAX_SYMBOL_VALUE + 1];
2149*01826a49SYabin Cui U32 rankStats[HUFv05_ABSOLUTEMAX_TABLELOG + 1] = { 0 };
2150*01826a49SYabin Cui U32 rankStart0[HUFv05_ABSOLUTEMAX_TABLELOG + 2] = { 0 };
2151*01826a49SYabin Cui U32* const rankStart = rankStart0+1;
2152*01826a49SYabin Cui rankVal_t rankVal;
2153*01826a49SYabin Cui U32 tableLog, maxW, sizeOfSort, nbSymbols;
2154*01826a49SYabin Cui const U32 memLog = DTable[0];
2155*01826a49SYabin Cui size_t iSize;
2156*01826a49SYabin Cui void* dtPtr = DTable;
2157*01826a49SYabin Cui HUFv05_DEltX4* const dt = ((HUFv05_DEltX4*)dtPtr) + 1;
2158*01826a49SYabin Cui
2159*01826a49SYabin Cui HUFv05_STATIC_ASSERT(sizeof(HUFv05_DEltX4) == sizeof(unsigned)); /* if compilation fails here, assertion is false */
2160*01826a49SYabin Cui if (memLog > HUFv05_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
2161*01826a49SYabin Cui /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
2162*01826a49SYabin Cui
2163*01826a49SYabin Cui iSize = HUFv05_readStats(weightList, HUFv05_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2164*01826a49SYabin Cui if (HUFv05_isError(iSize)) return iSize;
2165*01826a49SYabin Cui
2166*01826a49SYabin Cui /* check result */
2167*01826a49SYabin Cui if (tableLog > memLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */
2168*01826a49SYabin Cui
2169*01826a49SYabin Cui /* find maxWeight */
2170*01826a49SYabin Cui for (maxW = tableLog; rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */
2171*01826a49SYabin Cui
2172*01826a49SYabin Cui /* Get start index of each weight */
2173*01826a49SYabin Cui {
2174*01826a49SYabin Cui U32 w, nextRankStart = 0;
2175*01826a49SYabin Cui for (w=1; w<=maxW; w++) {
2176*01826a49SYabin Cui U32 current = nextRankStart;
2177*01826a49SYabin Cui nextRankStart += rankStats[w];
2178*01826a49SYabin Cui rankStart[w] = current;
2179*01826a49SYabin Cui }
2180*01826a49SYabin Cui rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
2181*01826a49SYabin Cui sizeOfSort = nextRankStart;
2182*01826a49SYabin Cui }
2183*01826a49SYabin Cui
2184*01826a49SYabin Cui /* sort symbols by weight */
2185*01826a49SYabin Cui {
2186*01826a49SYabin Cui U32 s;
2187*01826a49SYabin Cui for (s=0; s<nbSymbols; s++) {
2188*01826a49SYabin Cui U32 w = weightList[s];
2189*01826a49SYabin Cui U32 r = rankStart[w]++;
2190*01826a49SYabin Cui sortedSymbol[r].symbol = (BYTE)s;
2191*01826a49SYabin Cui sortedSymbol[r].weight = (BYTE)w;
2192*01826a49SYabin Cui }
2193*01826a49SYabin Cui rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
2194*01826a49SYabin Cui }
2195*01826a49SYabin Cui
2196*01826a49SYabin Cui /* Build rankVal */
2197*01826a49SYabin Cui {
2198*01826a49SYabin Cui const U32 minBits = tableLog+1 - maxW;
2199*01826a49SYabin Cui U32 nextRankVal = 0;
2200*01826a49SYabin Cui U32 w, consumed;
2201*01826a49SYabin Cui const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */
2202*01826a49SYabin Cui U32* rankVal0 = rankVal[0];
2203*01826a49SYabin Cui for (w=1; w<=maxW; w++) {
2204*01826a49SYabin Cui U32 current = nextRankVal;
2205*01826a49SYabin Cui nextRankVal += rankStats[w] << (w+rescale);
2206*01826a49SYabin Cui rankVal0[w] = current;
2207*01826a49SYabin Cui }
2208*01826a49SYabin Cui for (consumed = minBits; consumed <= memLog - minBits; consumed++) {
2209*01826a49SYabin Cui U32* rankValPtr = rankVal[consumed];
2210*01826a49SYabin Cui for (w = 1; w <= maxW; w++) {
2211*01826a49SYabin Cui rankValPtr[w] = rankVal0[w] >> consumed;
2212*01826a49SYabin Cui } } }
2213*01826a49SYabin Cui
2214*01826a49SYabin Cui HUFv05_fillDTableX4(dt, memLog,
2215*01826a49SYabin Cui sortedSymbol, sizeOfSort,
2216*01826a49SYabin Cui rankStart0, rankVal, maxW,
2217*01826a49SYabin Cui tableLog+1);
2218*01826a49SYabin Cui
2219*01826a49SYabin Cui return iSize;
2220*01826a49SYabin Cui }
2221*01826a49SYabin Cui
2222*01826a49SYabin Cui
HUFv05_decodeSymbolX4(void * op,BITv05_DStream_t * DStream,const HUFv05_DEltX4 * dt,const U32 dtLog)2223*01826a49SYabin Cui static U32 HUFv05_decodeSymbolX4(void* op, BITv05_DStream_t* DStream, const HUFv05_DEltX4* dt, const U32 dtLog)
2224*01826a49SYabin Cui {
2225*01826a49SYabin Cui const size_t val = BITv05_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2226*01826a49SYabin Cui memcpy(op, dt+val, 2);
2227*01826a49SYabin Cui BITv05_skipBits(DStream, dt[val].nbBits);
2228*01826a49SYabin Cui return dt[val].length;
2229*01826a49SYabin Cui }
2230*01826a49SYabin Cui
HUFv05_decodeLastSymbolX4(void * op,BITv05_DStream_t * DStream,const HUFv05_DEltX4 * dt,const U32 dtLog)2231*01826a49SYabin Cui static U32 HUFv05_decodeLastSymbolX4(void* op, BITv05_DStream_t* DStream, const HUFv05_DEltX4* dt, const U32 dtLog)
2232*01826a49SYabin Cui {
2233*01826a49SYabin Cui const size_t val = BITv05_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2234*01826a49SYabin Cui memcpy(op, dt+val, 1);
2235*01826a49SYabin Cui if (dt[val].length==1) BITv05_skipBits(DStream, dt[val].nbBits);
2236*01826a49SYabin Cui else {
2237*01826a49SYabin Cui if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8)) {
2238*01826a49SYabin Cui BITv05_skipBits(DStream, dt[val].nbBits);
2239*01826a49SYabin Cui if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
2240*01826a49SYabin Cui DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8); /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */
2241*01826a49SYabin Cui } }
2242*01826a49SYabin Cui return 1;
2243*01826a49SYabin Cui }
2244*01826a49SYabin Cui
2245*01826a49SYabin Cui
2246*01826a49SYabin Cui #define HUFv05_DECODE_SYMBOLX4_0(ptr, DStreamPtr) \
2247*01826a49SYabin Cui ptr += HUFv05_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2248*01826a49SYabin Cui
2249*01826a49SYabin Cui #define HUFv05_DECODE_SYMBOLX4_1(ptr, DStreamPtr) \
2250*01826a49SYabin Cui if (MEM_64bits() || (HUFv05_MAX_TABLELOG<=12)) \
2251*01826a49SYabin Cui ptr += HUFv05_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2252*01826a49SYabin Cui
2253*01826a49SYabin Cui #define HUFv05_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \
2254*01826a49SYabin Cui if (MEM_64bits()) \
2255*01826a49SYabin Cui ptr += HUFv05_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2256*01826a49SYabin Cui
HUFv05_decodeStreamX4(BYTE * p,BITv05_DStream_t * bitDPtr,BYTE * const pEnd,const HUFv05_DEltX4 * const dt,const U32 dtLog)2257*01826a49SYabin Cui static inline size_t HUFv05_decodeStreamX4(BYTE* p, BITv05_DStream_t* bitDPtr, BYTE* const pEnd, const HUFv05_DEltX4* const dt, const U32 dtLog)
2258*01826a49SYabin Cui {
2259*01826a49SYabin Cui BYTE* const pStart = p;
2260*01826a49SYabin Cui
2261*01826a49SYabin Cui /* up to 8 symbols at a time */
2262*01826a49SYabin Cui while ((BITv05_reloadDStream(bitDPtr) == BITv05_DStream_unfinished) && (p < pEnd-7)) {
2263*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_2(p, bitDPtr);
2264*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_1(p, bitDPtr);
2265*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_2(p, bitDPtr);
2266*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_0(p, bitDPtr);
2267*01826a49SYabin Cui }
2268*01826a49SYabin Cui
2269*01826a49SYabin Cui /* closer to the end */
2270*01826a49SYabin Cui while ((BITv05_reloadDStream(bitDPtr) == BITv05_DStream_unfinished) && (p <= pEnd-2))
2271*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_0(p, bitDPtr);
2272*01826a49SYabin Cui
2273*01826a49SYabin Cui while (p <= pEnd-2)
2274*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
2275*01826a49SYabin Cui
2276*01826a49SYabin Cui if (p < pEnd)
2277*01826a49SYabin Cui p += HUFv05_decodeLastSymbolX4(p, bitDPtr, dt, dtLog);
2278*01826a49SYabin Cui
2279*01826a49SYabin Cui return p-pStart;
2280*01826a49SYabin Cui }
2281*01826a49SYabin Cui
2282*01826a49SYabin Cui
HUFv05_decompress1X4_usingDTable(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const unsigned * DTable)2283*01826a49SYabin Cui size_t HUFv05_decompress1X4_usingDTable(
2284*01826a49SYabin Cui void* dst, size_t dstSize,
2285*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
2286*01826a49SYabin Cui const unsigned* DTable)
2287*01826a49SYabin Cui {
2288*01826a49SYabin Cui const BYTE* const istart = (const BYTE*) cSrc;
2289*01826a49SYabin Cui BYTE* const ostart = (BYTE*) dst;
2290*01826a49SYabin Cui BYTE* const oend = ostart + dstSize;
2291*01826a49SYabin Cui
2292*01826a49SYabin Cui const U32 dtLog = DTable[0];
2293*01826a49SYabin Cui const void* const dtPtr = DTable;
2294*01826a49SYabin Cui const HUFv05_DEltX4* const dt = ((const HUFv05_DEltX4*)dtPtr) +1;
2295*01826a49SYabin Cui size_t errorCode;
2296*01826a49SYabin Cui
2297*01826a49SYabin Cui /* Init */
2298*01826a49SYabin Cui BITv05_DStream_t bitD;
2299*01826a49SYabin Cui errorCode = BITv05_initDStream(&bitD, istart, cSrcSize);
2300*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return errorCode;
2301*01826a49SYabin Cui
2302*01826a49SYabin Cui /* finish bitStreams one by one */
2303*01826a49SYabin Cui HUFv05_decodeStreamX4(ostart, &bitD, oend, dt, dtLog);
2304*01826a49SYabin Cui
2305*01826a49SYabin Cui /* check */
2306*01826a49SYabin Cui if (!BITv05_endOfDStream(&bitD)) return ERROR(corruption_detected);
2307*01826a49SYabin Cui
2308*01826a49SYabin Cui /* decoded size */
2309*01826a49SYabin Cui return dstSize;
2310*01826a49SYabin Cui }
2311*01826a49SYabin Cui
HUFv05_decompress1X4(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)2312*01826a49SYabin Cui size_t HUFv05_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2313*01826a49SYabin Cui {
2314*01826a49SYabin Cui HUFv05_CREATE_STATIC_DTABLEX4(DTable, HUFv05_MAX_TABLELOG);
2315*01826a49SYabin Cui const BYTE* ip = (const BYTE*) cSrc;
2316*01826a49SYabin Cui
2317*01826a49SYabin Cui size_t hSize = HUFv05_readDTableX4 (DTable, cSrc, cSrcSize);
2318*01826a49SYabin Cui if (HUFv05_isError(hSize)) return hSize;
2319*01826a49SYabin Cui if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
2320*01826a49SYabin Cui ip += hSize;
2321*01826a49SYabin Cui cSrcSize -= hSize;
2322*01826a49SYabin Cui
2323*01826a49SYabin Cui return HUFv05_decompress1X4_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
2324*01826a49SYabin Cui }
2325*01826a49SYabin Cui
HUFv05_decompress4X4_usingDTable(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const unsigned * DTable)2326*01826a49SYabin Cui size_t HUFv05_decompress4X4_usingDTable(
2327*01826a49SYabin Cui void* dst, size_t dstSize,
2328*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
2329*01826a49SYabin Cui const unsigned* DTable)
2330*01826a49SYabin Cui {
2331*01826a49SYabin Cui if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
2332*01826a49SYabin Cui
2333*01826a49SYabin Cui {
2334*01826a49SYabin Cui const BYTE* const istart = (const BYTE*) cSrc;
2335*01826a49SYabin Cui BYTE* const ostart = (BYTE*) dst;
2336*01826a49SYabin Cui BYTE* const oend = ostart + dstSize;
2337*01826a49SYabin Cui const void* const dtPtr = DTable;
2338*01826a49SYabin Cui const HUFv05_DEltX4* const dt = ((const HUFv05_DEltX4*)dtPtr) +1;
2339*01826a49SYabin Cui const U32 dtLog = DTable[0];
2340*01826a49SYabin Cui size_t errorCode;
2341*01826a49SYabin Cui
2342*01826a49SYabin Cui /* Init */
2343*01826a49SYabin Cui BITv05_DStream_t bitD1;
2344*01826a49SYabin Cui BITv05_DStream_t bitD2;
2345*01826a49SYabin Cui BITv05_DStream_t bitD3;
2346*01826a49SYabin Cui BITv05_DStream_t bitD4;
2347*01826a49SYabin Cui const size_t length1 = MEM_readLE16(istart);
2348*01826a49SYabin Cui const size_t length2 = MEM_readLE16(istart+2);
2349*01826a49SYabin Cui const size_t length3 = MEM_readLE16(istart+4);
2350*01826a49SYabin Cui size_t length4;
2351*01826a49SYabin Cui const BYTE* const istart1 = istart + 6; /* jumpTable */
2352*01826a49SYabin Cui const BYTE* const istart2 = istart1 + length1;
2353*01826a49SYabin Cui const BYTE* const istart3 = istart2 + length2;
2354*01826a49SYabin Cui const BYTE* const istart4 = istart3 + length3;
2355*01826a49SYabin Cui const size_t segmentSize = (dstSize+3) / 4;
2356*01826a49SYabin Cui BYTE* const opStart2 = ostart + segmentSize;
2357*01826a49SYabin Cui BYTE* const opStart3 = opStart2 + segmentSize;
2358*01826a49SYabin Cui BYTE* const opStart4 = opStart3 + segmentSize;
2359*01826a49SYabin Cui BYTE* op1 = ostart;
2360*01826a49SYabin Cui BYTE* op2 = opStart2;
2361*01826a49SYabin Cui BYTE* op3 = opStart3;
2362*01826a49SYabin Cui BYTE* op4 = opStart4;
2363*01826a49SYabin Cui U32 endSignal;
2364*01826a49SYabin Cui
2365*01826a49SYabin Cui length4 = cSrcSize - (length1 + length2 + length3 + 6);
2366*01826a49SYabin Cui if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
2367*01826a49SYabin Cui errorCode = BITv05_initDStream(&bitD1, istart1, length1);
2368*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return errorCode;
2369*01826a49SYabin Cui errorCode = BITv05_initDStream(&bitD2, istart2, length2);
2370*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return errorCode;
2371*01826a49SYabin Cui errorCode = BITv05_initDStream(&bitD3, istart3, length3);
2372*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return errorCode;
2373*01826a49SYabin Cui errorCode = BITv05_initDStream(&bitD4, istart4, length4);
2374*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return errorCode;
2375*01826a49SYabin Cui
2376*01826a49SYabin Cui /* 16-32 symbols per loop (4-8 symbols per stream) */
2377*01826a49SYabin Cui endSignal = BITv05_reloadDStream(&bitD1) | BITv05_reloadDStream(&bitD2) | BITv05_reloadDStream(&bitD3) | BITv05_reloadDStream(&bitD4);
2378*01826a49SYabin Cui for ( ; (endSignal==BITv05_DStream_unfinished) && (op4<(oend-7)) ; ) {
2379*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_2(op1, &bitD1);
2380*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_2(op2, &bitD2);
2381*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_2(op3, &bitD3);
2382*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_2(op4, &bitD4);
2383*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_1(op1, &bitD1);
2384*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_1(op2, &bitD2);
2385*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_1(op3, &bitD3);
2386*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_1(op4, &bitD4);
2387*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_2(op1, &bitD1);
2388*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_2(op2, &bitD2);
2389*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_2(op3, &bitD3);
2390*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_2(op4, &bitD4);
2391*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_0(op1, &bitD1);
2392*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_0(op2, &bitD2);
2393*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_0(op3, &bitD3);
2394*01826a49SYabin Cui HUFv05_DECODE_SYMBOLX4_0(op4, &bitD4);
2395*01826a49SYabin Cui
2396*01826a49SYabin Cui endSignal = BITv05_reloadDStream(&bitD1) | BITv05_reloadDStream(&bitD2) | BITv05_reloadDStream(&bitD3) | BITv05_reloadDStream(&bitD4);
2397*01826a49SYabin Cui }
2398*01826a49SYabin Cui
2399*01826a49SYabin Cui /* check corruption */
2400*01826a49SYabin Cui if (op1 > opStart2) return ERROR(corruption_detected);
2401*01826a49SYabin Cui if (op2 > opStart3) return ERROR(corruption_detected);
2402*01826a49SYabin Cui if (op3 > opStart4) return ERROR(corruption_detected);
2403*01826a49SYabin Cui /* note : op4 supposed already verified within main loop */
2404*01826a49SYabin Cui
2405*01826a49SYabin Cui /* finish bitStreams one by one */
2406*01826a49SYabin Cui HUFv05_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog);
2407*01826a49SYabin Cui HUFv05_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog);
2408*01826a49SYabin Cui HUFv05_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog);
2409*01826a49SYabin Cui HUFv05_decodeStreamX4(op4, &bitD4, oend, dt, dtLog);
2410*01826a49SYabin Cui
2411*01826a49SYabin Cui /* check */
2412*01826a49SYabin Cui endSignal = BITv05_endOfDStream(&bitD1) & BITv05_endOfDStream(&bitD2) & BITv05_endOfDStream(&bitD3) & BITv05_endOfDStream(&bitD4);
2413*01826a49SYabin Cui if (!endSignal) return ERROR(corruption_detected);
2414*01826a49SYabin Cui
2415*01826a49SYabin Cui /* decoded size */
2416*01826a49SYabin Cui return dstSize;
2417*01826a49SYabin Cui }
2418*01826a49SYabin Cui }
2419*01826a49SYabin Cui
2420*01826a49SYabin Cui
HUFv05_decompress4X4(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)2421*01826a49SYabin Cui size_t HUFv05_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2422*01826a49SYabin Cui {
2423*01826a49SYabin Cui HUFv05_CREATE_STATIC_DTABLEX4(DTable, HUFv05_MAX_TABLELOG);
2424*01826a49SYabin Cui const BYTE* ip = (const BYTE*) cSrc;
2425*01826a49SYabin Cui
2426*01826a49SYabin Cui size_t hSize = HUFv05_readDTableX4 (DTable, cSrc, cSrcSize);
2427*01826a49SYabin Cui if (HUFv05_isError(hSize)) return hSize;
2428*01826a49SYabin Cui if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
2429*01826a49SYabin Cui ip += hSize;
2430*01826a49SYabin Cui cSrcSize -= hSize;
2431*01826a49SYabin Cui
2432*01826a49SYabin Cui return HUFv05_decompress4X4_usingDTable (dst, dstSize, ip, cSrcSize, DTable);
2433*01826a49SYabin Cui }
2434*01826a49SYabin Cui
2435*01826a49SYabin Cui
2436*01826a49SYabin Cui /* ********************************/
2437*01826a49SYabin Cui /* Generic decompression selector */
2438*01826a49SYabin Cui /* ********************************/
2439*01826a49SYabin Cui
2440*01826a49SYabin Cui typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t;
2441*01826a49SYabin Cui static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] =
2442*01826a49SYabin Cui {
2443*01826a49SYabin Cui /* single, double, quad */
2444*01826a49SYabin Cui {{0,0}, {1,1}, {2,2}}, /* Q==0 : impossible */
2445*01826a49SYabin Cui {{0,0}, {1,1}, {2,2}}, /* Q==1 : impossible */
2446*01826a49SYabin Cui {{ 38,130}, {1313, 74}, {2151, 38}}, /* Q == 2 : 12-18% */
2447*01826a49SYabin Cui {{ 448,128}, {1353, 74}, {2238, 41}}, /* Q == 3 : 18-25% */
2448*01826a49SYabin Cui {{ 556,128}, {1353, 74}, {2238, 47}}, /* Q == 4 : 25-32% */
2449*01826a49SYabin Cui {{ 714,128}, {1418, 74}, {2436, 53}}, /* Q == 5 : 32-38% */
2450*01826a49SYabin Cui {{ 883,128}, {1437, 74}, {2464, 61}}, /* Q == 6 : 38-44% */
2451*01826a49SYabin Cui {{ 897,128}, {1515, 75}, {2622, 68}}, /* Q == 7 : 44-50% */
2452*01826a49SYabin Cui {{ 926,128}, {1613, 75}, {2730, 75}}, /* Q == 8 : 50-56% */
2453*01826a49SYabin Cui {{ 947,128}, {1729, 77}, {3359, 77}}, /* Q == 9 : 56-62% */
2454*01826a49SYabin Cui {{1107,128}, {2083, 81}, {4006, 84}}, /* Q ==10 : 62-69% */
2455*01826a49SYabin Cui {{1177,128}, {2379, 87}, {4785, 88}}, /* Q ==11 : 69-75% */
2456*01826a49SYabin Cui {{1242,128}, {2415, 93}, {5155, 84}}, /* Q ==12 : 75-81% */
2457*01826a49SYabin Cui {{1349,128}, {2644,106}, {5260,106}}, /* Q ==13 : 81-87% */
2458*01826a49SYabin Cui {{1455,128}, {2422,124}, {4174,124}}, /* Q ==14 : 87-93% */
2459*01826a49SYabin Cui {{ 722,128}, {1891,145}, {1936,146}}, /* Q ==15 : 93-99% */
2460*01826a49SYabin Cui };
2461*01826a49SYabin Cui
2462*01826a49SYabin Cui typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
2463*01826a49SYabin Cui
HUFv05_decompress(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)2464*01826a49SYabin Cui size_t HUFv05_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2465*01826a49SYabin Cui {
2466*01826a49SYabin Cui static const decompressionAlgo decompress[3] = { HUFv05_decompress4X2, HUFv05_decompress4X4, NULL };
2467*01826a49SYabin Cui /* estimate decompression time */
2468*01826a49SYabin Cui U32 Q;
2469*01826a49SYabin Cui const U32 D256 = (U32)(dstSize >> 8);
2470*01826a49SYabin Cui U32 Dtime[3];
2471*01826a49SYabin Cui U32 algoNb = 0;
2472*01826a49SYabin Cui int n;
2473*01826a49SYabin Cui
2474*01826a49SYabin Cui /* validation checks */
2475*01826a49SYabin Cui if (dstSize == 0) return ERROR(dstSize_tooSmall);
2476*01826a49SYabin Cui if (cSrcSize >= dstSize) return ERROR(corruption_detected); /* invalid, or not compressed, but not compressed already dealt with */
2477*01826a49SYabin Cui if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
2478*01826a49SYabin Cui
2479*01826a49SYabin Cui /* decoder timing evaluation */
2480*01826a49SYabin Cui Q = (U32)(cSrcSize * 16 / dstSize); /* Q < 16 since dstSize > cSrcSize */
2481*01826a49SYabin Cui for (n=0; n<3; n++)
2482*01826a49SYabin Cui Dtime[n] = algoTime[Q][n].tableTime + (algoTime[Q][n].decode256Time * D256);
2483*01826a49SYabin Cui
2484*01826a49SYabin Cui Dtime[1] += Dtime[1] >> 4; Dtime[2] += Dtime[2] >> 3; /* advantage to algorithms using less memory, for cache eviction */
2485*01826a49SYabin Cui
2486*01826a49SYabin Cui if (Dtime[1] < Dtime[0]) algoNb = 1;
2487*01826a49SYabin Cui
2488*01826a49SYabin Cui return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
2489*01826a49SYabin Cui
2490*01826a49SYabin Cui /* return HUFv05_decompress4X2(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams single-symbol decoding */
2491*01826a49SYabin Cui /* return HUFv05_decompress4X4(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams double-symbols decoding */
2492*01826a49SYabin Cui /* return HUFv05_decompress4X6(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams quad-symbols decoding */
2493*01826a49SYabin Cui }
2494*01826a49SYabin Cui /*
2495*01826a49SYabin Cui zstd - standard compression library
2496*01826a49SYabin Cui Copyright (C) 2014-2016, Yann Collet.
2497*01826a49SYabin Cui
2498*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
2499*01826a49SYabin Cui
2500*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
2501*01826a49SYabin Cui modification, are permitted provided that the following conditions are
2502*01826a49SYabin Cui met:
2503*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
2504*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
2505*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
2506*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
2507*01826a49SYabin Cui in the documentation and/or other materials provided with the
2508*01826a49SYabin Cui distribution.
2509*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2510*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2511*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2512*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2513*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2514*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2515*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2516*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2517*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2518*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2519*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2520*01826a49SYabin Cui
2521*01826a49SYabin Cui You can contact the author at :
2522*01826a49SYabin Cui - zstd source repository : https://github.com/Cyan4973/zstd
2523*01826a49SYabin Cui */
2524*01826a49SYabin Cui
2525*01826a49SYabin Cui /* ***************************************************************
2526*01826a49SYabin Cui * Tuning parameters
2527*01826a49SYabin Cui *****************************************************************/
2528*01826a49SYabin Cui /*!
2529*01826a49SYabin Cui * HEAPMODE :
2530*01826a49SYabin Cui * Select how default decompression function ZSTDv05_decompress() will allocate memory,
2531*01826a49SYabin Cui * in memory stack (0), or in memory heap (1, requires malloc())
2532*01826a49SYabin Cui */
2533*01826a49SYabin Cui #ifndef ZSTDv05_HEAPMODE
2534*01826a49SYabin Cui # define ZSTDv05_HEAPMODE 1
2535*01826a49SYabin Cui #endif
2536*01826a49SYabin Cui
2537*01826a49SYabin Cui
2538*01826a49SYabin Cui /*-*******************************************************
2539*01826a49SYabin Cui * Dependencies
2540*01826a49SYabin Cui *********************************************************/
2541*01826a49SYabin Cui #include <stdlib.h> /* calloc */
2542*01826a49SYabin Cui #include <string.h> /* memcpy, memmove */
2543*01826a49SYabin Cui #include <stdio.h> /* debug only : printf */
2544*01826a49SYabin Cui
2545*01826a49SYabin Cui
2546*01826a49SYabin Cui /*-*******************************************************
2547*01826a49SYabin Cui * Compiler specifics
2548*01826a49SYabin Cui *********************************************************/
2549*01826a49SYabin Cui #ifdef _MSC_VER /* Visual Studio */
2550*01826a49SYabin Cui # include <intrin.h> /* For Visual 2005 */
2551*01826a49SYabin Cui # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2552*01826a49SYabin Cui # pragma warning(disable : 4324) /* disable: C4324: padded structure */
2553*01826a49SYabin Cui #endif
2554*01826a49SYabin Cui
2555*01826a49SYabin Cui
2556*01826a49SYabin Cui /*-*************************************
2557*01826a49SYabin Cui * Local types
2558*01826a49SYabin Cui ***************************************/
2559*01826a49SYabin Cui typedef struct
2560*01826a49SYabin Cui {
2561*01826a49SYabin Cui blockType_t blockType;
2562*01826a49SYabin Cui U32 origSize;
2563*01826a49SYabin Cui } blockProperties_t;
2564*01826a49SYabin Cui
2565*01826a49SYabin Cui
2566*01826a49SYabin Cui /* *******************************************************
2567*01826a49SYabin Cui * Memory operations
2568*01826a49SYabin Cui **********************************************************/
ZSTDv05_copy4(void * dst,const void * src)2569*01826a49SYabin Cui static void ZSTDv05_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
2570*01826a49SYabin Cui
2571*01826a49SYabin Cui
2572*01826a49SYabin Cui /* *************************************
2573*01826a49SYabin Cui * Error Management
2574*01826a49SYabin Cui ***************************************/
2575*01826a49SYabin Cui /*! ZSTDv05_isError() :
2576*01826a49SYabin Cui * tells if a return value is an error code */
ZSTDv05_isError(size_t code)2577*01826a49SYabin Cui unsigned ZSTDv05_isError(size_t code) { return ERR_isError(code); }
2578*01826a49SYabin Cui
2579*01826a49SYabin Cui
2580*01826a49SYabin Cui /*! ZSTDv05_getErrorName() :
2581*01826a49SYabin Cui * provides error code string (useful for debugging) */
ZSTDv05_getErrorName(size_t code)2582*01826a49SYabin Cui const char* ZSTDv05_getErrorName(size_t code) { return ERR_getErrorName(code); }
2583*01826a49SYabin Cui
2584*01826a49SYabin Cui
2585*01826a49SYabin Cui /* *************************************************************
2586*01826a49SYabin Cui * Context management
2587*01826a49SYabin Cui ***************************************************************/
2588*01826a49SYabin Cui typedef enum { ZSTDv05ds_getFrameHeaderSize, ZSTDv05ds_decodeFrameHeader,
2589*01826a49SYabin Cui ZSTDv05ds_decodeBlockHeader, ZSTDv05ds_decompressBlock } ZSTDv05_dStage;
2590*01826a49SYabin Cui
2591*01826a49SYabin Cui struct ZSTDv05_DCtx_s
2592*01826a49SYabin Cui {
2593*01826a49SYabin Cui FSEv05_DTable LLTable[FSEv05_DTABLE_SIZE_U32(LLFSEv05Log)];
2594*01826a49SYabin Cui FSEv05_DTable OffTable[FSEv05_DTABLE_SIZE_U32(OffFSEv05Log)];
2595*01826a49SYabin Cui FSEv05_DTable MLTable[FSEv05_DTABLE_SIZE_U32(MLFSEv05Log)];
2596*01826a49SYabin Cui unsigned hufTableX4[HUFv05_DTABLE_SIZE(ZSTD_HUFFDTABLE_CAPACITY_LOG)];
2597*01826a49SYabin Cui const void* previousDstEnd;
2598*01826a49SYabin Cui const void* base;
2599*01826a49SYabin Cui const void* vBase;
2600*01826a49SYabin Cui const void* dictEnd;
2601*01826a49SYabin Cui size_t expected;
2602*01826a49SYabin Cui size_t headerSize;
2603*01826a49SYabin Cui ZSTDv05_parameters params;
2604*01826a49SYabin Cui blockType_t bType; /* used in ZSTDv05_decompressContinue(), to transfer blockType between header decoding and block decoding stages */
2605*01826a49SYabin Cui ZSTDv05_dStage stage;
2606*01826a49SYabin Cui U32 flagStaticTables;
2607*01826a49SYabin Cui const BYTE* litPtr;
2608*01826a49SYabin Cui size_t litSize;
2609*01826a49SYabin Cui BYTE litBuffer[BLOCKSIZE + WILDCOPY_OVERLENGTH];
2610*01826a49SYabin Cui BYTE headerBuffer[ZSTDv05_frameHeaderSize_max];
2611*01826a49SYabin Cui }; /* typedef'd to ZSTDv05_DCtx within "zstd_static.h" */
2612*01826a49SYabin Cui
2613*01826a49SYabin Cui size_t ZSTDv05_sizeofDCtx (void); /* Hidden declaration */
ZSTDv05_sizeofDCtx(void)2614*01826a49SYabin Cui size_t ZSTDv05_sizeofDCtx (void) { return sizeof(ZSTDv05_DCtx); }
2615*01826a49SYabin Cui
ZSTDv05_decompressBegin(ZSTDv05_DCtx * dctx)2616*01826a49SYabin Cui size_t ZSTDv05_decompressBegin(ZSTDv05_DCtx* dctx)
2617*01826a49SYabin Cui {
2618*01826a49SYabin Cui dctx->expected = ZSTDv05_frameHeaderSize_min;
2619*01826a49SYabin Cui dctx->stage = ZSTDv05ds_getFrameHeaderSize;
2620*01826a49SYabin Cui dctx->previousDstEnd = NULL;
2621*01826a49SYabin Cui dctx->base = NULL;
2622*01826a49SYabin Cui dctx->vBase = NULL;
2623*01826a49SYabin Cui dctx->dictEnd = NULL;
2624*01826a49SYabin Cui dctx->hufTableX4[0] = ZSTD_HUFFDTABLE_CAPACITY_LOG;
2625*01826a49SYabin Cui dctx->flagStaticTables = 0;
2626*01826a49SYabin Cui return 0;
2627*01826a49SYabin Cui }
2628*01826a49SYabin Cui
ZSTDv05_createDCtx(void)2629*01826a49SYabin Cui ZSTDv05_DCtx* ZSTDv05_createDCtx(void)
2630*01826a49SYabin Cui {
2631*01826a49SYabin Cui ZSTDv05_DCtx* dctx = (ZSTDv05_DCtx*)malloc(sizeof(ZSTDv05_DCtx));
2632*01826a49SYabin Cui if (dctx==NULL) return NULL;
2633*01826a49SYabin Cui ZSTDv05_decompressBegin(dctx);
2634*01826a49SYabin Cui return dctx;
2635*01826a49SYabin Cui }
2636*01826a49SYabin Cui
ZSTDv05_freeDCtx(ZSTDv05_DCtx * dctx)2637*01826a49SYabin Cui size_t ZSTDv05_freeDCtx(ZSTDv05_DCtx* dctx)
2638*01826a49SYabin Cui {
2639*01826a49SYabin Cui free(dctx);
2640*01826a49SYabin Cui return 0; /* reserved as a potential error code in the future */
2641*01826a49SYabin Cui }
2642*01826a49SYabin Cui
ZSTDv05_copyDCtx(ZSTDv05_DCtx * dstDCtx,const ZSTDv05_DCtx * srcDCtx)2643*01826a49SYabin Cui void ZSTDv05_copyDCtx(ZSTDv05_DCtx* dstDCtx, const ZSTDv05_DCtx* srcDCtx)
2644*01826a49SYabin Cui {
2645*01826a49SYabin Cui memcpy(dstDCtx, srcDCtx,
2646*01826a49SYabin Cui sizeof(ZSTDv05_DCtx) - (BLOCKSIZE+WILDCOPY_OVERLENGTH + ZSTDv05_frameHeaderSize_max)); /* no need to copy workspace */
2647*01826a49SYabin Cui }
2648*01826a49SYabin Cui
2649*01826a49SYabin Cui
2650*01826a49SYabin Cui /* *************************************************************
2651*01826a49SYabin Cui * Decompression section
2652*01826a49SYabin Cui ***************************************************************/
2653*01826a49SYabin Cui
2654*01826a49SYabin Cui /* Frame format description
2655*01826a49SYabin Cui Frame Header - [ Block Header - Block ] - Frame End
2656*01826a49SYabin Cui 1) Frame Header
2657*01826a49SYabin Cui - 4 bytes - Magic Number : ZSTDv05_MAGICNUMBER (defined within zstd_internal.h)
2658*01826a49SYabin Cui - 1 byte - Window Descriptor
2659*01826a49SYabin Cui 2) Block Header
2660*01826a49SYabin Cui - 3 bytes, starting with a 2-bits descriptor
2661*01826a49SYabin Cui Uncompressed, Compressed, Frame End, unused
2662*01826a49SYabin Cui 3) Block
2663*01826a49SYabin Cui See Block Format Description
2664*01826a49SYabin Cui 4) Frame End
2665*01826a49SYabin Cui - 3 bytes, compatible with Block Header
2666*01826a49SYabin Cui */
2667*01826a49SYabin Cui
2668*01826a49SYabin Cui /* Block format description
2669*01826a49SYabin Cui
2670*01826a49SYabin Cui Block = Literal Section - Sequences Section
2671*01826a49SYabin Cui Prerequisite : size of (compressed) block, maximum size of regenerated data
2672*01826a49SYabin Cui
2673*01826a49SYabin Cui 1) Literal Section
2674*01826a49SYabin Cui
2675*01826a49SYabin Cui 1.1) Header : 1-5 bytes
2676*01826a49SYabin Cui flags: 2 bits
2677*01826a49SYabin Cui 00 compressed by Huff0
2678*01826a49SYabin Cui 01 unused
2679*01826a49SYabin Cui 10 is Raw (uncompressed)
2680*01826a49SYabin Cui 11 is Rle
2681*01826a49SYabin Cui Note : using 01 => Huff0 with precomputed table ?
2682*01826a49SYabin Cui Note : delta map ? => compressed ?
2683*01826a49SYabin Cui
2684*01826a49SYabin Cui 1.1.1) Huff0-compressed literal block : 3-5 bytes
2685*01826a49SYabin Cui srcSize < 1 KB => 3 bytes (2-2-10-10) => single stream
2686*01826a49SYabin Cui srcSize < 1 KB => 3 bytes (2-2-10-10)
2687*01826a49SYabin Cui srcSize < 16KB => 4 bytes (2-2-14-14)
2688*01826a49SYabin Cui else => 5 bytes (2-2-18-18)
2689*01826a49SYabin Cui big endian convention
2690*01826a49SYabin Cui
2691*01826a49SYabin Cui 1.1.2) Raw (uncompressed) literal block header : 1-3 bytes
2692*01826a49SYabin Cui size : 5 bits: (IS_RAW<<6) + (0<<4) + size
2693*01826a49SYabin Cui 12 bits: (IS_RAW<<6) + (2<<4) + (size>>8)
2694*01826a49SYabin Cui size&255
2695*01826a49SYabin Cui 20 bits: (IS_RAW<<6) + (3<<4) + (size>>16)
2696*01826a49SYabin Cui size>>8&255
2697*01826a49SYabin Cui size&255
2698*01826a49SYabin Cui
2699*01826a49SYabin Cui 1.1.3) Rle (repeated single byte) literal block header : 1-3 bytes
2700*01826a49SYabin Cui size : 5 bits: (IS_RLE<<6) + (0<<4) + size
2701*01826a49SYabin Cui 12 bits: (IS_RLE<<6) + (2<<4) + (size>>8)
2702*01826a49SYabin Cui size&255
2703*01826a49SYabin Cui 20 bits: (IS_RLE<<6) + (3<<4) + (size>>16)
2704*01826a49SYabin Cui size>>8&255
2705*01826a49SYabin Cui size&255
2706*01826a49SYabin Cui
2707*01826a49SYabin Cui 1.1.4) Huff0-compressed literal block, using precomputed CTables : 3-5 bytes
2708*01826a49SYabin Cui srcSize < 1 KB => 3 bytes (2-2-10-10) => single stream
2709*01826a49SYabin Cui srcSize < 1 KB => 3 bytes (2-2-10-10)
2710*01826a49SYabin Cui srcSize < 16KB => 4 bytes (2-2-14-14)
2711*01826a49SYabin Cui else => 5 bytes (2-2-18-18)
2712*01826a49SYabin Cui big endian convention
2713*01826a49SYabin Cui
2714*01826a49SYabin Cui 1- CTable available (stored into workspace ?)
2715*01826a49SYabin Cui 2- Small input (fast heuristic ? Full comparison ? depend on clevel ?)
2716*01826a49SYabin Cui
2717*01826a49SYabin Cui
2718*01826a49SYabin Cui 1.2) Literal block content
2719*01826a49SYabin Cui
2720*01826a49SYabin Cui 1.2.1) Huff0 block, using sizes from header
2721*01826a49SYabin Cui See Huff0 format
2722*01826a49SYabin Cui
2723*01826a49SYabin Cui 1.2.2) Huff0 block, using prepared table
2724*01826a49SYabin Cui
2725*01826a49SYabin Cui 1.2.3) Raw content
2726*01826a49SYabin Cui
2727*01826a49SYabin Cui 1.2.4) single byte
2728*01826a49SYabin Cui
2729*01826a49SYabin Cui
2730*01826a49SYabin Cui 2) Sequences section
2731*01826a49SYabin Cui TO DO
2732*01826a49SYabin Cui */
2733*01826a49SYabin Cui
2734*01826a49SYabin Cui
2735*01826a49SYabin Cui /** ZSTDv05_decodeFrameHeader_Part1() :
2736*01826a49SYabin Cui * decode the 1st part of the Frame Header, which tells Frame Header size.
2737*01826a49SYabin Cui * srcSize must be == ZSTDv05_frameHeaderSize_min.
2738*01826a49SYabin Cui * @return : the full size of the Frame Header */
ZSTDv05_decodeFrameHeader_Part1(ZSTDv05_DCtx * zc,const void * src,size_t srcSize)2739*01826a49SYabin Cui static size_t ZSTDv05_decodeFrameHeader_Part1(ZSTDv05_DCtx* zc, const void* src, size_t srcSize)
2740*01826a49SYabin Cui {
2741*01826a49SYabin Cui U32 magicNumber;
2742*01826a49SYabin Cui if (srcSize != ZSTDv05_frameHeaderSize_min)
2743*01826a49SYabin Cui return ERROR(srcSize_wrong);
2744*01826a49SYabin Cui magicNumber = MEM_readLE32(src);
2745*01826a49SYabin Cui if (magicNumber != ZSTDv05_MAGICNUMBER) return ERROR(prefix_unknown);
2746*01826a49SYabin Cui zc->headerSize = ZSTDv05_frameHeaderSize_min;
2747*01826a49SYabin Cui return zc->headerSize;
2748*01826a49SYabin Cui }
2749*01826a49SYabin Cui
2750*01826a49SYabin Cui
ZSTDv05_getFrameParams(ZSTDv05_parameters * params,const void * src,size_t srcSize)2751*01826a49SYabin Cui size_t ZSTDv05_getFrameParams(ZSTDv05_parameters* params, const void* src, size_t srcSize)
2752*01826a49SYabin Cui {
2753*01826a49SYabin Cui U32 magicNumber;
2754*01826a49SYabin Cui if (srcSize < ZSTDv05_frameHeaderSize_min) return ZSTDv05_frameHeaderSize_max;
2755*01826a49SYabin Cui magicNumber = MEM_readLE32(src);
2756*01826a49SYabin Cui if (magicNumber != ZSTDv05_MAGICNUMBER) return ERROR(prefix_unknown);
2757*01826a49SYabin Cui memset(params, 0, sizeof(*params));
2758*01826a49SYabin Cui params->windowLog = (((const BYTE*)src)[4] & 15) + ZSTDv05_WINDOWLOG_ABSOLUTEMIN;
2759*01826a49SYabin Cui if ((((const BYTE*)src)[4] >> 4) != 0) return ERROR(frameParameter_unsupported); /* reserved bits */
2760*01826a49SYabin Cui return 0;
2761*01826a49SYabin Cui }
2762*01826a49SYabin Cui
2763*01826a49SYabin Cui /** ZSTDv05_decodeFrameHeader_Part2() :
2764*01826a49SYabin Cui * decode the full Frame Header.
2765*01826a49SYabin Cui * srcSize must be the size provided by ZSTDv05_decodeFrameHeader_Part1().
2766*01826a49SYabin Cui * @return : 0, or an error code, which can be tested using ZSTDv05_isError() */
ZSTDv05_decodeFrameHeader_Part2(ZSTDv05_DCtx * zc,const void * src,size_t srcSize)2767*01826a49SYabin Cui static size_t ZSTDv05_decodeFrameHeader_Part2(ZSTDv05_DCtx* zc, const void* src, size_t srcSize)
2768*01826a49SYabin Cui {
2769*01826a49SYabin Cui size_t result;
2770*01826a49SYabin Cui if (srcSize != zc->headerSize)
2771*01826a49SYabin Cui return ERROR(srcSize_wrong);
2772*01826a49SYabin Cui result = ZSTDv05_getFrameParams(&(zc->params), src, srcSize);
2773*01826a49SYabin Cui if ((MEM_32bits()) && (zc->params.windowLog > 25)) return ERROR(frameParameter_unsupported);
2774*01826a49SYabin Cui return result;
2775*01826a49SYabin Cui }
2776*01826a49SYabin Cui
2777*01826a49SYabin Cui
ZSTDv05_getcBlockSize(const void * src,size_t srcSize,blockProperties_t * bpPtr)2778*01826a49SYabin Cui static size_t ZSTDv05_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
2779*01826a49SYabin Cui {
2780*01826a49SYabin Cui const BYTE* const in = (const BYTE*)src;
2781*01826a49SYabin Cui BYTE headerFlags;
2782*01826a49SYabin Cui U32 cSize;
2783*01826a49SYabin Cui
2784*01826a49SYabin Cui if (srcSize < 3)
2785*01826a49SYabin Cui return ERROR(srcSize_wrong);
2786*01826a49SYabin Cui
2787*01826a49SYabin Cui headerFlags = *in;
2788*01826a49SYabin Cui cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16);
2789*01826a49SYabin Cui
2790*01826a49SYabin Cui bpPtr->blockType = (blockType_t)(headerFlags >> 6);
2791*01826a49SYabin Cui bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0;
2792*01826a49SYabin Cui
2793*01826a49SYabin Cui if (bpPtr->blockType == bt_end) return 0;
2794*01826a49SYabin Cui if (bpPtr->blockType == bt_rle) return 1;
2795*01826a49SYabin Cui return cSize;
2796*01826a49SYabin Cui }
2797*01826a49SYabin Cui
2798*01826a49SYabin Cui
ZSTDv05_copyRawBlock(void * dst,size_t maxDstSize,const void * src,size_t srcSize)2799*01826a49SYabin Cui static size_t ZSTDv05_copyRawBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2800*01826a49SYabin Cui {
2801*01826a49SYabin Cui if (dst==NULL) return ERROR(dstSize_tooSmall);
2802*01826a49SYabin Cui if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2803*01826a49SYabin Cui memcpy(dst, src, srcSize);
2804*01826a49SYabin Cui return srcSize;
2805*01826a49SYabin Cui }
2806*01826a49SYabin Cui
2807*01826a49SYabin Cui
2808*01826a49SYabin Cui /*! ZSTDv05_decodeLiteralsBlock() :
2809*01826a49SYabin Cui @return : nb of bytes read from src (< srcSize ) */
ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx * dctx,const void * src,size_t srcSize)2810*01826a49SYabin Cui static size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
2811*01826a49SYabin Cui const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
2812*01826a49SYabin Cui {
2813*01826a49SYabin Cui const BYTE* const istart = (const BYTE*) src;
2814*01826a49SYabin Cui
2815*01826a49SYabin Cui /* any compressed block with literals segment must be at least this size */
2816*01826a49SYabin Cui if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected);
2817*01826a49SYabin Cui
2818*01826a49SYabin Cui switch(istart[0]>> 6)
2819*01826a49SYabin Cui {
2820*01826a49SYabin Cui case IS_HUFv05:
2821*01826a49SYabin Cui {
2822*01826a49SYabin Cui size_t litSize, litCSize, singleStream=0;
2823*01826a49SYabin Cui U32 lhSize = ((istart[0]) >> 4) & 3;
2824*01826a49SYabin Cui if (srcSize < 5) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3 */
2825*01826a49SYabin Cui switch(lhSize)
2826*01826a49SYabin Cui {
2827*01826a49SYabin Cui case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */
2828*01826a49SYabin Cui /* 2 - 2 - 10 - 10 */
2829*01826a49SYabin Cui lhSize=3;
2830*01826a49SYabin Cui singleStream = istart[0] & 16;
2831*01826a49SYabin Cui litSize = ((istart[0] & 15) << 6) + (istart[1] >> 2);
2832*01826a49SYabin Cui litCSize = ((istart[1] & 3) << 8) + istart[2];
2833*01826a49SYabin Cui break;
2834*01826a49SYabin Cui case 2:
2835*01826a49SYabin Cui /* 2 - 2 - 14 - 14 */
2836*01826a49SYabin Cui lhSize=4;
2837*01826a49SYabin Cui litSize = ((istart[0] & 15) << 10) + (istart[1] << 2) + (istart[2] >> 6);
2838*01826a49SYabin Cui litCSize = ((istart[2] & 63) << 8) + istart[3];
2839*01826a49SYabin Cui break;
2840*01826a49SYabin Cui case 3:
2841*01826a49SYabin Cui /* 2 - 2 - 18 - 18 */
2842*01826a49SYabin Cui lhSize=5;
2843*01826a49SYabin Cui litSize = ((istart[0] & 15) << 14) + (istart[1] << 6) + (istart[2] >> 2);
2844*01826a49SYabin Cui litCSize = ((istart[2] & 3) << 16) + (istart[3] << 8) + istart[4];
2845*01826a49SYabin Cui break;
2846*01826a49SYabin Cui }
2847*01826a49SYabin Cui if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2848*01826a49SYabin Cui if (litCSize + lhSize > srcSize) return ERROR(corruption_detected);
2849*01826a49SYabin Cui
2850*01826a49SYabin Cui if (HUFv05_isError(singleStream ?
2851*01826a49SYabin Cui HUFv05_decompress1X2(dctx->litBuffer, litSize, istart+lhSize, litCSize) :
2852*01826a49SYabin Cui HUFv05_decompress (dctx->litBuffer, litSize, istart+lhSize, litCSize) ))
2853*01826a49SYabin Cui return ERROR(corruption_detected);
2854*01826a49SYabin Cui
2855*01826a49SYabin Cui dctx->litPtr = dctx->litBuffer;
2856*01826a49SYabin Cui dctx->litSize = litSize;
2857*01826a49SYabin Cui memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
2858*01826a49SYabin Cui return litCSize + lhSize;
2859*01826a49SYabin Cui }
2860*01826a49SYabin Cui case IS_PCH:
2861*01826a49SYabin Cui {
2862*01826a49SYabin Cui size_t errorCode;
2863*01826a49SYabin Cui size_t litSize, litCSize;
2864*01826a49SYabin Cui U32 lhSize = ((istart[0]) >> 4) & 3;
2865*01826a49SYabin Cui if (lhSize != 1) /* only case supported for now : small litSize, single stream */
2866*01826a49SYabin Cui return ERROR(corruption_detected);
2867*01826a49SYabin Cui if (!dctx->flagStaticTables)
2868*01826a49SYabin Cui return ERROR(dictionary_corrupted);
2869*01826a49SYabin Cui
2870*01826a49SYabin Cui /* 2 - 2 - 10 - 10 */
2871*01826a49SYabin Cui lhSize=3;
2872*01826a49SYabin Cui litSize = ((istart[0] & 15) << 6) + (istart[1] >> 2);
2873*01826a49SYabin Cui litCSize = ((istart[1] & 3) << 8) + istart[2];
2874*01826a49SYabin Cui if (litCSize + lhSize > srcSize) return ERROR(corruption_detected);
2875*01826a49SYabin Cui
2876*01826a49SYabin Cui errorCode = HUFv05_decompress1X4_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->hufTableX4);
2877*01826a49SYabin Cui if (HUFv05_isError(errorCode)) return ERROR(corruption_detected);
2878*01826a49SYabin Cui
2879*01826a49SYabin Cui dctx->litPtr = dctx->litBuffer;
2880*01826a49SYabin Cui dctx->litSize = litSize;
2881*01826a49SYabin Cui memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
2882*01826a49SYabin Cui return litCSize + lhSize;
2883*01826a49SYabin Cui }
2884*01826a49SYabin Cui case IS_RAW:
2885*01826a49SYabin Cui {
2886*01826a49SYabin Cui size_t litSize;
2887*01826a49SYabin Cui U32 lhSize = ((istart[0]) >> 4) & 3;
2888*01826a49SYabin Cui switch(lhSize)
2889*01826a49SYabin Cui {
2890*01826a49SYabin Cui case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */
2891*01826a49SYabin Cui lhSize=1;
2892*01826a49SYabin Cui litSize = istart[0] & 31;
2893*01826a49SYabin Cui break;
2894*01826a49SYabin Cui case 2:
2895*01826a49SYabin Cui litSize = ((istart[0] & 15) << 8) + istart[1];
2896*01826a49SYabin Cui break;
2897*01826a49SYabin Cui case 3:
2898*01826a49SYabin Cui litSize = ((istart[0] & 15) << 16) + (istart[1] << 8) + istart[2];
2899*01826a49SYabin Cui break;
2900*01826a49SYabin Cui }
2901*01826a49SYabin Cui
2902*01826a49SYabin Cui if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */
2903*01826a49SYabin Cui if (litSize+lhSize > srcSize) return ERROR(corruption_detected);
2904*01826a49SYabin Cui memcpy(dctx->litBuffer, istart+lhSize, litSize);
2905*01826a49SYabin Cui dctx->litPtr = dctx->litBuffer;
2906*01826a49SYabin Cui dctx->litSize = litSize;
2907*01826a49SYabin Cui memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
2908*01826a49SYabin Cui return lhSize+litSize;
2909*01826a49SYabin Cui }
2910*01826a49SYabin Cui /* direct reference into compressed stream */
2911*01826a49SYabin Cui dctx->litPtr = istart+lhSize;
2912*01826a49SYabin Cui dctx->litSize = litSize;
2913*01826a49SYabin Cui return lhSize+litSize;
2914*01826a49SYabin Cui }
2915*01826a49SYabin Cui case IS_RLE:
2916*01826a49SYabin Cui {
2917*01826a49SYabin Cui size_t litSize;
2918*01826a49SYabin Cui U32 lhSize = ((istart[0]) >> 4) & 3;
2919*01826a49SYabin Cui switch(lhSize)
2920*01826a49SYabin Cui {
2921*01826a49SYabin Cui case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */
2922*01826a49SYabin Cui lhSize = 1;
2923*01826a49SYabin Cui litSize = istart[0] & 31;
2924*01826a49SYabin Cui break;
2925*01826a49SYabin Cui case 2:
2926*01826a49SYabin Cui litSize = ((istart[0] & 15) << 8) + istart[1];
2927*01826a49SYabin Cui break;
2928*01826a49SYabin Cui case 3:
2929*01826a49SYabin Cui litSize = ((istart[0] & 15) << 16) + (istart[1] << 8) + istart[2];
2930*01826a49SYabin Cui if (srcSize<4) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4 */
2931*01826a49SYabin Cui break;
2932*01826a49SYabin Cui }
2933*01826a49SYabin Cui if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2934*01826a49SYabin Cui memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH);
2935*01826a49SYabin Cui dctx->litPtr = dctx->litBuffer;
2936*01826a49SYabin Cui dctx->litSize = litSize;
2937*01826a49SYabin Cui return lhSize+1;
2938*01826a49SYabin Cui }
2939*01826a49SYabin Cui default:
2940*01826a49SYabin Cui return ERROR(corruption_detected); /* impossible */
2941*01826a49SYabin Cui }
2942*01826a49SYabin Cui }
2943*01826a49SYabin Cui
2944*01826a49SYabin Cui
ZSTDv05_decodeSeqHeaders(int * nbSeq,const BYTE ** dumpsPtr,size_t * dumpsLengthPtr,FSEv05_DTable * DTableLL,FSEv05_DTable * DTableML,FSEv05_DTable * DTableOffb,const void * src,size_t srcSize,U32 flagStaticTable)2945*01826a49SYabin Cui static size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr,
2946*01826a49SYabin Cui FSEv05_DTable* DTableLL, FSEv05_DTable* DTableML, FSEv05_DTable* DTableOffb,
2947*01826a49SYabin Cui const void* src, size_t srcSize, U32 flagStaticTable)
2948*01826a49SYabin Cui {
2949*01826a49SYabin Cui const BYTE* const istart = (const BYTE*)src;
2950*01826a49SYabin Cui const BYTE* ip = istart;
2951*01826a49SYabin Cui const BYTE* const iend = istart + srcSize;
2952*01826a49SYabin Cui U32 LLtype, Offtype, MLtype;
2953*01826a49SYabin Cui unsigned LLlog, Offlog, MLlog;
2954*01826a49SYabin Cui size_t dumpsLength;
2955*01826a49SYabin Cui
2956*01826a49SYabin Cui /* check */
2957*01826a49SYabin Cui if (srcSize < MIN_SEQUENCES_SIZE)
2958*01826a49SYabin Cui return ERROR(srcSize_wrong);
2959*01826a49SYabin Cui
2960*01826a49SYabin Cui /* SeqHead */
2961*01826a49SYabin Cui *nbSeq = *ip++;
2962*01826a49SYabin Cui if (*nbSeq==0) return 1;
2963*01826a49SYabin Cui if (*nbSeq >= 128) {
2964*01826a49SYabin Cui if (ip >= iend) return ERROR(srcSize_wrong);
2965*01826a49SYabin Cui *nbSeq = ((nbSeq[0]-128)<<8) + *ip++;
2966*01826a49SYabin Cui }
2967*01826a49SYabin Cui
2968*01826a49SYabin Cui if (ip >= iend) return ERROR(srcSize_wrong);
2969*01826a49SYabin Cui LLtype = *ip >> 6;
2970*01826a49SYabin Cui Offtype = (*ip >> 4) & 3;
2971*01826a49SYabin Cui MLtype = (*ip >> 2) & 3;
2972*01826a49SYabin Cui if (*ip & 2) {
2973*01826a49SYabin Cui if (ip+3 > iend) return ERROR(srcSize_wrong);
2974*01826a49SYabin Cui dumpsLength = ip[2];
2975*01826a49SYabin Cui dumpsLength += ip[1] << 8;
2976*01826a49SYabin Cui ip += 3;
2977*01826a49SYabin Cui } else {
2978*01826a49SYabin Cui if (ip+2 > iend) return ERROR(srcSize_wrong);
2979*01826a49SYabin Cui dumpsLength = ip[1];
2980*01826a49SYabin Cui dumpsLength += (ip[0] & 1) << 8;
2981*01826a49SYabin Cui ip += 2;
2982*01826a49SYabin Cui }
2983*01826a49SYabin Cui *dumpsPtr = ip;
2984*01826a49SYabin Cui ip += dumpsLength;
2985*01826a49SYabin Cui *dumpsLengthPtr = dumpsLength;
2986*01826a49SYabin Cui
2987*01826a49SYabin Cui /* check */
2988*01826a49SYabin Cui if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */
2989*01826a49SYabin Cui
2990*01826a49SYabin Cui /* sequences */
2991*01826a49SYabin Cui {
2992*01826a49SYabin Cui S16 norm[MaxML+1]; /* assumption : MaxML >= MaxLL >= MaxOff */
2993*01826a49SYabin Cui size_t headerSize;
2994*01826a49SYabin Cui
2995*01826a49SYabin Cui /* Build DTables */
2996*01826a49SYabin Cui switch(LLtype)
2997*01826a49SYabin Cui {
2998*01826a49SYabin Cui case FSEv05_ENCODING_RLE :
2999*01826a49SYabin Cui LLlog = 0;
3000*01826a49SYabin Cui FSEv05_buildDTable_rle(DTableLL, *ip++);
3001*01826a49SYabin Cui break;
3002*01826a49SYabin Cui case FSEv05_ENCODING_RAW :
3003*01826a49SYabin Cui LLlog = LLbits;
3004*01826a49SYabin Cui FSEv05_buildDTable_raw(DTableLL, LLbits);
3005*01826a49SYabin Cui break;
3006*01826a49SYabin Cui case FSEv05_ENCODING_STATIC:
3007*01826a49SYabin Cui if (!flagStaticTable) return ERROR(corruption_detected);
3008*01826a49SYabin Cui break;
3009*01826a49SYabin Cui case FSEv05_ENCODING_DYNAMIC :
3010*01826a49SYabin Cui default : /* impossible */
3011*01826a49SYabin Cui { unsigned max = MaxLL;
3012*01826a49SYabin Cui headerSize = FSEv05_readNCount(norm, &max, &LLlog, ip, iend-ip);
3013*01826a49SYabin Cui if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3014*01826a49SYabin Cui if (LLlog > LLFSEv05Log) return ERROR(corruption_detected);
3015*01826a49SYabin Cui ip += headerSize;
3016*01826a49SYabin Cui FSEv05_buildDTable(DTableLL, norm, max, LLlog);
3017*01826a49SYabin Cui } }
3018*01826a49SYabin Cui
3019*01826a49SYabin Cui switch(Offtype)
3020*01826a49SYabin Cui {
3021*01826a49SYabin Cui case FSEv05_ENCODING_RLE :
3022*01826a49SYabin Cui Offlog = 0;
3023*01826a49SYabin Cui if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
3024*01826a49SYabin Cui FSEv05_buildDTable_rle(DTableOffb, *ip++ & MaxOff); /* if *ip > MaxOff, data is corrupted */
3025*01826a49SYabin Cui break;
3026*01826a49SYabin Cui case FSEv05_ENCODING_RAW :
3027*01826a49SYabin Cui Offlog = Offbits;
3028*01826a49SYabin Cui FSEv05_buildDTable_raw(DTableOffb, Offbits);
3029*01826a49SYabin Cui break;
3030*01826a49SYabin Cui case FSEv05_ENCODING_STATIC:
3031*01826a49SYabin Cui if (!flagStaticTable) return ERROR(corruption_detected);
3032*01826a49SYabin Cui break;
3033*01826a49SYabin Cui case FSEv05_ENCODING_DYNAMIC :
3034*01826a49SYabin Cui default : /* impossible */
3035*01826a49SYabin Cui { unsigned max = MaxOff;
3036*01826a49SYabin Cui headerSize = FSEv05_readNCount(norm, &max, &Offlog, ip, iend-ip);
3037*01826a49SYabin Cui if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3038*01826a49SYabin Cui if (Offlog > OffFSEv05Log) return ERROR(corruption_detected);
3039*01826a49SYabin Cui ip += headerSize;
3040*01826a49SYabin Cui FSEv05_buildDTable(DTableOffb, norm, max, Offlog);
3041*01826a49SYabin Cui } }
3042*01826a49SYabin Cui
3043*01826a49SYabin Cui switch(MLtype)
3044*01826a49SYabin Cui {
3045*01826a49SYabin Cui case FSEv05_ENCODING_RLE :
3046*01826a49SYabin Cui MLlog = 0;
3047*01826a49SYabin Cui if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
3048*01826a49SYabin Cui FSEv05_buildDTable_rle(DTableML, *ip++);
3049*01826a49SYabin Cui break;
3050*01826a49SYabin Cui case FSEv05_ENCODING_RAW :
3051*01826a49SYabin Cui MLlog = MLbits;
3052*01826a49SYabin Cui FSEv05_buildDTable_raw(DTableML, MLbits);
3053*01826a49SYabin Cui break;
3054*01826a49SYabin Cui case FSEv05_ENCODING_STATIC:
3055*01826a49SYabin Cui if (!flagStaticTable) return ERROR(corruption_detected);
3056*01826a49SYabin Cui break;
3057*01826a49SYabin Cui case FSEv05_ENCODING_DYNAMIC :
3058*01826a49SYabin Cui default : /* impossible */
3059*01826a49SYabin Cui { unsigned max = MaxML;
3060*01826a49SYabin Cui headerSize = FSEv05_readNCount(norm, &max, &MLlog, ip, iend-ip);
3061*01826a49SYabin Cui if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3062*01826a49SYabin Cui if (MLlog > MLFSEv05Log) return ERROR(corruption_detected);
3063*01826a49SYabin Cui ip += headerSize;
3064*01826a49SYabin Cui FSEv05_buildDTable(DTableML, norm, max, MLlog);
3065*01826a49SYabin Cui } } }
3066*01826a49SYabin Cui
3067*01826a49SYabin Cui return ip-istart;
3068*01826a49SYabin Cui }
3069*01826a49SYabin Cui
3070*01826a49SYabin Cui
3071*01826a49SYabin Cui typedef struct {
3072*01826a49SYabin Cui size_t litLength;
3073*01826a49SYabin Cui size_t matchLength;
3074*01826a49SYabin Cui size_t offset;
3075*01826a49SYabin Cui } seq_t;
3076*01826a49SYabin Cui
3077*01826a49SYabin Cui typedef struct {
3078*01826a49SYabin Cui BITv05_DStream_t DStream;
3079*01826a49SYabin Cui FSEv05_DState_t stateLL;
3080*01826a49SYabin Cui FSEv05_DState_t stateOffb;
3081*01826a49SYabin Cui FSEv05_DState_t stateML;
3082*01826a49SYabin Cui size_t prevOffset;
3083*01826a49SYabin Cui const BYTE* dumps;
3084*01826a49SYabin Cui const BYTE* dumpsEnd;
3085*01826a49SYabin Cui } seqState_t;
3086*01826a49SYabin Cui
3087*01826a49SYabin Cui
3088*01826a49SYabin Cui
ZSTDv05_decodeSequence(seq_t * seq,seqState_t * seqState)3089*01826a49SYabin Cui static void ZSTDv05_decodeSequence(seq_t* seq, seqState_t* seqState)
3090*01826a49SYabin Cui {
3091*01826a49SYabin Cui size_t litLength;
3092*01826a49SYabin Cui size_t prevOffset;
3093*01826a49SYabin Cui size_t offset;
3094*01826a49SYabin Cui size_t matchLength;
3095*01826a49SYabin Cui const BYTE* dumps = seqState->dumps;
3096*01826a49SYabin Cui const BYTE* const de = seqState->dumpsEnd;
3097*01826a49SYabin Cui
3098*01826a49SYabin Cui /* Literal length */
3099*01826a49SYabin Cui litLength = FSEv05_peakSymbol(&(seqState->stateLL));
3100*01826a49SYabin Cui prevOffset = litLength ? seq->offset : seqState->prevOffset;
3101*01826a49SYabin Cui if (litLength == MaxLL) {
3102*01826a49SYabin Cui const U32 add = *dumps++;
3103*01826a49SYabin Cui if (add < 255) litLength += add;
3104*01826a49SYabin Cui else if (dumps + 2 <= de) {
3105*01826a49SYabin Cui litLength = MEM_readLE16(dumps);
3106*01826a49SYabin Cui dumps += 2;
3107*01826a49SYabin Cui if ((litLength & 1) && dumps < de) {
3108*01826a49SYabin Cui litLength += *dumps << 16;
3109*01826a49SYabin Cui dumps += 1;
3110*01826a49SYabin Cui }
3111*01826a49SYabin Cui litLength>>=1;
3112*01826a49SYabin Cui }
3113*01826a49SYabin Cui if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3114*01826a49SYabin Cui }
3115*01826a49SYabin Cui
3116*01826a49SYabin Cui /* Offset */
3117*01826a49SYabin Cui {
3118*01826a49SYabin Cui static const U32 offsetPrefix[MaxOff+1] = {
3119*01826a49SYabin Cui 1 /*fake*/, 1, 2, 4, 8, 16, 32, 64, 128, 256,
3120*01826a49SYabin Cui 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144,
3121*01826a49SYabin Cui 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, /*fake*/ 1, 1, 1, 1, 1 };
3122*01826a49SYabin Cui U32 offsetCode = FSEv05_peakSymbol(&(seqState->stateOffb)); /* <= maxOff, by table construction */
3123*01826a49SYabin Cui U32 nbBits = offsetCode - 1;
3124*01826a49SYabin Cui if (offsetCode==0) nbBits = 0; /* cmove */
3125*01826a49SYabin Cui offset = offsetPrefix[offsetCode] + BITv05_readBits(&(seqState->DStream), nbBits);
3126*01826a49SYabin Cui if (MEM_32bits()) BITv05_reloadDStream(&(seqState->DStream));
3127*01826a49SYabin Cui if (offsetCode==0) offset = prevOffset; /* repcode, cmove */
3128*01826a49SYabin Cui if (offsetCode | !litLength) seqState->prevOffset = seq->offset; /* cmove */
3129*01826a49SYabin Cui FSEv05_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream)); /* update */
3130*01826a49SYabin Cui }
3131*01826a49SYabin Cui
3132*01826a49SYabin Cui /* Literal length update */
3133*01826a49SYabin Cui FSEv05_decodeSymbol(&(seqState->stateLL), &(seqState->DStream)); /* update */
3134*01826a49SYabin Cui if (MEM_32bits()) BITv05_reloadDStream(&(seqState->DStream));
3135*01826a49SYabin Cui
3136*01826a49SYabin Cui /* MatchLength */
3137*01826a49SYabin Cui matchLength = FSEv05_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
3138*01826a49SYabin Cui if (matchLength == MaxML) {
3139*01826a49SYabin Cui const U32 add = dumps<de ? *dumps++ : 0;
3140*01826a49SYabin Cui if (add < 255) matchLength += add;
3141*01826a49SYabin Cui else if (dumps + 2 <= de) {
3142*01826a49SYabin Cui matchLength = MEM_readLE16(dumps);
3143*01826a49SYabin Cui dumps += 2;
3144*01826a49SYabin Cui if ((matchLength & 1) && dumps < de) {
3145*01826a49SYabin Cui matchLength += *dumps << 16;
3146*01826a49SYabin Cui dumps += 1;
3147*01826a49SYabin Cui }
3148*01826a49SYabin Cui matchLength >>= 1;
3149*01826a49SYabin Cui }
3150*01826a49SYabin Cui if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3151*01826a49SYabin Cui }
3152*01826a49SYabin Cui matchLength += MINMATCH;
3153*01826a49SYabin Cui
3154*01826a49SYabin Cui /* save result */
3155*01826a49SYabin Cui seq->litLength = litLength;
3156*01826a49SYabin Cui seq->offset = offset;
3157*01826a49SYabin Cui seq->matchLength = matchLength;
3158*01826a49SYabin Cui seqState->dumps = dumps;
3159*01826a49SYabin Cui
3160*01826a49SYabin Cui #if 0 /* debug */
3161*01826a49SYabin Cui {
3162*01826a49SYabin Cui static U64 totalDecoded = 0;
3163*01826a49SYabin Cui printf("pos %6u : %3u literals & match %3u bytes at distance %6u \n",
3164*01826a49SYabin Cui (U32)(totalDecoded), (U32)litLength, (U32)matchLength, (U32)offset);
3165*01826a49SYabin Cui totalDecoded += litLength + matchLength;
3166*01826a49SYabin Cui }
3167*01826a49SYabin Cui #endif
3168*01826a49SYabin Cui }
3169*01826a49SYabin Cui
3170*01826a49SYabin Cui
ZSTDv05_execSequence(BYTE * op,BYTE * const oend,seq_t sequence,const BYTE ** litPtr,const BYTE * const litLimit,const BYTE * const base,const BYTE * const vBase,const BYTE * const dictEnd)3171*01826a49SYabin Cui static size_t ZSTDv05_execSequence(BYTE* op,
3172*01826a49SYabin Cui BYTE* const oend, seq_t sequence,
3173*01826a49SYabin Cui const BYTE** litPtr, const BYTE* const litLimit,
3174*01826a49SYabin Cui const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
3175*01826a49SYabin Cui {
3176*01826a49SYabin Cui static const int dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
3177*01826a49SYabin Cui static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */
3178*01826a49SYabin Cui BYTE* const oLitEnd = op + sequence.litLength;
3179*01826a49SYabin Cui const size_t sequenceLength = sequence.litLength + sequence.matchLength;
3180*01826a49SYabin Cui BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
3181*01826a49SYabin Cui BYTE* const oend_8 = oend-8;
3182*01826a49SYabin Cui const BYTE* const litEnd = *litPtr + sequence.litLength;
3183*01826a49SYabin Cui const BYTE* match = oLitEnd - sequence.offset;
3184*01826a49SYabin Cui
3185*01826a49SYabin Cui /* checks */
3186*01826a49SYabin Cui size_t const seqLength = sequence.litLength + sequence.matchLength;
3187*01826a49SYabin Cui
3188*01826a49SYabin Cui if (seqLength > (size_t)(oend - op)) return ERROR(dstSize_tooSmall);
3189*01826a49SYabin Cui if (sequence.litLength > (size_t)(litLimit - *litPtr)) return ERROR(corruption_detected);
3190*01826a49SYabin Cui /* Now we know there are no overflow in literal nor match lengths, can use pointer checks */
3191*01826a49SYabin Cui if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall);
3192*01826a49SYabin Cui
3193*01826a49SYabin Cui if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
3194*01826a49SYabin Cui if (litEnd > litLimit) return ERROR(corruption_detected); /* overRead beyond lit buffer */
3195*01826a49SYabin Cui
3196*01826a49SYabin Cui /* copy Literals */
3197*01826a49SYabin Cui ZSTDv05_wildcopy(op, *litPtr, (ptrdiff_t)sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
3198*01826a49SYabin Cui op = oLitEnd;
3199*01826a49SYabin Cui *litPtr = litEnd; /* update for next sequence */
3200*01826a49SYabin Cui
3201*01826a49SYabin Cui /* copy Match */
3202*01826a49SYabin Cui if (sequence.offset > (size_t)(oLitEnd - base)) {
3203*01826a49SYabin Cui /* offset beyond prefix */
3204*01826a49SYabin Cui if (sequence.offset > (size_t)(oLitEnd - vBase))
3205*01826a49SYabin Cui return ERROR(corruption_detected);
3206*01826a49SYabin Cui match = dictEnd - (base-match);
3207*01826a49SYabin Cui if (match + sequence.matchLength <= dictEnd) {
3208*01826a49SYabin Cui memmove(oLitEnd, match, sequence.matchLength);
3209*01826a49SYabin Cui return sequenceLength;
3210*01826a49SYabin Cui }
3211*01826a49SYabin Cui /* span extDict & currentPrefixSegment */
3212*01826a49SYabin Cui {
3213*01826a49SYabin Cui size_t length1 = dictEnd - match;
3214*01826a49SYabin Cui memmove(oLitEnd, match, length1);
3215*01826a49SYabin Cui op = oLitEnd + length1;
3216*01826a49SYabin Cui sequence.matchLength -= length1;
3217*01826a49SYabin Cui match = base;
3218*01826a49SYabin Cui if (op > oend_8 || sequence.matchLength < MINMATCH) {
3219*01826a49SYabin Cui while (op < oMatchEnd) *op++ = *match++;
3220*01826a49SYabin Cui return sequenceLength;
3221*01826a49SYabin Cui }
3222*01826a49SYabin Cui } }
3223*01826a49SYabin Cui /* Requirement: op <= oend_8 */
3224*01826a49SYabin Cui
3225*01826a49SYabin Cui /* match within prefix */
3226*01826a49SYabin Cui if (sequence.offset < 8) {
3227*01826a49SYabin Cui /* close range match, overlap */
3228*01826a49SYabin Cui const int sub2 = dec64table[sequence.offset];
3229*01826a49SYabin Cui op[0] = match[0];
3230*01826a49SYabin Cui op[1] = match[1];
3231*01826a49SYabin Cui op[2] = match[2];
3232*01826a49SYabin Cui op[3] = match[3];
3233*01826a49SYabin Cui match += dec32table[sequence.offset];
3234*01826a49SYabin Cui ZSTDv05_copy4(op+4, match);
3235*01826a49SYabin Cui match -= sub2;
3236*01826a49SYabin Cui } else {
3237*01826a49SYabin Cui ZSTDv05_copy8(op, match);
3238*01826a49SYabin Cui }
3239*01826a49SYabin Cui op += 8; match += 8;
3240*01826a49SYabin Cui
3241*01826a49SYabin Cui if (oMatchEnd > oend-(16-MINMATCH)) {
3242*01826a49SYabin Cui if (op < oend_8) {
3243*01826a49SYabin Cui ZSTDv05_wildcopy(op, match, oend_8 - op);
3244*01826a49SYabin Cui match += oend_8 - op;
3245*01826a49SYabin Cui op = oend_8;
3246*01826a49SYabin Cui }
3247*01826a49SYabin Cui while (op < oMatchEnd)
3248*01826a49SYabin Cui *op++ = *match++;
3249*01826a49SYabin Cui } else {
3250*01826a49SYabin Cui ZSTDv05_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */
3251*01826a49SYabin Cui }
3252*01826a49SYabin Cui return sequenceLength;
3253*01826a49SYabin Cui }
3254*01826a49SYabin Cui
3255*01826a49SYabin Cui
ZSTDv05_decompressSequences(ZSTDv05_DCtx * dctx,void * dst,size_t maxDstSize,const void * seqStart,size_t seqSize)3256*01826a49SYabin Cui static size_t ZSTDv05_decompressSequences(
3257*01826a49SYabin Cui ZSTDv05_DCtx* dctx,
3258*01826a49SYabin Cui void* dst, size_t maxDstSize,
3259*01826a49SYabin Cui const void* seqStart, size_t seqSize)
3260*01826a49SYabin Cui {
3261*01826a49SYabin Cui const BYTE* ip = (const BYTE*)seqStart;
3262*01826a49SYabin Cui const BYTE* const iend = ip + seqSize;
3263*01826a49SYabin Cui BYTE* const ostart = (BYTE*)dst;
3264*01826a49SYabin Cui BYTE* op = ostart;
3265*01826a49SYabin Cui BYTE* const oend = ostart + maxDstSize;
3266*01826a49SYabin Cui size_t errorCode, dumpsLength=0;
3267*01826a49SYabin Cui const BYTE* litPtr = dctx->litPtr;
3268*01826a49SYabin Cui const BYTE* const litEnd = litPtr + dctx->litSize;
3269*01826a49SYabin Cui int nbSeq=0;
3270*01826a49SYabin Cui const BYTE* dumps = NULL;
3271*01826a49SYabin Cui unsigned* DTableLL = dctx->LLTable;
3272*01826a49SYabin Cui unsigned* DTableML = dctx->MLTable;
3273*01826a49SYabin Cui unsigned* DTableOffb = dctx->OffTable;
3274*01826a49SYabin Cui const BYTE* const base = (const BYTE*) (dctx->base);
3275*01826a49SYabin Cui const BYTE* const vBase = (const BYTE*) (dctx->vBase);
3276*01826a49SYabin Cui const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
3277*01826a49SYabin Cui
3278*01826a49SYabin Cui /* Build Decoding Tables */
3279*01826a49SYabin Cui errorCode = ZSTDv05_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength,
3280*01826a49SYabin Cui DTableLL, DTableML, DTableOffb,
3281*01826a49SYabin Cui ip, seqSize, dctx->flagStaticTables);
3282*01826a49SYabin Cui if (ZSTDv05_isError(errorCode)) return errorCode;
3283*01826a49SYabin Cui ip += errorCode;
3284*01826a49SYabin Cui
3285*01826a49SYabin Cui /* Regen sequences */
3286*01826a49SYabin Cui if (nbSeq) {
3287*01826a49SYabin Cui seq_t sequence;
3288*01826a49SYabin Cui seqState_t seqState;
3289*01826a49SYabin Cui
3290*01826a49SYabin Cui memset(&sequence, 0, sizeof(sequence));
3291*01826a49SYabin Cui sequence.offset = REPCODE_STARTVALUE;
3292*01826a49SYabin Cui seqState.dumps = dumps;
3293*01826a49SYabin Cui seqState.dumpsEnd = dumps + dumpsLength;
3294*01826a49SYabin Cui seqState.prevOffset = REPCODE_STARTVALUE;
3295*01826a49SYabin Cui errorCode = BITv05_initDStream(&(seqState.DStream), ip, iend-ip);
3296*01826a49SYabin Cui if (ERR_isError(errorCode)) return ERROR(corruption_detected);
3297*01826a49SYabin Cui FSEv05_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL);
3298*01826a49SYabin Cui FSEv05_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb);
3299*01826a49SYabin Cui FSEv05_initDState(&(seqState.stateML), &(seqState.DStream), DTableML);
3300*01826a49SYabin Cui
3301*01826a49SYabin Cui for ( ; (BITv05_reloadDStream(&(seqState.DStream)) <= BITv05_DStream_completed) && nbSeq ; ) {
3302*01826a49SYabin Cui size_t oneSeqSize;
3303*01826a49SYabin Cui nbSeq--;
3304*01826a49SYabin Cui ZSTDv05_decodeSequence(&sequence, &seqState);
3305*01826a49SYabin Cui oneSeqSize = ZSTDv05_execSequence(op, oend, sequence, &litPtr, litEnd, base, vBase, dictEnd);
3306*01826a49SYabin Cui if (ZSTDv05_isError(oneSeqSize)) return oneSeqSize;
3307*01826a49SYabin Cui op += oneSeqSize;
3308*01826a49SYabin Cui }
3309*01826a49SYabin Cui
3310*01826a49SYabin Cui /* check if reached exact end */
3311*01826a49SYabin Cui if (nbSeq) return ERROR(corruption_detected);
3312*01826a49SYabin Cui }
3313*01826a49SYabin Cui
3314*01826a49SYabin Cui /* last literal segment */
3315*01826a49SYabin Cui {
3316*01826a49SYabin Cui size_t lastLLSize = litEnd - litPtr;
3317*01826a49SYabin Cui if (litPtr > litEnd) return ERROR(corruption_detected); /* too many literals already used */
3318*01826a49SYabin Cui if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3319*01826a49SYabin Cui if (lastLLSize > 0) {
3320*01826a49SYabin Cui memcpy(op, litPtr, lastLLSize);
3321*01826a49SYabin Cui op += lastLLSize;
3322*01826a49SYabin Cui }
3323*01826a49SYabin Cui }
3324*01826a49SYabin Cui
3325*01826a49SYabin Cui return op-ostart;
3326*01826a49SYabin Cui }
3327*01826a49SYabin Cui
3328*01826a49SYabin Cui
ZSTDv05_checkContinuity(ZSTDv05_DCtx * dctx,const void * dst)3329*01826a49SYabin Cui static void ZSTDv05_checkContinuity(ZSTDv05_DCtx* dctx, const void* dst)
3330*01826a49SYabin Cui {
3331*01826a49SYabin Cui if (dst != dctx->previousDstEnd) { /* not contiguous */
3332*01826a49SYabin Cui dctx->dictEnd = dctx->previousDstEnd;
3333*01826a49SYabin Cui dctx->vBase = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base));
3334*01826a49SYabin Cui dctx->base = dst;
3335*01826a49SYabin Cui dctx->previousDstEnd = dst;
3336*01826a49SYabin Cui }
3337*01826a49SYabin Cui }
3338*01826a49SYabin Cui
3339*01826a49SYabin Cui
ZSTDv05_decompressBlock_internal(ZSTDv05_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)3340*01826a49SYabin Cui static size_t ZSTDv05_decompressBlock_internal(ZSTDv05_DCtx* dctx,
3341*01826a49SYabin Cui void* dst, size_t dstCapacity,
3342*01826a49SYabin Cui const void* src, size_t srcSize)
3343*01826a49SYabin Cui { /* blockType == blockCompressed */
3344*01826a49SYabin Cui const BYTE* ip = (const BYTE*)src;
3345*01826a49SYabin Cui size_t litCSize;
3346*01826a49SYabin Cui
3347*01826a49SYabin Cui if (srcSize >= BLOCKSIZE) return ERROR(srcSize_wrong);
3348*01826a49SYabin Cui
3349*01826a49SYabin Cui /* Decode literals sub-block */
3350*01826a49SYabin Cui litCSize = ZSTDv05_decodeLiteralsBlock(dctx, src, srcSize);
3351*01826a49SYabin Cui if (ZSTDv05_isError(litCSize)) return litCSize;
3352*01826a49SYabin Cui ip += litCSize;
3353*01826a49SYabin Cui srcSize -= litCSize;
3354*01826a49SYabin Cui
3355*01826a49SYabin Cui return ZSTDv05_decompressSequences(dctx, dst, dstCapacity, ip, srcSize);
3356*01826a49SYabin Cui }
3357*01826a49SYabin Cui
3358*01826a49SYabin Cui
ZSTDv05_decompressBlock(ZSTDv05_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)3359*01826a49SYabin Cui size_t ZSTDv05_decompressBlock(ZSTDv05_DCtx* dctx,
3360*01826a49SYabin Cui void* dst, size_t dstCapacity,
3361*01826a49SYabin Cui const void* src, size_t srcSize)
3362*01826a49SYabin Cui {
3363*01826a49SYabin Cui ZSTDv05_checkContinuity(dctx, dst);
3364*01826a49SYabin Cui return ZSTDv05_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize);
3365*01826a49SYabin Cui }
3366*01826a49SYabin Cui
3367*01826a49SYabin Cui
3368*01826a49SYabin Cui /*! ZSTDv05_decompress_continueDCtx
3369*01826a49SYabin Cui * dctx must have been properly initialized */
ZSTDv05_decompress_continueDCtx(ZSTDv05_DCtx * dctx,void * dst,size_t maxDstSize,const void * src,size_t srcSize)3370*01826a49SYabin Cui static size_t ZSTDv05_decompress_continueDCtx(ZSTDv05_DCtx* dctx,
3371*01826a49SYabin Cui void* dst, size_t maxDstSize,
3372*01826a49SYabin Cui const void* src, size_t srcSize)
3373*01826a49SYabin Cui {
3374*01826a49SYabin Cui const BYTE* ip = (const BYTE*)src;
3375*01826a49SYabin Cui const BYTE* iend = ip + srcSize;
3376*01826a49SYabin Cui BYTE* const ostart = (BYTE*)dst;
3377*01826a49SYabin Cui BYTE* op = ostart;
3378*01826a49SYabin Cui BYTE* const oend = ostart + maxDstSize;
3379*01826a49SYabin Cui size_t remainingSize = srcSize;
3380*01826a49SYabin Cui blockProperties_t blockProperties;
3381*01826a49SYabin Cui memset(&blockProperties, 0, sizeof(blockProperties));
3382*01826a49SYabin Cui
3383*01826a49SYabin Cui /* Frame Header */
3384*01826a49SYabin Cui { size_t frameHeaderSize;
3385*01826a49SYabin Cui if (srcSize < ZSTDv05_frameHeaderSize_min+ZSTDv05_blockHeaderSize) return ERROR(srcSize_wrong);
3386*01826a49SYabin Cui frameHeaderSize = ZSTDv05_decodeFrameHeader_Part1(dctx, src, ZSTDv05_frameHeaderSize_min);
3387*01826a49SYabin Cui if (ZSTDv05_isError(frameHeaderSize)) return frameHeaderSize;
3388*01826a49SYabin Cui if (srcSize < frameHeaderSize+ZSTDv05_blockHeaderSize) return ERROR(srcSize_wrong);
3389*01826a49SYabin Cui ip += frameHeaderSize; remainingSize -= frameHeaderSize;
3390*01826a49SYabin Cui frameHeaderSize = ZSTDv05_decodeFrameHeader_Part2(dctx, src, frameHeaderSize);
3391*01826a49SYabin Cui if (ZSTDv05_isError(frameHeaderSize)) return frameHeaderSize;
3392*01826a49SYabin Cui }
3393*01826a49SYabin Cui
3394*01826a49SYabin Cui /* Loop on each block */
3395*01826a49SYabin Cui while (1)
3396*01826a49SYabin Cui {
3397*01826a49SYabin Cui size_t decodedSize=0;
3398*01826a49SYabin Cui size_t cBlockSize = ZSTDv05_getcBlockSize(ip, iend-ip, &blockProperties);
3399*01826a49SYabin Cui if (ZSTDv05_isError(cBlockSize)) return cBlockSize;
3400*01826a49SYabin Cui
3401*01826a49SYabin Cui ip += ZSTDv05_blockHeaderSize;
3402*01826a49SYabin Cui remainingSize -= ZSTDv05_blockHeaderSize;
3403*01826a49SYabin Cui if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
3404*01826a49SYabin Cui
3405*01826a49SYabin Cui switch(blockProperties.blockType)
3406*01826a49SYabin Cui {
3407*01826a49SYabin Cui case bt_compressed:
3408*01826a49SYabin Cui decodedSize = ZSTDv05_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize);
3409*01826a49SYabin Cui break;
3410*01826a49SYabin Cui case bt_raw :
3411*01826a49SYabin Cui decodedSize = ZSTDv05_copyRawBlock(op, oend-op, ip, cBlockSize);
3412*01826a49SYabin Cui break;
3413*01826a49SYabin Cui case bt_rle :
3414*01826a49SYabin Cui return ERROR(GENERIC); /* not yet supported */
3415*01826a49SYabin Cui break;
3416*01826a49SYabin Cui case bt_end :
3417*01826a49SYabin Cui /* end of frame */
3418*01826a49SYabin Cui if (remainingSize) return ERROR(srcSize_wrong);
3419*01826a49SYabin Cui break;
3420*01826a49SYabin Cui default:
3421*01826a49SYabin Cui return ERROR(GENERIC); /* impossible */
3422*01826a49SYabin Cui }
3423*01826a49SYabin Cui if (cBlockSize == 0) break; /* bt_end */
3424*01826a49SYabin Cui
3425*01826a49SYabin Cui if (ZSTDv05_isError(decodedSize)) return decodedSize;
3426*01826a49SYabin Cui op += decodedSize;
3427*01826a49SYabin Cui ip += cBlockSize;
3428*01826a49SYabin Cui remainingSize -= cBlockSize;
3429*01826a49SYabin Cui }
3430*01826a49SYabin Cui
3431*01826a49SYabin Cui return op-ostart;
3432*01826a49SYabin Cui }
3433*01826a49SYabin Cui
3434*01826a49SYabin Cui
ZSTDv05_decompress_usingPreparedDCtx(ZSTDv05_DCtx * dctx,const ZSTDv05_DCtx * refDCtx,void * dst,size_t maxDstSize,const void * src,size_t srcSize)3435*01826a49SYabin Cui size_t ZSTDv05_decompress_usingPreparedDCtx(ZSTDv05_DCtx* dctx, const ZSTDv05_DCtx* refDCtx,
3436*01826a49SYabin Cui void* dst, size_t maxDstSize,
3437*01826a49SYabin Cui const void* src, size_t srcSize)
3438*01826a49SYabin Cui {
3439*01826a49SYabin Cui ZSTDv05_copyDCtx(dctx, refDCtx);
3440*01826a49SYabin Cui ZSTDv05_checkContinuity(dctx, dst);
3441*01826a49SYabin Cui return ZSTDv05_decompress_continueDCtx(dctx, dst, maxDstSize, src, srcSize);
3442*01826a49SYabin Cui }
3443*01826a49SYabin Cui
3444*01826a49SYabin Cui
ZSTDv05_decompress_usingDict(ZSTDv05_DCtx * dctx,void * dst,size_t maxDstSize,const void * src,size_t srcSize,const void * dict,size_t dictSize)3445*01826a49SYabin Cui size_t ZSTDv05_decompress_usingDict(ZSTDv05_DCtx* dctx,
3446*01826a49SYabin Cui void* dst, size_t maxDstSize,
3447*01826a49SYabin Cui const void* src, size_t srcSize,
3448*01826a49SYabin Cui const void* dict, size_t dictSize)
3449*01826a49SYabin Cui {
3450*01826a49SYabin Cui ZSTDv05_decompressBegin_usingDict(dctx, dict, dictSize);
3451*01826a49SYabin Cui ZSTDv05_checkContinuity(dctx, dst);
3452*01826a49SYabin Cui return ZSTDv05_decompress_continueDCtx(dctx, dst, maxDstSize, src, srcSize);
3453*01826a49SYabin Cui }
3454*01826a49SYabin Cui
3455*01826a49SYabin Cui
ZSTDv05_decompressDCtx(ZSTDv05_DCtx * dctx,void * dst,size_t maxDstSize,const void * src,size_t srcSize)3456*01826a49SYabin Cui size_t ZSTDv05_decompressDCtx(ZSTDv05_DCtx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3457*01826a49SYabin Cui {
3458*01826a49SYabin Cui return ZSTDv05_decompress_usingDict(dctx, dst, maxDstSize, src, srcSize, NULL, 0);
3459*01826a49SYabin Cui }
3460*01826a49SYabin Cui
ZSTDv05_decompress(void * dst,size_t maxDstSize,const void * src,size_t srcSize)3461*01826a49SYabin Cui size_t ZSTDv05_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3462*01826a49SYabin Cui {
3463*01826a49SYabin Cui #if defined(ZSTDv05_HEAPMODE) && (ZSTDv05_HEAPMODE==1)
3464*01826a49SYabin Cui size_t regenSize;
3465*01826a49SYabin Cui ZSTDv05_DCtx* dctx = ZSTDv05_createDCtx();
3466*01826a49SYabin Cui if (dctx==NULL) return ERROR(memory_allocation);
3467*01826a49SYabin Cui regenSize = ZSTDv05_decompressDCtx(dctx, dst, maxDstSize, src, srcSize);
3468*01826a49SYabin Cui ZSTDv05_freeDCtx(dctx);
3469*01826a49SYabin Cui return regenSize;
3470*01826a49SYabin Cui #else
3471*01826a49SYabin Cui ZSTDv05_DCtx dctx;
3472*01826a49SYabin Cui return ZSTDv05_decompressDCtx(&dctx, dst, maxDstSize, src, srcSize);
3473*01826a49SYabin Cui #endif
3474*01826a49SYabin Cui }
3475*01826a49SYabin Cui
3476*01826a49SYabin Cui /* ZSTD_errorFrameSizeInfoLegacy() :
3477*01826a49SYabin Cui assumes `cSize` and `dBound` are _not_ NULL */
ZSTD_errorFrameSizeInfoLegacy(size_t * cSize,unsigned long long * dBound,size_t ret)3478*01826a49SYabin Cui static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)
3479*01826a49SYabin Cui {
3480*01826a49SYabin Cui *cSize = ret;
3481*01826a49SYabin Cui *dBound = ZSTD_CONTENTSIZE_ERROR;
3482*01826a49SYabin Cui }
3483*01826a49SYabin Cui
ZSTDv05_findFrameSizeInfoLegacy(const void * src,size_t srcSize,size_t * cSize,unsigned long long * dBound)3484*01826a49SYabin Cui void ZSTDv05_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)
3485*01826a49SYabin Cui {
3486*01826a49SYabin Cui const BYTE* ip = (const BYTE*)src;
3487*01826a49SYabin Cui size_t remainingSize = srcSize;
3488*01826a49SYabin Cui size_t nbBlocks = 0;
3489*01826a49SYabin Cui blockProperties_t blockProperties;
3490*01826a49SYabin Cui
3491*01826a49SYabin Cui /* Frame Header */
3492*01826a49SYabin Cui if (srcSize < ZSTDv05_frameHeaderSize_min) {
3493*01826a49SYabin Cui ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3494*01826a49SYabin Cui return;
3495*01826a49SYabin Cui }
3496*01826a49SYabin Cui if (MEM_readLE32(src) != ZSTDv05_MAGICNUMBER) {
3497*01826a49SYabin Cui ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));
3498*01826a49SYabin Cui return;
3499*01826a49SYabin Cui }
3500*01826a49SYabin Cui ip += ZSTDv05_frameHeaderSize_min; remainingSize -= ZSTDv05_frameHeaderSize_min;
3501*01826a49SYabin Cui
3502*01826a49SYabin Cui /* Loop on each block */
3503*01826a49SYabin Cui while (1)
3504*01826a49SYabin Cui {
3505*01826a49SYabin Cui size_t cBlockSize = ZSTDv05_getcBlockSize(ip, remainingSize, &blockProperties);
3506*01826a49SYabin Cui if (ZSTDv05_isError(cBlockSize)) {
3507*01826a49SYabin Cui ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize);
3508*01826a49SYabin Cui return;
3509*01826a49SYabin Cui }
3510*01826a49SYabin Cui
3511*01826a49SYabin Cui ip += ZSTDv05_blockHeaderSize;
3512*01826a49SYabin Cui remainingSize -= ZSTDv05_blockHeaderSize;
3513*01826a49SYabin Cui if (cBlockSize > remainingSize) {
3514*01826a49SYabin Cui ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3515*01826a49SYabin Cui return;
3516*01826a49SYabin Cui }
3517*01826a49SYabin Cui
3518*01826a49SYabin Cui if (cBlockSize == 0) break; /* bt_end */
3519*01826a49SYabin Cui
3520*01826a49SYabin Cui ip += cBlockSize;
3521*01826a49SYabin Cui remainingSize -= cBlockSize;
3522*01826a49SYabin Cui nbBlocks++;
3523*01826a49SYabin Cui }
3524*01826a49SYabin Cui
3525*01826a49SYabin Cui *cSize = ip - (const BYTE*)src;
3526*01826a49SYabin Cui *dBound = nbBlocks * BLOCKSIZE;
3527*01826a49SYabin Cui }
3528*01826a49SYabin Cui
3529*01826a49SYabin Cui /* ******************************
3530*01826a49SYabin Cui * Streaming Decompression API
3531*01826a49SYabin Cui ********************************/
ZSTDv05_nextSrcSizeToDecompress(ZSTDv05_DCtx * dctx)3532*01826a49SYabin Cui size_t ZSTDv05_nextSrcSizeToDecompress(ZSTDv05_DCtx* dctx)
3533*01826a49SYabin Cui {
3534*01826a49SYabin Cui return dctx->expected;
3535*01826a49SYabin Cui }
3536*01826a49SYabin Cui
ZSTDv05_decompressContinue(ZSTDv05_DCtx * dctx,void * dst,size_t maxDstSize,const void * src,size_t srcSize)3537*01826a49SYabin Cui size_t ZSTDv05_decompressContinue(ZSTDv05_DCtx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3538*01826a49SYabin Cui {
3539*01826a49SYabin Cui /* Sanity check */
3540*01826a49SYabin Cui if (srcSize != dctx->expected) return ERROR(srcSize_wrong);
3541*01826a49SYabin Cui ZSTDv05_checkContinuity(dctx, dst);
3542*01826a49SYabin Cui
3543*01826a49SYabin Cui /* Decompress : frame header; part 1 */
3544*01826a49SYabin Cui switch (dctx->stage)
3545*01826a49SYabin Cui {
3546*01826a49SYabin Cui case ZSTDv05ds_getFrameHeaderSize :
3547*01826a49SYabin Cui /* get frame header size */
3548*01826a49SYabin Cui if (srcSize != ZSTDv05_frameHeaderSize_min) return ERROR(srcSize_wrong); /* impossible */
3549*01826a49SYabin Cui dctx->headerSize = ZSTDv05_decodeFrameHeader_Part1(dctx, src, ZSTDv05_frameHeaderSize_min);
3550*01826a49SYabin Cui if (ZSTDv05_isError(dctx->headerSize)) return dctx->headerSize;
3551*01826a49SYabin Cui memcpy(dctx->headerBuffer, src, ZSTDv05_frameHeaderSize_min);
3552*01826a49SYabin Cui if (dctx->headerSize > ZSTDv05_frameHeaderSize_min) return ERROR(GENERIC); /* should never happen */
3553*01826a49SYabin Cui dctx->expected = 0; /* not necessary to copy more */
3554*01826a49SYabin Cui /* fallthrough */
3555*01826a49SYabin Cui case ZSTDv05ds_decodeFrameHeader:
3556*01826a49SYabin Cui /* get frame header */
3557*01826a49SYabin Cui { size_t const result = ZSTDv05_decodeFrameHeader_Part2(dctx, dctx->headerBuffer, dctx->headerSize);
3558*01826a49SYabin Cui if (ZSTDv05_isError(result)) return result;
3559*01826a49SYabin Cui dctx->expected = ZSTDv05_blockHeaderSize;
3560*01826a49SYabin Cui dctx->stage = ZSTDv05ds_decodeBlockHeader;
3561*01826a49SYabin Cui return 0;
3562*01826a49SYabin Cui }
3563*01826a49SYabin Cui case ZSTDv05ds_decodeBlockHeader:
3564*01826a49SYabin Cui {
3565*01826a49SYabin Cui /* Decode block header */
3566*01826a49SYabin Cui blockProperties_t bp;
3567*01826a49SYabin Cui size_t blockSize = ZSTDv05_getcBlockSize(src, ZSTDv05_blockHeaderSize, &bp);
3568*01826a49SYabin Cui if (ZSTDv05_isError(blockSize)) return blockSize;
3569*01826a49SYabin Cui if (bp.blockType == bt_end) {
3570*01826a49SYabin Cui dctx->expected = 0;
3571*01826a49SYabin Cui dctx->stage = ZSTDv05ds_getFrameHeaderSize;
3572*01826a49SYabin Cui }
3573*01826a49SYabin Cui else {
3574*01826a49SYabin Cui dctx->expected = blockSize;
3575*01826a49SYabin Cui dctx->bType = bp.blockType;
3576*01826a49SYabin Cui dctx->stage = ZSTDv05ds_decompressBlock;
3577*01826a49SYabin Cui }
3578*01826a49SYabin Cui return 0;
3579*01826a49SYabin Cui }
3580*01826a49SYabin Cui case ZSTDv05ds_decompressBlock:
3581*01826a49SYabin Cui {
3582*01826a49SYabin Cui /* Decompress : block content */
3583*01826a49SYabin Cui size_t rSize;
3584*01826a49SYabin Cui switch(dctx->bType)
3585*01826a49SYabin Cui {
3586*01826a49SYabin Cui case bt_compressed:
3587*01826a49SYabin Cui rSize = ZSTDv05_decompressBlock_internal(dctx, dst, maxDstSize, src, srcSize);
3588*01826a49SYabin Cui break;
3589*01826a49SYabin Cui case bt_raw :
3590*01826a49SYabin Cui rSize = ZSTDv05_copyRawBlock(dst, maxDstSize, src, srcSize);
3591*01826a49SYabin Cui break;
3592*01826a49SYabin Cui case bt_rle :
3593*01826a49SYabin Cui return ERROR(GENERIC); /* not yet handled */
3594*01826a49SYabin Cui break;
3595*01826a49SYabin Cui case bt_end : /* should never happen (filtered at phase 1) */
3596*01826a49SYabin Cui rSize = 0;
3597*01826a49SYabin Cui break;
3598*01826a49SYabin Cui default:
3599*01826a49SYabin Cui return ERROR(GENERIC); /* impossible */
3600*01826a49SYabin Cui }
3601*01826a49SYabin Cui dctx->stage = ZSTDv05ds_decodeBlockHeader;
3602*01826a49SYabin Cui dctx->expected = ZSTDv05_blockHeaderSize;
3603*01826a49SYabin Cui if (ZSTDv05_isError(rSize)) return rSize;
3604*01826a49SYabin Cui dctx->previousDstEnd = (char*)dst + rSize;
3605*01826a49SYabin Cui return rSize;
3606*01826a49SYabin Cui }
3607*01826a49SYabin Cui default:
3608*01826a49SYabin Cui return ERROR(GENERIC); /* impossible */
3609*01826a49SYabin Cui }
3610*01826a49SYabin Cui }
3611*01826a49SYabin Cui
3612*01826a49SYabin Cui
ZSTDv05_refDictContent(ZSTDv05_DCtx * dctx,const void * dict,size_t dictSize)3613*01826a49SYabin Cui static void ZSTDv05_refDictContent(ZSTDv05_DCtx* dctx, const void* dict, size_t dictSize)
3614*01826a49SYabin Cui {
3615*01826a49SYabin Cui dctx->dictEnd = dctx->previousDstEnd;
3616*01826a49SYabin Cui dctx->vBase = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base));
3617*01826a49SYabin Cui dctx->base = dict;
3618*01826a49SYabin Cui dctx->previousDstEnd = (const char*)dict + dictSize;
3619*01826a49SYabin Cui }
3620*01826a49SYabin Cui
ZSTDv05_loadEntropy(ZSTDv05_DCtx * dctx,const void * dict,size_t dictSize)3621*01826a49SYabin Cui static size_t ZSTDv05_loadEntropy(ZSTDv05_DCtx* dctx, const void* dict, size_t dictSize)
3622*01826a49SYabin Cui {
3623*01826a49SYabin Cui size_t hSize, offcodeHeaderSize, matchlengthHeaderSize, errorCode, litlengthHeaderSize;
3624*01826a49SYabin Cui short offcodeNCount[MaxOff+1];
3625*01826a49SYabin Cui unsigned offcodeMaxValue=MaxOff, offcodeLog;
3626*01826a49SYabin Cui short matchlengthNCount[MaxML+1];
3627*01826a49SYabin Cui unsigned matchlengthMaxValue = MaxML, matchlengthLog;
3628*01826a49SYabin Cui short litlengthNCount[MaxLL+1];
3629*01826a49SYabin Cui unsigned litlengthMaxValue = MaxLL, litlengthLog;
3630*01826a49SYabin Cui
3631*01826a49SYabin Cui hSize = HUFv05_readDTableX4(dctx->hufTableX4, dict, dictSize);
3632*01826a49SYabin Cui if (HUFv05_isError(hSize)) return ERROR(dictionary_corrupted);
3633*01826a49SYabin Cui dict = (const char*)dict + hSize;
3634*01826a49SYabin Cui dictSize -= hSize;
3635*01826a49SYabin Cui
3636*01826a49SYabin Cui offcodeHeaderSize = FSEv05_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dict, dictSize);
3637*01826a49SYabin Cui if (FSEv05_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted);
3638*01826a49SYabin Cui if (offcodeLog > OffFSEv05Log) return ERROR(dictionary_corrupted);
3639*01826a49SYabin Cui errorCode = FSEv05_buildDTable(dctx->OffTable, offcodeNCount, offcodeMaxValue, offcodeLog);
3640*01826a49SYabin Cui if (FSEv05_isError(errorCode)) return ERROR(dictionary_corrupted);
3641*01826a49SYabin Cui dict = (const char*)dict + offcodeHeaderSize;
3642*01826a49SYabin Cui dictSize -= offcodeHeaderSize;
3643*01826a49SYabin Cui
3644*01826a49SYabin Cui matchlengthHeaderSize = FSEv05_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dict, dictSize);
3645*01826a49SYabin Cui if (FSEv05_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted);
3646*01826a49SYabin Cui if (matchlengthLog > MLFSEv05Log) return ERROR(dictionary_corrupted);
3647*01826a49SYabin Cui errorCode = FSEv05_buildDTable(dctx->MLTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog);
3648*01826a49SYabin Cui if (FSEv05_isError(errorCode)) return ERROR(dictionary_corrupted);
3649*01826a49SYabin Cui dict = (const char*)dict + matchlengthHeaderSize;
3650*01826a49SYabin Cui dictSize -= matchlengthHeaderSize;
3651*01826a49SYabin Cui
3652*01826a49SYabin Cui litlengthHeaderSize = FSEv05_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dict, dictSize);
3653*01826a49SYabin Cui if (litlengthLog > LLFSEv05Log) return ERROR(dictionary_corrupted);
3654*01826a49SYabin Cui if (FSEv05_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted);
3655*01826a49SYabin Cui errorCode = FSEv05_buildDTable(dctx->LLTable, litlengthNCount, litlengthMaxValue, litlengthLog);
3656*01826a49SYabin Cui if (FSEv05_isError(errorCode)) return ERROR(dictionary_corrupted);
3657*01826a49SYabin Cui
3658*01826a49SYabin Cui dctx->flagStaticTables = 1;
3659*01826a49SYabin Cui return hSize + offcodeHeaderSize + matchlengthHeaderSize + litlengthHeaderSize;
3660*01826a49SYabin Cui }
3661*01826a49SYabin Cui
ZSTDv05_decompress_insertDictionary(ZSTDv05_DCtx * dctx,const void * dict,size_t dictSize)3662*01826a49SYabin Cui static size_t ZSTDv05_decompress_insertDictionary(ZSTDv05_DCtx* dctx, const void* dict, size_t dictSize)
3663*01826a49SYabin Cui {
3664*01826a49SYabin Cui size_t eSize;
3665*01826a49SYabin Cui U32 magic = MEM_readLE32(dict);
3666*01826a49SYabin Cui if (magic != ZSTDv05_DICT_MAGIC) {
3667*01826a49SYabin Cui /* pure content mode */
3668*01826a49SYabin Cui ZSTDv05_refDictContent(dctx, dict, dictSize);
3669*01826a49SYabin Cui return 0;
3670*01826a49SYabin Cui }
3671*01826a49SYabin Cui /* load entropy tables */
3672*01826a49SYabin Cui dict = (const char*)dict + 4;
3673*01826a49SYabin Cui dictSize -= 4;
3674*01826a49SYabin Cui eSize = ZSTDv05_loadEntropy(dctx, dict, dictSize);
3675*01826a49SYabin Cui if (ZSTDv05_isError(eSize)) return ERROR(dictionary_corrupted);
3676*01826a49SYabin Cui
3677*01826a49SYabin Cui /* reference dictionary content */
3678*01826a49SYabin Cui dict = (const char*)dict + eSize;
3679*01826a49SYabin Cui dictSize -= eSize;
3680*01826a49SYabin Cui ZSTDv05_refDictContent(dctx, dict, dictSize);
3681*01826a49SYabin Cui
3682*01826a49SYabin Cui return 0;
3683*01826a49SYabin Cui }
3684*01826a49SYabin Cui
3685*01826a49SYabin Cui
ZSTDv05_decompressBegin_usingDict(ZSTDv05_DCtx * dctx,const void * dict,size_t dictSize)3686*01826a49SYabin Cui size_t ZSTDv05_decompressBegin_usingDict(ZSTDv05_DCtx* dctx, const void* dict, size_t dictSize)
3687*01826a49SYabin Cui {
3688*01826a49SYabin Cui size_t errorCode;
3689*01826a49SYabin Cui errorCode = ZSTDv05_decompressBegin(dctx);
3690*01826a49SYabin Cui if (ZSTDv05_isError(errorCode)) return errorCode;
3691*01826a49SYabin Cui
3692*01826a49SYabin Cui if (dict && dictSize) {
3693*01826a49SYabin Cui errorCode = ZSTDv05_decompress_insertDictionary(dctx, dict, dictSize);
3694*01826a49SYabin Cui if (ZSTDv05_isError(errorCode)) return ERROR(dictionary_corrupted);
3695*01826a49SYabin Cui }
3696*01826a49SYabin Cui
3697*01826a49SYabin Cui return 0;
3698*01826a49SYabin Cui }
3699*01826a49SYabin Cui
3700*01826a49SYabin Cui /*
3701*01826a49SYabin Cui Buffered version of Zstd compression library
3702*01826a49SYabin Cui Copyright (C) 2015-2016, Yann Collet.
3703*01826a49SYabin Cui
3704*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
3705*01826a49SYabin Cui
3706*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
3707*01826a49SYabin Cui modification, are permitted provided that the following conditions are
3708*01826a49SYabin Cui met:
3709*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
3710*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
3711*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
3712*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
3713*01826a49SYabin Cui in the documentation and/or other materials provided with the
3714*01826a49SYabin Cui distribution.
3715*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3716*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3717*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3718*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3719*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3720*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3721*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3722*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3723*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3724*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3725*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3726*01826a49SYabin Cui
3727*01826a49SYabin Cui You can contact the author at :
3728*01826a49SYabin Cui - zstd source repository : https://github.com/Cyan4973/zstd
3729*01826a49SYabin Cui - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
3730*01826a49SYabin Cui */
3731*01826a49SYabin Cui
3732*01826a49SYabin Cui /* The objects defined into this file should be considered experimental.
3733*01826a49SYabin Cui * They are not labelled stable, as their prototype may change in the future.
3734*01826a49SYabin Cui * You can use them for tests, provide feedback, or if you can endure risk of future changes.
3735*01826a49SYabin Cui */
3736*01826a49SYabin Cui
3737*01826a49SYabin Cui
3738*01826a49SYabin Cui
3739*01826a49SYabin Cui /* *************************************
3740*01826a49SYabin Cui * Constants
3741*01826a49SYabin Cui ***************************************/
3742*01826a49SYabin Cui static size_t ZBUFFv05_blockHeaderSize = 3;
3743*01826a49SYabin Cui
3744*01826a49SYabin Cui
3745*01826a49SYabin Cui
3746*01826a49SYabin Cui /* *** Compression *** */
3747*01826a49SYabin Cui
ZBUFFv05_limitCopy(void * dst,size_t maxDstSize,const void * src,size_t srcSize)3748*01826a49SYabin Cui static size_t ZBUFFv05_limitCopy(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3749*01826a49SYabin Cui {
3750*01826a49SYabin Cui size_t length = MIN(maxDstSize, srcSize);
3751*01826a49SYabin Cui if (length > 0) {
3752*01826a49SYabin Cui memcpy(dst, src, length);
3753*01826a49SYabin Cui }
3754*01826a49SYabin Cui return length;
3755*01826a49SYabin Cui }
3756*01826a49SYabin Cui
3757*01826a49SYabin Cui
3758*01826a49SYabin Cui
3759*01826a49SYabin Cui
3760*01826a49SYabin Cui /** ************************************************
3761*01826a49SYabin Cui * Streaming decompression
3762*01826a49SYabin Cui *
3763*01826a49SYabin Cui * A ZBUFFv05_DCtx object is required to track streaming operation.
3764*01826a49SYabin Cui * Use ZBUFFv05_createDCtx() and ZBUFFv05_freeDCtx() to create/release resources.
3765*01826a49SYabin Cui * Use ZBUFFv05_decompressInit() to start a new decompression operation.
3766*01826a49SYabin Cui * ZBUFFv05_DCtx objects can be reused multiple times.
3767*01826a49SYabin Cui *
3768*01826a49SYabin Cui * Use ZBUFFv05_decompressContinue() repetitively to consume your input.
3769*01826a49SYabin Cui * *srcSizePtr and *maxDstSizePtr can be any size.
3770*01826a49SYabin Cui * The function will report how many bytes were read or written by modifying *srcSizePtr and *maxDstSizePtr.
3771*01826a49SYabin Cui * Note that it may not consume the entire input, in which case it's up to the caller to call again the function with remaining input.
3772*01826a49SYabin Cui * The content of dst will be overwritten (up to *maxDstSizePtr) at each function call, so save its content if it matters or change dst .
3773*01826a49SYabin Cui * return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
3774*01826a49SYabin Cui * or 0 when a frame is completely decoded
3775*01826a49SYabin Cui * or an error code, which can be tested using ZBUFFv05_isError().
3776*01826a49SYabin Cui *
3777*01826a49SYabin Cui * Hint : recommended buffer sizes (not compulsory)
3778*01826a49SYabin Cui * output : 128 KB block size is the internal unit, it ensures it's always possible to write a full block when it's decoded.
3779*01826a49SYabin Cui * input : just follow indications from ZBUFFv05_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
3780*01826a49SYabin Cui * **************************************************/
3781*01826a49SYabin Cui
3782*01826a49SYabin Cui typedef enum { ZBUFFv05ds_init, ZBUFFv05ds_readHeader, ZBUFFv05ds_loadHeader, ZBUFFv05ds_decodeHeader,
3783*01826a49SYabin Cui ZBUFFv05ds_read, ZBUFFv05ds_load, ZBUFFv05ds_flush } ZBUFFv05_dStage;
3784*01826a49SYabin Cui
3785*01826a49SYabin Cui /* *** Resource management *** */
3786*01826a49SYabin Cui
3787*01826a49SYabin Cui #define ZSTDv05_frameHeaderSize_max 5 /* too magical, should come from reference */
3788*01826a49SYabin Cui struct ZBUFFv05_DCtx_s {
3789*01826a49SYabin Cui ZSTDv05_DCtx* zc;
3790*01826a49SYabin Cui ZSTDv05_parameters params;
3791*01826a49SYabin Cui char* inBuff;
3792*01826a49SYabin Cui size_t inBuffSize;
3793*01826a49SYabin Cui size_t inPos;
3794*01826a49SYabin Cui char* outBuff;
3795*01826a49SYabin Cui size_t outBuffSize;
3796*01826a49SYabin Cui size_t outStart;
3797*01826a49SYabin Cui size_t outEnd;
3798*01826a49SYabin Cui size_t hPos;
3799*01826a49SYabin Cui ZBUFFv05_dStage stage;
3800*01826a49SYabin Cui unsigned char headerBuffer[ZSTDv05_frameHeaderSize_max];
3801*01826a49SYabin Cui }; /* typedef'd to ZBUFFv05_DCtx within "zstd_buffered.h" */
3802*01826a49SYabin Cui
3803*01826a49SYabin Cui
ZBUFFv05_createDCtx(void)3804*01826a49SYabin Cui ZBUFFv05_DCtx* ZBUFFv05_createDCtx(void)
3805*01826a49SYabin Cui {
3806*01826a49SYabin Cui ZBUFFv05_DCtx* zbc = (ZBUFFv05_DCtx*)malloc(sizeof(ZBUFFv05_DCtx));
3807*01826a49SYabin Cui if (zbc==NULL) return NULL;
3808*01826a49SYabin Cui memset(zbc, 0, sizeof(*zbc));
3809*01826a49SYabin Cui zbc->zc = ZSTDv05_createDCtx();
3810*01826a49SYabin Cui zbc->stage = ZBUFFv05ds_init;
3811*01826a49SYabin Cui return zbc;
3812*01826a49SYabin Cui }
3813*01826a49SYabin Cui
ZBUFFv05_freeDCtx(ZBUFFv05_DCtx * zbc)3814*01826a49SYabin Cui size_t ZBUFFv05_freeDCtx(ZBUFFv05_DCtx* zbc)
3815*01826a49SYabin Cui {
3816*01826a49SYabin Cui if (zbc==NULL) return 0; /* support free on null */
3817*01826a49SYabin Cui ZSTDv05_freeDCtx(zbc->zc);
3818*01826a49SYabin Cui free(zbc->inBuff);
3819*01826a49SYabin Cui free(zbc->outBuff);
3820*01826a49SYabin Cui free(zbc);
3821*01826a49SYabin Cui return 0;
3822*01826a49SYabin Cui }
3823*01826a49SYabin Cui
3824*01826a49SYabin Cui
3825*01826a49SYabin Cui /* *** Initialization *** */
3826*01826a49SYabin Cui
ZBUFFv05_decompressInitDictionary(ZBUFFv05_DCtx * zbc,const void * dict,size_t dictSize)3827*01826a49SYabin Cui size_t ZBUFFv05_decompressInitDictionary(ZBUFFv05_DCtx* zbc, const void* dict, size_t dictSize)
3828*01826a49SYabin Cui {
3829*01826a49SYabin Cui zbc->stage = ZBUFFv05ds_readHeader;
3830*01826a49SYabin Cui zbc->hPos = zbc->inPos = zbc->outStart = zbc->outEnd = 0;
3831*01826a49SYabin Cui return ZSTDv05_decompressBegin_usingDict(zbc->zc, dict, dictSize);
3832*01826a49SYabin Cui }
3833*01826a49SYabin Cui
ZBUFFv05_decompressInit(ZBUFFv05_DCtx * zbc)3834*01826a49SYabin Cui size_t ZBUFFv05_decompressInit(ZBUFFv05_DCtx* zbc)
3835*01826a49SYabin Cui {
3836*01826a49SYabin Cui return ZBUFFv05_decompressInitDictionary(zbc, NULL, 0);
3837*01826a49SYabin Cui }
3838*01826a49SYabin Cui
3839*01826a49SYabin Cui
3840*01826a49SYabin Cui /* *** Decompression *** */
3841*01826a49SYabin Cui
ZBUFFv05_decompressContinue(ZBUFFv05_DCtx * zbc,void * dst,size_t * maxDstSizePtr,const void * src,size_t * srcSizePtr)3842*01826a49SYabin Cui size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* zbc, void* dst, size_t* maxDstSizePtr, const void* src, size_t* srcSizePtr)
3843*01826a49SYabin Cui {
3844*01826a49SYabin Cui const char* const istart = (const char*)src;
3845*01826a49SYabin Cui const char* ip = istart;
3846*01826a49SYabin Cui const char* const iend = istart + *srcSizePtr;
3847*01826a49SYabin Cui char* const ostart = (char*)dst;
3848*01826a49SYabin Cui char* op = ostart;
3849*01826a49SYabin Cui char* const oend = ostart + *maxDstSizePtr;
3850*01826a49SYabin Cui U32 notDone = 1;
3851*01826a49SYabin Cui
3852*01826a49SYabin Cui while (notDone) {
3853*01826a49SYabin Cui switch(zbc->stage)
3854*01826a49SYabin Cui {
3855*01826a49SYabin Cui case ZBUFFv05ds_init :
3856*01826a49SYabin Cui return ERROR(init_missing);
3857*01826a49SYabin Cui
3858*01826a49SYabin Cui case ZBUFFv05ds_readHeader :
3859*01826a49SYabin Cui /* read header from src */
3860*01826a49SYabin Cui {
3861*01826a49SYabin Cui size_t headerSize = ZSTDv05_getFrameParams(&(zbc->params), src, *srcSizePtr);
3862*01826a49SYabin Cui if (ZSTDv05_isError(headerSize)) return headerSize;
3863*01826a49SYabin Cui if (headerSize) {
3864*01826a49SYabin Cui /* not enough input to decode header : tell how many bytes would be necessary */
3865*01826a49SYabin Cui memcpy(zbc->headerBuffer+zbc->hPos, src, *srcSizePtr);
3866*01826a49SYabin Cui zbc->hPos += *srcSizePtr;
3867*01826a49SYabin Cui *maxDstSizePtr = 0;
3868*01826a49SYabin Cui zbc->stage = ZBUFFv05ds_loadHeader;
3869*01826a49SYabin Cui return headerSize - zbc->hPos;
3870*01826a49SYabin Cui }
3871*01826a49SYabin Cui zbc->stage = ZBUFFv05ds_decodeHeader;
3872*01826a49SYabin Cui break;
3873*01826a49SYabin Cui }
3874*01826a49SYabin Cui /* fall-through */
3875*01826a49SYabin Cui case ZBUFFv05ds_loadHeader:
3876*01826a49SYabin Cui /* complete header from src */
3877*01826a49SYabin Cui {
3878*01826a49SYabin Cui size_t headerSize = ZBUFFv05_limitCopy(
3879*01826a49SYabin Cui zbc->headerBuffer + zbc->hPos, ZSTDv05_frameHeaderSize_max - zbc->hPos,
3880*01826a49SYabin Cui src, *srcSizePtr);
3881*01826a49SYabin Cui zbc->hPos += headerSize;
3882*01826a49SYabin Cui ip += headerSize;
3883*01826a49SYabin Cui headerSize = ZSTDv05_getFrameParams(&(zbc->params), zbc->headerBuffer, zbc->hPos);
3884*01826a49SYabin Cui if (ZSTDv05_isError(headerSize)) return headerSize;
3885*01826a49SYabin Cui if (headerSize) {
3886*01826a49SYabin Cui /* not enough input to decode header : tell how many bytes would be necessary */
3887*01826a49SYabin Cui *maxDstSizePtr = 0;
3888*01826a49SYabin Cui return headerSize - zbc->hPos;
3889*01826a49SYabin Cui }
3890*01826a49SYabin Cui /* zbc->stage = ZBUFFv05ds_decodeHeader; break; */ /* useless : stage follows */
3891*01826a49SYabin Cui }
3892*01826a49SYabin Cui /* fall-through */
3893*01826a49SYabin Cui case ZBUFFv05ds_decodeHeader:
3894*01826a49SYabin Cui /* apply header to create / resize buffers */
3895*01826a49SYabin Cui {
3896*01826a49SYabin Cui size_t neededOutSize = (size_t)1 << zbc->params.windowLog;
3897*01826a49SYabin Cui size_t neededInSize = BLOCKSIZE; /* a block is never > BLOCKSIZE */
3898*01826a49SYabin Cui if (zbc->inBuffSize < neededInSize) {
3899*01826a49SYabin Cui free(zbc->inBuff);
3900*01826a49SYabin Cui zbc->inBuffSize = neededInSize;
3901*01826a49SYabin Cui zbc->inBuff = (char*)malloc(neededInSize);
3902*01826a49SYabin Cui if (zbc->inBuff == NULL) return ERROR(memory_allocation);
3903*01826a49SYabin Cui }
3904*01826a49SYabin Cui if (zbc->outBuffSize < neededOutSize) {
3905*01826a49SYabin Cui free(zbc->outBuff);
3906*01826a49SYabin Cui zbc->outBuffSize = neededOutSize;
3907*01826a49SYabin Cui zbc->outBuff = (char*)malloc(neededOutSize);
3908*01826a49SYabin Cui if (zbc->outBuff == NULL) return ERROR(memory_allocation);
3909*01826a49SYabin Cui } }
3910*01826a49SYabin Cui if (zbc->hPos) {
3911*01826a49SYabin Cui /* some data already loaded into headerBuffer : transfer into inBuff */
3912*01826a49SYabin Cui memcpy(zbc->inBuff, zbc->headerBuffer, zbc->hPos);
3913*01826a49SYabin Cui zbc->inPos = zbc->hPos;
3914*01826a49SYabin Cui zbc->hPos = 0;
3915*01826a49SYabin Cui zbc->stage = ZBUFFv05ds_load;
3916*01826a49SYabin Cui break;
3917*01826a49SYabin Cui }
3918*01826a49SYabin Cui zbc->stage = ZBUFFv05ds_read;
3919*01826a49SYabin Cui /* fall-through */
3920*01826a49SYabin Cui case ZBUFFv05ds_read:
3921*01826a49SYabin Cui {
3922*01826a49SYabin Cui size_t neededInSize = ZSTDv05_nextSrcSizeToDecompress(zbc->zc);
3923*01826a49SYabin Cui if (neededInSize==0) { /* end of frame */
3924*01826a49SYabin Cui zbc->stage = ZBUFFv05ds_init;
3925*01826a49SYabin Cui notDone = 0;
3926*01826a49SYabin Cui break;
3927*01826a49SYabin Cui }
3928*01826a49SYabin Cui if ((size_t)(iend-ip) >= neededInSize) {
3929*01826a49SYabin Cui /* directly decode from src */
3930*01826a49SYabin Cui size_t decodedSize = ZSTDv05_decompressContinue(zbc->zc,
3931*01826a49SYabin Cui zbc->outBuff + zbc->outStart, zbc->outBuffSize - zbc->outStart,
3932*01826a49SYabin Cui ip, neededInSize);
3933*01826a49SYabin Cui if (ZSTDv05_isError(decodedSize)) return decodedSize;
3934*01826a49SYabin Cui ip += neededInSize;
3935*01826a49SYabin Cui if (!decodedSize) break; /* this was just a header */
3936*01826a49SYabin Cui zbc->outEnd = zbc->outStart + decodedSize;
3937*01826a49SYabin Cui zbc->stage = ZBUFFv05ds_flush;
3938*01826a49SYabin Cui break;
3939*01826a49SYabin Cui }
3940*01826a49SYabin Cui if (ip==iend) { notDone = 0; break; } /* no more input */
3941*01826a49SYabin Cui zbc->stage = ZBUFFv05ds_load;
3942*01826a49SYabin Cui }
3943*01826a49SYabin Cui /* fall-through */
3944*01826a49SYabin Cui case ZBUFFv05ds_load:
3945*01826a49SYabin Cui {
3946*01826a49SYabin Cui size_t neededInSize = ZSTDv05_nextSrcSizeToDecompress(zbc->zc);
3947*01826a49SYabin Cui size_t toLoad = neededInSize - zbc->inPos; /* should always be <= remaining space within inBuff */
3948*01826a49SYabin Cui size_t loadedSize;
3949*01826a49SYabin Cui if (toLoad > zbc->inBuffSize - zbc->inPos) return ERROR(corruption_detected); /* should never happen */
3950*01826a49SYabin Cui loadedSize = ZBUFFv05_limitCopy(zbc->inBuff + zbc->inPos, toLoad, ip, iend-ip);
3951*01826a49SYabin Cui ip += loadedSize;
3952*01826a49SYabin Cui zbc->inPos += loadedSize;
3953*01826a49SYabin Cui if (loadedSize < toLoad) { notDone = 0; break; } /* not enough input, wait for more */
3954*01826a49SYabin Cui {
3955*01826a49SYabin Cui size_t decodedSize = ZSTDv05_decompressContinue(zbc->zc,
3956*01826a49SYabin Cui zbc->outBuff + zbc->outStart, zbc->outBuffSize - zbc->outStart,
3957*01826a49SYabin Cui zbc->inBuff, neededInSize);
3958*01826a49SYabin Cui if (ZSTDv05_isError(decodedSize)) return decodedSize;
3959*01826a49SYabin Cui zbc->inPos = 0; /* input is consumed */
3960*01826a49SYabin Cui if (!decodedSize) { zbc->stage = ZBUFFv05ds_read; break; } /* this was just a header */
3961*01826a49SYabin Cui zbc->outEnd = zbc->outStart + decodedSize;
3962*01826a49SYabin Cui zbc->stage = ZBUFFv05ds_flush;
3963*01826a49SYabin Cui /* break; */ /* ZBUFFv05ds_flush follows */
3964*01826a49SYabin Cui }
3965*01826a49SYabin Cui }
3966*01826a49SYabin Cui /* fall-through */
3967*01826a49SYabin Cui case ZBUFFv05ds_flush:
3968*01826a49SYabin Cui {
3969*01826a49SYabin Cui size_t toFlushSize = zbc->outEnd - zbc->outStart;
3970*01826a49SYabin Cui size_t flushedSize = ZBUFFv05_limitCopy(op, oend-op, zbc->outBuff + zbc->outStart, toFlushSize);
3971*01826a49SYabin Cui op += flushedSize;
3972*01826a49SYabin Cui zbc->outStart += flushedSize;
3973*01826a49SYabin Cui if (flushedSize == toFlushSize) {
3974*01826a49SYabin Cui zbc->stage = ZBUFFv05ds_read;
3975*01826a49SYabin Cui if (zbc->outStart + BLOCKSIZE > zbc->outBuffSize)
3976*01826a49SYabin Cui zbc->outStart = zbc->outEnd = 0;
3977*01826a49SYabin Cui break;
3978*01826a49SYabin Cui }
3979*01826a49SYabin Cui /* cannot flush everything */
3980*01826a49SYabin Cui notDone = 0;
3981*01826a49SYabin Cui break;
3982*01826a49SYabin Cui }
3983*01826a49SYabin Cui default: return ERROR(GENERIC); /* impossible */
3984*01826a49SYabin Cui } }
3985*01826a49SYabin Cui
3986*01826a49SYabin Cui *srcSizePtr = ip-istart;
3987*01826a49SYabin Cui *maxDstSizePtr = op-ostart;
3988*01826a49SYabin Cui
3989*01826a49SYabin Cui { size_t nextSrcSizeHint = ZSTDv05_nextSrcSizeToDecompress(zbc->zc);
3990*01826a49SYabin Cui if (nextSrcSizeHint > ZBUFFv05_blockHeaderSize) nextSrcSizeHint+= ZBUFFv05_blockHeaderSize; /* get next block header too */
3991*01826a49SYabin Cui nextSrcSizeHint -= zbc->inPos; /* already loaded*/
3992*01826a49SYabin Cui return nextSrcSizeHint;
3993*01826a49SYabin Cui }
3994*01826a49SYabin Cui }
3995*01826a49SYabin Cui
3996*01826a49SYabin Cui
3997*01826a49SYabin Cui
3998*01826a49SYabin Cui /* *************************************
3999*01826a49SYabin Cui * Tool functions
4000*01826a49SYabin Cui ***************************************/
ZBUFFv05_isError(size_t errorCode)4001*01826a49SYabin Cui unsigned ZBUFFv05_isError(size_t errorCode) { return ERR_isError(errorCode); }
ZBUFFv05_getErrorName(size_t errorCode)4002*01826a49SYabin Cui const char* ZBUFFv05_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
4003*01826a49SYabin Cui
ZBUFFv05_recommendedDInSize(void)4004*01826a49SYabin Cui size_t ZBUFFv05_recommendedDInSize(void) { return BLOCKSIZE + ZBUFFv05_blockHeaderSize /* block header size*/ ; }
ZBUFFv05_recommendedDOutSize(void)4005*01826a49SYabin Cui size_t ZBUFFv05_recommendedDOutSize(void) { return BLOCKSIZE; }
4006