| Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
| Nick Terrell | ac58c8d | 2020-03-26 15:19:05 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. |
| Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 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 |
| W. Felix Handte | 6028827 | 2020-05-01 16:07:57 -0400 | [diff] [blame^] | 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 | baa4e2e | 2020-04-30 17:07:49 -0400 | [diff] [blame] | 56 | /** |
| 57 | * Ignore: this is an internal helper. |
| 58 | * |
| 59 | * This is a helper function to help force C99-correctness during compilation. |
| 60 | * Under strict compilation modes, variadic macro arguments can't be empty. |
| 61 | * However, variadic function arguments can be. Using a function therefore lets |
| 62 | * us statically check that at least one (string) argument was passed, |
| 63 | * independent of the compilation flags. |
| 64 | */ |
| W. Felix Handte | 3764859 | 2020-04-30 17:43:20 -0400 | [diff] [blame] | 65 | static INLINE_KEYWORD UNUSED_ATTR |
| 66 | void _force_has_format_string(const char *format, ...) { |
| W. Felix Handte | 450542d | 2020-04-30 13:09:14 -0400 | [diff] [blame] | 67 | (void)format; |
| 68 | } |
| 69 | |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 70 | /** |
| W. Felix Handte | baa4e2e | 2020-04-30 17:07:49 -0400 | [diff] [blame] | 71 | * Ignore: this is an internal helper. |
| 72 | * |
| 73 | * We want to force this function invocation to be syntactically correct, but |
| 74 | * we don't want to force runtime evaluation of its arguments. |
| 75 | */ |
| 76 | #define _FORCE_HAS_FORMAT_STRING(...) \ |
| 77 | if (0) { \ |
| 78 | _force_has_format_string(__VA_ARGS__); \ |
| 79 | } |
| 80 | |
| 81 | /** |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 82 | * Return the specified error if the condition evaluates to true. |
| 83 | * |
| Yann Collet | 0b0b83e | 2019-08-03 16:43:34 +0200 | [diff] [blame] | 84 | * In debug modes, prints additional information. |
| 85 | * In order to do that (particularly, printing the conditional that failed), |
| 86 | * this can't just wrap RETURN_ERROR(). |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 87 | */ |
| W. Felix Handte | 54fa31f | 2018-12-05 16:23:18 -0800 | [diff] [blame] | 88 | #define RETURN_ERROR_IF(cond, err, ...) \ |
| 89 | if (cond) { \ |
| W. Felix Handte | 450542d | 2020-04-30 13:09:14 -0400 | [diff] [blame] | 90 | RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \ |
| 91 | __FILE__, __LINE__, ZSTD_QUOTE(cond), ZSTD_QUOTE(ERROR(err))); \ |
| W. Felix Handte | baa4e2e | 2020-04-30 17:07:49 -0400 | [diff] [blame] | 92 | _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \ |
| W. Felix Handte | 450542d | 2020-04-30 13:09:14 -0400 | [diff] [blame] | 93 | RAWLOG(3, ": " __VA_ARGS__); \ |
| W. Felix Handte | 54fa31f | 2018-12-05 16:23:18 -0800 | [diff] [blame] | 94 | RAWLOG(3, "\n"); \ |
| 95 | return ERROR(err); \ |
| 96 | } |
| 97 | |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 98 | /** |
| 99 | * Unconditionally return the specified error. |
| 100 | * |
| 101 | * In debug modes, prints additional information. |
| 102 | */ |
| 103 | #define RETURN_ERROR(err, ...) \ |
| 104 | do { \ |
| W. Felix Handte | 450542d | 2020-04-30 13:09:14 -0400 | [diff] [blame] | 105 | RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \ |
| 106 | __FILE__, __LINE__, ZSTD_QUOTE(ERROR(err))); \ |
| W. Felix Handte | baa4e2e | 2020-04-30 17:07:49 -0400 | [diff] [blame] | 107 | _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \ |
| W. Felix Handte | 450542d | 2020-04-30 13:09:14 -0400 | [diff] [blame] | 108 | RAWLOG(3, ": " __VA_ARGS__); \ |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 109 | RAWLOG(3, "\n"); \ |
| 110 | return ERROR(err); \ |
| 111 | } while(0); |
| 112 | |
| 113 | /** |
| 114 | * If the provided expression evaluates to an error code, returns that error code. |
| 115 | * |
| 116 | * In debug modes, prints additional information. |
| 117 | */ |
| W. Felix Handte | 501eb25 | 2019-01-29 12:56:07 -0500 | [diff] [blame] | 118 | #define FORWARD_IF_ERROR(err, ...) \ |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 119 | do { \ |
| 120 | size_t const err_code = (err); \ |
| 121 | if (ERR_isError(err_code)) { \ |
| W. Felix Handte | 450542d | 2020-04-30 13:09:14 -0400 | [diff] [blame] | 122 | RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \ |
| 123 | __FILE__, __LINE__, ZSTD_QUOTE(err), ERR_getErrorName(err_code)); \ |
| W. Felix Handte | baa4e2e | 2020-04-30 17:07:49 -0400 | [diff] [blame] | 124 | _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \ |
| W. Felix Handte | 450542d | 2020-04-30 13:09:14 -0400 | [diff] [blame] | 125 | RAWLOG(3, ": " __VA_ARGS__); \ |
| W. Felix Handte | a3538bb | 2019-01-28 12:34:08 -0500 | [diff] [blame] | 126 | RAWLOG(3, "\n"); \ |
| 127 | return err_code; \ |
| 128 | } \ |
| 129 | } while(0); |
| 130 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 131 | |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 132 | /*-************************************* |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 133 | * Common constants |
| 134 | ***************************************/ |
| inikep | 87d4f3d | 2016-03-02 15:56:24 +0100 | [diff] [blame] | 135 | #define ZSTD_OPT_NUM (1<<12) |
| Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 136 | |
| inikep | 5f49eba | 2016-08-10 15:01:53 +0200 | [diff] [blame] | 137 | #define ZSTD_REP_NUM 3 /* number of repcodes */ |
| inikep | 5f49eba | 2016-08-10 15:01:53 +0200 | [diff] [blame] | 138 | #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1) |
| Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 139 | static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 }; |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 140 | |
| 141 | #define KB *(1 <<10) |
| 142 | #define MB *(1 <<20) |
| 143 | #define GB *(1U<<30) |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 144 | |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 145 | #define BIT7 128 |
| 146 | #define BIT6 64 |
| 147 | #define BIT5 32 |
| 148 | #define BIT4 16 |
| 149 | #define BIT1 2 |
| 150 | #define BIT0 1 |
| 151 | |
| Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 152 | #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10 |
| 153 | static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 }; |
| Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 154 | static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 }; |
| Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 155 | |
| Yann Collet | 6e66bbf | 2018-08-14 12:56:21 -0700 | [diff] [blame] | 156 | #define ZSTD_FRAMEIDSIZE 4 /* magic number size */ |
| Yann Collet | b8d4a38 | 2017-09-25 15:25:07 -0700 | [diff] [blame] | 157 | |
| Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 158 | #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] | 159 | static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE; |
| Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 160 | typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e; |
| Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 161 | |
| Bimba Shrestha | b1f53b1 | 2020-01-03 16:53:51 -0800 | [diff] [blame] | 162 | #define ZSTD_FRAMECHECKSUMSIZE 4 |
| 163 | |
| Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 164 | #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */ |
| 165 | #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */ |
| 166 | |
| 167 | #define HufLog 12 |
| Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 168 | typedef enum { set_basic, set_rle, set_compressed, set_repeat } symbolEncodingType_e; |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 169 | |
| Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 170 | #define LONGNBSEQ 0x7F00 |
| 171 | |
| inikep | 7bc19b6 | 2016-04-06 09:46:01 +0200 | [diff] [blame] | 172 | #define MINMATCH 3 |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 173 | |
| inikep | 3bfcfc7 | 2016-02-03 18:47:30 +0100 | [diff] [blame] | 174 | #define Litbits 8 |
| inikep | 70b0545 | 2016-02-03 22:56:55 +0100 | [diff] [blame] | 175 | #define MaxLit ((1<<Litbits) - 1) |
| Yann Collet | 9542440 | 2018-02-09 04:25:15 -0800 | [diff] [blame] | 176 | #define MaxML 52 |
| 177 | #define MaxLL 35 |
| Nick Terrell | bbe7721 | 2017-09-18 16:54:53 -0700 | [diff] [blame] | 178 | #define DefaultMaxOff 28 |
| Yann Collet | 9542440 | 2018-02-09 04:25:15 -0800 | [diff] [blame] | 179 | #define MaxOff 31 |
| Yann Collet | 4db09ef | 2016-03-18 22:23:49 +0100 | [diff] [blame] | 180 | #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */ |
| Yann Collet | be39143 | 2016-03-22 23:19:28 +0100 | [diff] [blame] | 181 | #define MLFSELog 9 |
| Yann Collet | d64f435 | 2016-03-21 00:07:42 +0100 | [diff] [blame] | 182 | #define LLFSELog 9 |
| Yann Collet | 646693e | 2016-03-24 02:31:27 +0100 | [diff] [blame] | 183 | #define OffFSELog 8 |
| Yann Collet | 9542440 | 2018-02-09 04:25:15 -0800 | [diff] [blame] | 184 | #define MaxFSELog MAX(MAX(MLFSELog, LLFSELog), OffFSELog) |
| inikep | f3c6503 | 2016-03-04 20:04:25 +0100 | [diff] [blame] | 185 | |
| Yann Collet | 4191efa | 2017-11-08 11:05:32 -0800 | [diff] [blame] | 186 | static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, |
| 187 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 188 | 1, 1, 1, 1, 2, 2, 3, 3, |
| 189 | 4, 6, 7, 8, 9,10,11,12, |
| Yann Collet | b0aec17 | 2016-03-21 13:24:16 +0100 | [diff] [blame] | 190 | 13,14,15,16 }; |
| Yann Collet | 4191efa | 2017-11-08 11:05:32 -0800 | [diff] [blame] | 191 | static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, |
| 192 | 2, 2, 2, 2, 2, 1, 1, 1, |
| 193 | 2, 2, 2, 2, 2, 2, 2, 2, |
| 194 | 2, 3, 2, 1, 1, 1, 1, 1, |
| Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 195 | -1,-1,-1,-1 }; |
| Yann Collet | 51f4d56 | 2016-09-22 15:57:28 +0200 | [diff] [blame] | 196 | #define LL_DEFAULTNORMLOG 6 /* for static allocation */ |
| 197 | static const U32 LL_defaultNormLog = LL_DEFAULTNORMLOG; |
| Yann Collet | b0aec17 | 2016-03-21 13:24:16 +0100 | [diff] [blame] | 198 | |
| Yann Collet | 4191efa | 2017-11-08 11:05:32 -0800 | [diff] [blame] | 199 | static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, |
| 200 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 201 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 202 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 203 | 1, 1, 1, 1, 2, 2, 3, 3, |
| 204 | 4, 4, 5, 7, 8, 9,10,11, |
| Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 205 | 12,13,14,15,16 }; |
| Yann Collet | 4191efa | 2017-11-08 11:05:32 -0800 | [diff] [blame] | 206 | static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, |
| 207 | 2, 1, 1, 1, 1, 1, 1, 1, |
| 208 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 209 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 210 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 211 | 1, 1, 1, 1, 1, 1,-1,-1, |
| Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 212 | -1,-1,-1,-1,-1 }; |
| Yann Collet | 51f4d56 | 2016-09-22 15:57:28 +0200 | [diff] [blame] | 213 | #define ML_DEFAULTNORMLOG 6 /* for static allocation */ |
| 214 | static const U32 ML_defaultNormLog = ML_DEFAULTNORMLOG; |
| Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 215 | |
| Yann Collet | 4191efa | 2017-11-08 11:05:32 -0800 | [diff] [blame] | 216 | static const S16 OF_defaultNorm[DefaultMaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, |
| 217 | 2, 1, 1, 1, 1, 1, 1, 1, |
| 218 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 219 | -1,-1,-1,-1,-1 }; |
| Yann Collet | 51f4d56 | 2016-09-22 15:57:28 +0200 | [diff] [blame] | 220 | #define OF_DEFAULTNORMLOG 5 /* for static allocation */ |
| 221 | static const U32 OF_defaultNormLog = OF_DEFAULTNORMLOG; |
| Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 222 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 223 | |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 224 | /*-******************************************* |
| Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 225 | * Shared functions to include for inlining |
| Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 226 | *********************************************/ |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 227 | static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); } |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 228 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 229 | #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; } |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 230 | static void ZSTD_copy16(void* dst, const void* src) { memcpy(dst, src, 16); } |
| 231 | #define COPY16(d,s) { ZSTD_copy16(d,s); d+=16; s+=16; } |
| 232 | |
| Nick Terrell | cdad7fa | 2019-09-20 00:52:15 -0700 | [diff] [blame] | 233 | #define WILDCOPY_OVERLENGTH 32 |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 234 | #define WILDCOPY_VECLEN 16 |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 235 | |
| 236 | typedef enum { |
| 237 | ZSTD_no_overlap, |
| Yann Collet | cb18fff | 2019-09-24 17:50:58 -0700 | [diff] [blame] | 238 | ZSTD_overlap_src_before_dst |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 239 | /* ZSTD_overlap_dst_before_src, */ |
| 240 | } ZSTD_overlap_e; |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 241 | |
| Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 242 | /*! ZSTD_wildcopy() : |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 243 | * Custom version of memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0) |
| 244 | * @param ovtype controls the overlap detection |
| 245 | * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart. |
| 246 | * - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart. |
| 247 | * The src buffer must be before the dst buffer. |
| 248 | */ |
| Nick Terrell | e32e3e8 | 2020-01-28 20:37:04 -0800 | [diff] [blame] | 249 | MEM_STATIC FORCE_INLINE_ATTR |
| Nick Terrell | 44c65da | 2019-09-20 12:23:25 -0700 | [diff] [blame] | 250 | 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] | 251 | { |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 252 | ptrdiff_t diff = (BYTE*)dst - (const BYTE*)src; |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 253 | const BYTE* ip = (const BYTE*)src; |
| 254 | BYTE* op = (BYTE*)dst; |
| 255 | BYTE* const oend = op + length; |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 256 | |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 257 | assert(diff >= 8 || (ovtype == ZSTD_no_overlap && diff <= -WILDCOPY_VECLEN)); |
| mgrice | b830599 | 2019-08-27 14:49:23 -0700 | [diff] [blame] | 258 | |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 259 | if (ovtype == ZSTD_overlap_src_before_dst && diff < WILDCOPY_VECLEN) { |
| 260 | /* Handle short offset copies. */ |
| mgrice | b830599 | 2019-08-27 14:49:23 -0700 | [diff] [blame] | 261 | do { |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 262 | COPY8(op, ip) |
| 263 | } while (op < oend); |
| 264 | } else { |
| 265 | assert(diff >= WILDCOPY_VECLEN || diff <= -WILDCOPY_VECLEN); |
| Nick Terrell | e32e3e8 | 2020-01-28 20:37:04 -0800 | [diff] [blame] | 266 | /* Separate out the first COPY16() call because the copy length is |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 267 | * almost certain to be short, so the branches have different |
| Nick Terrell | e32e3e8 | 2020-01-28 20:37:04 -0800 | [diff] [blame] | 268 | * probabilities. Since it is almost certain to be short, only do |
| 269 | * one COPY16() in the first call. Then, do two calls per loop since |
| 270 | * at that point it is more likely to have a high trip count. |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 271 | */ |
| 272 | COPY16(op, ip); |
| Nick Terrell | efd37a6 | 2019-09-19 13:25:03 -0700 | [diff] [blame] | 273 | if (op >= oend) return; |
| 274 | do { |
| 275 | COPY16(op, ip); |
| Nick Terrell | cdad7fa | 2019-09-20 00:52:15 -0700 | [diff] [blame] | 276 | COPY16(op, ip); |
| mgrice | b830599 | 2019-08-27 14:49:23 -0700 | [diff] [blame] | 277 | } |
| 278 | while (op < oend); |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| Carl Woffenden | edd9a07 | 2020-04-07 11:02:06 +0200 | [diff] [blame] | 282 | MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
| 283 | { |
| 284 | size_t const length = MIN(dstCapacity, srcSize); |
| 285 | if (length > 0) { |
| 286 | memcpy(dst, src, length); |
| 287 | } |
| 288 | return length; |
| 289 | } |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 290 | |
| Bimba Shrestha | 0154866 | 2020-04-03 14:26:15 -0700 | [diff] [blame] | 291 | /* define "workspace is too large" as this number of times larger than needed */ |
| 292 | #define ZSTD_WORKSPACETOOLARGE_FACTOR 3 |
| 293 | |
| 294 | /* when workspace is continuously too large |
| 295 | * during at least this number of times, |
| 296 | * context's memory usage is considered wasteful, |
| 297 | * because it's sized to handle a worst case scenario which rarely happens. |
| 298 | * In which case, resize it down to free some memory */ |
| 299 | #define ZSTD_WORKSPACETOOLARGE_MAXDURATION 128 |
| 300 | |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 301 | |
| 302 | /*-******************************************* |
| Yann Collet | 8b6aecf | 2017-11-07 15:27:06 -0800 | [diff] [blame] | 303 | * Private declarations |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 304 | *********************************************/ |
| Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 305 | typedef struct seqDef_s { |
| 306 | U32 offset; |
| 307 | U16 litLength; |
| 308 | U16 matchLength; |
| 309 | } seqDef; |
| 310 | |
| inikep | 87d4f3d | 2016-03-02 15:56:24 +0100 | [diff] [blame] | 311 | typedef struct { |
| Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 312 | seqDef* sequencesStart; |
| Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 313 | seqDef* sequences; |
| Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 314 | BYTE* litStart; |
| 315 | BYTE* lit; |
| Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 316 | BYTE* llCode; |
| 317 | BYTE* mlCode; |
| 318 | BYTE* ofCode; |
| Nick Terrell | 924944e | 2018-08-21 14:20:02 -0700 | [diff] [blame] | 319 | size_t maxNbSeq; |
| Nick Terrell | 5e580de | 2018-08-28 13:24:44 -0700 | [diff] [blame] | 320 | size_t maxNbLit; |
| Yann Collet | 5d39357 | 2016-04-07 17:19:00 +0200 | [diff] [blame] | 321 | U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */ |
| 322 | U32 longLengthPos; |
| Nick Terrell | 7a28b9e | 2017-07-17 15:29:11 -0700 | [diff] [blame] | 323 | } seqStore_t; |
| 324 | |
| Nick Terrell | e103d7b | 2020-05-01 16:11:47 -0700 | [diff] [blame] | 325 | typedef struct { |
| 326 | U32 litLength; |
| 327 | U32 matchLength; |
| 328 | } ZSTD_sequenceLength; |
| 329 | |
| 330 | /** |
| 331 | * Returns the ZSTD_sequenceLength for the given sequences. It handles the decoding of long sequences |
| 332 | * indicated by longLengthPos and longLengthID, and adds MINMATCH back to matchLength. |
| 333 | */ |
| 334 | MEM_STATIC ZSTD_sequenceLength ZSTD_getSequenceLength(seqStore_t const* seqStore, seqDef const* seq) |
| 335 | { |
| 336 | ZSTD_sequenceLength seqLen; |
| 337 | seqLen.litLength = seq->litLength; |
| 338 | seqLen.matchLength = seq->matchLength + MINMATCH; |
| 339 | if (seqStore->longLengthPos == (U32)(seq - seqStore->sequencesStart)) { |
| 340 | if (seqStore->longLengthID == 1) { |
| 341 | seqLen.litLength += 0xFFFF; |
| 342 | } |
| 343 | if (seqStore->longLengthID == 2) { |
| 344 | seqLen.matchLength += 0xFFFF; |
| 345 | } |
| 346 | } |
| 347 | return seqLen; |
| 348 | } |
| 349 | |
| shakeelrao | 19b75b6 | 2019-03-15 18:04:19 -0700 | [diff] [blame] | 350 | /** |
| 351 | * 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] | 352 | * Note: before using `compressedSize`, check for errors using ZSTD_isError(). |
| 353 | * similarly, before using `decompressedBound`, check for errors using: |
| 354 | * `decompressedBound != ZSTD_CONTENTSIZE_ERROR` |
| shakeelrao | 19b75b6 | 2019-03-15 18:04:19 -0700 | [diff] [blame] | 355 | */ |
| 356 | typedef struct { |
| 357 | size_t compressedSize; |
| 358 | unsigned long long decompressedBound; |
| shakeelrao | 0033bb4 | 2019-03-17 17:41:27 -0700 | [diff] [blame] | 359 | } ZSTD_frameSizeInfo; /* decompress & legacy */ |
| shakeelrao | 19b75b6 | 2019-03-15 18:04:19 -0700 | [diff] [blame] | 360 | |
| Yann Collet | 8b6aecf | 2017-11-07 15:27:06 -0800 | [diff] [blame] | 361 | const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx); /* compress & dictBuilder */ |
| 362 | 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] | 363 | |
| inikep | c4807f4 | 2016-06-02 15:11:39 +0200 | [diff] [blame] | 364 | /* custom memory allocation functions */ |
| Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 365 | void* ZSTD_malloc(size_t size, ZSTD_customMem customMem); |
| Yann Collet | 44e45e8 | 2017-05-30 16:12:06 -0700 | [diff] [blame] | 366 | void* ZSTD_calloc(size_t size, ZSTD_customMem customMem); |
| Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 367 | void ZSTD_free(void* ptr, ZSTD_customMem customMem); |
| 368 | |
| inikep | 2a74609 | 2016-06-03 14:53:51 +0200 | [diff] [blame] | 369 | |
| Yann Collet | 8b6aecf | 2017-11-07 15:27:06 -0800 | [diff] [blame] | 370 | MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */ |
| Yann Collet | c154d9d | 2016-07-27 14:37:00 +0200 | [diff] [blame] | 371 | { |
| Stella Lau | e50ed1f | 2017-08-22 11:55:42 -0700 | [diff] [blame] | 372 | assert(val != 0); |
| 373 | { |
| Yann Collet | c154d9d | 2016-07-27 14:37:00 +0200 | [diff] [blame] | 374 | # if defined(_MSC_VER) /* Visual */ |
| Stella Lau | e50ed1f | 2017-08-22 11:55:42 -0700 | [diff] [blame] | 375 | unsigned long r=0; |
| Bimba Shrestha | dba3abc | 2020-03-05 12:20:59 -0800 | [diff] [blame] | 376 | return _BitScanReverse(&r, val) ? (unsigned)r : 0; |
| Yann Collet | c154d9d | 2016-07-27 14:37:00 +0200 | [diff] [blame] | 377 | # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */ |
| Dávid Bolvanský | 1ab1a40 | 2019-09-23 21:32:56 +0200 | [diff] [blame] | 378 | return __builtin_clz (val) ^ 31; |
| Joseph Chen | 3855bc4 | 2019-07-29 15:20:37 +0800 | [diff] [blame] | 379 | # elif defined(__ICCARM__) /* IAR Intrinsic */ |
| 380 | return 31 - __CLZ(val); |
| Yann Collet | c154d9d | 2016-07-27 14:37:00 +0200 | [diff] [blame] | 381 | # else /* Software version */ |
| Yann Collet | 0a0a212 | 2017-11-28 14:07:03 -0800 | [diff] [blame] | 382 | 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] | 383 | U32 v = val; |
| Stella Lau | e50ed1f | 2017-08-22 11:55:42 -0700 | [diff] [blame] | 384 | v |= v >> 1; |
| 385 | v |= v >> 2; |
| 386 | v |= v >> 4; |
| 387 | v |= v >> 8; |
| 388 | v |= v >> 16; |
| Yann Collet | 0a0a212 | 2017-11-28 14:07:03 -0800 | [diff] [blame] | 389 | return DeBruijnClz[(v * 0x07C4ACDDU) >> 27]; |
| Yann Collet | c154d9d | 2016-07-27 14:37:00 +0200 | [diff] [blame] | 390 | # endif |
| Stella Lau | e50ed1f | 2017-08-22 11:55:42 -0700 | [diff] [blame] | 391 | } |
| Yann Collet | c154d9d | 2016-07-27 14:37:00 +0200 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | |
| Yann Collet | 32dfae6 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 395 | /* ZSTD_invalidateRepCodes() : |
| 396 | * ensures next compression will not use repcodes from previous block. |
| 397 | * Note : only works with regular variant; |
| 398 | * do not use with extDict variant ! */ |
| Yann Collet | 8b6aecf | 2017-11-07 15:27:06 -0800 | [diff] [blame] | 399 | 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] | 400 | |
| 401 | |
| Yann Collet | f04deff | 2017-07-06 01:42:46 -0700 | [diff] [blame] | 402 | typedef struct { |
| 403 | blockType_e blockType; |
| 404 | U32 lastBlock; |
| 405 | U32 origSize; |
| Yann Collet | 2b49140 | 2018-10-25 16:28:41 -0700 | [diff] [blame] | 406 | } blockProperties_t; /* declared here for decompress and fullbench */ |
| Yann Collet | f04deff | 2017-07-06 01:42:46 -0700 | [diff] [blame] | 407 | |
| 408 | /*! ZSTD_getcBlockSize() : |
| Yann Collet | 4191efa | 2017-11-08 11:05:32 -0800 | [diff] [blame] | 409 | * Provides the size of compressed block from block header `src` */ |
| 410 | /* Used by: decompress, fullbench (does not get its definition from here) */ |
| Yann Collet | f04deff | 2017-07-06 01:42:46 -0700 | [diff] [blame] | 411 | size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, |
| 412 | blockProperties_t* bpPtr); |
| 413 | |
| Yann Collet | 2b49140 | 2018-10-25 16:28:41 -0700 | [diff] [blame] | 414 | /*! ZSTD_decodeSeqHeaders() : |
| 415 | * decode sequence header from src */ |
| 416 | /* Used by: decompress, fullbench (does not get its definition from here) */ |
| 417 | size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeqPtr, |
| 418 | const void* src, size_t srcSize); |
| 419 | |
| 420 | |
| Nick Terrell | de6c6bc | 2017-08-24 18:09:50 -0700 | [diff] [blame] | 421 | #if defined (__cplusplus) |
| 422 | } |
| 423 | #endif |
| Yann Collet | f04deff | 2017-07-06 01:42:46 -0700 | [diff] [blame] | 424 | |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 425 | #endif /* ZSTD_CCOMMON_H_MODULE */ |