blob: ab9634b3247c35faccc62f5ebe026bcfd3745485 [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 Collet19c27d22016-07-07 14:40:13 +020057MEM_STATIC unsigned ZSTD_isLegacy(const void* src, size_t srcSize)
Yann Colletaa074052015-10-30 11:21:50 +010058{
Yann Collet19c27d22016-07-07 14:40:13 +020059 U32 magicNumberLE;
60 if (srcSize<4) return 0;
61 magicNumberLE = MEM_readLE32(src);
inikepbf853d52016-06-09 17:59:18 +020062 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 }
Yann Colletaa074052015-10-30 11:21:50 +010072}
73
74
Yann Colletf323bf72016-07-07 13:14:21 +020075MEM_STATIC unsigned long long ZSTD_getDecompressedSize_legacy(const void* src, size_t srcSize)
76{
77 if (srcSize < 4) return 0;
78
Yann Collet19c27d22016-07-07 14:40:13 +020079 { U32 const version = ZSTD_isLegacy(src, srcSize);
80 if (version < 5) return 0; /* no decompressed size in frame header, or not a legacy format */
Yann Colletf323bf72016-07-07 13:14:21 +020081 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
Yann Colletaa074052015-10-30 11:21:50 +010097MEM_STATIC size_t ZSTD_decompressLegacy(
Yann Collet029267a2016-04-09 09:42:27 +020098 void* dst, size_t dstCapacity,
Yann Colletaa074052015-10-30 11:21:50 +010099 const void* src, size_t compressedSize,
Yann Collet19c27d22016-07-07 14:40:13 +0200100 const void* dict,size_t dictSize)
Yann Colletaa074052015-10-30 11:21:50 +0100101{
Yann Collet19c27d22016-07-07 14:40:13 +0200102 U32 const version = ZSTD_isLegacy(src, compressedSize);
103 switch(version)
inikepbf853d52016-06-09 17:59:18 +0200104 {
Yann Collet19c27d22016-07-07 14:40:13 +0200105 case 1 :
inikepbf853d52016-06-09 17:59:18 +0200106 return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize);
Yann Collet19c27d22016-07-07 14:40:13 +0200107 case 2 :
inikepbf853d52016-06-09 17:59:18 +0200108 return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize);
Yann Collet19c27d22016-07-07 14:40:13 +0200109 case 3 :
inikepbf853d52016-06-09 17:59:18 +0200110 return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize);
Yann Collet19c27d22016-07-07 14:40:13 +0200111 case 4 :
inikepbf853d52016-06-09 17:59:18 +0200112 return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize);
Yann Collet19c27d22016-07-07 14:40:13 +0200113 case 5 :
Yann Collet289bbd52016-06-11 00:23:43 +0200114 { size_t result;
115 ZSTDv05_DCtx* const zd = ZSTDv05_createDCtx();
inikepbf853d52016-06-09 17:59:18 +0200116 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 }
Yann Collet19c27d22016-07-07 14:40:13 +0200121 case 6 :
Yann Collet289bbd52016-06-11 00:23:43 +0200122 { size_t result;
123 ZSTDv06_DCtx* const zd = ZSTDv06_createDCtx();
inikepbf853d52016-06-09 17:59:18 +0200124 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 }
Yann Colletaa074052015-10-30 11:21:50 +0100132}
133
134
135
136#if defined (__cplusplus)
137}
138#endif
139
140#endif /* ZSTD_LEGACY_H */