extzstd 0.0.2.CONCEPT → 0.0.3.CONCEPT

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