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,36 +1,15 @@
1
1
  /*
2
- zstd_v03 - decoder for 0.3 format
3
- Header File
4
- Copyright (C) 2015, 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
- - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
32
- */
33
- #pragma once
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
+
11
+ #ifndef ZSTD_V03_H_298734209782
12
+ #define ZSTD_V03_H_298734209782
34
13
 
35
14
  #if defined (__cplusplus)
36
15
  extern "C" {
@@ -56,7 +35,20 @@ ZSTDv03_decompress() : decompress ZSTD frames compliant with v0.3.x format
56
35
  size_t ZSTDv03_decompress( void* dst, size_t maxOriginalSize,
57
36
  const void* src, size_t compressedSize);
58
37
 
59
- /**
38
+ /**
39
+ ZSTDv03_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.3.x format
40
+ srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
41
+ cSize (output parameter) : the number of bytes that would be read to decompress this frame
42
+ or an error code if it fails (which can be tested using ZSTDv01_isError())
43
+ dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
44
+ or ZSTD_CONTENTSIZE_ERROR if an error occurs
45
+
46
+ note : assumes `cSize` and `dBound` are _not_ NULL.
47
+ */
48
+ void ZSTDv03_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
49
+ size_t* cSize, unsigned long long* dBound);
50
+
51
+ /**
60
52
  ZSTDv03_isError() : tells if the result of ZSTDv03_decompress() is an error
61
53
  */
62
54
  unsigned ZSTDv03_isError(size_t code);
@@ -97,3 +89,5 @@ size_t ZSTDv03_decompressContinue(ZSTDv03_Dctx* dctx, void* dst, size_t maxDstSi
97
89
  #if defined (__cplusplus)
98
90
  }
99
91
  #endif
92
+
93
+ #endif /* ZSTD_V03_H_298734209782 */
@@ -1,75 +1,27 @@
1
- /* ******************************************************************
2
- zstd_v04.c
3
- Decompression module for ZSTD v0.4 legacy format
4
- Copyright (C) 2016, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
-
12
- * Redistributions of source code must retain the above copyright
13
- notice, this list of conditions and the following disclaimer.
14
- * Redistributions in binary form must reproduce the above
15
- copyright notice, this list of conditions and the following disclaimer
16
- in the documentation and/or other materials provided with the
17
- distribution.
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
+ */
18
10
 
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
11
 
31
- You can contact the author at :
32
- - Homepage : http://www.zstd.net/
33
- ****************************************************************** */
12
+ /******************************************
13
+ * Includes
14
+ ******************************************/
15
+ #include <stddef.h> /* size_t, ptrdiff_t */
16
+ #include <string.h> /* memcpy */
34
17
 
35
- /*- Dependencies -*/
36
18
  #include "zstd_v04.h"
19
+ #include "../common/error_private.h"
37
20
 
38
21
 
39
22
  /* ******************************************************************
40
- mem.h
41
- low-level memory access routines
42
- Copyright (C) 2013-2015, Yann Collet.
43
-
44
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
45
-
46
- Redistribution and use in source and binary forms, with or without
47
- modification, are permitted provided that the following conditions are
48
- met:
49
-
50
- * Redistributions of source code must retain the above copyright
51
- notice, this list of conditions and the following disclaimer.
52
- * Redistributions in binary form must reproduce the above
53
- copyright notice, this list of conditions and the following disclaimer
54
- in the documentation and/or other materials provided with the
55
- distribution.
56
-
57
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
58
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
59
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
60
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
61
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
62
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
63
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
67
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68
-
69
- You can contact the author at :
70
- - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
71
- - Public forum : https://groups.google.com/forum/#!forum/lz4c
72
- ****************************************************************** */
23
+ * mem.h
24
+ *******************************************************************/
73
25
  #ifndef MEM_H_MODULE
74
26
  #define MEM_H_MODULE
75
27
 
@@ -77,16 +29,14 @@
77
29
  extern "C" {
78
30
  #endif
79
31
 
80
- /******************************************
81
- * Includes
82
- ******************************************/
83
- #include <stddef.h> /* size_t, ptrdiff_t */
84
- #include <string.h> /* memcpy */
85
-
86
32
 
87
33
  /******************************************
88
34
  * Compiler-specific
89
35
  ******************************************/
36
+ #if defined(_MSC_VER) /* Visual Studio */
37
+ # include <stdlib.h> /* _byteswap_ulong */
38
+ # include <intrin.h> /* _byteswap_* */
39
+ #endif
90
40
  #if defined(__GNUC__)
91
41
  # define MEM_STATIC static __attribute__((unused))
92
42
  #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
@@ -121,6 +71,15 @@ extern "C" {
121
71
  #endif
122
72
 
123
73
 
74
+ /*-*************************************
75
+ * Debug
76
+ ***************************************/
77
+ #include "debug.h"
78
+ #ifndef assert
79
+ # define assert(condition) ((void)0)
80
+ #endif
81
+
82
+
124
83
  /****************************************************************
125
84
  * Memory I/O
126
85
  *****************************************************************/
@@ -140,7 +99,7 @@ extern "C" {
140
99
  #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
141
100
  # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
142
101
  # define MEM_FORCE_MEMORY_ACCESS 2
143
- # elif defined(__INTEL_COMPILER) || \
102
+ # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
144
103
  (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
145
104
  # define MEM_FORCE_MEMORY_ACCESS 1
146
105
  # endif
@@ -164,8 +123,6 @@ MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
164
123
  MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
165
124
 
166
125
  MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
167
- MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; }
168
- MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; }
169
126
 
170
127
  #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
171
128
 
@@ -178,8 +135,6 @@ MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign*)ptr)->u32;
178
135
  MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
179
136
 
180
137
  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
138
 
184
139
  #else
185
140
 
@@ -206,17 +161,7 @@ MEM_STATIC void MEM_write16(void* memPtr, U16 value)
206
161
  memcpy(memPtr, &value, sizeof(value));
207
162
  }
208
163
 
209
- MEM_STATIC void MEM_write32(void* memPtr, U32 value)
210
- {
211
- memcpy(memPtr, &value, sizeof(value));
212
- }
213
-
214
- MEM_STATIC void MEM_write64(void* memPtr, U64 value)
215
- {
216
- memcpy(memPtr, &value, sizeof(value));
217
- }
218
-
219
- #endif // MEM_FORCE_MEMORY_ACCESS
164
+ #endif /* MEM_FORCE_MEMORY_ACCESS */
220
165
 
221
166
 
222
167
  MEM_STATIC U16 MEM_readLE16(const void* memPtr)
@@ -244,6 +189,11 @@ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
244
189
  }
245
190
  }
246
191
 
192
+ MEM_STATIC U32 MEM_readLE24(const void* memPtr)
193
+ {
194
+ return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
195
+ }
196
+
247
197
  MEM_STATIC U32 MEM_readLE32(const void* memPtr)
