source: thirdparty/blosc/internal-complibs/zstd-0.7.4/legacy/zstd_v04.c @ 8ebc79b

Revision 8ebc79b, 149.3 KB checked in by Hal Finkel <hfinkel@…>, 8 years ago (diff)

Add the other internal compression libraries from blocs

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