extzstd 0.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. checksums.yaml +5 -5
  2. data/HISTORY.ja.md +39 -0
  3. data/README.md +38 -56
  4. data/contrib/zstd/CHANGELOG +613 -0
  5. data/contrib/zstd/CODE_OF_CONDUCT.md +5 -0
  6. data/contrib/zstd/CONTRIBUTING.md +406 -0
  7. data/contrib/zstd/COPYING +339 -0
  8. data/contrib/zstd/Makefile +420 -0
  9. data/contrib/zstd/README.md +179 -41
  10. data/contrib/zstd/TESTING.md +44 -0
  11. data/contrib/zstd/appveyor.yml +292 -0
  12. data/contrib/zstd/lib/BUCK +234 -0
  13. data/contrib/zstd/lib/Makefile +451 -0
  14. data/contrib/zstd/lib/README.md +207 -0
  15. data/contrib/zstd/{common → lib/common}/bitstream.h +187 -138
  16. data/contrib/zstd/lib/common/compiler.h +288 -0
  17. data/contrib/zstd/lib/common/cpu.h +213 -0
  18. data/contrib/zstd/lib/common/debug.c +24 -0
  19. data/contrib/zstd/lib/common/debug.h +107 -0
  20. data/contrib/zstd/lib/common/entropy_common.c +362 -0
  21. data/contrib/zstd/{common → lib/common}/error_private.c +25 -12
  22. data/contrib/zstd/{common → lib/common}/error_private.h +14 -10
  23. data/contrib/zstd/{common → lib/common}/fse.h +173 -92
  24. data/contrib/zstd/{common → lib/common}/fse_decompress.c +149 -85
  25. data/contrib/zstd/lib/common/huf.h +361 -0
  26. data/contrib/zstd/{common → lib/common}/mem.h +115 -59
  27. data/contrib/zstd/lib/common/pool.c +350 -0
  28. data/contrib/zstd/lib/common/pool.h +84 -0
  29. data/contrib/zstd/lib/common/threading.c +122 -0
  30. data/contrib/zstd/lib/common/threading.h +155 -0
  31. data/contrib/zstd/{common → lib/common}/xxhash.c +55 -96
  32. data/contrib/zstd/{common → lib/common}/xxhash.h +23 -47
  33. data/contrib/zstd/lib/common/zstd_common.c +83 -0
  34. data/contrib/zstd/lib/common/zstd_deps.h +111 -0
  35. data/contrib/zstd/lib/common/zstd_errors.h +95 -0
  36. data/contrib/zstd/lib/common/zstd_internal.h +478 -0
  37. data/contrib/zstd/{compress → lib/compress}/fse_compress.c +214 -319
  38. data/contrib/zstd/lib/compress/hist.c +181 -0
  39. data/contrib/zstd/lib/compress/hist.h +75 -0
  40. data/contrib/zstd/lib/compress/huf_compress.c +913 -0
  41. data/contrib/zstd/lib/compress/zstd_compress.c +5208 -0
  42. data/contrib/zstd/lib/compress/zstd_compress_internal.h +1203 -0
  43. data/contrib/zstd/lib/compress/zstd_compress_literals.c +158 -0
  44. data/contrib/zstd/lib/compress/zstd_compress_literals.h +29 -0
  45. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +433 -0
  46. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +54 -0
  47. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +849 -0
  48. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +32 -0
  49. data/contrib/zstd/lib/compress/zstd_cwksp.h +561 -0
  50. data/contrib/zstd/lib/compress/zstd_double_fast.c +521 -0
  51. data/contrib/zstd/lib/compress/zstd_double_fast.h +38 -0
  52. data/contrib/zstd/lib/compress/zstd_fast.c +496 -0
  53. data/contrib/zstd/lib/compress/zstd_fast.h +37 -0
  54. data/contrib/zstd/lib/compress/zstd_lazy.c +1412 -0
  55. data/contrib/zstd/lib/compress/zstd_lazy.h +87 -0
  56. data/contrib/zstd/lib/compress/zstd_ldm.c +660 -0
  57. data/contrib/zstd/lib/compress/zstd_ldm.h +116 -0
  58. data/contrib/zstd/lib/compress/zstd_opt.c +1345 -0
  59. data/contrib/zstd/lib/compress/zstd_opt.h +56 -0
  60. data/contrib/zstd/lib/compress/zstdmt_compress.c +1811 -0
  61. data/contrib/zstd/lib/compress/zstdmt_compress.h +110 -0
  62. data/contrib/zstd/lib/decompress/huf_decompress.c +1350 -0
  63. data/contrib/zstd/lib/decompress/zstd_ddict.c +244 -0
  64. data/contrib/zstd/lib/decompress/zstd_ddict.h +44 -0
  65. data/contrib/zstd/lib/decompress/zstd_decompress.c +1930 -0
  66. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1540 -0
  67. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +62 -0
  68. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +190 -0
  69. data/contrib/zstd/{common → lib/deprecated}/zbuff.h +68 -45
  70. data/contrib/zstd/lib/deprecated/zbuff_common.c +26 -0
  71. data/contrib/zstd/lib/deprecated/zbuff_compress.c +147 -0
  72. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +75 -0
  73. data/contrib/zstd/lib/dictBuilder/cover.c +1245 -0
  74. data/contrib/zstd/lib/dictBuilder/cover.h +157 -0
  75. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.c +3 -3
  76. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.h +0 -0
  77. data/contrib/zstd/lib/dictBuilder/fastcover.c +758 -0
  78. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/zdict.c +318 -194
  79. data/contrib/zstd/lib/dictBuilder/zdict.h +305 -0
  80. data/contrib/zstd/{legacy → lib/legacy}/zstd_legacy.h +171 -15
  81. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.c +191 -124
  82. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.h +19 -5
  83. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.c +125 -125
  84. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.h +19 -5
  85. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.c +125 -124
  86. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.h +20 -6
  87. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.c +151 -299
  88. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.h +19 -5
  89. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.c +237 -243
  90. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.h +19 -6
  91. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.c +130 -143
  92. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.h +18 -5
  93. data/contrib/zstd/{legacy → lib/legacy}/zstd_v07.c +158 -157
  94. data/contrib/zstd/{legacy → lib/legacy}/zstd_v07.h +19 -5
  95. data/contrib/zstd/lib/libzstd.pc.in +15 -0
  96. data/contrib/zstd/lib/zstd.h +2391 -0
  97. data/ext/depend +2 -0
  98. data/ext/extconf.rb +15 -6
  99. data/ext/extzstd.c +76 -145
  100. data/ext/extzstd.h +80 -31
  101. data/ext/extzstd_stream.c +417 -142
  102. data/ext/libzstd_conf.h +8 -0
  103. data/ext/zstd_common.c +10 -7
  104. data/ext/zstd_compress.c +14 -5
  105. data/ext/zstd_decompress.c +5 -4
  106. data/ext/zstd_dictbuilder.c +9 -4
  107. data/ext/zstd_dictbuilder_fastcover.c +3 -0
  108. data/ext/zstd_legacy_v01.c +3 -1
  109. data/ext/zstd_legacy_v02.c +3 -1
  110. data/ext/zstd_legacy_v03.c +3 -1
  111. data/ext/zstd_legacy_v04.c +3 -1
  112. data/ext/zstd_legacy_v05.c +3 -1
  113. data/ext/zstd_legacy_v06.c +3 -1
  114. data/ext/zstd_legacy_v07.c +3 -1
  115. data/gemstub.rb +10 -24
  116. data/lib/extzstd.rb +64 -179
  117. data/lib/extzstd/version.rb +6 -1
  118. data/test/test_basic.rb +9 -6
  119. metadata +113 -57
  120. data/HISTORY.ja +0 -5
  121. data/contrib/zstd/common/entropy_common.c +0 -225
  122. data/contrib/zstd/common/huf.h +0 -228
  123. data/contrib/zstd/common/zstd_common.c +0 -83
  124. data/contrib/zstd/common/zstd_errors.h +0 -60
  125. data/contrib/zstd/common/zstd_internal.h +0 -267
  126. data/contrib/zstd/compress/huf_compress.c +0 -533
  127. data/contrib/zstd/compress/zbuff_compress.c +0 -319
  128. data/contrib/zstd/compress/zstd_compress.c +0 -3264
  129. data/contrib/zstd/compress/zstd_opt.h +0 -900
  130. data/contrib/zstd/decompress/huf_decompress.c +0 -883
  131. data/contrib/zstd/decompress/zbuff_decompress.c +0 -252
  132. data/contrib/zstd/decompress/zstd_decompress.c +0 -1842
  133. data/contrib/zstd/dictBuilder/zdict.h +0 -111
  134. data/contrib/zstd/zstd.h +0 -640
