extzstd 0.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
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 ZSTDv06_H
@@ -41,6 +42,18 @@ extern "C" {
41
42
  ZSTDLIBv06_API size_t ZSTDv06_decompress( void* dst, size_t dstCapacity,
42
43
  const void* src, size_t compressedSize);
43
44
 
45
+ /**
46
+ ZSTDv06_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.6.x format
47
+ srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
48
+ cSize (output parameter) : the number of bytes that would be read to decompress this frame
49
+ or an error code if it fails (which can be tested using ZSTDv01_isError())
50
+ dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
51
+ or ZSTD_CONTENTSIZE_ERROR if an error occurs
52
+
53
+ note : assumes `cSize` and `dBound` are _not_ NULL.
54
+ */
55
+ void ZSTDv06_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
56
+ size_t* cSize, unsigned long long* dBound);
44
57
 
45
58
  /* *************************************
46
59
  * Helper functions
@@ -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
 
@@ -13,15 +14,17 @@
13
14
  #include <string.h> /* memcpy */
14
15
  #include <stdlib.h> /* malloc, free, qsort */
15
16
 
16
- #define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
17
- #include "xxhash.h" /* XXH64_* */
17
+ #ifndef XXH_STATIC_LINKING_ONLY
18
+ # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
19
+ #endif
20
+ #include "../common/xxhash.h" /* XXH64_* */
18
21
  #include "zstd_v07.h"
19
22
 
20
- #define FSEv07_STATIC_LINKING_ONLY /* FSEv07_MIN_TABLELOG */
21
- #define HUFv07_STATIC_LINKING_ONLY /* HUFv07_TABLELOG_ABSOLUTEMAX */
23
+ #define FSEv07_STATIC_LINKING_ONLY /* FSEv07_MIN_TABLELOG */
24
+ #define HUFv07_STATIC_LINKING_ONLY /* HUFv07_TABLELOG_ABSOLUTEMAX */
22
25
  #define ZSTDv07_STATIC_LINKING_ONLY
23
26
 
24
- #include "error_private.h"
27
+ #include "../common/error_private.h"
25
28
 
26
29
 
27
30
  #ifdef ZSTDv07_STATIC_LINKING_ONLY
