extzstd 0.0.3.CONCEPT → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +5 -5
  2. data/HISTORY.ja.md +39 -0
  3. data/LICENSE +6 -6
  4. data/README.md +26 -45
  5. data/contrib/zstd/CHANGELOG +555 -0
  6. data/contrib/zstd/CODE_OF_CONDUCT.md +5 -0
  7. data/contrib/zstd/CONTRIBUTING.md +392 -0
  8. data/contrib/zstd/COPYING +339 -0
  9. data/contrib/zstd/LICENSE +13 -9
  10. data/contrib/zstd/Makefile +414 -0
  11. data/contrib/zstd/README.md +170 -45
  12. data/contrib/zstd/TESTING.md +44 -0
  13. data/contrib/zstd/appveyor.yml +289 -0
  14. data/contrib/zstd/lib/BUCK +234 -0
  15. data/contrib/zstd/lib/Makefile +354 -0
  16. data/contrib/zstd/lib/README.md +179 -0
  17. data/contrib/zstd/{common → lib/common}/bitstream.h +170 -130
  18. data/contrib/zstd/lib/common/compiler.h +175 -0
  19. data/contrib/zstd/lib/common/cpu.h +215 -0
  20. data/contrib/zstd/lib/common/debug.c +24 -0
  21. data/contrib/zstd/lib/common/debug.h +114 -0
  22. data/contrib/zstd/{common → lib/common}/entropy_common.c +79 -94
  23. data/contrib/zstd/lib/common/error_private.c +55 -0
  24. data/contrib/zstd/lib/common/error_private.h +80 -0
  25. data/contrib/zstd/{common → lib/common}/fse.h +153 -93
  26. data/contrib/zstd/{common → lib/common}/fse_decompress.c +37 -82
  27. data/contrib/zstd/lib/common/huf.h +340 -0
  28. data/contrib/zstd/{common → lib/common}/mem.h +154 -78
  29. data/contrib/zstd/lib/common/pool.c +344 -0
  30. data/contrib/zstd/lib/common/pool.h +84 -0
  31. data/contrib/zstd/lib/common/threading.c +121 -0
  32. data/contrib/zstd/lib/common/threading.h +155 -0
  33. data/contrib/zstd/{common → lib/common}/xxhash.c +85 -75
  34. data/contrib/zstd/{common → lib/common}/xxhash.h +85 -73
  35. data/contrib/zstd/lib/common/zstd_common.c +83 -0
  36. data/contrib/zstd/lib/common/zstd_errors.h +94 -0
  37. data/contrib/zstd/lib/common/zstd_internal.h +447 -0
  38. data/contrib/zstd/{compress → lib/compress}/fse_compress.c +194 -303
  39. data/contrib/zstd/lib/compress/hist.c +183 -0
  40. data/contrib/zstd/lib/compress/hist.h +75 -0
  41. data/contrib/zstd/lib/compress/huf_compress.c +798 -0
  42. data/contrib/zstd/lib/compress/zstd_compress.c +4278 -0
  43. data/contrib/zstd/lib/compress/zstd_compress_internal.h +1125 -0
  44. data/contrib/zstd/lib/compress/zstd_compress_literals.c +158 -0
  45. data/contrib/zstd/lib/compress/zstd_compress_literals.h +29 -0
  46. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +419 -0
  47. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +54 -0
  48. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +845 -0
  49. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +32 -0
  50. data/contrib/zstd/lib/compress/zstd_cwksp.h +525 -0
  51. data/contrib/zstd/lib/compress/zstd_double_fast.c +521 -0
  52. data/contrib/zstd/lib/compress/zstd_double_fast.h +38 -0
  53. data/contrib/zstd/lib/compress/zstd_fast.c +496 -0
  54. data/contrib/zstd/lib/compress/zstd_fast.h +37 -0
  55. data/contrib/zstd/lib/compress/zstd_lazy.c +1138 -0
  56. data/contrib/zstd/lib/compress/zstd_lazy.h +67 -0
  57. data/contrib/zstd/lib/compress/zstd_ldm.c +619 -0
  58. data/contrib/zstd/lib/compress/zstd_ldm.h +110 -0
  59. data/contrib/zstd/lib/compress/zstd_opt.c +1200 -0
  60. data/contrib/zstd/lib/compress/zstd_opt.h +56 -0
  61. data/contrib/zstd/lib/compress/zstdmt_compress.c +2143 -0
  62. data/contrib/zstd/lib/compress/zstdmt_compress.h +192 -0
  63. data/contrib/zstd/lib/decompress/huf_decompress.c +1248 -0
  64. data/contrib/zstd/lib/decompress/zstd_ddict.c +244 -0
  65. data/contrib/zstd/lib/decompress/zstd_ddict.h +44 -0
  66. data/contrib/zstd/lib/decompress/zstd_decompress.c +1885 -0
  67. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1432 -0
  68. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +59 -0
  69. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +189 -0
  70. data/contrib/zstd/{common → lib/deprecated}/zbuff.h +86 -69
  71. data/contrib/zstd/lib/deprecated/zbuff_common.c +26 -0
  72. data/contrib/zstd/lib/deprecated/zbuff_compress.c +147 -0
  73. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +75 -0
  74. data/contrib/zstd/lib/dictBuilder/cover.c +1236 -0
  75. data/contrib/zstd/lib/dictBuilder/cover.h +157 -0
  76. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.c +3 -3
  77. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.h +5 -5
  78. data/contrib/zstd/lib/dictBuilder/fastcover.c +757 -0
  79. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/zdict.c +437 -347
  80. data/contrib/zstd/lib/dictBuilder/zdict.h +305 -0
  81. data/contrib/zstd/lib/legacy/zstd_legacy.h +415 -0
  82. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.c +272 -292
  83. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.h +26 -32
  84. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.c +162 -392
  85. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.h +26 -32
  86. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.c +162 -391
  87. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.h +27 -33
  88. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.c +195 -604
  89. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.h +26 -32
  90. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.c +300 -575
  91. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.h +22 -31
  92. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.c +165 -592
  93. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.h +54 -67
  94. data/contrib/zstd/lib/legacy/zstd_v07.c +4541 -0
  95. data/contrib/zstd/lib/legacy/zstd_v07.h +187 -0
  96. data/contrib/zstd/lib/libzstd.pc.in +15 -0
  97. data/contrib/zstd/lib/zstd.h +2090 -0
  98. data/ext/depend +2 -0
  99. data/ext/extconf.rb +18 -5
  100. data/ext/extzstd.c +296 -214
  101. data/ext/extzstd.h +81 -36
  102. data/ext/extzstd_nogvls.h +0 -117
  103. data/ext/extzstd_stream.c +622 -0
  104. data/ext/libzstd_conf.h +8 -0
  105. data/ext/zstd_common.c +11 -0
  106. data/ext/zstd_compress.c +15 -0
  107. data/ext/zstd_decompress.c +6 -0
  108. data/ext/zstd_dictbuilder.c +10 -0
  109. data/ext/zstd_dictbuilder_fastcover.c +3 -0
  110. data/ext/zstd_legacy_v01.c +3 -1
  111. data/ext/zstd_legacy_v02.c +3 -1
  112. data/ext/zstd_legacy_v03.c +3 -1
  113. data/ext/zstd_legacy_v04.c +3 -1
  114. data/ext/zstd_legacy_v05.c +3 -1
  115. data/ext/zstd_legacy_v06.c +3 -1
  116. data/ext/zstd_legacy_v07.c +3 -0
  117. data/gemstub.rb +27 -21
  118. data/lib/extzstd.rb +82 -161
  119. data/lib/extzstd/version.rb +1 -1
  120. data/test/test_basic.rb +19 -6
  121. metadata +127 -59
  122. data/contrib/zstd/common/error_private.h +0 -125
  123. data/contrib/zstd/common/error_public.h +0 -77
  124. data/contrib/zstd/common/huf.h +0 -228
  125. data/contrib/zstd/common/zstd.h +0 -475
  126. data/contrib/zstd/common/zstd_common.c +0 -91
  127. data/contrib/zstd/common/zstd_internal.h +0 -238
  128. data/contrib/zstd/compress/huf_compress.c +0 -577
  129. data/contrib/zstd/compress/zbuff_compress.c +0 -327
  130. data/contrib/zstd/compress/zstd_compress.c +0 -3074
  131. data/contrib/zstd/compress/zstd_opt.h +0 -1046
  132. data/contrib/zstd/decompress/huf_decompress.c +0 -894
  133. data/contrib/zstd/decompress/zbuff_decompress.c +0 -294
  134. data/contrib/zstd/decompress/zstd_decompress.c +0 -1362
  135. data/contrib/zstd/dictBuilder/zdict.h +0 -113
  136. data/contrib/zstd/legacy/zstd_legacy.h +0 -140
  137. data/ext/extzstd_buffered.c +0 -265
  138. data/ext/zstd_amalgam.c +0 -18
@@ -1,34 +1,13 @@
1
1
  /*
2
- zstd_v05 - decoder for 0.5 format
3
- Header File
4
- Copyright (C) 2014-2016, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
- * Redistributions of source code must retain the above copyright
12
- notice, this list of conditions and the following disclaimer.
13
- * Redistributions in binary form must reproduce the above
14
- copyright notice, this list of conditions and the following disclaimer
15
- in the documentation and/or other materials provided with the
16
- distribution.
17
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
-
29
- You can contact the author at :
30
- - zstd source repository : https://github.com/Cyan4973/zstd
31
- */
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under both the BSD-style license (found in the
6
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
+ * in the COPYING file in the root directory of this source tree).
8
+ * You may select, at your option, one of the above-listed licenses.
9
+ */
10
+
32
11
  #ifndef ZSTDv05_H