@@ -1,10 +1,11 @@
1
- /**
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
1
+ /*
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
- * This source code is licensed under the BSD-style license found in the
6
- * LICENSE file in the root directory of this source tree. An additional grant
7
- * of patent rights can be found in the PATENTS file in the same directory.
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.
8
9
  */
9
10
 
10
11
  #ifndef ZSTD_V03_H_298734209782
@@ -34,7 +35,20 @@ ZSTDv03_decompress() : decompress ZSTD frames compliant with v0.3.x format
34
35
  size_t ZSTDv03_decompress( void* dst, size_t maxOriginalSize,
35
36
  const void* src, size_t compressedSize);
36
37
 
37
- /**
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
+ /**
38
52
  ZSTDv03_isError() : tells if the result of ZSTDv03_decompress() is an error
39
53
  */
40
54
  unsigned ZSTDv03_isError(size_t code);
@@ -1,52 +1,27 @@
1
- /**
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
1
+ /*
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
- * This source code is licensed under the BSD-style license found in the
6
- * LICENSE file in the root directory of this source tree. An additional grant
7
- * of patent rights can be found in the PATENTS file in the same directory.
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.
8
9
  */
9
10
 
10
11
 
11
- /*- Dependencies -*/
12
+ /******************************************
13
+ * Includes
14
+ ******************************************/
15
+ #include <stddef.h> /* size_t, ptrdiff_t */
16
+ #include <string.h> /* memcpy */
17
+
12
18
  #include "zstd_v04.h"
13
- #include "error_private.h"
19
+ #include "../common/error_private.h"
14
20
 
15
21
 
16
22
  /* ******************************************************************
17
- mem.h
18
- low-level memory access routines
19
- Copyright (C) 2013-2015, Yann Collet.
20
-
21
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
22
-
23
- Redistribution and use in source and binary forms, with or without
24
- modification, are permitted provided that the following conditions are
25
- met:
26
-
27
- * Redistributions of source code must retain the above copyright
28
- notice, this list of conditions and the following disclaimer.
29
- * Redistributions in binary form must reproduce the above
30
- copyright notice, this list of conditions and the following disclaimer
31
- in the documentation and/or other materials provided with the
32
- distribution.
33
-
34
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
-
46
- You can contact the author at :
47
- - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
48
- - Public forum : https://groups.google.com/forum/#!forum/lz4c
49
- ****************************************************************** */
23
+ * mem.h
24
+ *******************************************************************/
50
25
  #ifndef MEM_H_MODULE
51
26
  #define MEM_H_MODULE
52
27
 
