extzstd 0.0.3.CONCEPT → 0.3.1

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 (138) hide show
  1. checksums.yaml +5 -5
  2. data/HISTORY.ja.md +39 -0
  3. data/LICENSE +6 -6
  4. data/README.md +26 -45
  5. data/contrib/zstd/CHANGELOG +555 -0
  6. data/contrib/zstd/CODE_OF_CONDUCT.md +5 -0
  7. data/contrib/zstd/CONTRIBUTING.md +392 -0
  8. data/contrib/zstd/COPYING +339 -0
  9. data/contrib/zstd/LICENSE +13 -9
  10. data/contrib/zstd/Makefile +414 -0
  11. data/contrib/zstd/README.md +170 -45
  12. data/contrib/zstd/TESTING.md +44 -0
  13. data/contrib/zstd/appveyor.yml +289 -0
  14. data/contrib/zstd/lib/BUCK +234 -0
  15. data/contrib/zstd/lib/Makefile +354 -0
  16. data/contrib/zstd/lib/README.md +179 -0
  17. data/contrib/zstd/{common → lib/common}/bitstream.h +170 -130
  18. data/contrib/zstd/lib/common/compiler.h +175 -0
  19. data/contrib/zstd/lib/common/cpu.h +215 -0
  20. data/contrib/zstd/lib/common/debug.c +24 -0
  21. data/contrib/zstd/lib/common/debug.h +114 -0
  22. data/contrib/zstd/{common → lib/common}/entropy_common.c +79 -94
  23. data/contrib/zstd/lib/common/error_private.c +55 -0
  24. data/contrib/zstd/lib/common/error_private.h +80 -0
  25. data/contrib/zstd/{common → lib/common}/fse.h +153 -93
  26. data/contrib/zstd/{common → lib/common}/fse_decompress.c +37 -82
  27. data/contrib/zstd/lib/common/huf.h +340 -0
  28. data/contrib/zstd/{common → lib/common}/mem.h +154 -78
  29. data/contrib/zstd/lib/common/pool.c +344 -0
  30. data/contrib/zstd/lib/common/pool.h +84 -0
  31. data/contrib/zstd/lib/common/threading.c +121 -0
  32. data/contrib/zstd/lib/common/threading.h +155 -0
  33. data/contrib/zstd/{common → lib/common}/xxhash.c +85 -75
  34. data/contrib/zstd/{common → lib/common}/xxhash.h +85 -73
  35. data/contrib/zstd/lib/common/zstd_common.c +83 -0
  36. data/contrib/zstd/lib/common/zstd_errors.h +94 -0
  37. data/contrib/zstd/lib/common/zstd_internal.h +447 -0
  38. data/contrib/zstd/{compress → lib/compress}/fse_compress.c +194 -303
  39. data/contrib/zstd/lib/compress/hist.c +183 -0
  40. data/contrib/zstd/lib/compress/hist.h +75 -0
  41. data/contrib/zstd/lib/compress/huf_compress.c +798 -0
  42. data/contrib/zstd/lib/compress/zstd_compress.c +4278 -0
  43. data/contrib/zstd/lib/compress/zstd_compress_internal.h +1125 -0
  44. data/contrib/zstd/lib/compress/zstd_compress_literals.c +158 -0
  45. data/contrib/zstd/lib/compress/zstd_compress_literals.h +29 -0
  46. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +419 -0
  47. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +54 -0
  48. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +845 -0
  49. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +32 -0
  50. data/contrib/zstd/lib/compress/zstd_cwksp.h +525 -0
  51. data/contrib/zstd/lib/compress/zstd_double_fast.c +521 -0
  52. data/contrib/zstd/lib/compress/zstd_double_fast.h +38 -0
  53. data/contrib/zstd/lib/compress/zstd_fast.c +496 -0
  54. data/contrib/zstd/lib/compress/zstd_fast.h +37 -0
  55. data/contrib/zstd/lib/compress/zstd_lazy.c +1138 -0
  56. data/contrib/zstd/lib/compress/zstd_lazy.h +67 -0
  57. data/contrib/zstd/lib/compress/zstd_ldm.c +619 -0
  58. data/contrib/zstd/lib/compress/zstd_ldm.h +110 -0
  59. data/contrib/zstd/lib/compress/zstd_opt.c +1200 -0
  60. data/contrib/zstd/lib/compress/zstd_opt.h +56 -0
  61. data/contrib/zstd/lib/compress/zstdmt_compress.c +2143 -0
  62. data/contrib/zstd/lib/compress/zstdmt_compress.h +192 -0
  63. data/contrib/zstd/lib/decompress/huf_decompress.c +1248 -0
  64. data/contrib/zstd/lib/decompress/zstd_ddict.c +244 -0
  65. data/contrib/zstd/lib/decompress/zstd_ddict.h +44 -0
  66. data/contrib/zstd/lib/decompress/zstd_decompress.c +1885 -0
  67. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1432 -0
  68. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +59 -0
  69. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +189 -0
  70. data/contrib/zstd/{common → lib/deprecated}/zbuff.h +86 -69
  71. data/contrib/zstd/lib/deprecated/zbuff_common.c +26 -0
  72. data/contrib/zstd/lib/deprecated/zbuff_compress.c +147 -0
  73. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +75 -0
  74. data/contrib/zstd/lib/dictBuilder/cover.c +1236 -0
  75. data/contrib/zstd/lib/dictBuilder/cover.h +157 -0
  76. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.c +3 -3
  77. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.h +5 -5
  78. data/contrib/zstd/lib/dictBuilder/fastcover.c +757 -0
  79. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/zdict.c +437 -347
  80. data/contrib/zstd/lib/dictBuilder/zdict.h +305 -0
  81. data/contrib/zstd/lib/legacy/zstd_legacy.h +415 -0
  82. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.c +272 -292
  83. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.h +26 -32
  84. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.c +162 -392
  85. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.h +26 -32
  86. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.c +162 -391
  87. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.h +27 -33
  88. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.c +195 -604
  89. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.h +26 -32
  90. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.c +300 -575
  91. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.h +22 -31
  92. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.c +165 -592
  93. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.h +54 -67
  94. data/contrib/zstd/lib/legacy/zstd_v07.c +4541 -0
  95. data/contrib/zstd/lib/legacy/zstd_v07.h +187 -0
  96. data/contrib/zstd/lib/libzstd.pc.in +15 -0
  97. data/contrib/zstd/lib/zstd.h +2090 -0
  98. data/ext/depend +2 -0
  99. data/ext/extconf.rb +18 -5
  100. data/ext/extzstd.c +296 -214
  101. data/ext/extzstd.h +81 -36
  102. data/ext/extzstd_nogvls.h +0 -117
  103. data/ext/extzstd_stream.c +622 -0
  104. data/ext/libzstd_conf.h +8 -0
  105. data/ext/zstd_common.c +11 -0
  106. data/ext/zstd_compress.c +15 -0
  107. data/ext/zstd_decompress.c +6 -0
  108. data/ext/zstd_dictbuilder.c +10 -0
  109. data/ext/zstd_dictbuilder_fastcover.c +3 -0
  110. data/ext/zstd_legacy_v01.c +3 -1
  111. data/ext/zstd_legacy_v02.c +3 -1
  112. data/ext/zstd_legacy_v03.c +3 -1
  113. data/ext/zstd_legacy_v04.c +3 -1
  114. data/ext/zstd_legacy_v05.c +3 -1
  115. data/ext/zstd_legacy_v06.c +3 -1
  116. data/ext/zstd_legacy_v07.c +3 -0
  117. data/gemstub.rb +27 -21
  118. data/lib/extzstd.rb +82 -161
  119. data/lib/extzstd/version.rb +1 -1
  120. data/test/test_basic.rb +19 -6
  121. metadata +127 -59
  122. data/contrib/zstd/common/error_private.h +0 -125
  123. data/contrib/zstd/common/error_public.h +0 -77
  124. data/contrib/zstd/common/huf.h +0 -228
  125. data/contrib/zstd/common/zstd.h +0 -475
  126. data/contrib/zstd/common/zstd_common.c +0 -91
  127. data/contrib/zstd/common/zstd_internal.h +0 -238
  128. data/contrib/zstd/compress/huf_compress.c +0 -577
  129. data/contrib/zstd/compress/zbuff_compress.c +0 -327
  130. data/contrib/zstd/compress/zstd_compress.c +0 -3074
  131. data/contrib/zstd/compress/zstd_opt.h +0 -1046
  132. data/contrib/zstd/decompress/huf_decompress.c +0 -894
  133. data/contrib/zstd/decompress/zbuff_decompress.c +0 -294
  134. data/contrib/zstd/decompress/zstd_decompress.c +0 -1362
  135. data/contrib/zstd/dictBuilder/zdict.h +0 -113
  136. data/contrib/zstd/legacy/zstd_legacy.h +0 -140
  137. data/ext/extzstd_buffered.c +0 -265
  138. data/ext/zstd_amalgam.c +0 -18
@@ -1,36 +1,15 @@
1
1
  /*
2
- zstd_v04 - decoder for 0.4 format
3
- Header File
4
- Copyright (C) 2016, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
- * Redistributions of source code must retain the above copyright
12
- notice, this list of conditions and the following disclaimer.
13
- * Redistributions in binary form must reproduce the above
14
- copyright notice, this list of conditions and the following disclaimer
15
- in the documentation and/or other materials provided with the
16
- distribution.
17
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
-
29
- You can contact the author at :
30
- - zstd source repository : https://github.com/Cyan4973/zstd
31
- - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
32
- */
33
- #pragma once
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under both the BSD-style license (found in the
6
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
+ * in the COPYING file in the root directory of this source tree).
8
+ * You may select, at your option, one of the above-listed licenses.
9
+ */
10
+
11
+ #ifndef ZSTD_V04_H_91868324769238
12
+ #define ZSTD_V04_H_91868324769238
34
13
 
35
14
  #if defined (__cplusplus)
36
15
  extern "C" {
@@ -56,6 +35,19 @@ ZSTDv04_decompress() : decompress ZSTD frames compliant with v0.4.x format
56
35
  size_t ZSTDv04_decompress( void* dst, size_t maxOriginalSize,
57
36
  const void* src, size_t compressedSize);
58
37
 
38
+ /**
39
+ ZSTDv04_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.4.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 ZSTDv04_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
49
+ size_t* cSize, unsigned long long* dBound);
50
+
59
51
  /**
60
52
  ZSTDv04_isError() : tells if the result of ZSTDv04_decompress() is an error
61
53
  */
@@ -146,3 +138,5 @@ size_t ZBUFFv04_recommendedDOutSize(void);
146
138
  #if defined (__cplusplus)
147
139
  }
148
140
  #endif
141
+
142
+ #endif /* ZSTD_V04_H_91868324769238 */
@@ -1,39 +1,17 @@
1
- /* ******************************************************************
2
- zstd_v05.c
3
- Decompression module for ZSTD v0.5 legacy format
4
- Copyright (C) 2016, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
-
12
- * Redistributions of source code must retain the above copyright
13
- notice, this list of conditions and the following disclaimer.
14
- * Redistributions in binary form must reproduce the above
15
- copyright notice, this list of conditions and the following disclaimer
16
- in the documentation and/or other materials provided with the
17
- distribution.
18
-
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1
+ /*
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under both the BSD-style license (found in the
6
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
+ * in the COPYING file in the root directory of this source tree).
8
+ * You may select, at your option, one of the above-listed licenses.
9
+ */
30
10
 
