blob: f2c4e6249fb89651fb1a7473aba351d92ca1496d [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 */
Yann Collet9a691e02017-05-31 01:17:44 -070021# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
Yann Collet69a54d12017-04-27 01:10:36 -070022# 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
Yann Collet58e8d792017-06-02 18:20:48 -070054# define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
Yann Collet4bcc69b2017-03-01 11:33:25 -080055#endif
Yann Collet58e8d792017-06-02 18:20:48 -070056#include "xxhash.h" /* XXH_reset, update, digest */
57
58
59/*-*************************************
60* Debug
61***************************************/
62#if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=1)
63# include <assert.h>
64#else
65# ifndef assert
66# define assert(condition) ((void)0)
67# endif
68#endif
69
70#define ZSTD_STATIC_ASSERT(c) { enum { ZSTD_static_assert = 1/(int)(!!(c)) }; }
71
72#if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=2)
73# include <stdio.h>
Yann Collet33a66392017-06-28 11:09:43 -070074/* recommended values for ZSTD_DEBUG display levels :
Yann Colleta3d99262017-06-29 14:44:49 -070075 * 1 : no display, enables assert() only
Yann Collet33a66392017-06-28 11:09:43 -070076 * 2 : reserved for currently active debugging path
77 * 3 : events once per object lifetime (CCtx, CDict)
78 * 4 : events once per frame
79 * 5 : events once per block
80 * 6 : events once per sequence (*very* verbose) */
81# define DEBUGLOG(l, ...) { \
82 if (l<=ZSTD_DEBUG) { \
83 fprintf(stderr, __FILE__ ": "); \
84 fprintf(stderr, __VA_ARGS__); \
85 fprintf(stderr, " \n"); \
Yann Collet58e8d792017-06-02 18:20:48 -070086 } }
87#else
88# define DEBUGLOG(l, ...) {} /* disabled */
89#endif
Yann Collet2acb5d32015-10-29 16:49:43 +010090
91
Yann Collet7d360282016-02-12 00:07:30 +010092/*-*************************************
Yann Collet3e21ec52016-09-06 15:36:19 +020093* shared macros
Yann Collet14983e72015-11-11 21:38:21 +010094***************************************/
Yann Collet4f818182017-04-17 17:57:35 -070095#undef MIN
96#undef MAX
Yann Colletbe2010e2015-10-31 12:57:14 +010097#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet14983e72015-11-11 21:38:21 +010098#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet3e21ec52016-09-06 15:36:19 +020099#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 +0200100#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 +0100101
102
Yann Collet7d360282016-02-12 00:07:30 +0100103/*-*************************************
Yann Collet14983e72015-11-11 21:38:21 +0100104* Common constants
105***************************************/
inikep87d4f3d2016-03-02 15:56:24 +0100106#define ZSTD_OPT_NUM (1<<12)
Yann Collet88fcd292015-11-25 14:42:45 +0100107
inikep5f49eba2016-08-10 15:01:53 +0200108#define ZSTD_REP_NUM 3 /* number of repcodes */
109#define ZSTD_REP_CHECK (ZSTD_REP_NUM) /* number of repcodes to check by the optimal parser */
110#define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
111#define ZSTD_REP_MOVE_OPT (ZSTD_REP_NUM)
Yann Collet4266c0a2016-06-14 01:49:25 +0200112static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
Yann Collet14983e72015-11-11 21:38:21 +0100113
114#define KB *(1 <<10)
115#define MB *(1 <<20)
116#define GB *(1U<<30)
Yann Collet2acb5d32015-10-29 16:49:43 +0100117
Yann Collet14983e72015-11-11 21:38:21 +0100118#define BIT7 128
119#define BIT6 64
120#define BIT5 32
121#define BIT4 16
122#define BIT1 2
123#define BIT0 1
124
Yann Collet673f0d72016-06-06 00:26:38 +0200125#define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
126static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
Yann Colletc46fb922016-05-29 05:01:04 +0200127static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
Yann Collet37f3d1b2016-03-19 15:11:42 +0100128
Yann Collet49bb0042016-06-04 20:17:38 +0200129#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 +0100130static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
Yann Colletc991cc12016-07-28 00:55:43 +0200131typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
Yann Collet37f3d1b2016-03-19 15:11:42 +0100132
133#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
134#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
135
136#define HufLog 12
Yann Colletf8e7b532016-07-23 16:31:49 +0200137typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
Yann Collet14983e72015-11-11 21:38:21 +0100138
Yann Collet37f3d1b2016-03-19 15:11:42 +0100139#define LONGNBSEQ 0x7F00
140
inikep7bc19b62016-04-06 09:46:01 +0200141#define MINMATCH 3
Yann Collet14983e72015-11-11 21:38:21 +0100142
inikep3bfcfc72016-02-03 18:47:30 +0100143#define Litbits 8
inikep70b05452016-02-03 22:56:55 +0100144#define MaxLit ((1<<Litbits) - 1)
Yann Colletfadda6c2016-03-22 12:14:26 +0100145#define MaxML 52
Yann Colletb0aec172016-03-21 13:24:16 +0100146#define MaxLL 35
Yann Collet48537162016-04-07 15:24:29 +0200147#define MaxOff 28
Yann Collet4db09ef2016-03-18 22:23:49 +0100148#define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
Yann Colletbe391432016-03-22 23:19:28 +0100149#define MLFSELog 9
Yann Colletd64f4352016-03-21 00:07:42 +0100150#define LLFSELog 9
Yann Collet646693e2016-03-24 02:31:27 +0100151#define OffFSELog 8
inikepf3c65032016-03-04 20:04:25 +0100152
Yann Colletb0aec172016-03-21 13:24:16 +0100153static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
154 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
155 13,14,15,16 };
Yann Collet48537162016-04-07 15:24:29 +0200156static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
157 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
158 -1,-1,-1,-1 };
Yann Collet51f4d562016-09-22 15:57:28 +0200159#define LL_DEFAULTNORMLOG 6 /* for static allocation */
160static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
Yann Colletb0aec172016-03-21 13:24:16 +0100161
Yann Colletfadda6c2016-03-22 12:14:26 +0100162static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11,
Yann Collet48537162016-04-07 15:24:29 +0200165 12,13,14,15,16 };
166static 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 +0100167 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Yann Collet48537162016-04-07 15:24:29 +0200168 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,
169 -1,-1,-1,-1,-1 };
Yann Collet51f4d562016-09-22 15:57:28 +0200170#define ML_DEFAULTNORMLOG 6 /* for static allocation */
171static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
Yann Colletfadda6c2016-03-22 12:14:26 +0100172
Yann Collet48537162016-04-07 15:24:29 +0200173static const S16 OF_defaultNorm[MaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
174 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1 };
Yann Collet51f4d562016-09-22 15:57:28 +0200175#define OF_DEFAULTNORMLOG 5 /* for static allocation */
176static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
Yann Collet48537162016-04-07 15:24:29 +0200177
Yann Collet2acb5d32015-10-29 16:49:43 +0100178
Yann Colletfb810d62016-01-28 00:18:06 +0100179/*-*******************************************
Yann Collet14983e72015-11-11 21:38:21 +0100180* Shared functions to include for inlining
Yann Colletfb810d62016-01-28 00:18:06 +0100181*********************************************/
Yann Collet2acb5d32015-10-29 16:49:43 +0100182static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
Yann Collet2acb5d32015-10-29 16:49:43 +0100183#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
184
Yann Collet953ce722016-02-04 15:28:14 +0100185/*! ZSTD_wildcopy() :
186* custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
Yann Collet37f3d1b2016-03-19 15:11:42 +0100187#define WILDCOPY_OVERLENGTH 8
Nick Terrell064a1432016-12-12 19:01:23 -0800188MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
Yann Collet2acb5d32015-10-29 16:49:43 +0100189{
190 const BYTE* ip = (const BYTE*)src;
191 BYTE* op = (BYTE*)dst;
192 BYTE* const oend = op + length;
Yann Collet50c5cdb2015-11-04 20:35:33 +0100193 do
194 COPY8(op, ip)
195 while (op < oend);
Yann Collet2acb5d32015-10-29 16:49:43 +0100196}
197
Yann Collet280f9a82016-08-08 00:44:00 +0200198MEM_STATIC void ZSTD_wildcopy_e(void* dst, const void* src, void* dstEnd) /* should be faster for decoding, but strangely, not verified on all platform */
199{
200 const BYTE* ip = (const BYTE*)src;
201 BYTE* op = (BYTE*)dst;
202 BYTE* const oend = (BYTE*)dstEnd;
203 do
204 COPY8(op, ip)
205 while (op < oend);
206}
207
Yann Collet7d360282016-02-12 00:07:30 +0100208
209/*-*******************************************
210* Private interfaces
211*********************************************/
inikep35b891c2016-05-20 19:42:20 +0200212typedef struct ZSTD_stats_s ZSTD_stats_t;
213
Yann Collet7d360282016-02-12 00:07:30 +0100214typedef struct {
inikep87d4f3d2016-03-02 15:56:24 +0100215 U32 off;
216 U32 len;
217} ZSTD_match_t;
218
219typedef struct {
220 U32 price;
221 U32 off;
222 U32 mlen;
223 U32 litlen;
inikep003c7a82016-07-27 11:07:13 +0200224 U32 rep[ZSTD_REP_NUM];
inikep87d4f3d2016-03-02 15:56:24 +0100225} ZSTD_optimal_t;
226
Yann Colleted57d852016-07-29 21:22:17 +0200227
228typedef struct seqDef_s {
229 U32 offset;
230 U16 litLength;
231 U16 matchLength;
232} seqDef;
233
234
inikep87d4f3d2016-03-02 15:56:24 +0100235typedef struct {
Yann Colletc0ce4f12016-07-30 00:55:13 +0200236 seqDef* sequencesStart;
Yann Colleted57d852016-07-29 21:22:17 +0200237 seqDef* sequences;
Yann Collet7d360282016-02-12 00:07:30 +0100238 BYTE* litStart;
239 BYTE* lit;
Yann Colleted57d852016-07-29 21:22:17 +0200240 BYTE* llCode;
241 BYTE* mlCode;
242 BYTE* ofCode;
Yann Collet5d393572016-04-07 17:19:00 +0200243 U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
244 U32 longLengthPos;
Yann Collet7d360282016-02-12 00:07:30 +0100245 /* opt */
inikep87d4f3d2016-03-02 15:56:24 +0100246 ZSTD_optimal_t* priceTable;
247 ZSTD_match_t* matchTable;
Yann Collet7d360282016-02-12 00:07:30 +0100248 U32* matchLengthFreq;
249 U32* litLengthFreq;
250 U32* litFreq;
251 U32* offCodeFreq;
252 U32 matchLengthSum;
inikepe0010e92016-02-23 16:25:04 +0100253 U32 matchSum;
Yann Collet7d360282016-02-12 00:07:30 +0100254 U32 litLengthSum;
255 U32 litSum;
256 U32 offCodeSum;
inikepb5a519f2016-03-09 15:45:01 +0100257 U32 log2matchLengthSum;
258 U32 log2matchSum;
259 U32 log2litLengthSum;
260 U32 log2litSum;
261 U32 log2offCodeSum;
262 U32 factor;
Przemyslaw Skibinski9ca65af2016-11-23 17:22:54 +0100263 U32 staticPrices;
inikepf7d210b2016-04-11 16:35:13 +0200264 U32 cachedPrice;
265 U32 cachedLitLength;
266 const BYTE* cachedLiterals;
Yann Collet7d360282016-02-12 00:07:30 +0100267} seqStore_t;
268
Yann Colletb44be742016-03-26 20:52:14 +0100269const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);
Yann Colleted57d852016-07-29 21:22:17 +0200270void ZSTD_seqToCodes(const seqStore_t* seqStorePtr);
Yann Collet2acb5d32015-10-29 16:49:43 +0100271
inikepc4807f42016-06-02 15:11:39 +0200272/* custom memory allocation functions */
Yann Collet23b6e052016-08-28 21:05:43 -0700273void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
Yann Collet44e45e82017-05-30 16:12:06 -0700274void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
Yann Collet23b6e052016-08-28 21:05:43 -0700275void ZSTD_free(void* ptr, ZSTD_customMem customMem);
276
inikep2a746092016-06-03 14:53:51 +0200277
Yann Colletc154d9d2016-07-27 14:37:00 +0200278/*====== common function ======*/
279
280MEM_STATIC U32 ZSTD_highbit32(U32 val)
281{
282# if defined(_MSC_VER) /* Visual */
283 unsigned long r=0;
284 _BitScanReverse(&r, val);
285 return (unsigned)r;
286# elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
287 return 31 - __builtin_clz(val);
288# else /* Software version */
289 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 };
290 U32 v = val;
291 int r;
292 v |= v >> 1;
293 v |= v >> 2;
294 v |= v >> 4;
295 v |= v >> 8;
296 v |= v >> 16;
297 r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27];
298 return r;
299# endif
300}
301
302
Yann Collet32dfae62017-01-19 10:32:55 -0800303/* hidden functions */
304
305/* ZSTD_invalidateRepCodes() :
306 * ensures next compression will not use repcodes from previous block.
307 * Note : only works with regular variant;
308 * do not use with extDict variant ! */
309void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx);
310
311
Yann Collet8c910d22017-06-03 01:15:02 -0700312/*! ZSTD_initCStream_internal() :
313 * Private use only. Init streaming operation.
314 * expects params to be valid.
315 * must receive dict, or cdict, or none, but not both.
316 * @return : 0, or an error code */
317size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs,
cyan49738bcbf422017-06-04 23:52:00 -0700318 const void* dict, size_t dictSize,
319 const ZSTD_CDict* cdict,
320 ZSTD_parameters params, unsigned long long pledgedSrcSize);
Yann Collet8c910d22017-06-03 01:15:02 -0700321
Yann Collet5a773612017-07-03 15:21:24 -0700322/*! ZSTD_compressStream_generic() :
Yann Colletd5c046c2017-06-30 14:51:01 -0700323 * Private use only. To be called from zstdmt_compress.c in single-thread mode. */
324size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs,
325 ZSTD_outBuffer* output,
326 ZSTD_inBuffer* input,
327 ZSTD_EndDirective const flushMode);
Yann Collet8c910d22017-06-03 01:15:02 -0700328
329/*! ZSTD_getParamsFromCDict() :
330 * as the name implies */
331ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict);
332
333
Yann Collet2acb5d32015-10-29 16:49:43 +0100334#endif /* ZSTD_CCOMMON_H_MODULE */