@@ -54,12 +29,6 @@
54
29
  extern "C" {
55
30
  #endif
56
31
 
57
- /******************************************
58
- * Includes
59
- ******************************************/
60
- #include <stddef.h> /* size_t, ptrdiff_t */
61
- #include <string.h> /* memcpy */
62
-
63
32
 
64
33
  /******************************************
65
34
  * Compiler-specific
@@ -83,7 +52,11 @@ extern "C" {
83
52
  * Basic Types
84
53
  *****************************************************************/
85
54
  #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
86
- # include <stdint.h>
55
+ # if defined(_AIX)
56
+ # include <inttypes.h>
57
+ # else
58
+ # include <stdint.h> /* intptr_t */
59
+ # endif
87
60
  typedef uint8_t BYTE;
88
61
  typedef uint16_t U16;
89
62
  typedef int16_t S16;
@@ -102,6 +75,15 @@ extern "C" {
102
75
  #endif
103
76
 
104
77
 
78
+ /*-*************************************
79
+ * Debug
80
+ ***************************************/
81
+ #include "../common/debug.h"
82
+ #ifndef assert
83
+ # define assert(condition) ((void)0)
84
+ #endif
85
+
86
+
105
87
  /****************************************************************
106
88
  * Memory I/O
107
89
  *****************************************************************/
@@ -183,7 +165,7 @@ MEM_STATIC void MEM_write16(void* memPtr, U16 value)
183
165
  memcpy(memPtr, &value, sizeof(value));
184
166
  }
185
167
 
186
- #endif // MEM_FORCE_MEMORY_ACCESS
168
+ #endif /* MEM_FORCE_MEMORY_ACCESS */
187
169
 
188
170
 
189
171
  MEM_STATIC U16 MEM_readLE16(const void* memPtr)
@@ -211,6 +193,11 @@ MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
211
193
  }
212
194
  }
213
195
 
196
+ MEM_STATIC U32 MEM_readLE24(const void* memPtr)
197
+ {
198
+ return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
199
+ }
200
+
214
201
  MEM_STATIC U32 MEM_readLE32(const void* memPtr)