33
12
  #define ZSTDv05_H
34
13
 
@@ -40,7 +19,7 @@ extern "C" {
40
19
  * Dependencies
41
20
  ***************************************/
42
21
  #include <stddef.h> /* size_t */
43
- #include "mem.h" /* U64, U32 */
22
+ #include "../common/mem.h" /* U64, U32 */
44
23
 
45
24
 
46
25
  /* *************************************
@@ -54,6 +33,18 @@ extern "C" {
54
33
  size_t ZSTDv05_decompress( void* dst, size_t dstCapacity,
55
34
  const void* src, size_t compressedSize);
56
35
 
36
+ /**
37
+ ZSTDv05_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.5.x format
38
+ srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
39
+ cSize (output parameter) : the number of bytes that would be read to decompress this frame
40
+ or an error code if it fails (which can be tested using ZSTDv01_isError())
41
+ dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
42
+ or ZSTD_CONTENTSIZE_ERROR if an error occurs
43
+
44
+ note : assumes `cSize` and `dBound` are _not_ NULL.
45
+ */
46
+ void ZSTDv05_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
47
+ size_t* cSize, unsigned long long* dBound);
57
48
 
58
49
  /* *************************************
59
50
  * Helper functions
@@ -1,42 +1,20 @@
1
- /* ******************************************************************
2
- zstd_v06.c
3
- Decompression module for ZSTD v0.6 legacy format
4
- Copyright (C) 2016, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
-
12
- * Redistributions of source code must retain the above copyright
13
- notice, this list of conditions and the following disclaimer.
14
- * Redistributions in binary form must reproduce the above
15
- copyright notice, this list of conditions and the following disclaimer
16
- in the documentation and/or other materials provided with the
17
- distribution.
18
-
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1
+ /*
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under both the BSD-style license (found in the
6
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
+ * in the COPYING file in the root directory of this source tree).
8
+ * You may select, at your option, one of the above-listed licenses.
9
+ */
30
10
 
31
- You can contact the author at :
32
- - Homepage : http://www.zstd.net/
33
- ****************************************************************** */
34
11
 
35
12
  /*- Dependencies -*/
36
13
  #include "zstd_v06.h"
37
14
  #include <stddef.h> /* size_t, ptrdiff_t */
38
15
  #include <string.h> /* memcpy */
39
16
  #include <stdlib.h> /* malloc, free, qsort */
17
+ #include "../common/error_private.h"
40
18
 
41
19
 
42
20
 
@@ -85,6 +63,10 @@ extern "C" {
85
63
  /*-****************************************
86
64
  * Compiler specifics
87
65
  ******************************************/
66
+ #if defined(_MSC_VER) /* Visual Studio */
67
+ # include <stdlib.h> /* _byteswap_ulong */
68
+ # include <intrin.h> /* _byteswap_* */
69
+ #endif
88
70
  #if defined(__GNUC__)
89
71
  # define MEM_STATIC static __attribute__((unused))
90
72
  #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
@@ -138,7 +120,7 @@ extern "C" {
138
120
  #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
139
121
  # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
140
122
  # define MEM_FORCE_MEMORY_ACCESS 2
141
- # elif defined(__INTEL_COMPILER) || \
123
+ # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
142
124
  (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
143
125
  # define MEM_FORCE_MEMORY_ACCESS 1
144
126
  # endif
@@ -160,11 +142,8 @@ Only use if no other choice to achieve best performance on target platform */
160
142
  MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
161
143
  MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
162
144
  MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
163
- MEM_STATIC U64 MEM_readST(const void* memPtr) { return *(const size_t*) memPtr; }
164
145
 
165
146
  MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
166
- MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; }
167
- MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; }
168
147
 
169
148
  #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
170
149
 
@@ -175,11 +154,8 @@ typedef union { U16 u16; U32 u32; U64 u64; size_t st; } __attribute__((packed))
175
154
  MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
176
155
  MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
177
156
  MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
178
- MEM_STATIC U64 MEM_readST(const void* ptr) { return ((const unalign*)ptr)->st; }
179
157
 
180
158
  MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; }
181
- MEM_STATIC void MEM_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; }
182
- MEM_STATIC void MEM_write64(void* memPtr, U64 value) { ((unalign*)memPtr)->u64 = value; }
183
159
 
184
160
  #else
185
161
 
@@ -201,25 +177,11 @@ MEM_STATIC U64 MEM_read64(const void* memPtr)
201
177
  U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
202
178
  }
203
179
 
204
- MEM_STATIC size_t MEM_readST(const void* memPtr)
205
- {
206
- size_t val; memcpy(&val, memPtr, sizeof(val)); return val;
207
- }
208
-
209
180
  MEM_STATIC void MEM_write16(void* memPtr, U16 value)
210
181
  {
211
182
  memcpy(memPtr, &value, sizeof(value));
212
183
  }
213
184
 
214
- MEM_STATIC void MEM_write32(void* memPtr, U32 value)
215
- {
216
- memcpy(memPtr, &value, sizeof(value));
217
- }
218
-
219
- MEM_STATIC void MEM_write64(void* memPtr, U64 value)
220
- {
221
- memcpy(memPtr, &value, sizeof(value));
222
- }
223
185
 
224
186
  #endif /* MEM_FORCE_MEMORY_ACCESS */
225
187
 
@@ -227,7 +189,7 @@ MEM_STATIC U32 MEM_swap32(U32 in)
227
189
  {
228
190
  #if defined(_MSC_VER) /* Visual Studio */
229
191
  return _byteswap_ulong(in);
230
- #elif defined (__GNUC__)
192
+ #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)
231
193
  return __builtin_bswap32(in);
232
194
  #else
233
195
  return ((in << 24) & 0xff000000 ) |
@@ -241,7 +203,7 @@ MEM_STATIC U64 MEM_swap64(U64 in)
241
203
  {
242
204
  #if defined(_MSC_VER) /* Visual Studio */
243
205
  return _byteswap_uint64(in);
244
- #elif defined (__GNUC__)
206
+ #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)
245
207
  return __builtin_bswap64(in);
246
208
  #else
247
209
  return ((in << 56) & 0xff00000000000000ULL) |
@@ -255,13 +217,6 @@ MEM_STATIC U64 MEM_swap64(U64 in)
255
217
  #endif
256
218
  }
257
219
 
258
- MEM_STATIC size_t MEM_swapST(size_t in)
259
- {
260
- if (MEM_32bits())
261
- return (size_t)MEM_swap32((U32)in);
262
- else
263
- return (size_t)MEM_swap64((U64)in);
264
- }
265
220
 
266
221
  /*=== Little endian r/w ===*/
267
222
 
@@ -294,13 +249,6 @@ MEM_STATIC U32 MEM_readLE32(const void* memPtr)
294
249
  return MEM_swap32(MEM_read32(memPtr));
295
250
  }
296
251
 
297
- MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32)
298
- {
299
- if (MEM_isLittleEndian())
300
- MEM_write32(memPtr, val32);
301
- else
302
- MEM_write32(memPtr, MEM_swap32(val32));
303
- }
304
252
 
305
253
  MEM_STATIC U64 MEM_readLE64(const void* memPtr)
306
254
  {
@@ -310,13 +258,6 @@ MEM_STATIC U64 MEM_readLE64(const void* memPtr)
310
258
  return MEM_swap64(MEM_read64(memPtr));
311
259
  }
312
260
 
313
- MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64)
314
- {
315
- if (MEM_isLittleEndian())
316
- MEM_write64(memPtr, val64);
317
- else
318
- MEM_write64(memPtr, MEM_swap64(val64));
319
- }
320
261
 
321
262
  MEM_STATIC size_t MEM_readLEST(const void* memPtr)
322
263
  {
@@ -326,78 +267,7 @@ MEM_STATIC size_t MEM_readLEST(const void* memPtr)
326
267
  return (size_t)MEM_readLE64(memPtr);
327
268
  }
328
269
 
329
- MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
330
- {
331
- if (MEM_32bits())
332
- MEM_writeLE32(memPtr, (U32)val);
333
- else
334
- MEM_writeLE64(memPtr, (U64)val);
335
- }
336
-
337
- /*=== Big endian r/w ===*/
338
-
339
- MEM_STATIC U32 MEM_readBE32(const void* memPtr)
340
- {
341
- if (MEM_isLittleEndian())
342
- return MEM_swap32(MEM_read32(memPtr));
343
- else
344
- return MEM_read32(memPtr);
345
- }
346
-
347
- MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32)
348
- {
349
- if (MEM_isLittleEndian())
350
- MEM_write32(memPtr, MEM_swap32(val32));
351
- else
352
- MEM_write32(memPtr, val32);
353
- }
354
-
355
- MEM_STATIC U64 MEM_readBE64(const void* memPtr)
356
- {
357
- if (MEM_isLittleEndian())
358
- return MEM_swap64(MEM_read64(memPtr));
359
- else
360
- return MEM_read64(memPtr);
361
- }
362
-
363
- MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64)
364
- {
365
- if (MEM_isLittleEndian())
366
- MEM_write64(memPtr, MEM_swap64(val64));
367
- else
368
- MEM_write64(memPtr, val64);
369
- }
370
-
371
- MEM_STATIC size_t MEM_readBEST(const void* memPtr)
372
- {
373
- if (MEM_32bits())
374
- return (size_t)MEM_readBE32(memPtr);
375
- else
376
- return (size_t)MEM_readBE64(memPtr);
377
- }
378
-
379
- MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val)
380
- {
381
- if (MEM_32bits())
382
- MEM_writeBE32(memPtr, (U32)val);
383
- else
384
- MEM_writeBE64(memPtr, (U64)val);
385
- }
386
-
387
270
 
