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