215
202
  {
216
203
  if (MEM_isLittleEndian())
@@ -254,61 +241,15 @@ MEM_STATIC size_t MEM_readLEST(const void* memPtr)
254
241
  /*
255
242
  zstd - standard compression library
256
243
  Header File for static linking only
257
- Copyright (C) 2014-2015, Yann Collet.
258
-
259
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
260
-
261
- Redistribution and use in source and binary forms, with or without
262
- modification, are permitted provided that the following conditions are
263
- met:
264
- * Redistributions of source code must retain the above copyright
265
- notice, this list of conditions and the following disclaimer.
266
- * Redistributions in binary form must reproduce the above
267
- copyright notice, this list of conditions and the following disclaimer
268
- in the documentation and/or other materials provided with the
269
- distribution.
270
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
271
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
272
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
273
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
274
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
275
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
276
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
277
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
278
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
279
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
280
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281
-
282
- You can contact the author at :
283
- - zstd source repository : https://github.com/Cyan4973/zstd
284
- - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
285
244
  */
286
245
  #ifndef ZSTD_STATIC_H
287
246
  #define ZSTD_STATIC_H
288
247
 
289
- /* The objects defined into this file shall be considered experimental.
290
- * They are not considered stable, as their prototype may change in the future.
291
- * You can use them for tests, provide feedback, or if you can endure risks of future changes.
292
- */
293
-
294
- #if defined (__cplusplus)
295
- extern "C" {
296
- #endif
297
248
 
298
249
  /* *************************************
299
250
  * Types
300
251
  ***************************************/
301
- #define ZSTD_WINDOWLOG_MAX 26
302
- #define ZSTD_WINDOWLOG_MIN 18
303
252
  #define ZSTD_WINDOWLOG_ABSOLUTEMIN 11
304
- #define ZSTD_CONTENTLOG_MAX (ZSTD_WINDOWLOG_MAX+1)
305
- #define ZSTD_CONTENTLOG_MIN 4
306
- #define ZSTD_HASHLOG_MAX 28
307
- #define ZSTD_HASHLOG_MIN 4
308
- #define ZSTD_SEARCHLOG_MAX (ZSTD_CONTENTLOG_MAX-1)
309
- #define ZSTD_SEARCHLOG_MIN 1
310
- #define ZSTD_SEARCHLENGTH_MAX 7
311
- #define ZSTD_SEARCHLENGTH_MIN 4
312
253
 
313
254
  /** from faster to stronger */
314
255
  typedef enum { ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2 } ZSTD_strategy;
@@ -380,9 +321,6 @@ static size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t maxDstS
380
321
  */
381
322
 
382
323
 
383
- #if defined (__cplusplus)
384
- }
385
- #endif
386
324
 
387
325
 
388
326
  #endif /* ZSTD_STATIC_H */
@@ -391,42 +329,10 @@ static size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t maxDstS
391
329
  /*
392
330
  zstd_internal - common functions to include
393
331
  Header File for include
394
- Copyright (C) 2014-2015, Yann Collet.
395
-
396
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
397
-
398
- Redistribution and use in source and binary forms, with or without
399
- modification, are permitted provided that the following conditions are
400
- met:
401
- * Redistributions of source code must retain the above copyright
402
- notice, this list of conditions and the following disclaimer.
403
- * Redistributions in binary form must reproduce the above
404
- copyright notice, this list of conditions and the following disclaimer
405
- in the documentation and/or other materials provided with the
406
- distribution.
407
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
408
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
409
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
410
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
411
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
412
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
413
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
414
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
415
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
416
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
417
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
418
-
419
- You can contact the author at :
420
- - zstd source repository : https://github.com/Cyan4973/zstd
421
- - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
422
332
  */
423
333
  #ifndef ZSTD_CCOMMON_H_MODULE
424
334
  #define ZSTD_CCOMMON_H_MODULE
425
335
 
426
- #if defined (__cplusplus)
427
- extern "C" {
428
- #endif
429
-
430
336
  /* *************************************
431
337
  * Common macros
432
338
  ***************************************/
@@ -476,6 +382,8 @@ static const size_t ZSTD_frameHeaderSize_min = 5;
476
382
  #define MIN_SEQUENCES_SIZE (2 /*seqNb*/ + 2 /*dumps*/ + 3 /*seqTables*/ + 1 /*bitStream*/)
477
383
  #define MIN_CBLOCK_SIZE (3 /*litCSize*/ + MIN_SEQUENCES_SIZE)
478
384
 
385
+ #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
386
+
479
387
  typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
480
388
 
481
389
 
@@ -487,7 +395,7 @@ static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
487
395
  #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
488
396
 
489
397
  /*! ZSTD_wildcopy : custom version of memcpy(), can copy up to 7-8 bytes too many */
490
- static void ZSTD_wildcopy(void* dst, const void* src, size_t length)
398
+ static void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
491
399
  {
492
400
  const BYTE* ip = (const BYTE*)src;
493
401
  BYTE* op = (BYTE*)dst;
@@ -498,44 +406,10 @@ static void ZSTD_wildcopy(void* dst, const void* src, size_t length)
498
406
  }
499
407
 
500
408
 
501
- #if defined (__cplusplus)
502
- }
503
- #endif
504
-
505
409
 
506
410
  /* ******************************************************************
507
411
  FSE : Finite State Entropy coder
508
412
  header file
509
- Copyright (C) 2013-2015, Yann Collet.
510
-
511
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
512
-
513
- Redistribution and use in source and binary forms, with or without
514
- modification, are permitted provided that the following conditions are
515
- met:
516
-
517
- * Redistributions of source code must retain the above copyright
518
- notice, this list of conditions and the following disclaimer.
519
- * Redistributions in binary form must reproduce the above
520
- copyright notice, this list of conditions and the following disclaimer
521
- in the documentation and/or other materials provided with the
522
- distribution.
523
-
524
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
525
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
526
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
527
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
528
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
529
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
530
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
531
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
532
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
533
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
534
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
535
-
536
- You can contact the author at :
537
- - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
538
- - Public forum : https://groups.google.com/forum/#!forum/lz4c
539
413
  ****************************************************************** */
540
414
  #ifndef FSE_H
541
415
  #define FSE_H
@@ -737,16 +611,6 @@ MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
737
611
  MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
738
612
 
739
613
 
740
- /*
741
- * Start by invoking BIT_initDStream().
742
- * A chunk of the bitStream is then stored into a local register.
743
- * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
744
- * You can then retrieve bitFields stored into the local register, **in reverse order**.
745
- * Local register is manually filled from memory by the BIT_reloadDStream() method.
746
- * A reload guarantee a minimum of ((8*sizeof(size_t))-7) bits when its result is BIT_DStream_unfinished.
747
- * Otherwise, it can be less than that, so proceed accordingly.
748
- * Checking if DStream has reached its end can be performed with BIT_endOfDStream()
749
- */
750
614
 
751
615
 
752
616
  /******************************************
@@ -760,14 +624,14 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
760
624
  /****************************************************************
761
625
  * Helper functions
762
626
  ****************************************************************/
763
- MEM_STATIC unsigned BIT_highbit32 (register U32 val)
627
+ MEM_STATIC unsigned BIT_highbit32 (U32 val)
764
628
  {
765
629
  # if defined(_MSC_VER) /* Visual */
766
630
  unsigned long r=0;
767
631
  _BitScanReverse ( &r, val );
768
632
  return (unsigned) r;
769
633
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
770
- return 31 - __builtin_clz (val);
634
+ return __builtin_clz (val) ^ 31;
771
635
  # else /* Software version */
772
636
  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 };
773
637
  U32 v = val;
@@ -816,13 +680,13 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
816
680
  bitD->bitContainer = *(const BYTE*)(bitD->start);
817
681
  switch(srcSize)
818
682
  {
819
- case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);
820
- case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);
821
- case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);
822
- case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24;
823
- case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16;
824
- case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8;
825
- default:;
683
+ case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);/* fall-through */
684
+ case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);/* fall-through */
685
+ case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);/* fall-through */
686
+ case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24; /* fall-through */
687
+ case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16; /* fall-through */
688
+ case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8; /* fall-through */
689
+ default: break;
826
690
  }
827
691
  contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
828
692
  if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
@@ -833,13 +697,6 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
833
697
  return srcSize;
834
698
  }
835
699
 
836
- /*!BIT_lookBits
837
- * Provides next n bits from local register
838
- * local register is not modified (bits are still present for next read/look)
839
- * On 32-bits, maxNbBits==25
840
- * On 64-bits, maxNbBits==57
841
- * @return : value extracted
842
- */
843
700
  MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits)
844
701
  {
845
702
  const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
@@ -859,11 +716,6 @@ MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
859
716
  bitD->bitsConsumed += nbBits;
860
717
  }
861
718
 
862
- /*!BIT_readBits
863
- * Read next n bits from local register.
864
- * pay attention to not read more than nbBits contained into local register.
865
- * @return : extracted value.
866
- */
867
719
  MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
868
720
  {
869
721
  size_t value = BIT_lookBits(bitD, nbBits);
@@ -882,8 +734,8 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
882
734
 
883
735
  MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
884
736
  {
885
- if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
886
- return BIT_DStream_overflow;
737
+ if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
738
+ return BIT_DStream_overflow;
887
739
 
888
740
  if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer))
