multi_compress 0.6.1 → 0.7.0

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 (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -0
  3. data/GET_STARTED.md +4 -2
  4. data/README.md +1 -1
  5. data/THIRD_PARTY_NOTICES.md +2 -2
  6. data/ext/multi_compress/exports.darwin +1 -0
  7. data/ext/multi_compress/exports.map +4 -0
  8. data/ext/multi_compress/extconf.rb +20 -0
  9. data/ext/multi_compress/multi_compress.c +123 -73
  10. data/ext/multi_compress/vendor/.vendored +1 -1
  11. data/ext/multi_compress/vendor/zstd/lib/BUCK +10 -5
  12. data/ext/multi_compress/vendor/zstd/lib/README.md +25 -5
  13. data/ext/multi_compress/vendor/zstd/lib/common/allocations.h +6 -2
  14. data/ext/multi_compress/vendor/zstd/lib/common/bits.h +1 -1
  15. data/ext/multi_compress/vendor/zstd/lib/common/bitstream.h +3 -3
  16. data/ext/multi_compress/vendor/zstd/lib/common/bmi2.h +40 -0
  17. data/ext/multi_compress/vendor/zstd/lib/common/compiler.h +37 -11
  18. data/ext/multi_compress/vendor/zstd/lib/common/entropy_common.c +7 -6
  19. data/ext/multi_compress/vendor/zstd/lib/common/fse_decompress.c +4 -3
  20. data/ext/multi_compress/vendor/zstd/lib/common/pool.c +16 -19
  21. data/ext/multi_compress/vendor/zstd/lib/common/portability_macros.h +31 -0
  22. data/ext/multi_compress/vendor/zstd/lib/common/threading.c +16 -2
  23. data/ext/multi_compress/vendor/zstd/lib/common/zstd_common.c +9 -0
  24. data/ext/multi_compress/vendor/zstd/lib/common/zstd_internal.h +4 -2
  25. data/ext/multi_compress/vendor/zstd/lib/compress/hist.c +300 -3
  26. data/ext/multi_compress/vendor/zstd/lib/compress/hist.h +4 -0
  27. data/ext/multi_compress/vendor/zstd/lib/compress/huf_compress.c +4 -1
  28. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress.c +775 -214
  29. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_internal.h +35 -6
  30. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_sequences.c +9 -5
  31. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_compress_superblock.c +14 -5
  32. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_double_fast.c +2 -2
  33. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_fast.c +2 -2
  34. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_lazy.c +49 -5
  35. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.c +11 -11
  36. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_ldm.h +11 -0
  37. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_opt.c +16 -18
  38. data/ext/multi_compress/vendor/zstd/lib/compress/zstd_preSplit.c +3 -3
  39. data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.c +64 -39
  40. data/ext/multi_compress/vendor/zstd/lib/compress/zstdmt_compress.h +30 -2
  41. data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress.c +78 -64
  42. data/ext/multi_compress/vendor/zstd/lib/decompress/huf_decompress_amd64.S +182 -18
  43. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.c +59 -27
  44. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_ddict.h +11 -0
  45. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress.c +194 -93
  46. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_block.c +225 -85
  47. data/ext/multi_compress/vendor/zstd/lib/decompress/zstd_decompress_internal.h +19 -2
  48. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/cover.c +136 -91
  49. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/divsufsort.c +77 -77
  50. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/fastcover.c +48 -42
  51. data/ext/multi_compress/vendor/zstd/lib/dictBuilder/zdict.c +18 -14
  52. data/ext/multi_compress/vendor/zstd/lib/dll/example/build_package.bat +66 -20
  53. data/ext/multi_compress/vendor/zstd/lib/dll/example/fullbench-dll.vcxproj +10 -6
  54. data/ext/multi_compress/vendor/zstd/lib/install_oses.mk +17 -0
  55. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v01.c +10 -8
  56. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v02.c +5 -364
  57. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v03.c +4 -5
  58. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v04.c +7 -3
  59. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v05.c +8 -5
  60. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v06.c +3 -3
  61. data/ext/multi_compress/vendor/zstd/lib/legacy/zstd_v07.c +1 -0
  62. data/ext/multi_compress/vendor/zstd/lib/libzstd.mk +8 -7
  63. data/ext/multi_compress/vendor/zstd/lib/libzstd.pc.in +1 -0
  64. data/ext/multi_compress/vendor/zstd/lib/module.modulemap +0 -21
  65. data/ext/multi_compress/vendor/zstd/lib/zstd.h +209 -112
  66. data/lib/multi_compress/reader/read_operations.rb +2 -0
  67. data/lib/multi_compress/version.rb +1 -1
  68. data/multi_compress.gemspec +2 -0
  69. data/mysql_udf/Makefile +12 -5
  70. data/mysql_udf/multi_compress_mysql.exports +6 -0
  71. data/postgres_extension/Makefile +2 -1
  72. data/postgres_extension/test/run_e2e.sh +28 -10
  73. metadata +6 -5
  74. data/ext/multi_compress/vendor/lz4/lib/Makefile +0 -244
  75. data/ext/multi_compress/vendor/lz4/lib/dll/example/Makefile +0 -63
  76. data/ext/multi_compress/vendor/zstd/lib/Makefile +0 -389
  77. data/ext/multi_compress/vendor/zstd/lib/dll/example/Makefile +0 -48
@@ -57,6 +57,7 @@
57
57
  *********************************************************/
58
58
  #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */
59
59
  #include "../common/allocations.h" /* ZSTD_customMalloc, ZSTD_customCalloc, ZSTD_customFree */
60
+ #include "../common/bmi2.h"
60
61
  #include "../common/error_private.h"
61
62
  #include "../common/zstd_internal.h" /* blockProperties_t */
62
63
  #include "../common/mem.h" /* low level memory routines */
@@ -82,7 +83,7 @@
82
83
  #define DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT 4
83
84
  #define DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT 3 /* These two constants represent SIZE_MULT/COUNT_MULT load factor without using a float.
84
85
  * Currently, that means a 0.75 load factor.
85
- * So, if count * COUNT_MULT / size * SIZE_MULT != 0, then we've exceeded
86
+ * So, if count * COUNT_MULT >= size * SIZE_MULT, then we've reached
86
87
  * the load factor of the ddict hash set.
87
88
  */
88
89
 
@@ -115,8 +116,7 @@ static size_t ZSTD_DDictHashSet_emplaceDDict(ZSTD_DDictHashSet* hashSet, const Z
115
116
  hashSet->ddictPtrTable[idx] = ddict;
116
117
  return 0;
117
118
  }
118
- idx &= idxRangeMask;
119
- idx++;
119
+ idx = (idx + 1) & idxRangeMask;
120
120
  }
