[8ebc79b] | 1 | /* |
---|
| 2 | zstd_internal - common functions to include |
---|
| 3 | Header File for include |
---|
| 4 | Copyright (C) 2014-2016, Yann Collet. |
---|
| 5 | |
---|
| 6 | BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) |
---|
| 7 | |
---|
| 8 | Redistribution and use in source and binary forms, with or without |
---|
| 9 | modification, are permitted provided that the following conditions are |
---|
| 10 | met: |
---|
| 11 | * Redistributions of source code must retain the above copyright |
---|
| 12 | notice, this list of conditions and the following disclaimer. |
---|
| 13 | * Redistributions in binary form must reproduce the above |
---|
| 14 | copyright notice, this list of conditions and the following disclaimer |
---|
| 15 | in the documentation and/or other materials provided with the |
---|
| 16 | distribution. |
---|
| 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
| 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
| 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
| 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
| 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
| 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
| 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
| 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 28 | |
---|
| 29 | You can contact the author at : |
---|
| 30 | - zstd homepage : https://www.zstd.net |
---|
| 31 | */ |
---|
| 32 | #ifndef ZSTD_CCOMMON_H_MODULE |
---|
| 33 | #define ZSTD_CCOMMON_H_MODULE |
---|
| 34 | |
---|
| 35 | /*-************************************* |
---|
| 36 | * Dependencies |
---|
| 37 | ***************************************/ |
---|
| 38 | #include "mem.h" |
---|
| 39 | #include "error_private.h" |
---|
| 40 | #define ZSTD_STATIC_LINKING_ONLY |
---|
| 41 | #include "zstd.h" |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | /*-************************************* |
---|
| 45 | * Common macros |
---|
| 46 | ***************************************/ |
---|
| 47 | #define MIN(a,b) ((a)<(b) ? (a) : (b)) |
---|
| 48 | #define MAX(a,b) ((a)>(b) ? (a) : (b)) |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | /*-************************************* |
---|
| 52 | * Common constants |
---|
| 53 | ***************************************/ |
---|
| 54 | #define ZSTD_OPT_DEBUG 0 /* 3 = compression stats; 5 = check encoded sequences; 9 = full logs */ |
---|
| 55 | #include <stdio.h> |
---|
| 56 | #if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9 |
---|
| 57 | #define ZSTD_LOG_PARSER(...) printf(__VA_ARGS__) |
---|
| 58 | #define ZSTD_LOG_ENCODE(...) printf(__VA_ARGS__) |
---|
| 59 | #define ZSTD_LOG_BLOCK(...) printf(__VA_ARGS__) |
---|
| 60 | #else |
---|
| 61 | #define ZSTD_LOG_PARSER(...) |
---|
| 62 | #define ZSTD_LOG_ENCODE(...) |
---|
| 63 | #define ZSTD_LOG_BLOCK(...) |
---|
| 64 | #endif |
---|
| 65 | |
---|
| 66 | #define ZSTD_OPT_NUM (1<<12) |
---|
| 67 | #define ZSTD_DICT_MAGIC 0xEC30A437 /* v0.7 */ |
---|
| 68 | |
---|
| 69 | #define ZSTD_REP_NUM 3 |
---|
| 70 | #define ZSTD_REP_INIT ZSTD_REP_NUM |
---|
| 71 | #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1) |
---|
| 72 | static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 }; |
---|
| 73 | |
---|
| 74 | #define KB *(1 <<10) |
---|
| 75 | #define MB *(1 <<20) |
---|
| 76 | #define GB *(1U<<30) |
---|
| 77 | |
---|
| 78 | #define BIT7 128 |
---|
| 79 | #define BIT6 64 |
---|
| 80 | #define BIT5 32 |
---|
| 81 | #define BIT4 16 |
---|
| 82 | #define BIT1 2 |
---|
| 83 | #define BIT0 1 |
---|
| 84 | |
---|
| 85 | #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10 |
---|
| 86 | static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 }; |
---|
| 87 | static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 }; |
---|
| 88 | |
---|
| 89 | #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */ |
---|
| 90 | static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE; |
---|
| 91 | typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t; |
---|
| 92 | |
---|
| 93 | #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */ |
---|
| 94 | #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */ |
---|
| 95 | |
---|
| 96 | #define HufLog 12 |
---|
| 97 | typedef enum { lbt_huffman, lbt_repeat, lbt_raw, lbt_rle } litBlockType_t; |
---|
| 98 | |
---|
| 99 | #define LONGNBSEQ 0x7F00 |
---|
| 100 | |
---|
| 101 | #define MINMATCH 3 |
---|
| 102 | #define EQUAL_READ32 4 |
---|
| 103 | |
---|
| 104 | #define Litbits 8 |
---|
| 105 | #define MaxLit ((1<<Litbits) - 1) |
---|
| 106 | #define MaxML 52 |
---|
| 107 | #define MaxLL 35 |
---|
| 108 | #define MaxOff 28 |
---|
| 109 | #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */ |
---|
| 110 | #define MLFSELog 9 |
---|
| 111 | #define LLFSELog 9 |
---|
| 112 | #define OffFSELog 8 |
---|
| 113 | |
---|
| 114 | #define FSE_ENCODING_RAW 0 |
---|
| 115 | #define FSE_ENCODING_RLE 1 |
---|
| 116 | #define FSE_ENCODING_STATIC 2 |
---|
| 117 | #define FSE_ENCODING_DYNAMIC 3 |
---|
| 118 | |
---|
| 119 | static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
| 120 | 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12, |
---|
| 121 | 13,14,15,16 }; |
---|
| 122 | static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, |
---|
| 123 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1, |
---|
| 124 | -1,-1,-1,-1 }; |
---|
| 125 | static const U32 LL_defaultNormLog = 6; |
---|
| 126 | |
---|
| 127 | static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
| 128 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
| 129 | 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11, |
---|
| 130 | 12,13,14,15,16 }; |
---|
| 131 | static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, |
---|
| 132 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
---|
| 133 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1, |
---|
| 134 | -1,-1,-1,-1,-1 }; |
---|
| 135 | static const U32 ML_defaultNormLog = 6; |
---|
| 136 | |
---|
| 137 | static const S16 OF_defaultNorm[MaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, |
---|
| 138 | 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1 }; |
---|
| 139 | static const U32 OF_defaultNormLog = 5; |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | /*-******************************************* |
---|
| 143 | * Shared functions to include for inlining |
---|
| 144 | *********************************************/ |
---|
| 145 | static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); } |
---|
| 146 | #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; } |
---|
| 147 | |
---|
| 148 | /*! ZSTD_wildcopy() : |
---|
| 149 | * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */ |
---|
| 150 | #define WILDCOPY_OVERLENGTH 8 |
---|
| 151 | MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, size_t length) |
---|
| 152 | { |
---|
| 153 | const BYTE* ip = (const BYTE*)src; |
---|
| 154 | BYTE* op = (BYTE*)dst; |
---|
| 155 | BYTE* const oend = op + length; |
---|
| 156 | do |
---|
| 157 | COPY8(op, ip) |
---|
| 158 | while (op < oend); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | /*-******************************************* |
---|
| 163 | * Private interfaces |
---|
| 164 | *********************************************/ |
---|
| 165 | typedef struct ZSTD_stats_s ZSTD_stats_t; |
---|
| 166 | |
---|
| 167 | typedef struct { |
---|
| 168 | U32 off; |
---|
| 169 | U32 len; |
---|
| 170 | } ZSTD_match_t; |
---|
| 171 | |
---|
| 172 | typedef struct { |
---|
| 173 | U32 price; |
---|
| 174 | U32 off; |
---|
| 175 | U32 mlen; |
---|
| 176 | U32 litlen; |
---|
| 177 | U32 rep[ZSTD_REP_INIT]; |
---|
| 178 | } ZSTD_optimal_t; |
---|
| 179 | |
---|
| 180 | #if ZSTD_OPT_DEBUG == 3 |
---|
| 181 | #include ".debug/zstd_stats.h" |
---|
| 182 | #else |
---|
| 183 | struct ZSTD_stats_s { U32 unused; }; |
---|
| 184 | MEM_STATIC void ZSTD_statsPrint(ZSTD_stats_t* stats, U32 searchLength) { (void)stats; (void)searchLength; } |
---|
| 185 | MEM_STATIC void ZSTD_statsInit(ZSTD_stats_t* stats) { (void)stats; } |
---|
| 186 | MEM_STATIC void ZSTD_statsResetFreqs(ZSTD_stats_t* stats) { (void)stats; } |
---|
| 187 | MEM_STATIC void ZSTD_statsUpdatePrices(ZSTD_stats_t* stats, size_t litLength, const BYTE* literals, size_t offset, size_t matchLength) { (void)stats; (void)litLength; (void)literals; (void)offset; (void)matchLength; } |
---|
| 188 | #endif /* #if ZSTD_OPT_DEBUG == 3 */ |
---|
| 189 | |
---|
| 190 | typedef struct { |
---|
| 191 | void* buffer; |
---|
| 192 | U32* offsetStart; |
---|
| 193 | U32* offset; |
---|
| 194 | BYTE* offCodeStart; |
---|
| 195 | BYTE* litStart; |
---|
| 196 | BYTE* lit; |
---|
| 197 | U16* litLengthStart; |
---|
| 198 | U16* litLength; |
---|
| 199 | BYTE* llCodeStart; |
---|
| 200 | U16* matchLengthStart; |
---|
| 201 | U16* matchLength; |
---|
| 202 | BYTE* mlCodeStart; |
---|
| 203 | U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */ |
---|
| 204 | U32 longLengthPos; |
---|
| 205 | /* opt */ |
---|
| 206 | ZSTD_optimal_t* priceTable; |
---|
| 207 | ZSTD_match_t* matchTable; |
---|
| 208 | U32* matchLengthFreq; |
---|
| 209 | U32* litLengthFreq; |
---|
| 210 | U32* litFreq; |
---|
| 211 | U32* offCodeFreq; |
---|
| 212 | U32 matchLengthSum; |
---|
| 213 | U32 matchSum; |
---|
| 214 | U32 litLengthSum; |
---|
| 215 | U32 litSum; |
---|
| 216 | U32 offCodeSum; |
---|
| 217 | U32 log2matchLengthSum; |
---|
| 218 | U32 log2matchSum; |
---|
| 219 | U32 log2litLengthSum; |
---|
| 220 | U32 log2litSum; |
---|
| 221 | U32 log2offCodeSum; |
---|
| 222 | U32 factor; |
---|
| 223 | U32 cachedPrice; |
---|
| 224 | U32 cachedLitLength; |
---|
| 225 | const BYTE* cachedLiterals; |
---|
| 226 | ZSTD_stats_t stats; |
---|
| 227 | } seqStore_t; |
---|
| 228 | |
---|
| 229 | const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); |
---|
| 230 | void ZSTD_seqToCodes(const seqStore_t* seqStorePtr, size_t const nbSeq); |
---|
| 231 | int ZSTD_isSkipFrame(ZSTD_DCtx* dctx); |
---|
| 232 | |
---|
| 233 | /* custom memory allocation functions */ |
---|
| 234 | void* ZSTD_defaultAllocFunction(void* opaque, size_t size); |
---|
| 235 | void ZSTD_defaultFreeFunction(void* opaque, void* address); |
---|
| 236 | static const ZSTD_customMem defaultCustomMem = { ZSTD_defaultAllocFunction, ZSTD_defaultFreeFunction, NULL }; |
---|
| 237 | |
---|
| 238 | #endif /* ZSTD_CCOMMON_H_MODULE */ |
---|