388
- /* function safe only for comparisons */
389
- MEM_STATIC U32 MEM_readMINMATCH(const void* memPtr, U32 length)
390
- {
391
- switch (length)
392
- {
393
- default :
394
- case 4 : return MEM_read32(memPtr);
395
- case 3 : if (MEM_isLittleEndian())
396
- return MEM_read32(memPtr)<<8;
397
- else
398
- return MEM_read32(memPtr)>>8;
399
- }
400
- }
401
271
 
402
272
  #if defined (__cplusplus)
403
273
  }
@@ -405,77 +275,6 @@ MEM_STATIC U32 MEM_readMINMATCH(const void* memPtr, U32 length)
405
275
 
406
276
  #endif /* MEM_H_MODULE */
407
277
 
408
- /* ******************************************************************
409
- Error codes list
410
- Copyright (C) 2016, Yann Collet
411
-
412
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
413
-
414
- Redistribution and use in source and binary forms, with or without
415
- modification, are permitted provided that the following conditions are
416
- met:
417
-
418
- * Redistributions of source code must retain the above copyright
419
- notice, this list of conditions and the following disclaimer.
420
- * Redistributions in binary form must reproduce the above
421
- copyright notice, this list of conditions and the following disclaimer
422
- in the documentation and/or other materials provided with the
423
- distribution.
424
-
425
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
426
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
427
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
428
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
429
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
430
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
431
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
432
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
433
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
434
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
435
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
436
-
437
- You can contact the author at :
438
- - Homepage : http://www.zstd.net
439
- ****************************************************************** */
440
- #ifndef ERROR_PUBLIC_H_MODULE
441
- #define ERROR_PUBLIC_H_MODULE
442
-
443
- #if defined (__cplusplus)
444
- extern "C" {
445
- #endif
446
-
447
-
448
- /* ****************************************
449
- * error codes list
450
- ******************************************/
451
- typedef enum {
452
- ZSTDv06_error_no_error,
453
- ZSTDv06_error_GENERIC,
454
- ZSTDv06_error_prefix_unknown,
455
- ZSTDv06_error_frameParameter_unsupported,
456
- ZSTDv06_error_frameParameter_unsupportedBy32bits,
457
- ZSTDv06_error_compressionParameter_unsupported,
458
- ZSTDv06_error_init_missing,
459
- ZSTDv06_error_memory_allocation,
460
- ZSTDv06_error_stage_wrong,
461
- ZSTDv06_error_dstSize_tooSmall,
462
- ZSTDv06_error_srcSize_wrong,
463
- ZSTDv06_error_corruption_detected,
464
- ZSTDv06_error_tableLog_tooLarge,
465
- ZSTDv06_error_maxSymbolValue_tooLarge,
466
- ZSTDv06_error_maxSymbolValue_tooSmall,
467
- ZSTDv06_error_dictionary_corrupted,
468
- ZSTDv06_error_maxCode
469
- } ZSTDv06_ErrorCode;
470
-
471
- /* note : compare with size_t function results using ZSTDv06_getError() */
472
-
473
-
474
- #if defined (__cplusplus)
475
- }
476
- #endif
477
-
478
- #endif /* ERROR_PUBLIC_H_MODULE */
479
278
  /*
480
279
  zstd - standard compression library
481
280
  Header File for static linking only
@@ -528,7 +327,7 @@ extern "C" {
528
327
  * It avoids reloading the dictionary each time.
529
328
  * `preparedDCtx` must have been properly initialized using ZSTDv06_decompressBegin_usingDict().
530
329
  * Requires 2 contexts : 1 for reference (preparedDCtx), which will not be modified, and 1 to run the decompression operation (dctx) */
531
- ZSTDLIB_API size_t ZSTDv06_decompress_usingPreparedDCtx(
330
+ ZSTDLIBv06_API size_t ZSTDv06_decompress_usingPreparedDCtx(
532
331
  ZSTDv06_DCtx* dctx, const ZSTDv06_DCtx* preparedDCtx,
533
332
  void* dst, size_t dstCapacity,
534
333
  const void* src, size_t srcSize);
@@ -539,7 +338,7 @@ ZSTDLIB_API size_t ZSTDv06_decompress_usingPreparedDCtx(
539
338
  static const size_t ZSTDv06_frameHeaderSize_min = 5;
540
339
  static const size_t ZSTDv06_frameHeaderSize_max = ZSTDv06_FRAMEHEADERSIZE_MAX;
541
340
 
542
- ZSTDLIB_API size_t ZSTDv06_decompressBegin(ZSTDv06_DCtx* dctx);
341
+ ZSTDLIBv06_API size_t ZSTDv06_decompressBegin(ZSTDv06_DCtx* dctx);
543
342
 
544
343
  /*
545
344
  Streaming decompression, direct mode (bufferless)
@@ -598,140 +397,15 @@ ZSTDLIB_API size_t ZSTDv06_decompressBegin(ZSTDv06_DCtx* dctx);
598
397
  */
599
398
 
600
399
  #define ZSTDv06_BLOCKSIZE_MAX (128 * 1024) /* define, for static allocation */
601
- ZSTDLIB_API size_t ZSTDv06_decompressBlock(ZSTDv06_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
400
+ ZSTDLIBv06_API size_t ZSTDv06_decompressBlock(ZSTDv06_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
602
401
 
603
402
 
604
- /*-*************************************
605
- * Error management
606
- ***************************************/
607
- /*! ZSTDv06_getErrorCode() :
608
- convert a `size_t` function result into a `ZSTDv06_ErrorCode` enum type,
609
- which can be used to compare directly with enum list published into "error_public.h" */
610
- ZSTDLIB_API ZSTDv06_ErrorCode ZSTDv06_getErrorCode(size_t functionResult);
611
- ZSTDLIB_API const char* ZSTDv06_getErrorString(ZSTDv06_ErrorCode code);
612
-
613
403
 
614
404
  #if defined (__cplusplus)
615
405
  }
616
406
  #endif
617
407
 
618
408
  #endif /* ZSTDv06_STATIC_H */
619
- /* ******************************************************************
620
- Error codes and messages
621
- Copyright (C) 2013-2016, Yann Collet
622
-
623
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
624
-
625
- Redistribution and use in source and binary forms, with or without
626
- modification, are permitted provided that the following conditions are
627
- met:
628
-
629
- * Redistributions of source code must retain the above copyright
630
- notice, this list of conditions and the following disclaimer.
631
- * Redistributions in binary form must reproduce the above
632
- copyright notice, this list of conditions and the following disclaimer
633
- in the documentation and/or other materials provided with the
634
- distribution.
635
-
636
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
637
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
638
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
639
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
640
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
641
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
642
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
643
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
644
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
645
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
646
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
647
-
648
- You can contact the author at :
649
- - Homepage : http://www.zstd.net
650
- ****************************************************************** */
651
- /* Note : this module is expected to remain private, do not expose it */
652
-
653
- #ifndef ERROR_H_MODULE
654
- #define ERROR_H_MODULE
655
-
656
- #if defined (__cplusplus)
657
- extern "C" {
658
- #endif
659
-
660
-
661
- /* ****************************************
662
- * Compiler-specific
663
- ******************************************/
664
- #if defined(__GNUC__)
665
- # define ERR_STATIC static __attribute__((unused))
666
- #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
667
- # define ERR_STATIC static inline
668
- #elif defined(_MSC_VER)
669
- # define ERR_STATIC static __inline
670
- #else
671
- # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
672
- #endif
673
-
674
-
675
- /*-****************************************
676
- * Customization (error_public.h)
677
- ******************************************/
678
- typedef ZSTDv06_ErrorCode ERR_enum;
679
- #define PREFIX(name) ZSTDv06_error_##name
680
-
681
-
682
- /*-****************************************
683
- * Error codes handling
684
- ******************************************/
685
- #ifdef ERROR
686
- # undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
687
- #endif
688
- #define ERROR(name) ((size_t)-PREFIX(name))
689
-
690
- ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
691
-
692
- ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
693
-
694
-
695
- /*-****************************************
696
- * Error Strings
697
- ******************************************/
698
-
699
- ERR_STATIC const char* ERR_getErrorString(ERR_enum code)
700
- {
701
- static const char* notErrorCode = "Unspecified error code";
702
- switch( code )
703
- {
704
- case PREFIX(no_error): return "No error detected";
705
- case PREFIX(GENERIC): return "Error (generic)";
706
- case PREFIX(prefix_unknown): return "Unknown frame descriptor";
707
- case PREFIX(frameParameter_unsupported): return "Unsupported frame parameter";
708
- case PREFIX(frameParameter_unsupportedBy32bits): return "Frame parameter unsupported in 32-bits mode";
709
- case PREFIX(compressionParameter_unsupported): return "Compression parameter is out of bound";
710
- case PREFIX(init_missing): return "Context should be init first";
711
- case PREFIX(memory_allocation): return "Allocation error : not enough memory";
712
- case PREFIX(stage_wrong): return "Operation not authorized at current processing stage";
713
- case PREFIX(dstSize_tooSmall): return "Destination buffer is too small";
714
- case PREFIX(srcSize_wrong): return "Src size incorrect";
715
- case PREFIX(corruption_detected): return "Corrupted block detected";
716
- case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory : unsupported";
717
- case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max Symbol Value : too large";
718
- case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small";
719
- case PREFIX(dictionary_corrupted): return "Dictionary is corrupted";
720
- case PREFIX(maxCode):
721
- default: return notErrorCode;
722
- }
723
- }
724
-
725
- ERR_STATIC const char* ERR_getErrorName(size_t code)
726
- {
727
- return ERR_getErrorString(ERR_getErrorCode(code));
728
- }
729
-
730
- #if defined (__cplusplus)
731
- }
732
- #endif
733
-
734
- #endif /* ERROR_H_MODULE */
735
409
  /*
736
410
  zstd_internal - common functions to include
737
411
  Header File for include
@@ -777,19 +451,6 @@ ERR_STATIC const char* ERR_getErrorName(size_t code)
777
451
  /*-*************************************
778
452
  * Common constants
779
453
  ***************************************/
780
- #define ZSTDv06_OPT_DEBUG 0 // 3 = compression stats; 5 = check encoded sequences; 9 = full logs
781
- #include <stdio.h>
782
- #if defined(ZSTDv06_OPT_DEBUG) && ZSTDv06_OPT_DEBUG>=9
783
- #define ZSTDv06_LOG_PARSER(...) printf(__VA_ARGS__)
784
- #define ZSTDv06_LOG_ENCODE(...) printf(__VA_ARGS__)
785
- #define ZSTDv06_LOG_BLOCK(...) printf(__VA_ARGS__)
786
- #else
787
- #define ZSTDv06_LOG_PARSER(...)
788
- #define ZSTDv06_LOG_ENCODE(...)
789
- #define ZSTDv06_LOG_BLOCK(...)
790
- #endif
791
-
792
- #define ZSTDv06_OPT_NUM (1<<12)
793
454
  #define ZSTDv06_DICT_MAGIC 0xEC30A436
794
455
 
795
456
  #define ZSTDv06_REP_NUM 3
@@ -845,6 +506,8 @@ typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
845
506
  #define FSEv06_ENCODING_STATIC 2
846
507
  #define FSEv06_ENCODING_DYNAMIC 3
847
508
 
509
+ #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
510
+
848
511
  static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
849
512
  1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
850
513
  13,14,15,16 };
@@ -877,7 +540,7 @@ static void ZSTDv06_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
877
540
  /*! ZSTDv06_wildcopy() :
878
541
  * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
879
542
  #define WILDCOPY_OVERLENGTH 8
880
- MEM_STATIC void ZSTDv06_wildcopy(void* dst, const void* src, size_t length)
543
+ MEM_STATIC void ZSTDv06_wildcopy(void* dst, const void* src, ptrdiff_t length)
881
544
  {
882
545
  const BYTE* ip = (const BYTE*)src;
883
546
  BYTE* op = (BYTE*)dst;
@@ -887,27 +550,6 @@ MEM_STATIC void ZSTDv06_wildcopy(void* dst, const void* src, size_t length)
887
550
  while (op < oend);
888
551
  }
889
552
 
890
- MEM_STATIC unsigned ZSTDv06_highbit(U32 val)
891
- {
892
- # if defined(_MSC_VER) /* Visual */
893
- unsigned long r=0;
894
- _BitScanReverse(&r, val);
895
- return (unsigned)r;
896
- # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
897
- return 31 - __builtin_clz(val);
898
- # else /* Software version */
899
- static const int DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
900
- U32 v = val;
901
- int r;
902
- v |= v >> 1;
903
- v |= v >> 2;
904
- v |= v >> 4;
905
- v |= v >> 8;
906
- v |= v >> 16;
907
- r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
908
- return r;
909
- # endif
910
- }
911
553
 
912
554
 
913
555
  /*-*******************************************
@@ -926,15 +568,7 @@ typedef struct {
926
568
  U32 rep[ZSTDv06_REP_INIT];
927
569
  } ZSTDv06_optimal_t;
928
570
 
929
- #if ZSTDv06_OPT_DEBUG == 3
930
- #include ".debug/zstd_stats.h"
931
- #else
932
- typedef struct { U32 unused; } ZSTDv06_stats_t;
933
- MEM_STATIC void ZSTDv06_statsPrint(ZSTDv06_stats_t* stats, U32 searchLength) { (void)stats; (void)searchLength; }
934
- MEM_STATIC void ZSTDv06_statsInit(ZSTDv06_stats_t* stats) { (void)stats; }
935
- MEM_STATIC void ZSTDv06_statsResetFreqs(ZSTDv06_stats_t* stats) { (void)stats; }
936
- MEM_STATIC void ZSTDv06_statsUpdatePrices(ZSTDv06_stats_t* stats, size_t litLength, const BYTE* literals, size_t offset, size_t matchLength) { (void)stats; (void)litLength; (void)literals; (void)offset; (void)matchLength; }
937
- #endif
571
+ typedef struct { U32 unused; } ZSTDv06_stats_t;
938
572
 
939
573
  typedef struct {
940
574
  void* buffer;
@@ -1207,16 +841,6 @@ MEM_STATIC BITv06_DStream_status BITv06_reloadDStream(BITv06_DStream_t* bitD);
1207
841
  MEM_STATIC unsigned BITv06_endOfDStream(const BITv06_DStream_t* bitD);
1208
842
 
1209
843
 
1210
- /* Start by invoking BITv06_initDStream().
1211
- * A chunk of the bitStream is then stored into a local register.
1212
- * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
1213
- * You can then retrieve bitFields stored into the local register, **in reverse order**.
1214
- * Local register is explicitly reloaded from memory by the BITv06_reloadDStream() method.
1215
- * A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BITv06_DStream_unfinished.
1216
- * Otherwise, it can be less than that, so proceed accordingly.
1217
- * Checking if DStream has reached its end can be performed with BITv06_endOfDStream().
1218
- */
1219
-
1220
844
 
1221
845
  /*-****************************************
1222
846
  * unsafe API
@@ -1229,14 +853,14 @@ MEM_STATIC size_t BITv06_readBitsFast(BITv06_DStream_t* bitD, unsigned nbBits);
1229
853
  /*-**************************************************************
1230
854
  * Internal functions
1231
855
  ****************************************************************/
1232
- MEM_STATIC unsigned BITv06_highbit32 (register U32 val)
856
+ MEM_STATIC unsigned BITv06_highbit32 ( U32 val)
1233
857
  {
1234
858
  # if defined(_MSC_VER) /* Visual */
1235
859
  unsigned long r=0;
1236
860
  _BitScanReverse ( &r, val );
1237
861
  return (unsigned) r;
1238
862
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
1239
- return 31 - __builtin_clz (val);
863
+ return __builtin_clz (val) ^ 31;
1240
864
  # else /* Software version */
1241
865
  static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
1242
866
  U32 v = val;
@@ -1251,9 +875,6 @@ MEM_STATIC unsigned BITv06_highbit32 (register U32 val)
1251
875
  # endif
1252
876
  }
1253
877
 
1254
- /*===== Local Constants =====*/
1255
- static const unsigned BITv06_mask[] = { 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF }; /* up to 26 bits */
1256
-
1257
878
 
1258
879
 
1259
880
  /*-********************************************************
@@ -1282,13 +903,13 @@ MEM_STATIC size_t BITv06_initDStream(BITv06_DStream_t* bitD, const void* srcBuff
1282
903
  bitD->bitContainer = *(const BYTE*)(bitD->start);
1283
904
  switch(srcSize)
1284
905
  {
1285
- case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
1286
- case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
1287
- case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
1288
- case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;
1289
- case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;
1290
- case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8;
1291
- default:;
906
+ case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);/* fall-through */
907
+ case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);/* fall-through */
908
+ case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);/* fall-through */
909
+ case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24; /* fall-through */
910
+ case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16; /* fall-through */
911
+ case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8; /* fall-through */
912
+ default: break;
1292
913
  }
1293
914
  { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
1294
915
  if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */
@@ -1299,45 +920,11 @@ MEM_STATIC size_t BITv06_initDStream(BITv06_DStream_t* bitD, const void* srcBuff
1299
920
  return srcSize;
1300
921
  }
1301
922
 
1302
- MEM_STATIC size_t BITv06_getUpperBits(size_t bitContainer, U32 const start)
1303
- {
1304
- return bitContainer >> start;
1305
- }
1306
923
 
1307
- MEM_STATIC size_t BITv06_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)
1308
- {
1309
- #if defined(__BMI__) && defined(__GNUC__) /* experimental */
1310
- # if defined(__x86_64__)
1311
- if (sizeof(bitContainer)==8)
1312
- return _bextr_u64(bitContainer, start, nbBits);
1313
- else
1314
- # endif
1315
- return _bextr_u32(bitContainer, start, nbBits);
1316
- #else
1317
- return (bitContainer >> start) & BITv06_mask[nbBits];
1318
- #endif
1319
- }
1320
-
1321
- MEM_STATIC size_t BITv06_getLowerBits(size_t bitContainer, U32 const nbBits)
1322
- {
1323
- return bitContainer & BITv06_mask[nbBits];
1324
- }
1325
-
1326
- /*! BITv06_lookBits() :
1327
- * Provides next n bits from local register.
1328
- * local register is not modified.
1329
- * On 32-bits, maxNbBits==24.
1330
- * On 64-bits, maxNbBits==56.
1331
- * @return : value extracted
1332
- */
1333
924
  MEM_STATIC size_t BITv06_lookBits(const BITv06_DStream_t* bitD, U32 nbBits)
1334
925
  {
1335
- #if defined(__BMI__) && defined(__GNUC__) /* experimental; fails if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8 */
1336
- return BITv06_getMiddleBits(bitD->bitContainer, (sizeof(bitD->bitContainer)*8) - bitD->bitsConsumed - nbBits, nbBits);
1337
- #else
1338
926
  U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
1339
927
  return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
1340
- #endif
1341
928
  }
1342
929
 
1343
930
  /*! BITv06_lookBitsFast() :
@@ -1353,11 +940,6 @@ MEM_STATIC void BITv06_skipBits(BITv06_DStream_t* bitD, U32 nbBits)
1353
940
  bitD->bitsConsumed += nbBits;
1354
941
  }
1355
942
 
1356
- /*! BITv06_readBits() :
1357
- * Read (consume) next n bits from local register and update.
1358
- * Pay attention to not read more than nbBits contained into local register.
1359
- * @return : extracted value.
1360
- */
1361
943
  MEM_STATIC size_t BITv06_readBits(BITv06_DStream_t* bitD, U32 nbBits)
1362
944
  {
1363
945
  size_t const value = BITv06_lookBits(bitD, nbBits);
@@ -1374,15 +956,10 @@ MEM_STATIC size_t BITv06_readBitsFast(BITv06_DStream_t* bitD, U32 nbBits)
1374
956
  return value;
1375
957
  }
1376
958
 
1377
- /*! BITv06_reloadDStream() :
1378
- * Refill `BITv06_DStream_t` from src buffer previously defined (see BITv06_initDStream() ).
1379
- * This function is safe, it guarantees it will not read beyond src buffer.
1380
- * @return : status of `BITv06_DStream_t` internal register.
1381
- if status == unfinished, internal register is filled with >= (sizeof(bitD->bitContainer)*8 - 7) bits */
1382
959
  MEM_STATIC BITv06_DStream_status BITv06_reloadDStream(BITv06_DStream_t* bitD)