121
121
  DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID, idx);
122
122
  hashSet->ddictPtrTable[idx] = ddict;
@@ -153,19 +153,15 @@ static size_t ZSTD_DDictHashSet_expand(ZSTD_DDictHashSet* hashSet, ZSTD_customMe
153
153
  /* Fetches a DDict with the given dictID
154
154
  * Returns the ZSTD_DDict* with the requested dictID. If it doesn't exist, then returns NULL.
155
155
  */
156
- static const ZSTD_DDict* ZSTD_DDictHashSet_getDDict(ZSTD_DDictHashSet* hashSet, U32 dictID) {
156
+ static const ZSTD_DDict* ZSTD_DDictHashSet_getDDict(const ZSTD_DDictHashSet* hashSet, U32 dictID) {
157
157
  size_t idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID);
158
158
  const size_t idxRangeMask = hashSet->ddictPtrTableSize - 1;
159
159
  DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID, idx);
160
- for (;;) {
161
- size_t currDictID = ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]);
162
- if (currDictID == dictID || currDictID == 0) {
163
- /* currDictID == 0 implies a NULL ddict entry */
160
+ while (hashSet->ddictPtrTable[idx] != NULL) {
161
+ if (ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]) == dictID)
164
162
  break;
165
- } else {
166
- idx &= idxRangeMask; /* Goes to start of table when we reach the end */
167
- idx++;
168
- }
163
+ /* ID 0 is valid for raw-content dictionaries, not an empty slot. */
164
+ idx = (idx + 1) & idxRangeMask;
169
165
  }
170
166
  DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID, idx);
171
167
  return hashSet->ddictPtrTable[idx];
@@ -208,7 +204,7 @@ static void ZSTD_freeDDictHashSet(ZSTD_DDictHashSet* hashSet, ZSTD_customMem cus
208
204
  */
209
205
  static size_t ZSTD_DDictHashSet_addDDict(ZSTD_DDictHashSet* hashSet, const ZSTD_DDict* ddict, ZSTD_customMem customMem) {
210
206
  DEBUGLOG(4, "Adding dict ID: %u to hashset with - Count: %zu Tablesize: %zu", ZSTD_getDictID_fromDDict(ddict), hashSet->ddictPtrCount, hashSet->ddictPtrTableSize);
211
- if (hashSet->ddictPtrCount * DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT / hashSet->ddictPtrTableSize * DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT != 0) {
207
+ if (hashSet->ddictPtrCount * DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT >= hashSet->ddictPtrTableSize * DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT) {
212
208
  FORWARD_IF_ERROR(ZSTD_DDictHashSet_expand(hashSet, customMem), "");
213
209
  }
214
210
  FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet, ddict), "");
@@ -249,8 +245,9 @@ static void ZSTD_DCtx_resetParameters(ZSTD_DCtx* dctx)
249
245
  dctx->maxBlockSizeParam = 0;
250
246
  }
251
247
 
252
- static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
248
+ static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx, ZSTD_customMem customMem)
253
249
  {
250
+ dctx->customMem = customMem;
254
251
  dctx->staticSize = 0;
255
252
  dctx->ddict = NULL;
256
253
  dctx->ddictLocal = NULL;
@@ -261,6 +258,8 @@ static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
261
258
  dctx->inBuffSize = 0;
262
259
  dctx->outBuffSize = 0;
263
260
  dctx->streamStage = zdss_init;
261
+ dctx->stage = ZSTDds_getFrameHeaderSize;
262
+ dctx->expected = 0; /* ZSTD_decompressContinue() requires ZSTD_decompressBegin() first */
264
263
  #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
265
264
  dctx->legacyContext = NULL;
266
265
  dctx->previousLegacyVersion = 0;
@@ -269,7 +268,7 @@ static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
269
268
  dctx->oversizedDuration = 0;
270
269
  dctx->isFrameDecompression = 1;
271
270
  #if DYNAMIC_BMI2
272
- dctx->bmi2 = ZSTD_cpuSupportsBmi2();
271
+ ZSTD_SET_BMI2(dctx->bmi2, ZSTD_cpuSupportsBmi2());
273
272
  #endif
274
273
  dctx->ddictSet = NULL;
275
274
  ZSTD_DCtx_resetParameters(dctx);
@@ -285,7 +284,7 @@ ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize)
285
284
  if ((size_t)workspace & 7) return NULL; /* 8-aligned */
