blob: 6c2a1019b52bf7f3ab46b987f0cc7ba44ccf504d [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"
inikepfca90f82016-07-25 17:49:08 +020051#include "zstd_v07.h"
Yann Colletaa074052015-10-30 11:21:50 +010052
Yann Collet18dedec2016-05-06 16:43:23 +020053
54/** ZSTD_isLegacy() :
55 @return : > 0 if supported by legacy decoder. 0 otherwise.
56 return value is the version.
57*/
Yann Collet19c27d22016-07-07 14:40:13 +020058MEM_STATIC unsigned ZSTD_isLegacy(const void* src, size_t srcSize)
Yann Colletaa074052015-10-30 11:21:50 +010059{
Yann Collet19c27d22016-07-07 14:40:13 +020060 U32 magicNumberLE;
61 if (srcSize<4) return 0;
62 magicNumberLE = MEM_readLE32(src);
inikepbf853d52016-06-09 17:59:18 +020063 switch(magicNumberLE)
64 {
65 case ZSTDv01_magicNumberLE:return 1;
66 case ZSTDv02_magicNumber : return 2;
67 case ZSTDv03_magicNumber : return 3;
68 case ZSTDv04_magicNumber : return 4;
69 case ZSTDv05_MAGICNUMBER : return 5;
70 case ZSTDv06_MAGICNUMBER : return 6;
inikepfca90f82016-07-25 17:49:08 +020071 case ZSTDv07_MAGICNUMBER : return 7;
inikepbf853d52016-06-09 17:59:18 +020072 default : return 0;
73 }
Yann Colletaa074052015-10-30 11:21:50 +010074}
75
76
Yann Colletf323bf72016-07-07 13:14:21 +020077MEM_STATIC unsigned long long ZSTD_getDecompressedSize_legacy(const void* src, size_t srcSize)
78{
79 if (srcSize < 4) return 0;
80
Yann Collet19c27d22016-07-07 14:40:13 +020081 { U32 const version = ZSTD_isLegacy(src, srcSize);
82 if (version < 5) return 0; /* no decompressed size in frame header, or not a legacy format */
Yann Colletf323bf72016-07-07 13:14:21 +020083 if (version==5) {
84 ZSTDv05_parameters fParams;
85 size_t const frResult = ZSTDv05_getFrameParams(&fParams, src, srcSize);
86 if (frResult != 0) return 0;
87 return fParams.srcSize;
88 }
89 if (version==6) {
90 ZSTDv06_frameParams fParams;
91 size_t const frResult = ZSTDv06_getFrameParams(&fParams, src, srcSize);
92 if (frResult != 0) return 0;
93 return fParams.frameContentSize;
94 }
inikepfca90f82016-07-25 17:49:08 +020095 if (version==7) {
96 ZSTDv07_frameParams fParams;
97 size_t const frResult = ZSTDv07_getFrameParams(&fParams, src, srcSize);
98 if (frResult != 0) return 0;
99 return fParams.frameContentSize;
100 }
Yann Colletf323bf72016-07-07 13:14:21 +0200101 return 0; /* should not be possible */
102 }
103}
104
Yann Colletaa074052015-10-30 11:21:50 +0100105MEM_STATIC size_t ZSTD_decompressLegacy(
Yann Collet029267a2016-04-09 09:42:27 +0200106 void* dst, size_t dstCapacity,
Yann Colletaa074052015-10-30 11:21:50 +0100107 const void* src, size_t compressedSize,
Yann Collet19c27d22016-07-07 14:40:13 +0200108 const void* dict,size_t dictSize)
Yann Colletaa074052015-10-30 11:21:50 +0100109{
Yann Collet19c27d22016-07-07 14:40:13 +0200110 U32 const version = ZSTD_isLegacy(src, compressedSize);
111 switch(version)
inikepbf853d52016-06-09 17:59:18 +0200112 {
Yann Collet19c27d22016-07-07 14:40:13 +0200113 case 1 :
inikepbf853d52016-06-09 17:59:18 +0200114 return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize);
Yann Collet19c27d22016-07-07 14:40:13 +0200115 case 2 :
inikepbf853d52016-06-09 17:59:18 +0200116 return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize);
Yann Collet19c27d22016-07-07 14:40:13 +0200117 case 3 :
inikepbf853d52016-06-09 17:59:18 +0200118 return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize);
Yann Collet19c27d22016-07-07 14:40:13 +0200119 case 4 :
inikepbf853d52016-06-09 17:59:18 +0200120 return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize);
Yann Collet19c27d22016-07-07 14:40:13 +0200121 case 5 :
Yann Collet289bbd52016-06-11 00:23:43 +0200122 { size_t result;
123 ZSTDv05_DCtx* const zd = ZSTDv05_createDCtx();
inikepbf853d52016-06-09 17:59:18 +0200124 if (zd==NULL) return ERROR(memory_allocation);
125 result = ZSTDv05_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
126 ZSTDv05_freeDCtx(zd);
127 return result;
128 }
Yann Collet19c27d22016-07-07 14:40:13 +0200129 case 6 :
Yann Collet289bbd52016-06-11 00:23:43 +0200130 { size_t result;
131 ZSTDv06_DCtx* const zd = ZSTDv06_createDCtx();
inikepbf853d52016-06-09 17:59:18 +0200132 if (zd==NULL) return ERROR(memory_allocation);
133 result = ZSTDv06_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
134 ZSTDv06_freeDCtx(zd);
135 return result;
136 }
inikepfca90f82016-07-25 17:49:08 +0200137 case 7 :
138 { size_t result;
139 ZSTDv07_DCtx* const zd = ZSTDv07_createDCtx();
140 if (zd==NULL) return ERROR(memory_allocation);
141 result = ZSTDv07_decompress_usingDict(zd, dst, dstCapacity, src, compressedSize, dict, dictSize);
142 ZSTDv07_freeDCtx(zd);
143 return result;
144 }
inikepbf853d52016-06-09 17:59:18 +0200145 default :
146 return ERROR(prefix_unknown);
147 }
Yann Colletaa074052015-10-30 11:21:50 +0100148}
149
150
151
152#if defined (__cplusplus)
153}
154#endif
155
156#endif /* ZSTD_LEGACY_H */