Path: blob/main/sys/contrib/zstd/lib/legacy/zstd_v01.c
48378 views
/*1* Copyright (c) Yann Collet, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/91011/******************************************12* Includes13******************************************/14#include <stddef.h> /* size_t, ptrdiff_t */15#include "zstd_v01.h"16#include "../common/error_private.h"171819/******************************************20* Static allocation21******************************************/22/* You can statically allocate FSE CTable/DTable as a table of unsigned using below macro */23#define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog))2425/* You can statically allocate Huff0 DTable as a table of unsigned short using below macro */26#define HUF_DTABLE_SIZE_U16(maxTableLog) (1 + (1<<maxTableLog))27#define HUF_CREATE_STATIC_DTABLE(DTable, maxTableLog) \28unsigned short DTable[HUF_DTABLE_SIZE_U16(maxTableLog)] = { maxTableLog }293031/******************************************32* Error Management33******************************************/34#define FSE_LIST_ERRORS(ITEM) \35ITEM(FSE_OK_NoError) ITEM(FSE_ERROR_GENERIC) \36ITEM(FSE_ERROR_tableLog_tooLarge) ITEM(FSE_ERROR_maxSymbolValue_tooLarge) ITEM(FSE_ERROR_maxSymbolValue_tooSmall) \37ITEM(FSE_ERROR_dstSize_tooSmall) ITEM(FSE_ERROR_srcSize_wrong)\38ITEM(FSE_ERROR_corruptionDetected) \39ITEM(FSE_ERROR_maxCode)4041#define FSE_GENERATE_ENUM(ENUM) ENUM,42typedef enum { FSE_LIST_ERRORS(FSE_GENERATE_ENUM) } FSE_errorCodes; /* enum is exposed, to detect & handle specific errors; compare function result to -enum value */434445/******************************************46* FSE symbol compression API47******************************************/48/*49This API consists of small unitary functions, which highly benefit from being inlined.50You will want to enable link-time-optimization to ensure these functions are properly inlined in your binary.51Visual seems to do it automatically.52For gcc or clang, you'll need to add -flto flag at compilation and linking stages.53If none of these solutions is applicable, include "fse.c" directly.54*/5556typedef unsigned FSE_CTable; /* don't allocate that. It's just a way to be more restrictive than void* */57typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */5859typedef struct60{61size_t bitContainer;62int bitPos;63char* startPtr;64char* ptr;65char* endPtr;66} FSE_CStream_t;6768typedef struct69{70ptrdiff_t value;71const void* stateTable;72const void* symbolTT;73unsigned stateLog;74} FSE_CState_t;7576typedef struct77{78size_t bitContainer;79unsigned bitsConsumed;80const char* ptr;81const char* start;82} FSE_DStream_t;8384typedef struct85{86size_t state;87const void* table; /* precise table may vary, depending on U16 */88} FSE_DState_t;8990typedef enum { FSE_DStream_unfinished = 0,91FSE_DStream_endOfBuffer = 1,92FSE_DStream_completed = 2,93FSE_DStream_tooFar = 3 } FSE_DStream_status; /* result of FSE_reloadDStream() */94/* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... ?! */959697/****************************************************************98* Tuning parameters99****************************************************************/100/* MEMORY_USAGE :101* Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)102* Increasing memory usage improves compression ratio103* Reduced memory usage can improve speed, due to cache effect104* Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */105#define FSE_MAX_MEMORY_USAGE 14106#define FSE_DEFAULT_MEMORY_USAGE 13107108/* FSE_MAX_SYMBOL_VALUE :109* Maximum symbol value authorized.110* Required for proper stack allocation */111#define FSE_MAX_SYMBOL_VALUE 255112113114/****************************************************************115* template functions type & suffix116****************************************************************/117#define FSE_FUNCTION_TYPE BYTE118#define FSE_FUNCTION_EXTENSION119120121/****************************************************************122* Byte symbol type123****************************************************************/124typedef struct125{126unsigned short newState;127unsigned char symbol;128unsigned char nbBits;129} FSE_decode_t; /* size == U32 */130131132133/****************************************************************134* Compiler specifics135****************************************************************/136#ifdef _MSC_VER /* Visual Studio */137# define FORCE_INLINE static __forceinline138# include <intrin.h> /* For Visual 2005 */139# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */140# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */141#else142# define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)143# if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */144# ifdef __GNUC__145# define FORCE_INLINE static inline __attribute__((always_inline))146# else147# define FORCE_INLINE static inline148# endif149# else150# define FORCE_INLINE static151# endif /* __STDC_VERSION__ */152#endif153154155/****************************************************************156* Includes157****************************************************************/158#include <stdlib.h> /* malloc, free, qsort */159#include <string.h> /* memcpy, memset */160#include <stdio.h> /* printf (debug) */161162163#ifndef MEM_ACCESS_MODULE164#define MEM_ACCESS_MODULE165/****************************************************************166* Basic Types167*****************************************************************/168#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */169# include <stdint.h>170typedef uint8_t BYTE;171typedef uint16_t U16;172typedef int16_t S16;173typedef uint32_t U32;174typedef int32_t S32;175typedef uint64_t U64;176typedef int64_t S64;177#else178typedef unsigned char BYTE;179typedef unsigned short U16;180typedef signed short S16;181typedef unsigned int U32;182typedef signed int S32;183typedef unsigned long long U64;184typedef signed long long S64;185#endif186187#endif /* MEM_ACCESS_MODULE */188189/****************************************************************190* Memory I/O191*****************************************************************/192/* FSE_FORCE_MEMORY_ACCESS193* By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.194* Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.195* The below switch allow to select different access method for improved performance.196* Method 0 (default) : use `memcpy()`. Safe and portable.197* Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).198* This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.199* Method 2 : direct access. This method is portable but violate C standard.200* It can generate buggy code on targets generating assembly depending on alignment.201* But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)202* See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.203* Prefer these methods in priority order (0 > 1 > 2)204*/205#ifndef FSE_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */206# if defined(__INTEL_COMPILER) || defined(__GNUC__) || defined(__ICCARM__)207# define FSE_FORCE_MEMORY_ACCESS 1208# endif209#endif210211212static unsigned FSE_32bits(void)213{214return sizeof(void*)==4;215}216217static unsigned FSE_isLittleEndian(void)218{219const union { U32 i; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */220return one.c[0];221}222223#if defined(FSE_FORCE_MEMORY_ACCESS) && (FSE_FORCE_MEMORY_ACCESS==2)224225static U16 FSE_read16(const void* memPtr) { return *(const U16*) memPtr; }226static U32 FSE_read32(const void* memPtr) { return *(const U32*) memPtr; }227static U64 FSE_read64(const void* memPtr) { return *(const U64*) memPtr; }228229#elif defined(FSE_FORCE_MEMORY_ACCESS) && (FSE_FORCE_MEMORY_ACCESS==1)230231/* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */232/* currently only defined for gcc and icc */233typedef union { U16 u16; U32 u32; U64 u64; } __attribute__((packed)) unalign;234235static U16 FSE_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }236static U32 FSE_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }237static U64 FSE_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }238239#else240241static U16 FSE_read16(const void* memPtr)242{243U16 val; memcpy(&val, memPtr, sizeof(val)); return val;244}245246static U32 FSE_read32(const void* memPtr)247{248U32 val; memcpy(&val, memPtr, sizeof(val)); return val;249}250251static U64 FSE_read64(const void* memPtr)252{253U64 val; memcpy(&val, memPtr, sizeof(val)); return val;254}255256#endif /* FSE_FORCE_MEMORY_ACCESS */257258static U16 FSE_readLE16(const void* memPtr)259{260if (FSE_isLittleEndian())261return FSE_read16(memPtr);262else263{264const BYTE* p = (const BYTE*)memPtr;265return (U16)(p[0] + (p[1]<<8));266}267}268269static U32 FSE_readLE32(const void* memPtr)270{271if (FSE_isLittleEndian())272return FSE_read32(memPtr);273else274{275const BYTE* p = (const BYTE*)memPtr;276return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24));277}278}279280281static U64 FSE_readLE64(const void* memPtr)282{283if (FSE_isLittleEndian())284return FSE_read64(memPtr);285else286{287const BYTE* p = (const BYTE*)memPtr;288return (U64)((U64)p[0] + ((U64)p[1]<<8) + ((U64)p[2]<<16) + ((U64)p[3]<<24)289+ ((U64)p[4]<<32) + ((U64)p[5]<<40) + ((U64)p[6]<<48) + ((U64)p[7]<<56));290}291}292293static size_t FSE_readLEST(const void* memPtr)294{295if (FSE_32bits())296return (size_t)FSE_readLE32(memPtr);297else298return (size_t)FSE_readLE64(memPtr);299}300301302303/****************************************************************304* Constants305*****************************************************************/306#define FSE_MAX_TABLELOG (FSE_MAX_MEMORY_USAGE-2)307#define FSE_MAX_TABLESIZE (1U<<FSE_MAX_TABLELOG)308#define FSE_MAXTABLESIZE_MASK (FSE_MAX_TABLESIZE-1)309#define FSE_DEFAULT_TABLELOG (FSE_DEFAULT_MEMORY_USAGE-2)310#define FSE_MIN_TABLELOG 5311312#define FSE_TABLELOG_ABSOLUTE_MAX 15313#if FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX314#error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported"315#endif316317318/****************************************************************319* Error Management320****************************************************************/321#define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */322323324/****************************************************************325* Complex types326****************************************************************/327typedef struct328{329int deltaFindState;330U32 deltaNbBits;331} FSE_symbolCompressionTransform; /* total 8 bytes */332333typedef U32 DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];334335/****************************************************************336* Internal functions337****************************************************************/338FORCE_INLINE unsigned FSE_highbit32 (U32 val)339{340# if defined(_MSC_VER) /* Visual */341unsigned long r;342return _BitScanReverse(&r, val) ? (unsigned)r : 0;343# elif defined(__GNUC__) && (GCC_VERSION >= 304) /* GCC Intrinsic */344return __builtin_clz (val) ^ 31;345# else /* Software version */346static 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 };347U32 v = val;348unsigned r;349v |= v >> 1;350v |= v >> 2;351v |= v >> 4;352v |= v >> 8;353v |= v >> 16;354r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];355return r;356# endif357}358359360/****************************************************************361* Templates362****************************************************************/363/*364designed to be included365for type-specific functions (template emulation in C)366Objective is to write these functions only once, for improved maintenance367*/368369/* safety checks */370#ifndef FSE_FUNCTION_EXTENSION371# error "FSE_FUNCTION_EXTENSION must be defined"372#endif373#ifndef FSE_FUNCTION_TYPE374# error "FSE_FUNCTION_TYPE must be defined"375#endif376377/* Function names */378#define FSE_CAT(X,Y) X##Y379#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)380#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)381382383384static U32 FSE_tableStep(U32 tableSize) { return (tableSize>>1) + (tableSize>>3) + 3; }385386#define FSE_DECODE_TYPE FSE_decode_t387388389typedef struct {390U16 tableLog;391U16 fastMode;392} FSE_DTableHeader; /* sizeof U32 */393394static size_t FSE_buildDTable395(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)396{397void* ptr = dt;398FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;399FSE_DECODE_TYPE* const tableDecode = (FSE_DECODE_TYPE*)(ptr) + 1; /* because dt is unsigned, 32-bits aligned on 32-bits */400const U32 tableSize = 1 << tableLog;401const U32 tableMask = tableSize-1;402const U32 step = FSE_tableStep(tableSize);403U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];404U32 position = 0;405U32 highThreshold = tableSize-1;406const S16 largeLimit= (S16)(1 << (tableLog-1));407U32 noLarge = 1;408U32 s;409410/* Sanity Checks */411if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) return (size_t)-FSE_ERROR_maxSymbolValue_tooLarge;412if (tableLog > FSE_MAX_TABLELOG) return (size_t)-FSE_ERROR_tableLog_tooLarge;413414/* Init, lay down lowprob symbols */415DTableH[0].tableLog = (U16)tableLog;416for (s=0; s<=maxSymbolValue; s++)417{418if (normalizedCounter[s]==-1)419{420tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s;421symbolNext[s] = 1;422}423else424{425if (normalizedCounter[s] >= largeLimit) noLarge=0;426symbolNext[s] = normalizedCounter[s];427}428}429430/* Spread symbols */431for (s=0; s<=maxSymbolValue; s++)432{433int i;434for (i=0; i<normalizedCounter[s]; i++)435{436tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;437position = (position + step) & tableMask;438while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */439}440}441442if (position!=0) return (size_t)-FSE_ERROR_GENERIC; /* position must reach all cells once, otherwise normalizedCounter is incorrect */443444/* Build Decoding table */445{446U32 i;447for (i=0; i<tableSize; i++)448{449FSE_FUNCTION_TYPE symbol = (FSE_FUNCTION_TYPE)(tableDecode[i].symbol);450U16 nextState = symbolNext[symbol]++;451tableDecode[i].nbBits = (BYTE) (tableLog - FSE_highbit32 ((U32)nextState) );452tableDecode[i].newState = (U16) ( (nextState << tableDecode[i].nbBits) - tableSize);453}454}455456DTableH->fastMode = (U16)noLarge;457return 0;458}459460461/******************************************462* FSE byte symbol463******************************************/464#ifndef FSE_COMMONDEFS_ONLY465466static unsigned FSE_isError(size_t code) { return (code > (size_t)(-FSE_ERROR_maxCode)); }467468static short FSE_abs(short a)469{470return a<0? -a : a;471}472473474/****************************************************************475* Header bitstream management476****************************************************************/477static size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,478const void* headerBuffer, size_t hbSize)479{480const BYTE* const istart = (const BYTE*) headerBuffer;481const BYTE* const iend = istart + hbSize;482const BYTE* ip = istart;483int nbBits;484int remaining;485int threshold;486U32 bitStream;487int bitCount;488unsigned charnum = 0;489int previous0 = 0;490491if (hbSize < 4) return (size_t)-FSE_ERROR_srcSize_wrong;492bitStream = FSE_readLE32(ip);493nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */494if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return (size_t)-FSE_ERROR_tableLog_tooLarge;495bitStream >>= 4;496bitCount = 4;497*tableLogPtr = nbBits;498remaining = (1<<nbBits)+1;499threshold = 1<<nbBits;500nbBits++;501502while ((remaining>1) && (charnum<=*maxSVPtr))503{504if (previous0)505{506unsigned n0 = charnum;507while ((bitStream & 0xFFFF) == 0xFFFF)508{509n0+=24;510if (ip < iend-5)511{512ip+=2;513bitStream = FSE_readLE32(ip) >> bitCount;514}515else516{517bitStream >>= 16;518bitCount+=16;519}520}521while ((bitStream & 3) == 3)522{523n0+=3;524bitStream>>=2;525bitCount+=2;526}527n0 += bitStream & 3;528bitCount += 2;529if (n0 > *maxSVPtr) return (size_t)-FSE_ERROR_maxSymbolValue_tooSmall;530while (charnum < n0) normalizedCounter[charnum++] = 0;531if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))532{533ip += bitCount>>3;534bitCount &= 7;535bitStream = FSE_readLE32(ip) >> bitCount;536}537else538bitStream >>= 2;539}540{541const short max = (short)((2*threshold-1)-remaining);542short count;543544if ((bitStream & (threshold-1)) < (U32)max)545{546count = (short)(bitStream & (threshold-1));547bitCount += nbBits-1;548}549else550{551count = (short)(bitStream & (2*threshold-1));552if (count >= threshold) count -= max;553bitCount += nbBits;554}555556count--; /* extra accuracy */557remaining -= FSE_abs(count);558normalizedCounter[charnum++] = count;559previous0 = !count;560while (remaining < threshold)561{562nbBits--;563threshold >>= 1;564}565566{567if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))568{569ip += bitCount>>3;570bitCount &= 7;571}572else573{574bitCount -= (int)(8 * (iend - 4 - ip));575ip = iend - 4;576}577bitStream = FSE_readLE32(ip) >> (bitCount & 31);578}579}580}581if (remaining != 1) return (size_t)-FSE_ERROR_GENERIC;582*maxSVPtr = charnum-1;583584ip += (bitCount+7)>>3;585if ((size_t)(ip-istart) > hbSize) return (size_t)-FSE_ERROR_srcSize_wrong;586return ip-istart;587}588589590/*********************************************************591* Decompression (Byte symbols)592*********************************************************/593static size_t FSE_buildDTable_rle (FSE_DTable* dt, BYTE symbolValue)594{595void* ptr = dt;596FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;597FSE_decode_t* const cell = (FSE_decode_t*)(ptr) + 1; /* because dt is unsigned */598599DTableH->tableLog = 0;600DTableH->fastMode = 0;601602cell->newState = 0;603cell->symbol = symbolValue;604cell->nbBits = 0;605606return 0;607}608609610static size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)611{612void* ptr = dt;613FSE_DTableHeader* const DTableH = (FSE_DTableHeader*)ptr;614FSE_decode_t* const dinfo = (FSE_decode_t*)(ptr) + 1; /* because dt is unsigned */615const unsigned tableSize = 1 << nbBits;616const unsigned tableMask = tableSize - 1;617const unsigned maxSymbolValue = tableMask;618unsigned s;619620/* Sanity checks */621if (nbBits < 1) return (size_t)-FSE_ERROR_GENERIC; /* min size */622623/* Build Decoding Table */624DTableH->tableLog = (U16)nbBits;625DTableH->fastMode = 1;626for (s=0; s<=maxSymbolValue; s++)627{628dinfo[s].newState = 0;629dinfo[s].symbol = (BYTE)s;630dinfo[s].nbBits = (BYTE)nbBits;631}632633return 0;634}635636637/* FSE_initDStream638* Initialize a FSE_DStream_t.639* srcBuffer must point at the beginning of an FSE block.640* The function result is the size of the FSE_block (== srcSize).641* If srcSize is too small, the function will return an errorCode;642*/643static size_t FSE_initDStream(FSE_DStream_t* bitD, const void* srcBuffer, size_t srcSize)644{645if (srcSize < 1) return (size_t)-FSE_ERROR_srcSize_wrong;646647if (srcSize >= sizeof(size_t))648{649U32 contain32;650bitD->start = (const char*)srcBuffer;651bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t);652bitD->bitContainer = FSE_readLEST(bitD->ptr);653contain32 = ((const BYTE*)srcBuffer)[srcSize-1];654if (contain32 == 0) return (size_t)-FSE_ERROR_GENERIC; /* stop bit not present */655bitD->bitsConsumed = 8 - FSE_highbit32(contain32);656}657else658{659U32 contain32;660bitD->start = (const char*)srcBuffer;661bitD->ptr = bitD->start;662bitD->bitContainer = *(const BYTE*)(bitD->start);663switch(srcSize)664{665case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16);666/* fallthrough */667case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24);668/* fallthrough */669case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32);670/* fallthrough */671case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24;672/* fallthrough */673case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16;674/* fallthrough */675case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8;676/* fallthrough */677default:;678}679contain32 = ((const BYTE*)srcBuffer)[srcSize-1];680if (contain32 == 0) return (size_t)-FSE_ERROR_GENERIC; /* stop bit not present */681bitD->bitsConsumed = 8 - FSE_highbit32(contain32);682bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8;683}684685return srcSize;686}687688689/*!FSE_lookBits690* Provides next n bits from the bitContainer.691* bitContainer is not modified (bits are still present for next read/look)692* On 32-bits, maxNbBits==25693* On 64-bits, maxNbBits==57694* return : value extracted.695*/696static size_t FSE_lookBits(FSE_DStream_t* bitD, U32 nbBits)697{698const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;699return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);700}701702static size_t FSE_lookBitsFast(FSE_DStream_t* bitD, U32 nbBits) /* only if nbBits >= 1 !! */703{704const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1;705return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);706}707708static void FSE_skipBits(FSE_DStream_t* bitD, U32 nbBits)709{710bitD->bitsConsumed += nbBits;711}712713714/*!FSE_readBits715* Read next n bits from the bitContainer.716* On 32-bits, don't read more than maxNbBits==25717* On 64-bits, don't read more than maxNbBits==57718* Use the fast variant *only* if n >= 1.719* return : value extracted.720*/721static size_t FSE_readBits(FSE_DStream_t* bitD, U32 nbBits)722{723size_t value = FSE_lookBits(bitD, nbBits);724FSE_skipBits(bitD, nbBits);725return value;726}727728static size_t FSE_readBitsFast(FSE_DStream_t* bitD, U32 nbBits) /* only if nbBits >= 1 !! */729{730size_t value = FSE_lookBitsFast(bitD, nbBits);731FSE_skipBits(bitD, nbBits);732return value;733}734735static unsigned FSE_reloadDStream(FSE_DStream_t* bitD)736{737if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */738return FSE_DStream_tooFar;739740if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer))741{742bitD->ptr -= bitD->bitsConsumed >> 3;743bitD->bitsConsumed &= 7;744bitD->bitContainer = FSE_readLEST(bitD->ptr);745return FSE_DStream_unfinished;746}747if (bitD->ptr == bitD->start)748{749if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return FSE_DStream_endOfBuffer;750return FSE_DStream_completed;751}752{753U32 nbBytes = bitD->bitsConsumed >> 3;754U32 result = FSE_DStream_unfinished;755if (bitD->ptr - nbBytes < bitD->start)756{757nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */758result = FSE_DStream_endOfBuffer;759}760bitD->ptr -= nbBytes;761bitD->bitsConsumed -= nbBytes*8;762bitD->bitContainer = FSE_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */763return result;764}765}766767768static void FSE_initDState(FSE_DState_t* DStatePtr, FSE_DStream_t* bitD, const FSE_DTable* dt)769{770const void* ptr = dt;771const FSE_DTableHeader* const DTableH = (const FSE_DTableHeader*)ptr;772DStatePtr->state = FSE_readBits(bitD, DTableH->tableLog);773FSE_reloadDStream(bitD);774DStatePtr->table = dt + 1;775}776777static BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, FSE_DStream_t* bitD)778{779const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];780const U32 nbBits = DInfo.nbBits;781BYTE symbol = DInfo.symbol;782size_t lowBits = FSE_readBits(bitD, nbBits);783784DStatePtr->state = DInfo.newState + lowBits;785return symbol;786}787788static BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, FSE_DStream_t* bitD)789{790const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];791const U32 nbBits = DInfo.nbBits;792BYTE symbol = DInfo.symbol;793size_t lowBits = FSE_readBitsFast(bitD, nbBits);794795DStatePtr->state = DInfo.newState + lowBits;796return symbol;797}798799/* FSE_endOfDStream800Tells if bitD has reached end of bitStream or not */801802static unsigned FSE_endOfDStream(const FSE_DStream_t* bitD)803{804return ((bitD->ptr == bitD->start) && (bitD->bitsConsumed == sizeof(bitD->bitContainer)*8));805}806807static unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)808{809return DStatePtr->state == 0;810}811812813FORCE_INLINE size_t FSE_decompress_usingDTable_generic(814void* dst, size_t maxDstSize,815const void* cSrc, size_t cSrcSize,816const FSE_DTable* dt, const unsigned fast)817{818BYTE* const ostart = (BYTE*) dst;819BYTE* op = ostart;820BYTE* const omax = op + maxDstSize;821BYTE* const olimit = omax-3;822823FSE_DStream_t bitD;824FSE_DState_t state1;825FSE_DState_t state2;826size_t errorCode;827828/* Init */829errorCode = FSE_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */830if (FSE_isError(errorCode)) return errorCode;831832FSE_initDState(&state1, &bitD, dt);833FSE_initDState(&state2, &bitD, dt);834835#define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)836837/* 4 symbols per loop */838for ( ; (FSE_reloadDStream(&bitD)==FSE_DStream_unfinished) && (op<olimit) ; op+=4)839{840op[0] = FSE_GETSYMBOL(&state1);841842if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */843FSE_reloadDStream(&bitD);844845op[1] = FSE_GETSYMBOL(&state2);846847if (FSE_MAX_TABLELOG*4+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */848{ if (FSE_reloadDStream(&bitD) > FSE_DStream_unfinished) { op+=2; break; } }849850op[2] = FSE_GETSYMBOL(&state1);851852if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */853FSE_reloadDStream(&bitD);854855op[3] = FSE_GETSYMBOL(&state2);856}857858/* tail */859/* note : FSE_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly FSE_DStream_completed */860while (1)861{862if ( (FSE_reloadDStream(&bitD)>FSE_DStream_completed) || (op==omax) || (FSE_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state1))) )863break;864865*op++ = FSE_GETSYMBOL(&state1);866867if ( (FSE_reloadDStream(&bitD)>FSE_DStream_completed) || (op==omax) || (FSE_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state2))) )868break;869870*op++ = FSE_GETSYMBOL(&state2);871}872873/* end ? */874if (FSE_endOfDStream(&bitD) && FSE_endOfDState(&state1) && FSE_endOfDState(&state2))875return op-ostart;876877if (op==omax) return (size_t)-FSE_ERROR_dstSize_tooSmall; /* dst buffer is full, but cSrc unfinished */878879return (size_t)-FSE_ERROR_corruptionDetected;880}881882883static size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,884const void* cSrc, size_t cSrcSize,885const FSE_DTable* dt)886{887FSE_DTableHeader DTableH;888memcpy(&DTableH, dt, sizeof(DTableH)); /* memcpy() into local variable, to avoid strict aliasing warning */889890/* select fast mode (static) */891if (DTableH.fastMode) return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1);892return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0);893}894895896static size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize)897{898const BYTE* const istart = (const BYTE*)cSrc;899const BYTE* ip = istart;900short counting[FSE_MAX_SYMBOL_VALUE+1];901DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */902unsigned tableLog;903unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;904size_t errorCode;905906if (cSrcSize<2) return (size_t)-FSE_ERROR_srcSize_wrong; /* too small input size */907908/* normal FSE decoding mode */909errorCode = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);910if (FSE_isError(errorCode)) return errorCode;911if (errorCode >= cSrcSize) return (size_t)-FSE_ERROR_srcSize_wrong; /* too small input size */912ip += errorCode;913cSrcSize -= errorCode;914915errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog);916if (FSE_isError(errorCode)) return errorCode;917918/* always return, even if it is an error code */919return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt);920}921922923924/* *******************************************************925* Huff0 : Huffman block compression926*********************************************************/927#define HUF_MAX_SYMBOL_VALUE 255928#define HUF_DEFAULT_TABLELOG 12 /* used by default, when not specified */929#define HUF_MAX_TABLELOG 12 /* max possible tableLog; for allocation purpose; can be modified */930#define HUF_ABSOLUTEMAX_TABLELOG 16 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */931#if (HUF_MAX_TABLELOG > HUF_ABSOLUTEMAX_TABLELOG)932# error "HUF_MAX_TABLELOG is too large !"933#endif934935typedef struct HUF_CElt_s {936U16 val;937BYTE nbBits;938} HUF_CElt ;939940typedef struct nodeElt_s {941U32 count;942U16 parent;943BYTE byte;944BYTE nbBits;945} nodeElt;946947948/* *******************************************************949* Huff0 : Huffman block decompression950*********************************************************/951typedef struct {952BYTE byte;953BYTE nbBits;954} HUF_DElt;955956static size_t HUF_readDTable (U16* DTable, const void* src, size_t srcSize)957{958BYTE huffWeight[HUF_MAX_SYMBOL_VALUE + 1];959U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; /* large enough for values from 0 to 16 */960U32 weightTotal;961U32 maxBits;962const BYTE* ip = (const BYTE*) src;963size_t iSize;964size_t oSize;965U32 n;966U32 nextRankStart;967void* ptr = DTable+1;968HUF_DElt* const dt = (HUF_DElt*)ptr;969970if (!srcSize) return (size_t)-FSE_ERROR_srcSize_wrong;971iSize = ip[0];972973FSE_STATIC_ASSERT(sizeof(HUF_DElt) == sizeof(U16)); /* if compilation fails here, assertion is false */974//memset(huffWeight, 0, sizeof(huffWeight)); /* should not be necessary, but some analyzer complain ... */975if (iSize >= 128) /* special header */976{977if (iSize >= (242)) /* RLE */978{979static int l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 };980oSize = l[iSize-242];981memset(huffWeight, 1, sizeof(huffWeight));982iSize = 0;983}984else /* Incompressible */985{986oSize = iSize - 127;987iSize = ((oSize+1)/2);988if (iSize+1 > srcSize) return (size_t)-FSE_ERROR_srcSize_wrong;989ip += 1;990for (n=0; n<oSize; n+=2)991{992huffWeight[n] = ip[n/2] >> 4;993huffWeight[n+1] = ip[n/2] & 15;994}995}996}997else /* header compressed with FSE (normal case) */998{999if (iSize+1 > srcSize) return (size_t)-FSE_ERROR_srcSize_wrong;1000oSize = FSE_decompress(huffWeight, HUF_MAX_SYMBOL_VALUE, ip+1, iSize); /* max 255 values decoded, last one is implied */1001if (FSE_isError(oSize)) return oSize;1002}10031004/* collect weight stats */1005memset(rankVal, 0, sizeof(rankVal));1006weightTotal = 0;1007for (n=0; n<oSize; n++)1008{1009if (huffWeight[n] >= HUF_ABSOLUTEMAX_TABLELOG) return (size_t)-FSE_ERROR_corruptionDetected;1010rankVal[huffWeight[n]]++;1011weightTotal += (1 << huffWeight[n]) >> 1;1012}1013if (weightTotal == 0) return (size_t)-FSE_ERROR_corruptionDetected;10141015/* get last non-null symbol weight (implied, total must be 2^n) */1016maxBits = FSE_highbit32(weightTotal) + 1;1017if (maxBits > DTable[0]) return (size_t)-FSE_ERROR_tableLog_tooLarge; /* DTable is too small */1018DTable[0] = (U16)maxBits;1019{1020U32 total = 1 << maxBits;1021U32 rest = total - weightTotal;1022U32 verif = 1 << FSE_highbit32(rest);1023U32 lastWeight = FSE_highbit32(rest) + 1;1024if (verif != rest) return (size_t)-FSE_ERROR_corruptionDetected; /* last value must be a clean power of 2 */1025huffWeight[oSize] = (BYTE)lastWeight;1026rankVal[lastWeight]++;1027}10281029/* check tree construction validity */1030if ((rankVal[1] < 2) || (rankVal[1] & 1)) return (size_t)-FSE_ERROR_corruptionDetected; /* by construction : at least 2 elts of rank 1, must be even */10311032/* Prepare ranks */1033nextRankStart = 0;1034for (n=1; n<=maxBits; n++)1035{1036U32 current = nextRankStart;1037nextRankStart += (rankVal[n] << (n-1));1038rankVal[n] = current;1039}10401041/* fill DTable */1042for (n=0; n<=oSize; n++)1043{1044const U32 w = huffWeight[n];1045const U32 length = (1 << w) >> 1;1046U32 i;1047HUF_DElt D;1048D.byte = (BYTE)n; D.nbBits = (BYTE)(maxBits + 1 - w);1049for (i = rankVal[w]; i < rankVal[w] + length; i++)1050dt[i] = D;1051rankVal[w] += length;1052}10531054return iSize+1;1055}105610571058static BYTE HUF_decodeSymbol(FSE_DStream_t* Dstream, const HUF_DElt* dt, const U32 dtLog)1059{1060const size_t val = FSE_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */1061const BYTE c = dt[val].byte;1062FSE_skipBits(Dstream, dt[val].nbBits);1063return c;1064}10651066static size_t HUF_decompress_usingDTable( /* -3% slower when non static */1067void* dst, size_t maxDstSize,1068const void* cSrc, size_t cSrcSize,1069const U16* DTable)1070{1071if (cSrcSize < 6) return (size_t)-FSE_ERROR_srcSize_wrong;1072{1073BYTE* const ostart = (BYTE*) dst;1074BYTE* op = ostart;1075BYTE* const omax = op + maxDstSize;1076BYTE* const olimit = maxDstSize < 15 ? op : omax-15;10771078const void* ptr = DTable;1079const HUF_DElt* const dt = (const HUF_DElt*)(ptr)+1;1080const U32 dtLog = DTable[0];1081size_t errorCode;1082U32 reloadStatus;10831084/* Init */10851086const U16* jumpTable = (const U16*)cSrc;1087const size_t length1 = FSE_readLE16(jumpTable);1088const size_t length2 = FSE_readLE16(jumpTable+1);1089const size_t length3 = FSE_readLE16(jumpTable+2);1090const size_t length4 = cSrcSize - 6 - length1 - length2 - length3; /* check coherency !! */1091const char* const start1 = (const char*)(cSrc) + 6;1092const char* const start2 = start1 + length1;1093const char* const start3 = start2 + length2;1094const char* const start4 = start3 + length3;1095FSE_DStream_t bitD1, bitD2, bitD3, bitD4;10961097if (length1+length2+length3+6 >= cSrcSize) return (size_t)-FSE_ERROR_srcSize_wrong;10981099errorCode = FSE_initDStream(&bitD1, start1, length1);1100if (FSE_isError(errorCode)) return errorCode;1101errorCode = FSE_initDStream(&bitD2, start2, length2);1102if (FSE_isError(errorCode)) return errorCode;1103errorCode = FSE_initDStream(&bitD3, start3, length3);1104if (FSE_isError(errorCode)) return errorCode;1105errorCode = FSE_initDStream(&bitD4, start4, length4);1106if (FSE_isError(errorCode)) return errorCode;11071108reloadStatus=FSE_reloadDStream(&bitD2);11091110/* 16 symbols per loop */1111for ( ; (reloadStatus<FSE_DStream_completed) && (op<olimit); /* D2-3-4 are supposed to be synchronized and finish together */1112op+=16, reloadStatus = FSE_reloadDStream(&bitD2) | FSE_reloadDStream(&bitD3) | FSE_reloadDStream(&bitD4), FSE_reloadDStream(&bitD1))1113{1114#define HUF_DECODE_SYMBOL_0(n, Dstream) \1115op[n] = HUF_decodeSymbol(&Dstream, dt, dtLog);11161117#define HUF_DECODE_SYMBOL_1(n, Dstream) \1118op[n] = HUF_decodeSymbol(&Dstream, dt, dtLog); \1119if (FSE_32bits() && (HUF_MAX_TABLELOG>12)) FSE_reloadDStream(&Dstream)11201121#define HUF_DECODE_SYMBOL_2(n, Dstream) \1122op[n] = HUF_decodeSymbol(&Dstream, dt, dtLog); \1123if (FSE_32bits()) FSE_reloadDStream(&Dstream)11241125HUF_DECODE_SYMBOL_1( 0, bitD1);1126HUF_DECODE_SYMBOL_1( 1, bitD2);1127HUF_DECODE_SYMBOL_1( 2, bitD3);1128HUF_DECODE_SYMBOL_1( 3, bitD4);1129HUF_DECODE_SYMBOL_2( 4, bitD1);1130HUF_DECODE_SYMBOL_2( 5, bitD2);1131HUF_DECODE_SYMBOL_2( 6, bitD3);1132HUF_DECODE_SYMBOL_2( 7, bitD4);1133HUF_DECODE_SYMBOL_1( 8, bitD1);1134HUF_DECODE_SYMBOL_1( 9, bitD2);1135HUF_DECODE_SYMBOL_1(10, bitD3);1136HUF_DECODE_SYMBOL_1(11, bitD4);1137HUF_DECODE_SYMBOL_0(12, bitD1);1138HUF_DECODE_SYMBOL_0(13, bitD2);1139HUF_DECODE_SYMBOL_0(14, bitD3);1140HUF_DECODE_SYMBOL_0(15, bitD4);1141}11421143if (reloadStatus!=FSE_DStream_completed) /* not complete : some bitStream might be FSE_DStream_unfinished */1144return (size_t)-FSE_ERROR_corruptionDetected;11451146/* tail */1147{1148/* bitTail = bitD1; */ /* *much* slower : -20% !??! */1149FSE_DStream_t bitTail;1150bitTail.ptr = bitD1.ptr;1151bitTail.bitsConsumed = bitD1.bitsConsumed;1152bitTail.bitContainer = bitD1.bitContainer; /* required in case of FSE_DStream_endOfBuffer */1153bitTail.start = start1;1154for ( ; (FSE_reloadDStream(&bitTail) < FSE_DStream_completed) && (op<omax) ; op++)1155{1156HUF_DECODE_SYMBOL_0(0, bitTail);1157}11581159if (FSE_endOfDStream(&bitTail))1160return op-ostart;1161}11621163if (op==omax) return (size_t)-FSE_ERROR_dstSize_tooSmall; /* dst buffer is full, but cSrc unfinished */11641165return (size_t)-FSE_ERROR_corruptionDetected;1166}1167}116811691170static size_t HUF_decompress (void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize)1171{1172HUF_CREATE_STATIC_DTABLE(DTable, HUF_MAX_TABLELOG);1173const BYTE* ip = (const BYTE*) cSrc;1174size_t errorCode;11751176errorCode = HUF_readDTable (DTable, cSrc, cSrcSize);1177if (FSE_isError(errorCode)) return errorCode;1178if (errorCode >= cSrcSize) return (size_t)-FSE_ERROR_srcSize_wrong;1179ip += errorCode;1180cSrcSize -= errorCode;11811182return HUF_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, DTable);1183}118411851186#endif /* FSE_COMMONDEFS_ONLY */11871188/*1189zstd - standard compression library1190Copyright (C) 2014-2015, Yann Collet.11911192BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)11931194Redistribution and use in source and binary forms, with or without1195modification, are permitted provided that the following conditions are1196met:1197* Redistributions of source code must retain the above copyright1198notice, this list of conditions and the following disclaimer.1199* Redistributions in binary form must reproduce the above1200copyright notice, this list of conditions and the following disclaimer1201in the documentation and/or other materials provided with the1202distribution.1203THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS1204"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT1205LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR1206A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT1207OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,1208SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT1209LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,1210DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY1211THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT1212(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE1213OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.12141215You can contact the author at :1216- zstd source repository : https://github.com/Cyan4973/zstd1217- ztsd public forum : https://groups.google.com/forum/#!forum/lz4c1218*/12191220/****************************************************************1221* Tuning parameters1222*****************************************************************/1223/* MEMORY_USAGE :1224* Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)1225* Increasing memory usage improves compression ratio1226* Reduced memory usage can improve speed, due to cache effect */1227#define ZSTD_MEMORY_USAGE 17122812291230/**************************************1231CPU Feature Detection1232**************************************/1233/*1234* Automated efficient unaligned memory access detection1235* Based on known hardware architectures1236* This list will be updated thanks to feedbacks1237*/1238#if defined(CPU_HAS_EFFICIENT_UNALIGNED_MEMORY_ACCESS) \1239|| defined(__ARM_FEATURE_UNALIGNED) \1240|| defined(__i386__) || defined(__x86_64__) \1241|| defined(_M_IX86) || defined(_M_X64) \1242|| defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_8__) \1243|| (defined(_M_ARM) && (_M_ARM >= 7))1244# define ZSTD_UNALIGNED_ACCESS 11245#else1246# define ZSTD_UNALIGNED_ACCESS 01247#endif124812491250/********************************************************1251* Includes1252*********************************************************/1253#include <stdlib.h> /* calloc */1254#include <string.h> /* memcpy, memmove */1255#include <stdio.h> /* debug : printf */125612571258/********************************************************1259* Compiler specifics1260*********************************************************/1261#ifdef __AVX2__1262# include <immintrin.h> /* AVX2 intrinsics */1263#endif12641265#ifdef _MSC_VER /* Visual Studio */1266# include <intrin.h> /* For Visual 2005 */1267# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */1268# pragma warning(disable : 4324) /* disable: C4324: padded structure */1269#endif127012711272#ifndef MEM_ACCESS_MODULE1273#define MEM_ACCESS_MODULE1274/********************************************************1275* Basic Types1276*********************************************************/1277#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */1278# if defined(_AIX)1279# include <inttypes.h>1280# else1281# include <stdint.h> /* intptr_t */1282# endif1283typedef uint8_t BYTE;1284typedef uint16_t U16;1285typedef int16_t S16;1286typedef uint32_t U32;1287typedef int32_t S32;1288typedef uint64_t U64;1289#else1290typedef unsigned char BYTE;1291typedef unsigned short U16;1292typedef signed short S16;1293typedef unsigned int U32;1294typedef signed int S32;1295typedef unsigned long long U64;1296#endif12971298#endif /* MEM_ACCESS_MODULE */129913001301/********************************************************1302* Constants1303*********************************************************/1304static const U32 ZSTD_magicNumber = 0xFD2FB51E; /* 3rd version : seqNb header */13051306#define HASH_LOG (ZSTD_MEMORY_USAGE - 2)1307#define HASH_TABLESIZE (1 << HASH_LOG)1308#define HASH_MASK (HASH_TABLESIZE - 1)13091310#define KNUTH 265443576113111312#define BIT7 1281313#define BIT6 641314#define BIT5 321315#define BIT4 1613161317#define KB *(1 <<10)1318#define MB *(1 <<20)1319#define GB *(1U<<30)13201321#define BLOCKSIZE (128 KB) /* define, for static allocation */13221323#define WORKPLACESIZE (BLOCKSIZE*3)1324#define MINMATCH 41325#define MLbits 71326#define LLbits 61327#define Offbits 51328#define MaxML ((1<<MLbits )-1)1329#define MaxLL ((1<<LLbits )-1)1330#define MaxOff ((1<<Offbits)-1)1331#define LitFSELog 111332#define MLFSELog 101333#define LLFSELog 101334#define OffFSELog 91335#define MAX(a,b) ((a)<(b)?(b):(a))1336#define MaxSeq MAX(MaxLL, MaxML)13371338#define LITERAL_NOENTROPY 631339#define COMMAND_NOENTROPY 7 /* to remove */13401341#define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)13421343static const size_t ZSTD_blockHeaderSize = 3;1344static const size_t ZSTD_frameHeaderSize = 4;134513461347/********************************************************1348* Memory operations1349*********************************************************/1350static unsigned ZSTD_32bits(void) { return sizeof(void*)==4; }13511352static unsigned ZSTD_isLittleEndian(void)1353{1354const union { U32 i; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */1355return one.c[0];1356}13571358static U16 ZSTD_read16(const void* p) { U16 r; memcpy(&r, p, sizeof(r)); return r; }13591360static void ZSTD_copy4(void* dst, const void* src) { memcpy(dst, src, 4); }13611362static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }13631364#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }13651366static void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)1367{1368const BYTE* ip = (const BYTE*)src;1369BYTE* op = (BYTE*)dst;1370BYTE* const oend = op + length;1371while (op < oend) COPY8(op, ip);1372}13731374static U16 ZSTD_readLE16(const void* memPtr)1375{1376if (ZSTD_isLittleEndian()) return ZSTD_read16(memPtr);1377else1378{1379const BYTE* p = (const BYTE*)memPtr;1380return (U16)((U16)p[0] + ((U16)p[1]<<8));1381}1382}13831384static U32 ZSTD_readLE24(const void* memPtr)1385{1386return ZSTD_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);1387}13881389static U32 ZSTD_readBE32(const void* memPtr)1390{1391const BYTE* p = (const BYTE*)memPtr;1392return (U32)(((U32)p[0]<<24) + ((U32)p[1]<<16) + ((U32)p[2]<<8) + ((U32)p[3]<<0));1393}139413951396/**************************************1397* Local structures1398***************************************/1399typedef struct ZSTD_Cctx_s ZSTD_Cctx;14001401typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;14021403typedef struct1404{1405blockType_t blockType;1406U32 origSize;1407} blockProperties_t;14081409typedef struct {1410void* buffer;1411U32* offsetStart;1412U32* offset;1413BYTE* offCodeStart;1414BYTE* offCode;1415BYTE* litStart;1416BYTE* lit;1417BYTE* litLengthStart;1418BYTE* litLength;1419BYTE* matchLengthStart;1420BYTE* matchLength;1421BYTE* dumpsStart;1422BYTE* dumps;1423} seqStore_t;142414251426typedef struct ZSTD_Cctx_s1427{1428const BYTE* base;1429U32 current;1430U32 nextUpdate;1431seqStore_t seqStore;1432#ifdef __AVX2__1433__m256i hashTable[HASH_TABLESIZE>>3];1434#else1435U32 hashTable[HASH_TABLESIZE];1436#endif1437BYTE buffer[WORKPLACESIZE];1438} cctxi_t;14391440144114421443/**************************************1444* Error Management1445**************************************/1446/* published entry point */1447unsigned ZSTDv01_isError(size_t code) { return ERR_isError(code); }144814491450/**************************************1451* Tool functions1452**************************************/1453#define ZSTD_VERSION_MAJOR 0 /* for breaking interface changes */1454#define ZSTD_VERSION_MINOR 1 /* for new (non-breaking) interface capabilities */1455#define ZSTD_VERSION_RELEASE 3 /* for tweaks, bug-fixes, or development */1456#define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)14571458/**************************************************************1459* Decompression code1460**************************************************************/14611462static size_t ZSTDv01_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr)1463{1464const BYTE* const in = (const BYTE* const)src;1465BYTE headerFlags;1466U32 cSize;14671468if (srcSize < 3) return ERROR(srcSize_wrong);14691470headerFlags = *in;1471cSize = in[2] + (in[1]<<8) + ((in[0] & 7)<<16);14721473bpPtr->blockType = (blockType_t)(headerFlags >> 6);1474bpPtr->origSize = (bpPtr->blockType == bt_rle) ? cSize : 0;14751476if (bpPtr->blockType == bt_end) return 0;1477if (bpPtr->blockType == bt_rle) return 1;1478return cSize;1479}148014811482static size_t ZSTD_copyUncompressedBlock(void* dst, size_t maxDstSize, const void* src, size_t srcSize)1483{1484if (srcSize > maxDstSize) return ERROR(dstSize_tooSmall);1485if (srcSize > 0) {1486memcpy(dst, src, srcSize);1487}1488return srcSize;1489}149014911492static size_t ZSTD_decompressLiterals(void* ctx,1493void* dst, size_t maxDstSize,1494const void* src, size_t srcSize)1495{1496BYTE* op = (BYTE*)dst;1497BYTE* const oend = op + maxDstSize;1498const BYTE* ip = (const BYTE*)src;1499size_t errorCode;1500size_t litSize;15011502/* check : minimum 2, for litSize, +1, for content */1503if (srcSize <= 3) return ERROR(corruption_detected);15041505litSize = ip[1] + (ip[0]<<8);1506litSize += ((ip[-3] >> 3) & 7) << 16; /* mmmmh.... */1507op = oend - litSize;15081509(void)ctx;1510if (litSize > maxDstSize) return ERROR(dstSize_tooSmall);1511errorCode = HUF_decompress(op, litSize, ip+2, srcSize-2);1512if (FSE_isError(errorCode)) return ERROR(GENERIC);1513return litSize;1514}151515161517static size_t ZSTDv01_decodeLiteralsBlock(void* ctx,1518void* dst, size_t maxDstSize,1519const BYTE** litStart, size_t* litSize,1520const void* src, size_t srcSize)1521{1522const BYTE* const istart = (const BYTE* const)src;1523const BYTE* ip = istart;1524BYTE* const ostart = (BYTE* const)dst;1525BYTE* const oend = ostart + maxDstSize;1526blockProperties_t litbp;15271528size_t litcSize = ZSTDv01_getcBlockSize(src, srcSize, &litbp);1529if (ZSTDv01_isError(litcSize)) return litcSize;1530if (litcSize > srcSize - ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);1531ip += ZSTD_blockHeaderSize;15321533switch(litbp.blockType)1534{1535case bt_raw:1536*litStart = ip;1537ip += litcSize;1538*litSize = litcSize;1539break;1540case bt_rle:1541{1542size_t rleSize = litbp.origSize;1543if (rleSize>maxDstSize) return ERROR(dstSize_tooSmall);1544if (!srcSize) return ERROR(srcSize_wrong);1545if (rleSize > 0) {1546memset(oend - rleSize, *ip, rleSize);1547}1548*litStart = oend - rleSize;1549*litSize = rleSize;1550ip++;1551break;1552}1553case bt_compressed:1554{1555size_t decodedLitSize = ZSTD_decompressLiterals(ctx, dst, maxDstSize, ip, litcSize);1556if (ZSTDv01_isError(decodedLitSize)) return decodedLitSize;1557*litStart = oend - decodedLitSize;1558*litSize = decodedLitSize;1559ip += litcSize;1560break;1561}1562case bt_end:1563default:1564return ERROR(GENERIC);1565}15661567return ip-istart;1568}156915701571static size_t ZSTDv01_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr,1572FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb,1573const void* src, size_t srcSize)1574{1575const BYTE* const istart = (const BYTE* const)src;1576const BYTE* ip = istart;1577const BYTE* const iend = istart + srcSize;1578U32 LLtype, Offtype, MLtype;1579U32 LLlog, Offlog, MLlog;1580size_t dumpsLength;15811582/* check */1583if (srcSize < 5) return ERROR(srcSize_wrong);15841585/* SeqHead */1586*nbSeq = ZSTD_readLE16(ip); ip+=2;1587LLtype = *ip >> 6;1588Offtype = (*ip >> 4) & 3;1589MLtype = (*ip >> 2) & 3;1590if (*ip & 2)1591{1592dumpsLength = ip[2];1593dumpsLength += ip[1] << 8;1594ip += 3;1595}1596else1597{1598dumpsLength = ip[1];1599dumpsLength += (ip[0] & 1) << 8;1600ip += 2;1601}1602*dumpsPtr = ip;1603ip += dumpsLength;1604*dumpsLengthPtr = dumpsLength;16051606/* check */1607if (ip > iend-3) return ERROR(srcSize_wrong); /* min : all 3 are "raw", hence no header, but at least xxLog bits per type */16081609/* sequences */1610{1611S16 norm[MaxML+1]; /* assumption : MaxML >= MaxLL and MaxOff */1612size_t headerSize;16131614/* Build DTables */1615switch(LLtype)1616{1617case bt_rle :1618LLlog = 0;1619FSE_buildDTable_rle(DTableLL, *ip++); break;1620case bt_raw :1621LLlog = LLbits;1622FSE_buildDTable_raw(DTableLL, LLbits); break;1623default :1624{ U32 max = MaxLL;1625headerSize = FSE_readNCount(norm, &max, &LLlog, ip, iend-ip);1626if (FSE_isError(headerSize)) return ERROR(GENERIC);1627if (LLlog > LLFSELog) return ERROR(corruption_detected);1628ip += headerSize;1629FSE_buildDTable(DTableLL, norm, max, LLlog);1630} }16311632switch(Offtype)1633{1634case bt_rle :1635Offlog = 0;1636if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */1637FSE_buildDTable_rle(DTableOffb, *ip++); break;1638case bt_raw :1639Offlog = Offbits;1640FSE_buildDTable_raw(DTableOffb, Offbits); break;1641default :1642{ U32 max = MaxOff;1643headerSize = FSE_readNCount(norm, &max, &Offlog, ip, iend-ip);1644if (FSE_isError(headerSize)) return ERROR(GENERIC);1645if (Offlog > OffFSELog) return ERROR(corruption_detected);1646ip += headerSize;1647FSE_buildDTable(DTableOffb, norm, max, Offlog);1648} }16491650switch(MLtype)1651{1652case bt_rle :1653MLlog = 0;1654if (ip > iend-2) return ERROR(srcSize_wrong); /* min : "raw", hence no header, but at least xxLog bits */1655FSE_buildDTable_rle(DTableML, *ip++); break;1656case bt_raw :1657MLlog = MLbits;1658FSE_buildDTable_raw(DTableML, MLbits); break;1659default :1660{ U32 max = MaxML;1661headerSize = FSE_readNCount(norm, &max, &MLlog, ip, iend-ip);1662if (FSE_isError(headerSize)) return ERROR(GENERIC);1663if (MLlog > MLFSELog) return ERROR(corruption_detected);1664ip += headerSize;1665FSE_buildDTable(DTableML, norm, max, MLlog);1666} } }16671668return ip-istart;1669}167016711672typedef struct {1673size_t litLength;1674size_t offset;1675size_t matchLength;1676} seq_t;16771678typedef struct {1679FSE_DStream_t DStream;1680FSE_DState_t stateLL;1681FSE_DState_t stateOffb;1682FSE_DState_t stateML;1683size_t prevOffset;1684const BYTE* dumps;1685const BYTE* dumpsEnd;1686} seqState_t;168716881689static void ZSTD_decodeSequence(seq_t* seq, seqState_t* seqState)1690{1691size_t litLength;1692size_t prevOffset;1693size_t offset;1694size_t matchLength;1695const BYTE* dumps = seqState->dumps;1696const BYTE* const de = seqState->dumpsEnd;16971698/* Literal length */1699litLength = FSE_decodeSymbol(&(seqState->stateLL), &(seqState->DStream));1700prevOffset = litLength ? seq->offset : seqState->prevOffset;1701seqState->prevOffset = seq->offset;1702if (litLength == MaxLL)1703{1704const U32 add = dumps<de ? *dumps++ : 0;1705if (add < 255) litLength += add;1706else1707{1708if (dumps<=(de-3))1709{1710litLength = ZSTD_readLE24(dumps);1711dumps += 3;1712}1713}1714}17151716/* Offset */1717{1718U32 offsetCode, nbBits;1719offsetCode = FSE_decodeSymbol(&(seqState->stateOffb), &(seqState->DStream));1720if (ZSTD_32bits()) FSE_reloadDStream(&(seqState->DStream));1721nbBits = offsetCode - 1;1722if (offsetCode==0) nbBits = 0; /* cmove */1723offset = ((size_t)1 << (nbBits & ((sizeof(offset)*8)-1))) + FSE_readBits(&(seqState->DStream), nbBits);1724if (ZSTD_32bits()) FSE_reloadDStream(&(seqState->DStream));1725if (offsetCode==0) offset = prevOffset;1726}17271728/* MatchLength */1729matchLength = FSE_decodeSymbol(&(seqState->stateML), &(seqState->DStream));1730if (matchLength == MaxML)1731{1732const U32 add = dumps<de ? *dumps++ : 0;1733if (add < 255) matchLength += add;1734else1735{1736if (dumps<=(de-3))1737{1738matchLength = ZSTD_readLE24(dumps);1739dumps += 3;1740}1741}1742}1743matchLength += MINMATCH;17441745/* save result */1746seq->litLength = litLength;1747seq->offset = offset;1748seq->matchLength = matchLength;1749seqState->dumps = dumps;1750}175117521753static size_t ZSTD_execSequence(BYTE* op,1754seq_t sequence,1755const BYTE** litPtr, const BYTE* const litLimit,1756BYTE* const base, BYTE* const oend)1757{1758static const int dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4}; /* added */1759static const int dec64table[] = {8, 8, 8, 7, 8, 9,10,11}; /* subtracted */1760const BYTE* const ostart = op;1761const size_t litLength = sequence.litLength;1762BYTE* const endMatch = op + litLength + sequence.matchLength; /* risk : address space overflow (32-bits) */1763const BYTE* const litEnd = *litPtr + litLength;17641765/* check */1766if (endMatch > oend) return ERROR(dstSize_tooSmall); /* overwrite beyond dst buffer */1767if (litEnd > litLimit) return ERROR(corruption_detected);1768if (sequence.matchLength > (size_t)(*litPtr-op)) return ERROR(dstSize_tooSmall); /* overwrite literal segment */17691770/* copy Literals */1771if (((size_t)(*litPtr - op) < 8) || ((size_t)(oend-litEnd) < 8) || (op+litLength > oend-8))1772memmove(op, *litPtr, litLength); /* overwrite risk */1773else1774ZSTD_wildcopy(op, *litPtr, litLength);1775op += litLength;1776*litPtr = litEnd; /* update for next sequence */17771778/* check : last match must be at a minimum distance of 8 from end of dest buffer */1779if (oend-op < 8) return ERROR(dstSize_tooSmall);17801781/* copy Match */1782{1783const U32 overlapRisk = (((size_t)(litEnd - endMatch)) < 12);1784const BYTE* match = op - sequence.offset; /* possible underflow at op - offset ? */1785size_t qutt = 12;1786U64 saved[2];17871788/* check */1789if (match < base) return ERROR(corruption_detected);1790if (sequence.offset > (size_t)base) return ERROR(corruption_detected);17911792/* save beginning of literal sequence, in case of write overlap */1793if (overlapRisk)1794{1795if ((endMatch + qutt) > oend) qutt = oend-endMatch;1796memcpy(saved, endMatch, qutt);1797}17981799if (sequence.offset < 8)1800{1801const int dec64 = dec64table[sequence.offset];1802op[0] = match[0];1803op[1] = match[1];1804op[2] = match[2];1805op[3] = match[3];1806match += dec32table[sequence.offset];1807ZSTD_copy4(op+4, match);1808match -= dec64;1809} else { ZSTD_copy8(op, match); }1810op += 8; match += 8;18111812if (endMatch > oend-(16-MINMATCH))1813{1814if (op < oend-8)1815{1816ZSTD_wildcopy(op, match, (oend-8) - op);1817match += (oend-8) - op;1818op = oend-8;1819}1820while (op<endMatch) *op++ = *match++;1821}1822else1823ZSTD_wildcopy(op, match, (ptrdiff_t)sequence.matchLength-8); /* works even if matchLength < 8 */18241825/* restore, in case of overlap */1826if (overlapRisk) memcpy(endMatch, saved, qutt);1827}18281829return endMatch-ostart;1830}18311832typedef struct ZSTDv01_Dctx_s1833{1834U32 LLTable[FSE_DTABLE_SIZE_U32(LLFSELog)];1835U32 OffTable[FSE_DTABLE_SIZE_U32(OffFSELog)];1836U32 MLTable[FSE_DTABLE_SIZE_U32(MLFSELog)];1837void* previousDstEnd;1838void* base;1839size_t expected;1840blockType_t bType;1841U32 phase;1842} dctx_t;184318441845static size_t ZSTD_decompressSequences(1846void* ctx,1847void* dst, size_t maxDstSize,1848const void* seqStart, size_t seqSize,1849const BYTE* litStart, size_t litSize)1850{1851dctx_t* dctx = (dctx_t*)ctx;1852const BYTE* ip = (const BYTE*)seqStart;1853const BYTE* const iend = ip + seqSize;1854BYTE* const ostart = (BYTE* const)dst;1855BYTE* op = ostart;1856BYTE* const oend = ostart + maxDstSize;1857size_t errorCode, dumpsLength;1858const BYTE* litPtr = litStart;1859const BYTE* const litEnd = litStart + litSize;1860int nbSeq;1861const BYTE* dumps;1862U32* DTableLL = dctx->LLTable;1863U32* DTableML = dctx->MLTable;1864U32* DTableOffb = dctx->OffTable;1865BYTE* const base = (BYTE*) (dctx->base);18661867/* Build Decoding Tables */1868errorCode = ZSTDv01_decodeSeqHeaders(&nbSeq, &dumps, &dumpsLength,1869DTableLL, DTableML, DTableOffb,1870ip, iend-ip);1871if (ZSTDv01_isError(errorCode)) return errorCode;1872ip += errorCode;18731874/* Regen sequences */1875{1876seq_t sequence;1877seqState_t seqState;18781879memset(&sequence, 0, sizeof(sequence));1880seqState.dumps = dumps;1881seqState.dumpsEnd = dumps + dumpsLength;1882seqState.prevOffset = 1;1883errorCode = FSE_initDStream(&(seqState.DStream), ip, iend-ip);1884if (FSE_isError(errorCode)) return ERROR(corruption_detected);1885FSE_initDState(&(seqState.stateLL), &(seqState.DStream), DTableLL);1886FSE_initDState(&(seqState.stateOffb), &(seqState.DStream), DTableOffb);1887FSE_initDState(&(seqState.stateML), &(seqState.DStream), DTableML);18881889for ( ; (FSE_reloadDStream(&(seqState.DStream)) <= FSE_DStream_completed) && (nbSeq>0) ; )1890{1891size_t oneSeqSize;1892nbSeq--;1893ZSTD_decodeSequence(&sequence, &seqState);1894oneSeqSize = ZSTD_execSequence(op, sequence, &litPtr, litEnd, base, oend);1895if (ZSTDv01_isError(oneSeqSize)) return oneSeqSize;1896op += oneSeqSize;1897}18981899/* check if reached exact end */1900if ( !FSE_endOfDStream(&(seqState.DStream)) ) return ERROR(corruption_detected); /* requested too much : data is corrupted */1901if (nbSeq<0) return ERROR(corruption_detected); /* requested too many sequences : data is corrupted */19021903/* last literal segment */1904{1905size_t lastLLSize = litEnd - litPtr;1906if (op+lastLLSize > oend) return ERROR(dstSize_tooSmall);1907if (lastLLSize > 0) {1908if (op != litPtr) memmove(op, litPtr, lastLLSize);1909op += lastLLSize;1910}1911}1912}19131914return op-ostart;1915}191619171918static size_t ZSTD_decompressBlock(1919void* ctx,1920void* dst, size_t maxDstSize,1921const void* src, size_t srcSize)1922{1923/* blockType == blockCompressed, srcSize is trusted */1924const BYTE* ip = (const BYTE*)src;1925const BYTE* litPtr = NULL;1926size_t litSize = 0;1927size_t errorCode;19281929/* Decode literals sub-block */1930errorCode = ZSTDv01_decodeLiteralsBlock(ctx, dst, maxDstSize, &litPtr, &litSize, src, srcSize);1931if (ZSTDv01_isError(errorCode)) return errorCode;1932ip += errorCode;1933srcSize -= errorCode;19341935return ZSTD_decompressSequences(ctx, dst, maxDstSize, ip, srcSize, litPtr, litSize);1936}193719381939size_t ZSTDv01_decompressDCtx(void* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)1940{1941const BYTE* ip = (const BYTE*)src;1942const BYTE* iend = ip + srcSize;1943BYTE* const ostart = (BYTE* const)dst;1944BYTE* op = ostart;1945BYTE* const oend = ostart + maxDstSize;1946size_t remainingSize = srcSize;1947U32 magicNumber;1948size_t errorCode=0;1949blockProperties_t blockProperties;19501951/* Frame Header */1952if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) return ERROR(srcSize_wrong);1953magicNumber = ZSTD_readBE32(src);1954if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);1955ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;19561957/* Loop on each block */1958while (1)1959{1960size_t blockSize = ZSTDv01_getcBlockSize(ip, iend-ip, &blockProperties);1961if (ZSTDv01_isError(blockSize)) return blockSize;19621963ip += ZSTD_blockHeaderSize;1964remainingSize -= ZSTD_blockHeaderSize;1965if (blockSize > remainingSize) return ERROR(srcSize_wrong);19661967switch(blockProperties.blockType)1968{1969case bt_compressed:1970errorCode = ZSTD_decompressBlock(ctx, op, oend-op, ip, blockSize);1971break;1972case bt_raw :1973errorCode = ZSTD_copyUncompressedBlock(op, oend-op, ip, blockSize);1974break;1975case bt_rle :1976return ERROR(GENERIC); /* not yet supported */1977break;1978case bt_end :1979/* end of frame */1980if (remainingSize) return ERROR(srcSize_wrong);1981break;1982default:1983return ERROR(GENERIC);1984}1985if (blockSize == 0) break; /* bt_end */19861987if (ZSTDv01_isError(errorCode)) return errorCode;1988op += errorCode;1989ip += blockSize;1990remainingSize -= blockSize;1991}19921993return op-ostart;1994}19951996size_t ZSTDv01_decompress(void* dst, size_t maxDstSize, const void* src, size_t srcSize)1997{1998dctx_t ctx;1999ctx.base = dst;2000return ZSTDv01_decompressDCtx(&ctx, dst, maxDstSize, src, srcSize);2001}20022003/* ZSTD_errorFrameSizeInfoLegacy() :2004assumes `cSize` and `dBound` are _not_ NULL */2005static void ZSTD_errorFrameSizeInfoLegacy(size_t* cSize, unsigned long long* dBound, size_t ret)2006{2007*cSize = ret;2008*dBound = ZSTD_CONTENTSIZE_ERROR;2009}20102011void ZSTDv01_findFrameSizeInfoLegacy(const void *src, size_t srcSize, size_t* cSize, unsigned long long* dBound)2012{2013const BYTE* ip = (const BYTE*)src;2014size_t remainingSize = srcSize;2015size_t nbBlocks = 0;2016U32 magicNumber;2017blockProperties_t blockProperties;20182019/* Frame Header */2020if (srcSize < ZSTD_frameHeaderSize+ZSTD_blockHeaderSize) {2021ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));2022return;2023}2024magicNumber = ZSTD_readBE32(src);2025if (magicNumber != ZSTD_magicNumber) {2026ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(prefix_unknown));2027return;2028}2029ip += ZSTD_frameHeaderSize; remainingSize -= ZSTD_frameHeaderSize;20302031/* Loop on each block */2032while (1)2033{2034size_t blockSize = ZSTDv01_getcBlockSize(ip, remainingSize, &blockProperties);2035if (ZSTDv01_isError(blockSize)) {2036ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, blockSize);2037return;2038}20392040ip += ZSTD_blockHeaderSize;2041remainingSize -= ZSTD_blockHeaderSize;2042if (blockSize > remainingSize) {2043ZSTD_errorFrameSizeInfoLegacy(cSize, dBound, ERROR(srcSize_wrong));2044return;2045}20462047if (blockSize == 0) break; /* bt_end */20482049ip += blockSize;2050remainingSize -= blockSize;2051nbBlocks++;2052}20532054*cSize = ip - (const BYTE*)src;2055*dBound = nbBlocks * BLOCKSIZE;2056}20572058/*******************************2059* Streaming Decompression API2060*******************************/20612062size_t ZSTDv01_resetDCtx(ZSTDv01_Dctx* dctx)2063{2064dctx->expected = ZSTD_frameHeaderSize;2065dctx->phase = 0;2066dctx->previousDstEnd = NULL;2067dctx->base = NULL;2068return 0;2069}20702071ZSTDv01_Dctx* ZSTDv01_createDCtx(void)2072{2073ZSTDv01_Dctx* dctx = (ZSTDv01_Dctx*)malloc(sizeof(ZSTDv01_Dctx));2074if (dctx==NULL) return NULL;2075ZSTDv01_resetDCtx(dctx);2076return dctx;2077}20782079size_t ZSTDv01_freeDCtx(ZSTDv01_Dctx* dctx)2080{2081free(dctx);2082return 0;2083}20842085size_t ZSTDv01_nextSrcSizeToDecompress(ZSTDv01_Dctx* dctx)2086{2087return ((dctx_t*)dctx)->expected;2088}20892090size_t ZSTDv01_decompressContinue(ZSTDv01_Dctx* dctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize)2091{2092dctx_t* ctx = (dctx_t*)dctx;20932094/* Sanity check */2095if (srcSize != ctx->expected) return ERROR(srcSize_wrong);2096if (dst != ctx->previousDstEnd) /* not contiguous */2097ctx->base = dst;20982099/* Decompress : frame header */2100if (ctx->phase == 0)2101{2102/* Check frame magic header */2103U32 magicNumber = ZSTD_readBE32(src);2104if (magicNumber != ZSTD_magicNumber) return ERROR(prefix_unknown);2105ctx->phase = 1;2106ctx->expected = ZSTD_blockHeaderSize;2107return 0;2108}21092110/* Decompress : block header */2111if (ctx->phase == 1)2112{2113blockProperties_t bp;2114size_t blockSize = ZSTDv01_getcBlockSize(src, ZSTD_blockHeaderSize, &bp);2115if (ZSTDv01_isError(blockSize)) return blockSize;2116if (bp.blockType == bt_end)2117{2118ctx->expected = 0;2119ctx->phase = 0;2120}2121else2122{2123ctx->expected = blockSize;2124ctx->bType = bp.blockType;2125ctx->phase = 2;2126}21272128return 0;2129}21302131/* Decompress : block content */2132{2133size_t rSize;2134switch(ctx->bType)2135{2136case bt_compressed:2137rSize = ZSTD_decompressBlock(ctx, dst, maxDstSize, src, srcSize);2138break;2139case bt_raw :2140rSize = ZSTD_copyUncompressedBlock(dst, maxDstSize, src, srcSize);2141break;2142case bt_rle :2143return ERROR(GENERIC); /* not yet handled */2144break;2145case bt_end : /* should never happen (filtered at phase 1) */2146rSize = 0;2147break;2148default:2149return ERROR(GENERIC);2150}2151ctx->phase = 1;2152ctx->expected = ZSTD_blockHeaderSize;2153ctx->previousDstEnd = (void*)( ((char*)dst) + rSize);2154return rSize;2155}21562157}215821592160