286
285
  if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL; /* minimum size */
287
286
 
288
- ZSTD_initDCtx_internal(dctx);
287
+ ZSTD_initDCtx_internal(dctx, ZSTD_defaultCMem);
289
288
  dctx->staticSize = workspaceSize;
290
289
  dctx->inBuff = (char*)(dctx+1);
291
290
  return dctx;
@@ -296,8 +295,7 @@ static ZSTD_DCtx* ZSTD_createDCtx_internal(ZSTD_customMem customMem) {
296
295
 
297
296
  { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_customMalloc(sizeof(*dctx), customMem);
298
297
  if (!dctx) return NULL;
299
- dctx->customMem = customMem;
300
- ZSTD_initDCtx_internal(dctx);
298
+ ZSTD_initDCtx_internal(dctx, customMem);
301
299
  return dctx;
302
300
  }
303
301
  }
@@ -321,6 +319,32 @@ static void ZSTD_clearDict(ZSTD_DCtx* dctx)
321
319
  dctx->dictUses = ZSTD_dont_use;
322
320
  }
323
321
 
322
+ /* Abandon any active or failed streaming session.
323
+ * This is the implementation of ZSTD_reset_session_only.
324
+ * Single-shot entry points call it first, so that a DCtx left mid-stream
325
+ * (or in an undefined state after a streaming error) can be reused, and
326
+ * so that it is returned to a clean streaming state afterwards. */
327
+ static void ZSTD_DCtx_resetSession(ZSTD_DCtx* dctx)
328
+ {
329
+ /* A prefix is only used once : if a frame was started, it is abandoned now,
330
+ * so the prefix must not leak into the next frame */
331
+ if (dctx->streamStage != zdss_init && dctx->dictUses == ZSTD_use_once)
332
+ ZSTD_clearDict(dctx);
333
+ dctx->streamStage = zdss_init;
334
+ dctx->noForwardProgress = 0;
335
+ dctx->isFrameDecompression = 1;
336
+ }
337
+
338
+ /* Discard borrowed dictionary references without freeing the DDicts. */
339
+ static void ZSTD_clearDDictSet(ZSTD_DCtx* dctx)
340
+ {
341
+ if (dctx->ddictSet != NULL) {
342
+ assert(!dctx->staticSize);
343
+ ZSTD_freeDDictHashSet(dctx->ddictSet, dctx->customMem);
344
+ dctx->ddictSet = NULL;
345
+ }
346
+ }
347
+
324
348
  size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
325
349
  {
326
350
  if (dctx==NULL) return 0; /* support free on NULL */
@@ -333,43 +357,34 @@ size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx)
333
357
  if (dctx->legacyContext)
334
358
  ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion);
335
359
  #endif
336
- if (dctx->ddictSet) {
337
- ZSTD_freeDDictHashSet(dctx->ddictSet, cMem);
338
- dctx->ddictSet = NULL;
339
- }
360
+ ZSTD_clearDDictSet(dctx);
340
361
  ZSTD_customFree(dctx, cMem);
341
362
  return 0;
342
363
  }
343
364
  }
344
365
 
345
- /* no longer useful */
346
- void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx)
347
- {
348
- size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx);
349
- ZSTD_memcpy(dstDCtx, srcDCtx, toCopy); /* no need to copy workspace */
350
- }
351
-
352
- /* Given a dctx with a digested frame params, re-selects the correct ZSTD_DDict based on
353
- * the requested dict ID from the frame. If there exists a reference to the correct ZSTD_DDict, then
354
- * accordingly sets the ddict to be used to decompress the frame.
366
+ /* Look up the DDict for a complete, valid Zstandard frame header.
367
+ * The result is for this frame only; it does not change the single-DDict state.
355
368
  *
356
- * If no DDict is found, then no action is taken, and the ZSTD_DCtx::ddict remains as-is.
369
+ * Returns dictionary_wrong if the set does not contain a requested nonzero ID.
370
+ * For ID 0, returns the registered DDict, or NULL to use no dictionary.
357
371
  *
372
+ * Must be called before installing the dictionary's decoding state.
358
373
  * ZSTD_d_refMultipleDDicts must be enabled for this function to be called.
359
374
  */
360
- static void ZSTD_DCtx_selectFrameDDict(ZSTD_DCtx* dctx) {
375
+ static size_t ZSTD_DCtx_getFrameDDict(const ZSTD_DCtx* dctx, U32 dictID,
376
+ const ZSTD_DDict** ddict) {
377
+ const ZSTD_DDict* frameDDict;
361
378
  assert(dctx->refMultipleDDicts && dctx->ddictSet);
362
- DEBUGLOG(4, "Adjusting DDict based on requested dict ID from frame");
363
- if (dctx->ddict) {
364
- const ZSTD_DDict* frameDDict = ZSTD_DDictHashSet_getDDict(dctx->ddictSet, dctx->fParams.dictID);
365
- if (frameDDict) {
366
- DEBUGLOG(4, "DDict found!");
367
- ZSTD_clearDict(dctx);
368
- dctx->dictID = dctx->fParams.dictID;
369
- dctx->ddict = frameDDict;
370
- dctx->dictUses = ZSTD_use_indefinitely;
371
- }
372
- }
379
+ assert(dctx->ddictLocal == NULL && dctx->ddict == NULL);
380
+ assert(dctx->dictUses == ZSTD_dont_use);
381
+ DEBUGLOG(4, "Looking up requested dict ID from frame");
382
+ frameDDict = ZSTD_DDictHashSet_getDDict(dctx->ddictSet, dictID);
383
+ RETURN_ERROR_IF(frameDDict == NULL && dictID != 0, dictionary_wrong, "No matching DDict in the set");
384
+ DEBUGLOG(4, "DDict lookup complete");
385
+ assert(frameDDict == NULL || ZSTD_getDictID_fromDDict(frameDDict) == dictID);
386
+ *ddict = frameDDict;
387
+ return 0;
373
388
  }
374
389
 
375
390
 
@@ -697,7 +712,7 @@ unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize)
697
712
 
