blob: e5b383ee4c0ce9c5c55edfb0ed4a2d597a1d5dba [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -04001/*
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
9 */
10
11#ifndef ZSTD_LEGACY_H
12#define ZSTD_LEGACY_H
13
14#if defined (__cplusplus)
15extern "C" {
16#endif
17
18/* *************************************
19* Includes
20***************************************/
21#include "mem.h" /* MEM_STATIC */
22#include "error_private.h" /* ERROR */
Abhilash S.L3b494632019-07-16 15:51:09 +053023#include "zstd_internal.h" /* ZSTD_inBuffer, ZSTD_outBuffer, ZSTD_frameSizeInfo */
William Kurkianea869482019-04-09 15:16:11 -040024
25#if !defined (ZSTD_LEGACY_SUPPORT) || (ZSTD_LEGACY_SUPPORT == 0)
26# undef ZSTD_LEGACY_SUPPORT
27# define ZSTD_LEGACY_SUPPORT 8
28#endif
29
30#if (ZSTD_LEGACY_SUPPORT <= 1)
31# include "zstd_v01.h"
32#endif
33#if (ZSTD_LEGACY_SUPPORT <= 2)
34# include "zstd_v02.h"
35#endif
36#if (ZSTD_LEGACY_SUPPORT <= 3)
37# include "zstd_v03.h"
38#endif
39#if (ZSTD_LEGACY_SUPPORT <= 4)
40# include "zstd_v04.h"
41#endif
42#if (ZSTD_LEGACY_SUPPORT <= 5)
43# include "zstd_v05.h"
44#endif
45#if (ZSTD_LEGACY_SUPPORT <= 6)
46# include "zstd_v06.h"
47#endif
48#if (ZSTD_LEGACY_SUPPORT <= 7)
49# include "zstd_v07.h"
50#endif
51
52/** ZSTD_isLegacy() :
53 @return : > 0 if supported by legacy decoder. 0 otherwise.
54 return value is the version.
55*/
56MEM_STATIC unsigned ZSTD_isLegacy(const void* src, size_t srcSize)
57{
58 U32 magicNumberLE;
59 if (srcSize<4) return 0;
60 magicNumberLE = MEM_readLE32(src);
61 switch(magicNumberLE)
62 {
63#if (ZSTD_LEGACY_SUPPORT <= 1)
64 case ZSTDv01_magicNumberLE:return 1;
65#endif
66#if (ZSTD_LEGACY_SUPPORT <= 2)
67 case ZSTDv02_magicNumber : return 2;
68#endif
69#if (ZSTD_LEGACY_SUPPORT <= 3)
70 case ZSTDv03_magicNumber : return 3;
71#endif
72#if (ZSTD_LEGACY_SUPPORT <= 4)
73 case ZSTDv04_magicNumber : return 4;
74#endif
75#if (ZSTD_LEGACY_SUPPORT <= 5)
76 case ZSTDv05_MAGICNUMBER : return 5;
77#endif
78#if (ZSTD_LEGACY_SUPPORT <= 6)
79 case ZSTDv06_MAGICNUMBER : return 6;
80#endif
81#if (ZSTD_LEGACY_SUPPORT <= 7)
82 case ZSTDv07_MAGICNUMBER : return 7;
83#endif
84 default : return 0;
85 }
86}
87
88
89MEM_STATIC unsigned long long ZSTD_getDecompressedSize_legacy(const void* src, size_t srcSize)
90{
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)
94 if (version==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;
99 }
100#endif
101#if (ZSTD_LEGACY_SUPPORT <= 6)
102 if (version==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;
107 }
108#endif
109#if (ZSTD_LEGACY_SUPPORT <= 7)
110 if (version==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;
115 }
116#endif
117 return 0; /* should not be possible */
118}
119
120
121MEM_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)
125{
126 U32 const version = ZSTD_isLegacy(src, compressedSize);
127 (void)dst; (void)dstCapacity; (void)dict; (void)dictSize; /* unused when ZSTD_LEGACY_SUPPORT >= 8 */
128 switch(version)
129 {
130#if (ZSTD_LEGACY_SUPPORT <= 1)
131 case 1 :
132 return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize);
133#endif
134#if (ZSTD_LEGACY_SUPPORT <= 2)
135 case 2 :
136 return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize);
137#endif
138#if (ZSTD_LEGACY_SUPPORT <= 3)
139 case 3 :
140 return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize);
141#endif
142#if (ZSTD_LEGACY_SUPPORT <= 4)
143 case 4 :
144 return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize);
145#endif
146#if (ZSTD_LEGACY_SUPPORT <= 5)
147 case 5 :
148 { size_t result;
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);
153 return result;
154 }
155#endif
156#if (ZSTD_LEGACY_SUPPORT <= 6)
157 case 6 :
158 { size_t result;
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);
163 return result;
164 }
165#endif
166#if (ZSTD_LEGACY_SUPPORT <= 7)
167 case 7 :
168 { size_t result;
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);
173 return result;
174 }
175#endif
176 default :
177 return ERROR(prefix_unknown);
178 }
179}
180
Abhilash S.L3b494632019-07-16 15:51:09 +0530181MEM_STATIC ZSTD_frameSizeInfo ZSTD_findFrameSizeInfoLegacy(const void *src, size_t srcSize)
William Kurkianea869482019-04-09 15:16:11 -0400182{
Abhilash S.L3b494632019-07-16 15:51:09 +0530183 ZSTD_frameSizeInfo frameSizeInfo;
184 U32 const version = ZSTD_isLegacy(src, srcSize);
William Kurkianea869482019-04-09 15:16:11 -0400185 switch(version)
186 {
187#if (ZSTD_LEGACY_SUPPORT <= 1)
188 case 1 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530189 ZSTDv01_findFrameSizeInfoLegacy(src, srcSize,
190 &frameSizeInfo.compressedSize,
191 &frameSizeInfo.decompressedBound);
192 break;
William Kurkianea869482019-04-09 15:16:11 -0400193#endif
194#if (ZSTD_LEGACY_SUPPORT <= 2)
195 case 2 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530196 ZSTDv02_findFrameSizeInfoLegacy(src, srcSize,
197 &frameSizeInfo.compressedSize,
198 &frameSizeInfo.decompressedBound);
199 break;
William Kurkianea869482019-04-09 15:16:11 -0400200#endif
201#if (ZSTD_LEGACY_SUPPORT <= 3)
202 case 3 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530203 ZSTDv03_findFrameSizeInfoLegacy(src, srcSize,
204 &frameSizeInfo.compressedSize,
205 &frameSizeInfo.decompressedBound);
206 break;
William Kurkianea869482019-04-09 15:16:11 -0400207#endif
208#if (ZSTD_LEGACY_SUPPORT <= 4)
209 case 4 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530210 ZSTDv04_findFrameSizeInfoLegacy(src, srcSize,
211 &frameSizeInfo.compressedSize,
212 &frameSizeInfo.decompressedBound);
213 break;
William Kurkianea869482019-04-09 15:16:11 -0400214#endif
215#if (ZSTD_LEGACY_SUPPORT <= 5)
216 case 5 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530217 ZSTDv05_findFrameSizeInfoLegacy(src, srcSize,
218 &frameSizeInfo.compressedSize,
219 &frameSizeInfo.decompressedBound);
220 break;
William Kurkianea869482019-04-09 15:16:11 -0400221#endif
222#if (ZSTD_LEGACY_SUPPORT <= 6)
223 case 6 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530224 ZSTDv06_findFrameSizeInfoLegacy(src, srcSize,
225 &frameSizeInfo.compressedSize,
226 &frameSizeInfo.decompressedBound);
227 break;
William Kurkianea869482019-04-09 15:16:11 -0400228#endif
229#if (ZSTD_LEGACY_SUPPORT <= 7)
230 case 7 :
Abhilash S.L3b494632019-07-16 15:51:09 +0530231 ZSTDv07_findFrameSizeInfoLegacy(src, srcSize,
232 &frameSizeInfo.compressedSize,
233 &frameSizeInfo.decompressedBound);
234 break;
William Kurkianea869482019-04-09 15:16:11 -0400235#endif
236 default :
Abhilash S.L3b494632019-07-16 15:51:09 +0530237 frameSizeInfo.compressedSize = ERROR(prefix_unknown);
238 frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
239 break;
William Kurkianea869482019-04-09 15:16:11 -0400240 }
Abhilash S.L3b494632019-07-16 15:51:09 +0530241 return frameSizeInfo;
242}
243
244MEM_STATIC size_t ZSTD_findFrameCompressedSizeLegacy(const void *src, size_t srcSize)
245{
246 ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfoLegacy(src, srcSize);
247 return frameSizeInfo.compressedSize;
William Kurkianea869482019-04-09 15:16:11 -0400248}
249
250MEM_STATIC size_t ZSTD_freeLegacyStreamContext(void* legacyContext, U32 version)
251{
252 switch(version)
253 {
254 default :
255 case 1 :
256 case 2 :
257 case 3 :
258 (void)legacyContext;
259 return ERROR(version_unsupported);
260#if (ZSTD_LEGACY_SUPPORT <= 4)
261 case 4 : return ZBUFFv04_freeDCtx((ZBUFFv04_DCtx*)legacyContext);
262#endif
263#if (ZSTD_LEGACY_SUPPORT <= 5)
264 case 5 : return ZBUFFv05_freeDCtx((ZBUFFv05_DCtx*)legacyContext);
265#endif
266#if (ZSTD_LEGACY_SUPPORT <= 6)
267 case 6 : return ZBUFFv06_freeDCtx((ZBUFFv06_DCtx*)legacyContext);
268#endif
269#if (ZSTD_LEGACY_SUPPORT <= 7)
270 case 7 : return ZBUFFv07_freeDCtx((ZBUFFv07_DCtx*)legacyContext);
271#endif
272 }
273}
274
275
276MEM_STATIC size_t ZSTD_initLegacyStream(void** legacyContext, U32 prevVersion, U32 newVersion,
277 const void* dict, size_t dictSize)
278{
279 DEBUGLOG(5, "ZSTD_initLegacyStream for v0.%u", newVersion);
280 if (prevVersion != newVersion) ZSTD_freeLegacyStreamContext(*legacyContext, prevVersion);
281 switch(newVersion)
282 {
283 default :
284 case 1 :
285 case 2 :
286 case 3 :
287 (void)dict; (void)dictSize;
288 return 0;
289#if (ZSTD_LEGACY_SUPPORT <= 4)
290 case 4 :
291 {
292 ZBUFFv04_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv04_createDCtx() : (ZBUFFv04_DCtx*)*legacyContext;
293 if (dctx==NULL) return ERROR(memory_allocation);
294 ZBUFFv04_decompressInit(dctx);
295 ZBUFFv04_decompressWithDictionary(dctx, dict, dictSize);
296 *legacyContext = dctx;
297 return 0;
298 }
299#endif
300#if (ZSTD_LEGACY_SUPPORT <= 5)
301 case 5 :
302 {
303 ZBUFFv05_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv05_createDCtx() : (ZBUFFv05_DCtx*)*legacyContext;
304 if (dctx==NULL) return ERROR(memory_allocation);
305 ZBUFFv05_decompressInitDictionary(dctx, dict, dictSize);
306 *legacyContext = dctx;
307 return 0;
308 }
309#endif
310#if (ZSTD_LEGACY_SUPPORT <= 6)
311 case 6 :
312 {
313 ZBUFFv06_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv06_createDCtx() : (ZBUFFv06_DCtx*)*legacyContext;
314 if (dctx==NULL) return ERROR(memory_allocation);
315 ZBUFFv06_decompressInitDictionary(dctx, dict, dictSize);
316 *legacyContext = dctx;
317 return 0;
318 }
319#endif
320#if (ZSTD_LEGACY_SUPPORT <= 7)
321 case 7 :
322 {
323 ZBUFFv07_DCtx* dctx = (prevVersion != newVersion) ? ZBUFFv07_createDCtx() : (ZBUFFv07_DCtx*)*legacyContext;
324 if (dctx==NULL) return ERROR(memory_allocation);
325 ZBUFFv07_decompressInitDictionary(dctx, dict, dictSize);
326 *legacyContext = dctx;
327 return 0;
328 }
329#endif
330 }
331}
332
333
334
335MEM_STATIC size_t ZSTD_decompressLegacyStream(void* legacyContext, U32 version,
336 ZSTD_outBuffer* output, ZSTD_inBuffer* input)
337{
338 DEBUGLOG(5, "ZSTD_decompressLegacyStream for v0.%u", version);
339 switch(version)
340 {
341 default :
342 case 1 :
343 case 2 :
344 case 3 :
345 (void)legacyContext; (void)output; (void)input;
346 return ERROR(version_unsupported);
347#if (ZSTD_LEGACY_SUPPORT <= 4)
348 case 4 :
349 {
350 ZBUFFv04_DCtx* dctx = (ZBUFFv04_DCtx*) legacyContext;
351 const void* src = (const char*)input->src + input->pos;
352 size_t readSize = input->size - input->pos;
353 void* dst = (char*)output->dst + output->pos;
354 size_t decodedSize = output->size - output->pos;
355 size_t const hintSize = ZBUFFv04_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
356 output->pos += decodedSize;
357 input->pos += readSize;
358 return hintSize;
359 }
360#endif
361#if (ZSTD_LEGACY_SUPPORT <= 5)
362 case 5 :
363 {
364 ZBUFFv05_DCtx* dctx = (ZBUFFv05_DCtx*) legacyContext;
365 const void* src = (const char*)input->src + input->pos;
366 size_t readSize = input->size - input->pos;
367 void* dst = (char*)output->dst + output->pos;
368 size_t decodedSize = output->size - output->pos;
369 size_t const hintSize = ZBUFFv05_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
370 output->pos += decodedSize;
371 input->pos += readSize;
372 return hintSize;
373 }
374#endif
375#if (ZSTD_LEGACY_SUPPORT <= 6)
376 case 6 :
377 {
378 ZBUFFv06_DCtx* dctx = (ZBUFFv06_DCtx*) legacyContext;
379 const void* src = (const char*)input->src + input->pos;
380 size_t readSize = input->size - input->pos;
381 void* dst = (char*)output->dst + output->pos;
382 size_t decodedSize = output->size - output->pos;
383 size_t const hintSize = ZBUFFv06_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
384 output->pos += decodedSize;
385 input->pos += readSize;
386 return hintSize;
387 }
388#endif
389#if (ZSTD_LEGACY_SUPPORT <= 7)
390 case 7 :
391 {
392 ZBUFFv07_DCtx* dctx = (ZBUFFv07_DCtx*) legacyContext;
393 const void* src = (const char*)input->src + input->pos;
394 size_t readSize = input->size - input->pos;
395 void* dst = (char*)output->dst + output->pos;
396 size_t decodedSize = output->size - output->pos;
397 size_t const hintSize = ZBUFFv07_decompressContinue(dctx, dst, &decodedSize, src, &readSize);
398 output->pos += decodedSize;
399 input->pos += readSize;
400 return hintSize;
401 }
402#endif
403 }
404}
405
406
407#if defined (__cplusplus)
408}
409#endif
410
411#endif /* ZSTD_LEGACY_H */