Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 1 | /* |
Yann Collet | 29a2c83 | 2015-11-26 16:02:04 +0100 | [diff] [blame] | 2 | zstd_legacy - decoder for legacy format |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 3 | Header File |
Yann Collet | 18dedec | 2016-05-06 16:43:23 +0200 | [diff] [blame] | 4 | Copyright (C) 2015-2016, Yann Collet. |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 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 | ***************************************/ |
Yann Collet | ffec740 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 43 | #include "mem.h" /* MEM_STATIC */ |
| 44 | #include "error_private.h" /* ERROR */ |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 45 | #include "zstd_v01.h" |
| 46 | #include "zstd_v02.h" |
Yann Collet | 29a2c83 | 2015-11-26 16:02:04 +0100 | [diff] [blame] | 47 | #include "zstd_v03.h" |
Yann Collet | 464fa99 | 2016-02-03 01:09:46 +0100 | [diff] [blame] | 48 | #include "zstd_v04.h" |
Yann Collet | 029267a | 2016-04-09 09:42:27 +0200 | [diff] [blame] | 49 | #include "zstd_v05.h" |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 50 | #include "zstd_v06.h" |
inikep | fca90f8 | 2016-07-25 17:49:08 +0200 | [diff] [blame^] | 51 | #include "zstd_v07.h" |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 52 | |
Yann Collet | 18dedec | 2016-05-06 16:43:23 +0200 | [diff] [blame] | 53 | |
| 54 | /** ZSTD_isLegacy() : |
| 55 | @return : > 0 if supported by legacy decoder. 0 otherwise. |
| 56 | return value is the version. |
| 57 | */ |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 58 | MEM_STATIC unsigned ZSTD_isLegacy(const void* src, size_t srcSize) |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 59 | { |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 60 | U32 magicNumberLE; |
| 61 | if (srcSize<4) return 0; |
| 62 | magicNumberLE = MEM_readLE32(src); |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 63 | 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; |
inikep | fca90f8 | 2016-07-25 17:49:08 +0200 | [diff] [blame^] | 71 | case ZSTDv07_MAGICNUMBER : return 7; |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 72 | default : return 0; |
| 73 | } |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 77 | MEM_STATIC unsigned long long ZSTD_getDecompressedSize_legacy(const void* src, size_t srcSize) |
| 78 | { |
| 79 | if (srcSize < 4) return 0; |
| 80 | |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 81 | { 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 Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 83 | 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 | } |
inikep | fca90f8 | 2016-07-25 17:49:08 +0200 | [diff] [blame^] | 95 | 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 Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 101 | return 0; /* should not be possible */ |
| 102 | } |
| 103 | } |
| 104 | |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 105 | MEM_STATIC size_t ZSTD_decompressLegacy( |
Yann Collet | 029267a | 2016-04-09 09:42:27 +0200 | [diff] [blame] | 106 | void* dst, size_t dstCapacity, |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 107 | const void* src, size_t compressedSize, |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 108 | const void* dict,size_t dictSize) |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 109 | { |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 110 | U32 const version = ZSTD_isLegacy(src, compressedSize); |
| 111 | switch(version) |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 112 | { |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 113 | case 1 : |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 114 | return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize); |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 115 | case 2 : |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 116 | return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize); |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 117 | case 3 : |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 118 | return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize); |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 119 | case 4 : |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 120 | return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize); |
Yann Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 121 | case 5 : |
Yann Collet | 289bbd5 | 2016-06-11 00:23:43 +0200 | [diff] [blame] | 122 | { size_t result; |
| 123 | ZSTDv05_DCtx* const zd = ZSTDv05_createDCtx(); |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 124 | 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 Collet | 19c27d2 | 2016-07-07 14:40:13 +0200 | [diff] [blame] | 129 | case 6 : |
Yann Collet | 289bbd5 | 2016-06-11 00:23:43 +0200 | [diff] [blame] | 130 | { size_t result; |
| 131 | ZSTDv06_DCtx* const zd = ZSTDv06_createDCtx(); |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 132 | 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 | } |
inikep | fca90f8 | 2016-07-25 17:49:08 +0200 | [diff] [blame^] | 137 | 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 | } |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 145 | default : |
| 146 | return ERROR(prefix_unknown); |
| 147 | } |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | #if defined (__cplusplus) |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | #endif /* ZSTD_LEGACY_H */ |