2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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.
14 #if defined (__cplusplus)
18 /* *************************************
20 ***************************************/
21 #include "mem.h" /* MEM_STATIC */
22 #include "error_private.h" /* ERROR */
23 #include "zstd.h" /* ZSTD_inBuffer, ZSTD_outBuffer */
25 #if !defined (ZSTD_LEGACY_SUPPORT) || (ZSTD_LEGACY_SUPPORT == 0)
26 # undef ZSTD_LEGACY_SUPPORT
27 # define ZSTD_LEGACY_SUPPORT 8
30 #if (ZSTD_LEGACY_SUPPORT <= 1)
31 # include "zstd_v01.h"
33 #if (ZSTD_LEGACY_SUPPORT <= 2)
34 # include "zstd_v02.h"
36 #if (ZSTD_LEGACY_SUPPORT <= 3)
37 # include "zstd_v03.h"
39 #if (ZSTD_LEGACY_SUPPORT <= 4)
40 # include "zstd_v04.h"
42 #if (ZSTD_LEGACY_SUPPORT <= 5)
43 # include "zstd_v05.h"
45 #if (ZSTD_LEGACY_SUPPORT <= 6)
46 # include "zstd_v06.h"
48 #if (ZSTD_LEGACY_SUPPORT <= 7)
49 # include "zstd_v07.h"
53 @return : > 0 if supported by legacy decoder. 0 otherwise.
54 return value is the version.
56 MEM_STATIC unsigned ZSTD_isLegacy(const void* src, size_t srcSize)
59 if (srcSize<4) return 0;
60 magicNumberLE = MEM_readLE32(src);
63 #if (ZSTD_LEGACY_SUPPORT <= 1)
64 case ZSTDv01_magicNumberLE:return 1;
66 #if (ZSTD_LEGACY_SUPPORT <= 2)
67 case ZSTDv02_magicNumber : return 2;
69 #if (ZSTD_LEGACY_SUPPORT <= 3)
70 case ZSTDv03_magicNumber : return 3;
72 #if (ZSTD_LEGACY_SUPPORT <= 4)
73 case ZSTDv04_magicNumber : return 4;
75 #if (ZSTD_LEGACY_SUPPORT <= 5)
76 case ZSTDv05_MAGICNUMBER : return 5;
78 #if (ZSTD_LEGACY_SUPPORT <= 6)
79 case ZSTDv06_MAGICNUMBER : return 6;
81 #if (ZSTD_LEGACY_SUPPORT <= 7)
82 case ZSTDv07_MAGICNUMBER : return 7;
89 MEM_STATIC unsigned long long ZSTD_getDecompressedSize_legacy(const void* src, size_t srcSize)
91 U32 const version = ZSTD_isLegacy(src, srcSize);
92 if (version < 5) return 0; /* no decompressed size in frame header, or not a legacy format */
93 #if (ZSTD_LEGACY_SUPPORT <= 5)
95 ZSTDv05_parameters fParams;
96 size_t const frResult = ZSTDv05_getFrameParams(&fParams, src, srcSize);
97 if (frResult != 0) return 0;
98 return fParams.srcSize;
101 #if (ZSTD_LEGACY_SUPPORT <= 6)
103 ZSTDv06_frameParams fParams;
104 size_t const frResult = ZSTDv06_getFrameParams(&fParams, src, srcSize);
105 if (frResult != 0) return 0;
106 return fParams.frameContentSize;
109 #if (ZSTD_LEGACY_SUPPORT <= 7)
111 ZSTDv07_frameParams fParams;
112 size_t const frResult = ZSTDv07_getFrameParams(&fParams, src, srcSize);
113 if (frResult != 0) return 0;
114 return fParams.frameContentSize;
117 return 0; /* should not be possible */
121 MEM_STATIC size_t ZSTD_decompressLegacy(
122 void* dst, size_t dstCapacity,
123 const void* src, size_t compressedSize,
124 const void* dict,size_t dictSize)
126 U32 const version = ZSTD_isLegacy(src, compressedSize);
127 (void)dst; (void)dstCapacity; (void)dict; (void)dictSize; /* unused when ZSTD_LEGACY_SUPPORT >= 8 */
130 #if (ZSTD_LEGACY_SUPPORT <= 1)
132 return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize);
134 #if (ZSTD_LEGACY_SUPPORT <= 2)
136 return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize);
138 #if (ZSTD_LEGACY_SUPPORT <= 3)
140 return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize);
142 #if (ZSTD_LEGACY_SUPPORT <= 4)
144 return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize);
146 #if (ZSTD_LEGACY_SUPPORT <= 5)
149 ZSTDv05_DCtx* const zd = ZSTDv05_createDCtx();
150 if (zd==NULL) return ERROR(memory_allocation);
151 result = ZSTDv05_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
152 ZSTDv05_freeDCtx(zd);
156 #if (ZSTD_LEGACY_SUPPORT <= 6)
159 ZSTDv06_DCtx* const zd = ZSTDv06_createDCtx();
160 if (zd==NULL) return ERROR(memory_allocation);
161 result = ZSTDv06_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
162 ZSTDv06_freeDCtx(zd);
166 #if (ZSTD_LEGACY_SUPPORT <= 7)
169 ZSTDv07_DCtx* const zd = ZSTDv07_createDCtx();
170 if (zd==NULL) return ERROR(memory_allocation);
171 result = ZSTDv07_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
172 ZSTDv07_freeDCtx(zd);
177 return ERROR(prefix_unknown);
181 MEM_STATIC size_t ZSTD_findFrameCompressedSizeLegacy(const void *src,
182 size_t compressedSize)
184 U32 const version = ZSTD_isLegacy(src, compressedSize);
187 #if (ZSTD_LEGACY_SUPPORT <= 1)
189 return ZSTDv01_findFrameCompressedSize(src, compressedSize);
191 #if (ZSTD_LEGACY_SUPPORT <= 2)
193 return ZSTDv02_findFrameCompressedSize(src, compressedSize);
195 #if (ZSTD_LEGACY_SUPPORT <= 3)
197 return ZSTDv03_findFrameCompressedSize(src, compressedSize);
199 #if (ZSTD_LEGACY_SUPPORT <= 4)
201 return ZSTDv04_findFrameCompressedSize(src, compressedSize);
203 #if (ZSTD_LEGACY_SUPPORT <= 5)
205 return ZSTDv05_findFrameCompressedSize(src, compressedSize);
207 #if (ZSTD_LEGACY_SUPPORT <= 6)
209 return ZSTDv06_findFrameCompressedSize(src, compressedSize);
211 #if (ZSTD_LEGACY_SUPPORT <= 7)
213 return ZSTDv07_findFrameCompressedSize(src, compressedSize);
216 return ERROR(prefix_unknown);
220 MEM_STATIC size_t ZSTD_freeLegacyStreamContext(void* legacyContext, U32 version)
229 return ERROR(version_unsupported);
230 #if (ZSTD_LEGACY_SUPPORT <= 4)
231 case 4 : return ZBUFFv04_freeDCtx((ZBUFFv04_DCtx*)legacyContext);
233 #if (ZSTD_LEGACY_SUPPORT <= 5)
234 case 5 : return ZBUFFv05_freeDCtx((ZBUFFv05_DCtx*)legacyContext);
236 #if (ZSTD_LEGACY_SUPPORT <= 6)
237 case 6 : return ZBUFFv06_freeDCtx((ZBUFFv06_DCtx*)legacyContext);
239 #if (ZSTD_LEGACY_SUPPORT <= 7)
240 case 7 : return ZBUFFv07_freeDCtx((ZBUFFv07_DCtx*)legacyContext);
246 MEM_STATIC size_t ZSTD_initLegacyStream(void** legacyContext, U32 prevVersion, U32 newVersion,
247 const void* dict, size_t dictSize)
249 if (prevVersion != newVersion) ZSTD_freeLegacyStreamContext(*legacyContext, prevVersion);
256 (void)dict; (void)dictSize;
258 #if (ZSTD_LEGACY_SUPPORT <= 4)
261 ZBUFFv04_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv04_createDCtx() : (ZBUFFv04_DCtx*)*legacyContext;
262 if (dctx==NULL) return ERROR(memory_allocation);
263 ZBUFFv04_decompressInit(dctx);
264 ZBUFFv04_decompressWithDictionary(dctx, dict, dictSize);
265 *legacyContext = dctx;
269 #if (ZSTD_LEGACY_SUPPORT <= 5)
272 ZBUFFv05_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv05_createDCtx() : (ZBUFFv05_DCtx*)*legacyContext;
273 if (dctx==NULL) return ERROR(memory_allocation);
274 ZBUFFv05_decompressInitDictionary(dctx, dict, dictSize);
275 *legacyContext = dctx;
279 #if (ZSTD_LEGACY_SUPPORT <= 6)
282 ZBUFFv06_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv06_createDCtx() : (ZBUFFv06_DCtx*)*legacyContext;
283 if (dctx==NULL) return ERROR(memory_allocation);
284 ZBUFFv06_decompressInitDictionary(dctx, dict, dictSize);
285 *legacyContext = dctx;
289 #if (ZSTD_LEGACY_SUPPORT <= 7)
292 ZBUFFv07_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv07_createDCtx() : (ZBUFFv07_DCtx*)*legacyContext;
293 if (dctx==NULL) return ERROR(memory_allocation);
294 ZBUFFv07_decompressInitDictionary(dctx, dict, dictSize);
295 *legacyContext = dctx;
304 MEM_STATIC size_t ZSTD_decompressLegacyStream(void* legacyContext, U32 version,
305 ZSTD_outBuffer* output, ZSTD_inBuffer* input)
313 (void)legacyContext; (void)output; (void)input;
314 return ERROR(version_unsupported);
315 #if (ZSTD_LEGACY_SUPPORT <= 4)
318 ZBUFFv04_DCtx* dctx = (ZBUFFv04_DCtx*) legacyContext;
319 const void* src = (const char*)input->src + input->pos;
320 size_t readSize = input->size - input->pos;
321 void* dst = (char*)output->dst + output->pos;
322 size_t decodedSize = output->size - output->pos;
323 size_t const hintSize = ZBUFFv04_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
324 output->pos += decodedSize;
325 input->pos += readSize;
329 #if (ZSTD_LEGACY_SUPPORT <= 5)
332 ZBUFFv05_DCtx* dctx = (ZBUFFv05_DCtx*) legacyContext;
333 const void* src = (const char*)input->src + input->pos;
334 size_t readSize = input->size - input->pos;
335 void* dst = (char*)output->dst + output->pos;
336 size_t decodedSize = output->size - output->pos;
337 size_t const hintSize = ZBUFFv05_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
338 output->pos += decodedSize;
339 input->pos += readSize;
343 #if (ZSTD_LEGACY_SUPPORT <= 6)
346 ZBUFFv06_DCtx* dctx = (ZBUFFv06_DCtx*) legacyContext;
347 const void* src = (const char*)input->src + input->pos;
348 size_t readSize = input->size - input->pos;
349 void* dst = (char*)output->dst + output->pos;
350 size_t decodedSize = output->size - output->pos;
351 size_t const hintSize = ZBUFFv06_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
352 output->pos += decodedSize;
353 input->pos += readSize;
357 #if (ZSTD_LEGACY_SUPPORT <= 7)
360 ZBUFFv07_DCtx* dctx = (ZBUFFv07_DCtx*) legacyContext;
361 const void* src = (const char*)input->src + input->pos;
362 size_t readSize = input->size - input->pos;
363 void* dst = (char*)output->dst + output->pos;
364 size_t decodedSize = output->size - output->pos;
365 size_t const hintSize = ZBUFFv07_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
366 output->pos += decodedSize;
367 input->pos += readSize;
375 #if defined (__cplusplus)
379 #endif /* ZSTD_LEGACY_H */