889
741
  {
@@ -1010,55 +862,6 @@ static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bi
1010
862
 
1011
863
  static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr);
1012
864
 
1013
- /*!
1014
- Let's now decompose FSE_decompress_usingDTable() into its unitary components.
1015
- You will decode FSE-encoded symbols from the bitStream,
1016
- and also any other bitFields you put in, **in reverse order**.
1017
-
1018
- You will need a few variables to track your bitStream. They are :
1019
-
1020
- BIT_DStream_t DStream; // Stream context
1021
- FSE_DState_t DState; // State context. Multiple ones are possible
1022
- FSE_DTable* DTablePtr; // Decoding table, provided by FSE_buildDTable()
1023
-
1024
- The first thing to do is to init the bitStream.
1025
- errorCode = BIT_initDStream(&DStream, srcBuffer, srcSize);
1026
-
1027
- You should then retrieve your initial state(s)
1028
- (in reverse flushing order if you have several ones) :
1029
- errorCode = FSE_initDState(&DState, &DStream, DTablePtr);
1030
-
1031
- You can then decode your data, symbol after symbol.
1032
- For information the maximum number of bits read by FSE_decodeSymbol() is 'tableLog'.
1033
- Keep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out).
1034
- unsigned char symbol = FSE_decodeSymbol(&DState, &DStream);
1035
-
1036
- You can retrieve any bitfield you eventually stored into the bitStream (in reverse order)
1037
- Note : maximum allowed nbBits is 25, for 32-bits compatibility
1038
- size_t bitField = BIT_readBits(&DStream, nbBits);
1039
-
1040
- All above operations only read from local register (which size depends on size_t).
1041
- Refueling the register from memory is manually performed by the reload method.
1042
- endSignal = FSE_reloadDStream(&DStream);
1043
-
1044
- BIT_reloadDStream() result tells if there is still some more data to read from DStream.
1045
- BIT_DStream_unfinished : there is still some data left into the DStream.
1046
- BIT_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled.
1047
- BIT_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed.
1048
- BIT_DStream_tooFar : Dstream went too far. Decompression result is corrupted.
1049
-
1050
- When reaching end of buffer (BIT_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop,
1051
- to properly detect the exact end of stream.
1052
- After each decoded symbol, check if DStream is fully consumed using this simple test :
1053
- BIT_reloadDStream(&DStream) >= BIT_DStream_completed
1054
-
1055
- When it's done, verify decompression is fully completed, by checking both DStream and the relevant states.
1056
- Checking if DStream has reached its end is performed by :
1057
- BIT_endOfDStream(&DStream);
1058
- Check also the states. There might be some symbols left there, if some high probability ones (>50%) are possible.
1059
- FSE_endOfDState(&DState);
1060
- */
1061
-
1062
865
 
1063
866
  /* *****************************************
1064
867
  * FSE unsafe API
@@ -1291,6 +1094,7 @@ static size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, un
1291
1094
  if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
1292
1095
 
1293
1096
  /* Init, lay down lowprob symbols */
1097
+ 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 */
1294
1098
  DTableH.tableLog = (U16)tableLog;
1295
1099
  for (s=0; s<=maxSymbolValue; s++)
1296
1100
  {
@@ -1451,8 +1255,8 @@ static size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsi
1451
1255
  else
1452
1256
  {
1453
1257
  bitCount -= (int)(8 * (iend - 4 - ip));
1454
- ip = iend - 4;
1455
- }
1258
+ ip = iend - 4;
1259
+ }
1456
1260
  bitStream = MEM_readLE32(ip) >> (bitCount & 31);
1457
1261
  }
1458
1262
  }
@@ -2706,7 +2510,6 @@ struct ZSTDv04_Dctx_s
2706
2510
  blockType_t bType;
2707
2511
  ZSTD_dStage stage;
2708
2512
  const BYTE* litPtr;
2709
- size_t litBufSize;
2710
2513
  size_t litSize;
2711
2514
  BYTE litBuffer[BLOCKSIZE + 8 /* margin for wildcopy */];
2712
2515
  BYTE headerBuffer[ZSTD_frameHeaderSize_max];
@@ -2777,7 +2580,7 @@ static size_t ZSTD_decodeFrameHeader_Part2(ZSTD_DCtx* zc, const void* src, size_
2777
2580
  size_t result;
2778
2581
  if (srcSize != zc->headerSize) return ERROR(srcSize_wrong);
2779
2582
  result = ZSTD_getFrameParams(&(zc->params), src, srcSize);
2780
- if ((MEM_32bits()) && (zc->params.windowLog > 25)) return ERROR(frameParameter_unsupportedBy32bits);
2583
+ if ((MEM_32bits()) && (zc->params.windowLog > 25)) return ERROR(frameParameter_unsupported);
2781
2584
  return result;
2782
2585
  }
2783
2586
 
@@ -2804,7 +2607,9 @@ static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockPropertie
2804
2607
  static size_t ZSTD_copyRawBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
2805
2608
  {
2806
2609
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
2807
- memcpy(dst, src, srcSize);
2610
+ if (srcSize > 0) {
2611
+ memcpy(dst, src, srcSize);
2612
+ }
2808
2613
  return srcSize;
2809
2614
  }
2810
2615
 
@@ -2847,8 +2652,8 @@ static size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
2847
2652
  size_t litSize = BLOCKSIZE;
2848
2653
  const size_t readSize = ZSTD_decompressLiterals(dctx->litBuffer, &litSize, src, srcSize);
2849
2654
  dctx->litPtr = dctx->litBuffer;
