1 | /* |
---|
2 | zstd_legacy - decoder for legacy format |
---|
3 | Header File |
---|
4 | Copyright (C) 2015-2016, Yann Collet. |
---|
5 | |
---|
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) |
---|
37 | extern "C" { |
---|
38 | #endif |
---|
39 | |
---|
40 | /* ************************************* |
---|
41 | * Includes |
---|
42 | ***************************************/ |
---|
43 | #include "mem.h" /* MEM_STATIC */ |
---|
44 | #include "error_private.h" /* ERROR */ |
---|
45 | #include "zstd_v01.h" |
---|
46 | #include "zstd_v02.h" |
---|
47 | #include "zstd_v03.h" |
---|
48 | #include "zstd_v04.h" |
---|
49 | #include "zstd_v05.h" |
---|
50 | #include "zstd_v06.h" |
---|
51 | |
---|
52 | |
---|
53 | /** ZSTD_isLegacy() : |
---|
54 | @return : > 0 if supported by legacy decoder. 0 otherwise. |
---|
55 | return value is the version. |
---|
56 | */ |
---|
57 | MEM_STATIC unsigned ZSTD_isLegacy(const void* src, size_t srcSize) |
---|
58 | { |
---|
59 | U32 magicNumberLE; |
---|
60 | if (srcSize<4) return 0; |
---|
61 | magicNumberLE = MEM_readLE32(src); |
---|
62 | switch(magicNumberLE) |
---|
63 | { |
---|
64 | case ZSTDv01_magicNumberLE:return 1; |
---|
65 | case ZSTDv02_magicNumber : return 2; |
---|
66 | case ZSTDv03_magicNumber : return 3; |
---|
67 | case ZSTDv04_magicNumber : return 4; |
---|
68 | case ZSTDv05_MAGICNUMBER : return 5; |
---|
69 | case ZSTDv06_MAGICNUMBER : return 6; |
---|
70 | default : return 0; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | MEM_STATIC unsigned long long ZSTD_getDecompressedSize_legacy(const void* src, size_t srcSize) |
---|
76 | { |
---|
77 | if (srcSize < 4) return 0; |
---|
78 | |
---|
79 | { U32 const version = ZSTD_isLegacy(src, srcSize); |
---|
80 | if (version < 5) return 0; /* no decompressed size in frame header, or not a legacy format */ |
---|
81 | if (version==5) { |
---|
82 | ZSTDv05_parameters fParams; |
---|
83 | size_t const frResult = ZSTDv05_getFrameParams(&fParams, src, srcSize); |
---|
84 | if (frResult != 0) return 0; |
---|
85 | return fParams.srcSize; |
---|
86 | } |
---|
87 | if (version==6) { |
---|
88 | ZSTDv06_frameParams fParams; |
---|
89 | size_t const frResult = ZSTDv06_getFrameParams(&fParams, src, srcSize); |
---|
90 | if (frResult != 0) return 0; |
---|
91 | return fParams.frameContentSize; |
---|
92 | } |
---|
93 | return 0; /* should not be possible */ |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | MEM_STATIC size_t ZSTD_decompressLegacy( |
---|
98 | void* dst, size_t dstCapacity, |
---|
99 | const void* src, size_t compressedSize, |
---|
100 | const void* dict,size_t dictSize) |
---|
101 | { |
---|
102 | U32 const version = ZSTD_isLegacy(src, compressedSize); |
---|
103 | switch(version) |
---|
104 | { |
---|
105 | case 1 : |
---|
106 | return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize); |
---|
107 | case 2 : |
---|
108 | return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize); |
---|
109 | case 3 : |
---|
110 | return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize); |
---|
111 | case 4 : |
---|
112 | return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize); |
---|
113 | case 5 : |
---|
114 | { size_t result; |
---|
115 | ZSTDv05_DCtx* const zd = ZSTDv05_createDCtx(); |
---|
116 | if (zd==NULL) return ERROR(memory_allocation); |
---|
117 | result = ZSTDv05_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize); |
---|
118 | ZSTDv05_freeDCtx(zd); |
---|
119 | return result; |
---|
120 | } |
---|
121 | case 6 : |
---|
122 | { size_t result; |
---|
123 | ZSTDv06_DCtx* const zd = ZSTDv06_createDCtx(); |
---|
124 | if (zd==NULL) return ERROR(memory_allocation); |
---|
125 | result = ZSTDv06_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize); |
---|
126 | ZSTDv06_freeDCtx(zd); |
---|
127 | return result; |
---|
128 | } |
---|
129 | default : |
---|
130 | return ERROR(prefix_unknown); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | #if defined (__cplusplus) |
---|
137 | } |
---|
138 | #endif |
---|
139 | |
---|
140 | #endif /* ZSTD_LEGACY_H */ |
---|