/src/zstd/lib/decompress/zstd_decompress.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
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 | | |
12 | | /* *************************************************************** |
13 | | * Tuning parameters |
14 | | *****************************************************************/ |
15 | | /*! |
16 | | * HEAPMODE : |
17 | | * Select how default decompression function ZSTD_decompress() allocates its context, |
18 | | * on stack (0), or into heap (1, default; requires malloc()). |
19 | | * Note that functions with explicit context such as ZSTD_decompressDCtx() are unaffected. |
20 | | */ |
21 | | #ifndef ZSTD_HEAPMODE |
22 | | # define ZSTD_HEAPMODE 1 |
23 | | #endif |
24 | | |
25 | | /*! |
26 | | * LEGACY_SUPPORT : |
27 | | * if set to 1+, ZSTD_decompress() can decode older formats (v0.1+) |
28 | | */ |
29 | | #ifndef ZSTD_LEGACY_SUPPORT |
30 | | # define ZSTD_LEGACY_SUPPORT 0 |
31 | | #endif |
32 | | |
33 | | /*! |
34 | | * MAXWINDOWSIZE_DEFAULT : |
35 | | * maximum window size accepted by DStream __by default__. |
36 | | * Frames requiring more memory will be rejected. |
37 | | * It's possible to set a different limit using ZSTD_DCtx_setMaxWindowSize(). |
38 | | */ |
39 | | #ifndef ZSTD_MAXWINDOWSIZE_DEFAULT |
40 | 45.6k | # define ZSTD_MAXWINDOWSIZE_DEFAULT (((U32)1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) + 1) |
41 | | #endif |
42 | | |
43 | | /*! |
44 | | * NO_FORWARD_PROGRESS_MAX : |
45 | | * maximum allowed nb of calls to ZSTD_decompressStream() |
46 | | * without any forward progress |
47 | | * (defined as: no byte read from input, and no byte flushed to output) |
48 | | * before triggering an error. |
49 | | */ |
50 | | #ifndef ZSTD_NO_FORWARD_PROGRESS_MAX |
51 | 0 | # define ZSTD_NO_FORWARD_PROGRESS_MAX 16 |
52 | | #endif |
53 | | |
54 | | |
55 | | /*-******************************************************* |
56 | | * Dependencies |
57 | | *********************************************************/ |
58 | | #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */ |
59 | | #include "../common/allocations.h" /* ZSTD_customMalloc, ZSTD_customCalloc, ZSTD_customFree */ |
60 | | #include "../common/error_private.h" |
61 | | #include "../common/zstd_internal.h" /* blockProperties_t */ |
62 | | #include "../common/mem.h" /* low level memory routines */ |
63 | | #include "../common/bits.h" /* ZSTD_highbit32 */ |
64 | | #define FSE_STATIC_LINKING_ONLY |
65 | | #include "../common/fse.h" |
66 | | #include "../common/huf.h" |
67 | | #include "../common/xxhash.h" /* XXH64_reset, XXH64_update, XXH64_digest, XXH64 */ |
68 | | #include "zstd_decompress_internal.h" /* ZSTD_DCtx */ |
69 | | #include "zstd_ddict.h" /* ZSTD_DDictDictContent */ |
70 | | #include "zstd_decompress_block.h" /* ZSTD_decompressBlock_internal */ |
71 | | |
72 | | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
73 | | # include "../legacy/zstd_legacy.h" |
74 | | #endif |
75 | | |
76 | | |
77 | | |
78 | | /************************************* |
79 | | * Multiple DDicts Hashset internals * |
80 | | *************************************/ |
81 | | |
82 | 0 | #define DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT 4 |
83 | 0 | #define DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT 3 /* These two constants represent SIZE_MULT/COUNT_MULT load factor without using a float. |
84 | | * Currently, that means a 0.75 load factor. |
85 | | * So, if count * COUNT_MULT / size * SIZE_MULT != 0, then we've exceeded |
86 | | * the load factor of the ddict hash set. |
87 | | */ |
88 | | |
89 | 0 | #define DDICT_HASHSET_TABLE_BASE_SIZE 64 |
90 | 0 | #define DDICT_HASHSET_RESIZE_FACTOR 2 |
91 | | |
92 | | /* Hash function to determine starting position of dict insertion within the table |
93 | | * Returns an index between [0, hashSet->ddictPtrTableSize] |
94 | | */ |
95 | 0 | static size_t ZSTD_DDictHashSet_getIndex(const ZSTD_DDictHashSet* hashSet, U32 dictID) { |
96 | 0 | const U64 hash = XXH64(&dictID, sizeof(U32), 0); |
97 | | /* DDict ptr table size is a multiple of 2, use size - 1 as mask to get index within [0, hashSet->ddictPtrTableSize) */ |
98 | 0 | return hash & (hashSet->ddictPtrTableSize - 1); |
99 | 0 | } |
100 | | |
101 | | /* Adds DDict to a hashset without resizing it. |
102 | | * If inserting a DDict with a dictID that already exists in the set, replaces the one in the set. |
103 | | * Returns 0 if successful, or a zstd error code if something went wrong. |
104 | | */ |
105 | 0 | static size_t ZSTD_DDictHashSet_emplaceDDict(ZSTD_DDictHashSet* hashSet, const ZSTD_DDict* ddict) { |
106 | 0 | const U32 dictID = ZSTD_getDictID_fromDDict(ddict); |
107 | 0 | size_t idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID); |
108 | 0 | const size_t idxRangeMask = hashSet->ddictPtrTableSize - 1; |
109 | 0 | RETURN_ERROR_IF(hashSet->ddictPtrCount == hashSet->ddictPtrTableSize, GENERIC, "Hash set is full!"); |
110 | 0 | DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID, idx); |
111 | 0 | while (hashSet->ddictPtrTable[idx] != NULL) { |
112 | | /* Replace existing ddict if inserting ddict with same dictID */ |
113 | 0 | if (ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]) == dictID) { |
114 | 0 | DEBUGLOG(4, "DictID already exists, replacing rather than adding"); |
115 | 0 | hashSet->ddictPtrTable[idx] = ddict; |
116 | 0 | return 0; |
117 | 0 | } |
118 | 0 | idx &= idxRangeMask; |
119 | 0 | idx++; |
120 | 0 | } |
121 | 0 | DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID, idx); |
122 | 0 | hashSet->ddictPtrTable[idx] = ddict; |
123 | 0 | hashSet->ddictPtrCount++; |
124 | 0 | return 0; |
125 | 0 | } |
126 | | |
127 | | /* Expands hash table by factor of DDICT_HASHSET_RESIZE_FACTOR and |
128 | | * rehashes all values, allocates new table, frees old table. |
129 | | * Returns 0 on success, otherwise a zstd error code. |
130 | | */ |
131 | 0 | static size_t ZSTD_DDictHashSet_expand(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) { |
132 | 0 | size_t newTableSize = hashSet->ddictPtrTableSize * DDICT_HASHSET_RESIZE_FACTOR; |
133 | 0 | const ZSTD_DDict** newTable = (const ZSTD_DDict**)ZSTD_customCalloc(sizeof(ZSTD_DDict*) * newTableSize, customMem); |
134 | 0 | const ZSTD_DDict** oldTable = hashSet->ddictPtrTable; |
135 | 0 | size_t oldTableSize = hashSet->ddictPtrTableSize; |
136 | 0 | size_t i; |
137 | |
|
138 | 0 | DEBUGLOG(4, "Expanding DDict hash table! Old size: %zu new size: %zu", oldTableSize, newTableSize); |
139 | 0 | RETURN_ERROR_IF(!newTable, memory_allocation, "Expanded hashset allocation failed!"); |
140 | 0 | hashSet->ddictPtrTable = newTable; |
141 | 0 | hashSet->ddictPtrTableSize = newTableSize; |
142 | 0 | hashSet->ddictPtrCount = 0; |
143 | 0 | for (i = 0; i < oldTableSize; ++i) { |
144 | 0 | if (oldTable[i] != NULL) { |
145 | 0 | FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet, oldTable[i]), ""); |
146 | 0 | } |
147 | 0 | } |
148 | 0 | ZSTD_customFree((void*)oldTable, customMem); |
149 | 0 | DEBUGLOG(4, "Finished re-hash"); |
150 | 0 | return 0; |
151 | 0 | } |
152 | | |
153 | | /* Fetches a DDict with the given dictID |
154 | | * Returns the ZSTD_DDict* with the requested dictID. If it doesn't exist, then returns NULL. |
155 | | */ |
156 | 0 | static const ZSTD_DDict* ZSTD_DDictHashSet_getDDict(ZSTD_DDictHashSet* hashSet, U32 dictID) { |
157 | 0 | size_t idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID); |
158 | 0 | const size_t idxRangeMask = hashSet->ddictPtrTableSize - 1; |
159 | 0 | DEBUGLOG(4, "Hashed index: for dictID: %u is %zu", dictID, idx); |
160 | 0 | for (;;) { |
161 | 0 | size_t currDictID = ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]); |
162 | 0 | if (currDictID == dictID || currDictID == 0) { |
163 | | /* currDictID == 0 implies a NULL ddict entry */ |
164 | 0 | break; |
165 | 0 | } else { |
166 | 0 | idx &= idxRangeMask; /* Goes to start of table when we reach the end */ |
167 | 0 | idx++; |
168 | 0 | } |
169 | 0 | } |
170 | 0 | DEBUGLOG(4, "Final idx after probing for dictID %u is: %zu", dictID, idx); |
171 | 0 | return hashSet->ddictPtrTable[idx]; |
172 | 0 | } |
173 | | |
174 | | /* Allocates space for and returns a ddict hash set |
175 | | * The hash set's ZSTD_DDict* table has all values automatically set to NULL to begin with. |
176 | | * Returns NULL if allocation failed. |
177 | | */ |
178 | 0 | static ZSTD_DDictHashSet* ZSTD_createDDictHashSet(ZSTD_customMem customMem) { |
179 | 0 | ZSTD_DDictHashSet* ret = (ZSTD_DDictHashSet*)ZSTD_customMalloc(sizeof(ZSTD_DDictHashSet), customMem); |
180 | 0 | DEBUGLOG(4, "Allocating new hash set"); |
181 | 0 | if (!ret) |
182 | 0 | return NULL; |
183 | 0 | ret->ddictPtrTable = (const ZSTD_DDict**)ZSTD_customCalloc(DDICT_HASHSET_TABLE_BASE_SIZE * sizeof(ZSTD_DDict*), customMem); |
184 | 0 | if (!ret->ddictPtrTable) { |
185 | 0 | ZSTD_customFree(ret, customMem); |
186 | 0 | return NULL; |
187 | 0 | } |
188 | 0 | ret->ddictPtrTableSize = DDICT_HASHSET_TABLE_BASE_SIZE; |
189 | 0 | ret->ddictPtrCount = 0; |
190 | 0 | return ret; |
191 | 0 | } |
192 | | |
193 | | /* Frees the table of ZSTD_DDict* within a hashset, then frees the hashset itself. |
194 | | * Note: The ZSTD_DDict* within the table are NOT freed. |
195 | | */ |
196 | 0 | static void ZSTD_freeDDictHashSet(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) { |
197 | 0 | DEBUGLOG(4, "Freeing ddict hash set"); |
198 | 0 | if (hashSet && hashSet->ddictPtrTable) { |
199 | 0 | ZSTD_customFree((void*)hashSet->ddictPtrTable, customMem); |
200 | 0 | } |
201 | 0 | if (hashSet) { |
202 | 0 | ZSTD_customFree(hashSet, customMem); |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | | /* Public function: Adds a DDict into the ZSTD_DDictHashSet, possibly triggering a resize of the hash set. |
207 | | * Returns 0 on success, or a ZSTD error. |
208 | | */ |
209 | 0 | static size_t ZSTD_DDictHashSet_addDDict(ZSTD_DDictHashSet* hashSet, const ZSTD_DDict* ddict, ZSTD_customMem customMem) { |
210 | 0 | DEBUGLOG(4, "Adding dict ID: %u to hashset with - Count: %zu Tablesize: %zu", ZSTD_getDictID_fromDDict(ddict), hashSet->ddictPtrCount, hashSet->ddictPtrTableSize); |
211 | 0 | if (hashSet->ddictPtrCount * DDICT_HASHSET_MAX_LOAD_FACTOR_COUNT_MULT / hashSet->ddictPtrTableSize * DDICT_HASHSET_MAX_LOAD_FACTOR_SIZE_MULT != 0) { |
212 | 0 | FORWARD_IF_ERROR(ZSTD_DDictHashSet_expand(hashSet, customMem), ""); |
213 | 0 | } |
214 | 0 | FORWARD_IF_ERROR(ZSTD_DDictHashSet_emplaceDDict(hashSet, ddict), ""); |
215 | 0 | return 0; |
216 | 0 | } |
217 | | |
218 | | /*-************************************************************* |
219 | | * Context management |
220 | | ***************************************************************/ |
221 | | size_t ZSTD_sizeof_DCtx (const ZSTD_DCtx* dctx) |
222 | 0 | { |
223 | 0 | if (dctx==NULL) return 0; /* support sizeof NULL */ |
224 | 0 | return sizeof(*dctx) |
225 | 0 | + ZSTD_sizeof_DDict(dctx->ddictLocal) |
226 | 0 | + dctx->inBuffSize + dctx->outBuffSize; |
227 | 0 | } |
228 | | |
229 | 0 | size_t ZSTD_estimateDCtxSize(void) { return sizeof(ZSTD_DCtx); } |
230 | | |
231 | | |
232 | | static size_t ZSTD_startingInputLength(ZSTD_format_e format) |
233 | 174k | { |
234 | 174k | size_t const startingInputLength = ZSTD_FRAMEHEADERSIZE_PREFIX(format); |
235 | | /* only supports formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless */ |
236 | 174k | assert( (format == ZSTD_f_zstd1) || (format == ZSTD_f_zstd1_magicless) ); |
237 | 174k | return startingInputLength; |
238 | 174k | } |
239 | | |
240 | | static void ZSTD_DCtx_resetParameters(ZSTD_DCtx* dctx) |
241 | 45.6k | { |
242 | 45.6k | assert(dctx->streamStage == zdss_init); |
243 | 45.6k | dctx->format = ZSTD_f_zstd1; |
244 | 45.6k | dctx->maxWindowSize = ZSTD_MAXWINDOWSIZE_DEFAULT; |
245 | 45.6k | dctx->outBufferMode = ZSTD_bm_buffered; |
246 | 45.6k | dctx->forceIgnoreChecksum = ZSTD_d_validateChecksum; |
247 | 45.6k | dctx->refMultipleDDicts = ZSTD_rmd_refSingleDDict; |
248 | 45.6k | dctx->disableHufAsm = 0; |
249 | 45.6k | dctx->maxBlockSizeParam = 0; |
250 | 45.6k | } |
251 | | |
252 | | static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx) |
253 | 45.6k | { |
254 | 45.6k | dctx->staticSize = 0; |
255 | 45.6k | dctx->ddict = NULL; |
256 | 45.6k | dctx->ddictLocal = NULL; |
257 | 45.6k | dctx->dictEnd = NULL; |
258 | 45.6k | dctx->ddictIsCold = 0; |
259 | 45.6k | dctx->dictUses = ZSTD_dont_use; |
260 | 45.6k | dctx->inBuff = NULL; |
261 | 45.6k | dctx->inBuffSize = 0; |
262 | 45.6k | dctx->outBuffSize = 0; |
263 | 45.6k | dctx->streamStage = zdss_init; |
264 | 45.6k | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
265 | 45.6k | dctx->legacyContext = NULL; |
266 | 45.6k | dctx->previousLegacyVersion = 0; |
267 | 45.6k | #endif |
268 | 45.6k | dctx->noForwardProgress = 0; |
269 | 45.6k | dctx->oversizedDuration = 0; |
270 | 45.6k | dctx->isFrameDecompression = 1; |
271 | 45.6k | #if DYNAMIC_BMI2 |
272 | 45.6k | dctx->bmi2 = ZSTD_cpuSupportsBmi2(); |
273 | 45.6k | #endif |
274 | 45.6k | dctx->ddictSet = NULL; |
275 | 45.6k | ZSTD_DCtx_resetParameters(dctx); |
276 | 45.6k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
277 | 45.6k | dctx->dictContentEndForFuzzing = NULL; |
278 | 45.6k | #endif |
279 | 45.6k | } |
280 | | |
281 | | ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize) |
282 | 0 | { |
283 | 0 | ZSTD_DCtx* const dctx = (ZSTD_DCtx*) workspace; |
284 | |
|
285 | 0 | if ((size_t)workspace & 7) return NULL; /* 8-aligned */ |
286 | 0 | if (workspaceSize < sizeof(ZSTD_DCtx)) return NULL; /* minimum size */ |
287 | | |
288 | 0 | ZSTD_initDCtx_internal(dctx); |
289 | 0 | dctx->staticSize = workspaceSize; |
290 | 0 | dctx->inBuff = (char*)(dctx+1); |
291 | 0 | return dctx; |
292 | 0 | } |
293 | | |
294 | 45.6k | static ZSTD_DCtx* ZSTD_createDCtx_internal(ZSTD_customMem customMem) { |
295 | 45.6k | if ((!customMem.customAlloc) ^ (!customMem.customFree)) return NULL; |
296 | | |
297 | 45.6k | { ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_customMalloc(sizeof(*dctx), customMem); |
298 | 45.6k | if (!dctx) return NULL; |
299 | 45.6k | dctx->customMem = customMem; |
300 | 45.6k | ZSTD_initDCtx_internal(dctx); |
301 | 45.6k | return dctx; |
302 | 45.6k | } |
303 | 45.6k | } |
304 | | |
305 | | ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem) |
306 | 0 | { |
307 | 0 | return ZSTD_createDCtx_internal(customMem); |
308 | 0 | } |
309 | | |
310 | | ZSTD_DCtx* ZSTD_createDCtx(void) |
311 | 0 | { |
312 | 0 | DEBUGLOG(3, "ZSTD_createDCtx"); |
313 | 0 | return ZSTD_createDCtx_internal(ZSTD_defaultCMem); |
314 | 0 | } |
315 | | |
316 | | static void ZSTD_clearDict(ZSTD_DCtx* dctx) |
317 | 84.4k | { |
318 | 84.4k | ZSTD_freeDDict(dctx->ddictLocal); |
319 | 84.4k | dctx->ddictLocal = NULL; |
320 | 84.4k | dctx->ddict = NULL; |
321 | 84.4k | dctx->dictUses = ZSTD_dont_use; |
322 | 84.4k | } |
323 | | |
324 | | size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx) |
325 | 45.6k | { |
326 | 45.6k | if (dctx==NULL) return 0; /* support free on NULL */ |
327 | 45.6k | RETURN_ERROR_IF(dctx->staticSize, memory_allocation, "not compatible with static DCtx"); |
328 | 45.6k | { ZSTD_customMem const cMem = dctx->customMem; |
329 | 45.6k | ZSTD_clearDict(dctx); |
330 | 45.6k | ZSTD_customFree(dctx->inBuff, cMem); |
331 | 45.6k | dctx->inBuff = NULL; |
332 | 45.6k | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
333 | 45.6k | if (dctx->legacyContext) |
334 | 25.5k | ZSTD_freeLegacyStreamContext(dctx->legacyContext, dctx->previousLegacyVersion); |
335 | 45.6k | #endif |
336 | 45.6k | if (dctx->ddictSet) { |
337 | 0 | ZSTD_freeDDictHashSet(dctx->ddictSet, cMem); |
338 | 0 | dctx->ddictSet = NULL; |
339 | 0 | } |
340 | 45.6k | ZSTD_customFree(dctx, cMem); |
341 | 45.6k | return 0; |
342 | 45.6k | } |
343 | 45.6k | } |
344 | | |
345 | | /* no longer useful */ |
346 | | void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx) |
347 | 0 | { |
348 | 0 | size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx); |
349 | 0 | ZSTD_memcpy(dstDCtx, srcDCtx, toCopy); /* no need to copy workspace */ |
350 | 0 | } |
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. |
355 | | * |
356 | | * If no DDict is found, then no action is taken, and the ZSTD_DCtx::ddict remains as-is. |
357 | | * |
358 | | * ZSTD_d_refMultipleDDicts must be enabled for this function to be called. |
359 | | */ |
360 | 0 | static void ZSTD_DCtx_selectFrameDDict(ZSTD_DCtx* dctx) { |
361 | 0 | assert(dctx->refMultipleDDicts && dctx->ddictSet); |
362 | 0 | DEBUGLOG(4, "Adjusting DDict based on requested dict ID from frame"); |
363 | 0 | if (dctx->ddict) { |
364 | 0 | const ZSTD_DDict* frameDDict = ZSTD_DDictHashSet_getDDict(dctx->ddictSet, dctx->fParams.dictID); |
365 | 0 | if (frameDDict) { |
366 | 0 | DEBUGLOG(4, "DDict found!"); |
367 | 0 | ZSTD_clearDict(dctx); |
368 | 0 | dctx->dictID = dctx->fParams.dictID; |
369 | 0 | dctx->ddict = frameDDict; |
370 | 0 | dctx->dictUses = ZSTD_use_indefinitely; |
371 | 0 | } |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | | |
376 | | /*-************************************************************* |
377 | | * Frame header decoding |
378 | | ***************************************************************/ |
379 | | |
380 | | /*! ZSTD_isFrame() : |
381 | | * Tells if the content of `buffer` starts with a valid Frame Identifier. |
382 | | * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. |
383 | | * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. |
384 | | * Note 3 : Skippable Frame Identifiers are considered valid. */ |
385 | | unsigned ZSTD_isFrame(const void* buffer, size_t size) |
386 | 0 | { |
387 | 0 | if (size < ZSTD_FRAMEIDSIZE) return 0; |
388 | 0 | { U32 const magic = MEM_readLE32(buffer); |
389 | 0 | if (magic == ZSTD_MAGICNUMBER) return 1; |
390 | 0 | if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1; |
391 | 0 | } |
392 | 0 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
393 | 0 | if (ZSTD_isLegacy(buffer, size)) return 1; |
394 | 0 | #endif |
395 | 0 | return 0; |
396 | 0 | } |
397 | | |
398 | | /*! ZSTD_isSkippableFrame() : |
399 | | * Tells if the content of `buffer` starts with a valid Frame Identifier for a skippable frame. |
400 | | * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. |
401 | | */ |
402 | | unsigned ZSTD_isSkippableFrame(const void* buffer, size_t size) |
403 | 0 | { |
404 | 0 | if (size < ZSTD_FRAMEIDSIZE) return 0; |
405 | 0 | { U32 const magic = MEM_readLE32(buffer); |
406 | 0 | if ((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) return 1; |
407 | 0 | } |
408 | 0 | return 0; |
409 | 0 | } |
410 | | |
411 | | /** ZSTD_frameHeaderSize_internal() : |
412 | | * srcSize must be large enough to reach header size fields. |
413 | | * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless. |
414 | | * @return : size of the Frame Header |
415 | | * or an error code, which can be tested with ZSTD_isError() */ |
416 | | static size_t ZSTD_frameHeaderSize_internal(const void* src, size_t srcSize, ZSTD_format_e format) |
417 | 41.5k | { |
418 | 41.5k | size_t const minInputSize = ZSTD_startingInputLength(format); |
419 | 41.5k | RETURN_ERROR_IF(srcSize < minInputSize, srcSize_wrong, ""); |
420 | | |
421 | 41.5k | { BYTE const fhd = ((const BYTE*)src)[minInputSize-1]; |
422 | 41.5k | U32 const dictID= fhd & 3; |
423 | 41.5k | U32 const singleSegment = (fhd >> 5) & 1; |
424 | 41.5k | U32 const fcsId = fhd >> 6; |
425 | 41.5k | return minInputSize + !singleSegment |
426 | 41.5k | + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId] |
427 | 41.5k | + (singleSegment && !fcsId); |
428 | 41.5k | } |
429 | 41.5k | } |
430 | | |
431 | | /** ZSTD_frameHeaderSize() : |
432 | | * srcSize must be >= ZSTD_frameHeaderSize_prefix. |
433 | | * @return : size of the Frame Header, |
434 | | * or an error code (if srcSize is too small) */ |
435 | | size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize) |
436 | 0 | { |
437 | 0 | return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_f_zstd1); |
438 | 0 | } |
439 | | |
440 | | |
441 | | /** ZSTD_getFrameHeader_advanced() : |
442 | | * decode Frame Header, or require larger `srcSize`. |
443 | | * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless |
444 | | * @return : 0, `zfhPtr` is correctly filled, |
445 | | * >0, `srcSize` is too small, value is wanted `srcSize` amount, |
446 | | ** or an error code, which can be tested using ZSTD_isError() */ |
447 | | size_t ZSTD_getFrameHeader_advanced(ZSTD_FrameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format) |
448 | 119k | { |
449 | 119k | const BYTE* ip = (const BYTE*)src; |
450 | 119k | size_t const minInputSize = ZSTD_startingInputLength(format); |
451 | | |
452 | 119k | DEBUGLOG(5, "ZSTD_getFrameHeader_advanced: minInputSize = %zu, srcSize = %zu", minInputSize, srcSize); |
453 | | |
454 | 119k | if (srcSize > 0) { |
455 | | /* note : technically could be considered an assert(), since it's an invalid entry */ |
456 | 73.4k | RETURN_ERROR_IF(src==NULL, GENERIC, "invalid parameter : src==NULL, but srcSize>0"); |
457 | 73.4k | } |
458 | 119k | if (srcSize < minInputSize) { |
459 | 45.8k | if (srcSize > 0 && format != ZSTD_f_zstd1_magicless) { |
460 | | /* when receiving less than @minInputSize bytes, |
461 | | * control these bytes at least correspond to a supported magic number |
462 | | * in order to error out early if they don't. |
463 | | **/ |
464 | 241 | size_t const toCopy = MIN(4, srcSize); |
465 | 241 | unsigned char hbuf[4]; MEM_writeLE32(hbuf, ZSTD_MAGICNUMBER); |
466 | 241 | assert(src != NULL); |
467 | 241 | ZSTD_memcpy(hbuf, src, toCopy); |
468 | 241 | if ( MEM_readLE32(hbuf) != ZSTD_MAGICNUMBER ) { |
469 | | /* not a zstd frame : let's check if it's a skippable frame */ |
470 | 215 | MEM_writeLE32(hbuf, ZSTD_MAGIC_SKIPPABLE_START); |
471 | 215 | ZSTD_memcpy(hbuf, src, toCopy); |
472 | 215 | if ((MEM_readLE32(hbuf) & ZSTD_MAGIC_SKIPPABLE_MASK) != ZSTD_MAGIC_SKIPPABLE_START) { |
473 | 202 | RETURN_ERROR(prefix_unknown, |
474 | 202 | "first bytes don't correspond to any supported magic number"); |
475 | 202 | } } } |
476 | 45.6k | return minInputSize; |
477 | 45.8k | } |
478 | | |
479 | 73.2k | ZSTD_memset(zfhPtr, 0, sizeof(*zfhPtr)); /* not strictly necessary, but static analyzers may not understand that zfhPtr will be read only if return value is zero, since they are 2 different signals */ |
480 | 73.2k | if ( (format != ZSTD_f_zstd1_magicless) |
481 | 73.2k | && (MEM_readLE32(src) != ZSTD_MAGICNUMBER) ) { |
482 | 32.1k | if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { |
483 | | /* skippable frame */ |
484 | 238 | if (srcSize < ZSTD_SKIPPABLEHEADERSIZE) |
485 | 121 | return ZSTD_SKIPPABLEHEADERSIZE; /* magic number + frame length */ |
486 | 117 | ZSTD_memset(zfhPtr, 0, sizeof(*zfhPtr)); |
487 | 117 | zfhPtr->frameType = ZSTD_skippableFrame; |
488 | 117 | zfhPtr->dictID = MEM_readLE32(src) - ZSTD_MAGIC_SKIPPABLE_START; |
489 | 117 | zfhPtr->headerSize = ZSTD_SKIPPABLEHEADERSIZE; |
490 | 117 | zfhPtr->frameContentSize = MEM_readLE32((const char *)src + ZSTD_FRAMEIDSIZE); |
491 | 117 | return 0; |
492 | 238 | } |
493 | 31.9k | RETURN_ERROR(prefix_unknown, ""); |
494 | 31.9k | } |
495 | | |
496 | | /* ensure there is enough `srcSize` to fully read/decode frame header */ |
497 | 41.0k | { size_t const fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format); |
498 | 41.0k | if (srcSize < fhsize) return fhsize; |
499 | 27.7k | zfhPtr->headerSize = (U32)fhsize; |
500 | 27.7k | } |
501 | | |
502 | 0 | { BYTE const fhdByte = ip[minInputSize-1]; |
503 | 27.7k | size_t pos = minInputSize; |
504 | 27.7k | U32 const dictIDSizeCode = fhdByte&3; |
505 | 27.7k | U32 const checksumFlag = (fhdByte>>2)&1; |
506 | 27.7k | U32 const singleSegment = (fhdByte>>5)&1; |
507 | 27.7k | U32 const fcsID = fhdByte>>6; |
508 | 27.7k | U64 windowSize = 0; |
509 | 27.7k | U32 dictID = 0; |
510 | 27.7k | U64 frameContentSize = ZSTD_CONTENTSIZE_UNKNOWN; |
511 | 27.7k | RETURN_ERROR_IF((fhdByte & 0x08) != 0, frameParameter_unsupported, |
512 | 27.7k | "reserved bits, must be zero"); |
513 | | |
514 | 27.6k | if (!singleSegment) { |
515 | 24.1k | BYTE const wlByte = ip[pos++]; |
516 | 24.1k | U32 const windowLog = (wlByte >> 3) + ZSTD_WINDOWLOG_ABSOLUTEMIN; |
517 | 24.1k | RETURN_ERROR_IF(windowLog > ZSTD_WINDOWLOG_MAX, frameParameter_windowTooLarge, ""); |
518 | 24.1k | windowSize = (1ULL << windowLog); |
519 | 24.1k | windowSize += (windowSize >> 3) * (wlByte&7); |
520 | 24.1k | } |
521 | 27.6k | switch(dictIDSizeCode) |
522 | 27.6k | { |
523 | 0 | default: |
524 | 0 | assert(0); /* impossible */ |
525 | 0 | ZSTD_FALLTHROUGH; |
526 | 6.85k | case 0 : break; |
527 | 1.59k | case 1 : dictID = ip[pos]; pos++; break; |
528 | 4.01k | case 2 : dictID = MEM_readLE16(ip+pos); pos+=2; break; |
529 | 15.1k | case 3 : dictID = MEM_readLE32(ip+pos); pos+=4; break; |
530 | 27.6k | } |
531 | 27.6k | switch(fcsID) |
532 | 27.6k | { |
533 | 0 | default: |
534 | 0 | assert(0); /* impossible */ |
535 | 0 | ZSTD_FALLTHROUGH; |
536 | 5.73k | case 0 : if (singleSegment) frameContentSize = ip[pos]; break; |
537 | 1.87k | case 1 : frameContentSize = MEM_readLE16(ip+pos)+256; break; |
538 | 852 | case 2 : frameContentSize = MEM_readLE32(ip+pos); break; |
539 | 19.1k | case 3 : frameContentSize = MEM_readLE64(ip+pos); break; |
540 | 27.6k | } |
541 | 27.6k | if (singleSegment) windowSize = frameContentSize; |
542 | | |
543 | 27.6k | zfhPtr->frameType = ZSTD_frame; |
544 | 27.6k | zfhPtr->frameContentSize = frameContentSize; |
545 | 27.6k | zfhPtr->windowSize = windowSize; |
546 | 27.6k | zfhPtr->blockSizeMax = (unsigned) MIN(windowSize, ZSTD_BLOCKSIZE_MAX); |
547 | 27.6k | zfhPtr->dictID = dictID; |
548 | 27.6k | zfhPtr->checksumFlag = checksumFlag; |
549 | 27.6k | } |
550 | 0 | return 0; |
551 | 27.6k | } |
552 | | |
553 | | /** ZSTD_getFrameHeader() : |
554 | | * decode Frame Header, or require larger `srcSize`. |
555 | | * note : this function does not consume input, it only reads it. |
556 | | * @return : 0, `zfhPtr` is correctly filled, |
557 | | * >0, `srcSize` is too small, value is wanted `srcSize` amount, |
558 | | * or an error code, which can be tested using ZSTD_isError() */ |
559 | | size_t ZSTD_getFrameHeader(ZSTD_FrameHeader* zfhPtr, const void* src, size_t srcSize) |
560 | 0 | { |
561 | 0 | return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_f_zstd1); |
562 | 0 | } |
563 | | |
564 | | /** ZSTD_getFrameContentSize() : |
565 | | * compatible with legacy mode |
566 | | * @return : decompressed size of the single frame pointed to be `src` if known, otherwise |
567 | | * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined |
568 | | * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */ |
569 | | unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize) |
570 | 0 | { |
571 | 0 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
572 | 0 | if (ZSTD_isLegacy(src, srcSize)) { |
573 | 0 | unsigned long long const ret = ZSTD_getDecompressedSize_legacy(src, srcSize); |
574 | 0 | return ret == 0 ? ZSTD_CONTENTSIZE_UNKNOWN : ret; |
575 | 0 | } |
576 | 0 | #endif |
577 | 0 | { ZSTD_FrameHeader zfh; |
578 | 0 | if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0) |
579 | 0 | return ZSTD_CONTENTSIZE_ERROR; |
580 | 0 | if (zfh.frameType == ZSTD_skippableFrame) { |
581 | 0 | return 0; |
582 | 0 | } else { |
583 | 0 | return zfh.frameContentSize; |
584 | 0 | } } |
585 | 0 | } |
586 | | |
587 | | static size_t readSkippableFrameSize(void const* src, size_t srcSize) |
588 | 0 | { |
589 | 0 | size_t const skippableHeaderSize = ZSTD_SKIPPABLEHEADERSIZE; |
590 | 0 | U32 sizeU32; |
591 | |
|
592 | 0 | RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong, ""); |
593 | | |
594 | 0 | sizeU32 = MEM_readLE32((BYTE const*)src + ZSTD_FRAMEIDSIZE); |
595 | 0 | RETURN_ERROR_IF((U32)(sizeU32 + ZSTD_SKIPPABLEHEADERSIZE) < sizeU32, |
596 | 0 | frameParameter_unsupported, ""); |
597 | 0 | { size_t const skippableSize = skippableHeaderSize + sizeU32; |
598 | 0 | RETURN_ERROR_IF(skippableSize > srcSize, srcSize_wrong, ""); |
599 | 0 | return skippableSize; |
600 | 0 | } |
601 | 0 | } |
602 | | |
603 | | /*! ZSTD_readSkippableFrame() : |
604 | | * Retrieves content of a skippable frame, and writes it to dst buffer. |
605 | | * |
606 | | * The parameter magicVariant will receive the magicVariant that was supplied when the frame was written, |
607 | | * i.e. magicNumber - ZSTD_MAGIC_SKIPPABLE_START. This can be NULL if the caller is not interested |
608 | | * in the magicVariant. |
609 | | * |
610 | | * Returns an error if destination buffer is not large enough, or if this is not a valid skippable frame. |
611 | | * |
612 | | * @return : number of bytes written or a ZSTD error. |
613 | | */ |
614 | | size_t ZSTD_readSkippableFrame(void* dst, size_t dstCapacity, |
615 | | unsigned* magicVariant, /* optional, can be NULL */ |
616 | | const void* src, size_t srcSize) |
617 | 0 | { |
618 | 0 | RETURN_ERROR_IF(srcSize < ZSTD_SKIPPABLEHEADERSIZE, srcSize_wrong, ""); |
619 | | |
620 | 0 | { U32 const magicNumber = MEM_readLE32(src); |
621 | 0 | size_t skippableFrameSize = readSkippableFrameSize(src, srcSize); |
622 | 0 | size_t skippableContentSize = skippableFrameSize - ZSTD_SKIPPABLEHEADERSIZE; |
623 | | |
624 | | /* check input validity */ |
625 | 0 | RETURN_ERROR_IF(!ZSTD_isSkippableFrame(src, srcSize), frameParameter_unsupported, ""); |
626 | 0 | RETURN_ERROR_IF(skippableFrameSize < ZSTD_SKIPPABLEHEADERSIZE || skippableFrameSize > srcSize, srcSize_wrong, ""); |
627 | 0 | RETURN_ERROR_IF(skippableContentSize > dstCapacity, dstSize_tooSmall, ""); |
628 | | |
629 | | /* deliver payload */ |
630 | 0 | if (skippableContentSize > 0 && dst != NULL) |
631 | 0 | ZSTD_memcpy(dst, (const BYTE *)src + ZSTD_SKIPPABLEHEADERSIZE, skippableContentSize); |
632 | 0 | if (magicVariant != NULL) |
633 | 0 | *magicVariant = magicNumber - ZSTD_MAGIC_SKIPPABLE_START; |
634 | 0 | return skippableContentSize; |
635 | 0 | } |
636 | 0 | } |
637 | | |
638 | | /** ZSTD_findDecompressedSize() : |
639 | | * `srcSize` must be the exact length of some number of ZSTD compressed and/or |
640 | | * skippable frames |
641 | | * note: compatible with legacy mode |
642 | | * @return : decompressed size of the frames contained */ |
643 | | unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize) |
644 | 0 | { |
645 | 0 | unsigned long long totalDstSize = 0; |
646 | |
|
647 | 0 | while (srcSize >= ZSTD_startingInputLength(ZSTD_f_zstd1)) { |
648 | 0 | U32 const magicNumber = MEM_readLE32(src); |
649 | |
|
650 | 0 | if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { |
651 | 0 | size_t const skippableSize = readSkippableFrameSize(src, srcSize); |
652 | 0 | if (ZSTD_isError(skippableSize)) return ZSTD_CONTENTSIZE_ERROR; |
653 | 0 | assert(skippableSize <= srcSize); |
654 | |
|
655 | 0 | src = (const BYTE *)src + skippableSize; |
656 | 0 | srcSize -= skippableSize; |
657 | 0 | continue; |
658 | 0 | } |
659 | | |
660 | 0 | { unsigned long long const fcs = ZSTD_getFrameContentSize(src, srcSize); |
661 | 0 | if (fcs >= ZSTD_CONTENTSIZE_ERROR) return fcs; |
662 | | |
663 | 0 | if (totalDstSize + fcs < totalDstSize) |
664 | 0 | return ZSTD_CONTENTSIZE_ERROR; /* check for overflow */ |
665 | 0 | totalDstSize += fcs; |
666 | 0 | } |
667 | | /* skip to next frame */ |
668 | 0 | { size_t const frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize); |
669 | 0 | if (ZSTD_isError(frameSrcSize)) return ZSTD_CONTENTSIZE_ERROR; |
670 | 0 | assert(frameSrcSize <= srcSize); |
671 | |
|
672 | 0 | src = (const BYTE *)src + frameSrcSize; |
673 | 0 | srcSize -= frameSrcSize; |
674 | 0 | } |
675 | 0 | } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */ |
676 | | |
677 | 0 | if (srcSize) return ZSTD_CONTENTSIZE_ERROR; |
678 | | |
679 | 0 | return totalDstSize; |
680 | 0 | } |
681 | | |
682 | | /** ZSTD_getDecompressedSize() : |
683 | | * compatible with legacy mode |
684 | | * @return : decompressed size if known, 0 otherwise |
685 | | note : 0 can mean any of the following : |
686 | | - frame content is empty |
687 | | - decompressed size field is not present in frame header |
688 | | - frame header unknown / not supported |
689 | | - frame header not complete (`srcSize` too small) */ |
690 | | unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize) |
691 | 0 | { |
692 | 0 | unsigned long long const ret = ZSTD_getFrameContentSize(src, srcSize); |
693 | 0 | ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_ERROR < ZSTD_CONTENTSIZE_UNKNOWN); |
694 | 0 | return (ret >= ZSTD_CONTENTSIZE_ERROR) ? 0 : ret; |
695 | 0 | } |
696 | | |
697 | | |
698 | | /** ZSTD_decodeFrameHeader() : |
699 | | * `headerSize` must be the size provided by ZSTD_frameHeaderSize(). |
700 | | * If multiple DDict references are enabled, also will choose the correct DDict to use. |
701 | | * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */ |
702 | | static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t headerSize) |
703 | 13.2k | { |
704 | 13.2k | size_t const result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format); |
705 | 13.2k | if (ZSTD_isError(result)) return result; /* invalid header */ |
706 | 13.2k | RETURN_ERROR_IF(result>0, srcSize_wrong, "headerSize too small"); |
707 | | |
708 | | /* Reference DDict requested by frame if dctx references multiple ddicts */ |
709 | 13.2k | if (dctx->refMultipleDDicts == ZSTD_rmd_refMultipleDDicts && dctx->ddictSet) { |
710 | 0 | ZSTD_DCtx_selectFrameDDict(dctx); |
711 | 0 | } |
712 | | |
713 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
714 | | /* Skip the dictID check in fuzzing mode, because it makes the search |
715 | | * harder. |
716 | | */ |
717 | | RETURN_ERROR_IF(dctx->fParams.dictID && (dctx->dictID != dctx->fParams.dictID), |
718 | | dictionary_wrong, ""); |
719 | | #endif |
720 | 13.2k | dctx->validateChecksum = (dctx->fParams.checksumFlag && !dctx->forceIgnoreChecksum) ? 1 : 0; |
721 | 13.2k | if (dctx->validateChecksum) XXH64_reset(&dctx->xxhState, 0); |
722 | 13.2k | dctx->processedCSize += headerSize; |
723 | 13.2k | return 0; |
724 | 13.2k | } |
725 | | |
726 | | static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret) |
727 | 701 | { |
728 | 701 | ZSTD_frameSizeInfo frameSizeInfo; |
729 | 701 | frameSizeInfo.compressedSize = ret; |
730 | 701 | frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR; |
731 | 701 | return frameSizeInfo; |
732 | 701 | } |
733 | | |
734 | | static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize, ZSTD_format_e format) |
735 | 1.18k | { |
736 | 1.18k | ZSTD_frameSizeInfo frameSizeInfo; |
737 | 1.18k | ZSTD_memset(&frameSizeInfo, 0, sizeof(ZSTD_frameSizeInfo)); |
738 | | |
739 | 1.18k | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
740 | 1.18k | if (format == ZSTD_f_zstd1 && ZSTD_isLegacy(src, srcSize)) |
741 | 0 | return ZSTD_findFrameSizeInfoLegacy(src, srcSize); |
742 | 1.18k | #endif |
743 | | |
744 | 1.18k | if (format == ZSTD_f_zstd1 && (srcSize >= ZSTD_SKIPPABLEHEADERSIZE) |
745 | 1.18k | && (MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { |
746 | 0 | frameSizeInfo.compressedSize = readSkippableFrameSize(src, srcSize); |
747 | 0 | assert(ZSTD_isError(frameSizeInfo.compressedSize) || |
748 | 0 | frameSizeInfo.compressedSize <= srcSize); |
749 | 0 | return frameSizeInfo; |
750 | 1.18k | } else { |
751 | 1.18k | const BYTE* ip = (const BYTE*)src; |
752 | 1.18k | const BYTE* const ipstart = ip; |
753 | 1.18k | size_t remainingSize = srcSize; |
754 | 1.18k | size_t nbBlocks = 0; |
755 | 1.18k | ZSTD_FrameHeader zfh; |
756 | | |
757 | | /* Extract Frame Header */ |
758 | 1.18k | { size_t const ret = ZSTD_getFrameHeader_advanced(&zfh, src, srcSize, format); |
759 | 1.18k | if (ZSTD_isError(ret)) |
760 | 0 | return ZSTD_errorFrameSizeInfo(ret); |
761 | 1.18k | if (ret > 0) |
762 | 0 | return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong)); |
763 | 1.18k | } |
764 | | |
765 | 1.18k | ip += zfh.headerSize; |
766 | 1.18k | remainingSize -= zfh.headerSize; |
767 | | |
768 | | /* Iterate over each block */ |
769 | 17.7k | while (1) { |
770 | 17.7k | blockProperties_t blockProperties; |
771 | 17.7k | size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties); |
772 | 17.7k | if (ZSTD_isError(cBlockSize)) |
773 | 281 | return ZSTD_errorFrameSizeInfo(cBlockSize); |
774 | | |
775 | 17.4k | if (ZSTD_blockHeaderSize + cBlockSize > remainingSize) |
776 | 359 | return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong)); |
777 | | |
778 | 17.1k | ip += ZSTD_blockHeaderSize + cBlockSize; |
779 | 17.1k | remainingSize -= ZSTD_blockHeaderSize + cBlockSize; |
780 | 17.1k | nbBlocks++; |
781 | | |
782 | 17.1k | if (blockProperties.lastBlock) break; |
783 | 17.1k | } |
784 | | |
785 | | /* Final frame content checksum */ |
786 | 546 | if (zfh.checksumFlag) { |
787 | 185 | if (remainingSize < 4) |
788 | 61 | return ZSTD_errorFrameSizeInfo(ERROR(srcSize_wrong)); |
789 | 124 | ip += 4; |
790 | 124 | } |
791 | | |
792 | 485 | frameSizeInfo.nbBlocks = nbBlocks; |
793 | 485 | frameSizeInfo.compressedSize = (size_t)(ip - ipstart); |
794 | 485 | frameSizeInfo.decompressedBound = (zfh.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) |
795 | 485 | ? zfh.frameContentSize |
796 | 485 | : (unsigned long long)nbBlocks * zfh.blockSizeMax; |
797 | 485 | return frameSizeInfo; |
798 | 546 | } |
799 | 1.18k | } |
800 | | |
801 | 1.18k | static size_t ZSTD_findFrameCompressedSize_advanced(const void *src, size_t srcSize, ZSTD_format_e format) { |
802 | 1.18k | ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, format); |
803 | 1.18k | return frameSizeInfo.compressedSize; |
804 | 1.18k | } |
805 | | |
806 | | /** ZSTD_findFrameCompressedSize() : |
807 | | * See docs in zstd.h |
808 | | * Note: compatible with legacy mode */ |
809 | | size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize) |
810 | 0 | { |
811 | 0 | return ZSTD_findFrameCompressedSize_advanced(src, srcSize, ZSTD_f_zstd1); |
812 | 0 | } |
813 | | |
814 | | /** ZSTD_decompressBound() : |
815 | | * compatible with legacy mode |
816 | | * `src` must point to the start of a ZSTD frame or a skippable frame |
817 | | * `srcSize` must be at least as large as the frame contained |
818 | | * @return : the maximum decompressed size of the compressed source |
819 | | */ |
820 | | unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize) |
821 | 0 | { |
822 | 0 | unsigned long long bound = 0; |
823 | | /* Iterate over each frame */ |
824 | 0 | while (srcSize > 0) { |
825 | 0 | ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, ZSTD_f_zstd1); |
826 | 0 | size_t const compressedSize = frameSizeInfo.compressedSize; |
827 | 0 | unsigned long long const decompressedBound = frameSizeInfo.decompressedBound; |
828 | 0 | if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR) |
829 | 0 | return ZSTD_CONTENTSIZE_ERROR; |
830 | 0 | assert(srcSize >= compressedSize); |
831 | 0 | src = (const BYTE*)src + compressedSize; |
832 | 0 | srcSize -= compressedSize; |
833 | 0 | bound += decompressedBound; |
834 | 0 | } |
835 | 0 | return bound; |
836 | 0 | } |
837 | | |
838 | | size_t ZSTD_decompressionMargin(void const* src, size_t srcSize) |
839 | 0 | { |
840 | 0 | size_t margin = 0; |
841 | 0 | unsigned maxBlockSize = 0; |
842 | | |
843 | | /* Iterate over each frame */ |
844 | 0 | while (srcSize > 0) { |
845 | 0 | ZSTD_frameSizeInfo const frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, ZSTD_f_zstd1); |
846 | 0 | size_t const compressedSize = frameSizeInfo.compressedSize; |
847 | 0 | unsigned long long const decompressedBound = frameSizeInfo.decompressedBound; |
848 | 0 | ZSTD_FrameHeader zfh; |
849 | |
|
850 | 0 | FORWARD_IF_ERROR(ZSTD_getFrameHeader(&zfh, src, srcSize), ""); |
851 | 0 | if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR) |
852 | 0 | return ERROR(corruption_detected); |
853 | | |
854 | 0 | if (zfh.frameType == ZSTD_frame) { |
855 | | /* Add the frame header to our margin */ |
856 | 0 | margin += zfh.headerSize; |
857 | | /* Add the checksum to our margin */ |
858 | 0 | margin += zfh.checksumFlag ? 4 : 0; |
859 | | /* Add 3 bytes per block */ |
860 | 0 | margin += 3 * frameSizeInfo.nbBlocks; |
861 | | |
862 | | /* Compute the max block size */ |
863 | 0 | maxBlockSize = MAX(maxBlockSize, zfh.blockSizeMax); |
864 | 0 | } else { |
865 | 0 | assert(zfh.frameType == ZSTD_skippableFrame); |
866 | | /* Add the entire skippable frame size to our margin. */ |
867 | 0 | margin += compressedSize; |
868 | 0 | } |
869 | |
|
870 | 0 | assert(srcSize >= compressedSize); |
871 | 0 | src = (const BYTE*)src + compressedSize; |
872 | 0 | srcSize -= compressedSize; |
873 | 0 | } |
874 | | |
875 | | /* Add the max block size back to the margin. */ |
876 | 0 | margin += maxBlockSize; |
877 | |
|
878 | 0 | return margin; |
879 | 0 | } |
880 | | |
881 | | /*-************************************************************* |
882 | | * Frame decoding |
883 | | ***************************************************************/ |
884 | | |
885 | | /** ZSTD_insertBlock() : |
886 | | * insert `src` block into `dctx` history. Useful to track uncompressed blocks. */ |
887 | | size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize) |
888 | 0 | { |
889 | 0 | DEBUGLOG(5, "ZSTD_insertBlock: %u bytes", (unsigned)blockSize); |
890 | 0 | ZSTD_checkContinuity(dctx, blockStart, blockSize); |
891 | 0 | dctx->previousDstEnd = (const char*)blockStart + blockSize; |
892 | 0 | return blockSize; |
893 | 0 | } |
894 | | |
895 | | |
896 | | static size_t ZSTD_copyRawBlock(void* dst, size_t dstCapacity, |
897 | | const void* src, size_t srcSize) |
898 | 22.5k | { |
899 | 22.5k | DEBUGLOG(5, "ZSTD_copyRawBlock"); |
900 | 22.5k | RETURN_ERROR_IF(srcSize > dstCapacity, dstSize_tooSmall, ""); |
901 | 22.5k | if (dst == NULL) { |
902 | 0 | if (srcSize == 0) return 0; |
903 | 0 | RETURN_ERROR(dstBuffer_null, ""); |
904 | 0 | } |
905 | 22.5k | ZSTD_memmove(dst, src, srcSize); |
906 | 22.5k | return srcSize; |
907 | 22.5k | } |
908 | | |
909 | | static size_t ZSTD_setRleBlock(void* dst, size_t dstCapacity, |
910 | | BYTE b, |
911 | | size_t regenSize) |
912 | 4.17k | { |
913 | 4.17k | RETURN_ERROR_IF(regenSize > dstCapacity, dstSize_tooSmall, ""); |
914 | 4.07k | if (dst == NULL) { |
915 | 0 | if (regenSize == 0) return 0; |
916 | 0 | RETURN_ERROR(dstBuffer_null, ""); |
917 | 0 | } |
918 | 4.07k | ZSTD_memset(dst, b, regenSize); |
919 | 4.07k | return regenSize; |
920 | 4.07k | } |
921 | | |
922 | | static void ZSTD_DCtx_trace_end(ZSTD_DCtx const* dctx, U64 uncompressedSize, U64 compressedSize, int streaming) |
923 | 85 | { |
924 | 85 | #if ZSTD_TRACE |
925 | 85 | if (dctx->traceCtx && ZSTD_trace_decompress_end != NULL) { |
926 | 0 | ZSTD_Trace trace; |
927 | 0 | ZSTD_memset(&trace, 0, sizeof(trace)); |
928 | 0 | trace.version = ZSTD_VERSION_NUMBER; |
929 | 0 | trace.streaming = streaming; |
930 | 0 | if (dctx->ddict) { |
931 | 0 | trace.dictionaryID = ZSTD_getDictID_fromDDict(dctx->ddict); |
932 | 0 | trace.dictionarySize = ZSTD_DDict_dictSize(dctx->ddict); |
933 | 0 | trace.dictionaryIsCold = dctx->ddictIsCold; |
934 | 0 | } |
935 | 0 | trace.uncompressedSize = (size_t)uncompressedSize; |
936 | 0 | trace.compressedSize = (size_t)compressedSize; |
937 | 0 | trace.dctx = dctx; |
938 | 0 | ZSTD_trace_decompress_end(dctx->traceCtx, &trace); |
939 | 0 | } |
940 | | #else |
941 | | (void)dctx; |
942 | | (void)uncompressedSize; |
943 | | (void)compressedSize; |
944 | | (void)streaming; |
945 | | #endif |
946 | 85 | } |
947 | | |
948 | | |
949 | | /*! ZSTD_decompressFrame() : |
950 | | * @dctx must be properly initialized |
951 | | * will update *srcPtr and *srcSizePtr, |
952 | | * to make *srcPtr progress by one frame. */ |
953 | | static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx, |
954 | | void* dst, size_t dstCapacity, |
955 | | const void** srcPtr, size_t *srcSizePtr) |
956 | 485 | { |
957 | 485 | const BYTE* const istart = (const BYTE*)(*srcPtr); |
958 | 485 | const BYTE* ip = istart; |
959 | 485 | BYTE* const ostart = (BYTE*)dst; |
960 | 485 | BYTE* const oend = dstCapacity != 0 ? ostart + dstCapacity : ostart; |
961 | 485 | BYTE* op = ostart; |
962 | 485 | size_t remainingSrcSize = *srcSizePtr; |
963 | | |
964 | 485 | DEBUGLOG(4, "ZSTD_decompressFrame (srcSize:%i)", (int)*srcSizePtr); |
965 | | |
966 | | /* check */ |
967 | 485 | RETURN_ERROR_IF( |
968 | 485 | remainingSrcSize < ZSTD_FRAMEHEADERSIZE_MIN(dctx->format)+ZSTD_blockHeaderSize, |
969 | 485 | srcSize_wrong, ""); |
970 | | |
971 | | /* Frame Header */ |
972 | 485 | { size_t const frameHeaderSize = ZSTD_frameHeaderSize_internal( |
973 | 485 | ip, ZSTD_FRAMEHEADERSIZE_PREFIX(dctx->format), dctx->format); |
974 | 485 | if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize; |
975 | 485 | RETURN_ERROR_IF(remainingSrcSize < frameHeaderSize+ZSTD_blockHeaderSize, |
976 | 485 | srcSize_wrong, ""); |
977 | 485 | FORWARD_IF_ERROR( ZSTD_decodeFrameHeader(dctx, ip, frameHeaderSize) , ""); |
978 | 485 | ip += frameHeaderSize; remainingSrcSize -= frameHeaderSize; |
979 | 485 | } |
980 | | |
981 | | /* Shrink the blockSizeMax if enabled */ |
982 | 485 | if (dctx->maxBlockSizeParam != 0) |
983 | 0 | dctx->fParams.blockSizeMax = MIN(dctx->fParams.blockSizeMax, (unsigned)dctx->maxBlockSizeParam); |
984 | | |
985 | | /* Loop on each block */ |
986 | 4.17k | while (1) { |
987 | 4.17k | BYTE* oBlockEnd = oend; |
988 | 4.17k | size_t decodedSize; |
989 | 4.17k | blockProperties_t blockProperties; |
990 | 4.17k | size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSrcSize, &blockProperties); |
991 | 4.17k | if (ZSTD_isError(cBlockSize)) return cBlockSize; |
992 | | |
993 | 4.17k | ip += ZSTD_blockHeaderSize; |
994 | 4.17k | remainingSrcSize -= ZSTD_blockHeaderSize; |
995 | 4.17k | RETURN_ERROR_IF(cBlockSize > remainingSrcSize, srcSize_wrong, ""); |
996 | | |
997 | 4.17k | if (ip >= op && ip < oBlockEnd) { |
998 | | /* We are decompressing in-place. Limit the output pointer so that we |
999 | | * don't overwrite the block that we are currently reading. This will |
1000 | | * fail decompression if the input & output pointers aren't spaced |
1001 | | * far enough apart. |
1002 | | * |
1003 | | * This is important to set, even when the pointers are far enough |
1004 | | * apart, because ZSTD_decompressBlock_internal() can decide to store |
1005 | | * literals in the output buffer, after the block it is decompressing. |
1006 | | * Since we don't want anything to overwrite our input, we have to tell |
1007 | | * ZSTD_decompressBlock_internal to never write past ip. |
1008 | | * |
1009 | | * See ZSTD_allocateLiteralsBuffer() for reference. |
1010 | | */ |
1011 | 0 | oBlockEnd = op + (ip - op); |
1012 | 0 | } |
1013 | | |
1014 | 4.17k | switch(blockProperties.blockType) |
1015 | 4.17k | { |
1016 | 1.15k | case bt_compressed: |
1017 | 1.15k | assert(dctx->isFrameDecompression == 1); |
1018 | 1.15k | decodedSize = ZSTD_decompressBlock_internal(dctx, op, (size_t)(oBlockEnd-op), ip, cBlockSize, not_streaming); |
1019 | 1.15k | break; |
1020 | 2.89k | case bt_raw : |
1021 | | /* Use oend instead of oBlockEnd because this function is safe to overlap. It uses memmove. */ |
1022 | 2.89k | decodedSize = ZSTD_copyRawBlock(op, (size_t)(oend-op), ip, cBlockSize); |
1023 | 2.89k | break; |
1024 | 125 | case bt_rle : |
1025 | 125 | decodedSize = ZSTD_setRleBlock(op, (size_t)(oBlockEnd-op), *ip, blockProperties.origSize); |
1026 | 125 | break; |
1027 | 0 | case bt_reserved : |
1028 | 0 | default: |
1029 | 0 | RETURN_ERROR(corruption_detected, "invalid block type"); |
1030 | 4.17k | } |
1031 | 4.17k | FORWARD_IF_ERROR(decodedSize, "Block decompression failure"); |
1032 | 3.80k | DEBUGLOG(5, "Decompressed block of dSize = %u", (unsigned)decodedSize); |
1033 | 3.80k | if (dctx->validateChecksum) { |
1034 | 2.41k | XXH64_update(&dctx->xxhState, op, decodedSize); |
1035 | 2.41k | } |
1036 | 3.80k | if (decodedSize) /* support dst = NULL,0 */ { |
1037 | 638 | op += decodedSize; |
1038 | 638 | } |
1039 | 3.80k | assert(ip != NULL); |
1040 | 3.80k | ip += cBlockSize; |
1041 | 3.80k | remainingSrcSize -= cBlockSize; |
1042 | 3.80k | if (blockProperties.lastBlock) break; |
1043 | 3.80k | } |
1044 | | |
1045 | 120 | if (dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) { |
1046 | 120 | RETURN_ERROR_IF((U64)(op-ostart) != dctx->fParams.frameContentSize, |
1047 | 120 | corruption_detected, ""); |
1048 | 120 | } |
1049 | 54 | if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */ |
1050 | 37 | RETURN_ERROR_IF(remainingSrcSize<4, checksum_wrong, ""); |
1051 | 37 | if (!dctx->forceIgnoreChecksum) { |
1052 | 37 | U32 const checkCalc = (U32)XXH64_digest(&dctx->xxhState); |
1053 | 37 | U32 checkRead; |
1054 | 37 | checkRead = MEM_readLE32(ip); |
1055 | 37 | RETURN_ERROR_IF(checkRead != checkCalc, checksum_wrong, ""); |
1056 | 37 | } |
1057 | 0 | ip += 4; |
1058 | 0 | remainingSrcSize -= 4; |
1059 | 0 | } |
1060 | 17 | ZSTD_DCtx_trace_end(dctx, (U64)(op-ostart), (U64)(ip-istart), /* streaming */ 0); |
1061 | | /* Allow caller to get size read */ |
1062 | 17 | DEBUGLOG(4, "ZSTD_decompressFrame: decompressed frame of size %i, consuming %i bytes of input", (int)(op-ostart), (int)(ip - (const BYTE*)*srcPtr)); |
1063 | 17 | *srcPtr = ip; |
1064 | 17 | *srcSizePtr = remainingSrcSize; |
1065 | 17 | return (size_t)(op-ostart); |
1066 | 54 | } |
1067 | | |
1068 | | static |
1069 | | ZSTD_ALLOW_POINTER_OVERFLOW_ATTR |
1070 | | size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx, |
1071 | | void* dst, size_t dstCapacity, |
1072 | | const void* src, size_t srcSize, |
1073 | | const void* dict, size_t dictSize, |
1074 | | const ZSTD_DDict* ddict) |
1075 | 485 | { |
1076 | 485 | void* const dststart = dst; |
1077 | 485 | int moreThan1Frame = 0; |
1078 | | |
1079 | 485 | DEBUGLOG(5, "ZSTD_decompressMultiFrame"); |
1080 | 485 | assert(dict==NULL || ddict==NULL); /* either dict or ddict set, not both */ |
1081 | | |
1082 | 485 | if (ddict) { |
1083 | 0 | dict = ZSTD_DDict_dictContent(ddict); |
1084 | 0 | dictSize = ZSTD_DDict_dictSize(ddict); |
1085 | 0 | } |
1086 | | |
1087 | 502 | while (srcSize >= ZSTD_startingInputLength(dctx->format)) { |
1088 | | |
1089 | 485 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1) |
1090 | 485 | if (dctx->format == ZSTD_f_zstd1 && ZSTD_isLegacy(src, srcSize)) { |
1091 | 0 | size_t decodedSize; |
1092 | 0 | size_t const frameSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize); |
1093 | 0 | if (ZSTD_isError(frameSize)) return frameSize; |
1094 | 0 | RETURN_ERROR_IF(dctx->staticSize, memory_allocation, |
1095 | 0 | "legacy support is not compatible with static dctx"); |
1096 | | |
1097 | 0 | decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize); |
1098 | 0 | if (ZSTD_isError(decodedSize)) return decodedSize; |
1099 | | |
1100 | 0 | { |
1101 | 0 | unsigned long long const expectedSize = ZSTD_getFrameContentSize(src, srcSize); |
1102 | 0 | RETURN_ERROR_IF(expectedSize == ZSTD_CONTENTSIZE_ERROR, corruption_detected, "Corrupted frame header!"); |
1103 | 0 | if (expectedSize != ZSTD_CONTENTSIZE_UNKNOWN) { |
1104 | 0 | RETURN_ERROR_IF(expectedSize != decodedSize, corruption_detected, |
1105 | 0 | "Frame header size does not match decoded size!"); |
1106 | 0 | } |
1107 | 0 | } |
1108 | | |
1109 | 0 | assert(decodedSize <= dstCapacity); |
1110 | 0 | dst = (BYTE*)dst + decodedSize; |
1111 | 0 | dstCapacity -= decodedSize; |
1112 | |
|
1113 | 0 | src = (const BYTE*)src + frameSize; |
1114 | 0 | srcSize -= frameSize; |
1115 | |
|
1116 | 0 | continue; |
1117 | 0 | } |
1118 | 485 | #endif |
1119 | | |
1120 | 485 | if (dctx->format == ZSTD_f_zstd1 && srcSize >= 4) { |
1121 | 485 | U32 const magicNumber = MEM_readLE32(src); |
1122 | 485 | DEBUGLOG(5, "reading magic number %08X", (unsigned)magicNumber); |
1123 | 485 | if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { |
1124 | | /* skippable frame detected : skip it */ |
1125 | 0 | size_t const skippableSize = readSkippableFrameSize(src, srcSize); |
1126 | 0 | FORWARD_IF_ERROR(skippableSize, "invalid skippable frame"); |
1127 | 0 | assert(skippableSize <= srcSize); |
1128 | |
|
1129 | 0 | src = (const BYTE *)src + skippableSize; |
1130 | 0 | srcSize -= skippableSize; |
1131 | 0 | continue; /* check next frame */ |
1132 | 0 | } } |
1133 | | |
1134 | 485 | if (ddict) { |
1135 | | /* we were called from ZSTD_decompress_usingDDict */ |
1136 | 0 | FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(dctx, ddict), ""); |
1137 | 485 | } else { |
1138 | | /* this will initialize correctly with no dict if dict == NULL, so |
1139 | | * use this in all cases but ddict */ |
1140 | 485 | FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDict(dctx, dict, dictSize), ""); |
1141 | 485 | } |
1142 | 485 | ZSTD_checkContinuity(dctx, dst, dstCapacity); |
1143 | | |
1144 | 485 | { const size_t res = ZSTD_decompressFrame(dctx, dst, dstCapacity, |
1145 | 485 | &src, &srcSize); |
1146 | 485 | RETURN_ERROR_IF( |
1147 | 485 | (ZSTD_getErrorCode(res) == ZSTD_error_prefix_unknown) |
1148 | 485 | && (moreThan1Frame==1), |
1149 | 485 | srcSize_wrong, |
1150 | 485 | "At least one frame successfully completed, " |
1151 | 485 | "but following bytes are garbage: " |
1152 | 485 | "it's more likely to be a srcSize error, " |
1153 | 485 | "specifying more input bytes than size of frame(s). " |
1154 | 485 | "Note: one could be unlucky, it might be a corruption error instead, " |
1155 | 485 | "happening right at the place where we expect zstd magic bytes. " |
1156 | 485 | "But this is _much_ less likely than a srcSize field error."); |
1157 | 485 | if (ZSTD_isError(res)) return res; |
1158 | 17 | assert(res <= dstCapacity); |
1159 | 17 | if (res != 0) |
1160 | 3 | dst = (BYTE*)dst + res; |
1161 | 17 | dstCapacity -= res; |
1162 | 17 | } |
1163 | 0 | moreThan1Frame = 1; |
1164 | 17 | } /* while (srcSize >= ZSTD_frameHeaderSize_prefix) */ |
1165 | | |
1166 | 17 | RETURN_ERROR_IF(srcSize, srcSize_wrong, "input not entirely consumed"); |
1167 | | |
1168 | 17 | return (size_t)((BYTE*)dst - (BYTE*)dststart); |
1169 | 17 | } |
1170 | | |
1171 | | size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, |
1172 | | void* dst, size_t dstCapacity, |
1173 | | const void* src, size_t srcSize, |
1174 | | const void* dict, size_t dictSize) |
1175 | 0 | { |
1176 | 0 | return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, NULL); |
1177 | 0 | } |
1178 | | |
1179 | | |
1180 | | static ZSTD_DDict const* ZSTD_getDDict(ZSTD_DCtx* dctx) |
1181 | 38.8k | { |
1182 | 38.8k | switch (dctx->dictUses) { |
1183 | 0 | default: |
1184 | 0 | assert(0 /* Impossible */); |
1185 | 0 | ZSTD_FALLTHROUGH; |
1186 | 38.8k | case ZSTD_dont_use: |
1187 | 38.8k | ZSTD_clearDict(dctx); |
1188 | 38.8k | return NULL; |
1189 | 0 | case ZSTD_use_indefinitely: |
1190 | 0 | return dctx->ddict; |
1191 | 0 | case ZSTD_use_once: |
1192 | 0 | dctx->dictUses = ZSTD_dont_use; |
1193 | 0 | return dctx->ddict; |
1194 | 38.8k | } |
1195 | 38.8k | } |
1196 | | |
1197 | | size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
1198 | 0 | { |
1199 | 0 | return ZSTD_decompress_usingDDict(dctx, dst, dstCapacity, src, srcSize, ZSTD_getDDict(dctx)); |
1200 | 0 | } |
1201 | | |
1202 | | |
1203 | | size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
1204 | 0 | { |
1205 | 0 | #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE>=1) |
1206 | 0 | size_t regenSize; |
1207 | 0 | ZSTD_DCtx* const dctx = ZSTD_createDCtx_internal(ZSTD_defaultCMem); |
1208 | 0 | RETURN_ERROR_IF(dctx==NULL, memory_allocation, "NULL pointer!"); |
1209 | 0 | regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize); |
1210 | 0 | ZSTD_freeDCtx(dctx); |
1211 | 0 | return regenSize; |
1212 | | #else /* stack mode */ |
1213 | | ZSTD_DCtx dctx; |
1214 | | ZSTD_initDCtx_internal(&dctx); |
1215 | | return ZSTD_decompressDCtx(&dctx, dst, dstCapacity, src, srcSize); |
1216 | | #endif |
1217 | 0 | } |
1218 | | |
1219 | | |
1220 | | /*-************************************** |
1221 | | * Advanced Streaming Decompression API |
1222 | | * Bufferless and synchronous |
1223 | | ****************************************/ |
1224 | 16.1k | size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) { return dctx->expected; } |
1225 | | |
1226 | | /** |
1227 | | * Similar to ZSTD_nextSrcSizeToDecompress(), but when a block input can be streamed, we |
1228 | | * allow taking a partial block as the input. Currently only raw uncompressed blocks can |
1229 | | * be streamed. |
1230 | | * |
1231 | | * For blocks that can be streamed, this allows us to reduce the latency until we produce |
1232 | | * output, and avoid copying the input. |
1233 | | * |
1234 | | * @param inputSize - The total amount of input that the caller currently has. |
1235 | | */ |
1236 | 4.05M | static size_t ZSTD_nextSrcSizeToDecompressWithInputSize(ZSTD_DCtx* dctx, size_t inputSize) { |
1237 | 4.05M | if (!(dctx->stage == ZSTDds_decompressBlock || dctx->stage == ZSTDds_decompressLastBlock)) |
1238 | 3.95M | return dctx->expected; |
1239 | 92.4k | if (dctx->bType != bt_raw) |
1240 | 43.4k | return dctx->expected; |
1241 | 48.9k | return BOUNDED(1, inputSize, dctx->expected); |
1242 | 92.4k | } |
1243 | | |
1244 | 13.4k | ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) { |
1245 | 13.4k | switch(dctx->stage) |
1246 | 13.4k | { |
1247 | 0 | default: /* should not happen */ |
1248 | 0 | assert(0); |
1249 | 0 | ZSTD_FALLTHROUGH; |
1250 | 0 | case ZSTDds_getFrameHeaderSize: |
1251 | 0 | ZSTD_FALLTHROUGH; |
1252 | 0 | case ZSTDds_decodeFrameHeader: |
1253 | 0 | return ZSTDnit_frameHeader; |
1254 | 2.48k | case ZSTDds_decodeBlockHeader: |
1255 | 2.48k | return ZSTDnit_blockHeader; |
1256 | 10.6k | case ZSTDds_decompressBlock: |
1257 | 10.6k | return ZSTDnit_block; |
1258 | 154 | case ZSTDds_decompressLastBlock: |
1259 | 154 | return ZSTDnit_lastBlock; |
1260 | 39 | case ZSTDds_checkChecksum: |
1261 | 39 | return ZSTDnit_checksum; |
1262 | 0 | case ZSTDds_decodeSkippableHeader: |
1263 | 0 | ZSTD_FALLTHROUGH; |
1264 | 170 | case ZSTDds_skipFrame: |
1265 | 170 | return ZSTDnit_skippableFrame; |
1266 | 13.4k | } |
1267 | 13.4k | } |
1268 | | |
1269 | 2.02M | static int ZSTD_isSkipFrame(ZSTD_DCtx* dctx) { return dctx->stage == ZSTDds_skipFrame; } |
1270 | | |
1271 | | /** ZSTD_decompressContinue() : |
1272 | | * srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress()) |
1273 | | * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity) |
1274 | | * or an error code, which can be tested using ZSTD_isError() */ |
1275 | | size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
1276 | 2.02M | { |
1277 | 2.02M | DEBUGLOG(5, "ZSTD_decompressContinue (srcSize:%u)", (unsigned)srcSize); |
1278 | | /* Sanity check */ |
1279 | 2.02M | RETURN_ERROR_IF(srcSize != ZSTD_nextSrcSizeToDecompressWithInputSize(dctx, srcSize), srcSize_wrong, "not allowed"); |
1280 | 2.02M | ZSTD_checkContinuity(dctx, dst, dstCapacity); |
1281 | | |
1282 | 2.02M | dctx->processedCSize += srcSize; |
1283 | | |
1284 | 2.02M | switch (dctx->stage) |
1285 | 2.02M | { |
1286 | 0 | case ZSTDds_getFrameHeaderSize : |
1287 | 0 | assert(src != NULL); |
1288 | 0 | if (dctx->format == ZSTD_f_zstd1) { /* allows header */ |
1289 | 0 | assert(srcSize >= ZSTD_FRAMEIDSIZE); /* to read skippable magic number */ |
1290 | 0 | if ((MEM_readLE32(src) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */ |
1291 | 0 | ZSTD_memcpy(dctx->headerBuffer, src, srcSize); |
1292 | 0 | dctx->expected = ZSTD_SKIPPABLEHEADERSIZE - srcSize; /* remaining to load to get full skippable frame header */ |
1293 | 0 | dctx->stage = ZSTDds_decodeSkippableHeader; |
1294 | 0 | return 0; |
1295 | 0 | } } |
1296 | 0 | dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format); |
1297 | 0 | if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize; |
1298 | 0 | ZSTD_memcpy(dctx->headerBuffer, src, srcSize); |
1299 | 0 | dctx->expected = dctx->headerSize - srcSize; |
1300 | 0 | dctx->stage = ZSTDds_decodeFrameHeader; |
1301 | 0 | return 0; |
1302 | | |
1303 | 0 | case ZSTDds_decodeFrameHeader: |
1304 | 0 | assert(src != NULL); |
1305 | 0 | ZSTD_memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, srcSize); |
1306 | 0 | FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize), ""); |
1307 | 0 | dctx->expected = ZSTD_blockHeaderSize; |
1308 | 0 | dctx->stage = ZSTDds_decodeBlockHeader; |
1309 | 0 | return 0; |
1310 | | |
1311 | 1.97M | case ZSTDds_decodeBlockHeader: |
1312 | 1.97M | { blockProperties_t bp; |
1313 | 1.97M | size_t const cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp); |
1314 | 1.97M | if (ZSTD_isError(cBlockSize)) return cBlockSize; |
1315 | 1.97M | RETURN_ERROR_IF(cBlockSize > dctx->fParams.blockSizeMax, corruption_detected, "Block Size Exceeds Maximum"); |
1316 | 1.97M | dctx->expected = cBlockSize; |
1317 | 1.97M | dctx->bType = bp.blockType; |
1318 | 1.97M | dctx->rleSize = bp.origSize; |
1319 | 1.97M | if (cBlockSize) { |
1320 | 32.5k | dctx->stage = bp.lastBlock ? ZSTDds_decompressLastBlock : ZSTDds_decompressBlock; |
1321 | 32.5k | return 0; |
1322 | 32.5k | } |
1323 | | /* empty block */ |
1324 | 1.94M | if (bp.lastBlock) { |
1325 | 287 | if (dctx->fParams.checksumFlag) { |
1326 | 214 | dctx->expected = 4; |
1327 | 214 | dctx->stage = ZSTDds_checkChecksum; |
1328 | 214 | } else { |
1329 | 73 | dctx->expected = 0; /* end of frame */ |
1330 | 73 | dctx->stage = ZSTDds_getFrameHeaderSize; |
1331 | 73 | } |
1332 | 1.94M | } else { |
1333 | 1.94M | dctx->expected = ZSTD_blockHeaderSize; /* jump to next header */ |
1334 | 1.94M | dctx->stage = ZSTDds_decodeBlockHeader; |
1335 | 1.94M | } |
1336 | 1.94M | return 0; |
1337 | 1.97M | } |
1338 | | |
1339 | 4.40k | case ZSTDds_decompressLastBlock: |
1340 | 41.3k | case ZSTDds_decompressBlock: |
1341 | 41.3k | DEBUGLOG(5, "ZSTD_decompressContinue: case ZSTDds_decompressBlock"); |
1342 | 41.3k | { size_t rSize; |
1343 | 41.3k | switch(dctx->bType) |
1344 | 41.3k | { |
1345 | 17.6k | case bt_compressed: |
1346 | 17.6k | DEBUGLOG(5, "ZSTD_decompressContinue: case bt_compressed"); |
1347 | 17.6k | assert(dctx->isFrameDecompression == 1); |
1348 | 17.6k | rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, is_streaming); |
1349 | 17.6k | dctx->expected = 0; /* Streaming not supported */ |
1350 | 17.6k | break; |
1351 | 19.7k | case bt_raw : |
1352 | 19.7k | assert(srcSize <= dctx->expected); |
1353 | 19.7k | rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize); |
1354 | 19.7k | FORWARD_IF_ERROR(rSize, "ZSTD_copyRawBlock failed"); |
1355 | 19.6k | assert(rSize == srcSize); |
1356 | 19.6k | dctx->expected -= rSize; |
1357 | 19.6k | break; |
1358 | 4.05k | case bt_rle : |
1359 | 4.05k | rSize = ZSTD_setRleBlock(dst, dstCapacity, *(const BYTE*)src, dctx->rleSize); |
1360 | 4.05k | dctx->expected = 0; /* Streaming not supported */ |
1361 | 4.05k | break; |
1362 | 0 | case bt_reserved : /* should never happen */ |
1363 | 0 | default: |
1364 | 0 | RETURN_ERROR(corruption_detected, "invalid block type"); |
1365 | 41.3k | } |
1366 | 41.3k | FORWARD_IF_ERROR(rSize, ""); |
1367 | 31.0k | RETURN_ERROR_IF(rSize > dctx->fParams.blockSizeMax, corruption_detected, "Decompressed Block Size Exceeds Maximum"); |
1368 | 30.9k | DEBUGLOG(5, "ZSTD_decompressContinue: decoded size from block : %u", (unsigned)rSize); |
1369 | 30.9k | dctx->decodedSize += rSize; |
1370 | 30.9k | if (dctx->validateChecksum) XXH64_update(&dctx->xxhState, dst, rSize); |
1371 | 30.9k | dctx->previousDstEnd = (char*)dst + rSize; |
1372 | | |
1373 | | /* Stay on the same stage until we are finished streaming the block. */ |
1374 | 30.9k | if (dctx->expected > 0) { |
1375 | 9.57k | return rSize; |
1376 | 9.57k | } |
1377 | | |
1378 | 21.4k | if (dctx->stage == ZSTDds_decompressLastBlock) { /* end of frame */ |
1379 | 314 | DEBUGLOG(4, "ZSTD_decompressContinue: decoded size from frame : %u", (unsigned)dctx->decodedSize); |
1380 | 314 | RETURN_ERROR_IF( |
1381 | 314 | dctx->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN |
1382 | 314 | && dctx->decodedSize != dctx->fParams.frameContentSize, |
1383 | 314 | corruption_detected, ""); |
1384 | 118 | if (dctx->fParams.checksumFlag) { /* another round for frame checksum */ |
1385 | 51 | dctx->expected = 4; |
1386 | 51 | dctx->stage = ZSTDds_checkChecksum; |
1387 | 67 | } else { |
1388 | 67 | ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, /* streaming */ 1); |
1389 | 67 | dctx->expected = 0; /* ends here */ |
1390 | 67 | dctx->stage = ZSTDds_getFrameHeaderSize; |
1391 | 67 | } |
1392 | 21.1k | } else { |
1393 | 21.1k | dctx->stage = ZSTDds_decodeBlockHeader; |
1394 | 21.1k | dctx->expected = ZSTD_blockHeaderSize; |
1395 | 21.1k | } |
1396 | 21.2k | return rSize; |
1397 | 21.4k | } |
1398 | | |
1399 | 233 | case ZSTDds_checkChecksum: |
1400 | 233 | assert(srcSize == 4); /* guaranteed by dctx->expected */ |
1401 | 233 | { |
1402 | 233 | if (dctx->validateChecksum) { |
1403 | 233 | U32 const h32 = (U32)XXH64_digest(&dctx->xxhState); |
1404 | 233 | U32 const check32 = MEM_readLE32(src); |
1405 | 233 | DEBUGLOG(4, "ZSTD_decompressContinue: checksum : calculated %08X :: %08X read", (unsigned)h32, (unsigned)check32); |
1406 | 233 | RETURN_ERROR_IF(check32 != h32, checksum_wrong, ""); |
1407 | 233 | } |
1408 | 1 | ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, /* streaming */ 1); |
1409 | 1 | dctx->expected = 0; |
1410 | 1 | dctx->stage = ZSTDds_getFrameHeaderSize; |
1411 | 1 | return 0; |
1412 | 233 | } |
1413 | | |
1414 | 0 | case ZSTDds_decodeSkippableHeader: |
1415 | 0 | assert(src != NULL); |
1416 | 0 | assert(srcSize <= ZSTD_SKIPPABLEHEADERSIZE); |
1417 | 0 | assert(dctx->format != ZSTD_f_zstd1_magicless); |
1418 | 0 | ZSTD_memcpy(dctx->headerBuffer + (ZSTD_SKIPPABLEHEADERSIZE - srcSize), src, srcSize); /* complete skippable header */ |
1419 | 0 | dctx->expected = MEM_readLE32(dctx->headerBuffer + ZSTD_FRAMEIDSIZE); /* note : dctx->expected can grow seriously large, beyond local buffer size */ |
1420 | 0 | dctx->stage = ZSTDds_skipFrame; |
1421 | 0 | return 0; |
1422 | | |
1423 | 2 | case ZSTDds_skipFrame: |
1424 | 2 | dctx->expected = 0; |
1425 | 2 | dctx->stage = ZSTDds_getFrameHeaderSize; |
1426 | 2 | return 0; |
1427 | | |
1428 | 0 | default: |
1429 | 0 | assert(0); /* impossible */ |
1430 | 0 | RETURN_ERROR(GENERIC, "impossible to reach"); /* some compilers require default to do something */ |
1431 | 2.02M | } |
1432 | 2.02M | } |
1433 | | |
1434 | | |
1435 | | static size_t ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
1436 | 0 | { |
1437 | 0 | dctx->dictEnd = dctx->previousDstEnd; |
1438 | 0 | dctx->virtualStart = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart)); |
1439 | 0 | dctx->prefixStart = dict; |
1440 | 0 | dctx->previousDstEnd = (const char*)dict + dictSize; |
1441 | 0 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
1442 | 0 | dctx->dictContentBeginForFuzzing = dctx->prefixStart; |
1443 | 0 | dctx->dictContentEndForFuzzing = dctx->previousDstEnd; |
1444 | 0 | #endif |
1445 | 0 | return 0; |
1446 | 0 | } |
1447 | | |
1448 | | /*! ZSTD_loadDEntropy() : |
1449 | | * dict : must point at beginning of a valid zstd dictionary. |
1450 | | * @return : size of entropy tables read */ |
1451 | | size_t |
1452 | | ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy, |
1453 | | const void* const dict, size_t const dictSize) |
1454 | 0 | { |
1455 | 0 | const BYTE* dictPtr = (const BYTE*)dict; |
1456 | 0 | const BYTE* const dictEnd = dictPtr + dictSize; |
1457 | |
|
1458 | 0 | RETURN_ERROR_IF(dictSize <= 8, dictionary_corrupted, "dict is too small"); |
1459 | 0 | assert(MEM_readLE32(dict) == ZSTD_MAGIC_DICTIONARY); /* dict must be valid */ |
1460 | 0 | dictPtr += 8; /* skip header = magic + dictID */ |
1461 | |
|
1462 | 0 | ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, OFTable) == offsetof(ZSTD_entropyDTables_t, LLTable) + sizeof(entropy->LLTable)); |
1463 | 0 | ZSTD_STATIC_ASSERT(offsetof(ZSTD_entropyDTables_t, MLTable) == offsetof(ZSTD_entropyDTables_t, OFTable) + sizeof(entropy->OFTable)); |
1464 | 0 | ZSTD_STATIC_ASSERT(sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable) >= HUF_DECOMPRESS_WORKSPACE_SIZE); |
1465 | 0 | { void* const workspace = &entropy->LLTable; /* use fse tables as temporary workspace; implies fse tables are grouped together */ |
1466 | 0 | size_t const workspaceSize = sizeof(entropy->LLTable) + sizeof(entropy->OFTable) + sizeof(entropy->MLTable); |
1467 | | #ifdef HUF_FORCE_DECOMPRESS_X1 |
1468 | | /* in minimal huffman, we always use X1 variants */ |
1469 | | size_t const hSize = HUF_readDTableX1_wksp(entropy->hufTable, |
1470 | | dictPtr, dictEnd - dictPtr, |
1471 | | workspace, workspaceSize, /* flags */ 0); |
1472 | | #else |
1473 | 0 | size_t const hSize = HUF_readDTableX2_wksp(entropy->hufTable, |
1474 | 0 | dictPtr, (size_t)(dictEnd - dictPtr), |
1475 | 0 | workspace, workspaceSize, /* flags */ 0); |
1476 | 0 | #endif |
1477 | 0 | RETURN_ERROR_IF(HUF_isError(hSize), dictionary_corrupted, ""); |
1478 | 0 | dictPtr += hSize; |
1479 | 0 | } |
1480 | | |
1481 | 0 | { short offcodeNCount[MaxOff+1]; |
1482 | 0 | unsigned offcodeMaxValue = MaxOff, offcodeLog; |
1483 | 0 | size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, (size_t)(dictEnd-dictPtr)); |
1484 | 0 | RETURN_ERROR_IF(FSE_isError(offcodeHeaderSize), dictionary_corrupted, ""); |
1485 | 0 | RETURN_ERROR_IF(offcodeMaxValue > MaxOff, dictionary_corrupted, ""); |
1486 | 0 | RETURN_ERROR_IF(offcodeLog > OffFSELog, dictionary_corrupted, ""); |
1487 | 0 | ZSTD_buildFSETable( entropy->OFTable, |
1488 | 0 | offcodeNCount, offcodeMaxValue, |
1489 | 0 | OF_base, OF_bits, |
1490 | 0 | offcodeLog, |
1491 | 0 | entropy->workspace, sizeof(entropy->workspace), |
1492 | 0 | /* bmi2 */0); |
1493 | 0 | dictPtr += offcodeHeaderSize; |
1494 | 0 | } |
1495 | | |
1496 | 0 | { short matchlengthNCount[MaxML+1]; |
1497 | 0 | unsigned matchlengthMaxValue = MaxML, matchlengthLog; |
1498 | 0 | size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, (size_t)(dictEnd-dictPtr)); |
1499 | 0 | RETURN_ERROR_IF(FSE_isError(matchlengthHeaderSize), dictionary_corrupted, ""); |
1500 | 0 | RETURN_ERROR_IF(matchlengthMaxValue > MaxML, dictionary_corrupted, ""); |
1501 | 0 | RETURN_ERROR_IF(matchlengthLog > MLFSELog, dictionary_corrupted, ""); |
1502 | 0 | ZSTD_buildFSETable( entropy->MLTable, |
1503 | 0 | matchlengthNCount, matchlengthMaxValue, |
1504 | 0 | ML_base, ML_bits, |
1505 | 0 | matchlengthLog, |
1506 | 0 | entropy->workspace, sizeof(entropy->workspace), |
1507 | 0 | /* bmi2 */ 0); |
1508 | 0 | dictPtr += matchlengthHeaderSize; |
1509 | 0 | } |
1510 | | |
1511 | 0 | { short litlengthNCount[MaxLL+1]; |
1512 | 0 | unsigned litlengthMaxValue = MaxLL, litlengthLog; |
1513 | 0 | size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, (size_t)(dictEnd-dictPtr)); |
1514 | 0 | RETURN_ERROR_IF(FSE_isError(litlengthHeaderSize), dictionary_corrupted, ""); |
1515 | 0 | RETURN_ERROR_IF(litlengthMaxValue > MaxLL, dictionary_corrupted, ""); |
1516 | 0 | RETURN_ERROR_IF(litlengthLog > LLFSELog, dictionary_corrupted, ""); |
1517 | 0 | ZSTD_buildFSETable( entropy->LLTable, |
1518 | 0 | litlengthNCount, litlengthMaxValue, |
1519 | 0 | LL_base, LL_bits, |
1520 | 0 | litlengthLog, |
1521 | 0 | entropy->workspace, sizeof(entropy->workspace), |
1522 | 0 | /* bmi2 */ 0); |
1523 | 0 | dictPtr += litlengthHeaderSize; |
1524 | 0 | } |
1525 | | |
1526 | 0 | RETURN_ERROR_IF(dictPtr+12 > dictEnd, dictionary_corrupted, ""); |
1527 | 0 | { int i; |
1528 | 0 | size_t const dictContentSize = (size_t)(dictEnd - (dictPtr+12)); |
1529 | 0 | for (i=0; i<3; i++) { |
1530 | 0 | U32 const rep = MEM_readLE32(dictPtr); dictPtr += 4; |
1531 | 0 | RETURN_ERROR_IF(rep==0 || rep > dictContentSize, |
1532 | 0 | dictionary_corrupted, ""); |
1533 | 0 | entropy->rep[i] = rep; |
1534 | 0 | } } |
1535 | | |
1536 | 0 | return (size_t)(dictPtr - (const BYTE*)dict); |
1537 | 0 | } |
1538 | | |
1539 | | static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
1540 | 0 | { |
1541 | 0 | if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize); |
1542 | 0 | { U32 const magic = MEM_readLE32(dict); |
1543 | 0 | if (magic != ZSTD_MAGIC_DICTIONARY) { |
1544 | 0 | return ZSTD_refDictContent(dctx, dict, dictSize); /* pure content mode */ |
1545 | 0 | } } |
1546 | 0 | dctx->dictID = MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE); |
1547 | | |
1548 | | /* load entropy tables */ |
1549 | 0 | { size_t const eSize = ZSTD_loadDEntropy(&dctx->entropy, dict, dictSize); |
1550 | 0 | RETURN_ERROR_IF(ZSTD_isError(eSize), dictionary_corrupted, ""); |
1551 | 0 | dict = (const char*)dict + eSize; |
1552 | 0 | dictSize -= eSize; |
1553 | 0 | } |
1554 | 0 | dctx->litEntropy = dctx->fseEntropy = 1; |
1555 | | |
1556 | | /* reference dictionary content */ |
1557 | 0 | return ZSTD_refDictContent(dctx, dict, dictSize); |
1558 | 0 | } |
1559 | | |
1560 | | size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx) |
1561 | 13.3k | { |
1562 | 13.3k | assert(dctx != NULL); |
1563 | 13.3k | #if ZSTD_TRACE |
1564 | 13.3k | dctx->traceCtx = (ZSTD_trace_decompress_begin != NULL) ? ZSTD_trace_decompress_begin(dctx) : 0; |
1565 | 13.3k | #endif |
1566 | 13.3k | dctx->expected = ZSTD_startingInputLength(dctx->format); /* dctx->format must be properly set */ |
1567 | 13.3k | dctx->stage = ZSTDds_getFrameHeaderSize; |
1568 | 13.3k | dctx->processedCSize = 0; |
1569 | 13.3k | dctx->decodedSize = 0; |
1570 | 13.3k | dctx->previousDstEnd = NULL; |
1571 | 13.3k | dctx->prefixStart = NULL; |
1572 | 13.3k | dctx->virtualStart = NULL; |
1573 | 13.3k | dctx->dictEnd = NULL; |
1574 | 13.3k | dctx->entropy.hufTable[0] = (HUF_DTable)((ZSTD_HUFFDTABLE_CAPACITY_LOG)*0x1000001); /* cover both little and big endian */ |
1575 | 13.3k | dctx->litEntropy = dctx->fseEntropy = 0; |
1576 | 13.3k | dctx->dictID = 0; |
1577 | 13.3k | dctx->bType = bt_reserved; |
1578 | 13.3k | dctx->isFrameDecompression = 1; |
1579 | 13.3k | ZSTD_STATIC_ASSERT(sizeof(dctx->entropy.rep) == sizeof(repStartValue)); |
1580 | 13.3k | ZSTD_memcpy(dctx->entropy.rep, repStartValue, sizeof(repStartValue)); /* initial repcodes */ |
1581 | 13.3k | dctx->LLTptr = dctx->entropy.LLTable; |
1582 | 13.3k | dctx->MLTptr = dctx->entropy.MLTable; |
1583 | 13.3k | dctx->OFTptr = dctx->entropy.OFTable; |
1584 | 13.3k | dctx->HUFptr = dctx->entropy.hufTable; |
1585 | 13.3k | return 0; |
1586 | 13.3k | } |
1587 | | |
1588 | | size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
1589 | 485 | { |
1590 | 485 | FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , ""); |
1591 | 485 | if (dict && dictSize) |
1592 | 0 | RETURN_ERROR_IF( |
1593 | 485 | ZSTD_isError(ZSTD_decompress_insertDictionary(dctx, dict, dictSize)), |
1594 | 485 | dictionary_corrupted, ""); |
1595 | 485 | return 0; |
1596 | 485 | } |
1597 | | |
1598 | | |
1599 | | /* ====== ZSTD_DDict ====== */ |
1600 | | |
1601 | | size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict) |
1602 | 12.8k | { |
1603 | 12.8k | DEBUGLOG(4, "ZSTD_decompressBegin_usingDDict"); |
1604 | 12.8k | assert(dctx != NULL); |
1605 | 12.8k | if (ddict) { |
1606 | 0 | const char* const dictStart = (const char*)ZSTD_DDict_dictContent(ddict); |
1607 | 0 | size_t const dictSize = ZSTD_DDict_dictSize(ddict); |
1608 | 0 | const void* const dictEnd = dictStart + dictSize; |
1609 | 0 | dctx->ddictIsCold = (dctx->dictEnd != dictEnd); |
1610 | 0 | DEBUGLOG(4, "DDict is %s", |
1611 | 0 | dctx->ddictIsCold ? "~cold~" : "hot!"); |
1612 | 0 | } |
1613 | 12.8k | FORWARD_IF_ERROR( ZSTD_decompressBegin(dctx) , ""); |
1614 | 12.8k | if (ddict) { /* NULL ddict is equivalent to no dictionary */ |
1615 | 0 | ZSTD_copyDDictParameters(dctx, ddict); |
1616 | 0 | } |
1617 | 12.8k | return 0; |
1618 | 12.8k | } |
1619 | | |
1620 | | /*! ZSTD_getDictID_fromDict() : |
1621 | | * Provides the dictID stored within dictionary. |
1622 | | * if @return == 0, the dictionary is not conformant with Zstandard specification. |
1623 | | * It can still be loaded, but as a content-only dictionary. */ |
1624 | | unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize) |
1625 | 0 | { |
1626 | 0 | if (dictSize < 8) return 0; |
1627 | 0 | if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) return 0; |
1628 | 0 | return MEM_readLE32((const char*)dict + ZSTD_FRAMEIDSIZE); |
1629 | 0 | } |
1630 | | |
1631 | | /*! ZSTD_getDictID_fromFrame() : |
1632 | | * Provides the dictID required to decompress frame stored within `src`. |
1633 | | * If @return == 0, the dictID could not be decoded. |
1634 | | * This could for one of the following reasons : |
1635 | | * - The frame does not require a dictionary (most common case). |
1636 | | * - The frame was built with dictID intentionally removed. |
1637 | | * Needed dictionary is a hidden piece of information. |
1638 | | * Note : this use case also happens when using a non-conformant dictionary. |
1639 | | * - `srcSize` is too small, and as a result, frame header could not be decoded. |
1640 | | * Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`. |
1641 | | * - This is not a Zstandard frame. |
1642 | | * When identifying the exact failure cause, it's possible to use |
1643 | | * ZSTD_getFrameHeader(), which will provide a more precise error code. */ |
1644 | | unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize) |
1645 | 0 | { |
1646 | 0 | ZSTD_FrameHeader zfp = { 0, 0, 0, ZSTD_frame, 0, 0, 0, 0, 0 }; |
1647 | 0 | size_t const hError = ZSTD_getFrameHeader(&zfp, src, srcSize); |
1648 | 0 | if (ZSTD_isError(hError)) return 0; |
1649 | 0 | return zfp.dictID; |
1650 | 0 | } |
1651 | | |
1652 | | |
1653 | | /*! ZSTD_decompress_usingDDict() : |
1654 | | * Decompression using a pre-digested Dictionary |
1655 | | * Use dictionary without significant overhead. */ |
1656 | | size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, |
1657 | | void* dst, size_t dstCapacity, |
1658 | | const void* src, size_t srcSize, |
1659 | | const ZSTD_DDict* ddict) |
1660 | 485 | { |
1661 | | /* pass content and size in case legacy frames are encountered */ |
1662 | 485 | return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, |
1663 | 485 | NULL, 0, |
1664 | 485 | ddict); |
1665 | 485 | } |
1666 | | |
1667 | | |
1668 | | /*===================================== |
1669 | | * Streaming decompression |
1670 | | *====================================*/ |
1671 | | |
1672 | | ZSTD_DStream* ZSTD_createDStream(void) |
1673 | 45.6k | { |
1674 | 45.6k | DEBUGLOG(3, "ZSTD_createDStream"); |
1675 | 45.6k | return ZSTD_createDCtx_internal(ZSTD_defaultCMem); |
1676 | 45.6k | } |
1677 | | |
1678 | | ZSTD_DStream* ZSTD_initStaticDStream(void *workspace, size_t workspaceSize) |
1679 | 0 | { |
1680 | 0 | return ZSTD_initStaticDCtx(workspace, workspaceSize); |
1681 | 0 | } |
1682 | | |
1683 | | ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem) |
1684 | 0 | { |
1685 | 0 | return ZSTD_createDCtx_internal(customMem); |
1686 | 0 | } |
1687 | | |
1688 | | size_t ZSTD_freeDStream(ZSTD_DStream* zds) |
1689 | 45.6k | { |
1690 | 45.6k | return ZSTD_freeDCtx(zds); |
1691 | 45.6k | } |
1692 | | |
1693 | | |
1694 | | /* *** Initialization *** */ |
1695 | | |
1696 | 0 | size_t ZSTD_DStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize; } |
1697 | 0 | size_t ZSTD_DStreamOutSize(void) { return ZSTD_BLOCKSIZE_MAX; } |
1698 | | |
1699 | | size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, |
1700 | | const void* dict, size_t dictSize, |
1701 | | ZSTD_dictLoadMethod_e dictLoadMethod, |
1702 | | ZSTD_dictContentType_e dictContentType) |
1703 | 0 | { |
1704 | 0 | RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, ""); |
1705 | 0 | ZSTD_clearDict(dctx); |
1706 | 0 | if (dict && dictSize != 0) { |
1707 | 0 | dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem); |
1708 | 0 | RETURN_ERROR_IF(dctx->ddictLocal == NULL, memory_allocation, "NULL pointer!"); |
1709 | 0 | dctx->ddict = dctx->ddictLocal; |
1710 | 0 | dctx->dictUses = ZSTD_use_indefinitely; |
1711 | 0 | } |
1712 | 0 | return 0; |
1713 | 0 | } |
1714 | | |
1715 | | size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
1716 | 0 | { |
1717 | 0 | return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dct_auto); |
1718 | 0 | } |
1719 | | |
1720 | | size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
1721 | 0 | { |
1722 | 0 | return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dct_auto); |
1723 | 0 | } |
1724 | | |
1725 | | size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType) |
1726 | 0 | { |
1727 | 0 | FORWARD_IF_ERROR(ZSTD_DCtx_loadDictionary_advanced(dctx, prefix, prefixSize, ZSTD_dlm_byRef, dictContentType), ""); |
1728 | 0 | dctx->dictUses = ZSTD_use_once; |
1729 | 0 | return 0; |
1730 | 0 | } |
1731 | | |
1732 | | size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize) |
1733 | 0 | { |
1734 | 0 | return ZSTD_DCtx_refPrefix_advanced(dctx, prefix, prefixSize, ZSTD_dct_rawContent); |
1735 | 0 | } |
1736 | | |
1737 | | |
1738 | | /* ZSTD_initDStream_usingDict() : |
1739 | | * return : expected size, aka ZSTD_startingInputLength(). |
1740 | | * this function cannot fail */ |
1741 | | size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize) |
1742 | 0 | { |
1743 | 0 | DEBUGLOG(4, "ZSTD_initDStream_usingDict"); |
1744 | 0 | FORWARD_IF_ERROR( ZSTD_DCtx_reset(zds, ZSTD_reset_session_only) , ""); |
1745 | 0 | FORWARD_IF_ERROR( ZSTD_DCtx_loadDictionary(zds, dict, dictSize) , ""); |
1746 | 0 | return ZSTD_startingInputLength(zds->format); |
1747 | 0 | } |
1748 | | |
1749 | | /* note : this variant can't fail */ |
1750 | | size_t ZSTD_initDStream(ZSTD_DStream* zds) |
1751 | 0 | { |
1752 | 0 | DEBUGLOG(4, "ZSTD_initDStream"); |
1753 | 0 | FORWARD_IF_ERROR(ZSTD_DCtx_reset(zds, ZSTD_reset_session_only), ""); |
1754 | 0 | FORWARD_IF_ERROR(ZSTD_DCtx_refDDict(zds, NULL), ""); |
1755 | 0 | return ZSTD_startingInputLength(zds->format); |
1756 | 0 | } |
1757 | | |
1758 | | /* ZSTD_initDStream_usingDDict() : |
1759 | | * ddict will just be referenced, and must outlive decompression session |
1760 | | * this function cannot fail */ |
1761 | | size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* dctx, const ZSTD_DDict* ddict) |
1762 | 0 | { |
1763 | 0 | DEBUGLOG(4, "ZSTD_initDStream_usingDDict"); |
1764 | 0 | FORWARD_IF_ERROR( ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only) , ""); |
1765 | 0 | FORWARD_IF_ERROR( ZSTD_DCtx_refDDict(dctx, ddict) , ""); |
1766 | 0 | return ZSTD_startingInputLength(dctx->format); |
1767 | 0 | } |
1768 | | |
1769 | | /* ZSTD_resetDStream() : |
1770 | | * return : expected size, aka ZSTD_startingInputLength(). |
1771 | | * this function cannot fail */ |
1772 | | size_t ZSTD_resetDStream(ZSTD_DStream* dctx) |
1773 | 0 | { |
1774 | 0 | DEBUGLOG(4, "ZSTD_resetDStream"); |
1775 | 0 | FORWARD_IF_ERROR(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only), ""); |
1776 | 0 | return ZSTD_startingInputLength(dctx->format); |
1777 | 0 | } |
1778 | | |
1779 | | |
1780 | | size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict) |
1781 | 0 | { |
1782 | 0 | RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, ""); |
1783 | 0 | ZSTD_clearDict(dctx); |
1784 | 0 | if (ddict) { |
1785 | 0 | dctx->ddict = ddict; |
1786 | 0 | dctx->dictUses = ZSTD_use_indefinitely; |
1787 | 0 | if (dctx->refMultipleDDicts == ZSTD_rmd_refMultipleDDicts) { |
1788 | 0 | if (dctx->ddictSet == NULL) { |
1789 | 0 | dctx->ddictSet = ZSTD_createDDictHashSet(dctx->customMem); |
1790 | 0 | if (!dctx->ddictSet) { |
1791 | 0 | RETURN_ERROR(memory_allocation, "Failed to allocate memory for hash set!"); |
1792 | 0 | } |
1793 | 0 | } |
1794 | 0 | assert(!dctx->staticSize); /* Impossible: ddictSet cannot have been allocated if static dctx */ |
1795 | 0 | FORWARD_IF_ERROR(ZSTD_DDictHashSet_addDDict(dctx->ddictSet, ddict, dctx->customMem), ""); |
1796 | 0 | } |
1797 | 0 | } |
1798 | 0 | return 0; |
1799 | 0 | } |
1800 | | |
1801 | | /* ZSTD_DCtx_setMaxWindowSize() : |
1802 | | * note : no direct equivalence in ZSTD_DCtx_setParameter, |
1803 | | * since this version sets windowSize, and the other sets windowLog */ |
1804 | | size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize) |
1805 | 0 | { |
1806 | 0 | ZSTD_bounds const bounds = ZSTD_dParam_getBounds(ZSTD_d_windowLogMax); |
1807 | 0 | size_t const min = (size_t)1 << bounds.lowerBound; |
1808 | 0 | size_t const max = (size_t)1 << bounds.upperBound; |
1809 | 0 | RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, ""); |
1810 | 0 | RETURN_ERROR_IF(maxWindowSize < min, parameter_outOfBound, ""); |
1811 | 0 | RETURN_ERROR_IF(maxWindowSize > max, parameter_outOfBound, ""); |
1812 | 0 | dctx->maxWindowSize = maxWindowSize; |
1813 | 0 | return 0; |
1814 | 0 | } |
1815 | | |
1816 | | size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format) |
1817 | 0 | { |
1818 | 0 | return ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, (int)format); |
1819 | 0 | } |
1820 | | |
1821 | | ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam) |
1822 | 0 | { |
1823 | 0 | ZSTD_bounds bounds = { 0, 0, 0 }; |
1824 | 0 | switch(dParam) { |
1825 | 0 | case ZSTD_d_windowLogMax: |
1826 | 0 | bounds.lowerBound = ZSTD_WINDOWLOG_ABSOLUTEMIN; |
1827 | 0 | bounds.upperBound = ZSTD_WINDOWLOG_MAX; |
1828 | 0 | return bounds; |
1829 | 0 | case ZSTD_d_format: |
1830 | 0 | bounds.lowerBound = (int)ZSTD_f_zstd1; |
1831 | 0 | bounds.upperBound = (int)ZSTD_f_zstd1_magicless; |
1832 | 0 | ZSTD_STATIC_ASSERT(ZSTD_f_zstd1 < ZSTD_f_zstd1_magicless); |
1833 | 0 | return bounds; |
1834 | 0 | case ZSTD_d_stableOutBuffer: |
1835 | 0 | bounds.lowerBound = (int)ZSTD_bm_buffered; |
1836 | 0 | bounds.upperBound = (int)ZSTD_bm_stable; |
1837 | 0 | return bounds; |
1838 | 0 | case ZSTD_d_forceIgnoreChecksum: |
1839 | 0 | bounds.lowerBound = (int)ZSTD_d_validateChecksum; |
1840 | 0 | bounds.upperBound = (int)ZSTD_d_ignoreChecksum; |
1841 | 0 | return bounds; |
1842 | 0 | case ZSTD_d_refMultipleDDicts: |
1843 | 0 | bounds.lowerBound = (int)ZSTD_rmd_refSingleDDict; |
1844 | 0 | bounds.upperBound = (int)ZSTD_rmd_refMultipleDDicts; |
1845 | 0 | return bounds; |
1846 | 0 | case ZSTD_d_disableHuffmanAssembly: |
1847 | 0 | bounds.lowerBound = 0; |
1848 | 0 | bounds.upperBound = 1; |
1849 | 0 | return bounds; |
1850 | 0 | case ZSTD_d_maxBlockSize: |
1851 | 0 | bounds.lowerBound = ZSTD_BLOCKSIZE_MAX_MIN; |
1852 | 0 | bounds.upperBound = ZSTD_BLOCKSIZE_MAX; |
1853 | 0 | return bounds; |
1854 | | |
1855 | 0 | default:; |
1856 | 0 | } |
1857 | 0 | bounds.error = ERROR(parameter_unsupported); |
1858 | 0 | return bounds; |
1859 | 0 | } |
1860 | | |
1861 | | /* ZSTD_dParam_withinBounds: |
1862 | | * @return 1 if value is within dParam bounds, |
1863 | | * 0 otherwise */ |
1864 | | static int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam, int value) |
1865 | 0 | { |
1866 | 0 | ZSTD_bounds const bounds = ZSTD_dParam_getBounds(dParam); |
1867 | 0 | if (ZSTD_isError(bounds.error)) return 0; |
1868 | 0 | if (value < bounds.lowerBound) return 0; |
1869 | 0 | if (value > bounds.upperBound) return 0; |
1870 | 0 | return 1; |
1871 | 0 | } |
1872 | | |
1873 | 0 | #define CHECK_DBOUNDS(p,v) { \ |
1874 | 0 | RETURN_ERROR_IF(!ZSTD_dParam_withinBounds(p, v), parameter_outOfBound, ""); \ |
1875 | 0 | } |
1876 | | |
1877 | | size_t ZSTD_DCtx_getParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int* value) |
1878 | 0 | { |
1879 | 0 | switch (param) { |
1880 | 0 | case ZSTD_d_windowLogMax: |
1881 | 0 | *value = (int)ZSTD_highbit32((U32)dctx->maxWindowSize); |
1882 | 0 | return 0; |
1883 | 0 | case ZSTD_d_format: |
1884 | 0 | *value = (int)dctx->format; |
1885 | 0 | return 0; |
1886 | 0 | case ZSTD_d_stableOutBuffer: |
1887 | 0 | *value = (int)dctx->outBufferMode; |
1888 | 0 | return 0; |
1889 | 0 | case ZSTD_d_forceIgnoreChecksum: |
1890 | 0 | *value = (int)dctx->forceIgnoreChecksum; |
1891 | 0 | return 0; |
1892 | 0 | case ZSTD_d_refMultipleDDicts: |
1893 | 0 | *value = (int)dctx->refMultipleDDicts; |
1894 | 0 | return 0; |
1895 | 0 | case ZSTD_d_disableHuffmanAssembly: |
1896 | 0 | *value = (int)dctx->disableHufAsm; |
1897 | 0 | return 0; |
1898 | 0 | case ZSTD_d_maxBlockSize: |
1899 | 0 | *value = dctx->maxBlockSizeParam; |
1900 | 0 | return 0; |
1901 | 0 | default:; |
1902 | 0 | } |
1903 | 0 | RETURN_ERROR(parameter_unsupported, ""); |
1904 | 0 | } |
1905 | | |
1906 | | size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter dParam, int value) |
1907 | 0 | { |
1908 | 0 | RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, ""); |
1909 | 0 | switch(dParam) { |
1910 | 0 | case ZSTD_d_windowLogMax: |
1911 | 0 | if (value == 0) value = ZSTD_WINDOWLOG_LIMIT_DEFAULT; |
1912 | 0 | CHECK_DBOUNDS(ZSTD_d_windowLogMax, value); |
1913 | 0 | dctx->maxWindowSize = ((size_t)1) << value; |
1914 | 0 | return 0; |
1915 | 0 | case ZSTD_d_format: |
1916 | 0 | CHECK_DBOUNDS(ZSTD_d_format, value); |
1917 | 0 | dctx->format = (ZSTD_format_e)value; |
1918 | 0 | return 0; |
1919 | 0 | case ZSTD_d_stableOutBuffer: |
1920 | 0 | CHECK_DBOUNDS(ZSTD_d_stableOutBuffer, value); |
1921 | 0 | dctx->outBufferMode = (ZSTD_bufferMode_e)value; |
1922 | 0 | return 0; |
1923 | 0 | case ZSTD_d_forceIgnoreChecksum: |
1924 | 0 | CHECK_DBOUNDS(ZSTD_d_forceIgnoreChecksum, value); |
1925 | 0 | dctx->forceIgnoreChecksum = (ZSTD_forceIgnoreChecksum_e)value; |
1926 | 0 | return 0; |
1927 | 0 | case ZSTD_d_refMultipleDDicts: |
1928 | 0 | CHECK_DBOUNDS(ZSTD_d_refMultipleDDicts, value); |
1929 | 0 | if (dctx->staticSize != 0) { |
1930 | 0 | RETURN_ERROR(parameter_unsupported, "Static dctx does not support multiple DDicts!"); |
1931 | 0 | } |
1932 | 0 | dctx->refMultipleDDicts = (ZSTD_refMultipleDDicts_e)value; |
1933 | 0 | return 0; |
1934 | 0 | case ZSTD_d_disableHuffmanAssembly: |
1935 | 0 | CHECK_DBOUNDS(ZSTD_d_disableHuffmanAssembly, value); |
1936 | 0 | dctx->disableHufAsm = value != 0; |
1937 | 0 | return 0; |
1938 | 0 | case ZSTD_d_maxBlockSize: |
1939 | 0 | if (value != 0) CHECK_DBOUNDS(ZSTD_d_maxBlockSize, value); |
1940 | 0 | dctx->maxBlockSizeParam = value; |
1941 | 0 | return 0; |
1942 | 0 | default:; |
1943 | 0 | } |
1944 | 0 | RETURN_ERROR(parameter_unsupported, ""); |
1945 | 0 | } |
1946 | | |
1947 | | size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset) |
1948 | 0 | { |
1949 | 0 | if ( (reset == ZSTD_reset_session_only) |
1950 | 0 | || (reset == ZSTD_reset_session_and_parameters) ) { |
1951 | 0 | dctx->streamStage = zdss_init; |
1952 | 0 | dctx->noForwardProgress = 0; |
1953 | 0 | dctx->isFrameDecompression = 1; |
1954 | 0 | } |
1955 | 0 | if ( (reset == ZSTD_reset_parameters) |
1956 | 0 | || (reset == ZSTD_reset_session_and_parameters) ) { |
1957 | 0 | RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, ""); |
1958 | 0 | ZSTD_clearDict(dctx); |
1959 | 0 | ZSTD_DCtx_resetParameters(dctx); |
1960 | 0 | } |
1961 | 0 | return 0; |
1962 | 0 | } |
1963 | | |
1964 | | |
1965 | | size_t ZSTD_sizeof_DStream(const ZSTD_DStream* dctx) |
1966 | 0 | { |
1967 | 0 | return ZSTD_sizeof_DCtx(dctx); |
1968 | 0 | } |
1969 | | |
1970 | | static size_t ZSTD_decodingBufferSize_internal(unsigned long long windowSize, unsigned long long frameContentSize, size_t blockSizeMax) |
1971 | 12.6k | { |
1972 | 12.6k | size_t const blockSize = MIN((size_t)MIN(windowSize, ZSTD_BLOCKSIZE_MAX), blockSizeMax); |
1973 | | /* We need blockSize + WILDCOPY_OVERLENGTH worth of buffer so that if a block |
1974 | | * ends at windowSize + WILDCOPY_OVERLENGTH + 1 bytes, we can start writing |
1975 | | * the block at the beginning of the output buffer, and maintain a full window. |
1976 | | * |
1977 | | * We need another blockSize worth of buffer so that we can store split |
1978 | | * literals at the end of the block without overwriting the extDict window. |
1979 | | */ |
1980 | 12.6k | unsigned long long const neededRBSize = windowSize + (blockSize * 2) + (WILDCOPY_OVERLENGTH * 2); |
1981 | 12.6k | unsigned long long const neededSize = MIN(frameContentSize, neededRBSize); |
1982 | 12.6k | size_t const minRBSize = (size_t) neededSize; |
1983 | 12.6k | RETURN_ERROR_IF((unsigned long long)minRBSize != neededSize, |
1984 | 12.6k | frameParameter_windowTooLarge, ""); |
1985 | 12.6k | return minRBSize; |
1986 | 12.6k | } |
1987 | | |
1988 | | size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize) |
1989 | 0 | { |
1990 | 0 | return ZSTD_decodingBufferSize_internal(windowSize, frameContentSize, ZSTD_BLOCKSIZE_MAX); |
1991 | 0 | } |
1992 | | |
1993 | | size_t ZSTD_estimateDStreamSize(size_t windowSize) |
1994 | 0 | { |
1995 | 0 | size_t const blockSize = MIN(windowSize, ZSTD_BLOCKSIZE_MAX); |
1996 | 0 | size_t const inBuffSize = blockSize; /* no block can be larger */ |
1997 | 0 | size_t const outBuffSize = ZSTD_decodingBufferSize_min(windowSize, ZSTD_CONTENTSIZE_UNKNOWN); |
1998 | 0 | return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize; |
1999 | 0 | } |
2000 | | |
2001 | | size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize) |
2002 | 0 | { |
2003 | 0 | U32 const windowSizeMax = 1U << ZSTD_WINDOWLOG_MAX; /* note : should be user-selectable, but requires an additional parameter (or a dctx) */ |
2004 | 0 | ZSTD_FrameHeader zfh; |
2005 | 0 | size_t const err = ZSTD_getFrameHeader(&zfh, src, srcSize); |
2006 | 0 | if (ZSTD_isError(err)) return err; |
2007 | 0 | RETURN_ERROR_IF(err>0, srcSize_wrong, ""); |
2008 | 0 | RETURN_ERROR_IF(zfh.windowSize > windowSizeMax, |
2009 | 0 | frameParameter_windowTooLarge, ""); |
2010 | 0 | return ZSTD_estimateDStreamSize((size_t)zfh.windowSize); |
2011 | 0 | } |
2012 | | |
2013 | | |
2014 | | /* ***** Decompression ***** */ |
2015 | | |
2016 | | static int ZSTD_DCtx_isOverflow(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize) |
2017 | 12.6k | { |
2018 | 12.6k | return (zds->inBuffSize + zds->outBuffSize) >= (neededInBuffSize + neededOutBuffSize) * ZSTD_WORKSPACETOOLARGE_FACTOR; |
2019 | 12.6k | } |
2020 | | |
2021 | | static void ZSTD_DCtx_updateOversizedDuration(ZSTD_DStream* zds, size_t const neededInBuffSize, size_t const neededOutBuffSize) |
2022 | 12.6k | { |
2023 | 12.6k | if (ZSTD_DCtx_isOverflow(zds, neededInBuffSize, neededOutBuffSize)) |
2024 | 0 | zds->oversizedDuration++; |
2025 | 12.6k | else |
2026 | 12.6k | zds->oversizedDuration = 0; |
2027 | 12.6k | } |
2028 | | |
2029 | | static int ZSTD_DCtx_isOversizedTooLong(ZSTD_DStream* zds) |
2030 | 12.6k | { |
2031 | 12.6k | return zds->oversizedDuration >= ZSTD_WORKSPACETOOLARGE_MAXDURATION; |
2032 | 12.6k | } |
2033 | | |
2034 | | /* Checks that the output buffer hasn't changed if ZSTD_obm_stable is used. */ |
2035 | | static size_t ZSTD_checkOutBuffer(ZSTD_DStream const* zds, ZSTD_outBuffer const* output) |
2036 | 80.8k | { |
2037 | 80.8k | ZSTD_outBuffer const expect = zds->expectedOutBuffer; |
2038 | | /* No requirement when ZSTD_obm_stable is not enabled. */ |
2039 | 80.8k | if (zds->outBufferMode != ZSTD_bm_stable) |
2040 | 80.8k | return 0; |
2041 | | /* Any buffer is allowed in zdss_init, this must be the same for every other call until |
2042 | | * the context is reset. |
2043 | | */ |
2044 | 0 | if (zds->streamStage == zdss_init) |
2045 | 0 | return 0; |
2046 | | /* The buffer must match our expectation exactly. */ |
2047 | 0 | if (expect.dst == output->dst && expect.pos == output->pos && expect.size == output->size) |
2048 | 0 | return 0; |
2049 | 0 | RETURN_ERROR(dstBuffer_wrong, "ZSTD_d_stableOutBuffer enabled but output differs!"); |
2050 | 0 | } |
2051 | | |
2052 | | /* Calls ZSTD_decompressContinue() with the right parameters for ZSTD_decompressStream() |
2053 | | * and updates the stage and the output buffer state. This call is extracted so it can be |
2054 | | * used both when reading directly from the ZSTD_inBuffer, and in buffered input mode. |
2055 | | * NOTE: You must break after calling this function since the streamStage is modified. |
2056 | | */ |
2057 | | static size_t ZSTD_decompressContinueStream( |
2058 | | ZSTD_DStream* zds, char** op, char* oend, |
2059 | 2.02M | void const* src, size_t srcSize) { |
2060 | 2.02M | int const isSkipFrame = ZSTD_isSkipFrame(zds); |
2061 | 2.02M | if (zds->outBufferMode == ZSTD_bm_buffered) { |
2062 | 2.02M | size_t const dstSize = isSkipFrame ? 0 : zds->outBuffSize - zds->outStart; |
2063 | 2.02M | size_t const decodedSize = ZSTD_decompressContinue(zds, |
2064 | 2.02M | zds->outBuff + zds->outStart, dstSize, src, srcSize); |
2065 | 2.02M | FORWARD_IF_ERROR(decodedSize, ""); |
2066 | 2.00M | if (!decodedSize && !isSkipFrame) { |
2067 | 1.97M | zds->streamStage = zdss_read; |
2068 | 1.97M | } else { |
2069 | 29.8k | zds->outEnd = zds->outStart + decodedSize; |
2070 | 29.8k | zds->streamStage = zdss_flush; |
2071 | 29.8k | } |
2072 | 2.00M | } else { |
2073 | | /* Write directly into the output buffer */ |
2074 | 0 | size_t const dstSize = isSkipFrame ? 0 : (size_t)(oend - *op); |
2075 | 0 | size_t const decodedSize = ZSTD_decompressContinue(zds, *op, dstSize, src, srcSize); |
2076 | 0 | FORWARD_IF_ERROR(decodedSize, ""); |
2077 | 0 | *op += decodedSize; |
2078 | | /* Flushing is not needed. */ |
2079 | 0 | zds->streamStage = zdss_read; |
2080 | 0 | assert(*op <= oend); |
2081 | 0 | assert(zds->outBufferMode == ZSTD_bm_stable); |
2082 | 0 | } |
2083 | 2.00M | return 0; |
2084 | 2.02M | } |
2085 | | |
2086 | | size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input) |
2087 | 80.8k | { |
2088 | 80.8k | const char* const src = (const char*)input->src; |
2089 | 80.8k | const char* const istart = input->pos != 0 ? src + input->pos : src; |
2090 | 80.8k | const char* const iend = input->size != 0 ? src + input->size : src; |
2091 | 80.8k | const char* ip = istart; |
2092 | 80.8k | char* const dst = (char*)output->dst; |
2093 | 80.8k | char* const ostart = output->pos != 0 ? dst + output->pos : dst; |
2094 | 80.8k | char* const oend = output->size != 0 ? dst + output->size : dst; |
2095 | 80.8k | char* op = ostart; |
2096 | 80.8k | U32 someMoreWork = 1; |
2097 | | |
2098 | 80.8k | DEBUGLOG(5, "ZSTD_decompressStream"); |
2099 | 80.8k | assert(zds != NULL); |
2100 | 80.8k | RETURN_ERROR_IF( |
2101 | 80.8k | input->pos > input->size, |
2102 | 80.8k | srcSize_wrong, |
2103 | 80.8k | "forbidden. in: pos: %u vs size: %u", |
2104 | 80.8k | (U32)input->pos, (U32)input->size); |
2105 | 80.8k | RETURN_ERROR_IF( |
2106 | 80.8k | output->pos > output->size, |
2107 | 80.8k | dstSize_tooSmall, |
2108 | 80.8k | "forbidden. out: pos: %u vs size: %u", |
2109 | 80.8k | (U32)output->pos, (U32)output->size); |
2110 | 80.8k | DEBUGLOG(5, "input size : %u", (U32)(input->size - input->pos)); |
2111 | 80.8k | FORWARD_IF_ERROR(ZSTD_checkOutBuffer(zds, output), ""); |
2112 | | |
2113 | 2.19M | while (someMoreWork) { |
2114 | 2.17M | switch(zds->streamStage) |
2115 | 2.17M | { |
2116 | 45.6k | case zdss_init : |
2117 | 45.6k | DEBUGLOG(5, "stage zdss_init => transparent reset "); |
2118 | 45.6k | zds->streamStage = zdss_loadHeader; |
2119 | 45.6k | zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0; |
2120 | 45.6k | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
2121 | 45.6k | zds->legacyVersion = 0; |
2122 | 45.6k | #endif |
2123 | 45.6k | zds->hostageByte = 0; |
2124 | 45.6k | zds->expectedOutBuffer = *output; |
2125 | 45.6k | ZSTD_FALLTHROUGH; |
2126 | | |
2127 | 127k | case zdss_loadHeader : |
2128 | 127k | DEBUGLOG(5, "stage zdss_loadHeader (srcSize : %u)", (U32)(iend - ip)); |
2129 | 127k | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
2130 | 127k | if (zds->legacyVersion) { |
2131 | 23.1k | RETURN_ERROR_IF(zds->staticSize, memory_allocation, |
2132 | 23.1k | "legacy support is incompatible with static dctx"); |
2133 | 23.1k | { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, zds->legacyVersion, output, input); |
2134 | 23.1k | if (hint==0) zds->streamStage = zdss_init; |
2135 | 23.1k | return hint; |
2136 | 23.1k | } } |
2137 | 104k | #endif |
2138 | 104k | { size_t const hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format); |
2139 | 104k | if (zds->refMultipleDDicts && zds->ddictSet) { |
2140 | 0 | ZSTD_DCtx_selectFrameDDict(zds); |
2141 | 0 | } |
2142 | 104k | if (ZSTD_isError(hSize)) { |
2143 | 31.9k | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>=1) |
2144 | 31.9k | U32 const legacyVersion = ZSTD_isLegacy(istart, iend-istart); |
2145 | 31.9k | if (legacyVersion) { |
2146 | 25.5k | ZSTD_DDict const* const ddict = ZSTD_getDDict(zds); |
2147 | 25.5k | const void* const dict = ddict ? ZSTD_DDict_dictContent(ddict) : NULL; |
2148 | 25.5k | size_t const dictSize = ddict ? ZSTD_DDict_dictSize(ddict) : 0; |
2149 | 25.5k | DEBUGLOG(5, "ZSTD_decompressStream: detected legacy version v0.%u", legacyVersion); |
2150 | 25.5k | RETURN_ERROR_IF(zds->staticSize, memory_allocation, |
2151 | 25.5k | "legacy support is incompatible with static dctx"); |
2152 | 25.5k | FORWARD_IF_ERROR(ZSTD_initLegacyStream(&zds->legacyContext, |
2153 | 25.5k | zds->previousLegacyVersion, legacyVersion, |
2154 | 25.5k | dict, dictSize), ""); |
2155 | 25.5k | zds->legacyVersion = zds->previousLegacyVersion = legacyVersion; |
2156 | 25.5k | { size_t const hint = ZSTD_decompressLegacyStream(zds->legacyContext, legacyVersion, output, input); |
2157 | 25.5k | if (hint==0) zds->streamStage = zdss_init; /* or stay in stage zdss_loadHeader */ |
2158 | 25.5k | return hint; |
2159 | 25.5k | } } |
2160 | 6.46k | #endif |
2161 | 6.46k | return hSize; /* error */ |
2162 | 31.9k | } |
2163 | 72.4k | if (hSize != 0) { /* need more input */ |
2164 | 59.0k | size_t const toLoad = hSize - zds->lhSize; /* if hSize!=0, hSize > zds->lhSize */ |
2165 | 59.0k | size_t const remainingInput = (size_t)(iend-ip); |
2166 | 59.0k | assert(iend >= ip); |
2167 | 59.0k | if (toLoad > remainingInput) { /* not enough input to load full header */ |
2168 | 272 | if (remainingInput > 0) { |
2169 | 247 | ZSTD_memcpy(zds->headerBuffer + zds->lhSize, ip, remainingInput); |
2170 | 247 | zds->lhSize += remainingInput; |
2171 | 247 | } |
2172 | 272 | input->pos = input->size; |
2173 | | /* check first few bytes */ |
2174 | 272 | FORWARD_IF_ERROR( |
2175 | 272 | ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format), |
2176 | 272 | "First few bytes detected incorrect" ); |
2177 | | /* return hint input size */ |
2178 | 70 | return (MAX((size_t)ZSTD_FRAMEHEADERSIZE_MIN(zds->format), hSize) - zds->lhSize) + ZSTD_blockHeaderSize; /* remaining header bytes + next block header */ |
2179 | 272 | } |
2180 | 58.7k | assert(ip != NULL); |
2181 | 58.7k | ZSTD_memcpy(zds->headerBuffer + zds->lhSize, ip, toLoad); zds->lhSize = hSize; ip += toLoad; |
2182 | 58.7k | break; |
2183 | 59.0k | } } |
2184 | | |
2185 | | /* check for single-pass mode opportunity */ |
2186 | 13.3k | if (zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN |
2187 | 13.3k | && zds->fParams.frameType != ZSTD_skippableFrame |
2188 | 13.3k | && (U64)(size_t)(oend-op) >= zds->fParams.frameContentSize) { |
2189 | 1.18k | size_t const cSize = ZSTD_findFrameCompressedSize_advanced(istart, (size_t)(iend-istart), zds->format); |
2190 | 1.18k | if (cSize <= (size_t)(iend-istart)) { |
2191 | | /* shortcut : using single-pass mode */ |
2192 | 485 | size_t const decompressedSize = ZSTD_decompress_usingDDict(zds, op, (size_t)(oend-op), istart, cSize, ZSTD_getDDict(zds)); |
2193 | 485 | if (ZSTD_isError(decompressedSize)) return decompressedSize; |
2194 | 17 | DEBUGLOG(4, "shortcut to single-pass ZSTD_decompress_usingDDict()"); |
2195 | 17 | assert(istart != NULL); |
2196 | 17 | ip = istart + cSize; |
2197 | 17 | op = op ? op + decompressedSize : op; /* can occur if frameContentSize = 0 (empty frame) */ |
2198 | 17 | zds->expected = 0; |
2199 | 17 | zds->streamStage = zdss_init; |
2200 | 17 | someMoreWork = 0; |
2201 | 17 | break; |
2202 | 485 | } } |
2203 | | |
2204 | | /* Check output buffer is large enough for ZSTD_odm_stable. */ |
2205 | 12.8k | if (zds->outBufferMode == ZSTD_bm_stable |
2206 | 12.8k | && zds->fParams.frameType != ZSTD_skippableFrame |
2207 | 12.8k | && zds->fParams.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN |
2208 | 12.8k | && (U64)(size_t)(oend-op) < zds->fParams.frameContentSize) { |
2209 | 0 | RETURN_ERROR(dstSize_tooSmall, "ZSTD_obm_stable passed but ZSTD_outBuffer is too small"); |
2210 | 0 | } |
2211 | | |
2212 | | /* Consume header (see ZSTDds_decodeFrameHeader) */ |
2213 | 12.8k | DEBUGLOG(4, "Consume header"); |
2214 | 12.8k | FORWARD_IF_ERROR(ZSTD_decompressBegin_usingDDict(zds, ZSTD_getDDict(zds)), ""); |
2215 | | |
2216 | 12.8k | if (zds->format == ZSTD_f_zstd1 |
2217 | 12.8k | && (MEM_readLE32(zds->headerBuffer) & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { /* skippable frame */ |
2218 | 117 | zds->expected = MEM_readLE32(zds->headerBuffer + ZSTD_FRAMEIDSIZE); |
2219 | 117 | zds->stage = ZSTDds_skipFrame; |
2220 | 12.7k | } else { |
2221 | 12.7k | FORWARD_IF_ERROR(ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize), ""); |
2222 | 12.7k | zds->expected = ZSTD_blockHeaderSize; |
2223 | 12.7k | zds->stage = ZSTDds_decodeBlockHeader; |
2224 | 12.7k | } |
2225 | | |
2226 | | /* control buffer memory usage */ |
2227 | 12.8k | DEBUGLOG(4, "Control max memory usage (%u KB <= max %u KB)", |
2228 | 12.8k | (U32)(zds->fParams.windowSize >>10), |
2229 | 12.8k | (U32)(zds->maxWindowSize >> 10) ); |
2230 | 12.8k | zds->fParams.windowSize = MAX(zds->fParams.windowSize, 1U << ZSTD_WINDOWLOG_ABSOLUTEMIN); |
2231 | 12.8k | RETURN_ERROR_IF(zds->fParams.windowSize > zds->maxWindowSize, |
2232 | 12.8k | frameParameter_windowTooLarge, ""); |
2233 | 12.6k | if (zds->maxBlockSizeParam != 0) |
2234 | 0 | zds->fParams.blockSizeMax = MIN(zds->fParams.blockSizeMax, (unsigned)zds->maxBlockSizeParam); |
2235 | | |
2236 | | /* Adapt buffer sizes to frame header instructions */ |
2237 | 12.6k | { size_t const neededInBuffSize = MAX(zds->fParams.blockSizeMax, 4 /* frame checksum */); |
2238 | 12.6k | size_t const neededOutBuffSize = zds->outBufferMode == ZSTD_bm_buffered |
2239 | 12.6k | ? ZSTD_decodingBufferSize_internal(zds->fParams.windowSize, zds->fParams.frameContentSize, zds->fParams.blockSizeMax) |
2240 | 12.6k | : 0; |
2241 | | |
2242 | 12.6k | ZSTD_DCtx_updateOversizedDuration(zds, neededInBuffSize, neededOutBuffSize); |
2243 | | |
2244 | 12.6k | { int const tooSmall = (zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize); |
2245 | 12.6k | int const tooLarge = ZSTD_DCtx_isOversizedTooLong(zds); |
2246 | | |
2247 | 12.6k | if (tooSmall || tooLarge) { |
2248 | 12.6k | size_t const bufferSize = neededInBuffSize + neededOutBuffSize; |
2249 | 12.6k | DEBUGLOG(4, "inBuff : from %u to %u", |
2250 | 12.6k | (U32)zds->inBuffSize, (U32)neededInBuffSize); |
2251 | 12.6k | DEBUGLOG(4, "outBuff : from %u to %u", |
2252 | 12.6k | (U32)zds->outBuffSize, (U32)neededOutBuffSize); |
2253 | 12.6k | if (zds->staticSize) { /* static DCtx */ |
2254 | 0 | DEBUGLOG(4, "staticSize : %u", (U32)zds->staticSize); |
2255 | 0 | assert(zds->staticSize >= sizeof(ZSTD_DCtx)); /* controlled at init */ |
2256 | 0 | RETURN_ERROR_IF( |
2257 | 0 | bufferSize > zds->staticSize - sizeof(ZSTD_DCtx), |
2258 | 0 | memory_allocation, ""); |
2259 | 12.6k | } else { |
2260 | 12.6k | ZSTD_customFree(zds->inBuff, zds->customMem); |
2261 | 12.6k | zds->inBuffSize = 0; |
2262 | 12.6k | zds->outBuffSize = 0; |
2263 | 12.6k | zds->inBuff = (char*)ZSTD_customMalloc(bufferSize, zds->customMem); |
2264 | 12.6k | RETURN_ERROR_IF(zds->inBuff == NULL, memory_allocation, ""); |
2265 | 12.6k | } |
2266 | 12.6k | zds->inBuffSize = neededInBuffSize; |
2267 | 12.6k | zds->outBuff = zds->inBuff + zds->inBuffSize; |
2268 | 12.6k | zds->outBuffSize = neededOutBuffSize; |
2269 | 12.6k | } } } |
2270 | 12.6k | zds->streamStage = zdss_read; |
2271 | 12.6k | ZSTD_FALLTHROUGH; |
2272 | | |
2273 | 2.03M | case zdss_read: |
2274 | 2.03M | DEBUGLOG(5, "stage zdss_read"); |
2275 | 2.03M | { size_t const neededInSize = ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (size_t)(iend - ip)); |
2276 | 2.03M | DEBUGLOG(5, "neededInSize = %u", (U32)neededInSize); |
2277 | 2.03M | if (neededInSize==0) { /* end of frame */ |
2278 | 100 | zds->streamStage = zdss_init; |
2279 | 100 | someMoreWork = 0; |
2280 | 100 | break; |
2281 | 100 | } |
2282 | 2.03M | if ((size_t)(iend-ip) >= neededInSize) { /* decode directly from src */ |
2283 | 2.01M | FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, ip, neededInSize), ""); |
2284 | 2.00M | assert(ip != NULL); |
2285 | 2.00M | ip += neededInSize; |
2286 | | /* Function modifies the stage so we must break */ |
2287 | 2.00M | break; |
2288 | 2.01M | } } |
2289 | 10.8k | if (ip==iend) { someMoreWork = 0; break; } /* no more input */ |
2290 | 976 | zds->streamStage = zdss_load; |
2291 | 976 | ZSTD_FALLTHROUGH; |
2292 | | |
2293 | 2.49k | case zdss_load: |
2294 | 2.49k | { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zds); |
2295 | 2.49k | size_t const toLoad = neededInSize - zds->inPos; |
2296 | 2.49k | int const isSkipFrame = ZSTD_isSkipFrame(zds); |
2297 | 2.49k | size_t loadedSize; |
2298 | | /* At this point we shouldn't be decompressing a block that we can stream. */ |
2299 | 2.49k | assert(neededInSize == ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (size_t)(iend - ip))); |
2300 | 2.49k | if (isSkipFrame) { |
2301 | 147 | loadedSize = MIN(toLoad, (size_t)(iend-ip)); |
2302 | 2.34k | } else { |
2303 | 2.34k | RETURN_ERROR_IF(toLoad > zds->inBuffSize - zds->inPos, |
2304 | 2.34k | corruption_detected, |
2305 | 2.34k | "should never happen"); |
2306 | 2.34k | loadedSize = ZSTD_limitCopy(zds->inBuff + zds->inPos, toLoad, ip, (size_t)(iend-ip)); |
2307 | 2.34k | } |
2308 | 2.49k | if (loadedSize != 0) { |
2309 | | /* ip may be NULL */ |
2310 | 2.49k | ip += loadedSize; |
2311 | 2.49k | zds->inPos += loadedSize; |
2312 | 2.49k | } |
2313 | 2.49k | if (loadedSize < toLoad) { someMoreWork = 0; break; } /* not enough input, wait for more */ |
2314 | | |
2315 | | /* decode loaded input */ |
2316 | 560 | zds->inPos = 0; /* input is consumed */ |
2317 | 560 | FORWARD_IF_ERROR(ZSTD_decompressContinueStream(zds, &op, oend, zds->inBuff, neededInSize), ""); |
2318 | | /* Function modifies the stage so we must break */ |
2319 | 412 | break; |
2320 | 560 | } |
2321 | 31.3k | case zdss_flush: |
2322 | 31.3k | { |
2323 | 31.3k | size_t const toFlushSize = zds->outEnd - zds->outStart; |
2324 | 31.3k | size_t const flushedSize = ZSTD_limitCopy(op, (size_t)(oend-op), zds->outBuff + zds->outStart, toFlushSize); |
2325 | | |
2326 | 31.3k | op = op ? op + flushedSize : op; |
2327 | | |
2328 | 31.3k | zds->outStart += flushedSize; |
2329 | 31.3k | if (flushedSize == toFlushSize) { /* flush completed */ |
2330 | 29.7k | zds->streamStage = zdss_read; |
2331 | 29.7k | if ( (zds->outBuffSize < zds->fParams.frameContentSize) |
2332 | 29.7k | && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize) ) { |
2333 | 72 | DEBUGLOG(5, "restart filling outBuff from beginning (left:%i, needed:%u)", |
2334 | 72 | (int)(zds->outBuffSize - zds->outStart), |
2335 | 72 | (U32)zds->fParams.blockSizeMax); |
2336 | 72 | zds->outStart = zds->outEnd = 0; |
2337 | 72 | } |
2338 | 29.7k | break; |
2339 | 29.7k | } } |
2340 | | /* cannot complete flush */ |
2341 | 1.66k | someMoreWork = 0; |
2342 | 1.66k | break; |
2343 | | |
2344 | 0 | default: |
2345 | 0 | assert(0); /* impossible */ |
2346 | 0 | RETURN_ERROR(GENERIC, "impossible to reach"); /* some compilers require default to do something */ |
2347 | 2.17M | } } |
2348 | | |
2349 | | /* result */ |
2350 | 13.6k | input->pos = (size_t)(ip - (const char*)(input->src)); |
2351 | 13.6k | output->pos = (size_t)(op - (char*)(output->dst)); |
2352 | | |
2353 | | /* Update the expected output buffer for ZSTD_obm_stable. */ |
2354 | 13.6k | zds->expectedOutBuffer = *output; |
2355 | | |
2356 | 13.6k | if ((ip==istart) && (op==ostart)) { /* no forward progress */ |
2357 | 0 | zds->noForwardProgress ++; |
2358 | 0 | if (zds->noForwardProgress >= ZSTD_NO_FORWARD_PROGRESS_MAX) { |
2359 | 0 | RETURN_ERROR_IF(op==oend, noForwardProgress_destFull, ""); |
2360 | 0 | RETURN_ERROR_IF(ip==iend, noForwardProgress_inputEmpty, ""); |
2361 | 0 | assert(0); |
2362 | 0 | } |
2363 | 13.6k | } else { |
2364 | 13.6k | zds->noForwardProgress = 0; |
2365 | 13.6k | } |
2366 | 13.6k | { size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds); |
2367 | 13.6k | if (!nextSrcSizeHint) { /* frame fully decoded */ |
2368 | 161 | if (zds->outEnd == zds->outStart) { /* output fully flushed */ |
2369 | 117 | if (zds->hostageByte) { |
2370 | 0 | if (input->pos >= input->size) { |
2371 | | /* can't release hostage (not present) */ |
2372 | 0 | zds->streamStage = zdss_read; |
2373 | 0 | return 1; |
2374 | 0 | } |
2375 | 0 | input->pos++; /* release hostage */ |
2376 | 0 | } /* zds->hostageByte */ |
2377 | 117 | return 0; |
2378 | 117 | } /* zds->outEnd == zds->outStart */ |
2379 | 44 | if (!zds->hostageByte) { /* output not fully flushed; keep last byte as hostage; will be released when all output is flushed */ |
2380 | 44 | input->pos--; /* note : pos > 0, otherwise, impossible to finish reading last block */ |
2381 | 44 | zds->hostageByte=1; |
2382 | 44 | } |
2383 | 44 | return 1; |
2384 | 161 | } /* nextSrcSizeHint==0 */ |
2385 | 13.4k | nextSrcSizeHint += ZSTD_blockHeaderSize * (ZSTD_nextInputType(zds) == ZSTDnit_block); /* preload header of next block */ |
2386 | 13.4k | assert(zds->inPos <= nextSrcSizeHint); |
2387 | 13.4k | nextSrcSizeHint -= zds->inPos; /* part already loaded*/ |
2388 | 13.4k | return nextSrcSizeHint; |
2389 | 13.6k | } |
2390 | 13.6k | } |
2391 | | |
2392 | | size_t ZSTD_decompressStream_simpleArgs ( |
2393 | | ZSTD_DCtx* dctx, |
2394 | | void* dst, size_t dstCapacity, size_t* dstPos, |
2395 | | const void* src, size_t srcSize, size_t* srcPos) |
2396 | 0 | { |
2397 | 0 | ZSTD_outBuffer output; |
2398 | 0 | ZSTD_inBuffer input; |
2399 | 0 | output.dst = dst; |
2400 | 0 | output.size = dstCapacity; |
2401 | 0 | output.pos = *dstPos; |
2402 | 0 | input.src = src; |
2403 | 0 | input.size = srcSize; |
2404 | 0 | input.pos = *srcPos; |
2405 | 0 | { size_t const cErr = ZSTD_decompressStream(dctx, &output, &input); |
2406 | 0 | *dstPos = output.pos; |
2407 | 0 | *srcPos = input.pos; |
2408 | 0 | return cErr; |
2409 | 0 | } |
2410 | 0 | } |