31
- You can contact the author at :
32
- - Homepage : http://www.zstd.net/
33
- ****************************************************************** */
34
11
 
35
12
  /*- Dependencies -*/
36
13
  #include "zstd_v05.h"
14
+ #include "../common/error_private.h"
37
15
 
38
16
 
39
17
  /* ******************************************************************
@@ -140,7 +118,7 @@ extern "C" {
140
118
  #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
141
119
  # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
142
120
  # define MEM_FORCE_MEMORY_ACCESS 2
143
- # elif defined(__INTEL_COMPILER) || \
121
+ # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
144
122
  (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
145
123
  # define MEM_FORCE_MEMORY_ACCESS 1
146
124
  # endif
@@ -250,18 +228,6 @@ MEM_STATIC U32 MEM_readLE32(const void* memPtr)
250
228
  }
251
229
  }
252
230
 
253
- MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32)
254
- {
255
- if (MEM_isLittleEndian()) {
256
- MEM_write32(memPtr, val32);
257
- } else {
258
- BYTE* p = (BYTE*)memPtr;
259
- p[0] = (BYTE)val32;
260
- p[1] = (BYTE)(val32>>8);
261
- p[2] = (BYTE)(val32>>16);
262
- p[3] = (BYTE)(val32>>24);
263
- }
264
- }
265
231
 
266
232
  MEM_STATIC U64 MEM_readLE64(const void* memPtr)
267
233
  {
@@ -274,22 +240,6 @@ MEM_STATIC U64 MEM_readLE64(const void* memPtr)
274
240
  }
275
241
  }
276
242
 
277
- MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64)
278
- {
279
- if (MEM_isLittleEndian()) {
280
- MEM_write64(memPtr, val64);
281
- } else {
282
- BYTE* p = (BYTE*)memPtr;
283
- p[0] = (BYTE)val64;
284
- p[1] = (BYTE)(val64>>8);
285
- p[2] = (BYTE)(val64>>16);
286
- p[3] = (BYTE)(val64>>24);
287
- p[4] = (BYTE)(val64>>32);
288
- p[5] = (BYTE)(val64>>40);
289
- p[6] = (BYTE)(val64>>48);
290
- p[7] = (BYTE)(val64>>56);
291
- }
292
- }
293
243
 
294
244
  MEM_STATIC size_t MEM_readLEST(const void* memPtr)
295
245
  {
@@ -299,13 +249,6 @@ MEM_STATIC size_t MEM_readLEST(const void* memPtr)
299
249
  return (size_t)MEM_readLE64(memPtr);
300
250
  }
301
251
 
302
- MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
303
- {
304
- if (MEM_32bits())
305
- MEM_writeLE32(memPtr, (U32)val);
306
- else
307
- MEM_writeLE64(memPtr, (U64)val);
308
- }
309
252
 
310
253
  #if defined (__cplusplus)
311
254
  }
@@ -313,79 +256,6 @@ MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
313
256
 
314
257
  #endif /* MEM_H_MODULE */
315
258
 
316
- /* ******************************************************************
317
- Error codes list
318
- Copyright (C) 2016, Yann Collet
319
-
320
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
321
-
322
- Redistribution and use in source and binary forms, with or without
323
- modification, are permitted provided that the following conditions are
324
- met:
325
-
326
- * Redistributions of source code must retain the above copyright
327
- notice, this list of conditions and the following disclaimer.
328
- * Redistributions in binary form must reproduce the above
329
- copyright notice, this list of conditions and the following disclaimer
330
- in the documentation and/or other materials provided with the
331
- distribution.
332
-
333
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
334
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
335
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
336
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
337
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
338
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
339
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
340
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
341
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
342
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
343
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
344
-
345
- You can contact the author at :
346
- - Source repository : https://github.com/Cyan4973/zstd
347
- ****************************************************************** */
348
- #ifndef ERROR_PUBLIC_H_MODULE
349
- #define ERROR_PUBLIC_H_MODULE
350
-
351
- #if defined (__cplusplus)
352
- extern "C" {
353
- #endif
354
-
355
-
356
- /* ****************************************
357
- * error codes list
358
- ******************************************/
359
- typedef enum {
360
- ZSTDv05_error_no_error,
361
- ZSTDv05_error_GENERIC,
362
- ZSTDv05_error_prefix_unknown,
363
- ZSTDv05_error_frameParameter_unsupported,
364
- ZSTDv05_error_frameParameter_unsupportedBy32bits,
365
- ZSTDv05_error_init_missing,
366
- ZSTDv05_error_memory_allocation,
367
- ZSTDv05_error_stage_wrong,
368
- ZSTDv05_error_dstSize_tooSmall,
369
- ZSTDv05_error_srcSize_wrong,
370
- ZSTDv05_error_corruption_detected,
371
- ZSTDv05_error_tableLog_tooLarge,
372
- ZSTDv05_error_maxSymbolValue_tooLarge,
373
- ZSTDv05_error_maxSymbolValue_tooSmall,
374
- ZSTDv05_error_dictionary_corrupted,
375
- ZSTDv05_error_maxCode
376
- } ZSTDv05_ErrorCode;
377
-
378
- /* note : functions provide error codes in reverse negative order,
379
- so compare with (size_t)(0-enum) */
380
-
381
-
382
- #if defined (__cplusplus)
383
- }
384
- #endif
385
-
386
- #endif /* ERROR_PUBLIC_H_MODULE */
387
-
388
-
389
259
  /*
390
260
  zstd - standard compression library
391
261
  Header File for static linking only
@@ -457,13 +327,6 @@ size_t ZSTDv05_decompress_usingPreparedDCtx(
457
327
  * Streaming functions (direct mode)
458
328
  ****************************************/
459
329
  size_t ZSTDv05_decompressBegin(ZSTDv05_DCtx* dctx);
