zstd-ruby 0.1.0

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