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 <stddef.h> /* size_t, ptrdiff_t */
14*01826a49SYabin Cui #include <string.h> /* memcpy */
15*01826a49SYabin Cui #include <stdlib.h> /* malloc, free, qsort */
16*01826a49SYabin Cui
17*01826a49SYabin Cui #ifndef XXH_STATIC_LINKING_ONLY
18*01826a49SYabin Cui # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
19*01826a49SYabin Cui #endif
20*01826a49SYabin Cui #include "../common/xxhash.h" /* XXH64_* */
21*01826a49SYabin Cui #include "zstd_v07.h"
22*01826a49SYabin Cui
23*01826a49SYabin Cui #define FSEv07_STATIC_LINKING_ONLY /* FSEv07_MIN_TABLELOG */
24*01826a49SYabin Cui #define HUFv07_STATIC_LINKING_ONLY /* HUFv07_TABLELOG_ABSOLUTEMAX */
25*01826a49SYabin Cui #define ZSTDv07_STATIC_LINKING_ONLY
26*01826a49SYabin Cui
27*01826a49SYabin Cui #include "../common/compiler.h"
28*01826a49SYabin Cui #include "../common/error_private.h"
29*01826a49SYabin Cui
30*01826a49SYabin Cui
31*01826a49SYabin Cui #ifdef ZSTDv07_STATIC_LINKING_ONLY
32*01826a49SYabin Cui
33*01826a49SYabin Cui /* ====================================================================================
34*01826a49SYabin Cui * The definitions in this section are considered experimental.
35*01826a49SYabin Cui * They should never be used with a dynamic library, as they may change in the future.
36*01826a49SYabin Cui * They are provided for advanced usages.
37*01826a49SYabin Cui * Use them only in association with static linking.
38*01826a49SYabin Cui * ==================================================================================== */
39*01826a49SYabin Cui
40*01826a49SYabin Cui /*--- Constants ---*/
41*01826a49SYabin Cui #define ZSTDv07_MAGIC_SKIPPABLE_START 0x184D2A50U
42*01826a49SYabin Cui
43*01826a49SYabin Cui #define ZSTDv07_WINDOWLOG_MAX_32 25
44*01826a49SYabin Cui #define ZSTDv07_WINDOWLOG_MAX_64 27
45*01826a49SYabin Cui #define ZSTDv07_WINDOWLOG_MAX ((U32)(MEM_32bits() ? ZSTDv07_WINDOWLOG_MAX_32 : ZSTDv07_WINDOWLOG_MAX_64))
46*01826a49SYabin Cui #define ZSTDv07_WINDOWLOG_MIN 18
47*01826a49SYabin Cui #define ZSTDv07_CHAINLOG_MAX (ZSTDv07_WINDOWLOG_MAX+1)
48*01826a49SYabin Cui #define ZSTDv07_CHAINLOG_MIN 4
49*01826a49SYabin Cui #define ZSTDv07_HASHLOG_MAX ZSTDv07_WINDOWLOG_MAX
50*01826a49SYabin Cui #define ZSTDv07_HASHLOG_MIN 12
51*01826a49SYabin Cui #define ZSTDv07_HASHLOG3_MAX 17
52*01826a49SYabin Cui #define ZSTDv07_SEARCHLOG_MAX (ZSTDv07_WINDOWLOG_MAX-1)
53*01826a49SYabin Cui #define ZSTDv07_SEARCHLOG_MIN 1
54*01826a49SYabin Cui #define ZSTDv07_SEARCHLENGTH_MAX 7
55*01826a49SYabin Cui #define ZSTDv07_SEARCHLENGTH_MIN 3
56*01826a49SYabin Cui #define ZSTDv07_TARGETLENGTH_MIN 4
57*01826a49SYabin Cui #define ZSTDv07_TARGETLENGTH_MAX 999
58*01826a49SYabin Cui
59*01826a49SYabin Cui #define ZSTDv07_FRAMEHEADERSIZE_MAX 18 /* for static allocation */
60*01826a49SYabin Cui static const size_t ZSTDv07_frameHeaderSize_min = 5;
61*01826a49SYabin Cui static const size_t ZSTDv07_frameHeaderSize_max = ZSTDv07_FRAMEHEADERSIZE_MAX;
62*01826a49SYabin Cui static const size_t ZSTDv07_skippableHeaderSize = 8; /* magic number + skippable frame length */
63*01826a49SYabin Cui
64*01826a49SYabin Cui
65*01826a49SYabin Cui /* custom memory allocation functions */
66*01826a49SYabin Cui typedef void* (*ZSTDv07_allocFunction) (void* opaque, size_t size);
67*01826a49SYabin Cui typedef void (*ZSTDv07_freeFunction) (void* opaque, void* address);
68*01826a49SYabin Cui typedef struct { ZSTDv07_allocFunction customAlloc; ZSTDv07_freeFunction customFree; void* opaque; } ZSTDv07_customMem;
69*01826a49SYabin Cui
70*01826a49SYabin Cui
71*01826a49SYabin Cui /*--- Advanced Decompression functions ---*/
72*01826a49SYabin Cui
73*01826a49SYabin Cui /*! ZSTDv07_estimateDCtxSize() :
74*01826a49SYabin Cui * Gives the potential amount of memory allocated to create a ZSTDv07_DCtx */
75*01826a49SYabin Cui ZSTDLIBv07_API size_t ZSTDv07_estimateDCtxSize(void);
76*01826a49SYabin Cui
77*01826a49SYabin Cui /*! ZSTDv07_createDCtx_advanced() :
78*01826a49SYabin Cui * Create a ZSTD decompression context using external alloc and free functions */
79*01826a49SYabin Cui ZSTDLIBv07_API ZSTDv07_DCtx* ZSTDv07_createDCtx_advanced(ZSTDv07_customMem customMem);
80*01826a49SYabin Cui
81*01826a49SYabin Cui /*! ZSTDv07_sizeofDCtx() :
82*01826a49SYabin Cui * Gives the amount of memory used by a given ZSTDv07_DCtx */
83*01826a49SYabin Cui ZSTDLIBv07_API size_t ZSTDv07_sizeofDCtx(const ZSTDv07_DCtx* dctx);
84*01826a49SYabin Cui
85*01826a49SYabin Cui
86*01826a49SYabin Cui /* ******************************************************************
87*01826a49SYabin Cui * Buffer-less streaming functions (synchronous mode)
88*01826a49SYabin Cui ********************************************************************/
89*01826a49SYabin Cui
90*01826a49SYabin Cui ZSTDLIBv07_API size_t ZSTDv07_decompressBegin(ZSTDv07_DCtx* dctx);
91*01826a49SYabin Cui ZSTDLIBv07_API size_t ZSTDv07_decompressBegin_usingDict(ZSTDv07_DCtx* dctx, const void* dict, size_t dictSize);
92*01826a49SYabin Cui ZSTDLIBv07_API void ZSTDv07_copyDCtx(ZSTDv07_DCtx* dctx, const ZSTDv07_DCtx* preparedDCtx);
93*01826a49SYabin Cui
94*01826a49SYabin Cui ZSTDLIBv07_API size_t ZSTDv07_nextSrcSizeToDecompress(ZSTDv07_DCtx* dctx);
95*01826a49SYabin Cui ZSTDLIBv07_API size_t ZSTDv07_decompressContinue(ZSTDv07_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
96*01826a49SYabin Cui
97*01826a49SYabin Cui /*
98*01826a49SYabin Cui Buffer-less streaming decompression (synchronous mode)
99*01826a49SYabin Cui
100*01826a49SYabin Cui A ZSTDv07_DCtx object is required to track streaming operations.
101*01826a49SYabin Cui Use ZSTDv07_createDCtx() / ZSTDv07_freeDCtx() to manage it.
102*01826a49SYabin Cui A ZSTDv07_DCtx object can be re-used multiple times.
103*01826a49SYabin Cui
104*01826a49SYabin Cui First optional operation is to retrieve frame parameters, using ZSTDv07_getFrameParams(), which doesn't consume the input.
105*01826a49SYabin Cui It can provide the minimum size of rolling buffer required to properly decompress data (`windowSize`),
106*01826a49SYabin Cui and optionally the final size of uncompressed content.
107*01826a49SYabin Cui (Note : content size is an optional info that may not be present. 0 means : content size unknown)
108*01826a49SYabin Cui Frame parameters are extracted from the beginning of compressed frame.
109*01826a49SYabin Cui The amount of data to read is variable, from ZSTDv07_frameHeaderSize_min to ZSTDv07_frameHeaderSize_max (so if `srcSize` >= ZSTDv07_frameHeaderSize_max, it will always work)
110*01826a49SYabin Cui If `srcSize` is too small for operation to succeed, function will return the minimum size it requires to produce a result.
111*01826a49SYabin Cui Result : 0 when successful, it means the ZSTDv07_frameParams structure has been filled.
112*01826a49SYabin Cui >0 : means there is not enough data into `src`. Provides the expected size to successfully decode header.
113*01826a49SYabin Cui errorCode, which can be tested using ZSTDv07_isError()
114*01826a49SYabin Cui
115*01826a49SYabin Cui Start decompression, with ZSTDv07_decompressBegin() or ZSTDv07_decompressBegin_usingDict().
116*01826a49SYabin Cui Alternatively, you can copy a prepared context, using ZSTDv07_copyDCtx().
117*01826a49SYabin Cui
118*01826a49SYabin Cui Then use ZSTDv07_nextSrcSizeToDecompress() and ZSTDv07_decompressContinue() alternatively.
119*01826a49SYabin Cui ZSTDv07_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTDv07_decompressContinue().
120*01826a49SYabin Cui ZSTDv07_decompressContinue() requires this exact amount of bytes, or it will fail.
121*01826a49SYabin Cui
122*01826a49SYabin Cui @result of ZSTDv07_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
123*01826a49SYabin Cui It can be zero, which is not an error; it just means ZSTDv07_decompressContinue() has decoded some header.
124*01826a49SYabin Cui
125*01826a49SYabin Cui ZSTDv07_decompressContinue() needs previous data blocks during decompression, up to `windowSize`.
126*01826a49SYabin Cui They should preferably be located contiguously, prior to current block.
127*01826a49SYabin Cui Alternatively, a round buffer of sufficient size is also possible. Sufficient size is determined by frame parameters.
128*01826a49SYabin Cui ZSTDv07_decompressContinue() is very sensitive to contiguity,
129*01826a49SYabin Cui if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,
130*01826a49SYabin Cui or that previous contiguous segment is large enough to properly handle maximum back-reference.
131*01826a49SYabin Cui
132*01826a49SYabin Cui A frame is fully decoded when ZSTDv07_nextSrcSizeToDecompress() returns zero.
133*01826a49SYabin Cui Context can then be reset to start a new decompression.
134*01826a49SYabin Cui
135*01826a49SYabin Cui
136*01826a49SYabin Cui == Special case : skippable frames ==
137*01826a49SYabin Cui
138*01826a49SYabin Cui Skippable frames allow the integration of user-defined data into a flow of concatenated frames.
139*01826a49SYabin Cui Skippable frames will be ignored (skipped) by a decompressor. The format of skippable frame is following:
140*01826a49SYabin Cui a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
141*01826a49SYabin Cui b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
142*01826a49SYabin Cui c) Frame Content - any content (User Data) of length equal to Frame Size
143*01826a49SYabin Cui For skippable frames ZSTDv07_decompressContinue() always returns 0.
144*01826a49SYabin Cui For skippable frames ZSTDv07_getFrameParams() returns fparamsPtr->windowLog==0 what means that a frame is skippable.
145*01826a49SYabin Cui It also returns Frame Size as fparamsPtr->frameContentSize.
146*01826a49SYabin Cui */
147*01826a49SYabin Cui
148*01826a49SYabin Cui
149*01826a49SYabin Cui /* **************************************
150*01826a49SYabin Cui * Block functions
151*01826a49SYabin Cui ****************************************/
152*01826a49SYabin Cui /*! Block functions produce and decode raw zstd blocks, without frame metadata.
153*01826a49SYabin Cui Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes).
154*01826a49SYabin Cui User will have to take in charge required information to regenerate data, such as compressed and content sizes.
155*01826a49SYabin Cui
156*01826a49SYabin Cui A few rules to respect :
157*01826a49SYabin Cui - Compressing and decompressing require a context structure
158*01826a49SYabin Cui + Use ZSTDv07_createCCtx() and ZSTDv07_createDCtx()
159*01826a49SYabin Cui - It is necessary to init context before starting
160*01826a49SYabin Cui + compression : ZSTDv07_compressBegin()
161*01826a49SYabin Cui + decompression : ZSTDv07_decompressBegin()
162*01826a49SYabin Cui + variants _usingDict() are also allowed
163*01826a49SYabin Cui + copyCCtx() and copyDCtx() work too
164*01826a49SYabin Cui - Block size is limited, it must be <= ZSTDv07_getBlockSizeMax()
165*01826a49SYabin Cui + If you need to compress more, cut data into multiple blocks
166*01826a49SYabin Cui + Consider using the regular ZSTDv07_compress() instead, as frame metadata costs become negligible when source size is large.
167*01826a49SYabin Cui - When a block is considered not compressible enough, ZSTDv07_compressBlock() result will be zero.
168*01826a49SYabin Cui In which case, nothing is produced into `dst`.
169*01826a49SYabin Cui + User must test for such outcome and deal directly with uncompressed data
170*01826a49SYabin Cui + ZSTDv07_decompressBlock() doesn't accept uncompressed data as input !!!
171*01826a49SYabin Cui + In case of multiple successive blocks, decoder must be informed of uncompressed block existence to follow proper history.
172*01826a49SYabin Cui Use ZSTDv07_insertBlock() in such a case.
173*01826a49SYabin Cui */
174*01826a49SYabin Cui
175*01826a49SYabin Cui #define ZSTDv07_BLOCKSIZE_ABSOLUTEMAX (128 * 1024) /* define, for static allocation */
176*01826a49SYabin Cui ZSTDLIBv07_API size_t ZSTDv07_decompressBlock(ZSTDv07_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
177*01826a49SYabin Cui ZSTDLIBv07_API size_t ZSTDv07_insertBlock(ZSTDv07_DCtx* dctx, const void* blockStart, size_t blockSize); /**< insert block into `dctx` history. Useful for uncompressed blocks */
178*01826a49SYabin Cui
179*01826a49SYabin Cui
180*01826a49SYabin Cui #endif /* ZSTDv07_STATIC_LINKING_ONLY */
181*01826a49SYabin Cui
182*01826a49SYabin Cui
183*01826a49SYabin Cui /* ******************************************************************
184*01826a49SYabin Cui mem.h
185*01826a49SYabin Cui low-level memory access routines
186*01826a49SYabin Cui Copyright (C) 2013-2015, Yann Collet.
187*01826a49SYabin Cui
188*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
189*01826a49SYabin Cui
190*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
191*01826a49SYabin Cui modification, are permitted provided that the following conditions are
192*01826a49SYabin Cui met:
193*01826a49SYabin Cui
194*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
195*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
196*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
197*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
198*01826a49SYabin Cui in the documentation and/or other materials provided with the
199*01826a49SYabin Cui distribution.
200*01826a49SYabin Cui
201*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
202*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
203*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
204*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
206*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
207*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
208*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
209*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
211*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
212*01826a49SYabin Cui
213*01826a49SYabin Cui You can contact the author at :
214*01826a49SYabin Cui - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
215*01826a49SYabin Cui - Public forum : https://groups.google.com/forum/#!forum/lz4c
216*01826a49SYabin Cui ****************************************************************** */
217*01826a49SYabin Cui #ifndef MEM_H_MODULE
218*01826a49SYabin Cui #define MEM_H_MODULE
219*01826a49SYabin Cui
220*01826a49SYabin Cui #if defined (__cplusplus)
221*01826a49SYabin Cui extern "C" {
222*01826a49SYabin Cui #endif
223*01826a49SYabin Cui
224*01826a49SYabin Cui /*-****************************************
225*01826a49SYabin Cui * Compiler specifics
226*01826a49SYabin Cui ******************************************/
227*01826a49SYabin Cui #if defined(_MSC_VER) /* Visual Studio */
228*01826a49SYabin Cui # include <stdlib.h> /* _byteswap_ulong */
229*01826a49SYabin Cui # include <intrin.h> /* _byteswap_* */
230*01826a49SYabin Cui #endif
231*01826a49SYabin Cui
232*01826a49SYabin Cui
233*01826a49SYabin Cui /*-**************************************************************
234*01826a49SYabin Cui * Basic Types
235*01826a49SYabin Cui *****************************************************************/
236*01826a49SYabin Cui #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
237*01826a49SYabin Cui # if defined(_AIX)
238*01826a49SYabin Cui # include <inttypes.h>
239*01826a49SYabin Cui # else
240*01826a49SYabin Cui # include <stdint.h> /* intptr_t */
241*01826a49SYabin Cui # endif
242*01826a49SYabin Cui typedef uint8_t BYTE;
243*01826a49SYabin Cui typedef uint16_t U16;
244*01826a49SYabin Cui typedef int16_t S16;
245*01826a49SYabin Cui typedef uint32_t U32;
246*01826a49SYabin Cui typedef int32_t S32;
247*01826a49SYabin Cui typedef uint64_t U64;
248*01826a49SYabin Cui typedef int64_t S64;
249*01826a49SYabin Cui #else
250*01826a49SYabin Cui typedef unsigned char BYTE;
251*01826a49SYabin Cui typedef unsigned short U16;
252*01826a49SYabin Cui typedef signed short S16;
253*01826a49SYabin Cui typedef unsigned int U32;
254*01826a49SYabin Cui typedef signed int S32;
255*01826a49SYabin Cui typedef unsigned long long U64;
256*01826a49SYabin Cui typedef signed long long S64;
257*01826a49SYabin Cui #endif
258*01826a49SYabin Cui
259*01826a49SYabin Cui
260*01826a49SYabin Cui /*-**************************************************************
261*01826a49SYabin Cui * Memory I/O
262*01826a49SYabin Cui *****************************************************************/
263*01826a49SYabin Cui
MEM_32bits(void)264*01826a49SYabin Cui MEM_STATIC unsigned MEM_32bits(void) { return sizeof(size_t)==4; }
MEM_64bits(void)265*01826a49SYabin Cui MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; }
266*01826a49SYabin Cui
MEM_isLittleEndian(void)267*01826a49SYabin Cui MEM_STATIC unsigned MEM_isLittleEndian(void)
268*01826a49SYabin Cui {
269*01826a49SYabin Cui const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
270*01826a49SYabin Cui return one.c[0];
271*01826a49SYabin Cui }
272*01826a49SYabin Cui
MEM_read16(const void * memPtr)273*01826a49SYabin Cui MEM_STATIC U16 MEM_read16(const void* memPtr)
274*01826a49SYabin Cui {
275*01826a49SYabin Cui U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
276*01826a49SYabin Cui }
277*01826a49SYabin Cui
MEM_read32(const void * memPtr)278*01826a49SYabin Cui MEM_STATIC U32 MEM_read32(const void* memPtr)
279*01826a49SYabin Cui {
280*01826a49SYabin Cui U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
281*01826a49SYabin Cui }
282*01826a49SYabin Cui
MEM_read64(const void * memPtr)283*01826a49SYabin Cui MEM_STATIC U64 MEM_read64(const void* memPtr)
284*01826a49SYabin Cui {
285*01826a49SYabin Cui U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
286*01826a49SYabin Cui }
287*01826a49SYabin Cui
MEM_write16(void * memPtr,U16 value)288*01826a49SYabin Cui MEM_STATIC void MEM_write16(void* memPtr, U16 value)
289*01826a49SYabin Cui {
290*01826a49SYabin Cui memcpy(memPtr, &value, sizeof(value));
291*01826a49SYabin Cui }
292*01826a49SYabin Cui
MEM_swap32(U32 in)293*01826a49SYabin Cui MEM_STATIC U32 MEM_swap32(U32 in)
294*01826a49SYabin Cui {
295*01826a49SYabin Cui #if defined(_MSC_VER) /* Visual Studio */
296*01826a49SYabin Cui return _byteswap_ulong(in);
297*01826a49SYabin Cui #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)
298*01826a49SYabin Cui return __builtin_bswap32(in);
299*01826a49SYabin Cui #else
300*01826a49SYabin Cui return ((in << 24) & 0xff000000 ) |
301*01826a49SYabin Cui ((in << 8) & 0x00ff0000 ) |
302*01826a49SYabin Cui ((in >> 8) & 0x0000ff00 ) |
303*01826a49SYabin Cui ((in >> 24) & 0x000000ff );
304*01826a49SYabin Cui #endif
305*01826a49SYabin Cui }
306*01826a49SYabin Cui
MEM_swap64(U64 in)307*01826a49SYabin Cui MEM_STATIC U64 MEM_swap64(U64 in)
308*01826a49SYabin Cui {
309*01826a49SYabin Cui #if defined(_MSC_VER) /* Visual Studio */
310*01826a49SYabin Cui return _byteswap_uint64(in);
311*01826a49SYabin Cui #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)
312*01826a49SYabin Cui return __builtin_bswap64(in);
313*01826a49SYabin Cui #else
314*01826a49SYabin Cui return ((in << 56) & 0xff00000000000000ULL) |
315*01826a49SYabin Cui ((in << 40) & 0x00ff000000000000ULL) |
316*01826a49SYabin Cui ((in << 24) & 0x0000ff0000000000ULL) |
317*01826a49SYabin Cui ((in << 8) & 0x000000ff00000000ULL) |
318*01826a49SYabin Cui ((in >> 8) & 0x00000000ff000000ULL) |
319*01826a49SYabin Cui ((in >> 24) & 0x0000000000ff0000ULL) |
320*01826a49SYabin Cui ((in >> 40) & 0x000000000000ff00ULL) |
321*01826a49SYabin Cui ((in >> 56) & 0x00000000000000ffULL);
322*01826a49SYabin Cui #endif
323*01826a49SYabin Cui }
324*01826a49SYabin Cui
325*01826a49SYabin Cui
326*01826a49SYabin Cui /*=== Little endian r/w ===*/
327*01826a49SYabin Cui
MEM_readLE16(const void * memPtr)328*01826a49SYabin Cui MEM_STATIC U16 MEM_readLE16(const void* memPtr)
329*01826a49SYabin Cui {
330*01826a49SYabin Cui if (MEM_isLittleEndian())
331*01826a49SYabin Cui return MEM_read16(memPtr);
332*01826a49SYabin Cui else {
333*01826a49SYabin Cui const BYTE* p = (const BYTE*)memPtr;
334*01826a49SYabin Cui return (U16)(p[0] + (p[1]<<8));
335*01826a49SYabin Cui }
336*01826a49SYabin Cui }
337*01826a49SYabin Cui
MEM_writeLE16(void * memPtr,U16 val)338*01826a49SYabin Cui MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
339*01826a49SYabin Cui {
340*01826a49SYabin Cui if (MEM_isLittleEndian()) {
341*01826a49SYabin Cui MEM_write16(memPtr, val);
342*01826a49SYabin Cui } else {
343*01826a49SYabin Cui BYTE* p = (BYTE*)memPtr;
344*01826a49SYabin Cui p[0] = (BYTE)val;
345*01826a49SYabin Cui p[1] = (BYTE)(val>>8);
346*01826a49SYabin Cui }
347*01826a49SYabin Cui }
348*01826a49SYabin Cui
MEM_readLE32(const void * memPtr)349*01826a49SYabin Cui MEM_STATIC U32 MEM_readLE32(const void* memPtr)
350*01826a49SYabin Cui {
351*01826a49SYabin Cui if (MEM_isLittleEndian())
352*01826a49SYabin Cui return MEM_read32(memPtr);
353*01826a49SYabin Cui else
354*01826a49SYabin Cui return MEM_swap32(MEM_read32(memPtr));
355*01826a49SYabin Cui }
356*01826a49SYabin Cui
357*01826a49SYabin Cui
MEM_readLE64(const void * memPtr)358*01826a49SYabin Cui MEM_STATIC U64 MEM_readLE64(const void* memPtr)
359*01826a49SYabin Cui {
360*01826a49SYabin Cui if (MEM_isLittleEndian())
361*01826a49SYabin Cui return MEM_read64(memPtr);
362*01826a49SYabin Cui else
363*01826a49SYabin Cui return MEM_swap64(MEM_read64(memPtr));
364*01826a49SYabin Cui }
365*01826a49SYabin Cui
MEM_readLEST(const void * memPtr)366*01826a49SYabin Cui MEM_STATIC size_t MEM_readLEST(const void* memPtr)
367*01826a49SYabin Cui {
368*01826a49SYabin Cui if (MEM_32bits())
369*01826a49SYabin Cui return (size_t)MEM_readLE32(memPtr);
370*01826a49SYabin Cui else
371*01826a49SYabin Cui return (size_t)MEM_readLE64(memPtr);
372*01826a49SYabin Cui }
373*01826a49SYabin Cui
374*01826a49SYabin Cui
375*01826a49SYabin Cui
376*01826a49SYabin Cui #if defined (__cplusplus)
377*01826a49SYabin Cui }
378*01826a49SYabin Cui #endif
379*01826a49SYabin Cui
380*01826a49SYabin Cui #endif /* MEM_H_MODULE */
381*01826a49SYabin Cui /* ******************************************************************
382*01826a49SYabin Cui bitstream
383*01826a49SYabin Cui Part of FSE library
384*01826a49SYabin Cui header file (to include)
385*01826a49SYabin Cui Copyright (C) 2013-2016, Yann Collet.
386*01826a49SYabin Cui
387*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
388*01826a49SYabin Cui
389*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
390*01826a49SYabin Cui modification, are permitted provided that the following conditions are
391*01826a49SYabin Cui met:
392*01826a49SYabin Cui
393*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
394*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
395*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
396*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
397*01826a49SYabin Cui in the documentation and/or other materials provided with the
398*01826a49SYabin Cui distribution.
399*01826a49SYabin Cui
400*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
401*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
402*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
403*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
404*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
405*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
406*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
407*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
408*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
409*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
410*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
411*01826a49SYabin Cui
412*01826a49SYabin Cui You can contact the author at :
413*01826a49SYabin Cui - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
414*01826a49SYabin Cui ****************************************************************** */
415*01826a49SYabin Cui #ifndef BITSTREAM_H_MODULE
416*01826a49SYabin Cui #define BITSTREAM_H_MODULE
417*01826a49SYabin Cui
418*01826a49SYabin Cui #if defined (__cplusplus)
419*01826a49SYabin Cui extern "C" {
420*01826a49SYabin Cui #endif
421*01826a49SYabin Cui
422*01826a49SYabin Cui
423*01826a49SYabin Cui /*
424*01826a49SYabin Cui * This API consists of small unitary functions, which must be inlined for best performance.
425*01826a49SYabin Cui * Since link-time-optimization is not available for all compilers,
426*01826a49SYabin Cui * these functions are defined into a .h to be included.
427*01826a49SYabin Cui */
428*01826a49SYabin Cui
429*01826a49SYabin Cui
430*01826a49SYabin Cui /*=========================================
431*01826a49SYabin Cui * Target specific
432*01826a49SYabin Cui =========================================*/
433*01826a49SYabin Cui #if defined(__BMI__) && defined(__GNUC__)
434*01826a49SYabin Cui # include <immintrin.h> /* support for bextr (experimental) */
435*01826a49SYabin Cui #endif
436*01826a49SYabin Cui
437*01826a49SYabin Cui /*-********************************************
438*01826a49SYabin Cui * bitStream decoding API (read backward)
439*01826a49SYabin Cui **********************************************/
440*01826a49SYabin Cui typedef struct
441*01826a49SYabin Cui {
442*01826a49SYabin Cui size_t bitContainer;
443*01826a49SYabin Cui unsigned bitsConsumed;
444*01826a49SYabin Cui const char* ptr;
445*01826a49SYabin Cui const char* start;
446*01826a49SYabin Cui } BITv07_DStream_t;
447*01826a49SYabin Cui
448*01826a49SYabin Cui typedef enum { BITv07_DStream_unfinished = 0,
449*01826a49SYabin Cui BITv07_DStream_endOfBuffer = 1,
450*01826a49SYabin Cui BITv07_DStream_completed = 2,
451*01826a49SYabin Cui BITv07_DStream_overflow = 3 } BITv07_DStream_status; /* result of BITv07_reloadDStream() */
452*01826a49SYabin Cui /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
453*01826a49SYabin Cui
454*01826a49SYabin Cui MEM_STATIC size_t BITv07_initDStream(BITv07_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
455*01826a49SYabin Cui MEM_STATIC size_t BITv07_readBits(BITv07_DStream_t* bitD, unsigned nbBits);
456*01826a49SYabin Cui MEM_STATIC BITv07_DStream_status BITv07_reloadDStream(BITv07_DStream_t* bitD);
457*01826a49SYabin Cui MEM_STATIC unsigned BITv07_endOfDStream(const BITv07_DStream_t* bitD);
458*01826a49SYabin Cui
459*01826a49SYabin Cui
460*01826a49SYabin Cui
461*01826a49SYabin Cui /*-****************************************
462*01826a49SYabin Cui * unsafe API
463*01826a49SYabin Cui ******************************************/
464*01826a49SYabin Cui MEM_STATIC size_t BITv07_readBitsFast(BITv07_DStream_t* bitD, unsigned nbBits);
465*01826a49SYabin Cui /* faster, but works only if nbBits >= 1 */
466*01826a49SYabin Cui
467*01826a49SYabin Cui
468*01826a49SYabin Cui
469*01826a49SYabin Cui /*-**************************************************************
470*01826a49SYabin Cui * Internal functions
471*01826a49SYabin Cui ****************************************************************/
BITv07_highbit32(U32 val)472*01826a49SYabin Cui MEM_STATIC unsigned BITv07_highbit32 (U32 val)
473*01826a49SYabin Cui {
474*01826a49SYabin Cui # if defined(_MSC_VER) /* Visual */
475*01826a49SYabin Cui unsigned long r;
476*01826a49SYabin Cui return _BitScanReverse(&r, val) ? (unsigned)r : 0;
477*01826a49SYabin Cui # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
478*01826a49SYabin Cui return __builtin_clz (val) ^ 31;
479*01826a49SYabin Cui # else /* Software version */
480*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 };
481*01826a49SYabin Cui U32 v = val;
482*01826a49SYabin Cui v |= v >> 1;
483*01826a49SYabin Cui v |= v >> 2;
484*01826a49SYabin Cui v |= v >> 4;
485*01826a49SYabin Cui v |= v >> 8;
486*01826a49SYabin Cui v |= v >> 16;
487*01826a49SYabin Cui return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
488*01826a49SYabin Cui # endif
489*01826a49SYabin Cui }
490*01826a49SYabin Cui
491*01826a49SYabin Cui
492*01826a49SYabin Cui
493*01826a49SYabin Cui /*-********************************************************
494*01826a49SYabin Cui * bitStream decoding
495*01826a49SYabin Cui **********************************************************/
496*01826a49SYabin Cui /*! BITv07_initDStream() :
497*01826a49SYabin Cui * Initialize a BITv07_DStream_t.
498*01826a49SYabin Cui * `bitD` : a pointer to an already allocated BITv07_DStream_t structure.
499*01826a49SYabin Cui * `srcSize` must be the *exact* size of the bitStream, in bytes.
500*01826a49SYabin Cui * @return : size of stream (== srcSize) or an errorCode if a problem is detected
501*01826a49SYabin Cui */
BITv07_initDStream(BITv07_DStream_t * bitD,const void * srcBuffer,size_t srcSize)502*01826a49SYabin Cui MEM_STATIC size_t BITv07_initDStream(BITv07_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
503*01826a49SYabin Cui {
504*01826a49SYabin Cui if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
505*01826a49SYabin Cui
506*01826a49SYabin Cui if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */
507*01826a49SYabin Cui bitD->start = (const char*)srcBuffer;
508*01826a49SYabin Cui bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer);
509*01826a49SYabin Cui bitD->bitContainer = MEM_readLEST(bitD->ptr);
510*01826a49SYabin Cui { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
511*01826a49SYabin Cui bitD->bitsConsumed = lastByte ? 8 - BITv07_highbit32(lastByte) : 0;
512*01826a49SYabin Cui if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
513*01826a49SYabin Cui } else {
514*01826a49SYabin Cui bitD->start = (const char*)srcBuffer;
515*01826a49SYabin Cui bitD->ptr = bitD->start;
516*01826a49SYabin Cui bitD->bitContainer = *(const BYTE*)(bitD->start);
517*01826a49SYabin Cui switch(srcSize)
518*01826a49SYabin Cui {
519*01826a49SYabin Cui case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);/* fall-through */
520*01826a49SYabin Cui case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);/* fall-through */
521*01826a49SYabin Cui case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);/* fall-through */
522*01826a49SYabin Cui case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24; /* fall-through */
523*01826a49SYabin Cui case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16; /* fall-through */
524*01826a49SYabin Cui case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8; /* fall-through */
525*01826a49SYabin Cui default: break;
526*01826a49SYabin Cui }
527*01826a49SYabin Cui { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
528*01826a49SYabin Cui bitD->bitsConsumed = lastByte ? 8 - BITv07_highbit32(lastByte) : 0;
529*01826a49SYabin Cui if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
530*01826a49SYabin Cui bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8;
531*01826a49SYabin Cui }
532*01826a49SYabin Cui
533*01826a49SYabin Cui return srcSize;
534*01826a49SYabin Cui }
535*01826a49SYabin Cui
536*01826a49SYabin Cui
BITv07_lookBits(const BITv07_DStream_t * bitD,U32 nbBits)537*01826a49SYabin Cui MEM_STATIC size_t BITv07_lookBits(const BITv07_DStream_t* bitD, U32 nbBits)
538*01826a49SYabin Cui {
539*01826a49SYabin Cui U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
540*01826a49SYabin Cui return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
541*01826a49SYabin Cui }
542*01826a49SYabin Cui
543*01826a49SYabin Cui /*! BITv07_lookBitsFast() :
544*01826a49SYabin Cui * unsafe version; only works if nbBits >= 1 */
BITv07_lookBitsFast(const BITv07_DStream_t * bitD,U32 nbBits)545*01826a49SYabin Cui MEM_STATIC size_t BITv07_lookBitsFast(const BITv07_DStream_t* bitD, U32 nbBits)
546*01826a49SYabin Cui {
547*01826a49SYabin Cui U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
548*01826a49SYabin Cui return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);
549*01826a49SYabin Cui }
550*01826a49SYabin Cui
BITv07_skipBits(BITv07_DStream_t * bitD,U32 nbBits)551*01826a49SYabin Cui MEM_STATIC void BITv07_skipBits(BITv07_DStream_t* bitD, U32 nbBits)
552*01826a49SYabin Cui {
553*01826a49SYabin Cui bitD->bitsConsumed += nbBits;
554*01826a49SYabin Cui }
555*01826a49SYabin Cui
BITv07_readBits(BITv07_DStream_t * bitD,U32 nbBits)556*01826a49SYabin Cui MEM_STATIC size_t BITv07_readBits(BITv07_DStream_t* bitD, U32 nbBits)
557*01826a49SYabin Cui {
558*01826a49SYabin Cui size_t const value = BITv07_lookBits(bitD, nbBits);
559*01826a49SYabin Cui BITv07_skipBits(bitD, nbBits);
560*01826a49SYabin Cui return value;
561*01826a49SYabin Cui }
562*01826a49SYabin Cui
563*01826a49SYabin Cui /*! BITv07_readBitsFast() :
564*01826a49SYabin Cui * unsafe version; only works if nbBits >= 1 */
BITv07_readBitsFast(BITv07_DStream_t * bitD,U32 nbBits)565*01826a49SYabin Cui MEM_STATIC size_t BITv07_readBitsFast(BITv07_DStream_t* bitD, U32 nbBits)
566*01826a49SYabin Cui {
567*01826a49SYabin Cui size_t const value = BITv07_lookBitsFast(bitD, nbBits);
568*01826a49SYabin Cui BITv07_skipBits(bitD, nbBits);
569*01826a49SYabin Cui return value;
570*01826a49SYabin Cui }
571*01826a49SYabin Cui
BITv07_reloadDStream(BITv07_DStream_t * bitD)572*01826a49SYabin Cui MEM_STATIC BITv07_DStream_status BITv07_reloadDStream(BITv07_DStream_t* bitD)
573*01826a49SYabin Cui {
574*01826a49SYabin Cui if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should not happen => corruption detected */
575*01826a49SYabin Cui return BITv07_DStream_overflow;
576*01826a49SYabin Cui
577*01826a49SYabin Cui if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) {
578*01826a49SYabin Cui bitD->ptr -= bitD->bitsConsumed >> 3;
579*01826a49SYabin Cui bitD->bitsConsumed &= 7;
580*01826a49SYabin Cui bitD->bitContainer = MEM_readLEST(bitD->ptr);
581*01826a49SYabin Cui return BITv07_DStream_unfinished;
582*01826a49SYabin Cui }
583*01826a49SYabin Cui if (bitD->ptr == bitD->start) {
584*01826a49SYabin Cui if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BITv07_DStream_endOfBuffer;
585*01826a49SYabin Cui return BITv07_DStream_completed;
586*01826a49SYabin Cui }
587*01826a49SYabin Cui { U32 nbBytes = bitD->bitsConsumed >> 3;
588*01826a49SYabin Cui BITv07_DStream_status result = BITv07_DStream_unfinished;
589*01826a49SYabin Cui if (bitD->ptr - nbBytes < bitD->start) {
590*01826a49SYabin Cui nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
591*01826a49SYabin Cui result = BITv07_DStream_endOfBuffer;
592*01826a49SYabin Cui }
593*01826a49SYabin Cui bitD->ptr -= nbBytes;
594*01826a49SYabin Cui bitD->bitsConsumed -= nbBytes*8;
595*01826a49SYabin Cui bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */
596*01826a49SYabin Cui return result;
597*01826a49SYabin Cui }
598*01826a49SYabin Cui }
599*01826a49SYabin Cui
600*01826a49SYabin Cui /*! BITv07_endOfDStream() :
601*01826a49SYabin Cui * @return Tells if DStream has exactly reached its end (all bits consumed).
602*01826a49SYabin Cui */
BITv07_endOfDStream(const BITv07_DStream_t * DStream)603*01826a49SYabin Cui MEM_STATIC unsigned BITv07_endOfDStream(const BITv07_DStream_t* DStream)
604*01826a49SYabin Cui {
605*01826a49SYabin Cui return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8));
606*01826a49SYabin Cui }
607*01826a49SYabin Cui
608*01826a49SYabin Cui #if defined (__cplusplus)
609*01826a49SYabin Cui }
610*01826a49SYabin Cui #endif
611*01826a49SYabin Cui
612*01826a49SYabin Cui #endif /* BITSTREAM_H_MODULE */
613*01826a49SYabin Cui /* ******************************************************************
614*01826a49SYabin Cui FSE : Finite State Entropy codec
615*01826a49SYabin Cui Public Prototypes declaration
616*01826a49SYabin Cui Copyright (C) 2013-2016, Yann Collet.
617*01826a49SYabin Cui
618*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
619*01826a49SYabin Cui
620*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
621*01826a49SYabin Cui modification, are permitted provided that the following conditions are
622*01826a49SYabin Cui met:
623*01826a49SYabin Cui
624*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
625*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
626*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
627*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
628*01826a49SYabin Cui in the documentation and/or other materials provided with the
629*01826a49SYabin Cui distribution.
630*01826a49SYabin Cui
631*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
632*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
633*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
634*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
635*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
636*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
637*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
638*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
639*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
640*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
641*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
642*01826a49SYabin Cui
643*01826a49SYabin Cui You can contact the author at :
644*01826a49SYabin Cui - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
645*01826a49SYabin Cui ****************************************************************** */
646*01826a49SYabin Cui #ifndef FSEv07_H
647*01826a49SYabin Cui #define FSEv07_H
648*01826a49SYabin Cui
649*01826a49SYabin Cui #if defined (__cplusplus)
650*01826a49SYabin Cui extern "C" {
651*01826a49SYabin Cui #endif
652*01826a49SYabin Cui
653*01826a49SYabin Cui
654*01826a49SYabin Cui
655*01826a49SYabin Cui /*-****************************************
656*01826a49SYabin Cui * FSE simple functions
657*01826a49SYabin Cui ******************************************/
658*01826a49SYabin Cui
659*01826a49SYabin Cui /*! FSEv07_decompress():
660*01826a49SYabin Cui Decompress FSE data from buffer 'cSrc', of size 'cSrcSize',
661*01826a49SYabin Cui into already allocated destination buffer 'dst', of size 'dstCapacity'.
662*01826a49SYabin Cui @return : size of regenerated data (<= maxDstSize),
663*01826a49SYabin Cui or an error code, which can be tested using FSEv07_isError() .
664*01826a49SYabin Cui
665*01826a49SYabin Cui ** Important ** : FSEv07_decompress() does not decompress non-compressible nor RLE data !!!
666*01826a49SYabin Cui Why ? : making this distinction requires a header.
667*01826a49SYabin Cui Header management is intentionally delegated to the user layer, which can better manage special cases.
668*01826a49SYabin Cui */
669*01826a49SYabin Cui size_t FSEv07_decompress(void* dst, size_t dstCapacity,
670*01826a49SYabin Cui const void* cSrc, size_t cSrcSize);
671*01826a49SYabin Cui
672*01826a49SYabin Cui
673*01826a49SYabin Cui /* Error Management */
674*01826a49SYabin Cui unsigned FSEv07_isError(size_t code); /* tells if a return value is an error code */
675*01826a49SYabin Cui const char* FSEv07_getErrorName(size_t code); /* provides error code string (useful for debugging) */
676*01826a49SYabin Cui
677*01826a49SYabin Cui
678*01826a49SYabin Cui /*-*****************************************
679*01826a49SYabin Cui * FSE detailed API
680*01826a49SYabin Cui ******************************************/
681*01826a49SYabin Cui /*!
682*01826a49SYabin Cui FSEv07_decompress() does the following:
683*01826a49SYabin Cui 1. read normalized counters with readNCount()
684*01826a49SYabin Cui 2. build decoding table 'DTable' from normalized counters
685*01826a49SYabin Cui 3. decode the data stream using decoding table 'DTable'
686*01826a49SYabin Cui
687*01826a49SYabin Cui The following API allows targeting specific sub-functions for advanced tasks.
688*01826a49SYabin Cui For example, it's possible to compress several blocks using the same 'CTable',
689*01826a49SYabin Cui or to save and provide normalized distribution using external method.
690*01826a49SYabin Cui */
691*01826a49SYabin Cui
692*01826a49SYabin Cui
693*01826a49SYabin Cui /* *** DECOMPRESSION *** */
694*01826a49SYabin Cui
695*01826a49SYabin Cui /*! FSEv07_readNCount():
696*01826a49SYabin Cui Read compactly saved 'normalizedCounter' from 'rBuffer'.
697*01826a49SYabin Cui @return : size read from 'rBuffer',
698*01826a49SYabin Cui or an errorCode, which can be tested using FSEv07_isError().
699*01826a49SYabin Cui maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */
700*01826a49SYabin Cui size_t FSEv07_readNCount (short* normalizedCounter, unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, const void* rBuffer, size_t rBuffSize);
701*01826a49SYabin Cui
702*01826a49SYabin Cui /*! Constructor and Destructor of FSEv07_DTable.
703*01826a49SYabin Cui Note that its size depends on 'tableLog' */
704*01826a49SYabin Cui typedef unsigned FSEv07_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */
705*01826a49SYabin Cui FSEv07_DTable* FSEv07_createDTable(unsigned tableLog);
706*01826a49SYabin Cui void FSEv07_freeDTable(FSEv07_DTable* dt);
707*01826a49SYabin Cui
708*01826a49SYabin Cui /*! FSEv07_buildDTable():
709*01826a49SYabin Cui Builds 'dt', which must be already allocated, using FSEv07_createDTable().
710*01826a49SYabin Cui return : 0, or an errorCode, which can be tested using FSEv07_isError() */
711*01826a49SYabin Cui size_t FSEv07_buildDTable (FSEv07_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
712*01826a49SYabin Cui
713*01826a49SYabin Cui /*! FSEv07_decompress_usingDTable():
714*01826a49SYabin Cui Decompress compressed source `cSrc` of size `cSrcSize` using `dt`
715*01826a49SYabin Cui into `dst` which must be already allocated.
716*01826a49SYabin Cui @return : size of regenerated data (necessarily <= `dstCapacity`),
717*01826a49SYabin Cui or an errorCode, which can be tested using FSEv07_isError() */
718*01826a49SYabin Cui size_t FSEv07_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSEv07_DTable* dt);
719*01826a49SYabin Cui
720*01826a49SYabin Cui /*!
721*01826a49SYabin Cui Tutorial :
722*01826a49SYabin Cui ----------
723*01826a49SYabin Cui (Note : these functions only decompress FSE-compressed blocks.
724*01826a49SYabin Cui If block is uncompressed, use memcpy() instead
725*01826a49SYabin Cui If block is a single repeated byte, use memset() instead )
726*01826a49SYabin Cui
727*01826a49SYabin Cui The first step is to obtain the normalized frequencies of symbols.
728*01826a49SYabin Cui This can be performed by FSEv07_readNCount() if it was saved using FSEv07_writeNCount().
729*01826a49SYabin Cui 'normalizedCounter' must be already allocated, and have at least 'maxSymbolValuePtr[0]+1' cells of signed short.
730*01826a49SYabin Cui In practice, that means it's necessary to know 'maxSymbolValue' beforehand,
731*01826a49SYabin Cui or size the table to handle worst case situations (typically 256).
732*01826a49SYabin Cui FSEv07_readNCount() will provide 'tableLog' and 'maxSymbolValue'.
733*01826a49SYabin Cui The result of FSEv07_readNCount() is the number of bytes read from 'rBuffer'.
734*01826a49SYabin Cui Note that 'rBufferSize' must be at least 4 bytes, even if useful information is less than that.
735*01826a49SYabin Cui If there is an error, the function will return an error code, which can be tested using FSEv07_isError().
736*01826a49SYabin Cui
737*01826a49SYabin Cui The next step is to build the decompression tables 'FSEv07_DTable' from 'normalizedCounter'.
738*01826a49SYabin Cui This is performed by the function FSEv07_buildDTable().
739*01826a49SYabin Cui The space required by 'FSEv07_DTable' must be already allocated using FSEv07_createDTable().
740*01826a49SYabin Cui If there is an error, the function will return an error code, which can be tested using FSEv07_isError().
741*01826a49SYabin Cui
742*01826a49SYabin Cui `FSEv07_DTable` can then be used to decompress `cSrc`, with FSEv07_decompress_usingDTable().
743*01826a49SYabin Cui `cSrcSize` must be strictly correct, otherwise decompression will fail.
744*01826a49SYabin Cui FSEv07_decompress_usingDTable() result will tell how many bytes were regenerated (<=`dstCapacity`).
745*01826a49SYabin Cui If there is an error, the function will return an error code, which can be tested using FSEv07_isError(). (ex: dst buffer too small)
746*01826a49SYabin Cui */
747*01826a49SYabin Cui
748*01826a49SYabin Cui
749*01826a49SYabin Cui #ifdef FSEv07_STATIC_LINKING_ONLY
750*01826a49SYabin Cui
751*01826a49SYabin Cui
752*01826a49SYabin Cui /* *****************************************
753*01826a49SYabin Cui * Static allocation
754*01826a49SYabin Cui *******************************************/
755*01826a49SYabin Cui /* FSE buffer bounds */
756*01826a49SYabin Cui #define FSEv07_NCOUNTBOUND 512
757*01826a49SYabin Cui #define FSEv07_BLOCKBOUND(size) (size + (size>>7))
758*01826a49SYabin Cui
759*01826a49SYabin Cui /* It is possible to statically allocate FSE CTable/DTable as a table of unsigned using below macros */
760*01826a49SYabin Cui #define FSEv07_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog))
761*01826a49SYabin Cui
762*01826a49SYabin Cui
763*01826a49SYabin Cui /* *****************************************
764*01826a49SYabin Cui * FSE advanced API
765*01826a49SYabin Cui *******************************************/
766*01826a49SYabin Cui size_t FSEv07_countFast(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize);
767*01826a49SYabin Cui /**< same as FSEv07_count(), but blindly trusts that all byte values within src are <= *maxSymbolValuePtr */
768*01826a49SYabin Cui
769*01826a49SYabin Cui unsigned FSEv07_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus);
770*01826a49SYabin Cui /**< same as FSEv07_optimalTableLog(), which used `minus==2` */
771*01826a49SYabin Cui
772*01826a49SYabin Cui size_t FSEv07_buildDTable_raw (FSEv07_DTable* dt, unsigned nbBits);
773*01826a49SYabin Cui /**< build a fake FSEv07_DTable, designed to read an uncompressed bitstream where each symbol uses nbBits */
774*01826a49SYabin Cui
775*01826a49SYabin Cui size_t FSEv07_buildDTable_rle (FSEv07_DTable* dt, unsigned char symbolValue);
776*01826a49SYabin Cui /**< build a fake FSEv07_DTable, designed to always generate the same symbolValue */
777*01826a49SYabin Cui
778*01826a49SYabin Cui
779*01826a49SYabin Cui
780*01826a49SYabin Cui /* *****************************************
781*01826a49SYabin Cui * FSE symbol decompression API
782*01826a49SYabin Cui *******************************************/
783*01826a49SYabin Cui typedef struct
784*01826a49SYabin Cui {
785*01826a49SYabin Cui size_t state;
786*01826a49SYabin Cui const void* table; /* precise table may vary, depending on U16 */
787*01826a49SYabin Cui } FSEv07_DState_t;
788*01826a49SYabin Cui
789*01826a49SYabin Cui
790*01826a49SYabin Cui static void FSEv07_initDState(FSEv07_DState_t* DStatePtr, BITv07_DStream_t* bitD, const FSEv07_DTable* dt);
791*01826a49SYabin Cui
792*01826a49SYabin Cui static unsigned char FSEv07_decodeSymbol(FSEv07_DState_t* DStatePtr, BITv07_DStream_t* bitD);
793*01826a49SYabin Cui
794*01826a49SYabin Cui
795*01826a49SYabin Cui
796*01826a49SYabin Cui /* *****************************************
797*01826a49SYabin Cui * FSE unsafe API
798*01826a49SYabin Cui *******************************************/
799*01826a49SYabin Cui static unsigned char FSEv07_decodeSymbolFast(FSEv07_DState_t* DStatePtr, BITv07_DStream_t* bitD);
800*01826a49SYabin Cui /* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */
801*01826a49SYabin Cui
802*01826a49SYabin Cui
803*01826a49SYabin Cui /* ====== Decompression ====== */
804*01826a49SYabin Cui
805*01826a49SYabin Cui typedef struct {
806*01826a49SYabin Cui U16 tableLog;
807*01826a49SYabin Cui U16 fastMode;
808*01826a49SYabin Cui } FSEv07_DTableHeader; /* sizeof U32 */
809*01826a49SYabin Cui
810*01826a49SYabin Cui typedef struct
811*01826a49SYabin Cui {
812*01826a49SYabin Cui unsigned short newState;
813*01826a49SYabin Cui unsigned char symbol;
814*01826a49SYabin Cui unsigned char nbBits;
815*01826a49SYabin Cui } FSEv07_decode_t; /* size == U32 */
816*01826a49SYabin Cui
FSEv07_initDState(FSEv07_DState_t * DStatePtr,BITv07_DStream_t * bitD,const FSEv07_DTable * dt)817*01826a49SYabin Cui MEM_STATIC void FSEv07_initDState(FSEv07_DState_t* DStatePtr, BITv07_DStream_t* bitD, const FSEv07_DTable* dt)
818*01826a49SYabin Cui {
819*01826a49SYabin Cui const void* ptr = dt;
820*01826a49SYabin Cui const FSEv07_DTableHeader* const DTableH = (const FSEv07_DTableHeader*)ptr;
821*01826a49SYabin Cui DStatePtr->state = BITv07_readBits(bitD, DTableH->tableLog);
822*01826a49SYabin Cui BITv07_reloadDStream(bitD);
823*01826a49SYabin Cui DStatePtr->table = dt + 1;
824*01826a49SYabin Cui }
825*01826a49SYabin Cui
FSEv07_peekSymbol(const FSEv07_DState_t * DStatePtr)826*01826a49SYabin Cui MEM_STATIC BYTE FSEv07_peekSymbol(const FSEv07_DState_t* DStatePtr)
827*01826a49SYabin Cui {
828*01826a49SYabin Cui FSEv07_decode_t const DInfo = ((const FSEv07_decode_t*)(DStatePtr->table))[DStatePtr->state];
829*01826a49SYabin Cui return DInfo.symbol;
830*01826a49SYabin Cui }
831*01826a49SYabin Cui
FSEv07_updateState(FSEv07_DState_t * DStatePtr,BITv07_DStream_t * bitD)832*01826a49SYabin Cui MEM_STATIC void FSEv07_updateState(FSEv07_DState_t* DStatePtr, BITv07_DStream_t* bitD)
833*01826a49SYabin Cui {
834*01826a49SYabin Cui FSEv07_decode_t const DInfo = ((const FSEv07_decode_t*)(DStatePtr->table))[DStatePtr->state];
835*01826a49SYabin Cui U32 const nbBits = DInfo.nbBits;
836*01826a49SYabin Cui size_t const lowBits = BITv07_readBits(bitD, nbBits);
837*01826a49SYabin Cui DStatePtr->state = DInfo.newState + lowBits;
838*01826a49SYabin Cui }
839*01826a49SYabin Cui
FSEv07_decodeSymbol(FSEv07_DState_t * DStatePtr,BITv07_DStream_t * bitD)840*01826a49SYabin Cui MEM_STATIC BYTE FSEv07_decodeSymbol(FSEv07_DState_t* DStatePtr, BITv07_DStream_t* bitD)
841*01826a49SYabin Cui {
842*01826a49SYabin Cui FSEv07_decode_t const DInfo = ((const FSEv07_decode_t*)(DStatePtr->table))[DStatePtr->state];
843*01826a49SYabin Cui U32 const nbBits = DInfo.nbBits;
844*01826a49SYabin Cui BYTE const symbol = DInfo.symbol;
845*01826a49SYabin Cui size_t const lowBits = BITv07_readBits(bitD, nbBits);
846*01826a49SYabin Cui
847*01826a49SYabin Cui DStatePtr->state = DInfo.newState + lowBits;
848*01826a49SYabin Cui return symbol;
849*01826a49SYabin Cui }
850*01826a49SYabin Cui
851*01826a49SYabin Cui /*! FSEv07_decodeSymbolFast() :
852*01826a49SYabin Cui unsafe, only works if no symbol has a probability > 50% */
FSEv07_decodeSymbolFast(FSEv07_DState_t * DStatePtr,BITv07_DStream_t * bitD)853*01826a49SYabin Cui MEM_STATIC BYTE FSEv07_decodeSymbolFast(FSEv07_DState_t* DStatePtr, BITv07_DStream_t* bitD)
854*01826a49SYabin Cui {
855*01826a49SYabin Cui FSEv07_decode_t const DInfo = ((const FSEv07_decode_t*)(DStatePtr->table))[DStatePtr->state];
856*01826a49SYabin Cui U32 const nbBits = DInfo.nbBits;
857*01826a49SYabin Cui BYTE const symbol = DInfo.symbol;
858*01826a49SYabin Cui size_t const lowBits = BITv07_readBitsFast(bitD, nbBits);
859*01826a49SYabin Cui
860*01826a49SYabin Cui DStatePtr->state = DInfo.newState + lowBits;
861*01826a49SYabin Cui return symbol;
862*01826a49SYabin Cui }
863*01826a49SYabin Cui
864*01826a49SYabin Cui
865*01826a49SYabin Cui
866*01826a49SYabin Cui #ifndef FSEv07_COMMONDEFS_ONLY
867*01826a49SYabin Cui
868*01826a49SYabin Cui /* **************************************************************
869*01826a49SYabin Cui * Tuning parameters
870*01826a49SYabin Cui ****************************************************************/
871*01826a49SYabin Cui /*!MEMORY_USAGE :
872*01826a49SYabin Cui * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
873*01826a49SYabin Cui * Increasing memory usage improves compression ratio
874*01826a49SYabin Cui * Reduced memory usage can improve speed, due to cache effect
875*01826a49SYabin Cui * Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */
876*01826a49SYabin Cui #define FSEv07_MAX_MEMORY_USAGE 14
877*01826a49SYabin Cui #define FSEv07_DEFAULT_MEMORY_USAGE 13
878*01826a49SYabin Cui
879*01826a49SYabin Cui /*!FSEv07_MAX_SYMBOL_VALUE :
880*01826a49SYabin Cui * Maximum symbol value authorized.
881*01826a49SYabin Cui * Required for proper stack allocation */
882*01826a49SYabin Cui #define FSEv07_MAX_SYMBOL_VALUE 255
883*01826a49SYabin Cui
884*01826a49SYabin Cui
885*01826a49SYabin Cui /* **************************************************************
886*01826a49SYabin Cui * template functions type & suffix
887*01826a49SYabin Cui ****************************************************************/
888*01826a49SYabin Cui #define FSEv07_FUNCTION_TYPE BYTE
889*01826a49SYabin Cui #define FSEv07_FUNCTION_EXTENSION
890*01826a49SYabin Cui #define FSEv07_DECODE_TYPE FSEv07_decode_t
891*01826a49SYabin Cui
892*01826a49SYabin Cui
893*01826a49SYabin Cui #endif /* !FSEv07_COMMONDEFS_ONLY */
894*01826a49SYabin Cui
895*01826a49SYabin Cui
896*01826a49SYabin Cui /* ***************************************************************
897*01826a49SYabin Cui * Constants
898*01826a49SYabin Cui *****************************************************************/
899*01826a49SYabin Cui #define FSEv07_MAX_TABLELOG (FSEv07_MAX_MEMORY_USAGE-2)
900*01826a49SYabin Cui #define FSEv07_MAX_TABLESIZE (1U<<FSEv07_MAX_TABLELOG)
901*01826a49SYabin Cui #define FSEv07_MAXTABLESIZE_MASK (FSEv07_MAX_TABLESIZE-1)
902*01826a49SYabin Cui #define FSEv07_DEFAULT_TABLELOG (FSEv07_DEFAULT_MEMORY_USAGE-2)
903*01826a49SYabin Cui #define FSEv07_MIN_TABLELOG 5
904*01826a49SYabin Cui
905*01826a49SYabin Cui #define FSEv07_TABLELOG_ABSOLUTE_MAX 15
906*01826a49SYabin Cui #if FSEv07_MAX_TABLELOG > FSEv07_TABLELOG_ABSOLUTE_MAX
907*01826a49SYabin Cui # error "FSEv07_MAX_TABLELOG > FSEv07_TABLELOG_ABSOLUTE_MAX is not supported"
908*01826a49SYabin Cui #endif
909*01826a49SYabin Cui
910*01826a49SYabin Cui #define FSEv07_TABLESTEP(tableSize) ((tableSize>>1) + (tableSize>>3) + 3)
911*01826a49SYabin Cui
912*01826a49SYabin Cui
913*01826a49SYabin Cui #endif /* FSEv07_STATIC_LINKING_ONLY */
914*01826a49SYabin Cui
915*01826a49SYabin Cui
916*01826a49SYabin Cui #if defined (__cplusplus)
917*01826a49SYabin Cui }
918*01826a49SYabin Cui #endif
919*01826a49SYabin Cui
920*01826a49SYabin Cui #endif /* FSEv07_H */
921*01826a49SYabin Cui /* ******************************************************************
922*01826a49SYabin Cui Huffman coder, part of New Generation Entropy library
923*01826a49SYabin Cui header file
924*01826a49SYabin Cui Copyright (C) 2013-2016, Yann Collet.
925*01826a49SYabin Cui
926*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
927*01826a49SYabin Cui
928*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
929*01826a49SYabin Cui modification, are permitted provided that the following conditions are
930*01826a49SYabin Cui met:
931*01826a49SYabin Cui
932*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
933*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
934*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
935*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
936*01826a49SYabin Cui in the documentation and/or other materials provided with the
937*01826a49SYabin Cui distribution.
938*01826a49SYabin Cui
939*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
940*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
941*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
942*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
943*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
944*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
945*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
946*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
947*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
948*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
949*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
950*01826a49SYabin Cui
951*01826a49SYabin Cui You can contact the author at :
952*01826a49SYabin Cui - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
953*01826a49SYabin Cui ****************************************************************** */
954*01826a49SYabin Cui #ifndef HUFv07_H_298734234
955*01826a49SYabin Cui #define HUFv07_H_298734234
956*01826a49SYabin Cui
957*01826a49SYabin Cui #if defined (__cplusplus)
958*01826a49SYabin Cui extern "C" {
959*01826a49SYabin Cui #endif
960*01826a49SYabin Cui
961*01826a49SYabin Cui
962*01826a49SYabin Cui
963*01826a49SYabin Cui /* *** simple functions *** */
964*01826a49SYabin Cui /**
965*01826a49SYabin Cui HUFv07_decompress() :
966*01826a49SYabin Cui Decompress HUF data from buffer 'cSrc', of size 'cSrcSize',
967*01826a49SYabin Cui into already allocated buffer 'dst', of minimum size 'dstSize'.
968*01826a49SYabin Cui `dstSize` : **must** be the ***exact*** size of original (uncompressed) data.
969*01826a49SYabin Cui Note : in contrast with FSE, HUFv07_decompress can regenerate
970*01826a49SYabin Cui RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data,
971*01826a49SYabin Cui because it knows size to regenerate.
972*01826a49SYabin Cui @return : size of regenerated data (== dstSize),
973*01826a49SYabin Cui or an error code, which can be tested using HUFv07_isError()
974*01826a49SYabin Cui */
975*01826a49SYabin Cui size_t HUFv07_decompress(void* dst, size_t dstSize,
976*01826a49SYabin Cui const void* cSrc, size_t cSrcSize);
977*01826a49SYabin Cui
978*01826a49SYabin Cui
979*01826a49SYabin Cui /* ****************************************
980*01826a49SYabin Cui * Tool functions
981*01826a49SYabin Cui ******************************************/
982*01826a49SYabin Cui #define HUFv07_BLOCKSIZE_MAX (128 * 1024)
983*01826a49SYabin Cui
984*01826a49SYabin Cui /* Error Management */
985*01826a49SYabin Cui unsigned HUFv07_isError(size_t code); /**< tells if a return value is an error code */
986*01826a49SYabin Cui const char* HUFv07_getErrorName(size_t code); /**< provides error code string (useful for debugging) */
987*01826a49SYabin Cui
988*01826a49SYabin Cui
989*01826a49SYabin Cui /* *** Advanced function *** */
990*01826a49SYabin Cui
991*01826a49SYabin Cui
992*01826a49SYabin Cui #ifdef HUFv07_STATIC_LINKING_ONLY
993*01826a49SYabin Cui
994*01826a49SYabin Cui
995*01826a49SYabin Cui /* *** Constants *** */
996*01826a49SYabin Cui #define HUFv07_TABLELOG_ABSOLUTEMAX 16 /* absolute limit of HUFv07_MAX_TABLELOG. Beyond that value, code does not work */
997*01826a49SYabin Cui #define HUFv07_TABLELOG_MAX 12 /* max configured tableLog (for static allocation); can be modified up to HUFv07_ABSOLUTEMAX_TABLELOG */
998*01826a49SYabin Cui #define HUFv07_TABLELOG_DEFAULT 11 /* tableLog by default, when not specified */
999*01826a49SYabin Cui #define HUFv07_SYMBOLVALUE_MAX 255
1000*01826a49SYabin Cui #if (HUFv07_TABLELOG_MAX > HUFv07_TABLELOG_ABSOLUTEMAX)
1001*01826a49SYabin Cui # error "HUFv07_TABLELOG_MAX is too large !"
1002*01826a49SYabin Cui #endif
1003*01826a49SYabin Cui
1004*01826a49SYabin Cui
1005*01826a49SYabin Cui /* ****************************************
1006*01826a49SYabin Cui * Static allocation
1007*01826a49SYabin Cui ******************************************/
1008*01826a49SYabin Cui /* HUF buffer bounds */
1009*01826a49SYabin Cui #define HUFv07_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true if incompressible pre-filtered with fast heuristic */
1010*01826a49SYabin Cui
1011*01826a49SYabin Cui /* static allocation of HUF's DTable */
1012*01826a49SYabin Cui typedef U32 HUFv07_DTable;
1013*01826a49SYabin Cui #define HUFv07_DTABLE_SIZE(maxTableLog) (1 + (1<<(maxTableLog)))
1014*01826a49SYabin Cui #define HUFv07_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
1015*01826a49SYabin Cui HUFv07_DTable DTable[HUFv07_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1)*0x1000001) }
1016*01826a49SYabin Cui #define HUFv07_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \
1017*01826a49SYabin Cui HUFv07_DTable DTable[HUFv07_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog)*0x1000001) }
1018*01826a49SYabin Cui
1019*01826a49SYabin Cui
1020*01826a49SYabin Cui /* ****************************************
1021*01826a49SYabin Cui * Advanced decompression functions
1022*01826a49SYabin Cui ******************************************/
1023*01826a49SYabin Cui size_t HUFv07_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
1024*01826a49SYabin Cui size_t HUFv07_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
1025*01826a49SYabin Cui
1026*01826a49SYabin Cui size_t HUFv07_decompress4X_DCtx (HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< decodes RLE and uncompressed */
1027*01826a49SYabin Cui size_t HUFv07_decompress4X_hufOnly(HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< considers RLE and uncompressed as errors */
1028*01826a49SYabin Cui size_t HUFv07_decompress4X2_DCtx(HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
1029*01826a49SYabin Cui size_t HUFv07_decompress4X4_DCtx(HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
1030*01826a49SYabin Cui
1031*01826a49SYabin Cui size_t HUFv07_decompress1X_DCtx (HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
1032*01826a49SYabin Cui size_t HUFv07_decompress1X2_DCtx(HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
1033*01826a49SYabin Cui size_t HUFv07_decompress1X4_DCtx(HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
1034*01826a49SYabin Cui
1035*01826a49SYabin Cui
1036*01826a49SYabin Cui /* ****************************************
1037*01826a49SYabin Cui * HUF detailed API
1038*01826a49SYabin Cui ******************************************/
1039*01826a49SYabin Cui /*!
1040*01826a49SYabin Cui The following API allows targeting specific sub-functions for advanced tasks.
1041*01826a49SYabin Cui For example, it's possible to compress several blocks using the same 'CTable',
1042*01826a49SYabin Cui or to save and regenerate 'CTable' using external methods.
1043*01826a49SYabin Cui */
1044*01826a49SYabin Cui /* FSEv07_count() : find it within "fse.h" */
1045*01826a49SYabin Cui
1046*01826a49SYabin Cui /*! HUFv07_readStats() :
1047*01826a49SYabin Cui Read compact Huffman tree, saved by HUFv07_writeCTable().
1048*01826a49SYabin Cui `huffWeight` is destination buffer.
1049*01826a49SYabin Cui @return : size read from `src` , or an error Code .
1050*01826a49SYabin Cui Note : Needed by HUFv07_readCTable() and HUFv07_readDTableXn() . */
1051*01826a49SYabin Cui size_t HUFv07_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
1052*01826a49SYabin Cui U32* nbSymbolsPtr, U32* tableLogPtr,
1053*01826a49SYabin Cui const void* src, size_t srcSize);
1054*01826a49SYabin Cui
1055*01826a49SYabin Cui
1056*01826a49SYabin Cui /*
1057*01826a49SYabin Cui HUFv07_decompress() does the following:
1058*01826a49SYabin Cui 1. select the decompression algorithm (X2, X4) based on pre-computed heuristics
1059*01826a49SYabin Cui 2. build Huffman table from save, using HUFv07_readDTableXn()
1060*01826a49SYabin Cui 3. decode 1 or 4 segments in parallel using HUFv07_decompressSXn_usingDTable
1061*01826a49SYabin Cui */
1062*01826a49SYabin Cui
1063*01826a49SYabin Cui /** HUFv07_selectDecoder() :
1064*01826a49SYabin Cui * Tells which decoder is likely to decode faster,
1065*01826a49SYabin Cui * based on a set of pre-determined metrics.
1066*01826a49SYabin Cui * @return : 0==HUFv07_decompress4X2, 1==HUFv07_decompress4X4 .
1067*01826a49SYabin Cui * Assumption : 0 < cSrcSize < dstSize <= 128 KB */
1068*01826a49SYabin Cui U32 HUFv07_selectDecoder (size_t dstSize, size_t cSrcSize);
1069*01826a49SYabin Cui
1070*01826a49SYabin Cui size_t HUFv07_readDTableX2 (HUFv07_DTable* DTable, const void* src, size_t srcSize);
1071*01826a49SYabin Cui size_t HUFv07_readDTableX4 (HUFv07_DTable* DTable, const void* src, size_t srcSize);
1072*01826a49SYabin Cui
1073*01826a49SYabin Cui size_t HUFv07_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUFv07_DTable* DTable);
1074*01826a49SYabin Cui size_t HUFv07_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUFv07_DTable* DTable);
1075*01826a49SYabin Cui size_t HUFv07_decompress4X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUFv07_DTable* DTable);
1076*01826a49SYabin Cui
1077*01826a49SYabin Cui
1078*01826a49SYabin Cui /* single stream variants */
1079*01826a49SYabin Cui size_t HUFv07_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
1080*01826a49SYabin Cui size_t HUFv07_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */
1081*01826a49SYabin Cui
1082*01826a49SYabin Cui size_t HUFv07_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUFv07_DTable* DTable);
1083*01826a49SYabin Cui size_t HUFv07_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUFv07_DTable* DTable);
1084*01826a49SYabin Cui size_t HUFv07_decompress1X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUFv07_DTable* DTable);
1085*01826a49SYabin Cui
1086*01826a49SYabin Cui
1087*01826a49SYabin Cui #endif /* HUFv07_STATIC_LINKING_ONLY */
1088*01826a49SYabin Cui
1089*01826a49SYabin Cui
1090*01826a49SYabin Cui #if defined (__cplusplus)
1091*01826a49SYabin Cui }
1092*01826a49SYabin Cui #endif
1093*01826a49SYabin Cui
1094*01826a49SYabin Cui #endif /* HUFv07_H_298734234 */
1095*01826a49SYabin Cui /*
1096*01826a49SYabin Cui Common functions of New Generation Entropy library
1097*01826a49SYabin Cui Copyright (C) 2016, Yann Collet.
1098*01826a49SYabin Cui
1099*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1100*01826a49SYabin Cui
1101*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
1102*01826a49SYabin Cui modification, are permitted provided that the following conditions are
1103*01826a49SYabin Cui met:
1104*01826a49SYabin Cui
1105*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
1106*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
1107*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
1108*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
1109*01826a49SYabin Cui in the documentation and/or other materials provided with the
1110*01826a49SYabin Cui distribution.
1111*01826a49SYabin Cui
1112*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1113*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1114*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1115*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1116*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1117*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1118*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1119*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1120*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1121*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1122*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1123*01826a49SYabin Cui
1124*01826a49SYabin Cui You can contact the author at :
1125*01826a49SYabin Cui - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
1126*01826a49SYabin Cui - Public forum : https://groups.google.com/forum/#!forum/lz4c
1127*01826a49SYabin Cui *************************************************************************** */
1128*01826a49SYabin Cui
1129*01826a49SYabin Cui
1130*01826a49SYabin Cui
1131*01826a49SYabin Cui /*-****************************************
1132*01826a49SYabin Cui * FSE Error Management
1133*01826a49SYabin Cui ******************************************/
FSEv07_isError(size_t code)1134*01826a49SYabin Cui unsigned FSEv07_isError(size_t code) { return ERR_isError(code); }
1135*01826a49SYabin Cui
FSEv07_getErrorName(size_t code)1136*01826a49SYabin Cui const char* FSEv07_getErrorName(size_t code) { return ERR_getErrorName(code); }
1137*01826a49SYabin Cui
1138*01826a49SYabin Cui
1139*01826a49SYabin Cui /* **************************************************************
1140*01826a49SYabin Cui * HUF Error Management
1141*01826a49SYabin Cui ****************************************************************/
HUFv07_isError(size_t code)1142*01826a49SYabin Cui unsigned HUFv07_isError(size_t code) { return ERR_isError(code); }
1143*01826a49SYabin Cui
HUFv07_getErrorName(size_t code)1144*01826a49SYabin Cui const char* HUFv07_getErrorName(size_t code) { return ERR_getErrorName(code); }
1145*01826a49SYabin Cui
1146*01826a49SYabin Cui
1147*01826a49SYabin Cui /*-**************************************************************
1148*01826a49SYabin Cui * FSE NCount encoding-decoding
1149*01826a49SYabin Cui ****************************************************************/
FSEv07_abs(short a)1150*01826a49SYabin Cui static short FSEv07_abs(short a) { return (short)(a<0 ? -a : a); }
1151*01826a49SYabin Cui
FSEv07_readNCount(short * normalizedCounter,unsigned * maxSVPtr,unsigned * tableLogPtr,const void * headerBuffer,size_t hbSize)1152*01826a49SYabin Cui size_t FSEv07_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
1153*01826a49SYabin Cui const void* headerBuffer, size_t hbSize)
1154*01826a49SYabin Cui {
1155*01826a49SYabin Cui const BYTE* const istart = (const BYTE*) headerBuffer;
1156*01826a49SYabin Cui const BYTE* const iend = istart + hbSize;
1157*01826a49SYabin Cui const BYTE* ip = istart;
1158*01826a49SYabin Cui int nbBits;
1159*01826a49SYabin Cui int remaining;
1160*01826a49SYabin Cui int threshold;
1161*01826a49SYabin Cui U32 bitStream;
1162*01826a49SYabin Cui int bitCount;
1163*01826a49SYabin Cui unsigned charnum = 0;
1164*01826a49SYabin Cui int previous0 = 0;
1165*01826a49SYabin Cui
1166*01826a49SYabin Cui if (hbSize < 4) return ERROR(srcSize_wrong);
1167*01826a49SYabin Cui bitStream = MEM_readLE32(ip);
1168*01826a49SYabin Cui nbBits = (bitStream & 0xF) + FSEv07_MIN_TABLELOG; /* extract tableLog */
1169*01826a49SYabin Cui if (nbBits > FSEv07_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
1170*01826a49SYabin Cui bitStream >>= 4;
1171*01826a49SYabin Cui bitCount = 4;
1172*01826a49SYabin Cui *tableLogPtr = nbBits;
1173*01826a49SYabin Cui remaining = (1<<nbBits)+1;
1174*01826a49SYabin Cui threshold = 1<<nbBits;
1175*01826a49SYabin Cui nbBits++;
1176*01826a49SYabin Cui
1177*01826a49SYabin Cui while ((remaining>1) && (charnum<=*maxSVPtr)) {
1178*01826a49SYabin Cui if (previous0) {
1179*01826a49SYabin Cui unsigned n0 = charnum;
1180*01826a49SYabin Cui while ((bitStream & 0xFFFF) == 0xFFFF) {
1181*01826a49SYabin Cui n0+=24;
1182*01826a49SYabin Cui if (ip < iend-5) {
1183*01826a49SYabin Cui ip+=2;
1184*01826a49SYabin Cui bitStream = MEM_readLE32(ip) >> bitCount;
1185*01826a49SYabin Cui } else {
1186*01826a49SYabin Cui bitStream >>= 16;
1187*01826a49SYabin Cui bitCount+=16;
1188*01826a49SYabin Cui } }
1189*01826a49SYabin Cui while ((bitStream & 3) == 3) {
1190*01826a49SYabin Cui n0+=3;
1191*01826a49SYabin Cui bitStream>>=2;
1192*01826a49SYabin Cui bitCount+=2;
1193*01826a49SYabin Cui }
1194*01826a49SYabin Cui n0 += bitStream & 3;
1195*01826a49SYabin Cui bitCount += 2;
1196*01826a49SYabin Cui if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
1197*01826a49SYabin Cui while (charnum < n0) normalizedCounter[charnum++] = 0;
1198*01826a49SYabin Cui if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
1199*01826a49SYabin Cui ip += bitCount>>3;
1200*01826a49SYabin Cui bitCount &= 7;
1201*01826a49SYabin Cui bitStream = MEM_readLE32(ip) >> bitCount;
1202*01826a49SYabin Cui }
1203*01826a49SYabin Cui else
1204*01826a49SYabin Cui bitStream >>= 2;
1205*01826a49SYabin Cui }
1206*01826a49SYabin Cui { short const max = (short)((2*threshold-1)-remaining);
1207*01826a49SYabin Cui short count;
1208*01826a49SYabin Cui
1209*01826a49SYabin Cui if ((bitStream & (threshold-1)) < (U32)max) {
1210*01826a49SYabin Cui count = (short)(bitStream & (threshold-1));
1211*01826a49SYabin Cui bitCount += nbBits-1;
1212*01826a49SYabin Cui } else {
1213*01826a49SYabin Cui count = (short)(bitStream & (2*threshold-1));
1214*01826a49SYabin Cui if (count >= threshold) count -= max;
1215*01826a49SYabin Cui bitCount += nbBits;
1216*01826a49SYabin Cui }
1217*01826a49SYabin Cui
1218*01826a49SYabin Cui count--; /* extra accuracy */
1219*01826a49SYabin Cui remaining -= FSEv07_abs(count);
1220*01826a49SYabin Cui normalizedCounter[charnum++] = count;
1221*01826a49SYabin Cui previous0 = !count;
1222*01826a49SYabin Cui while (remaining < threshold) {
1223*01826a49SYabin Cui nbBits--;
1224*01826a49SYabin Cui threshold >>= 1;
1225*01826a49SYabin Cui }
1226*01826a49SYabin Cui
1227*01826a49SYabin Cui if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
1228*01826a49SYabin Cui ip += bitCount>>3;
1229*01826a49SYabin Cui bitCount &= 7;
1230*01826a49SYabin Cui } else {
1231*01826a49SYabin Cui bitCount -= (int)(8 * (iend - 4 - ip));
1232*01826a49SYabin Cui ip = iend - 4;
1233*01826a49SYabin Cui }
1234*01826a49SYabin Cui bitStream = MEM_readLE32(ip) >> (bitCount & 31);
1235*01826a49SYabin Cui } } /* while ((remaining>1) && (charnum<=*maxSVPtr)) */
1236*01826a49SYabin Cui if (remaining != 1) return ERROR(GENERIC);
1237*01826a49SYabin Cui *maxSVPtr = charnum-1;
1238*01826a49SYabin Cui
1239*01826a49SYabin Cui ip += (bitCount+7)>>3;
1240*01826a49SYabin Cui if ((size_t)(ip-istart) > hbSize) return ERROR(srcSize_wrong);
1241*01826a49SYabin Cui return ip-istart;
1242*01826a49SYabin Cui }
1243*01826a49SYabin Cui
1244*01826a49SYabin Cui
1245*01826a49SYabin Cui /*! HUFv07_readStats() :
1246*01826a49SYabin Cui Read compact Huffman tree, saved by HUFv07_writeCTable().
1247*01826a49SYabin Cui `huffWeight` is destination buffer.
1248*01826a49SYabin Cui @return : size read from `src` , or an error Code .
1249*01826a49SYabin Cui Note : Needed by HUFv07_readCTable() and HUFv07_readDTableXn() .
1250*01826a49SYabin Cui */
HUFv07_readStats(BYTE * huffWeight,size_t hwSize,U32 * rankStats,U32 * nbSymbolsPtr,U32 * tableLogPtr,const void * src,size_t srcSize)1251*01826a49SYabin Cui size_t HUFv07_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
1252*01826a49SYabin Cui U32* nbSymbolsPtr, U32* tableLogPtr,
1253*01826a49SYabin Cui const void* src, size_t srcSize)
1254*01826a49SYabin Cui {
1255*01826a49SYabin Cui U32 weightTotal;
1256*01826a49SYabin Cui const BYTE* ip = (const BYTE*) src;
1257*01826a49SYabin Cui size_t iSize;
1258*01826a49SYabin Cui size_t oSize;
1259*01826a49SYabin Cui
1260*01826a49SYabin Cui if (!srcSize) return ERROR(srcSize_wrong);
1261*01826a49SYabin Cui iSize = ip[0];
1262*01826a49SYabin Cui /* memset(huffWeight, 0, hwSize); */ /* is not necessary, even though some analyzer complain ... */
1263*01826a49SYabin Cui
1264*01826a49SYabin Cui if (iSize >= 128) { /* special header */
1265*01826a49SYabin Cui if (iSize >= (242)) { /* RLE */
1266*01826a49SYabin Cui static U32 l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 };
1267*01826a49SYabin Cui oSize = l[iSize-242];
1268*01826a49SYabin Cui memset(huffWeight, 1, hwSize);
1269*01826a49SYabin Cui iSize = 0;
1270*01826a49SYabin Cui }
1271*01826a49SYabin Cui else { /* Incompressible */
1272*01826a49SYabin Cui oSize = iSize - 127;
1273*01826a49SYabin Cui iSize = ((oSize+1)/2);
1274*01826a49SYabin Cui if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
1275*01826a49SYabin Cui if (oSize >= hwSize) return ERROR(corruption_detected);
1276*01826a49SYabin Cui ip += 1;
1277*01826a49SYabin Cui { U32 n;
1278*01826a49SYabin Cui for (n=0; n<oSize; n+=2) {
1279*01826a49SYabin Cui huffWeight[n] = ip[n/2] >> 4;
1280*01826a49SYabin Cui huffWeight[n+1] = ip[n/2] & 15;
1281*01826a49SYabin Cui } } } }
1282*01826a49SYabin Cui else { /* header compressed with FSE (normal case) */
1283*01826a49SYabin Cui if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
1284*01826a49SYabin Cui oSize = FSEv07_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */
1285*01826a49SYabin Cui if (FSEv07_isError(oSize)) return oSize;
1286*01826a49SYabin Cui }
1287*01826a49SYabin Cui
1288*01826a49SYabin Cui /* collect weight stats */
1289*01826a49SYabin Cui memset(rankStats, 0, (HUFv07_TABLELOG_ABSOLUTEMAX + 1) * sizeof(U32));
1290*01826a49SYabin Cui weightTotal = 0;
1291*01826a49SYabin Cui { U32 n; for (n=0; n<oSize; n++) {
1292*01826a49SYabin Cui if (huffWeight[n] >= HUFv07_TABLELOG_ABSOLUTEMAX) return ERROR(corruption_detected);
1293*01826a49SYabin Cui rankStats[huffWeight[n]]++;
1294*01826a49SYabin Cui weightTotal += (1 << huffWeight[n]) >> 1;
1295*01826a49SYabin Cui } }
1296*01826a49SYabin Cui if (weightTotal == 0) return ERROR(corruption_detected);
1297*01826a49SYabin Cui
1298*01826a49SYabin Cui /* get last non-null symbol weight (implied, total must be 2^n) */
1299*01826a49SYabin Cui { U32 const tableLog = BITv07_highbit32(weightTotal) + 1;
1300*01826a49SYabin Cui if (tableLog > HUFv07_TABLELOG_ABSOLUTEMAX) return ERROR(corruption_detected);
1301*01826a49SYabin Cui *tableLogPtr = tableLog;
1302*01826a49SYabin Cui /* determine last weight */
1303*01826a49SYabin Cui { U32 const total = 1 << tableLog;
1304*01826a49SYabin Cui U32 const rest = total - weightTotal;
1305*01826a49SYabin Cui U32 const verif = 1 << BITv07_highbit32(rest);
1306*01826a49SYabin Cui U32 const lastWeight = BITv07_highbit32(rest) + 1;
1307*01826a49SYabin Cui if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
1308*01826a49SYabin Cui huffWeight[oSize] = (BYTE)lastWeight;
1309*01826a49SYabin Cui rankStats[lastWeight]++;
1310*01826a49SYabin Cui } }
1311*01826a49SYabin Cui
1312*01826a49SYabin Cui /* check tree construction validity */
1313*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 */
1314*01826a49SYabin Cui
1315*01826a49SYabin Cui /* results */
1316*01826a49SYabin Cui *nbSymbolsPtr = (U32)(oSize+1);
1317*01826a49SYabin Cui return iSize+1;
1318*01826a49SYabin Cui }
1319*01826a49SYabin Cui /* ******************************************************************
1320*01826a49SYabin Cui FSE : Finite State Entropy decoder
1321*01826a49SYabin Cui Copyright (C) 2013-2015, Yann Collet.
1322*01826a49SYabin Cui
1323*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1324*01826a49SYabin Cui
1325*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
1326*01826a49SYabin Cui modification, are permitted provided that the following conditions are
1327*01826a49SYabin Cui met:
1328*01826a49SYabin Cui
1329*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
1330*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
1331*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
1332*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
1333*01826a49SYabin Cui in the documentation and/or other materials provided with the
1334*01826a49SYabin Cui distribution.
1335*01826a49SYabin Cui
1336*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1337*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1338*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1339*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1340*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1341*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1342*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1343*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1344*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1345*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1346*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1347*01826a49SYabin Cui
1348*01826a49SYabin Cui You can contact the author at :
1349*01826a49SYabin Cui - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
1350*01826a49SYabin Cui - Public forum : https://groups.google.com/forum/#!forum/lz4c
1351*01826a49SYabin Cui ****************************************************************** */
1352*01826a49SYabin Cui
1353*01826a49SYabin Cui
1354*01826a49SYabin Cui /* **************************************************************
1355*01826a49SYabin Cui * Compiler specifics
1356*01826a49SYabin Cui ****************************************************************/
1357*01826a49SYabin Cui #ifdef _MSC_VER /* Visual Studio */
1358*01826a49SYabin Cui # define FORCE_INLINE static __forceinline
1359*01826a49SYabin Cui # include <intrin.h> /* For Visual 2005 */
1360*01826a49SYabin Cui # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1361*01826a49SYabin Cui # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
1362*01826a49SYabin Cui #else
1363*01826a49SYabin Cui # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
1364*01826a49SYabin Cui # ifdef __GNUC__
1365*01826a49SYabin Cui # define FORCE_INLINE static inline __attribute__((always_inline))
1366*01826a49SYabin Cui # else
1367*01826a49SYabin Cui # define FORCE_INLINE static inline
1368*01826a49SYabin Cui # endif
1369*01826a49SYabin Cui # else
1370*01826a49SYabin Cui # define FORCE_INLINE static
1371*01826a49SYabin Cui # endif /* __STDC_VERSION__ */
1372*01826a49SYabin Cui #endif
1373*01826a49SYabin Cui
1374*01826a49SYabin Cui
1375*01826a49SYabin Cui /* **************************************************************
1376*01826a49SYabin Cui * Error Management
1377*01826a49SYabin Cui ****************************************************************/
1378*01826a49SYabin Cui #define FSEv07_isError ERR_isError
1379*01826a49SYabin Cui #define FSEv07_STATIC_ASSERT(c) { enum { FSEv07_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
1380*01826a49SYabin Cui
1381*01826a49SYabin Cui
1382*01826a49SYabin Cui /* **************************************************************
1383*01826a49SYabin Cui * Complex types
1384*01826a49SYabin Cui ****************************************************************/
1385*01826a49SYabin Cui typedef U32 DTable_max_t[FSEv07_DTABLE_SIZE_U32(FSEv07_MAX_TABLELOG)];
1386*01826a49SYabin Cui
1387*01826a49SYabin Cui
1388*01826a49SYabin Cui /* **************************************************************
1389*01826a49SYabin Cui * Templates
1390*01826a49SYabin Cui ****************************************************************/
1391*01826a49SYabin Cui /*
1392*01826a49SYabin Cui designed to be included
1393*01826a49SYabin Cui for type-specific functions (template emulation in C)
1394*01826a49SYabin Cui Objective is to write these functions only once, for improved maintenance
1395*01826a49SYabin Cui */
1396*01826a49SYabin Cui
1397*01826a49SYabin Cui /* safety checks */
1398*01826a49SYabin Cui #ifndef FSEv07_FUNCTION_EXTENSION
1399*01826a49SYabin Cui # error "FSEv07_FUNCTION_EXTENSION must be defined"
1400*01826a49SYabin Cui #endif
1401*01826a49SYabin Cui #ifndef FSEv07_FUNCTION_TYPE
1402*01826a49SYabin Cui # error "FSEv07_FUNCTION_TYPE must be defined"
1403*01826a49SYabin Cui #endif
1404*01826a49SYabin Cui
1405*01826a49SYabin Cui /* Function names */
1406*01826a49SYabin Cui #define FSEv07_CAT(X,Y) X##Y
1407*01826a49SYabin Cui #define FSEv07_FUNCTION_NAME(X,Y) FSEv07_CAT(X,Y)
1408*01826a49SYabin Cui #define FSEv07_TYPE_NAME(X,Y) FSEv07_CAT(X,Y)
1409*01826a49SYabin Cui
1410*01826a49SYabin Cui
1411*01826a49SYabin Cui /* Function templates */
FSEv07_createDTable(unsigned tableLog)1412*01826a49SYabin Cui FSEv07_DTable* FSEv07_createDTable (unsigned tableLog)
1413*01826a49SYabin Cui {
1414*01826a49SYabin Cui if (tableLog > FSEv07_TABLELOG_ABSOLUTE_MAX) tableLog = FSEv07_TABLELOG_ABSOLUTE_MAX;
1415*01826a49SYabin Cui return (FSEv07_DTable*)malloc( FSEv07_DTABLE_SIZE_U32(tableLog) * sizeof (U32) );
1416*01826a49SYabin Cui }
1417*01826a49SYabin Cui
FSEv07_freeDTable(FSEv07_DTable * dt)1418*01826a49SYabin Cui void FSEv07_freeDTable (FSEv07_DTable* dt)
1419*01826a49SYabin Cui {
1420*01826a49SYabin Cui free(dt);
1421*01826a49SYabin Cui }
1422*01826a49SYabin Cui
FSEv07_buildDTable(FSEv07_DTable * dt,const short * normalizedCounter,unsigned maxSymbolValue,unsigned tableLog)1423*01826a49SYabin Cui size_t FSEv07_buildDTable(FSEv07_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
1424*01826a49SYabin Cui {
1425*01826a49SYabin Cui void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */
1426*01826a49SYabin Cui FSEv07_DECODE_TYPE* const tableDecode = (FSEv07_DECODE_TYPE*) (tdPtr);
1427*01826a49SYabin Cui U16 symbolNext[FSEv07_MAX_SYMBOL_VALUE+1];
1428*01826a49SYabin Cui
1429*01826a49SYabin Cui U32 const maxSV1 = maxSymbolValue + 1;
1430*01826a49SYabin Cui U32 const tableSize = 1 << tableLog;
1431*01826a49SYabin Cui U32 highThreshold = tableSize-1;
1432*01826a49SYabin Cui
1433*01826a49SYabin Cui /* Sanity Checks */
1434*01826a49SYabin Cui if (maxSymbolValue > FSEv07_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge);
1435*01826a49SYabin Cui if (tableLog > FSEv07_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
1436*01826a49SYabin Cui
1437*01826a49SYabin Cui /* Init, lay down lowprob symbols */
1438*01826a49SYabin Cui { FSEv07_DTableHeader DTableH;
1439*01826a49SYabin Cui DTableH.tableLog = (U16)tableLog;
1440*01826a49SYabin Cui DTableH.fastMode = 1;
1441*01826a49SYabin Cui { S16 const largeLimit= (S16)(1 << (tableLog-1));
1442*01826a49SYabin Cui U32 s;
1443*01826a49SYabin Cui for (s=0; s<maxSV1; s++) {
1444*01826a49SYabin Cui if (normalizedCounter[s]==-1) {
1445*01826a49SYabin Cui tableDecode[highThreshold--].symbol = (FSEv07_FUNCTION_TYPE)s;
1446*01826a49SYabin Cui symbolNext[s] = 1;
1447*01826a49SYabin Cui } else {
1448*01826a49SYabin Cui if (normalizedCounter[s] >= largeLimit) DTableH.fastMode=0;
1449*01826a49SYabin Cui symbolNext[s] = normalizedCounter[s];
1450*01826a49SYabin Cui } } }
1451*01826a49SYabin Cui memcpy(dt, &DTableH, sizeof(DTableH));
1452*01826a49SYabin Cui }
1453*01826a49SYabin Cui
1454*01826a49SYabin Cui /* Spread symbols */
1455*01826a49SYabin Cui { U32 const tableMask = tableSize-1;
1456*01826a49SYabin Cui U32 const step = FSEv07_TABLESTEP(tableSize);
1457*01826a49SYabin Cui U32 s, position = 0;
1458*01826a49SYabin Cui for (s=0; s<maxSV1; s++) {
1459*01826a49SYabin Cui int i;
1460*01826a49SYabin Cui for (i=0; i<normalizedCounter[s]; i++) {
1461*01826a49SYabin Cui tableDecode[position].symbol = (FSEv07_FUNCTION_TYPE)s;
1462*01826a49SYabin Cui position = (position + step) & tableMask;
1463*01826a49SYabin Cui while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
1464*01826a49SYabin Cui } }
1465*01826a49SYabin Cui
1466*01826a49SYabin Cui if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
1467*01826a49SYabin Cui }
1468*01826a49SYabin Cui
1469*01826a49SYabin Cui /* Build Decoding table */
1470*01826a49SYabin Cui { U32 u;
1471*01826a49SYabin Cui for (u=0; u<tableSize; u++) {
1472*01826a49SYabin Cui FSEv07_FUNCTION_TYPE const symbol = (FSEv07_FUNCTION_TYPE)(tableDecode[u].symbol);
1473*01826a49SYabin Cui U16 nextState = symbolNext[symbol]++;
1474*01826a49SYabin Cui tableDecode[u].nbBits = (BYTE) (tableLog - BITv07_highbit32 ((U32)nextState) );
1475*01826a49SYabin Cui tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
1476*01826a49SYabin Cui } }
1477*01826a49SYabin Cui
1478*01826a49SYabin Cui return 0;
1479*01826a49SYabin Cui }
1480*01826a49SYabin Cui
1481*01826a49SYabin Cui
1482*01826a49SYabin Cui
1483*01826a49SYabin Cui #ifndef FSEv07_COMMONDEFS_ONLY
1484*01826a49SYabin Cui
1485*01826a49SYabin Cui /*-*******************************************************
1486*01826a49SYabin Cui * Decompression (Byte symbols)
1487*01826a49SYabin Cui *********************************************************/
FSEv07_buildDTable_rle(FSEv07_DTable * dt,BYTE symbolValue)1488*01826a49SYabin Cui size_t FSEv07_buildDTable_rle (FSEv07_DTable* dt, BYTE symbolValue)
1489*01826a49SYabin Cui {
1490*01826a49SYabin Cui void* ptr = dt;
1491*01826a49SYabin Cui FSEv07_DTableHeader* const DTableH = (FSEv07_DTableHeader*)ptr;
1492*01826a49SYabin Cui void* dPtr = dt + 1;
1493*01826a49SYabin Cui FSEv07_decode_t* const cell = (FSEv07_decode_t*)dPtr;
1494*01826a49SYabin Cui
1495*01826a49SYabin Cui DTableH->tableLog = 0;
1496*01826a49SYabin Cui DTableH->fastMode = 0;
1497*01826a49SYabin Cui
1498*01826a49SYabin Cui cell->newState = 0;
1499*01826a49SYabin Cui cell->symbol = symbolValue;
1500*01826a49SYabin Cui cell->nbBits = 0;
1501*01826a49SYabin Cui
1502*01826a49SYabin Cui return 0;
1503*01826a49SYabin Cui }
1504*01826a49SYabin Cui
1505*01826a49SYabin Cui
FSEv07_buildDTable_raw(FSEv07_DTable * dt,unsigned nbBits)1506*01826a49SYabin Cui size_t FSEv07_buildDTable_raw (FSEv07_DTable* dt, unsigned nbBits)
1507*01826a49SYabin Cui {
1508*01826a49SYabin Cui void* ptr = dt;
1509*01826a49SYabin Cui FSEv07_DTableHeader* const DTableH = (FSEv07_DTableHeader*)ptr;
1510*01826a49SYabin Cui void* dPtr = dt + 1;
1511*01826a49SYabin Cui FSEv07_decode_t* const dinfo = (FSEv07_decode_t*)dPtr;
1512*01826a49SYabin Cui const unsigned tableSize = 1 << nbBits;
1513*01826a49SYabin Cui const unsigned tableMask = tableSize - 1;
1514*01826a49SYabin Cui const unsigned maxSV1 = tableMask+1;
1515*01826a49SYabin Cui unsigned s;
1516*01826a49SYabin Cui
1517*01826a49SYabin Cui /* Sanity checks */
1518*01826a49SYabin Cui if (nbBits < 1) return ERROR(GENERIC); /* min size */
1519*01826a49SYabin Cui
1520*01826a49SYabin Cui /* Build Decoding Table */
1521*01826a49SYabin Cui DTableH->tableLog = (U16)nbBits;
1522*01826a49SYabin Cui DTableH->fastMode = 1;
1523*01826a49SYabin Cui for (s=0; s<maxSV1; s++) {
1524*01826a49SYabin Cui dinfo[s].newState = 0;
1525*01826a49SYabin Cui dinfo[s].symbol = (BYTE)s;
1526*01826a49SYabin Cui dinfo[s].nbBits = (BYTE)nbBits;
1527*01826a49SYabin Cui }
1528*01826a49SYabin Cui
1529*01826a49SYabin Cui return 0;
1530*01826a49SYabin Cui }
1531*01826a49SYabin Cui
FSEv07_decompress_usingDTable_generic(void * dst,size_t maxDstSize,const void * cSrc,size_t cSrcSize,const FSEv07_DTable * dt,const unsigned fast)1532*01826a49SYabin Cui FORCE_INLINE size_t FSEv07_decompress_usingDTable_generic(
1533*01826a49SYabin Cui void* dst, size_t maxDstSize,
1534*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1535*01826a49SYabin Cui const FSEv07_DTable* dt, const unsigned fast)
1536*01826a49SYabin Cui {
1537*01826a49SYabin Cui BYTE* const ostart = (BYTE*) dst;
1538*01826a49SYabin Cui BYTE* op = ostart;
1539*01826a49SYabin Cui BYTE* const omax = op + maxDstSize;
1540*01826a49SYabin Cui BYTE* const olimit = omax-3;
1541*01826a49SYabin Cui
1542*01826a49SYabin Cui BITv07_DStream_t bitD;
1543*01826a49SYabin Cui FSEv07_DState_t state1;
1544*01826a49SYabin Cui FSEv07_DState_t state2;
1545*01826a49SYabin Cui
1546*01826a49SYabin Cui /* Init */
1547*01826a49SYabin Cui { size_t const errorCode = BITv07_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */
1548*01826a49SYabin Cui if (FSEv07_isError(errorCode)) return errorCode; }
1549*01826a49SYabin Cui
1550*01826a49SYabin Cui FSEv07_initDState(&state1, &bitD, dt);
1551*01826a49SYabin Cui FSEv07_initDState(&state2, &bitD, dt);
1552*01826a49SYabin Cui
1553*01826a49SYabin Cui #define FSEv07_GETSYMBOL(statePtr) fast ? FSEv07_decodeSymbolFast(statePtr, &bitD) : FSEv07_decodeSymbol(statePtr, &bitD)
1554*01826a49SYabin Cui
1555*01826a49SYabin Cui /* 4 symbols per loop */
1556*01826a49SYabin Cui for ( ; (BITv07_reloadDStream(&bitD)==BITv07_DStream_unfinished) && (op<olimit) ; op+=4) {
1557*01826a49SYabin Cui op[0] = FSEv07_GETSYMBOL(&state1);
1558*01826a49SYabin Cui
1559*01826a49SYabin Cui if (FSEv07_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1560*01826a49SYabin Cui BITv07_reloadDStream(&bitD);
1561*01826a49SYabin Cui
1562*01826a49SYabin Cui op[1] = FSEv07_GETSYMBOL(&state2);
1563*01826a49SYabin Cui
1564*01826a49SYabin Cui if (FSEv07_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1565*01826a49SYabin Cui { if (BITv07_reloadDStream(&bitD) > BITv07_DStream_unfinished) { op+=2; break; } }
1566*01826a49SYabin Cui
1567*01826a49SYabin Cui op[2] = FSEv07_GETSYMBOL(&state1);
1568*01826a49SYabin Cui
1569*01826a49SYabin Cui if (FSEv07_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
1570*01826a49SYabin Cui BITv07_reloadDStream(&bitD);
1571*01826a49SYabin Cui
1572*01826a49SYabin Cui op[3] = FSEv07_GETSYMBOL(&state2);
1573*01826a49SYabin Cui }
1574*01826a49SYabin Cui
1575*01826a49SYabin Cui /* tail */
1576*01826a49SYabin Cui /* note : BITv07_reloadDStream(&bitD) >= FSEv07_DStream_partiallyFilled; Ends at exactly BITv07_DStream_completed */
1577*01826a49SYabin Cui while (1) {
1578*01826a49SYabin Cui if (op>(omax-2)) return ERROR(dstSize_tooSmall);
1579*01826a49SYabin Cui
1580*01826a49SYabin Cui *op++ = FSEv07_GETSYMBOL(&state1);
1581*01826a49SYabin Cui
1582*01826a49SYabin Cui if (BITv07_reloadDStream(&bitD)==BITv07_DStream_overflow) {
1583*01826a49SYabin Cui *op++ = FSEv07_GETSYMBOL(&state2);
1584*01826a49SYabin Cui break;
1585*01826a49SYabin Cui }
1586*01826a49SYabin Cui
1587*01826a49SYabin Cui if (op>(omax-2)) return ERROR(dstSize_tooSmall);
1588*01826a49SYabin Cui
1589*01826a49SYabin Cui *op++ = FSEv07_GETSYMBOL(&state2);
1590*01826a49SYabin Cui
1591*01826a49SYabin Cui if (BITv07_reloadDStream(&bitD)==BITv07_DStream_overflow) {
1592*01826a49SYabin Cui *op++ = FSEv07_GETSYMBOL(&state1);
1593*01826a49SYabin Cui break;
1594*01826a49SYabin Cui } }
1595*01826a49SYabin Cui
1596*01826a49SYabin Cui return op-ostart;
1597*01826a49SYabin Cui }
1598*01826a49SYabin Cui
1599*01826a49SYabin Cui
FSEv07_decompress_usingDTable(void * dst,size_t originalSize,const void * cSrc,size_t cSrcSize,const FSEv07_DTable * dt)1600*01826a49SYabin Cui size_t FSEv07_decompress_usingDTable(void* dst, size_t originalSize,
1601*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1602*01826a49SYabin Cui const FSEv07_DTable* dt)
1603*01826a49SYabin Cui {
1604*01826a49SYabin Cui const void* ptr = dt;
1605*01826a49SYabin Cui const FSEv07_DTableHeader* DTableH = (const FSEv07_DTableHeader*)ptr;
1606*01826a49SYabin Cui const U32 fastMode = DTableH->fastMode;
1607*01826a49SYabin Cui
1608*01826a49SYabin Cui /* select fast mode (static) */
1609*01826a49SYabin Cui if (fastMode) return FSEv07_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);
1610*01826a49SYabin Cui return FSEv07_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);
1611*01826a49SYabin Cui }
1612*01826a49SYabin Cui
1613*01826a49SYabin Cui
FSEv07_decompress(void * dst,size_t maxDstSize,const void * cSrc,size_t cSrcSize)1614*01826a49SYabin Cui size_t FSEv07_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize)
1615*01826a49SYabin Cui {
1616*01826a49SYabin Cui const BYTE* const istart = (const BYTE*)cSrc;
1617*01826a49SYabin Cui const BYTE* ip = istart;
1618*01826a49SYabin Cui short counting[FSEv07_MAX_SYMBOL_VALUE+1];
1619*01826a49SYabin Cui DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
1620*01826a49SYabin Cui unsigned tableLog;
1621*01826a49SYabin Cui unsigned maxSymbolValue = FSEv07_MAX_SYMBOL_VALUE;
1622*01826a49SYabin Cui
1623*01826a49SYabin Cui if (cSrcSize<2) return ERROR(srcSize_wrong); /* too small input size */
1624*01826a49SYabin Cui
1625*01826a49SYabin Cui /* normal FSE decoding mode */
1626*01826a49SYabin Cui { size_t const NCountLength = FSEv07_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
1627*01826a49SYabin Cui if (FSEv07_isError(NCountLength)) return NCountLength;
1628*01826a49SYabin Cui if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */
1629*01826a49SYabin Cui ip += NCountLength;
1630*01826a49SYabin Cui cSrcSize -= NCountLength;
1631*01826a49SYabin Cui }
1632*01826a49SYabin Cui
1633*01826a49SYabin Cui { size_t const errorCode = FSEv07_buildDTable (dt, counting, maxSymbolValue, tableLog);
1634*01826a49SYabin Cui if (FSEv07_isError(errorCode)) return errorCode; }
1635*01826a49SYabin Cui
1636*01826a49SYabin Cui return FSEv07_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt); /* always return, even if it is an error code */
1637*01826a49SYabin Cui }
1638*01826a49SYabin Cui
1639*01826a49SYabin Cui
1640*01826a49SYabin Cui
1641*01826a49SYabin Cui #endif /* FSEv07_COMMONDEFS_ONLY */
1642*01826a49SYabin Cui
1643*01826a49SYabin Cui /* ******************************************************************
1644*01826a49SYabin Cui Huffman decoder, part of New Generation Entropy library
1645*01826a49SYabin Cui Copyright (C) 2013-2016, Yann Collet.
1646*01826a49SYabin Cui
1647*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
1648*01826a49SYabin Cui
1649*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
1650*01826a49SYabin Cui modification, are permitted provided that the following conditions are
1651*01826a49SYabin Cui met:
1652*01826a49SYabin Cui
1653*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
1654*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
1655*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
1656*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
1657*01826a49SYabin Cui in the documentation and/or other materials provided with the
1658*01826a49SYabin Cui distribution.
1659*01826a49SYabin Cui
1660*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1661*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1662*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1663*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1664*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1665*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1666*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1667*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1668*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1669*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1670*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1671*01826a49SYabin Cui
1672*01826a49SYabin Cui You can contact the author at :
1673*01826a49SYabin Cui - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
1674*01826a49SYabin Cui - Public forum : https://groups.google.com/forum/#!forum/lz4c
1675*01826a49SYabin Cui ****************************************************************** */
1676*01826a49SYabin Cui
1677*01826a49SYabin Cui /* **************************************************************
1678*01826a49SYabin Cui * Compiler specifics
1679*01826a49SYabin Cui ****************************************************************/
1680*01826a49SYabin Cui #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
1681*01826a49SYabin Cui /* inline is defined */
1682*01826a49SYabin Cui #elif defined(_MSC_VER)
1683*01826a49SYabin Cui # define inline __inline
1684*01826a49SYabin Cui #else
1685*01826a49SYabin Cui # define inline /* disable inline */
1686*01826a49SYabin Cui #endif
1687*01826a49SYabin Cui
1688*01826a49SYabin Cui
1689*01826a49SYabin Cui #ifdef _MSC_VER /* Visual Studio */
1690*01826a49SYabin Cui # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1691*01826a49SYabin Cui #endif
1692*01826a49SYabin Cui
1693*01826a49SYabin Cui
1694*01826a49SYabin Cui
1695*01826a49SYabin Cui /* **************************************************************
1696*01826a49SYabin Cui * Error Management
1697*01826a49SYabin Cui ****************************************************************/
1698*01826a49SYabin Cui #define HUFv07_STATIC_ASSERT(c) { enum { HUFv07_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
1699*01826a49SYabin Cui
1700*01826a49SYabin Cui
1701*01826a49SYabin Cui /*-***************************/
1702*01826a49SYabin Cui /* generic DTableDesc */
1703*01826a49SYabin Cui /*-***************************/
1704*01826a49SYabin Cui
1705*01826a49SYabin Cui typedef struct { BYTE maxTableLog; BYTE tableType; BYTE tableLog; BYTE reserved; } DTableDesc;
1706*01826a49SYabin Cui
HUFv07_getDTableDesc(const HUFv07_DTable * table)1707*01826a49SYabin Cui static DTableDesc HUFv07_getDTableDesc(const HUFv07_DTable* table)
1708*01826a49SYabin Cui {
1709*01826a49SYabin Cui DTableDesc dtd;
1710*01826a49SYabin Cui memcpy(&dtd, table, sizeof(dtd));
1711*01826a49SYabin Cui return dtd;
1712*01826a49SYabin Cui }
1713*01826a49SYabin Cui
1714*01826a49SYabin Cui
1715*01826a49SYabin Cui /*-***************************/
1716*01826a49SYabin Cui /* single-symbol decoding */
1717*01826a49SYabin Cui /*-***************************/
1718*01826a49SYabin Cui
1719*01826a49SYabin Cui typedef struct { BYTE byte; BYTE nbBits; } HUFv07_DEltX2; /* single-symbol decoding */
1720*01826a49SYabin Cui
HUFv07_readDTableX2(HUFv07_DTable * DTable,const void * src,size_t srcSize)1721*01826a49SYabin Cui size_t HUFv07_readDTableX2 (HUFv07_DTable* DTable, const void* src, size_t srcSize)
1722*01826a49SYabin Cui {
1723*01826a49SYabin Cui BYTE huffWeight[HUFv07_SYMBOLVALUE_MAX + 1];
1724*01826a49SYabin Cui U32 rankVal[HUFv07_TABLELOG_ABSOLUTEMAX + 1]; /* large enough for values from 0 to 16 */
1725*01826a49SYabin Cui U32 tableLog = 0;
1726*01826a49SYabin Cui U32 nbSymbols = 0;
1727*01826a49SYabin Cui size_t iSize;
1728*01826a49SYabin Cui void* const dtPtr = DTable + 1;
1729*01826a49SYabin Cui HUFv07_DEltX2* const dt = (HUFv07_DEltX2*)dtPtr;
1730*01826a49SYabin Cui
1731*01826a49SYabin Cui HUFv07_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUFv07_DTable));
1732*01826a49SYabin Cui /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
1733*01826a49SYabin Cui
1734*01826a49SYabin Cui iSize = HUFv07_readStats(huffWeight, HUFv07_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
1735*01826a49SYabin Cui if (HUFv07_isError(iSize)) return iSize;
1736*01826a49SYabin Cui
1737*01826a49SYabin Cui /* Table header */
1738*01826a49SYabin Cui { DTableDesc dtd = HUFv07_getDTableDesc(DTable);
1739*01826a49SYabin Cui if (tableLog > (U32)(dtd.maxTableLog+1)) return ERROR(tableLog_tooLarge); /* DTable too small, huffman tree cannot fit in */
1740*01826a49SYabin Cui dtd.tableType = 0;
1741*01826a49SYabin Cui dtd.tableLog = (BYTE)tableLog;
1742*01826a49SYabin Cui memcpy(DTable, &dtd, sizeof(dtd));
1743*01826a49SYabin Cui }
1744*01826a49SYabin Cui
1745*01826a49SYabin Cui /* Prepare ranks */
1746*01826a49SYabin Cui { U32 n, nextRankStart = 0;
1747*01826a49SYabin Cui for (n=1; n<tableLog+1; n++) {
1748*01826a49SYabin Cui U32 current = nextRankStart;
1749*01826a49SYabin Cui nextRankStart += (rankVal[n] << (n-1));
1750*01826a49SYabin Cui rankVal[n] = current;
1751*01826a49SYabin Cui } }
1752*01826a49SYabin Cui
1753*01826a49SYabin Cui /* fill DTable */
1754*01826a49SYabin Cui { U32 n;
1755*01826a49SYabin Cui for (n=0; n<nbSymbols; n++) {
1756*01826a49SYabin Cui U32 const w = huffWeight[n];
1757*01826a49SYabin Cui U32 const length = (1 << w) >> 1;
1758*01826a49SYabin Cui U32 i;
1759*01826a49SYabin Cui HUFv07_DEltX2 D;
1760*01826a49SYabin Cui D.byte = (BYTE)n; D.nbBits = (BYTE)(tableLog + 1 - w);
1761*01826a49SYabin Cui for (i = rankVal[w]; i < rankVal[w] + length; i++)
1762*01826a49SYabin Cui dt[i] = D;
1763*01826a49SYabin Cui rankVal[w] += length;
1764*01826a49SYabin Cui } }
1765*01826a49SYabin Cui
1766*01826a49SYabin Cui return iSize;
1767*01826a49SYabin Cui }
1768*01826a49SYabin Cui
1769*01826a49SYabin Cui
HUFv07_decodeSymbolX2(BITv07_DStream_t * Dstream,const HUFv07_DEltX2 * dt,const U32 dtLog)1770*01826a49SYabin Cui static BYTE HUFv07_decodeSymbolX2(BITv07_DStream_t* Dstream, const HUFv07_DEltX2* dt, const U32 dtLog)
1771*01826a49SYabin Cui {
1772*01826a49SYabin Cui size_t const val = BITv07_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
1773*01826a49SYabin Cui BYTE const c = dt[val].byte;
1774*01826a49SYabin Cui BITv07_skipBits(Dstream, dt[val].nbBits);
1775*01826a49SYabin Cui return c;
1776*01826a49SYabin Cui }
1777*01826a49SYabin Cui
1778*01826a49SYabin Cui #define HUFv07_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \
1779*01826a49SYabin Cui *ptr++ = HUFv07_decodeSymbolX2(DStreamPtr, dt, dtLog)
1780*01826a49SYabin Cui
1781*01826a49SYabin Cui #define HUFv07_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \
1782*01826a49SYabin Cui if (MEM_64bits() || (HUFv07_TABLELOG_MAX<=12)) \
1783*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
1784*01826a49SYabin Cui
1785*01826a49SYabin Cui #define HUFv07_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \
1786*01826a49SYabin Cui if (MEM_64bits()) \
1787*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_0(ptr, DStreamPtr)
1788*01826a49SYabin Cui
HUFv07_decodeStreamX2(BYTE * p,BITv07_DStream_t * const bitDPtr,BYTE * const pEnd,const HUFv07_DEltX2 * const dt,const U32 dtLog)1789*01826a49SYabin Cui static inline size_t HUFv07_decodeStreamX2(BYTE* p, BITv07_DStream_t* const bitDPtr, BYTE* const pEnd, const HUFv07_DEltX2* const dt, const U32 dtLog)
1790*01826a49SYabin Cui {
1791*01826a49SYabin Cui BYTE* const pStart = p;
1792*01826a49SYabin Cui
1793*01826a49SYabin Cui /* up to 4 symbols at a time */
1794*01826a49SYabin Cui while ((BITv07_reloadDStream(bitDPtr) == BITv07_DStream_unfinished) && (p <= pEnd-4)) {
1795*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_2(p, bitDPtr);
1796*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_1(p, bitDPtr);
1797*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_2(p, bitDPtr);
1798*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_0(p, bitDPtr);
1799*01826a49SYabin Cui }
1800*01826a49SYabin Cui
1801*01826a49SYabin Cui /* closer to the end */
1802*01826a49SYabin Cui while ((BITv07_reloadDStream(bitDPtr) == BITv07_DStream_unfinished) && (p < pEnd))
1803*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_0(p, bitDPtr);
1804*01826a49SYabin Cui
1805*01826a49SYabin Cui /* no more data to retrieve from bitstream, hence no need to reload */
1806*01826a49SYabin Cui while (p < pEnd)
1807*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_0(p, bitDPtr);
1808*01826a49SYabin Cui
1809*01826a49SYabin Cui return pEnd-pStart;
1810*01826a49SYabin Cui }
1811*01826a49SYabin Cui
HUFv07_decompress1X2_usingDTable_internal(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUFv07_DTable * DTable)1812*01826a49SYabin Cui static size_t HUFv07_decompress1X2_usingDTable_internal(
1813*01826a49SYabin Cui void* dst, size_t dstSize,
1814*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1815*01826a49SYabin Cui const HUFv07_DTable* DTable)
1816*01826a49SYabin Cui {
1817*01826a49SYabin Cui BYTE* op = (BYTE*)dst;
1818*01826a49SYabin Cui BYTE* const oend = op + dstSize;
1819*01826a49SYabin Cui const void* dtPtr = DTable + 1;
1820*01826a49SYabin Cui const HUFv07_DEltX2* const dt = (const HUFv07_DEltX2*)dtPtr;
1821*01826a49SYabin Cui BITv07_DStream_t bitD;
1822*01826a49SYabin Cui DTableDesc const dtd = HUFv07_getDTableDesc(DTable);
1823*01826a49SYabin Cui U32 const dtLog = dtd.tableLog;
1824*01826a49SYabin Cui
1825*01826a49SYabin Cui { size_t const errorCode = BITv07_initDStream(&bitD, cSrc, cSrcSize);
1826*01826a49SYabin Cui if (HUFv07_isError(errorCode)) return errorCode; }
1827*01826a49SYabin Cui
1828*01826a49SYabin Cui HUFv07_decodeStreamX2(op, &bitD, oend, dt, dtLog);
1829*01826a49SYabin Cui
1830*01826a49SYabin Cui /* check */
1831*01826a49SYabin Cui if (!BITv07_endOfDStream(&bitD)) return ERROR(corruption_detected);
1832*01826a49SYabin Cui
1833*01826a49SYabin Cui return dstSize;
1834*01826a49SYabin Cui }
1835*01826a49SYabin Cui
HUFv07_decompress1X2_usingDTable(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUFv07_DTable * DTable)1836*01826a49SYabin Cui size_t HUFv07_decompress1X2_usingDTable(
1837*01826a49SYabin Cui void* dst, size_t dstSize,
1838*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1839*01826a49SYabin Cui const HUFv07_DTable* DTable)
1840*01826a49SYabin Cui {
1841*01826a49SYabin Cui DTableDesc dtd = HUFv07_getDTableDesc(DTable);
1842*01826a49SYabin Cui if (dtd.tableType != 0) return ERROR(GENERIC);
1843*01826a49SYabin Cui return HUFv07_decompress1X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
1844*01826a49SYabin Cui }
1845*01826a49SYabin Cui
HUFv07_decompress1X2_DCtx(HUFv07_DTable * DCtx,void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)1846*01826a49SYabin Cui size_t HUFv07_decompress1X2_DCtx (HUFv07_DTable* DCtx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1847*01826a49SYabin Cui {
1848*01826a49SYabin Cui const BYTE* ip = (const BYTE*) cSrc;
1849*01826a49SYabin Cui
1850*01826a49SYabin Cui size_t const hSize = HUFv07_readDTableX2 (DCtx, cSrc, cSrcSize);
1851*01826a49SYabin Cui if (HUFv07_isError(hSize)) return hSize;
1852*01826a49SYabin Cui if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
1853*01826a49SYabin Cui ip += hSize; cSrcSize -= hSize;
1854*01826a49SYabin Cui
1855*01826a49SYabin Cui return HUFv07_decompress1X2_usingDTable_internal (dst, dstSize, ip, cSrcSize, DCtx);
1856*01826a49SYabin Cui }
1857*01826a49SYabin Cui
HUFv07_decompress1X2(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)1858*01826a49SYabin Cui size_t HUFv07_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1859*01826a49SYabin Cui {
1860*01826a49SYabin Cui HUFv07_CREATE_STATIC_DTABLEX2(DTable, HUFv07_TABLELOG_MAX);
1861*01826a49SYabin Cui return HUFv07_decompress1X2_DCtx (DTable, dst, dstSize, cSrc, cSrcSize);
1862*01826a49SYabin Cui }
1863*01826a49SYabin Cui
1864*01826a49SYabin Cui
HUFv07_decompress4X2_usingDTable_internal(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUFv07_DTable * DTable)1865*01826a49SYabin Cui static size_t HUFv07_decompress4X2_usingDTable_internal(
1866*01826a49SYabin Cui void* dst, size_t dstSize,
1867*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1868*01826a49SYabin Cui const HUFv07_DTable* DTable)
1869*01826a49SYabin Cui {
1870*01826a49SYabin Cui /* Check */
1871*01826a49SYabin Cui if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
1872*01826a49SYabin Cui
1873*01826a49SYabin Cui { const BYTE* const istart = (const BYTE*) cSrc;
1874*01826a49SYabin Cui BYTE* const ostart = (BYTE*) dst;
1875*01826a49SYabin Cui BYTE* const oend = ostart + dstSize;
1876*01826a49SYabin Cui const void* const dtPtr = DTable + 1;
1877*01826a49SYabin Cui const HUFv07_DEltX2* const dt = (const HUFv07_DEltX2*)dtPtr;
1878*01826a49SYabin Cui
1879*01826a49SYabin Cui /* Init */
1880*01826a49SYabin Cui BITv07_DStream_t bitD1;
1881*01826a49SYabin Cui BITv07_DStream_t bitD2;
1882*01826a49SYabin Cui BITv07_DStream_t bitD3;
1883*01826a49SYabin Cui BITv07_DStream_t bitD4;
1884*01826a49SYabin Cui size_t const length1 = MEM_readLE16(istart);
1885*01826a49SYabin Cui size_t const length2 = MEM_readLE16(istart+2);
1886*01826a49SYabin Cui size_t const length3 = MEM_readLE16(istart+4);
1887*01826a49SYabin Cui size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
1888*01826a49SYabin Cui const BYTE* const istart1 = istart + 6; /* jumpTable */
1889*01826a49SYabin Cui const BYTE* const istart2 = istart1 + length1;
1890*01826a49SYabin Cui const BYTE* const istart3 = istart2 + length2;
1891*01826a49SYabin Cui const BYTE* const istart4 = istart3 + length3;
1892*01826a49SYabin Cui const size_t segmentSize = (dstSize+3) / 4;
1893*01826a49SYabin Cui BYTE* const opStart2 = ostart + segmentSize;
1894*01826a49SYabin Cui BYTE* const opStart3 = opStart2 + segmentSize;
1895*01826a49SYabin Cui BYTE* const opStart4 = opStart3 + segmentSize;
1896*01826a49SYabin Cui BYTE* op1 = ostart;
1897*01826a49SYabin Cui BYTE* op2 = opStart2;
1898*01826a49SYabin Cui BYTE* op3 = opStart3;
1899*01826a49SYabin Cui BYTE* op4 = opStart4;
1900*01826a49SYabin Cui U32 endSignal;
1901*01826a49SYabin Cui DTableDesc const dtd = HUFv07_getDTableDesc(DTable);
1902*01826a49SYabin Cui U32 const dtLog = dtd.tableLog;
1903*01826a49SYabin Cui
1904*01826a49SYabin Cui if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
1905*01826a49SYabin Cui { size_t const errorCode = BITv07_initDStream(&bitD1, istart1, length1);
1906*01826a49SYabin Cui if (HUFv07_isError(errorCode)) return errorCode; }
1907*01826a49SYabin Cui { size_t const errorCode = BITv07_initDStream(&bitD2, istart2, length2);
1908*01826a49SYabin Cui if (HUFv07_isError(errorCode)) return errorCode; }
1909*01826a49SYabin Cui { size_t const errorCode = BITv07_initDStream(&bitD3, istart3, length3);
1910*01826a49SYabin Cui if (HUFv07_isError(errorCode)) return errorCode; }
1911*01826a49SYabin Cui { size_t const errorCode = BITv07_initDStream(&bitD4, istart4, length4);
1912*01826a49SYabin Cui if (HUFv07_isError(errorCode)) return errorCode; }
1913*01826a49SYabin Cui
1914*01826a49SYabin Cui /* 16-32 symbols per loop (4-8 symbols per stream) */
1915*01826a49SYabin Cui endSignal = BITv07_reloadDStream(&bitD1) | BITv07_reloadDStream(&bitD2) | BITv07_reloadDStream(&bitD3) | BITv07_reloadDStream(&bitD4);
1916*01826a49SYabin Cui for ( ; (endSignal==BITv07_DStream_unfinished) && (op4<(oend-7)) ; ) {
1917*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_2(op1, &bitD1);
1918*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_2(op2, &bitD2);
1919*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_2(op3, &bitD3);
1920*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_2(op4, &bitD4);
1921*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_1(op1, &bitD1);
1922*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_1(op2, &bitD2);
1923*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_1(op3, &bitD3);
1924*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_1(op4, &bitD4);
1925*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_2(op1, &bitD1);
1926*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_2(op2, &bitD2);
1927*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_2(op3, &bitD3);
1928*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_2(op4, &bitD4);
1929*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_0(op1, &bitD1);
1930*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_0(op2, &bitD2);
1931*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_0(op3, &bitD3);
1932*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX2_0(op4, &bitD4);
1933*01826a49SYabin Cui endSignal = BITv07_reloadDStream(&bitD1) | BITv07_reloadDStream(&bitD2) | BITv07_reloadDStream(&bitD3) | BITv07_reloadDStream(&bitD4);
1934*01826a49SYabin Cui }
1935*01826a49SYabin Cui
1936*01826a49SYabin Cui /* check corruption */
1937*01826a49SYabin Cui if (op1 > opStart2) return ERROR(corruption_detected);
1938*01826a49SYabin Cui if (op2 > opStart3) return ERROR(corruption_detected);
1939*01826a49SYabin Cui if (op3 > opStart4) return ERROR(corruption_detected);
1940*01826a49SYabin Cui /* note : op4 supposed already verified within main loop */
1941*01826a49SYabin Cui
1942*01826a49SYabin Cui /* finish bitStreams one by one */
1943*01826a49SYabin Cui HUFv07_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
1944*01826a49SYabin Cui HUFv07_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
1945*01826a49SYabin Cui HUFv07_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
1946*01826a49SYabin Cui HUFv07_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
1947*01826a49SYabin Cui
1948*01826a49SYabin Cui /* check */
1949*01826a49SYabin Cui endSignal = BITv07_endOfDStream(&bitD1) & BITv07_endOfDStream(&bitD2) & BITv07_endOfDStream(&bitD3) & BITv07_endOfDStream(&bitD4);
1950*01826a49SYabin Cui if (!endSignal) return ERROR(corruption_detected);
1951*01826a49SYabin Cui
1952*01826a49SYabin Cui /* decoded size */
1953*01826a49SYabin Cui return dstSize;
1954*01826a49SYabin Cui }
1955*01826a49SYabin Cui }
1956*01826a49SYabin Cui
1957*01826a49SYabin Cui
HUFv07_decompress4X2_usingDTable(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUFv07_DTable * DTable)1958*01826a49SYabin Cui size_t HUFv07_decompress4X2_usingDTable(
1959*01826a49SYabin Cui void* dst, size_t dstSize,
1960*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
1961*01826a49SYabin Cui const HUFv07_DTable* DTable)
1962*01826a49SYabin Cui {
1963*01826a49SYabin Cui DTableDesc dtd = HUFv07_getDTableDesc(DTable);
1964*01826a49SYabin Cui if (dtd.tableType != 0) return ERROR(GENERIC);
1965*01826a49SYabin Cui return HUFv07_decompress4X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
1966*01826a49SYabin Cui }
1967*01826a49SYabin Cui
1968*01826a49SYabin Cui
HUFv07_decompress4X2_DCtx(HUFv07_DTable * dctx,void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)1969*01826a49SYabin Cui size_t HUFv07_decompress4X2_DCtx (HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1970*01826a49SYabin Cui {
1971*01826a49SYabin Cui const BYTE* ip = (const BYTE*) cSrc;
1972*01826a49SYabin Cui
1973*01826a49SYabin Cui size_t const hSize = HUFv07_readDTableX2 (dctx, cSrc, cSrcSize);
1974*01826a49SYabin Cui if (HUFv07_isError(hSize)) return hSize;
1975*01826a49SYabin Cui if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
1976*01826a49SYabin Cui ip += hSize; cSrcSize -= hSize;
1977*01826a49SYabin Cui
1978*01826a49SYabin Cui return HUFv07_decompress4X2_usingDTable_internal (dst, dstSize, ip, cSrcSize, dctx);
1979*01826a49SYabin Cui }
1980*01826a49SYabin Cui
HUFv07_decompress4X2(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)1981*01826a49SYabin Cui size_t HUFv07_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1982*01826a49SYabin Cui {
1983*01826a49SYabin Cui HUFv07_CREATE_STATIC_DTABLEX2(DTable, HUFv07_TABLELOG_MAX);
1984*01826a49SYabin Cui return HUFv07_decompress4X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
1985*01826a49SYabin Cui }
1986*01826a49SYabin Cui
1987*01826a49SYabin Cui
1988*01826a49SYabin Cui /* *************************/
1989*01826a49SYabin Cui /* double-symbols decoding */
1990*01826a49SYabin Cui /* *************************/
1991*01826a49SYabin Cui typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUFv07_DEltX4; /* double-symbols decoding */
1992*01826a49SYabin Cui
1993*01826a49SYabin Cui typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t;
1994*01826a49SYabin Cui
HUFv07_fillDTableX4Level2(HUFv07_DEltX4 * DTable,U32 sizeLog,const U32 consumed,const U32 * rankValOrigin,const int minWeight,const sortedSymbol_t * sortedSymbols,const U32 sortedListSize,U32 nbBitsBaseline,U16 baseSeq)1995*01826a49SYabin Cui static void HUFv07_fillDTableX4Level2(HUFv07_DEltX4* DTable, U32 sizeLog, const U32 consumed,
1996*01826a49SYabin Cui const U32* rankValOrigin, const int minWeight,
1997*01826a49SYabin Cui const sortedSymbol_t* sortedSymbols, const U32 sortedListSize,
1998*01826a49SYabin Cui U32 nbBitsBaseline, U16 baseSeq)
1999*01826a49SYabin Cui {
2000*01826a49SYabin Cui HUFv07_DEltX4 DElt;
2001*01826a49SYabin Cui U32 rankVal[HUFv07_TABLELOG_ABSOLUTEMAX + 1];
2002*01826a49SYabin Cui
2003*01826a49SYabin Cui /* get pre-calculated rankVal */
2004*01826a49SYabin Cui memcpy(rankVal, rankValOrigin, sizeof(rankVal));
2005*01826a49SYabin Cui
2006*01826a49SYabin Cui /* fill skipped values */
2007*01826a49SYabin Cui if (minWeight>1) {
2008*01826a49SYabin Cui U32 i, skipSize = rankVal[minWeight];
2009*01826a49SYabin Cui MEM_writeLE16(&(DElt.sequence), baseSeq);
2010*01826a49SYabin Cui DElt.nbBits = (BYTE)(consumed);
2011*01826a49SYabin Cui DElt.length = 1;
2012*01826a49SYabin Cui for (i = 0; i < skipSize; i++)
2013*01826a49SYabin Cui DTable[i] = DElt;
2014*01826a49SYabin Cui }
2015*01826a49SYabin Cui
2016*01826a49SYabin Cui /* fill DTable */
2017*01826a49SYabin Cui { U32 s; for (s=0; s<sortedListSize; s++) { /* note : sortedSymbols already skipped */
2018*01826a49SYabin Cui const U32 symbol = sortedSymbols[s].symbol;
2019*01826a49SYabin Cui const U32 weight = sortedSymbols[s].weight;
2020*01826a49SYabin Cui const U32 nbBits = nbBitsBaseline - weight;
2021*01826a49SYabin Cui const U32 length = 1 << (sizeLog-nbBits);
2022*01826a49SYabin Cui const U32 start = rankVal[weight];
2023*01826a49SYabin Cui U32 i = start;
2024*01826a49SYabin Cui const U32 end = start + length;
2025*01826a49SYabin Cui
2026*01826a49SYabin Cui MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8)));
2027*01826a49SYabin Cui DElt.nbBits = (BYTE)(nbBits + consumed);
2028*01826a49SYabin Cui DElt.length = 2;
2029*01826a49SYabin Cui do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */
2030*01826a49SYabin Cui
2031*01826a49SYabin Cui rankVal[weight] += length;
2032*01826a49SYabin Cui }}
2033*01826a49SYabin Cui }
2034*01826a49SYabin Cui
2035*01826a49SYabin Cui typedef U32 rankVal_t[HUFv07_TABLELOG_ABSOLUTEMAX][HUFv07_TABLELOG_ABSOLUTEMAX + 1];
2036*01826a49SYabin Cui
HUFv07_fillDTableX4(HUFv07_DEltX4 * DTable,const U32 targetLog,const sortedSymbol_t * sortedList,const U32 sortedListSize,const U32 * rankStart,rankVal_t rankValOrigin,const U32 maxWeight,const U32 nbBitsBaseline)2037*01826a49SYabin Cui static void HUFv07_fillDTableX4(HUFv07_DEltX4* DTable, const U32 targetLog,
2038*01826a49SYabin Cui const sortedSymbol_t* sortedList, const U32 sortedListSize,
2039*01826a49SYabin Cui const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight,
2040*01826a49SYabin Cui const U32 nbBitsBaseline)
2041*01826a49SYabin Cui {
2042*01826a49SYabin Cui U32 rankVal[HUFv07_TABLELOG_ABSOLUTEMAX + 1];
2043*01826a49SYabin Cui const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */
2044*01826a49SYabin Cui const U32 minBits = nbBitsBaseline - maxWeight;
2045*01826a49SYabin Cui U32 s;
2046*01826a49SYabin Cui
2047*01826a49SYabin Cui memcpy(rankVal, rankValOrigin, sizeof(rankVal));
2048*01826a49SYabin Cui
2049*01826a49SYabin Cui /* fill DTable */
2050*01826a49SYabin Cui for (s=0; s<sortedListSize; s++) {
2051*01826a49SYabin Cui const U16 symbol = sortedList[s].symbol;
2052*01826a49SYabin Cui const U32 weight = sortedList[s].weight;
2053*01826a49SYabin Cui const U32 nbBits = nbBitsBaseline - weight;
2054*01826a49SYabin Cui const U32 start = rankVal[weight];
2055*01826a49SYabin Cui const U32 length = 1 << (targetLog-nbBits);
2056*01826a49SYabin Cui
2057*01826a49SYabin Cui if (targetLog-nbBits >= minBits) { /* enough room for a second symbol */
2058*01826a49SYabin Cui U32 sortedRank;
2059*01826a49SYabin Cui int minWeight = nbBits + scaleLog;
2060*01826a49SYabin Cui if (minWeight < 1) minWeight = 1;
2061*01826a49SYabin Cui sortedRank = rankStart[minWeight];
2062*01826a49SYabin Cui HUFv07_fillDTableX4Level2(DTable+start, targetLog-nbBits, nbBits,
2063*01826a49SYabin Cui rankValOrigin[nbBits], minWeight,
2064*01826a49SYabin Cui sortedList+sortedRank, sortedListSize-sortedRank,
2065*01826a49SYabin Cui nbBitsBaseline, symbol);
2066*01826a49SYabin Cui } else {
2067*01826a49SYabin Cui HUFv07_DEltX4 DElt;
2068*01826a49SYabin Cui MEM_writeLE16(&(DElt.sequence), symbol);
2069*01826a49SYabin Cui DElt.nbBits = (BYTE)(nbBits);
2070*01826a49SYabin Cui DElt.length = 1;
2071*01826a49SYabin Cui { U32 u;
2072*01826a49SYabin Cui const U32 end = start + length;
2073*01826a49SYabin Cui for (u = start; u < end; u++) DTable[u] = DElt;
2074*01826a49SYabin Cui } }
2075*01826a49SYabin Cui rankVal[weight] += length;
2076*01826a49SYabin Cui }
2077*01826a49SYabin Cui }
2078*01826a49SYabin Cui
HUFv07_readDTableX4(HUFv07_DTable * DTable,const void * src,size_t srcSize)2079*01826a49SYabin Cui size_t HUFv07_readDTableX4 (HUFv07_DTable* DTable, const void* src, size_t srcSize)
2080*01826a49SYabin Cui {
2081*01826a49SYabin Cui BYTE weightList[HUFv07_SYMBOLVALUE_MAX + 1];
2082*01826a49SYabin Cui sortedSymbol_t sortedSymbol[HUFv07_SYMBOLVALUE_MAX + 1];
2083*01826a49SYabin Cui U32 rankStats[HUFv07_TABLELOG_ABSOLUTEMAX + 1] = { 0 };
2084*01826a49SYabin Cui U32 rankStart0[HUFv07_TABLELOG_ABSOLUTEMAX + 2] = { 0 };
2085*01826a49SYabin Cui U32* const rankStart = rankStart0+1;
2086*01826a49SYabin Cui rankVal_t rankVal;
2087*01826a49SYabin Cui U32 tableLog, maxW, sizeOfSort, nbSymbols;
2088*01826a49SYabin Cui DTableDesc dtd = HUFv07_getDTableDesc(DTable);
2089*01826a49SYabin Cui U32 const maxTableLog = dtd.maxTableLog;
2090*01826a49SYabin Cui size_t iSize;
2091*01826a49SYabin Cui void* dtPtr = DTable+1; /* force compiler to avoid strict-aliasing */
2092*01826a49SYabin Cui HUFv07_DEltX4* const dt = (HUFv07_DEltX4*)dtPtr;
2093*01826a49SYabin Cui
2094*01826a49SYabin Cui HUFv07_STATIC_ASSERT(sizeof(HUFv07_DEltX4) == sizeof(HUFv07_DTable)); /* if compilation fails here, assertion is false */
2095*01826a49SYabin Cui if (maxTableLog > HUFv07_TABLELOG_ABSOLUTEMAX) return ERROR(tableLog_tooLarge);
2096*01826a49SYabin Cui /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
2097*01826a49SYabin Cui
2098*01826a49SYabin Cui iSize = HUFv07_readStats(weightList, HUFv07_SYMBOLVALUE_MAX + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2099*01826a49SYabin Cui if (HUFv07_isError(iSize)) return iSize;
2100*01826a49SYabin Cui
2101*01826a49SYabin Cui /* check result */
2102*01826a49SYabin Cui if (tableLog > maxTableLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */
2103*01826a49SYabin Cui
2104*01826a49SYabin Cui /* find maxWeight */
2105*01826a49SYabin Cui for (maxW = tableLog; rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */
2106*01826a49SYabin Cui
2107*01826a49SYabin Cui /* Get start index of each weight */
2108*01826a49SYabin Cui { U32 w, nextRankStart = 0;
2109*01826a49SYabin Cui for (w=1; w<maxW+1; w++) {
2110*01826a49SYabin Cui U32 current = nextRankStart;
2111*01826a49SYabin Cui nextRankStart += rankStats[w];
2112*01826a49SYabin Cui rankStart[w] = current;
2113*01826a49SYabin Cui }
2114*01826a49SYabin Cui rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
2115*01826a49SYabin Cui sizeOfSort = nextRankStart;
2116*01826a49SYabin Cui }
2117*01826a49SYabin Cui
2118*01826a49SYabin Cui /* sort symbols by weight */
2119*01826a49SYabin Cui { U32 s;
2120*01826a49SYabin Cui for (s=0; s<nbSymbols; s++) {
2121*01826a49SYabin Cui U32 const w = weightList[s];
2122*01826a49SYabin Cui U32 const r = rankStart[w]++;
2123*01826a49SYabin Cui sortedSymbol[r].symbol = (BYTE)s;
2124*01826a49SYabin Cui sortedSymbol[r].weight = (BYTE)w;
2125*01826a49SYabin Cui }
2126*01826a49SYabin Cui rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
2127*01826a49SYabin Cui }
2128*01826a49SYabin Cui
2129*01826a49SYabin Cui /* Build rankVal */
2130*01826a49SYabin Cui { U32* const rankVal0 = rankVal[0];
2131*01826a49SYabin Cui { int const rescale = (maxTableLog-tableLog) - 1; /* tableLog <= maxTableLog */
2132*01826a49SYabin Cui U32 nextRankVal = 0;
2133*01826a49SYabin Cui U32 w;
2134*01826a49SYabin Cui for (w=1; w<maxW+1; w++) {
2135*01826a49SYabin Cui U32 current = nextRankVal;
2136*01826a49SYabin Cui nextRankVal += rankStats[w] << (w+rescale);
2137*01826a49SYabin Cui rankVal0[w] = current;
2138*01826a49SYabin Cui } }
2139*01826a49SYabin Cui { U32 const minBits = tableLog+1 - maxW;
2140*01826a49SYabin Cui U32 consumed;
2141*01826a49SYabin Cui for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) {
2142*01826a49SYabin Cui U32* const rankValPtr = rankVal[consumed];
2143*01826a49SYabin Cui U32 w;
2144*01826a49SYabin Cui for (w = 1; w < maxW+1; w++) {
2145*01826a49SYabin Cui rankValPtr[w] = rankVal0[w] >> consumed;
2146*01826a49SYabin Cui } } } }
2147*01826a49SYabin Cui
2148*01826a49SYabin Cui HUFv07_fillDTableX4(dt, maxTableLog,
2149*01826a49SYabin Cui sortedSymbol, sizeOfSort,
2150*01826a49SYabin Cui rankStart0, rankVal, maxW,
2151*01826a49SYabin Cui tableLog+1);
2152*01826a49SYabin Cui
2153*01826a49SYabin Cui dtd.tableLog = (BYTE)maxTableLog;
2154*01826a49SYabin Cui dtd.tableType = 1;
2155*01826a49SYabin Cui memcpy(DTable, &dtd, sizeof(dtd));
2156*01826a49SYabin Cui return iSize;
2157*01826a49SYabin Cui }
2158*01826a49SYabin Cui
2159*01826a49SYabin Cui
HUFv07_decodeSymbolX4(void * op,BITv07_DStream_t * DStream,const HUFv07_DEltX4 * dt,const U32 dtLog)2160*01826a49SYabin Cui static U32 HUFv07_decodeSymbolX4(void* op, BITv07_DStream_t* DStream, const HUFv07_DEltX4* dt, const U32 dtLog)
2161*01826a49SYabin Cui {
2162*01826a49SYabin Cui const size_t val = BITv07_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2163*01826a49SYabin Cui memcpy(op, dt+val, 2);
2164*01826a49SYabin Cui BITv07_skipBits(DStream, dt[val].nbBits);
2165*01826a49SYabin Cui return dt[val].length;
2166*01826a49SYabin Cui }
2167*01826a49SYabin Cui
HUFv07_decodeLastSymbolX4(void * op,BITv07_DStream_t * DStream,const HUFv07_DEltX4 * dt,const U32 dtLog)2168*01826a49SYabin Cui static U32 HUFv07_decodeLastSymbolX4(void* op, BITv07_DStream_t* DStream, const HUFv07_DEltX4* dt, const U32 dtLog)
2169*01826a49SYabin Cui {
2170*01826a49SYabin Cui const size_t val = BITv07_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
2171*01826a49SYabin Cui memcpy(op, dt+val, 1);
2172*01826a49SYabin Cui if (dt[val].length==1) BITv07_skipBits(DStream, dt[val].nbBits);
2173*01826a49SYabin Cui else {
2174*01826a49SYabin Cui if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8)) {
2175*01826a49SYabin Cui BITv07_skipBits(DStream, dt[val].nbBits);
2176*01826a49SYabin Cui if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8))
2177*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 */
2178*01826a49SYabin Cui } }
2179*01826a49SYabin Cui return 1;
2180*01826a49SYabin Cui }
2181*01826a49SYabin Cui
2182*01826a49SYabin Cui
2183*01826a49SYabin Cui #define HUFv07_DECODE_SYMBOLX4_0(ptr, DStreamPtr) \
2184*01826a49SYabin Cui ptr += HUFv07_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2185*01826a49SYabin Cui
2186*01826a49SYabin Cui #define HUFv07_DECODE_SYMBOLX4_1(ptr, DStreamPtr) \
2187*01826a49SYabin Cui if (MEM_64bits() || (HUFv07_TABLELOG_MAX<=12)) \
2188*01826a49SYabin Cui ptr += HUFv07_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2189*01826a49SYabin Cui
2190*01826a49SYabin Cui #define HUFv07_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \
2191*01826a49SYabin Cui if (MEM_64bits()) \
2192*01826a49SYabin Cui ptr += HUFv07_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog)
2193*01826a49SYabin Cui
HUFv07_decodeStreamX4(BYTE * p,BITv07_DStream_t * bitDPtr,BYTE * const pEnd,const HUFv07_DEltX4 * const dt,const U32 dtLog)2194*01826a49SYabin Cui static inline size_t HUFv07_decodeStreamX4(BYTE* p, BITv07_DStream_t* bitDPtr, BYTE* const pEnd, const HUFv07_DEltX4* const dt, const U32 dtLog)
2195*01826a49SYabin Cui {
2196*01826a49SYabin Cui BYTE* const pStart = p;
2197*01826a49SYabin Cui
2198*01826a49SYabin Cui /* up to 8 symbols at a time */
2199*01826a49SYabin Cui while ((BITv07_reloadDStream(bitDPtr) == BITv07_DStream_unfinished) && (p < pEnd-7)) {
2200*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_2(p, bitDPtr);
2201*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_1(p, bitDPtr);
2202*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_2(p, bitDPtr);
2203*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_0(p, bitDPtr);
2204*01826a49SYabin Cui }
2205*01826a49SYabin Cui
2206*01826a49SYabin Cui /* closer to end : up to 2 symbols at a time */
2207*01826a49SYabin Cui while ((BITv07_reloadDStream(bitDPtr) == BITv07_DStream_unfinished) && (p <= pEnd-2))
2208*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_0(p, bitDPtr);
2209*01826a49SYabin Cui
2210*01826a49SYabin Cui while (p <= pEnd-2)
2211*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_0(p, bitDPtr); /* no need to reload : reached the end of DStream */
2212*01826a49SYabin Cui
2213*01826a49SYabin Cui if (p < pEnd)
2214*01826a49SYabin Cui p += HUFv07_decodeLastSymbolX4(p, bitDPtr, dt, dtLog);
2215*01826a49SYabin Cui
2216*01826a49SYabin Cui return p-pStart;
2217*01826a49SYabin Cui }
2218*01826a49SYabin Cui
2219*01826a49SYabin Cui
HUFv07_decompress1X4_usingDTable_internal(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUFv07_DTable * DTable)2220*01826a49SYabin Cui static size_t HUFv07_decompress1X4_usingDTable_internal(
2221*01826a49SYabin Cui void* dst, size_t dstSize,
2222*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
2223*01826a49SYabin Cui const HUFv07_DTable* DTable)
2224*01826a49SYabin Cui {
2225*01826a49SYabin Cui BITv07_DStream_t bitD;
2226*01826a49SYabin Cui
2227*01826a49SYabin Cui /* Init */
2228*01826a49SYabin Cui { size_t const errorCode = BITv07_initDStream(&bitD, cSrc, cSrcSize);
2229*01826a49SYabin Cui if (HUFv07_isError(errorCode)) return errorCode;
2230*01826a49SYabin Cui }
2231*01826a49SYabin Cui
2232*01826a49SYabin Cui /* decode */
2233*01826a49SYabin Cui { BYTE* const ostart = (BYTE*) dst;
2234*01826a49SYabin Cui BYTE* const oend = ostart + dstSize;
2235*01826a49SYabin Cui const void* const dtPtr = DTable+1; /* force compiler to not use strict-aliasing */
2236*01826a49SYabin Cui const HUFv07_DEltX4* const dt = (const HUFv07_DEltX4*)dtPtr;
2237*01826a49SYabin Cui DTableDesc const dtd = HUFv07_getDTableDesc(DTable);
2238*01826a49SYabin Cui HUFv07_decodeStreamX4(ostart, &bitD, oend, dt, dtd.tableLog);
2239*01826a49SYabin Cui }
2240*01826a49SYabin Cui
2241*01826a49SYabin Cui /* check */
2242*01826a49SYabin Cui if (!BITv07_endOfDStream(&bitD)) return ERROR(corruption_detected);
2243*01826a49SYabin Cui
2244*01826a49SYabin Cui /* decoded size */
2245*01826a49SYabin Cui return dstSize;
2246*01826a49SYabin Cui }
2247*01826a49SYabin Cui
HUFv07_decompress1X4_usingDTable(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUFv07_DTable * DTable)2248*01826a49SYabin Cui size_t HUFv07_decompress1X4_usingDTable(
2249*01826a49SYabin Cui void* dst, size_t dstSize,
2250*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
2251*01826a49SYabin Cui const HUFv07_DTable* DTable)
2252*01826a49SYabin Cui {
2253*01826a49SYabin Cui DTableDesc dtd = HUFv07_getDTableDesc(DTable);
2254*01826a49SYabin Cui if (dtd.tableType != 1) return ERROR(GENERIC);
2255*01826a49SYabin Cui return HUFv07_decompress1X4_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
2256*01826a49SYabin Cui }
2257*01826a49SYabin Cui
HUFv07_decompress1X4_DCtx(HUFv07_DTable * DCtx,void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)2258*01826a49SYabin Cui size_t HUFv07_decompress1X4_DCtx (HUFv07_DTable* DCtx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2259*01826a49SYabin Cui {
2260*01826a49SYabin Cui const BYTE* ip = (const BYTE*) cSrc;
2261*01826a49SYabin Cui
2262*01826a49SYabin Cui size_t const hSize = HUFv07_readDTableX4 (DCtx, cSrc, cSrcSize);
2263*01826a49SYabin Cui if (HUFv07_isError(hSize)) return hSize;
2264*01826a49SYabin Cui if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
2265*01826a49SYabin Cui ip += hSize; cSrcSize -= hSize;
2266*01826a49SYabin Cui
2267*01826a49SYabin Cui return HUFv07_decompress1X4_usingDTable_internal (dst, dstSize, ip, cSrcSize, DCtx);
2268*01826a49SYabin Cui }
2269*01826a49SYabin Cui
HUFv07_decompress1X4(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)2270*01826a49SYabin Cui size_t HUFv07_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2271*01826a49SYabin Cui {
2272*01826a49SYabin Cui HUFv07_CREATE_STATIC_DTABLEX4(DTable, HUFv07_TABLELOG_MAX);
2273*01826a49SYabin Cui return HUFv07_decompress1X4_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
2274*01826a49SYabin Cui }
2275*01826a49SYabin Cui
HUFv07_decompress4X4_usingDTable_internal(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUFv07_DTable * DTable)2276*01826a49SYabin Cui static size_t HUFv07_decompress4X4_usingDTable_internal(
2277*01826a49SYabin Cui void* dst, size_t dstSize,
2278*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
2279*01826a49SYabin Cui const HUFv07_DTable* DTable)
2280*01826a49SYabin Cui {
2281*01826a49SYabin Cui if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
2282*01826a49SYabin Cui
2283*01826a49SYabin Cui { const BYTE* const istart = (const BYTE*) cSrc;
2284*01826a49SYabin Cui BYTE* const ostart = (BYTE*) dst;
2285*01826a49SYabin Cui BYTE* const oend = ostart + dstSize;
2286*01826a49SYabin Cui const void* const dtPtr = DTable+1;
2287*01826a49SYabin Cui const HUFv07_DEltX4* const dt = (const HUFv07_DEltX4*)dtPtr;
2288*01826a49SYabin Cui
2289*01826a49SYabin Cui /* Init */
2290*01826a49SYabin Cui BITv07_DStream_t bitD1;
2291*01826a49SYabin Cui BITv07_DStream_t bitD2;
2292*01826a49SYabin Cui BITv07_DStream_t bitD3;
2293*01826a49SYabin Cui BITv07_DStream_t bitD4;
2294*01826a49SYabin Cui size_t const length1 = MEM_readLE16(istart);
2295*01826a49SYabin Cui size_t const length2 = MEM_readLE16(istart+2);
2296*01826a49SYabin Cui size_t const length3 = MEM_readLE16(istart+4);
2297*01826a49SYabin Cui size_t const length4 = cSrcSize - (length1 + length2 + length3 + 6);
2298*01826a49SYabin Cui const BYTE* const istart1 = istart + 6; /* jumpTable */
2299*01826a49SYabin Cui const BYTE* const istart2 = istart1 + length1;
2300*01826a49SYabin Cui const BYTE* const istart3 = istart2 + length2;
2301*01826a49SYabin Cui const BYTE* const istart4 = istart3 + length3;
2302*01826a49SYabin Cui size_t const segmentSize = (dstSize+3) / 4;
2303*01826a49SYabin Cui BYTE* const opStart2 = ostart + segmentSize;
2304*01826a49SYabin Cui BYTE* const opStart3 = opStart2 + segmentSize;
2305*01826a49SYabin Cui BYTE* const opStart4 = opStart3 + segmentSize;
2306*01826a49SYabin Cui BYTE* op1 = ostart;
2307*01826a49SYabin Cui BYTE* op2 = opStart2;
2308*01826a49SYabin Cui BYTE* op3 = opStart3;
2309*01826a49SYabin Cui BYTE* op4 = opStart4;
2310*01826a49SYabin Cui U32 endSignal;
2311*01826a49SYabin Cui DTableDesc const dtd = HUFv07_getDTableDesc(DTable);
2312*01826a49SYabin Cui U32 const dtLog = dtd.tableLog;
2313*01826a49SYabin Cui
2314*01826a49SYabin Cui if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
2315*01826a49SYabin Cui { size_t const errorCode = BITv07_initDStream(&bitD1, istart1, length1);
2316*01826a49SYabin Cui if (HUFv07_isError(errorCode)) return errorCode; }
2317*01826a49SYabin Cui { size_t const errorCode = BITv07_initDStream(&bitD2, istart2, length2);
2318*01826a49SYabin Cui if (HUFv07_isError(errorCode)) return errorCode; }
2319*01826a49SYabin Cui { size_t const errorCode = BITv07_initDStream(&bitD3, istart3, length3);
2320*01826a49SYabin Cui if (HUFv07_isError(errorCode)) return errorCode; }
2321*01826a49SYabin Cui { size_t const errorCode = BITv07_initDStream(&bitD4, istart4, length4);
2322*01826a49SYabin Cui if (HUFv07_isError(errorCode)) return errorCode; }
2323*01826a49SYabin Cui
2324*01826a49SYabin Cui /* 16-32 symbols per loop (4-8 symbols per stream) */
2325*01826a49SYabin Cui endSignal = BITv07_reloadDStream(&bitD1) | BITv07_reloadDStream(&bitD2) | BITv07_reloadDStream(&bitD3) | BITv07_reloadDStream(&bitD4);
2326*01826a49SYabin Cui for ( ; (endSignal==BITv07_DStream_unfinished) && (op4<(oend-7)) ; ) {
2327*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_2(op1, &bitD1);
2328*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_2(op2, &bitD2);
2329*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_2(op3, &bitD3);
2330*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_2(op4, &bitD4);
2331*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_1(op1, &bitD1);
2332*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_1(op2, &bitD2);
2333*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_1(op3, &bitD3);
2334*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_1(op4, &bitD4);
2335*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_2(op1, &bitD1);
2336*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_2(op2, &bitD2);
2337*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_2(op3, &bitD3);
2338*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_2(op4, &bitD4);
2339*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_0(op1, &bitD1);
2340*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_0(op2, &bitD2);
2341*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_0(op3, &bitD3);
2342*01826a49SYabin Cui HUFv07_DECODE_SYMBOLX4_0(op4, &bitD4);
2343*01826a49SYabin Cui
2344*01826a49SYabin Cui endSignal = BITv07_reloadDStream(&bitD1) | BITv07_reloadDStream(&bitD2) | BITv07_reloadDStream(&bitD3) | BITv07_reloadDStream(&bitD4);
2345*01826a49SYabin Cui }
2346*01826a49SYabin Cui
2347*01826a49SYabin Cui /* check corruption */
2348*01826a49SYabin Cui if (op1 > opStart2) return ERROR(corruption_detected);
2349*01826a49SYabin Cui if (op2 > opStart3) return ERROR(corruption_detected);
2350*01826a49SYabin Cui if (op3 > opStart4) return ERROR(corruption_detected);
2351*01826a49SYabin Cui /* note : op4 supposed already verified within main loop */
2352*01826a49SYabin Cui
2353*01826a49SYabin Cui /* finish bitStreams one by one */
2354*01826a49SYabin Cui HUFv07_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog);
2355*01826a49SYabin Cui HUFv07_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog);
2356*01826a49SYabin Cui HUFv07_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog);
2357*01826a49SYabin Cui HUFv07_decodeStreamX4(op4, &bitD4, oend, dt, dtLog);
2358*01826a49SYabin Cui
2359*01826a49SYabin Cui /* check */
2360*01826a49SYabin Cui { U32 const endCheck = BITv07_endOfDStream(&bitD1) & BITv07_endOfDStream(&bitD2) & BITv07_endOfDStream(&bitD3) & BITv07_endOfDStream(&bitD4);
2361*01826a49SYabin Cui if (!endCheck) return ERROR(corruption_detected); }
2362*01826a49SYabin Cui
2363*01826a49SYabin Cui /* decoded size */
2364*01826a49SYabin Cui return dstSize;
2365*01826a49SYabin Cui }
2366*01826a49SYabin Cui }
2367*01826a49SYabin Cui
2368*01826a49SYabin Cui
HUFv07_decompress4X4_usingDTable(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize,const HUFv07_DTable * DTable)2369*01826a49SYabin Cui size_t HUFv07_decompress4X4_usingDTable(
2370*01826a49SYabin Cui void* dst, size_t dstSize,
2371*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
2372*01826a49SYabin Cui const HUFv07_DTable* DTable)
2373*01826a49SYabin Cui {
2374*01826a49SYabin Cui DTableDesc dtd = HUFv07_getDTableDesc(DTable);
2375*01826a49SYabin Cui if (dtd.tableType != 1) return ERROR(GENERIC);
2376*01826a49SYabin Cui return HUFv07_decompress4X4_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable);
2377*01826a49SYabin Cui }
2378*01826a49SYabin Cui
2379*01826a49SYabin Cui
HUFv07_decompress4X4_DCtx(HUFv07_DTable * dctx,void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)2380*01826a49SYabin Cui size_t HUFv07_decompress4X4_DCtx (HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2381*01826a49SYabin Cui {
2382*01826a49SYabin Cui const BYTE* ip = (const BYTE*) cSrc;
2383*01826a49SYabin Cui
2384*01826a49SYabin Cui size_t hSize = HUFv07_readDTableX4 (dctx, cSrc, cSrcSize);
2385*01826a49SYabin Cui if (HUFv07_isError(hSize)) return hSize;
2386*01826a49SYabin Cui if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
2387*01826a49SYabin Cui ip += hSize; cSrcSize -= hSize;
2388*01826a49SYabin Cui
2389*01826a49SYabin Cui return HUFv07_decompress4X4_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx);
2390*01826a49SYabin Cui }
2391*01826a49SYabin Cui
HUFv07_decompress4X4(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)2392*01826a49SYabin Cui size_t HUFv07_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2393*01826a49SYabin Cui {
2394*01826a49SYabin Cui HUFv07_CREATE_STATIC_DTABLEX4(DTable, HUFv07_TABLELOG_MAX);
2395*01826a49SYabin Cui return HUFv07_decompress4X4_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
2396*01826a49SYabin Cui }
2397*01826a49SYabin Cui
2398*01826a49SYabin Cui
2399*01826a49SYabin Cui /* ********************************/
2400*01826a49SYabin Cui /* Generic decompression selector */
2401*01826a49SYabin Cui /* ********************************/
2402*01826a49SYabin Cui
HUFv07_decompress1X_usingDTable(void * dst,size_t maxDstSize,const void * cSrc,size_t cSrcSize,const HUFv07_DTable * DTable)2403*01826a49SYabin Cui size_t HUFv07_decompress1X_usingDTable(void* dst, size_t maxDstSize,
2404*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
2405*01826a49SYabin Cui const HUFv07_DTable* DTable)
2406*01826a49SYabin Cui {
2407*01826a49SYabin Cui DTableDesc const dtd = HUFv07_getDTableDesc(DTable);
2408*01826a49SYabin Cui return dtd.tableType ? HUFv07_decompress1X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable) :
2409*01826a49SYabin Cui HUFv07_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable);
2410*01826a49SYabin Cui }
2411*01826a49SYabin Cui
HUFv07_decompress4X_usingDTable(void * dst,size_t maxDstSize,const void * cSrc,size_t cSrcSize,const HUFv07_DTable * DTable)2412*01826a49SYabin Cui size_t HUFv07_decompress4X_usingDTable(void* dst, size_t maxDstSize,
2413*01826a49SYabin Cui const void* cSrc, size_t cSrcSize,
2414*01826a49SYabin Cui const HUFv07_DTable* DTable)
2415*01826a49SYabin Cui {
2416*01826a49SYabin Cui DTableDesc const dtd = HUFv07_getDTableDesc(DTable);
2417*01826a49SYabin Cui return dtd.tableType ? HUFv07_decompress4X4_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable) :
2418*01826a49SYabin Cui HUFv07_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable);
2419*01826a49SYabin Cui }
2420*01826a49SYabin Cui
2421*01826a49SYabin Cui
2422*01826a49SYabin Cui typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t;
2423*01826a49SYabin Cui static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] =
2424*01826a49SYabin Cui {
2425*01826a49SYabin Cui /* single, double, quad */
2426*01826a49SYabin Cui {{0,0}, {1,1}, {2,2}}, /* Q==0 : impossible */
2427*01826a49SYabin Cui {{0,0}, {1,1}, {2,2}}, /* Q==1 : impossible */
2428*01826a49SYabin Cui {{ 38,130}, {1313, 74}, {2151, 38}}, /* Q == 2 : 12-18% */
2429*01826a49SYabin Cui {{ 448,128}, {1353, 74}, {2238, 41}}, /* Q == 3 : 18-25% */
2430*01826a49SYabin Cui {{ 556,128}, {1353, 74}, {2238, 47}}, /* Q == 4 : 25-32% */
2431*01826a49SYabin Cui {{ 714,128}, {1418, 74}, {2436, 53}}, /* Q == 5 : 32-38% */
2432*01826a49SYabin Cui {{ 883,128}, {1437, 74}, {2464, 61}}, /* Q == 6 : 38-44% */
2433*01826a49SYabin Cui {{ 897,128}, {1515, 75}, {2622, 68}}, /* Q == 7 : 44-50% */
2434*01826a49SYabin Cui {{ 926,128}, {1613, 75}, {2730, 75}}, /* Q == 8 : 50-56% */
2435*01826a49SYabin Cui {{ 947,128}, {1729, 77}, {3359, 77}}, /* Q == 9 : 56-62% */
2436*01826a49SYabin Cui {{1107,128}, {2083, 81}, {4006, 84}}, /* Q ==10 : 62-69% */
2437*01826a49SYabin Cui {{1177,128}, {2379, 87}, {4785, 88}}, /* Q ==11 : 69-75% */
2438*01826a49SYabin Cui {{1242,128}, {2415, 93}, {5155, 84}}, /* Q ==12 : 75-81% */
2439*01826a49SYabin Cui {{1349,128}, {2644,106}, {5260,106}}, /* Q ==13 : 81-87% */
2440*01826a49SYabin Cui {{1455,128}, {2422,124}, {4174,124}}, /* Q ==14 : 87-93% */
2441*01826a49SYabin Cui {{ 722,128}, {1891,145}, {1936,146}}, /* Q ==15 : 93-99% */
2442*01826a49SYabin Cui };
2443*01826a49SYabin Cui
2444*01826a49SYabin Cui /** HUFv07_selectDecoder() :
2445*01826a49SYabin Cui * Tells which decoder is likely to decode faster,
2446*01826a49SYabin Cui * based on a set of pre-determined metrics.
2447*01826a49SYabin Cui * @return : 0==HUFv07_decompress4X2, 1==HUFv07_decompress4X4 .
2448*01826a49SYabin Cui * Assumption : 0 < cSrcSize < dstSize <= 128 KB */
HUFv07_selectDecoder(size_t dstSize,size_t cSrcSize)2449*01826a49SYabin Cui U32 HUFv07_selectDecoder (size_t dstSize, size_t cSrcSize)
2450*01826a49SYabin Cui {
2451*01826a49SYabin Cui /* decoder timing evaluation */
2452*01826a49SYabin Cui U32 const Q = (U32)(cSrcSize * 16 / dstSize); /* Q < 16 since dstSize > cSrcSize */
2453*01826a49SYabin Cui U32 const D256 = (U32)(dstSize >> 8);
2454*01826a49SYabin Cui U32 const DTime0 = algoTime[Q][0].tableTime + (algoTime[Q][0].decode256Time * D256);
2455*01826a49SYabin Cui U32 DTime1 = algoTime[Q][1].tableTime + (algoTime[Q][1].decode256Time * D256);
2456*01826a49SYabin Cui DTime1 += DTime1 >> 3; /* advantage to algorithm using less memory, for cache eviction */
2457*01826a49SYabin Cui
2458*01826a49SYabin Cui return DTime1 < DTime0;
2459*01826a49SYabin Cui }
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
HUFv07_decompress(void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)2464*01826a49SYabin Cui size_t HUFv07_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2465*01826a49SYabin Cui {
2466*01826a49SYabin Cui static const decompressionAlgo decompress[2] = { HUFv07_decompress4X2, HUFv07_decompress4X4 };
2467*01826a49SYabin Cui
2468*01826a49SYabin Cui /* validation checks */
2469*01826a49SYabin Cui if (dstSize == 0) return ERROR(dstSize_tooSmall);
2470*01826a49SYabin Cui if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
2471*01826a49SYabin Cui if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
2472*01826a49SYabin Cui if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
2473*01826a49SYabin Cui
2474*01826a49SYabin Cui { U32 const algoNb = HUFv07_selectDecoder(dstSize, cSrcSize);
2475*01826a49SYabin Cui return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
2476*01826a49SYabin Cui }
2477*01826a49SYabin Cui
2478*01826a49SYabin Cui /* return HUFv07_decompress4X2(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams single-symbol decoding */
2479*01826a49SYabin Cui /* return HUFv07_decompress4X4(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams double-symbols decoding */
2480*01826a49SYabin Cui }
2481*01826a49SYabin Cui
HUFv07_decompress4X_DCtx(HUFv07_DTable * dctx,void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)2482*01826a49SYabin Cui size_t HUFv07_decompress4X_DCtx (HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2483*01826a49SYabin Cui {
2484*01826a49SYabin Cui /* validation checks */
2485*01826a49SYabin Cui if (dstSize == 0) return ERROR(dstSize_tooSmall);
2486*01826a49SYabin Cui if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
2487*01826a49SYabin Cui if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
2488*01826a49SYabin Cui if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
2489*01826a49SYabin Cui
2490*01826a49SYabin Cui { U32 const algoNb = HUFv07_selectDecoder(dstSize, cSrcSize);
2491*01826a49SYabin Cui return algoNb ? HUFv07_decompress4X4_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) :
2492*01826a49SYabin Cui HUFv07_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) ;
2493*01826a49SYabin Cui }
2494*01826a49SYabin Cui }
2495*01826a49SYabin Cui
HUFv07_decompress4X_hufOnly(HUFv07_DTable * dctx,void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)2496*01826a49SYabin Cui size_t HUFv07_decompress4X_hufOnly (HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2497*01826a49SYabin Cui {
2498*01826a49SYabin Cui /* validation checks */
2499*01826a49SYabin Cui if (dstSize == 0) return ERROR(dstSize_tooSmall);
2500*01826a49SYabin Cui if ((cSrcSize >= dstSize) || (cSrcSize <= 1)) return ERROR(corruption_detected); /* invalid */
2501*01826a49SYabin Cui
2502*01826a49SYabin Cui { U32 const algoNb = HUFv07_selectDecoder(dstSize, cSrcSize);
2503*01826a49SYabin Cui return algoNb ? HUFv07_decompress4X4_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) :
2504*01826a49SYabin Cui HUFv07_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) ;
2505*01826a49SYabin Cui }
2506*01826a49SYabin Cui }
2507*01826a49SYabin Cui
HUFv07_decompress1X_DCtx(HUFv07_DTable * dctx,void * dst,size_t dstSize,const void * cSrc,size_t cSrcSize)2508*01826a49SYabin Cui size_t HUFv07_decompress1X_DCtx (HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
2509*01826a49SYabin Cui {
2510*01826a49SYabin Cui /* validation checks */
2511*01826a49SYabin Cui if (dstSize == 0) return ERROR(dstSize_tooSmall);
2512*01826a49SYabin Cui if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
2513*01826a49SYabin Cui if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
2514*01826a49SYabin Cui if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
2515*01826a49SYabin Cui
2516*01826a49SYabin Cui { U32 const algoNb = HUFv07_selectDecoder(dstSize, cSrcSize);
2517*01826a49SYabin Cui return algoNb ? HUFv07_decompress1X4_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) :
2518*01826a49SYabin Cui HUFv07_decompress1X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) ;
2519*01826a49SYabin Cui }
2520*01826a49SYabin Cui }
2521*01826a49SYabin Cui /*
2522*01826a49SYabin Cui Common functions of Zstd compression library
2523*01826a49SYabin Cui Copyright (C) 2015-2016, Yann Collet.
2524*01826a49SYabin Cui
2525*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
2526*01826a49SYabin Cui
2527*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
2528*01826a49SYabin Cui modification, are permitted provided that the following conditions are
2529*01826a49SYabin Cui met:
2530*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
2531*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
2532*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
2533*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
2534*01826a49SYabin Cui in the documentation and/or other materials provided with the
2535*01826a49SYabin Cui distribution.
2536*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2537*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2538*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2539*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2540*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2541*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2542*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2543*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2544*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2545*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2546*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2547*01826a49SYabin Cui
2548*01826a49SYabin Cui You can contact the author at :
2549*01826a49SYabin Cui - zstd homepage : https://facebook.github.io/zstd/
2550*01826a49SYabin Cui */
2551*01826a49SYabin Cui
2552*01826a49SYabin Cui
2553*01826a49SYabin Cui
2554*01826a49SYabin Cui /*-****************************************
2555*01826a49SYabin Cui * ZSTD Error Management
2556*01826a49SYabin Cui ******************************************/
2557*01826a49SYabin Cui /*! ZSTDv07_isError() :
2558*01826a49SYabin Cui * tells if a return value is an error code */
ZSTDv07_isError(size_t code)2559*01826a49SYabin Cui unsigned ZSTDv07_isError(size_t code) { return ERR_isError(code); }
2560*01826a49SYabin Cui
2561*01826a49SYabin Cui /*! ZSTDv07_getErrorName() :
2562*01826a49SYabin Cui * provides error code string from function result (useful for debugging) */
ZSTDv07_getErrorName(size_t code)2563*01826a49SYabin Cui const char* ZSTDv07_getErrorName(size_t code) { return ERR_getErrorName(code); }
2564*01826a49SYabin Cui
2565*01826a49SYabin Cui
2566*01826a49SYabin Cui
2567*01826a49SYabin Cui /* **************************************************************
2568*01826a49SYabin Cui * ZBUFF Error Management
2569*01826a49SYabin Cui ****************************************************************/
ZBUFFv07_isError(size_t errorCode)2570*01826a49SYabin Cui unsigned ZBUFFv07_isError(size_t errorCode) { return ERR_isError(errorCode); }
2571*01826a49SYabin Cui
ZBUFFv07_getErrorName(size_t errorCode)2572*01826a49SYabin Cui const char* ZBUFFv07_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
2573*01826a49SYabin Cui
2574*01826a49SYabin Cui
2575*01826a49SYabin Cui
ZSTDv07_defaultAllocFunction(void * opaque,size_t size)2576*01826a49SYabin Cui static void* ZSTDv07_defaultAllocFunction(void* opaque, size_t size)
2577*01826a49SYabin Cui {
2578*01826a49SYabin Cui void* address = malloc(size);
2579*01826a49SYabin Cui (void)opaque;
2580*01826a49SYabin Cui /* printf("alloc %p, %d opaque=%p \n", address, (int)size, opaque); */
2581*01826a49SYabin Cui return address;
2582*01826a49SYabin Cui }
2583*01826a49SYabin Cui
ZSTDv07_defaultFreeFunction(void * opaque,void * address)2584*01826a49SYabin Cui static void ZSTDv07_defaultFreeFunction(void* opaque, void* address)
2585*01826a49SYabin Cui {
2586*01826a49SYabin Cui (void)opaque;
2587*01826a49SYabin Cui /* if (address) printf("free %p opaque=%p \n", address, opaque); */
2588*01826a49SYabin Cui free(address);
2589*01826a49SYabin Cui }
2590*01826a49SYabin Cui /*
2591*01826a49SYabin Cui zstd_internal - common functions to include
2592*01826a49SYabin Cui Header File for include
2593*01826a49SYabin Cui Copyright (C) 2014-2016, Yann Collet.
2594*01826a49SYabin Cui
2595*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
2596*01826a49SYabin Cui
2597*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
2598*01826a49SYabin Cui modification, are permitted provided that the following conditions are
2599*01826a49SYabin Cui met:
2600*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
2601*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
2602*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
2603*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
2604*01826a49SYabin Cui in the documentation and/or other materials provided with the
2605*01826a49SYabin Cui distribution.
2606*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2607*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2608*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2609*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2610*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2611*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2612*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2613*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2614*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2615*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2616*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2617*01826a49SYabin Cui
2618*01826a49SYabin Cui You can contact the author at :
2619*01826a49SYabin Cui - zstd homepage : https://www.zstd.net
2620*01826a49SYabin Cui */
2621*01826a49SYabin Cui #ifndef ZSTDv07_CCOMMON_H_MODULE
2622*01826a49SYabin Cui #define ZSTDv07_CCOMMON_H_MODULE
2623*01826a49SYabin Cui
2624*01826a49SYabin Cui
2625*01826a49SYabin Cui /*-*************************************
2626*01826a49SYabin Cui * Common macros
2627*01826a49SYabin Cui ***************************************/
2628*01826a49SYabin Cui #define MIN(a,b) ((a)<(b) ? (a) : (b))
2629*01826a49SYabin Cui #define MAX(a,b) ((a)>(b) ? (a) : (b))
2630*01826a49SYabin Cui
2631*01826a49SYabin Cui
2632*01826a49SYabin Cui /*-*************************************
2633*01826a49SYabin Cui * Common constants
2634*01826a49SYabin Cui ***************************************/
2635*01826a49SYabin Cui #define ZSTDv07_OPT_NUM (1<<12)
2636*01826a49SYabin Cui #define ZSTDv07_DICT_MAGIC 0xEC30A437 /* v0.7 */
2637*01826a49SYabin Cui
2638*01826a49SYabin Cui #define ZSTDv07_REP_NUM 3
2639*01826a49SYabin Cui #define ZSTDv07_REP_INIT ZSTDv07_REP_NUM
2640*01826a49SYabin Cui #define ZSTDv07_REP_MOVE (ZSTDv07_REP_NUM-1)
2641*01826a49SYabin Cui static const U32 repStartValue[ZSTDv07_REP_NUM] = { 1, 4, 8 };
2642*01826a49SYabin Cui
2643*01826a49SYabin Cui #define KB *(1 <<10)
2644*01826a49SYabin Cui #define MB *(1 <<20)
2645*01826a49SYabin Cui #define GB *(1U<<30)
2646*01826a49SYabin Cui
2647*01826a49SYabin Cui #define BIT7 128
2648*01826a49SYabin Cui #define BIT6 64
2649*01826a49SYabin Cui #define BIT5 32
2650*01826a49SYabin Cui #define BIT4 16
2651*01826a49SYabin Cui #define BIT1 2
2652*01826a49SYabin Cui #define BIT0 1
2653*01826a49SYabin Cui
2654*01826a49SYabin Cui #define ZSTDv07_WINDOWLOG_ABSOLUTEMIN 10
2655*01826a49SYabin Cui static const size_t ZSTDv07_fcs_fieldSize[4] = { 0, 2, 4, 8 };
2656*01826a49SYabin Cui static const size_t ZSTDv07_did_fieldSize[4] = { 0, 1, 2, 4 };
2657*01826a49SYabin Cui
2658*01826a49SYabin Cui #define ZSTDv07_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
2659*01826a49SYabin Cui static const size_t ZSTDv07_blockHeaderSize = ZSTDv07_BLOCKHEADERSIZE;
2660*01826a49SYabin Cui typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
2661*01826a49SYabin Cui
2662*01826a49SYabin Cui #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
2663*01826a49SYabin Cui #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
2664*01826a49SYabin Cui
2665*01826a49SYabin Cui #define ZSTD_HUFFDTABLE_CAPACITY_LOG 12
2666*01826a49SYabin Cui typedef enum { lbt_huffman, lbt_repeat, lbt_raw, lbt_rle } litBlockType_t;
2667*01826a49SYabin Cui
2668*01826a49SYabin Cui #define LONGNBSEQ 0x7F00
2669*01826a49SYabin Cui
2670*01826a49SYabin Cui #define MINMATCH 3
2671*01826a49SYabin Cui #define EQUAL_READ32 4
2672*01826a49SYabin Cui
2673*01826a49SYabin Cui #define Litbits 8
2674*01826a49SYabin Cui #define MaxLit ((1<<Litbits) - 1)
2675*01826a49SYabin Cui #define MaxML 52
2676*01826a49SYabin Cui #define MaxLL 35
2677*01826a49SYabin Cui #define MaxOff 28
2678*01826a49SYabin Cui #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
2679*01826a49SYabin Cui #define MLFSELog 9
2680*01826a49SYabin Cui #define LLFSELog 9
2681*01826a49SYabin Cui #define OffFSELog 8
2682*01826a49SYabin Cui
2683*01826a49SYabin Cui #define FSEv07_ENCODING_RAW 0
2684*01826a49SYabin Cui #define FSEv07_ENCODING_RLE 1
2685*01826a49SYabin Cui #define FSEv07_ENCODING_STATIC 2
2686*01826a49SYabin Cui #define FSEv07_ENCODING_DYNAMIC 3
2687*01826a49SYabin Cui
2688*01826a49SYabin Cui #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
2689*01826a49SYabin Cui
2690*01826a49SYabin Cui static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2691*01826a49SYabin Cui 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
2692*01826a49SYabin Cui 13,14,15,16 };
2693*01826a49SYabin Cui static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
2694*01826a49SYabin Cui 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
2695*01826a49SYabin Cui -1,-1,-1,-1 };
2696*01826a49SYabin Cui static const U32 LL_defaultNormLog = 6;
2697*01826a49SYabin Cui
2698*01826a49SYabin Cui static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2699*01826a49SYabin Cui 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2700*01826a49SYabin Cui 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11,
2701*01826a49SYabin Cui 12,13,14,15,16 };
2702*01826a49SYabin Cui static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
2703*01826a49SYabin Cui 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2704*01826a49SYabin Cui 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,
2705*01826a49SYabin Cui -1,-1,-1,-1,-1 };
2706*01826a49SYabin Cui static const U32 ML_defaultNormLog = 6;
2707*01826a49SYabin Cui
2708*01826a49SYabin Cui static const S16 OF_defaultNorm[MaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
2709*01826a49SYabin Cui 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1 };
2710*01826a49SYabin Cui static const U32 OF_defaultNormLog = 5;
2711*01826a49SYabin Cui
2712*01826a49SYabin Cui
2713*01826a49SYabin Cui /*-*******************************************
2714*01826a49SYabin Cui * Shared functions to include for inlining
2715*01826a49SYabin Cui *********************************************/
ZSTDv07_copy8(void * dst,const void * src)2716*01826a49SYabin Cui static void ZSTDv07_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
2717*01826a49SYabin Cui #define COPY8(d,s) { ZSTDv07_copy8(d,s); d+=8; s+=8; }
2718*01826a49SYabin Cui
2719*01826a49SYabin Cui /*! ZSTDv07_wildcopy() :
2720*01826a49SYabin Cui * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
2721*01826a49SYabin Cui #define WILDCOPY_OVERLENGTH 8
ZSTDv07_wildcopy(void * dst,const void * src,ptrdiff_t length)2722*01826a49SYabin Cui MEM_STATIC void ZSTDv07_wildcopy(void* dst, const void* src, ptrdiff_t length)
2723*01826a49SYabin Cui {
2724*01826a49SYabin Cui const BYTE* ip = (const BYTE*)src;
2725*01826a49SYabin Cui BYTE* op = (BYTE*)dst;
2726*01826a49SYabin Cui BYTE* const oend = op + length;
2727*01826a49SYabin Cui do
2728*01826a49SYabin Cui COPY8(op, ip)
2729*01826a49SYabin Cui while (op < oend);
2730*01826a49SYabin Cui }
2731*01826a49SYabin Cui
2732*01826a49SYabin Cui
2733*01826a49SYabin Cui /*-*******************************************
2734*01826a49SYabin Cui * Private interfaces
2735*01826a49SYabin Cui *********************************************/
2736*01826a49SYabin Cui typedef struct ZSTDv07_stats_s ZSTDv07_stats_t;
2737*01826a49SYabin Cui
2738*01826a49SYabin Cui typedef struct {
2739*01826a49SYabin Cui U32 off;
2740*01826a49SYabin Cui U32 len;
2741*01826a49SYabin Cui } ZSTDv07_match_t;
2742*01826a49SYabin Cui
2743*01826a49SYabin Cui typedef struct {
2744*01826a49SYabin Cui U32 price;
2745*01826a49SYabin Cui U32 off;
2746*01826a49SYabin Cui U32 mlen;
2747*01826a49SYabin Cui U32 litlen;
2748*01826a49SYabin Cui U32 rep[ZSTDv07_REP_INIT];
2749*01826a49SYabin Cui } ZSTDv07_optimal_t;
2750*01826a49SYabin Cui
2751*01826a49SYabin Cui struct ZSTDv07_stats_s { U32 unused; };
2752*01826a49SYabin Cui
2753*01826a49SYabin Cui typedef struct {
2754*01826a49SYabin Cui void* buffer;
2755*01826a49SYabin Cui U32* offsetStart;
2756*01826a49SYabin Cui U32* offset;
2757*01826a49SYabin Cui BYTE* offCodeStart;
2758*01826a49SYabin Cui BYTE* litStart;
2759*01826a49SYabin Cui BYTE* lit;
2760*01826a49SYabin Cui U16* litLengthStart;
2761*01826a49SYabin Cui U16* litLength;
2762*01826a49SYabin Cui BYTE* llCodeStart;
2763*01826a49SYabin Cui U16* matchLengthStart;
2764*01826a49SYabin Cui U16* matchLength;
2765*01826a49SYabin Cui BYTE* mlCodeStart;
2766*01826a49SYabin Cui U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
2767*01826a49SYabin Cui U32 longLengthPos;
2768*01826a49SYabin Cui /* opt */
2769*01826a49SYabin Cui ZSTDv07_optimal_t* priceTable;
2770*01826a49SYabin Cui ZSTDv07_match_t* matchTable;
2771*01826a49SYabin Cui U32* matchLengthFreq;
2772*01826a49SYabin Cui U32* litLengthFreq;
2773*01826a49SYabin Cui U32* litFreq;
2774*01826a49SYabin Cui U32* offCodeFreq;
2775*01826a49SYabin Cui U32 matchLengthSum;
2776*01826a49SYabin Cui U32 matchSum;
2777*01826a49SYabin Cui U32 litLengthSum;
2778*01826a49SYabin Cui U32 litSum;
2779*01826a49SYabin Cui U32 offCodeSum;
2780*01826a49SYabin Cui U32 log2matchLengthSum;
2781*01826a49SYabin Cui U32 log2matchSum;
2782*01826a49SYabin Cui U32 log2litLengthSum;
2783*01826a49SYabin Cui U32 log2litSum;
2784*01826a49SYabin Cui U32 log2offCodeSum;
2785*01826a49SYabin Cui U32 factor;
2786*01826a49SYabin Cui U32 cachedPrice;
2787*01826a49SYabin Cui U32 cachedLitLength;
2788*01826a49SYabin Cui const BYTE* cachedLiterals;
2789*01826a49SYabin Cui ZSTDv07_stats_t stats;
2790*01826a49SYabin Cui } seqStore_t;
2791*01826a49SYabin Cui
2792*01826a49SYabin Cui void ZSTDv07_seqToCodes(const seqStore_t* seqStorePtr, size_t const nbSeq);
2793*01826a49SYabin Cui
2794*01826a49SYabin Cui /* custom memory allocation functions */
2795*01826a49SYabin Cui static const ZSTDv07_customMem defaultCustomMem = { ZSTDv07_defaultAllocFunction, ZSTDv07_defaultFreeFunction, NULL };
2796*01826a49SYabin Cui
2797*01826a49SYabin Cui #endif /* ZSTDv07_CCOMMON_H_MODULE */
2798*01826a49SYabin Cui /*
2799*01826a49SYabin Cui zstd - standard compression library
2800*01826a49SYabin Cui Copyright (C) 2014-2016, Yann Collet.
2801*01826a49SYabin Cui
2802*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
2803*01826a49SYabin Cui
2804*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
2805*01826a49SYabin Cui modification, are permitted provided that the following conditions are
2806*01826a49SYabin Cui met:
2807*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
2808*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
2809*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
2810*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
2811*01826a49SYabin Cui in the documentation and/or other materials provided with the
2812*01826a49SYabin Cui distribution.
2813*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2814*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2815*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2816*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2817*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2818*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2819*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2820*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2821*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2822*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2823*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2824*01826a49SYabin Cui
2825*01826a49SYabin Cui You can contact the author at :
2826*01826a49SYabin Cui - zstd homepage : https://facebook.github.io/zstd
2827*01826a49SYabin Cui */
2828*01826a49SYabin Cui
2829*01826a49SYabin Cui /* ***************************************************************
2830*01826a49SYabin Cui * Tuning parameters
2831*01826a49SYabin Cui *****************************************************************/
2832*01826a49SYabin Cui /*!
2833*01826a49SYabin Cui * HEAPMODE :
2834*01826a49SYabin Cui * Select how default decompression function ZSTDv07_decompress() will allocate memory,
2835*01826a49SYabin Cui * in memory stack (0), or in memory heap (1, requires malloc())
2836*01826a49SYabin Cui */
2837*01826a49SYabin Cui #ifndef ZSTDv07_HEAPMODE
2838*01826a49SYabin Cui # define ZSTDv07_HEAPMODE 1
2839*01826a49SYabin Cui #endif
2840*01826a49SYabin Cui
2841*01826a49SYabin Cui
2842*01826a49SYabin Cui /*-*******************************************************
2843*01826a49SYabin Cui * Compiler specifics
2844*01826a49SYabin Cui *********************************************************/
2845*01826a49SYabin Cui #ifdef _MSC_VER /* Visual Studio */
2846*01826a49SYabin Cui # include <intrin.h> /* For Visual 2005 */
2847*01826a49SYabin Cui # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2848*01826a49SYabin Cui # pragma warning(disable : 4324) /* disable: C4324: padded structure */
2849*01826a49SYabin Cui # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
2850*01826a49SYabin Cui #endif
2851*01826a49SYabin Cui
2852*01826a49SYabin Cui
2853*01826a49SYabin Cui /*-*************************************
2854*01826a49SYabin Cui * Macros
2855*01826a49SYabin Cui ***************************************/
2856*01826a49SYabin Cui #define ZSTDv07_isError ERR_isError /* for inlining */
2857*01826a49SYabin Cui #define FSEv07_isError ERR_isError
2858*01826a49SYabin Cui #define HUFv07_isError ERR_isError
2859*01826a49SYabin Cui
2860*01826a49SYabin Cui
2861*01826a49SYabin Cui /*_*******************************************************
2862*01826a49SYabin Cui * Memory operations
2863*01826a49SYabin Cui **********************************************************/
ZSTDv07_copy4(void * dst,const void * src)2864*01826a49SYabin Cui static void ZSTDv07_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
2865*01826a49SYabin Cui
2866*01826a49SYabin Cui
2867*01826a49SYabin Cui /*-*************************************************************
2868*01826a49SYabin Cui * Context management
2869*01826a49SYabin Cui ***************************************************************/
2870*01826a49SYabin Cui typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader,
2871*01826a49SYabin Cui ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock,
2872*01826a49SYabin Cui ZSTDds_decodeSkippableHeader, ZSTDds_skipFrame } ZSTDv07_dStage;
2873*01826a49SYabin Cui
2874*01826a49SYabin Cui struct ZSTDv07_DCtx_s
2875*01826a49SYabin Cui {
2876*01826a49SYabin Cui FSEv07_DTable LLTable[FSEv07_DTABLE_SIZE_U32(LLFSELog)];
2877*01826a49SYabin Cui FSEv07_DTable OffTable[FSEv07_DTABLE_SIZE_U32(OffFSELog)];
2878*01826a49SYabin Cui FSEv07_DTable MLTable[FSEv07_DTABLE_SIZE_U32(MLFSELog)];
2879*01826a49SYabin Cui HUFv07_DTable hufTable[HUFv07_DTABLE_SIZE(ZSTD_HUFFDTABLE_CAPACITY_LOG)]; /* can accommodate HUFv07_decompress4X */
2880*01826a49SYabin Cui const void* previousDstEnd;
2881*01826a49SYabin Cui const void* base;
2882*01826a49SYabin Cui const void* vBase;
2883*01826a49SYabin Cui const void* dictEnd;
2884*01826a49SYabin Cui size_t expected;
2885*01826a49SYabin Cui U32 rep[3];
2886*01826a49SYabin Cui ZSTDv07_frameParams fParams;
2887*01826a49SYabin Cui blockType_t bType; /* used in ZSTDv07_decompressContinue(), to transfer blockType between header decoding and block decoding stages */
2888*01826a49SYabin Cui ZSTDv07_dStage stage;
2889*01826a49SYabin Cui U32 litEntropy;
2890*01826a49SYabin Cui U32 fseEntropy;
2891*01826a49SYabin Cui XXH64_state_t xxhState;
2892*01826a49SYabin Cui size_t headerSize;
2893*01826a49SYabin Cui U32 dictID;
2894*01826a49SYabin Cui const BYTE* litPtr;
2895*01826a49SYabin Cui ZSTDv07_customMem customMem;
2896*01826a49SYabin Cui size_t litSize;
2897*01826a49SYabin Cui BYTE litBuffer[ZSTDv07_BLOCKSIZE_ABSOLUTEMAX + WILDCOPY_OVERLENGTH];
2898*01826a49SYabin Cui BYTE headerBuffer[ZSTDv07_FRAMEHEADERSIZE_MAX];
2899*01826a49SYabin Cui }; /* typedef'd to ZSTDv07_DCtx within "zstd_static.h" */
2900*01826a49SYabin Cui
2901*01826a49SYabin Cui int ZSTDv07_isSkipFrame(ZSTDv07_DCtx* dctx);
2902*01826a49SYabin Cui
ZSTDv07_sizeofDCtx(const ZSTDv07_DCtx * dctx)2903*01826a49SYabin Cui size_t ZSTDv07_sizeofDCtx (const ZSTDv07_DCtx* dctx) { return sizeof(*dctx); }
2904*01826a49SYabin Cui
ZSTDv07_estimateDCtxSize(void)2905*01826a49SYabin Cui size_t ZSTDv07_estimateDCtxSize(void) { return sizeof(ZSTDv07_DCtx); }
2906*01826a49SYabin Cui
ZSTDv07_decompressBegin(ZSTDv07_DCtx * dctx)2907*01826a49SYabin Cui size_t ZSTDv07_decompressBegin(ZSTDv07_DCtx* dctx)
2908*01826a49SYabin Cui {
2909*01826a49SYabin Cui dctx->expected = ZSTDv07_frameHeaderSize_min;
2910*01826a49SYabin Cui dctx->stage = ZSTDds_getFrameHeaderSize;
2911*01826a49SYabin Cui dctx->previousDstEnd = NULL;
2912*01826a49SYabin Cui dctx->base = NULL;
2913*01826a49SYabin Cui dctx->vBase = NULL;
2914*01826a49SYabin Cui dctx->dictEnd = NULL;
2915*01826a49SYabin Cui dctx->hufTable[0] = (HUFv07_DTable)((ZSTD_HUFFDTABLE_CAPACITY_LOG)*0x1000001);
2916*01826a49SYabin Cui dctx->litEntropy = dctx->fseEntropy = 0;
2917*01826a49SYabin Cui dctx->dictID = 0;
2918*01826a49SYabin Cui { int i; for (i=0; i<ZSTDv07_REP_NUM; i++) dctx->rep[i] = repStartValue[i]; }
2919*01826a49SYabin Cui return 0;
2920*01826a49SYabin Cui }
2921*01826a49SYabin Cui
ZSTDv07_createDCtx_advanced(ZSTDv07_customMem customMem)2922*01826a49SYabin Cui ZSTDv07_DCtx* ZSTDv07_createDCtx_advanced(ZSTDv07_customMem customMem)
2923*01826a49SYabin Cui {
2924*01826a49SYabin Cui ZSTDv07_DCtx* dctx;
2925*01826a49SYabin Cui
2926*01826a49SYabin Cui if (!customMem.customAlloc && !customMem.customFree)
2927*01826a49SYabin Cui customMem = defaultCustomMem;
2928*01826a49SYabin Cui
2929*01826a49SYabin Cui if (!customMem.customAlloc || !customMem.customFree)
2930*01826a49SYabin Cui return NULL;
2931*01826a49SYabin Cui
2932*01826a49SYabin Cui dctx = (ZSTDv07_DCtx*) customMem.customAlloc(customMem.opaque, sizeof(ZSTDv07_DCtx));
2933*01826a49SYabin Cui if (!dctx) return NULL;
2934*01826a49SYabin Cui memcpy(&dctx->customMem, &customMem, sizeof(ZSTDv07_customMem));
2935*01826a49SYabin Cui ZSTDv07_decompressBegin(dctx);
2936*01826a49SYabin Cui return dctx;
2937*01826a49SYabin Cui }
2938*01826a49SYabin Cui
ZSTDv07_createDCtx(void)2939*01826a49SYabin Cui ZSTDv07_DCtx* ZSTDv07_createDCtx(void)
2940*01826a49SYabin Cui {
2941*01826a49SYabin Cui return ZSTDv07_createDCtx_advanced(defaultCustomMem);
2942*01826a49SYabin Cui }
2943*01826a49SYabin Cui
ZSTDv07_freeDCtx(ZSTDv07_DCtx * dctx)2944*01826a49SYabin Cui size_t ZSTDv07_freeDCtx(ZSTDv07_DCtx* dctx)
2945*01826a49SYabin Cui {
2946*01826a49SYabin Cui if (dctx==NULL) return 0; /* support free on NULL */
2947*01826a49SYabin Cui dctx->customMem.customFree(dctx->customMem.opaque, dctx);
2948*01826a49SYabin Cui return 0; /* reserved as a potential error code in the future */
2949*01826a49SYabin Cui }
2950*01826a49SYabin Cui
ZSTDv07_copyDCtx(ZSTDv07_DCtx * dstDCtx,const ZSTDv07_DCtx * srcDCtx)2951*01826a49SYabin Cui void ZSTDv07_copyDCtx(ZSTDv07_DCtx* dstDCtx, const ZSTDv07_DCtx* srcDCtx)
2952*01826a49SYabin Cui {
2953*01826a49SYabin Cui memcpy(dstDCtx, srcDCtx,
2954*01826a49SYabin Cui sizeof(ZSTDv07_DCtx) - (ZSTDv07_BLOCKSIZE_ABSOLUTEMAX+WILDCOPY_OVERLENGTH + ZSTDv07_frameHeaderSize_max)); /* no need to copy workspace */
2955*01826a49SYabin Cui }
2956*01826a49SYabin Cui
2957*01826a49SYabin Cui
2958*01826a49SYabin Cui /*-*************************************************************
2959*01826a49SYabin Cui * Decompression section
2960*01826a49SYabin Cui ***************************************************************/
2961*01826a49SYabin Cui
2962*01826a49SYabin Cui /* Frame format description
2963*01826a49SYabin Cui Frame Header - [ Block Header - Block ] - Frame End
2964*01826a49SYabin Cui 1) Frame Header
2965*01826a49SYabin Cui - 4 bytes - Magic Number : ZSTDv07_MAGICNUMBER (defined within zstd.h)
2966*01826a49SYabin Cui - 1 byte - Frame Descriptor
2967*01826a49SYabin Cui 2) Block Header
2968*01826a49SYabin Cui - 3 bytes, starting with a 2-bits descriptor
2969*01826a49SYabin Cui Uncompressed, Compressed, Frame End, unused
2970*01826a49SYabin Cui 3) Block
2971*01826a49SYabin Cui See Block Format Description
2972*01826a49SYabin Cui 4) Frame End
2973*01826a49SYabin Cui - 3 bytes, compatible with Block Header
2974*01826a49SYabin Cui */
2975*01826a49SYabin Cui
2976*01826a49SYabin Cui
2977*01826a49SYabin Cui /* Frame Header :
2978*01826a49SYabin Cui
2979*01826a49SYabin Cui 1 byte - FrameHeaderDescription :
2980*01826a49SYabin Cui bit 0-1 : dictID (0, 1, 2 or 4 bytes)
2981*01826a49SYabin Cui bit 2 : checksumFlag
2982*01826a49SYabin Cui bit 3 : reserved (must be zero)
2983*01826a49SYabin Cui bit 4 : reserved (unused, can be any value)
2984*01826a49SYabin Cui bit 5 : Single Segment (if 1, WindowLog byte is not present)
2985*01826a49SYabin Cui bit 6-7 : FrameContentFieldSize (0, 2, 4, or 8)
2986*01826a49SYabin Cui if (SkippedWindowLog && !FrameContentFieldsize) FrameContentFieldsize=1;
2987*01826a49SYabin Cui
2988*01826a49SYabin Cui Optional : WindowLog (0 or 1 byte)
2989*01826a49SYabin Cui bit 0-2 : octal Fractional (1/8th)
2990*01826a49SYabin Cui bit 3-7 : Power of 2, with 0 = 1 KB (up to 2 TB)
2991*01826a49SYabin Cui
2992*01826a49SYabin Cui Optional : dictID (0, 1, 2 or 4 bytes)
2993*01826a49SYabin Cui Automatic adaptation
2994*01826a49SYabin Cui 0 : no dictID
2995*01826a49SYabin Cui 1 : 1 - 255
2996*01826a49SYabin Cui 2 : 256 - 65535
2997*01826a49SYabin Cui 4 : all other values
2998*01826a49SYabin Cui
2999*01826a49SYabin Cui Optional : content size (0, 1, 2, 4 or 8 bytes)
3000*01826a49SYabin Cui 0 : unknown (fcfs==0 and swl==0)
3001*01826a49SYabin Cui 1 : 0-255 bytes (fcfs==0 and swl==1)
3002*01826a49SYabin Cui 2 : 256 - 65535+256 (fcfs==1)
3003*01826a49SYabin Cui 4 : 0 - 4GB-1 (fcfs==2)
3004*01826a49SYabin Cui 8 : 0 - 16EB-1 (fcfs==3)
3005*01826a49SYabin Cui */
3006*01826a49SYabin Cui
3007*01826a49SYabin Cui
3008*01826a49SYabin Cui /* Compressed Block, format description
3009*01826a49SYabin Cui
3010*01826a49SYabin Cui Block = Literal Section - Sequences Section
3011*01826a49SYabin Cui Prerequisite : size of (compressed) block, maximum size of regenerated data
3012*01826a49SYabin Cui
3013*01826a49SYabin Cui 1) Literal Section
3014*01826a49SYabin Cui
3015*01826a49SYabin Cui 1.1) Header : 1-5 bytes
3016*01826a49SYabin Cui flags: 2 bits
3017*01826a49SYabin Cui 00 compressed by Huff0
3018*01826a49SYabin Cui 01 unused
3019*01826a49SYabin Cui 10 is Raw (uncompressed)
3020*01826a49SYabin Cui 11 is Rle
3021*01826a49SYabin Cui Note : using 01 => Huff0 with precomputed table ?
3022*01826a49SYabin Cui Note : delta map ? => compressed ?
3023*01826a49SYabin Cui
3024*01826a49SYabin Cui 1.1.1) Huff0-compressed literal block : 3-5 bytes
3025*01826a49SYabin Cui srcSize < 1 KB => 3 bytes (2-2-10-10) => single stream
3026*01826a49SYabin Cui srcSize < 1 KB => 3 bytes (2-2-10-10)
3027*01826a49SYabin Cui srcSize < 16KB => 4 bytes (2-2-14-14)
3028*01826a49SYabin Cui else => 5 bytes (2-2-18-18)
3029*01826a49SYabin Cui big endian convention
3030*01826a49SYabin Cui
3031*01826a49SYabin Cui 1.1.2) Raw (uncompressed) literal block header : 1-3 bytes
3032*01826a49SYabin Cui size : 5 bits: (IS_RAW<<6) + (0<<4) + size
3033*01826a49SYabin Cui 12 bits: (IS_RAW<<6) + (2<<4) + (size>>8)
3034*01826a49SYabin Cui size&255
3035*01826a49SYabin Cui 20 bits: (IS_RAW<<6) + (3<<4) + (size>>16)
3036*01826a49SYabin Cui size>>8&255
3037*01826a49SYabin Cui size&255
3038*01826a49SYabin Cui
3039*01826a49SYabin Cui 1.1.3) Rle (repeated single byte) literal block header : 1-3 bytes
3040*01826a49SYabin Cui size : 5 bits: (IS_RLE<<6) + (0<<4) + size
3041*01826a49SYabin Cui 12 bits: (IS_RLE<<6) + (2<<4) + (size>>8)
3042*01826a49SYabin Cui size&255
3043*01826a49SYabin Cui 20 bits: (IS_RLE<<6) + (3<<4) + (size>>16)
3044*01826a49SYabin Cui size>>8&255
3045*01826a49SYabin Cui size&255
3046*01826a49SYabin Cui
3047*01826a49SYabin Cui 1.1.4) Huff0-compressed literal block, using precomputed CTables : 3-5 bytes
3048*01826a49SYabin Cui srcSize < 1 KB => 3 bytes (2-2-10-10) => single stream
3049*01826a49SYabin Cui srcSize < 1 KB => 3 bytes (2-2-10-10)
3050*01826a49SYabin Cui srcSize < 16KB => 4 bytes (2-2-14-14)
3051*01826a49SYabin Cui else => 5 bytes (2-2-18-18)
3052*01826a49SYabin Cui big endian convention
3053*01826a49SYabin Cui
3054*01826a49SYabin Cui 1- CTable available (stored into workspace ?)
3055*01826a49SYabin Cui 2- Small input (fast heuristic ? Full comparison ? depend on clevel ?)
3056*01826a49SYabin Cui
3057*01826a49SYabin Cui
3058*01826a49SYabin Cui 1.2) Literal block content
3059*01826a49SYabin Cui
3060*01826a49SYabin Cui 1.2.1) Huff0 block, using sizes from header
3061*01826a49SYabin Cui See Huff0 format
3062*01826a49SYabin Cui
3063*01826a49SYabin Cui 1.2.2) Huff0 block, using prepared table
3064*01826a49SYabin Cui
3065*01826a49SYabin Cui 1.2.3) Raw content
3066*01826a49SYabin Cui
3067*01826a49SYabin Cui 1.2.4) single byte
3068*01826a49SYabin Cui
3069*01826a49SYabin Cui
3070*01826a49SYabin Cui 2) Sequences section
3071*01826a49SYabin Cui TO DO
3072*01826a49SYabin Cui */
3073*01826a49SYabin Cui
3074*01826a49SYabin Cui /** ZSTDv07_frameHeaderSize() :
3075*01826a49SYabin Cui * srcSize must be >= ZSTDv07_frameHeaderSize_min.
3076*01826a49SYabin Cui * @return : size of the Frame Header */
ZSTDv07_frameHeaderSize(const void * src,size_t srcSize)3077*01826a49SYabin Cui static size_t ZSTDv07_frameHeaderSize(const void* src, size_t srcSize)
3078*01826a49SYabin Cui {
3079*01826a49SYabin Cui if (srcSize < ZSTDv07_frameHeaderSize_min) return ERROR(srcSize_wrong);
3080*01826a49SYabin Cui { BYTE const fhd = ((const BYTE*)src)[4];
3081*01826a49SYabin Cui U32 const dictID= fhd & 3;
3082*01826a49SYabin Cui U32 const directMode = (fhd >> 5) & 1;
3083*01826a49SYabin Cui U32 const fcsId = fhd >> 6;
3084*01826a49SYabin Cui return ZSTDv07_frameHeaderSize_min + !directMode + ZSTDv07_did_fieldSize[dictID] + ZSTDv07_fcs_fieldSize[fcsId]
3085*01826a49SYabin Cui + (directMode && !ZSTDv07_fcs_fieldSize[fcsId]);
3086*01826a49SYabin Cui }
3087*01826a49SYabin Cui }
3088*01826a49SYabin Cui
3089*01826a49SYabin Cui
3090*01826a49SYabin Cui /** ZSTDv07_getFrameParams() :
3091*01826a49SYabin Cui * decode Frame Header, or require larger `srcSize`.
3092*01826a49SYabin Cui * @return : 0, `fparamsPtr` is correctly filled,
3093*01826a49SYabin Cui * >0, `srcSize` is too small, result is expected `srcSize`,
3094*01826a49SYabin Cui * or an error code, which can be tested using ZSTDv07_isError() */
ZSTDv07_getFrameParams(ZSTDv07_frameParams * fparamsPtr,const void * src,size_t srcSize)3095*01826a49SYabin Cui size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src, size_t srcSize)
3096*01826a49SYabin Cui {
3097*01826a49SYabin Cui const BYTE* ip = (const BYTE*)src;
3098*01826a49SYabin Cui
3099*01826a49SYabin Cui if (srcSize < ZSTDv07_frameHeaderSize_min) return ZSTDv07_frameHeaderSize_min;
3100*01826a49SYabin Cui memset(fparamsPtr, 0, sizeof(*fparamsPtr));
3101*01826a49SYabin Cui if (MEM_readLE32(src) != ZSTDv07_MAGICNUMBER) {
3102*01826a49SYabin Cui if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTDv07_MAGIC_SKIPPABLE_START) {
3103*01826a49SYabin Cui if (srcSize < ZSTDv07_skippableHeaderSize) return ZSTDv07_skippableHeaderSize; /* magic number + skippable frame length */
3104*01826a49SYabin Cui fparamsPtr->frameContentSize = MEM_readLE32((const char *)src + 4);
3105*01826a49SYabin Cui fparamsPtr->windowSize = 0; /* windowSize==0 means a frame is skippable */
3106*01826a49SYabin Cui return 0;
3107*01826a49SYabin Cui }
3108*01826a49SYabin Cui return ERROR(prefix_unknown);
3109*01826a49SYabin Cui }
3110*01826a49SYabin Cui
3111*01826a49SYabin Cui /* ensure there is enough `srcSize` to fully read/decode frame header */
3112*01826a49SYabin Cui { size_t const fhsize = ZSTDv07_frameHeaderSize(src, srcSize);
3113*01826a49SYabin Cui if (srcSize < fhsize) return fhsize; }
3114*01826a49SYabin Cui
3115*01826a49SYabin Cui { BYTE const fhdByte = ip[4];
3116*01826a49SYabin Cui size_t pos = 5;
3117*01826a49SYabin Cui U32 const dictIDSizeCode = fhdByte&3;
3118*01826a49SYabin Cui U32 const checksumFlag = (fhdByte>>2)&1;
3119*01826a49SYabin Cui U32 const directMode = (fhdByte>>5)&1;
3120*01826a49SYabin Cui U32 const fcsID = fhdByte>>6;
3121*01826a49SYabin Cui U32 const windowSizeMax = 1U << ZSTDv07_WINDOWLOG_MAX;
3122*01826a49SYabin Cui U32 windowSize = 0;
3123*01826a49SYabin Cui U32 dictID = 0;
3124*01826a49SYabin Cui U64 frameContentSize = 0;
3125*01826a49SYabin Cui if ((fhdByte & 0x08) != 0) /* reserved bits, which must be zero */
3126*01826a49SYabin Cui return ERROR(frameParameter_unsupported);
3127*01826a49SYabin Cui if (!directMode) {
3128*01826a49SYabin Cui BYTE const wlByte = ip[pos++];
3129*01826a49SYabin Cui U32 const windowLog = (wlByte >> 3) + ZSTDv07_WINDOWLOG_ABSOLUTEMIN;
3130*01826a49SYabin Cui if (windowLog > ZSTDv07_WINDOWLOG_MAX)
3131*01826a49SYabin Cui return ERROR(frameParameter_unsupported);
3132*01826a49SYabin Cui windowSize = (1U << windowLog);
3133*01826a49SYabin Cui windowSize += (windowSize >> 3) * (wlByte&7);
3134*01826a49SYabin Cui }
3135*01826a49SYabin Cui
3136*01826a49SYabin Cui switch(dictIDSizeCode)
3137*01826a49SYabin Cui {
3138*01826a49SYabin Cui default: /* impossible */
3139*01826a49SYabin Cui case 0 : break;
3140*01826a49SYabin Cui case 1 : dictID = ip[pos]; pos++; break;
3141*01826a49SYabin Cui case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break;
3142*01826a49SYabin Cui case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break;
3143*01826a49SYabin Cui }
3144*01826a49SYabin Cui switch(fcsID)
3145*01826a49SYabin Cui {
3146*01826a49SYabin Cui default: /* impossible */
3147*01826a49SYabin Cui case 0 : if (directMode) frameContentSize = ip[pos]; break;
3148*01826a49SYabin Cui case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break;
3149*01826a49SYabin Cui case 2 : frameContentSize = MEM_readLE32(ip+pos); break;
3150*01826a49SYabin Cui case 3 : frameContentSize = MEM_readLE64(ip+pos); break;
3151*01826a49SYabin Cui }
3152*01826a49SYabin Cui if (!windowSize) windowSize = (U32)frameContentSize;
3153*01826a49SYabin Cui if (windowSize > windowSizeMax)
3154*01826a49SYabin Cui return ERROR(frameParameter_unsupported);
3155*01826a49SYabin Cui fparamsPtr->frameContentSize = frameContentSize;
3156*01826a49SYabin Cui fparamsPtr->windowSize = windowSize;
3157*01826a49SYabin Cui fparamsPtr->dictID = dictID;
3158*01826a49SYabin Cui fparamsPtr->checksumFlag = checksumFlag;
3159*01826a49SYabin Cui }
3160*01826a49SYabin Cui return 0;
3161*01826a49SYabin Cui }
3162*01826a49SYabin Cui
3163*01826a49SYabin Cui
3164*01826a49SYabin Cui /** ZSTDv07_getDecompressedSize() :
3165*01826a49SYabin Cui * compatible with legacy mode
3166*01826a49SYabin Cui * @return : decompressed size if known, 0 otherwise
3167*01826a49SYabin Cui note : 0 can mean any of the following :
3168*01826a49SYabin Cui - decompressed size is not provided within frame header
3169*01826a49SYabin Cui - frame header unknown / not supported
3170*01826a49SYabin Cui - frame header not completely provided (`srcSize` too small) */
ZSTDv07_getDecompressedSize(const void * src,size_t srcSize)3171*01826a49SYabin Cui unsigned long long ZSTDv07_getDecompressedSize(const void* src, size_t srcSize)
3172*01826a49SYabin Cui {
3173*01826a49SYabin Cui ZSTDv07_frameParams fparams;
3174*01826a49SYabin Cui size_t const frResult = ZSTDv07_getFrameParams(&fparams, src, srcSize);
3175*01826a49SYabin Cui if (frResult!=0) return 0;
3176*01826a49SYabin Cui return fparams.frameContentSize;
3177*01826a49SYabin Cui }
3178*01826a49SYabin Cui
3179*01826a49SYabin Cui
3180*01826a49SYabin Cui /** ZSTDv07_decodeFrameHeader() :
3181*01826a49SYabin Cui * `srcSize` must be the size provided by ZSTDv07_frameHeaderSize().
3182*01826a49SYabin Cui * @return : 0 if success, or an error code, which can be tested using ZSTDv07_isError() */
ZSTDv07_decodeFrameHeader(ZSTDv07_DCtx * dctx,const void * src,size_t srcSize)3183*01826a49SYabin Cui static size_t ZSTDv07_decodeFrameHeader(ZSTDv07_DCtx* dctx, const void* src, size_t srcSize)
3184*01826a49SYabin Cui {
3185*01826a49SYabin Cui size_t const result = ZSTDv07_getFrameParams(&(dctx->fParams), src, srcSize);
3186*01826a49SYabin Cui if (dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID)) return ERROR(dictionary_wrong);
3187*01826a49SYabin Cui if (dctx->fParams.checksumFlag) XXH64_reset(&dctx->xxhState, 0);
3188*01826a49SYabin Cui return result;
3189*01826a49SYabin Cui }
3190*01826a49SYabin Cui
3191*01826a49SYabin Cui
3192*01826a49SYabin Cui typedef struct
3193*01826a49SYabin Cui {
3194*01826a49SYabin Cui blockType_t blockType;
3195*01826a49SYabin Cui U32 origSize;
3196*01826a49SYabin Cui } blockProperties_t;
3197*01826a49SYabin Cui
3198*01826a49SYabin Cui /*! ZSTDv07_getcBlockSize() :
3199*01826a49SYabin Cui * Provides the size of compressed block from block header `src` */
ZSTDv07_getcBlockSize(const void * src,size_t srcSize,blockProperties_t * bpPtr)3200*01826a49SYabin Cui static size_t ZSTDv07_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
3201*01826a49SYabin Cui {
3202*01826a49SYabin Cui const BYTE* const in = (const BYTE*)src;
3203*01826a49SYabin Cui U32 cSize;
3204*01826a49SYabin Cui
3205*01826a49SYabin Cui if (srcSize < ZSTDv07_blockHeaderSize) return ERROR(srcSize_wrong);
3206*01826a49SYabin Cui
3207*01826a49SYabin Cui bpPtr->blockType = (blockType_t)((*in) >> 6);
3208*01826a49SYabin Cui cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16);
3209*01826a49SYabin Cui bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0;
3210*01826a49SYabin Cui
3211*01826a49SYabin Cui if (bpPtr->blockType == bt_end) return 0;
3212*01826a49SYabin Cui if (bpPtr->blockType == bt_rle) return 1;
3213*01826a49SYabin Cui return cSize;
3214*01826a49SYabin Cui }
3215*01826a49SYabin Cui
3216*01826a49SYabin Cui
ZSTDv07_copyRawBlock(void * dst,size_t dstCapacity,const void * src,size_t srcSize)3217*01826a49SYabin Cui static size_t ZSTDv07_copyRawBlock(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
3218*01826a49SYabin Cui {
3219*01826a49SYabin Cui if (srcSize > dstCapacity) return ERROR(dstSize_tooSmall);
3220*01826a49SYabin Cui if (srcSize > 0) {
3221*01826a49SYabin Cui memcpy(dst, src, srcSize);
3222*01826a49SYabin Cui }
3223*01826a49SYabin Cui return srcSize;
3224*01826a49SYabin Cui }
3225*01826a49SYabin Cui
3226*01826a49SYabin Cui
3227*01826a49SYabin Cui /*! ZSTDv07_decodeLiteralsBlock() :
3228*01826a49SYabin Cui @return : nb of bytes read from src (< srcSize ) */
ZSTDv07_decodeLiteralsBlock(ZSTDv07_DCtx * dctx,const void * src,size_t srcSize)3229*01826a49SYabin Cui static size_t ZSTDv07_decodeLiteralsBlock(ZSTDv07_DCtx* dctx,
3230*01826a49SYabin Cui const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
3231*01826a49SYabin Cui {
3232*01826a49SYabin Cui const BYTE* const istart = (const BYTE*) src;
3233*01826a49SYabin Cui
3234*01826a49SYabin Cui if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected);
3235*01826a49SYabin Cui
3236*01826a49SYabin Cui switch((litBlockType_t)(istart[0]>> 6))
3237*01826a49SYabin Cui {
3238*01826a49SYabin Cui case lbt_huffman:
3239*01826a49SYabin Cui { size_t litSize, litCSize, singleStream=0;
3240*01826a49SYabin Cui U32 lhSize = (istart[0] >> 4) & 3;
3241*01826a49SYabin Cui if (srcSize < 5) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for lhSize, + cSize (+nbSeq) */
3242*01826a49SYabin Cui switch(lhSize)
3243*01826a49SYabin Cui {
3244*01826a49SYabin Cui case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */
3245*01826a49SYabin Cui /* 2 - 2 - 10 - 10 */
3246*01826a49SYabin Cui lhSize=3;
3247*01826a49SYabin Cui singleStream = istart[0] & 16;
3248*01826a49SYabin Cui litSize = ((istart[0] & 15) << 6) + (istart[1] >> 2);
3249*01826a49SYabin Cui litCSize = ((istart[1] & 3) << 8) + istart[2];
3250*01826a49SYabin Cui break;
3251*01826a49SYabin Cui case 2:
3252*01826a49SYabin Cui /* 2 - 2 - 14 - 14 */
3253*01826a49SYabin Cui lhSize=4;
3254*01826a49SYabin Cui litSize = ((istart[0] & 15) << 10) + (istart[1] << 2) + (istart[2] >> 6);
3255*01826a49SYabin Cui litCSize = ((istart[2] & 63) << 8) + istart[3];
3256*01826a49SYabin Cui break;
3257*01826a49SYabin Cui case 3:
3258*01826a49SYabin Cui /* 2 - 2 - 18 - 18 */
3259*01826a49SYabin Cui lhSize=5;
3260*01826a49SYabin Cui litSize = ((istart[0] & 15) << 14) + (istart[1] << 6) + (istart[2] >> 2);
3261*01826a49SYabin Cui litCSize = ((istart[2] & 3) << 16) + (istart[3] << 8) + istart[4];
3262*01826a49SYabin Cui break;
3263*01826a49SYabin Cui }
3264*01826a49SYabin Cui if (litSize > ZSTDv07_BLOCKSIZE_ABSOLUTEMAX) return ERROR(corruption_detected);
3265*01826a49SYabin Cui if (litCSize + lhSize > srcSize) return ERROR(corruption_detected);
3266*01826a49SYabin Cui
3267*01826a49SYabin Cui if (HUFv07_isError(singleStream ?
3268*01826a49SYabin Cui HUFv07_decompress1X2_DCtx(dctx->hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize) :
3269*01826a49SYabin Cui HUFv07_decompress4X_hufOnly (dctx->hufTable, dctx->litBuffer, litSize, istart+lhSize, litCSize) ))
3270*01826a49SYabin Cui return ERROR(corruption_detected);
3271*01826a49SYabin Cui
3272*01826a49SYabin Cui dctx->litPtr = dctx->litBuffer;
3273*01826a49SYabin Cui dctx->litSize = litSize;
3274*01826a49SYabin Cui dctx->litEntropy = 1;
3275*01826a49SYabin Cui memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
3276*01826a49SYabin Cui return litCSize + lhSize;
3277*01826a49SYabin Cui }
3278*01826a49SYabin Cui case lbt_repeat:
3279*01826a49SYabin Cui { size_t litSize, litCSize;
3280*01826a49SYabin Cui U32 lhSize = ((istart[0]) >> 4) & 3;
3281*01826a49SYabin Cui if (lhSize != 1) /* only case supported for now : small litSize, single stream */
3282*01826a49SYabin Cui return ERROR(corruption_detected);
3283*01826a49SYabin Cui if (dctx->litEntropy==0)
3284*01826a49SYabin Cui return ERROR(dictionary_corrupted);
3285*01826a49SYabin Cui
3286*01826a49SYabin Cui /* 2 - 2 - 10 - 10 */
3287*01826a49SYabin Cui lhSize=3;
3288*01826a49SYabin Cui litSize = ((istart[0] & 15) << 6) + (istart[1] >> 2);
3289*01826a49SYabin Cui litCSize = ((istart[1] & 3) << 8) + istart[2];
3290*01826a49SYabin Cui if (litCSize + lhSize > srcSize) return ERROR(corruption_detected);
3291*01826a49SYabin Cui
3292*01826a49SYabin Cui { size_t const errorCode = HUFv07_decompress1X4_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->hufTable);
3293*01826a49SYabin Cui if (HUFv07_isError(errorCode)) return ERROR(corruption_detected);
3294*01826a49SYabin Cui }
3295*01826a49SYabin Cui dctx->litPtr = dctx->litBuffer;
3296*01826a49SYabin Cui dctx->litSize = litSize;
3297*01826a49SYabin Cui memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
3298*01826a49SYabin Cui return litCSize + lhSize;
3299*01826a49SYabin Cui }
3300*01826a49SYabin Cui case lbt_raw:
3301*01826a49SYabin Cui { size_t litSize;
3302*01826a49SYabin Cui U32 lhSize = ((istart[0]) >> 4) & 3;
3303*01826a49SYabin Cui switch(lhSize)
3304*01826a49SYabin Cui {
3305*01826a49SYabin Cui case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */
3306*01826a49SYabin Cui lhSize=1;
3307*01826a49SYabin Cui litSize = istart[0] & 31;
3308*01826a49SYabin Cui break;
3309*01826a49SYabin Cui case 2:
3310*01826a49SYabin Cui litSize = ((istart[0] & 15) << 8) + istart[1];
3311*01826a49SYabin Cui break;
3312*01826a49SYabin Cui case 3:
3313*01826a49SYabin Cui litSize = ((istart[0] & 15) << 16) + (istart[1] << 8) + istart[2];
3314*01826a49SYabin Cui break;
3315*01826a49SYabin Cui }
3316*01826a49SYabin Cui
3317*01826a49SYabin Cui if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */
3318*01826a49SYabin Cui if (litSize+lhSize > srcSize) return ERROR(corruption_detected);
3319*01826a49SYabin Cui memcpy(dctx->litBuffer, istart+lhSize, litSize);
3320*01826a49SYabin Cui dctx->litPtr = dctx->litBuffer;
3321*01826a49SYabin Cui dctx->litSize = litSize;
3322*01826a49SYabin Cui memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
3323*01826a49SYabin Cui return lhSize+litSize;
3324*01826a49SYabin Cui }
3325*01826a49SYabin Cui /* direct reference into compressed stream */
3326*01826a49SYabin Cui dctx->litPtr = istart+lhSize;
3327*01826a49SYabin Cui dctx->litSize = litSize;
3328*01826a49SYabin Cui return lhSize+litSize;
3329*01826a49SYabin Cui }
3330*01826a49SYabin Cui case lbt_rle:
3331*01826a49SYabin Cui { size_t litSize;
3332*01826a49SYabin Cui U32 lhSize = ((istart[0]) >> 4) & 3;
3333*01826a49SYabin Cui switch(lhSize)
3334*01826a49SYabin Cui {
3335*01826a49SYabin Cui case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */
3336*01826a49SYabin Cui lhSize = 1;
3337*01826a49SYabin Cui litSize = istart[0] & 31;
3338*01826a49SYabin Cui break;
3339*01826a49SYabin Cui case 2:
3340*01826a49SYabin Cui litSize = ((istart[0] & 15) << 8) + istart[1];
3341*01826a49SYabin Cui break;
3342*01826a49SYabin Cui case 3:
3343*01826a49SYabin Cui litSize = ((istart[0] & 15) << 16) + (istart[1] << 8) + istart[2];
3344*01826a49SYabin Cui if (srcSize<4) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4 */
3345*01826a49SYabin Cui break;
3346*01826a49SYabin Cui }
3347*01826a49SYabin Cui if (litSize > ZSTDv07_BLOCKSIZE_ABSOLUTEMAX) return ERROR(corruption_detected);
3348*01826a49SYabin Cui memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH);
3349*01826a49SYabin Cui dctx->litPtr = dctx->litBuffer;
3350*01826a49SYabin Cui dctx->litSize = litSize;
3351*01826a49SYabin Cui return lhSize+1;
3352*01826a49SYabin Cui }
3353*01826a49SYabin Cui default:
3354*01826a49SYabin Cui return ERROR(corruption_detected); /* impossible */
3355*01826a49SYabin Cui }
3356*01826a49SYabin Cui }
3357*01826a49SYabin Cui
3358*01826a49SYabin Cui
3359*01826a49SYabin Cui /*! ZSTDv07_buildSeqTable() :
3360*01826a49SYabin Cui @return : nb bytes read from src,
3361*01826a49SYabin Cui or an error code if it fails, testable with ZSTDv07_isError()
3362*01826a49SYabin Cui */
ZSTDv07_buildSeqTable(FSEv07_DTable * DTable,U32 type,U32 max,U32 maxLog,const void * src,size_t srcSize,const S16 * defaultNorm,U32 defaultLog,U32 flagRepeatTable)3363*01826a49SYabin Cui static size_t ZSTDv07_buildSeqTable(FSEv07_DTable* DTable, U32 type, U32 max, U32 maxLog,
3364*01826a49SYabin Cui const void* src, size_t srcSize,
3365*01826a49SYabin Cui const S16* defaultNorm, U32 defaultLog, U32 flagRepeatTable)
3366*01826a49SYabin Cui {
3367*01826a49SYabin Cui switch(type)
3368*01826a49SYabin Cui {
3369*01826a49SYabin Cui case FSEv07_ENCODING_RLE :
3370*01826a49SYabin Cui if (!srcSize) return ERROR(srcSize_wrong);
3371*01826a49SYabin Cui if ( (*(const BYTE*)src) > max) return ERROR(corruption_detected);
3372*01826a49SYabin Cui FSEv07_buildDTable_rle(DTable, *(const BYTE*)src); /* if *src > max, data is corrupted */
3373*01826a49SYabin Cui return 1;
3374*01826a49SYabin Cui case FSEv07_ENCODING_RAW :
3375*01826a49SYabin Cui FSEv07_buildDTable(DTable, defaultNorm, max, defaultLog);
3376*01826a49SYabin Cui return 0;
3377*01826a49SYabin Cui case FSEv07_ENCODING_STATIC:
3378*01826a49SYabin Cui if (!flagRepeatTable) return ERROR(corruption_detected);
3379*01826a49SYabin Cui return 0;
3380*01826a49SYabin Cui default : /* impossible */
3381*01826a49SYabin Cui case FSEv07_ENCODING_DYNAMIC :
3382*01826a49SYabin Cui { U32 tableLog;
3383*01826a49SYabin Cui S16 norm[MaxSeq+1];
3384*01826a49SYabin Cui size_t const headerSize = FSEv07_readNCount(norm, &max, &tableLog, src, srcSize);
3385*01826a49SYabin Cui if (FSEv07_isError(headerSize)) return ERROR(corruption_detected);
3386*01826a49SYabin Cui if (tableLog > maxLog) return ERROR(corruption_detected);
3387*01826a49SYabin Cui FSEv07_buildDTable(DTable, norm, max, tableLog);
3388*01826a49SYabin Cui return headerSize;
3389*01826a49SYabin Cui } }
3390*01826a49SYabin Cui }
3391*01826a49SYabin Cui
3392*01826a49SYabin Cui
ZSTDv07_decodeSeqHeaders(int * nbSeqPtr,FSEv07_DTable * DTableLL,FSEv07_DTable * DTableML,FSEv07_DTable * DTableOffb,U32 flagRepeatTable,const void * src,size_t srcSize)3393*01826a49SYabin Cui static size_t ZSTDv07_decodeSeqHeaders(int* nbSeqPtr,
3394*01826a49SYabin Cui FSEv07_DTable* DTableLL, FSEv07_DTable* DTableML, FSEv07_DTable* DTableOffb, U32 flagRepeatTable,
3395*01826a49SYabin Cui const void* src, size_t srcSize)
3396*01826a49SYabin Cui {
3397*01826a49SYabin Cui const BYTE* const istart = (const BYTE*)src;
3398*01826a49SYabin Cui const BYTE* const iend = istart + srcSize;
3399*01826a49SYabin Cui const BYTE* ip = istart;
3400*01826a49SYabin Cui
3401*01826a49SYabin Cui /* check */
3402*01826a49SYabin Cui if (srcSize < MIN_SEQUENCES_SIZE) return ERROR(srcSize_wrong);
3403*01826a49SYabin Cui
3404*01826a49SYabin Cui /* SeqHead */
3405*01826a49SYabin Cui { int nbSeq = *ip++;
3406*01826a49SYabin Cui if (!nbSeq) { *nbSeqPtr=0; return 1; }
3407*01826a49SYabin Cui if (nbSeq > 0x7F) {
3408*01826a49SYabin Cui if (nbSeq == 0xFF) {
3409*01826a49SYabin Cui if (ip+2 > iend) return ERROR(srcSize_wrong);
3410*01826a49SYabin Cui nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2;
3411*01826a49SYabin Cui } else {
3412*01826a49SYabin Cui if (ip >= iend) return ERROR(srcSize_wrong);
3413*01826a49SYabin Cui nbSeq = ((nbSeq-0x80)<<8) + *ip++;
3414*01826a49SYabin Cui }
3415*01826a49SYabin Cui }
3416*01826a49SYabin Cui *nbSeqPtr = nbSeq;
3417*01826a49SYabin Cui }
3418*01826a49SYabin Cui
3419*01826a49SYabin Cui /* FSE table descriptors */
3420*01826a49SYabin Cui if (ip + 4 > iend) return ERROR(srcSize_wrong); /* min : header byte + all 3 are "raw", hence no header, but at least xxLog bits per type */
3421*01826a49SYabin Cui { U32 const LLtype = *ip >> 6;
3422*01826a49SYabin Cui U32 const OFtype = (*ip >> 4) & 3;
3423*01826a49SYabin Cui U32 const MLtype = (*ip >> 2) & 3;
3424*01826a49SYabin Cui ip++;
3425*01826a49SYabin Cui
3426*01826a49SYabin Cui /* Build DTables */
3427*01826a49SYabin Cui { size_t const llhSize = ZSTDv07_buildSeqTable(DTableLL, LLtype, MaxLL, LLFSELog, ip, iend-ip, LL_defaultNorm, LL_defaultNormLog, flagRepeatTable);
3428*01826a49SYabin Cui if (ZSTDv07_isError(llhSize)) return ERROR(corruption_detected);
3429*01826a49SYabin Cui ip += llhSize;
3430*01826a49SYabin Cui }
3431*01826a49SYabin Cui { size_t const ofhSize = ZSTDv07_buildSeqTable(DTableOffb, OFtype, MaxOff, OffFSELog, ip, iend-ip, OF_defaultNorm, OF_defaultNormLog, flagRepeatTable);
3432*01826a49SYabin Cui if (ZSTDv07_isError(ofhSize)) return ERROR(corruption_detected);
3433*01826a49SYabin Cui ip += ofhSize;
3434*01826a49SYabin Cui }
3435*01826a49SYabin Cui { size_t const mlhSize = ZSTDv07_buildSeqTable(DTableML, MLtype, MaxML, MLFSELog, ip, iend-ip, ML_defaultNorm, ML_defaultNormLog, flagRepeatTable);
3436*01826a49SYabin Cui if (ZSTDv07_isError(mlhSize)) return ERROR(corruption_detected);
3437*01826a49SYabin Cui ip += mlhSize;
3438*01826a49SYabin Cui } }
3439*01826a49SYabin Cui
3440*01826a49SYabin Cui return ip-istart;
3441*01826a49SYabin Cui }
3442*01826a49SYabin Cui
3443*01826a49SYabin Cui
3444*01826a49SYabin Cui typedef struct {
3445*01826a49SYabin Cui size_t litLength;
3446*01826a49SYabin Cui size_t matchLength;
3447*01826a49SYabin Cui size_t offset;
3448*01826a49SYabin Cui } seq_t;
3449*01826a49SYabin Cui
3450*01826a49SYabin Cui typedef struct {
3451*01826a49SYabin Cui BITv07_DStream_t DStream;
3452*01826a49SYabin Cui FSEv07_DState_t stateLL;
3453*01826a49SYabin Cui FSEv07_DState_t stateOffb;
3454*01826a49SYabin Cui FSEv07_DState_t stateML;
3455*01826a49SYabin Cui size_t prevOffset[ZSTDv07_REP_INIT];
3456*01826a49SYabin Cui } seqState_t;
3457*01826a49SYabin Cui
3458*01826a49SYabin Cui
ZSTDv07_decodeSequence(seqState_t * seqState)3459*01826a49SYabin Cui static seq_t ZSTDv07_decodeSequence(seqState_t* seqState)
3460*01826a49SYabin Cui {
3461*01826a49SYabin Cui seq_t seq;
3462*01826a49SYabin Cui
3463*01826a49SYabin Cui U32 const llCode = FSEv07_peekSymbol(&(seqState->stateLL));
3464*01826a49SYabin Cui U32 const mlCode = FSEv07_peekSymbol(&(seqState->stateML));
3465*01826a49SYabin Cui U32 const ofCode = FSEv07_peekSymbol(&(seqState->stateOffb)); /* <= maxOff, by table construction */
3466*01826a49SYabin Cui
3467*01826a49SYabin Cui U32 const llBits = LL_bits[llCode];
3468*01826a49SYabin Cui U32 const mlBits = ML_bits[mlCode];
3469*01826a49SYabin Cui U32 const ofBits = ofCode;
3470*01826a49SYabin Cui U32 const totalBits = llBits+mlBits+ofBits;
3471*01826a49SYabin Cui
3472*01826a49SYabin Cui static const U32 LL_base[MaxLL+1] = {
3473*01826a49SYabin Cui 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
3474*01826a49SYabin Cui 16, 18, 20, 22, 24, 28, 32, 40, 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000,
3475*01826a49SYabin Cui 0x2000, 0x4000, 0x8000, 0x10000 };
3476*01826a49SYabin Cui
3477*01826a49SYabin Cui static const U32 ML_base[MaxML+1] = {
3478*01826a49SYabin Cui 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
3479*01826a49SYabin Cui 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
3480*01826a49SYabin Cui 35, 37, 39, 41, 43, 47, 51, 59, 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803,
3481*01826a49SYabin Cui 0x1003, 0x2003, 0x4003, 0x8003, 0x10003 };
3482*01826a49SYabin Cui
3483*01826a49SYabin Cui static const U32 OF_base[MaxOff+1] = {
3484*01826a49SYabin Cui 0, 1, 1, 5, 0xD, 0x1D, 0x3D, 0x7D,
3485*01826a49SYabin Cui 0xFD, 0x1FD, 0x3FD, 0x7FD, 0xFFD, 0x1FFD, 0x3FFD, 0x7FFD,
3486*01826a49SYabin Cui 0xFFFD, 0x1FFFD, 0x3FFFD, 0x7FFFD, 0xFFFFD, 0x1FFFFD, 0x3FFFFD, 0x7FFFFD,
3487*01826a49SYabin Cui 0xFFFFFD, 0x1FFFFFD, 0x3FFFFFD, 0x7FFFFFD, 0xFFFFFFD };
3488*01826a49SYabin Cui
3489*01826a49SYabin Cui /* sequence */
3490*01826a49SYabin Cui { size_t offset;
3491*01826a49SYabin Cui if (!ofCode)
3492*01826a49SYabin Cui offset = 0;
3493*01826a49SYabin Cui else {
3494*01826a49SYabin Cui offset = OF_base[ofCode] + BITv07_readBits(&(seqState->DStream), ofBits); /* <= (ZSTDv07_WINDOWLOG_MAX-1) bits */
3495*01826a49SYabin Cui if (MEM_32bits()) BITv07_reloadDStream(&(seqState->DStream));
3496*01826a49SYabin Cui }
3497*01826a49SYabin Cui
3498*01826a49SYabin Cui if (ofCode <= 1) {
3499*01826a49SYabin Cui if ((llCode == 0) & (offset <= 1)) offset = 1-offset;
3500*01826a49SYabin Cui if (offset) {
3501*01826a49SYabin Cui size_t const temp = seqState->prevOffset[offset];
3502*01826a49SYabin Cui if (offset != 1) seqState->prevOffset[2] = seqState->prevOffset[1];
3503*01826a49SYabin Cui seqState->prevOffset[1] = seqState->prevOffset[0];
3504*01826a49SYabin Cui seqState->prevOffset[0] = offset = temp;
3505*01826a49SYabin Cui } else {
3506*01826a49SYabin Cui offset = seqState->prevOffset[0];
3507*01826a49SYabin Cui }
3508*01826a49SYabin Cui } else {
3509*01826a49SYabin Cui seqState->prevOffset[2] = seqState->prevOffset[1];
3510*01826a49SYabin Cui seqState->prevOffset[1] = seqState->prevOffset[0];
3511*01826a49SYabin Cui seqState->prevOffset[0] = offset;
3512*01826a49SYabin Cui }
3513*01826a49SYabin Cui seq.offset = offset;
3514*01826a49SYabin Cui }
3515*01826a49SYabin Cui
3516*01826a49SYabin Cui seq.matchLength = ML_base[mlCode] + ((mlCode>31) ? BITv07_readBits(&(seqState->DStream), mlBits) : 0); /* <= 16 bits */
3517*01826a49SYabin Cui if (MEM_32bits() && (mlBits+llBits>24)) BITv07_reloadDStream(&(seqState->DStream));
3518*01826a49SYabin Cui
3519*01826a49SYabin Cui seq.litLength = LL_base[llCode] + ((llCode>15) ? BITv07_readBits(&(seqState->DStream), llBits) : 0); /* <= 16 bits */
3520*01826a49SYabin Cui if (MEM_32bits() ||
3521*01826a49SYabin Cui (totalBits > 64 - 7 - (LLFSELog+MLFSELog+OffFSELog)) ) BITv07_reloadDStream(&(seqState->DStream));
3522*01826a49SYabin Cui
3523*01826a49SYabin Cui /* ANS state update */
3524*01826a49SYabin Cui FSEv07_updateState(&(seqState->stateLL), &(seqState->DStream)); /* <= 9 bits */
3525*01826a49SYabin Cui FSEv07_updateState(&(seqState->stateML), &(seqState->DStream)); /* <= 9 bits */
3526*01826a49SYabin Cui if (MEM_32bits()) BITv07_reloadDStream(&(seqState->DStream)); /* <= 18 bits */
3527*01826a49SYabin Cui FSEv07_updateState(&(seqState->stateOffb), &(seqState->DStream)); /* <= 8 bits */
3528*01826a49SYabin Cui
3529*01826a49SYabin Cui return seq;
3530*01826a49SYabin Cui }
3531*01826a49SYabin Cui
3532*01826a49SYabin Cui
3533*01826a49SYabin Cui static
ZSTDv07_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)3534*01826a49SYabin Cui size_t ZSTDv07_execSequence(BYTE* op,
3535*01826a49SYabin Cui BYTE* const oend, seq_t sequence,
3536*01826a49SYabin Cui const BYTE** litPtr, const BYTE* const litLimit,
3537*01826a49SYabin Cui const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
3538*01826a49SYabin Cui {
3539*01826a49SYabin Cui BYTE* const oLitEnd = op + sequence.litLength;
3540*01826a49SYabin Cui size_t const sequenceLength = sequence.litLength + sequence.matchLength;
3541*01826a49SYabin Cui BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
3542*01826a49SYabin Cui BYTE* const oend_w = oend-WILDCOPY_OVERLENGTH;
3543*01826a49SYabin Cui const BYTE* const iLitEnd = *litPtr + sequence.litLength;
3544*01826a49SYabin Cui const BYTE* match = oLitEnd - sequence.offset;
3545*01826a49SYabin Cui
3546*01826a49SYabin Cui /* check */
3547*01826a49SYabin Cui assert(oend >= op);
3548*01826a49SYabin Cui if (sequence.litLength + WILDCOPY_OVERLENGTH > (size_t)(oend - op)) return ERROR(dstSize_tooSmall);
3549*01826a49SYabin Cui if (sequenceLength > (size_t)(oend - op)) return ERROR(dstSize_tooSmall);
3550*01826a49SYabin Cui assert(litLimit >= *litPtr);
3551*01826a49SYabin Cui if (sequence.litLength > (size_t)(litLimit - *litPtr)) return ERROR(corruption_detected);;
3552*01826a49SYabin Cui
3553*01826a49SYabin Cui /* copy Literals */
3554*01826a49SYabin Cui ZSTDv07_wildcopy(op, *litPtr, (ptrdiff_t)sequence.litLength); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */
3555*01826a49SYabin Cui op = oLitEnd;
3556*01826a49SYabin Cui *litPtr = iLitEnd; /* update for next sequence */
3557*01826a49SYabin Cui
3558*01826a49SYabin Cui /* copy Match */
3559*01826a49SYabin Cui if (sequence.offset > (size_t)(oLitEnd - base)) {
3560*01826a49SYabin Cui /* offset beyond prefix */
3561*01826a49SYabin Cui if (sequence.offset > (size_t)(oLitEnd - vBase)) return ERROR(corruption_detected);
3562*01826a49SYabin Cui match = dictEnd - (base-match);
3563*01826a49SYabin Cui if (match + sequence.matchLength <= dictEnd) {
3564*01826a49SYabin Cui memmove(oLitEnd, match, sequence.matchLength);
3565*01826a49SYabin Cui return sequenceLength;
3566*01826a49SYabin Cui }
3567*01826a49SYabin Cui /* span extDict & currentPrefixSegment */
3568*01826a49SYabin Cui { size_t const length1 = (size_t)(dictEnd - match);
3569*01826a49SYabin Cui memmove(oLitEnd, match, length1);
3570*01826a49SYabin Cui op = oLitEnd + length1;
3571*01826a49SYabin Cui sequence.matchLength -= length1;
3572*01826a49SYabin Cui match = base;
3573*01826a49SYabin Cui if (op > oend_w || sequence.matchLength < MINMATCH) {
3574*01826a49SYabin Cui while (op < oMatchEnd) *op++ = *match++;
3575*01826a49SYabin Cui return sequenceLength;
3576*01826a49SYabin Cui }
3577*01826a49SYabin Cui } }
3578*01826a49SYabin Cui /* Requirement: op <= oend_w */
3579*01826a49SYabin Cui
3580*01826a49SYabin Cui /* match within prefix */
3581*01826a49SYabin Cui if (sequence.offset < 8) {
3582*01826a49SYabin Cui /* close range match, overlap */
3583*01826a49SYabin Cui static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
3584*01826a49SYabin Cui static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */
3585*01826a49SYabin Cui int const sub2 = dec64table[sequence.offset];
3586*01826a49SYabin Cui op[0] = match[0];
3587*01826a49SYabin Cui op[1] = match[1];
3588*01826a49SYabin Cui op[2] = match[2];
3589*01826a49SYabin Cui op[3] = match[3];
3590*01826a49SYabin Cui match += dec32table[sequence.offset];
3591*01826a49SYabin Cui ZSTDv07_copy4(op+4, match);
3592*01826a49SYabin Cui match -= sub2;
3593*01826a49SYabin Cui } else {
3594*01826a49SYabin Cui ZSTDv07_copy8(op, match);
3595*01826a49SYabin Cui }
3596*01826a49SYabin Cui op += 8; match += 8;
3597*01826a49SYabin Cui
3598*01826a49SYabin Cui if (oMatchEnd > oend-(16-MINMATCH)) {
3599*01826a49SYabin Cui if (op < oend_w) {
3600*01826a49SYabin Cui ZSTDv07_wildcopy(op, match, oend_w - op);
3601*01826a49SYabin Cui match += oend_w - op;
3602*01826a49SYabin Cui op = oend_w;
3603*01826a49SYabin Cui }
3604*01826a49SYabin Cui while (op < oMatchEnd) *op++ = *match++;
3605*01826a49SYabin Cui } else {
3606*01826a49SYabin Cui ZSTDv07_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */
3607*01826a49SYabin Cui }
3608*01826a49SYabin Cui return sequenceLength;
3609*01826a49SYabin Cui }
3610*01826a49SYabin Cui
3611*01826a49SYabin Cui
ZSTDv07_decompressSequences(ZSTDv07_DCtx * dctx,void * dst,size_t maxDstSize,const void * seqStart,size_t seqSize)3612*01826a49SYabin Cui static size_t ZSTDv07_decompressSequences(
3613*01826a49SYabin Cui ZSTDv07_DCtx* dctx,
3614*01826a49SYabin Cui void* dst, size_t maxDstSize,
3615*01826a49SYabin Cui const void* seqStart, size_t seqSize)
3616*01826a49SYabin Cui {
3617*01826a49SYabin Cui const BYTE* ip = (const BYTE*)seqStart;
3618*01826a49SYabin Cui const BYTE* const iend = ip + seqSize;
3619*01826a49SYabin Cui BYTE* const ostart = (BYTE*)dst;
3620*01826a49SYabin Cui BYTE* const oend = ostart + maxDstSize;
3621*01826a49SYabin Cui BYTE* op = ostart;
3622*01826a49SYabin Cui const BYTE* litPtr = dctx->litPtr;
3623*01826a49SYabin Cui const BYTE* const litEnd = litPtr + dctx->litSize;
3624*01826a49SYabin Cui FSEv07_DTable* DTableLL = dctx->LLTable;
3625*01826a49SYabin Cui FSEv07_DTable* DTableML = dctx->MLTable;
3626*01826a49SYabin Cui FSEv07_DTable* DTableOffb = dctx->OffTable;
3627*01826a49SYabin Cui const BYTE* const base = (const BYTE*) (dctx->base);
3628*01826a49SYabin Cui const BYTE* const vBase = (const BYTE*) (dctx->vBase);
3629*01826a49SYabin Cui const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
3630*01826a49SYabin Cui int nbSeq;
3631*01826a49SYabin Cui
3632*01826a49SYabin Cui /* Build Decoding Tables */
3633*01826a49SYabin Cui { size_t const seqHSize = ZSTDv07_decodeSeqHeaders(&nbSeq, DTableLL, DTableML, DTableOffb, dctx->fseEntropy, ip, seqSize);
3634*01826a49SYabin Cui if (ZSTDv07_isError(seqHSize)) return seqHSize;
3635*01826a49SYabin Cui ip += seqHSize;
3636*01826a49SYabin Cui }
3637*01826a49SYabin Cui
3638*01826a49SYabin Cui /* Regen sequences */
3639*01826a49SYabin Cui if (nbSeq) {
3640*01826a49SYabin Cui seqState_t seqState;
3641*01826a49SYabin Cui dctx->fseEntropy = 1;
3642*01826a49SYabin Cui { U32 i; for (i=0; i<ZSTDv07_REP_INIT; i++) seqState.prevOffset[i] = dctx->rep[i]; }
3643*01826a49SYabin Cui { size_t const errorCode = BITv07_initDStream(&(seqState.DStream), ip, iend-ip);
3644*01826a49SYabin Cui if (ERR_isError(errorCode)) return ERROR(corruption_detected); }
3645*01826a49SYabin Cui FSEv07_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL);
3646*01826a49SYabin Cui FSEv07_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb);
3647*01826a49SYabin Cui FSEv07_initDState(&(seqState.stateML), &(seqState.DStream), DTableML);
3648*01826a49SYabin Cui
3649*01826a49SYabin Cui for ( ; (BITv07_reloadDStream(&(seqState.DStream)) <= BITv07_DStream_completed) && nbSeq ; ) {
3650*01826a49SYabin Cui nbSeq--;
3651*01826a49SYabin Cui { seq_t const sequence = ZSTDv07_decodeSequence(&seqState);
3652*01826a49SYabin Cui size_t const oneSeqSize = ZSTDv07_execSequence(op, oend, sequence, &litPtr, litEnd, base, vBase, dictEnd);
3653*01826a49SYabin Cui if (ZSTDv07_isError(oneSeqSize)) return oneSeqSize;
3654*01826a49SYabin Cui op += oneSeqSize;
3655*01826a49SYabin Cui } }
3656*01826a49SYabin Cui
3657*01826a49SYabin Cui /* check if reached exact end */
3658*01826a49SYabin Cui if (nbSeq) return ERROR(corruption_detected);
3659*01826a49SYabin Cui /* save reps for next block */
3660*01826a49SYabin Cui { U32 i; for (i=0; i<ZSTDv07_REP_INIT; i++) dctx->rep[i] = (U32)(seqState.prevOffset[i]); }
3661*01826a49SYabin Cui }
3662*01826a49SYabin Cui
3663*01826a49SYabin Cui /* last literal segment */
3664*01826a49SYabin Cui { size_t const lastLLSize = litEnd - litPtr;
3665*01826a49SYabin Cui /* if (litPtr > litEnd) return ERROR(corruption_detected); */ /* too many literals already used */
3666*01826a49SYabin Cui if (lastLLSize > (size_t)(oend-op)) return ERROR(dstSize_tooSmall);
3667*01826a49SYabin Cui if (lastLLSize > 0) {
3668*01826a49SYabin Cui memcpy(op, litPtr, lastLLSize);
3669*01826a49SYabin Cui op += lastLLSize;
3670*01826a49SYabin Cui }
3671*01826a49SYabin Cui }
3672*01826a49SYabin Cui
3673*01826a49SYabin Cui return op-ostart;
3674*01826a49SYabin Cui }
3675*01826a49SYabin Cui
3676*01826a49SYabin Cui
ZSTDv07_checkContinuity(ZSTDv07_DCtx * dctx,const void * dst)3677*01826a49SYabin Cui static void ZSTDv07_checkContinuity(ZSTDv07_DCtx* dctx, const void* dst)
3678*01826a49SYabin Cui {
3679*01826a49SYabin Cui if (dst != dctx->previousDstEnd) { /* not contiguous */
3680*01826a49SYabin Cui dctx->dictEnd = dctx->previousDstEnd;
3681*01826a49SYabin Cui dctx->vBase = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base));
3682*01826a49SYabin Cui dctx->base = dst;
3683*01826a49SYabin Cui dctx->previousDstEnd = dst;
3684*01826a49SYabin Cui }
3685*01826a49SYabin Cui }
3686*01826a49SYabin Cui
3687*01826a49SYabin Cui
ZSTDv07_decompressBlock_internal(ZSTDv07_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)3688*01826a49SYabin Cui static size_t ZSTDv07_decompressBlock_internal(ZSTDv07_DCtx* dctx,
3689*01826a49SYabin Cui void* dst, size_t dstCapacity,
3690*01826a49SYabin Cui const void* src, size_t srcSize)
3691*01826a49SYabin Cui { /* blockType == blockCompressed */
3692*01826a49SYabin Cui const BYTE* ip = (const BYTE*)src;
3693*01826a49SYabin Cui
3694*01826a49SYabin Cui if (srcSize >= ZSTDv07_BLOCKSIZE_ABSOLUTEMAX) return ERROR(srcSize_wrong);
3695*01826a49SYabin Cui
3696*01826a49SYabin Cui /* Decode literals sub-block */
3697*01826a49SYabin Cui { size_t const litCSize = ZSTDv07_decodeLiteralsBlock(dctx, src, srcSize);
3698*01826a49SYabin Cui if (ZSTDv07_isError(litCSize)) return litCSize;
3699*01826a49SYabin Cui ip += litCSize;
3700*01826a49SYabin Cui srcSize -= litCSize;
3701*01826a49SYabin Cui }
3702*01826a49SYabin Cui return ZSTDv07_decompressSequences(dctx, dst, dstCapacity, ip, srcSize);
3703*01826a49SYabin Cui }
3704*01826a49SYabin Cui
3705*01826a49SYabin Cui
ZSTDv07_decompressBlock(ZSTDv07_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)3706*01826a49SYabin Cui size_t ZSTDv07_decompressBlock(ZSTDv07_DCtx* dctx,
3707*01826a49SYabin Cui void* dst, size_t dstCapacity,
3708*01826a49SYabin Cui const void* src, size_t srcSize)
3709*01826a49SYabin Cui {
3710*01826a49SYabin Cui size_t dSize;
3711*01826a49SYabin Cui ZSTDv07_checkContinuity(dctx, dst);
3712*01826a49SYabin Cui dSize = ZSTDv07_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize);
3713*01826a49SYabin Cui dctx->previousDstEnd = (char*)dst + dSize;
3714*01826a49SYabin Cui return dSize;
3715*01826a49SYabin Cui }
3716*01826a49SYabin Cui
3717*01826a49SYabin Cui
3718*01826a49SYabin Cui /** ZSTDv07_insertBlock() :
3719*01826a49SYabin Cui insert `src` block into `dctx` history. Useful to track uncompressed blocks. */
ZSTDv07_insertBlock(ZSTDv07_DCtx * dctx,const void * blockStart,size_t blockSize)3720*01826a49SYabin Cui ZSTDLIBv07_API size_t ZSTDv07_insertBlock(ZSTDv07_DCtx* dctx, const void* blockStart, size_t blockSize)
3721*01826a49SYabin Cui {
3722*01826a49SYabin Cui ZSTDv07_checkContinuity(dctx, blockStart);
3723*01826a49SYabin Cui dctx->previousDstEnd = (const char*)blockStart + blockSize;
3724*01826a49SYabin Cui return blockSize;
3725*01826a49SYabin Cui }
3726*01826a49SYabin Cui
3727*01826a49SYabin Cui
ZSTDv07_generateNxBytes(void * dst,size_t dstCapacity,BYTE byte,size_t length)3728*01826a49SYabin Cui static size_t ZSTDv07_generateNxBytes(void* dst, size_t dstCapacity, BYTE byte, size_t length)
3729*01826a49SYabin Cui {
3730*01826a49SYabin Cui if (length > dstCapacity) return ERROR(dstSize_tooSmall);
3731*01826a49SYabin Cui if (length > 0) {
3732*01826a49SYabin Cui memset(dst, byte, length);
3733*01826a49SYabin Cui }
3734*01826a49SYabin Cui return length;
3735*01826a49SYabin Cui }
3736*01826a49SYabin Cui
3737*01826a49SYabin Cui
3738*01826a49SYabin Cui /*! ZSTDv07_decompressFrame() :
3739*01826a49SYabin Cui * `dctx` must be properly initialized */
ZSTDv07_decompressFrame(ZSTDv07_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)3740*01826a49SYabin Cui static size_t ZSTDv07_decompressFrame(ZSTDv07_DCtx* dctx,
3741*01826a49SYabin Cui void* dst, size_t dstCapacity,
3742*01826a49SYabin Cui const void* src, size_t srcSize)
3743*01826a49SYabin Cui {
3744*01826a49SYabin Cui const BYTE* ip = (const BYTE*)src;
3745*01826a49SYabin Cui const BYTE* const iend = ip + srcSize;
3746*01826a49SYabin Cui BYTE* const ostart = (BYTE*)dst;
3747*01826a49SYabin Cui BYTE* const oend = ostart + dstCapacity;
3748*01826a49SYabin Cui BYTE* op = ostart;
3749*01826a49SYabin Cui size_t remainingSize = srcSize;
3750*01826a49SYabin Cui
3751*01826a49SYabin Cui /* check */
3752*01826a49SYabin Cui if (srcSize < ZSTDv07_frameHeaderSize_min+ZSTDv07_blockHeaderSize) return ERROR(srcSize_wrong);
3753*01826a49SYabin Cui
3754*01826a49SYabin Cui /* Frame Header */
3755*01826a49SYabin Cui { size_t const frameHeaderSize = ZSTDv07_frameHeaderSize(src, ZSTDv07_frameHeaderSize_min);
3756*01826a49SYabin Cui if (ZSTDv07_isError(frameHeaderSize)) return frameHeaderSize;
3757*01826a49SYabin Cui if (srcSize < frameHeaderSize+ZSTDv07_blockHeaderSize) return ERROR(srcSize_wrong);
3758*01826a49SYabin Cui if (ZSTDv07_decodeFrameHeader(dctx, src, frameHeaderSize)) return ERROR(corruption_detected);
3759*01826a49SYabin Cui ip += frameHeaderSize; remainingSize -= frameHeaderSize;
3760*01826a49SYabin Cui }
3761*01826a49SYabin Cui
3762*01826a49SYabin Cui /* Loop on each block */
3763*01826a49SYabin Cui while (1) {
3764*01826a49SYabin Cui size_t decodedSize;
3765*01826a49SYabin Cui blockProperties_t blockProperties;
3766*01826a49SYabin Cui size_t const cBlockSize = ZSTDv07_getcBlockSize(ip, iend-ip, &blockProperties);
3767*01826a49SYabin Cui if (ZSTDv07_isError(cBlockSize)) return cBlockSize;
3768*01826a49SYabin Cui
3769*01826a49SYabin Cui ip += ZSTDv07_blockHeaderSize;
3770*01826a49SYabin Cui remainingSize -= ZSTDv07_blockHeaderSize;
3771*01826a49SYabin Cui if (cBlockSize > remainingSize) return ERROR(srcSize_wrong);
3772*01826a49SYabin Cui
3773*01826a49SYabin Cui switch(blockProperties.blockType)
3774*01826a49SYabin Cui {
3775*01826a49SYabin Cui case bt_compressed:
3776*01826a49SYabin Cui decodedSize = ZSTDv07_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize);
3777*01826a49SYabin Cui break;
3778*01826a49SYabin Cui case bt_raw :
3779*01826a49SYabin Cui decodedSize = ZSTDv07_copyRawBlock(op, oend-op, ip, cBlockSize);
3780*01826a49SYabin Cui break;
3781*01826a49SYabin Cui case bt_rle :
3782*01826a49SYabin Cui decodedSize = ZSTDv07_generateNxBytes(op, oend-op, *ip, blockProperties.origSize);
3783*01826a49SYabin Cui break;
3784*01826a49SYabin Cui case bt_end :
3785*01826a49SYabin Cui /* end of frame */
3786*01826a49SYabin Cui if (remainingSize) return ERROR(srcSize_wrong);
3787*01826a49SYabin Cui decodedSize = 0;
3788*01826a49SYabin Cui break;
3789*01826a49SYabin Cui default:
3790*01826a49SYabin Cui return ERROR(GENERIC); /* impossible */
3791*01826a49SYabin Cui }
3792*01826a49SYabin Cui if (blockProperties.blockType == bt_end) break; /* bt_end */
3793*01826a49SYabin Cui
3794*01826a49SYabin Cui if (ZSTDv07_isError(decodedSize)) return decodedSize;
3795*01826a49SYabin Cui if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, op, decodedSize);
3796*01826a49SYabin Cui op += decodedSize;
3797*01826a49SYabin Cui ip += cBlockSize;
3798*01826a49SYabin Cui remainingSize -= cBlockSize;
3799*01826a49SYabin Cui }
3800*01826a49SYabin Cui
3801*01826a49SYabin Cui return op-ostart;
3802*01826a49SYabin Cui }
3803*01826a49SYabin Cui
3804*01826a49SYabin Cui
3805*01826a49SYabin Cui /*! ZSTDv07_decompress_usingPreparedDCtx() :
3806*01826a49SYabin Cui * Same as ZSTDv07_decompress_usingDict, but using a reference context `preparedDCtx`, where dictionary has been loaded.
3807*01826a49SYabin Cui * It avoids reloading the dictionary each time.
3808*01826a49SYabin Cui * `preparedDCtx` must have been properly initialized using ZSTDv07_decompressBegin_usingDict().
3809*01826a49SYabin Cui * Requires 2 contexts : 1 for reference (preparedDCtx), which will not be modified, and 1 to run the decompression operation (dctx) */
ZSTDv07_decompress_usingPreparedDCtx(ZSTDv07_DCtx * dctx,const ZSTDv07_DCtx * refDCtx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)3810*01826a49SYabin Cui static size_t ZSTDv07_decompress_usingPreparedDCtx(ZSTDv07_DCtx* dctx, const ZSTDv07_DCtx* refDCtx,
3811*01826a49SYabin Cui void* dst, size_t dstCapacity,
3812*01826a49SYabin Cui const void* src, size_t srcSize)
3813*01826a49SYabin Cui {
3814*01826a49SYabin Cui ZSTDv07_copyDCtx(dctx, refDCtx);
3815*01826a49SYabin Cui ZSTDv07_checkContinuity(dctx, dst);
3816*01826a49SYabin Cui return ZSTDv07_decompressFrame(dctx, dst, dstCapacity, src, srcSize);
3817*01826a49SYabin Cui }
3818*01826a49SYabin Cui
3819*01826a49SYabin Cui
ZSTDv07_decompress_usingDict(ZSTDv07_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize,const void * dict,size_t dictSize)3820*01826a49SYabin Cui size_t ZSTDv07_decompress_usingDict(ZSTDv07_DCtx* dctx,
3821*01826a49SYabin Cui void* dst, size_t dstCapacity,
3822*01826a49SYabin Cui const void* src, size_t srcSize,
3823*01826a49SYabin Cui const void* dict, size_t dictSize)
3824*01826a49SYabin Cui {
3825*01826a49SYabin Cui ZSTDv07_decompressBegin_usingDict(dctx, dict, dictSize);
3826*01826a49SYabin Cui ZSTDv07_checkContinuity(dctx, dst);
3827*01826a49SYabin Cui return ZSTDv07_decompressFrame(dctx, dst, dstCapacity, src, srcSize);
3828*01826a49SYabin Cui }
3829*01826a49SYabin Cui
3830*01826a49SYabin Cui
ZSTDv07_decompressDCtx(ZSTDv07_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)3831*01826a49SYabin Cui size_t ZSTDv07_decompressDCtx(ZSTDv07_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
3832*01826a49SYabin Cui {
3833*01826a49SYabin Cui return ZSTDv07_decompress_usingDict(dctx, dst, dstCapacity, src, srcSize, NULL, 0);
3834*01826a49SYabin Cui }
3835*01826a49SYabin Cui
3836*01826a49SYabin Cui
ZSTDv07_decompress(void * dst,size_t dstCapacity,const void * src,size_t srcSize)3837*01826a49SYabin Cui size_t ZSTDv07_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
3838*01826a49SYabin Cui {
3839*01826a49SYabin Cui #if defined(ZSTDv07_HEAPMODE) && (ZSTDv07_HEAPMODE==1)
3840*01826a49SYabin Cui size_t regenSize;
3841*01826a49SYabin Cui ZSTDv07_DCtx* const dctx = ZSTDv07_createDCtx();
3842*01826a49SYabin Cui if (dctx==NULL) return ERROR(memory_allocation);
3843*01826a49SYabin Cui regenSize = ZSTDv07_decompressDCtx(dctx, dst, dstCapacity, src, srcSize);
3844*01826a49SYabin Cui ZSTDv07_freeDCtx(dctx);
3845*01826a49SYabin Cui return regenSize;
3846*01826a49SYabin Cui #else /* stack mode */
3847*01826a49SYabin Cui ZSTDv07_DCtx dctx;
3848*01826a49SYabin Cui return ZSTDv07_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);
3849*01826a49SYabin Cui #endif
3850*01826a49SYabin Cui }
3851*01826a49SYabin Cui
3852*01826a49SYabin Cui /* ZSTD_errorFrameSizeInfoLegacy() :
3853*01826a49SYabin Cui assumes `cSize` and `dBound` are _not_ NULL */
ZSTD_errorFrameSizeInfoLegacy(size_t * cSize,unsigned long long * dBound,size_t ret)3854*01826a49SYabin Cui static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)
3855*01826a49SYabin Cui {
3856*01826a49SYabin Cui *cSize = ret;
3857*01826a49SYabin Cui *dBound = ZSTD_CONTENTSIZE_ERROR;
3858*01826a49SYabin Cui }
3859*01826a49SYabin Cui
ZSTDv07_findFrameSizeInfoLegacy(const void * src,size_t srcSize,size_t * cSize,unsigned long long * dBound)3860*01826a49SYabin Cui void ZSTDv07_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)
3861*01826a49SYabin Cui {
3862*01826a49SYabin Cui const BYTE* ip = (const BYTE*)src;
3863*01826a49SYabin Cui size_t remainingSize = srcSize;
3864*01826a49SYabin Cui size_t nbBlocks = 0;
3865*01826a49SYabin Cui
3866*01826a49SYabin Cui /* check */
3867*01826a49SYabin Cui if (srcSize < ZSTDv07_frameHeaderSize_min+ZSTDv07_blockHeaderSize) {
3868*01826a49SYabin Cui ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3869*01826a49SYabin Cui return;
3870*01826a49SYabin Cui }
3871*01826a49SYabin Cui
3872*01826a49SYabin Cui /* Frame Header */
3873*01826a49SYabin Cui { size_t const frameHeaderSize = ZSTDv07_frameHeaderSize(src, srcSize);
3874*01826a49SYabin Cui if (ZSTDv07_isError(frameHeaderSize)) {
3875*01826a49SYabin Cui ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, frameHeaderSize);
3876*01826a49SYabin Cui return;
3877*01826a49SYabin Cui }
3878*01826a49SYabin Cui if (MEM_readLE32(src) != ZSTDv07_MAGICNUMBER) {
3879*01826a49SYabin Cui ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));
3880*01826a49SYabin Cui return;
3881*01826a49SYabin Cui }
3882*01826a49SYabin Cui if (srcSize < frameHeaderSize+ZSTDv07_blockHeaderSize) {
3883*01826a49SYabin Cui ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3884*01826a49SYabin Cui return;
3885*01826a49SYabin Cui }
3886*01826a49SYabin Cui ip += frameHeaderSize; remainingSize -= frameHeaderSize;
3887*01826a49SYabin Cui }
3888*01826a49SYabin Cui
3889*01826a49SYabin Cui /* Loop on each block */
3890*01826a49SYabin Cui while (1) {
3891*01826a49SYabin Cui blockProperties_t blockProperties;
3892*01826a49SYabin Cui size_t const cBlockSize = ZSTDv07_getcBlockSize(ip, remainingSize, &blockProperties);
3893*01826a49SYabin Cui if (ZSTDv07_isError(cBlockSize)) {
3894*01826a49SYabin Cui ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize);
3895*01826a49SYabin Cui return;
3896*01826a49SYabin Cui }
3897*01826a49SYabin Cui
3898*01826a49SYabin Cui ip += ZSTDv07_blockHeaderSize;
3899*01826a49SYabin Cui remainingSize -= ZSTDv07_blockHeaderSize;
3900*01826a49SYabin Cui
3901*01826a49SYabin Cui if (blockProperties.blockType == bt_end) break;
3902*01826a49SYabin Cui
3903*01826a49SYabin Cui if (cBlockSize > remainingSize) {
3904*01826a49SYabin Cui ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3905*01826a49SYabin Cui return;
3906*01826a49SYabin Cui }
3907*01826a49SYabin Cui
3908*01826a49SYabin Cui ip += cBlockSize;
3909*01826a49SYabin Cui remainingSize -= cBlockSize;
3910*01826a49SYabin Cui nbBlocks++;
3911*01826a49SYabin Cui }
3912*01826a49SYabin Cui
3913*01826a49SYabin Cui *cSize = ip - (const BYTE*)src;
3914*01826a49SYabin Cui *dBound = nbBlocks * ZSTDv07_BLOCKSIZE_ABSOLUTEMAX;
3915*01826a49SYabin Cui }
3916*01826a49SYabin Cui
3917*01826a49SYabin Cui /*_******************************
3918*01826a49SYabin Cui * Streaming Decompression API
3919*01826a49SYabin Cui ********************************/
ZSTDv07_nextSrcSizeToDecompress(ZSTDv07_DCtx * dctx)3920*01826a49SYabin Cui size_t ZSTDv07_nextSrcSizeToDecompress(ZSTDv07_DCtx* dctx)
3921*01826a49SYabin Cui {
3922*01826a49SYabin Cui return dctx->expected;
3923*01826a49SYabin Cui }
3924*01826a49SYabin Cui
ZSTDv07_isSkipFrame(ZSTDv07_DCtx * dctx)3925*01826a49SYabin Cui int ZSTDv07_isSkipFrame(ZSTDv07_DCtx* dctx)
3926*01826a49SYabin Cui {
3927*01826a49SYabin Cui return dctx->stage == ZSTDds_skipFrame;
3928*01826a49SYabin Cui }
3929*01826a49SYabin Cui
3930*01826a49SYabin Cui /** ZSTDv07_decompressContinue() :
3931*01826a49SYabin Cui * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)
3932*01826a49SYabin Cui * or an error code, which can be tested using ZSTDv07_isError() */
ZSTDv07_decompressContinue(ZSTDv07_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize)3933*01826a49SYabin Cui size_t ZSTDv07_decompressContinue(ZSTDv07_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
3934*01826a49SYabin Cui {
3935*01826a49SYabin Cui /* Sanity check */
3936*01826a49SYabin Cui if (srcSize != dctx->expected) return ERROR(srcSize_wrong);
3937*01826a49SYabin Cui if (dstCapacity) ZSTDv07_checkContinuity(dctx, dst);
3938*01826a49SYabin Cui
3939*01826a49SYabin Cui switch (dctx->stage)
3940*01826a49SYabin Cui {
3941*01826a49SYabin Cui case ZSTDds_getFrameHeaderSize :
3942*01826a49SYabin Cui if (srcSize != ZSTDv07_frameHeaderSize_min) return ERROR(srcSize_wrong); /* impossible */
3943*01826a49SYabin Cui if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTDv07_MAGIC_SKIPPABLE_START) {
3944*01826a49SYabin Cui memcpy(dctx->headerBuffer, src, ZSTDv07_frameHeaderSize_min);
3945*01826a49SYabin Cui dctx->expected = ZSTDv07_skippableHeaderSize - ZSTDv07_frameHeaderSize_min; /* magic number + skippable frame length */
3946*01826a49SYabin Cui dctx->stage = ZSTDds_decodeSkippableHeader;
3947*01826a49SYabin Cui return 0;
3948*01826a49SYabin Cui }
3949*01826a49SYabin Cui dctx->headerSize = ZSTDv07_frameHeaderSize(src, ZSTDv07_frameHeaderSize_min);
3950*01826a49SYabin Cui if (ZSTDv07_isError(dctx->headerSize)) return dctx->headerSize;
3951*01826a49SYabin Cui memcpy(dctx->headerBuffer, src, ZSTDv07_frameHeaderSize_min);
3952*01826a49SYabin Cui if (dctx->headerSize > ZSTDv07_frameHeaderSize_min) {
3953*01826a49SYabin Cui dctx->expected = dctx->headerSize - ZSTDv07_frameHeaderSize_min;
3954*01826a49SYabin Cui dctx->stage = ZSTDds_decodeFrameHeader;
3955*01826a49SYabin Cui return 0;
3956*01826a49SYabin Cui }
3957*01826a49SYabin Cui dctx->expected = 0; /* not necessary to copy more */
3958*01826a49SYabin Cui /* fall-through */
3959*01826a49SYabin Cui case ZSTDds_decodeFrameHeader:
3960*01826a49SYabin Cui { size_t result;
3961*01826a49SYabin Cui memcpy(dctx->headerBuffer + ZSTDv07_frameHeaderSize_min, src, dctx->expected);
3962*01826a49SYabin Cui result = ZSTDv07_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize);
3963*01826a49SYabin Cui if (ZSTDv07_isError(result)) return result;
3964*01826a49SYabin Cui dctx->expected = ZSTDv07_blockHeaderSize;
3965*01826a49SYabin Cui dctx->stage = ZSTDds_decodeBlockHeader;
3966*01826a49SYabin Cui return 0;
3967*01826a49SYabin Cui }
3968*01826a49SYabin Cui case ZSTDds_decodeBlockHeader:
3969*01826a49SYabin Cui { blockProperties_t bp;
3970*01826a49SYabin Cui size_t const cBlockSize = ZSTDv07_getcBlockSize(src, ZSTDv07_blockHeaderSize, &bp);
3971*01826a49SYabin Cui if (ZSTDv07_isError(cBlockSize)) return cBlockSize;
3972*01826a49SYabin Cui if (bp.blockType == bt_end) {
3973*01826a49SYabin Cui if (dctx->fParams.checksumFlag) {
3974*01826a49SYabin Cui U64 const h64 = XXH64_digest(&dctx->xxhState);
3975*01826a49SYabin Cui U32 const h32 = (U32)(h64>>11) & ((1<<22)-1);
3976*01826a49SYabin Cui const BYTE* const ip = (const BYTE*)src;
3977*01826a49SYabin Cui U32 const check32 = ip[2] + (ip[1] << 8) + ((ip[0] & 0x3F) << 16);
3978*01826a49SYabin Cui if (check32 != h32) return ERROR(checksum_wrong);
3979*01826a49SYabin Cui }
3980*01826a49SYabin Cui dctx->expected = 0;
3981*01826a49SYabin Cui dctx->stage = ZSTDds_getFrameHeaderSize;
3982*01826a49SYabin Cui } else {
3983*01826a49SYabin Cui dctx->expected = cBlockSize;
3984*01826a49SYabin Cui dctx->bType = bp.blockType;
3985*01826a49SYabin Cui dctx->stage = ZSTDds_decompressBlock;
3986*01826a49SYabin Cui }
3987*01826a49SYabin Cui return 0;
3988*01826a49SYabin Cui }
3989*01826a49SYabin Cui case ZSTDds_decompressBlock:
3990*01826a49SYabin Cui { size_t rSize;
3991*01826a49SYabin Cui switch(dctx->bType)
3992*01826a49SYabin Cui {
3993*01826a49SYabin Cui case bt_compressed:
3994*01826a49SYabin Cui rSize = ZSTDv07_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize);
3995*01826a49SYabin Cui break;
3996*01826a49SYabin Cui case bt_raw :
3997*01826a49SYabin Cui rSize = ZSTDv07_copyRawBlock(dst, dstCapacity, src, srcSize);
3998*01826a49SYabin Cui break;
3999*01826a49SYabin Cui case bt_rle :
4000*01826a49SYabin Cui return ERROR(GENERIC); /* not yet handled */
4001*01826a49SYabin Cui break;
4002*01826a49SYabin Cui case bt_end : /* should never happen (filtered at phase 1) */
4003*01826a49SYabin Cui rSize = 0;
4004*01826a49SYabin Cui break;
4005*01826a49SYabin Cui default:
4006*01826a49SYabin Cui return ERROR(GENERIC); /* impossible */
4007*01826a49SYabin Cui }
4008*01826a49SYabin Cui dctx->stage = ZSTDds_decodeBlockHeader;
4009*01826a49SYabin Cui dctx->expected = ZSTDv07_blockHeaderSize;
4010*01826a49SYabin Cui if (ZSTDv07_isError(rSize)) return rSize;
4011*01826a49SYabin Cui dctx->previousDstEnd = (char*)dst + rSize;
4012*01826a49SYabin Cui if (dctx->fParams.checksumFlag) XXH64_update(&dctx->xxhState, dst, rSize);
4013*01826a49SYabin Cui return rSize;
4014*01826a49SYabin Cui }
4015*01826a49SYabin Cui case ZSTDds_decodeSkippableHeader:
4016*01826a49SYabin Cui { memcpy(dctx->headerBuffer + ZSTDv07_frameHeaderSize_min, src, dctx->expected);
4017*01826a49SYabin Cui dctx->expected = MEM_readLE32(dctx->headerBuffer + 4);
4018*01826a49SYabin Cui dctx->stage = ZSTDds_skipFrame;
4019*01826a49SYabin Cui return 0;
4020*01826a49SYabin Cui }
4021*01826a49SYabin Cui case ZSTDds_skipFrame:
4022*01826a49SYabin Cui { dctx->expected = 0;
4023*01826a49SYabin Cui dctx->stage = ZSTDds_getFrameHeaderSize;
4024*01826a49SYabin Cui return 0;
4025*01826a49SYabin Cui }
4026*01826a49SYabin Cui default:
4027*01826a49SYabin Cui return ERROR(GENERIC); /* impossible */
4028*01826a49SYabin Cui }
4029*01826a49SYabin Cui }
4030*01826a49SYabin Cui
4031*01826a49SYabin Cui
ZSTDv07_refDictContent(ZSTDv07_DCtx * dctx,const void * dict,size_t dictSize)4032*01826a49SYabin Cui static size_t ZSTDv07_refDictContent(ZSTDv07_DCtx* dctx, const void* dict, size_t dictSize)
4033*01826a49SYabin Cui {
4034*01826a49SYabin Cui dctx->dictEnd = dctx->previousDstEnd;
4035*01826a49SYabin Cui dctx->vBase = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base));
4036*01826a49SYabin Cui dctx->base = dict;
4037*01826a49SYabin Cui dctx->previousDstEnd = (const char*)dict + dictSize;
4038*01826a49SYabin Cui return 0;
4039*01826a49SYabin Cui }
4040*01826a49SYabin Cui
ZSTDv07_loadEntropy(ZSTDv07_DCtx * dctx,const void * const dict,size_t const dictSize)4041*01826a49SYabin Cui static size_t ZSTDv07_loadEntropy(ZSTDv07_DCtx* dctx, const void* const dict, size_t const dictSize)
4042*01826a49SYabin Cui {
4043*01826a49SYabin Cui const BYTE* dictPtr = (const BYTE*)dict;
4044*01826a49SYabin Cui const BYTE* const dictEnd = dictPtr + dictSize;
4045*01826a49SYabin Cui
4046*01826a49SYabin Cui { size_t const hSize = HUFv07_readDTableX4(dctx->hufTable, dict, dictSize);
4047*01826a49SYabin Cui if (HUFv07_isError(hSize)) return ERROR(dictionary_corrupted);
4048*01826a49SYabin Cui dictPtr += hSize;
4049*01826a49SYabin Cui }
4050*01826a49SYabin Cui
4051*01826a49SYabin Cui { short offcodeNCount[MaxOff+1];
4052*01826a49SYabin Cui U32 offcodeMaxValue=MaxOff, offcodeLog;
4053*01826a49SYabin Cui size_t const offcodeHeaderSize = FSEv07_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr);
4054*01826a49SYabin Cui if (FSEv07_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted);
4055*01826a49SYabin Cui if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted);
4056*01826a49SYabin Cui { size_t const errorCode = FSEv07_buildDTable(dctx->OffTable, offcodeNCount, offcodeMaxValue, offcodeLog);
4057*01826a49SYabin Cui if (FSEv07_isError(errorCode)) return ERROR(dictionary_corrupted); }
4058*01826a49SYabin Cui dictPtr += offcodeHeaderSize;
4059*01826a49SYabin Cui }
4060*01826a49SYabin Cui
4061*01826a49SYabin Cui { short matchlengthNCount[MaxML+1];
4062*01826a49SYabin Cui unsigned matchlengthMaxValue = MaxML, matchlengthLog;
4063*01826a49SYabin Cui size_t const matchlengthHeaderSize = FSEv07_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr);
4064*01826a49SYabin Cui if (FSEv07_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted);
4065*01826a49SYabin Cui if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted);
4066*01826a49SYabin Cui { size_t const errorCode = FSEv07_buildDTable(dctx->MLTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog);
4067*01826a49SYabin Cui if (FSEv07_isError(errorCode)) return ERROR(dictionary_corrupted); }
4068*01826a49SYabin Cui dictPtr += matchlengthHeaderSize;
4069*01826a49SYabin Cui }
4070*01826a49SYabin Cui
4071*01826a49SYabin Cui { short litlengthNCount[MaxLL+1];
4072*01826a49SYabin Cui unsigned litlengthMaxValue = MaxLL, litlengthLog;
4073*01826a49SYabin Cui size_t const litlengthHeaderSize = FSEv07_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr);
4074*01826a49SYabin Cui if (FSEv07_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted);
4075*01826a49SYabin Cui if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted);
4076*01826a49SYabin Cui { size_t const errorCode = FSEv07_buildDTable(dctx->LLTable, litlengthNCount, litlengthMaxValue, litlengthLog);
4077*01826a49SYabin Cui if (FSEv07_isError(errorCode)) return ERROR(dictionary_corrupted); }
4078*01826a49SYabin Cui dictPtr += litlengthHeaderSize;
4079*01826a49SYabin Cui }
4080*01826a49SYabin Cui
4081*01826a49SYabin Cui if (dictPtr+12 > dictEnd) return ERROR(dictionary_corrupted);
4082*01826a49SYabin Cui dctx->rep[0] = MEM_readLE32(dictPtr+0); if (dctx->rep[0] == 0 || dctx->rep[0] >= dictSize) return ERROR(dictionary_corrupted);
4083*01826a49SYabin Cui dctx->rep[1] = MEM_readLE32(dictPtr+4); if (dctx->rep[1] == 0 || dctx->rep[1] >= dictSize) return ERROR(dictionary_corrupted);
4084*01826a49SYabin Cui dctx->rep[2] = MEM_readLE32(dictPtr+8); if (dctx->rep[2] == 0 || dctx->rep[2] >= dictSize) return ERROR(dictionary_corrupted);
4085*01826a49SYabin Cui dictPtr += 12;
4086*01826a49SYabin Cui
4087*01826a49SYabin Cui dctx->litEntropy = dctx->fseEntropy = 1;
4088*01826a49SYabin Cui return dictPtr - (const BYTE*)dict;
4089*01826a49SYabin Cui }
4090*01826a49SYabin Cui
ZSTDv07_decompress_insertDictionary(ZSTDv07_DCtx * dctx,const void * dict,size_t dictSize)4091*01826a49SYabin Cui static size_t ZSTDv07_decompress_insertDictionary(ZSTDv07_DCtx* dctx, const void* dict, size_t dictSize)
4092*01826a49SYabin Cui {
4093*01826a49SYabin Cui if (dictSize < 8) return ZSTDv07_refDictContent(dctx, dict, dictSize);
4094*01826a49SYabin Cui { U32 const magic = MEM_readLE32(dict);
4095*01826a49SYabin Cui if (magic != ZSTDv07_DICT_MAGIC) {
4096*01826a49SYabin Cui return ZSTDv07_refDictContent(dctx, dict, dictSize); /* pure content mode */
4097*01826a49SYabin Cui } }
4098*01826a49SYabin Cui dctx->dictID = MEM_readLE32((const char*)dict + 4);
4099*01826a49SYabin Cui
4100*01826a49SYabin Cui /* load entropy tables */
4101*01826a49SYabin Cui dict = (const char*)dict + 8;
4102*01826a49SYabin Cui dictSize -= 8;
4103*01826a49SYabin Cui { size_t const eSize = ZSTDv07_loadEntropy(dctx, dict, dictSize);
4104*01826a49SYabin Cui if (ZSTDv07_isError(eSize)) return ERROR(dictionary_corrupted);
4105*01826a49SYabin Cui dict = (const char*)dict + eSize;
4106*01826a49SYabin Cui dictSize -= eSize;
4107*01826a49SYabin Cui }
4108*01826a49SYabin Cui
4109*01826a49SYabin Cui /* reference dictionary content */
4110*01826a49SYabin Cui return ZSTDv07_refDictContent(dctx, dict, dictSize);
4111*01826a49SYabin Cui }
4112*01826a49SYabin Cui
4113*01826a49SYabin Cui
ZSTDv07_decompressBegin_usingDict(ZSTDv07_DCtx * dctx,const void * dict,size_t dictSize)4114*01826a49SYabin Cui size_t ZSTDv07_decompressBegin_usingDict(ZSTDv07_DCtx* dctx, const void* dict, size_t dictSize)
4115*01826a49SYabin Cui {
4116*01826a49SYabin Cui { size_t const errorCode = ZSTDv07_decompressBegin(dctx);
4117*01826a49SYabin Cui if (ZSTDv07_isError(errorCode)) return errorCode; }
4118*01826a49SYabin Cui
4119*01826a49SYabin Cui if (dict && dictSize) {
4120*01826a49SYabin Cui size_t const errorCode = ZSTDv07_decompress_insertDictionary(dctx, dict, dictSize);
4121*01826a49SYabin Cui if (ZSTDv07_isError(errorCode)) return ERROR(dictionary_corrupted);
4122*01826a49SYabin Cui }
4123*01826a49SYabin Cui
4124*01826a49SYabin Cui return 0;
4125*01826a49SYabin Cui }
4126*01826a49SYabin Cui
4127*01826a49SYabin Cui
4128*01826a49SYabin Cui struct ZSTDv07_DDict_s {
4129*01826a49SYabin Cui void* dict;
4130*01826a49SYabin Cui size_t dictSize;
4131*01826a49SYabin Cui ZSTDv07_DCtx* refContext;
4132*01826a49SYabin Cui }; /* typedef'd tp ZSTDv07_CDict within zstd.h */
4133*01826a49SYabin Cui
ZSTDv07_createDDict_advanced(const void * dict,size_t dictSize,ZSTDv07_customMem customMem)4134*01826a49SYabin Cui static ZSTDv07_DDict* ZSTDv07_createDDict_advanced(const void* dict, size_t dictSize, ZSTDv07_customMem customMem)
4135*01826a49SYabin Cui {
4136*01826a49SYabin Cui if (!customMem.customAlloc && !customMem.customFree)
4137*01826a49SYabin Cui customMem = defaultCustomMem;
4138*01826a49SYabin Cui
4139*01826a49SYabin Cui if (!customMem.customAlloc || !customMem.customFree)
4140*01826a49SYabin Cui return NULL;
4141*01826a49SYabin Cui
4142*01826a49SYabin Cui { ZSTDv07_DDict* const ddict = (ZSTDv07_DDict*) customMem.customAlloc(customMem.opaque, sizeof(*ddict));
4143*01826a49SYabin Cui void* const dictContent = customMem.customAlloc(customMem.opaque, dictSize);
4144*01826a49SYabin Cui ZSTDv07_DCtx* const dctx = ZSTDv07_createDCtx_advanced(customMem);
4145*01826a49SYabin Cui
4146*01826a49SYabin Cui if (!dictContent || !ddict || !dctx) {
4147*01826a49SYabin Cui customMem.customFree(customMem.opaque, dictContent);
4148*01826a49SYabin Cui customMem.customFree(customMem.opaque, ddict);
4149*01826a49SYabin Cui customMem.customFree(customMem.opaque, dctx);
4150*01826a49SYabin Cui return NULL;
4151*01826a49SYabin Cui }
4152*01826a49SYabin Cui
4153*01826a49SYabin Cui memcpy(dictContent, dict, dictSize);
4154*01826a49SYabin Cui { size_t const errorCode = ZSTDv07_decompressBegin_usingDict(dctx, dictContent, dictSize);
4155*01826a49SYabin Cui if (ZSTDv07_isError(errorCode)) {
4156*01826a49SYabin Cui customMem.customFree(customMem.opaque, dictContent);
4157*01826a49SYabin Cui customMem.customFree(customMem.opaque, ddict);
4158*01826a49SYabin Cui customMem.customFree(customMem.opaque, dctx);
4159*01826a49SYabin Cui return NULL;
4160*01826a49SYabin Cui } }
4161*01826a49SYabin Cui
4162*01826a49SYabin Cui ddict->dict = dictContent;
4163*01826a49SYabin Cui ddict->dictSize = dictSize;
4164*01826a49SYabin Cui ddict->refContext = dctx;
4165*01826a49SYabin Cui return ddict;
4166*01826a49SYabin Cui }
4167*01826a49SYabin Cui }
4168*01826a49SYabin Cui
4169*01826a49SYabin Cui /*! ZSTDv07_createDDict() :
4170*01826a49SYabin Cui * Create a digested dictionary, ready to start decompression without startup delay.
4171*01826a49SYabin Cui * `dict` can be released after `ZSTDv07_DDict` creation */
ZSTDv07_createDDict(const void * dict,size_t dictSize)4172*01826a49SYabin Cui ZSTDv07_DDict* ZSTDv07_createDDict(const void* dict, size_t dictSize)
4173*01826a49SYabin Cui {
4174*01826a49SYabin Cui ZSTDv07_customMem const allocator = { NULL, NULL, NULL };
4175*01826a49SYabin Cui return ZSTDv07_createDDict_advanced(dict, dictSize, allocator);
4176*01826a49SYabin Cui }
4177*01826a49SYabin Cui
ZSTDv07_freeDDict(ZSTDv07_DDict * ddict)4178*01826a49SYabin Cui size_t ZSTDv07_freeDDict(ZSTDv07_DDict* ddict)
4179*01826a49SYabin Cui {
4180*01826a49SYabin Cui ZSTDv07_freeFunction const cFree = ddict->refContext->customMem.customFree;
4181*01826a49SYabin Cui void* const opaque = ddict->refContext->customMem.opaque;
4182*01826a49SYabin Cui ZSTDv07_freeDCtx(ddict->refContext);
4183*01826a49SYabin Cui cFree(opaque, ddict->dict);
4184*01826a49SYabin Cui cFree(opaque, ddict);
4185*01826a49SYabin Cui return 0;
4186*01826a49SYabin Cui }
4187*01826a49SYabin Cui
4188*01826a49SYabin Cui /*! ZSTDv07_decompress_usingDDict() :
4189*01826a49SYabin Cui * Decompression using a pre-digested Dictionary
4190*01826a49SYabin Cui * Use dictionary without significant overhead. */
ZSTDv07_decompress_usingDDict(ZSTDv07_DCtx * dctx,void * dst,size_t dstCapacity,const void * src,size_t srcSize,const ZSTDv07_DDict * ddict)4191*01826a49SYabin Cui ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDDict(ZSTDv07_DCtx* dctx,
4192*01826a49SYabin Cui void* dst, size_t dstCapacity,
4193*01826a49SYabin Cui const void* src, size_t srcSize,
4194*01826a49SYabin Cui const ZSTDv07_DDict* ddict)
4195*01826a49SYabin Cui {
4196*01826a49SYabin Cui return ZSTDv07_decompress_usingPreparedDCtx(dctx, ddict->refContext,
4197*01826a49SYabin Cui dst, dstCapacity,
4198*01826a49SYabin Cui src, srcSize);
4199*01826a49SYabin Cui }
4200*01826a49SYabin Cui /*
4201*01826a49SYabin Cui Buffered version of Zstd compression library
4202*01826a49SYabin Cui Copyright (C) 2015-2016, Yann Collet.
4203*01826a49SYabin Cui
4204*01826a49SYabin Cui BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
4205*01826a49SYabin Cui
4206*01826a49SYabin Cui Redistribution and use in source and binary forms, with or without
4207*01826a49SYabin Cui modification, are permitted provided that the following conditions are
4208*01826a49SYabin Cui met:
4209*01826a49SYabin Cui * Redistributions of source code must retain the above copyright
4210*01826a49SYabin Cui notice, this list of conditions and the following disclaimer.
4211*01826a49SYabin Cui * Redistributions in binary form must reproduce the above
4212*01826a49SYabin Cui copyright notice, this list of conditions and the following disclaimer
4213*01826a49SYabin Cui in the documentation and/or other materials provided with the
4214*01826a49SYabin Cui distribution.
4215*01826a49SYabin Cui THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4216*01826a49SYabin Cui "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
4217*01826a49SYabin Cui LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
4218*01826a49SYabin Cui A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
4219*01826a49SYabin Cui OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4220*01826a49SYabin Cui SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
4221*01826a49SYabin Cui LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
4222*01826a49SYabin Cui DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
4223*01826a49SYabin Cui THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4224*01826a49SYabin Cui (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
4225*01826a49SYabin Cui OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4226*01826a49SYabin Cui
4227*01826a49SYabin Cui You can contact the author at :
4228*01826a49SYabin Cui - zstd homepage : https://facebook.github.io/zstd/
4229*01826a49SYabin Cui */
4230*01826a49SYabin Cui
4231*01826a49SYabin Cui
4232*01826a49SYabin Cui
4233*01826a49SYabin Cui /*-***************************************************************************
4234*01826a49SYabin Cui * Streaming decompression howto
4235*01826a49SYabin Cui *
4236*01826a49SYabin Cui * A ZBUFFv07_DCtx object is required to track streaming operations.
4237*01826a49SYabin Cui * Use ZBUFFv07_createDCtx() and ZBUFFv07_freeDCtx() to create/release resources.
4238*01826a49SYabin Cui * Use ZBUFFv07_decompressInit() to start a new decompression operation,
4239*01826a49SYabin Cui * or ZBUFFv07_decompressInitDictionary() if decompression requires a dictionary.
4240*01826a49SYabin Cui * Note that ZBUFFv07_DCtx objects can be re-init multiple times.
4241*01826a49SYabin Cui *
4242*01826a49SYabin Cui * Use ZBUFFv07_decompressContinue() repetitively to consume your input.
4243*01826a49SYabin Cui * *srcSizePtr and *dstCapacityPtr can be any size.
4244*01826a49SYabin Cui * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
4245*01826a49SYabin Cui * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
4246*01826a49SYabin Cui * The content of @dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change @dst.
4247*01826a49SYabin Cui * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
4248*01826a49SYabin Cui * or 0 when a frame is completely decoded,
4249*01826a49SYabin Cui * or an error code, which can be tested using ZBUFFv07_isError().
4250*01826a49SYabin Cui *
4251*01826a49SYabin Cui * Hint : recommended buffer sizes (not compulsory) : ZBUFFv07_recommendedDInSize() and ZBUFFv07_recommendedDOutSize()
4252*01826a49SYabin Cui * output : ZBUFFv07_recommendedDOutSize==128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
4253*01826a49SYabin Cui * input : ZBUFFv07_recommendedDInSize == 128KB + 3;
4254*01826a49SYabin Cui * just follow indications from ZBUFFv07_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
4255*01826a49SYabin Cui * *******************************************************************************/
4256*01826a49SYabin Cui
4257*01826a49SYabin Cui typedef enum { ZBUFFds_init, ZBUFFds_loadHeader,
4258*01826a49SYabin Cui ZBUFFds_read, ZBUFFds_load, ZBUFFds_flush } ZBUFFv07_dStage;
4259*01826a49SYabin Cui
4260*01826a49SYabin Cui /* *** Resource management *** */
4261*01826a49SYabin Cui struct ZBUFFv07_DCtx_s {
4262*01826a49SYabin Cui ZSTDv07_DCtx* zd;
4263*01826a49SYabin Cui ZSTDv07_frameParams fParams;
4264*01826a49SYabin Cui ZBUFFv07_dStage stage;
4265*01826a49SYabin Cui char* inBuff;
4266*01826a49SYabin Cui size_t inBuffSize;
4267*01826a49SYabin Cui size_t inPos;
4268*01826a49SYabin Cui char* outBuff;
4269*01826a49SYabin Cui size_t outBuffSize;
4270*01826a49SYabin Cui size_t outStart;
4271*01826a49SYabin Cui size_t outEnd;
4272*01826a49SYabin Cui size_t blockSize;
4273*01826a49SYabin Cui BYTE headerBuffer[ZSTDv07_FRAMEHEADERSIZE_MAX];
4274*01826a49SYabin Cui size_t lhSize;
4275*01826a49SYabin Cui ZSTDv07_customMem customMem;
4276*01826a49SYabin Cui }; /* typedef'd to ZBUFFv07_DCtx within "zstd_buffered.h" */
4277*01826a49SYabin Cui
4278*01826a49SYabin Cui ZSTDLIBv07_API ZBUFFv07_DCtx* ZBUFFv07_createDCtx_advanced(ZSTDv07_customMem customMem);
4279*01826a49SYabin Cui
ZBUFFv07_createDCtx(void)4280*01826a49SYabin Cui ZBUFFv07_DCtx* ZBUFFv07_createDCtx(void)
4281*01826a49SYabin Cui {
4282*01826a49SYabin Cui return ZBUFFv07_createDCtx_advanced(defaultCustomMem);
4283*01826a49SYabin Cui }
4284*01826a49SYabin Cui
ZBUFFv07_createDCtx_advanced(ZSTDv07_customMem customMem)4285*01826a49SYabin Cui ZBUFFv07_DCtx* ZBUFFv07_createDCtx_advanced(ZSTDv07_customMem customMem)
4286*01826a49SYabin Cui {
4287*01826a49SYabin Cui ZBUFFv07_DCtx* zbd;
4288*01826a49SYabin Cui
4289*01826a49SYabin Cui if (!customMem.customAlloc && !customMem.customFree)
4290*01826a49SYabin Cui customMem = defaultCustomMem;
4291*01826a49SYabin Cui
4292*01826a49SYabin Cui if (!customMem.customAlloc || !customMem.customFree)
4293*01826a49SYabin Cui return NULL;
4294*01826a49SYabin Cui
4295*01826a49SYabin Cui zbd = (ZBUFFv07_DCtx*)customMem.customAlloc(customMem.opaque, sizeof(ZBUFFv07_DCtx));
4296*01826a49SYabin Cui if (zbd==NULL) return NULL;
4297*01826a49SYabin Cui memset(zbd, 0, sizeof(ZBUFFv07_DCtx));
4298*01826a49SYabin Cui memcpy(&zbd->customMem, &customMem, sizeof(ZSTDv07_customMem));
4299*01826a49SYabin Cui zbd->zd = ZSTDv07_createDCtx_advanced(customMem);
4300*01826a49SYabin Cui if (zbd->zd == NULL) { ZBUFFv07_freeDCtx(zbd); return NULL; }
4301*01826a49SYabin Cui zbd->stage = ZBUFFds_init;
4302*01826a49SYabin Cui return zbd;
4303*01826a49SYabin Cui }
4304*01826a49SYabin Cui
ZBUFFv07_freeDCtx(ZBUFFv07_DCtx * zbd)4305*01826a49SYabin Cui size_t ZBUFFv07_freeDCtx(ZBUFFv07_DCtx* zbd)
4306*01826a49SYabin Cui {
4307*01826a49SYabin Cui if (zbd==NULL) return 0; /* support free on null */
4308*01826a49SYabin Cui ZSTDv07_freeDCtx(zbd->zd);
4309*01826a49SYabin Cui if (zbd->inBuff) zbd->customMem.customFree(zbd->customMem.opaque, zbd->inBuff);
4310*01826a49SYabin Cui if (zbd->outBuff) zbd->customMem.customFree(zbd->customMem.opaque, zbd->outBuff);
4311*01826a49SYabin Cui zbd->customMem.customFree(zbd->customMem.opaque, zbd);
4312*01826a49SYabin Cui return 0;
4313*01826a49SYabin Cui }
4314*01826a49SYabin Cui
4315*01826a49SYabin Cui
4316*01826a49SYabin Cui /* *** Initialization *** */
4317*01826a49SYabin Cui
ZBUFFv07_decompressInitDictionary(ZBUFFv07_DCtx * zbd,const void * dict,size_t dictSize)4318*01826a49SYabin Cui size_t ZBUFFv07_decompressInitDictionary(ZBUFFv07_DCtx* zbd, const void* dict, size_t dictSize)
4319*01826a49SYabin Cui {
4320*01826a49SYabin Cui zbd->stage = ZBUFFds_loadHeader;
4321*01826a49SYabin Cui zbd->lhSize = zbd->inPos = zbd->outStart = zbd->outEnd = 0;
4322*01826a49SYabin Cui return ZSTDv07_decompressBegin_usingDict(zbd->zd, dict, dictSize);
4323*01826a49SYabin Cui }
4324*01826a49SYabin Cui
ZBUFFv07_decompressInit(ZBUFFv07_DCtx * zbd)4325*01826a49SYabin Cui size_t ZBUFFv07_decompressInit(ZBUFFv07_DCtx* zbd)
4326*01826a49SYabin Cui {
4327*01826a49SYabin Cui return ZBUFFv07_decompressInitDictionary(zbd, NULL, 0);
4328*01826a49SYabin Cui }
4329*01826a49SYabin Cui
4330*01826a49SYabin Cui
4331*01826a49SYabin Cui /* internal util function */
ZBUFFv07_limitCopy(void * dst,size_t dstCapacity,const void * src,size_t srcSize)4332*01826a49SYabin Cui MEM_STATIC size_t ZBUFFv07_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
4333*01826a49SYabin Cui {
4334*01826a49SYabin Cui size_t const length = MIN(dstCapacity, srcSize);
4335*01826a49SYabin Cui if (length > 0) {
4336*01826a49SYabin Cui memcpy(dst, src, length);
4337*01826a49SYabin Cui }
4338*01826a49SYabin Cui return length;
4339*01826a49SYabin Cui }
4340*01826a49SYabin Cui
4341*01826a49SYabin Cui
4342*01826a49SYabin Cui /* *** Decompression *** */
4343*01826a49SYabin Cui
ZBUFFv07_decompressContinue(ZBUFFv07_DCtx * zbd,void * dst,size_t * dstCapacityPtr,const void * src,size_t * srcSizePtr)4344*01826a49SYabin Cui size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* zbd,
4345*01826a49SYabin Cui void* dst, size_t* dstCapacityPtr,
4346*01826a49SYabin Cui const void* src, size_t* srcSizePtr)
4347*01826a49SYabin Cui {
4348*01826a49SYabin Cui const char* const istart = (const char*)src;
4349*01826a49SYabin Cui const char* const iend = istart + *srcSizePtr;
4350*01826a49SYabin Cui const char* ip = istart;
4351*01826a49SYabin Cui char* const ostart = (char*)dst;
4352*01826a49SYabin Cui char* const oend = ostart + *dstCapacityPtr;
4353*01826a49SYabin Cui char* op = ostart;
4354*01826a49SYabin Cui U32 notDone = 1;
4355*01826a49SYabin Cui
4356*01826a49SYabin Cui while (notDone) {
4357*01826a49SYabin Cui switch(zbd->stage)
4358*01826a49SYabin Cui {
4359*01826a49SYabin Cui case ZBUFFds_init :
4360*01826a49SYabin Cui return ERROR(init_missing);
4361*01826a49SYabin Cui
4362*01826a49SYabin Cui case ZBUFFds_loadHeader :
4363*01826a49SYabin Cui { size_t const hSize = ZSTDv07_getFrameParams(&(zbd->fParams), zbd->headerBuffer, zbd->lhSize);
4364*01826a49SYabin Cui if (ZSTDv07_isError(hSize)) return hSize;
4365*01826a49SYabin Cui if (hSize != 0) {
4366*01826a49SYabin Cui size_t const toLoad = hSize - zbd->lhSize; /* if hSize!=0, hSize > zbd->lhSize */
4367*01826a49SYabin Cui if (toLoad > (size_t)(iend-ip)) { /* not enough input to load full header */
4368*01826a49SYabin Cui if (ip != NULL)
4369*01826a49SYabin Cui memcpy(zbd->headerBuffer + zbd->lhSize, ip, iend-ip);
4370*01826a49SYabin Cui zbd->lhSize += iend-ip;
4371*01826a49SYabin Cui *dstCapacityPtr = 0;
4372*01826a49SYabin Cui return (hSize - zbd->lhSize) + ZSTDv07_blockHeaderSize; /* remaining header bytes + next block header */
4373*01826a49SYabin Cui }
4374*01826a49SYabin Cui memcpy(zbd->headerBuffer + zbd->lhSize, ip, toLoad); zbd->lhSize = hSize; ip += toLoad;
4375*01826a49SYabin Cui break;
4376*01826a49SYabin Cui } }
4377*01826a49SYabin Cui
4378*01826a49SYabin Cui /* Consume header */
4379*01826a49SYabin Cui { size_t const h1Size = ZSTDv07_nextSrcSizeToDecompress(zbd->zd); /* == ZSTDv07_frameHeaderSize_min */
4380*01826a49SYabin Cui size_t const h1Result = ZSTDv07_decompressContinue(zbd->zd, NULL, 0, zbd->headerBuffer, h1Size);
4381*01826a49SYabin Cui if (ZSTDv07_isError(h1Result)) return h1Result;
4382*01826a49SYabin Cui if (h1Size < zbd->lhSize) { /* long header */
4383*01826a49SYabin Cui size_t const h2Size = ZSTDv07_nextSrcSizeToDecompress(zbd->zd);
4384*01826a49SYabin Cui size_t const h2Result = ZSTDv07_decompressContinue(zbd->zd, NULL, 0, zbd->headerBuffer+h1Size, h2Size);
4385*01826a49SYabin Cui if (ZSTDv07_isError(h2Result)) return h2Result;
4386*01826a49SYabin Cui } }
4387*01826a49SYabin Cui
4388*01826a49SYabin Cui zbd->fParams.windowSize = MAX(zbd->fParams.windowSize, 1U << ZSTDv07_WINDOWLOG_ABSOLUTEMIN);
4389*01826a49SYabin Cui
4390*01826a49SYabin Cui /* Frame header instruct buffer sizes */
4391*01826a49SYabin Cui { size_t const blockSize = MIN(zbd->fParams.windowSize, ZSTDv07_BLOCKSIZE_ABSOLUTEMAX);
4392*01826a49SYabin Cui zbd->blockSize = blockSize;
4393*01826a49SYabin Cui if (zbd->inBuffSize < blockSize) {
4394*01826a49SYabin Cui zbd->customMem.customFree(zbd->customMem.opaque, zbd->inBuff);
4395*01826a49SYabin Cui zbd->inBuffSize = blockSize;
4396*01826a49SYabin Cui zbd->inBuff = (char*)zbd->customMem.customAlloc(zbd->customMem.opaque, blockSize);
4397*01826a49SYabin Cui if (zbd->inBuff == NULL) return ERROR(memory_allocation);
4398*01826a49SYabin Cui }
4399*01826a49SYabin Cui { size_t const neededOutSize = zbd->fParams.windowSize + blockSize + WILDCOPY_OVERLENGTH * 2;
4400*01826a49SYabin Cui if (zbd->outBuffSize < neededOutSize) {
4401*01826a49SYabin Cui zbd->customMem.customFree(zbd->customMem.opaque, zbd->outBuff);
4402*01826a49SYabin Cui zbd->outBuffSize = neededOutSize;
4403*01826a49SYabin Cui zbd->outBuff = (char*)zbd->customMem.customAlloc(zbd->customMem.opaque, neededOutSize);
4404*01826a49SYabin Cui if (zbd->outBuff == NULL) return ERROR(memory_allocation);
4405*01826a49SYabin Cui } } }
4406*01826a49SYabin Cui zbd->stage = ZBUFFds_read;
4407*01826a49SYabin Cui /* pass-through */
4408*01826a49SYabin Cui /* fall-through */
4409*01826a49SYabin Cui case ZBUFFds_read:
4410*01826a49SYabin Cui { size_t const neededInSize = ZSTDv07_nextSrcSizeToDecompress(zbd->zd);
4411*01826a49SYabin Cui if (neededInSize==0) { /* end of frame */
4412*01826a49SYabin Cui zbd->stage = ZBUFFds_init;
4413*01826a49SYabin Cui notDone = 0;
4414*01826a49SYabin Cui break;
4415*01826a49SYabin Cui }
4416*01826a49SYabin Cui if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */
4417*01826a49SYabin Cui const int isSkipFrame = ZSTDv07_isSkipFrame(zbd->zd);
4418*01826a49SYabin Cui size_t const decodedSize = ZSTDv07_decompressContinue(zbd->zd,
4419*01826a49SYabin Cui zbd->outBuff + zbd->outStart, (isSkipFrame ? 0 : zbd->outBuffSize - zbd->outStart),
4420*01826a49SYabin Cui ip, neededInSize);
4421*01826a49SYabin Cui if (ZSTDv07_isError(decodedSize)) return decodedSize;
4422*01826a49SYabin Cui ip += neededInSize;
4423*01826a49SYabin Cui if (!decodedSize && !isSkipFrame) break; /* this was just a header */
4424*01826a49SYabin Cui zbd->outEnd = zbd->outStart + decodedSize;
4425*01826a49SYabin Cui zbd->stage = ZBUFFds_flush;
4426*01826a49SYabin Cui break;
4427*01826a49SYabin Cui }
4428*01826a49SYabin Cui if (ip==iend) { notDone = 0; break; } /* no more input */
4429*01826a49SYabin Cui zbd->stage = ZBUFFds_load;
4430*01826a49SYabin Cui }
4431*01826a49SYabin Cui /* fall-through */
4432*01826a49SYabin Cui case ZBUFFds_load:
4433*01826a49SYabin Cui { size_t const neededInSize = ZSTDv07_nextSrcSizeToDecompress(zbd->zd);
4434*01826a49SYabin Cui size_t const toLoad = neededInSize - zbd->inPos; /* should always be <= remaining space within inBuff */
4435*01826a49SYabin Cui size_t loadedSize;
4436*01826a49SYabin Cui if (toLoad > zbd->inBuffSize - zbd->inPos) return ERROR(corruption_detected); /* should never happen */
4437*01826a49SYabin Cui loadedSize = ZBUFFv07_limitCopy(zbd->inBuff + zbd->inPos, toLoad, ip, iend-ip);
4438*01826a49SYabin Cui ip += loadedSize;
4439*01826a49SYabin Cui zbd->inPos += loadedSize;
4440*01826a49SYabin Cui if (loadedSize < toLoad) { notDone = 0; break; } /* not enough input, wait for more */
4441*01826a49SYabin Cui
4442*01826a49SYabin Cui /* decode loaded input */
4443*01826a49SYabin Cui { const int isSkipFrame = ZSTDv07_isSkipFrame(zbd->zd);
4444*01826a49SYabin Cui size_t const decodedSize = ZSTDv07_decompressContinue(zbd->zd,
4445*01826a49SYabin Cui zbd->outBuff + zbd->outStart, zbd->outBuffSize - zbd->outStart,
4446*01826a49SYabin Cui zbd->inBuff, neededInSize);
4447*01826a49SYabin Cui if (ZSTDv07_isError(decodedSize)) return decodedSize;
4448*01826a49SYabin Cui zbd->inPos = 0; /* input is consumed */
4449*01826a49SYabin Cui if (!decodedSize && !isSkipFrame) { zbd->stage = ZBUFFds_read; break; } /* this was just a header */
4450*01826a49SYabin Cui zbd->outEnd = zbd->outStart + decodedSize;
4451*01826a49SYabin Cui zbd->stage = ZBUFFds_flush;
4452*01826a49SYabin Cui /* break; */
4453*01826a49SYabin Cui /* pass-through */
4454*01826a49SYabin Cui }
4455*01826a49SYabin Cui }
4456*01826a49SYabin Cui /* fall-through */
4457*01826a49SYabin Cui case ZBUFFds_flush:
4458*01826a49SYabin Cui { size_t const toFlushSize = zbd->outEnd - zbd->outStart;
4459*01826a49SYabin Cui size_t const flushedSize = ZBUFFv07_limitCopy(op, oend-op, zbd->outBuff + zbd->outStart, toFlushSize);
4460*01826a49SYabin Cui op += flushedSize;
4461*01826a49SYabin Cui zbd->outStart += flushedSize;
4462*01826a49SYabin Cui if (flushedSize == toFlushSize) {
4463*01826a49SYabin Cui zbd->stage = ZBUFFds_read;
4464*01826a49SYabin Cui if (zbd->outStart + zbd->blockSize > zbd->outBuffSize)
4465*01826a49SYabin Cui zbd->outStart = zbd->outEnd = 0;
4466*01826a49SYabin Cui break;
4467*01826a49SYabin Cui }
4468*01826a49SYabin Cui /* cannot flush everything */
4469*01826a49SYabin Cui notDone = 0;
4470*01826a49SYabin Cui break;
4471*01826a49SYabin Cui }
4472*01826a49SYabin Cui default: return ERROR(GENERIC); /* impossible */
4473*01826a49SYabin Cui } }
4474*01826a49SYabin Cui
4475*01826a49SYabin Cui /* result */
4476*01826a49SYabin Cui *srcSizePtr = ip-istart;
4477*01826a49SYabin Cui *dstCapacityPtr = op-ostart;
4478*01826a49SYabin Cui { size_t nextSrcSizeHint = ZSTDv07_nextSrcSizeToDecompress(zbd->zd);
4479*01826a49SYabin Cui nextSrcSizeHint -= zbd->inPos; /* already loaded*/
4480*01826a49SYabin Cui return nextSrcSizeHint;
4481*01826a49SYabin Cui }
4482*01826a49SYabin Cui }
4483*01826a49SYabin Cui
4484*01826a49SYabin Cui
4485*01826a49SYabin Cui
4486*01826a49SYabin Cui /* *************************************
4487*01826a49SYabin Cui * Tool functions
4488*01826a49SYabin Cui ***************************************/
ZBUFFv07_recommendedDInSize(void)4489*01826a49SYabin Cui size_t ZBUFFv07_recommendedDInSize(void) { return ZSTDv07_BLOCKSIZE_ABSOLUTEMAX + ZSTDv07_blockHeaderSize /* block header size*/ ; }
ZBUFFv07_recommendedDOutSize(void)4490*01826a49SYabin Cui size_t ZBUFFv07_recommendedDOutSize(void) { return ZSTDv07_BLOCKSIZE_ABSOLUTEMAX; }
4491