460
- size_t ZSTDv05_decompressBegin_usingDict(ZSTDv05_DCtx* dctx, const void* dict, size_t dictSize);
461
- void ZSTDv05_copyDCtx(ZSTDv05_DCtx* dctx, const ZSTDv05_DCtx* preparedDCtx);
462
-
463
- size_t ZSTDv05_getFrameParams(ZSTDv05_parameters* params, const void* src, size_t srcSize);
464
-
465
- size_t ZSTDv05_nextSrcSizeToDecompress(ZSTDv05_DCtx* dctx);
466
- size_t ZSTDv05_decompressContinue(ZSTDv05_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
467
330
 
468
331
  /*
469
332
  Streaming decompression, direct mode (bufferless)
@@ -529,119 +392,6 @@ size_t ZSTDv05_decompressBlock(ZSTDv05_DCtx* dctx, void* dst, size_t dstCapacity
529
392
  #endif /* ZSTDv05_STATIC_H */
530
393
 
531
394
 
532
-
533
- /* ******************************************************************
534
- Error codes and messages
535
- Copyright (C) 2013-2016, Yann Collet
536
-
537
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
538
-
539
- Redistribution and use in source and binary forms, with or without
540
- modification, are permitted provided that the following conditions are
541
- met:
542
-
543
- * Redistributions of source code must retain the above copyright
544
- notice, this list of conditions and the following disclaimer.
545
- * Redistributions in binary form must reproduce the above
546
- copyright notice, this list of conditions and the following disclaimer
547
- in the documentation and/or other materials provided with the
548
- distribution.
549
-
550
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
551
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
552
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
553
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
554
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
555
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
556
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
557
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
558
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
559
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
560
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
561
-
562
- You can contact the author at :
563
- - Source repository : https://github.com/Cyan4973/zstd
564
- ****************************************************************** */
565
- /* Note : this module is expected to remain private, do not expose it */
566
-
567
- #ifndef ERROR_H_MODULE
568
- #define ERROR_H_MODULE
569
-
570
- #if defined (__cplusplus)
571
- extern "C" {
572
- #endif
573
-
574
-
575
-
576
- /* ****************************************
577
- * Compiler-specific
578
- ******************************************/
579
- #if defined(__GNUC__)
580
- # define ERR_STATIC static __attribute__((unused))
581
- #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
582
- # define ERR_STATIC static inline
583
- #elif defined(_MSC_VER)
584
- # define ERR_STATIC static __inline
585
- #else
586
- # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
587
- #endif
588
-
589
-
590
- /*-****************************************
591
- * Customization
592
- ******************************************/
593
- typedef ZSTDv05_ErrorCode ERR_enum;
594
- #define PREFIX(name) ZSTDv05_error_##name
595
-
596
-
597
- /*-****************************************
598
- * Error codes handling
599
- ******************************************/
600
- #ifdef ERROR
601
- # undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
602
- #endif
603
- #define ERROR(name) (size_t)-PREFIX(name)
604
-
605
- ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
606
-
607
- ERR_STATIC ERR_enum ERR_getError(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
608
-
609
-
610
- /*-****************************************
611
- * Error Strings
612
- ******************************************/
613
-
614
- ERR_STATIC const char* ERR_getErrorName(size_t code)
615
- {
616
- static const char* notErrorCode = "Unspecified error code";
617
- switch( ERR_getError(code) )
618
- {
619
- case PREFIX(no_error): return "No error detected";
620
- case PREFIX(GENERIC): return "Error (generic)";
621
- case PREFIX(prefix_unknown): return "Unknown frame descriptor";
622
- case PREFIX(frameParameter_unsupported): return "Unsupported frame parameter";
623
- case PREFIX(frameParameter_unsupportedBy32bits): return "Frame parameter unsupported in 32-bits mode";
624
- case PREFIX(init_missing): return "Context should be init first";
625
- case PREFIX(memory_allocation): return "Allocation error : not enough memory";
626
- case PREFIX(stage_wrong): return "Operation not authorized at current processing stage";
627
- case PREFIX(dstSize_tooSmall): return "Destination buffer is too small";
628
- case PREFIX(srcSize_wrong): return "Src size incorrect";
629
- case PREFIX(corruption_detected): return "Corrupted block detected";
630
- case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory";
631
- case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max possible Symbol Value : too large";
632
- case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small";
633
- case PREFIX(dictionary_corrupted): return "Dictionary is corrupted";
634
- case PREFIX(maxCode):
635
- default: return notErrorCode; /* should be impossible, due to ERR_getError() */
636
- }
637
- }
638
-
639
-
640
- #if defined (__cplusplus)
641
- }
642
- #endif
643
-
644
- #endif /* ERROR_H_MODULE */
645
395
  /*
646
396
  zstd_internal - common functions to include
647
397
  Header File for include
@@ -741,6 +491,8 @@ static const size_t ZSTDv05_frameHeaderSize_min = 5;
741
491
 
742
492
  #define WILDCOPY_OVERLENGTH 8
743
493
 
494
+ #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
495
+
744
496
  typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
745
497
 
746
498
 
@@ -753,7 +505,7 @@ static void ZSTDv05_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
753
505
 
754
506
  /*! ZSTDv05_wildcopy() :
755
507
  * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
756
- MEM_STATIC void ZSTDv05_wildcopy(void* dst, const void* src, size_t length)
508
+ MEM_STATIC void ZSTDv05_wildcopy(void* dst, const void* src, ptrdiff_t length)
757
509
  {
758
510
  const BYTE* ip = (const BYTE*)src;
759
511
  BYTE* op = (BYTE*)dst;
@@ -763,28 +515,6 @@ MEM_STATIC void ZSTDv05_wildcopy(void* dst, const void* src, size_t length)
763
515
  while (op < oend);
764
516
  }
765
517
 
766
- MEM_STATIC unsigned ZSTDv05_highbit(U32 val)
767
- {
768
- # if defined(_MSC_VER) /* Visual */
769
- unsigned long r=0;
770
- _BitScanReverse(&r, val);
771
- return (unsigned)r;
772
- # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
773
- return 31 - __builtin_clz(val);
774
- # else /* Software version */
775
- static const int DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
776
- U32 v = val;
777
- int r;
778
- v |= v >> 1;
779
- v |= v >> 2;
780
- v |= v >> 4;
781
- v |= v >> 8;
782
- v |= v >> 16;
783
- r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
784
- return r;
785
- # endif
786
- }
787
-
788
518
 
789
519
  /*-*******************************************
790
520
  * Private interfaces
@@ -916,16 +646,16 @@ void FSEv05_freeDTable(FSEv05_DTable* dt);
916
646
  /*!
917
647
  FSEv05_buildDTable():
918
648
  Builds 'dt', which must be already allocated, using FSEv05_createDTable()
919
- return : 0,
920
- or an errorCode, which can be tested using FSEv05_isError() */
649
+ @return : 0,
650
+ or an errorCode, which can be tested using FSEv05_isError() */
921
651
  size_t FSEv05_buildDTable (FSEv05_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
922
652
 
923
653
  /*!
924
654
  FSEv05_decompress_usingDTable():
925
- Decompress compressed source @cSrc of size @cSrcSize using @dt
926
- into @dst which must be already allocated.
927
- return : size of regenerated data (necessarily <= @dstCapacity)
928
- or an errorCode, which can be tested using FSEv05_isError() */
655
+ Decompress compressed source @cSrc of size @cSrcSize using `dt`
656
+ into `dst` which must be already allocated.
657
+ @return : size of regenerated data (necessarily <= @dstCapacity)
658
+ or an errorCode, which can be tested using FSEv05_isError() */
929
659
  size_t FSEv05_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSEv05_DTable* dt);
930
660
 
931
661
 
@@ -1008,18 +738,6 @@ MEM_STATIC BITv05_DStream_status BITv05_reloadDStream(BITv05_DStream_t* bitD);
1008
738
  MEM_STATIC unsigned BITv05_endOfDStream(const BITv05_DStream_t* bitD);
1009
739
 
1010
740
 
1011
- /*!
1012
- * Start by invoking BITv05_initDStream().
1013
- * A chunk of the bitStream is then stored into a local register.
1014
- * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
1015
- * You can then retrieve bitFields stored into the local register, **in reverse order**.
1016
- * Local register is explicitly reloaded from memory by the BITv05_reloadDStream() method.
1017
- * A reload guarantee a minimum of ((8*sizeof(size_t))-7) bits when its result is BITv05_DStream_unfinished.
1018
- * Otherwise, it can be less than that, so proceed accordingly.
1019
- * Checking if DStream has reached its end can be performed with BITv05_endOfDStream()
1020
- */
1021
-
1022
-
1023
741
  /*-****************************************
1024
742
  * unsafe API
1025
743
  ******************************************/
@@ -1031,14 +749,14 @@ MEM_STATIC size_t BITv05_readBitsFast(BITv05_DStream_t* bitD, unsigned nbBits);
1031
749
  /*-**************************************************************
1032
750
  * Helper functions
1033
751
  ****************************************************************/
1034
- MEM_STATIC unsigned BITv05_highbit32 (register U32 val)
752
+ MEM_STATIC unsigned BITv05_highbit32 (U32 val)
1035
753
  {
1036
754
  # if defined(_MSC_VER) /* Visual */
1037
755
  unsigned long r=0;
1038
756
  _BitScanReverse ( &r, val );
1039
757
  return (unsigned) r;
1040
758
  # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
1041
- return 31 - __builtin_clz (val);
759
+ return __builtin_clz (val) ^ 31;
1042
760
  # else /* Software version */
1043
761
  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 };
1044
762
  U32 v = val;
@@ -1084,13 +802,13 @@ MEM_STATIC size_t BITv05_initDStream(BITv05_DStream_t* bitD, const void* srcBuff
1084
802
  bitD->bitContainer = *(const BYTE*)(bitD->start);
1085
803
  switch(srcSize)
1086
804
  {
1087
- case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);
1088
- case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);
1089
- case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);
1090
- case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24;
1091
- case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16;
1092
- case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8;
1093
- default:;
805
+ case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);/* fall-through */
806
+ case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);/* fall-through */
807
+ case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);/* fall-through */
808
+ case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24; /* fall-through */
809
+ case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16; /* fall-through */
810
+ case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8; /* fall-through */
811
+ default: break;
1094
812
  }
1095
813
  contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
1096
814
  if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
@@ -1101,13 +819,6 @@ MEM_STATIC size_t BITv05_initDStream(BITv05_DStream_t* bitD, const void* srcBuff
1101
819
  return srcSize;
1102
820
  }
1103
821
 
1104
- /*!BITv05_lookBits
1105
- * Provides next n bits from local register
1106
- * local register is not modified (bits are still present for next read/look)
1107
- * On 32-bits, maxNbBits==25
1108
- * On 64-bits, maxNbBits==57
1109
- * @return : value extracted
1110
- */
1111
822
  MEM_STATIC size_t BITv05_lookBits(BITv05_DStream_t* bitD, U32 nbBits)
1112
823
  {
1113
824
  const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;
@@ -1127,12 +838,7 @@ MEM_STATIC void BITv05_skipBits(BITv05_DStream_t* bitD, U32 nbBits)
1127
838
  bitD->bitsConsumed += nbBits;
1128
839
  }
1129
840
 
