blob: 9bcb40ffcc2bba00c73ecc9ebb52dff468047822 [file] [log] [blame]
Yann Collet4ded9e52016-08-30 10:04:33 -07001/**
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
Yann Collet2acb5d32015-10-29 16:49:43 +01009
Yann Collet2acb5d32015-10-29 16:49:43 +010010#ifndef ZSTD_CCOMMON_H_MODULE
11#define ZSTD_CCOMMON_H_MODULE
12
Yann Collet5c956d52016-09-06 15:05:19 +020013/*-*******************************************************
14* Compiler specifics
15*********************************************************/
16#ifdef _MSC_VER /* Visual Studio */
17# define FORCE_INLINE static __forceinline
18# include <intrin.h> /* For Visual 2005 */
Yann Collet5c956d52016-09-06 15:05:19 +020019# pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
Yann Collet69a54d12017-04-27 01:10:36 -070020# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
21# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
22# pragma warning(disable : 4324) /* disable: C4324: padded structure */
Yann Collet5c956d52016-09-06 15:05:19 +020023#else
24# if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
25# ifdef __GNUC__
26# define FORCE_INLINE static inline __attribute__((always_inline))
27# else
28# define FORCE_INLINE static inline
29# endif
30# else
31# define FORCE_INLINE static
32# endif /* __STDC_VERSION__ */
33#endif
34
Nick Terrelleb7873a2016-10-21 16:55:26 -070035#ifdef _MSC_VER
36# define FORCE_NOINLINE static __declspec(noinline)
37#else
38# ifdef __GNUC__
39# define FORCE_NOINLINE static __attribute__((__noinline__))
40# else
41# define FORCE_NOINLINE static
42# endif
43#endif
44
Yann Collet5c956d52016-09-06 15:05:19 +020045
Yann Collet7d360282016-02-12 00:07:30 +010046/*-*************************************
Yann Collet953ce722016-02-04 15:28:14 +010047* Dependencies
Yann Collet2acb5d32015-10-29 16:49:43 +010048***************************************/
49#include "mem.h"
Yann Collet977f1f32016-01-21 15:38:47 +010050#include "error_private.h"
Yann Colletd3b7f8d2016-06-04 19:47:02 +020051#define ZSTD_STATIC_LINKING_ONLY
52#include "zstd.h"
Yann Collet4bcc69b2017-03-01 11:33:25 -080053#ifndef XXH_STATIC_LINKING_ONLY
54# define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
55#endif
56#include "xxhash.h" /* XXH_reset, update, digest */
Yann Collet2acb5d32015-10-29 16:49:43 +010057
58
Yann Collet7d360282016-02-12 00:07:30 +010059/*-*************************************
Yann Collet3e21ec52016-09-06 15:36:19 +020060* shared macros
Yann Collet14983e72015-11-11 21:38:21 +010061***************************************/
Yann Collet4f818182017-04-17 17:57:35 -070062#undef MIN
63#undef MAX
Yann Colletbe2010e2015-10-31 12:57:14 +010064#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet14983e72015-11-11 21:38:21 +010065#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet3e21ec52016-09-06 15:36:19 +020066#define CHECK_F(f) { size_t const errcod = f; if (ERR_isError(errcod)) return errcod; } /* check and Forward error code */
Yann Collet95d07d72016-09-06 16:38:51 +020067#define CHECK_E(f, e) { size_t const errcod = f; if (ERR_isError(errcod)) return ERROR(e); } /* check and send Error code */
Yann Collet2acb5d32015-10-29 16:49:43 +010068
69
Yann Collet7d360282016-02-12 00:07:30 +010070/*-*************************************
Yann Collet14983e72015-11-11 21:38:21 +010071* Common constants
72***************************************/
inikep87d4f3d2016-03-02 15:56:24 +010073#define ZSTD_OPT_NUM (1<<12)
Yann Collet8cebfd12016-07-31 01:59:23 +020074#define ZSTD_DICT_MAGIC 0xEC30A437 /* v0.7+ */
Yann Collet88fcd292015-11-25 14:42:45 +010075
inikep5f49eba2016-08-10 15:01:53 +020076#define ZSTD_REP_NUM 3 /* number of repcodes */
77#define ZSTD_REP_CHECK (ZSTD_REP_NUM) /* number of repcodes to check by the optimal parser */
78#define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
79#define ZSTD_REP_MOVE_OPT (ZSTD_REP_NUM)
Yann Collet4266c0a2016-06-14 01:49:25 +020080static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
Yann Collet14983e72015-11-11 21:38:21 +010081
82#define KB *(1 <<10)
83#define MB *(1 <<20)
84#define GB *(1U<<30)
Yann Collet2acb5d32015-10-29 16:49:43 +010085
Yann Collet14983e72015-11-11 21:38:21 +010086#define BIT7 128
87#define BIT6 64
88#define BIT5 32
89#define BIT4 16
90#define BIT1 2
91#define BIT0 1
92
Yann Collet673f0d72016-06-06 00:26:38 +020093#define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
94static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
Yann Colletc46fb922016-05-29 05:01:04 +020095static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
Yann Collet37f3d1b2016-03-19 15:11:42 +010096
Yann Collet49bb0042016-06-04 20:17:38 +020097#define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
Yann Collet37f3d1b2016-03-19 15:11:42 +010098static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
Yann Colletc991cc12016-07-28 00:55:43 +020099typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
Yann Collet37f3d1b2016-03-19 15:11:42 +0100100
101#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
102#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
103
104#define HufLog 12
Yann Colletf8e7b532016-07-23 16:31:49 +0200105typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
Yann Collet14983e72015-11-11 21:38:21 +0100106
Yann Collet37f3d1b2016-03-19 15:11:42 +0100107#define LONGNBSEQ 0x7F00
108
inikep7bc19b62016-04-06 09:46:01 +0200109#define MINMATCH 3
Yann Collet14983e72015-11-11 21:38:21 +0100110
inikep3bfcfc72016-02-03 18:47:30 +0100111#define Litbits 8
inikep70b05452016-02-03 22:56:55 +0100112#define MaxLit ((1<<Litbits) - 1)
Yann Colletfadda6c2016-03-22 12:14:26 +0100113#define MaxML 52
Yann Colletb0aec172016-03-21 13:24:16 +0100114#define MaxLL 35
Yann Collet48537162016-04-07 15:24:29 +0200115#define MaxOff 28
Yann Collet4db09ef2016-03-18 22:23:49 +0100116#define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
Yann Colletbe391432016-03-22 23:19:28 +0100117#define MLFSELog 9
Yann Colletd64f4352016-03-21 00:07:42 +0100118#define LLFSELog 9
Yann Collet646693e2016-03-24 02:31:27 +0100119#define OffFSELog 8
inikepf3c65032016-03-04 20:04:25 +0100120
Yann Colletb0aec172016-03-21 13:24:16 +0100121static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
122 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
123 13,14,15,16 };
Yann Collet48537162016-04-07 15:24:29 +0200124static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
125 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
126 -1,-1,-1,-1 };
Yann Collet51f4d562016-09-22 15:57:28 +0200127#define LL_DEFAULTNORMLOG 6 /* for static allocation */
128static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
Yann Colletb0aec172016-03-21 13:24:16 +0100129
Yann Colletfadda6c2016-03-22 12:14:26 +0100130static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11,
Yann Collet48537162016-04-07 15:24:29 +0200133 12,13,14,15,16 };
134static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
Yann Colletfadda6c2016-03-22 12:14:26 +0100135 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Yann Collet48537162016-04-07 15:24:29 +0200136 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,
137 -1,-1,-1,-1,-1 };
Yann Collet51f4d562016-09-22 15:57:28 +0200138#define ML_DEFAULTNORMLOG 6 /* for static allocation */
139static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
Yann Colletfadda6c2016-03-22 12:14:26 +0100140
Yann Collet48537162016-04-07 15:24:29 +0200141static const S16 OF_defaultNorm[MaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
142 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1 };
Yann Collet51f4d562016-09-22 15:57:28 +0200143#define OF_DEFAULTNORMLOG 5 /* for static allocation */
144static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
Yann Collet48537162016-04-07 15:24:29 +0200145
Yann Collet2acb5d32015-10-29 16:49:43 +0100146
Yann Colletfb810d62016-01-28 00:18:06 +0100147/*-*******************************************
Yann Collet14983e72015-11-11 21:38:21 +0100148* Shared functions to include for inlining
Yann Colletfb810d62016-01-28 00:18:06 +0100149*********************************************/
Yann Collet2acb5d32015-10-29 16:49:43 +0100150static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
Yann Collet2acb5d32015-10-29 16:49:43 +0100151#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
152
Yann Collet953ce722016-02-04 15:28:14 +0100153/*! ZSTD_wildcopy() :
154* custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
Yann Collet37f3d1b2016-03-19 15:11:42 +0100155#define WILDCOPY_OVERLENGTH 8
Nick Terrell064a1432016-12-12 19:01:23 -0800156MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
Yann Collet2acb5d32015-10-29 16:49:43 +0100157{
158 const BYTE* ip = (const BYTE*)src;
159 BYTE* op = (BYTE*)dst;
160 BYTE* const oend = op + length;
Yann Collet50c5cdb2015-11-04 20:35:33 +0100161 do
162 COPY8(op, ip)
163 while (op < oend);
Yann Collet2acb5d32015-10-29 16:49:43 +0100164}
165
Yann Collet280f9a82016-08-08 00:44:00 +0200166MEM_STATIC void ZSTD_wildcopy_e(void* dst, const void* src, void* dstEnd) /* should be faster for decoding, but strangely, not verified on all platform */
167{
168 const BYTE* ip = (const BYTE*)src;
169 BYTE* op = (BYTE*)dst;
170 BYTE* const oend = (BYTE*)dstEnd;
171 do
172 COPY8(op, ip)
173 while (op < oend);
174}
175
Yann Collet7d360282016-02-12 00:07:30 +0100176
177/*-*******************************************
178* Private interfaces
179*********************************************/
inikep35b891c2016-05-20 19:42:20 +0200180typedef struct ZSTD_stats_s ZSTD_stats_t;
181
Yann Collet7d360282016-02-12 00:07:30 +0100182typedef struct {
inikep87d4f3d2016-03-02 15:56:24 +0100183 U32 off;
184 U32 len;
185} ZSTD_match_t;
186
187typedef struct {
188 U32 price;
189 U32 off;
190 U32 mlen;
191 U32 litlen;
inikep003c7a82016-07-27 11:07:13 +0200192 U32 rep[ZSTD_REP_NUM];
inikep87d4f3d2016-03-02 15:56:24 +0100193} ZSTD_optimal_t;
194
Yann Colleted57d852016-07-29 21:22:17 +0200195
196typedef struct seqDef_s {
197 U32 offset;
198 U16 litLength;
199 U16 matchLength;
200} seqDef;
201
202
inikep87d4f3d2016-03-02 15:56:24 +0100203typedef struct {
Yann Colletc0ce4f12016-07-30 00:55:13 +0200204 seqDef* sequencesStart;
Yann Colleted57d852016-07-29 21:22:17 +0200205 seqDef* sequences;
Yann Collet7d360282016-02-12 00:07:30 +0100206 BYTE* litStart;
207 BYTE* lit;
Yann Colleted57d852016-07-29 21:22:17 +0200208 BYTE* llCode;
209 BYTE* mlCode;
210 BYTE* ofCode;
Yann Collet5d393572016-04-07 17:19:00 +0200211 U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
212 U32 longLengthPos;
Yann Collet7d360282016-02-12 00:07:30 +0100213 /* opt */
inikep87d4f3d2016-03-02 15:56:24 +0100214 ZSTD_optimal_t* priceTable;
215 ZSTD_match_t* matchTable;
Yann Collet7d360282016-02-12 00:07:30 +0100216 U32* matchLengthFreq;
217 U32* litLengthFreq;
218 U32* litFreq;
219 U32* offCodeFreq;
220 U32 matchLengthSum;
inikepe0010e92016-02-23 16:25:04 +0100221 U32 matchSum;
Yann Collet7d360282016-02-12 00:07:30 +0100222 U32 litLengthSum;
223 U32 litSum;
224 U32 offCodeSum;
inikepb5a519f2016-03-09 15:45:01 +0100225 U32 log2matchLengthSum;
226 U32 log2matchSum;
227 U32 log2litLengthSum;
228 U32 log2litSum;
229 U32 log2offCodeSum;
230 U32 factor;
Przemyslaw Skibinski9ca65af2016-11-23 17:22:54 +0100231 U32 staticPrices;
inikepf7d210b2016-04-11 16:35:13 +0200232 U32 cachedPrice;
233 U32 cachedLitLength;
234 const BYTE* cachedLiterals;
Yann Collet7d360282016-02-12 00:07:30 +0100235} seqStore_t;
236
Yann Colletb44be742016-03-26 20:52:14 +0100237const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);
Yann Colleted57d852016-07-29 21:22:17 +0200238void ZSTD_seqToCodes(const seqStore_t* seqStorePtr);
inikepf772bf52016-05-31 12:43:46 +0200239int ZSTD_isSkipFrame(ZSTD_DCtx* dctx);
Yann Collet2acb5d32015-10-29 16:49:43 +0100240
inikepc4807f42016-06-02 15:11:39 +0200241/* custom memory allocation functions */
242void* ZSTD_defaultAllocFunction(void* opaque, size_t size);
243void ZSTD_defaultFreeFunction(void* opaque, void* address);
Przemyslaw Skibinski179555c2016-11-15 18:05:46 +0100244#ifndef ZSTD_DLL_IMPORT
Yann Collet63b5e7a2016-06-26 17:42:15 +0200245static const ZSTD_customMem defaultCustomMem = { ZSTD_defaultAllocFunction, ZSTD_defaultFreeFunction, NULL };
Przemyslaw Skibinski179555c2016-11-15 18:05:46 +0100246#endif
Yann Collet23b6e052016-08-28 21:05:43 -0700247void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
248void ZSTD_free(void* ptr, ZSTD_customMem customMem);
249
inikep2a746092016-06-03 14:53:51 +0200250
Yann Colletc154d9d2016-07-27 14:37:00 +0200251/*====== common function ======*/
252
253MEM_STATIC U32 ZSTD_highbit32(U32 val)
254{
255# if defined(_MSC_VER) /* Visual */
256 unsigned long r=0;
257 _BitScanReverse(&r, val);
258 return (unsigned)r;
259# elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
260 return 31 - __builtin_clz(val);
261# else /* Software version */
262 static const int 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 };
263 U32 v = val;
264 int r;
265 v |= v >> 1;
266 v |= v >> 2;
267 v |= v >> 4;
268 v |= v >> 8;
269 v |= v >> 16;
270 r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
271 return r;
272# endif
273}
274
275
Yann Collet32dfae62017-01-19 10:32:55 -0800276/* hidden functions */
277
278/* ZSTD_invalidateRepCodes() :
279 * ensures next compression will not use repcodes from previous block.
280 * Note : only works with regular variant;
281 * do not use with extDict variant ! */
282void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx);
283
284
Yann Collet2acb5d32015-10-29 16:49:43 +0100285#endif /* ZSTD_CCOMMON_H_MODULE */