blob: f2af062f747dbef3b288304586166693f7d43249 [file] [log] [blame]
Yann Collet2acb5d32015-10-29 16:49:43 +01001/*
Yann Collet71bcdb52015-10-29 17:08:03 +01002 zstd_internal - common functions to include
Yann Collet2acb5d32015-10-29 16:49:43 +01003 Header File for include
4 Copyright (C) 2014-2015, 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 source repository : https://github.com/Cyan4973/zstd
31 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
32*/
33#ifndef ZSTD_CCOMMON_H_MODULE
34#define ZSTD_CCOMMON_H_MODULE
35
36#if defined (__cplusplus)
37extern "C" {
38#endif
39
40/* *************************************
41* Includes
42***************************************/
43#include "mem.h"
Yann Collet977f1f32016-01-21 15:38:47 +010044#include "error_private.h"
Yann Collet59d1f792016-01-23 19:28:41 +010045#include "zstd_static.h"
Yann Collet2acb5d32015-10-29 16:49:43 +010046
47
Yann Collet14983e72015-11-11 21:38:21 +010048/* *************************************
49* Common macros
50***************************************/
Yann Colletbe2010e2015-10-31 12:57:14 +010051#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet14983e72015-11-11 21:38:21 +010052#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet2acb5d32015-10-29 16:49:43 +010053
54
Yann Collet14983e72015-11-11 21:38:21 +010055/* *************************************
56* Common constants
57***************************************/
Yann Colletb923f652016-01-26 03:14:20 +010058#define ZSTD_MAGICNUMBER 0xFD2FB525 /* v0.5 */
59#define ZSTD_DICT_MAGIC 0xEC30A435
Yann Collet88fcd292015-11-25 14:42:45 +010060
Yann Collet14983e72015-11-11 21:38:21 +010061#define KB *(1 <<10)
62#define MB *(1 <<20)
63#define GB *(1U<<30)
Yann Collet2acb5d32015-10-29 16:49:43 +010064
Yann Collet14983e72015-11-11 21:38:21 +010065#define BLOCKSIZE (128 KB) /* define, for static allocation */
Yann Collet2acb5d32015-10-29 16:49:43 +010066
Yann Collet14983e72015-11-11 21:38:21 +010067static const size_t ZSTD_blockHeaderSize = 3;
Yann Collet88fcd292015-11-25 14:42:45 +010068static const size_t ZSTD_frameHeaderSize_min = 5;
69#define ZSTD_frameHeaderSize_max 5 /* define, for static allocation */
Yann Collet14983e72015-11-11 21:38:21 +010070
71#define BIT7 128
72#define BIT6 64
73#define BIT5 32
74#define BIT4 16
75#define BIT1 2
76#define BIT0 1
77
Yann Collet59d1f792016-01-23 19:28:41 +010078#define IS_HUF 0
79#define IS_PCH 1
80#define IS_RAW 2
81#define IS_RLE 3
Yann Collet14983e72015-11-11 21:38:21 +010082
83#define MINMATCH 4
Yann Collet61e16ce2016-01-31 02:04:15 +010084#define REPCODE_STARTVALUE 1
Yann Collet14983e72015-11-11 21:38:21 +010085
inikep3bfcfc72016-02-03 18:47:30 +010086#define Litbits 8
Yann Collet14983e72015-11-11 21:38:21 +010087#define MLbits 7
88#define LLbits 6
89#define Offbits 5
90#define MaxML ((1<<MLbits) - 1)
91#define MaxLL ((1<<LLbits) - 1)
92#define MaxOff ((1<<Offbits)- 1)
93#define MLFSELog 10
94#define LLFSELog 10
95#define OffFSELog 9
96#define MaxSeq MAX(MaxLL, MaxML)
97
Yann Colletfb810d62016-01-28 00:18:06 +010098#define FSE_ENCODING_RAW 0
99#define FSE_ENCODING_RLE 1
100#define FSE_ENCODING_STATIC 2
101#define FSE_ENCODING_DYNAMIC 3
102
103
Yann Colletb923f652016-01-26 03:14:20 +0100104#define HufLog 12
105
Yann Colletb010b3b2016-02-03 12:39:34 +0100106#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
107#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
Yann Colletbc4c8aa2016-01-25 17:26:01 +0100108
109#define WILDCOPY_OVERLENGTH 8
Yann Collet14983e72015-11-11 21:38:21 +0100110
111typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
Yann Collet2acb5d32015-10-29 16:49:43 +0100112
113
Yann Colletfb810d62016-01-28 00:18:06 +0100114/*-*******************************************
Yann Collet14983e72015-11-11 21:38:21 +0100115* Shared functions to include for inlining
Yann Colletfb810d62016-01-28 00:18:06 +0100116*********************************************/
Yann Collet2acb5d32015-10-29 16:49:43 +0100117static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
118
119#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
120
Yann Colletbc4c8aa2016-01-25 17:26:01 +0100121/*! ZSTD_wildcopy : custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
Yann Collet59d1f792016-01-23 19:28:41 +0100122MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, size_t length)
Yann Collet2acb5d32015-10-29 16:49:43 +0100123{
124 const BYTE* ip = (const BYTE*)src;
125 BYTE* op = (BYTE*)dst;
126 BYTE* const oend = op + length;
Yann Collet50c5cdb2015-11-04 20:35:33 +0100127 do
128 COPY8(op, ip)
129 while (op < oend);
Yann Collet2acb5d32015-10-29 16:49:43 +0100130}
131
132
Yann Collet2acb5d32015-10-29 16:49:43 +0100133#if defined (__cplusplus)
134}
135#endif
136
137#endif /* ZSTD_CCOMMON_H_MODULE */