| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 1 | /* |
| Yann Collet | 71bcdb5 | 2015-10-29 17:08:03 +0100 | [diff] [blame] | 2 | zstd_internal - common functions to include |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 3 | 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) |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
| 40 | /* ************************************* |
| 41 | * Includes |
| 42 | ***************************************/ |
| 43 | #include "mem.h" |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 44 | #include "error_private.h" |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 45 | #include "zstd_static.h" |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 46 | |
| 47 | |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 48 | /* ************************************* |
| 49 | * Common macros |
| 50 | ***************************************/ |
| Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 51 | #define MIN(a,b) ((a)<(b) ? (a) : (b)) |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 52 | #define MAX(a,b) ((a)>(b) ? (a) : (b)) |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 53 | |
| 54 | |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 55 | /* ************************************* |
| 56 | * Common constants |
| 57 | ***************************************/ |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 58 | #define ZSTD_MAGICNUMBER 0xFD2FB525 /* v0.5 */ |
| 59 | #define ZSTD_DICT_MAGIC 0xEC30A435 |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 60 | |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 61 | #define KB *(1 <<10) |
| 62 | #define MB *(1 <<20) |
| 63 | #define GB *(1U<<30) |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 64 | |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 65 | #define BLOCKSIZE (128 KB) /* define, for static allocation */ |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 66 | |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 67 | static const size_t ZSTD_blockHeaderSize = 3; |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 68 | static const size_t ZSTD_frameHeaderSize_min = 5; |
| 69 | #define ZSTD_frameHeaderSize_max 5 /* define, for static allocation */ |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 70 | |
| 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 Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 78 | #define IS_HUF 0 |
| 79 | #define IS_PCH 1 |
| 80 | #define IS_RAW 2 |
| 81 | #define IS_RLE 3 |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 82 | |
| 83 | #define MINMATCH 4 |
| Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 84 | #define REPCODE_STARTVALUE 1 |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 85 | |
| 86 | #define MLbits 7 |
| 87 | #define LLbits 6 |
| 88 | #define Offbits 5 |
| 89 | #define MaxML ((1<<MLbits) - 1) |
| 90 | #define MaxLL ((1<<LLbits) - 1) |
| 91 | #define MaxOff ((1<<Offbits)- 1) |
| 92 | #define MLFSELog 10 |
| 93 | #define LLFSELog 10 |
| 94 | #define OffFSELog 9 |
| 95 | #define MaxSeq MAX(MaxLL, MaxML) |
| 96 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 97 | #define FSE_ENCODING_RAW 0 |
| 98 | #define FSE_ENCODING_RLE 1 |
| 99 | #define FSE_ENCODING_STATIC 2 |
| 100 | #define FSE_ENCODING_DYNAMIC 3 |
| 101 | |
| 102 | |
| Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 103 | #define HufLog 12 |
| 104 | |
| Yann Collet | b010b3b | 2016-02-03 12:39:34 +0100 | [diff] [blame] | 105 | #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */ |
| 106 | #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */ |
| Yann Collet | bc4c8aa | 2016-01-25 17:26:01 +0100 | [diff] [blame] | 107 | |
| 108 | #define WILDCOPY_OVERLENGTH 8 |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 109 | |
| 110 | typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t; |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 111 | |
| 112 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 113 | /*-******************************************* |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 114 | * Shared functions to include for inlining |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 115 | *********************************************/ |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 116 | static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); } |
| 117 | |
| 118 | #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; } |
| 119 | |
| Yann Collet | bc4c8aa | 2016-01-25 17:26:01 +0100 | [diff] [blame] | 120 | /*! ZSTD_wildcopy : custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */ |
| Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 121 | MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, size_t length) |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 122 | { |
| 123 | const BYTE* ip = (const BYTE*)src; |
| 124 | BYTE* op = (BYTE*)dst; |
| 125 | BYTE* const oend = op + length; |
| Yann Collet | 50c5cdb | 2015-11-04 20:35:33 +0100 | [diff] [blame] | 126 | do |
| 127 | COPY8(op, ip) |
| 128 | while (op < oend); |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 132 | #if defined (__cplusplus) |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | #endif /* ZSTD_CCOMMON_H_MODULE */ |