blob: 56b3e82150b58297dedda5e34c18ad5f8835e6db [file] [log] [blame]
Yann Colletaa074052015-10-30 11:21:50 +01001/*
Yann Collet29a2c832015-11-26 16:02:04 +01002 zstd_legacy - decoder for legacy format
Yann Colletaa074052015-10-30 11:21:50 +01003 Header File
Yann Collet18dedec2016-05-06 16:43:23 +02004 Copyright (C) 2015-2016, Yann Collet.
Yann Colletaa074052015-10-30 11:21:50 +01005
6 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are
10 met:
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following disclaimer
15 in the documentation and/or other materials provided with the
16 distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 You can contact the author at :
30 - zstd source repository : https://github.com/Cyan4973/zstd
31 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
32*/
33#ifndef ZSTD_LEGACY_H
34#define ZSTD_LEGACY_H
35
36#if defined (__cplusplus)
37extern "C" {
38#endif
39
40/* *************************************
41* Includes
42***************************************/
Yann Colletffec7402016-01-21 15:50:11 +010043#include "mem.h" /* MEM_STATIC */
44#include "error_private.h" /* ERROR */
Yann Colletaa074052015-10-30 11:21:50 +010045#include "zstd_v01.h"
46#include "zstd_v02.h"
Yann Collet29a2c832015-11-26 16:02:04 +010047#include "zstd_v03.h"
Yann Collet464fa992016-02-03 01:09:46 +010048#include "zstd_v04.h"
Yann Collet029267a2016-04-09 09:42:27 +020049#include "zstd_v05.h"
inikepbf853d52016-06-09 17:59:18 +020050#include "zstd_v06.h"
Yann Colletaa074052015-10-30 11:21:50 +010051
Yann Collet18dedec2016-05-06 16:43:23 +020052
53/** ZSTD_isLegacy() :
54 @return : > 0 if supported by legacy decoder. 0 otherwise.
55 return value is the version.
56*/
Yann Colletaa074052015-10-30 11:21:50 +010057MEM_STATIC unsigned ZSTD_isLegacy (U32 magicNumberLE)
58{
inikepbf853d52016-06-09 17:59:18 +020059 switch(magicNumberLE)
60 {
61 case ZSTDv01_magicNumberLE:return 1;
62 case ZSTDv02_magicNumber : return 2;
63 case ZSTDv03_magicNumber : return 3;
64 case ZSTDv04_magicNumber : return 4;
65 case ZSTDv05_MAGICNUMBER : return 5;
66 case ZSTDv06_MAGICNUMBER : return 6;
67 default : return 0;
68 }
Yann Colletaa074052015-10-30 11:21:50 +010069}
70
71
Yann Colletf323bf72016-07-07 13:14:21 +020072MEM_STATIC unsigned long long ZSTD_getDecompressedSize_legacy(const void* src, size_t srcSize)
73{
74 if (srcSize < 4) return 0;
75
76 { U32 const magic = MEM_readLE32(src);
77 U32 const version = ZSTD_isLegacy(magic);
78 if (!version) return 0; /* not a supported legacy format */
79 if (version < 5) return 0; /* no decompressed size in frame header */
80 if (version==5) {
81 ZSTDv05_parameters fParams;
82 size_t const frResult = ZSTDv05_getFrameParams(&fParams, src, srcSize);
83 if (frResult != 0) return 0;
84 return fParams.srcSize;
85 }
86 if (version==6) {
87 ZSTDv06_frameParams fParams;
88 size_t const frResult = ZSTDv06_getFrameParams(&fParams, src, srcSize);
89 if (frResult != 0) return 0;
90 return fParams.frameContentSize;
91 }
92 return 0; /* should not be possible */
93 }
94}
95
Yann Colletaa074052015-10-30 11:21:50 +010096MEM_STATIC size_t ZSTD_decompressLegacy(
Yann Collet029267a2016-04-09 09:42:27 +020097 void* dst, size_t dstCapacity,
Yann Colletaa074052015-10-30 11:21:50 +010098 const void* src, size_t compressedSize,
Yann Collet18dedec2016-05-06 16:43:23 +020099 const void* dict,size_t dictSize,
Yann Colletaa074052015-10-30 11:21:50 +0100100 U32 magicNumberLE)
101{
inikepbf853d52016-06-09 17:59:18 +0200102 switch(magicNumberLE)
103 {
104 case ZSTDv01_magicNumberLE :
105 return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize);
106 case ZSTDv02_magicNumber :
107 return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize);
108 case ZSTDv03_magicNumber :
109 return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize);
110 case ZSTDv04_magicNumber :
111 return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize);
112 case ZSTDv05_MAGICNUMBER :
Yann Collet289bbd52016-06-11 00:23:43 +0200113 { size_t result;
114 ZSTDv05_DCtx* const zd = ZSTDv05_createDCtx();
inikepbf853d52016-06-09 17:59:18 +0200115 if (zd==NULL) return ERROR(memory_allocation);
116 result = ZSTDv05_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
117 ZSTDv05_freeDCtx(zd);
118 return result;
119 }
120 case ZSTDv06_MAGICNUMBER :
Yann Collet289bbd52016-06-11 00:23:43 +0200121 { size_t result;
122 ZSTDv06_DCtx* const zd = ZSTDv06_createDCtx();
inikepbf853d52016-06-09 17:59:18 +0200123 if (zd==NULL) return ERROR(memory_allocation);
124 result = ZSTDv06_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
125 ZSTDv06_freeDCtx(zd);
126 return result;
127 }
128 default :
129 return ERROR(prefix_unknown);
130 }
Yann Colletaa074052015-10-30 11:21:50 +0100131}
132
133
134
135#if defined (__cplusplus)
136}
137#endif
138
139#endif /* ZSTD_LEGACY_H */