248
198
  {
249
199
  if (MEM_isLittleEndian())
@@ -255,21 +205,6 @@ MEM_STATIC U32 MEM_readLE32(const void* memPtr)
255
205
  }
256
206
  }
257
207
 
258
- MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32)
259
- {
260
- if (MEM_isLittleEndian())
261
- {
262
- MEM_write32(memPtr, val32);
263
- }
264
- else
265
- {
266
- BYTE* p = (BYTE*)memPtr;
267
- p[0] = (BYTE)val32;
268
- p[1] = (BYTE)(val32>>8);
269
- p[2] = (BYTE)(val32>>16);
270
- p[3] = (BYTE)(val32>>24);
271
- }
272
- }
273
208
 
274
209
  MEM_STATIC U64 MEM_readLE64(const void* memPtr)
275
210
  {
@@ -283,25 +218,6 @@ MEM_STATIC U64 MEM_readLE64(const void* memPtr)
283
218
  }
284
219
  }
285
220
 
286
- MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64)
287
- {
288
- if (MEM_isLittleEndian())
289
- {
290
- MEM_write64(memPtr, val64);
291
- }
292
- else
293
- {
294
- BYTE* p = (BYTE*)memPtr;
295
- p[0] = (BYTE)val64;
296
- p[1] = (BYTE)(val64>>8);
297
- p[2] = (BYTE)(val64>>16);
298
- p[3] = (BYTE)(val64>>24);
299
- p[4] = (BYTE)(val64>>32);
300
- p[5] = (BYTE)(val64>>40);
301
- p[6] = (BYTE)(val64>>48);
302
- p[7] = (BYTE)(val64>>56);
303
- }
304
- }
305
221
 
306
222
  MEM_STATIC size_t MEM_readLEST(const void* memPtr)
307
223
  {
@@ -311,13 +227,6 @@ MEM_STATIC size_t MEM_readLEST(const void* memPtr)
311
227
  return (size_t)MEM_readLE64(memPtr);
312
228
  }
313
229
 
314
- MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
315
- {
316
- if (MEM_32bits())
317
- MEM_writeLE32(memPtr, (U32)val);
318
- else
319
- MEM_writeLE64(memPtr, (U64)val);
320
- }
321
230
 
322
231
  #if defined (__cplusplus)
323
232
  }
@@ -325,137 +234,18 @@ MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
325
234
 
326
235
  #endif /* MEM_H_MODULE */
327
236
 
328
- /* ******************************************************************
329
- Error codes list
330
- Copyright (C) 2016, Yann Collet
331
-
332
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
333
-
334
- Redistribution and use in source and binary forms, with or without
335
- modification, are permitted provided that the following conditions are
336
- met:
337
-
338
- * Redistributions of source code must retain the above copyright
339
- notice, this list of conditions and the following disclaimer.
340
- * Redistributions in binary form must reproduce the above
341
- copyright notice, this list of conditions and the following disclaimer
342
- in the documentation and/or other materials provided with the
343
- distribution.
344
-
345
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
346
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
347
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
348
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
349
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
350
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
351
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
353
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
354
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
355
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
356
-
357
- You can contact the author at :
358
- - Source repository : https://github.com/Cyan4973/zstd
359
- ****************************************************************** */
360
- #ifndef ERROR_PUBLIC_H_MODULE
361
- #define ERROR_PUBLIC_H_MODULE
362
-
363
- #if defined (__cplusplus)
364
- extern "C" {
365
- #endif
366
-
367
-
368
- /* ****************************************
369
- * error list
370
- ******************************************/
371
- enum {
372
- ZSTD_error_No_Error,
373
- ZSTD_error_GENERIC,
374
- ZSTD_error_prefix_unknown,
375
- ZSTD_error_frameParameter_unsupported,
376
- ZSTD_error_frameParameter_unsupportedBy32bitsImplementation,
377
- ZSTD_error_init_missing,
378
- ZSTD_error_memory_allocation,
379
- ZSTD_error_stage_wrong,
380
- ZSTD_error_dstSize_tooSmall,
381
- ZSTD_error_srcSize_wrong,
382
- ZSTD_error_corruption_detected,
383
- ZSTD_error_tableLog_tooLarge,
384
- ZSTD_error_maxSymbolValue_tooLarge,
385
- ZSTD_error_maxSymbolValue_tooSmall,
386
- ZSTD_error_maxCode
387
- };
388
-
389
- /* note : functions provide error codes in reverse negative order,
390
- so compare with (size_t)(0-enum) */
391
-
392
-
393
- #if defined (__cplusplus)
394
- }
395
- #endif
396
-
397
- #endif /* ERROR_PUBLIC_H_MODULE */
398
-
399
-
400
-
401
237
  /*
402
238
  zstd - standard compression library
403
239
  Header File for static linking only
404
- Copyright (C) 2014-2015, Yann Collet.
405
-
406
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
407
-
408
- Redistribution and use in source and binary forms, with or without
409
- modification, are permitted provided that the following conditions are
410
- met:
411
- * Redistributions of source code must retain the above copyright
412
- notice, this list of conditions and the following disclaimer.
413
- * Redistributions in binary form must reproduce the above
414
- copyright notice, this list of conditions and the following disclaimer
415
- in the documentation and/or other materials provided with the
416
- distribution.
417
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
418
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
419
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
420
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
421
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
422
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
423
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
424
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
425
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
426
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
427
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
428
-
429
- You can contact the author at :
430
- - zstd source repository : https://github.com/Cyan4973/zstd
431
- - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
432
240
  */
433
241
  #ifndef ZSTD_STATIC_H
434
242
  #define ZSTD_STATIC_H
435
243
 