698
713
  /** ZSTD_decodeFrameHeader() :
699
714
  * `headerSize` must be the size provided by ZSTD_frameHeaderSize().
700
- * If multiple DDict references are enabled, also will choose the correct DDict to use.
715
+ * The dictionary's decoding state must already be installed.
701
716
  * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */
702
717
  static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize)
703
718
  {
@@ -705,11 +720,6 @@ static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t he
705
720
  if (ZSTD_isError(result)) return result; /* invalid header */
706
721
  RETURN_ERROR_IF(result>0, srcSize_wrong, "headerSize too small");
707
722
 
708
- /* Reference DDict requested by frame if dctx references multiple ddicts */
709
- if (dctx->refMultipleDDicts == ZSTD_rmd_refMultipleDDicts && dctx->ddictSet) {
710
- ZSTD_DCtx_selectFrameDDict(dctx);
711
- }
712
-
713
723
  #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
714
724
  /* Skip the dictID check in fuzzing mode, because it makes the search
715
725
  * harder.
@@ -719,7 +729,6 @@ static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t he
719
729
  #endif
720
730
  dctx->validateChecksum = (dctx->fParams.checksumFlag && !dctx->forceIgnoreChecksum) ? 1 : 0;
721
731
  if (dctx->validateChecksum) XXH64_reset(&dctx->xxhState, 0);
722
- dctx->processedCSize += headerSize;
723
732
  return 0;
724
733
  }
725
734
 
@@ -827,6 +836,11 @@ unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize)
827
836
  unsigned long long const decompressedBound = frameSizeInfo.decompressedBound;
828
837
  if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR)
829
838
  return ZSTD_CONTENTSIZE_ERROR;
839
+ /* Invariant: bound < ZSTD_CONTENTSIZE_ERROR, so this subtraction
840
+ * cannot underflow. Keep the sum below both reserved values, which
841
+ * also prevents unsigned overflow. */
842
+ if (decompressedBound >= ZSTD_CONTENTSIZE_ERROR - bound)
843
+ return ZSTD_CONTENTSIZE_ERROR;
830
844
  assert(srcSize >= compressedSize);
831
845
  src = (const BYTE*)src + compressedSize;
832
846
  srcSize -= compressedSize;
@@ -888,7 +902,9 @@ size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSiz
888
902
  {
889
903
  DEBUGLOG(5, "ZSTD_insertBlock: %u bytes", (unsigned)blockSize);
890
904
  ZSTD_checkContinuity(dctx, blockStart, blockSize);
891
- dctx->previousDstEnd = (const char*)blockStart + blockSize;
905
+ /* An empty insertion must not replace the current history endpoint. */
906
+ if (blockSize != 0)
907
+ dctx->previousDstEnd = (const char*)blockStart + blockSize;
892
908
  return blockSize;
893
909
  }
894
910
 
@@ -927,9 +943,9 @@ static void ZSTD_DCtx_trace_end(ZSTD_DCtx const* dctx, U64 uncompressedSize, U64
927
943
  ZSTD_memset(&trace, 0, sizeof(trace));
928
944
  trace.version = ZSTD_VERSION_NUMBER;
929
945
  trace.streaming = streaming;
930
- if (dctx->ddict) {
931
- trace.dictionaryID = ZSTD_getDictID_fromDDict(dctx->ddict);
932
- trace.dictionarySize = ZSTD_DDict_dictSize(dctx->ddict);
946
+ if (dctx->traceDictSize != 0) {
947
+ trace.dictionaryID = dctx->dictID;
948
+ trace.dictionarySize = dctx->traceDictSize;
933
949
  trace.dictionaryIsCold = dctx->ddictIsCold;
934
950
  }
935
951
  trace.uncompressedSize = (size_t)uncompressedSize;
@@ -1029,6 +1045,7 @@ static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
1029
1045
  RETURN_ERROR(corruption_detected, "invalid block type");
1030
1046
  }
1031
1047
  FORWARD_IF_ERROR(decodedSize, "Block decompression failure");
1048
+ RETURN_ERROR_IF(decodedSize > dctx->fParams.blockSizeMax, corruption_detected, "Decompressed Block Size Exceeds Maximum");
1032
1049
  DEBUGLOG(5, "Decompressed block of dSize = %u", (unsigned)decodedSize);
1033
1050
  if (dctx->validateChecksum) {
1034
1051
  XXH64_update(&dctx->xxhState, op, decodedSize);
@@ -1071,7 +1088,8 @@ size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
1071
1088
  void* dst, size_t dstCapacity,
1072
1089
  const void* src, size_t srcSize,
1073
1090
  const void* dict, size_t dictSize,
1074
- const ZSTD_DDict* ddict)
1091
+ const ZSTD_DDict* ddict,
1092
+ int useDDictSet)
1075
1093
  {
1076
1094
  void* const dststart = dst;
1077
1095
  int moreThan1Frame = 0;
@@ -1079,22 +1097,27 @@ size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
1079
1097
  DEBUGLOG(5, "ZSTD_decompressMultiFrame");
1080
1098
  assert(dict==NULL || ddict==NULL); /* either dict or ddict set, not both */
1081
1099
 
1082
- if (ddict) {
1083
- dict = ZSTD_DDict_dictContent(ddict);
1084
- dictSize = ZSTD_DDict_dictSize(ddict);
1085
- }
1100
+ assert(!useDDictSet || (dctx->refMultipleDDicts && dctx->ddictSet
1101
+ && dctx->ddictLocal == NULL && dctx->ddict == NULL
1102
+ && dict == NULL && ddict == NULL));
1086
1103
 
1087
1104
  while (srcSize >= ZSTD_startingInputLength(dctx->format)) {
1105
+ const ZSTD_DDict* frameDDict = ddict;
1088
1106
 
1089
1107
  #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
1090
1108
  if (dctx->format == ZSTD_f_zstd1 && ZSTD_isLegacy(src, srcSize)) {
1091
1109
  size_t decodedSize;
1092
- size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
1110
+ const void* const legacyDict = ddict ? ZSTD_DDict_dictContent(ddict) : dict;
1111
+ size_t const legacyDictSize = ddict ? ZSTD_DDict_dictSize(ddict) : dictSize;
1112
+ size_t frameSize;
1113
+ RETURN_ERROR_IF(useDDictSet, parameter_unsupported,
1114
+ "DDict-set selection is not supported for legacy frames");
1115
+ frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
1093
1116
  if (ZSTD_isError(frameSize)) return frameSize;
1094
1117
  RETURN_ERROR_IF(dctx->staticSize, memory_allocation,
1095
1118
  "legacy support is not compatible with static dctx");
1096
1119
 
1097
- decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize);
1120
+ decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, legacyDict, legacyDictSize);
1098
1121
  if (ZSTD_isError(decodedSize)) return decodedSize;
1099
1122
 
1100
1123
  {
@@ -1131,9 +1154,17 @@ size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
1131
1154
  continue; /* check next frame */
1132
1155
  } }
1133
1156
 
1134
- if (ddict) {
1135
- /* we were called from ZSTD_decompress_usingDDict */
1136
- FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(dctx, ddict), "");
1157
+ if (useDDictSet) {
1158
+ ZSTD_FrameHeader zfh;
1159
+ size_t const hSize = ZSTD_getFrameHeader_advanced(&zfh, src, srcSize, dctx->format);
1160
+ /* Invalid/incomplete headers must not change dictionary selection.
1161
+ * ZSTD_decompressFrame() will report the header error. */
1162
+ if (hSize == 0 && zfh.frameType == ZSTD_frame)
1163
+ FORWARD_IF_ERROR(ZSTD_DCtx_getFrameDDict(dctx, zfh.dictID, &frameDDict), "");
1164
+ }
1165
+ if (frameDDict) {
1166
+ /* Use either the explicit DDict or the one selected from the set. */
1167
+ FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(dctx, frameDDict), "");
1137
1168
  } else {
1138
1169
  /* this will initialize correctly with no dict if dict == NULL, so
1139
1170
  * use this in all cases but ddict */
@@ -1173,12 +1204,14 @@ size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
1173
1204
  const void* src, size_t srcSize,
1174
1205
  const void* dict, size_t dictSize)