2850
- dctx->litBufSize = BLOCKSIZE+8;
2851
2655
  dctx->litSize = litSize;
2656
+ memset(dctx->litBuffer + dctx->litSize, 0, 8);
2852
2657
  return readSize; /* works if it's an error too */
2853
2658
  }
2854
2659
  case IS_RAW:
@@ -2856,25 +2661,24 @@ static size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx,
2856
2661
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2857
2662
  if (litSize > srcSize-11) /* risk of reading too far with wildcopy */
2858
2663
  {
2664
+ if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2859
2665
  if (litSize > srcSize-3) return ERROR(corruption_detected);
2860
2666
  memcpy(dctx->litBuffer, istart, litSize);
2861
2667
  dctx->litPtr = dctx->litBuffer;
2862
- dctx->litBufSize = BLOCKSIZE+8;
2863
2668
  dctx->litSize = litSize;
2669
+ memset(dctx->litBuffer + dctx->litSize, 0, 8);
2864
2670
  return litSize+3;
2865
2671
  }
2866
2672
  /* direct reference into compressed stream */
2867
2673
  dctx->litPtr = istart+3;
2868
- dctx->litBufSize = srcSize-3;
2869
2674
  dctx->litSize = litSize;
2870
2675
  return litSize+3; }
2871
2676
  case IS_RLE:
2872
2677
  {
2873
2678
  const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */
2874
2679
  if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2875
- memset(dctx->litBuffer, istart[3], litSize);
2680
+ memset(dctx->litBuffer, istart[3], litSize + 8);
2876
2681
  dctx->litPtr = dctx->litBuffer;
2877
- dctx->litBufSize = BLOCKSIZE+8;
2878
2682
  dctx->litSize = litSize;
2879
2683
  return 4;
2880
2684
  }
@@ -3015,21 +2819,18 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3015
2819
  /* Literal length */
3016
2820
  litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream));
3017
2821
  prevOffset = litLength ? seq->offset : seqState->prevOffset;