436
- /* The objects defined into this file shall be considered experimental.
437
- * They are not considered stable, as their prototype may change in the future.
438
- * You can use them for tests, provide feedback, or if you can endure risks of future changes.
439
- */
440
-
441
- #if defined (__cplusplus)
442
- extern "C" {
443
- #endif
444
244
 
445
245
  /* *************************************
446
246
  * Types
447
247
  ***************************************/
448
- #define ZSTD_WINDOWLOG_MAX 26
449
- #define ZSTD_WINDOWLOG_MIN 18
450
248
  #define ZSTD_WINDOWLOG_ABSOLUTEMIN 11
451
- #define ZSTD_CONTENTLOG_MAX (ZSTD_WINDOWLOG_MAX+1)
452
- #define ZSTD_CONTENTLOG_MIN 4
453
- #define ZSTD_HASHLOG_MAX 28
454
- #define ZSTD_HASHLOG_MIN 4
455
- #define ZSTD_SEARCHLOG_MAX (ZSTD_CONTENTLOG_MAX-1)
456
- #define ZSTD_SEARCHLOG_MIN 1
457
- #define ZSTD_SEARCHLENGTH_MAX 7
458
- #define ZSTD_SEARCHLENGTH_MIN 4
459
249
 
460
250
  /** from faster to stronger */
461
251
  typedef enum { ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2 } ZSTD_strategy;
@@ -527,118 +317,6 @@ static size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t maxDstS
527
317
  */
528
318
 
529
319
 
530
- #if defined (__cplusplus)
531
- }
532
- #endif
533
-
534
- /* ******************************************************************
535
- Error codes and messages
536
- Copyright (C) 2013-2016, Yann Collet
537
-
538
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
539
-
540
- Redistribution and use in source and binary forms, with or without
541
- modification, are permitted provided that the following conditions are
542
- met:
543
-
544
- * Redistributions of source code must retain the above copyright
545
- notice, this list of conditions and the following disclaimer.
546
- * Redistributions in binary form must reproduce the above
547
- copyright notice, this list of conditions and the following disclaimer
548
- in the documentation and/or other materials provided with the
549
- distribution.
550
-
551
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
552
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
553
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
554
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
555
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
556
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
557
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
558
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
559
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
560
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
561
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
562
-
563
- You can contact the author at :
564
- - Source repository : https://github.com/Cyan4973/zstd
565
- ****************************************************************** */
566
- /* Note : this module is expected to remain private, do not expose it */
567
-
568
- #ifndef ERROR_H_MODULE
569
- #define ERROR_H_MODULE
570
-
571
- #if defined (__cplusplus)
572
- extern "C" {
573
- #endif
574
-
575
-
576
- /* *****************************************
577
- * Includes
578
- ******************************************/
579
- #include <stddef.h> /* size_t, ptrdiff_t */
580
-
581
-
582
- /* *****************************************
583
- * Compiler-specific
584
- ******************************************/
585
- #if defined(__GNUC__)
586
- # define ERR_STATIC static __attribute__((unused))
587
- #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
588
- # define ERR_STATIC static inline
589
- #elif defined(_MSC_VER)
590
- # define ERR_STATIC static __inline
591
- #else
592
- # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
593
- #endif
594
-
595
-
596
- /* *****************************************
597
- * Error Codes
598
- ******************************************/
599
- #define PREFIX(name) ZSTD_error_##name
600
-
601
- #ifdef ERROR
602
- # undef ERROR /* reported already defined on VS 2015 by Rich Geldreich */
603
- #endif
604
- #define ERROR(name) (size_t)-PREFIX(name)
605
-
606
- ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
607
-
608
-
609
- /* *****************************************
610
- * Error Strings
611
- ******************************************/
612
-
613
- ERR_STATIC const char* ERR_getErrorName(size_t code)
614
- {
615
- static const char* codeError = "Unspecified error code";
616
- switch( (size_t)(0-code) )
617
- {
618
- case ZSTD_error_No_Error: return "No error detected";
619
- case ZSTD_error_GENERIC: return "Error (generic)";
620
- case ZSTD_error_prefix_unknown: return "Unknown frame descriptor";
621
- case ZSTD_error_frameParameter_unsupported: return "Unsupported frame parameter";
622
- case ZSTD_error_frameParameter_unsupportedBy32bitsImplementation: return "Frame parameter unsupported in 32-bits mode";
623
- case ZSTD_error_init_missing: return "Context should be init first";
624
- case ZSTD_error_memory_allocation: return "Allocation error : not enough memory";
625
- case ZSTD_error_dstSize_tooSmall: return "Destination buffer is too small";
626
- case ZSTD_error_srcSize_wrong: return "Src size incorrect";
627
- case ZSTD_error_corruption_detected: return "Corrupted block detected";
628
- case ZSTD_error_tableLog_tooLarge: return "tableLog requires too much memory";
629
- case ZSTD_error_maxSymbolValue_tooLarge: return "Unsupported max possible Symbol Value : too large";
630
- case ZSTD_error_maxSymbolValue_tooSmall: return "Specified maxSymbolValue is too small";
631
- case ZSTD_error_maxCode:
632
- default: return codeError;
633
- }
634
- }
635
-
636
-
637
- #if defined (__cplusplus)
638
- }
639
- #endif
640
-
641
- #endif /* ERROR_H_MODULE */
642
320
 
643
321
 
644
322
  #endif /* ZSTD_STATIC_H */
@@ -647,42 +325,10 @@ ERR_STATIC const char* ERR_getErrorName(size_t code)
647
325
  /*
648
326
  zstd_internal - common functions to include
649
327
  Header File for include
650
- Copyright (C) 2014-2015, Yann Collet.
651
-
652
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
653
-
654
- Redistribution and use in source and binary forms, with or without
655
- modification, are permitted provided that the following conditions are
656
- met:
657
- * Redistributions of source code must retain the above copyright
658
- notice, this list of conditions and the following disclaimer.
659
- * Redistributions in binary form must reproduce the above
660
- copyright notice, this list of conditions and the following disclaimer
661
- in the documentation and/or other materials provided with the
662
- distribution.
663
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
664
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
665
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
666
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
667
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
668
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
669
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
670
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
671
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
672
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
673
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
674
-
675
- You can contact the author at :
676
- - zstd source repository : https://github.com/Cyan4973/zstd
677
- - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
678
328
  */
679
329
  #ifndef ZSTD_CCOMMON_H_MODULE
680
330
  #define ZSTD_CCOMMON_H_MODULE
681
331
 
682
- #if defined (__cplusplus)
683
- extern "C" {
684
- #endif
685
-
686
332
  /* *************************************
687
333
  * Common macros
688
334
  ***************************************/
@@ -732,6 +378,8 @@ static const size_t ZSTD_frameHeaderSize_min = 5;
732
378
  #define MIN_SEQUENCES_SIZE (2 /*seqNb*/ + 2 /*dumps*/ + 3 /*seqTables*/ + 1 /*bitStream*/)
733
379
  #define MIN_CBLOCK_SIZE (3 /*litCSize*/ + MIN_SEQUENCES_SIZE)
734
380
 
381
+ #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
382
+
735
383
  typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
736
384
 
737
385
 
@@ -743,7 +391,7 @@ static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
743
391
  #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
744
392
 
745
393
  /*! ZSTD_wildcopy : custom version of memcpy(), can copy up to 7-8 bytes too many */
746
- static void ZSTD_wildcopy(void* dst, const void* src, size_t length)
394
+ static void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
747
395
  {
748
396
  const BYTE* ip = (const BYTE*)src;
749
397
  BYTE* op = (BYTE*)dst;
@@ -754,44 +402,10 @@ static void ZSTD_wildcopy(void* dst, const void* src, size_t length)
754
402
  }
755
403
 
756
404
 
757
- #if defined (__cplusplus)
758
- }
759
- #endif
760
-
761
405
 
