blob: 5d2900eb7687a1d853e1abcc8f2f84514a432ef0 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Yann Collet4ded9e52016-08-30 10:04:33 -07002 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Collet2acb5d32015-10-29 16:49:43 +010010
Yann Collet2acb5d32015-10-29 16:49:43 +010011#ifndef ZSTD_CCOMMON_H_MODULE
12#define ZSTD_CCOMMON_H_MODULE
13
Yann Collet8b6aecf2017-11-07 15:27:06 -080014/* this module contains definitions which must be identical
15 * across compression, decompression and dictBuilder.
16 * It also contains a few functions useful to at least 2 of them
17 * and which benefit from being inlined */
Yann Collet5c956d52016-09-06 15:05:19 +020018
Yann Collet7d360282016-02-12 00:07:30 +010019/*-*************************************
Yann Collet953ce722016-02-04 15:28:14 +010020* Dependencies
Yann Collet2acb5d32015-10-29 16:49:43 +010021***************************************/
Nick Terrell565e9252017-08-14 17:20:50 -070022#include "compiler.h"
Yann Collet2acb5d32015-10-29 16:49:43 +010023#include "mem.h"
Yann Collet977f1f32016-01-21 15:38:47 +010024#include "error_private.h"
Yann Colletd3b7f8d2016-06-04 19:47:02 +020025#define ZSTD_STATIC_LINKING_ONLY
26#include "zstd.h"
Nick Terrellde0414b2017-07-12 19:08:24 -070027#define FSE_STATIC_LINKING_ONLY
28#include "fse.h"
29#define HUF_STATIC_LINKING_ONLY
30#include "huf.h"
Yann Collet4bcc69b2017-03-01 11:33:25 -080031#ifndef XXH_STATIC_LINKING_ONLY
Yann Collet58e8d792017-06-02 18:20:48 -070032# define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
Yann Collet4bcc69b2017-03-01 11:33:25 -080033#endif
Yann Collet58e8d792017-06-02 18:20:48 -070034#include "xxhash.h" /* XXH_reset, update, digest */
35
36
Nick Terrellde6c6bc2017-08-24 18:09:50 -070037#if defined (__cplusplus)
38extern "C" {
39#endif
40
41
Yann Collet58e8d792017-06-02 18:20:48 -070042/*-*************************************
43* Debug
44***************************************/
45#if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=1)
46# include <assert.h>
47#else
48# ifndef assert
49# define assert(condition) ((void)0)
50# endif
51#endif
52
53#define ZSTD_STATIC_ASSERT(c) { enum { ZSTD_static_assert = 1/(int)(!!(c)) }; }
54
55#if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=2)
56# include <stdio.h>
Yann Collet9a11f702017-11-13 02:19:36 -080057extern int g_debuglog_enable;
Yann Collet33a66392017-06-28 11:09:43 -070058/* recommended values for ZSTD_DEBUG display levels :
Yann Colleta3d99262017-06-29 14:44:49 -070059 * 1 : no display, enables assert() only
Yann Colletf299fa32017-12-19 09:43:03 +010060 * 2 : reserved for currently active debug path
61 * 3 : events once per object lifetime (CCtx, CDict, etc.)
Yann Collet33a66392017-06-28 11:09:43 -070062 * 4 : events once per frame
63 * 5 : events once per block
64 * 6 : events once per sequence (*very* verbose) */
Yann Colletf299fa32017-12-19 09:43:03 +010065# define RAWLOG(l, ...) { \
66 if ((g_debuglog_enable) & (l<=ZSTD_DEBUG)) { \
67 fprintf(stderr, __VA_ARGS__); \
Yann Collet4202b2e2017-11-14 18:08:17 -080068 } }
Yann Colletf299fa32017-12-19 09:43:03 +010069# define DEBUGLOG(l, ...) { \
70 if ((g_debuglog_enable) & (l<=ZSTD_DEBUG)) { \
71 fprintf(stderr, __FILE__ ": " __VA_ARGS__); \
72 fprintf(stderr, " \n"); \
Yann Collet58e8d792017-06-02 18:20:48 -070073 } }
74#else
Yann Collet4202b2e2017-11-14 18:08:17 -080075# define RAWLOG(l, ...) {} /* disabled */
76# define DEBUGLOG(l, ...) {} /* disabled */
Yann Collet58e8d792017-06-02 18:20:48 -070077#endif
Yann Collet2acb5d32015-10-29 16:49:43 +010078
79
Yann Collet7d360282016-02-12 00:07:30 +010080/*-*************************************
Yann Collet3e21ec52016-09-06 15:36:19 +020081* shared macros
Yann Collet14983e72015-11-11 21:38:21 +010082***************************************/
Yann Collet4f818182017-04-17 17:57:35 -070083#undef MIN
84#undef MAX
Yann Colletbe2010e2015-10-31 12:57:14 +010085#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet14983e72015-11-11 21:38:21 +010086#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet3e21ec52016-09-06 15:36:19 +020087#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 +020088#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 +010089
90
Yann Collet7d360282016-02-12 00:07:30 +010091/*-*************************************
Yann Collet14983e72015-11-11 21:38:21 +010092* Common constants
93***************************************/
inikep87d4f3d2016-03-02 15:56:24 +010094#define ZSTD_OPT_NUM (1<<12)
Yann Collet88fcd292015-11-25 14:42:45 +010095
inikep5f49eba2016-08-10 15:01:53 +020096#define ZSTD_REP_NUM 3 /* number of repcodes */
inikep5f49eba2016-08-10 15:01:53 +020097#define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
Yann Collet4266c0a2016-06-14 01:49:25 +020098static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
Yann Collet14983e72015-11-11 21:38:21 +010099
100#define KB *(1 <<10)
101#define MB *(1 <<20)
102#define GB *(1U<<30)
Yann Collet2acb5d32015-10-29 16:49:43 +0100103
Yann Collet14983e72015-11-11 21:38:21 +0100104#define BIT7 128
105#define BIT6 64
106#define BIT5 32
107#define BIT4 16
108#define BIT1 2
109#define BIT0 1
110
Yann Collet673f0d72016-06-06 00:26:38 +0200111#define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
Nick Terrellc233bdb2017-09-22 14:04:39 -0700112#define ZSTD_WINDOWLOG_DEFAULTMAX 27 /* Default maximum allowed window log */
Yann Collet673f0d72016-06-06 00:26:38 +0200113static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
Yann Colletc46fb922016-05-29 05:01:04 +0200114static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
Yann Collet37f3d1b2016-03-19 15:11:42 +0100115
Yann Colletdf4e9bb2017-09-26 14:31:06 -0700116#define ZSTD_FRAMEIDSIZE 4
117static const size_t ZSTD_frameIdSize = ZSTD_FRAMEIDSIZE; /* magic number size */
Yann Colletb8d4a382017-09-25 15:25:07 -0700118
Yann Collet49bb0042016-06-04 20:17:38 +0200119#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 +0100120static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
Yann Colletc991cc12016-07-28 00:55:43 +0200121typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
Yann Collet37f3d1b2016-03-19 15:11:42 +0100122
123#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
124#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
125
126#define HufLog 12
Yann Colletf8e7b532016-07-23 16:31:49 +0200127typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
Yann Collet14983e72015-11-11 21:38:21 +0100128
Yann Collet37f3d1b2016-03-19 15:11:42 +0100129#define LONGNBSEQ 0x7F00
130
inikep7bc19b62016-04-06 09:46:01 +0200131#define MINMATCH 3
Yann Collet14983e72015-11-11 21:38:21 +0100132
inikep3bfcfc72016-02-03 18:47:30 +0100133#define Litbits 8
inikep70b05452016-02-03 22:56:55 +0100134#define MaxLit ((1<<Litbits) - 1)
Yann Colletfadda6c2016-03-22 12:14:26 +0100135#define MaxML 52
Yann Colletb0aec172016-03-21 13:24:16 +0100136#define MaxLL 35
Nick Terrellbbe77212017-09-18 16:54:53 -0700137#define DefaultMaxOff 28
138#define MaxOff 31
Yann Collet4db09ef2016-03-18 22:23:49 +0100139#define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
Yann Colletbe391432016-03-22 23:19:28 +0100140#define MLFSELog 9
Yann Colletd64f4352016-03-21 00:07:42 +0100141#define LLFSELog 9
Yann Collet646693e2016-03-24 02:31:27 +0100142#define OffFSELog 8
inikepf3c65032016-03-04 20:04:25 +0100143
Yann Collet4191efa2017-11-08 11:05:32 -0800144static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
145 0, 0, 0, 0, 0, 0, 0, 0,
146 1, 1, 1, 1, 2, 2, 3, 3,
147 4, 6, 7, 8, 9,10,11,12,
Yann Colletb0aec172016-03-21 13:24:16 +0100148 13,14,15,16 };
Yann Collet4191efa2017-11-08 11:05:32 -0800149static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2,
150 2, 2, 2, 2, 2, 1, 1, 1,
151 2, 2, 2, 2, 2, 2, 2, 2,
152 2, 3, 2, 1, 1, 1, 1, 1,
Yann Collet48537162016-04-07 15:24:29 +0200153 -1,-1,-1,-1 };
Yann Collet51f4d562016-09-22 15:57:28 +0200154#define LL_DEFAULTNORMLOG 6 /* for static allocation */
155static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
Yann Colletb0aec172016-03-21 13:24:16 +0100156
Yann Collet4191efa2017-11-08 11:05:32 -0800157static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
158 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0, 0, 0, 0,
161 1, 1, 1, 1, 2, 2, 3, 3,
162 4, 4, 5, 7, 8, 9,10,11,
Yann Collet48537162016-04-07 15:24:29 +0200163 12,13,14,15,16 };
Yann Collet4191efa2017-11-08 11:05:32 -0800164static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2,
165 2, 1, 1, 1, 1, 1, 1, 1,
166 1, 1, 1, 1, 1, 1, 1, 1,
167 1, 1, 1, 1, 1, 1, 1, 1,
168 1, 1, 1, 1, 1, 1, 1, 1,
169 1, 1, 1, 1, 1, 1,-1,-1,
Yann Collet48537162016-04-07 15:24:29 +0200170 -1,-1,-1,-1,-1 };
Yann Collet51f4d562016-09-22 15:57:28 +0200171#define ML_DEFAULTNORMLOG 6 /* for static allocation */
172static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
Yann Colletfadda6c2016-03-22 12:14:26 +0100173
Yann Collet4191efa2017-11-08 11:05:32 -0800174static const S16 OF_defaultNorm[DefaultMaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2,
175 2, 1, 1, 1, 1, 1, 1, 1,
176 1, 1, 1, 1, 1, 1, 1, 1,
177 -1,-1,-1,-1,-1 };
Yann Collet51f4d562016-09-22 15:57:28 +0200178#define OF_DEFAULTNORMLOG 5 /* for static allocation */
179static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
Yann Collet48537162016-04-07 15:24:29 +0200180
Yann Collet2acb5d32015-10-29 16:49:43 +0100181
Yann Colletfb810d62016-01-28 00:18:06 +0100182/*-*******************************************
Yann Collet14983e72015-11-11 21:38:21 +0100183* Shared functions to include for inlining
Yann Colletfb810d62016-01-28 00:18:06 +0100184*********************************************/
Yann Collet2acb5d32015-10-29 16:49:43 +0100185static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
Yann Collet2acb5d32015-10-29 16:49:43 +0100186#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
187
Yann Collet953ce722016-02-04 15:28:14 +0100188/*! ZSTD_wildcopy() :
Yann Colletcdade552017-11-17 11:40:08 -0800189 * custom version of memcpy(), can overwrite up to WILDCOPY_OVERLENGTH bytes (if length==0) */
Yann Collet37f3d1b2016-03-19 15:11:42 +0100190#define WILDCOPY_OVERLENGTH 8
Nick Terrell064a1432016-12-12 19:01:23 -0800191MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
Yann Collet2acb5d32015-10-29 16:49:43 +0100192{
193 const BYTE* ip = (const BYTE*)src;
194 BYTE* op = (BYTE*)dst;
195 BYTE* const oend = op + length;
Yann Collet50c5cdb2015-11-04 20:35:33 +0100196 do
197 COPY8(op, ip)
198 while (op < oend);
Yann Collet2acb5d32015-10-29 16:49:43 +0100199}
200
Yann Collet280f9a82016-08-08 00:44:00 +0200201MEM_STATIC void ZSTD_wildcopy_e(void* dst, const void* src, void* dstEnd) /* should be faster for decoding, but strangely, not verified on all platform */
202{
203 const BYTE* ip = (const BYTE*)src;
204 BYTE* op = (BYTE*)dst;
205 BYTE* const oend = (BYTE*)dstEnd;
206 do
207 COPY8(op, ip)
208 while (op < oend);
209}
210
Yann Collet7d360282016-02-12 00:07:30 +0100211
212/*-*******************************************
Yann Collet8b6aecf2017-11-07 15:27:06 -0800213* Private declarations
Yann Collet7d360282016-02-12 00:07:30 +0100214*********************************************/
Yann Colleted57d852016-07-29 21:22:17 +0200215typedef struct seqDef_s {
216 U32 offset;
217 U16 litLength;
218 U16 matchLength;
219} seqDef;
220
inikep87d4f3d2016-03-02 15:56:24 +0100221typedef struct {
Yann Colletc0ce4f12016-07-30 00:55:13 +0200222 seqDef* sequencesStart;
Yann Colleted57d852016-07-29 21:22:17 +0200223 seqDef* sequences;
Yann Collet7d360282016-02-12 00:07:30 +0100224 BYTE* litStart;
225 BYTE* lit;
Yann Colleted57d852016-07-29 21:22:17 +0200226 BYTE* llCode;
227 BYTE* mlCode;
228 BYTE* ofCode;
Yann Collet5d393572016-04-07 17:19:00 +0200229 U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
230 U32 longLengthPos;
Nick Terrelle1982302017-07-17 12:27:24 -0700231 U32 rep[ZSTD_REP_NUM];
232 U32 repToConfirm[ZSTD_REP_NUM];
Nick Terrell7a28b9e2017-07-17 15:29:11 -0700233} seqStore_t;
234
Yann Collet8b6aecf2017-11-07 15:27:06 -0800235const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); /* compress & dictBuilder */
236void ZSTD_seqToCodes(const seqStore_t* seqStorePtr); /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
Yann Collet2acb5d32015-10-29 16:49:43 +0100237
inikepc4807f42016-06-02 15:11:39 +0200238/* custom memory allocation functions */
Yann Collet23b6e052016-08-28 21:05:43 -0700239void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
Yann Collet44e45e82017-05-30 16:12:06 -0700240void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
Yann Collet23b6e052016-08-28 21:05:43 -0700241void ZSTD_free(void* ptr, ZSTD_customMem customMem);
242
inikep2a746092016-06-03 14:53:51 +0200243
Yann Collet8b6aecf2017-11-07 15:27:06 -0800244MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
Yann Colletc154d9d2016-07-27 14:37:00 +0200245{
Stella Laue50ed1f2017-08-22 11:55:42 -0700246 assert(val != 0);
247 {
Yann Colletc154d9d2016-07-27 14:37:00 +0200248# if defined(_MSC_VER) /* Visual */
Stella Laue50ed1f2017-08-22 11:55:42 -0700249 unsigned long r=0;
250 _BitScanReverse(&r, val);
251 return (unsigned)r;
Yann Colletc154d9d2016-07-27 14:37:00 +0200252# elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
Stella Laue50ed1f2017-08-22 11:55:42 -0700253 return 31 - __builtin_clz(val);
Yann Colletc154d9d2016-07-27 14:37:00 +0200254# else /* Software version */
Yann Collet0a0a2122017-11-28 14:07:03 -0800255 static const U32 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 };
Stella Laue50ed1f2017-08-22 11:55:42 -0700256 U32 v = val;
Stella Laue50ed1f2017-08-22 11:55:42 -0700257 v |= v >> 1;
258 v |= v >> 2;
259 v |= v >> 4;
260 v |= v >> 8;
261 v |= v >> 16;
Yann Collet0a0a2122017-11-28 14:07:03 -0800262 return DeBruijnClz[(v * 0x07C4ACDDU) >> 27];
Yann Colletc154d9d2016-07-27 14:37:00 +0200263# endif
Stella Laue50ed1f2017-08-22 11:55:42 -0700264 }
Yann Colletc154d9d2016-07-27 14:37:00 +0200265}
266
267
Yann Collet32dfae62017-01-19 10:32:55 -0800268/* ZSTD_invalidateRepCodes() :
269 * ensures next compression will not use repcodes from previous block.
270 * Note : only works with regular variant;
271 * do not use with extDict variant ! */
Yann Collet8b6aecf2017-11-07 15:27:06 -0800272void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); /* zstdmt, adaptive_compression (shouldn't get this definition from here) */
Yann Collet32dfae62017-01-19 10:32:55 -0800273
274
Yann Colletf04deff2017-07-06 01:42:46 -0700275typedef struct {
276 blockType_e blockType;
277 U32 lastBlock;
278 U32 origSize;
279} blockProperties_t;
280
281/*! ZSTD_getcBlockSize() :
Yann Collet4191efa2017-11-08 11:05:32 -0800282 * Provides the size of compressed block from block header `src` */
283/* Used by: decompress, fullbench (does not get its definition from here) */
Yann Colletf04deff2017-07-06 01:42:46 -0700284size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
285 blockProperties_t* bpPtr);
286
Nick Terrellde6c6bc2017-08-24 18:09:50 -0700287#if defined (__cplusplus)
288}
289#endif
Yann Colletf04deff2017-07-06 01:42:46 -0700290
Yann Collet2acb5d32015-10-29 16:49:43 +0100291#endif /* ZSTD_CCOMMON_H_MODULE */