3018
- if (litLength == MaxLL)
3019
- {
3020
- U32 add = *dumps++;
2822
+ if (litLength == MaxLL) {
2823
+ const U32 add = dumps<de ? *dumps++ : 0;
3021
2824
  if (add < 255) litLength += add;
3022
- else
3023
- {
3024
- litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
2825
+ else if (dumps + 3 <= de) {
2826
+ litLength = MEM_readLE24(dumps);
3025
2827
  dumps += 3;
3026
2828
  }
3027
- if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
2829
+ if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3028
2830
  }
3029
2831
 
3030
2832
  /* Offset */
3031
- {
3032
- static const U32 offsetPrefix[MaxOff+1] = {
2833
+ { static const U32 offsetPrefix[MaxOff+1] = {
3033
2834
  1 /*fake*/, 1, 2, 4, 8, 16, 32, 64, 128, 256,
3034
2835
  512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144,
3035
2836
  524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, /*fake*/ 1, 1, 1, 1, 1 };
@@ -3046,16 +2847,14 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3046
2847
 
3047
2848
  /* MatchLength */
3048
2849
  matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
3049
- if (matchLength == MaxML)
3050
- {
3051
- U32 add = *dumps++;
2850
+ if (matchLength == MaxML) {
2851
+ const U32 add = dumps<de ? *dumps++ : 0;
3052
2852
  if (add < 255) matchLength += add;
3053
- else
3054
- {
3055
- matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
2853
+ else if (dumps + 3 <= de){
2854
+ matchLength = MEM_readLE24(dumps);
3056
2855
  dumps += 3;
3057
2856
  }
3058
- if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
2857
+ if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3059
2858
  }
3060
2859
  matchLength += MINMATCH;
3061
2860
 
@@ -3069,11 +2868,11 @@ static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)
3069
2868
 
3070
2869
  static size_t ZSTD_execSequence(BYTE* op,
3071
2870
  BYTE* const oend, seq_t sequence,
3072
- const BYTE** litPtr, const BYTE* const litLimit_8,
2871
+ const BYTE** litPtr, const BYTE* const litLimit,
3073
2872
  const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
3074
2873
  {
3075
2874
  static const int dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
3076
- static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* substracted */
2875
+ static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */
3077
2876
  BYTE* const oLitEnd = op + sequence.litLength;
3078
2877
  const size_t sequenceLength = sequence.litLength + sequence.matchLength;
3079
2878
  BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
@@ -3084,7 +2883,7 @@ static size_t ZSTD_execSequence(BYTE* op,
3084
2883
  /* check */
3085
2884
  if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */
3086
2885
  if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
3087
- if (litEnd > litLimit_8) return ERROR(corruption_detected); /* risk read beyond lit buffer */
2886
+ if (litEnd > litLimit) return ERROR(corruption_detected); /* risk read beyond lit buffer */
3088
2887
 
3089
2888
  /* copy Literals */
3090
2889
  ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
@@ -3110,7 +2909,7 @@ static size_t ZSTD_execSequence(BYTE* op,
3110
2909
  op = oLitEnd + length1;
3111
2910
  sequence.matchLength -= length1;
3112
2911
  match = base;
3113
- if (op > oend_8) {
2912
+ if (op > oend_8 || sequence.matchLength < MINMATCH) {
3114
2913
  while (op < oMatchEnd) *op++ = *match++;
3115
2914
  return sequenceLength;
3116
2915
  }
@@ -3119,8 +2918,7 @@ static size_t ZSTD_execSequence(BYTE* op,
3119
2918
  /* Requirement: op <= oend_8 */
3120
2919
 
3121
2920
  /* match within prefix */
3122
- if (sequence.offset < 8)
3123
- {
2921
+ if (sequence.offset < 8) {
3124
2922
  /* close range match, overlap */
3125
2923
  const int sub2 = dec64table[sequence.offset];
3126
2924
  op[0] = match[0];
@@ -3130,14 +2928,12 @@ static size_t ZSTD_execSequence(BYTE* op,
3130
2928
  match += dec32table[sequence.offset];
3131
2929
  ZSTD_copy4(op+4, match);
3132
2930
  match -= sub2;
3133
- }
3134
- else
3135
- {
2931
+ } else {
3136
2932
  ZSTD_copy8(op, match);
3137
2933
  }
3138
2934
  op += 8; match += 8;
3139
2935
 
3140
- if (oMatchEnd > oend-12)
2936
+ if (oMatchEnd > oend-(16-MINMATCH))
3141
2937
  {
3142
2938
  if (op < oend_8)
3143
2939
  {
@@ -3149,7 +2945,7 @@ static size_t ZSTD_execSequence(BYTE* op,
3149
2945
  }
3150
2946
  else
3151
2947
  {
3152
- ZSTD_wildcopy(op, match, sequence.matchLength-8); /* works even if matchLength < 8 */
2948
+ ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8, but must be signed */
3153
2949
  }
3154
2950
  return sequenceLength;
3155
2951
  }
@@ -3167,7 +2963,6 @@ static size_t ZSTD_decompressSequences(
3167
2963
  BYTE* const oend = ostart + maxDstSize;
3168
2964
  size_t errorCode, dumpsLength;
3169
2965
  const BYTE* litPtr = dctx->litPtr;
3170
- const BYTE* const litLimit_8 = litPtr + dctx->litBufSize - 8;
3171
2966
  const BYTE* const litEnd = litPtr + dctx->litSize;
3172
2967
  int nbSeq;
3173
2968
  const BYTE* dumps;
@@ -3206,7 +3001,7 @@ static size_t ZSTD_decompressSequences(
3206
3001
  size_t oneSeqSize;
3207
3002
  nbSeq--;
3208
3003
  ZSTD_decodeSequence(&sequence, &seqState);
3209
- oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litLimit_8, base, vBase, dictEnd);
3004
+ oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, base, vBase, dictEnd);
3210
3005
  if (ZSTD_isError(oneSeqSize)) return oneSeqSize;
3211
3006
  op += oneSeqSize;
3212
3007
  }
@@ -3219,8 +3014,10 @@ static size_t ZSTD_decompressSequences(
3219
3014
  size_t lastLLSize = litEnd - litPtr;
3220
3015
  if (litPtr > litEnd) return ERROR(corruption_detected);
3221
3016
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3222
- if (op != litPtr) memcpy(op, litPtr, lastLLSize);
3223
- op += lastLLSize;
3017
+ if (lastLLSize > 0) {
3018
+ if (op != litPtr) memcpy(op, litPtr, lastLLSize);
3019
+ op += lastLLSize;
3020
+ }
3224
3021
  }
3225
3022
  }
3226
3023
 
@@ -3246,9 +3043,12 @@ static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx,
3246
3043
  {
3247
3044
  /* blockType == blockCompressed */
3248
3045
  const BYTE* ip = (const BYTE*)src;
3046
+ size_t litCSize;
3047
+
3048
+ if (srcSize > BLOCKSIZE) return ERROR(corruption_detected);
3249
3049
 
3250
3050
  /* Decode literals sub-block */
3251
- size_t litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
3051
+ litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize);
3252
3052
  if (ZSTD_isError(litCSize)) return litCSize;
3253
3053
  ip += litCSize;
3254
3054
  srcSize -= litCSize;
@@ -3336,6 +3136,58 @@ static size_t ZSTD_decompress_usingDict(ZSTD_DCtx* ctx,
3336
3136
  return op-ostart;
3337
3137
  }
3338
3138
 
3139
+ /* ZSTD_errorFrameSizeInfoLegacy() :
3140
+ assumes `cSize` and `dBound` are _not_ NULL */
3141
+ static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)
3142
+ {
3143
+ *cSize = ret;
3144
+ *dBound = ZSTD_CONTENTSIZE_ERROR;
3145
+ }
3146
+
3147
+ void ZSTDv04_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)
3148
+ {
3149
+ const BYTE* ip = (const BYTE*)src;
3150
+ size_t remainingSize = srcSize;
3151
+ size_t nbBlocks = 0;
3152
+ blockProperties_t blockProperties;
3153
+
3154
+ /* Frame Header */
3155
+ if (srcSize < ZSTD_frameHeaderSize_min) {
3156
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3157
+ return;
3158
+ }
3159
+ if (MEM_readLE32(src) != ZSTD_MAGICNUMBER) {
3160
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));
3161
+ return;
3162
+ }
3163
+ ip += ZSTD_frameHeaderSize_min; remainingSize -= ZSTD_frameHeaderSize_min;
3164
+
3165
+ /* Loop on each block */
3166
+ while (1)
3167
+ {
3168
+ size_t cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
3169
+ if (ZSTD_isError(cBlockSize)) {
3170
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize);
3171
+ return;
3172
+ }
3173
+
3174
+ ip += ZSTD_blockHeaderSize;
3175
+ remainingSize -= ZSTD_blockHeaderSize;
3176
+ if (cBlockSize > remainingSize) {
3177
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3178
+ return;
3179
+ }
3180
+
3181
+ if (cBlockSize == 0) break; /* bt_end */
3182
+
3183
+ ip += cBlockSize;
3184
+ remainingSize -= cBlockSize;
3185
+ nbBlocks++;
3186
+ }
3187
+
3188
+ *cSize = ip - (const BYTE*)src;
3189
+ *dBound = nbBlocks * BLOCKSIZE;
3190
+ }
3339
3191
 
3340
3192
  /* ******************************
3341
3193
  * Streaming Decompression API
@@ -3563,7 +3415,9 @@ static size_t ZBUFF_decompressWithDictionary(ZBUFF_DCtx* zbc, const void* src, s
3563
3415
  static size_t ZBUFF_limitCopy(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3564
3416
  {
3565
3417
  size_t length = MIN(maxDstSize, srcSize);
3566
- memcpy(dst, src, length);
3418
+ if (length > 0) {
3419
+ memcpy(dst, src, length);
3420
+ }
3567
3421
  return length;
3568
3422
  }
3569
3423
 
@@ -3579,12 +3433,14 @@ static size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDs
3579
3433
  char* const oend = ostart + *maxDstSizePtr;
3580
3434
  U32 notDone = 1;
3581
3435
 
3436
+ DEBUGLOG(5, "ZBUFF_decompressContinue");
3582
3437
  while (notDone)
3583
3438
  {
3584
3439
  switch(zbc->stage)
3585
3440
  {
3586
3441
 
3587
3442
  case ZBUFFds_init :
3443
+ DEBUGLOG(5, "ZBUFF_decompressContinue: stage==ZBUFFds_init => ERROR(init_missing)");
3588
3444
  return ERROR(init_missing);
3589
3445
 
3590
3446
  case ZBUFFds_readHeader :
@@ -3646,7 +3502,7 @@ static size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDs
3646
3502
  break;
3647
3503
  }
3648
3504
  zbc->stage = ZBUFFds_read;
3649
-
3505
+ /* fall-through */
3650
3506
  case ZBUFFds_read:
3651
3507
  {
3652
3508
  size_t neededInSize = ZSTD_nextSrcSizeToDecompress(zbc->zc);
@@ -3672,7 +3528,7 @@ static size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDs
3672
3528
  if (ip==iend) { notDone = 0; break; } /* no more input */
3673
3529
  zbc->stage = ZBUFFds_load;
3674
3530
  }
3675
-
3531
+ /* fall-through */
3676
3532
  case ZBUFFds_load:
3677
3533
  {
3678
3534
  size_t neededInSize = ZSTD_nextSrcSizeToDecompress(zbc->zc);
@@ -3692,9 +3548,10 @@ static size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDs
3692
3548
  if (!decodedSize) { zbc->stage = ZBUFFds_read; break; } /* this was just a header */
3693
3549
  zbc->outEnd = zbc->outStart + decodedSize;
3694
3550
  zbc->stage = ZBUFFds_flush;
3695
- // break; /* ZBUFFds_flush follows */
3551
+ /* ZBUFFds_flush follows */
3696
3552
  }
3697
3553
  }
3554
+ /* fall-through */
3698
3555
  case ZBUFFds_flush:
3699
3556
  {
3700
3557
  size_t toFlushSize = zbc->outEnd - zbc->outStart;
@@ -3763,7 +3620,6 @@ size_t ZSTDv04_decompress(void* dst, size_t maxDstSize, const void* src, size_t
3763
3620
  #endif
3764
3621
  }
3765
3622
 
3766
-
3767
3623
  size_t ZSTDv04_resetDCtx(ZSTDv04_Dctx* dctx) { return ZSTD_resetDCtx(dctx); }
3768
3624
 
3769
3625
  size_t ZSTDv04_nextSrcSizeToDecompress(ZSTDv04_Dctx* dctx)
@@ -3779,7 +3635,7 @@ size_t ZSTDv04_decompressContinue(ZSTDv04_Dctx* dctx, void* dst, size_t maxDstSi
3779
3635
 
3780
3636
 
3781
3637
  ZBUFFv04_DCtx* ZBUFFv04_createDCtx(void) { return ZBUFF_createDCtx(); }
3782
- size_t ZBUFFv04_freeDCtx(ZBUFFv04_DCtx* dctx) { return ZBUFF_freeDCtx(dctx); }
3638
+ size_t ZBUFFv04_freeDCtx(ZBUFFv04_DCtx* dctx) { return ZBUFF_freeDCtx(dctx); }
3783
3639
 
3784
3640
  size_t ZBUFFv04_decompressInit(ZBUFFv04_DCtx* dctx) { return ZBUFF_decompressInit(dctx); }
3785
3641
  size_t ZBUFFv04_decompressWithDictionary(ZBUFFv04_DCtx* dctx, const void* src, size_t srcSize)
@@ -3787,13 +3643,9 @@ size_t ZBUFFv04_decompressWithDictionary(ZBUFFv04_DCtx* dctx, const void* src, s
3787
3643
 
3788
3644
  size_t ZBUFFv04_decompressContinue(ZBUFFv04_DCtx* dctx, void* dst, size_t* maxDstSizePtr, const void* src, size_t* srcSizePtr)
3789
3645
  {
3646
+ DEBUGLOG(5, "ZBUFFv04_decompressContinue");
3790
3647
  return ZBUFF_decompressContinue(dctx, dst, maxDstSizePtr, src, srcSizePtr);
3791
3648
  }
3792
3649
 
3793
3650
  ZSTD_DCtx* ZSTDv04_createDCtx(void) { return ZSTD_createDCtx(); }
3794
3651
  size_t ZSTDv04_freeDCtx(ZSTD_DCtx* dctx) { return ZSTD_freeDCtx(dctx); }
3795
-
3796
- size_t ZSTDv04_getFrameParams(ZSTD_parameters* params, const void* src, size_t srcSize)
3797
- {
3798
- return ZSTD_getFrameParams(params, src, srcSize);
3799
- }