| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | zstd - standard compression library |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 3 | Copyright (C) 2014-2016, Yann Collet. |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 4 | |
| 5 | BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) |
| 6 | |
| 7 | Redistribution and use in source and binary forms, with or without |
| 8 | modification, are permitted provided that the following conditions are |
| 9 | met: |
| 10 | * Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
| 12 | * Redistributions in binary form must reproduce the above |
| 13 | copyright notice, this list of conditions and the following disclaimer |
| 14 | in the documentation and/or other materials provided with the |
| 15 | distribution. |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | You can contact the author at : |
| 29 | - zstd source repository : https://github.com/Cyan4973/zstd |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | /* *************************************************************** |
| 33 | * Tuning parameters |
| 34 | *****************************************************************/ |
| 35 | /*! |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 36 | * HEAPMODE : |
| Yann Collet | 3a3b72f | 2016-01-11 12:56:11 +0100 | [diff] [blame] | 37 | * Select how default decompression function ZSTD_decompress() will allocate memory, |
| 38 | * in memory stack (0), or in memory heap (1, requires malloc()) |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 39 | */ |
| 40 | #ifndef ZSTD_HEAPMODE |
| 41 | # define ZSTD_HEAPMODE 1 |
| Yann Collet | 3a3b72f | 2016-01-11 12:56:11 +0100 | [diff] [blame] | 42 | #endif |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 43 | |
| 44 | /*! |
| 45 | * LEGACY_SUPPORT : |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 46 | * if set to 1, ZSTD_decompress() can decode older formats (v0.1+) |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 47 | */ |
| 48 | #ifndef ZSTD_LEGACY_SUPPORT |
| Yann Collet | fba6aed | 2016-01-18 12:03:27 +0100 | [diff] [blame] | 49 | # define ZSTD_LEGACY_SUPPORT 0 |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 50 | #endif |
| 51 | |
| 52 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 53 | /*-******************************************************* |
| Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 54 | * Dependencies |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 55 | *********************************************************/ |
| 56 | #include <stdlib.h> /* calloc */ |
| 57 | #include <string.h> /* memcpy, memmove */ |
| Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 58 | #include <stdio.h> /* debug only : printf */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 59 | #include "mem.h" /* low level memory routines */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 60 | #include "zstd_internal.h" |
| 61 | #include "fse_static.h" |
| Yann Collet | afe0709 | 2016-01-25 04:10:46 +0100 | [diff] [blame] | 62 | #include "huff0_static.h" |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 63 | |
| 64 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1) |
| 65 | # include "zstd_legacy.h" |
| 66 | #endif |
| 67 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 68 | |
| 69 | /*-******************************************************* |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 70 | * Compiler specifics |
| 71 | *********************************************************/ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 72 | #ifdef _MSC_VER /* Visual Studio */ |
| 73 | # define FORCE_INLINE static __forceinline |
| 74 | # include <intrin.h> /* For Visual 2005 */ |
| 75 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 76 | # pragma warning(disable : 4324) /* disable: C4324: padded structure */ |
| 77 | #else |
| 78 | # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) |
| 79 | # ifdef __GNUC__ |
| 80 | # define FORCE_INLINE static inline __attribute__((always_inline)) |
| 81 | # else |
| 82 | # define FORCE_INLINE static inline |
| 83 | # endif |
| 84 | #endif |
| 85 | |
| 86 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 87 | /*-************************************* |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 88 | * Local types |
| 89 | ***************************************/ |
| 90 | typedef struct |
| 91 | { |
| 92 | blockType_t blockType; |
| 93 | U32 origSize; |
| 94 | } blockProperties_t; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 95 | |
| 96 | |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 97 | /*_******************************************************* |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 98 | * Memory operations |
| 99 | **********************************************************/ |
| 100 | static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); } |
| 101 | |
| 102 | |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 103 | /*-************************************* |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 104 | * Error Management |
| 105 | ***************************************/ |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 106 | unsigned ZSTD_versionNumber (void) { return ZSTD_VERSION_NUMBER; } |
| 107 | |
| Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 108 | /*! ZSTD_isError() : |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 109 | * tells if a return value is an error code */ |
| 110 | unsigned ZSTD_isError(size_t code) { return ERR_isError(code); } |
| 111 | |
| Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 112 | /*! ZSTD_getError() : |
| Yann Collet | 72bff50 | 2016-02-03 12:06:24 +0100 | [diff] [blame] | 113 | * convert a `size_t` function result into a proper ZSTD_errorCode enum */ |
| Yann Collet | 982ffc7 | 2016-02-05 02:33:10 +0100 | [diff] [blame] | 114 | ZSTD_ErrorCode ZSTD_getError(size_t code) { return ERR_getError(code); } |
| Yann Collet | 72bff50 | 2016-02-03 12:06:24 +0100 | [diff] [blame] | 115 | |
| Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 116 | /*! ZSTD_getErrorName() : |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 117 | * provides error code string (useful for debugging) */ |
| 118 | const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); } |
| 119 | |
| 120 | |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 121 | /*-************************************************************* |
| Yann Collet | 5b78d2f | 2015-11-12 15:36:05 +0100 | [diff] [blame] | 122 | * Context management |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 123 | ***************************************************************/ |
| Yann Collet | e4fdad5 | 2015-11-25 21:09:17 +0100 | [diff] [blame] | 124 | typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader, |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 125 | ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock } ZSTD_dStage; |
| 126 | |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 127 | struct ZSTD_DCtx_s |
| 128 | { |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 129 | FSE_DTable LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)]; |
| 130 | FSE_DTable OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)]; |
| 131 | FSE_DTable MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)]; |
| 132 | unsigned hufTableX4[HUF_DTABLE_SIZE(HufLog)]; |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 133 | const void* previousDstEnd; |
| 134 | const void* base; |
| 135 | const void* vBase; |
| 136 | const void* dictEnd; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 137 | size_t expected; |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 138 | size_t headerSize; |
| Yann Collet | 0e491c0 | 2016-03-11 21:58:04 +0100 | [diff] [blame] | 139 | ZSTD_frameParams fParams; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 140 | blockType_t bType; /* used in ZSTD_decompressContinue(), to transfer blockType between header decoding and block decoding stages */ |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 141 | ZSTD_dStage stage; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 142 | U32 flagStaticTables; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 143 | const BYTE* litPtr; |
| 144 | size_t litBufSize; |
| 145 | size_t litSize; |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 146 | BYTE litBuffer[BLOCKSIZE + WILDCOPY_OVERLENGTH]; |
| Yann Collet | 0e491c0 | 2016-03-11 21:58:04 +0100 | [diff] [blame] | 147 | BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX]; |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 148 | }; /* typedef'd to ZSTD_DCtx within "zstd_static.h" */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 149 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 150 | size_t sizeofDCtx (void) { return sizeof(ZSTD_DCtx); } |
| 151 | |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 152 | size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx) |
| Yann Collet | 5b78d2f | 2015-11-12 15:36:05 +0100 | [diff] [blame] | 153 | { |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 154 | dctx->expected = ZSTD_frameHeaderSize_min; |
| 155 | dctx->stage = ZSTDds_getFrameHeaderSize; |
| Yann Collet | 5b78d2f | 2015-11-12 15:36:05 +0100 | [diff] [blame] | 156 | dctx->previousDstEnd = NULL; |
| 157 | dctx->base = NULL; |
| 158 | dctx->vBase = NULL; |
| 159 | dctx->dictEnd = NULL; |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 160 | dctx->hufTableX4[0] = HufLog; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 161 | dctx->flagStaticTables = 0; |
| Yann Collet | 0e491c0 | 2016-03-11 21:58:04 +0100 | [diff] [blame] | 162 | dctx->fParams.mml = MINMATCH; /* overwritten by frame but forces ZSTD_btopt to MINMATCH in block mode */ |
| inikep | a4dde25 | 2016-03-01 14:14:35 +0100 | [diff] [blame] | 163 | ZSTD_LOG_BLOCK("%p: ZSTD_decompressBegin searchLength=%d\n", dctx->base, dctx->params.searchLength); |
| Yann Collet | 5b78d2f | 2015-11-12 15:36:05 +0100 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | ZSTD_DCtx* ZSTD_createDCtx(void) |
| 168 | { |
| 169 | ZSTD_DCtx* dctx = (ZSTD_DCtx*)malloc(sizeof(ZSTD_DCtx)); |
| 170 | if (dctx==NULL) return NULL; |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 171 | ZSTD_decompressBegin(dctx); |
| Yann Collet | 5b78d2f | 2015-11-12 15:36:05 +0100 | [diff] [blame] | 172 | return dctx; |
| 173 | } |
| 174 | |
| 175 | size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx) |
| 176 | { |
| 177 | free(dctx); |
| Yann Collet | 982ffc7 | 2016-02-05 02:33:10 +0100 | [diff] [blame] | 178 | return 0; /* reserved as a potential error code in the future */ |
| Yann Collet | 5b78d2f | 2015-11-12 15:36:05 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 181 | void ZSTD_copyDCtx(ZSTD_DCtx* dstDCtx, const ZSTD_DCtx* srcDCtx) |
| 182 | { |
| 183 | memcpy(dstDCtx, srcDCtx, |
| 184 | sizeof(ZSTD_DCtx) - (BLOCKSIZE+WILDCOPY_OVERLENGTH + ZSTD_frameHeaderSize_max)); /* no need to copy workspace */ |
| 185 | } |
| 186 | |
| Yann Collet | 5b78d2f | 2015-11-12 15:36:05 +0100 | [diff] [blame] | 187 | |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 188 | /*-************************************************************* |
| Yann Collet | 5b78d2f | 2015-11-12 15:36:05 +0100 | [diff] [blame] | 189 | * Decompression section |
| 190 | ***************************************************************/ |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 191 | |
| 192 | /* Frame format description |
| 193 | Frame Header - [ Block Header - Block ] - Frame End |
| 194 | 1) Frame Header |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 195 | - 4 bytes - Magic Number : ZSTD_MAGICNUMBER (defined within zstd_static.h) |
| 196 | - 1 byte - Frame Descriptor |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 197 | 2) Block Header |
| 198 | - 3 bytes, starting with a 2-bits descriptor |
| 199 | Uncompressed, Compressed, Frame End, unused |
| 200 | 3) Block |
| 201 | See Block Format Description |
| 202 | 4) Frame End |
| 203 | - 3 bytes, compatible with Block Header |
| 204 | */ |
| 205 | |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 206 | |
| 207 | /* Frame descriptor |
| 208 | |
| 209 | 1 byte, using : |
| 210 | bit 0-3 : windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN (see zstd_internal.h) |
| 211 | bit 4 : minmatch 4(0) or 3(1) |
| 212 | bit 5 : reserved (must be zero) |
| 213 | bit 6-7 : Frame content size : unknown, 1 byte, 2 bytes, 8 bytes |
| Yann Collet | fb79735 | 2016-03-13 11:08:40 +0100 | [diff] [blame] | 214 | |
| 215 | Optional : content size (0, 1, 2 or 8 bytes) |
| 216 | 0 : unknown |
| 217 | 1 : 0-255 bytes |
| 218 | 2 : 256 - 65535+256 |
| 219 | 8 : up to 16 exa |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 220 | */ |
| 221 | |
| 222 | |
| 223 | /* Compressed Block, format description |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 224 | |
| 225 | Block = Literal Section - Sequences Section |
| 226 | Prerequisite : size of (compressed) block, maximum size of regenerated data |
| 227 | |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 228 | 1) Literal Section |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 229 | |
| 230 | 1.1) Header : 1-5 bytes |
| 231 | flags: 2 bits |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 232 | 00 compressed by Huff0 |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 233 | 01 unused |
| 234 | 10 is Raw (uncompressed) |
| 235 | 11 is Rle |
| 236 | Note : using 01 => Huff0 with precomputed table ? |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 237 | Note : delta map ? => compressed ? |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 238 | |
| 239 | 1.1.1) Huff0-compressed literal block : 3-5 bytes |
| 240 | srcSize < 1 KB => 3 bytes (2-2-10-10) => single stream |
| 241 | srcSize < 1 KB => 3 bytes (2-2-10-10) |
| 242 | srcSize < 16KB => 4 bytes (2-2-14-14) |
| 243 | else => 5 bytes (2-2-18-18) |
| 244 | big endian convention |
| 245 | |
| 246 | 1.1.2) Raw (uncompressed) literal block header : 1-3 bytes |
| 247 | size : 5 bits: (IS_RAW<<6) + (0<<4) + size |
| 248 | 12 bits: (IS_RAW<<6) + (2<<4) + (size>>8) |
| 249 | size&255 |
| 250 | 20 bits: (IS_RAW<<6) + (3<<4) + (size>>16) |
| 251 | size>>8&255 |
| 252 | size&255 |
| 253 | |
| 254 | 1.1.3) Rle (repeated single byte) literal block header : 1-3 bytes |
| 255 | size : 5 bits: (IS_RLE<<6) + (0<<4) + size |
| 256 | 12 bits: (IS_RLE<<6) + (2<<4) + (size>>8) |
| 257 | size&255 |
| 258 | 20 bits: (IS_RLE<<6) + (3<<4) + (size>>16) |
| 259 | size>>8&255 |
| 260 | size&255 |
| 261 | |
| 262 | 1.1.4) Huff0-compressed literal block, using precomputed CTables : 3-5 bytes |
| 263 | srcSize < 1 KB => 3 bytes (2-2-10-10) => single stream |
| 264 | srcSize < 1 KB => 3 bytes (2-2-10-10) |
| 265 | srcSize < 16KB => 4 bytes (2-2-14-14) |
| 266 | else => 5 bytes (2-2-18-18) |
| 267 | big endian convention |
| 268 | |
| 269 | 1- CTable available (stored into workspace ?) |
| 270 | 2- Small input (fast heuristic ? Full comparison ? depend on clevel ?) |
| 271 | |
| 272 | |
| 273 | 1.2) Literal block content |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 274 | |
| 275 | 1.2.1) Huff0 block, using sizes from header |
| 276 | See Huff0 format |
| 277 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 278 | 1.2.2) Huff0 block, using prepared table |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 279 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 280 | 1.2.3) Raw content |
| 281 | |
| 282 | 1.2.4) single byte |
| 283 | |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 284 | |
| 285 | 2) Sequences section |
| 286 | TO DO |
| 287 | */ |
| 288 | |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 289 | static const size_t ZSTD_fcs_fieldSize[4] = { 0, 1, 2, 8 }; |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 290 | |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 291 | /** ZSTD_frameHeaderSize() : |
| 292 | * srcSize must be >= ZSTD_frameHeaderSize_min. |
| 293 | * @return : size of the Frame Header */ |
| 294 | static size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize) |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 295 | { |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 296 | U32 fcsId; |
| 297 | if (srcSize < ZSTD_frameHeaderSize_min) return ERROR(srcSize_wrong); |
| 298 | fcsId = (((const BYTE*)src)[4]) >> 6; |
| 299 | return ZSTD_frameHeaderSize_min + ZSTD_fcs_fieldSize[fcsId]; |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 300 | } |
| 301 | |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 302 | |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 303 | /** ZSTD_getFrameParams() : |
| 304 | * decode Frame Header, or provide expected `srcSize`. |
| 305 | * @return : 0, `fparamsPtr` is correctly filled, |
| Yann Collet | 1c2c2bc | 2016-03-15 01:33:36 +0100 | [diff] [blame] | 306 | * >0, `srcSize` is too small, result is expected `srcSize`, |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 307 | * or an error code, which can be tested using ZSTD_isError() */ |
| Yann Collet | 09b21ee | 2016-03-15 12:56:03 +0100 | [diff] [blame] | 308 | size_t ZSTD_getFrameParams(ZSTD_frameParams* fparamsPtr, const void* src, size_t srcSize) |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 309 | { |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 310 | const BYTE* ip = (const BYTE*)src; |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 311 | |
| 312 | if (srcSize < ZSTD_frameHeaderSize_min) return ZSTD_frameHeaderSize_min; |
| Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 313 | if (MEM_readLE32(src) != ZSTD_MAGICNUMBER) return ERROR(prefix_unknown); |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 314 | |
| Yann Collet | 1c2c2bc | 2016-03-15 01:33:36 +0100 | [diff] [blame] | 315 | /* ensure there is enough `srcSize` to fully read/decode frame header */ |
| Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 316 | { size_t const fhsize = ZSTD_frameHeaderSize(src, srcSize); |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 317 | if (srcSize < fhsize) return fhsize; } |
| 318 | |
| Yann Collet | 0e491c0 | 2016-03-11 21:58:04 +0100 | [diff] [blame] | 319 | memset(fparamsPtr, 0, sizeof(*fparamsPtr)); |
| Yann Collet | 1c2c2bc | 2016-03-15 01:33:36 +0100 | [diff] [blame] | 320 | { BYTE const frameDesc = ip[4]; |
| 321 | fparamsPtr->windowLog = (frameDesc & 0xF) + ZSTD_WINDOWLOG_ABSOLUTEMIN; |
| 322 | fparamsPtr->mml = (frameDesc & 0x10) ? MINMATCH-1 : MINMATCH; |
| 323 | if ((frameDesc & 0x20) != 0) return ERROR(frameParameter_unsupported); /* reserved 1 bit */ |
| 324 | switch(frameDesc >> 6) /* fcsId */ |
| 325 | { |
| 326 | default: /* impossible */ |
| 327 | case 0 : fparamsPtr->frameContentSize = 0; break; |
| 328 | case 1 : fparamsPtr->frameContentSize = ip[5]; break; |
| 329 | case 2 : fparamsPtr->frameContentSize = MEM_readLE16(ip+5)+256; break; |
| 330 | case 3 : fparamsPtr->frameContentSize = MEM_readLE64(ip+5); break; |
| 331 | } } |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 335 | |
| 336 | /** ZSTD_decodeFrameHeader() : |
| Yann Collet | 1c2c2bc | 2016-03-15 01:33:36 +0100 | [diff] [blame] | 337 | * `srcSize` must be the size provided by ZSTD_frameHeaderSize(). |
| Yann Collet | 26415d3 | 2015-11-26 12:43:28 +0100 | [diff] [blame] | 338 | * @return : 0, or an error code, which can be tested using ZSTD_isError() */ |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 339 | static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* zc, const void* src, size_t srcSize) |
| Yann Collet | 26415d3 | 2015-11-26 12:43:28 +0100 | [diff] [blame] | 340 | { |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 341 | size_t result = ZSTD_getFrameParams(&(zc->fParams), src, srcSize); |
| Yann Collet | 0e491c0 | 2016-03-11 21:58:04 +0100 | [diff] [blame] | 342 | if ((MEM_32bits()) && (zc->fParams.windowLog > 25)) return ERROR(frameParameter_unsupportedBy32bits); |
| Yann Collet | 00fd7a2 | 2015-11-28 16:03:22 +0100 | [diff] [blame] | 343 | return result; |
| Yann Collet | 26415d3 | 2015-11-26 12:43:28 +0100 | [diff] [blame] | 344 | } |
| 345 | |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 346 | |
| 347 | size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr) |
| 348 | { |
| 349 | const BYTE* const in = (const BYTE* const)src; |
| 350 | BYTE headerFlags; |
| 351 | U32 cSize; |
| 352 | |
| Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 353 | if (srcSize < 3) |
| 354 | return ERROR(srcSize_wrong); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 355 | |
| 356 | headerFlags = *in; |
| 357 | cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16); |
| 358 | |
| 359 | bpPtr->blockType = (blockType_t)(headerFlags >> 6); |
| 360 | bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0; |
| 361 | |
| 362 | if (bpPtr->blockType == bt_end) return 0; |
| 363 | if (bpPtr->blockType == bt_rle) return 1; |
| 364 | return cSize; |
| 365 | } |
| 366 | |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 367 | |
| Yann Collet | 0f366c6 | 2015-11-12 16:19:30 +0100 | [diff] [blame] | 368 | static size_t ZSTD_copyRawBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 369 | { |
| 370 | if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall); |
| 371 | memcpy(dst, src, srcSize); |
| 372 | return srcSize; |
| 373 | } |
| 374 | |
| 375 | |
| Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 376 | /*! ZSTD_decodeLiteralsBlock() : |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 377 | @return : nb of bytes read from src (< srcSize ) */ |
| Yann Collet | 5b78d2f | 2015-11-12 15:36:05 +0100 | [diff] [blame] | 378 | size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx, |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 379 | const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */ |
| 380 | { |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 381 | const BYTE* const istart = (const BYTE*) src; |
| 382 | |
| 383 | /* any compressed block with literals segment must be at least this size */ |
| 384 | if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected); |
| 385 | |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 386 | switch(istart[0]>> 6) |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 387 | { |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 388 | case IS_HUF: |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 389 | { |
| Yann Collet | afe0709 | 2016-01-25 04:10:46 +0100 | [diff] [blame] | 390 | size_t litSize, litCSize, singleStream=0; |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 391 | U32 lhSize = ((istart[0]) >> 4) & 3; |
| 392 | switch(lhSize) |
| 393 | { |
| 394 | case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */ |
| 395 | /* 2 - 2 - 10 - 10 */ |
| 396 | lhSize=3; |
| Yann Collet | afe0709 | 2016-01-25 04:10:46 +0100 | [diff] [blame] | 397 | singleStream = istart[0] & 16; |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 398 | litSize = ((istart[0] & 15) << 6) + (istart[1] >> 2); |
| 399 | litCSize = ((istart[1] & 3) << 8) + istart[2]; |
| 400 | break; |
| 401 | case 2: |
| 402 | /* 2 - 2 - 14 - 14 */ |
| 403 | lhSize=4; |
| 404 | litSize = ((istart[0] & 15) << 10) + (istart[1] << 2) + (istart[2] >> 6); |
| 405 | litCSize = ((istart[2] & 63) << 8) + istart[3]; |
| 406 | break; |
| 407 | case 3: |
| 408 | /* 2 - 2 - 18 - 18 */ |
| 409 | lhSize=5; |
| 410 | litSize = ((istart[0] & 15) << 14) + (istart[1] << 6) + (istart[2] >> 2); |
| 411 | litCSize = ((istart[2] & 3) << 16) + (istart[3] << 8) + istart[4]; |
| 412 | break; |
| 413 | } |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 414 | if (litSize > BLOCKSIZE) return ERROR(corruption_detected); |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 415 | |
| Yann Collet | afe0709 | 2016-01-25 04:10:46 +0100 | [diff] [blame] | 416 | if (HUF_isError(singleStream ? |
| 417 | HUF_decompress1X2(dctx->litBuffer, litSize, istart+lhSize, litCSize) : |
| 418 | HUF_decompress (dctx->litBuffer, litSize, istart+lhSize, litCSize) )) |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 419 | return ERROR(corruption_detected); |
| 420 | |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 421 | dctx->litPtr = dctx->litBuffer; |
| 422 | dctx->litBufSize = BLOCKSIZE+8; |
| 423 | dctx->litSize = litSize; |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 424 | return litCSize + lhSize; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 425 | } |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 426 | case IS_PCH: |
| 427 | { |
| 428 | size_t errorCode; |
| 429 | size_t litSize, litCSize; |
| 430 | U32 lhSize = ((istart[0]) >> 4) & 3; |
| 431 | if (lhSize != 1) /* only case supported for now : small litSize, single stream */ |
| 432 | return ERROR(corruption_detected); |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 433 | if (!dctx->flagStaticTables) |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 434 | return ERROR(dictionary_corrupted); |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 435 | |
| 436 | /* 2 - 2 - 10 - 10 */ |
| 437 | lhSize=3; |
| 438 | litSize = ((istart[0] & 15) << 6) + (istart[1] >> 2); |
| 439 | litCSize = ((istart[1] & 3) << 8) + istart[2]; |
| 440 | |
| 441 | errorCode = HUF_decompress1X4_usingDTable(dctx->litBuffer, litSize, istart+lhSize, litCSize, dctx->hufTableX4); |
| 442 | if (HUF_isError(errorCode)) return ERROR(corruption_detected); |
| 443 | |
| 444 | dctx->litPtr = dctx->litBuffer; |
| 445 | dctx->litBufSize = BLOCKSIZE+WILDCOPY_OVERLENGTH; |
| 446 | dctx->litSize = litSize; |
| 447 | return litCSize + lhSize; |
| 448 | } |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 449 | case IS_RAW: |
| 450 | { |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 451 | size_t litSize; |
| 452 | U32 lhSize = ((istart[0]) >> 4) & 3; |
| 453 | switch(lhSize) |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 454 | { |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 455 | case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */ |
| 456 | lhSize=1; |
| 457 | litSize = istart[0] & 31; |
| 458 | break; |
| 459 | case 2: |
| 460 | litSize = ((istart[0] & 15) << 8) + istart[1]; |
| 461 | break; |
| 462 | case 3: |
| 463 | litSize = ((istart[0] & 15) << 16) + (istart[1] << 8) + istart[2]; |
| 464 | break; |
| 465 | } |
| 466 | |
| Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 467 | if (lhSize+litSize+WILDCOPY_OVERLENGTH > srcSize) { /* risk reading beyond src buffer with wildcopy */ |
| Yann Collet | b010b3b | 2016-02-03 12:39:34 +0100 | [diff] [blame] | 468 | if (litSize+lhSize > srcSize) return ERROR(corruption_detected); |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 469 | memcpy(dctx->litBuffer, istart+lhSize, litSize); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 470 | dctx->litPtr = dctx->litBuffer; |
| 471 | dctx->litBufSize = BLOCKSIZE+8; |
| 472 | dctx->litSize = litSize; |
| Yann Collet | bc4c8aa | 2016-01-25 17:26:01 +0100 | [diff] [blame] | 473 | return lhSize+litSize; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 474 | } |
| 475 | /* direct reference into compressed stream */ |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 476 | dctx->litPtr = istart+lhSize; |
| 477 | dctx->litBufSize = srcSize-lhSize; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 478 | dctx->litSize = litSize; |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 479 | return lhSize+litSize; |
| 480 | } |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 481 | case IS_RLE: |
| 482 | { |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 483 | size_t litSize; |
| 484 | U32 lhSize = ((istart[0]) >> 4) & 3; |
| 485 | switch(lhSize) |
| 486 | { |
| 487 | case 0: case 1: default: /* note : default is impossible, since lhSize into [0..3] */ |
| 488 | lhSize = 1; |
| 489 | litSize = istart[0] & 31; |
| 490 | break; |
| 491 | case 2: |
| 492 | litSize = ((istart[0] & 15) << 8) + istart[1]; |
| 493 | break; |
| 494 | case 3: |
| 495 | litSize = ((istart[0] & 15) << 16) + (istart[1] << 8) + istart[2]; |
| 496 | break; |
| 497 | } |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 498 | if (litSize > BLOCKSIZE) return ERROR(corruption_detected); |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 499 | memset(dctx->litBuffer, istart[lhSize], litSize); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 500 | dctx->litPtr = dctx->litBuffer; |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 501 | dctx->litBufSize = BLOCKSIZE+WILDCOPY_OVERLENGTH; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 502 | dctx->litSize = litSize; |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 503 | return lhSize+1; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 504 | } |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 505 | default: |
| 506 | return ERROR(corruption_detected); /* impossible */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
| 510 | |
| 511 | size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr, |
| 512 | FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb, |
| 513 | const void* src, size_t srcSize) |
| 514 | { |
| 515 | const BYTE* const istart = (const BYTE* const)src; |
| 516 | const BYTE* ip = istart; |
| 517 | const BYTE* const iend = istart + srcSize; |
| 518 | U32 LLtype, Offtype, MLtype; |
| 519 | U32 LLlog, Offlog, MLlog; |
| 520 | size_t dumpsLength; |
| 521 | |
| 522 | /* check */ |
| Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 523 | if (srcSize < MIN_SEQUENCES_SIZE) |
| 524 | return ERROR(srcSize_wrong); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 525 | |
| 526 | /* SeqHead */ |
| Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 527 | *nbSeq = *ip++; |
| 528 | if (*nbSeq==0) return 1; |
| Yann Collet | d409db6 | 2016-03-04 14:45:31 +0100 | [diff] [blame] | 529 | if (*nbSeq >= 0x7F) { |
| 530 | if (*nbSeq == 0xFF) |
| 531 | *nbSeq = MEM_readLE16(ip) + LONGNBSEQ, ip+=2; |
| 532 | else |
| 533 | *nbSeq = ((nbSeq[0]-0x80)<<8) + *ip++; |
| 534 | } |
| Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 535 | |
| Yann Collet | d409db6 | 2016-03-04 14:45:31 +0100 | [diff] [blame] | 536 | /* FSE table descriptors */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 537 | LLtype = *ip >> 6; |
| 538 | Offtype = (*ip >> 4) & 3; |
| 539 | MLtype = (*ip >> 2) & 3; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 540 | if (*ip & 2) { |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 541 | dumpsLength = ip[2]; |
| 542 | dumpsLength += ip[1] << 8; |
| 543 | ip += 3; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 544 | } else { |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 545 | dumpsLength = ip[1]; |
| 546 | dumpsLength += (ip[0] & 1) << 8; |
| 547 | ip += 2; |
| 548 | } |
| 549 | *dumpsPtr = ip; |
| 550 | ip += dumpsLength; |
| 551 | *dumpsLengthPtr = dumpsLength; |
| 552 | |
| 553 | /* check */ |
| 554 | if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */ |
| 555 | |
| 556 | /* sequences */ |
| 557 | { |
| Yann Collet | 82368cf | 2015-11-16 19:10:56 +0100 | [diff] [blame] | 558 | S16 norm[MaxML+1]; /* assumption : MaxML >= MaxLL >= MaxOff */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 559 | size_t headerSize; |
| 560 | |
| 561 | /* Build DTables */ |
| 562 | switch(LLtype) |
| 563 | { |
| 564 | U32 max; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 565 | case FSE_ENCODING_RLE : |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 566 | LLlog = 0; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 567 | FSE_buildDTable_rle(DTableLL, *ip++); |
| 568 | break; |
| 569 | case FSE_ENCODING_RAW : |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 570 | LLlog = LLbits; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 571 | FSE_buildDTable_raw(DTableLL, LLbits); |
| 572 | break; |
| 573 | case FSE_ENCODING_STATIC: |
| 574 | break; |
| 575 | case FSE_ENCODING_DYNAMIC : |
| 576 | default : /* impossible */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 577 | max = MaxLL; |
| 578 | headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip); |
| 579 | if (FSE_isError(headerSize)) return ERROR(GENERIC); |
| 580 | if (LLlog > LLFSELog) return ERROR(corruption_detected); |
| 581 | ip += headerSize; |
| 582 | FSE_buildDTable(DTableLL, norm, max, LLlog); |
| 583 | } |
| 584 | |
| 585 | switch(Offtype) |
| 586 | { |
| 587 | U32 max; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 588 | case FSE_ENCODING_RLE : |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 589 | Offlog = 0; |
| 590 | if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */ |
| 591 | FSE_buildDTable_rle(DTableOffb, *ip++ & MaxOff); /* if *ip > MaxOff, data is corrupted */ |
| 592 | break; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 593 | case FSE_ENCODING_RAW : |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 594 | Offlog = Offbits; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 595 | FSE_buildDTable_raw(DTableOffb, Offbits); |
| 596 | break; |
| 597 | case FSE_ENCODING_STATIC: |
| 598 | break; |
| 599 | case FSE_ENCODING_DYNAMIC : |
| 600 | default : /* impossible */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 601 | max = MaxOff; |
| 602 | headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip); |
| 603 | if (FSE_isError(headerSize)) return ERROR(GENERIC); |
| 604 | if (Offlog > OffFSELog) return ERROR(corruption_detected); |
| 605 | ip += headerSize; |
| 606 | FSE_buildDTable(DTableOffb, norm, max, Offlog); |
| 607 | } |
| 608 | |
| 609 | switch(MLtype) |
| 610 | { |
| 611 | U32 max; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 612 | case FSE_ENCODING_RLE : |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 613 | MLlog = 0; |
| 614 | if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */ |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 615 | FSE_buildDTable_rle(DTableML, *ip++); |
| 616 | break; |
| 617 | case FSE_ENCODING_RAW : |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 618 | MLlog = MLbits; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 619 | FSE_buildDTable_raw(DTableML, MLbits); |
| 620 | break; |
| 621 | case FSE_ENCODING_STATIC: |
| 622 | break; |
| 623 | case FSE_ENCODING_DYNAMIC : |
| 624 | default : /* impossible */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 625 | max = MaxML; |
| 626 | headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip); |
| 627 | if (FSE_isError(headerSize)) return ERROR(GENERIC); |
| 628 | if (MLlog > MLFSELog) return ERROR(corruption_detected); |
| 629 | ip += headerSize; |
| 630 | FSE_buildDTable(DTableML, norm, max, MLlog); |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 631 | } } |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 632 | |
| 633 | return ip-istart; |
| 634 | } |
| 635 | |
| 636 | |
| 637 | typedef struct { |
| 638 | size_t litLength; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 639 | size_t matchLength; |
| Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 640 | size_t offset; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 641 | } seq_t; |
| 642 | |
| 643 | typedef struct { |
| 644 | BIT_DStream_t DStream; |
| 645 | FSE_DState_t stateLL; |
| 646 | FSE_DState_t stateOffb; |
| 647 | FSE_DState_t stateML; |
| 648 | size_t prevOffset; |
| 649 | const BYTE* dumps; |
| 650 | const BYTE* dumpsEnd; |
| 651 | } seqState_t; |
| 652 | |
| inikep | e9f30ea | 2016-02-03 12:53:07 +0100 | [diff] [blame] | 653 | |
| 654 | |
| inikep | 6b3739c | 2016-02-22 15:53:42 +0100 | [diff] [blame] | 655 | static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState, const U32 mls) |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 656 | { |
| 657 | size_t litLength; |
| 658 | size_t prevOffset; |
| 659 | size_t offset; |
| 660 | size_t matchLength; |
| 661 | const BYTE* dumps = seqState->dumps; |
| 662 | const BYTE* const de = seqState->dumpsEnd; |
| 663 | |
| 664 | /* Literal length */ |
| Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 665 | litLength = FSE_peakSymbol(&(seqState->stateLL)); |
| Yann Collet | e4fdad5 | 2015-11-25 21:09:17 +0100 | [diff] [blame] | 666 | prevOffset = litLength ? seq->offset : seqState->prevOffset; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 667 | if (litLength == MaxLL) { |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 668 | U32 add = *dumps++; |
| 669 | if (add < 255) litLength += add; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 670 | else { |
| Yann Collet | 7d8e6bd | 2016-02-02 17:30:37 +0100 | [diff] [blame] | 671 | litLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no risk : dumps is always followed by seq tables > 1 byte */ |
| 672 | if (litLength&1) litLength>>=1, dumps += 3; |
| 673 | else litLength = (U16)(litLength)>>1, dumps += 2; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 674 | } |
| 675 | if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */ |
| 676 | } |
| 677 | |
| 678 | /* Offset */ |
| 679 | { |
| 680 | static const U32 offsetPrefix[MaxOff+1] = { |
| Yann Collet | 95cd0c2 | 2016-03-08 18:24:21 +0100 | [diff] [blame] | 681 | 1 /*fake*/, 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, 0x100, |
| 682 | 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000, 0x10000, 0x20000, 0x40000, |
| 683 | 0x80000, 0x100000, 0x200000, 0x400000, 0x800000, 0x1000000, 0x2000000, 0x4000000, /*fake*/ 1, 1, 1, 1 }; |
| Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 684 | U32 offsetCode = FSE_peakSymbol(&(seqState->stateOffb)); /* <= maxOff, by table construction */ |
| 685 | U32 nbBits = offsetCode - 1; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 686 | if (offsetCode==0) nbBits = 0; /* cmove */ |
| 687 | offset = offsetPrefix[offsetCode] + BIT_readBits(&(seqState->DStream), nbBits); |
| 688 | if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream)); |
| Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 689 | if (offsetCode==0) offset = prevOffset; /* repcode, cmove */ |
| Yann Collet | 55aa7f9 | 2015-11-20 12:04:52 +0100 | [diff] [blame] | 690 | if (offsetCode | !litLength) seqState->prevOffset = seq->offset; /* cmove */ |
| Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 691 | FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream)); /* update */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 692 | } |
| 693 | |
| Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 694 | /* Literal length update */ |
| 695 | FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream)); /* update */ |
| 696 | if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream)); |
| 697 | |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 698 | /* MatchLength */ |
| 699 | matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream)); |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 700 | if (matchLength == MaxML) { |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 701 | U32 add = *dumps++; |
| 702 | if (add < 255) matchLength += add; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 703 | else { |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 704 | matchLength = MEM_readLE32(dumps) & 0xFFFFFF; /* no pb : dumps is always followed by seq tables > 1 byte */ |
| Yann Collet | 7d8e6bd | 2016-02-02 17:30:37 +0100 | [diff] [blame] | 705 | if (matchLength&1) matchLength>>=1, dumps += 3; |
| 706 | else matchLength = (U16)(matchLength)>>1, dumps += 2; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 707 | } |
| 708 | if (dumps >= de) dumps = de-1; /* late correction, to avoid read overflow (data is now corrupted anyway) */ |
| 709 | } |
| inikep | 6b3739c | 2016-02-22 15:53:42 +0100 | [diff] [blame] | 710 | matchLength += mls; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 711 | |
| 712 | /* save result */ |
| 713 | seq->litLength = litLength; |
| 714 | seq->offset = offset; |
| 715 | seq->matchLength = matchLength; |
| 716 | seqState->dumps = dumps; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 717 | |
| Yann Collet | 7d8e6bd | 2016-02-02 17:30:37 +0100 | [diff] [blame] | 718 | #if 0 /* debug */ |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 719 | { |
| 720 | static U64 totalDecoded = 0; |
| 721 | printf("pos %6u : %3u literals & match %3u bytes at distance %6u \n", |
| 722 | (U32)(totalDecoded), (U32)litLength, (U32)matchLength, (U32)offset); |
| 723 | totalDecoded += litLength + matchLength; |
| 724 | } |
| 725 | #endif |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | |
| Yann Collet | 5b78d2f | 2015-11-12 15:36:05 +0100 | [diff] [blame] | 729 | FORCE_INLINE size_t ZSTD_execSequence(BYTE* op, |
| Yann Collet | b3a2af9 | 2015-11-19 17:13:19 +0100 | [diff] [blame] | 730 | BYTE* const oend, seq_t sequence, |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 731 | const BYTE** litPtr, const BYTE* const litLimit_8, |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 732 | const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd) |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 733 | { |
| Yann Collet | b3a2af9 | 2015-11-19 17:13:19 +0100 | [diff] [blame] | 734 | static const int dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */ |
| 735 | static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* substracted */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 736 | BYTE* const oLitEnd = op + sequence.litLength; |
| Yann Collet | b3a2af9 | 2015-11-19 17:13:19 +0100 | [diff] [blame] | 737 | const size_t sequenceLength = sequence.litLength + sequence.matchLength; |
| 738 | BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 739 | BYTE* const oend_8 = oend-8; |
| 740 | const BYTE* const litEnd = *litPtr + sequence.litLength; |
| Yann Collet | b3a2af9 | 2015-11-19 17:13:19 +0100 | [diff] [blame] | 741 | const BYTE* match = oLitEnd - sequence.offset; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 742 | |
| 743 | /* check */ |
| 744 | if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); /* last match must start at a minimum distance of 8 from oend */ |
| 745 | if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */ |
| 746 | if (litEnd > litLimit_8) return ERROR(corruption_detected); /* risk read beyond lit buffer */ |
| 747 | |
| 748 | /* copy Literals */ |
| 749 | ZSTD_wildcopy(op, *litPtr, sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */ |
| 750 | op = oLitEnd; |
| 751 | *litPtr = litEnd; /* update for next sequence */ |
| 752 | |
| 753 | /* copy Match */ |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 754 | if (sequence.offset > (size_t)(oLitEnd - base)) { |
| Yann Collet | 44287a3 | 2015-11-30 23:13:56 +0100 | [diff] [blame] | 755 | /* offset beyond prefix */ |
| Yann Collet | 9f5ab1a | 2015-12-11 00:27:41 +0100 | [diff] [blame] | 756 | if (sequence.offset > (size_t)(oLitEnd - vBase)) |
| 757 | return ERROR(corruption_detected); |
| Yann Collet | 44287a3 | 2015-11-30 23:13:56 +0100 | [diff] [blame] | 758 | match = dictEnd - (base-match); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 759 | if (match + sequence.matchLength <= dictEnd) { |
| Yann Collet | 4bfe415 | 2015-12-06 13:18:37 +0100 | [diff] [blame] | 760 | memmove(oLitEnd, match, sequence.matchLength); |
| Yann Collet | 44287a3 | 2015-11-30 23:13:56 +0100 | [diff] [blame] | 761 | return sequenceLength; |
| 762 | } |
| 763 | /* span extDict & currentPrefixSegment */ |
| 764 | { |
| 765 | size_t length1 = dictEnd - match; |
| Yann Collet | 4bfe415 | 2015-12-06 13:18:37 +0100 | [diff] [blame] | 766 | memmove(oLitEnd, match, length1); |
| Yann Collet | 44287a3 | 2015-11-30 23:13:56 +0100 | [diff] [blame] | 767 | op = oLitEnd + length1; |
| 768 | sequence.matchLength -= length1; |
| 769 | match = base; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 770 | } } |
| Yann Collet | 0f366c6 | 2015-11-12 16:19:30 +0100 | [diff] [blame] | 771 | |
| Yann Collet | 44287a3 | 2015-11-30 23:13:56 +0100 | [diff] [blame] | 772 | /* match within prefix */ |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 773 | if (sequence.offset < 8) { |
| Yann Collet | 44287a3 | 2015-11-30 23:13:56 +0100 | [diff] [blame] | 774 | /* close range match, overlap */ |
| 775 | const int sub2 = dec64table[sequence.offset]; |
| 776 | op[0] = match[0]; |
| 777 | op[1] = match[1]; |
| 778 | op[2] = match[2]; |
| 779 | op[3] = match[3]; |
| 780 | match += dec32table[sequence.offset]; |
| 781 | ZSTD_copy4(op+4, match); |
| 782 | match -= sub2; |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 783 | } else { |
| Yann Collet | 44287a3 | 2015-11-30 23:13:56 +0100 | [diff] [blame] | 784 | ZSTD_copy8(op, match); |
| 785 | } |
| 786 | op += 8; match += 8; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 787 | |
| inikep | a4dde25 | 2016-03-01 14:14:35 +0100 | [diff] [blame] | 788 | if (oMatchEnd > oend-(16-3)) { // 3 = MINMATCH |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 789 | if (op < oend_8) { |
| Yann Collet | 44287a3 | 2015-11-30 23:13:56 +0100 | [diff] [blame] | 790 | ZSTD_wildcopy(op, match, oend_8 - op); |
| 791 | match += oend_8 - op; |
| 792 | op = oend_8; |
| 793 | } |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 794 | while (op < oMatchEnd) |
| 795 | *op++ = *match++; |
| 796 | } else { |
| Yann Collet | 44287a3 | 2015-11-30 23:13:56 +0100 | [diff] [blame] | 797 | ZSTD_wildcopy(op, match, sequence.matchLength-8); /* works even if matchLength < 8 */ |
| 798 | } |
| 799 | return sequenceLength; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| Yann Collet | b3a2af9 | 2015-11-19 17:13:19 +0100 | [diff] [blame] | 802 | |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 803 | static size_t ZSTD_decompressSequences( |
| Yann Collet | 5b78d2f | 2015-11-12 15:36:05 +0100 | [diff] [blame] | 804 | ZSTD_DCtx* dctx, |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 805 | void* dst, size_t maxDstSize, |
| 806 | const void* seqStart, size_t seqSize) |
| 807 | { |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 808 | const BYTE* ip = (const BYTE*)seqStart; |
| 809 | const BYTE* const iend = ip + seqSize; |
| 810 | BYTE* const ostart = (BYTE* const)dst; |
| 811 | BYTE* op = ostart; |
| 812 | BYTE* const oend = ostart + maxDstSize; |
| 813 | size_t errorCode, dumpsLength; |
| 814 | const BYTE* litPtr = dctx->litPtr; |
| 815 | const BYTE* const litLimit_8 = litPtr + dctx->litBufSize - 8; |
| 816 | const BYTE* const litEnd = litPtr + dctx->litSize; |
| 817 | int nbSeq; |
| 818 | const BYTE* dumps; |
| 819 | U32* DTableLL = dctx->LLTable; |
| 820 | U32* DTableML = dctx->MLTable; |
| 821 | U32* DTableOffb = dctx->OffTable; |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 822 | const BYTE* const base = (const BYTE*) (dctx->base); |
| 823 | const BYTE* const vBase = (const BYTE*) (dctx->vBase); |
| 824 | const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); |
| Yann Collet | 0e491c0 | 2016-03-11 21:58:04 +0100 | [diff] [blame] | 825 | const U32 mls = dctx->fParams.mml; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 826 | |
| 827 | /* Build Decoding Tables */ |
| 828 | errorCode = ZSTD_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength, |
| 829 | DTableLL, DTableML, DTableOffb, |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 830 | ip, seqSize); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 831 | if (ZSTD_isError(errorCode)) return errorCode; |
| 832 | ip += errorCode; |
| 833 | |
| 834 | /* Regen sequences */ |
| Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 835 | if (nbSeq) { |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 836 | seq_t sequence; |
| 837 | seqState_t seqState; |
| 838 | |
| 839 | memset(&sequence, 0, sizeof(sequence)); |
| Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 840 | sequence.offset = REPCODE_STARTVALUE; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 841 | seqState.dumps = dumps; |
| 842 | seqState.dumpsEnd = dumps + dumpsLength; |
| Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 843 | seqState.prevOffset = REPCODE_STARTVALUE; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 844 | errorCode = BIT_initDStream(&(seqState.DStream), ip, iend-ip); |
| 845 | if (ERR_isError(errorCode)) return ERROR(corruption_detected); |
| 846 | FSE_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL); |
| 847 | FSE_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb); |
| 848 | FSE_initDState(&(seqState.stateML), &(seqState.DStream), DTableML); |
| 849 | |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 850 | for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && nbSeq ; ) { |
| Yann Collet | b3a2af9 | 2015-11-19 17:13:19 +0100 | [diff] [blame] | 851 | size_t oneSeqSize; |
| 852 | nbSeq--; |
| inikep | 6b3739c | 2016-02-22 15:53:42 +0100 | [diff] [blame] | 853 | ZSTD_decodeSequence(&sequence, &seqState, mls); |
| Yann Collet | b3a2af9 | 2015-11-19 17:13:19 +0100 | [diff] [blame] | 854 | oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litLimit_8, base, vBase, dictEnd); |
| Yann Collet | 370b08e | 2016-03-08 00:03:59 +0100 | [diff] [blame] | 855 | if (ZSTD_isError(oneSeqSize)) |
| 856 | return oneSeqSize; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 857 | op += oneSeqSize; |
| 858 | } |
| 859 | |
| 860 | /* check if reached exact end */ |
| Yann Collet | 35f7de5 | 2016-01-31 02:51:03 +0100 | [diff] [blame] | 861 | if (nbSeq) return ERROR(corruption_detected); |
| Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 862 | } |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 863 | |
| Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 864 | /* last literal segment */ |
| 865 | { |
| 866 | size_t lastLLSize = litEnd - litPtr; |
| 867 | if (litPtr > litEnd) return ERROR(corruption_detected); /* too many literals already used */ |
| 868 | if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall); |
| 869 | memcpy(op, litPtr, lastLLSize); |
| 870 | op += lastLLSize; |
| 871 | } |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 872 | |
| 873 | return op-ostart; |
| 874 | } |
| 875 | |
| 876 | |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 877 | static void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst) |
| 878 | { |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 879 | if (dst != dctx->previousDstEnd) { /* not contiguous */ |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 880 | dctx->dictEnd = dctx->previousDstEnd; |
| 881 | dctx->vBase = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base)); |
| 882 | dctx->base = dst; |
| 883 | dctx->previousDstEnd = dst; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | |
| 888 | static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx, |
| Yann Collet | b010b3b | 2016-02-03 12:39:34 +0100 | [diff] [blame] | 889 | void* dst, size_t dstCapacity, |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 890 | const void* src, size_t srcSize) |
| Yann Collet | b010b3b | 2016-02-03 12:39:34 +0100 | [diff] [blame] | 891 | { /* blockType == blockCompressed */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 892 | const BYTE* ip = (const BYTE*)src; |
| Yann Collet | b010b3b | 2016-02-03 12:39:34 +0100 | [diff] [blame] | 893 | size_t litCSize; |
| 894 | |
| 895 | if (srcSize >= BLOCKSIZE) return ERROR(srcSize_wrong); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 896 | |
| inikep | a4dde25 | 2016-03-01 14:14:35 +0100 | [diff] [blame] | 897 | ZSTD_LOG_BLOCK("%p: ZSTD_decompressBlock_internal searchLength=%d\n", dctx->base, dctx->params.searchLength); |
| 898 | |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 899 | /* Decode literals sub-block */ |
| Yann Collet | b010b3b | 2016-02-03 12:39:34 +0100 | [diff] [blame] | 900 | litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 901 | if (ZSTD_isError(litCSize)) return litCSize; |
| 902 | ip += litCSize; |
| 903 | srcSize -= litCSize; |
| 904 | |
| Yann Collet | b010b3b | 2016-02-03 12:39:34 +0100 | [diff] [blame] | 905 | return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 909 | size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, |
| Yann Collet | b010b3b | 2016-02-03 12:39:34 +0100 | [diff] [blame] | 910 | void* dst, size_t dstCapacity, |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 911 | const void* src, size_t srcSize) |
| 912 | { |
| 913 | ZSTD_checkContinuity(dctx, dst); |
| Yann Collet | b010b3b | 2016-02-03 12:39:34 +0100 | [diff] [blame] | 914 | return ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize); |
| Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | |
| Yann Collet | 0e491c0 | 2016-03-11 21:58:04 +0100 | [diff] [blame] | 918 | /*! ZSTD_decompress_continueDCtx() : |
| 919 | * `dctx` must have been properly initialized */ |
| 920 | static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx, |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 921 | void* dst, size_t maxDstSize, |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 922 | const void* src, size_t srcSize) |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 923 | { |
| 924 | const BYTE* ip = (const BYTE*)src; |
| 925 | const BYTE* iend = ip + srcSize; |
| 926 | BYTE* const ostart = (BYTE* const)dst; |
| 927 | BYTE* op = ostart; |
| 928 | BYTE* const oend = ostart + maxDstSize; |
| 929 | size_t remainingSize = srcSize; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 930 | blockProperties_t blockProperties; |
| 931 | |
| 932 | /* Frame Header */ |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 933 | { |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 934 | size_t frameHeaderSize, errorCode; |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 935 | if (srcSize < ZSTD_frameHeaderSize_min+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 936 | #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1) |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 937 | { |
| 938 | const U32 magicNumber = MEM_readLE32(src); |
| 939 | if (ZSTD_isLegacy(magicNumber)) |
| 940 | return ZSTD_decompressLegacy(dst, maxDstSize, src, srcSize, magicNumber); |
| 941 | } |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 942 | #endif |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 943 | frameHeaderSize = ZSTD_frameHeaderSize(src, ZSTD_frameHeaderSize_min); |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 944 | if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize; |
| 945 | if (srcSize < frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong); |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 946 | errorCode = ZSTD_decodeFrameHeader(dctx, src, frameHeaderSize); |
| 947 | if (ZSTD_isError(errorCode)) return errorCode; |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 948 | ip += frameHeaderSize; remainingSize -= frameHeaderSize; |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 949 | } |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 950 | |
| 951 | /* Loop on each block */ |
| 952 | while (1) |
| 953 | { |
| 954 | size_t decodedSize=0; |
| 955 | size_t cBlockSize = ZSTD_getcBlockSize(ip, iend-ip, &blockProperties); |
| 956 | if (ZSTD_isError(cBlockSize)) return cBlockSize; |
| 957 | |
| 958 | ip += ZSTD_blockHeaderSize; |
| 959 | remainingSize -= ZSTD_blockHeaderSize; |
| 960 | if (cBlockSize > remainingSize) return ERROR(srcSize_wrong); |
| 961 | |
| 962 | switch(blockProperties.blockType) |
| 963 | { |
| 964 | case bt_compressed: |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 965 | decodedSize = ZSTD_decompressBlock_internal(dctx, op, oend-op, ip, cBlockSize); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 966 | break; |
| 967 | case bt_raw : |
| Yann Collet | 0f366c6 | 2015-11-12 16:19:30 +0100 | [diff] [blame] | 968 | decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 969 | break; |
| 970 | case bt_rle : |
| 971 | return ERROR(GENERIC); /* not yet supported */ |
| 972 | break; |
| 973 | case bt_end : |
| 974 | /* end of frame */ |
| 975 | if (remainingSize) return ERROR(srcSize_wrong); |
| 976 | break; |
| 977 | default: |
| 978 | return ERROR(GENERIC); /* impossible */ |
| 979 | } |
| 980 | if (cBlockSize == 0) break; /* bt_end */ |
| 981 | |
| 982 | if (ZSTD_isError(decodedSize)) return decodedSize; |
| 983 | op += decodedSize; |
| 984 | ip += cBlockSize; |
| 985 | remainingSize -= cBlockSize; |
| 986 | } |
| 987 | |
| 988 | return op-ostart; |
| 989 | } |
| 990 | |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 991 | |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 992 | size_t ZSTD_decompress_usingPreparedDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* refDCtx, |
| 993 | void* dst, size_t maxDstSize, |
| 994 | const void* src, size_t srcSize) |
| 995 | { |
| 996 | ZSTD_copyDCtx(dctx, refDCtx); |
| 997 | ZSTD_checkContinuity(dctx, dst); |
| Yann Collet | 0e491c0 | 2016-03-11 21:58:04 +0100 | [diff] [blame] | 998 | return ZSTD_decompressFrame(dctx, dst, maxDstSize, src, srcSize); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | |
| 1002 | size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, |
| 1003 | void* dst, size_t maxDstSize, |
| 1004 | const void* src, size_t srcSize, |
| 1005 | const void* dict, size_t dictSize) |
| 1006 | { |
| 1007 | ZSTD_decompressBegin_usingDict(dctx, dict, dictSize); |
| inikep | a4dde25 | 2016-03-01 14:14:35 +0100 | [diff] [blame] | 1008 | ZSTD_LOG_BLOCK("%p: ZSTD_decompressBegin_usingDict searchLength=%d\n", dctx->base, dctx->params.searchLength); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1009 | ZSTD_checkContinuity(dctx, dst); |
| Yann Collet | 0e491c0 | 2016-03-11 21:58:04 +0100 | [diff] [blame] | 1010 | return ZSTD_decompressFrame(dctx, dst, maxDstSize, src, srcSize); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 1014 | size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| 1015 | { |
| 1016 | return ZSTD_decompress_usingDict(dctx, dst, maxDstSize, src, srcSize, NULL, 0); |
| 1017 | } |
| 1018 | |
| Yann Collet | 0e491c0 | 2016-03-11 21:58:04 +0100 | [diff] [blame] | 1019 | |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1020 | size_t ZSTD_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| 1021 | { |
| Yann Collet | 3a3b72f | 2016-01-11 12:56:11 +0100 | [diff] [blame] | 1022 | #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE==1) |
| 1023 | size_t regenSize; |
| 1024 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
| 1025 | if (dctx==NULL) return ERROR(memory_allocation); |
| 1026 | regenSize = ZSTD_decompressDCtx(dctx, dst, maxDstSize, src, srcSize); |
| 1027 | ZSTD_freeDCtx(dctx); |
| 1028 | return regenSize; |
| 1029 | #else |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 1030 | ZSTD_DCtx dctx; |
| 1031 | return ZSTD_decompressDCtx(&dctx, dst, maxDstSize, src, srcSize); |
| Yann Collet | a768a30 | 2016-01-21 16:04:35 +0100 | [diff] [blame] | 1032 | #endif |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | |
| Yann Collet | 346bffb | 2016-03-15 15:24:52 +0100 | [diff] [blame^] | 1036 | /*_****************************** |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1037 | * Streaming Decompression API |
| 1038 | ********************************/ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1039 | size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) |
| 1040 | { |
| 1041 | return dctx->expected; |
| 1042 | } |
| 1043 | |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1044 | size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1045 | { |
| 1046 | /* Sanity check */ |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1047 | if (srcSize != dctx->expected) return ERROR(srcSize_wrong); |
| 1048 | ZSTD_checkContinuity(dctx, dst); |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1049 | |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1050 | /* Decompress : frame header; part 1 */ |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1051 | switch (dctx->stage) |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1052 | { |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1053 | case ZSTDds_getFrameHeaderSize : |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1054 | { |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1055 | /* get frame header size */ |
| 1056 | if (srcSize != ZSTD_frameHeaderSize_min) return ERROR(srcSize_wrong); /* impossible */ |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 1057 | dctx->headerSize = ZSTD_frameHeaderSize(src, ZSTD_frameHeaderSize_min); |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1058 | if (ZSTD_isError(dctx->headerSize)) return dctx->headerSize; |
| 1059 | memcpy(dctx->headerBuffer, src, ZSTD_frameHeaderSize_min); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1060 | if (dctx->headerSize > ZSTD_frameHeaderSize_min) { |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1061 | dctx->expected = dctx->headerSize - ZSTD_frameHeaderSize_min; |
| 1062 | dctx->stage = ZSTDds_decodeFrameHeader; |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1063 | return 0; |
| 1064 | } |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1065 | dctx->expected = 0; /* not necessary to copy more */ |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1066 | } |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1067 | case ZSTDds_decodeFrameHeader: |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1068 | { |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1069 | /* get frame header */ |
| 1070 | size_t result; |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1071 | memcpy(dctx->headerBuffer + ZSTD_frameHeaderSize_min, src, dctx->expected); |
| Yann Collet | 03ea59b | 2016-03-12 01:25:40 +0100 | [diff] [blame] | 1072 | result = ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize); |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1073 | if (ZSTD_isError(result)) return result; |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1074 | dctx->expected = ZSTD_blockHeaderSize; |
| 1075 | dctx->stage = ZSTDds_decodeBlockHeader; |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1076 | return 0; |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1077 | } |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1078 | case ZSTDds_decodeBlockHeader: |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1079 | { |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1080 | /* Decode block header */ |
| 1081 | blockProperties_t bp; |
| 1082 | size_t blockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp); |
| 1083 | if (ZSTD_isError(blockSize)) return blockSize; |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1084 | if (bp.blockType == bt_end) { |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1085 | dctx->expected = 0; |
| 1086 | dctx->stage = ZSTDds_getFrameHeaderSize; |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1087 | } |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1088 | else { |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1089 | dctx->expected = blockSize; |
| 1090 | dctx->bType = bp.blockType; |
| 1091 | dctx->stage = ZSTDds_decompressBlock; |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1092 | } |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1095 | case ZSTDds_decompressBlock: |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1096 | { |
| 1097 | /* Decompress : block content */ |
| 1098 | size_t rSize; |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1099 | switch(dctx->bType) |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1100 | { |
| 1101 | case bt_compressed: |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1102 | rSize = ZSTD_decompressBlock_internal(dctx, dst, maxDstSize, src, srcSize); |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1103 | break; |
| 1104 | case bt_raw : |
| 1105 | rSize = ZSTD_copyRawBlock(dst, maxDstSize, src, srcSize); |
| 1106 | break; |
| 1107 | case bt_rle : |
| 1108 | return ERROR(GENERIC); /* not yet handled */ |
| 1109 | break; |
| 1110 | case bt_end : /* should never happen (filtered at phase 1) */ |
| 1111 | rSize = 0; |
| 1112 | break; |
| 1113 | default: |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1114 | return ERROR(GENERIC); /* impossible */ |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1115 | } |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1116 | dctx->stage = ZSTDds_decodeBlockHeader; |
| 1117 | dctx->expected = ZSTD_blockHeaderSize; |
| 1118 | dctx->previousDstEnd = (char*)dst + rSize; |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 1119 | return rSize; |
| 1120 | } |
| 1121 | default: |
| 1122 | return ERROR(GENERIC); /* impossible */ |
| 1123 | } |
| Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 1127 | static void ZSTD_refDictContent(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1128 | { |
| Yann Collet | 3742219 | 2016-01-25 16:54:05 +0100 | [diff] [blame] | 1129 | dctx->dictEnd = dctx->previousDstEnd; |
| 1130 | dctx->vBase = (const char*)dict - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base)); |
| 1131 | dctx->base = dict; |
| 1132 | dctx->previousDstEnd = (const char*)dict + dictSize; |
| Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1133 | } |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 1134 | |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 1135 | static size_t ZSTD_loadEntropy(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
| 1136 | { |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1137 | size_t hSize, offcodeHeaderSize, matchlengthHeaderSize, errorCode, litlengthHeaderSize; |
| 1138 | short offcodeNCount[MaxOff+1]; |
| 1139 | U32 offcodeMaxValue=MaxOff, offcodeLog=OffFSELog; |
| 1140 | short matchlengthNCount[MaxML+1]; |
| 1141 | unsigned matchlengthMaxValue = MaxML, matchlengthLog = MLFSELog; |
| 1142 | short litlengthNCount[MaxLL+1]; |
| 1143 | unsigned litlengthMaxValue = MaxLL, litlengthLog = LLFSELog; |
| 1144 | |
| 1145 | hSize = HUF_readDTableX4(dctx->hufTableX4, dict, dictSize); |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 1146 | if (HUF_isError(hSize)) return ERROR(dictionary_corrupted); |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1147 | dict = (const char*)dict + hSize; |
| 1148 | dictSize -= hSize; |
| 1149 | |
| 1150 | offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dict, dictSize); |
| 1151 | if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted); |
| 1152 | errorCode = FSE_buildDTable(dctx->OffTable, offcodeNCount, offcodeMaxValue, offcodeLog); |
| 1153 | if (FSE_isError(errorCode)) return ERROR(dictionary_corrupted); |
| 1154 | dict = (const char*)dict + offcodeHeaderSize; |
| 1155 | dictSize -= offcodeHeaderSize; |
| 1156 | |
| 1157 | matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dict, dictSize); |
| 1158 | if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted); |
| 1159 | errorCode = FSE_buildDTable(dctx->MLTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog); |
| 1160 | if (FSE_isError(errorCode)) return ERROR(dictionary_corrupted); |
| 1161 | dict = (const char*)dict + matchlengthHeaderSize; |
| 1162 | dictSize -= matchlengthHeaderSize; |
| 1163 | |
| 1164 | litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dict, dictSize); |
| 1165 | if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted); |
| 1166 | errorCode = FSE_buildDTable(dctx->LLTable, litlengthNCount, litlengthMaxValue, litlengthLog); |
| 1167 | if (FSE_isError(errorCode)) return ERROR(dictionary_corrupted); |
| 1168 | |
| 1169 | dctx->flagStaticTables = 1; |
| 1170 | return hSize + offcodeHeaderSize + matchlengthHeaderSize + litlengthHeaderSize; |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1173 | static size_t ZSTD_decompress_insertDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 1174 | { |
| 1175 | size_t eSize; |
| 1176 | U32 magic = MEM_readLE32(dict); |
| 1177 | if (magic != ZSTD_DICT_MAGIC) { |
| 1178 | /* pure content mode */ |
| 1179 | ZSTD_refDictContent(dctx, dict, dictSize); |
| 1180 | return 0; |
| 1181 | } |
| 1182 | /* load entropy tables */ |
| 1183 | dict = (const char*)dict + 4; |
| 1184 | dictSize -= 4; |
| 1185 | eSize = ZSTD_loadEntropy(dctx, dict, dictSize); |
| 1186 | if (ZSTD_isError(eSize)) return ERROR(dictionary_corrupted); |
| 1187 | |
| 1188 | /* reference dictionary content */ |
| 1189 | dict = (const char*)dict + eSize; |
| 1190 | dictSize -= eSize; |
| 1191 | ZSTD_refDictContent(dctx, dict, dictSize); |
| 1192 | |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1196 | |
| 1197 | size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize) |
| 1198 | { |
| 1199 | size_t errorCode; |
| 1200 | errorCode = ZSTD_decompressBegin(dctx); |
| 1201 | if (ZSTD_isError(errorCode)) return errorCode; |
| 1202 | |
| 1203 | if (dict && dictSize) { |
| 1204 | errorCode = ZSTD_decompress_insertDictionary(dctx, dict, dictSize); |
| 1205 | if (ZSTD_isError(errorCode)) return ERROR(dictionary_corrupted); |
| 1206 | } |
| 1207 | |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |