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