762
406
  /* ******************************************************************
763
407
  FSE : Finite State Entropy coder
764
408
  header file
765
- Copyright (C) 2013-2015, Yann Collet.
766
-
767
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
768
-
769
- Redistribution and use in source and binary forms, with or without
770
- modification, are permitted provided that the following conditions are
771
- met:
772
-
773
- * Redistributions of source code must retain the above copyright
774
- notice, this list of conditions and the following disclaimer.
775
- * Redistributions in binary form must reproduce the above
776
- copyright notice, this list of conditions and the following disclaimer
777
- in the documentation and/or other materials provided with the
778
- distribution.
779
-
780
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
781
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
782
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
783
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
784
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
785
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
786
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
787
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
788
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
789
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
790
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
791
-
792
- You can contact the author at :
793
- - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
794
- - Public forum : https://groups.google.com/forum/#!forum/lz4c
795
409
  ****************************************************************** */
796
410
  #ifndef FSE_H
797
411
  #define FSE_H
@@ -993,16 +607,6 @@ MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
993
607
  MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
994
608
 
995
609
 
996
- /*
997
- * Start by invoking BIT_initDStream().
998
- * A chunk of the bitStream is then stored into a local register.
999
- * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
1000
- * You can then retrieve bitFields stored into the local register, **in reverse order**.
1001
- * Local register is manually filled from memory by the BIT_reloadDStream() method.
1002
- * A reload guarantee a minimum of ((8*sizeof(size_t))-7) bits when its result is BIT_DStream_unfinished.
1003
- * Otherwise, it can be less than that, so proceed accordingly.
1004
- * Checking if DStream has reached its end can be performed with BIT_endOfDStream()
1005
- */
1006
610
 
1007
611
 
1008
612
  /******************************************
@@ -1016,14 +620,14 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
1016
620
  /****************************************************************
1017
621
  * Helper functions
1018
622
  ****************************************************************/
1019
- MEM_STATIC unsigned BIT_highbit32 (register U32 val)
623
+ MEM_STATIC unsigned BIT_highbit32 (U32 val)
1020
624
  {
1021
625
  # if defined(_MSC_VER) /* Visual */
1022
626
  unsigned long r=0;
1023
627
  _BitScanReverse ( &r, val );
1024
628
  return (unsigned) r;
1025
629
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
1026
- return 31 - __builtin_clz (val);
630
+ return __builtin_clz (val) ^ 31;
1027
631
  # else /* Software version */
1028
632
  static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
1029
633
  U32 v = val;
@@ -1072,13 +676,13 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
1072
676
  bitD->bitContainer = *(const BYTE*)(bitD->start);
1073
677
  switch(srcSize)
1074
678
  {
1075
- case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);
1076
- case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);
1077
- case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);
1078
- case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24;
1079
- case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16;
1080
- case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8;
1081
- default:;
679
+ case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);/* fall-through */
680
+ case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);/* fall-through */
681
+ case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);/* fall-through */
682
+ case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24; /* fall-through */
683
+ case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16; /* fall-through */
684
+ case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8; /* fall-through */
685
+ default: break;
1082
686
  }
1083
687
  contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
1084
688
  if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
@@ -1089,13 +693,6 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
1089
693
  return srcSize;
1090
694
  }
1091
695
 
1092
- /*!BIT_lookBits
1093
- * Provides next n bits from local register
1094
- * local register is not modified (bits are still present for next read/look)
1095
- * On 32-bits, maxNbBits==25
1096
- * On 64-bits, maxNbBits==57
1097
- * @return : value extracted
1098
- */
1099
696
  MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits)
1100
697
  {
1101
698
  const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
@@ -1115,11 +712,6 @@ MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
1115
712
  bitD->bitsConsumed += nbBits;
1116
713
  }
1117
714
 
1118
- /*!BIT_readBits
1119
- * Read next n bits from local register.
1120
- * pay attention to not read more than nbBits contained into local register.
1121
- * @return : extracted value.
1122
- */
1123
715
  MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
1124
716
  {
1125
717
  size_t value = BIT_lookBits(bitD, nbBits);
@@ -1138,8 +730,8 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
1138
730
 
1139
731
  MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
1140
732
  {
1141
- if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
1142
- return BIT_DStream_overflow;
733
+ if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
734
+ return BIT_DStream_overflow;
1143
735
 
1144
736
  if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer))
1145
737
  {
@@ -1266,55 +858,6 @@ static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bi
1266
858
 
1267
859
  static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr);
1268
860
 
1269
- /*!
1270
- Let's now decompose FSE_decompress_usingDTable() into its unitary components.
1271
- You will decode FSE-encoded symbols from the bitStream,
1272
- and also any other bitFields you put in, **in reverse order**.
1273
-
1274
- You will need a few variables to track your bitStream. They are :
1275
-
1276
- BIT_DStream_t DStream; // Stream context
1277
- FSE_DState_t DState; // State context. Multiple ones are possible
1278
- FSE_DTable* DTablePtr; // Decoding table, provided by FSE_buildDTable()
1279
-
1280
- The first thing to do is to init the bitStream.
1281
- errorCode = BIT_initDStream(&DStream, srcBuffer, srcSize);
1282
-
1283
- You should then retrieve your initial state(s)
1284
- (in reverse flushing order if you have several ones) :
1285
- errorCode = FSE_initDState(&DState, &DStream, DTablePtr);
1286
-
1287
- You can then decode your data, symbol after symbol.
1288
- For information the maximum number of bits read by FSE_decodeSymbol() is 'tableLog'.
1289
- Keep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out).
1290
- unsigned char symbol = FSE_decodeSymbol(&DState, &DStream);
1291
-
1292
- You can retrieve any bitfield you eventually stored into the bitStream (in reverse order)
1293
- Note : maximum allowed nbBits is 25, for 32-bits compatibility
1294
- size_t bitField = BIT_readBits(&DStream, nbBits);
1295
-
1296
- All above operations only read from local register (which size depends on size_t).
1297
- Refueling the register from memory is manually performed by the reload method.
1298
- endSignal = FSE_reloadDStream(&DStream);
1299
-
1300
- BIT_reloadDStream() result tells if there is still some more data to read from DStream.
1301
- BIT_DStream_unfinished : there is still some data left into the DStream.
1302
- BIT_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled.
1303
- BIT_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed.
1304
- BIT_DStream_tooFar : Dstream went too far. Decompression result is corrupted.
1305
-
1306
- When reaching end of buffer (BIT_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop,
1307
- to properly detect the exact end of stream.
1308
- After each decoded symbol, check if DStream is fully consumed using this simple test :
1309
- BIT_reloadDStream(&DStream) >= BIT_DStream_completed
1310
-
1311
- When it's done, verify decompression is fully completed, by checking both DStream and the relevant states.
1312
- Checking if DStream has reached its end is performed by :
1313
- BIT_endOfDStream(&DStream);
1314
- Check also the states. There might be some symbols left there, if some high probability ones (>50%) are possible.
1315
- FSE_endOfDState(&DState);
1316
- */
1317
-
1318
861
 
