| Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
| Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 5 | * 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 Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
| Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 9 | */ |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 10 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 11 | #ifndef ZSTD_CCOMMON_H_MODULE |
| 12 | #define ZSTD_CCOMMON_H_MODULE |
| 13 | |
| Yann Collet | 8b6aecf | 2017-11-07 15:27:06 -0800 | [diff] [blame] | 14 | /* 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 Collet | 5c956d5 | 2016-09-06 15:05:19 +0200 | [diff] [blame] | 18 | |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 19 | /*-************************************* |
| Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 20 | * Dependencies |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 21 | ***************************************/ |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 22 | #include "compiler.h" |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 23 | #include "mem.h" |
| Yann Collet | fa41bcc | 2018-06-13 14:59:26 -0400 | [diff] [blame] | 24 | #include "debug.h" /* assert, DEBUGLOG, RAWLOG, g_debuglevel */ |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 25 | #include "error_private.h" |
| Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 26 | #define ZSTD_STATIC_LINKING_ONLY |
| 27 | #include "zstd.h" |
| Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 28 | #define FSE_STATIC_LINKING_ONLY |
| 29 | #include "fse.h" |
| 30 | #define HUF_STATIC_LINKING_ONLY |
| 31 | #include "huf.h" |
| Yann Collet | 4bcc69b | 2017-03-01 11:33:25 -0800 | [diff] [blame] | 32 | #ifndef XXH_STATIC_LINKING_ONLY |
| Yann Collet | 58e8d79 | 2017-06-02 18:20:48 -0700 | [diff] [blame] | 33 | # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */ |
| Yann Collet | 4bcc69b | 2017-03-01 11:33:25 -0800 | [diff] [blame] | 34 | #endif |
| Yann Collet | 58e8d79 | 2017-06-02 18:20:48 -0700 | [diff] [blame] | 35 | #include "xxhash.h" /* XXH_reset, update, digest */ |
| 36 | |
| Nick Terrell | de6c6bc | 2017-08-24 18:09:50 -0700 | [diff] [blame] | 37 | #if defined (__cplusplus) |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
| Yann Collet | fa41bcc | 2018-06-13 14:59:26 -0400 | [diff] [blame] | 41 | /* ---- static assert (debug) --- */ |
| 42 | #define ZSTD_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) |
| Yann Collet | ccd2d42 | 2018-10-23 17:25:49 -0700 | [diff] [blame] | 43 | #define ZSTD_isError ERR_isError /* for inlining */ |
| 44 | #define FSE_isError ERR_isError |
| 45 | #define HUF_isError ERR_isError |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 46 | |
| 47 | |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 48 | /*-************************************* |
| Yann Collet | 3e21ec5 | 2016-09-06 15:36:19 +0200 | [diff] [blame] | 49 | * shared macros |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 50 | ***************************************/ |
| Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 51 | #undef MIN |
| 52 | #undef MAX |
| Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 53 | #define MIN(a,b) ((a)<(b) ? (a) : (b)) |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 54 | #define MAX(a,b) ((a)>(b) ? (a) : (b)) |
| W. Felix Handte | 7ebd897 | 2019-01-28 17:16:32 -0500 | [diff] [blame] | 55 | |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 56 | /** |
| 57 | * Return the specified error if the condition evaluates to true. |
| 58 | * |
| Yann Collet | 0b0b83e | 2019-08-03 16:43:34 +0200 | [diff] [blame] | 59 | * In debug modes, prints additional information. |
| 60 | * In order to do that (particularly, printing the conditional that failed), |
| 61 | * this can't just wrap RETURN_ERROR(). |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 62 | */ |
| W. Felix Handte | 54fa31f | 2018-12-05 16:23:18 -0800 | [diff] [blame] | 63 | #define RETURN_ERROR_IF(cond, err, ...) \ |
| 64 | if (cond) { \ |
| W. Felix Handte | 324e965 | 2019-01-28 12:50:36 -0500 | [diff] [blame] | 65 | RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", __FILE__, __LINE__, ZSTD_QUOTE(cond), ZSTD_QUOTE(ERROR(err))); \ |
| W. Felix Handte | 54fa31f | 2018-12-05 16:23:18 -0800 | [diff] [blame] | 66 | RAWLOG(3, ": " __VA_ARGS__); \ |
| 67 | RAWLOG(3, "\n"); \ |
| 68 | return ERROR(err); \ |
| 69 | } |
| 70 | |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 71 | /** |
| 72 | * Unconditionally return the specified error. |
| 73 | * |
| 74 | * In debug modes, prints additional information. |
| 75 | */ |
| 76 | #define RETURN_ERROR(err, ...) \ |
| 77 | do { \ |
| W. Felix Handte | 324e965 | 2019-01-28 12:50:36 -0500 | [diff] [blame] | 78 | RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", __FILE__, __LINE__, ZSTD_QUOTE(ERROR(err))); \ |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 79 | RAWLOG(3, ": " __VA_ARGS__); \ |
| 80 | RAWLOG(3, "\n"); \ |
| 81 | return ERROR(err); \ |
| 82 | } while(0); |
| 83 | |
| 84 | /** |
| 85 | * If the provided expression evaluates to an error code, returns that error code. |
| 86 | * |
| 87 | * In debug modes, prints additional information. |
| 88 | */ |
| W. Felix Handte | 501eb25 | 2019-01-29 12:56:07 -0500 | [diff] [blame] | 89 | #define FORWARD_IF_ERROR(err, ...) \ |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 90 | do { \ |
| 91 | size_t const err_code = (err); \ |
| 92 | if (ERR_isError(err_code)) { \ |
| W. Felix Handte | 324e965 | 2019-01-28 12:50:36 -0500 | [diff] [blame] | 93 | RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", __FILE__, __LINE__, ZSTD_QUOTE(err), ERR_getErrorName(err_code)); \ |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 94 | RAWLOG(3, ": " __VA_ARGS__); \ |
| 95 | RAWLOG(3, "\n"); \ |
| 96 | return err_code; \ |
| 97 | } \ |
| 98 | } while(0); |
| 99 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 100 | |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 101 | /*-************************************* |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 102 | * Common constants |
| 103 | ***************************************/ |
| inikep | 87d4f3d | 2016-03-02 15:56:24 +0100 | [diff] [blame] | 104 | #define ZSTD_OPT_NUM (1<<12) |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 105 | |
| inikep | 5f49eba | 2016-08-10 15:01:53 +0200 | [diff] [blame] | 106 | #define ZSTD_REP_NUM 3 /* number of repcodes */ |
| inikep | 5f49eba | 2016-08-10 15:01:53 +0200 | [diff] [blame] | 107 | #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1) |
| Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 108 | static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 }; |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 109 | |
| 110 | #define KB *(1 <<10) |
| 111 | #define MB *(1 <<20) |
| 112 | #define GB *(1U<<30) |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 113 | |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 114 | #define BIT7 128 |
| 115 | #define BIT6 64 |
| 116 | #define BIT5 32 |
| 117 | #define BIT4 16 |
| 118 | #define BIT1 2 |
| 119 | #define BIT0 1 |
| 120 | |
| Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 121 | #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10 |
| 122 | static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 }; |
| Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 123 | static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 }; |
| Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 124 | |
| Yann Collet | 6e66bbf | 2018-08-14 12:56:21 -0700 | [diff] [blame] | 125 | #define ZSTD_FRAMEIDSIZE 4 /* magic number size */ |
| Yann Collet | b8d4a38 | 2017-09-25 15:25:07 -0700 | [diff] [blame] | 126 | |
| Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 127 | #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */ |
| Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 128 | static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE; |
| Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 129 | typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e; |
| Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 130 | |
| 131 | #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */ |
| 132 | #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */ |
| 133 | |
| 134 | #define HufLog 12 |
| Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 135 | typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e; |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 136 | |
| Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 137 | #define LONGNBSEQ 0x7F00 |
| 138 | |
| inikep | 7bc19b6 | 2016-04-06 09:46:01 +0200 | [diff] [blame] | 139 | #define MINMATCH 3 |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 140 | |
| inikep | 3bfcfc7 | 2016-02-03 18:47:30 +0100 | [diff] [blame] | 141 | #define Litbits 8 |
| inikep | 70b0545 | 2016-02-03 22:56:55 +0100 | [diff] [blame] | 142 | #define MaxLit ((1<<Litbits) - 1) |
| Yann Collet | 9542440 | 2018-02-09 04:25:15 -0800 | [diff] [blame] | 143 | #define MaxML 52 |
| 144 | #define MaxLL 35 |
| Nick Terrell | bbe7721 | 2017-09-18 16:54:53 -0700 | [diff] [blame] | 145 | #define DefaultMaxOff 28 |
| Yann Collet | 9542440 | 2018-02-09 04:25:15 -0800 | [diff] [blame] | 146 | #define MaxOff 31 |
| Yann Collet | 4db09ef | 2016-03-18 22:23:49 +0100 | [diff] [blame] | 147 | #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */ |
| Yann Collet | be39143 | 2016-03-22 23:19:28 +0100 | [diff] [blame] | 148 | #define MLFSELog 9 |
| Yann Collet | d64f435 | 2016-03-21 00:07:42 +0100 | [diff] [blame] | 149 | #define LLFSELog 9 |
| Yann Collet | 646693e | 2016-03-24 02:31:27 +0100 | [diff] [blame] | 150 | #define OffFSELog 8 |
| Yann Collet | 9542440 | 2018-02-09 04:25:15 -0800 | [diff] [blame] | 151 | #define MaxFSELog MAX(MAX(MLFSELog, LLFSELog), OffFSELog) |
| inikep | f3c6503 | 2016-03-04 20:04:25 +0100 | [diff] [blame] | 152 | |
| Yann Collet | 4191efa | 2017-11-08 11:05:32 -0800 | [diff] [blame] | 153 | static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, |
| 154 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 155 | 1, 1, 1, 1, 2, 2, 3, 3, |
| 156 | 4, 6, 7, 8, 9,10,11,12, |
| Yann Collet | b0aec17 | 2016-03-21 13:24:16 +0100 | [diff] [blame] | 157 | 13,14,15,16 }; |
| Yann Collet | 4191efa | 2017-11-08 11:05:32 -0800 | [diff] [blame] | 158 | static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, |
| 159 | 2, 2, 2, 2, 2, 1, 1, 1, |
| 160 | 2, 2, 2, 2, 2, 2, 2, 2, |
| 161 | 2, 3, 2, 1, 1, 1, 1, 1, |
| Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 162 | -1,-1,-1,-1 }; |
| Yann Collet | 51f4d56 | 2016-09-22 15:57:28 +0200 | [diff] [blame] | 163 | #define LL_DEFAULTNORMLOG 6 /* for static allocation */ |
| 164 | static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG; |
| Yann Collet | b0aec17 | 2016-03-21 13:24:16 +0100 | [diff] [blame] | 165 | |
| Yann Collet | 4191efa | 2017-11-08 11:05:32 -0800 | [diff] [blame] | 166 | static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, |
| 167 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 169 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 170 | 1, 1, 1, 1, 2, 2, 3, 3, |
| 171 | 4, 4, 5, 7, 8, 9,10,11, |
| Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 172 | 12,13,14,15,16 }; |
| Yann Collet | 4191efa | 2017-11-08 11:05:32 -0800 | [diff] [blame] | 173 | static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, |
| 174 | 2, 1, 1, 1, 1, 1, 1, 1, |
| 175 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 176 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 177 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 178 | 1, 1, 1, 1, 1, 1,-1,-1, |
| Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 179 | -1,-1,-1,-1,-1 }; |
| Yann Collet | 51f4d56 | 2016-09-22 15:57:28 +0200 | [diff] [blame] | 180 | #define ML_DEFAULTNORMLOG 6 /* for static allocation */ |
| 181 | static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG; |
| Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 182 | |
| Yann Collet | 4191efa | 2017-11-08 11:05:32 -0800 | [diff] [blame] | 183 | static const S16 OF_defaultNorm[DefaultMaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, |
| 184 | 2, 1, 1, 1, 1, 1, 1, 1, |
| 185 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 186 | -1,-1,-1,-1,-1 }; |
| Yann Collet | 51f4d56 | 2016-09-22 15:57:28 +0200 | [diff] [blame] | 187 | #define OF_DEFAULTNORMLOG 5 /* for static allocation */ |
| 188 | static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG; |
| Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 189 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 190 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 191 | /*-******************************************* |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 192 | * Shared functions to include for inlining |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 193 | *********************************************/ |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 194 | static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); } |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 195 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 196 | #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; } |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 197 | static void ZSTD_copy16(void* dst, const void* src) { memcpy(dst, src, 16); } |
| 198 | #define COPY16(d,s) { ZSTD_copy16(d,s); d+=16; s+=16; } |
| 199 | |
| Nick Terrell | cdad7fa | 2019-09-20 00:52:15 -0700 | [diff] [blame] | 200 | #define WILDCOPY_OVERLENGTH 32 |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 201 | #define WILDCOPY_VECLEN 16 |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 202 | |
| 203 | typedef enum { |
| 204 | ZSTD_no_overlap, |
| 205 | ZSTD_overlap_src_before_dst, |
| 206 | /* ZSTD_overlap_dst_before_src, */ |
| 207 | } ZSTD_overlap_e; |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 208 | |
| Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 209 | /*! ZSTD_wildcopy() : |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 210 | * Custom version of memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0) |
| 211 | * @param ovtype controls the overlap detection |
| 212 | * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart. |
| 213 | * - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart. |
| 214 | * The src buffer must be before the dst buffer. |
| 215 | */ |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 216 | MEM_STATIC FORCE_INLINE_ATTR DONT_VECTORIZE |
| Nick Terrell | 44c65da | 2019-09-20 12:23:25 -0700 | [diff] [blame] | 217 | void ZSTD_wildcopy(void* dst, const void* src, ptrdiff_t length, ZSTD_overlap_e const ovtype) |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 218 | { |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 219 | ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src; |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 220 | const BYTE* ip = (const BYTE*)src; |
| 221 | BYTE* op = (BYTE*)dst; |
| 222 | BYTE* const oend = op + length; |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 223 | |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 224 | assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff <= -WILDCOPY_VECLEN)); |
| mgrice | b830599 | 2019-08-27 14:49:23 -0700 | [diff] [blame] | 225 | |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 226 | if (ovtype == ZSTD_overlap_src_before_dst && diff < WILDCOPY_VECLEN) { |
| 227 | /* Handle short offset copies. */ |
| mgrice | b830599 | 2019-08-27 14:49:23 -0700 | [diff] [blame] | 228 | do { |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 229 | COPY8(op, ip) |
| 230 | } while (op < oend); |
| 231 | } else { |
| 232 | assert(diff >= WILDCOPY_VECLEN || diff <= -WILDCOPY_VECLEN); |
| 233 | /* Separate out the first two COPY16() calls because the copy length is |
| 234 | * almost certain to be short, so the branches have different |
| 235 | * probabilities. |
| 236 | * On gcc-9 unrolling once is +1.6%, twice is +2%, thrice is +1.8%. |
| 237 | * On clang-8 unrolling once is +1.4%, twice is +3.3%, thrice is +3%. |
| 238 | */ |
| 239 | COPY16(op, ip); |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 240 | COPY16(op, ip); |
| 241 | if (op >= oend) return; |
| 242 | do { |
| 243 | COPY16(op, ip); |
| Nick Terrell | cdad7fa | 2019-09-20 00:52:15 -0700 | [diff] [blame] | 244 | COPY16(op, ip); |
| mgrice | b830599 | 2019-08-27 14:49:23 -0700 | [diff] [blame] | 245 | } |
| 246 | while (op < oend); |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 250 | /*! ZSTD_wildcopy8() : |
| 251 | * The same as ZSTD_wildcopy(), but it can only overwrite 8 bytes, and works for |
| 252 | * overlapping buffers that are at least 8 bytes apart. |
| 253 | */ |
| 254 | MEM_STATIC void ZSTD_wildcopy8(void* dst, const void* src, ptrdiff_t length) |
| Yann Collet | 280f9a8 | 2016-08-08 00:44:00 +0200 | [diff] [blame] | 255 | { |
| 256 | const BYTE* ip = (const BYTE*)src; |
| 257 | BYTE* op = (BYTE*)dst; |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 258 | BYTE* const oend = (BYTE*)op + length; |
| 259 | do { |
| Nick Terrell | cdad7fa | 2019-09-20 00:52:15 -0700 | [diff] [blame] | 260 | COPY8(op, ip); |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 261 | } while (op < oend); |
| Yann Collet | 280f9a8 | 2016-08-08 00:44:00 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 264 | |
| 265 | /*-******************************************* |
| Yann Collet | 8b6aecf | 2017-11-07 15:27:06 -0800 | [diff] [blame] | 266 | * Private declarations |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 267 | *********************************************/ |
| Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 268 | typedef struct seqDef_s { |
| 269 | U32 offset; |
| 270 | U16 litLength; |
| 271 | U16 matchLength; |
| 272 | } seqDef; |
| 273 | |
| inikep | 87d4f3d | 2016-03-02 15:56:24 +0100 | [diff] [blame] | 274 | typedef struct { |
| Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 275 | seqDef* sequencesStart; |
| Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 276 | seqDef* sequences; |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 277 | BYTE* litStart; |
| 278 | BYTE* lit; |
| Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 279 | BYTE* llCode; |
| 280 | BYTE* mlCode; |
| 281 | BYTE* ofCode; |
| Nick Terrell | 924944e | 2018-08-21 14:20:02 -0700 | [diff] [blame] | 282 | size_t maxNbSeq; |
| Nick Terrell | 5e580de | 2018-08-28 13:24:44 -0700 | [diff] [blame] | 283 | size_t maxNbLit; |
| Yann Collet | 5d39357 | 2016-04-07 17:19:00 +0200 | [diff] [blame] | 284 | U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */ |
| 285 | U32 longLengthPos; |
| Nick Terrell | 7a28b9e | 2017-07-17 15:29:11 -0700 | [diff] [blame] | 286 | } seqStore_t; |
| 287 | |
| shakeelrao | 19b75b6 | 2019-03-15 18:04:19 -0700 | [diff] [blame] | 288 | /** |
| 289 | * Contains the compressed frame size and an upper-bound for the decompressed frame size. |
| shakeelrao | 0033bb4 | 2019-03-17 17:41:27 -0700 | [diff] [blame] | 290 | * Note: before using `compressedSize`, check for errors using ZSTD_isError(). |
| 291 | * similarly, before using `decompressedBound`, check for errors using: |
| 292 | * `decompressedBound != ZSTD_CONTENTSIZE_ERROR` |
| shakeelrao | 19b75b6 | 2019-03-15 18:04:19 -0700 | [diff] [blame] | 293 | */ |
| 294 | typedef struct { |
| 295 | size_t compressedSize; |
| 296 | unsigned long long decompressedBound; |
| shakeelrao | 0033bb4 | 2019-03-17 17:41:27 -0700 | [diff] [blame] | 297 | } ZSTD_frameSizeInfo; /* decompress & legacy */ |
| shakeelrao | 19b75b6 | 2019-03-15 18:04:19 -0700 | [diff] [blame] | 298 | |
| Yann Collet | 8b6aecf | 2017-11-07 15:27:06 -0800 | [diff] [blame] | 299 | const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); /* compress & dictBuilder */ |
| 300 | void ZSTD_seqToCodes(const seqStore_t* seqStorePtr); /* compress, dictBuilder, decodeCorpus (shouldn't get its definition from here) */ |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 301 | |
| inikep | c4807f4 | 2016-06-02 15:11:39 +0200 | [diff] [blame] | 302 | /* custom memory allocation functions */ |
| Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 303 | void* ZSTD_malloc(size_t size, ZSTD_customMem customMem); |
| Yann Collet | 44e45e8 | 2017-05-30 16:12:06 -0700 | [diff] [blame] | 304 | void* ZSTD_calloc(size_t size, ZSTD_customMem customMem); |
| Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 305 | void ZSTD_free(void* ptr, ZSTD_customMem customMem); |
| 306 | |
| inikep | 2a74609 | 2016-06-03 14:53:51 +0200 | [diff] [blame] | 307 | |
| Yann Collet | 8b6aecf | 2017-11-07 15:27:06 -0800 | [diff] [blame] | 308 | MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */ |
| Yann Collet | c154d9d | 2016-07-27 14:37:00 +0200 | [diff] [blame] | 309 | { |
| Stella Lau | e50ed1f | 2017-08-22 11:55:42 -0700 | [diff] [blame] | 310 | assert(val != 0); |
| 311 | { |
| Yann Collet | c154d9d | 2016-07-27 14:37:00 +0200 | [diff] [blame] | 312 | # if defined(_MSC_VER) /* Visual */ |
| Stella Lau | e50ed1f | 2017-08-22 11:55:42 -0700 | [diff] [blame] | 313 | unsigned long r=0; |
| 314 | _BitScanReverse(&r, val); |
| 315 | return (unsigned)r; |
| Yann Collet | c154d9d | 2016-07-27 14:37:00 +0200 | [diff] [blame] | 316 | # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */ |
| Dávid Bolvanský | 1ab1a40 | 2019-09-23 21:32:56 +0200 | [diff] [blame^] | 317 | return __builtin_clz (val) ^ 31; |
| Joseph Chen | 3855bc4 | 2019-07-29 15:20:37 +0800 | [diff] [blame] | 318 | # elif defined(__ICCARM__) /* IAR Intrinsic */ |
| 319 | return 31 - __CLZ(val); |
| Yann Collet | c154d9d | 2016-07-27 14:37:00 +0200 | [diff] [blame] | 320 | # else /* Software version */ |
| Yann Collet | 0a0a212 | 2017-11-28 14:07:03 -0800 | [diff] [blame] | 321 | 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 Lau | e50ed1f | 2017-08-22 11:55:42 -0700 | [diff] [blame] | 322 | U32 v = val; |
| Stella Lau | e50ed1f | 2017-08-22 11:55:42 -0700 | [diff] [blame] | 323 | v |= v >> 1; |
| 324 | v |= v >> 2; |
| 325 | v |= v >> 4; |
| 326 | v |= v >> 8; |
| 327 | v |= v >> 16; |
| Yann Collet | 0a0a212 | 2017-11-28 14:07:03 -0800 | [diff] [blame] | 328 | return DeBruijnClz[(v * 0x07C4ACDDU) >> 27]; |
| Yann Collet | c154d9d | 2016-07-27 14:37:00 +0200 | [diff] [blame] | 329 | # endif |
| Stella Lau | e50ed1f | 2017-08-22 11:55:42 -0700 | [diff] [blame] | 330 | } |
| Yann Collet | c154d9d | 2016-07-27 14:37:00 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | |
| Yann Collet | 32dfae6 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 334 | /* ZSTD_invalidateRepCodes() : |
| 335 | * ensures next compression will not use repcodes from previous block. |
| 336 | * Note : only works with regular variant; |
| 337 | * do not use with extDict variant ! */ |
| Yann Collet | 8b6aecf | 2017-11-07 15:27:06 -0800 | [diff] [blame] | 338 | void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx); /* zstdmt, adaptive_compression (shouldn't get this definition from here) */ |
| Yann Collet | 32dfae6 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 339 | |
| 340 | |
| Yann Collet | f04deff | 2017-07-06 01:42:46 -0700 | [diff] [blame] | 341 | typedef struct { |
| 342 | blockType_e blockType; |
| 343 | U32 lastBlock; |
| 344 | U32 origSize; |
| Yann Collet | 2b49140 | 2018-10-25 16:28:41 -0700 | [diff] [blame] | 345 | } blockProperties_t; /* declared here for decompress and fullbench */ |
| Yann Collet | f04deff | 2017-07-06 01:42:46 -0700 | [diff] [blame] | 346 | |
| 347 | /*! ZSTD_getcBlockSize() : |
| Yann Collet | 4191efa | 2017-11-08 11:05:32 -0800 | [diff] [blame] | 348 | * Provides the size of compressed block from block header `src` */ |
| 349 | /* Used by: decompress, fullbench (does not get its definition from here) */ |
| Yann Collet | f04deff | 2017-07-06 01:42:46 -0700 | [diff] [blame] | 350 | size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, |
| 351 | blockProperties_t* bpPtr); |
| 352 | |
| Yann Collet | 2b49140 | 2018-10-25 16:28:41 -0700 | [diff] [blame] | 353 | /*! ZSTD_decodeSeqHeaders() : |
| 354 | * decode sequence header from src */ |
| 355 | /* Used by: decompress, fullbench (does not get its definition from here) */ |
| 356 | size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr, |
| 357 | const void* src, size_t srcSize); |
| 358 | |
| 359 | |
| Nick Terrell | de6c6bc | 2017-08-24 18:09:50 -0700 | [diff] [blame] | 360 | #if defined (__cplusplus) |
| 361 | } |
| 362 | #endif |
| Yann Collet | f04deff | 2017-07-06 01:42:46 -0700 | [diff] [blame] | 363 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 364 | #endif /* ZSTD_CCOMMON_H_MODULE */ |