1383
960
  {
1384
- if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
1385
- return BITv06_DStream_overflow;
961
+ if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
962
+ return BITv06_DStream_overflow;
1386
963
 
1387
964
  if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) {
1388
965
  bitD->ptr -= bitD->bitsConsumed >> 3;
@@ -1501,57 +1078,6 @@ static void FSEv06_initDState(FSEv06_DState_t* DStatePtr, BITv06_DStream_t*
1501
1078
 
1502
1079
  static unsigned char FSEv06_decodeSymbol(FSEv06_DState_t* DStatePtr, BITv06_DStream_t* bitD);
1503
1080
 
1504
- static unsigned FSEv06_endOfDState(const FSEv06_DState_t* DStatePtr);
1505
-
1506
- /*!
1507
- Let's now decompose FSEv06_decompress_usingDTable() into its unitary components.
1508
- You will decode FSE-encoded symbols from the bitStream,
1509
- and also any other bitFields you put in, **in reverse order**.
1510
-
1511
- You will need a few variables to track your bitStream. They are :
1512
-
1513
- BITv06_DStream_t DStream; // Stream context
1514
- FSEv06_DState_t DState; // State context. Multiple ones are possible
1515
- FSEv06_DTable* DTablePtr; // Decoding table, provided by FSEv06_buildDTable()
1516
-
1517
- The first thing to do is to init the bitStream.
1518
- errorCode = BITv06_initDStream(&DStream, srcBuffer, srcSize);
1519
-
1520
- You should then retrieve your initial state(s)
1521
- (in reverse flushing order if you have several ones) :
1522
- errorCode = FSEv06_initDState(&DState, &DStream, DTablePtr);
1523
-
1524
- You can then decode your data, symbol after symbol.
1525
- For information the maximum number of bits read by FSEv06_decodeSymbol() is 'tableLog'.
1526
- Keep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out).
1527
- unsigned char symbol = FSEv06_decodeSymbol(&DState, &DStream);
1528
-
1529
- You can retrieve any bitfield you eventually stored into the bitStream (in reverse order)
1530
- Note : maximum allowed nbBits is 25, for 32-bits compatibility
1531
- size_t bitField = BITv06_readBits(&DStream, nbBits);
1532
-
1533
- All above operations only read from local register (which size depends on size_t).
1534
- Refueling the register from memory is manually performed by the reload method.
1535
- endSignal = FSEv06_reloadDStream(&DStream);
1536
-
1537
- BITv06_reloadDStream() result tells if there is still some more data to read from DStream.
1538
- BITv06_DStream_unfinished : there is still some data left into the DStream.
1539
- BITv06_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled.
1540
- BITv06_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed.
1541
- BITv06_DStream_tooFar : Dstream went too far. Decompression result is corrupted.
1542
-
1543
- When reaching end of buffer (BITv06_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop,
1544
- to properly detect the exact end of stream.
1545
- After each decoded symbol, check if DStream is fully consumed using this simple test :
1546
- BITv06_reloadDStream(&DStream) >= BITv06_DStream_completed
1547
-
1548
- When it's done, verify decompression is fully completed, by checking both DStream and the relevant states.
1549
- Checking if DStream has reached its end is performed by :
1550
- BITv06_endOfDStream(&DStream);
1551
- Check also the states. There might be some symbols left there, if some high probability ones (>50%) are possible.
1552
- FSEv06_endOfDState(&DState);
1553
- */
1554
-
1555
1081
 
1556
1082
  /* *****************************************
1557
1083
  * FSE unsafe API
@@ -1565,7 +1091,7 @@ static unsigned char FSEv06_decodeSymbolFast(FSEv06_DState_t* DStatePtr, BITv06_
1565
1091
  *******************************************/
1566
1092
 
1567
1093
 
1568
- /*<===== Decompression =====>*/
1094
+ /* ====== Decompression ====== */
1569
1095
 
1570
1096
  typedef struct {
1571
1097
  U16 tableLog;
@@ -1626,11 +1152,6 @@ MEM_STATIC BYTE FSEv06_decodeSymbolFast(FSEv06_DState_t* DStatePtr, BITv06_DStre
1626
1152
  return symbol;
1627
1153
  }
1628
1154
 
1629
- MEM_STATIC unsigned FSEv06_endOfDState(const FSEv06_DState_t* DStatePtr)
1630
- {
1631
- return DStatePtr->state == 0;
1632
- }
1633
-
1634
1155
 
1635
1156
 
1636
1157
  #ifndef FSEv06_COMMONDEFS_ONLY
@@ -1731,9 +1252,7 @@ const char* FSEv06_getErrorName(size_t code) { return ERR_getErrorName(code); }
1731
1252
  /* **************************************************************
1732
1253
  * HUF Error Management
1733
1254
  ****************************************************************/
1734
- unsigned HUFv06_isError(size_t code) { return ERR_isError(code); }
1735
-
1736
- const char* HUFv06_getErrorName(size_t code) { return ERR_getErrorName(code); }
1255
+ static unsigned HUFv06_isError(size_t code) { return ERR_isError(code); }
1737
1256
 
1738
1257
 
1739
1258
  /*-**************************************************************
@@ -1876,12 +1395,15 @@ size_t FSEv06_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned
1876
1395
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1877
1396
  # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
1878
1397
  #else
1879
- # ifdef __GNUC__
1880
- # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
1881
- # define FORCE_INLINE static inline __attribute__((always_inline))
1398
+ # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
1399
+ # ifdef __GNUC__
1400
+ # define FORCE_INLINE static inline __attribute__((always_inline))
1401
+ # else
1402
+ # define FORCE_INLINE static inline
1403
+ # endif
1882
1404
  # else
1883
- # define FORCE_INLINE static inline
1884
- # endif
1405
+ # define FORCE_INLINE static
1406
+ # endif /* __STDC_VERSION__ */
1885
1407
  #endif
1886
1408
 
1887
1409
 
@@ -2335,10 +1857,12 @@ MEM_STATIC size_t HUFv06_readStats(BYTE* huffWeight, size_t hwSize, U32* rankSta
2335
1857
  {
2336
1858
  U32 weightTotal;
2337
1859
  const BYTE* ip = (const BYTE*) src;
2338
- size_t iSize = ip[0];
1860
+ size_t iSize;
2339
1861
  size_t oSize;
2340
1862
 
2341
- //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
1863
+ if (!srcSize) return ERROR(srcSize_wrong);
1864
+ iSize = ip[0];
1865
+ /* memset(huffWeight, 0, hwSize); */ /* is not necessary, even though some analyzer complain ... */
2342
1866
 
2343
1867
  if (iSize >= 128) { /* special header */
2344
1868
  if (iSize >= (242)) { /* RLE */
@@ -2372,6 +1896,7 @@ MEM_STATIC size_t HUFv06_readStats(BYTE* huffWeight, size_t hwSize, U32* rankSta
2372
1896
  rankStats[huffWeight[n]]++;
2373
1897
  weightTotal += (1 << huffWeight[n]) >> 1;
2374
1898
  } }
1899
+ if (weightTotal == 0) return ERROR(corruption_detected);
2375
1900
 
2376
1901
  /* get last non-null symbol weight (implied, total must be 2^n) */
2377
1902
  { U32 const tableLog = BITv06_highbit32(weightTotal) + 1;
@@ -2449,14 +1974,7 @@ MEM_STATIC size_t HUFv06_readStats(BYTE* huffWeight, size_t hwSize, U32* rankSta
2449
1974
 
2450
1975
 
2451
1976
  #ifdef _MSC_VER /* Visual Studio */
2452
- # define FORCE_INLINE static __forceinline
2453
1977
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2454
- #else
2455
- # ifdef __GNUC__
2456
- # define FORCE_INLINE static inline __attribute__((always_inline))
2457
- # else
2458
- # define FORCE_INLINE static inline
2459
- # endif
2460
1978
  #endif
2461
1979
 
2462
1980
 
@@ -2496,7 +2014,7 @@ size_t HUFv06_readDTableX2 (U16* DTable, const void* src, size_t srcSize)
2496
2014
  HUFv06_DEltX2* const dt = (HUFv06_DEltX2*)dtPtr;
2497
2015
 
2498
2016
  HUFv06_STATIC_ASSERT(sizeof(HUFv06_DEltX2) == sizeof(U16)); /* if compilation fails here, assertion is false */
2499
- //memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */
2017
+ /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
2500
2018
 
2501
2019
  iSize = HUFv06_readStats(huffWeight, HUFv06_MAX_SYMBOL_VALUE + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
2502
2020
  if (HUFv06_isError(iSize)) return iSize;
@@ -2822,7 +2340,7 @@ size_t HUFv06_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
2822
2340
 
2823
2341
  HUFv06_STATIC_ASSERT(sizeof(HUFv06_DEltX4) == sizeof(U32)); /* if compilation fails here, assertion is false */
2824
2342
  if (memLog > HUFv06_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
2825
- //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */
2343
+ /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
2826
2344
 
2827
2345
  iSize = HUFv06_readStats(weightList, HUFv06_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2828
2346
  if (HUFv06_isError(iSize)) return iSize;
@@ -3146,13 +2664,13 @@ size_t HUFv06_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cS
3146
2664
 
3147
2665
  { U32 algoNb = 0;
3148
2666
  if (Dtime[1] < Dtime[0]) algoNb = 1;
3149
- // if (Dtime[2] < Dtime[algoNb]) algoNb = 2; /* current speed of HUFv06_decompress4X6 is not good */
2667
+ /* if (Dtime[2] < Dtime[algoNb]) algoNb = 2; */ /* current speed of HUFv06_decompress4X6 is not good */
3150
2668
  return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
3151
2669
  }
3152
2670
 
3153
- //return HUFv06_decompress4X2(dst, dstSize, cSrc, cSrcSize); /* multi-streams single-symbol decoding */
3154
- //return HUFv06_decompress4X4(dst, dstSize, cSrc, cSrcSize); /* multi-streams double-symbols decoding */
3155
- //return HUFv06_decompress4X6(dst, dstSize, cSrc, cSrcSize); /* multi-streams quad-symbols decoding */
2671
+ /* return HUFv06_decompress4X2(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams single-symbol decoding */
2672
+ /* return HUFv06_decompress4X4(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams double-symbols decoding */
2673
+ /* return HUFv06_decompress4X6(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams quad-symbols decoding */
3156
2674
  }
3157
2675
  /*
3158
2676
  Common functions of Zstd compression library
@@ -3201,14 +2719,6 @@ unsigned ZSTDv06_isError(size_t code) { return ERR_isError(code); }
3201
2719
  * provides error code string from function result (useful for debugging) */
3202
2720
  const char* ZSTDv06_getErrorName(size_t code) { return ERR_getErrorName(code); }
3203
2721
 
3204
- /*! ZSTDv06_getError() :
3205
- * convert a `size_t` function result into a proper ZSTDv06_errorCode enum */
3206
- ZSTDv06_ErrorCode ZSTDv06_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
3207
-
3208
- /*! ZSTDv06_getErrorString() :
3209
- * provides error code string from enum */
3210
- const char* ZSTDv06_getErrorString(ZSTDv06_ErrorCode code) { return ERR_getErrorName(code); }
3211
-
3212
2722
 
3213
2723
  /* **************************************************************
3214
2724
  * ZBUFF Error Management
@@ -3265,16 +2775,9 @@ const char* ZBUFFv06_getErrorName(size_t errorCode) { return ERR_getErrorName(er
3265
2775
  * Compiler specifics
3266
2776
  *********************************************************/
3267
2777
  #ifdef _MSC_VER /* Visual Studio */
3268
- # define FORCE_INLINE static __forceinline
3269
2778
  # include <intrin.h> /* For Visual 2005 */
3270
2779
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
3271
2780
  # pragma warning(disable : 4324) /* disable: C4324: padded structure */
3272
- #else
3273
- # ifdef __GNUC__
3274
- # define FORCE_INLINE static inline __attribute__((always_inline))
3275
- # else
3276
- # define FORCE_INLINE static inline
3277
- # endif
3278
2781
  #endif
3279
2782
 
3280
2783
 
@@ -3315,13 +2818,13 @@ struct ZSTDv06_DCtx_s
3315
2818
  ZSTDv06_dStage stage;
3316
2819
  U32 flagRepeatTable;
3317
2820
  const BYTE* litPtr;
3318
- size_t litBufSize;
3319
2821
  size_t litSize;
3320
2822
  BYTE litBuffer[ZSTDv06_BLOCKSIZE_MAX + WILDCOPY_OVERLENGTH];
3321
2823
  BYTE headerBuffer[ZSTDv06_FRAMEHEADERSIZE_MAX];
3322
2824
  }; /* typedef'd to ZSTDv06_DCtx within "zstd_static.h" */
3323
2825
 
3324
- size_t ZSTDv06_sizeofDCtx (void) { return sizeof(ZSTDv06_DCtx); } /* non published interface */
2826
+ size_t ZSTDv06_sizeofDCtx (void); /* Hidden declaration */
2827
+ size_t ZSTDv06_sizeofDCtx (void) { return sizeof(ZSTDv06_DCtx); }
3325
2828
 
3326
2829
  size_t ZSTDv06_decompressBegin(ZSTDv06_DCtx* dctx)
3327
2830
  {
@@ -3507,7 +3010,7 @@ size_t ZSTDv06_getFrameParams(ZSTDv06_frameParams* fparamsPtr, const void* src,
3507
3010
  static size_t ZSTDv06_decodeFrameHeader(ZSTDv06_DCtx* zc, const void* src, size_t srcSize)
3508
3011
  {
3509
3012
  size_t const result = ZSTDv06_getFrameParams(&(zc->fParams), src, srcSize);
3510
- if ((MEM_32bits()) && (zc->fParams.windowLog > 25)) return ERROR(frameParameter_unsupportedBy32bits);
3013
+ if ((MEM_32bits()) && (zc->fParams.windowLog > 25)) return ERROR(frameParameter_unsupported);
3511
3014
  return result;
3512
3015
  }
3513
3016
 
@@ -3520,7 +3023,7 @@ typedef struct
3520
3023
 
3521
3024
  /*! ZSTDv06_getcBlockSize() :
3522
3025
  * Provides the size of compressed block from block header `src` */
3523
- size_t ZSTDv06_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
3026
+ static size_t ZSTDv06_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
3524
3027
  {
3525
3028
  const BYTE* const in = (const BYTE* const)src;
3526
3029
  U32 cSize;
@@ -3539,6 +3042,7 @@ size_t ZSTDv06_getcBlockSize(const void* src, size_t srcSize, blockProperties_t*
3539
3042
 
3540
3043
  static size_t ZSTDv06_copyRawBlock(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
3541
3044
  {
3045
+ if (dst==NULL) return ERROR(dstSize_tooSmall);
3542
3046
  if (srcSize > dstCapacity) return ERROR(dstSize_tooSmall);
3543
3047
  memcpy(dst, src, srcSize);
3544
3048
  return srcSize;
@@ -3547,7 +3051,7 @@ static size_t ZSTDv06_copyRawBlock(void* dst, size_t dstCapacity, const void* sr
3547
3051
 
3548
3052
  /*! ZSTDv06_decodeLiteralsBlock() :
3549
3053
  @return : nb of bytes read from src (< srcSize ) */
3550
- size_t ZSTDv06_decodeLiteralsBlock(ZSTDv06_DCtx* dctx,
3054
+ static size_t ZSTDv06_decodeLiteralsBlock(ZSTDv06_DCtx* dctx,
3551
3055
  const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
3552
3056
  {
3553
3057
  const BYTE* const istart = (const BYTE*) src;
@@ -3592,8 +3096,8 @@ size_t ZSTDv06_decodeLiteralsBlock(ZSTDv06_DCtx* dctx,
3592
3096
  return ERROR(corruption_detected);
3593
3097
 
3594
3098
  dctx->litPtr = dctx->litBuffer;
3595
- dctx->litBufSize = ZSTDv06_BLOCKSIZE_MAX+8;
3596
3099
  dctx->litSize = litSize;
3100
+ memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
3597
3101
  return litCSize + lhSize;
3598
3102
  }
3599
3103
  case IS_PCH:
@@ -3608,13 +3112,14 @@ size_t ZSTDv06_decodeLiteralsBlock(ZSTDv06_DCtx* dctx,
3608
3112
  lhSize=3;
3609
3113
  litSize = ((istart[0] & 15) << 6) + (istart[1] >> 2);
3610
3114
  litCSize = ((istart[1] & 3) << 8) + istart[2];
3115
+ if (litCSize + lhSize > srcSize) return ERROR(corruption_detected);
3611
3116
 
3612
3117
  { size_t const errorCode = HUFv06_decompress1X4_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->hufTableX4);
3613
3118
  if (HUFv06_isError(errorCode)) return ERROR(corruption_detected);
3614
3119
  }
3615
3120
  dctx->litPtr = dctx->litBuffer;
3616
- dctx->litBufSize = ZSTDv06_BLOCKSIZE_MAX+WILDCOPY_OVERLENGTH;
3617
3121
  dctx->litSize = litSize;
3122
+ memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
3618
3123
  return litCSize + lhSize;
3619
3124
  }
3620
3125
  case IS_RAW:
@@ -3638,13 +3143,12 @@ size_t ZSTDv06_decodeLiteralsBlock(ZSTDv06_DCtx* dctx,
3638
3143
  if (litSize+lhSize > srcSize) return ERROR(corruption_detected);
3639
3144
  memcpy(dctx->litBuffer, istart+lhSize, litSize);
3640
3145
  dctx->litPtr = dctx->litBuffer;
3641
- dctx->litBufSize = ZSTDv06_BLOCKSIZE_MAX+8;
3642
3146
  dctx->litSize = litSize;
3147
+ memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
3643
3148
  return lhSize+litSize;
3644
3149
  }
3645
3150
  /* direct reference into compressed stream */
3646
3151
  dctx->litPtr = istart+lhSize;
3647
- dctx->litBufSize = srcSize-lhSize;
3648
3152
  dctx->litSize = litSize;
3649
3153
  return lhSize+litSize;
3650
3154
  }
@@ -3666,9 +3170,8 @@ size_t ZSTDv06_decodeLiteralsBlock(ZSTDv06_DCtx* dctx,
3666
3170
  break;
3667
3171
  }
3668
3172
  if (litSize > ZSTDv06_BLOCKSIZE_MAX) return ERROR(corruption_detected);
3669
- memset(dctx->litBuffer, istart[lhSize], litSize);
3173
+ memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH);
3670
3174
  dctx->litPtr = dctx->litBuffer;
3671
- dctx->litBufSize = ZSTDv06_BLOCKSIZE_MAX+WILDCOPY_OVERLENGTH;
3672
3175
  dctx->litSize = litSize;
3673
3176
  return lhSize+1;
3674
3177
  }
@@ -3682,7 +3185,7 @@ size_t ZSTDv06_decodeLiteralsBlock(ZSTDv06_DCtx* dctx,
3682
3185
  @return : nb bytes read from src,
3683
3186
  or an error code if it fails, testable with ZSTDv06_isError()
3684
3187
  */
3685
- size_t ZSTDv06_buildSeqTable(FSEv06_DTable* DTable, U32 type, U32 max, U32 maxLog,
3188
+ static size_t ZSTDv06_buildSeqTable(FSEv06_DTable* DTable, U32 type, U32 max, U32 maxLog,
3686
3189
  const void* src, size_t srcSize,
3687
3190
  const S16* defaultNorm, U32 defaultLog, U32 flagRepeatTable)
3688
3191
  {
@@ -3712,7 +3215,7 @@ size_t ZSTDv06_buildSeqTable(FSEv06_DTable* DTable, U32 type, U32 max, U32 maxLo
3712
3215
  }
3713
3216
 
3714
3217
 
3715
- size_t ZSTDv06_decodeSeqHeaders(int* nbSeqPtr,
3218
+ static size_t ZSTDv06_decodeSeqHeaders(int* nbSeqPtr,
3716
3219
  FSEv06_DTable* DTableLL, FSEv06_DTable* DTableML, FSEv06_DTable* DTableOffb, U32 flagRepeatTable,
3717
3220
  const void* src, size_t srcSize)
3718
3221
  {
@@ -3727,23 +3230,24 @@ size_t ZSTDv06_decodeSeqHeaders(int* nbSeqPtr,
3727
3230
  { int nbSeq = *ip++;
3728
3231
  if (!nbSeq) { *nbSeqPtr=0; return 1; }
3729
3232
  if (nbSeq > 0x7F) {
3730
- if (nbSeq == 0xFF)
3233
+ if (nbSeq == 0xFF) {
3234
+ if (ip+2 > iend) return ERROR(srcSize_wrong);
3731
3235
  nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2;
3732
- else
3236
+ } else {
3237
+ if (ip >= iend) return ERROR(srcSize_wrong);
3733
3238
  nbSeq = ((nbSeq-0x80)<<8) + *ip++;
3239
+ }
3734
3240
  }
3735
3241
  *nbSeqPtr = nbSeq;
3736
3242
  }
3737
3243
 
3738
3244
  /* FSE table descriptors */
3245
+ if (ip + 4 > iend) return ERROR(srcSize_wrong); /* min : header byte + all 3 are "raw", hence no header, but at least xxLog bits per type */
3739
3246
  { U32 const LLtype = *ip >> 6;
3740
3247
  U32 const Offtype = (*ip >> 4) & 3;
3741
3248
  U32 const MLtype = (*ip >> 2) & 3;
3742
3249
  ip++;
3743
3250
 
3744
- /* check */
3745
- if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */
3746
-
3747
3251
  /* Build DTables */
3748
3252
  { size_t const bhSize = ZSTDv06_buildSeqTable(DTableLL, LLtype, MaxLL, LLFSELog, ip, iend-ip, LL_defaultNorm, LL_defaultNormLog, flagRepeatTable);
3749
3253
  if (ZSTDv06_isError(bhSize)) return ERROR(corruption_detected);
@@ -3854,9 +3358,9 @@ static void ZSTDv06_decodeSequence(seq_t* seq, seqState_t* seqState)
3854
3358
  }
3855
3359
 
3856
3360
 
3857
- size_t ZSTDv06_execSequence(BYTE* op,
3361
+ static size_t ZSTDv06_execSequence(BYTE* op,
3858
3362
  BYTE* const oend, seq_t sequence,
3859
- const BYTE** litPtr, const BYTE* const litLimit_8,
3363
+ const BYTE** litPtr, const BYTE* const litLimit,
3860
3364
  const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
3861
3365
  {
3862
3366
  BYTE* const oLitEnd = op + sequence.litLength;
@@ -3869,7 +3373,7 @@ size_t ZSTDv06_execSequence(BYTE* op,
3869
3373
  /* check */
3870
3374
  if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */
3871
3375
  if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
3872
- if (iLitEnd > litLimit_8) return ERROR(corruption_detected); /* over-read beyond lit buffer */
3376
+ if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */
3873
3377
 
3874
3378
  /* copy Literals */
3875
3379
  ZSTDv06_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
@@ -3891,13 +3395,18 @@ size_t ZSTDv06_execSequence(BYTE* op,
3891
3395
  op = oLitEnd + length1;
3892
3396
  sequence.matchLength -= length1;
3893
3397
  match = base;
3398
+ if (op > oend_8 || sequence.matchLength < MINMATCH) {
3399
+ while (op < oMatchEnd) *op++ = *match++;
3400
+ return sequenceLength;
3401
+ }
3894
3402
  } }
3403
+ /* Requirement: op <= oend_8 */
3895
3404
 
3896
3405
  /* match within prefix */
3897
3406
  if (sequence.offset < 8) {
3898
3407
  /* close range match, overlap */
3899
3408
  static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
3900
- static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* substracted */
3409
+ static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */
3901
3410
  int const sub2 = dec64table[sequence.offset];
3902
3411
  op[0] = match[0];
3903
3412
  op[1] = match[1];
@@ -3919,7 +3428,7 @@ size_t ZSTDv06_execSequence(BYTE* op,
3919
3428
  }
3920
3429
  while (op < oMatchEnd) *op++ = *match++;
3921
3430
  } else {
3922
- ZSTDv06_wildcopy(op, match, sequence.matchLength-8); /* works even if matchLength < 8 */
3431
+ ZSTDv06_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */
3923
3432
  }
3924
3433
  return sequenceLength;
3925
3434
  }
@@ -3936,7 +3445,6 @@ static size_t ZSTDv06_decompressSequences(
3936
3445
  BYTE* const oend = ostart + maxDstSize;
3937
3446
  BYTE* op = ostart;
3938
3447
  const BYTE* litPtr = dctx->litPtr;
3939
- const BYTE* const litLimit_8 = litPtr + dctx->litBufSize - 8;
3940
3448
  const BYTE* const litEnd = litPtr + dctx->litSize;
3941
3449
  FSEv06_DTable* DTableLL = dctx->LLTable;
3942
3450
  FSEv06_DTable* DTableML = dctx->MLTable;
@@ -3980,7 +3488,7 @@ static size_t ZSTDv06_decompressSequences(
3980
3488
  pos, (U32)sequence.litLength, (U32)sequence.matchLength, (U32)sequence.offset);
3981
3489
  #endif
3982
3490
 
3983
- { size_t const oneSeqSize = ZSTDv06_execSequence(op, oend, sequence, &litPtr, litLimit_8, base, vBase, dictEnd);
3491
+ { size_t const oneSeqSize = ZSTDv06_execSequence(op, oend, sequence, &litPtr, litEnd, base, vBase, dictEnd);
3984
3492
  if (ZSTDv06_isError(oneSeqSize)) return oneSeqSize;
3985
3493
  op += oneSeqSize;
3986
3494
  } }
@@ -3993,8 +3501,10 @@ static size_t ZSTDv06_decompressSequences(
3993
3501
  { size_t const lastLLSize = litEnd - litPtr;
3994
3502
  if (litPtr > litEnd) return ERROR(corruption_detected); /* too many literals already used */
3995
3503
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3996
- memcpy(op, litPtr, lastLLSize);
3997
- op += lastLLSize;
3504
+ if (lastLLSize > 0) {
3505
+ memcpy(op, litPtr, lastLLSize);
3506
+ op += lastLLSize;
3507
+ }
3998
3508
  }
3999
3509
 
4000
3510
  return op-ostart;
@@ -4146,6 +3656,63 @@ size_t ZSTDv06_decompress(void* dst, size_t dstCapacity, const void* src, size_t
4146
3656
  #endif
4147
3657
  }
4148
3658
 
3659
+ /* ZSTD_errorFrameSizeInfoLegacy() :
3660
+ assumes `cSize` and `dBound` are _not_ NULL */
3661
+ static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)
3662
+ {
3663
+ *cSize = ret;
3664
+ *dBound = ZSTD_CONTENTSIZE_ERROR;
3665
+ }
3666
+
3667
+ void ZSTDv06_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)
3668
+ {
3669
+ const BYTE* ip = (const BYTE*)src;
3670
+ size_t remainingSize = srcSize;
3671
+ size_t nbBlocks = 0;
3672
+ blockProperties_t blockProperties = { bt_compressed, 0 };
3673
+
3674
+ /* Frame Header */
3675
+ { size_t const frameHeaderSize = ZSTDv06_frameHeaderSize(src, srcSize);
3676
+ if (ZSTDv06_isError(frameHeaderSize)) {
3677
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, frameHeaderSize);
3678
+ return;
3679
+ }
3680
+ if (MEM_readLE32(src) != ZSTDv06_MAGICNUMBER) {
3681
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));
3682
+ return;
3683
+ }
3684
+ if (srcSize < frameHeaderSize+ZSTDv06_blockHeaderSize) {
3685
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3686
+ return;
3687
+ }
3688
+ ip += frameHeaderSize; remainingSize -= frameHeaderSize;
3689
+ }
3690
+
3691
+ /* Loop on each block */
3692
+ while (1) {
3693
+ size_t const cBlockSize = ZSTDv06_getcBlockSize(ip, remainingSize, &blockProperties);
3694
+ if (ZSTDv06_isError(cBlockSize)) {
3695
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize);
3696
+ return;
3697
+ }
3698
+
3699
+ ip += ZSTDv06_blockHeaderSize;
3700
+ remainingSize -= ZSTDv06_blockHeaderSize;
3701
+ if (cBlockSize > remainingSize) {
3702
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3703
+ return;
3704
+ }
3705
+
3706
+ if (cBlockSize == 0) break; /* bt_end */
3707
+
3708
+ ip += cBlockSize;
3709
+ remainingSize -= cBlockSize;
3710
+ nbBlocks++;
3711
+ }
3712
+
3713
+ *cSize = ip - (const BYTE*)src;
3714
+ *dBound = nbBlocks * ZSTDv06_BLOCKSIZE_MAX;
3715
+ }
4149
3716
 
4150
3717
  /*_******************************
4151
3718
  * Streaming Decompression API
@@ -4175,7 +3742,7 @@ size_t ZSTDv06_decompressContinue(ZSTDv06_DCtx* dctx, void* dst, size_t dstCapac
4175
3742
  return 0;
4176
3743
  }
4177
3744
  dctx->expected = 0; /* not necessary to copy more */
4178
-
3745
+ /* fall-through */
4179
3746
  case ZSTDds_decodeFrameHeader:
4180
3747
  { size_t result;
4181
3748
  memcpy(dctx->headerBuffer + ZSTDv06_frameHeaderSize_min, src, dctx->expected);
@@ -4247,9 +3814,10 @@ static size_t ZSTDv06_loadEntropy(ZSTDv06_DCtx* dctx, const void* dict, size_t d
4247
3814
  dictSize -= hSize;
4248
3815
 
4249
3816
  { short offcodeNCount[MaxOff+1];
4250
- U32 offcodeMaxValue=MaxOff, offcodeLog=OffFSELog;
3817
+ U32 offcodeMaxValue=MaxOff, offcodeLog;
4251
3818
  offcodeHeaderSize = FSEv06_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dict, dictSize);
4252
3819
  if (FSEv06_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted);
3820
+ if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted);
4253
3821
  { size_t const errorCode = FSEv06_buildDTable(dctx->OffTable, offcodeNCount, offcodeMaxValue, offcodeLog);
4254
3822
  if (FSEv06_isError(errorCode)) return ERROR(dictionary_corrupted); }
4255
3823
  dict = (const char*)dict + offcodeHeaderSize;
@@ -4257,9 +3825,10 @@ static size_t ZSTDv06_loadEntropy(ZSTDv06_DCtx* dctx, const void* dict, size_t d
4257
3825
  }
4258
3826
 
4259
3827
  { short matchlengthNCount[MaxML+1];
4260
- unsigned matchlengthMaxValue = MaxML, matchlengthLog = MLFSELog;
3828
+ unsigned matchlengthMaxValue = MaxML, matchlengthLog;
4261
3829
  matchlengthHeaderSize = FSEv06_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dict, dictSize);
4262
3830
  if (FSEv06_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted);
3831
+ if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted);
4263
3832
  { size_t const errorCode = FSEv06_buildDTable(dctx->MLTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog);
4264
3833
  if (FSEv06_isError(errorCode)) return ERROR(dictionary_corrupted); }
4265
3834
  dict = (const char*)dict + matchlengthHeaderSize;
@@ -4267,9 +3836,10 @@ static size_t ZSTDv06_loadEntropy(ZSTDv06_DCtx* dctx, const void* dict, size_t d
4267
3836
  }
4268
3837
 
4269
3838
  { short litlengthNCount[MaxLL+1];
4270
- unsigned litlengthMaxValue = MaxLL, litlengthLog = LLFSELog;
3839
+ unsigned litlengthMaxValue = MaxLL, litlengthLog;
4271
3840
  litlengthHeaderSize = FSEv06_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dict, dictSize);
4272
3841
  if (FSEv06_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted);
3842
+ if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted);
4273
3843
  { size_t const errorCode = FSEv06_buildDTable(dctx->LLTable, litlengthNCount, litlengthMaxValue, litlengthLog);
4274
3844
  if (FSEv06_isError(errorCode)) return ERROR(dictionary_corrupted); }
4275
3845
  }
@@ -4432,7 +4002,9 @@ size_t ZBUFFv06_decompressInit(ZBUFFv06_DCtx* zbd)
4432
4002
  MEM_STATIC size_t ZBUFFv06_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
4433
4003
  {
4434
4004
  size_t length = MIN(dstCapacity, srcSize);
4435
- memcpy(dst, src, length);
4005
+ if (length > 0) {
4006
+ memcpy(dst, src, length);
4007
+ }
4436
4008
  return length;
4437
4009
  }
4438
4010
 
@@ -4464,7 +4036,7 @@ size_t ZBUFFv06_decompressContinue(ZBUFFv06_DCtx* zbd,
4464
4036
  if (ZSTDv06_isError(hSize)) return hSize;
4465
4037
  if (toLoad > (size_t)(iend-ip)) { /* not enough input to load full header */
4466
4038
  memcpy(zbd->headerBuffer + zbd->lhSize, ip, iend-ip);
4467
- zbd->lhSize += iend-ip; ip = iend; notDone = 0;
4039
+ zbd->lhSize += iend-ip;
4468
4040
  *dstCapacityPtr = 0;
4469
4041
  return (hSize - zbd->lhSize) + ZSTDv06_blockHeaderSize; /* remaining header bytes + next block header */
4470
4042
  }
@@ -4491,7 +4063,7 @@ size_t ZBUFFv06_decompressContinue(ZBUFFv06_DCtx* zbd,
4491
4063
  zbd->inBuff = (char*)malloc(blockSize);
4492
4064
  if (zbd->inBuff == NULL) return ERROR(memory_allocation);
4493
4065
  }
4494
- { size_t const neededOutSize = ((size_t)1 << zbd->fParams.windowLog) + blockSize;
4066
+ { size_t const neededOutSize = ((size_t)1 << zbd->fParams.windowLog) + blockSize + WILDCOPY_OVERLENGTH * 2;
4495
4067
  if (zbd->outBuffSize < neededOutSize) {
4496
4068
  free(zbd->outBuff);
4497
4069
  zbd->outBuffSize = neededOutSize;
@@ -4499,7 +4071,7 @@ size_t ZBUFFv06_decompressContinue(ZBUFFv06_DCtx* zbd,
4499
4071
  if (zbd->outBuff == NULL) return ERROR(memory_allocation);
4500
4072
  } } }
4501
4073
  zbd->stage = ZBUFFds_read;
4502
-
4074
+ /* fall-through */
4503
4075
  case ZBUFFds_read:
4504
4076
  { size_t const neededInSize = ZSTDv06_nextSrcSizeToDecompress(zbd->zd);
4505
4077
  if (neededInSize==0) { /* end of frame */
@@ -4521,7 +4093,7 @@ size_t ZBUFFv06_decompressContinue(ZBUFFv06_DCtx* zbd,
4521
4093
  if (ip==iend) { notDone = 0; break; } /* no more input */
4522
4094
  zbd->stage = ZBUFFds_load;
4523
4095
  }
4524
-
4096
+ /* fall-through */
4525
4097
  case ZBUFFds_load:
4526
4098
  { size_t const neededInSize = ZSTDv06_nextSrcSizeToDecompress(zbd->zd);
4527
4099
  size_t const toLoad = neededInSize - zbd->inPos; /* should always be <= remaining space within inBuff */
@@ -4541,9 +4113,10 @@ size_t ZBUFFv06_decompressContinue(ZBUFFv06_DCtx* zbd,
4541
4113
  if (!decodedSize) { zbd->stage = ZBUFFds_read; break; } /* this was just a header */
4542
4114
  zbd->outEnd = zbd->outStart + decodedSize;
4543
4115
  zbd->stage = ZBUFFds_flush;
4544
- // break; /* ZBUFFds_flush follows */
4545
- } }
4546
-
4116
+ /* break; */ /* ZBUFFds_flush follows */
4117
+ }
4118
+ }
4119
+ /* fall-through */
4547
4120
  case ZBUFFds_flush:
4548
4121
  { size_t const toFlushSize = zbd->outEnd - zbd->outStart;
4549
4122
  size_t const flushedSize = ZBUFFv06_limitCopy(op, oend-op, zbd->outBuff + zbd->outStart, toFlushSize);