1319
862
  /* *****************************************
1320
863
  * FSE unsafe API
@@ -1455,12 +998,15 @@ MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
1455
998
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1456
999
  # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
1457
1000
  #else
1458
- # ifdef __GNUC__
1459
- # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
1460
- # define FORCE_INLINE static inline __attribute__((always_inline))
1001
+ # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
1002
+ # ifdef __GNUC__
1003
+ # define FORCE_INLINE static inline __attribute__((always_inline))
1004
+ # else
1005
+ # define FORCE_INLINE static inline
1006
+ # endif
1461
1007
  # else
1462
- # define FORCE_INLINE static inline
1463
- # endif
1008
+ # define FORCE_INLINE static
1009
+ # endif /* __STDC_VERSION__ */
1464
1010
  #endif
1465
1011
 
1466
1012
 
@@ -1544,6 +1090,7 @@ static size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, un
1544
1090
  if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
1545
1091
 
1546
1092
  /* Init, lay down lowprob symbols */
1093
+ memset(tableDecode, 0, sizeof(FSE_DECODE_TYPE) * (maxSymbolValue+1) ); /* useless init, but keep static analyzer happy, and we don't need to performance optimize legacy decoders */
1547
1094
  DTableH.tableLog = (U16)tableLog;
1548
1095
  for (s=0; s<=maxSymbolValue; s++)
1549
1096
  {
@@ -1704,8 +1251,8 @@ static size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsi
1704
1251
  else
1705
1252
  {
1706
1253
  bitCount -= (int)(8 * (iend - 4 - ip));
1707
- ip = iend - 4;
1708
- }
1254
+ ip = iend - 4;
1255
+ }
1709
1256
  bitStream = MEM_readLE32(ip) >> (bitCount & 31);
1710
1257
  }
1711
1258
  }
@@ -2096,15 +1643,7 @@ static size_t HUF_decompress4X4_usingDTable(void* dst, size_t maxDstSize, const
2096
1643
 
2097
1644
 
2098
1645
  #ifdef _MSC_VER /* Visual Studio */
2099
- # define FORCE_INLINE static __forceinline
2100
1646
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2101
- #else
2102
- # ifdef __GNUC__
2103
- # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
2104
- # define FORCE_INLINE static inline __attribute__((always_inline))
2105
- # else
2106
- # define FORCE_INLINE static inline
2107
- # endif
2108
1647
  #endif
2109
1648
 
2110
1649
 
@@ -2157,10 +1696,12 @@ static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
2157
1696
  U32 weightTotal;
2158
1697
  U32 tableLog;
2159
1698
  const BYTE* ip = (const BYTE*) src;
2160
- size_t iSize = ip[0];
1699
+ size_t iSize;
2161
1700
  size_t oSize;
2162
1701
  U32 n;
2163
1702
 
1703
+ if (!srcSize) return ERROR(srcSize_wrong);
1704
+ iSize = ip[0];
2164
1705
  //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
2165
1706
 
2166
1707
  if (iSize >= 128) /* special header */
@@ -2202,6 +1743,7 @@ static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
2202
1743
  rankStats[huffWeight[n]]++;
2203
1744
  weightTotal += (1 << huffWeight[n]) >> 1;
2204
1745
  }
1746
+ if (weightTotal == 0) return ERROR(corruption_detected);
2205
1747
 
2206
1748
  /* get last non-null symbol weight (implied, total must be 2^n) */
2207
1749
  tableLog = BIT_highbit32(weightTotal) + 1;
@@ -2912,17 +2454,9 @@ static size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_
2912
2454
  * Compiler specifics
2913
2455
  *********************************************************/
2914
2456
  #ifdef _MSC_VER /* Visual Studio */
2915
- # define FORCE_INLINE static __forceinline
2916
2457
  # include <intrin.h> /* For Visual 2005 */
2917
2458
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2918
2459
  # pragma warning(disable : 4324) /* disable: C4324: padded structure */
2919
- #else
2920
- # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
2921
- # ifdef __GNUC__
2922
- # define FORCE_INLINE static inline __attribute__((always_inline))
2923
- # else
2924
- # define FORCE_INLINE static inline
2925
- # endif
2926
2460
  #endif
2927
2461
 
2928
2462
 
@@ -2972,7 +2506,6 @@ struct ZSTDv04_Dctx_s
2972
2506
  blockType_t bType;
2973
2507
  ZSTD_dStage stage;
2974
2508
  const BYTE* litPtr;
2975
- size_t litBufSize;
2976
2509
  size_t litSize;
2977
2510
  BYTE litBuffer[BLOCKSIZE + 8 /* margin for wildcopy */];
2978
2511
  BYTE headerBuffer[ZSTD_frameHeaderSize_max];
@@ -3043,7 +2576,7 @@ static size_t ZSTD_decodeFrameHeader_Part2(ZSTD_DCtx* zc, const void* src, size_
3043
2576
  size_t result;
3044
2577
  if (srcSize != zc->headerSize) return ERROR(srcSize_wrong);
3045
2578
  result = ZSTD_getFrameParams(&(zc->params), src, srcSize);
3046
- if ((MEM_32bits()) && (zc->params.windowLog > 25)) return ERROR(frameParameter_unsupportedBy32bitsImplementation);
2579
+ if ((MEM_32bits()) && (zc->params.windowLog > 25)) return ERROR(frameParameter_unsupported);
3047
2580
  return result;
3048
2581
  }
3049
2582
 
@@ -3070,7 +2603,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
3070
2603
  static size_t ZSTD_copyRawBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3071
2604
  {
3072
2605
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
3073
- memcpy(dst, src, srcSize);
2606
+ if (srcSize > 0) {
2607
+ memcpy(dst, src, srcSize);
2608
+ }
3074
2609
  return srcSize;
3075
2610
  }
3076
2611
 
@@ -3113,8 +2648,8 @@ static size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
3113
2648
  size_t litSize = BLOCKSIZE;
3114
2649
  const size_t readSize = ZSTD_decompressLiterals(dctx->litBuffer, &litSize, src, srcSize);
3115
2650
  dctx->litPtr = dctx->litBuffer;
3116
- dctx->litBufSize = BLOCKSIZE+8;
3117
2651
  dctx->litSize = litSize;
2652
+ memset(dctx->litBuffer + dctx->litSize, 0, 8);
3118
2653
  return readSize; /* works if it's an error too */
3119
2654
  }
3120
2655
  case IS_RAW:
