blob: 981ce7f54c8589602fbf2136d91d53dc0543b2e2 [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
Yann Collet4d5bd322018-05-09 12:00:12 -070055#undef RAWLOG
56#undef DEBUGLOG
Yann Collet58e8d792017-06-02 18:20:48 -070057#if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=2)
58# include <stdio.h>
Yann Collet9a11f702017-11-13 02:19:36 -080059extern int g_debuglog_enable;
Yann Collet33a66392017-06-28 11:09:43 -070060/* recommended values for ZSTD_DEBUG display levels :
Yann Colleta3d99262017-06-29 14:44:49 -070061 * 1 : no display, enables assert() only
Yann Colletf299fa32017-12-19 09:43:03 +010062 * 2 : reserved for currently active debug path
63 * 3 : events once per object lifetime (CCtx, CDict, etc.)
Yann Collet33a66392017-06-28 11:09:43 -070064 * 4 : events once per frame
65 * 5 : events once per block
66 * 6 : events once per sequence (*very* verbose) */
Yann Colletf299fa32017-12-19 09:43:03 +010067# define RAWLOG(l, ...) { \
68 if ((g_debuglog_enable) & (l<=ZSTD_DEBUG)) { \
69 fprintf(stderr, __VA_ARGS__); \
Yann Collet4202b2e2017-11-14 18:08:17 -080070 } }
Yann Colletf299fa32017-12-19 09:43:03 +010071# define DEBUGLOG(l, ...) { \
72 if ((g_debuglog_enable) & (l<=ZSTD_DEBUG)) { \
73 fprintf(stderr, __FILE__ ": " __VA_ARGS__); \
74 fprintf(stderr, " \n"); \
Yann Collet58e8d792017-06-02 18:20:48 -070075 } }
76#else
Yann Collet4202b2e2017-11-14 18:08:17 -080077# define RAWLOG(l, ...) {} /* disabled */
78# define DEBUGLOG(l, ...) {} /* disabled */
Yann Collet58e8d792017-06-02 18:20:48 -070079#endif
Yann Collet2acb5d32015-10-29 16:49:43 +010080
81
Yann Collet7d360282016-02-12 00:07:30 +010082/*-*************************************
Yann Collet3e21ec52016-09-06 15:36:19 +020083* shared macros
Yann Collet14983e72015-11-11 21:38:21 +010084***************************************/
Yann Collet4f818182017-04-17 17:57:35 -070085#undef MIN
86#undef MAX
Yann Colletbe2010e2015-10-31 12:57:14 +010087#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet14983e72015-11-11 21:38:21 +010088#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet3e21ec52016-09-06 15:36:19 +020089#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 +020090#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 +010091
92
Yann Collet7d360282016-02-12 00:07:30 +010093/*-*************************************
Yann Collet14983e72015-11-11 21:38:21 +010094* Common constants
95***************************************/
inikep87d4f3d2016-03-02 15:56:24 +010096#define ZSTD_OPT_NUM (1<<12)
Yann Collet88fcd292015-11-25 14:42:45 +010097
inikep5f49eba2016-08-10 15:01:53 +020098#define ZSTD_REP_NUM 3 /* number of repcodes */
inikep5f49eba2016-08-10 15:01:53 +020099#define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
Yann Collet4266c0a2016-06-14 01:49:25 +0200100static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
Yann Collet14983e72015-11-11 21:38:21 +0100101
102#define KB *(1 <<10)
103#define MB *(1 <<20)
104#define GB *(1U<<30)
Yann Collet2acb5d32015-10-29 16:49:43 +0100105
Yann Collet14983e72015-11-11 21:38:21 +0100106#define BIT7 128
107#define BIT6 64
108#define BIT5 32
109#define BIT4 16
110#define BIT1 2
111#define BIT0 1
112
Yann Collet673f0d72016-06-06 00:26:38 +0200113#define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
Nick Terrellc233bdb2017-09-22 14:04:39 -0700114#define ZSTD_WINDOWLOG_DEFAULTMAX 27 /* Default maximum allowed window log */
Yann Collet673f0d72016-06-06 00:26:38 +0200115static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
Yann Colletc46fb922016-05-29 05:01:04 +0200116static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
Yann Collet37f3d1b2016-03-19 15:11:42 +0100117
Yann Colletdf4e9bb2017-09-26 14:31:06 -0700118#define ZSTD_FRAMEIDSIZE 4
119static const size_t ZSTD_frameIdSize = ZSTD_FRAMEIDSIZE; /* magic number size */
Yann Colletb8d4a382017-09-25 15:25:07 -0700120
Yann Collet49bb0042016-06-04 20:17:38 +0200121#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 +0100122static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
Yann Colletc991cc12016-07-28 00:55:43 +0200123typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
Yann Collet37f3d1b2016-03-19 15:11:42 +0100124
125#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
126#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
127
128#define HufLog 12
Yann Colletf8e7b532016-07-23 16:31:49 +0200129typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e;
Yann Collet14983e72015-11-11 21:38:21 +0100130
Yann Collet37f3d1b2016-03-19 15:11:42 +0100131#define LONGNBSEQ 0x7F00
132
inikep7bc19b62016-04-06 09:46:01 +0200133#define MINMATCH 3
Yann Collet14983e72015-11-11 21:38:21 +0100134
inikep3bfcfc72016-02-03 18:47:30 +0100135#define Litbits 8
inikep70b05452016-02-03 22:56:55 +0100136#define MaxLit ((1<<Litbits) - 1)
Yann Collet95424402018-02-09 04:25:15 -0800137#define MaxML 52
138#define MaxLL 35
Nick Terrellbbe77212017-09-18 16:54:53 -0700139#define DefaultMaxOff 28
Yann Collet95424402018-02-09 04:25:15 -0800140#define MaxOff 31
Yann Collet4db09ef2016-03-18 22:23:49 +0100141#define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
Yann Colletbe391432016-03-22 23:19:28 +0100142#define MLFSELog 9
Yann Colletd64f4352016-03-21 00:07:42 +0100143#define LLFSELog 9
Yann Collet646693e2016-03-24 02:31:27 +0100144#define OffFSELog 8
Yann Collet95424402018-02-09 04:25:15 -0800145#define MaxFSELog MAX(MAX(MLFSELog, LLFSELog), OffFSELog)
inikepf3c65032016-03-04 20:04:25 +0100146
Yann Collet4191efa2017-11-08 11:05:32 -0800147static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
148 0, 0, 0, 0, 0, 0, 0, 0,
149 1, 1, 1, 1, 2, 2, 3, 3,
150 4, 6, 7, 8, 9,10,11,12,
Yann Colletb0aec172016-03-21 13:24:16 +0100151 13,14,15,16 };
Yann Collet4191efa2017-11-08 11:05:32 -0800152static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2,
153 2, 2, 2, 2, 2, 1, 1, 1,
154 2, 2, 2, 2, 2, 2, 2, 2,
155 2, 3, 2, 1, 1, 1, 1, 1,
Yann Collet48537162016-04-07 15:24:29 +0200156 -1,-1,-1,-1 };
Yann Collet51f4d562016-09-22 15:57:28 +0200157#define LL_DEFAULTNORMLOG 6 /* for static allocation */
158static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG;
Yann Colletb0aec172016-03-21 13:24:16 +0100159
Yann Collet4191efa2017-11-08 11:05:32 -0800160static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0,
161 0, 0, 0, 0, 0, 0, 0, 0,
162 0, 0, 0, 0, 0, 0, 0, 0,
163 0, 0, 0, 0, 0, 0, 0, 0,
164 1, 1, 1, 1, 2, 2, 3, 3,
165 4, 4, 5, 7, 8, 9,10,11,
Yann Collet48537162016-04-07 15:24:29 +0200166 12,13,14,15,16 };
Yann Collet4191efa2017-11-08 11:05:32 -0800167static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2,
168 2, 1, 1, 1, 1, 1, 1, 1,
169 1, 1, 1, 1, 1, 1, 1, 1,
170 1, 1, 1, 1, 1, 1, 1, 1,
171 1, 1, 1, 1, 1, 1, 1, 1,
172 1, 1, 1, 1, 1, 1,-1,-1,
Yann Collet48537162016-04-07 15:24:29 +0200173 -1,-1,-1,-1,-1 };
Yann Collet51f4d562016-09-22 15:57:28 +0200174#define ML_DEFAULTNORMLOG 6 /* for static allocation */
175static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG;
Yann Colletfadda6c2016-03-22 12:14:26 +0100176
Yann Collet4191efa2017-11-08 11:05:32 -0800177static const S16 OF_defaultNorm[DefaultMaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2,
178 2, 1, 1, 1, 1, 1, 1, 1,
179 1, 1, 1, 1, 1, 1, 1, 1,
180 -1,-1,-1,-1,-1 };
Yann Collet51f4d562016-09-22 15:57:28 +0200181#define OF_DEFAULTNORMLOG 5 /* for static allocation */
182static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG;
Yann Collet48537162016-04-07 15:24:29 +0200183
Yann Collet2acb5d32015-10-29 16:49:43 +0100184
Yann Colletfb810d62016-01-28 00:18:06 +0100185/*-*******************************************
Yann Collet14983e72015-11-11 21:38:21 +0100186* Shared functions to include for inlining
Yann Colletfb810d62016-01-28 00:18:06 +0100187*********************************************/
Yann Collet2acb5d32015-10-29 16:49:43 +0100188static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
Yann Collet2acb5d32015-10-29 16:49:43 +0100189#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
190
Yann Collet953ce722016-02-04 15:28:14 +0100191/*! ZSTD_wildcopy() :
Yann Colletcdade552017-11-17 11:40:08 -0800192 * custom version of memcpy(), can overwrite up to WILDCOPY_OVERLENGTH bytes (if length==0) */
Yann Collet37f3d1b2016-03-19 15:11:42 +0100193#define WILDCOPY_OVERLENGTH 8
Nick Terrell064a1432016-12-12 19:01:23 -0800194MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length)
Yann Collet2acb5d32015-10-29 16:49:43 +0100195{
196 const BYTE* ip = (const BYTE*)src;
197 BYTE* op = (BYTE*)dst;
198 BYTE* const oend = op + length;
Yann Collet50c5cdb2015-11-04 20:35:33 +0100199 do
200 COPY8(op, ip)
201 while (op < oend);
Yann Collet2acb5d32015-10-29 16:49:43 +0100202}
203
Yann Collet280f9a82016-08-08 00:44:00 +0200204MEM_STATIC void ZSTD_wildcopy_e(void* dst, const void* src, void* dstEnd) /* should be faster for decoding, but strangely, not verified on all platform */
205{
206 const BYTE* ip = (const BYTE*)src;
207 BYTE* op = (BYTE*)dst;
208 BYTE* const oend = (BYTE*)dstEnd;
209 do
210 COPY8(op, ip)
211 while (op < oend);
212}
213
Yann Collet7d360282016-02-12 00:07:30 +0100214
215/*-*******************************************
Yann Collet8b6aecf2017-11-07 15:27:06 -0800216* Private declarations
Yann Collet7d360282016-02-12 00:07:30 +0100217*********************************************/
Yann Colleted57d852016-07-29 21:22:17 +0200218typedef struct seqDef_s {
219 U32 offset;
220 U16 litLength;
221 U16 matchLength;
222} seqDef;
223
inikep87d4f3d2016-03-02 15:56:24 +0100224typedef struct {
Yann Colletc0ce4f12016-07-30 00:55:13 +0200225 seqDef* sequencesStart;
Yann Colleted57d852016-07-29 21:22:17 +0200226 seqDef* sequences;
Yann Collet7d360282016-02-12 00:07:30 +0100227 BYTE* litStart;
228 BYTE* lit;
Yann Colleted57d852016-07-29 21:22:17 +0200229 BYTE* llCode;
230 BYTE* mlCode;
231 BYTE* ofCode;
Yann Collet5d393572016-04-07 17:19:00 +0200232 U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
233 U32 longLengthPos;
Nick Terrell7a28b9e2017-07-17 15:29:11 -0700234} seqStore_t;
235
Yann Collet8b6aecf2017-11-07 15:27:06 -0800236const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); /* compress & dictBuilder */
237void ZSTD_seqToCodes(const seqStore_t* seqStorePtr); /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */
Yann Collet2acb5d32015-10-29 16:49:43 +0100238
inikepc4807f42016-06-02 15:11:39 +0200239/* custom memory allocation functions */
Yann Collet23b6e052016-08-28 21:05:43 -0700240void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
Yann Collet44e45e82017-05-30 16:12:06 -0700241void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
Yann Collet23b6e052016-08-28 21:05:43 -0700242void ZSTD_free(void* ptr, ZSTD_customMem customMem);
243
inikep2a746092016-06-03 14:53:51 +0200244
Yann Collet8b6aecf2017-11-07 15:27:06 -0800245MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
Yann Colletc154d9d2016-07-27 14:37:00 +0200246{
Stella Laue50ed1f2017-08-22 11:55:42 -0700247 assert(val != 0);
248 {
Yann Colletc154d9d2016-07-27 14:37:00 +0200249# if defined(_MSC_VER) /* Visual */
Stella Laue50ed1f2017-08-22 11:55:42 -0700250 unsigned long r=0;
251 _BitScanReverse(&r, val);
252 return (unsigned)r;
Yann Colletc154d9d2016-07-27 14:37:00 +0200253# elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */
Stella Laue50ed1f2017-08-22 11:55:42 -0700254 return 31 - __builtin_clz(val);
Yann Colletc154d9d2016-07-27 14:37:00 +0200255# else /* Software version */
Yann Collet0a0a2122017-11-28 14:07:03 -0800256 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 -0700257 U32 v = val;
Stella Laue50ed1f2017-08-22 11:55:42 -0700258 v |= v >> 1;
259 v |= v >> 2;
260 v |= v >> 4;
261 v |= v >> 8;
262 v |= v >> 16;
Yann Collet0a0a2122017-11-28 14:07:03 -0800263 return DeBruijnClz[(v * 0x07C4ACDDU) >> 27];
Yann Colletc154d9d2016-07-27 14:37:00 +0200264# endif
Stella Laue50ed1f2017-08-22 11:55:42 -0700265 }
Yann Colletc154d9d2016-07-27 14:37:00 +0200266}
267
268
Yann Collet32dfae62017-01-19 10:32:55 -0800269/* ZSTD_invalidateRepCodes() :
270 * ensures next compression will not use repcodes from previous block.
271 * Note : only works with regular variant;
272 * do not use with extDict variant ! */
Yann Collet8b6aecf2017-11-07 15:27:06 -0800273void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); /* zstdmt, adaptive_compression (shouldn't get this definition from here) */
Yann Collet32dfae62017-01-19 10:32:55 -0800274
275
Yann Colletf04deff2017-07-06 01:42:46 -0700276typedef struct {
277 blockType_e blockType;
278 U32 lastBlock;
279 U32 origSize;
280} blockProperties_t;
281
282/*! ZSTD_getcBlockSize() :
Yann Collet4191efa2017-11-08 11:05:32 -0800283 * Provides the size of compressed block from block header `src` */
284/* Used by: decompress, fullbench (does not get its definition from here) */
Yann Colletf04deff2017-07-06 01:42:46 -0700285size_t ZSTD_getcBlockSize(const void* src, size_t srcSize,
286 blockProperties_t* bpPtr);
287
Nick Terrellde6c6bc2017-08-24 18:09:50 -0700288#if defined (__cplusplus)
289}
290#endif
Yann Colletf04deff2017-07-06 01:42:46 -0700291
Yann Collet2acb5d32015-10-29 16:49:43 +0100292#endif /* ZSTD_CCOMMON_H_MODULE */