1130
- /*!BITv05_readBits
1131
- * Read next n bits from local register.
1132
- * pay attention to not read more than nbBits contained into local register.
1133
- * @return : extracted value.
1134
- */
1135
- MEM_STATIC size_t BITv05_readBits(BITv05_DStream_t* bitD, U32 nbBits)
841
+ MEM_STATIC size_t BITv05_readBits(BITv05_DStream_t* bitD, unsigned nbBits)
1136
842
  {
1137
843
  size_t value = BITv05_lookBits(bitD, nbBits);
1138
844
  BITv05_skipBits(bitD, nbBits);
@@ -1141,7 +847,7 @@ MEM_STATIC size_t BITv05_readBits(BITv05_DStream_t* bitD, U32 nbBits)
1141
847
 
1142
848
  /*!BITv05_readBitsFast :
1143
849
  * unsafe version; only works only if nbBits >= 1 */
1144
- MEM_STATIC size_t BITv05_readBitsFast(BITv05_DStream_t* bitD, U32 nbBits)
850
+ MEM_STATIC size_t BITv05_readBitsFast(BITv05_DStream_t* bitD, unsigned nbBits)
1145
851
  {
1146
852
  size_t value = BITv05_lookBitsFast(bitD, nbBits);
1147
853
  BITv05_skipBits(bitD, nbBits);
@@ -1150,8 +856,8 @@ MEM_STATIC size_t BITv05_readBitsFast(BITv05_DStream_t* bitD, U32 nbBits)
1150
856
 
1151
857
  MEM_STATIC BITv05_DStream_status BITv05_reloadDStream(BITv05_DStream_t* bitD)
1152
858
  {
1153
- if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
1154
- return BITv05_DStream_overflow;
859
+ if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
860
+ return BITv05_DStream_overflow;
1155
861
 
1156
862
  if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) {
1157
863
  bitD->ptr -= bitD->bitsConsumed >> 3;
@@ -1267,54 +973,6 @@ static unsigned char FSEv05_decodeSymbol(FSEv05_DState_t* DStatePtr, BITv05_DStr
1267
973
 
1268
974
  static unsigned FSEv05_endOfDState(const FSEv05_DState_t* DStatePtr);
1269
975
 
1270
- /*!
1271
- Let's now decompose FSEv05_decompress_usingDTable() into its unitary components.
1272
- You will decode FSEv05-encoded symbols from the bitStream,
1273
- and also any other bitFields you put in, **in reverse order**.
1274
-
1275
- You will need a few variables to track your bitStream. They are :
1276
-
1277
- BITv05_DStream_t DStream; // Stream context
1278
- FSEv05_DState_t DState; // State context. Multiple ones are possible
1279
- FSEv05_DTable* DTablePtr; // Decoding table, provided by FSEv05_buildDTable()
1280
-
1281
- The first thing to do is to init the bitStream.
1282
- errorCode = BITv05_initDStream(&DStream, srcBuffer, srcSize);
1283
-
1284
- You should then retrieve your initial state(s)
1285
- (in reverse flushing order if you have several ones) :
1286
- errorCode = FSEv05_initDState(&DState, &DStream, DTablePtr);
1287
-
1288
- You can then decode your data, symbol after symbol.
1289
- For information the maximum number of bits read by FSEv05_decodeSymbol() is 'tableLog'.
1290
- Keep in mind that symbols are decoded in reverse order, like a LIFO stack (last in, first out).
1291
- unsigned char symbol = FSEv05_decodeSymbol(&DState, &DStream);
1292
-
1293
- You can retrieve any bitfield you eventually stored into the bitStream (in reverse order)
1294
- Note : maximum allowed nbBits is 25, for 32-bits compatibility
1295
- size_t bitField = BITv05_readBits(&DStream, nbBits);
1296
-
1297
- All above operations only read from local register (which size depends on size_t).
1298
- Refueling the register from memory is manually performed by the reload method.
1299
- endSignal = FSEv05_reloadDStream(&DStream);
1300
-
1301
- BITv05_reloadDStream() result tells if there is still some more data to read from DStream.
1302
- BITv05_DStream_unfinished : there is still some data left into the DStream.
1303
- BITv05_DStream_endOfBuffer : Dstream reached end of buffer. Its container may no longer be completely filled.
1304
- BITv05_DStream_completed : Dstream reached its exact end, corresponding in general to decompression completed.
1305
- BITv05_DStream_tooFar : Dstream went too far. Decompression result is corrupted.
1306
-
1307
- When reaching end of buffer (BITv05_DStream_endOfBuffer), progress slowly, notably if you decode multiple symbols per loop,
1308
- to properly detect the exact end of stream.
1309
- After each decoded symbol, check if DStream is fully consumed using this simple test :
1310
- BITv05_reloadDStream(&DStream) >= BITv05_DStream_completed
1311
-
1312
- When it's done, verify decompression is fully completed, by checking both DStream and the relevant states.
1313
- Checking if DStream has reached its end is performed by :
1314
- BITv05_endOfDStream(&DStream);
1315
- Check also the states. There might be some symbols left there, if some high probability ones (>50%) are possible.
1316
- FSEv05_endOfDState(&DState);
1317
- */
1318
976
 
1319
977
 
1320
978
  /* *****************************************
@@ -1350,11 +1008,6 @@ MEM_STATIC void FSEv05_initDState(FSEv05_DState_t* DStatePtr, BITv05_DStream_t*
1350
1008
  DStatePtr->table = dt + 1;
1351
1009
  }
1352
1010
 
1353
- MEM_STATIC size_t FSEv05_getStateValue(FSEv05_DState_t* DStatePtr)
1354
- {
1355
- return DStatePtr->state;
1356
- }
1357
-
1358
1011
  MEM_STATIC BYTE FSEv05_peakSymbol(FSEv05_DState_t* DStatePtr)
1359
1012
  {
1360
1013
  const FSEv05_decode_t DInfo = ((const FSEv05_decode_t*)(DStatePtr->table))[DStatePtr->state];
@@ -1466,12 +1119,15 @@ MEM_STATIC unsigned FSEv05_endOfDState(const FSEv05_DState_t* DStatePtr)
1466
1119
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
1467
1120
  # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
1468
1121
  #else
1469
- # ifdef __GNUC__
1470
- # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
1471
- # define FORCE_INLINE static inline __attribute__((always_inline))
1122
+ # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
1123
+ # ifdef __GNUC__
1124
+ # define FORCE_INLINE static inline __attribute__((always_inline))
1125
+ # else
1126
+ # define FORCE_INLINE static inline
1127
+ # endif
1472
1128
  # else
1473
- # define FORCE_INLINE static inline
1474
- # endif
1129
+ # define FORCE_INLINE static
1130
+ # endif /* __STDC_VERSION__ */
1475
1131
  #endif
1476
1132
 
1477
1133
 
@@ -1508,7 +1164,7 @@ MEM_STATIC unsigned FSEv05_endOfDState(const FSEv05_DState_t* DStatePtr)
1508
1164
  /* **************************************************************
1509
1165
  * Complex types
1510
1166
  ****************************************************************/
1511
- typedef U32 DTable_max_t[FSEv05_DTABLE_SIZE_U32(FSEv05_MAX_TABLELOG)];
1167
+ typedef unsigned DTable_max_t[FSEv05_DTABLE_SIZE_U32(FSEv05_MAX_TABLELOG)];
1512
1168
 
1513
1169
 
1514
1170
  /* **************************************************************
@@ -1570,6 +1226,7 @@ size_t FSEv05_buildDTable(FSEv05_DTable* dt, const short* normalizedCounter, uns
1570
1226
  if (tableLog > FSEv05_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
1571
1227
 
1572
1228
  /* Init, lay down lowprob symbols */
1229
+ memset(tableDecode, 0, sizeof(FSEv05_FUNCTION_TYPE) * (maxSymbolValue+1) ); /* useless init, but keep static analyzer happy, and we don't need to performance optimize legacy decoders */
1573
1230
  DTableH.tableLog = (U16)tableLog;
1574
1231
  for (s=0; s<=maxSymbolValue; s++) {
1575
1232
  if (normalizedCounter[s]==-1) {
@@ -2088,14 +1745,7 @@ size_t HUFv05_decompress1X4_usingDTable(void* dst, size_t maxDstSize, const void
2088
1745
 
2089
1746
 
2090
1747
  #ifdef _MSC_VER /* Visual Studio */
2091
- # define FORCE_INLINE static __forceinline
2092
1748
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2093
- #else
2094
- # ifdef __GNUC__
2095
- # define FORCE_INLINE static inline __attribute__((always_inline))
2096
- # else
2097
- # define FORCE_INLINE static inline
2098
- # endif
2099
1749
  #endif
2100
1750
 
2101
1751
 
@@ -2148,11 +1798,13 @@ static size_t HUFv05_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
2148
1798
  U32 weightTotal;
2149
1799
  U32 tableLog;
2150
1800
  const BYTE* ip = (const BYTE*) src;
2151
- size_t iSize = ip[0];
1801
+ size_t iSize;
2152
1802
  size_t oSize;
2153
1803
  U32 n;
2154
1804
 
2155
- //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
1805
+ if (!srcSize) return ERROR(srcSize_wrong);
1806
+ iSize = ip[0];
1807
+ /* memset(huffWeight, 0, hwSize); */ /* is not necessary, even though some analyzer complain ... */
2156
1808
 
2157
1809
  if (iSize >= 128) { /* special header */
2158
1810
  if (iSize >= (242)) { /* RLE */
@@ -2185,6 +1837,7 @@ static size_t HUFv05_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
2185
1837
  rankStats[huffWeight[n]]++;
2186
1838
  weightTotal += (1 << huffWeight[n]) >> 1;
2187
1839
  }
1840
+ if (weightTotal == 0) return ERROR(corruption_detected);
2188
1841
 
2189
1842
  /* get last non-null symbol weight (implied, total must be 2^n) */
2190
1843
  tableLog = BITv05_highbit32(weightTotal) + 1;
@@ -2226,7 +1879,7 @@ size_t HUFv05_readDTableX2 (U16* DTable, const void* src, size_t srcSize)
2226
1879
  HUFv05_DEltX2* const dt = (HUFv05_DEltX2*)dtPtr;
2227
1880
 
2228
1881
  HUFv05_STATIC_ASSERT(sizeof(HUFv05_DEltX2) == sizeof(U16)); /* if compilation fails here, assertion is false */
2229
- //memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */
1882
+ /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
2230
1883
 
2231
1884
  iSize = HUFv05_readStats(huffWeight, HUFv05_MAX_SYMBOL_VALUE + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
2232
1885
  if (HUFv05_isError(iSize)) return iSize;
@@ -2307,13 +1960,14 @@ size_t HUFv05_decompress1X2_usingDTable(
2307
1960
  {
2308
1961
  BYTE* op = (BYTE*)dst;
2309
1962
  BYTE* const oend = op + dstSize;
2310
- size_t errorCode;
2311
1963
  const U32 dtLog = DTable[0];
2312
1964
  const void* dtPtr = DTable;
2313
1965
  const HUFv05_DEltX2* const dt = ((const HUFv05_DEltX2*)dtPtr)+1;
2314
1966
  BITv05_DStream_t bitD;
2315
- errorCode = BITv05_initDStream(&bitD, cSrc, cSrcSize);
2316
- if (HUFv05_isError(errorCode)) return errorCode;
1967
+
1968
+ if (dstSize <= cSrcSize) return ERROR(dstSize_tooSmall);
1969
+ { size_t const errorCode = BITv05_initDStream(&bitD, cSrc, cSrcSize);
1970
+ if (HUFv05_isError(errorCode)) return errorCode; }
2317
1971
 
2318
1972
  HUFv05_decodeStreamX2(op, &bitD, oend, dt, dtLog);
2319
1973
 
@@ -2344,91 +1998,92 @@ size_t HUFv05_decompress4X2_usingDTable(
2344
1998
  const void* cSrc, size_t cSrcSize,
2345
1999
  const U16* DTable)
2346
2000
  {
2347
- const BYTE* const istart = (const BYTE*) cSrc;
2348
- BYTE* const ostart = (BYTE*) dst;
2349
- BYTE* const oend = ostart + dstSize;
2350
- const void* const dtPtr = DTable;
2351
- const HUFv05_DEltX2* const dt = ((const HUFv05_DEltX2*)dtPtr) +1;
2352
- const U32 dtLog = DTable[0];
2353
- size_t errorCode;
2354
-
2355
- /* Init */
2356
- BITv05_DStream_t bitD1;
2357
- BITv05_DStream_t bitD2;
2358
- BITv05_DStream_t bitD3;
2359
- BITv05_DStream_t bitD4;
2360
- const size_t length1 = MEM_readLE16(istart);
2361
- const size_t length2 = MEM_readLE16(istart+2);
2362
- const size_t length3 = MEM_readLE16(istart+4);
2363
- size_t length4;
2364
- const BYTE* const istart1 = istart + 6; /* jumpTable */
2365
- const BYTE* const istart2 = istart1 + length1;
2366
- const BYTE* const istart3 = istart2 + length2;
2367
- const BYTE* const istart4 = istart3 + length3;
2368
- const size_t segmentSize = (dstSize+3) / 4;
2369
- BYTE* const opStart2 = ostart + segmentSize;
2370
- BYTE* const opStart3 = opStart2 + segmentSize;
2371
- BYTE* const opStart4 = opStart3 + segmentSize;
2372
- BYTE* op1 = ostart;
2373
- BYTE* op2 = opStart2;
2374
- BYTE* op3 = opStart3;
2375
- BYTE* op4 = opStart4;
2376
- U32 endSignal;
2377
-
2378
2001
  /* Check */
2379
2002
  if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
2003
+ {
2004
+ const BYTE* const istart = (const BYTE*) cSrc;
2005
+ BYTE* const ostart = (BYTE*) dst;
2006
+ BYTE* const oend = ostart + dstSize;
2007
+ const void* const dtPtr = DTable;
2008
+ const HUFv05_DEltX2* const dt = ((const HUFv05_DEltX2*)dtPtr) +1;
2009
+ const U32 dtLog = DTable[0];
2010
+ size_t errorCode;
2380
2011
 
2381
- length4 = cSrcSize - (length1 + length2 + length3 + 6);
2382
- if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
2383
- errorCode = BITv05_initDStream(&bitD1, istart1, length1);
2384
- if (HUFv05_isError(errorCode)) return errorCode;
2385
- errorCode = BITv05_initDStream(&bitD2, istart2, length2);
2386
- if (HUFv05_isError(errorCode)) return errorCode;
2387
- errorCode = BITv05_initDStream(&bitD3, istart3, length3);
2388
- if (HUFv05_isError(errorCode)) return errorCode;
2389
- errorCode = BITv05_initDStream(&bitD4, istart4, length4);
2390
- if (HUFv05_isError(errorCode)) return errorCode;
2012
+ /* Init */
2013
+ BITv05_DStream_t bitD1;
2014
+ BITv05_DStream_t bitD2;
2015
+ BITv05_DStream_t bitD3;
2016
+ BITv05_DStream_t bitD4;
2017
+ const size_t length1 = MEM_readLE16(istart);
2018
+ const size_t length2 = MEM_readLE16(istart+2);
2019
+ const size_t length3 = MEM_readLE16(istart+4);
2020
+ size_t length4;
2021
+ const BYTE* const istart1 = istart + 6; /* jumpTable */
2022
+ const BYTE* const istart2 = istart1 + length1;
2023
+ const BYTE* const istart3 = istart2 + length2;
2024
+ const BYTE* const istart4 = istart3 + length3;
2025
+ const size_t segmentSize = (dstSize+3) / 4;
2026
+ BYTE* const opStart2 = ostart + segmentSize;
2027
+ BYTE* const opStart3 = opStart2 + segmentSize;
2028
+ BYTE* const opStart4 = opStart3 + segmentSize;
2029
+ BYTE* op1 = ostart;
2030
+ BYTE* op2 = opStart2;
2031
+ BYTE* op3 = opStart3;
2032
+ BYTE* op4 = opStart4;
2033
+ U32 endSignal;
2391
2034
 
2392
- /* 16-32 symbols per loop (4-8 symbols per stream) */
2393
- endSignal = BITv05_reloadDStream(&bitD1) | BITv05_reloadDStream(&bitD2) | BITv05_reloadDStream(&bitD3) | BITv05_reloadDStream(&bitD4);
2394
- for ( ; (endSignal==BITv05_DStream_unfinished) && (op4<(oend-7)) ; ) {
2395
- HUFv05_DECODE_SYMBOLX2_2(op1, &bitD1);
2396
- HUFv05_DECODE_SYMBOLX2_2(op2, &bitD2);
2397
- HUFv05_DECODE_SYMBOLX2_2(op3, &bitD3);
2398
- HUFv05_DECODE_SYMBOLX2_2(op4, &bitD4);
2399
- HUFv05_DECODE_SYMBOLX2_1(op1, &bitD1);
2400
- HUFv05_DECODE_SYMBOLX2_1(op2, &bitD2);
2401
- HUFv05_DECODE_SYMBOLX2_1(op3, &bitD3);
2402
- HUFv05_DECODE_SYMBOLX2_1(op4, &bitD4);
2403
- HUFv05_DECODE_SYMBOLX2_2(op1, &bitD1);
2404
- HUFv05_DECODE_SYMBOLX2_2(op2, &bitD2);
2405
- HUFv05_DECODE_SYMBOLX2_2(op3, &bitD3);
2406
- HUFv05_DECODE_SYMBOLX2_2(op4, &bitD4);
2407
- HUFv05_DECODE_SYMBOLX2_0(op1, &bitD1);
2408
- HUFv05_DECODE_SYMBOLX2_0(op2, &bitD2);
2409
- HUFv05_DECODE_SYMBOLX2_0(op3, &bitD3);
2410
- HUFv05_DECODE_SYMBOLX2_0(op4, &bitD4);
2035
+ length4 = cSrcSize - (length1 + length2 + length3 + 6);
2036
+ if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
2037
+ errorCode = BITv05_initDStream(&bitD1, istart1, length1);
2038
+ if (HUFv05_isError(errorCode)) return errorCode;
2039
+ errorCode = BITv05_initDStream(&bitD2, istart2, length2);
2040
+ if (HUFv05_isError(errorCode)) return errorCode;
2041
+ errorCode = BITv05_initDStream(&bitD3, istart3, length3);
2042
+ if (HUFv05_isError(errorCode)) return errorCode;
2043
+ errorCode = BITv05_initDStream(&bitD4, istart4, length4);
2044
+ if (HUFv05_isError(errorCode)) return errorCode;
2045
+
2046
+ /* 16-32 symbols per loop (4-8 symbols per stream) */
2411
2047
  endSignal = BITv05_reloadDStream(&bitD1) | BITv05_reloadDStream(&bitD2) | BITv05_reloadDStream(&bitD3) | BITv05_reloadDStream(&bitD4);
2412
- }
2048
+ for ( ; (endSignal==BITv05_DStream_unfinished) && (op4<(oend-7)) ; ) {
2049
+ HUFv05_DECODE_SYMBOLX2_2(op1, &bitD1);
2050
+ HUFv05_DECODE_SYMBOLX2_2(op2, &bitD2);
2051
+ HUFv05_DECODE_SYMBOLX2_2(op3, &bitD3);
2052
+ HUFv05_DECODE_SYMBOLX2_2(op4, &bitD4);
2053
+ HUFv05_DECODE_SYMBOLX2_1(op1, &bitD1);
2054
+ HUFv05_DECODE_SYMBOLX2_1(op2, &bitD2);
2055
+ HUFv05_DECODE_SYMBOLX2_1(op3, &bitD3);
2056
+ HUFv05_DECODE_SYMBOLX2_1(op4, &bitD4);
2057
+ HUFv05_DECODE_SYMBOLX2_2(op1, &bitD1);
2058
+ HUFv05_DECODE_SYMBOLX2_2(op2, &bitD2);
2059
+ HUFv05_DECODE_SYMBOLX2_2(op3, &bitD3);
2060
+ HUFv05_DECODE_SYMBOLX2_2(op4, &bitD4);
2061
+ HUFv05_DECODE_SYMBOLX2_0(op1, &bitD1);
2062
+ HUFv05_DECODE_SYMBOLX2_0(op2, &bitD2);
2063
+ HUFv05_DECODE_SYMBOLX2_0(op3, &bitD3);
2064
+ HUFv05_DECODE_SYMBOLX2_0(op4, &bitD4);
2065
+ endSignal = BITv05_reloadDStream(&bitD1) | BITv05_reloadDStream(&bitD2) | BITv05_reloadDStream(&bitD3) | BITv05_reloadDStream(&bitD4);
2066
+ }
2413
2067
 
2414
- /* check corruption */
2415
- if (op1 > opStart2) return ERROR(corruption_detected);
2416
- if (op2 > opStart3) return ERROR(corruption_detected);
2417
- if (op3 > opStart4) return ERROR(corruption_detected);
2418
- /* note : op4 supposed already verified within main loop */
2068
+ /* check corruption */
2069
+ if (op1 > opStart2) return ERROR(corruption_detected);
2070
+ if (op2 > opStart3) return ERROR(corruption_detected);
2071
+ if (op3 > opStart4) return ERROR(corruption_detected);
2072
+ /* note : op4 supposed already verified within main loop */
2419
2073
 
2420
- /* finish bitStreams one by one */
2421
- HUFv05_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
2422
- HUFv05_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
2423
- HUFv05_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
2424
- HUFv05_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
2074
+ /* finish bitStreams one by one */
2075
+ HUFv05_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
2076
+ HUFv05_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
2077
+ HUFv05_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
2078
+ HUFv05_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
2425
2079
 
2426
- /* check */
2427
- endSignal = BITv05_endOfDStream(&bitD1) & BITv05_endOfDStream(&bitD2) & BITv05_endOfDStream(&bitD3) & BITv05_endOfDStream(&bitD4);
2428
- if (!endSignal) return ERROR(corruption_detected);
2080
+ /* check */
2081
+ endSignal = BITv05_endOfDStream(&bitD1) & BITv05_endOfDStream(&bitD2) & BITv05_endOfDStream(&bitD3) & BITv05_endOfDStream(&bitD4);
2082
+ if (!endSignal) return ERROR(corruption_detected);
2429
2083
 
2430
- /* decoded size */
2431
- return dstSize;
2084
+ /* decoded size */
2085
+ return dstSize;
2086
+ }
2432
2087
  }
2433
2088
 
2434
2089
 
@@ -2539,7 +2194,7 @@ static void HUFv05_fillDTableX4(HUFv05_DEltX4* DTable, const U32 targetLog,
2539
2194
  }
2540
2195
  }
2541
2196
 
2542
- size_t HUFv05_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
2197
+ size_t HUFv05_readDTableX4 (unsigned* DTable, const void* src, size_t srcSize)
2543
2198
  {
2544
2199
  BYTE weightList[HUFv05_MAX_SYMBOL_VALUE + 1];
2545
2200
  sortedSymbol_t sortedSymbol[HUFv05_MAX_SYMBOL_VALUE + 1];
@@ -2553,9 +2208,9 @@ size_t HUFv05_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
2553
2208
  void* dtPtr = DTable;
2554
2209
  HUFv05_DEltX4* const dt = ((HUFv05_DEltX4*)dtPtr) + 1;
2555
2210
 
2556
- HUFv05_STATIC_ASSERT(sizeof(HUFv05_DEltX4) == sizeof(U32)); /* if compilation fails here, assertion is false */
2211
+ HUFv05_STATIC_ASSERT(sizeof(HUFv05_DEltX4) == sizeof(unsigned)); /* if compilation fails here, assertion is false */
2557
2212
  if (memLog > HUFv05_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
2558
- //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */
2213
+ /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
2559
2214
 
2560
2215
  iSize = HUFv05_readStats(weightList, HUFv05_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
2561
2216
  if (HUFv05_isError(iSize)) return iSize;
@@ -2680,7 +2335,7 @@ static inline size_t HUFv05_decodeStreamX4(BYTE* p, BITv05_DStream_t* bitDPtr, B
2680
2335
  size_t HUFv05_decompress1X4_usingDTable(
2681
2336
  void* dst, size_t dstSize,
2682
2337
  const void* cSrc, size_t cSrcSize,
2683
- const U32* DTable)
2338
+ const unsigned* DTable)
2684
2339
  {
2685
2340
  const BYTE* const istart = (const BYTE*) cSrc;
2686
2341
  BYTE* const ostart = (BYTE*) dst;
@@ -2723,7 +2378,7 @@ size_t HUFv05_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t
2723
2378
  size_t HUFv05_decompress4X4_usingDTable(
2724
2379
  void* dst, size_t dstSize,
2725
2380
  const void* cSrc, size_t cSrcSize,
2726
- const U32* DTable)
2381
+ const unsigned* DTable)
2727
2382
  {
2728
2383
  if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
2729
2384
 
@@ -2884,9 +2539,9 @@ size_t HUFv05_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cS
2884
2539
 
2885
2540
  return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
2886
2541
 
2887
- //return HUFv05_decompress4X2(dst, dstSize, cSrc, cSrcSize); /* multi-streams single-symbol decoding */
2888
- //return HUFv05_decompress4X4(dst, dstSize, cSrc, cSrcSize); /* multi-streams double-symbols decoding */
2889
- //return HUFv05_decompress4X6(dst, dstSize, cSrc, cSrcSize); /* multi-streams quad-symbols decoding */
2542
+ /* return HUFv05_decompress4X2(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams single-symbol decoding */
2543
+ /* return HUFv05_decompress4X4(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams double-symbols decoding */
2544
+ /* return HUFv05_decompress4X6(dst, dstSize, cSrc, cSrcSize); */ /* multi-streams quad-symbols decoding */
2890
2545
  }
2891
2546
  /*
2892
2547
  zstd - standard compression library
@@ -2944,17 +2599,9 @@ size_t HUFv05_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cS
2944
2599
  * Compiler specifics
2945
2600
  *********************************************************/
2946
2601
  #ifdef _MSC_VER /* Visual Studio */
2947
- # define FORCE_INLINE static __forceinline
2948
2602
  # include <intrin.h> /* For Visual 2005 */
2949
2603
  # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2950
2604
  # pragma warning(disable : 4324) /* disable: C4324: padded structure */
2951
- #else
2952
- # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
2953
- # ifdef __GNUC__
2954
- # define FORCE_INLINE static inline __attribute__((always_inline))
2955
- # else
2956
- # define FORCE_INLINE static inline
2957
- # endif
2958
2605
  #endif
2959
2606
 
2960
2607
 
@@ -2981,9 +2628,6 @@ static void ZSTDv05_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }
2981
2628
  * tells if a return value is an error code */
2982
2629
  unsigned ZSTDv05_isError(size_t code) { return ERR_isError(code); }
2983
2630
 
2984
- /*! ZSTDv05_getError() :
2985
- * convert a `size_t` function result into a proper ZSTDv05_errorCode enum */
2986
- ZSTDv05_ErrorCode ZSTDv05_getError(size_t code) { return ERR_getError(code); }
2987
2631
 
2988
2632
  /*! ZSTDv05_getErrorName() :
2989
2633
  * provides error code string (useful for debugging) */
@@ -3013,12 +2657,12 @@ struct ZSTDv05_DCtx_s
3013
2657
  ZSTDv05_dStage stage;
3014
2658
  U32 flagStaticTables;
3015
2659
  const BYTE* litPtr;
3016
- size_t litBufSize;
3017
2660
  size_t litSize;
3018
2661
  BYTE litBuffer[BLOCKSIZE + WILDCOPY_OVERLENGTH];
3019
2662
  BYTE headerBuffer[ZSTDv05_frameHeaderSize_max];
3020
2663
  }; /* typedef'd to ZSTDv05_DCtx within "zstd_static.h" */
3021
2664
 
2665
+ size_t ZSTDv05_sizeofDCtx (void); /* Hidden declaration */
3022
2666
  size_t ZSTDv05_sizeofDCtx (void) { return sizeof(ZSTDv05_DCtx); }
3023
2667
 
3024
2668
  size_t ZSTDv05_decompressBegin(ZSTDv05_DCtx* dctx)
@@ -3178,12 +2822,12 @@ static size_t ZSTDv05_decodeFrameHeader_Part2(ZSTDv05_DCtx* zc, const void* src,
3178
2822
  if (srcSize != zc->headerSize)
3179
2823
  return ERROR(srcSize_wrong);
3180
2824
  result = ZSTDv05_getFrameParams(&(zc->params), src, srcSize);
3181
- if ((MEM_32bits()) && (zc->params.windowLog > 25)) return ERROR(frameParameter_unsupportedBy32bits);
2825
+ if ((MEM_32bits()) && (zc->params.windowLog > 25)) return ERROR(frameParameter_unsupported);
3182
2826
  return result;
3183
2827
  }
3184
2828
 
3185
2829
 
3186
- size_t ZSTDv05_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
2830
+ static size_t ZSTDv05_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)
3187
2831
  {
3188
2832
  const BYTE* const in = (const BYTE* const)src;
3189
2833
  BYTE headerFlags;
@@ -3206,6 +2850,7 @@ size_t ZSTDv05_getcBlockSize(const void* src, size_t srcSize, blockProperties_t*
3206
2850
 
3207
2851
  static size_t ZSTDv05_copyRawBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
3208
2852
  {
2853
+ if (dst==NULL) return ERROR(dstSize_tooSmall);
3209
2854
  if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);
3210
2855
  memcpy(dst, src, srcSize);
3211
2856
  return srcSize;
@@ -3214,8 +2859,8 @@ static size_t ZSTDv05_copyRawBlock(void* dst, size_t maxDstSize, const void* src
3214
2859
 
3215
2860
  /*! ZSTDv05_decodeLiteralsBlock() :
3216
2861
  @return : nb of bytes read from src (< srcSize ) */
3217
- size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3218
- const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
2862
+ static size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
2863
+ const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */
3219
2864
  {
3220
2865
  const BYTE* const istart = (const BYTE*) src;
3221
2866
 
@@ -3228,6 +2873,7 @@ size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3228
2873
  {
3229
2874
  size_t litSize, litCSize, singleStream=0;
3230
2875
  U32 lhSize = ((istart[0]) >> 4) & 3;
2876
+ if (srcSize < 5) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3 */
3231
2877
  switch(lhSize)
3232
2878
  {
3233
2879
  case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */
@@ -3251,6 +2897,7 @@ size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3251
2897
  break;
3252
2898
  }
3253
2899
  if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
2900
+ if (litCSize + lhSize > srcSize) return ERROR(corruption_detected);
3254
2901
 
3255
2902
  if (HUFv05_isError(singleStream ?
3256
2903
  HUFv05_decompress1X2(dctx->litBuffer, litSize, istart+lhSize, litCSize) :
@@ -3258,8 +2905,8 @@ size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3258
2905
  return ERROR(corruption_detected);
3259
2906
 
3260
2907
  dctx->litPtr = dctx->litBuffer;
3261
- dctx->litBufSize = BLOCKSIZE+8;
3262
2908
  dctx->litSize = litSize;
2909
+ memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
3263
2910
  return litCSize + lhSize;
3264
2911
  }
3265
2912
  case IS_PCH:
@@ -3276,13 +2923,14 @@ size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3276
2923
  lhSize=3;
3277
2924
  litSize = ((istart[0] & 15) << 6) + (istart[1] >> 2);
3278
2925
  litCSize = ((istart[1] & 3) << 8) + istart[2];
2926
+ if (litCSize + lhSize > srcSize) return ERROR(corruption_detected);
3279
2927
 
3280
2928
  errorCode = HUFv05_decompress1X4_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->hufTableX4);
3281
2929
  if (HUFv05_isError(errorCode)) return ERROR(corruption_detected);
3282
2930
 
3283
2931
  dctx->litPtr = dctx->litBuffer;
3284
- dctx->litBufSize = BLOCKSIZE+WILDCOPY_OVERLENGTH;
3285
2932
  dctx->litSize = litSize;
2933
+ memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
3286
2934
  return litCSize + lhSize;
3287
2935
  }
3288
2936
  case IS_RAW:
@@ -3307,13 +2955,12 @@ size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3307
2955
  if (litSize+lhSize > srcSize) return ERROR(corruption_detected);
3308
2956
  memcpy(dctx->litBuffer, istart+lhSize, litSize);
3309
2957
  dctx->litPtr = dctx->litBuffer;
3310
- dctx->litBufSize = BLOCKSIZE+8;
3311
2958
  dctx->litSize = litSize;
2959
+ memset(dctx->litBuffer + dctx->litSize, 0, WILDCOPY_OVERLENGTH);
3312
2960
  return lhSize+litSize;
3313
2961
  }
3314
2962
  /* direct reference into compressed stream */
3315
2963
  dctx->litPtr = istart+lhSize;
3316
- dctx->litBufSize = srcSize-lhSize;
3317
2964
  dctx->litSize = litSize;
3318
2965
  return lhSize+litSize;
3319
2966
  }
@@ -3332,12 +2979,12 @@ size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3332
2979
  break;
3333
2980
  case 3:
3334
2981
  litSize = ((istart[0] & 15) << 16) + (istart[1] << 8) + istart[2];
2982
+ if (srcSize<4) return ERROR(corruption_detected); /* srcSize >= MIN_CBLOCK_SIZE == 3; here we need lhSize+1 = 4 */
3335
2983
  break;
3336
2984
  }
3337
2985
  if (litSize > BLOCKSIZE) return ERROR(corruption_detected);
3338
- memset(dctx->litBuffer, istart[lhSize], litSize);
2986
+ memset(dctx->litBuffer, istart[lhSize], litSize + WILDCOPY_OVERLENGTH);
3339
2987
  dctx->litPtr = dctx->litBuffer;
3340
- dctx->litBufSize = BLOCKSIZE+WILDCOPY_OVERLENGTH;
3341
2988
  dctx->litSize = litSize;
3342
2989
  return lhSize+1;
3343
2990
  }
@@ -3347,15 +2994,15 @@ size_t ZSTDv05_decodeLiteralsBlock(ZSTDv05_DCtx* dctx,
3347
2994
  }
3348
2995
 
3349
2996
 
3350
- size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr,
2997
+ static size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr,
3351
2998
  FSEv05_DTable* DTableLL, FSEv05_DTable* DTableML, FSEv05_DTable* DTableOffb,
3352
- const void* src, size_t srcSize)
2999
+ const void* src, size_t srcSize, U32 flagStaticTable)
3353
3000
  {
3354
3001
  const BYTE* const istart = (const BYTE* const)src;
3355
3002
  const BYTE* ip = istart;
3356
3003
  const BYTE* const iend = istart + srcSize;
3357
3004
  U32 LLtype, Offtype, MLtype;
3358
- U32 LLlog, Offlog, MLlog;
3005
+ unsigned LLlog, Offlog, MLlog;
3359
3006
  size_t dumpsLength;
3360
3007
 
3361
3008
  /* check */
@@ -3365,17 +3012,22 @@ size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumps
3365
3012
  /* SeqHead */
3366
3013
  *nbSeq = *ip++;
3367
3014
  if (*nbSeq==0) return 1;
3368
- if (*nbSeq >= 128)
3015
+ if (*nbSeq >= 128) {
3016
+ if (ip >= iend) return ERROR(srcSize_wrong);
3369
3017
  *nbSeq = ((nbSeq[0]-128)<<8) + *ip++;
3018
+ }
3370
3019
 
3020
+ if (ip >= iend) return ERROR(srcSize_wrong);
3371
3021
  LLtype = *ip >> 6;
3372
3022
  Offtype = (*ip >> 4) & 3;
3373
3023
  MLtype = (*ip >> 2) & 3;
3374
3024
  if (*ip & 2) {
3025
+ if (ip+3 > iend) return ERROR(srcSize_wrong);
3375
3026
  dumpsLength = ip[2];
3376
3027
  dumpsLength += ip[1] << 8;
3377
3028
  ip += 3;
3378
3029
  } else {
3030
+ if (ip+2 > iend) return ERROR(srcSize_wrong);
3379
3031
  dumpsLength = ip[1];
3380
3032
  dumpsLength += (ip[0] & 1) << 8;
3381
3033
  ip += 2;
@@ -3395,7 +3047,6 @@ size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumps
3395
3047
  /* Build DTables */
3396
3048
  switch(LLtype)
3397
3049
  {
3398
- U32 max;
3399
3050
  case FSEv05_ENCODING_RLE :
3400
3051
  LLlog = 0;
3401
3052
  FSEv05_buildDTable_rle(DTableLL, *ip++);
@@ -3405,20 +3056,20 @@ size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumps
3405
3056
  FSEv05_buildDTable_raw(DTableLL, LLbits);
3406
3057
  break;
3407
3058
  case FSEv05_ENCODING_STATIC:
3059
+ if (!flagStaticTable) return ERROR(corruption_detected);
3408
3060
  break;
3409
3061
  case FSEv05_ENCODING_DYNAMIC :
3410
3062
  default : /* impossible */
3411
- max = MaxLL;
3412
- headerSize = FSEv05_readNCount(norm, &max, &LLlog, ip, iend-ip);
3413
- if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3414
- if (LLlog > LLFSEv05Log) return ERROR(corruption_detected);
3415
- ip += headerSize;
3416
- FSEv05_buildDTable(DTableLL, norm, max, LLlog);
3417
- }
3063
+ { unsigned max = MaxLL;
3064
+ headerSize = FSEv05_readNCount(norm, &max, &LLlog, ip, iend-ip);
3065
+ if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3066
+ if (LLlog > LLFSEv05Log) return ERROR(corruption_detected);
3067
+ ip += headerSize;
3068
+ FSEv05_buildDTable(DTableLL, norm, max, LLlog);
3069
+ } }
3418
3070
 
3419
3071
  switch(Offtype)
3420
3072
  {
3421
- U32 max;
3422
3073
  case FSEv05_ENCODING_RLE :
3423
3074
  Offlog = 0;
3424
3075
  if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
@@ -3429,20 +3080,20 @@ size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumps
3429
3080
  FSEv05_buildDTable_raw(DTableOffb, Offbits);
3430
3081
  break;
3431
3082
  case FSEv05_ENCODING_STATIC:
3083
+ if (!flagStaticTable) return ERROR(corruption_detected);
3432
3084
  break;
3433
3085
  case FSEv05_ENCODING_DYNAMIC :
3434
3086
  default : /* impossible */
3435
- max = MaxOff;
3436
- headerSize = FSEv05_readNCount(norm, &max, &Offlog, ip, iend-ip);
3437
- if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3438
- if (Offlog > OffFSEv05Log) return ERROR(corruption_detected);
3439
- ip += headerSize;
3440
- FSEv05_buildDTable(DTableOffb, norm, max, Offlog);
3441
- }
3087
+ { unsigned max = MaxOff;
3088
+ headerSize = FSEv05_readNCount(norm, &max, &Offlog, ip, iend-ip);
3089
+ if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3090
+ if (Offlog > OffFSEv05Log) return ERROR(corruption_detected);
3091
+ ip += headerSize;
3092
+ FSEv05_buildDTable(DTableOffb, norm, max, Offlog);
3093
+ } }
3442
3094
 
3443
3095
  switch(MLtype)
3444
3096
  {
3445
- U32 max;
3446
3097
  case FSEv05_ENCODING_RLE :
3447
3098
  MLlog = 0;
3448
3099
  if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */
@@ -3453,16 +3104,17 @@ size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumps
3453
3104
  FSEv05_buildDTable_raw(DTableML, MLbits);
3454
3105
  break;
3455
3106
  case FSEv05_ENCODING_STATIC:
3107
+ if (!flagStaticTable) return ERROR(corruption_detected);
3456
3108
  break;
3457
3109
  case FSEv05_ENCODING_DYNAMIC :
3458
3110
  default : /* impossible */
3459
- max = MaxML;
3460
- headerSize = FSEv05_readNCount(norm, &max, &MLlog, ip, iend-ip);
3461
- if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3462
- if (MLlog > MLFSEv05Log) return ERROR(corruption_detected);
3463
- ip += headerSize;
3464
- FSEv05_buildDTable(DTableML, norm, max, MLlog);
3465
- } }
3111
+ { unsigned max = MaxML;
3112
+ headerSize = FSEv05_readNCount(norm, &max, &MLlog, ip, iend-ip);
3113
+ if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3114
+ if (MLlog > MLFSEv05Log) return ERROR(corruption_detected);
3115
+ ip += headerSize;
3116
+ FSEv05_buildDTable(DTableML, norm, max, MLlog);
3117
+ } } }
3466
3118
 
3467
3119
  return ip-istart;
3468
3120
  }
@@ -3499,14 +3151,18 @@ static void ZSTDv05_decodeSequence(seq_t* seq, seqState_t* seqState)
3499
3151
  litLength = FSEv05_peakSymbol(&(seqState->stateLL));
3500
3152
  prevOffset = litLength ? seq->offset : seqState->prevOffset;
3501
3153
  if (litLength == MaxLL) {
3502
- U32 add = *dumps++;
3154
+ const U32 add = *dumps++;
3503
3155
  if (add < 255) litLength += add;
3504
- else {
3505
- litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no risk : dumps is always followed by seq tables > 1 byte */
3506
- if (litLength&1) litLength>>=1, dumps += 3;
3507
- else litLength = (U16)(litLength)>>1, dumps += 2;
3156
+ else if (dumps + 2 <= de) {
3157
+ litLength = MEM_readLE16(dumps);
3158
+ dumps += 2;
3159
+ if ((litLength & 1) && dumps < de) {
3160
+ litLength += *dumps << 16;
3161
+ dumps += 1;
3162
+ }
3163
+ litLength>>=1;
3508
3164
  }
3509
- if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
3165
+ if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3510
3166
  }
3511
3167
 
3512
3168
  /* Offset */
@@ -3532,14 +3188,18 @@ static void ZSTDv05_decodeSequence(seq_t* seq, seqState_t* seqState)
3532
3188
  /* MatchLength */
3533
3189
  matchLength = FSEv05_decodeSymbol(&(seqState->stateML), &(seqState->DStream));
3534
3190
  if (matchLength == MaxML) {
3535
- U32 add = *dumps++;
3191
+ const U32 add = dumps<de ? *dumps++ : 0;
3536
3192
  if (add < 255) matchLength += add;
3537
- else {
3538
- matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */
3539
- if (matchLength&1) matchLength>>=1, dumps += 3;
3540
- else matchLength = (U16)(matchLength)>>1, dumps += 2;
3193
+ else if (dumps + 2 <= de) {
3194
+ matchLength = MEM_readLE16(dumps);
3195
+ dumps += 2;
3196
+ if ((matchLength & 1) && dumps < de) {
3197
+ matchLength += *dumps << 16;
3198
+ dumps += 1;
3199
+ }
3200
+ matchLength >>= 1;
3541
3201
  }
3542
- if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */
3202
+ if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */
3543
3203
  }
3544
3204
  matchLength += MINMATCH;
3545
3205
 
@@ -3562,11 +3222,11 @@ static void ZSTDv05_decodeSequence(seq_t* seq, seqState_t* seqState)
3562
3222
 
3563
3223
  static size_t ZSTDv05_execSequence(BYTE* op,
3564
3224
  BYTE* const oend, seq_t sequence,
3565
- const BYTE** litPtr, const BYTE* const litLimit_8,
3225
+ const BYTE** litPtr, const BYTE* const litLimit,
3566
3226
  const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd)
3567
3227
  {
3568
3228
  static const int dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */
3569
- static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* substracted */
3229
+ static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */
3570
3230
  BYTE* const oLitEnd = op + sequence.litLength;
3571
3231
  const size_t sequenceLength = sequence.litLength + sequence.matchLength;
3572
3232
  BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */
@@ -3577,7 +3237,7 @@ static size_t ZSTDv05_execSequence(BYTE* op,
3577
3237
  /* check */
3578
3238
  if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */
3579
3239
  if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */
3580
- if (litEnd > litLimit_8) return ERROR(corruption_detected); /* risk read beyond lit buffer */
3240
+ if (litEnd > litLimit) return ERROR(corruption_detected); /* risk read beyond lit buffer */
3581
3241
 
3582
3242
  /* copy Literals */
3583
3243
  ZSTDv05_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */
@@ -3601,7 +3261,12 @@ static size_t ZSTDv05_execSequence(BYTE* op,
3601
3261
  op = oLitEnd + length1;
3602
3262
  sequence.matchLength -= length1;
3603
3263
  match = base;
3264
+ if (op > oend_8 || sequence.matchLength < MINMATCH) {
3265
+ while (op < oMatchEnd) *op++ = *match++;
3266
+ return sequenceLength;
3267
+ }
3604
3268
  } }
3269
+ /* Requirement: op <= oend_8 */
3605
3270
 
3606
3271
  /* match within prefix */
3607
3272
  if (sequence.offset < 8) {
@@ -3619,7 +3284,7 @@ static size_t ZSTDv05_execSequence(BYTE* op,
3619
3284
  }
3620
3285
  op += 8; match += 8;
3621
3286
 
3622
- if (oMatchEnd > oend-12) {
3287
+ if (oMatchEnd > oend-(16-MINMATCH)) {
3623
3288
  if (op < oend_8) {
3624
3289
  ZSTDv05_wildcopy(op, match, oend_8 - op);
3625
3290
  match += oend_8 - op;
@@ -3628,7 +3293,7 @@ static size_t ZSTDv05_execSequence(BYTE* op,
3628
3293
  while (op < oMatchEnd)
3629
3294
  *op++ = *match++;
3630
3295
  } else {
3631
- ZSTDv05_wildcopy(op, match, sequence.matchLength-8); /* works even if matchLength < 8 */
3296
+ ZSTDv05_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */
3632
3297
  }
3633
3298
  return sequenceLength;
3634
3299
  }
@@ -3644,15 +3309,14 @@ static size_t ZSTDv05_decompressSequences(
3644
3309
  BYTE* const ostart = (BYTE* const)dst;
3645
3310
  BYTE* op = ostart;
3646
3311
  BYTE* const oend = ostart + maxDstSize;
3647
- size_t errorCode, dumpsLength;
3312
+ size_t errorCode, dumpsLength=0;
3648
3313
  const BYTE* litPtr = dctx->litPtr;
3649
- const BYTE* const litLimit_8 = litPtr + dctx->litBufSize - 8;
3650
3314
  const BYTE* const litEnd = litPtr + dctx->litSize;
3651
- int nbSeq;
3652
- const BYTE* dumps;
3653
- U32* DTableLL = dctx->LLTable;
3654
- U32* DTableML = dctx->MLTable;
3655
- U32* DTableOffb = dctx->OffTable;
3315
+ int nbSeq=0;
3316
+ const BYTE* dumps = NULL;
3317
+ unsigned* DTableLL = dctx->LLTable;
3318
+ unsigned* DTableML = dctx->MLTable;
3319
+ unsigned* DTableOffb = dctx->OffTable;
3656
3320
  const BYTE* const base = (const BYTE*) (dctx->base);
3657
3321
  const BYTE* const vBase = (const BYTE*) (dctx->vBase);
3658
3322
  const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
@@ -3660,7 +3324,7 @@ static size_t ZSTDv05_decompressSequences(
3660
3324
  /* Build Decoding Tables */
3661
3325
  errorCode = ZSTDv05_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength,
3662
3326
  DTableLL, DTableML, DTableOffb,
3663
- ip, seqSize);
3327
+ ip, seqSize, dctx->flagStaticTables);
3664
3328
  if (ZSTDv05_isError(errorCode)) return errorCode;
3665
3329
  ip += errorCode;
3666
3330
 
@@ -3684,7 +3348,7 @@ static size_t ZSTDv05_decompressSequences(
3684
3348
  size_t oneSeqSize;
3685
3349
  nbSeq--;
3686
3350
  ZSTDv05_decodeSequence(&sequence, &seqState);
3687
- oneSeqSize = ZSTDv05_execSequence(op, oend, sequence, &litPtr, litLimit_8, base, vBase, dictEnd);
3351
+ oneSeqSize = ZSTDv05_execSequence(op, oend, sequence, &litPtr, litEnd, base, vBase, dictEnd);
3688
3352
  if (ZSTDv05_isError(oneSeqSize)) return oneSeqSize;
3689
3353
  op += oneSeqSize;
3690
3354
  }
@@ -3698,8 +3362,10 @@ static size_t ZSTDv05_decompressSequences(
3698
3362
  size_t lastLLSize = litEnd - litPtr;
3699
3363
  if (litPtr > litEnd) return ERROR(corruption_detected); /* too many literals already used */
3700
3364
  if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);
3701
- memcpy(op, litPtr, lastLLSize);
3702
- op += lastLLSize;
3365
+ if (lastLLSize > 0) {
3366
+ memcpy(op, litPtr, lastLLSize);
3367
+ op += lastLLSize;
3368
+ }
3703
3369
  }
3704
3370
 
3705
3371
  return op-ostart;
@@ -3758,10 +3424,10 @@ static size_t ZSTDv05_decompress_continueDCtx(ZSTDv05_DCtx* dctx,
3758
3424
  BYTE* const oend = ostart + maxDstSize;
3759
3425
  size_t remainingSize = srcSize;
3760
3426
  blockProperties_t blockProperties;
3427
+ memset(&blockProperties, 0, sizeof(blockProperties));
3761
3428
 
3762
3429
  /* Frame Header */
3763
- {
3764
- size_t frameHeaderSize;
3430
+ { size_t frameHeaderSize;
3765
3431
  if (srcSize < ZSTDv05_frameHeaderSize_min+ZSTDv05_blockHeaderSize) return ERROR(srcSize_wrong);
3766
3432
  frameHeaderSize = ZSTDv05_decodeFrameHeader_Part1(dctx, src, ZSTDv05_frameHeaderSize_min);
3767
3433
  if (ZSTDv05_isError(frameHeaderSize)) return frameHeaderSize;
@@ -3853,6 +3519,58 @@ size_t ZSTDv05_decompress(void* dst, size_t maxDstSize, const void* src, size_t
3853
3519
  #endif
3854
3520
  }
3855
3521
 
3522
+ /* ZSTD_errorFrameSizeInfoLegacy() :
3523
+ assumes `cSize` and `dBound` are _not_ NULL */
3524
+ static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)
3525
+ {
3526
+ *cSize = ret;
3527
+ *dBound = ZSTD_CONTENTSIZE_ERROR;
3528
+ }
3529
+
3530
+ void ZSTDv05_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)
3531
+ {
3532
+ const BYTE* ip = (const BYTE*)src;
3533
+ size_t remainingSize = srcSize;
3534
+ size_t nbBlocks = 0;
3535
+ blockProperties_t blockProperties;
3536
+
3537
+ /* Frame Header */
3538
+ if (srcSize < ZSTDv05_frameHeaderSize_min) {
3539
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3540
+ return;
3541
+ }
3542
+ if (MEM_readLE32(src) != ZSTDv05_MAGICNUMBER) {
3543
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));
3544
+ return;
3545
+ }
3546
+ ip += ZSTDv05_frameHeaderSize_min; remainingSize -= ZSTDv05_frameHeaderSize_min;
3547
+
3548
+ /* Loop on each block */
3549
+ while (1)
3550
+ {
3551
+ size_t cBlockSize = ZSTDv05_getcBlockSize(ip, remainingSize, &blockProperties);
3552
+ if (ZSTDv05_isError(cBlockSize)) {
3553
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize);
3554
+ return;
3555
+ }
3556
+
3557
+ ip += ZSTDv05_blockHeaderSize;
3558
+ remainingSize -= ZSTDv05_blockHeaderSize;
3559
+ if (cBlockSize > remainingSize) {
3560
+ ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));
3561
+ return;
3562
+ }
3563
+
3564
+ if (cBlockSize == 0) break; /* bt_end */
3565
+
3566
+ ip += cBlockSize;
3567
+ remainingSize -= cBlockSize;
3568
+ nbBlocks++;
3569
+ }
3570
+
3571
+ *cSize = ip - (const BYTE*)src;
3572
+ *dBound = nbBlocks * BLOCKSIZE;
3573
+ }
3856
3574
 
3857
3575
  /* ******************************
3858
3576
  * Streaming Decompression API
@@ -3949,11 +3667,11 @@ static size_t ZSTDv05_loadEntropy(ZSTDv05_DCtx* dctx, const void* dict, size_t d
3949
3667
  {
3950
3668
  size_t hSize, offcodeHeaderSize, matchlengthHeaderSize, errorCode, litlengthHeaderSize;
3951
3669
  short offcodeNCount[MaxOff+1];
3952
- U32 offcodeMaxValue=MaxOff, offcodeLog=OffFSEv05Log;
3670
+ unsigned offcodeMaxValue=MaxOff, offcodeLog;
3953
3671
  short matchlengthNCount[MaxML+1];
3954
- unsigned matchlengthMaxValue = MaxML, matchlengthLog = MLFSEv05Log;
3672
+ unsigned matchlengthMaxValue = MaxML, matchlengthLog;
3955
3673
  short litlengthNCount[MaxLL+1];
3956
- unsigned litlengthMaxValue = MaxLL, litlengthLog = LLFSEv05Log;
3674
+ unsigned litlengthMaxValue = MaxLL, litlengthLog;
3957
3675
 
3958
3676
  hSize = HUFv05_readDTableX4(dctx->hufTableX4, dict, dictSize);
3959
3677
  if (HUFv05_isError(hSize)) return ERROR(dictionary_corrupted);
@@ -3962,6 +3680,7 @@ static size_t ZSTDv05_loadEntropy(ZSTDv05_DCtx* dctx, const void* dict, size_t d
3962
3680
 
3963
3681
  offcodeHeaderSize = FSEv05_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dict, dictSize);
3964
3682
  if (FSEv05_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted);
3683
+ if (offcodeLog > OffFSEv05Log) return ERROR(dictionary_corrupted);
3965
3684
  errorCode = FSEv05_buildDTable(dctx->OffTable, offcodeNCount, offcodeMaxValue, offcodeLog);
3966
3685
  if (FSEv05_isError(errorCode)) return ERROR(dictionary_corrupted);
3967
3686
  dict = (const char*)dict + offcodeHeaderSize;
@@ -3969,12 +3688,14 @@ static size_t ZSTDv05_loadEntropy(ZSTDv05_DCtx* dctx, const void* dict, size_t d
3969
3688
 
3970
3689
  matchlengthHeaderSize = FSEv05_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dict, dictSize);
3971
3690
  if (FSEv05_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted);
3691
+ if (matchlengthLog > MLFSEv05Log) return ERROR(dictionary_corrupted);
3972
3692
  errorCode = FSEv05_buildDTable(dctx->MLTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog);
3973
3693
  if (FSEv05_isError(errorCode)) return ERROR(dictionary_corrupted);
3974
3694
  dict = (const char*)dict + matchlengthHeaderSize;
3975
3695
  dictSize -= matchlengthHeaderSize;
3976
3696
 
3977
3697
  litlengthHeaderSize = FSEv05_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dict, dictSize);
3698
+ if (litlengthLog > LLFSEv05Log) return ERROR(dictionary_corrupted);
3978
3699
  if (FSEv05_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted);
3979
3700
  errorCode = FSEv05_buildDTable(dctx->LLTable, litlengthNCount, litlengthMaxValue, litlengthLog);
3980
3701
  if (FSEv05_isError(errorCode)) return ERROR(dictionary_corrupted);
@@ -4072,7 +3793,9 @@ static size_t ZBUFFv05_blockHeaderSize = 3;
4072
3793
  static size_t ZBUFFv05_limitCopy(void* dst, size_t maxDstSize, const void* src, size_t srcSize)
4073
3794
  {
4074
3795
  size_t length = MIN(maxDstSize, srcSize);
4075
- memcpy(dst, src, length);
3796
+ if (length > 0) {
3797
+ memcpy(dst, src, length);
3798
+ }
4076
3799
  return length;
4077
3800
  }
4078
3801
 
@@ -4092,7 +3815,7 @@ static size_t ZBUFFv05_limitCopy(void* dst, size_t maxDstSize, const void* src,
4092
3815
  * The function will report how many bytes were read or written by modifying *srcSizePtr and *maxDstSizePtr.
4093
3816
  * Note that it may not consume the entire input, in which case it's up to the caller to call again the function with remaining input.
4094
3817
  * The content of dst will be overwritten (up to *maxDstSizePtr) at each function call, so save its content if it matters or change dst .
4095
- * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
3818
+ * return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
4096
3819
  * or 0 when a frame is completely decoded
4097
3820
  * or an error code, which can be tested using ZBUFFv05_isError().
4098
3821
  *
@@ -4193,7 +3916,7 @@ size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* zbc, void* dst, size_t* maxDst
4193
3916
  zbc->stage = ZBUFFv05ds_decodeHeader;
4194
3917
  break;
4195
3918
  }
4196
-
3919
+ /* fall-through */
4197
3920
  case ZBUFFv05ds_loadHeader:
4198
3921
  /* complete header from src */
4199
3922
  {
@@ -4209,9 +3932,9 @@ size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* zbc, void* dst, size_t* maxDst
4209
3932
  *maxDstSizePtr = 0;
4210
3933
  return headerSize - zbc->hPos;
4211
3934
  }
4212
- // zbc->stage = ZBUFFv05ds_decodeHeader; break; /* useless : stage follows */
3935
+ /* zbc->stage = ZBUFFv05ds_decodeHeader; break; */ /* useless : stage follows */
4213
3936
  }
4214
-
3937
+ /* fall-through */
4215
3938
  case ZBUFFv05ds_decodeHeader:
4216
3939
  /* apply header to create / resize buffers */
4217
3940
  {
@@ -4238,7 +3961,7 @@ size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* zbc, void* dst, size_t* maxDst
4238
3961
  break;
4239
3962
  }
4240
3963
  zbc->stage = ZBUFFv05ds_read;
4241
-
3964
+ /* fall-through */
4242
3965
  case ZBUFFv05ds_read:
4243
3966
  {
4244
3967
  size_t neededInSize = ZSTDv05_nextSrcSizeToDecompress(zbc->zc);
@@ -4262,7 +3985,7 @@ size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* zbc, void* dst, size_t* maxDst
4262
3985
  if (ip==iend) { notDone = 0; break; } /* no more input */
4263
3986
  zbc->stage = ZBUFFv05ds_load;
4264
3987
  }
4265
-
3988
+ /* fall-through */
4266
3989
  case ZBUFFv05ds_load:
4267
3990
  {
4268
3991
  size_t neededInSize = ZSTDv05_nextSrcSizeToDecompress(zbc->zc);
@@ -4282,8 +4005,10 @@ size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* zbc, void* dst, size_t* maxDst
4282
4005
  if (!decodedSize) { zbc->stage = ZBUFFv05ds_read; break; } /* this was just a header */
4283
4006
  zbc->outEnd = zbc->outStart + decodedSize;
4284
4007
  zbc->stage = ZBUFFv05ds_flush;
4285
- // break; /* ZBUFFv05ds_flush follows */
4286
- } }
4008
+ /* break; */ /* ZBUFFv05ds_flush follows */
4009
+ }
4010
+ }
4011
+ /* fall-through */
4287
4012
  case ZBUFFv05ds_flush:
4288
4013
  {
4289
4014
  size_t toFlushSize = zbc->outEnd - zbc->outStart;