1175
1206
  {
1176
- return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL);
1207
+ ZSTD_DCtx_resetSession(dctx);
1208
+ return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL, 0);
1177
1209
  }
1178
1210
 
1179
1211
 
1180
1212
  static ZSTD_DDict const* ZSTD_getDDict(ZSTD_DCtx* dctx)
1181
1213
  {
1214
+ assert(dctx->ddictSet == NULL);
1182
1215
  switch (dctx->dictUses) {
1183
1216
  default:
1184
1217
  assert(0 /* Impossible */);
@@ -1196,7 +1229,16 @@ static ZSTD_DDict const* ZSTD_getDDict(ZSTD_DCtx* dctx)
1196
1229
 
1197
1230
  size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
1198
1231
  {
1199
- return ZSTD_decompress_usingDDict(dctx, dst, dstCapacity, src, srcSize, ZSTD_getDDict(dctx));
1232
+ /* Reset first: this may discard a prefix abandoned by an unfinished
1233
+ * stream, which must happen before the dictionary is resolved. */
1234
+ ZSTD_DCtx_resetSession(dctx);
1235
+ if (dctx->refMultipleDDicts && dctx->ddictSet)
1236
+ return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize,
1237
+ NULL, 0, NULL, 1);
1238
+ /* Preserve the single-dictionary behavior: resolve once for the entire
1239
+ * call, including concatenated frames and single-use prefixes. */
1240
+ return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize,
1241
+ NULL, 0, ZSTD_getDDict(dctx), 0);
1200
1242
  }
1201
1243
 
1202
1244
 
@@ -1211,7 +1253,7 @@ size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t sr
1211
1253
  return regenSize;
1212
1254
  #else /* stack mode */
1213
1255
  ZSTD_DCtx dctx;
1214
- ZSTD_initDCtx_internal(&dctx);
1256
+ ZSTD_initDCtx_internal(&dctx, ZSTD_defaultCMem);
1215
1257
  return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize);
1216
1258
  #endif
1217
1259
  }
@@ -1269,12 +1311,17 @@ ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) {
1269
1311
  static int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; }
1270
1312
 
