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