@@ -3122,25 +2657,24 @@ static size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
3122
2657
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
3123
2658
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
3124
2659
  {
2660
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
3125
2661
  if (litSize > srcSize-3) return ERROR(corruption_detected);
3126
2662
  memcpy(dctx->litBuffer, istart, litSize);
3127
2663
  dctx->litPtr = dctx->litBuffer;
3128
- dctx->litBufSize = BLOCKSIZE+8;
3129
2664
  dctx->litSize = litSize;
2665
+ memset(dctx->litBuffer + dctx->litSize, 0, 8);
3130
2666
  return litSize+3;
3131
2667
  }
3132
2668
  /* direct reference into compressed stream */
3133
2669
  dctx->litPtr = istart+3;
3134
- dctx->litBufSize = srcSize-3;
3135
2670
  dctx->litSize = litSize;
3136
2671
  return litSize+3; }
3137
2672
  case IS_RLE:
3138
2673
  {
3139
2674
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
3140
2675
  if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
3141
- memset(dctx->litBuffer, istart[3], litSize);
2676
+ memset(dctx->litBuffer, istart[3], litSize + 8);
3142
2677
  dctx->litPtr = dctx->litBuffer;
3143
- dctx->litBufSize = BLOCKSIZE+8;
3144
2678
  dctx->litSize = litSize;
3145
2679
  return 4;
3146
2680
  }
@@ -3196,7 +2730,6 @@ static size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* d
3196
2730
  /* Build DTables */
3197
2731
  switch(LLtype)
3198
2732
  {
3199
- U32 max;
3200
2733
  case bt_rle :
3201
2734
  LLlog = 0;
3202
2735
  FSE_buildDTable_rle(DTableLL, *ip++); break;
@@ -3204,17 +2737,16 @@ static size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* d
3204
2737
  LLlog = LLbits;
3205
2738
  FSE_buildDTable_raw(DTableLL, LLbits); break;
3206
2739
  default :
3207
- max = MaxLL;
3208
- headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip);
3209
- if (FSE_isError(headerSize)) return ERROR(GENERIC);
3210
- if (LLlog > LLFSELog) return ERROR(corruption_detected);
3211
- ip += headerSize;
3212
- FSE_buildDTable(DTableLL, norm, max, LLlog);
3213
- }
2740
+ { U32 max = MaxLL;
2741
+ headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip);
2742
+ if (FSE_isError(headerSize)) return ERROR(GENERIC);
2743
+ if (LLlog > LLFSELog) return ERROR(corruption_detected);
2744
+ ip += headerSize;
2745
+ FSE_buildDTable(DTableLL, norm, max, LLlog);
2746
+ } }
3214
2747
 
3215
2748
  switch(Offtype)
3216
2749
  {
3217
- U32 max;
3218
2750
  case bt_rle :
3219
2751
  Offlog = 0;
3220
2752
  if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
@@ -3224,17 +2756,16 @@ static size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* d
3224
2756
  Offlog = Offbits;
3225
2757
  FSE_buildDTable_raw(DTableOffb, Offbits); break;
3226
2758
  default :
3227
- max = MaxOff;
3228
- headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip);
3229
- if (FSE_isError(headerSize)) return ERROR(GENERIC);
3230
- if (Offlog > OffFSELog) return ERROR(corruption_detected);
3231
- ip += headerSize;
3232
- FSE_buildDTable(DTableOffb, norm, max, Offlog);
3233
- }
2759
+ { U32 max = MaxOff;
2760
+ headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip);
2761
+ if (FSE_isError(headerSize)) return ERROR(GENERIC);
2762
+ if (Offlog > OffFSELog) return ERROR(corruption_detected);
2763
+ ip += headerSize;
2764
+ FSE_buildDTable(DTableOffb, norm, max, Offlog);
2765
+ } }
3234
2766
 
3235
2767
  switch(MLtype)
3236
2768
  {
3237
- U32 max;
3238
2769
  case bt_rle :
3239
2770
  MLlog = 0;
3240
2771
  if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
@@ -3243,14 +2774,13 @@ static size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* d
3243
2774
  MLlog = MLbits;
3244
2775
  FSE_buildDTable_raw(DTableML, MLbits); break;
3245
2776
  default :
3246
- max = MaxML;
3247
- headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip);
3248
- if (FSE_isError(headerSize)) return ERROR(GENERIC);
3249
- if (MLlog > MLFSELog) return ERROR(corruption_detected);
3250
- ip += headerSize;
3251
- FSE_buildDTable(DTableML, norm, max, MLlog);
3252
- }
3253
- }
2777
+ { U32 max = MaxML;
2778
+ headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip);
2779
+ if (FSE_isError(headerSize)) return ERROR(GENERIC);
2780
+ if (MLlog > MLFSELog) return ERROR(corruption_detected);
2781
+ ip += headerSize;
2782
+ FSE_buildDTable(DTableML, norm, max, MLlog);
2783
+ } } }
3254
2784
 
3255
2785
  return ip-istart;
3256
2786
  }
@@ -3285,21 +2815,18 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3285
2815
  /* Literal length */
3286
2816
  litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream));
3287
2817
  prevOffset = litLength ? seq->offset : seqState->prevOffset;
3288
- if (litLength == MaxLL)
3289
- {
3290
- U32 add = *dumps++;
2818
+ if (litLength == MaxLL) {
2819
+ const U32 add = dumps<de ? *dumps++ : 0;
3291
2820
  if (add < 255) litLength += add;
3292
- else
3293
- {
3294
- litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
2821
+ else if (dumps + 3 <= de) {
2822
+ litLength = MEM_readLE24(dumps);
3295
2823
  dumps += 3;
3296
2824
  }
3297
- if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
2825
+ if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3298
2826
  }
3299
2827
 
3300
2828
  /* Offset */
3301
- {
3302
- static const U32 offsetPrefix[MaxOff+1] = {
2829
+ { static const U32 offsetPrefix[MaxOff+1] = {
3303
2830
  1 /*fake*/, 1, 2, 4, 8, 16, 32, 64, 128, 256,
3304
2831
  512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144,
3305
2832
  524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, /*fake*/ 1, 1, 1, 1, 1 };
@@ -3316,16 +2843,14 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3316
2843
 
3317
2844
  /* MatchLength */
3318
2845
  matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
3319
- if (matchLength == MaxML)
3320
- {
3321
- U32 add = *dumps++;
2846
+ if (matchLength == MaxML) {
2847
+ const U32 add = dumps<de ? *dumps++ : 0;
3322
2848
  if (add < 255) matchLength += add;
3323
- else
3324
- {
3325
- matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
2849
+ else if (dumps + 3 <= de){
2850
+ matchLength = MEM_readLE24(dumps);
3326
2851
  dumps += 3;
3327
2852
  }
3328
- if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
2853
+ if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3329
2854
  }
3330
2855
  matchLength += MINMATCH;
3331
2856
 
@@ -3339,11 +2864,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3339
2864
 
3340
2865
  static size_t ZSTD_execSequence(BYTE* op,
3341
2866
  BYTE* const oend, seq_t sequence,
3342
- const BYTE** litPtr, const BYTE* const litLimit_8,
2867
+ const BYTE** litPtr, const BYTE* const litLimit,
3343
2868
  const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
3344
2869
  {
3345
2870
  static const int dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
3346
- static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* substracted */
2871
+ static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */
3347
2872
  BYTE* const oLitEnd = op + sequence.litLength;
3348
2873
  const size_t sequenceLength = sequence.litLength + sequence.matchLength;
3349
2874
  BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
@@ -3354,7 +2879,7 @@ static size_t ZSTD_execSequence(BYTE* op,
3354
2879
  /* check */
3355
2880
  if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */
3356
2881
  if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
3357
- if (litEnd > litLimit_8) return ERROR(corruption_detected); /* risk read beyond lit buffer */
2882
+ if (litEnd > litLimit) return ERROR(corruption_detected); /* risk read beyond lit buffer */
3358
2883
 
3359
2884
  /* copy Literals */
3360
2885
  ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
@@ -3380,12 +2905,16 @@ static size_t ZSTD_execSequence(BYTE* op,
3380
2905
  op = oLitEnd + length1;
3381
2906
  sequence.matchLength -= length1;
3382
2907
  match = base;
2908
+ if (op > oend_8 || sequence.matchLength < MINMATCH) {
2909
+ while (op < oMatchEnd) *op++ = *match++;
2910
+ return sequenceLength;
2911
+ }
3383
2912
  }
3384
2913
  }
2914
+ /* Requirement: op <= oend_8 */
3385
2915
 
3386
2916
  /* match within prefix */
3387
- if (sequence.offset < 8)
3388
- {
2917
+ if (sequence.offset < 8) {
3389
2918
  /* close range match, overlap */
3390
2919
  const int sub2 = dec64table[sequence.offset];
3391
2920
  op[0] = match[0];
@@ -3395,14 +2924,12 @@ static size_t ZSTD_execSequence(BYTE* op,
3395
2924
  match += dec32table[sequence.offset];
3396
2925
  ZSTD_copy4(op+4, match);
3397
2926
  match -= sub2;
3398
- }
3399
- else
3400
- {
2927
+ } else {
3401
2928
  ZSTD_copy8(op, match);
3402
2929
  }
3403
2930
  op += 8; match += 8;
3404
2931
 
3405
- if (oMatchEnd > oend-12)
2932
+ if (oMatchEnd > oend-(16-MINMATCH))
3406
2933
  {
3407
2934
  if (op < oend_8)
3408
2935
  {
@@ -3414,7 +2941,7 @@ static size_t ZSTD_execSequence(BYTE* op,
3414
2941
  }
3415
2942
  else
3416
2943
  {
3417
- ZSTD_wildcopy(op, match, sequence.matchLength-8); /* works even if matchLength < 8 */
2944
+ ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8, but must be signed */
3418
2945
  }
3419
2946
  return sequenceLength;
3420
2947
  }
@@ -3432,7 +2959,6 @@ static size_t ZSTD_decompressSequences(
3432
2959
  BYTE* const oend = ostart + maxDstSize;
3433
2960
  size_t errorCode, dumpsLength;
3434
2961
  const BYTE* litPtr = dctx->litPtr;
3435
- const BYTE* const litLimit_8 = litPtr + dctx->litBufSize - 8;
3436
2962
  const BYTE* const litEnd = litPtr + dctx->litSize;
3437
2963
  int nbSeq;
3438
2964
  const BYTE* dumps;
@@ -3471,7 +2997,7 @@ static size_t ZSTD_decompressSequences(
3471
2997
  size_t oneSeqSize;
3472
2998
  nbSeq--;
3473
2999
  ZSTD_decodeSequence(&sequence, &seqState);
3474
- oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litLimit_8, base, vBase, dictEnd);
3000
+ oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, base, vBase, dictEnd);
3475
3001
  if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
3476
3002
  op += oneSeqSize;
3477
3003
  }
@@ -3484,8 +3010,10 @@ static size_t ZSTD_decompressSequences(
3484
3010
  size_t lastLLSize = litEnd - litPtr;
3485
3011
  if (litPtr > litEnd) return ERROR(corruption_detected);
3486
3012
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3487
- if (op != litPtr) memcpy(op, litPtr, lastLLSize);
3488
- op += lastLLSize;
3013
+ if (lastLLSize > 0) {
3014
+ if (op != litPtr) memcpy(op, litPtr, lastLLSize);
3015
+ op += lastLLSize;
3016
+ }
3489
3017
  }
3490
3018
  }
3491
3019
 
@@ -3511,9 +3039,12 @@ static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,
3511
3039
  {
3512
3040
  /* blockType == blockCompressed */
3513
3041
  const BYTE* ip = (const BYTE*)src;
3042
+ size_t litCSize;
3043
+
3044
+ if (srcSize > BLOCKSIZE) return ERROR(corruption_detected);
3514
3045
 
3515
3046
  /* Decode literals sub-block */
3516
- size_t litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
3047
+ litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
3517
3048
  if (ZSTD_isError(litCSize)) return litCSize;
3518
3049
  ip += litCSize;
3519
3050
  srcSize -= litCSize;
@@ -3601,6 +3132,58 @@ static size_t ZSTD_decompress_usingDict(ZSTD_DCtx* ctx,
3601
3132
  return op-ostart;
3602
3133
  }
3603
3134
 
3135
+ /* ZSTD_errorFrameSizeInfoLegacy() :
3136
+ assumes `cSize` and `dBound` are _not_ NULL */
3137
+ static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)
3138
+ {
3139
+ *cSize = ret;
3140
+ *dBound = ZSTD_CONTENTSIZE_ERROR;
3141
+ }
3142
+
3143
+ void ZSTDv04_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)
3144
+ {
3145
+ const BYTE* ip = (const BYTE*)src;
3146
+ size_t remainingSize = srcSize;
3147
+ size_t nbBlocks = 0;
3148
+ blockProperties_t blockProperties;
3149
+
3150
+ /* Frame Header */
3151
+ if (srcSize < ZSTD_frameHeaderSize_min) {
3152
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3153
+ return;
3154
+ }
3155
+ if (MEM_readLE32(src) != ZSTD_MAGICNUMBER) {
3156
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));
3157
+ return;
3158
+ }
3159
+ ip += ZSTD_frameHeaderSize_min; remainingSize -= ZSTD_frameHeaderSize_min;
3160
+
3161
+ /* Loop on each block */
3162
+ while (1)
3163
+ {
3164
+ size_t cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
3165
+ if (ZSTD_isError(cBlockSize)) {
3166
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize);
3167
+ return;
3168
+ }
3169
+
3170
+ ip += ZSTD_blockHeaderSize;
3171
+ remainingSize -= ZSTD_blockHeaderSize;
3172
+ if (cBlockSize > remainingSize) {
3173
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3174
+ return;
3175
+ }
3176
+
3177
+ if (cBlockSize == 0) break; /* bt_end */
3178
+
3179
+ ip += cBlockSize;
3180
+ remainingSize -= cBlockSize;
3181
+ nbBlocks++;
3182
+ }
3183
+
3184
+ *cSize = ip - (const BYTE*)src;
3185
+ *dBound = nbBlocks * BLOCKSIZE;
3186
+ }
3604
3187
 
3605
3188
  /* ******************************
3606
3189
  * Streaming Decompression API
@@ -3752,7 +3335,7 @@ static void ZSTD_decompress_insertDictionary(ZSTD_DCtx* ctx, const void* dict, s
3752
3335
  * The function will report how many bytes were read or written by modifying *srcSizePtr and *maxDstSizePtr.
3753
3336
  * Note that it may not consume the entire input, in which case it's up to the caller to call again the function with remaining input.
3754
3337
  * The content of dst will be overwritten (up to *maxDstSizePtr) at each function call, so save its content if it matters or change dst .
3755
- * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
3338
+ * return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
3756
3339
  * or 0 when a frame is completely decoded
3757
3340
  * or an error code, which can be tested using ZBUFF_isError().
3758
3341
  *
@@ -3828,7 +3411,9 @@ static size_t ZBUFF_decompressWithDictionary(ZBUFF_DCtx* zbc, const void* src, s
3828
3411
  static size_t ZBUFF_limitCopy(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3829
3412
  {
3830
3413
  size_t length = MIN(maxDstSize, srcSize);
3831
- memcpy(dst, src, length);
3414
+ if (length > 0) {
3415
+ memcpy(dst, src, length);
3416
+ }
3832
3417
  return length;
3833
3418
  }
3834
3419
 
@@ -3844,12 +3429,14 @@ static size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDs
3844
3429
  char* const oend = ostart + *maxDstSizePtr;
3845
3430
  U32 notDone = 1;
3846
3431
 
3432
+ DEBUGLOG(5, "ZBUFF_decompressContinue");
3847
3433
  while (notDone)
3848
3434
  {
3849
3435
  switch(zbc->stage)
3850
3436
  {
3851
3437
 
3852
3438
  case ZBUFFds_init :
3439
+ DEBUGLOG(5, "ZBUFF_decompressContinue: stage==ZBUFFds_init => ERROR(init_missing)");
3853
3440
  return ERROR(init_missing);
3854
3441
 
3855
3442
  case ZBUFFds_readHeader :
@@ -3911,7 +3498,7 @@ static size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDs
3911
3498
  break;
3912
3499
  }
3913
3500
  zbc->stage = ZBUFFds_read;
3914
-
3501
+ /* fall-through */
3915
3502
  case ZBUFFds_read:
3916
3503
  {
3917
3504
  size_t neededInSize = ZSTD_nextSrcSizeToDecompress(zbc->zc);
@@ -3937,7 +3524,7 @@ static size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDs
3937
3524
  if (ip==iend) { notDone = 0; break; } /* no more input */
3938
3525
  zbc->stage = ZBUFFds_load;
3939
3526
  }
3940
-
3527
+ /* fall-through */
3941
3528
  case ZBUFFds_load:
3942
3529
  {
3943
3530
  size_t neededInSize = ZSTD_nextSrcSizeToDecompress(zbc->zc);
@@ -3957,9 +3544,10 @@ static size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDs
3957
3544
  if (!decodedSize) { zbc->stage = ZBUFFds_read; break; } /* this was just a header */
3958
3545
  zbc->outEnd = zbc->outStart + decodedSize;
3959
3546
  zbc->stage = ZBUFFds_flush;
3960
- // break; /* ZBUFFds_flush follows */
3547
+ /* ZBUFFds_flush follows */
3961
3548
  }
3962
3549
  }
3550
+ /* fall-through */
3963
3551
  case ZBUFFds_flush:
3964
3552
  {
3965
3553
  size_t toFlushSize = zbc->outEnd - zbc->outStart;
@@ -4024,11 +3612,10 @@ size_t ZSTDv04_decompress(void* dst, size_t maxDstSize, const void* src, size_t
4024
3612
  return regenSize;
4025
3613
  #else
4026
3614
  ZSTD_DCtx dctx;
4027
- return ZSTD_decompressDCtx(&dctx, dst, maxDstSize, src, srcSize);
3615
+ return ZSTDv04_decompressDCtx(&dctx, dst, maxDstSize, src, srcSize);
4028
3616
  #endif
4029
3617
  }
4030
3618
 
4031
-
4032
3619
  size_t ZSTDv04_resetDCtx(ZSTDv04_Dctx* dctx) { return ZSTD_resetDCtx(dctx); }
4033
3620
 
4034
3621
  size_t ZSTDv04_nextSrcSizeToDecompress(ZSTDv04_Dctx* dctx)
@@ -4044,7 +3631,7 @@ size_t ZSTDv04_decompressContinue(ZSTDv04_Dctx* dctx, void* dst, size_t maxDstSi
4044
3631
 
4045
3632
 
4046
3633
  ZBUFFv04_DCtx* ZBUFFv04_createDCtx(void) { return ZBUFF_createDCtx(); }
4047
- size_t ZBUFFv04_freeDCtx(ZBUFFv04_DCtx* dctx) { return ZBUFF_freeDCtx(dctx); }
3634
+ size_t ZBUFFv04_freeDCtx(ZBUFFv04_DCtx* dctx) { return ZBUFF_freeDCtx(dctx); }
4048
3635
 
4049
3636
  size_t ZBUFFv04_decompressInit(ZBUFFv04_DCtx* dctx) { return ZBUFF_decompressInit(dctx); }
4050
3637
  size_t ZBUFFv04_decompressWithDictionary(ZBUFFv04_DCtx* dctx, const void* src, size_t srcSize)
@@ -4052,5 +3639,9 @@ size_t ZBUFFv04_decompressWithDictionary(ZBUFFv04_DCtx* dctx, const void* src, s
4052
3639
 
4053
3640
  size_t ZBUFFv04_decompressContinue(ZBUFFv04_DCtx* dctx, void* dst, size_t* maxDstSizePtr, const void* src, size_t* srcSizePtr)
4054
3641
  {
3642
+ DEBUGLOG(5, "ZBUFFv04_decompressContinue");
4055
3643
  return ZBUFF_decompressContinue(dctx, dst, maxDstSizePtr, src, srcSizePtr);
4056
3644
  }
3645
+
3646
+ ZSTD_DCtx* ZSTDv04_createDCtx(void) { return ZSTD_createDCtx(); }
3647
+ size_t ZSTDv04_freeDCtx(ZSTD_DCtx* dctx) { return ZSTD_freeDCtx(dctx); }