@@ -239,7 +242,11 @@ extern "C" {
239
242
  * Basic Types
240
243
  *****************************************************************/
241
244
  #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
242
- # include <stdint.h>
245
+ # if defined(_AIX)
246
+ # include <inttypes.h>
247
+ # else
248
+ # include <stdint.h> /* intptr_t */
249
+ # endif
243
250
  typedef uint8_t BYTE;
244
251
  typedef uint16_t U16;
245
252
  typedef int16_t S16;
@@ -345,7 +352,7 @@ MEM_STATIC U32 MEM_swap32(U32 in)
345
352
  {
346
353
  #if defined(_MSC_VER) /* Visual Studio */
347
354
  return _byteswap_ulong(in);
348
- #elif defined (__GNUC__)
355
+ #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)
349
356
  return __builtin_bswap32(in);
350
357
  #else
351
358
  return ((in << 24) & 0xff000000 ) |
@@ -359,7 +366,7 @@ MEM_STATIC U64 MEM_swap64(U64 in)
359
366
  {
360
367
  #if defined(_MSC_VER) /* Visual Studio */
361
368
  return _byteswap_uint64(in);
362
- #elif defined (__GNUC__)
369
+ #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)
363
370
  return __builtin_bswap64(in);
364
371
  #else
365
372
  return ((in << 56) & 0xff00000000000000ULL) |
@@ -508,16 +515,6 @@ MEM_STATIC BITv07_DStream_status BITv07_reloadDStream(BITv07_DStream_t* bitD);
508
515
  MEM_STATIC unsigned BITv07_endOfDStream(const BITv07_DStream_t* bitD);
509
516
 
510
517
 
511
- /* Start by invoking BITv07_initDStream().
512
- * A chunk of the bitStream is then stored into a local register.
513
- * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
514
- * You can then retrieve bitFields stored into the local register, **in reverse order**.
515
- * Local register is explicitly reloaded from memory by the BITv07_reloadDStream() method.
516
- * A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BITv07_DStream_unfinished.
517
- * Otherwise, it can be less than that, so proceed accordingly.
518
- * Checking if DStream has reached its end can be performed with BITv07_endOfDStream().
519
- */
520
-
521
518
 
522
519
  /*-****************************************
523
520
  * unsafe API
@@ -530,14 +527,14 @@ MEM_STATIC size_t BITv07_readBitsFast(BITv07_DStream_t* bitD, unsigned nbBits);
530
527
  /*-**************************************************************
531
528
  * Internal functions
532
529
  ****************************************************************/
533
- MEM_STATIC unsigned BITv07_highbit32 (register U32 val)
530
+ MEM_STATIC unsigned BITv07_highbit32 (U32 val)
534
531
  {
535
532
  # if defined(_MSC_VER) /* Visual */
536
533
  unsigned long r=0;
537
534
  _BitScanReverse ( &r, val );
538
535
  return (unsigned) r;
539
536
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
540
- return 31 - __builtin_clz (val);
537
+ return __builtin_clz (val) ^ 31;
541
538
  # else /* Software version */
542
539
  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 };
543
540
  U32 v = val;
@@ -578,13 +575,13 @@ MEM_STATIC size_t BITv07_initDStream(BITv07_DStream_t* bitD, const void* srcBuff
578
575
  bitD->bitContainer = *(const BYTE*)(bitD->start);
579
576
  switch(srcSize)
580
577
  {
581
- case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
582
- case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
583
- case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
584
- case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;
585
- case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;
586
- case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8;
587
- default:;
578
+ case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);/* fall-through */
579
+ case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);/* fall-through */
580
+ case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);/* fall-through */
581
+ case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24; /* fall-through */
582
+ case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16; /* fall-through */
583
+ case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8; /* fall-through */
584
+ default: break;
588
585
  }
589
586
  { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
590
587
  bitD->bitsConsumed = lastByte ? 8 - BITv07_highbit32(lastByte) : 0;
@@ -596,13 +593,6 @@ MEM_STATIC size_t BITv07_initDStream(BITv07_DStream_t* bitD, const void* srcBuff
596
593
  }
597
594
 
598
595
 
599
- /*! BITv07_lookBits() :
600
- * Provides next n bits from local register.
601
- * local register is not modified.
602
- * On 32-bits, maxNbBits==24.
603
- * On 64-bits, maxNbBits==56.
604
- * @return : value extracted
605
- */
606
596
  MEM_STATIC size_t BITv07_lookBits(const BITv07_DStream_t* bitD, U32 nbBits)
607
597
  {
608
598
  U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
@@ -622,11 +612,6 @@ MEM_STATIC void BITv07_skipBits(BITv07_DStream_t* bitD, U32 nbBits)
622
612
  bitD->bitsConsumed += nbBits;
623
613
  }
624
614
 
625
- /*! BITv07_readBits() :
626
- * Read (consume) next n bits from local register and update.
627
- * Pay attention to not read more than nbBits contained into local register.
628
- * @return : extracted value.
629
- */
630
615
  MEM_STATIC size_t BITv07_readBits(BITv07_DStream_t* bitD, U32 nbBits)
631
616
  {
632
617
  size_t const value = BITv07_lookBits(bitD, nbBits);
@@ -643,11 +628,6 @@ MEM_STATIC size_t BITv07_readBitsFast(BITv07_DStream_t* bitD, U32 nbBits)
643
628
  return value;
644
629
  }
645
630
 
646
- /*! BITv07_reloadDStream() :
647
- * Refill `BITv07_DStream_t` from src buffer previously defined (see BITv07_initDStream() ).
648
- * This function is safe, it guarantees it will not read beyond src buffer.
649
- * @return : status of `BITv07_DStream_t` internal register.
650
- if status == unfinished, internal register is filled with >= (sizeof(bitD->bitContainer)*8 - 7) bits */
651
631
  MEM_STATIC BITv07_DStream_status BITv07_reloadDStream(BITv07_DStream_t* bitD)
652
632
  {
653
633
  if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should not happen => corruption detected */
@@ -871,55 +851,6 @@ static void FSEv07_initDState(FSEv07_DState_t* DStatePtr, BITv07_DStream_t*
871
851
  static unsigned char FSEv07_decodeSymbol(FSEv07_DState_t* DStatePtr, BITv07_DStream_t* bitD);
872
852
 
873
853
 
874
- /**<
875
- Let's now decompose FSEv07_decompress_usingDTable() into its unitary components.
876
- You will decode FSE-encoded symbols from the bitStream,
877
- and also any other bitFields you put in, **in reverse order**.
878
-
879
- You will need a few variables to track your bitStream. They are :
880
-
881
- BITv07_DStream_t DStream; // Stream context
882
- FSEv07_DState_t DState; // State context. Multiple ones are possible
883
- FSEv07_DTable* DTablePtr; // Decoding table, provided by FSEv07_buildDTable()
884
-
885
- The first thing to do is to init the bitStream.
886
- errorCode = BITv07_initDStream(&DStream, srcBuffer, srcSize);
887
-
888
- You should then retrieve your initial state(s)
889
- (in reverse flushing order if you have several ones) :
890
- errorCode = FSEv07_initDState(&DState, &DStream, DTablePtr);
891
-
892
- You can then decode your data, symbol after symbol.
893
- For information the maximum number of bits read by FSEv07_decodeSymbol() is 'tableLog'.
894
- Keep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out).
895
- unsigned char symbol = FSEv07_decodeSymbol(&DState, &DStream);
896
-
897
- You can retrieve any bitfield you eventually stored into the bitStream (in reverse order)
898
- Note : maximum allowed nbBits is 25, for 32-bits compatibility
899
- size_t bitField = BITv07_readBits(&DStream, nbBits);
900
-
901
- All above operations only read from local register (which size depends on size_t).
902
- Refueling the register from memory is manually performed by the reload method.
903
- endSignal = FSEv07_reloadDStream(&DStream);
904
-
905
- BITv07_reloadDStream() result tells if there is still some more data to read from DStream.
906
- BITv07_DStream_unfinished : there is still some data left into the DStream.
907
- BITv07_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled.
908
- BITv07_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed.
909
- BITv07_DStream_tooFar : Dstream went too far. Decompression result is corrupted.
910
-
911
- When reaching end of buffer (BITv07_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop,
912
- to properly detect the exact end of stream.
913
- After each decoded symbol, check if DStream is fully consumed using this simple test :
914
- BITv07_reloadDStream(&DStream) >= BITv07_DStream_completed
915
-
916
- When it's done, verify decompression is fully completed, by checking both DStream and the relevant states.
917
- Checking if DStream has reached its end is performed by :
918
- BITv07_endOfDStream(&DStream);
919
- Check also the states. There might be some symbols left there, if some high probability ones (>50%) are possible.
920
- FSEv07_endOfDState(&DState);
921
- */
922
-
923
854
 
924
855
  /* *****************************************
925
856
  * FSE unsafe API
@@ -1387,7 +1318,7 @@ size_t HUFv07_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
1387
1318
 
1388
1319
  if (!srcSize) return ERROR(srcSize_wrong);
1389
1320
  iSize = ip[0];
1390
- //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
1321
+ /* memset(huffWeight, 0, hwSize); */ /* is not necessary, even though some analyzer complain ... */
1391
1322
 
1392
1323
  if (iSize >= 128) { /* special header */
1393
1324
  if (iSize >= (242)) { /* RLE */
@@ -1857,7 +1788,7 @@ size_t HUFv07_readDTableX2 (HUFv07_DTable* DTable, const void* src, size_t srcSi
1857
1788
  HUFv07_DEltX2* const dt = (HUFv07_DEltX2*)dtPtr;
1858
1789
 
1859
1790
  HUFv07_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUFv07_DTable));
1860
- //memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */
1791
+ /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
1861
1792
 
1862
1793
  iSize = HUFv07_readStats(huffWeight, HUFv07_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
1863
1794
  if (HUFv07_isError(iSize)) return iSize;
@@ -2221,7 +2152,7 @@ size_t HUFv07_readDTableX4 (HUFv07_DTable* DTable, const void* src, size_t srcSi
2221
2152
 
2222
2153
  HUFv07_STATIC_ASSERT(sizeof(HUFv07_DEltX4) == sizeof(HUFv07_DTable)); /* if compilation fails here, assertion is false */
2223
2154
  if (maxTableLog > HUFv07_TABLELOG_ABSOLUTEMAX) return ERROR(tableLog_tooLarge);
2224
- //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */
2155
+ /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
2225
2156
 
2226
2157
  iSize = HUFv07_readStats(weightList, HUFv07_SYMBOLVALUE_MAX + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2227
2158
  if (HUFv07_isError(iSize)) return iSize;
@@ -2603,8 +2534,8 @@ size_t HUFv07_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cS
2603
2534
  return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
2604
2535
  }
2605
2536
 
2606
- //return HUFv07_decompress4X2(dst, dstSize, cSrc, cSrcSize); /* multi-streams single-symbol decoding */
2607
- //return HUFv07_decompress4X4(dst, dstSize, cSrc, cSrcSize); /* multi-streams double-symbols decoding */
2537
+ /* return HUFv07_decompress4X2(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams single-symbol decoding */
2538
+ /* return HUFv07_decompress4X4(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams double-symbols decoding */
2608
2539
  }
2609
2540
 
2610
2541
  size_t HUFv07_decompress4X_DCtx (HUFv07_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
@@ -2701,7 +2632,7 @@ const char* ZBUFFv07_getErrorName(size_t errorCode) { return ERR_getErrorName(er
2701
2632
 
2702
2633
 
2703
2634
 
2704
- void* ZSTDv07_defaultAllocFunction(void* opaque, size_t size)
2635
+ static void* ZSTDv07_defaultAllocFunction(void* opaque, size_t size)
2705
2636
  {
2706
2637
  void* address = malloc(size);
2707
2638
  (void)opaque;
@@ -2709,7 +2640,7 @@ void* ZSTDv07_defaultAllocFunction(void* opaque, size_t size)
2709
2640
  return address;
2710
2641
  }
2711
2642
 
2712
- void ZSTDv07_defaultFreeFunction(void* opaque, void* address)
2643
+ static void ZSTDv07_defaultFreeFunction(void* opaque, void* address)
2713
2644
  {
2714
2645
  (void)opaque;
2715
2646
  /* if (address) printf("free %p opaque=%p \n", address, opaque); */
@@ -2813,6 +2744,8 @@ typedef enum { lbt_huffman, lbt_repeat, lbt_raw, lbt_rle } litBlockType_t;
2813
2744
  #define FSEv07_ENCODING_STATIC 2
2814
2745
  #define FSEv07_ENCODING_DYNAMIC 3
2815
2746
 
2747
+ #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
2748
+
2816
2749
  static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2817
2750
  1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
2818
2751
  13,14,15,16 };
@@ -2845,7 +2778,7 @@ static void ZSTDv07_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
2845
2778
  /*! ZSTDv07_wildcopy() :
2846
2779
  * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
2847
2780
  #define WILDCOPY_OVERLENGTH 8
2848
- MEM_STATIC void ZSTDv07_wildcopy(void* dst, const void* src, size_t length)
2781
+ MEM_STATIC void ZSTDv07_wildcopy(void* dst, const void* src, ptrdiff_t length)
2849
2782
  {
2850
2783
  const BYTE* ip = (const BYTE*)src;
2851
2784
  BYTE* op = (BYTE*)dst;
@@ -2918,8 +2851,6 @@ typedef struct {
2918
2851
  void ZSTDv07_seqToCodes(const seqStore_t* seqStorePtr, size_t const nbSeq);
2919
2852
 
2920
2853
  /* custom memory allocation functions */
2921
- void* ZSTDv07_defaultAllocFunction(void* opaque, size_t size);
2922
- void ZSTDv07_defaultFreeFunction(void* opaque, void* address);
2923
2854
  static const ZSTDv07_customMem defaultCustomMem = { ZSTDv07_defaultAllocFunction, ZSTDv07_defaultFreeFunction, NULL };
2924
2855
 
2925
2856
  #endif /* ZSTDv07_CCOMMON_H_MODULE */
@@ -3021,7 +2952,6 @@ struct ZSTDv07_DCtx_s
3021
2952
  U32 dictID;
3022
2953
  const BYTE* litPtr;
3023
2954
  ZSTDv07_customMem customMem;
3024
- size_t litBufSize;
3025
2955
  size_t litSize;
3026
2956
  BYTE litBuffer[ZSTDv07_BLOCKSIZE_ABSOLUTEMAX + WILDCOPY_OVERLENGTH];
3027
2957
  BYTE headerBuffer[ZSTDv07_FRAMEHEADERSIZE_MAX];
@@ -3226,10 +3156,10 @@ size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src,
3226
3156
  const BYTE* ip = (const BYTE*)src;
3227
3157
 
3228
3158
  if (srcSize < ZSTDv07_frameHeaderSize_min) return ZSTDv07_frameHeaderSize_min;
3159
+ memset(fparamsPtr, 0, sizeof(*fparamsPtr));
3229
3160
  if (MEM_readLE32(src) != ZSTDv07_MAGICNUMBER) {
3230
3161
  if ((MEM_readLE32(src) & 0xFFFFFFF0U) == ZSTDv07_MAGIC_SKIPPABLE_START) {
3231
3162
  if (srcSize < ZSTDv07_skippableHeaderSize) return ZSTDv07_skippableHeaderSize; /* magic number + skippable frame length */
3232
- memset(fparamsPtr, 0, sizeof(*fparamsPtr));
3233
3163
  fparamsPtr->frameContentSize = MEM_readLE32((const char *)src + 4);
3234
3164
  fparamsPtr->windowSize = 0; /* windowSize==0 means a frame is skippable */
3235
3165
  return 0;
@@ -3251,11 +3181,13 @@ size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src,
3251
3181
  U32 windowSize = 0;
3252
3182
  U32 dictID = 0;
3253
3183
  U64 frameContentSize = 0;
3254
- if ((fhdByte & 0x08) != 0) return ERROR(frameParameter_unsupported); /* reserved bits, which must be zero */
3184
+ if ((fhdByte & 0x08) != 0) /* reserved bits, which must be zero */
3185
+ return ERROR(frameParameter_unsupported);
3255
3186
  if (!directMode) {
3256
3187
  BYTE const wlByte = ip[pos++];
3257
3188
  U32 const windowLog = (wlByte >> 3) + ZSTDv07_WINDOWLOG_ABSOLUTEMIN;
3258
- if (windowLog > ZSTDv07_WINDOWLOG_MAX) return ERROR(frameParameter_unsupported);
3189
+ if (windowLog > ZSTDv07_WINDOWLOG_MAX)
3190
+ return ERROR(frameParameter_unsupported);
3259
3191
  windowSize = (1U << windowLog);
3260
3192
  windowSize += (windowSize >> 3) * (wlByte&7);
3261
3193
  }
@@ -3277,7 +3209,8 @@ size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src,
3277
3209
  case 3 : frameContentSize = MEM_readLE64(ip+pos); break;
3278
3210
  }
3279
3211
  if (!windowSize) windowSize = (U32)frameContentSize;
3280
- if (windowSize > windowSizeMax) return ERROR(frameParameter_unsupported);
3212
+ if (windowSize > windowSizeMax)
3213
+ return ERROR(frameParameter_unsupported);
3281
3214
  fparamsPtr->frameContentSize = frameContentSize;
3282
3215
  fparamsPtr->windowSize = windowSize;
3283
3216
  fparamsPtr->dictID = dictID;
@@ -3296,11 +3229,10 @@ size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src,
3296
3229
  - frame header not completely provided (`srcSize` too small) */
3297
3230
  unsigned long long ZSTDv07_getDecompressedSize(const void* src, size_t srcSize)
3298
3231
  {
3299
- { ZSTDv07_frameParams fparams;
3300
- size_t const frResult = ZSTDv07_getFrameParams(&fparams, src, srcSize);
3301
- if (frResult!=0) return 0;
3302
- return fparams.frameContentSize;
3303
- }
3232
+ ZSTDv07_frameParams fparams;
3233
+ size_t const frResult = ZSTDv07_getFrameParams(&fparams, src, srcSize);
3234
+ if (frResult!=0) return 0;
3235
+ return fparams.frameContentSize;
3304
3236
  }
3305
3237
 
3306
3238
 
@@ -3324,7 +3256,7 @@ typedef struct
3324
3256
 
3325
3257
  /*! ZSTDv07_getcBlockSize() :
3326
3258
  * Provides the size of compressed block from block header `src` */
3327
- size_t ZSTDv07_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
3259
+ static size_t ZSTDv07_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
3328
3260
  {
3329
3261
  const BYTE* const in = (const BYTE* const)src;
3330
3262
  U32 cSize;
@@ -3344,14 +3276,16 @@ size_t ZSTDv07_getcBlockSize(const void* src, size_t srcSize, blockProperties_t*
3344
3276
  static size_t ZSTDv07_copyRawBlock(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
3345
3277
  {
3346
3278
  if (srcSize > dstCapacity) return ERROR(dstSize_tooSmall);
3347
- memcpy(dst, src, srcSize);
3279
+ if (srcSize > 0) {
3280
+ memcpy(dst, src, srcSize);
3281
+ }
3348
3282
  return srcSize;
3349
3283
  }
3350
3284
 
3351
3285
 
3352
3286
  /*! ZSTDv07_decodeLiteralsBlock() :
3353
3287
  @return : nb of bytes read from src (< srcSize ) */
3354
- size_t ZSTDv07_decodeLiteralsBlock(ZSTDv07_DCtx* dctx,
3288
+ static size_t ZSTDv07_decodeLiteralsBlock(ZSTDv07_DCtx* dctx,
3355
3289
  const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
3356
3290
  {
3357
3291
  const BYTE* const istart = (const BYTE*) src;
@@ -3395,9 +3329,9 @@ size_t ZSTDv07_decodeLiteralsBlock(ZSTDv07_DCtx* dctx,
3395
3329
  return ERROR(corruption_detected);
3396
3330
 
3397
3331
  dctx->litPtr = dctx->litBuffer;
3398
- dctx->litBufSize = ZSTDv07_BLOCKSIZE_ABSOLUTEMAX+8;
3399
3332
  dctx->litSize = litSize;
3400
3333
  dctx->litEntropy = 1;
3334
+ memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
3401
3335
  return litCSize + lhSize;
3402
3336
  }
3403
3337
  case lbt_repeat:
@@ -3418,8 +3352,8 @@ size_t ZSTDv07_decodeLiteralsBlock(ZSTDv07_DCtx* dctx,
3418
3352
  if (HUFv07_isError(errorCode)) return ERROR(corruption_detected);
3419
3353
  }
3420
3354
  dctx->litPtr = dctx->litBuffer;
3421
- dctx->litBufSize = ZSTDv07_BLOCKSIZE_ABSOLUTEMAX+WILDCOPY_OVERLENGTH;
3422
3355
  dctx->litSize = litSize;
3356
+ memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
3423
3357
  return litCSize + lhSize;
3424
3358
  }
3425
3359
  case lbt_raw:
@@ -3443,13 +3377,12 @@ size_t ZSTDv07_decodeLiteralsBlock(ZSTDv07_DCtx* dctx,
3443
3377
  if (litSize+lhSize > srcSize) return ERROR(corruption_detected);
3444
3378
  memcpy(dctx->litBuffer, istart+lhSize, litSize);
3445
3379
  dctx->litPtr = dctx->litBuffer;
3446
- dctx->litBufSize = ZSTDv07_BLOCKSIZE_ABSOLUTEMAX+8;
3447
3380
  dctx->litSize = litSize;
3381
+ memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
3448
3382
  return lhSize+litSize;
3449
3383
  }
3450
3384
  /* direct reference into compressed stream */
3451
3385
  dctx->litPtr = istart+lhSize;
3452
- dctx->litBufSize = srcSize-lhSize;
3453
3386
  dctx->litSize = litSize;
3454
3387
  return lhSize+litSize;
3455
3388
  }
@@ -3471,9 +3404,8 @@ size_t ZSTDv07_decodeLiteralsBlock(ZSTDv07_DCtx* dctx,
3471
3404
  break;
3472
3405
  }
3473
3406
  if (litSize > ZSTDv07_BLOCKSIZE_ABSOLUTEMAX) return ERROR(corruption_detected);
3474
- memset(dctx->litBuffer, istart[lhSize], litSize);
3407
+ memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH);
3475
3408
  dctx->litPtr = dctx->litBuffer;
3476
- dctx->litBufSize = ZSTDv07_BLOCKSIZE_ABSOLUTEMAX+WILDCOPY_OVERLENGTH;
3477
3409
  dctx->litSize = litSize;
3478
3410
  return lhSize+1;
3479
3411
  }
@@ -3487,7 +3419,7 @@ size_t ZSTDv07_decodeLiteralsBlock(ZSTDv07_DCtx* dctx,
3487
3419
  @return : nb bytes read from src,
3488
3420
  or an error code if it fails, testable with ZSTDv07_isError()
3489
3421
  */
3490
- size_t ZSTDv07_buildSeqTable(FSEv07_DTable* DTable, U32 type, U32 max, U32 maxLog,
3422
+ static size_t ZSTDv07_buildSeqTable(FSEv07_DTable* DTable, U32 type, U32 max, U32 maxLog,
3491
3423
  const void* src, size_t srcSize,
3492
3424
  const S16* defaultNorm, U32 defaultLog, U32 flagRepeatTable)
3493
3425
  {
@@ -3517,7 +3449,7 @@ size_t ZSTDv07_buildSeqTable(FSEv07_DTable* DTable, U32 type, U32 max, U32 maxLo
3517
3449
  }
3518
3450
 
3519
3451
 
3520
- size_t ZSTDv07_decodeSeqHeaders(int* nbSeqPtr,
3452
+ static size_t ZSTDv07_decodeSeqHeaders(int* nbSeqPtr,
3521
3453
  FSEv07_DTable* DTableLL, FSEv07_DTable* DTableML, FSEv07_DTable* DTableOffb, U32 flagRepeatTable,
3522
3454
  const void* src, size_t srcSize)
3523
3455
  {
@@ -3544,14 +3476,12 @@ size_t ZSTDv07_decodeSeqHeaders(int* nbSeqPtr,
3544
3476
  }
3545
3477
 
3546
3478
  /* FSE table descriptors */
3479
+ if (ip + 4 > iend) return ERROR(srcSize_wrong); /* min : header byte + all 3 are "raw", hence no header, but at least xxLog bits per type */
3547
3480
  { U32 const LLtype = *ip >> 6;
3548
3481
  U32 const OFtype = (*ip >> 4) & 3;
3549
3482
  U32 const MLtype = (*ip >> 2) & 3;
3550
3483
  ip++;
3551
3484
 
3552
- /* check */
3553
- if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */
3554
-
3555
3485
  /* Build DTables */
3556
3486
  { size_t const llhSize = ZSTDv07_buildSeqTable(DTableLL, LLtype, MaxLL, LLFSELog, ip, iend-ip, LL_defaultNorm, LL_defaultNormLog, flagRepeatTable);
3557
3487
  if (ZSTDv07_isError(llhSize)) return ERROR(corruption_detected);
@@ -3662,7 +3592,7 @@ static seq_t ZSTDv07_decodeSequence(seqState_t* seqState)
3662
3592
  static
3663
3593
  size_t ZSTDv07_execSequence(BYTE* op,
3664
3594
  BYTE* const oend, seq_t sequence,
3665
- const BYTE** litPtr, const BYTE* const litLimit_w,
3595
+ const BYTE** litPtr, const BYTE* const litLimit,
3666
3596
  const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
3667
3597
  {
3668
3598
  BYTE* const oLitEnd = op + sequence.litLength;
@@ -3674,7 +3604,7 @@ size_t ZSTDv07_execSequence(BYTE* op,
3674
3604
 
3675
3605
  /* check */
3676
3606
  if ((oLitEnd>oend_w) | (oMatchEnd>oend)) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of WILDCOPY_OVERLENGTH from oend */
3677
- if (iLitEnd > litLimit_w) return ERROR(corruption_detected); /* over-read beyond lit buffer */
3607
+ if (iLitEnd > litLimit) return ERROR(corruption_detected); /* over-read beyond lit buffer */
3678
3608
 
3679
3609
  /* copy Literals */
3680
3610
  ZSTDv07_wildcopy(op, *litPtr, sequence.litLength); /* note : since oLitEnd <= oend-WILDCOPY_OVERLENGTH, no risk of overwrite beyond oend */
@@ -3696,7 +3626,7 @@ size_t ZSTDv07_execSequence(BYTE* op,
3696
3626
  op = oLitEnd + length1;
3697
3627
  sequence.matchLength -= length1;
3698
3628
  match = base;
3699
- if (op > oend_w) {
3629
+ if (op > oend_w || sequence.matchLength < MINMATCH) {
3700
3630
  while (op < oMatchEnd) *op++ = *match++;
3701
3631
  return sequenceLength;
3702
3632
  }
@@ -3707,7 +3637,7 @@ size_t ZSTDv07_execSequence(BYTE* op,
3707
3637
  if (sequence.offset < 8) {
3708
3638
  /* close range match, overlap */
3709
3639
  static const U32 dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
3710
- static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* substracted */
3640
+ static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */
3711
3641
  int const sub2 = dec64table[sequence.offset];
3712
3642
  op[0] = match[0];
3713
3643
  op[1] = match[1];
@@ -3729,7 +3659,7 @@ size_t ZSTDv07_execSequence(BYTE* op,
3729
3659
  }
3730
3660
  while (op < oMatchEnd) *op++ = *match++;
3731
3661
  } else {
3732
- ZSTDv07_wildcopy(op, match, sequence.matchLength-8); /* works even if matchLength < 8 */
3662
+ ZSTDv07_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */
3733
3663
  }
3734
3664
  return sequenceLength;
3735
3665
  }
@@ -3746,7 +3676,6 @@ static size_t ZSTDv07_decompressSequences(
3746
3676
  BYTE* const oend = ostart + maxDstSize;
3747
3677
  BYTE* op = ostart;
3748
3678
  const BYTE* litPtr = dctx->litPtr;
3749
- const BYTE* const litLimit_w = litPtr + dctx->litBufSize - WILDCOPY_OVERLENGTH;
3750
3679
  const BYTE* const litEnd = litPtr + dctx->litSize;
3751
3680
  FSEv07_DTable* DTableLL = dctx->LLTable;
3752
3681
  FSEv07_DTable* DTableML = dctx->MLTable;
@@ -3776,7 +3705,7 @@ static size_t ZSTDv07_decompressSequences(
3776
3705
  for ( ; (BITv07_reloadDStream(&(seqState.DStream)) <= BITv07_DStream_completed) && nbSeq ; ) {
3777
3706
  nbSeq--;
3778
3707
  { seq_t const sequence = ZSTDv07_decodeSequence(&seqState);
3779
- size_t const oneSeqSize = ZSTDv07_execSequence(op, oend, sequence, &litPtr, litLimit_w, base, vBase, dictEnd);
3708
+ size_t const oneSeqSize = ZSTDv07_execSequence(op, oend, sequence, &litPtr, litEnd, base, vBase, dictEnd);
3780
3709
  if (ZSTDv07_isError(oneSeqSize)) return oneSeqSize;
3781
3710
  op += oneSeqSize;
3782
3711
  } }
@@ -3789,10 +3718,12 @@ static size_t ZSTDv07_decompressSequences(
3789
3718
 
3790
3719
  /* last literal segment */
3791
3720
  { size_t const lastLLSize = litEnd - litPtr;
3792
- //if (litPtr > litEnd) return ERROR(corruption_detected); /* too many literals already used */
3721
+ /* if (litPtr > litEnd) return ERROR(corruption_detected); */ /* too many literals already used */
3793
3722
  if (lastLLSize > (size_t)(oend-op)) return ERROR(dstSize_tooSmall);
3794
- memcpy(op, litPtr, lastLLSize);
3795
- op += lastLLSize;
3723
+ if (lastLLSize > 0) {
3724
+ memcpy(op, litPtr, lastLLSize);
3725
+ op += lastLLSize;
3726
+ }
3796
3727
  }
3797
3728
 
3798
3729
  return op-ostart;
@@ -3850,10 +3781,12 @@ ZSTDLIBv07_API size_t ZSTDv07_insertBlock(ZSTDv07_DCtx* dctx, const void* blockS
3850
3781
  }
3851
3782
 
3852
3783
 
3853
- size_t ZSTDv07_generateNxBytes(void* dst, size_t dstCapacity, BYTE byte, size_t length)
3784
+ static size_t ZSTDv07_generateNxBytes(void* dst, size_t dstCapacity, BYTE byte, size_t length)
3854
3785
  {
3855
3786
  if (length > dstCapacity) return ERROR(dstSize_tooSmall);
3856
- memset(dst, byte, length);
3787
+ if (length > 0) {
3788
+ memset(dst, byte, length);
3789
+ }
3857
3790
  return length;
3858
3791
  }
3859
3792
 
@@ -3930,7 +3863,7 @@ static size_t ZSTDv07_decompressFrame(ZSTDv07_DCtx* dctx,
3930
3863
  * It avoids reloading the dictionary each time.
3931
3864
  * `preparedDCtx` must have been properly initialized using ZSTDv07_decompressBegin_usingDict().
3932
3865
  * Requires 2 contexts : 1 for reference (preparedDCtx), which will not be modified, and 1 to run the decompression operation (dctx) */
3933
- size_t ZSTDv07_decompress_usingPreparedDCtx(ZSTDv07_DCtx* dctx, const ZSTDv07_DCtx* refDCtx,
3866
+ static size_t ZSTDv07_decompress_usingPreparedDCtx(ZSTDv07_DCtx* dctx, const ZSTDv07_DCtx* refDCtx,
3934
3867
  void* dst, size_t dstCapacity,
3935
3868
  const void* src, size_t srcSize)
3936
3869
  {
@@ -3972,6 +3905,70 @@ size_t ZSTDv07_decompress(void* dst, size_t dstCapacity, const void* src, size_t
3972
3905
  #endif
3973
3906
  }
3974
3907
 
3908
+ /* ZSTD_errorFrameSizeInfoLegacy() :
3909
+ assumes `cSize` and `dBound` are _not_ NULL */
3910
+ static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)
3911
+ {
3912
+ *cSize = ret;
3913
+ *dBound = ZSTD_CONTENTSIZE_ERROR;
3914
+ }
3915
+
3916
+ void ZSTDv07_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)
3917
+ {
3918
+ const BYTE* ip = (const BYTE*)src;
3919
+ size_t remainingSize = srcSize;
3920
+ size_t nbBlocks = 0;
3921
+
3922
+ /* check */
3923
+ if (srcSize < ZSTDv07_frameHeaderSize_min+ZSTDv07_blockHeaderSize) {
3924
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3925
+ return;
3926
+ }
3927
+
3928
+ /* Frame Header */
3929
+ { size_t const frameHeaderSize = ZSTDv07_frameHeaderSize(src, srcSize);
3930
+ if (ZSTDv07_isError(frameHeaderSize)) {
3931
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, frameHeaderSize);
3932
+ return;
3933
+ }
3934
+ if (MEM_readLE32(src) != ZSTDv07_MAGICNUMBER) {
3935
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));
3936
+ return;
3937
+ }
3938
+ if (srcSize < frameHeaderSize+ZSTDv07_blockHeaderSize) {
3939
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3940
+ return;
3941
+ }
3942
+ ip += frameHeaderSize; remainingSize -= frameHeaderSize;
3943
+ }
3944
+
3945
+ /* Loop on each block */
3946
+ while (1) {
3947
+ blockProperties_t blockProperties;
3948
+ size_t const cBlockSize = ZSTDv07_getcBlockSize(ip, remainingSize, &blockProperties);
3949
+ if (ZSTDv07_isError(cBlockSize)) {
3950
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize);
3951
+ return;
3952
+ }
3953
+
3954
+ ip += ZSTDv07_blockHeaderSize;
3955
+ remainingSize -= ZSTDv07_blockHeaderSize;
3956
+
3957
+ if (blockProperties.blockType == bt_end) break;
3958
+
3959
+ if (cBlockSize > remainingSize) {
3960
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3961
+ return;
3962
+ }
3963
+
3964
+ ip += cBlockSize;
3965
+ remainingSize -= cBlockSize;
3966
+ nbBlocks++;
3967
+ }
3968
+
3969
+ *cSize = ip - (const BYTE*)src;
3970
+ *dBound = nbBlocks * ZSTDv07_BLOCKSIZE_ABSOLUTEMAX;
3971
+ }
3975
3972
 
3976
3973
  /*_******************************
3977
3974
  * Streaming Decompression API
@@ -4014,7 +4011,7 @@ size_t ZSTDv07_decompressContinue(ZSTDv07_DCtx* dctx, void* dst, size_t dstCapac
4014
4011
  return 0;
4015
4012
  }
4016
4013
  dctx->expected = 0; /* not necessary to copy more */
4017
-
4014
+ /* fall-through */
4018
4015
  case ZSTDds_decodeFrameHeader:
4019
4016
  { size_t result;
4020
4017
  memcpy(dctx->headerBuffer + ZSTDv07_frameHeaderSize_min, src, dctx->expected);
@@ -4138,9 +4135,9 @@ static size_t ZSTDv07_loadEntropy(ZSTDv07_DCtx* dctx, const void* const dict, si
4138
4135
  }
4139
4136
 
4140
4137
  if (dictPtr+12 > dictEnd) return ERROR(dictionary_corrupted);
4141
- dctx->rep[0] = MEM_readLE32(dictPtr+0); if (dctx->rep[0] >= dictSize) return ERROR(dictionary_corrupted);
4142
- dctx->rep[1] = MEM_readLE32(dictPtr+4); if (dctx->rep[1] >= dictSize) return ERROR(dictionary_corrupted);
4143
- dctx->rep[2] = MEM_readLE32(dictPtr+8); if (dctx->rep[2] >= dictSize) return ERROR(dictionary_corrupted);
4138
+ dctx->rep[0] = MEM_readLE32(dictPtr+0); if (dctx->rep[0] == 0 || dctx->rep[0] >= dictSize) return ERROR(dictionary_corrupted);
4139
+ dctx->rep[1] = MEM_readLE32(dictPtr+4); if (dctx->rep[1] == 0 || dctx->rep[1] >= dictSize) return ERROR(dictionary_corrupted);
4140
+ dctx->rep[2] = MEM_readLE32(dictPtr+8); if (dctx->rep[2] == 0 || dctx->rep[2] >= dictSize) return ERROR(dictionary_corrupted);
4144
4141
  dictPtr += 12;
4145
4142
 
4146
4143
  dctx->litEntropy = dctx->fseEntropy = 1;
@@ -4190,7 +4187,7 @@ struct ZSTDv07_DDict_s {
4190
4187
  ZSTDv07_DCtx* refContext;
4191
4188
  }; /* typedef'd tp ZSTDv07_CDict within zstd.h */
4192
4189
 
4193
- ZSTDv07_DDict* ZSTDv07_createDDict_advanced(const void* dict, size_t dictSize, ZSTDv07_customMem customMem)
4190
+ static ZSTDv07_DDict* ZSTDv07_createDDict_advanced(const void* dict, size_t dictSize, ZSTDv07_customMem customMem)
4194
4191
  {
4195
4192
  if (!customMem.customAlloc && !customMem.customFree)
4196
4193
  customMem = defaultCustomMem;
@@ -4391,7 +4388,9 @@ size_t ZBUFFv07_decompressInit(ZBUFFv07_DCtx* zbd)
4391
4388
  MEM_STATIC size_t ZBUFFv07_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
4392
4389
  {
4393
4390
  size_t const length = MIN(dstCapacity, srcSize);
4394
- memcpy(dst, src, length);
4391
+ if (length > 0) {
4392
+ memcpy(dst, src, length);
4393
+ }
4395
4394
  return length;
4396
4395
  }
4397
4396
 
@@ -4452,7 +4451,7 @@ size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* zbd,
4452
4451
  zbd->inBuff = (char*)zbd->customMem.customAlloc(zbd->customMem.opaque, blockSize);
4453
4452
  if (zbd->inBuff == NULL) return ERROR(memory_allocation);
4454
4453
  }
4455
- { size_t const neededOutSize = zbd->fParams.windowSize + blockSize;
4454
+ { size_t const neededOutSize = zbd->fParams.windowSize + blockSize + WILDCOPY_OVERLENGTH * 2;
4456
4455
  if (zbd->outBuffSize < neededOutSize) {
4457
4456
  zbd->customMem.customFree(zbd->customMem.opaque, zbd->outBuff);
4458
4457
  zbd->outBuffSize = neededOutSize;
@@ -4461,7 +4460,7 @@ size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* zbd,
4461
4460
  } } }
4462
4461
  zbd->stage = ZBUFFds_read;
4463
4462
  /* pass-through */
4464
-
4463
+ /* fall-through */
4465
4464
  case ZBUFFds_read:
4466
4465
  { size_t const neededInSize = ZSTDv07_nextSrcSizeToDecompress(zbd->zd);
4467
4466
  if (neededInSize==0) { /* end of frame */
@@ -4484,7 +4483,7 @@ size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* zbd,
4484
4483
  if (ip==iend) { notDone = 0; break; } /* no more input */
4485
4484
  zbd->stage = ZBUFFds_load;
4486
4485
  }
4487
-
4486
+ /* fall-through */
4488
4487
  case ZBUFFds_load:
4489
4488
  { size_t const neededInSize = ZSTDv07_nextSrcSizeToDecompress(zbd->zd);
4490
4489
  size_t const toLoad = neededInSize - zbd->inPos; /* should always be <= remaining space within inBuff */
@@ -4505,9 +4504,11 @@ size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* zbd,
4505
4504
  if (!decodedSize && !isSkipFrame) { zbd->stage = ZBUFFds_read; break; } /* this was just a header */
4506
4505
  zbd->outEnd = zbd->outStart + decodedSize;
4507
4506
  zbd->stage = ZBUFFds_flush;
4508
- // break; /* ZBUFFds_flush follows */
4509
- } }
4510
-
4507
+ /* break; */
4508
+ /* pass-through */
4509
+ }
4510
+ }
4511
+ /* fall-through */
4511
4512
  case ZBUFFds_flush:
4512
4513
  { size_t const toFlushSize = zbd->outEnd - zbd->outStart;
4513
4514
  size_t const flushedSize = ZBUFFv07_limitCopy(op, oend-op, zbd->outBuff + zbd->outStart, toFlushSize);