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" |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 51 | |
Yann Collet | 18dedec | 2016-05-06 16:43:23 +0200 | [diff] [blame] | 52 | |
| 53 | /** ZSTD_isLegacy() : |
| 54 | @return : > 0 if supported by legacy decoder. 0 otherwise. |
| 55 | return value is the version. |
| 56 | */ |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 57 | MEM_STATIC unsigned ZSTD_isLegacy (U32 magicNumberLE) |
| 58 | { |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 59 | 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 Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame^] | 72 | MEM_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 Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 96 | MEM_STATIC size_t ZSTD_decompressLegacy( |
Yann Collet | 029267a | 2016-04-09 09:42:27 +0200 | [diff] [blame] | 97 | void* dst, size_t dstCapacity, |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 98 | const void* src, size_t compressedSize, |
Yann Collet | 18dedec | 2016-05-06 16:43:23 +0200 | [diff] [blame] | 99 | const void* dict,size_t dictSize, |
Yann Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 100 | U32 magicNumberLE) |
| 101 | { |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 102 | 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 Collet | 289bbd5 | 2016-06-11 00:23:43 +0200 | [diff] [blame] | 113 | { size_t result; |
| 114 | ZSTDv05_DCtx* const zd = ZSTDv05_createDCtx(); |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 115 | 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 Collet | 289bbd5 | 2016-06-11 00:23:43 +0200 | [diff] [blame] | 121 | { size_t result; |
| 122 | ZSTDv06_DCtx* const zd = ZSTDv06_createDCtx(); |
inikep | bf853d5 | 2016-06-09 17:59:18 +0200 | [diff] [blame] | 123 | 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 Collet | aa07405 | 2015-10-30 11:21:50 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | #if defined (__cplusplus) |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | #endif /* ZSTD_LEGACY_H */ |