1271
1313
  /** ZSTD_decompressContinue() :
1314
+ * The frame must first be initialized with ZSTD_decompressBegin() or one of its dictionary-taking variants.
1315
+ * ZSTD_DCtx_reset() does not replace this initialization.
1272
1316
  * srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress())
1273
1317
  * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity)
1274
1318
  * or an error code, which can be tested using ZSTD_isError() */
1275
1319
  size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize)
1276
1320
  {
1277
1321
  DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (unsigned)srcSize);
1322
+ /* The previous frame is finished, or the context was never initialized */
1323
+ RETURN_ERROR_IF(dctx->stage == ZSTDds_getFrameHeaderSize && dctx->expected == 0,
1324
+ stage_wrong, "missing init (ZSTD_decompressBegin)");
1278
1325
  /* Sanity check */
1279
1326
  RETURN_ERROR_IF(srcSize != ZSTD_nextSrcSizeToDecompressWithInputSize(dctx, srcSize), srcSize_wrong, "not allowed");
1280
1327
  ZSTD_checkContinuity(dctx, dst, dstCapacity);
@@ -1320,12 +1367,22 @@ size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, c
1320
1367
  dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock;
1321
1368
  return 0;
1322
1369
  }
1370
+ RETURN_ERROR_IF(bp.blockType != bt_raw, corruption_detected,
1371
+ "Only raw blocks can have an empty payload");
1323
1372
  /* empty block */
1324
1373
  if (bp.lastBlock) {
1374
+ /* An empty last block ends the frame here, so the content size
1375
+ * check that normally runs in ZSTDds_decompressLastBlock must
1376
+ * be performed at this point as well. */
1377
+ RETURN_ERROR_IF(
1378
+ dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
1379
+ && dctx->decodedSize != dctx->fParams.frameContentSize,
1380
+ corruption_detected, "");
1325
1381
  if (dctx->fParams.checksumFlag) {
1326
1382
  dctx->expected = 4;
1327
1383
  dctx->stage = ZSTDds_checkChecksum;
1328
1384
  } else {
1385
+ ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, /* streaming */ 1);
1329
1386
  dctx->expected = 0; /* end of frame */
1330
1387
  dctx->stage = ZSTDds_getFrameHeaderSize;
1331
1388
  }
@@ -1368,7 +1425,9 @@ size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, c
1368
1425
  DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (unsigned)rSize);
1369
1426
  dctx->decodedSize += rSize;
1370
1427
  if (dctx->validateChecksum) XXH64_update(&dctx->xxhState, dst, rSize);
1371
- dctx->previousDstEnd = (char*)dst + rSize;
1428
+ /* No output means no new history; dst may be NULL. */
1429
+ if (rSize != 0)
1430
+ dctx->previousDstEnd = (char*)dst + rSize;
1372
1431
 
1373
1432
  /* Stay on the same stage until we are finished streaming the block. */
1374
1433
  if (dctx->expected > 0) {
@@ -1562,6 +1621,7 @@ size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx)
1562
1621
  assert(dctx != NULL);
1563
1622
  #if ZSTD_TRACE
1564
1623
  dctx->traceCtx = (ZSTD_trace_decompress_begin != NULL) ? ZSTD_trace_decompress_begin(dctx) : 0;
1624
+ dctx->traceDictSize = 0;
1565
1625
  #endif
1566
1626
  dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */
1567
1627
  dctx->stage = ZSTDds_getFrameHeaderSize;
@@ -1592,6 +1652,9 @@ size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t
1592
1652
  RETURN_ERROR_IF(
1593
1653
  ZSTD_isError(ZSTD_decompress_insertDictionary(dctx, dict, dictSize)),
1594
1654
  dictionary_corrupted, "");
1655
+ #if ZSTD_TRACE
1656
+ dctx->traceDictSize = dict ? dictSize : 0;
1657
+ #endif
1595
1658
  return 0;
1596
1659
  }
1597
1660
 
@@ -1605,7 +1668,8 @@ size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
1605
1668
  if (ddict) {
1606
1669
  const char* const dictStart = (const char*)ZSTD_DDict_dictContent(ddict);
1607
1670
  size_t const dictSize = ZSTD_DDict_dictSize(ddict);
1608
- const void* const dictEnd = dictStart + dictSize;
1671
+ /* An empty DDict may have NULL content. */
1672
+ const void* const dictEnd = dictSize != 0 ? dictStart + dictSize : dictStart;
1609
1673
  dctx->ddictIsCold = (dctx->dictEnd != dictEnd);
1610
1674
  DEBUGLOG(4, "DDict is %s",
1611
1675
  dctx->ddictIsCold ? "~cold~" : "hot!");
@@ -1613,6 +1677,9 @@ size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
1613
1677
  FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , "");
1614
1678
  if (ddict) { /* NULL ddict is equivalent to no dictionary */
1615
1679
  ZSTD_copyDDictParameters(dctx, ddict);
1680
+ #if ZSTD_TRACE
1681
+ dctx->traceDictSize = ZSTD_DDict_dictSize(ddict);
1682
+ #endif
1616
1683
  }
1617
1684
  return 0;
1618
1685
  }
@@ -1658,10 +1725,10 @@ size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
1658
1725
  const void* src, size_t srcSize,
1659
1726
  const ZSTD_DDict* ddict)
1660
1727
  {
1661
- /* pass content and size in case legacy frames are encountered */
1728
+ ZSTD_DCtx_resetSession(dctx);
1662
1729
  return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize,
1663
1730
  NULL, 0,
1664
- ddict);
1731
+ ddict, 0);
1665
1732
  }
