1 1.1 christos /* 2 1.1 christos * Copyright (c) Yann Collet, Meta Platforms, Inc. and affiliates. 3 1.1 christos * All rights reserved. 4 1.1 christos * 5 1.1 christos * This source code is licensed under both the BSD-style license (found in the 6 1.1 christos * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7 1.1 christos * in the COPYING file in the root directory of this source tree). 8 1.1 christos * You may select, at your option, one of the above-listed licenses. 9 1.1 christos */ 10 1.1 christos 11 1.1 christos 12 1.1 christos /****************************************** 13 1.1 christos * Includes 14 1.1 christos ******************************************/ 15 1.1 christos #include <stddef.h> /* size_t, ptrdiff_t */ 16 1.1 christos #include <string.h> /* memcpy */ 17 1.1 christos 18 1.1 christos #include "zstd_v04.h" 19 1.1 christos #include "../common/compiler.h" 20 1.1 christos #include "../common/error_private.h" 21 1.1 christos 22 1.1 christos 23 1.1 christos /* ****************************************************************** 24 1.1 christos * mem.h 25 1.1 christos *******************************************************************/ 26 1.1 christos #ifndef MEM_H_MODULE 27 1.1 christos #define MEM_H_MODULE 28 1.1 christos 29 1.1 christos #if defined (__cplusplus) 30 1.1 christos extern "C" { 31 1.1 christos #endif 32 1.1 christos 33 1.1 christos 34 1.1 christos /****************************************** 35 1.1 christos * Compiler-specific 36 1.1 christos ******************************************/ 37 1.1 christos #if defined(_MSC_VER) /* Visual Studio */ 38 1.1 christos # include <stdlib.h> /* _byteswap_ulong */ 39 1.1 christos # include <intrin.h> /* _byteswap_* */ 40 1.1 christos #endif 41 1.1 christos 42 1.1 christos 43 1.1 christos /**************************************************************** 44 1.1 christos * Basic Types 45 1.1 christos *****************************************************************/ 46 1.1 christos #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) 47 1.1 christos # if defined(_AIX) 48 1.1 christos # include <inttypes.h> 49 1.1 christos # else 50 1.1 christos # include <stdint.h> /* intptr_t */ 51 1.1 christos # endif 52 1.1 christos typedef uint8_t BYTE; 53 1.1 christos typedef uint16_t U16; 54 1.1 christos typedef int16_t S16; 55 1.1 christos typedef uint32_t U32; 56 1.1 christos typedef int32_t S32; 57 1.1 christos typedef uint64_t U64; 58 1.1 christos typedef int64_t S64; 59 1.1 christos #else 60 1.1 christos typedef unsigned char BYTE; 61 1.1 christos typedef unsigned short U16; 62 1.1 christos typedef signed short S16; 63 1.1 christos typedef unsigned int U32; 64 1.1 christos typedef signed int S32; 65 1.1 christos typedef unsigned long long U64; 66 1.1 christos typedef signed long long S64; 67 1.1 christos #endif 68 1.1 christos 69 1.1 christos 70 1.1 christos /*-************************************* 71 1.1 christos * Debug 72 1.1 christos ***************************************/ 73 1.1 christos #include "../common/debug.h" 74 1.1 christos #ifndef assert 75 1.1 christos # define assert(condition) ((void)0) 76 1.1 christos #endif 77 1.1 christos 78 1.1 christos 79 1.1 christos /**************************************************************** 80 1.1 christos * Memory I/O 81 1.1 christos *****************************************************************/ 82 1.1 christos 83 1.1 christos MEM_STATIC unsigned MEM_32bits(void) { return sizeof(void*)==4; } 84 1.1 christos MEM_STATIC unsigned MEM_64bits(void) { return sizeof(void*)==8; } 85 1.1 christos 86 1.1 christos MEM_STATIC unsigned MEM_isLittleEndian(void) 87 1.1 christos { 88 1.1 christos const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ 89 1.1 christos return one.c[0]; 90 1.1 christos } 91 1.1 christos 92 1.1 christos MEM_STATIC U16 MEM_read16(const void* memPtr) 93 1.1 christos { 94 1.1 christos U16 val; memcpy(&val, memPtr, sizeof(val)); return val; 95 1.1 christos } 96 1.1 christos 97 1.1 christos MEM_STATIC U32 MEM_read32(const void* memPtr) 98 1.1 christos { 99 1.1 christos U32 val; memcpy(&val, memPtr, sizeof(val)); return val; 100 1.1 christos } 101 1.1 christos 102 1.1 christos MEM_STATIC U64 MEM_read64(const void* memPtr) 103 1.1 christos { 104 1.1 christos U64 val; memcpy(&val, memPtr, sizeof(val)); return val; 105 1.1 christos } 106 1.1 christos 107 1.1 christos MEM_STATIC void MEM_write16(void* memPtr, U16 value) 108 1.1 christos { 109 1.1 christos memcpy(memPtr, &value, sizeof(value)); 110 1.1 christos } 111 1.1 christos 112 1.1 christos MEM_STATIC U16 MEM_readLE16(const void* memPtr) 113 1.1 christos { 114 1.1 christos if (MEM_isLittleEndian()) 115 1.1 christos return MEM_read16(memPtr); 116 1.1 christos else 117 1.1 christos { 118 1.1 christos const BYTE* p = (const BYTE*)memPtr; 119 1.1 christos return (U16)(p[0] + (p[1]<<8)); 120 1.1 christos } 121 1.1 christos } 122 1.1 christos 123 1.1 christos MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val) 124 1.1 christos { 125 1.1 christos if (MEM_isLittleEndian()) 126 1.1 christos { 127 1.1 christos MEM_write16(memPtr, val); 128 1.1 christos } 129 1.1 christos else 130 1.1 christos { 131 1.1 christos BYTE* p = (BYTE*)memPtr; 132 1.1 christos p[0] = (BYTE)val; 133 1.1 christos p[1] = (BYTE)(val>>8); 134 1.1 christos } 135 1.1 christos } 136 1.1 christos 137 1.1 christos MEM_STATIC U32 MEM_readLE24(const void* memPtr) 138 1.1 christos { 139 1.1 christos return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16); 140 1.1 christos } 141 1.1 christos 142 1.1 christos MEM_STATIC U32 MEM_readLE32(const void* memPtr) 143 1.1 christos { 144 1.1 christos if (MEM_isLittleEndian()) 145 1.1 christos return MEM_read32(memPtr); 146 1.1 christos else 147 1.1 christos { 148 1.1 christos const BYTE* p = (const BYTE*)memPtr; 149 1.1 christos return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24)); 150 1.1 christos } 151 1.1 christos } 152 1.1 christos 153 1.1 christos 154 1.1 christos MEM_STATIC U64 MEM_readLE64(const void* memPtr) 155 1.1 christos { 156 1.1 christos if (MEM_isLittleEndian()) 157 1.1 christos return MEM_read64(memPtr); 158 1.1 christos else 159 1.1 christos { 160 1.1 christos const BYTE* p = (const BYTE*)memPtr; 161 1.1 christos return (U64)((U64)p[0] + ((U64)p[1]<<8) + ((U64)p[2]<<16) + ((U64)p[3]<<24) 162 1.1 christos + ((U64)p[4]<<32) + ((U64)p[5]<<40) + ((U64)p[6]<<48) + ((U64)p[7]<<56)); 163 1.1 christos } 164 1.1 christos } 165 1.1 christos 166 1.1 christos 167 1.1 christos MEM_STATIC size_t MEM_readLEST(const void* memPtr) 168 1.1 christos { 169 1.1 christos if (MEM_32bits()) 170 1.1 christos return (size_t)MEM_readLE32(memPtr); 171 1.1 christos else 172 1.1 christos return (size_t)MEM_readLE64(memPtr); 173 1.1 christos } 174 1.1 christos 175 1.1 christos 176 1.1 christos #if defined (__cplusplus) 177 1.1 christos } 178 1.1 christos #endif 179 1.1 christos 180 1.1 christos #endif /* MEM_H_MODULE */ 181 1.1 christos 182 1.1 christos /* 183 1.1 christos zstd - standard compression library 184 1.1 christos Header File for static linking only 185 1.1 christos */ 186 1.1 christos #ifndef ZSTD_STATIC_H 187 1.1 christos #define ZSTD_STATIC_H 188 1.1 christos 189 1.1 christos 190 1.1 christos /* ************************************* 191 1.1 christos * Types 192 1.1 christos ***************************************/ 193 1.1 christos #define ZSTD_WINDOWLOG_ABSOLUTEMIN 11 194 1.1 christos 195 1.1 christos /** from faster to stronger */ 196 1.1 christos typedef enum { ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2 } ZSTD_strategy; 197 1.1 christos 198 1.1 christos typedef struct 199 1.1 christos { 200 1.1 christos U64 srcSize; /* optional : tells how much bytes are present in the frame. Use 0 if not known. */ 201 1.1 christos U32 windowLog; /* largest match distance : larger == more compression, more memory needed during decompression */ 202 1.1 christos U32 contentLog; /* full search segment : larger == more compression, slower, more memory (useless for fast) */ 203 1.1 christos U32 hashLog; /* dispatch table : larger == more memory, faster */ 204 1.1 christos U32 searchLog; /* nb of searches : larger == more compression, slower */ 205 1.1 christos U32 searchLength; /* size of matches : larger == faster decompression, sometimes less compression */ 206 1.1 christos ZSTD_strategy strategy; 207 1.1 christos } ZSTD_parameters; 208 1.1 christos 209 1.1 christos typedef ZSTDv04_Dctx ZSTD_DCtx; 210 1.1 christos 211 1.1 christos /* ************************************* 212 1.1 christos * Advanced functions 213 1.1 christos ***************************************/ 214 1.1 christos /** ZSTD_decompress_usingDict 215 1.1 christos * Same as ZSTD_decompressDCtx, using a Dictionary content as prefix 216 1.1 christos * Note : dict can be NULL, in which case, it's equivalent to ZSTD_decompressDCtx() */ 217 1.1 christos static size_t ZSTD_decompress_usingDict(ZSTD_DCtx* ctx, 218 1.1 christos void* dst, size_t maxDstSize, 219 1.1 christos const void* src, size_t srcSize, 220 1.1 christos const void* dict,size_t dictSize); 221 1.1 christos 222 1.1 christos 223 1.1 christos /* ************************************** 224 1.1 christos * Streaming functions (direct mode) 225 1.1 christos ****************************************/ 226 1.1 christos static size_t ZSTD_resetDCtx(ZSTD_DCtx* dctx); 227 1.1 christos static size_t ZSTD_getFrameParams(ZSTD_parameters* params, const void* src, size_t srcSize); 228 1.1 christos static void ZSTD_decompress_insertDictionary(ZSTD_DCtx* ctx, const void* src, size_t srcSize); 229 1.1 christos 230 1.1 christos static size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx); 231 1.1 christos static size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize); 232 1.1 christos 233 1.1 christos /** 234 1.1 christos Streaming decompression, bufferless mode 235 1.1 christos 236 1.1 christos A ZSTD_DCtx object is required to track streaming operations. 237 1.1 christos Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. 238 1.1 christos A ZSTD_DCtx object can be re-used multiple times. Use ZSTD_resetDCtx() to return to fresh status. 239 1.1 christos 240 1.1 christos First operation is to retrieve frame parameters, using ZSTD_getFrameParams(). 241 1.1 christos This function doesn't consume its input. It needs enough input data to properly decode the frame header. 242 1.1 christos Objective is to retrieve *params.windowlog, to know minimum amount of memory required during decoding. 243 1.1 christos Result : 0 when successful, it means the ZSTD_parameters structure has been filled. 244 1.1 christos >0 : means there is not enough data into src. Provides the expected size to successfully decode header. 245 1.1 christos errorCode, which can be tested using ZSTD_isError() (For example, if it's not a ZSTD header) 246 1.1 christos 247 1.1 christos Then, you can optionally insert a dictionary. 248 1.1 christos This operation must mimic the compressor behavior, otherwise decompression will fail or be corrupted. 249 1.1 christos 250 1.1 christos Then it's possible to start decompression. 251 1.1 christos Use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. 252 1.1 christos ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue(). 253 1.1 christos ZSTD_decompressContinue() requires this exact amount of bytes, or it will fail. 254 1.1 christos ZSTD_decompressContinue() needs previous data blocks during decompression, up to (1 << windowlog). 255 1.1 christos They should preferably be located contiguously, prior to current block. Alternatively, a round buffer is also possible. 256 1.1 christos 257 1.1 christos @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst'. 258 1.1 christos It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header. 259 1.1 christos 260 1.1 christos A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. 261 1.1 christos Context can then be reset to start a new decompression. 262 1.1 christos */ 263 1.1 christos 264 1.1 christos 265 1.1 christos 266 1.1 christos 267 1.1 christos #endif /* ZSTD_STATIC_H */ 268 1.1 christos 269 1.1 christos 270 1.1 christos /* 271 1.1 christos zstd_internal - common functions to include 272 1.1 christos Header File for include 273 1.1 christos */ 274 1.1 christos #ifndef ZSTD_CCOMMON_H_MODULE 275 1.1 christos #define ZSTD_CCOMMON_H_MODULE 276 1.1 christos 277 1.1 christos /* ************************************* 278 1.1 christos * Common macros 279 1.1 christos ***************************************/ 280 1.1 christos #define MIN(a,b) ((a)<(b) ? (a) : (b)) 281 1.1 christos #define MAX(a,b) ((a)>(b) ? (a) : (b)) 282 1.1 christos 283 1.1 christos 284 1.1 christos /* ************************************* 285 1.1 christos * Common constants 286 1.1 christos ***************************************/ 287 1.1 christos #define ZSTD_MAGICNUMBER 0xFD2FB524 /* v0.4 */ 288 1.1 christos 289 1.1 christos #define KB *(1 <<10) 290 1.1 christos #define MB *(1 <<20) 291 1.1 christos #define GB *(1U<<30) 292 1.1 christos 293 1.1 christos #define BLOCKSIZE (128 KB) /* define, for static allocation */ 294 1.1 christos 295 1.1 christos static const size_t ZSTD_blockHeaderSize = 3; 296 1.1 christos static const size_t ZSTD_frameHeaderSize_min = 5; 297 1.1 christos #define ZSTD_frameHeaderSize_max 5 /* define, for static allocation */ 298 1.1 christos 299 1.1 christos #define BIT7 128 300 1.1 christos #define BIT6 64 301 1.1 christos #define BIT5 32 302 1.1 christos #define BIT4 16 303 1.1 christos #define BIT1 2 304 1.1 christos #define BIT0 1 305 1.1 christos 306 1.1 christos #define IS_RAW BIT0 307 1.1 christos #define IS_RLE BIT1 308 1.1 christos 309 1.1 christos #define MINMATCH 4 310 1.1 christos #define REPCODE_STARTVALUE 4 311 1.1 christos 312 1.1 christos #define MLbits 7 313 1.1 christos #define LLbits 6 314 1.1 christos #define Offbits 5 315 1.1 christos #define MaxML ((1<<MLbits) - 1) 316 1.1 christos #define MaxLL ((1<<LLbits) - 1) 317 1.1 christos #define MaxOff ((1<<Offbits)- 1) 318 1.1 christos #define MLFSELog 10 319 1.1 christos #define LLFSELog 10 320 1.1 christos #define OffFSELog 9 321 1.1 christos #define MaxSeq MAX(MaxLL, MaxML) 322 1.1 christos 323 1.1 christos #define MIN_SEQUENCES_SIZE (2 /*seqNb*/ + 2 /*dumps*/ + 3 /*seqTables*/ + 1 /*bitStream*/) 324 1.1 christos #define MIN_CBLOCK_SIZE (3 /*litCSize*/ + MIN_SEQUENCES_SIZE) 325 1.1 christos 326 1.1 christos #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) 327 1.1 christos 328 1.1 christos typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t; 329 1.1 christos 330 1.1 christos 331 1.1 christos /* ****************************************** 332 1.1 christos * Shared functions to include for inlining 333 1.1 christos ********************************************/ 334 1.1 christos static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); } 335 1.1 christos 336 1.1 christos #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; } 337 1.1 christos 338 1.1 christos /*! ZSTD_wildcopy : custom version of memcpy(), can copy up to 7-8 bytes too many */ 339 1.1 christos static void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length) 340 1.1 christos { 341 1.1 christos const BYTE* ip = (const BYTE*)src; 342 1.1 christos BYTE* op = (BYTE*)dst; 343 1.1 christos BYTE* const oend = op + length; 344 1.1 christos do 345 1.1 christos COPY8(op, ip) 346 1.1 christos while (op < oend); 347 1.1 christos } 348 1.1 christos 349 1.1 christos 350 1.1 christos 351 1.1 christos /* ****************************************************************** 352 1.1 christos FSE : Finite State Entropy coder 353 1.1 christos header file 354 1.1 christos ****************************************************************** */ 355 1.1 christos #ifndef FSE_H 356 1.1 christos #define FSE_H 357 1.1 christos 358 1.1 christos #if defined (__cplusplus) 359 1.1 christos extern "C" { 360 1.1 christos #endif 361 1.1 christos 362 1.1 christos 363 1.1 christos /* ***************************************** 364 1.1 christos * Includes 365 1.1 christos ******************************************/ 366 1.1 christos #include <stddef.h> /* size_t, ptrdiff_t */ 367 1.1 christos 368 1.1 christos 369 1.1 christos /* ***************************************** 370 1.1 christos * FSE simple functions 371 1.1 christos ******************************************/ 372 1.1 christos static size_t FSE_decompress(void* dst, size_t maxDstSize, 373 1.1 christos const void* cSrc, size_t cSrcSize); 374 1.1 christos /*! 375 1.1 christos FSE_decompress(): 376 1.1 christos Decompress FSE data from buffer 'cSrc', of size 'cSrcSize', 377 1.1 christos into already allocated destination buffer 'dst', of size 'maxDstSize'. 378 1.1 christos return : size of regenerated data (<= maxDstSize) 379 1.1 christos or an error code, which can be tested using FSE_isError() 380 1.1 christos 381 1.1 christos ** Important ** : FSE_decompress() doesn't decompress non-compressible nor RLE data !!! 382 1.1 christos Why ? : making this distinction requires a header. 383 1.1 christos Header management is intentionally delegated to the user layer, which can better manage special cases. 384 1.1 christos */ 385 1.1 christos 386 1.1 christos 387 1.1 christos /* ***************************************** 388 1.1 christos * Tool functions 389 1.1 christos ******************************************/ 390 1.1 christos /* Error Management */ 391 1.1 christos static unsigned FSE_isError(size_t code); /* tells if a return value is an error code */ 392 1.1 christos 393 1.1 christos 394 1.1 christos 395 1.1 christos /* ***************************************** 396 1.1 christos * FSE detailed API 397 1.1 christos ******************************************/ 398 1.1 christos /*! 399 1.1 christos FSE_compress() does the following: 400 1.1 christos 1. count symbol occurrence from source[] into table count[] 401 1.1 christos 2. normalize counters so that sum(count[]) == Power_of_2 (2^tableLog) 402 1.1 christos 3. save normalized counters to memory buffer using writeNCount() 403 1.1 christos 4. build encoding table 'CTable' from normalized counters 404 1.1 christos 5. encode the data stream using encoding table 'CTable' 405 1.1 christos 406 1.1 christos FSE_decompress() does the following: 407 1.1 christos 1. read normalized counters with readNCount() 408 1.1 christos 2. build decoding table 'DTable' from normalized counters 409 1.1 christos 3. decode the data stream using decoding table 'DTable' 410 1.1 christos 411 1.1 christos The following API allows targeting specific sub-functions for advanced tasks. 412 1.1 christos For example, it's possible to compress several blocks using the same 'CTable', 413 1.1 christos or to save and provide normalized distribution using external method. 414 1.1 christos */ 415 1.1 christos 416 1.1 christos 417 1.1 christos /* *** DECOMPRESSION *** */ 418 1.1 christos 419 1.1 christos /*! 420 1.1 christos FSE_readNCount(): 421 1.1 christos Read compactly saved 'normalizedCounter' from 'rBuffer'. 422 1.1 christos return : size read from 'rBuffer' 423 1.1 christos or an errorCode, which can be tested using FSE_isError() 424 1.1 christos maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */ 425 1.1 christos static size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, const void* rBuffer, size_t rBuffSize); 426 1.1 christos 427 1.1 christos /*! 428 1.1 christos Constructor and Destructor of type FSE_DTable 429 1.1 christos Note that its size depends on 'tableLog' */ 430 1.1 christos typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */ 431 1.1 christos 432 1.1 christos /*! 433 1.1 christos FSE_buildDTable(): 434 1.1 christos Builds 'dt', which must be already allocated, using FSE_createDTable() 435 1.1 christos return : 0, 436 1.1 christos or an errorCode, which can be tested using FSE_isError() */ 437 1.1 christos static size_t FSE_buildDTable ( FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); 438 1.1 christos 439 1.1 christos /*! 440 1.1 christos FSE_decompress_usingDTable(): 441 1.1 christos Decompress compressed source 'cSrc' of size 'cSrcSize' using 'dt' 442 1.1 christos into 'dst' which must be already allocated. 443 1.1 christos return : size of regenerated data (necessarily <= maxDstSize) 444 1.1 christos or an errorCode, which can be tested using FSE_isError() */ 445 1.1 christos static size_t FSE_decompress_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt); 446 1.1 christos 447 1.1 christos /*! 448 1.1 christos Tutorial : 449 1.1 christos ---------- 450 1.1 christos (Note : these functions only decompress FSE-compressed blocks. 451 1.1 christos If block is uncompressed, use memcpy() instead 452 1.1 christos If block is a single repeated byte, use memset() instead ) 453 1.1 christos 454 1.1 christos The first step is to obtain the normalized frequencies of symbols. 455 1.1 christos This can be performed by FSE_readNCount() if it was saved using FSE_writeNCount(). 456 1.1 christos 'normalizedCounter' must be already allocated, and have at least 'maxSymbolValuePtr[0]+1' cells of signed short. 457 1.1 christos In practice, that means it's necessary to know 'maxSymbolValue' beforehand, 458 1.1 christos or size the table to handle worst case situations (typically 256). 459 1.1 christos FSE_readNCount() will provide 'tableLog' and 'maxSymbolValue'. 460 1.1 christos The result of FSE_readNCount() is the number of bytes read from 'rBuffer'. 461 1.1 christos Note that 'rBufferSize' must be at least 4 bytes, even if useful information is less than that. 462 1.1 christos If there is an error, the function will return an error code, which can be tested using FSE_isError(). 463 1.1 christos 464 1.1 christos The next step is to build the decompression tables 'FSE_DTable' from 'normalizedCounter'. 465 1.1 christos This is performed by the function FSE_buildDTable(). 466 1.1 christos The space required by 'FSE_DTable' must be already allocated using FSE_createDTable(). 467 1.1 christos If there is an error, the function will return an error code, which can be tested using FSE_isError(). 468 1.1 christos 469 1.1 christos 'FSE_DTable' can then be used to decompress 'cSrc', with FSE_decompress_usingDTable(). 470 1.1 christos 'cSrcSize' must be strictly correct, otherwise decompression will fail. 471 1.1 christos FSE_decompress_usingDTable() result will tell how many bytes were regenerated (<=maxDstSize). 472 1.1 christos If there is an error, the function will return an error code, which can be tested using FSE_isError(). (ex: dst buffer too small) 473 1.1 christos */ 474 1.1 christos 475 1.1 christos 476 1.1 christos #if defined (__cplusplus) 477 1.1 christos } 478 1.1 christos #endif 479 1.1 christos 480 1.1 christos #endif /* FSE_H */ 481 1.1 christos 482 1.1 christos 483 1.1 christos /* ****************************************************************** 484 1.1 christos bitstream 485 1.1 christos Part of NewGen Entropy library 486 1.1 christos header file (to include) 487 1.1 christos Copyright (C) 2013-2015, Yann Collet. 488 1.1 christos 489 1.1 christos BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) 490 1.1 christos 491 1.1 christos Redistribution and use in source and binary forms, with or without 492 1.1 christos modification, are permitted provided that the following conditions are 493 1.1 christos met: 494 1.1 christos 495 1.1 christos * Redistributions of source code must retain the above copyright 496 1.1 christos notice, this list of conditions and the following disclaimer. 497 1.1 christos * Redistributions in binary form must reproduce the above 498 1.1 christos copyright notice, this list of conditions and the following disclaimer 499 1.1 christos in the documentation and/or other materials provided with the 500 1.1 christos distribution. 501 1.1 christos 502 1.1 christos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 503 1.1 christos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 504 1.1 christos LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 505 1.1 christos A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 506 1.1 christos OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 507 1.1 christos SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 508 1.1 christos LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 509 1.1 christos DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 510 1.1 christos THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 511 1.1 christos (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 512 1.1 christos OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 513 1.1 christos 514 1.1 christos You can contact the author at : 515 1.1 christos - Source repository : https://github.com/Cyan4973/FiniteStateEntropy 516 1.1 christos - Public forum : https://groups.google.com/forum/#!forum/lz4c 517 1.1 christos ****************************************************************** */ 518 1.1 christos #ifndef BITSTREAM_H_MODULE 519 1.1 christos #define BITSTREAM_H_MODULE 520 1.1 christos 521 1.1 christos #if defined (__cplusplus) 522 1.1 christos extern "C" { 523 1.1 christos #endif 524 1.1 christos 525 1.1 christos 526 1.1 christos /* 527 1.1 christos * This API consists of small unitary functions, which highly benefit from being inlined. 528 1.1 christos * Since link-time-optimization is not available for all compilers, 529 1.1 christos * these functions are defined into a .h to be included. 530 1.1 christos */ 531 1.1 christos 532 1.1 christos /********************************************** 533 1.1 christos * bitStream decompression API (read backward) 534 1.1 christos **********************************************/ 535 1.1 christos typedef struct 536 1.1 christos { 537 1.1 christos size_t bitContainer; 538 1.1 christos unsigned bitsConsumed; 539 1.1 christos const char* ptr; 540 1.1 christos const char* start; 541 1.1 christos } BIT_DStream_t; 542 1.1 christos 543 1.1 christos typedef enum { BIT_DStream_unfinished = 0, 544 1.1 christos BIT_DStream_endOfBuffer = 1, 545 1.1 christos BIT_DStream_completed = 2, 546 1.1 christos BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */ 547 1.1 christos /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */ 548 1.1 christos 549 1.1 christos MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize); 550 1.1 christos MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits); 551 1.1 christos MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD); 552 1.1 christos MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD); 553 1.1 christos 554 1.1 christos 555 1.1 christos 556 1.1 christos 557 1.1 christos /****************************************** 558 1.1 christos * unsafe API 559 1.1 christos ******************************************/ 560 1.1 christos MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits); 561 1.1 christos /* faster, but works only if nbBits >= 1 */ 562 1.1 christos 563 1.1 christos 564 1.1 christos 565 1.1 christos /**************************************************************** 566 1.1 christos * Helper functions 567 1.1 christos ****************************************************************/ 568 1.1 christos MEM_STATIC unsigned BIT_highbit32 (U32 val) 569 1.1 christos { 570 1.1 christos # if defined(_MSC_VER) /* Visual */ 571 1.1 christos unsigned long r; 572 1.1 christos return _BitScanReverse(&r, val) ? (unsigned)r : 0; 573 1.1 christos # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */ 574 1.1 christos return __builtin_clz (val) ^ 31; 575 1.1 christos # else /* Software version */ 576 1.1 christos static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 }; 577 1.1 christos U32 v = val; 578 1.1 christos unsigned r; 579 1.1 christos v |= v >> 1; 580 1.1 christos v |= v >> 2; 581 1.1 christos v |= v >> 4; 582 1.1 christos v |= v >> 8; 583 1.1 christos v |= v >> 16; 584 1.1 christos r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27]; 585 1.1 christos return r; 586 1.1 christos # endif 587 1.1 christos } 588 1.1 christos 589 1.1 christos 590 1.1 christos /********************************************************** 591 1.1 christos * bitStream decoding 592 1.1 christos **********************************************************/ 593 1.1 christos 594 1.1 christos /*!BIT_initDStream 595 1.1 christos * Initialize a BIT_DStream_t. 596 1.1 christos * @bitD : a pointer to an already allocated BIT_DStream_t structure 597 1.1 christos * @srcBuffer must point at the beginning of a bitStream 598 1.1 christos * @srcSize must be the exact size of the bitStream 599 1.1 christos * @result : size of stream (== srcSize) or an errorCode if a problem is detected 600 1.1 christos */ 601 1.1 christos MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize) 602 1.1 christos { 603 1.1 christos if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); } 604 1.1 christos 605 1.1 christos if (srcSize >= sizeof(size_t)) /* normal case */ 606 1.1 christos { 607 1.1 christos U32 contain32; 608 1.1 christos bitD->start = (const char*)srcBuffer; 609 1.1 christos bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t); 610 1.1 christos bitD->bitContainer = MEM_readLEST(bitD->ptr); 611 1.1 christos contain32 = ((const BYTE*)srcBuffer)[srcSize-1]; 612 1.1 christos if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */ 613 1.1 christos bitD->bitsConsumed = 8 - BIT_highbit32(contain32); 614 1.1 christos } 615 1.1 christos else 616 1.1 christos { 617 1.1 christos U32 contain32; 618 1.1 christos bitD->start = (const char*)srcBuffer; 619 1.1 christos bitD->ptr = bitD->start; 620 1.1 christos bitD->bitContainer = *(const BYTE*)(bitD->start); 621 1.1 christos switch(srcSize) 622 1.1 christos { 623 1.1 christos case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);/* fall-through */ 624 1.1 christos case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);/* fall-through */ 625 1.1 christos case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);/* fall-through */ 626 1.1 christos case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24; /* fall-through */ 627 1.1 christos case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16; /* fall-through */ 628 1.1 christos case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8; /* fall-through */ 629 1.1 christos default: break; 630 1.1 christos } 631 1.1 christos contain32 = ((const BYTE*)srcBuffer)[srcSize-1]; 632 1.1 christos if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */ 633 1.1 christos bitD->bitsConsumed = 8 - BIT_highbit32(contain32); 634 1.1 christos bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8; 635 1.1 christos } 636 1.1 christos 637 1.1 christos return srcSize; 638 1.1 christos } 639 1.1 christos 640 1.1 christos MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits) 641 1.1 christos { 642 1.1 christos const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1; 643 1.1 christos return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask); 644 1.1 christos } 645 1.1 christos 646 1.1 christos /*! BIT_lookBitsFast : 647 1.1 christos * unsafe version; only works if nbBits >= 1 */ 648 1.1 christos MEM_STATIC size_t BIT_lookBitsFast(BIT_DStream_t* bitD, U32 nbBits) 649 1.1 christos { 650 1.1 christos const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1; 651 1.1 christos return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask); 652 1.1 christos } 653 1.1 christos 654 1.1 christos MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits) 655 1.1 christos { 656 1.1 christos bitD->bitsConsumed += nbBits; 657 1.1 christos } 658 1.1 christos 659 1.1 christos MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits) 660 1.1 christos { 661 1.1 christos size_t value = BIT_lookBits(bitD, nbBits); 662 1.1 christos BIT_skipBits(bitD, nbBits); 663 1.1 christos return value; 664 1.1 christos } 665 1.1 christos 666 1.1 christos /*!BIT_readBitsFast : 667 1.1 christos * unsafe version; only works if nbBits >= 1 */ 668 1.1 christos MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits) 669 1.1 christos { 670 1.1 christos size_t value = BIT_lookBitsFast(bitD, nbBits); 671 1.1 christos BIT_skipBits(bitD, nbBits); 672 1.1 christos return value; 673 1.1 christos } 674 1.1 christos 675 1.1 christos MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD) 676 1.1 christos { 677 1.1 christos if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */ 678 1.1 christos return BIT_DStream_overflow; 679 1.1 christos 680 1.1 christos if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) 681 1.1 christos { 682 1.1 christos bitD->ptr -= bitD->bitsConsumed >> 3; 683 1.1 christos bitD->bitsConsumed &= 7; 684 1.1 christos bitD->bitContainer = MEM_readLEST(bitD->ptr); 685 1.1 christos return BIT_DStream_unfinished; 686 1.1 christos } 687 1.1 christos if (bitD->ptr == bitD->start) 688 1.1 christos { 689 1.1 christos if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer; 690 1.1 christos return BIT_DStream_completed; 691 1.1 christos } 692 1.1 christos { 693 1.1 christos U32 nbBytes = bitD->bitsConsumed >> 3; 694 1.1 christos BIT_DStream_status result = BIT_DStream_unfinished; 695 1.1 christos if (bitD->ptr - nbBytes < bitD->start) 696 1.1 christos { 697 1.1 christos nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ 698 1.1 christos result = BIT_DStream_endOfBuffer; 699 1.1 christos } 700 1.1 christos bitD->ptr -= nbBytes; 701 1.1 christos bitD->bitsConsumed -= nbBytes*8; 702 1.1 christos bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */ 703 1.1 christos return result; 704 1.1 christos } 705 1.1 christos } 706 1.1 christos 707 1.1 christos /*! BIT_endOfDStream 708 1.1 christos * @return Tells if DStream has reached its exact end 709 1.1 christos */ 710 1.1 christos MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream) 711 1.1 christos { 712 1.1 christos return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8)); 713 1.1 christos } 714 1.1 christos 715 1.1 christos #if defined (__cplusplus) 716 1.1 christos } 717 1.1 christos #endif 718 1.1 christos 719 1.1 christos #endif /* BITSTREAM_H_MODULE */ 720 1.1 christos 721 1.1 christos 722 1.1 christos 723 1.1 christos /* ****************************************************************** 724 1.1 christos FSE : Finite State Entropy coder 725 1.1 christos header file for static linking (only) 726 1.1 christos Copyright (C) 2013-2015, Yann Collet 727 1.1 christos 728 1.1 christos BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) 729 1.1 christos 730 1.1 christos Redistribution and use in source and binary forms, with or without 731 1.1 christos modification, are permitted provided that the following conditions are 732 1.1 christos met: 733 1.1 christos 734 1.1 christos * Redistributions of source code must retain the above copyright 735 1.1 christos notice, this list of conditions and the following disclaimer. 736 1.1 christos * Redistributions in binary form must reproduce the above 737 1.1 christos copyright notice, this list of conditions and the following disclaimer 738 1.1 christos in the documentation and/or other materials provided with the 739 1.1 christos distribution. 740 1.1 christos 741 1.1 christos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 742 1.1 christos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 743 1.1 christos LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 744 1.1 christos A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 745 1.1 christos OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 746 1.1 christos SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 747 1.1 christos LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 748 1.1 christos DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 749 1.1 christos THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 750 1.1 christos (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 751 1.1 christos OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 752 1.1 christos 753 1.1 christos You can contact the author at : 754 1.1 christos - Source repository : https://github.com/Cyan4973/FiniteStateEntropy 755 1.1 christos - Public forum : https://groups.google.com/forum/#!forum/lz4c 756 1.1 christos ****************************************************************** */ 757 1.1 christos #ifndef FSE_STATIC_H 758 1.1 christos #define FSE_STATIC_H 759 1.1 christos 760 1.1 christos #if defined (__cplusplus) 761 1.1 christos extern "C" { 762 1.1 christos #endif 763 1.1 christos 764 1.1 christos 765 1.1 christos /* ***************************************** 766 1.1 christos * Static allocation 767 1.1 christos *******************************************/ 768 1.1 christos /* FSE buffer bounds */ 769 1.1 christos #define FSE_NCOUNTBOUND 512 770 1.1 christos #define FSE_BLOCKBOUND(size) (size + (size>>7)) 771 1.1 christos #define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ 772 1.1 christos 773 1.1 christos /* It is possible to statically allocate FSE CTable/DTable as a table of unsigned using below macros */ 774 1.1 christos #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2)) 775 1.1 christos #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog)) 776 1.1 christos 777 1.1 christos 778 1.1 christos /* ***************************************** 779 1.1 christos * FSE advanced API 780 1.1 christos *******************************************/ 781 1.1 christos static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits); 782 1.1 christos /* build a fake FSE_DTable, designed to read an uncompressed bitstream where each symbol uses nbBits */ 783 1.1 christos 784 1.1 christos static size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue); 785 1.1 christos /* build a fake FSE_DTable, designed to always generate the same symbolValue */ 786 1.1 christos 787 1.1 christos 788 1.1 christos 789 1.1 christos /* ***************************************** 790 1.1 christos * FSE symbol decompression API 791 1.1 christos *******************************************/ 792 1.1 christos typedef struct 793 1.1 christos { 794 1.1 christos size_t state; 795 1.1 christos const void* table; /* precise table may vary, depending on U16 */ 796 1.1 christos } FSE_DState_t; 797 1.1 christos 798 1.1 christos 799 1.1 christos static void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt); 800 1.1 christos 801 1.1 christos static unsigned char FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD); 802 1.1 christos 803 1.1 christos static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr); 804 1.1 christos 805 1.1 christos 806 1.1 christos /* ***************************************** 807 1.1 christos * FSE unsafe API 808 1.1 christos *******************************************/ 809 1.1 christos static unsigned char FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD); 810 1.1 christos /* faster, but works only if nbBits is always >= 1 (otherwise, result will be corrupted) */ 811 1.1 christos 812 1.1 christos 813 1.1 christos /* ***************************************** 814 1.1 christos * Implementation of inlined functions 815 1.1 christos *******************************************/ 816 1.1 christos /* decompression */ 817 1.1 christos 818 1.1 christos typedef struct { 819 1.1 christos U16 tableLog; 820 1.1 christos U16 fastMode; 821 1.1 christos } FSE_DTableHeader; /* sizeof U32 */ 822 1.1 christos 823 1.1 christos typedef struct 824 1.1 christos { 825 1.1 christos unsigned short newState; 826 1.1 christos unsigned char symbol; 827 1.1 christos unsigned char nbBits; 828 1.1 christos } FSE_decode_t; /* size == U32 */ 829 1.1 christos 830 1.1 christos MEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, const FSE_DTable* dt) 831 1.1 christos { 832 1.1 christos FSE_DTableHeader DTableH; 833 1.1 christos memcpy(&DTableH, dt, sizeof(DTableH)); 834 1.1 christos DStatePtr->state = BIT_readBits(bitD, DTableH.tableLog); 835 1.1 christos BIT_reloadDStream(bitD); 836 1.1 christos DStatePtr->table = dt + 1; 837 1.1 christos } 838 1.1 christos 839 1.1 christos MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) 840 1.1 christos { 841 1.1 christos const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; 842 1.1 christos const U32 nbBits = DInfo.nbBits; 843 1.1 christos BYTE symbol = DInfo.symbol; 844 1.1 christos size_t lowBits = BIT_readBits(bitD, nbBits); 845 1.1 christos 846 1.1 christos DStatePtr->state = DInfo.newState + lowBits; 847 1.1 christos return symbol; 848 1.1 christos } 849 1.1 christos 850 1.1 christos MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) 851 1.1 christos { 852 1.1 christos const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; 853 1.1 christos const U32 nbBits = DInfo.nbBits; 854 1.1 christos BYTE symbol = DInfo.symbol; 855 1.1 christos size_t lowBits = BIT_readBitsFast(bitD, nbBits); 856 1.1 christos 857 1.1 christos DStatePtr->state = DInfo.newState + lowBits; 858 1.1 christos return symbol; 859 1.1 christos } 860 1.1 christos 861 1.1 christos MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr) 862 1.1 christos { 863 1.1 christos return DStatePtr->state == 0; 864 1.1 christos } 865 1.1 christos 866 1.1 christos 867 1.1 christos #if defined (__cplusplus) 868 1.1 christos } 869 1.1 christos #endif 870 1.1 christos 871 1.1 christos #endif /* FSE_STATIC_H */ 872 1.1 christos 873 1.1 christos /* ****************************************************************** 874 1.1 christos FSE : Finite State Entropy coder 875 1.1 christos Copyright (C) 2013-2015, Yann Collet. 876 1.1 christos 877 1.1 christos BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) 878 1.1 christos 879 1.1 christos Redistribution and use in source and binary forms, with or without 880 1.1 christos modification, are permitted provided that the following conditions are 881 1.1 christos met: 882 1.1 christos 883 1.1 christos * Redistributions of source code must retain the above copyright 884 1.1 christos notice, this list of conditions and the following disclaimer. 885 1.1 christos * Redistributions in binary form must reproduce the above 886 1.1 christos copyright notice, this list of conditions and the following disclaimer 887 1.1 christos in the documentation and/or other materials provided with the 888 1.1 christos distribution. 889 1.1 christos 890 1.1 christos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 891 1.1 christos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 892 1.1 christos LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 893 1.1 christos A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 894 1.1 christos OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 895 1.1 christos SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 896 1.1 christos LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 897 1.1 christos DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 898 1.1 christos THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 899 1.1 christos (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 900 1.1 christos OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 901 1.1 christos 902 1.1 christos You can contact the author at : 903 1.1 christos - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy 904 1.1 christos - Public forum : https://groups.google.com/forum/#!forum/lz4c 905 1.1 christos ****************************************************************** */ 906 1.1 christos 907 1.1 christos #ifndef FSE_COMMONDEFS_ONLY 908 1.1 christos 909 1.1 christos /* ************************************************************** 910 1.1 christos * Tuning parameters 911 1.1 christos ****************************************************************/ 912 1.1 christos /*!MEMORY_USAGE : 913 1.1 christos * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) 914 1.1 christos * Increasing memory usage improves compression ratio 915 1.1 christos * Reduced memory usage can improve speed, due to cache effect 916 1.1 christos * Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */ 917 1.1 christos #define FSE_MAX_MEMORY_USAGE 14 918 1.1 christos #define FSE_DEFAULT_MEMORY_USAGE 13 919 1.1 christos 920 1.1 christos /*!FSE_MAX_SYMBOL_VALUE : 921 1.1 christos * Maximum symbol value authorized. 922 1.1 christos * Required for proper stack allocation */ 923 1.1 christos #define FSE_MAX_SYMBOL_VALUE 255 924 1.1 christos 925 1.1 christos 926 1.1 christos /* ************************************************************** 927 1.1 christos * template functions type & suffix 928 1.1 christos ****************************************************************/ 929 1.1 christos #define FSE_FUNCTION_TYPE BYTE 930 1.1 christos #define FSE_FUNCTION_EXTENSION 931 1.1 christos #define FSE_DECODE_TYPE FSE_decode_t 932 1.1 christos 933 1.1 christos 934 1.1 christos #endif /* !FSE_COMMONDEFS_ONLY */ 935 1.1 christos 936 1.1 christos /* ************************************************************** 937 1.1 christos * Compiler specifics 938 1.1 christos ****************************************************************/ 939 1.1 christos #ifdef _MSC_VER /* Visual Studio */ 940 1.1 christos # define FORCE_INLINE static __forceinline 941 1.1 christos # include <intrin.h> /* For Visual 2005 */ 942 1.1 christos # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ 943 1.1 christos # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */ 944 1.1 christos #else 945 1.1 christos # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ 946 1.1 christos # ifdef __GNUC__ 947 1.1 christos # define FORCE_INLINE static inline __attribute__((always_inline)) 948 1.1 christos # else 949 1.1 christos # define FORCE_INLINE static inline 950 1.1 christos # endif 951 1.1 christos # else 952 1.1 christos # define FORCE_INLINE static 953 1.1 christos # endif /* __STDC_VERSION__ */ 954 1.1 christos #endif 955 1.1 christos 956 1.1 christos 957 1.1 christos /* ************************************************************** 958 1.1 christos * Dependencies 959 1.1 christos ****************************************************************/ 960 1.1 christos #include <stdlib.h> /* malloc, free, qsort */ 961 1.1 christos #include <string.h> /* memcpy, memset */ 962 1.1 christos #include <stdio.h> /* printf (debug) */ 963 1.1 christos 964 1.1 christos 965 1.1 christos /* *************************************************************** 966 1.1 christos * Constants 967 1.1 christos *****************************************************************/ 968 1.1 christos #define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2) 969 1.1 christos #define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG) 970 1.1 christos #define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1) 971 1.1 christos #define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2) 972 1.1 christos #define FSE_MIN_TABLELOG 5 973 1.1 christos 974 1.1 christos #define FSE_TABLELOG_ABSOLUTE_MAX 15 975 1.1 christos #if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX 976 1.1 christos #error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported" 977 1.1 christos #endif 978 1.1 christos 979 1.1 christos 980 1.1 christos /* ************************************************************** 981 1.1 christos * Error Management 982 1.1 christos ****************************************************************/ 983 1.1 christos #define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ 984 1.1 christos 985 1.1 christos 986 1.1 christos /* ************************************************************** 987 1.1 christos * Complex types 988 1.1 christos ****************************************************************/ 989 1.1 christos typedef U32 DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)]; 990 1.1 christos 991 1.1 christos 992 1.1 christos /*-************************************************************** 993 1.1 christos * Templates 994 1.1 christos ****************************************************************/ 995 1.1 christos /* 996 1.1 christos designed to be included 997 1.1 christos for type-specific functions (template emulation in C) 998 1.1 christos Objective is to write these functions only once, for improved maintenance 999 1.1 christos */ 1000 1.1 christos 1001 1.1 christos /* safety checks */ 1002 1.1 christos #ifndef FSE_FUNCTION_EXTENSION 1003 1.1 christos # error "FSE_FUNCTION_EXTENSION must be defined" 1004 1.1 christos #endif 1005 1.1 christos #ifndef FSE_FUNCTION_TYPE 1006 1.1 christos # error "FSE_FUNCTION_TYPE must be defined" 1007 1.1 christos #endif 1008 1.1 christos 1009 1.1 christos /* Function names */ 1010 1.1 christos #define FSE_CAT(X,Y) X##Y 1011 1.1 christos #define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y) 1012 1.1 christos #define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y) 1013 1.1 christos 1014 1.1 christos static U32 FSE_tableStep(U32 tableSize) { return (tableSize>>1) + (tableSize>>3) + 3; } 1015 1.1 christos 1016 1.1 christos 1017 1.1 christos static size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog) 1018 1.1 christos { 1019 1.1 christos FSE_DTableHeader DTableH; 1020 1.1 christos void* const tdPtr = dt+1; /* because dt is unsigned, 32-bits aligned on 32-bits */ 1021 1.1 christos FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*) (tdPtr); 1022 1.1 christos const U32 tableSize = 1 << tableLog; 1023 1.1 christos const U32 tableMask = tableSize-1; 1024 1.1 christos const U32 step = FSE_tableStep(tableSize); 1025 1.1 christos U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1]; 1026 1.1 christos U32 position = 0; 1027 1.1 christos U32 highThreshold = tableSize-1; 1028 1.1 christos const S16 largeLimit= (S16)(1 << (tableLog-1)); 1029 1.1 christos U32 noLarge = 1; 1030 1.1 christos U32 s; 1031 1.1 christos 1032 1.1 christos /* Sanity Checks */ 1033 1.1 christos if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return ERROR(maxSymbolValue_tooLarge); 1034 1.1 christos if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); 1035 1.1 christos 1036 1.1 christos /* Init, lay down lowprob symbols */ 1037 1.1 christos memset(tableDecode, 0, sizeof(FSE_DECODE_TYPE) * (maxSymbolValue+1) ); /* useless init, but keep static analyzer happy, and we don't need to performance optimize legacy decoders */ 1038 1.1 christos DTableH.tableLog = (U16)tableLog; 1039 1.1 christos for (s=0; s<=maxSymbolValue; s++) 1040 1.1 christos { 1041 1.1 christos if (normalizedCounter[s]==-1) 1042 1.1 christos { 1043 1.1 christos tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s; 1044 1.1 christos symbolNext[s] = 1; 1045 1.1 christos } 1046 1.1 christos else 1047 1.1 christos { 1048 1.1 christos if (normalizedCounter[s] >= largeLimit) noLarge=0; 1049 1.1 christos symbolNext[s] = normalizedCounter[s]; 1050 1.1 christos } 1051 1.1 christos } 1052 1.1 christos 1053 1.1 christos /* Spread symbols */ 1054 1.1 christos for (s=0; s<=maxSymbolValue; s++) 1055 1.1 christos { 1056 1.1 christos int i; 1057 1.1 christos for (i=0; i<normalizedCounter[s]; i++) 1058 1.1 christos { 1059 1.1 christos tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s; 1060 1.1 christos position = (position + step) & tableMask; 1061 1.1 christos while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */ 1062 1.1 christos } 1063 1.1 christos } 1064 1.1 christos 1065 1.1 christos if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */ 1066 1.1 christos 1067 1.1 christos /* Build Decoding table */ 1068 1.1 christos { 1069 1.1 christos U32 i; 1070 1.1 christos for (i=0; i<tableSize; i++) 1071 1.1 christos { 1072 1.1 christos FSE_FUNCTION_TYPE symbol = (FSE_FUNCTION_TYPE)(tableDecode[i].symbol); 1073 1.1 christos U16 nextState = symbolNext[symbol]++; 1074 1.1 christos tableDecode[i].nbBits = (BYTE) (tableLog - BIT_highbit32 ((U32)nextState) ); 1075 1.1 christos tableDecode[i].newState = (U16) ( (nextState << tableDecode[i].nbBits) - tableSize); 1076 1.1 christos } 1077 1.1 christos } 1078 1.1 christos 1079 1.1 christos DTableH.fastMode = (U16)noLarge; 1080 1.1 christos memcpy(dt, &DTableH, sizeof(DTableH)); 1081 1.1 christos return 0; 1082 1.1 christos } 1083 1.1 christos 1084 1.1 christos 1085 1.1 christos #ifndef FSE_COMMONDEFS_ONLY 1086 1.1 christos /****************************************** 1087 1.1 christos * FSE helper functions 1088 1.1 christos ******************************************/ 1089 1.1 christos static unsigned FSE_isError(size_t code) { return ERR_isError(code); } 1090 1.1 christos 1091 1.1 christos 1092 1.1 christos /**************************************************************** 1093 1.1 christos * FSE NCount encoding-decoding 1094 1.1 christos ****************************************************************/ 1095 1.1 christos static short FSE_abs(short a) 1096 1.1 christos { 1097 1.1 christos return a<0 ? -a : a; 1098 1.1 christos } 1099 1.1 christos 1100 1.1 christos static size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, 1101 1.1 christos const void* headerBuffer, size_t hbSize) 1102 1.1 christos { 1103 1.1 christos const BYTE* const istart = (const BYTE*) headerBuffer; 1104 1.1 christos const BYTE* const iend = istart + hbSize; 1105 1.1 christos const BYTE* ip = istart; 1106 1.1 christos int nbBits; 1107 1.1 christos int remaining; 1108 1.1 christos int threshold; 1109 1.1 christos U32 bitStream; 1110 1.1 christos int bitCount; 1111 1.1 christos unsigned charnum = 0; 1112 1.1 christos int previous0 = 0; 1113 1.1 christos 1114 1.1 christos if (hbSize < 4) return ERROR(srcSize_wrong); 1115 1.1 christos bitStream = MEM_readLE32(ip); 1116 1.1 christos nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */ 1117 1.1 christos if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge); 1118 1.1 christos bitStream >>= 4; 1119 1.1 christos bitCount = 4; 1120 1.1 christos *tableLogPtr = nbBits; 1121 1.1 christos remaining = (1<<nbBits)+1; 1122 1.1 christos threshold = 1<<nbBits; 1123 1.1 christos nbBits++; 1124 1.1 christos 1125 1.1 christos while ((remaining>1) && (charnum<=*maxSVPtr)) 1126 1.1 christos { 1127 1.1 christos if (previous0) 1128 1.1 christos { 1129 1.1 christos unsigned n0 = charnum; 1130 1.1 christos while ((bitStream & 0xFFFF) == 0xFFFF) 1131 1.1 christos { 1132 1.1 christos n0+=24; 1133 1.1 christos if (ip < iend-5) 1134 1.1 christos { 1135 1.1 christos ip+=2; 1136 1.1 christos bitStream = MEM_readLE32(ip) >> bitCount; 1137 1.1 christos } 1138 1.1 christos else 1139 1.1 christos { 1140 1.1 christos bitStream >>= 16; 1141 1.1 christos bitCount+=16; 1142 1.1 christos } 1143 1.1 christos } 1144 1.1 christos while ((bitStream & 3) == 3) 1145 1.1 christos { 1146 1.1 christos n0+=3; 1147 1.1 christos bitStream>>=2; 1148 1.1 christos bitCount+=2; 1149 1.1 christos } 1150 1.1 christos n0 += bitStream & 3; 1151 1.1 christos bitCount += 2; 1152 1.1 christos if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall); 1153 1.1 christos while (charnum < n0) normalizedCounter[charnum++] = 0; 1154 1.1 christos if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) 1155 1.1 christos { 1156 1.1 christos ip += bitCount>>3; 1157 1.1 christos bitCount &= 7; 1158 1.1 christos bitStream = MEM_readLE32(ip) >> bitCount; 1159 1.1 christos } 1160 1.1 christos else 1161 1.1 christos bitStream >>= 2; 1162 1.1 christos } 1163 1.1 christos { 1164 1.1 christos const short max = (short)((2*threshold-1)-remaining); 1165 1.1 christos short count; 1166 1.1 christos 1167 1.1 christos if ((bitStream & (threshold-1)) < (U32)max) 1168 1.1 christos { 1169 1.1 christos count = (short)(bitStream & (threshold-1)); 1170 1.1 christos bitCount += nbBits-1; 1171 1.1 christos } 1172 1.1 christos else 1173 1.1 christos { 1174 1.1 christos count = (short)(bitStream & (2*threshold-1)); 1175 1.1 christos if (count >= threshold) count -= max; 1176 1.1 christos bitCount += nbBits; 1177 1.1 christos } 1178 1.1 christos 1179 1.1 christos count--; /* extra accuracy */ 1180 1.1 christos remaining -= FSE_abs(count); 1181 1.1 christos normalizedCounter[charnum++] = count; 1182 1.1 christos previous0 = !count; 1183 1.1 christos while (remaining < threshold) 1184 1.1 christos { 1185 1.1 christos nbBits--; 1186 1.1 christos threshold >>= 1; 1187 1.1 christos } 1188 1.1 christos 1189 1.1 christos { 1190 1.1 christos if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) 1191 1.1 christos { 1192 1.1 christos ip += bitCount>>3; 1193 1.1 christos bitCount &= 7; 1194 1.1 christos } 1195 1.1 christos else 1196 1.1 christos { 1197 1.1 christos bitCount -= (int)(8 * (iend - 4 - ip)); 1198 1.1 christos ip = iend - 4; 1199 1.1 christos } 1200 1.1 christos bitStream = MEM_readLE32(ip) >> (bitCount & 31); 1201 1.1 christos } 1202 1.1 christos } 1203 1.1 christos } 1204 1.1 christos if (remaining != 1) return ERROR(GENERIC); 1205 1.1 christos *maxSVPtr = charnum-1; 1206 1.1 christos 1207 1.1 christos ip += (bitCount+7)>>3; 1208 1.1 christos if ((size_t)(ip-istart) > hbSize) return ERROR(srcSize_wrong); 1209 1.1 christos return ip-istart; 1210 1.1 christos } 1211 1.1 christos 1212 1.1 christos 1213 1.1 christos /********************************************************* 1214 1.1 christos * Decompression (Byte symbols) 1215 1.1 christos *********************************************************/ 1216 1.1 christos static size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue) 1217 1.1 christos { 1218 1.1 christos void* ptr = dt; 1219 1.1 christos FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr; 1220 1.1 christos void* dPtr = dt + 1; 1221 1.1 christos FSE_decode_t* const cell = (FSE_decode_t*)dPtr; 1222 1.1 christos 1223 1.1 christos DTableH->tableLog = 0; 1224 1.1 christos DTableH->fastMode = 0; 1225 1.1 christos 1226 1.1 christos cell->newState = 0; 1227 1.1 christos cell->symbol = symbolValue; 1228 1.1 christos cell->nbBits = 0; 1229 1.1 christos 1230 1.1 christos return 0; 1231 1.1 christos } 1232 1.1 christos 1233 1.1 christos 1234 1.1 christos static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits) 1235 1.1 christos { 1236 1.1 christos void* ptr = dt; 1237 1.1 christos FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr; 1238 1.1 christos void* dPtr = dt + 1; 1239 1.1 christos FSE_decode_t* const dinfo = (FSE_decode_t*)dPtr; 1240 1.1 christos const unsigned tableSize = 1 << nbBits; 1241 1.1 christos const unsigned tableMask = tableSize - 1; 1242 1.1 christos const unsigned maxSymbolValue = tableMask; 1243 1.1 christos unsigned s; 1244 1.1 christos 1245 1.1 christos /* Sanity checks */ 1246 1.1 christos if (nbBits < 1) return ERROR(GENERIC); /* min size */ 1247 1.1 christos 1248 1.1 christos /* Build Decoding Table */ 1249 1.1 christos DTableH->tableLog = (U16)nbBits; 1250 1.1 christos DTableH->fastMode = 1; 1251 1.1 christos for (s=0; s<=maxSymbolValue; s++) 1252 1.1 christos { 1253 1.1 christos dinfo[s].newState = 0; 1254 1.1 christos dinfo[s].symbol = (BYTE)s; 1255 1.1 christos dinfo[s].nbBits = (BYTE)nbBits; 1256 1.1 christos } 1257 1.1 christos 1258 1.1 christos return 0; 1259 1.1 christos } 1260 1.1 christos 1261 1.1 christos FORCE_INLINE size_t FSE_decompress_usingDTable_generic( 1262 1.1 christos void* dst, size_t maxDstSize, 1263 1.1 christos const void* cSrc, size_t cSrcSize, 1264 1.1 christos const FSE_DTable* dt, const unsigned fast) 1265 1.1 christos { 1266 1.1 christos BYTE* const ostart = (BYTE*) dst; 1267 1.1 christos BYTE* op = ostart; 1268 1.1 christos BYTE* const omax = op + maxDstSize; 1269 1.1 christos BYTE* const olimit = omax-3; 1270 1.1 christos 1271 1.1 christos BIT_DStream_t bitD; 1272 1.1 christos FSE_DState_t state1; 1273 1.1 christos FSE_DState_t state2; 1274 1.1 christos size_t errorCode; 1275 1.1 christos 1276 1.1 christos /* Init */ 1277 1.1 christos errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */ 1278 1.1 christos if (FSE_isError(errorCode)) return errorCode; 1279 1.1 christos 1280 1.1 christos FSE_initDState(&state1, &bitD, dt); 1281 1.1 christos FSE_initDState(&state2, &bitD, dt); 1282 1.1 christos 1283 1.1 christos #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD) 1284 1.1 christos 1285 1.1 christos /* 4 symbols per loop */ 1286 1.1 christos for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) && (op<olimit) ; op+=4) 1287 1.1 christos { 1288 1.1 christos op[0] = FSE_GETSYMBOL(&state1); 1289 1.1 christos 1290 1.1 christos if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ 1291 1.1 christos BIT_reloadDStream(&bitD); 1292 1.1 christos 1293 1.1 christos op[1] = FSE_GETSYMBOL(&state2); 1294 1.1 christos 1295 1.1 christos if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ 1296 1.1 christos { if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { op+=2; break; } } 1297 1.1 christos 1298 1.1 christos op[2] = FSE_GETSYMBOL(&state1); 1299 1.1 christos 1300 1.1 christos if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */ 1301 1.1 christos BIT_reloadDStream(&bitD); 1302 1.1 christos 1303 1.1 christos op[3] = FSE_GETSYMBOL(&state2); 1304 1.1 christos } 1305 1.1 christos 1306 1.1 christos /* tail */ 1307 1.1 christos /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */ 1308 1.1 christos while (1) 1309 1.1 christos { 1310 1.1 christos if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state1))) ) 1311 1.1 christos break; 1312 1.1 christos 1313 1.1 christos *op++ = FSE_GETSYMBOL(&state1); 1314 1.1 christos 1315 1.1 christos if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state2))) ) 1316 1.1 christos break; 1317 1.1 christos 1318 1.1 christos *op++ = FSE_GETSYMBOL(&state2); 1319 1.1 christos } 1320 1.1 christos 1321 1.1 christos /* end ? */ 1322 1.1 christos if (BIT_endOfDStream(&bitD) && FSE_endOfDState(&state1) && FSE_endOfDState(&state2)) 1323 1.1 christos return op-ostart; 1324 1.1 christos 1325 1.1 christos if (op==omax) return ERROR(dstSize_tooSmall); /* dst buffer is full, but cSrc unfinished */ 1326 1.1 christos 1327 1.1 christos return ERROR(corruption_detected); 1328 1.1 christos } 1329 1.1 christos 1330 1.1 christos 1331 1.1 christos static size_t FSE_decompress_usingDTable(void* dst, size_t originalSize, 1332 1.1 christos const void* cSrc, size_t cSrcSize, 1333 1.1 christos const FSE_DTable* dt) 1334 1.1 christos { 1335 1.1 christos FSE_DTableHeader DTableH; 1336 1.1 christos U32 fastMode; 1337 1.1 christos 1338 1.1 christos memcpy(&DTableH, dt, sizeof(DTableH)); 1339 1.1 christos fastMode = DTableH.fastMode; 1340 1.1 christos 1341 1.1 christos /* select fast mode (static) */ 1342 1.1 christos if (fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1); 1343 1.1 christos return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0); 1344 1.1 christos } 1345 1.1 christos 1346 1.1 christos 1347 1.1 christos static size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize) 1348 1.1 christos { 1349 1.1 christos const BYTE* const istart = (const BYTE*)cSrc; 1350 1.1 christos const BYTE* ip = istart; 1351 1.1 christos short counting[FSE_MAX_SYMBOL_VALUE+1]; 1352 1.1 christos DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */ 1353 1.1 christos unsigned tableLog; 1354 1.1 christos unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE; 1355 1.1 christos size_t errorCode; 1356 1.1 christos 1357 1.1 christos if (cSrcSize<2) return ERROR(srcSize_wrong); /* too small input size */ 1358 1.1 christos 1359 1.1 christos /* normal FSE decoding mode */ 1360 1.1 christos errorCode = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize); 1361 1.1 christos if (FSE_isError(errorCode)) return errorCode; 1362 1.1 christos if (errorCode >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */ 1363 1.1 christos ip += errorCode; 1364 1.1 christos cSrcSize -= errorCode; 1365 1.1 christos 1366 1.1 christos errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog); 1367 1.1 christos if (FSE_isError(errorCode)) return errorCode; 1368 1.1 christos 1369 1.1 christos /* always return, even if it is an error code */ 1370 1.1 christos return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt); 1371 1.1 christos } 1372 1.1 christos 1373 1.1 christos 1374 1.1 christos 1375 1.1 christos #endif /* FSE_COMMONDEFS_ONLY */ 1376 1.1 christos 1377 1.1 christos 1378 1.1 christos /* ****************************************************************** 1379 1.1 christos Huff0 : Huffman coder, part of New Generation Entropy library 1380 1.1 christos header file 1381 1.1 christos Copyright (C) 2013-2015, Yann Collet. 1382 1.1 christos 1383 1.1 christos BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) 1384 1.1 christos 1385 1.1 christos Redistribution and use in source and binary forms, with or without 1386 1.1 christos modification, are permitted provided that the following conditions are 1387 1.1 christos met: 1388 1.1 christos 1389 1.1 christos * Redistributions of source code must retain the above copyright 1390 1.1 christos notice, this list of conditions and the following disclaimer. 1391 1.1 christos * Redistributions in binary form must reproduce the above 1392 1.1 christos copyright notice, this list of conditions and the following disclaimer 1393 1.1 christos in the documentation and/or other materials provided with the 1394 1.1 christos distribution. 1395 1.1 christos 1396 1.1 christos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1397 1.1 christos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1398 1.1 christos LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1399 1.1 christos A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1400 1.1 christos OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1401 1.1 christos SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1402 1.1 christos LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1403 1.1 christos DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1404 1.1 christos THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1405 1.1 christos (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1406 1.1 christos OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1407 1.1 christos 1408 1.1 christos You can contact the author at : 1409 1.1 christos - Source repository : https://github.com/Cyan4973/FiniteStateEntropy 1410 1.1 christos - Public forum : https://groups.google.com/forum/#!forum/lz4c 1411 1.1 christos ****************************************************************** */ 1412 1.1 christos #ifndef HUFF0_H 1413 1.1 christos #define HUFF0_H 1414 1.1 christos 1415 1.1 christos #if defined (__cplusplus) 1416 1.1 christos extern "C" { 1417 1.1 christos #endif 1418 1.1 christos 1419 1.1 christos 1420 1.1 christos /* **************************************** 1421 1.1 christos * Dependency 1422 1.1 christos ******************************************/ 1423 1.1 christos #include <stddef.h> /* size_t */ 1424 1.1 christos 1425 1.1 christos 1426 1.1 christos /* **************************************** 1427 1.1 christos * Huff0 simple functions 1428 1.1 christos ******************************************/ 1429 1.1 christos static size_t HUF_decompress(void* dst, size_t dstSize, 1430 1.1 christos const void* cSrc, size_t cSrcSize); 1431 1.1 christos /*! 1432 1.1 christos HUF_decompress(): 1433 1.1 christos Decompress Huff0 data from buffer 'cSrc', of size 'cSrcSize', 1434 1.1 christos into already allocated destination buffer 'dst', of size 'dstSize'. 1435 1.1 christos 'dstSize' must be the exact size of original (uncompressed) data. 1436 1.1 christos Note : in contrast with FSE, HUF_decompress can regenerate RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data, because it knows size to regenerate. 1437 1.1 christos @return : size of regenerated data (== dstSize) 1438 1.1 christos or an error code, which can be tested using HUF_isError() 1439 1.1 christos */ 1440 1.1 christos 1441 1.1 christos 1442 1.1 christos /* **************************************** 1443 1.1 christos * Tool functions 1444 1.1 christos ******************************************/ 1445 1.1 christos /* Error Management */ 1446 1.1 christos static unsigned HUF_isError(size_t code); /* tells if a return value is an error code */ 1447 1.1 christos 1448 1.1 christos 1449 1.1 christos #if defined (__cplusplus) 1450 1.1 christos } 1451 1.1 christos #endif 1452 1.1 christos 1453 1.1 christos #endif /* HUFF0_H */ 1454 1.1 christos 1455 1.1 christos 1456 1.1 christos /* ****************************************************************** 1457 1.1 christos Huff0 : Huffman coder, part of New Generation Entropy library 1458 1.1 christos header file for static linking (only) 1459 1.1 christos Copyright (C) 2013-2015, Yann Collet 1460 1.1 christos 1461 1.1 christos BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) 1462 1.1 christos 1463 1.1 christos Redistribution and use in source and binary forms, with or without 1464 1.1 christos modification, are permitted provided that the following conditions are 1465 1.1 christos met: 1466 1.1 christos 1467 1.1 christos * Redistributions of source code must retain the above copyright 1468 1.1 christos notice, this list of conditions and the following disclaimer. 1469 1.1 christos * Redistributions in binary form must reproduce the above 1470 1.1 christos copyright notice, this list of conditions and the following disclaimer 1471 1.1 christos in the documentation and/or other materials provided with the 1472 1.1 christos distribution. 1473 1.1 christos 1474 1.1 christos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1475 1.1 christos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1476 1.1 christos LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1477 1.1 christos A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1478 1.1 christos OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1479 1.1 christos SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1480 1.1 christos LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1481 1.1 christos DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1482 1.1 christos THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1483 1.1 christos (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1484 1.1 christos OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1485 1.1 christos 1486 1.1 christos You can contact the author at : 1487 1.1 christos - Source repository : https://github.com/Cyan4973/FiniteStateEntropy 1488 1.1 christos - Public forum : https://groups.google.com/forum/#!forum/lz4c 1489 1.1 christos ****************************************************************** */ 1490 1.1 christos #ifndef HUFF0_STATIC_H 1491 1.1 christos #define HUFF0_STATIC_H 1492 1.1 christos 1493 1.1 christos #if defined (__cplusplus) 1494 1.1 christos extern "C" { 1495 1.1 christos #endif 1496 1.1 christos 1497 1.1 christos 1498 1.1 christos 1499 1.1 christos /* **************************************** 1500 1.1 christos * Static allocation macros 1501 1.1 christos ******************************************/ 1502 1.1 christos /* static allocation of Huff0's DTable */ 1503 1.1 christos #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<maxTableLog)) /* nb Cells; use unsigned short for X2, unsigned int for X4 */ 1504 1.1 christos #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \ 1505 1.1 christos unsigned short DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog } 1506 1.1 christos #define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \ 1507 1.1 christos unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog)] = { maxTableLog } 1508 1.1 christos #define HUF_CREATE_STATIC_DTABLEX6(DTable, maxTableLog) \ 1509 1.1 christos unsigned int DTable[HUF_DTABLE_SIZE(maxTableLog) * 3 / 2] = { maxTableLog } 1510 1.1 christos 1511 1.1 christos 1512 1.1 christos /* **************************************** 1513 1.1 christos * Advanced decompression functions 1514 1.1 christos ******************************************/ 1515 1.1 christos static size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */ 1516 1.1 christos static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbols decoder */ 1517 1.1 christos 1518 1.1 christos 1519 1.1 christos /* **************************************** 1520 1.1 christos * Huff0 detailed API 1521 1.1 christos ******************************************/ 1522 1.1 christos /*! 1523 1.1 christos HUF_decompress() does the following: 1524 1.1 christos 1. select the decompression algorithm (X2, X4, X6) based on pre-computed heuristics 1525 1.1 christos 2. build Huffman table from save, using HUF_readDTableXn() 1526 1.1 christos 3. decode 1 or 4 segments in parallel using HUF_decompressSXn_usingDTable 1527 1.1 christos 1528 1.1 christos */ 1529 1.1 christos static size_t HUF_readDTableX2 (unsigned short* DTable, const void* src, size_t srcSize); 1530 1.1 christos static size_t HUF_readDTableX4 (unsigned* DTable, const void* src, size_t srcSize); 1531 1.1 christos 1532 1.1 christos static size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const unsigned short* DTable); 1533 1.1 christos static size_t HUF_decompress4X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const unsigned* DTable); 1534 1.1 christos 1535 1.1 christos 1536 1.1 christos #if defined (__cplusplus) 1537 1.1 christos } 1538 1.1 christos #endif 1539 1.1 christos 1540 1.1 christos #endif /* HUFF0_STATIC_H */ 1541 1.1 christos 1542 1.1 christos 1543 1.1 christos 1544 1.1 christos /* ****************************************************************** 1545 1.1 christos Huff0 : Huffman coder, part of New Generation Entropy library 1546 1.1 christos Copyright (C) 2013-2015, Yann Collet. 1547 1.1 christos 1548 1.1 christos BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) 1549 1.1 christos 1550 1.1 christos Redistribution and use in source and binary forms, with or without 1551 1.1 christos modification, are permitted provided that the following conditions are 1552 1.1 christos met: 1553 1.1 christos 1554 1.1 christos * Redistributions of source code must retain the above copyright 1555 1.1 christos notice, this list of conditions and the following disclaimer. 1556 1.1 christos * Redistributions in binary form must reproduce the above 1557 1.1 christos copyright notice, this list of conditions and the following disclaimer 1558 1.1 christos in the documentation and/or other materials provided with the 1559 1.1 christos distribution. 1560 1.1 christos 1561 1.1 christos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1562 1.1 christos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1563 1.1 christos LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1564 1.1 christos A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1565 1.1 christos OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1566 1.1 christos SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1567 1.1 christos LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1568 1.1 christos DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1569 1.1 christos THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1570 1.1 christos (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1571 1.1 christos OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1572 1.1 christos 1573 1.1 christos You can contact the author at : 1574 1.1 christos - FSE+Huff0 source repository : https://github.com/Cyan4973/FiniteStateEntropy 1575 1.1 christos ****************************************************************** */ 1576 1.1 christos 1577 1.1 christos /* ************************************************************** 1578 1.1 christos * Compiler specifics 1579 1.1 christos ****************************************************************/ 1580 1.1 christos #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) 1581 1.1 christos /* inline is defined */ 1582 1.1 christos #elif defined(_MSC_VER) 1583 1.1 christos # define inline __inline 1584 1.1 christos #else 1585 1.1 christos # define inline /* disable inline */ 1586 1.1 christos #endif 1587 1.1 christos 1588 1.1 christos 1589 1.1 christos #ifdef _MSC_VER /* Visual Studio */ 1590 1.1 christos # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ 1591 1.1 christos #endif 1592 1.1 christos 1593 1.1 christos 1594 1.1 christos /* ************************************************************** 1595 1.1 christos * Includes 1596 1.1 christos ****************************************************************/ 1597 1.1 christos #include <stdlib.h> /* malloc, free, qsort */ 1598 1.1 christos #include <string.h> /* memcpy, memset */ 1599 1.1 christos #include <stdio.h> /* printf (debug) */ 1600 1.1 christos 1601 1.1 christos 1602 1.1 christos /* ************************************************************** 1603 1.1 christos * Constants 1604 1.1 christos ****************************************************************/ 1605 1.1 christos #define HUF_ABSOLUTEMAX_TABLELOG 16 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */ 1606 1.1 christos #define HUF_MAX_TABLELOG 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */ 1607 1.1 christos #define HUF_DEFAULT_TABLELOG HUF_MAX_TABLELOG /* tableLog by default, when not specified */ 1608 1.1 christos #define HUF_MAX_SYMBOL_VALUE 255 1609 1.1 christos #if (HUF_MAX_TABLELOG > HUF_ABSOLUTEMAX_TABLELOG) 1610 1.1 christos # error "HUF_MAX_TABLELOG is too large !" 1611 1.1 christos #endif 1612 1.1 christos 1613 1.1 christos 1614 1.1 christos /* ************************************************************** 1615 1.1 christos * Error Management 1616 1.1 christos ****************************************************************/ 1617 1.1 christos static unsigned HUF_isError(size_t code) { return ERR_isError(code); } 1618 1.1 christos #define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ 1619 1.1 christos 1620 1.1 christos 1621 1.1 christos 1622 1.1 christos /*-******************************************************* 1623 1.1 christos * Huff0 : Huffman block decompression 1624 1.1 christos *********************************************************/ 1625 1.1 christos typedef struct { BYTE byte; BYTE nbBits; } HUF_DEltX2; /* single-symbol decoding */ 1626 1.1 christos 1627 1.1 christos typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX4; /* double-symbols decoding */ 1628 1.1 christos 1629 1.1 christos typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t; 1630 1.1 christos 1631 1.1 christos /*! HUF_readStats 1632 1.1 christos Read compact Huffman tree, saved by HUF_writeCTable 1633 1.1 christos @huffWeight : destination buffer 1634 1.1 christos @return : size read from `src` 1635 1.1 christos */ 1636 1.1 christos static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats, 1637 1.1 christos U32* nbSymbolsPtr, U32* tableLogPtr, 1638 1.1 christos const void* src, size_t srcSize) 1639 1.1 christos { 1640 1.1 christos U32 weightTotal; 1641 1.1 christos U32 tableLog; 1642 1.1 christos const BYTE* ip = (const BYTE*) src; 1643 1.1 christos size_t iSize; 1644 1.1 christos size_t oSize; 1645 1.1 christos U32 n; 1646 1.1 christos 1647 1.1 christos if (!srcSize) return ERROR(srcSize_wrong); 1648 1.1 christos iSize = ip[0]; 1649 1.1 christos //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */ 1650 1.1 christos 1651 1.1 christos if (iSize >= 128) /* special header */ 1652 1.1 christos { 1653 1.1 christos if (iSize >= (242)) /* RLE */ 1654 1.1 christos { 1655 1.1 christos static int l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 }; 1656 1.1 christos oSize = l[iSize-242]; 1657 1.1 christos memset(huffWeight, 1, hwSize); 1658 1.1 christos iSize = 0; 1659 1.1 christos } 1660 1.1 christos else /* Incompressible */ 1661 1.1 christos { 1662 1.1 christos oSize = iSize - 127; 1663 1.1 christos iSize = ((oSize+1)/2); 1664 1.1 christos if (iSize+1 > srcSize) return ERROR(srcSize_wrong); 1665 1.1 christos if (oSize >= hwSize) return ERROR(corruption_detected); 1666 1.1 christos ip += 1; 1667 1.1 christos for (n=0; n<oSize; n+=2) 1668 1.1 christos { 1669 1.1 christos huffWeight[n] = ip[n/2] >> 4; 1670 1.1 christos huffWeight[n+1] = ip[n/2] & 15; 1671 1.1 christos } 1672 1.1 christos } 1673 1.1 christos } 1674 1.1 christos else /* header compressed with FSE (normal case) */ 1675 1.1 christos { 1676 1.1 christos if (iSize+1 > srcSize) return ERROR(srcSize_wrong); 1677 1.1 christos oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */ 1678 1.1 christos if (FSE_isError(oSize)) return oSize; 1679 1.1 christos } 1680 1.1 christos 1681 1.1 christos /* collect weight stats */ 1682 1.1 christos memset(rankStats, 0, (HUF_ABSOLUTEMAX_TABLELOG + 1) * sizeof(U32)); 1683 1.1 christos weightTotal = 0; 1684 1.1 christos for (n=0; n<oSize; n++) 1685 1.1 christos { 1686 1.1 christos if (huffWeight[n] >= HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected); 1687 1.1 christos rankStats[huffWeight[n]]++; 1688 1.1 christos weightTotal += (1 << huffWeight[n]) >> 1; 1689 1.1 christos } 1690 1.1 christos if (weightTotal == 0) return ERROR(corruption_detected); 1691 1.1 christos 1692 1.1 christos /* get last non-null symbol weight (implied, total must be 2^n) */ 1693 1.1 christos tableLog = BIT_highbit32(weightTotal) + 1; 1694 1.1 christos if (tableLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected); 1695 1.1 christos { 1696 1.1 christos U32 total = 1 << tableLog; 1697 1.1 christos U32 rest = total - weightTotal; 1698 1.1 christos U32 verif = 1 << BIT_highbit32(rest); 1699 1.1 christos U32 lastWeight = BIT_highbit32(rest) + 1; 1700 1.1 christos if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */ 1701 1.1 christos huffWeight[oSize] = (BYTE)lastWeight; 1702 1.1 christos rankStats[lastWeight]++; 1703 1.1 christos } 1704 1.1 christos 1705 1.1 christos /* check tree construction validity */ 1706 1.1 christos if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */ 1707 1.1 christos 1708 1.1 christos /* results */ 1709 1.1 christos *nbSymbolsPtr = (U32)(oSize+1); 1710 1.1 christos *tableLogPtr = tableLog; 1711 1.1 christos return iSize+1; 1712 1.1 christos } 1713 1.1 christos 1714 1.1 christos 1715 1.1 christos /**************************/ 1716 1.1 christos /* single-symbol decoding */ 1717 1.1 christos /**************************/ 1718 1.1 christos 1719 1.1 christos static size_t HUF_readDTableX2 (U16* DTable, const void* src, size_t srcSize) 1720 1.1 christos { 1721 1.1 christos BYTE huffWeight[HUF_MAX_SYMBOL_VALUE + 1]; 1722 1.1 christos U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; /* large enough for values from 0 to 16 */ 1723 1.1 christos U32 tableLog = 0; 1724 1.1 christos size_t iSize; 1725 1.1 christos U32 nbSymbols = 0; 1726 1.1 christos U32 n; 1727 1.1 christos U32 nextRankStart; 1728 1.1 christos void* const dtPtr = DTable + 1; 1729 1.1 christos HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr; 1730 1.1 christos 1731 1.1 christos HUF_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(U16)); /* if compilation fails here, assertion is false */ 1732 1.1 christos //memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */ 1733 1.1 christos 1734 1.1 christos iSize = HUF_readStats(huffWeight, HUF_MAX_SYMBOL_VALUE + 1, rankVal, &nbSymbols, &tableLog, src, srcSize); 1735 1.1 christos if (HUF_isError(iSize)) return iSize; 1736 1.1 christos 1737 1.1 christos /* check result */ 1738 1.1 christos if (tableLog > DTable[0]) return ERROR(tableLog_tooLarge); /* DTable is too small */ 1739 1.1 christos DTable[0] = (U16)tableLog; /* maybe should separate sizeof DTable, as allocated, from used size of DTable, in case of DTable re-use */ 1740 1.1 christos 1741 1.1 christos /* Prepare ranks */ 1742 1.1 christos nextRankStart = 0; 1743 1.1 christos for (n=1; n<=tableLog; n++) 1744 1.1 christos { 1745 1.1 christos U32 current = nextRankStart; 1746 1.1 christos nextRankStart += (rankVal[n] << (n-1)); 1747 1.1 christos rankVal[n] = current; 1748 1.1 christos } 1749 1.1 christos 1750 1.1 christos /* fill DTable */ 1751 1.1 christos for (n=0; n<nbSymbols; n++) 1752 1.1 christos { 1753 1.1 christos const U32 w = huffWeight[n]; 1754 1.1 christos const U32 length = (1 << w) >> 1; 1755 1.1 christos U32 i; 1756 1.1 christos HUF_DEltX2 D; 1757 1.1 christos D.byte = (BYTE)n; D.nbBits = (BYTE)(tableLog + 1 - w); 1758 1.1 christos for (i = rankVal[w]; i < rankVal[w] + length; i++) 1759 1.1 christos dt[i] = D; 1760 1.1 christos rankVal[w] += length; 1761 1.1 christos } 1762 1.1 christos 1763 1.1 christos return iSize; 1764 1.1 christos } 1765 1.1 christos 1766 1.1 christos static BYTE HUF_decodeSymbolX2(BIT_DStream_t* Dstream, const HUF_DEltX2* dt, const U32 dtLog) 1767 1.1 christos { 1768 1.1 christos const size_t val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */ 1769 1.1 christos const BYTE c = dt[val].byte; 1770 1.1 christos BIT_skipBits(Dstream, dt[val].nbBits); 1771 1.1 christos return c; 1772 1.1 christos } 1773 1.1 christos 1774 1.1 christos #define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \ 1775 1.1 christos *ptr++ = HUF_decodeSymbolX2(DStreamPtr, dt, dtLog) 1776 1.1 christos 1777 1.1 christos #define HUF_DECODE_SYMBOLX2_1(ptr, DStreamPtr) \ 1778 1.1 christos if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \ 1779 1.1 christos HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) 1780 1.1 christos 1781 1.1 christos #define HUF_DECODE_SYMBOLX2_2(ptr, DStreamPtr) \ 1782 1.1 christos if (MEM_64bits()) \ 1783 1.1 christos HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) 1784 1.1 christos 1785 1.1 christos static inline size_t HUF_decodeStreamX2(BYTE* p, BIT_DStream_t* const bitDPtr, BYTE* const pEnd, const HUF_DEltX2* const dt, const U32 dtLog) 1786 1.1 christos { 1787 1.1 christos BYTE* const pStart = p; 1788 1.1 christos 1789 1.1 christos /* up to 4 symbols at a time */ 1790 1.1 christos while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-4)) 1791 1.1 christos { 1792 1.1 christos HUF_DECODE_SYMBOLX2_2(p, bitDPtr); 1793 1.1 christos HUF_DECODE_SYMBOLX2_1(p, bitDPtr); 1794 1.1 christos HUF_DECODE_SYMBOLX2_2(p, bitDPtr); 1795 1.1 christos HUF_DECODE_SYMBOLX2_0(p, bitDPtr); 1796 1.1 christos } 1797 1.1 christos 1798 1.1 christos /* closer to the end */ 1799 1.1 christos while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd)) 1800 1.1 christos HUF_DECODE_SYMBOLX2_0(p, bitDPtr); 1801 1.1 christos 1802 1.1 christos /* no more data to retrieve from bitstream, hence no need to reload */ 1803 1.1 christos while (p < pEnd) 1804 1.1 christos HUF_DECODE_SYMBOLX2_0(p, bitDPtr); 1805 1.1 christos 1806 1.1 christos return pEnd-pStart; 1807 1.1 christos } 1808 1.1 christos 1809 1.1 christos 1810 1.1 christos static size_t HUF_decompress4X2_usingDTable( 1811 1.1 christos void* dst, size_t dstSize, 1812 1.1 christos const void* cSrc, size_t cSrcSize, 1813 1.1 christos const U16* DTable) 1814 1.1 christos { 1815 1.1 christos if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ 1816 1.1 christos 1817 1.1 christos { 1818 1.1 christos const BYTE* const istart = (const BYTE*) cSrc; 1819 1.1 christos BYTE* const ostart = (BYTE*) dst; 1820 1.1 christos BYTE* const oend = ostart + dstSize; 1821 1.1 christos const void* const dtPtr = DTable; 1822 1.1 christos const HUF_DEltX2* const dt = ((const HUF_DEltX2*)dtPtr) +1; 1823 1.1 christos const U32 dtLog = DTable[0]; 1824 1.1 christos size_t errorCode; 1825 1.1 christos 1826 1.1 christos /* Init */ 1827 1.1 christos BIT_DStream_t bitD1; 1828 1.1 christos BIT_DStream_t bitD2; 1829 1.1 christos BIT_DStream_t bitD3; 1830 1.1 christos BIT_DStream_t bitD4; 1831 1.1 christos const size_t length1 = MEM_readLE16(istart); 1832 1.1 christos const size_t length2 = MEM_readLE16(istart+2); 1833 1.1 christos const size_t length3 = MEM_readLE16(istart+4); 1834 1.1 christos size_t length4; 1835 1.1 christos const BYTE* const istart1 = istart + 6; /* jumpTable */ 1836 1.1 christos const BYTE* const istart2 = istart1 + length1; 1837 1.1 christos const BYTE* const istart3 = istart2 + length2; 1838 1.1 christos const BYTE* const istart4 = istart3 + length3; 1839 1.1 christos const size_t segmentSize = (dstSize+3) / 4; 1840 1.1 christos BYTE* const opStart2 = ostart + segmentSize; 1841 1.1 christos BYTE* const opStart3 = opStart2 + segmentSize; 1842 1.1 christos BYTE* const opStart4 = opStart3 + segmentSize; 1843 1.1 christos BYTE* op1 = ostart; 1844 1.1 christos BYTE* op2 = opStart2; 1845 1.1 christos BYTE* op3 = opStart3; 1846 1.1 christos BYTE* op4 = opStart4; 1847 1.1 christos U32 endSignal; 1848 1.1 christos 1849 1.1 christos length4 = cSrcSize - (length1 + length2 + length3 + 6); 1850 1.1 christos if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */ 1851 1.1 christos errorCode = BIT_initDStream(&bitD1, istart1, length1); 1852 1.1 christos if (HUF_isError(errorCode)) return errorCode; 1853 1.1 christos errorCode = BIT_initDStream(&bitD2, istart2, length2); 1854 1.1 christos if (HUF_isError(errorCode)) return errorCode; 1855 1.1 christos errorCode = BIT_initDStream(&bitD3, istart3, length3); 1856 1.1 christos if (HUF_isError(errorCode)) return errorCode; 1857 1.1 christos errorCode = BIT_initDStream(&bitD4, istart4, length4); 1858 1.1 christos if (HUF_isError(errorCode)) return errorCode; 1859 1.1 christos 1860 1.1 christos /* 16-32 symbols per loop (4-8 symbols per stream) */ 1861 1.1 christos endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); 1862 1.1 christos for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; ) 1863 1.1 christos { 1864 1.1 christos HUF_DECODE_SYMBOLX2_2(op1, &bitD1); 1865 1.1 christos HUF_DECODE_SYMBOLX2_2(op2, &bitD2); 1866 1.1 christos HUF_DECODE_SYMBOLX2_2(op3, &bitD3); 1867 1.1 christos HUF_DECODE_SYMBOLX2_2(op4, &bitD4); 1868 1.1 christos HUF_DECODE_SYMBOLX2_1(op1, &bitD1); 1869 1.1 christos HUF_DECODE_SYMBOLX2_1(op2, &bitD2); 1870 1.1 christos HUF_DECODE_SYMBOLX2_1(op3, &bitD3); 1871 1.1 christos HUF_DECODE_SYMBOLX2_1(op4, &bitD4); 1872 1.1 christos HUF_DECODE_SYMBOLX2_2(op1, &bitD1); 1873 1.1 christos HUF_DECODE_SYMBOLX2_2(op2, &bitD2); 1874 1.1 christos HUF_DECODE_SYMBOLX2_2(op3, &bitD3); 1875 1.1 christos HUF_DECODE_SYMBOLX2_2(op4, &bitD4); 1876 1.1 christos HUF_DECODE_SYMBOLX2_0(op1, &bitD1); 1877 1.1 christos HUF_DECODE_SYMBOLX2_0(op2, &bitD2); 1878 1.1 christos HUF_DECODE_SYMBOLX2_0(op3, &bitD3); 1879 1.1 christos HUF_DECODE_SYMBOLX2_0(op4, &bitD4); 1880 1.1 christos 1881 1.1 christos endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); 1882 1.1 christos } 1883 1.1 christos 1884 1.1 christos /* check corruption */ 1885 1.1 christos if (op1 > opStart2) return ERROR(corruption_detected); 1886 1.1 christos if (op2 > opStart3) return ERROR(corruption_detected); 1887 1.1 christos if (op3 > opStart4) return ERROR(corruption_detected); 1888 1.1 christos /* note : op4 supposed already verified within main loop */ 1889 1.1 christos 1890 1.1 christos /* finish bitStreams one by one */ 1891 1.1 christos HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog); 1892 1.1 christos HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog); 1893 1.1 christos HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog); 1894 1.1 christos HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog); 1895 1.1 christos 1896 1.1 christos /* check */ 1897 1.1 christos endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4); 1898 1.1 christos if (!endSignal) return ERROR(corruption_detected); 1899 1.1 christos 1900 1.1 christos /* decoded size */ 1901 1.1 christos return dstSize; 1902 1.1 christos } 1903 1.1 christos } 1904 1.1 christos 1905 1.1 christos 1906 1.1 christos static size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) 1907 1.1 christos { 1908 1.1 christos HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_MAX_TABLELOG); 1909 1.1 christos const BYTE* ip = (const BYTE*) cSrc; 1910 1.1 christos size_t errorCode; 1911 1.1 christos 1912 1.1 christos errorCode = HUF_readDTableX2 (DTable, cSrc, cSrcSize); 1913 1.1 christos if (HUF_isError(errorCode)) return errorCode; 1914 1.1 christos if (errorCode >= cSrcSize) return ERROR(srcSize_wrong); 1915 1.1 christos ip += errorCode; 1916 1.1 christos cSrcSize -= errorCode; 1917 1.1 christos 1918 1.1 christos return HUF_decompress4X2_usingDTable (dst, dstSize, ip, cSrcSize, DTable); 1919 1.1 christos } 1920 1.1 christos 1921 1.1 christos 1922 1.1 christos /***************************/ 1923 1.1 christos /* double-symbols decoding */ 1924 1.1 christos /***************************/ 1925 1.1 christos 1926 1.1 christos static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 consumed, 1927 1.1 christos const U32* rankValOrigin, const int minWeight, 1928 1.1 christos const sortedSymbol_t* sortedSymbols, const U32 sortedListSize, 1929 1.1 christos U32 nbBitsBaseline, U16 baseSeq) 1930 1.1 christos { 1931 1.1 christos HUF_DEltX4 DElt; 1932 1.1 christos U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; 1933 1.1 christos U32 s; 1934 1.1 christos 1935 1.1 christos /* get pre-calculated rankVal */ 1936 1.1 christos memcpy(rankVal, rankValOrigin, sizeof(rankVal)); 1937 1.1 christos 1938 1.1 christos /* fill skipped values */ 1939 1.1 christos if (minWeight>1) 1940 1.1 christos { 1941 1.1 christos U32 i, skipSize = rankVal[minWeight]; 1942 1.1 christos MEM_writeLE16(&(DElt.sequence), baseSeq); 1943 1.1 christos DElt.nbBits = (BYTE)(consumed); 1944 1.1 christos DElt.length = 1; 1945 1.1 christos for (i = 0; i < skipSize; i++) 1946 1.1 christos DTable[i] = DElt; 1947 1.1 christos } 1948 1.1 christos 1949 1.1 christos /* fill DTable */ 1950 1.1 christos for (s=0; s<sortedListSize; s++) /* note : sortedSymbols already skipped */ 1951 1.1 christos { 1952 1.1 christos const U32 symbol = sortedSymbols[s].symbol; 1953 1.1 christos const U32 weight = sortedSymbols[s].weight; 1954 1.1 christos const U32 nbBits = nbBitsBaseline - weight; 1955 1.1 christos const U32 length = 1 << (sizeLog-nbBits); 1956 1.1 christos const U32 start = rankVal[weight]; 1957 1.1 christos U32 i = start; 1958 1.1 christos const U32 end = start + length; 1959 1.1 christos 1960 1.1 christos MEM_writeLE16(&(DElt.sequence), (U16)(baseSeq + (symbol << 8))); 1961 1.1 christos DElt.nbBits = (BYTE)(nbBits + consumed); 1962 1.1 christos DElt.length = 2; 1963 1.1 christos do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */ 1964 1.1 christos 1965 1.1 christos rankVal[weight] += length; 1966 1.1 christos } 1967 1.1 christos } 1968 1.1 christos 1969 1.1 christos typedef U32 rankVal_t[HUF_ABSOLUTEMAX_TABLELOG][HUF_ABSOLUTEMAX_TABLELOG + 1]; 1970 1.1 christos 1971 1.1 christos static void HUF_fillDTableX4(HUF_DEltX4* DTable, const U32 targetLog, 1972 1.1 christos const sortedSymbol_t* sortedList, const U32 sortedListSize, 1973 1.1 christos const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight, 1974 1.1 christos const U32 nbBitsBaseline) 1975 1.1 christos { 1976 1.1 christos U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; 1977 1.1 christos const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */ 1978 1.1 christos const U32 minBits = nbBitsBaseline - maxWeight; 1979 1.1 christos U32 s; 1980 1.1 christos 1981 1.1 christos memcpy(rankVal, rankValOrigin, sizeof(rankVal)); 1982 1.1 christos 1983 1.1 christos /* fill DTable */ 1984 1.1 christos for (s=0; s<sortedListSize; s++) 1985 1.1 christos { 1986 1.1 christos const U16 symbol = sortedList[s].symbol; 1987 1.1 christos const U32 weight = sortedList[s].weight; 1988 1.1 christos const U32 nbBits = nbBitsBaseline - weight; 1989 1.1 christos const U32 start = rankVal[weight]; 1990 1.1 christos const U32 length = 1 << (targetLog-nbBits); 1991 1.1 christos 1992 1.1 christos if (targetLog-nbBits >= minBits) /* enough room for a second symbol */ 1993 1.1 christos { 1994 1.1 christos U32 sortedRank; 1995 1.1 christos int minWeight = nbBits + scaleLog; 1996 1.1 christos if (minWeight < 1) minWeight = 1; 1997 1.1 christos sortedRank = rankStart[minWeight]; 1998 1.1 christos HUF_fillDTableX4Level2(DTable+start, targetLog-nbBits, nbBits, 1999 1.1 christos rankValOrigin[nbBits], minWeight, 2000 1.1 christos sortedList+sortedRank, sortedListSize-sortedRank, 2001 1.1 christos nbBitsBaseline, symbol); 2002 1.1 christos } 2003 1.1 christos else 2004 1.1 christos { 2005 1.1 christos U32 i; 2006 1.1 christos const U32 end = start + length; 2007 1.1 christos HUF_DEltX4 DElt; 2008 1.1 christos 2009 1.1 christos MEM_writeLE16(&(DElt.sequence), symbol); 2010 1.1 christos DElt.nbBits = (BYTE)(nbBits); 2011 1.1 christos DElt.length = 1; 2012 1.1 christos for (i = start; i < end; i++) 2013 1.1 christos DTable[i] = DElt; 2014 1.1 christos } 2015 1.1 christos rankVal[weight] += length; 2016 1.1 christos } 2017 1.1 christos } 2018 1.1 christos 2019 1.1 christos static size_t HUF_readDTableX4 (U32* DTable, const void* src, size_t srcSize) 2020 1.1 christos { 2021 1.1 christos BYTE weightList[HUF_MAX_SYMBOL_VALUE + 1]; 2022 1.1 christos sortedSymbol_t sortedSymbol[HUF_MAX_SYMBOL_VALUE + 1]; 2023 1.1 christos U32 rankStats[HUF_ABSOLUTEMAX_TABLELOG + 1] = { 0 }; 2024 1.1 christos U32 rankStart0[HUF_ABSOLUTEMAX_TABLELOG + 2] = { 0 }; 2025 1.1 christos U32* const rankStart = rankStart0+1; 2026 1.1 christos rankVal_t rankVal; 2027 1.1 christos U32 tableLog, maxW, sizeOfSort, nbSymbols; 2028 1.1 christos const U32 memLog = DTable[0]; 2029 1.1 christos size_t iSize; 2030 1.1 christos void* dtPtr = DTable; 2031 1.1 christos HUF_DEltX4* const dt = ((HUF_DEltX4*)dtPtr) + 1; 2032 1.1 christos 2033 1.1 christos HUF_STATIC_ASSERT(sizeof(HUF_DEltX4) == sizeof(U32)); /* if compilation fails here, assertion is false */ 2034 1.1 christos if (memLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge); 2035 1.1 christos //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */ 2036 1.1 christos 2037 1.1 christos iSize = HUF_readStats(weightList, HUF_MAX_SYMBOL_VALUE + 1, rankStats, &nbSymbols, &tableLog, src, srcSize); 2038 1.1 christos if (HUF_isError(iSize)) return iSize; 2039 1.1 christos 2040 1.1 christos /* check result */ 2041 1.1 christos if (tableLog > memLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */ 2042 1.1 christos 2043 1.1 christos /* find maxWeight */ 2044 1.1 christos for (maxW = tableLog; rankStats[maxW]==0; maxW--) 2045 1.1 christos { if (!maxW) return ERROR(GENERIC); } /* necessarily finds a solution before maxW==0 */ 2046 1.1 christos 2047 1.1 christos /* Get start index of each weight */ 2048 1.1 christos { 2049 1.1 christos U32 w, nextRankStart = 0; 2050 1.1 christos for (w=1; w<=maxW; w++) 2051 1.1 christos { 2052 1.1 christos U32 current = nextRankStart; 2053 1.1 christos nextRankStart += rankStats[w]; 2054 1.1 christos rankStart[w] = current; 2055 1.1 christos } 2056 1.1 christos rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/ 2057 1.1 christos sizeOfSort = nextRankStart; 2058 1.1 christos } 2059 1.1 christos 2060 1.1 christos /* sort symbols by weight */ 2061 1.1 christos { 2062 1.1 christos U32 s; 2063 1.1 christos for (s=0; s<nbSymbols; s++) 2064 1.1 christos { 2065 1.1 christos U32 w = weightList[s]; 2066 1.1 christos U32 r = rankStart[w]++; 2067 1.1 christos sortedSymbol[r].symbol = (BYTE)s; 2068 1.1 christos sortedSymbol[r].weight = (BYTE)w; 2069 1.1 christos } 2070 1.1 christos rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */ 2071 1.1 christos } 2072 1.1 christos 2073 1.1 christos /* Build rankVal */ 2074 1.1 christos { 2075 1.1 christos const U32 minBits = tableLog+1 - maxW; 2076 1.1 christos U32 nextRankVal = 0; 2077 1.1 christos U32 w, consumed; 2078 1.1 christos const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */ 2079 1.1 christos U32* rankVal0 = rankVal[0]; 2080 1.1 christos for (w=1; w<=maxW; w++) 2081 1.1 christos { 2082 1.1 christos U32 current = nextRankVal; 2083 1.1 christos nextRankVal += rankStats[w] << (w+rescale); 2084 1.1 christos rankVal0[w] = current; 2085 1.1 christos } 2086 1.1 christos for (consumed = minBits; consumed <= memLog - minBits; consumed++) 2087 1.1 christos { 2088 1.1 christos U32* rankValPtr = rankVal[consumed]; 2089 1.1 christos for (w = 1; w <= maxW; w++) 2090 1.1 christos { 2091 1.1 christos rankValPtr[w] = rankVal0[w] >> consumed; 2092 1.1 christos } 2093 1.1 christos } 2094 1.1 christos } 2095 1.1 christos 2096 1.1 christos HUF_fillDTableX4(dt, memLog, 2097 1.1 christos sortedSymbol, sizeOfSort, 2098 1.1 christos rankStart0, rankVal, maxW, 2099 1.1 christos tableLog+1); 2100 1.1 christos 2101 1.1 christos return iSize; 2102 1.1 christos } 2103 1.1 christos 2104 1.1 christos 2105 1.1 christos static U32 HUF_decodeSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog) 2106 1.1 christos { 2107 1.1 christos const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */ 2108 1.1 christos memcpy(op, dt+val, 2); 2109 1.1 christos BIT_skipBits(DStream, dt[val].nbBits); 2110 1.1 christos return dt[val].length; 2111 1.1 christos } 2112 1.1 christos 2113 1.1 christos static U32 HUF_decodeLastSymbolX4(void* op, BIT_DStream_t* DStream, const HUF_DEltX4* dt, const U32 dtLog) 2114 1.1 christos { 2115 1.1 christos const size_t val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */ 2116 1.1 christos memcpy(op, dt+val, 1); 2117 1.1 christos if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits); 2118 1.1 christos else 2119 1.1 christos { 2120 1.1 christos if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8)) 2121 1.1 christos { 2122 1.1 christos BIT_skipBits(DStream, dt[val].nbBits); 2123 1.1 christos if (DStream->bitsConsumed > (sizeof(DStream->bitContainer)*8)) 2124 1.1 christos DStream->bitsConsumed = (sizeof(DStream->bitContainer)*8); /* ugly hack; works only because it's the last symbol. Note : can't easily extract nbBits from just this symbol */ 2125 1.1 christos } 2126 1.1 christos } 2127 1.1 christos return 1; 2128 1.1 christos } 2129 1.1 christos 2130 1.1 christos 2131 1.1 christos #define HUF_DECODE_SYMBOLX4_0(ptr, DStreamPtr) \ 2132 1.1 christos ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog) 2133 1.1 christos 2134 1.1 christos #define HUF_DECODE_SYMBOLX4_1(ptr, DStreamPtr) \ 2135 1.1 christos if (MEM_64bits() || (HUF_MAX_TABLELOG<=12)) \ 2136 1.1 christos ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog) 2137 1.1 christos 2138 1.1 christos #define HUF_DECODE_SYMBOLX4_2(ptr, DStreamPtr) \ 2139 1.1 christos if (MEM_64bits()) \ 2140 1.1 christos ptr += HUF_decodeSymbolX4(ptr, DStreamPtr, dt, dtLog) 2141 1.1 christos 2142 1.1 christos static inline size_t HUF_decodeStreamX4(BYTE* p, BIT_DStream_t* bitDPtr, BYTE* const pEnd, const HUF_DEltX4* const dt, const U32 dtLog) 2143 1.1 christos { 2144 1.1 christos BYTE* const pStart = p; 2145 1.1 christos 2146 1.1 christos /* up to 8 symbols at a time */ 2147 1.1 christos while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p < pEnd-7)) 2148 1.1 christos { 2149 1.1 christos HUF_DECODE_SYMBOLX4_2(p, bitDPtr); 2150 1.1 christos HUF_DECODE_SYMBOLX4_1(p, bitDPtr); 2151 1.1 christos HUF_DECODE_SYMBOLX4_2(p, bitDPtr); 2152 1.1 christos HUF_DECODE_SYMBOLX4_0(p, bitDPtr); 2153 1.1 christos } 2154 1.1 christos 2155 1.1 christos /* closer to the end */ 2156 1.1 christos while ((BIT_reloadDStream(bitDPtr) == BIT_DStream_unfinished) && (p <= pEnd-2)) 2157 1.1 christos HUF_DECODE_SYMBOLX4_0(p, bitDPtr); 2158 1.1 christos 2159 1.1 christos while (p <= pEnd-2) 2160 1.1 christos HUF_DECODE_SYMBOLX4_0(p, bitDPtr); /* no need to reload : reached the end of DStream */ 2161 1.1 christos 2162 1.1 christos if (p < pEnd) 2163 1.1 christos p += HUF_decodeLastSymbolX4(p, bitDPtr, dt, dtLog); 2164 1.1 christos 2165 1.1 christos return p-pStart; 2166 1.1 christos } 2167 1.1 christos 2168 1.1 christos static size_t HUF_decompress4X4_usingDTable( 2169 1.1 christos void* dst, size_t dstSize, 2170 1.1 christos const void* cSrc, size_t cSrcSize, 2171 1.1 christos const U32* DTable) 2172 1.1 christos { 2173 1.1 christos if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ 2174 1.1 christos 2175 1.1 christos { 2176 1.1 christos const BYTE* const istart = (const BYTE*) cSrc; 2177 1.1 christos BYTE* const ostart = (BYTE*) dst; 2178 1.1 christos BYTE* const oend = ostart + dstSize; 2179 1.1 christos const void* const dtPtr = DTable; 2180 1.1 christos const HUF_DEltX4* const dt = ((const HUF_DEltX4*)dtPtr) +1; 2181 1.1 christos const U32 dtLog = DTable[0]; 2182 1.1 christos size_t errorCode; 2183 1.1 christos 2184 1.1 christos /* Init */ 2185 1.1 christos BIT_DStream_t bitD1; 2186 1.1 christos BIT_DStream_t bitD2; 2187 1.1 christos BIT_DStream_t bitD3; 2188 1.1 christos BIT_DStream_t bitD4; 2189 1.1 christos const size_t length1 = MEM_readLE16(istart); 2190 1.1 christos const size_t length2 = MEM_readLE16(istart+2); 2191 1.1 christos const size_t length3 = MEM_readLE16(istart+4); 2192 1.1 christos size_t length4; 2193 1.1 christos const BYTE* const istart1 = istart + 6; /* jumpTable */ 2194 1.1 christos const BYTE* const istart2 = istart1 + length1; 2195 1.1 christos const BYTE* const istart3 = istart2 + length2; 2196 1.1 christos const BYTE* const istart4 = istart3 + length3; 2197 1.1 christos const size_t segmentSize = (dstSize+3) / 4; 2198 1.1 christos BYTE* const opStart2 = ostart + segmentSize; 2199 1.1 christos BYTE* const opStart3 = opStart2 + segmentSize; 2200 1.1 christos BYTE* const opStart4 = opStart3 + segmentSize; 2201 1.1 christos BYTE* op1 = ostart; 2202 1.1 christos BYTE* op2 = opStart2; 2203 1.1 christos BYTE* op3 = opStart3; 2204 1.1 christos BYTE* op4 = opStart4; 2205 1.1 christos U32 endSignal; 2206 1.1 christos 2207 1.1 christos length4 = cSrcSize - (length1 + length2 + length3 + 6); 2208 1.1 christos if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */ 2209 1.1 christos errorCode = BIT_initDStream(&bitD1, istart1, length1); 2210 1.1 christos if (HUF_isError(errorCode)) return errorCode; 2211 1.1 christos errorCode = BIT_initDStream(&bitD2, istart2, length2); 2212 1.1 christos if (HUF_isError(errorCode)) return errorCode; 2213 1.1 christos errorCode = BIT_initDStream(&bitD3, istart3, length3); 2214 1.1 christos if (HUF_isError(errorCode)) return errorCode; 2215 1.1 christos errorCode = BIT_initDStream(&bitD4, istart4, length4); 2216 1.1 christos if (HUF_isError(errorCode)) return errorCode; 2217 1.1 christos 2218 1.1 christos /* 16-32 symbols per loop (4-8 symbols per stream) */ 2219 1.1 christos endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); 2220 1.1 christos for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; ) 2221 1.1 christos { 2222 1.1 christos HUF_DECODE_SYMBOLX4_2(op1, &bitD1); 2223 1.1 christos HUF_DECODE_SYMBOLX4_2(op2, &bitD2); 2224 1.1 christos HUF_DECODE_SYMBOLX4_2(op3, &bitD3); 2225 1.1 christos HUF_DECODE_SYMBOLX4_2(op4, &bitD4); 2226 1.1 christos HUF_DECODE_SYMBOLX4_1(op1, &bitD1); 2227 1.1 christos HUF_DECODE_SYMBOLX4_1(op2, &bitD2); 2228 1.1 christos HUF_DECODE_SYMBOLX4_1(op3, &bitD3); 2229 1.1 christos HUF_DECODE_SYMBOLX4_1(op4, &bitD4); 2230 1.1 christos HUF_DECODE_SYMBOLX4_2(op1, &bitD1); 2231 1.1 christos HUF_DECODE_SYMBOLX4_2(op2, &bitD2); 2232 1.1 christos HUF_DECODE_SYMBOLX4_2(op3, &bitD3); 2233 1.1 christos HUF_DECODE_SYMBOLX4_2(op4, &bitD4); 2234 1.1 christos HUF_DECODE_SYMBOLX4_0(op1, &bitD1); 2235 1.1 christos HUF_DECODE_SYMBOLX4_0(op2, &bitD2); 2236 1.1 christos HUF_DECODE_SYMBOLX4_0(op3, &bitD3); 2237 1.1 christos HUF_DECODE_SYMBOLX4_0(op4, &bitD4); 2238 1.1 christos 2239 1.1 christos endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); 2240 1.1 christos } 2241 1.1 christos 2242 1.1 christos /* check corruption */ 2243 1.1 christos if (op1 > opStart2) return ERROR(corruption_detected); 2244 1.1 christos if (op2 > opStart3) return ERROR(corruption_detected); 2245 1.1 christos if (op3 > opStart4) return ERROR(corruption_detected); 2246 1.1 christos /* note : op4 supposed already verified within main loop */ 2247 1.1 christos 2248 1.1 christos /* finish bitStreams one by one */ 2249 1.1 christos HUF_decodeStreamX4(op1, &bitD1, opStart2, dt, dtLog); 2250 1.1 christos HUF_decodeStreamX4(op2, &bitD2, opStart3, dt, dtLog); 2251 1.1 christos HUF_decodeStreamX4(op3, &bitD3, opStart4, dt, dtLog); 2252 1.1 christos HUF_decodeStreamX4(op4, &bitD4, oend, dt, dtLog); 2253 1.1 christos 2254 1.1 christos /* check */ 2255 1.1 christos endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4); 2256 1.1 christos if (!endSignal) return ERROR(corruption_detected); 2257 1.1 christos 2258 1.1 christos /* decoded size */ 2259 1.1 christos return dstSize; 2260 1.1 christos } 2261 1.1 christos } 2262 1.1 christos 2263 1.1 christos 2264 1.1 christos static size_t HUF_decompress4X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) 2265 1.1 christos { 2266 1.1 christos HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_MAX_TABLELOG); 2267 1.1 christos const BYTE* ip = (const BYTE*) cSrc; 2268 1.1 christos 2269 1.1 christos size_t hSize = HUF_readDTableX4 (DTable, cSrc, cSrcSize); 2270 1.1 christos if (HUF_isError(hSize)) return hSize; 2271 1.1 christos if (hSize >= cSrcSize) return ERROR(srcSize_wrong); 2272 1.1 christos ip += hSize; 2273 1.1 christos cSrcSize -= hSize; 2274 1.1 christos 2275 1.1 christos return HUF_decompress4X4_usingDTable (dst, dstSize, ip, cSrcSize, DTable); 2276 1.1 christos } 2277 1.1 christos 2278 1.1 christos 2279 1.1 christos /**********************************/ 2280 1.1 christos /* Generic decompression selector */ 2281 1.1 christos /**********************************/ 2282 1.1 christos 2283 1.1 christos typedef struct { U32 tableTime; U32 decode256Time; } algo_time_t; 2284 1.1 christos static const algo_time_t algoTime[16 /* Quantization */][3 /* single, double, quad */] = 2285 1.1 christos { 2286 1.1 christos /* single, double, quad */ 2287 1.1 christos {{0,0}, {1,1}, {2,2}}, /* Q==0 : impossible */ 2288 1.1 christos {{0,0}, {1,1}, {2,2}}, /* Q==1 : impossible */ 2289 1.1 christos {{ 38,130}, {1313, 74}, {2151, 38}}, /* Q == 2 : 12-18% */ 2290 1.1 christos {{ 448,128}, {1353, 74}, {2238, 41}}, /* Q == 3 : 18-25% */ 2291 1.1 christos {{ 556,128}, {1353, 74}, {2238, 47}}, /* Q == 4 : 25-32% */ 2292 1.1 christos {{ 714,128}, {1418, 74}, {2436, 53}}, /* Q == 5 : 32-38% */ 2293 1.1 christos {{ 883,128}, {1437, 74}, {2464, 61}}, /* Q == 6 : 38-44% */ 2294 1.1 christos {{ 897,128}, {1515, 75}, {2622, 68}}, /* Q == 7 : 44-50% */ 2295 1.1 christos {{ 926,128}, {1613, 75}, {2730, 75}}, /* Q == 8 : 50-56% */ 2296 1.1 christos {{ 947,128}, {1729, 77}, {3359, 77}}, /* Q == 9 : 56-62% */ 2297 1.1 christos {{1107,128}, {2083, 81}, {4006, 84}}, /* Q ==10 : 62-69% */ 2298 1.1 christos {{1177,128}, {2379, 87}, {4785, 88}}, /* Q ==11 : 69-75% */ 2299 1.1 christos {{1242,128}, {2415, 93}, {5155, 84}}, /* Q ==12 : 75-81% */ 2300 1.1 christos {{1349,128}, {2644,106}, {5260,106}}, /* Q ==13 : 81-87% */ 2301 1.1 christos {{1455,128}, {2422,124}, {4174,124}}, /* Q ==14 : 87-93% */ 2302 1.1 christos {{ 722,128}, {1891,145}, {1936,146}}, /* Q ==15 : 93-99% */ 2303 1.1 christos }; 2304 1.1 christos 2305 1.1 christos typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); 2306 1.1 christos 2307 1.1 christos static size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize) 2308 1.1 christos { 2309 1.1 christos static const decompressionAlgo decompress[3] = { HUF_decompress4X2, HUF_decompress4X4, NULL }; 2310 1.1 christos /* estimate decompression time */ 2311 1.1 christos U32 Q; 2312 1.1 christos const U32 D256 = (U32)(dstSize >> 8); 2313 1.1 christos U32 Dtime[3]; 2314 1.1 christos U32 algoNb = 0; 2315 1.1 christos int n; 2316 1.1 christos 2317 1.1 christos /* validation checks */ 2318 1.1 christos if (dstSize == 0) return ERROR(dstSize_tooSmall); 2319 1.1 christos if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */ 2320 1.1 christos if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */ 2321 1.1 christos if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */ 2322 1.1 christos 2323 1.1 christos /* decoder timing evaluation */ 2324 1.1 christos Q = (U32)(cSrcSize * 16 / dstSize); /* Q < 16 since dstSize > cSrcSize */ 2325 1.1 christos for (n=0; n<3; n++) 2326 1.1 christos Dtime[n] = algoTime[Q][n].tableTime + (algoTime[Q][n].decode256Time * D256); 2327 1.1 christos 2328 1.1 christos Dtime[1] += Dtime[1] >> 4; Dtime[2] += Dtime[2] >> 3; /* advantage to algorithms using less memory, for cache eviction */ 2329 1.1 christos 2330 1.1 christos if (Dtime[1] < Dtime[0]) algoNb = 1; 2331 1.1 christos 2332 1.1 christos return decompress[algoNb](dst, dstSize, cSrc, cSrcSize); 2333 1.1 christos 2334 1.1 christos //return HUF_decompress4X2(dst, dstSize, cSrc, cSrcSize); /* multi-streams single-symbol decoding */ 2335 1.1 christos //return HUF_decompress4X4(dst, dstSize, cSrc, cSrcSize); /* multi-streams double-symbols decoding */ 2336 1.1 christos //return HUF_decompress4X6(dst, dstSize, cSrc, cSrcSize); /* multi-streams quad-symbols decoding */ 2337 1.1 christos } 2338 1.1 christos 2339 1.1 christos 2340 1.1 christos 2341 1.1 christos #endif /* ZSTD_CCOMMON_H_MODULE */ 2342 1.1 christos 2343 1.1 christos 2344 1.1 christos /* 2345 1.1 christos zstd - decompression module fo v0.4 legacy format 2346 1.1 christos Copyright (C) 2015-2016, Yann Collet. 2347 1.1 christos 2348 1.1 christos BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) 2349 1.1 christos 2350 1.1 christos Redistribution and use in source and binary forms, with or without 2351 1.1 christos modification, are permitted provided that the following conditions are 2352 1.1 christos met: 2353 1.1 christos * Redistributions of source code must retain the above copyright 2354 1.1 christos notice, this list of conditions and the following disclaimer. 2355 1.1 christos * Redistributions in binary form must reproduce the above 2356 1.1 christos copyright notice, this list of conditions and the following disclaimer 2357 1.1 christos in the documentation and/or other materials provided with the 2358 1.1 christos distribution. 2359 1.1 christos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2360 1.1 christos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2361 1.1 christos LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2362 1.1 christos A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2363 1.1 christos OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2364 1.1 christos SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2365 1.1 christos LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2366 1.1 christos DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2367 1.1 christos THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2368 1.1 christos (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2369 1.1 christos OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2370 1.1 christos 2371 1.1 christos You can contact the author at : 2372 1.1 christos - zstd source repository : https://github.com/Cyan4973/zstd 2373 1.1 christos - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c 2374 1.1 christos */ 2375 1.1 christos 2376 1.1 christos /* *************************************************************** 2377 1.1 christos * Tuning parameters 2378 1.1 christos *****************************************************************/ 2379 1.1 christos /*! 2380 1.1 christos * HEAPMODE : 2381 1.1 christos * Select how default decompression function ZSTD_decompress() will allocate memory, 2382 1.1 christos * in memory stack (0), or in memory heap (1, requires malloc()) 2383 1.1 christos */ 2384 1.1 christos #ifndef ZSTD_HEAPMODE 2385 1.1 christos # define ZSTD_HEAPMODE 1 2386 1.1 christos #endif 2387 1.1 christos 2388 1.1 christos 2389 1.1 christos /* ******************************************************* 2390 1.1 christos * Includes 2391 1.1 christos *********************************************************/ 2392 1.1 christos #include <stdlib.h> /* calloc */ 2393 1.1 christos #include <string.h> /* memcpy, memmove */ 2394 1.1 christos #include <stdio.h> /* debug : printf */ 2395 1.1 christos 2396 1.1 christos 2397 1.1 christos /* ******************************************************* 2398 1.1 christos * Compiler specifics 2399 1.1 christos *********************************************************/ 2400 1.1 christos #ifdef _MSC_VER /* Visual Studio */ 2401 1.1 christos # include <intrin.h> /* For Visual 2005 */ 2402 1.1 christos # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ 2403 1.1 christos # pragma warning(disable : 4324) /* disable: C4324: padded structure */ 2404 1.1 christos #endif 2405 1.1 christos 2406 1.1 christos 2407 1.1 christos /* ************************************* 2408 1.1 christos * Local types 2409 1.1 christos ***************************************/ 2410 1.1 christos typedef struct 2411 1.1 christos { 2412 1.1 christos blockType_t blockType; 2413 1.1 christos U32 origSize; 2414 1.1 christos } blockProperties_t; 2415 1.1 christos 2416 1.1 christos 2417 1.1 christos /* ******************************************************* 2418 1.1 christos * Memory operations 2419 1.1 christos **********************************************************/ 2420 1.1 christos static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); } 2421 1.1 christos 2422 1.1 christos 2423 1.1 christos /* ************************************* 2424 1.1 christos * Error Management 2425 1.1 christos ***************************************/ 2426 1.1 christos 2427 1.1 christos /*! ZSTD_isError 2428 1.1 christos * tells if a return value is an error code */ 2429 1.1 christos static unsigned ZSTD_isError(size_t code) { return ERR_isError(code); } 2430 1.1 christos 2431 1.1 christos 2432 1.1 christos /* ************************************************************* 2433 1.1 christos * Context management 2434 1.1 christos ***************************************************************/ 2435 1.1 christos typedef enum { ZSTDds_getFrameHeaderSize, ZSTDds_decodeFrameHeader, 2436 1.1 christos ZSTDds_decodeBlockHeader, ZSTDds_decompressBlock } ZSTD_dStage; 2437 1.1 christos 2438 1.1 christos struct ZSTDv04_Dctx_s 2439 1.1 christos { 2440 1.1 christos U32 LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)]; 2441 1.1 christos U32 OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)]; 2442 1.1 christos U32 MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)]; 2443 1.1 christos const void* previousDstEnd; 2444 1.1 christos const void* base; 2445 1.1 christos const void* vBase; 2446 1.1 christos const void* dictEnd; 2447 1.1 christos size_t expected; 2448 1.1 christos size_t headerSize; 2449 1.1 christos ZSTD_parameters params; 2450 1.1 christos blockType_t bType; 2451 1.1 christos ZSTD_dStage stage; 2452 1.1 christos const BYTE* litPtr; 2453 1.1 christos size_t litSize; 2454 1.1 christos BYTE litBuffer[BLOCKSIZE + 8 /* margin for wildcopy */]; 2455 1.1 christos BYTE headerBuffer[ZSTD_frameHeaderSize_max]; 2456 1.1 christos }; /* typedef'd to ZSTD_DCtx within "zstd_static.h" */ 2457 1.1 christos 2458 1.1 christos static size_t ZSTD_resetDCtx(ZSTD_DCtx* dctx) 2459 1.1 christos { 2460 1.1 christos dctx->expected = ZSTD_frameHeaderSize_min; 2461 1.1 christos dctx->stage = ZSTDds_getFrameHeaderSize; 2462 1.1 christos dctx->previousDstEnd = NULL; 2463 1.1 christos dctx->base = NULL; 2464 1.1 christos dctx->vBase = NULL; 2465 1.1 christos dctx->dictEnd = NULL; 2466 1.1 christos return 0; 2467 1.1 christos } 2468 1.1 christos 2469 1.1 christos static ZSTD_DCtx* ZSTD_createDCtx(void) 2470 1.1 christos { 2471 1.1 christos ZSTD_DCtx* dctx = (ZSTD_DCtx*)malloc(sizeof(ZSTD_DCtx)); 2472 1.1 christos if (dctx==NULL) return NULL; 2473 1.1 christos ZSTD_resetDCtx(dctx); 2474 1.1 christos return dctx; 2475 1.1 christos } 2476 1.1 christos 2477 1.1 christos static size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx) 2478 1.1 christos { 2479 1.1 christos free(dctx); 2480 1.1 christos return 0; 2481 1.1 christos } 2482 1.1 christos 2483 1.1 christos 2484 1.1 christos /* ************************************************************* 2485 1.1 christos * Decompression section 2486 1.1 christos ***************************************************************/ 2487 1.1 christos /** ZSTD_decodeFrameHeader_Part1 2488 1.1 christos * decode the 1st part of the Frame Header, which tells Frame Header size. 2489 1.1 christos * srcSize must be == ZSTD_frameHeaderSize_min 2490 1.1 christos * @return : the full size of the Frame Header */ 2491 1.1 christos static size_t ZSTD_decodeFrameHeader_Part1(ZSTD_DCtx* zc, const void* src, size_t srcSize) 2492 1.1 christos { 2493 1.1 christos U32 magicNumber; 2494 1.1 christos if (srcSize != ZSTD_frameHeaderSize_min) return ERROR(srcSize_wrong); 2495 1.1 christos magicNumber = MEM_readLE32(src); 2496 1.1 christos if (magicNumber != ZSTD_MAGICNUMBER) return ERROR(prefix_unknown); 2497 1.1 christos zc->headerSize = ZSTD_frameHeaderSize_min; 2498 1.1 christos return zc->headerSize; 2499 1.1 christos } 2500 1.1 christos 2501 1.1 christos 2502 1.1 christos static size_t ZSTD_getFrameParams(ZSTD_parameters* params, const void* src, size_t srcSize) 2503 1.1 christos { 2504 1.1 christos U32 magicNumber; 2505 1.1 christos if (srcSize < ZSTD_frameHeaderSize_min) return ZSTD_frameHeaderSize_max; 2506 1.1 christos magicNumber = MEM_readLE32(src); 2507 1.1 christos if (magicNumber != ZSTD_MAGICNUMBER) return ERROR(prefix_unknown); 2508 1.1 christos memset(params, 0, sizeof(*params)); 2509 1.1 christos params->windowLog = (((const BYTE*)src)[4] & 15) + ZSTD_WINDOWLOG_ABSOLUTEMIN; 2510 1.1 christos if ((((const BYTE*)src)[4] >> 4) != 0) return ERROR(frameParameter_unsupported); /* reserved bits */ 2511 1.1 christos return 0; 2512 1.1 christos } 2513 1.1 christos 2514 1.1 christos /** ZSTD_decodeFrameHeader_Part2 2515 1.1 christos * decode the full Frame Header 2516 1.1 christos * srcSize must be the size provided by ZSTD_decodeFrameHeader_Part1 2517 1.1 christos * @return : 0, or an error code, which can be tested using ZSTD_isError() */ 2518 1.1 christos static size_t ZSTD_decodeFrameHeader_Part2(ZSTD_DCtx* zc, const void* src, size_t srcSize) 2519 1.1 christos { 2520 1.1 christos size_t result; 2521 1.1 christos if (srcSize != zc->headerSize) return ERROR(srcSize_wrong); 2522 1.1 christos result = ZSTD_getFrameParams(&(zc->params), src, srcSize); 2523 1.1 christos if ((MEM_32bits()) && (zc->params.windowLog > 25)) return ERROR(frameParameter_unsupported); 2524 1.1 christos return result; 2525 1.1 christos } 2526 1.1 christos 2527 1.1 christos 2528 1.1 christos static size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr) 2529 1.1 christos { 2530 1.1 christos const BYTE* const in = (const BYTE* const)src; 2531 1.1 christos BYTE headerFlags; 2532 1.1 christos U32 cSize; 2533 1.1 christos 2534 1.1 christos if (srcSize < 3) return ERROR(srcSize_wrong); 2535 1.1 christos 2536 1.1 christos headerFlags = *in; 2537 1.1 christos cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16); 2538 1.1 christos 2539 1.1 christos bpPtr->blockType = (blockType_t)(headerFlags >> 6); 2540 1.1 christos bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0; 2541 1.1 christos 2542 1.1 christos if (bpPtr->blockType == bt_end) return 0; 2543 1.1 christos if (bpPtr->blockType == bt_rle) return 1; 2544 1.1 christos return cSize; 2545 1.1 christos } 2546 1.1 christos 2547 1.1 christos static size_t ZSTD_copyRawBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize) 2548 1.1 christos { 2549 1.1 christos if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall); 2550 1.1 christos if (srcSize > 0) { 2551 1.1 christos memcpy(dst, src, srcSize); 2552 1.1 christos } 2553 1.1 christos return srcSize; 2554 1.1 christos } 2555 1.1 christos 2556 1.1 christos 2557 1.1 christos /** ZSTD_decompressLiterals 2558 1.1 christos @return : nb of bytes read from src, or an error code*/ 2559 1.1 christos static size_t ZSTD_decompressLiterals(void* dst, size_t* maxDstSizePtr, 2560 1.1 christos const void* src, size_t srcSize) 2561 1.1 christos { 2562 1.1 christos const BYTE* ip = (const BYTE*)src; 2563 1.1 christos 2564 1.1 christos const size_t litSize = (MEM_readLE32(src) & 0x1FFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */ 2565 1.1 christos const size_t litCSize = (MEM_readLE32(ip+2) & 0xFFFFFF) >> 5; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */ 2566 1.1 christos 2567 1.1 christos if (litSize > *maxDstSizePtr) return ERROR(corruption_detected); 2568 1.1 christos if (litCSize + 5 > srcSize) return ERROR(corruption_detected); 2569 1.1 christos 2570 1.1 christos if (HUF_isError(HUF_decompress(dst, litSize, ip+5, litCSize))) return ERROR(corruption_detected); 2571 1.1 christos 2572 1.1 christos *maxDstSizePtr = litSize; 2573 1.1 christos return litCSize + 5; 2574 1.1 christos } 2575 1.1 christos 2576 1.1 christos 2577 1.1 christos /** ZSTD_decodeLiteralsBlock 2578 1.1 christos @return : nb of bytes read from src (< srcSize ) */ 2579 1.1 christos static size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* dctx, 2580 1.1 christos const void* src, size_t srcSize) /* note : srcSize < BLOCKSIZE */ 2581 1.1 christos { 2582 1.1 christos const BYTE* const istart = (const BYTE*) src; 2583 1.1 christos 2584 1.1 christos /* any compressed block with literals segment must be at least this size */ 2585 1.1 christos if (srcSize < MIN_CBLOCK_SIZE) return ERROR(corruption_detected); 2586 1.1 christos 2587 1.1 christos switch(*istart & 3) 2588 1.1 christos { 2589 1.1 christos /* compressed */ 2590 1.1 christos case 0: 2591 1.1 christos { 2592 1.1 christos size_t litSize = BLOCKSIZE; 2593 1.1 christos const size_t readSize = ZSTD_decompressLiterals(dctx->litBuffer, &litSize, src, srcSize); 2594 1.1 christos dctx->litPtr = dctx->litBuffer; 2595 1.1 christos dctx->litSize = litSize; 2596 1.1 christos memset(dctx->litBuffer + dctx->litSize, 0, 8); 2597 1.1 christos return readSize; /* works if it's an error too */ 2598 1.1 christos } 2599 1.1 christos case IS_RAW: 2600 1.1 christos { 2601 1.1 christos const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */ 2602 1.1 christos if (litSize > srcSize-11) /* risk of reading too far with wildcopy */ 2603 1.1 christos { 2604 1.1 christos if (litSize > BLOCKSIZE) return ERROR(corruption_detected); 2605 1.1 christos if (litSize > srcSize-3) return ERROR(corruption_detected); 2606 1.1 christos memcpy(dctx->litBuffer, istart, litSize); 2607 1.1 christos dctx->litPtr = dctx->litBuffer; 2608 1.1 christos dctx->litSize = litSize; 2609 1.1 christos memset(dctx->litBuffer + dctx->litSize, 0, 8); 2610 1.1 christos return litSize+3; 2611 1.1 christos } 2612 1.1 christos /* direct reference into compressed stream */ 2613 1.1 christos dctx->litPtr = istart+3; 2614 1.1 christos dctx->litSize = litSize; 2615 1.1 christos return litSize+3; } 2616 1.1 christos case IS_RLE: 2617 1.1 christos { 2618 1.1 christos const size_t litSize = (MEM_readLE32(istart) & 0xFFFFFF) >> 2; /* no buffer issue : srcSize >= MIN_CBLOCK_SIZE */ 2619 1.1 christos if (litSize > BLOCKSIZE) return ERROR(corruption_detected); 2620 1.1 christos memset(dctx->litBuffer, istart[3], litSize + 8); 2621 1.1 christos dctx->litPtr = dctx->litBuffer; 2622 1.1 christos dctx->litSize = litSize; 2623 1.1 christos return 4; 2624 1.1 christos } 2625 1.1 christos default: 2626 1.1 christos return ERROR(corruption_detected); /* forbidden nominal case */ 2627 1.1 christos } 2628 1.1 christos } 2629 1.1 christos 2630 1.1 christos 2631 1.1 christos static size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr, 2632 1.1 christos FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb, 2633 1.1 christos const void* src, size_t srcSize) 2634 1.1 christos { 2635 1.1 christos const BYTE* const istart = (const BYTE* const)src; 2636 1.1 christos const BYTE* ip = istart; 2637 1.1 christos const BYTE* const iend = istart + srcSize; 2638 1.1 christos U32 LLtype, Offtype, MLtype; 2639 1.1 christos U32 LLlog, Offlog, MLlog; 2640 1.1 christos size_t dumpsLength; 2641 1.1 christos 2642 1.1 christos /* check */ 2643 1.1 christos if (srcSize < 5) return ERROR(srcSize_wrong); 2644 1.1 christos 2645 1.1 christos /* SeqHead */ 2646 1.1 christos *nbSeq = MEM_readLE16(ip); ip+=2; 2647 1.1 christos LLtype = *ip >> 6; 2648 1.1 christos Offtype = (*ip >> 4) & 3; 2649 1.1 christos MLtype = (*ip >> 2) & 3; 2650 1.1 christos if (*ip & 2) 2651 1.1 christos { 2652 1.1 christos dumpsLength = ip[2]; 2653 1.1 christos dumpsLength += ip[1] << 8; 2654 1.1 christos ip += 3; 2655 1.1 christos } 2656 1.1 christos else 2657 1.1 christos { 2658 1.1 christos dumpsLength = ip[1]; 2659 1.1 christos dumpsLength += (ip[0] & 1) << 8; 2660 1.1 christos ip += 2; 2661 1.1 christos } 2662 1.1 christos *dumpsPtr = ip; 2663 1.1 christos ip += dumpsLength; 2664 1.1 christos *dumpsLengthPtr = dumpsLength; 2665 1.1 christos 2666 1.1 christos /* check */ 2667 1.1 christos if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */ 2668 1.1 christos 2669 1.1 christos /* sequences */ 2670 1.1 christos { 2671 1.1 christos S16 norm[MaxML+1]; /* assumption : MaxML >= MaxLL >= MaxOff */ 2672 1.1 christos size_t headerSize; 2673 1.1 christos 2674 1.1 christos /* Build DTables */ 2675 1.1 christos switch(LLtype) 2676 1.1 christos { 2677 1.1 christos case bt_rle : 2678 1.1 christos LLlog = 0; 2679 1.1 christos FSE_buildDTable_rle(DTableLL, *ip++); break; 2680 1.1 christos case bt_raw : 2681 1.1 christos LLlog = LLbits; 2682 1.1 christos FSE_buildDTable_raw(DTableLL, LLbits); break; 2683 1.1 christos default : 2684 1.1 christos { U32 max = MaxLL; 2685 1.1 christos headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip); 2686 1.1 christos if (FSE_isError(headerSize)) return ERROR(GENERIC); 2687 1.1 christos if (LLlog > LLFSELog) return ERROR(corruption_detected); 2688 1.1 christos ip += headerSize; 2689 1.1 christos FSE_buildDTable(DTableLL, norm, max, LLlog); 2690 1.1 christos } } 2691 1.1 christos 2692 1.1 christos switch(Offtype) 2693 1.1 christos { 2694 1.1 christos case bt_rle : 2695 1.1 christos Offlog = 0; 2696 1.1 christos if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */ 2697 1.1 christos FSE_buildDTable_rle(DTableOffb, *ip++ & MaxOff); /* if *ip > MaxOff, data is corrupted */ 2698 1.1 christos break; 2699 1.1 christos case bt_raw : 2700 1.1 christos Offlog = Offbits; 2701 1.1 christos FSE_buildDTable_raw(DTableOffb, Offbits); break; 2702 1.1 christos default : 2703 1.1 christos { U32 max = MaxOff; 2704 1.1 christos headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip); 2705 1.1 christos if (FSE_isError(headerSize)) return ERROR(GENERIC); 2706 1.1 christos if (Offlog > OffFSELog) return ERROR(corruption_detected); 2707 1.1 christos ip += headerSize; 2708 1.1 christos FSE_buildDTable(DTableOffb, norm, max, Offlog); 2709 1.1 christos } } 2710 1.1 christos 2711 1.1 christos switch(MLtype) 2712 1.1 christos { 2713 1.1 christos case bt_rle : 2714 1.1 christos MLlog = 0; 2715 1.1 christos if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */ 2716 1.1 christos FSE_buildDTable_rle(DTableML, *ip++); break; 2717 1.1 christos case bt_raw : 2718 1.1 christos MLlog = MLbits; 2719 1.1 christos FSE_buildDTable_raw(DTableML, MLbits); break; 2720 1.1 christos default : 2721 1.1 christos { U32 max = MaxML; 2722 1.1 christos headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip); 2723 1.1 christos if (FSE_isError(headerSize)) return ERROR(GENERIC); 2724 1.1 christos if (MLlog > MLFSELog) return ERROR(corruption_detected); 2725 1.1 christos ip += headerSize; 2726 1.1 christos FSE_buildDTable(DTableML, norm, max, MLlog); 2727 1.1 christos } } } 2728 1.1 christos 2729 1.1 christos return ip-istart; 2730 1.1 christos } 2731 1.1 christos 2732 1.1 christos 2733 1.1 christos typedef struct { 2734 1.1 christos size_t litLength; 2735 1.1 christos size_t offset; 2736 1.1 christos size_t matchLength; 2737 1.1 christos } seq_t; 2738 1.1 christos 2739 1.1 christos typedef struct { 2740 1.1 christos BIT_DStream_t DStream; 2741 1.1 christos FSE_DState_t stateLL; 2742 1.1 christos FSE_DState_t stateOffb; 2743 1.1 christos FSE_DState_t stateML; 2744 1.1 christos size_t prevOffset; 2745 1.1 christos const BYTE* dumps; 2746 1.1 christos const BYTE* dumpsEnd; 2747 1.1 christos } seqState_t; 2748 1.1 christos 2749 1.1 christos 2750 1.1 christos static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState) 2751 1.1 christos { 2752 1.1 christos size_t litLength; 2753 1.1 christos size_t prevOffset; 2754 1.1 christos size_t offset; 2755 1.1 christos size_t matchLength; 2756 1.1 christos const BYTE* dumps = seqState->dumps; 2757 1.1 christos const BYTE* const de = seqState->dumpsEnd; 2758 1.1 christos 2759 1.1 christos /* Literal length */ 2760 1.1 christos litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream)); 2761 1.1 christos prevOffset = litLength ? seq->offset : seqState->prevOffset; 2762 1.1 christos if (litLength == MaxLL) { 2763 1.1 christos const U32 add = dumps<de ? *dumps++ : 0; 2764 1.1 christos if (add < 255) litLength += add; 2765 1.1 christos else if (dumps + 3 <= de) { 2766 1.1 christos litLength = MEM_readLE24(dumps); 2767 1.1 christos dumps += 3; 2768 1.1 christos } 2769 1.1 christos if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */ 2770 1.1 christos } 2771 1.1 christos 2772 1.1 christos /* Offset */ 2773 1.1 christos { static const U32 offsetPrefix[MaxOff+1] = { 2774 1.1 christos 1 /*fake*/, 1, 2, 4, 8, 16, 32, 64, 128, 256, 2775 1.1 christos 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 2776 1.1 christos 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, /*fake*/ 1, 1, 1, 1, 1 }; 2777 1.1 christos U32 offsetCode, nbBits; 2778 1.1 christos offsetCode = FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream)); /* <= maxOff, by table construction */ 2779 1.1 christos if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream)); 2780 1.1 christos nbBits = offsetCode - 1; 2781 1.1 christos if (offsetCode==0) nbBits = 0; /* cmove */ 2782 1.1 christos offset = offsetPrefix[offsetCode] + BIT_readBits(&(seqState->DStream), nbBits); 2783 1.1 christos if (MEM_32bits()) BIT_reloadDStream(&(seqState->DStream)); 2784 1.1 christos if (offsetCode==0) offset = prevOffset; /* cmove */ 2785 1.1 christos if (offsetCode | !litLength) seqState->prevOffset = seq->offset; /* cmove */ 2786 1.1 christos } 2787 1.1 christos 2788 1.1 christos /* MatchLength */ 2789 1.1 christos matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream)); 2790 1.1 christos if (matchLength == MaxML) { 2791 1.1 christos const U32 add = dumps<de ? *dumps++ : 0; 2792 1.1 christos if (add < 255) matchLength += add; 2793 1.1 christos else if (dumps + 3 <= de){ 2794 1.1 christos matchLength = MEM_readLE24(dumps); 2795 1.1 christos dumps += 3; 2796 1.1 christos } 2797 1.1 christos if (dumps >= de) { dumps = de-1; } /* late correction, to avoid read overflow (data is now corrupted anyway) */ 2798 1.1 christos } 2799 1.1 christos matchLength += MINMATCH; 2800 1.1 christos 2801 1.1 christos /* save result */ 2802 1.1 christos seq->litLength = litLength; 2803 1.1 christos seq->offset = offset; 2804 1.1 christos seq->matchLength = matchLength; 2805 1.1 christos seqState->dumps = dumps; 2806 1.1 christos } 2807 1.1 christos 2808 1.1 christos 2809 1.1 christos static size_t ZSTD_execSequence(BYTE* op, 2810 1.1 christos BYTE* const oend, seq_t sequence, 2811 1.1 christos const BYTE** litPtr, const BYTE* const litLimit, 2812 1.1 christos const BYTE* const base, const BYTE* const vBase, const BYTE* const dictEnd) 2813 1.1 christos { 2814 1.1 christos static const int dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 }; /* added */ 2815 1.1 christos static const int dec64table[] = { 8, 8, 8, 7, 8, 9,10,11 }; /* subtracted */ 2816 1.1 christos BYTE* const oLitEnd = op + sequence.litLength; 2817 1.1 christos const size_t sequenceLength = sequence.litLength + sequence.matchLength; 2818 1.1 christos BYTE* const oMatchEnd = op + sequenceLength; /* risk : address space overflow (32-bits) */ 2819 1.1 christos BYTE* const oend_8 = oend-8; 2820 1.1 christos const BYTE* const litEnd = *litPtr + sequence.litLength; 2821 1.1 christos const BYTE* match = oLitEnd - sequence.offset; 2822 1.1 christos 2823 1.1 christos /* checks */ 2824 1.1 christos size_t const seqLength = sequence.litLength + sequence.matchLength; 2825 1.1 christos 2826 1.1 christos if (seqLength > (size_t)(oend - op)) return ERROR(dstSize_tooSmall); 2827 1.1 christos if (sequence.litLength > (size_t)(litLimit - *litPtr)) return ERROR(corruption_detected); 2828 1.1 christos /* Now we know there are no overflow in literal nor match lengths, can use pointer checks */ 2829 1.1 christos if (oLitEnd > oend_8) return ERROR(dstSize_tooSmall); 2830 1.1 christos 2831 1.1 christos if (oMatchEnd > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */ 2832 1.1 christos if (litEnd > litLimit) return ERROR(corruption_detected); /* overRead beyond lit buffer */ 2833 1.1 christos 2834 1.1 christos /* copy Literals */ 2835 1.1 christos ZSTD_wildcopy(op, *litPtr, (ptrdiff_t)sequence.litLength); /* note : oLitEnd <= oend-8 : no risk of overwrite beyond oend */ 2836 1.1 christos op = oLitEnd; 2837 1.1 christos *litPtr = litEnd; /* update for next sequence */ 2838 1.1 christos 2839 1.1 christos /* copy Match */ 2840 1.1 christos if (sequence.offset > (size_t)(oLitEnd - base)) 2841 1.1 christos { 2842 1.1 christos /* offset beyond prefix */ 2843 1.1 christos if (sequence.offset > (size_t)(oLitEnd - vBase)) 2844 1.1 christos return ERROR(corruption_detected); 2845 1.1 christos match = dictEnd - (base-match); 2846 1.1 christos if (match + sequence.matchLength <= dictEnd) 2847 1.1 christos { 2848 1.1 christos memmove(oLitEnd, match, sequence.matchLength); 2849 1.1 christos return sequenceLength; 2850 1.1 christos } 2851 1.1 christos /* span extDict & currentPrefixSegment */ 2852 1.1 christos { 2853 1.1 christos size_t length1 = dictEnd - match; 2854 1.1 christos memmove(oLitEnd, match, length1); 2855 1.1 christos op = oLitEnd + length1; 2856 1.1 christos sequence.matchLength -= length1; 2857 1.1 christos match = base; 2858 1.1 christos if (op > oend_8 || sequence.matchLength < MINMATCH) { 2859 1.1 christos while (op < oMatchEnd) *op++ = *match++; 2860 1.1 christos return sequenceLength; 2861 1.1 christos } 2862 1.1 christos } 2863 1.1 christos } 2864 1.1 christos /* Requirement: op <= oend_8 */ 2865 1.1 christos 2866 1.1 christos /* match within prefix */ 2867 1.1 christos if (sequence.offset < 8) { 2868 1.1 christos /* close range match, overlap */ 2869 1.1 christos const int sub2 = dec64table[sequence.offset]; 2870 1.1 christos op[0] = match[0]; 2871 1.1 christos op[1] = match[1]; 2872 1.1 christos op[2] = match[2]; 2873 1.1 christos op[3] = match[3]; 2874 1.1 christos match += dec32table[sequence.offset]; 2875 1.1 christos ZSTD_copy4(op+4, match); 2876 1.1 christos match -= sub2; 2877 1.1 christos } else { 2878 1.1 christos ZSTD_copy8(op, match); 2879 1.1 christos } 2880 1.1 christos op += 8; match += 8; 2881 1.1 christos 2882 1.1 christos if (oMatchEnd > oend-(16-MINMATCH)) 2883 1.1 christos { 2884 1.1 christos if (op < oend_8) 2885 1.1 christos { 2886 1.1 christos ZSTD_wildcopy(op, match, oend_8 - op); 2887 1.1 christos match += oend_8 - op; 2888 1.1 christos op = oend_8; 2889 1.1 christos } 2890 1.1 christos while (op < oMatchEnd) *op++ = *match++; 2891 1.1 christos } 2892 1.1 christos else 2893 1.1 christos { 2894 1.1 christos ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8, but must be signed */ 2895 1.1 christos } 2896 1.1 christos return sequenceLength; 2897 1.1 christos } 2898 1.1 christos 2899 1.1 christos 2900 1.1 christos static size_t ZSTD_decompressSequences( 2901 1.1 christos ZSTD_DCtx* dctx, 2902 1.1 christos void* dst, size_t maxDstSize, 2903 1.1 christos const void* seqStart, size_t seqSize) 2904 1.1 christos { 2905 1.1 christos const BYTE* ip = (const BYTE*)seqStart; 2906 1.1 christos const BYTE* const iend = ip + seqSize; 2907 1.1 christos BYTE* const ostart = (BYTE* const)dst; 2908 1.1 christos BYTE* op = ostart; 2909 1.1 christos BYTE* const oend = ostart + maxDstSize; 2910 1.1 christos size_t errorCode, dumpsLength; 2911 1.1 christos const BYTE* litPtr = dctx->litPtr; 2912 1.1 christos const BYTE* const litEnd = litPtr + dctx->litSize; 2913 1.1 christos int nbSeq; 2914 1.1 christos const BYTE* dumps; 2915 1.1 christos U32* DTableLL = dctx->LLTable; 2916 1.1 christos U32* DTableML = dctx->MLTable; 2917 1.1 christos U32* DTableOffb = dctx->OffTable; 2918 1.1 christos const BYTE* const base = (const BYTE*) (dctx->base); 2919 1.1 christos const BYTE* const vBase = (const BYTE*) (dctx->vBase); 2920 1.1 christos const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd); 2921 1.1 christos 2922 1.1 christos /* Build Decoding Tables */ 2923 1.1 christos errorCode = ZSTD_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength, 2924 1.1 christos DTableLL, DTableML, DTableOffb, 2925 1.1 christos ip, iend-ip); 2926 1.1 christos if (ZSTD_isError(errorCode)) return errorCode; 2927 1.1 christos ip += errorCode; 2928 1.1 christos 2929 1.1 christos /* Regen sequences */ 2930 1.1 christos { 2931 1.1 christos seq_t sequence; 2932 1.1 christos seqState_t seqState; 2933 1.1 christos 2934 1.1 christos memset(&sequence, 0, sizeof(sequence)); 2935 1.1 christos sequence.offset = 4; 2936 1.1 christos seqState.dumps = dumps; 2937 1.1 christos seqState.dumpsEnd = dumps + dumpsLength; 2938 1.1 christos seqState.prevOffset = 4; 2939 1.1 christos errorCode = BIT_initDStream(&(seqState.DStream), ip, iend-ip); 2940 1.1 christos if (ERR_isError(errorCode)) return ERROR(corruption_detected); 2941 1.1 christos FSE_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL); 2942 1.1 christos FSE_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb); 2943 1.1 christos FSE_initDState(&(seqState.stateML), &(seqState.DStream), DTableML); 2944 1.1 christos 2945 1.1 christos for ( ; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_completed) && nbSeq ; ) 2946 1.1 christos { 2947 1.1 christos size_t oneSeqSize; 2948 1.1 christos nbSeq--; 2949 1.1 christos ZSTD_decodeSequence(&sequence, &seqState); 2950 1.1 christos oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, base, vBase, dictEnd); 2951 1.1 christos if (ZSTD_isError(oneSeqSize)) return oneSeqSize; 2952 1.1 christos op += oneSeqSize; 2953 1.1 christos } 2954 1.1 christos 2955 1.1 christos /* check if reached exact end */ 2956 1.1 christos if ( !BIT_endOfDStream(&(seqState.DStream)) ) return ERROR(corruption_detected); /* DStream should be entirely and exactly consumed; otherwise data is corrupted */ 2957 1.1 christos 2958 1.1 christos /* last literal segment */ 2959 1.1 christos { 2960 1.1 christos size_t lastLLSize = litEnd - litPtr; 2961 1.1 christos if (litPtr > litEnd) return ERROR(corruption_detected); 2962 1.1 christos if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall); 2963 1.1 christos if (lastLLSize > 0) { 2964 1.1 christos if (op != litPtr) memcpy(op, litPtr, lastLLSize); 2965 1.1 christos op += lastLLSize; 2966 1.1 christos } 2967 1.1 christos } 2968 1.1 christos } 2969 1.1 christos 2970 1.1 christos return op-ostart; 2971 1.1 christos } 2972 1.1 christos 2973 1.1 christos 2974 1.1 christos static void ZSTD_checkContinuity(ZSTD_DCtx* dctx, const void* dst) 2975 1.1 christos { 2976 1.1 christos if (dst != dctx->previousDstEnd) /* not contiguous */ 2977 1.1 christos { 2978 1.1 christos dctx->dictEnd = dctx->previousDstEnd; 2979 1.1 christos dctx->vBase = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->base)); 2980 1.1 christos dctx->base = dst; 2981 1.1 christos dctx->previousDstEnd = dst; 2982 1.1 christos } 2983 1.1 christos } 2984 1.1 christos 2985 1.1 christos 2986 1.1 christos static size_t ZSTD_decompressBlock_internal(ZSTD_DCtx* dctx, 2987 1.1 christos void* dst, size_t maxDstSize, 2988 1.1 christos const void* src, size_t srcSize) 2989 1.1 christos { 2990 1.1 christos /* blockType == blockCompressed */ 2991 1.1 christos const BYTE* ip = (const BYTE*)src; 2992 1.1 christos size_t litCSize; 2993 1.1 christos 2994 1.1 christos if (srcSize > BLOCKSIZE) return ERROR(corruption_detected); 2995 1.1 christos 2996 1.1 christos /* Decode literals sub-block */ 2997 1.1 christos litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize); 2998 1.1 christos if (ZSTD_isError(litCSize)) return litCSize; 2999 1.1 christos ip += litCSize; 3000 1.1 christos srcSize -= litCSize; 3001 1.1 christos 3002 1.1 christos return ZSTD_decompressSequences(dctx, dst, maxDstSize, ip, srcSize); 3003 1.1 christos } 3004 1.1 christos 3005 1.1 christos 3006 1.1 christos static size_t ZSTD_decompress_usingDict(ZSTD_DCtx* ctx, 3007 1.1 christos void* dst, size_t maxDstSize, 3008 1.1 christos const void* src, size_t srcSize, 3009 1.1 christos const void* dict, size_t dictSize) 3010 1.1 christos { 3011 1.1 christos const BYTE* ip = (const BYTE*)src; 3012 1.1 christos const BYTE* iend = ip + srcSize; 3013 1.1 christos BYTE* const ostart = (BYTE* const)dst; 3014 1.1 christos BYTE* op = ostart; 3015 1.1 christos BYTE* const oend = ostart + maxDstSize; 3016 1.1 christos size_t remainingSize = srcSize; 3017 1.1 christos blockProperties_t blockProperties; 3018 1.1 christos 3019 1.1 christos /* init */ 3020 1.1 christos ZSTD_resetDCtx(ctx); 3021 1.1 christos if (dict) 3022 1.1 christos { 3023 1.1 christos ZSTD_decompress_insertDictionary(ctx, dict, dictSize); 3024 1.1 christos ctx->dictEnd = ctx->previousDstEnd; 3025 1.1 christos ctx->vBase = (const char*)dst - ((const char*)(ctx->previousDstEnd) - (const char*)(ctx->base)); 3026 1.1 christos ctx->base = dst; 3027 1.1 christos } 3028 1.1 christos else 3029 1.1 christos { 3030 1.1 christos ctx->vBase = ctx->base = ctx->dictEnd = dst; 3031 1.1 christos } 3032 1.1 christos 3033 1.1 christos /* Frame Header */ 3034 1.1 christos { 3035 1.1 christos size_t frameHeaderSize; 3036 1.1 christos if (srcSize < ZSTD_frameHeaderSize_min+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong); 3037 1.1 christos frameHeaderSize = ZSTD_decodeFrameHeader_Part1(ctx, src, ZSTD_frameHeaderSize_min); 3038 1.1 christos if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize; 3039 1.1 christos if (srcSize < frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong); 3040 1.1 christos ip += frameHeaderSize; remainingSize -= frameHeaderSize; 3041 1.1 christos frameHeaderSize = ZSTD_decodeFrameHeader_Part2(ctx, src, frameHeaderSize); 3042 1.1 christos if (ZSTD_isError(frameHeaderSize)) return frameHeaderSize; 3043 1.1 christos } 3044 1.1 christos 3045 1.1 christos /* Loop on each block */ 3046 1.1 christos while (1) 3047 1.1 christos { 3048 1.1 christos size_t decodedSize=0; 3049 1.1 christos size_t cBlockSize = ZSTD_getcBlockSize(ip, iend-ip, &blockProperties); 3050 1.1 christos if (ZSTD_isError(cBlockSize)) return cBlockSize; 3051 1.1 christos 3052 1.1 christos ip += ZSTD_blockHeaderSize; 3053 1.1 christos remainingSize -= ZSTD_blockHeaderSize; 3054 1.1 christos if (cBlockSize > remainingSize) return ERROR(srcSize_wrong); 3055 1.1 christos 3056 1.1 christos switch(blockProperties.blockType) 3057 1.1 christos { 3058 1.1 christos case bt_compressed: 3059 1.1 christos decodedSize = ZSTD_decompressBlock_internal(ctx, op, oend-op, ip, cBlockSize); 3060 1.1 christos break; 3061 1.1 christos case bt_raw : 3062 1.1 christos decodedSize = ZSTD_copyRawBlock(op, oend-op, ip, cBlockSize); 3063 1.1 christos break; 3064 1.1 christos case bt_rle : 3065 1.1 christos return ERROR(GENERIC); /* not yet supported */ 3066 1.1 christos break; 3067 1.1 christos case bt_end : 3068 1.1 christos /* end of frame */ 3069 1.1 christos if (remainingSize) return ERROR(srcSize_wrong); 3070 1.1 christos break; 3071 1.1 christos default: 3072 1.1 christos return ERROR(GENERIC); /* impossible */ 3073 1.1 christos } 3074 1.1 christos if (cBlockSize == 0) break; /* bt_end */ 3075 1.1 christos 3076 1.1 christos if (ZSTD_isError(decodedSize)) return decodedSize; 3077 1.1 christos op += decodedSize; 3078 1.1 christos ip += cBlockSize; 3079 1.1 christos remainingSize -= cBlockSize; 3080 1.1 christos } 3081 1.1 christos 3082 1.1 christos return op-ostart; 3083 1.1 christos } 3084 1.1 christos 3085 1.1 christos /* ZSTD_errorFrameSizeInfoLegacy() : 3086 1.1 christos assumes `cSize` and `dBound` are _not_ NULL */ 3087 1.1 christos static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret) 3088 1.1 christos { 3089 1.1 christos *cSize = ret; 3090 1.1 christos *dBound = ZSTD_CONTENTSIZE_ERROR; 3091 1.1 christos } 3092 1.1 christos 3093 1.1 christos void ZSTDv04_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound) 3094 1.1 christos { 3095 1.1 christos const BYTE* ip = (const BYTE*)src; 3096 1.1 christos size_t remainingSize = srcSize; 3097 1.1 christos size_t nbBlocks = 0; 3098 1.1 christos blockProperties_t blockProperties; 3099 1.1 christos 3100 1.1 christos /* Frame Header */ 3101 1.1 christos if (srcSize < ZSTD_frameHeaderSize_min) { 3102 1.1 christos ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong)); 3103 1.1 christos return; 3104 1.1 christos } 3105 1.1 christos if (MEM_readLE32(src) != ZSTD_MAGICNUMBER) { 3106 1.1 christos ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown)); 3107 1.1 christos return; 3108 1.1 christos } 3109 1.1 christos ip += ZSTD_frameHeaderSize_min; remainingSize -= ZSTD_frameHeaderSize_min; 3110 1.1 christos 3111 1.1 christos /* Loop on each block */ 3112 1.1 christos while (1) 3113 1.1 christos { 3114 1.1 christos size_t cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties); 3115 1.1 christos if (ZSTD_isError(cBlockSize)) { 3116 1.1 christos ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, cBlockSize); 3117 1.1 christos return; 3118 1.1 christos } 3119 1.1 christos 3120 1.1 christos ip += ZSTD_blockHeaderSize; 3121 1.1 christos remainingSize -= ZSTD_blockHeaderSize; 3122 1.1 christos if (cBlockSize > remainingSize) { 3123 1.1 christos ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong)); 3124 1.1 christos return; 3125 1.1 christos } 3126 1.1 christos 3127 1.1 christos if (cBlockSize == 0) break; /* bt_end */ 3128 1.1 christos 3129 1.1 christos ip += cBlockSize; 3130 1.1 christos remainingSize -= cBlockSize; 3131 1.1 christos nbBlocks++; 3132 1.1 christos } 3133 1.1 christos 3134 1.1 christos *cSize = ip - (const BYTE*)src; 3135 1.1 christos *dBound = nbBlocks * BLOCKSIZE; 3136 1.1 christos } 3137 1.1 christos 3138 1.1 christos /* ****************************** 3139 1.1 christos * Streaming Decompression API 3140 1.1 christos ********************************/ 3141 1.1 christos static size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx) 3142 1.1 christos { 3143 1.1 christos return dctx->expected; 3144 1.1 christos } 3145 1.1 christos 3146 1.1 christos static size_t ZSTD_decompressContinue(ZSTD_DCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) 3147 1.1 christos { 3148 1.1 christos /* Sanity check */ 3149 1.1 christos if (srcSize != ctx->expected) return ERROR(srcSize_wrong); 3150 1.1 christos ZSTD_checkContinuity(ctx, dst); 3151 1.1 christos 3152 1.1 christos /* Decompress : frame header; part 1 */ 3153 1.1 christos switch (ctx->stage) 3154 1.1 christos { 3155 1.1 christos case ZSTDds_getFrameHeaderSize : 3156 1.1 christos /* get frame header size */ 3157 1.1 christos if (srcSize != ZSTD_frameHeaderSize_min) return ERROR(srcSize_wrong); /* impossible */ 3158 1.1 christos ctx->headerSize = ZSTD_decodeFrameHeader_Part1(ctx, src, ZSTD_frameHeaderSize_min); 3159 1.1 christos if (ZSTD_isError(ctx->headerSize)) return ctx->headerSize; 3160 1.1 christos memcpy(ctx->headerBuffer, src, ZSTD_frameHeaderSize_min); 3161 1.1 christos if (ctx->headerSize > ZSTD_frameHeaderSize_min) return ERROR(GENERIC); /* impossible */ 3162 1.1 christos ctx->expected = 0; /* not necessary to copy more */ 3163 1.1 christos /* fallthrough */ 3164 1.1 christos case ZSTDds_decodeFrameHeader: 3165 1.1 christos /* get frame header */ 3166 1.1 christos { size_t const result = ZSTD_decodeFrameHeader_Part2(ctx, ctx->headerBuffer, ctx->headerSize); 3167 1.1 christos if (ZSTD_isError(result)) return result; 3168 1.1 christos ctx->expected = ZSTD_blockHeaderSize; 3169 1.1 christos ctx->stage = ZSTDds_decodeBlockHeader; 3170 1.1 christos return 0; 3171 1.1 christos } 3172 1.1 christos case ZSTDds_decodeBlockHeader: 3173 1.1 christos /* Decode block header */ 3174 1.1 christos { blockProperties_t bp; 3175 1.1 christos size_t const blockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp); 3176 1.1 christos if (ZSTD_isError(blockSize)) return blockSize; 3177 1.1 christos if (bp.blockType == bt_end) 3178 1.1 christos { 3179 1.1 christos ctx->expected = 0; 3180 1.1 christos ctx->stage = ZSTDds_getFrameHeaderSize; 3181 1.1 christos } 3182 1.1 christos else 3183 1.1 christos { 3184 1.1 christos ctx->expected = blockSize; 3185 1.1 christos ctx->bType = bp.blockType; 3186 1.1 christos ctx->stage = ZSTDds_decompressBlock; 3187 1.1 christos } 3188 1.1 christos return 0; 3189 1.1 christos } 3190 1.1 christos case ZSTDds_decompressBlock: 3191 1.1 christos { 3192 1.1 christos /* Decompress : block content */ 3193 1.1 christos size_t rSize; 3194 1.1 christos switch(ctx->bType) 3195 1.1 christos { 3196 1.1 christos case bt_compressed: 3197 1.1 christos rSize = ZSTD_decompressBlock_internal(ctx, dst, maxDstSize, src, srcSize); 3198 1.1 christos break; 3199 1.1 christos case bt_raw : 3200 1.1 christos rSize = ZSTD_copyRawBlock(dst, maxDstSize, src, srcSize); 3201 1.1 christos break; 3202 1.1 christos case bt_rle : 3203 1.1 christos return ERROR(GENERIC); /* not yet handled */ 3204 1.1 christos break; 3205 1.1 christos case bt_end : /* should never happen (filtered at phase 1) */ 3206 1.1 christos rSize = 0; 3207 1.1 christos break; 3208 1.1 christos default: 3209 1.1 christos return ERROR(GENERIC); 3210 1.1 christos } 3211 1.1 christos ctx->stage = ZSTDds_decodeBlockHeader; 3212 1.1 christos ctx->expected = ZSTD_blockHeaderSize; 3213 1.1 christos if (ZSTD_isError(rSize)) return rSize; 3214 1.1 christos ctx->previousDstEnd = (char*)dst + rSize; 3215 1.1 christos return rSize; 3216 1.1 christos } 3217 1.1 christos default: 3218 1.1 christos return ERROR(GENERIC); /* impossible */ 3219 1.1 christos } 3220 1.1 christos } 3221 1.1 christos 3222 1.1 christos 3223 1.1 christos static void ZSTD_decompress_insertDictionary(ZSTD_DCtx* ctx, const void* dict, size_t dictSize) 3224 1.1 christos { 3225 1.1 christos ctx->dictEnd = ctx->previousDstEnd; 3226 1.1 christos ctx->vBase = (const char*)dict - ((const char*)(ctx->previousDstEnd) - (const char*)(ctx->base)); 3227 1.1 christos ctx->base = dict; 3228 1.1 christos ctx->previousDstEnd = (const char*)dict + dictSize; 3229 1.1 christos } 3230 1.1 christos 3231 1.1 christos 3232 1.1 christos 3233 1.1 christos /* 3234 1.1 christos Buffered version of Zstd compression library 3235 1.1 christos Copyright (C) 2015, Yann Collet. 3236 1.1 christos 3237 1.1 christos BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php) 3238 1.1 christos 3239 1.1 christos Redistribution and use in source and binary forms, with or without 3240 1.1 christos modification, are permitted provided that the following conditions are 3241 1.1 christos met: 3242 1.1 christos * Redistributions of source code must retain the above copyright 3243 1.1 christos notice, this list of conditions and the following disclaimer. 3244 1.1 christos * Redistributions in binary form must reproduce the above 3245 1.1 christos copyright notice, this list of conditions and the following disclaimer 3246 1.1 christos in the documentation and/or other materials provided with the 3247 1.1 christos distribution. 3248 1.1 christos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 3249 1.1 christos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3250 1.1 christos LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 3251 1.1 christos A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3252 1.1 christos OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3253 1.1 christos SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3254 1.1 christos LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3255 1.1 christos DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3256 1.1 christos THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3257 1.1 christos (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3258 1.1 christos OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3259 1.1 christos 3260 1.1 christos You can contact the author at : 3261 1.1 christos - zstd source repository : https://github.com/Cyan4973/zstd 3262 1.1 christos - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c 3263 1.1 christos */ 3264 1.1 christos 3265 1.1 christos /* The objects defined into this file should be considered experimental. 3266 1.1 christos * They are not labelled stable, as their prototype may change in the future. 3267 1.1 christos * You can use them for tests, provide feedback, or if you can endure risk of future changes. 3268 1.1 christos */ 3269 1.1 christos 3270 1.1 christos /* ************************************* 3271 1.1 christos * Includes 3272 1.1 christos ***************************************/ 3273 1.1 christos #include <stdlib.h> 3274 1.1 christos 3275 1.1 christos 3276 1.1 christos /** ************************************************ 3277 1.1 christos * Streaming decompression 3278 1.1 christos * 3279 1.1 christos * A ZBUFF_DCtx object is required to track streaming operation. 3280 1.1 christos * Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources. 3281 1.1 christos * Use ZBUFF_decompressInit() to start a new decompression operation. 3282 1.1 christos * ZBUFF_DCtx objects can be reused multiple times. 3283 1.1 christos * 3284 1.1 christos * Use ZBUFF_decompressContinue() repetitively to consume your input. 3285 1.1 christos * *srcSizePtr and *maxDstSizePtr can be any size. 3286 1.1 christos * The function will report how many bytes were read or written by modifying *srcSizePtr and *maxDstSizePtr. 3287 1.1 christos * Note that it may not consume the entire input, in which case it's up to the caller to call again the function with remaining input. 3288 1.1 christos * The content of dst will be overwritten (up to *maxDstSizePtr) at each function call, so save its content if it matters or change dst . 3289 1.1 christos * return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency) 3290 1.1 christos * or 0 when a frame is completely decoded 3291 1.1 christos * or an error code, which can be tested using ZBUFF_isError(). 3292 1.1 christos * 3293 1.1 christos * Hint : recommended buffer sizes (not compulsory) 3294 1.1 christos * output : 128 KB block size is the internal unit, it ensures it's always possible to write a full block when it's decoded. 3295 1.1 christos * input : just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 . 3296 1.1 christos * **************************************************/ 3297 1.1 christos 3298 1.1 christos typedef enum { ZBUFFds_init, ZBUFFds_readHeader, ZBUFFds_loadHeader, ZBUFFds_decodeHeader, 3299 1.1 christos ZBUFFds_read, ZBUFFds_load, ZBUFFds_flush } ZBUFF_dStage; 3300 1.1 christos 3301 1.1 christos /* *** Resource management *** */ 3302 1.1 christos 3303 1.1 christos #define ZSTD_frameHeaderSize_max 5 /* too magical, should come from reference */ 3304 1.1 christos struct ZBUFFv04_DCtx_s { 3305 1.1 christos ZSTD_DCtx* zc; 3306 1.1 christos ZSTD_parameters params; 3307 1.1 christos char* inBuff; 3308 1.1 christos size_t inBuffSize; 3309 1.1 christos size_t inPos; 3310 1.1 christos char* outBuff; 3311 1.1 christos size_t outBuffSize; 3312 1.1 christos size_t outStart; 3313 1.1 christos size_t outEnd; 3314 1.1 christos size_t hPos; 3315 1.1 christos const char* dict; 3316 1.1 christos size_t dictSize; 3317 1.1 christos ZBUFF_dStage stage; 3318 1.1 christos unsigned char headerBuffer[ZSTD_frameHeaderSize_max]; 3319 1.1 christos }; /* typedef'd to ZBUFF_DCtx within "zstd_buffered.h" */ 3320 1.1 christos 3321 1.1 christos typedef ZBUFFv04_DCtx ZBUFF_DCtx; 3322 1.1 christos 3323 1.1 christos 3324 1.1 christos static ZBUFF_DCtx* ZBUFF_createDCtx(void) 3325 1.1 christos { 3326 1.1 christos ZBUFF_DCtx* zbc = (ZBUFF_DCtx*)malloc(sizeof(ZBUFF_DCtx)); 3327 1.1 christos if (zbc==NULL) return NULL; 3328 1.1 christos memset(zbc, 0, sizeof(*zbc)); 3329 1.1 christos zbc->zc = ZSTD_createDCtx(); 3330 1.1 christos zbc->stage = ZBUFFds_init; 3331 1.1 christos return zbc; 3332 1.1 christos } 3333 1.1 christos 3334 1.1 christos static size_t ZBUFF_freeDCtx(ZBUFF_DCtx* zbc) 3335 1.1 christos { 3336 1.1 christos if (zbc==NULL) return 0; /* support free on null */ 3337 1.1 christos ZSTD_freeDCtx(zbc->zc); 3338 1.1 christos free(zbc->inBuff); 3339 1.1 christos free(zbc->outBuff); 3340 1.1 christos free(zbc); 3341 1.1 christos return 0; 3342 1.1 christos } 3343 1.1 christos 3344 1.1 christos 3345 1.1 christos /* *** Initialization *** */ 3346 1.1 christos 3347 1.1 christos static size_t ZBUFF_decompressInit(ZBUFF_DCtx* zbc) 3348 1.1 christos { 3349 1.1 christos zbc->stage = ZBUFFds_readHeader; 3350 1.1 christos zbc->hPos = zbc->inPos = zbc->outStart = zbc->outEnd = zbc->dictSize = 0; 3351 1.1 christos return ZSTD_resetDCtx(zbc->zc); 3352 1.1 christos } 3353 1.1 christos 3354 1.1 christos 3355 1.1 christos static size_t ZBUFF_decompressWithDictionary(ZBUFF_DCtx* zbc, const void* src, size_t srcSize) 3356 1.1 christos { 3357 1.1 christos zbc->dict = (const char*)src; 3358 1.1 christos zbc->dictSize = srcSize; 3359 1.1 christos return 0; 3360 1.1 christos } 3361 1.1 christos 3362 1.1 christos static size_t ZBUFF_limitCopy(void* dst, size_t maxDstSize, const void* src, size_t srcSize) 3363 1.1 christos { 3364 1.1 christos size_t length = MIN(maxDstSize, srcSize); 3365 1.1 christos if (length > 0) { 3366 1.1 christos memcpy(dst, src, length); 3367 1.1 christos } 3368 1.1 christos return length; 3369 1.1 christos } 3370 1.1 christos 3371 1.1 christos /* *** Decompression *** */ 3372 1.1 christos 3373 1.1 christos static size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDstSizePtr, const void* src, size_t* srcSizePtr) 3374 1.1 christos { 3375 1.1 christos const char* const istart = (const char*)src; 3376 1.1 christos const char* ip = istart; 3377 1.1 christos const char* const iend = istart + *srcSizePtr; 3378 1.1 christos char* const ostart = (char*)dst; 3379 1.1 christos char* op = ostart; 3380 1.1 christos char* const oend = ostart + *maxDstSizePtr; 3381 1.1 christos U32 notDone = 1; 3382 1.1 christos 3383 1.1 christos DEBUGLOG(5, "ZBUFF_decompressContinue"); 3384 1.1 christos while (notDone) 3385 1.1 christos { 3386 1.1 christos switch(zbc->stage) 3387 1.1 christos { 3388 1.1 christos 3389 1.1 christos case ZBUFFds_init : 3390 1.1 christos DEBUGLOG(5, "ZBUFF_decompressContinue: stage==ZBUFFds_init => ERROR(init_missing)"); 3391 1.1 christos return ERROR(init_missing); 3392 1.1 christos 3393 1.1 christos case ZBUFFds_readHeader : 3394 1.1 christos /* read header from src */ 3395 1.1 christos { size_t const headerSize = ZSTD_getFrameParams(&(zbc->params), src, *srcSizePtr); 3396 1.1 christos if (ZSTD_isError(headerSize)) return headerSize; 3397 1.1 christos if (headerSize) { 3398 1.1 christos /* not enough input to decode header : tell how many bytes would be necessary */ 3399 1.1 christos memcpy(zbc->headerBuffer+zbc->hPos, src, *srcSizePtr); 3400 1.1 christos zbc->hPos += *srcSizePtr; 3401 1.1 christos *maxDstSizePtr = 0; 3402 1.1 christos zbc->stage = ZBUFFds_loadHeader; 3403 1.1 christos return headerSize - zbc->hPos; 3404 1.1 christos } 3405 1.1 christos zbc->stage = ZBUFFds_decodeHeader; 3406 1.1 christos break; 3407 1.1 christos } 3408 1.1 christos 3409 1.1 christos case ZBUFFds_loadHeader: 3410 1.1 christos /* complete header from src */ 3411 1.1 christos { size_t headerSize = ZBUFF_limitCopy( 3412 1.1 christos zbc->headerBuffer + zbc->hPos, ZSTD_frameHeaderSize_max - zbc->hPos, 3413 1.1 christos src, *srcSizePtr); 3414 1.1 christos zbc->hPos += headerSize; 3415 1.1 christos ip += headerSize; 3416 1.1 christos headerSize = ZSTD_getFrameParams(&(zbc->params), zbc->headerBuffer, zbc->hPos); 3417 1.1 christos if (ZSTD_isError(headerSize)) return headerSize; 3418 1.1 christos if (headerSize) { 3419 1.1 christos /* not enough input to decode header : tell how many bytes would be necessary */ 3420 1.1 christos *maxDstSizePtr = 0; 3421 1.1 christos return headerSize - zbc->hPos; 3422 1.1 christos } } 3423 1.1 christos /* intentional fallthrough */ 3424 1.1 christos 3425 1.1 christos case ZBUFFds_decodeHeader: 3426 1.1 christos /* apply header to create / resize buffers */ 3427 1.1 christos { size_t const neededOutSize = (size_t)1 << zbc->params.windowLog; 3428 1.1 christos size_t const neededInSize = BLOCKSIZE; /* a block is never > BLOCKSIZE */ 3429 1.1 christos if (zbc->inBuffSize < neededInSize) { 3430 1.1 christos free(zbc->inBuff); 3431 1.1 christos zbc->inBuffSize = neededInSize; 3432 1.1 christos zbc->inBuff = (char*)malloc(neededInSize); 3433 1.1 christos if (zbc->inBuff == NULL) return ERROR(memory_allocation); 3434 1.1 christos } 3435 1.1 christos if (zbc->outBuffSize < neededOutSize) { 3436 1.1 christos free(zbc->outBuff); 3437 1.1 christos zbc->outBuffSize = neededOutSize; 3438 1.1 christos zbc->outBuff = (char*)malloc(neededOutSize); 3439 1.1 christos if (zbc->outBuff == NULL) return ERROR(memory_allocation); 3440 1.1 christos } } 3441 1.1 christos if (zbc->dictSize) 3442 1.1 christos ZSTD_decompress_insertDictionary(zbc->zc, zbc->dict, zbc->dictSize); 3443 1.1 christos if (zbc->hPos) { 3444 1.1 christos /* some data already loaded into headerBuffer : transfer into inBuff */ 3445 1.1 christos memcpy(zbc->inBuff, zbc->headerBuffer, zbc->hPos); 3446 1.1 christos zbc->inPos = zbc->hPos; 3447 1.1 christos zbc->hPos = 0; 3448 1.1 christos zbc->stage = ZBUFFds_load; 3449 1.1 christos break; 3450 1.1 christos } 3451 1.1 christos zbc->stage = ZBUFFds_read; 3452 1.1 christos /* fall-through */ 3453 1.1 christos case ZBUFFds_read: 3454 1.1 christos { 3455 1.1 christos size_t neededInSize = ZSTD_nextSrcSizeToDecompress(zbc->zc); 3456 1.1 christos if (neededInSize==0) /* end of frame */ 3457 1.1 christos { 3458 1.1 christos zbc->stage = ZBUFFds_init; 3459 1.1 christos notDone = 0; 3460 1.1 christos break; 3461 1.1 christos } 3462 1.1 christos if ((size_t)(iend-ip) >= neededInSize) 3463 1.1 christos { 3464 1.1 christos /* directly decode from src */ 3465 1.1 christos size_t decodedSize = ZSTD_decompressContinue(zbc->zc, 3466 1.1 christos zbc->outBuff + zbc->outStart, zbc->outBuffSize - zbc->outStart, 3467 1.1 christos ip, neededInSize); 3468 1.1 christos if (ZSTD_isError(decodedSize)) return decodedSize; 3469 1.1 christos ip += neededInSize; 3470 1.1 christos if (!decodedSize) break; /* this was just a header */ 3471 1.1 christos zbc->outEnd = zbc->outStart + decodedSize; 3472 1.1 christos zbc->stage = ZBUFFds_flush; 3473 1.1 christos break; 3474 1.1 christos } 3475 1.1 christos if (ip==iend) { notDone = 0; break; } /* no more input */ 3476 1.1 christos zbc->stage = ZBUFFds_load; 3477 1.1 christos } 3478 1.1 christos /* fall-through */ 3479 1.1 christos case ZBUFFds_load: 3480 1.1 christos { 3481 1.1 christos size_t neededInSize = ZSTD_nextSrcSizeToDecompress(zbc->zc); 3482 1.1 christos size_t toLoad = neededInSize - zbc->inPos; /* should always be <= remaining space within inBuff */ 3483 1.1 christos size_t loadedSize; 3484 1.1 christos if (toLoad > zbc->inBuffSize - zbc->inPos) return ERROR(corruption_detected); /* should never happen */ 3485 1.1 christos loadedSize = ZBUFF_limitCopy(zbc->inBuff + zbc->inPos, toLoad, ip, iend-ip); 3486 1.1 christos ip += loadedSize; 3487 1.1 christos zbc->inPos += loadedSize; 3488 1.1 christos if (loadedSize < toLoad) { notDone = 0; break; } /* not enough input, wait for more */ 3489 1.1 christos { 3490 1.1 christos size_t decodedSize = ZSTD_decompressContinue(zbc->zc, 3491 1.1 christos zbc->outBuff + zbc->outStart, zbc->outBuffSize - zbc->outStart, 3492 1.1 christos zbc->inBuff, neededInSize); 3493 1.1 christos if (ZSTD_isError(decodedSize)) return decodedSize; 3494 1.1 christos zbc->inPos = 0; /* input is consumed */ 3495 1.1 christos if (!decodedSize) { zbc->stage = ZBUFFds_read; break; } /* this was just a header */ 3496 1.1 christos zbc->outEnd = zbc->outStart + decodedSize; 3497 1.1 christos zbc->stage = ZBUFFds_flush; 3498 1.1 christos /* ZBUFFds_flush follows */ 3499 1.1 christos } 3500 1.1 christos } 3501 1.1 christos /* fall-through */ 3502 1.1 christos case ZBUFFds_flush: 3503 1.1 christos { 3504 1.1 christos size_t toFlushSize = zbc->outEnd - zbc->outStart; 3505 1.1 christos size_t flushedSize = ZBUFF_limitCopy(op, oend-op, zbc->outBuff + zbc->outStart, toFlushSize); 3506 1.1 christos op += flushedSize; 3507 1.1 christos zbc->outStart += flushedSize; 3508 1.1 christos if (flushedSize == toFlushSize) 3509 1.1 christos { 3510 1.1 christos zbc->stage = ZBUFFds_read; 3511 1.1 christos if (zbc->outStart + BLOCKSIZE > zbc->outBuffSize) 3512 1.1 christos zbc->outStart = zbc->outEnd = 0; 3513 1.1 christos break; 3514 1.1 christos } 3515 1.1 christos /* cannot flush everything */ 3516 1.1 christos notDone = 0; 3517 1.1 christos break; 3518 1.1 christos } 3519 1.1 christos default: return ERROR(GENERIC); /* impossible */ 3520 1.1 christos } 3521 1.1 christos } 3522 1.1 christos 3523 1.1 christos *srcSizePtr = ip-istart; 3524 1.1 christos *maxDstSizePtr = op-ostart; 3525 1.1 christos 3526 1.1 christos { 3527 1.1 christos size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zbc->zc); 3528 1.1 christos if (nextSrcSizeHint > 3) nextSrcSizeHint+= 3; /* get the next block header while at it */ 3529 1.1 christos nextSrcSizeHint -= zbc->inPos; /* already loaded*/ 3530 1.1 christos return nextSrcSizeHint; 3531 1.1 christos } 3532 1.1 christos } 3533 1.1 christos 3534 1.1 christos 3535 1.1 christos /* ************************************* 3536 1.1 christos * Tool functions 3537 1.1 christos ***************************************/ 3538 1.1 christos unsigned ZBUFFv04_isError(size_t errorCode) { return ERR_isError(errorCode); } 3539 1.1 christos const char* ZBUFFv04_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); } 3540 1.1 christos 3541 1.1 christos size_t ZBUFFv04_recommendedDInSize(void) { return BLOCKSIZE + 3; } 3542 1.1 christos size_t ZBUFFv04_recommendedDOutSize(void) { return BLOCKSIZE; } 3543 1.1 christos 3544 1.1 christos 3545 1.1 christos 3546 1.1 christos /*- ========================================================================= -*/ 3547 1.1 christos 3548 1.1 christos /* final wrapping stage */ 3549 1.1 christos 3550 1.1 christos size_t ZSTDv04_decompressDCtx(ZSTD_DCtx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) 3551 1.1 christos { 3552 1.1 christos return ZSTD_decompress_usingDict(dctx, dst, maxDstSize, src, srcSize, NULL, 0); 3553 1.1 christos } 3554 1.1 christos 3555 1.1 christos size_t ZSTDv04_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize) 3556 1.1 christos { 3557 1.1 christos #if defined(ZSTD_HEAPMODE) && (ZSTD_HEAPMODE==1) 3558 1.1 christos size_t regenSize; 3559 1.1 christos ZSTD_DCtx* dctx = ZSTD_createDCtx(); 3560 1.1 christos if (dctx==NULL) return ERROR(memory_allocation); 3561 1.1 christos regenSize = ZSTDv04_decompressDCtx(dctx, dst, maxDstSize, src, srcSize); 3562 1.1 christos ZSTD_freeDCtx(dctx); 3563 1.1 christos return regenSize; 3564 1.1 christos #else 3565 1.1 christos ZSTD_DCtx dctx; 3566 1.1 christos return ZSTDv04_decompressDCtx(&dctx, dst, maxDstSize, src, srcSize); 3567 1.1 christos #endif 3568 1.1 christos } 3569 1.1 christos 3570 1.1 christos size_t ZSTDv04_resetDCtx(ZSTDv04_Dctx* dctx) { return ZSTD_resetDCtx(dctx); } 3571 1.1 christos 3572 1.1 christos size_t ZSTDv04_nextSrcSizeToDecompress(ZSTDv04_Dctx* dctx) 3573 1.1 christos { 3574 1.1 christos return ZSTD_nextSrcSizeToDecompress(dctx); 3575 1.1 christos } 3576 1.1 christos 3577 1.1 christos size_t ZSTDv04_decompressContinue(ZSTDv04_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) 3578 1.1 christos { 3579 1.1 christos return ZSTD_decompressContinue(dctx, dst, maxDstSize, src, srcSize); 3580 1.1 christos } 3581 1.1 christos 3582 1.1 christos 3583 1.1 christos 3584 1.1 christos ZBUFFv04_DCtx* ZBUFFv04_createDCtx(void) { return ZBUFF_createDCtx(); } 3585 1.1 christos size_t ZBUFFv04_freeDCtx(ZBUFFv04_DCtx* dctx) { return ZBUFF_freeDCtx(dctx); } 3586 1.1 christos 3587 1.1 christos size_t ZBUFFv04_decompressInit(ZBUFFv04_DCtx* dctx) { return ZBUFF_decompressInit(dctx); } 3588 1.1 christos size_t ZBUFFv04_decompressWithDictionary(ZBUFFv04_DCtx* dctx, const void* src, size_t srcSize) 3589 1.1 christos { return ZBUFF_decompressWithDictionary(dctx, src, srcSize); } 3590 1.1 christos 3591 1.1 christos size_t ZBUFFv04_decompressContinue(ZBUFFv04_DCtx* dctx, void* dst, size_t* maxDstSizePtr, const void* src, size_t* srcSizePtr) 3592 1.1 christos { 3593 1.1 christos DEBUGLOG(5, "ZBUFFv04_decompressContinue"); 3594 1.1 christos return ZBUFF_decompressContinue(dctx, dst, maxDstSizePtr, src, srcSizePtr); 3595 1.1 christos } 3596 1.1 christos 3597 1.1 christos ZSTD_DCtx* ZSTDv04_createDCtx(void) { return ZSTD_createDCtx(); } 3598 1.1 christos size_t ZSTDv04_freeDCtx(ZSTD_DCtx* dctx) { return ZSTD_freeDCtx(dctx); } 3599