1666
1733
 
1667
1734
 
@@ -1703,9 +1770,12 @@ size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx,
1703
1770
  {
1704
1771
  RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1705
1772
  ZSTD_clearDict(dctx);
1773
+ ZSTD_clearDDictSet(dctx);
1706
1774
  if (dict && dictSize != 0) {
1707
- dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem);
1708
- RETURN_ERROR_IF(dctx->ddictLocal == NULL, memory_allocation, "NULL pointer!");
1775
+ RETURN_ERROR_IF(dctx->staticSize, memory_allocation,
1776
+ "static DCtx can't allocate an internal DDict");
1777
+ FORWARD_IF_ERROR(ZSTD_createDDict_advanced_internal(&dctx->ddictLocal,
1778
+ dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem), "");
1709
1779
  dctx->ddict = dctx->ddictLocal;
1710
1780
  dctx->dictUses = ZSTD_use_indefinitely;
1711
1781
  }
@@ -1782,8 +1852,6 @@ size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
1782
1852
  RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1783
1853
  ZSTD_clearDict(dctx);
1784
1854
  if (ddict) {
1785
- dctx->ddict = ddict;
1786
- dctx->dictUses = ZSTD_use_indefinitely;
1787
1855
  if (dctx->refMultipleDDicts == ZSTD_rmd_refMultipleDDicts) {
1788
1856
  if (dctx->ddictSet == NULL) {
1789
1857
  dctx->ddictSet = ZSTD_createDDictHashSet(dctx->customMem);
@@ -1793,7 +1861,13 @@ size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict)
1793
1861
  }
1794
1862
  assert(!dctx->staticSize); /* Impossible: ddictSet cannot have been allocated if static dctx */
1795
1863
  FORWARD_IF_ERROR(ZSTD_DDictHashSet_addDDict(dctx->ddictSet, ddict, dctx->customMem), "");
1864
+ } else {
1865
+ assert(dctx->ddictSet == NULL);
1866
+ dctx->ddict = ddict;
1867
+ dctx->dictUses = ZSTD_use_indefinitely;
1796
1868
  }
1869
+ } else {
1870
+ ZSTD_clearDDictSet(dctx);
1797
1871
  }
1798
1872
  return 0;
1799
1873
  }
@@ -1929,6 +2003,12 @@ size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value
1929
2003
  if (dctx->staticSize != 0) {
1930
2004
  RETURN_ERROR(parameter_unsupported, "Static dctx does not support multiple DDicts!");
1931
2005
  }
2006
+ if (value == ZSTD_rmd_refSingleDDict && dctx->ddictSet != NULL) {
2007
+ /* No dictionary from the set is inherited by single-DDict mode. */
2008
+ assert(dctx->ddict == NULL && dctx->ddictLocal == NULL);
2009
+ assert(dctx->dictUses == ZSTD_dont_use);
2010
+ ZSTD_clearDDictSet(dctx);
2011
+ }
1932
2012
  dctx->refMultipleDDicts = (ZSTD_refMultipleDDicts_e)value;
1933
2013
  return 0;
1934
2014
  case ZSTD_d_disableHuffmanAssembly:
@@ -1948,14 +2028,13 @@ size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset)
1948
2028
  {
1949
2029
  if ( (reset == ZSTD_reset_session_only)
1950
2030
  || (reset == ZSTD_reset_session_and_parameters) ) {
1951
- dctx->streamStage = zdss_init;
1952
- dctx->noForwardProgress = 0;
1953
- dctx->isFrameDecompression = 1;
2031
+ ZSTD_DCtx_resetSession(dctx);
1954
2032
  }
1955
2033
  if ( (reset == ZSTD_reset_parameters)
1956
2034
  || (reset == ZSTD_reset_session_and_parameters) ) {
1957
2035
  RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, "");
1958
2036
  ZSTD_clearDict(dctx);
2037
+ ZSTD_clearDDictSet(dctx);
1959
2038
  ZSTD_DCtx_resetParameters(dctx);
1960
2039
  }
1961
2040
  return 0;
@@ -2074,7 +2153,7 @@ static size_t ZSTD_decompressContinueStream(
2074
2153
  size_t const dstSize = isSkipFrame ? 0 : (size_t)(oend - *op);
2075
2154
  size_t const decodedSize = ZSTD_decompressContinue(zds, *op, dstSize, src, srcSize);
2076
2155
  FORWARD_IF_ERROR(decodedSize, "");
2077
- *op += decodedSize;
2156
+ *op = (char*)ZSTD_maybeNullPtrAdd(*op, (ptrdiff_t)decodedSize);
2078
2157
  /* Flushing is not needed. */
2079
2158
  zds->streamStage = zdss_read;
2080
2159
  assert(*op <= oend);
@@ -2085,6 +2164,8 @@ static size_t ZSTD_decompressContinueStream(
2085
2164
 
2086
2165
  size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
2087
2166
  {
2167
+ const ZSTD_DDict* frameDDict = NULL;
2168
+ int const useDDictSet = zds->refMultipleDDicts && zds->ddictSet != NULL;
2088
2169
  const char* const src = (const char*)input->src;
2089
2170
  const char* const istart = input->pos != 0 ? src + input->pos : src;
2090
2171
  const char* const iend = input->size != 0 ? src + input->size : src;
@@ -2124,7 +2205,12 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
2124
2205
  zds->expectedOutBuffer = *output;
2125
2206
  ZSTD_FALLTHROUGH;
2126
2207
 
2127
- case zdss_loadHeader :
2208
+ case zdss_loadHeader : {
2209
+ /* Legacy dispatch and the one-shot decoder both start at istart.
2210
+ * Equality means no header bytes came from an earlier call.
2211
+ * Check istart first to avoid subtracting NULL on empty input. */
2212
+ int const headerFromThisCall = istart != NULL
2213
+ && (size_t)(ip - istart) == zds->lhSize;
2128
2214
  DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32)(iend - ip));
2129
2215
  #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
2130
2216
  if (zds->legacyVersion) {
@@ -2136,16 +2222,16 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
2136
2222
  } }
2137
2223
  #endif
2138
2224
  { size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format);
2139
- if (zds->refMultipleDDicts && zds->ddictSet) {
2140
- ZSTD_DCtx_selectFrameDDict(zds);
2141
- }
2142
2225
  if (ZSTD_isError(hSize)) {
2143
2226
  #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1)
2144
- U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart);
2227
+ U32 const legacyVersion = headerFromThisCall
2228
+ ? ZSTD_isLegacy(istart, (size_t)(iend-istart)) : 0;
2145
2229
  if (legacyVersion) {
2146
- ZSTD_DDict const* const ddict = ZSTD_getDDict(zds);
2230
+ ZSTD_DDict const* const ddict = useDDictSet ? NULL : ZSTD_getDDict(zds);
2147
2231
  const void* const dict = ddict ? ZSTD_DDict_dictContent(ddict) : NULL;
2148
2232
  size_t const dictSize = ddict ? ZSTD_DDict_dictSize(ddict) : 0;
2233
+ RETURN_ERROR_IF(useDDictSet, parameter_unsupported,
2234
+ "DDict-set selection is not supported for legacy frames");
2149
2235
  DEBUGLOG(5, "ZSTD_decompressStream: detected legacy version v0.%u", legacyVersion);
2150
2236
  RETURN_ERROR_IF(zds->staticSize, memory_allocation,
2151
2237
  "legacy support is incompatible with static dctx");
@@ -2182,17 +2268,27 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
2182
2268
  break;
2183
2269
  } }
2184
2270
 
2185
- /* check for single-pass mode opportunity */
2186
- if (zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
2271
+ /* Select only after a complete ordinary frame header is available,
2272
+ * before any dictionary-backed decoding state is installed. */
2273
+ frameDDict = NULL;
2274
+ if (useDDictSet && zds->fParams.frameType == ZSTD_frame) {
2275
+ FORWARD_IF_ERROR(ZSTD_DCtx_getFrameDDict(zds, zds->fParams.dictID, &frameDDict), "");
2276
+ }
2277
+
2278
+ /* A split header needs buffered decoding even if the rest of
2279
+ * the frame is contiguous in this input. */
2280
+ if (headerFromThisCall
2281
+ && zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN
2187
2282
  && zds->fParams.frameType != ZSTD_skippableFrame
2188
2283
  && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) {
2189
2284
  size_t const cSize = ZSTD_findFrameCompressedSize_advanced(istart, (size_t)(iend-istart), zds->format);
2190
2285
  if (cSize <= (size_t)(iend-istart)) {
2191
2286
  /* shortcut : using single-pass mode */
2192
- size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, (size_t)(oend-op), istart, cSize, ZSTD_getDDict(zds));
2287
+ /* Not ZSTD_decompress_usingDDict(), which resets the streaming session */
2288
+ size_t const decompressedSize = ZSTD_decompressMultiFrame(zds, op, (size_t)(oend-op), istart, cSize,
2289
+ NULL, 0, useDDictSet ? frameDDict : ZSTD_getDDict(zds), 0);
2193
2290
  if (ZSTD_isError(decompressedSize)) return decompressedSize;
2194
- DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()");
2195
- assert(istart != NULL);
2291
+ DEBUGLOG(4, "shortcut to single-pass ZSTD_decompressMultiFrame()");
2196
2292
  ip = istart + cSize;
2197
2293
  op = op ? op + decompressedSize : op; /* can occur if frameContentSize = 0 (empty frame) */
2198
2294
  zds->expected = 0;
@@ -2211,7 +2307,8 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
2211
2307
 
2212
2308
  /* Consume header (see ZSTDds_decodeFrameHeader) */
2213
2309
  DEBUGLOG(4, "Consume header");
2214
- FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(zds, ZSTD_getDDict(zds)), "");
2310
+ FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(zds,
2311
+ useDDictSet ? frameDDict : ZSTD_getDDict(zds)), "");
2215
2312
 
2216
2313
  if (zds->format == ZSTD_f_zstd1
2217
2314
  && (MEM_readLE32(zds->headerBuffer) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */
@@ -2219,6 +2316,9 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
2219
2316
  zds->stage = ZSTDds_skipFrame;
2220
2317
  } else {
2221
2318
  FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize), "");
2319
+ /* Unlike the bufferless API, this path reads the frame header
2320
+ * without passing it through ZSTD_decompressContinue(). */
2321
+ zds->processedCSize = zds->lhSize;
2222
2322
  zds->expected = ZSTD_blockHeaderSize;
2223
2323
  zds->stage = ZSTDds_decodeBlockHeader;
2224
2324
  }
@@ -2268,6 +2368,7 @@ size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inB
2268
2368
  zds->outBuffSize = neededOutBuffSize;
2269
2369
  } } }
2270
2370
  zds->streamStage = zdss_read;
2371
+ }
2271
2372
  ZSTD_FALLTHROUGH;
2272
2373
 
2273
2374
  case zdss_read: