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 | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 10 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 11 | |
| 12 | /*-************************************ |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 13 | * Compiler specific |
| 14 | **************************************/ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 15 | #ifdef _MSC_VER /* Visual Studio */ |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 16 | # define _CRT_SECURE_NO_WARNINGS /* fgets */ |
| 17 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 18 | # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 19 | #endif |
| 20 | |
| 21 | |
| 22 | /*-************************************ |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 23 | * Includes |
| 24 | **************************************/ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 25 | #include <stdlib.h> /* free */ |
| 26 | #include <stdio.h> /* fgets, sscanf */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 27 | #include <string.h> /* strcmp */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 28 | #include <assert.h> /* assert */ |
Lzu Tao | fb6901b | 2019-04-12 21:28:17 +0700 | [diff] [blame] | 29 | #include "timefn.h" /* UTIL_time_t, UTIL_getTime */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 30 | #include "mem.h" |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 31 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem, ZSTD_getDictID_fromFrame */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 32 | #include "zstd.h" /* ZSTD_compressBound */ |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 33 | #include "zstd_errors.h" /* ZSTD_error_srcSize_wrong */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 34 | #include "zstdmt_compress.h" |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 35 | #include "zdict.h" /* ZDICT_trainFromBuffer */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 36 | #include "datagen.h" /* RDG_genBuffer */ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 37 | #define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 38 | #include "xxhash.h" /* XXH64_* */ |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 39 | #include "seqgen.h" |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 40 | #include "util.h" |
Yann Collet | 59a7116 | 2019-04-10 12:37:03 -0700 | [diff] [blame] | 41 | #include "timefn.h" /* UTIL_time_t, UTIL_clockSpanMicro, UTIL_getTime */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 42 | |
| 43 | |
| 44 | /*-************************************ |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 45 | * Constants |
| 46 | **************************************/ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 47 | #define KB *(1U<<10) |
| 48 | #define MB *(1U<<20) |
| 49 | #define GB *(1U<<30) |
| 50 | |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 51 | static const int nbTestsDefault = 10000; |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 52 | static const U32 g_cLevelMax_smallTests = 10; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 53 | #define COMPRESSIBLE_NOISE_LENGTH (10 MB) |
| 54 | #define FUZ_COMPRESSIBILITY_DEFAULT 50 |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 55 | static const U32 prime32 = 2654435761U; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 56 | |
| 57 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 58 | /*-************************************ |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 59 | * Display Macros |
| 60 | **************************************/ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 61 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 62 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { \ |
| 63 | DISPLAY(__VA_ARGS__); \ |
| 64 | if (g_displayLevel>=4) fflush(stderr); } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 65 | static U32 g_displayLevel = 2; |
| 66 | |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 67 | static const U64 g_refreshRate = SEC_TO_MICRO / 6; |
| 68 | static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 69 | |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 70 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
| 71 | if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ |
| 72 | { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \ |
| 73 | if (g_displayLevel>=4) fflush(stderr); } } |
| 74 | |
| 75 | static U64 g_clockTime = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 76 | |
| 77 | |
| 78 | /*-******************************************************* |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 79 | * Check macros |
| 80 | *********************************************************/ |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 81 | #undef MIN |
| 82 | #undef MAX |
| 83 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 84 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 85 | /*! FUZ_rand() : |
| 86 | @return : a 27 bits random value, from a 32-bits `seed`. |
| 87 | `seed` is also modified */ |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 88 | #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 89 | static U32 FUZ_rand(U32* seedPtr) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 90 | { |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 91 | static const U32 prime2 = 2246822519U; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 92 | U32 rand32 = *seedPtr; |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 93 | rand32 *= prime32; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 94 | rand32 += prime2; |
| 95 | rand32 = FUZ_rotl32(rand32, 13); |
| 96 | *seedPtr = rand32; |
| 97 | return rand32 >> 5; |
| 98 | } |
| 99 | |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 100 | #define CHECK(cond, ...) { \ |
| 101 | if (cond) { \ |
| 102 | DISPLAY("Error => "); \ |
| 103 | DISPLAY(__VA_ARGS__); \ |
Yann Collet | ebd955e | 2018-01-23 13:12:40 -0800 | [diff] [blame] | 104 | DISPLAY(" (seed %u, test nb %u, line %u) \n", \ |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 105 | (unsigned)seed, testNb, __LINE__); \ |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 106 | goto _output_error; \ |
| 107 | } } |
| 108 | |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 109 | #define CHECK_Z(f) { \ |
| 110 | size_t const err = f; \ |
| 111 | CHECK(ZSTD_isError(err), "%s : %s ", \ |
| 112 | #f, ZSTD_getErrorName(err)); \ |
| 113 | } |
| 114 | |
Nick Terrell | ca77822 | 2018-04-25 16:32:29 -0700 | [diff] [blame] | 115 | #define CHECK_RET(ret, cond, ...) { \ |
| 116 | if (cond) { \ |
| 117 | DISPLAY("Error %llu => ", (unsigned long long)ret); \ |
| 118 | DISPLAY(__VA_ARGS__); \ |
| 119 | DISPLAY(" (line %u)\n", __LINE__); \ |
| 120 | return ret; \ |
| 121 | } } |
| 122 | |
| 123 | #define CHECK_RET_Z(f) { \ |
| 124 | size_t const err = f; \ |
| 125 | CHECK_RET(err, ZSTD_isError(err), "%s : %s ", \ |
| 126 | #f, ZSTD_getErrorName(err)); \ |
| 127 | } |
| 128 | |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 129 | |
| 130 | /*====================================================== |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 131 | * Basic Unit tests |
| 132 | *======================================================*/ |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 133 | |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 134 | typedef struct { |
| 135 | void* start; |
| 136 | size_t size; |
| 137 | size_t filled; |
| 138 | } buffer_t; |
| 139 | |
Yann Collet | d195eec | 2018-09-13 12:29:52 -0700 | [diff] [blame] | 140 | static const buffer_t kBuffNull = { NULL, 0 , 0 }; |
| 141 | |
| 142 | static void FUZ_freeDictionary(buffer_t dict) |
| 143 | { |
| 144 | free(dict.start); |
| 145 | } |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 146 | |
| 147 | static buffer_t FUZ_createDictionary(const void* src, size_t srcSize, size_t blockSize, size_t requestedDictSize) |
| 148 | { |
Yann Collet | d195eec | 2018-09-13 12:29:52 -0700 | [diff] [blame] | 149 | buffer_t dict = kBuffNull; |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 150 | size_t const nbBlocks = (srcSize + (blockSize-1)) / blockSize; |
Yann Collet | d195eec | 2018-09-13 12:29:52 -0700 | [diff] [blame] | 151 | size_t* const blockSizes = (size_t*)malloc(nbBlocks * sizeof(size_t)); |
| 152 | if (!blockSizes) return kBuffNull; |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 153 | dict.start = malloc(requestedDictSize); |
Yann Collet | d195eec | 2018-09-13 12:29:52 -0700 | [diff] [blame] | 154 | if (!dict.start) { free(blockSizes); return kBuffNull; } |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 155 | { size_t nb; |
| 156 | for (nb=0; nb<nbBlocks-1; nb++) blockSizes[nb] = blockSize; |
| 157 | blockSizes[nbBlocks-1] = srcSize - (blockSize * (nbBlocks-1)); |
| 158 | } |
| 159 | { size_t const dictSize = ZDICT_trainFromBuffer(dict.start, requestedDictSize, src, blockSizes, (unsigned)nbBlocks); |
| 160 | free(blockSizes); |
Yann Collet | d195eec | 2018-09-13 12:29:52 -0700 | [diff] [blame] | 161 | if (ZDICT_isError(dictSize)) { FUZ_freeDictionary(dict); return kBuffNull; } |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 162 | dict.size = requestedDictSize; |
| 163 | dict.filled = dictSize; |
Yann Collet | d195eec | 2018-09-13 12:29:52 -0700 | [diff] [blame] | 164 | return dict; |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 168 | /* Round trips data and updates xxh with the decompressed data produced */ |
| 169 | static size_t SEQ_roundTrip(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx, |
| 170 | XXH64_state_t* xxh, void* data, size_t size, |
| 171 | ZSTD_EndDirective endOp) |
| 172 | { |
| 173 | static BYTE compressed[1024]; |
| 174 | static BYTE uncompressed[1024]; |
| 175 | |
| 176 | ZSTD_inBuffer cin = {data, size, 0}; |
| 177 | size_t cret; |
| 178 | |
| 179 | do { |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 180 | ZSTD_outBuffer cout = { compressed, sizeof(compressed), 0 }; |
| 181 | ZSTD_inBuffer din = { compressed, 0, 0 }; |
| 182 | ZSTD_outBuffer dout = { uncompressed, 0, 0 }; |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 183 | |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 184 | cret = ZSTD_compressStream2(cctx, &cout, &cin, endOp); |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 185 | if (ZSTD_isError(cret)) |
| 186 | return cret; |
| 187 | |
| 188 | din.size = cout.pos; |
| 189 | while (din.pos < din.size || (endOp == ZSTD_e_end && cret == 0)) { |
| 190 | size_t dret; |
| 191 | |
| 192 | dout.pos = 0; |
| 193 | dout.size = sizeof(uncompressed); |
| 194 | dret = ZSTD_decompressStream(dctx, &dout, &din); |
| 195 | if (ZSTD_isError(dret)) |
| 196 | return dret; |
| 197 | XXH64_update(xxh, dout.dst, dout.pos); |
| 198 | if (dret == 0) |
| 199 | break; |
| 200 | } |
| 201 | } while (cin.pos < cin.size || (endOp != ZSTD_e_continue && cret != 0)); |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /* Generates some data and round trips it */ |
| 206 | static size_t SEQ_generateRoundTrip(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx, |
| 207 | XXH64_state_t* xxh, SEQ_stream* seq, |
| 208 | SEQ_gen_type type, unsigned value) |
| 209 | { |
| 210 | static BYTE data[1024]; |
| 211 | size_t gen; |
| 212 | |
| 213 | do { |
| 214 | SEQ_outBuffer sout = {data, sizeof(data), 0}; |
| 215 | size_t ret; |
| 216 | gen = SEQ_gen(seq, type, value, &sout); |
| 217 | |
| 218 | ret = SEQ_roundTrip(cctx, dctx, xxh, sout.dst, sout.pos, ZSTD_e_continue); |
| 219 | if (ZSTD_isError(ret)) |
| 220 | return ret; |
| 221 | } while (gen != 0); |
| 222 | |
| 223 | return 0; |
| 224 | } |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 225 | |
Nick Terrell | ca77822 | 2018-04-25 16:32:29 -0700 | [diff] [blame] | 226 | static size_t getCCtxParams(ZSTD_CCtx* zc, ZSTD_parameters* savedParams) |
| 227 | { |
Yann Collet | d4d4e10 | 2018-11-21 15:37:26 -0800 | [diff] [blame] | 228 | int value; |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 229 | CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_c_windowLog, (int*)&savedParams->cParams.windowLog)); |
| 230 | CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_c_hashLog, (int*)&savedParams->cParams.hashLog)); |
| 231 | CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_c_chainLog, (int*)&savedParams->cParams.chainLog)); |
| 232 | CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_c_searchLog, (int*)&savedParams->cParams.searchLog)); |
| 233 | CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_c_minMatch, (int*)&savedParams->cParams.minMatch)); |
| 234 | CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_c_targetLength, (int*)&savedParams->cParams.targetLength)); |
Yann Collet | be9e561 | 2018-12-06 15:00:52 -0800 | [diff] [blame] | 235 | CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_c_strategy, &value)); |
Nick Terrell | ca77822 | 2018-04-25 16:32:29 -0700 | [diff] [blame] | 236 | savedParams->cParams.strategy = value; |
| 237 | |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 238 | CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_c_checksumFlag, &savedParams->fParams.checksumFlag)); |
| 239 | CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_c_contentSizeFlag, &savedParams->fParams.contentSizeFlag)); |
| 240 | CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_c_dictIDFlag, &value)); |
Nick Terrell | ca77822 | 2018-04-25 16:32:29 -0700 | [diff] [blame] | 241 | savedParams->fParams.noDictIDFlag = !value; |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static U32 badParameters(ZSTD_CCtx* zc, ZSTD_parameters const savedParams) |
| 246 | { |
| 247 | ZSTD_parameters params; |
| 248 | if (ZSTD_isError(getCCtxParams(zc, ¶ms))) return 10; |
| 249 | CHECK_RET(1, params.cParams.windowLog != savedParams.cParams.windowLog, "windowLog"); |
| 250 | CHECK_RET(2, params.cParams.hashLog != savedParams.cParams.hashLog, "hashLog"); |
| 251 | CHECK_RET(3, params.cParams.chainLog != savedParams.cParams.chainLog, "chainLog"); |
| 252 | CHECK_RET(4, params.cParams.searchLog != savedParams.cParams.searchLog, "searchLog"); |
Yann Collet | e874dac | 2018-11-20 14:56:07 -0800 | [diff] [blame] | 253 | CHECK_RET(5, params.cParams.minMatch != savedParams.cParams.minMatch, "minMatch"); |
Nick Terrell | ca77822 | 2018-04-25 16:32:29 -0700 | [diff] [blame] | 254 | CHECK_RET(6, params.cParams.targetLength != savedParams.cParams.targetLength, "targetLength"); |
| 255 | |
| 256 | CHECK_RET(7, params.fParams.checksumFlag != savedParams.fParams.checksumFlag, "checksumFlag"); |
| 257 | CHECK_RET(8, params.fParams.contentSizeFlag != savedParams.fParams.contentSizeFlag, "contentSizeFlag"); |
| 258 | CHECK_RET(9, params.fParams.noDictIDFlag != savedParams.fParams.noDictIDFlag, "noDictIDFlag"); |
| 259 | return 0; |
| 260 | } |
| 261 | |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 262 | static int basicUnitTests(U32 seed, double compressibility) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 263 | { |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 264 | size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 265 | void* CNBuffer = malloc(CNBufferSize); |
Nick Terrell | e19b082 | 2017-11-01 13:10:03 -0700 | [diff] [blame] | 266 | size_t const skippableFrameSize = 200 KB; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 267 | size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH); |
| 268 | void* compressedBuffer = malloc(compressedBufferSize); |
| 269 | size_t const decodedBufferSize = CNBufferSize; |
| 270 | void* decodedBuffer = malloc(decodedBufferSize); |
| 271 | size_t cSize; |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 272 | int testResult = 0; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 273 | int testNb = 1; |
Yann Collet | 6711396 | 2018-01-19 22:11:11 -0800 | [diff] [blame] | 274 | U32 coreSeed = 0; /* this name to conform with CHECK_Z macro display */ |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 275 | ZSTD_CStream* zc = ZSTD_createCStream(); |
| 276 | ZSTD_DStream* zd = ZSTD_createDStream(); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 277 | ZSTDMT_CCtx* mtctx = ZSTDMT_createCCtx(2); |
| 278 | |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 279 | ZSTD_inBuffer inBuff, inBuff2; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 280 | ZSTD_outBuffer outBuff; |
Yann Collet | d195eec | 2018-09-13 12:29:52 -0700 | [diff] [blame] | 281 | buffer_t dictionary = kBuffNull; |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 282 | size_t const dictSize = 128 KB; |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 283 | unsigned dictID = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 284 | |
| 285 | /* Create compressible test buffer */ |
| 286 | if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) { |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 287 | DISPLAY("Not enough memory, aborting \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 288 | goto _output_error; |
| 289 | } |
| 290 | RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed); |
| 291 | |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 292 | /* Create dictionary */ |
Yann Collet | e0065cf | 2017-09-28 18:34:38 -0700 | [diff] [blame] | 293 | DISPLAYLEVEL(3, "creating dictionary for unit tests \n"); |
Yann Collet | b18cb7e | 2018-03-20 16:18:48 -0700 | [diff] [blame] | 294 | dictionary = FUZ_createDictionary(CNBuffer, CNBufferSize / 3, 16 KB, 48 KB); |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 295 | if (!dictionary.start) { |
| 296 | DISPLAY("Error creating dictionary, aborting \n"); |
| 297 | goto _output_error; |
| 298 | } |
| 299 | dictID = ZDICT_getDictID(dictionary.start, dictionary.filled); |
| 300 | |
Yann Collet | 21f7672 | 2017-12-07 03:06:01 -0500 | [diff] [blame] | 301 | /* Basic compression test */ |
| 302 | DISPLAYLEVEL(3, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
| 303 | CHECK_Z( ZSTD_initCStream(zc, 1 /* cLevel */) ); |
| 304 | outBuff.dst = (char*)(compressedBuffer); |
| 305 | outBuff.size = compressedBufferSize; |
| 306 | outBuff.pos = 0; |
| 307 | inBuff.src = CNBuffer; |
| 308 | inBuff.size = CNBufferSize; |
| 309 | inBuff.pos = 0; |
| 310 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
| 311 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 312 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 313 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 314 | DISPLAYLEVEL(3, "OK (%u bytes)\n", (unsigned)outBuff.pos); |
Yann Collet | 21f7672 | 2017-12-07 03:06:01 -0500 | [diff] [blame] | 315 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 316 | /* generate skippable frame */ |
| 317 | MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START); |
| 318 | MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize); |
| 319 | cSize = skippableFrameSize + 8; |
| 320 | |
Yann Collet | 21f7672 | 2017-12-07 03:06:01 -0500 | [diff] [blame] | 321 | /* Basic compression test using dict */ |
| 322 | DISPLAYLEVEL(3, "test%3i : skipframe + compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
Yann Collet | 3c1e3f8 | 2017-10-13 18:32:06 -0700 | [diff] [blame] | 323 | CHECK_Z( ZSTD_initCStream_usingDict(zc, CNBuffer, dictSize, 1 /* cLevel */) ); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 324 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
Yann Collet | 3c1e3f8 | 2017-10-13 18:32:06 -0700 | [diff] [blame] | 325 | assert(compressedBufferSize > cSize); |
| 326 | outBuff.size = compressedBufferSize - cSize; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 327 | outBuff.pos = 0; |
| 328 | inBuff.src = CNBuffer; |
| 329 | inBuff.size = CNBufferSize; |
| 330 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 331 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 332 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 333 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
Yann Collet | 9a021c1 | 2016-08-26 09:05:06 +0200 | [diff] [blame] | 334 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 335 | cSize += outBuff.pos; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 336 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", |
| 337 | (unsigned)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 338 | |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 339 | /* context size functions */ |
| 340 | DISPLAYLEVEL(3, "test%3i : estimate CStream size : ", testNb++); |
| 341 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictSize); |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 342 | size_t const cstreamSize = ZSTD_estimateCStreamSize_usingCParams(cParams); |
| 343 | size_t const cdictSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy); /* uses ZSTD_initCStream_usingDict() */ |
| 344 | if (ZSTD_isError(cstreamSize)) goto _output_error; |
| 345 | if (ZSTD_isError(cdictSize)) goto _output_error; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 346 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (unsigned)(cstreamSize + cdictSize)); |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Nick Terrell | 56682a7 | 2019-04-01 17:51:28 -0700 | [diff] [blame] | 349 | /* context size functions */ |
| 350 | DISPLAYLEVEL(3, "test%3i : estimate CStream size using CCtxParams : ", testNb++); |
| 351 | { ZSTD_CCtx_params* const params = ZSTD_createCCtxParams(); |
| 352 | size_t cstreamSize, cctxSize; |
| 353 | CHECK_Z( ZSTD_CCtxParams_setParameter(params, ZSTD_c_compressionLevel, 19) ); |
| 354 | cstreamSize = ZSTD_estimateCStreamSize_usingCCtxParams(params); |
| 355 | CHECK_Z(cstreamSize); |
| 356 | cctxSize = ZSTD_estimateCCtxSize_usingCCtxParams(params); |
| 357 | CHECK_Z(cctxSize); |
| 358 | if (cstreamSize <= cctxSize + 2 * ZSTD_BLOCKSIZE_MAX) goto _output_error; |
| 359 | ZSTD_freeCCtxParams(params); |
| 360 | DISPLAYLEVEL(3, "OK \n"); |
| 361 | } |
| 362 | |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 363 | DISPLAYLEVEL(3, "test%3i : check actual CStream size : ", testNb++); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 364 | { size_t const s = ZSTD_sizeof_CStream(zc); |
| 365 | if (ZSTD_isError(s)) goto _output_error; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 366 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (unsigned)s); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 369 | /* Attempt bad compression parameters */ |
| 370 | DISPLAYLEVEL(3, "test%3i : use bad compression parameters : ", testNb++); |
| 371 | { size_t r; |
| 372 | ZSTD_parameters params = ZSTD_getParams(1, 0, 0); |
Yann Collet | e874dac | 2018-11-20 14:56:07 -0800 | [diff] [blame] | 373 | params.cParams.minMatch = 2; |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 374 | r = ZSTD_initCStream_advanced(zc, NULL, 0, params, 0); |
| 375 | if (!ZSTD_isError(r)) goto _output_error; |
| 376 | DISPLAYLEVEL(3, "init error : %s \n", ZSTD_getErrorName(r)); |
| 377 | } |
| 378 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 379 | /* skippable frame test */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 380 | DISPLAYLEVEL(3, "test%3i : decompress skippable frame : ", testNb++); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 381 | CHECK_Z( ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize) ); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 382 | inBuff.src = compressedBuffer; |
| 383 | inBuff.size = cSize; |
| 384 | inBuff.pos = 0; |
| 385 | outBuff.dst = decodedBuffer; |
| 386 | outBuff.size = CNBufferSize; |
| 387 | outBuff.pos = 0; |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 388 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 389 | DISPLAYLEVEL(5, " ( ZSTD_decompressStream => %u ) ", (unsigned)r); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 390 | if (r != 0) goto _output_error; |
| 391 | } |
Yann Collet | a33ae64 | 2017-02-28 01:15:28 -0800 | [diff] [blame] | 392 | if (outBuff.pos != 0) goto _output_error; /* skippable frame output len is 0 */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 393 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 394 | |
| 395 | /* Basic decompression test */ |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 396 | inBuff2 = inBuff; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 397 | DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 398 | ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
Yann Collet | 3e042d5 | 2018-12-04 17:30:58 -0800 | [diff] [blame] | 399 | CHECK_Z( ZSTD_DCtx_setParameter(zd, ZSTD_d_windowLogMax, ZSTD_WINDOWLOG_LIMIT_DEFAULT+1) ); /* large limit */ |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 400 | { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 401 | if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
| 402 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 403 | if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 404 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 405 | |
| 406 | /* Re-use without init */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 407 | DISPLAYLEVEL(3, "test%3i : decompress again without init (re-use previous settings): ", testNb++); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 408 | outBuff.pos = 0; |
| 409 | { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2); |
| 410 | if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 411 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 412 | if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 413 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 414 | |
| 415 | /* check regenerated data is byte exact */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 416 | DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 417 | { size_t i; |
| 418 | for (i=0; i<CNBufferSize; i++) { |
| 419 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error; |
| 420 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 421 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 422 | |
Yann Collet | f16f449 | 2017-05-09 16:18:17 -0700 | [diff] [blame] | 423 | /* context size functions */ |
| 424 | DISPLAYLEVEL(3, "test%3i : estimate DStream size : ", testNb++); |
| 425 | { ZSTD_frameHeader fhi; |
| 426 | const void* cStart = (char*)compressedBuffer + (skippableFrameSize + 8); |
| 427 | size_t const gfhError = ZSTD_getFrameHeader(&fhi, cStart, cSize); |
| 428 | if (gfhError!=0) goto _output_error; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 429 | DISPLAYLEVEL(5, " (windowSize : %u) ", (unsigned)fhi.windowSize); |
Yann Collet | dde10b2 | 2017-06-26 17:44:26 -0700 | [diff] [blame] | 430 | { size_t const s = ZSTD_estimateDStreamSize(fhi.windowSize) |
Yann Collet | 25989e3 | 2017-05-25 15:07:37 -0700 | [diff] [blame] | 431 | /* uses ZSTD_initDStream_usingDict() */ |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 432 | + ZSTD_estimateDDictSize(dictSize, ZSTD_dlm_byCopy); |
Yann Collet | f16f449 | 2017-05-09 16:18:17 -0700 | [diff] [blame] | 433 | if (ZSTD_isError(s)) goto _output_error; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 434 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (unsigned)s); |
Yann Collet | f16f449 | 2017-05-09 16:18:17 -0700 | [diff] [blame] | 435 | } } |
| 436 | |
| 437 | DISPLAYLEVEL(3, "test%3i : check actual DStream size : ", testNb++); |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 438 | { size_t const s = ZSTD_sizeof_DStream(zd); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 439 | if (ZSTD_isError(s)) goto _output_error; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 440 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (unsigned)s); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Yann Collet | 6711396 | 2018-01-19 22:11:11 -0800 | [diff] [blame] | 443 | /* Decompression by small increment */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 444 | DISPLAYLEVEL(3, "test%3i : decompress byte-by-byte : ", testNb++); |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 445 | { /* skippable frame */ |
| 446 | size_t r = 1; |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 447 | ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 448 | inBuff.src = compressedBuffer; |
| 449 | outBuff.dst = decodedBuffer; |
| 450 | inBuff.pos = 0; |
| 451 | outBuff.pos = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 452 | while (r) { /* skippable frame */ |
Yann Collet | 31769ce | 2018-06-22 17:58:21 -0700 | [diff] [blame] | 453 | size_t const inSize = (FUZ_rand(&coreSeed) & 15) + 1; |
| 454 | size_t const outSize = (FUZ_rand(&coreSeed) & 15) + 1; |
Yann Collet | 6711396 | 2018-01-19 22:11:11 -0800 | [diff] [blame] | 455 | inBuff.size = inBuff.pos + inSize; |
| 456 | outBuff.size = outBuff.pos + outSize; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 457 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | 31769ce | 2018-06-22 17:58:21 -0700 | [diff] [blame] | 458 | if (ZSTD_isError(r)) DISPLAYLEVEL(4, "ZSTD_decompressStream on skippable frame error : %s \n", ZSTD_getErrorName(r)); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 459 | if (ZSTD_isError(r)) goto _output_error; |
| 460 | } |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 461 | /* normal frame */ |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 462 | ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 463 | r=1; |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 464 | while (r) { |
Yann Collet | 6711396 | 2018-01-19 22:11:11 -0800 | [diff] [blame] | 465 | size_t const inSize = FUZ_rand(&coreSeed) & 15; |
Yann Collet | 31769ce | 2018-06-22 17:58:21 -0700 | [diff] [blame] | 466 | size_t const outSize = (FUZ_rand(&coreSeed) & 15) + (!inSize); /* avoid having both sizes at 0 => would trigger a no_forward_progress error */ |
Yann Collet | 6711396 | 2018-01-19 22:11:11 -0800 | [diff] [blame] | 467 | inBuff.size = inBuff.pos + inSize; |
| 468 | outBuff.size = outBuff.pos + outSize; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 469 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | 31769ce | 2018-06-22 17:58:21 -0700 | [diff] [blame] | 470 | if (ZSTD_isError(r)) DISPLAYLEVEL(4, "ZSTD_decompressStream error : %s \n", ZSTD_getErrorName(r)); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 471 | if (ZSTD_isError(r)) goto _output_error; |
| 472 | } |
| 473 | } |
Yann Collet | 31769ce | 2018-06-22 17:58:21 -0700 | [diff] [blame] | 474 | if (outBuff.pos != CNBufferSize) DISPLAYLEVEL(4, "outBuff.pos != CNBufferSize : should have regenerated same amount ! \n"); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 475 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
Yann Collet | 31769ce | 2018-06-22 17:58:21 -0700 | [diff] [blame] | 476 | if (inBuff.pos != cSize) DISPLAYLEVEL(4, "inBuff.pos != cSize : should have real all input ! \n"); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 477 | if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 478 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 479 | |
| 480 | /* check regenerated data is byte exact */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 481 | DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 482 | { size_t i; |
| 483 | for (i=0; i<CNBufferSize; i++) { |
| 484 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; |
| 485 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 486 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 487 | |
Yann Collet | 31769ce | 2018-06-22 17:58:21 -0700 | [diff] [blame] | 488 | /* Decompression forward progress */ |
| 489 | DISPLAYLEVEL(3, "test%3i : generate error when ZSTD_decompressStream() doesn't progress : ", testNb++); |
| 490 | { /* skippable frame */ |
| 491 | size_t r = 0; |
| 492 | int decNb = 0; |
| 493 | int const maxDec = 100; |
| 494 | inBuff.src = compressedBuffer; |
| 495 | inBuff.size = cSize; |
| 496 | inBuff.pos = 0; |
| 497 | |
| 498 | outBuff.dst = decodedBuffer; |
| 499 | outBuff.pos = 0; |
| 500 | outBuff.size = CNBufferSize-1; /* 1 byte missing */ |
| 501 | |
| 502 | for (decNb=0; decNb<maxDec; decNb++) { |
| 503 | if (r==0) ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
| 504 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 505 | if (ZSTD_isError(r)) break; |
| 506 | } |
| 507 | if (!ZSTD_isError(r)) DISPLAYLEVEL(4, "ZSTD_decompressStream should have triggered a no_forward_progress error \n"); |
| 508 | if (!ZSTD_isError(r)) goto _output_error; /* should have triggered no_forward_progress error */ |
| 509 | } |
| 510 | DISPLAYLEVEL(3, "OK \n"); |
| 511 | |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 512 | /* _srcSize compression test */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 513 | DISPLAYLEVEL(3, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
Nick Terrell | e55da9e | 2019-03-13 14:05:18 -0700 | [diff] [blame] | 514 | CHECK_Z( ZSTD_initCStream_srcSize(zc, 1, CNBufferSize) ); |
Yann Collet | d564faa | 2016-12-18 21:39:15 +0100 | [diff] [blame] | 515 | outBuff.dst = (char*)(compressedBuffer); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 516 | outBuff.size = compressedBufferSize; |
| 517 | outBuff.pos = 0; |
| 518 | inBuff.src = CNBuffer; |
| 519 | inBuff.size = CNBufferSize; |
| 520 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 521 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Nick Terrell | e55da9e | 2019-03-13 14:05:18 -0700 | [diff] [blame] | 522 | CHECK(inBuff.pos != inBuff.size, "Entire input should be consumed"); |
| 523 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 524 | CHECK(r != 0, "Error or some data not flushed (ret=%zu)", r); |
| 525 | } |
| 526 | { unsigned long long origSize = ZSTD_findDecompressedSize(outBuff.dst, outBuff.pos); |
| 527 | CHECK(origSize == ZSTD_CONTENTSIZE_UNKNOWN, "Unknown!"); |
| 528 | CHECK((size_t)origSize != CNBufferSize, "Exact original size must be present (got %llu)", origSize); |
| 529 | } |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 530 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 531 | |
| 532 | /* wrong _srcSize compression test */ |
Nick Terrell | 48acadd | 2018-02-01 12:04:05 -0800 | [diff] [blame] | 533 | DISPLAYLEVEL(3, "test%3i : too large srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1); |
| 534 | ZSTD_initCStream_srcSize(zc, 1, CNBufferSize+1); |
Yann Collet | d564faa | 2016-12-18 21:39:15 +0100 | [diff] [blame] | 535 | outBuff.dst = (char*)(compressedBuffer); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 536 | outBuff.size = compressedBufferSize; |
| 537 | outBuff.pos = 0; |
| 538 | inBuff.src = CNBuffer; |
| 539 | inBuff.size = CNBufferSize; |
| 540 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 541 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 542 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 543 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 544 | if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 545 | DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); } |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 546 | |
Nick Terrell | 48acadd | 2018-02-01 12:04:05 -0800 | [diff] [blame] | 547 | /* wrong _srcSize compression test */ |
| 548 | DISPLAYLEVEL(3, "test%3i : too small srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1); |
| 549 | ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1); |
| 550 | outBuff.dst = (char*)(compressedBuffer); |
| 551 | outBuff.size = compressedBufferSize; |
| 552 | outBuff.pos = 0; |
| 553 | inBuff.src = CNBuffer; |
| 554 | inBuff.size = CNBufferSize; |
| 555 | inBuff.pos = 0; |
| 556 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 557 | if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */ |
| 558 | DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); |
| 559 | } |
| 560 | |
Nick Terrell | 3c3f59e | 2018-04-12 16:02:03 -0700 | [diff] [blame] | 561 | DISPLAYLEVEL(3, "test%3i : wrong srcSize !contentSizeFlag : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1); |
| 562 | { ZSTD_parameters params = ZSTD_getParams(1, CNBufferSize, 0); |
| 563 | params.fParams.contentSizeFlag = 0; |
| 564 | CHECK_Z(ZSTD_initCStream_advanced(zc, NULL, 0, params, CNBufferSize - MIN(CNBufferSize, 200 KB))); |
| 565 | outBuff.dst = (char*)compressedBuffer; |
| 566 | outBuff.size = compressedBufferSize; |
| 567 | outBuff.pos = 0; |
| 568 | inBuff.src = CNBuffer; |
| 569 | inBuff.size = CNBufferSize; |
| 570 | inBuff.pos = 0; |
| 571 | { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff); |
| 572 | if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */ |
| 573 | DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); |
| 574 | } } |
| 575 | |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 576 | /* Complex context re-use scenario */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 577 | DISPLAYLEVEL(3, "test%3i : context re-use : ", testNb++); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 578 | ZSTD_freeCStream(zc); |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 579 | zc = ZSTD_createCStream(); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 580 | if (zc==NULL) goto _output_error; /* memory allocation issue */ |
| 581 | /* use 1 */ |
| 582 | { size_t const inSize = 513; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 583 | DISPLAYLEVEL(5, "use1 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 584 | ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */ |
| 585 | inBuff.src = CNBuffer; |
| 586 | inBuff.size = inSize; |
| 587 | inBuff.pos = 0; |
| 588 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 589 | outBuff.size = ZSTD_compressBound(inSize); |
| 590 | outBuff.pos = 0; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 591 | DISPLAYLEVEL(5, "compress1 "); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 592 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 593 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 594 | DISPLAYLEVEL(5, "end1 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 595 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 596 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 597 | } |
| 598 | /* use 2 */ |
| 599 | { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */ |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 600 | DISPLAYLEVEL(5, "use2 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 601 | ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */ |
| 602 | inBuff.src = CNBuffer; |
| 603 | inBuff.size = inSize; |
| 604 | inBuff.pos = 0; |
| 605 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 606 | outBuff.size = ZSTD_compressBound(inSize); |
| 607 | outBuff.pos = 0; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 608 | DISPLAYLEVEL(5, "compress2 "); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 609 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 610 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 611 | DISPLAYLEVEL(5, "end2 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 612 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 613 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 614 | } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 615 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 616 | |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 617 | /* CDict scenario */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 618 | DISPLAYLEVEL(3, "test%3i : digested dictionary : ", testNb++); |
Yann Collet | 2e42742 | 2017-06-27 17:09:12 -0700 | [diff] [blame] | 619 | { ZSTD_CDict* const cdict = ZSTD_createCDict(dictionary.start, dictionary.filled, 1 /*byRef*/ ); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 620 | size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 621 | DISPLAYLEVEL(5, "ZSTD_initCStream_usingCDict result : %u ", (unsigned)initError); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 622 | if (ZSTD_isError(initError)) goto _output_error; |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 623 | outBuff.dst = compressedBuffer; |
| 624 | outBuff.size = compressedBufferSize; |
| 625 | outBuff.pos = 0; |
| 626 | inBuff.src = CNBuffer; |
| 627 | inBuff.size = CNBufferSize; |
| 628 | inBuff.pos = 0; |
Yann Collet | 15768ca | 2017-11-16 15:02:28 -0800 | [diff] [blame] | 629 | DISPLAYLEVEL(5, "- starting ZSTD_compressStream "); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 630 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 631 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
Yann Collet | 15768ca | 2017-11-16 15:02:28 -0800 | [diff] [blame] | 632 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 633 | DISPLAYLEVEL(5, "- ZSTD_endStream result : %u ", (unsigned)r); |
Yann Collet | 15768ca | 2017-11-16 15:02:28 -0800 | [diff] [blame] | 634 | if (r != 0) goto _output_error; /* error, or some data not flushed */ |
| 635 | } |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 636 | cSize = outBuff.pos; |
| 637 | ZSTD_freeCDict(cdict); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 638 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBufferSize*100); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 641 | DISPLAYLEVEL(3, "test%3i : check CStream size : ", testNb++); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 642 | { size_t const s = ZSTD_sizeof_CStream(zc); |
| 643 | if (ZSTD_isError(s)) goto _output_error; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 644 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (unsigned)s); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 645 | } |
| 646 | |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 647 | DISPLAYLEVEL(4, "test%3i : check Dictionary ID : ", testNb++); |
| 648 | { unsigned const dID = ZSTD_getDictID_fromFrame(compressedBuffer, cSize); |
| 649 | if (dID != dictID) goto _output_error; |
| 650 | DISPLAYLEVEL(4, "OK (%u) \n", dID); |
| 651 | } |
| 652 | |
Yann Collet | 335ad5d | 2016-10-25 17:47:02 -0700 | [diff] [blame] | 653 | /* DDict scenario */ |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 654 | DISPLAYLEVEL(3, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (unsigned)CNBufferSize); |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 655 | { ZSTD_DDict* const ddict = ZSTD_createDDict(dictionary.start, dictionary.filled); |
Yann Collet | 335ad5d | 2016-10-25 17:47:02 -0700 | [diff] [blame] | 656 | size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict); |
| 657 | if (ZSTD_isError(initError)) goto _output_error; |
Yann Collet | 8a104fd | 2017-12-12 12:51:49 -0800 | [diff] [blame] | 658 | outBuff.dst = decodedBuffer; |
| 659 | outBuff.size = CNBufferSize; |
| 660 | outBuff.pos = 0; |
Yann Collet | 03832b7 | 2017-12-12 14:01:54 -0800 | [diff] [blame] | 661 | inBuff.src = compressedBuffer; |
| 662 | inBuff.size = cSize; |
| 663 | inBuff.pos = 0; |
Yann Collet | 335ad5d | 2016-10-25 17:47:02 -0700 | [diff] [blame] | 664 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 665 | if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
| 666 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 667 | if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */ |
| 668 | ZSTD_freeDDict(ddict); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 669 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 335ad5d | 2016-10-25 17:47:02 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 672 | /* Memory restriction */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 673 | DISPLAYLEVEL(3, "test%3i : maxWindowSize < frame requirement : ", testNb++); |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 674 | ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
Yann Collet | 3e042d5 | 2018-12-04 17:30:58 -0800 | [diff] [blame] | 675 | CHECK_Z( ZSTD_DCtx_setParameter(zd, ZSTD_d_windowLogMax, 10) ); /* too small limit */ |
Yann Collet | 8a104fd | 2017-12-12 12:51:49 -0800 | [diff] [blame] | 676 | outBuff.dst = decodedBuffer; |
| 677 | outBuff.size = CNBufferSize; |
| 678 | outBuff.pos = 0; |
Yann Collet | 03832b7 | 2017-12-12 14:01:54 -0800 | [diff] [blame] | 679 | inBuff.src = compressedBuffer; |
| 680 | inBuff.size = cSize; |
| 681 | inBuff.pos = 0; |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 682 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 683 | if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 684 | DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); } |
Yann Collet | 5c68639 | 2018-11-15 16:12:39 -0800 | [diff] [blame] | 685 | ZSTD_DCtx_reset(zd, ZSTD_reset_session_and_parameters); /* leave zd in good shape for next tests */ |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 686 | |
Nick Terrell | ab3346a | 2018-01-30 13:30:30 -0800 | [diff] [blame] | 687 | DISPLAYLEVEL(3, "test%3i : dictionary source size and level : ", testNb++); |
| 688 | { ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
Yann Collet | 6cda8c9 | 2018-03-20 16:16:13 -0700 | [diff] [blame] | 689 | int const maxLevel = 16; /* first level with zstd_opt */ |
Nick Terrell | ab3346a | 2018-01-30 13:30:30 -0800 | [diff] [blame] | 690 | int level; |
Yann Collet | 2af4159 | 2018-03-20 15:59:25 -0700 | [diff] [blame] | 691 | assert(maxLevel < ZSTD_maxCLevel()); |
| 692 | CHECK_Z( ZSTD_DCtx_loadDictionary_byReference(dctx, dictionary.start, dictionary.filled) ); |
Nick Terrell | ab3346a | 2018-01-30 13:30:30 -0800 | [diff] [blame] | 693 | for (level = 1; level <= maxLevel; ++level) { |
| 694 | ZSTD_CDict* const cdict = ZSTD_createCDict(dictionary.start, dictionary.filled, level); |
Yann Collet | 2af4159 | 2018-03-20 15:59:25 -0700 | [diff] [blame] | 695 | size_t const maxSize = MIN(1 MB, CNBufferSize); |
| 696 | size_t size; |
Nick Terrell | ab3346a | 2018-01-30 13:30:30 -0800 | [diff] [blame] | 697 | for (size = 512; size <= maxSize; size <<= 1) { |
Yann Collet | 2af4159 | 2018-03-20 15:59:25 -0700 | [diff] [blame] | 698 | U64 const crcOrig = XXH64(CNBuffer, size, 0); |
Nick Terrell | ab3346a | 2018-01-30 13:30:30 -0800 | [diff] [blame] | 699 | ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
Nick Terrell | ca77822 | 2018-04-25 16:32:29 -0700 | [diff] [blame] | 700 | ZSTD_parameters savedParams; |
| 701 | getCCtxParams(cctx, &savedParams); |
Nick Terrell | ab3346a | 2018-01-30 13:30:30 -0800 | [diff] [blame] | 702 | outBuff.dst = compressedBuffer; |
| 703 | outBuff.size = compressedBufferSize; |
| 704 | outBuff.pos = 0; |
| 705 | inBuff.src = CNBuffer; |
| 706 | inBuff.size = size; |
| 707 | inBuff.pos = 0; |
| 708 | CHECK_Z(ZSTD_CCtx_refCDict(cctx, cdict)); |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 709 | CHECK_Z(ZSTD_compressStream2(cctx, &outBuff, &inBuff, ZSTD_e_end)); |
Nick Terrell | ca77822 | 2018-04-25 16:32:29 -0700 | [diff] [blame] | 710 | CHECK(badParameters(cctx, savedParams), "Bad CCtx params"); |
Nick Terrell | ab3346a | 2018-01-30 13:30:30 -0800 | [diff] [blame] | 711 | if (inBuff.pos != inBuff.size) goto _output_error; |
Yann Collet | 2af4159 | 2018-03-20 15:59:25 -0700 | [diff] [blame] | 712 | { ZSTD_outBuffer decOut = {decodedBuffer, size, 0}; |
| 713 | ZSTD_inBuffer decIn = {outBuff.dst, outBuff.pos, 0}; |
Yann Collet | 34e146f | 2018-12-04 10:28:36 -0800 | [diff] [blame] | 714 | CHECK_Z( ZSTD_decompressStream(dctx, &decOut, &decIn) ); |
Yann Collet | 2af4159 | 2018-03-20 15:59:25 -0700 | [diff] [blame] | 715 | if (decIn.pos != decIn.size) goto _output_error; |
| 716 | if (decOut.pos != size) goto _output_error; |
| 717 | { U64 const crcDec = XXH64(decOut.dst, decOut.pos, 0); |
| 718 | if (crcDec != crcOrig) goto _output_error; |
| 719 | } } |
Nick Terrell | ab3346a | 2018-01-30 13:30:30 -0800 | [diff] [blame] | 720 | ZSTD_freeCCtx(cctx); |
| 721 | } |
| 722 | ZSTD_freeCDict(cdict); |
| 723 | } |
Nick Terrell | ab3346a | 2018-01-30 13:30:30 -0800 | [diff] [blame] | 724 | ZSTD_freeDCtx(dctx); |
| 725 | } |
| 726 | DISPLAYLEVEL(3, "OK\n"); |
| 727 | |
Nick Terrell | 50b9c41 | 2019-04-10 12:34:21 -0700 | [diff] [blame] | 728 | ZSTD_CCtx_reset(zc, ZSTD_reset_session_and_parameters); |
| 729 | CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dictionary.start, dictionary.filled) ); |
| 730 | cSize = ZSTD_compress2(zc, compressedBuffer, compressedBufferSize, CNBuffer, MIN(CNBufferSize, 100 KB)); |
| 731 | CHECK_Z(cSize); |
| 732 | DISPLAYLEVEL(3, "test%3i : ZSTD_decompressStream() with dictionary : ", testNb++); |
| 733 | { |
| 734 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
| 735 | /* We should fail to decompress without a dictionary. */ |
| 736 | ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters); |
| 737 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 738 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 739 | size_t const ret = ZSTD_decompressStream(dctx, &out, &in); |
| 740 | if (!ZSTD_isError(ret)) goto _output_error; |
| 741 | } |
| 742 | /* We should succeed to decompress with the dictionary. */ |
| 743 | ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters); |
| 744 | CHECK_Z( ZSTD_DCtx_loadDictionary(dctx, dictionary.start, dictionary.filled) ); |
| 745 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 746 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 747 | if (ZSTD_decompressStream(dctx, &out, &in) != 0) goto _output_error; |
| 748 | if (in.pos != in.size) goto _output_error; |
| 749 | } |
| 750 | /* The dictionary should presist across calls. */ |
| 751 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 752 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 753 | if (ZSTD_decompressStream(dctx, &out, &in) != 0) goto _output_error; |
| 754 | if (in.pos != in.size) goto _output_error; |
| 755 | } |
| 756 | /* The dictionary should not be cleared by ZSTD_reset_session_only. */ |
| 757 | ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only); |
| 758 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 759 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 760 | if (ZSTD_decompressStream(dctx, &out, &in) != 0) goto _output_error; |
| 761 | if (in.pos != in.size) goto _output_error; |
| 762 | } |
| 763 | /* When we reset the context the dictionary is cleared. */ |
| 764 | ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters); |
| 765 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 766 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 767 | size_t const ret = ZSTD_decompressStream(dctx, &out, &in); |
| 768 | if (!ZSTD_isError(ret)) goto _output_error; |
| 769 | } |
| 770 | ZSTD_freeDCtx(dctx); |
| 771 | } |
| 772 | DISPLAYLEVEL(3, "OK \n"); |
| 773 | |
Josh Soref | a880ca2 | 2019-04-12 14:18:11 -0400 | [diff] [blame] | 774 | DISPLAYLEVEL(3, "test%3i : ZSTD_resetDStream() with dictionary : ", testNb++); |
Nick Terrell | 50b9c41 | 2019-04-10 12:34:21 -0700 | [diff] [blame] | 775 | { |
| 776 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
| 777 | /* We should succeed to decompress with the dictionary. */ |
| 778 | ZSTD_resetDStream(dctx); |
| 779 | CHECK_Z( ZSTD_DCtx_loadDictionary(dctx, dictionary.start, dictionary.filled) ); |
| 780 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 781 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 782 | if (ZSTD_decompressStream(dctx, &out, &in) != 0) goto _output_error; |
| 783 | if (in.pos != in.size) goto _output_error; |
| 784 | } |
| 785 | /* The dictionary should not be cleared by ZSTD_resetDStream(). */ |
| 786 | ZSTD_resetDStream(dctx); |
| 787 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 788 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 789 | if (ZSTD_decompressStream(dctx, &out, &in) != 0) goto _output_error; |
| 790 | if (in.pos != in.size) goto _output_error; |
| 791 | } |
| 792 | /* The dictionary should be cleared by ZSTD_initDStream(). */ |
| 793 | CHECK_Z( ZSTD_initDStream(dctx) ); |
| 794 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 795 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 796 | size_t const ret = ZSTD_decompressStream(dctx, &out, &in); |
| 797 | if (!ZSTD_isError(ret)) goto _output_error; |
| 798 | } |
| 799 | ZSTD_freeDCtx(dctx); |
| 800 | } |
| 801 | DISPLAYLEVEL(3, "OK \n"); |
| 802 | |
| 803 | DISPLAYLEVEL(3, "test%3i : ZSTD_decompressStream() with ddict : ", testNb++); |
| 804 | { |
| 805 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
| 806 | ZSTD_DDict* ddict = ZSTD_createDDict(dictionary.start, dictionary.filled); |
| 807 | /* We should succeed to decompress with the ddict. */ |
| 808 | ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters); |
| 809 | CHECK_Z( ZSTD_DCtx_refDDict(dctx, ddict) ); |
| 810 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 811 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 812 | if (ZSTD_decompressStream(dctx, &out, &in) != 0) goto _output_error; |
| 813 | if (in.pos != in.size) goto _output_error; |
| 814 | } |
| 815 | /* The ddict should presist across calls. */ |
| 816 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 817 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 818 | if (ZSTD_decompressStream(dctx, &out, &in) != 0) goto _output_error; |
| 819 | if (in.pos != in.size) goto _output_error; |
| 820 | } |
| 821 | /* When we reset the context the ddict is cleared. */ |
| 822 | ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters); |
| 823 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 824 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 825 | size_t const ret = ZSTD_decompressStream(dctx, &out, &in); |
| 826 | if (!ZSTD_isError(ret)) goto _output_error; |
| 827 | } |
| 828 | ZSTD_freeDCtx(dctx); |
| 829 | ZSTD_freeDDict(ddict); |
| 830 | } |
| 831 | DISPLAYLEVEL(3, "OK \n"); |
| 832 | |
| 833 | DISPLAYLEVEL(3, "test%3i : ZSTD_decompressDCtx() with prefix : ", testNb++); |
| 834 | { |
| 835 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
| 836 | /* We should succeed to decompress with the prefix. */ |
| 837 | ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters); |
| 838 | CHECK_Z( ZSTD_DCtx_refPrefix_advanced(dctx, dictionary.start, dictionary.filled, ZSTD_dct_auto) ); |
| 839 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 840 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 841 | if (ZSTD_decompressStream(dctx, &out, &in) != 0) goto _output_error; |
| 842 | if (in.pos != in.size) goto _output_error; |
| 843 | } |
| 844 | /* The prefix should be cleared after the first compression. */ |
| 845 | { ZSTD_outBuffer out = {decodedBuffer, decodedBufferSize, 0}; |
| 846 | ZSTD_inBuffer in = {compressedBuffer, cSize, 0}; |
| 847 | size_t const ret = ZSTD_decompressStream(dctx, &out, &in); |
| 848 | if (!ZSTD_isError(ret)) goto _output_error; |
| 849 | } |
| 850 | ZSTD_freeDCtx(dctx); |
| 851 | } |
| 852 | DISPLAYLEVEL(3, "OK \n"); |
| 853 | |
| 854 | DISPLAYLEVEL(3, "test%3i : ZSTD_initDStream*() with dictionary : ", testNb++); |
| 855 | { |
| 856 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
| 857 | ZSTD_DDict* ddict = ZSTD_createDDict(dictionary.start, dictionary.filled); |
| 858 | size_t ret; |
| 859 | /* We should succeed to decompress with the dictionary. */ |
| 860 | CHECK_Z( ZSTD_initDStream_usingDict(dctx, dictionary.start, dictionary.filled) ); |
| 861 | CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, decodedBufferSize, compressedBuffer, cSize) ); |
| 862 | /* The dictionary should presist across calls. */ |
| 863 | CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, decodedBufferSize, compressedBuffer, cSize) ); |
| 864 | /* We should succeed to decompress with the ddict. */ |
| 865 | CHECK_Z( ZSTD_initDStream_usingDDict(dctx, ddict) ); |
| 866 | CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, decodedBufferSize, compressedBuffer, cSize) ); |
| 867 | /* The ddict should presist across calls. */ |
| 868 | CHECK_Z( ZSTD_decompressDCtx(dctx, decodedBuffer, decodedBufferSize, compressedBuffer, cSize) ); |
| 869 | /* When we reset the context the ddict is cleared. */ |
| 870 | CHECK_Z( ZSTD_initDStream(dctx) ); |
| 871 | ret = ZSTD_decompressDCtx(dctx, decodedBuffer, decodedBufferSize, compressedBuffer, cSize); |
| 872 | if (!ZSTD_isError(ret)) goto _output_error; |
| 873 | ZSTD_freeDCtx(dctx); |
| 874 | ZSTD_freeDDict(ddict); |
| 875 | } |
| 876 | DISPLAYLEVEL(3, "OK \n"); |
| 877 | |
Yann Collet | 7d283cd | 2017-04-27 14:48:34 -0700 | [diff] [blame] | 878 | DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_usingCDict_advanced with masked dictID : ", testNb++); |
| 879 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictionary.filled); |
| 880 | ZSTD_frameParameters const fParams = { 1 /* contentSize */, 1 /* checksum */, 1 /* noDictID */}; |
Yann Collet | 6873fec | 2018-03-20 15:13:14 -0700 | [diff] [blame] | 881 | ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dct_auto, cParams, ZSTD_defaultCMem); |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 882 | size_t const initError = ZSTD_initCStream_usingCDict_advanced(zc, cdict, fParams, CNBufferSize); |
Yann Collet | 7d283cd | 2017-04-27 14:48:34 -0700 | [diff] [blame] | 883 | if (ZSTD_isError(initError)) goto _output_error; |
Nick Terrell | 62ecad3 | 2017-04-03 20:56:39 -0700 | [diff] [blame] | 884 | outBuff.dst = compressedBuffer; |
| 885 | outBuff.size = compressedBufferSize; |
| 886 | outBuff.pos = 0; |
| 887 | inBuff.src = CNBuffer; |
| 888 | inBuff.size = CNBufferSize; |
| 889 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 890 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 891 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
Nick Terrell | 62ecad3 | 2017-04-03 20:56:39 -0700 | [diff] [blame] | 892 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 893 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 894 | cSize = outBuff.pos; |
| 895 | ZSTD_freeCDict(cdict); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 896 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBufferSize*100); |
Yann Collet | 4ee6b15 | 2017-04-11 11:59:44 -0700 | [diff] [blame] | 897 | } |
Nick Terrell | 62ecad3 | 2017-04-03 20:56:39 -0700 | [diff] [blame] | 898 | |
Yann Collet | a92cbb7 | 2017-04-27 15:08:56 -0700 | [diff] [blame] | 899 | DISPLAYLEVEL(3, "test%3i : try retrieving dictID from frame : ", testNb++); |
| 900 | { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize); |
| 901 | if (did != 0) goto _output_error; |
| 902 | } |
| 903 | DISPLAYLEVEL(3, "OK (not detected) \n"); |
| 904 | |
Yann Collet | 4ee6b15 | 2017-04-11 11:59:44 -0700 | [diff] [blame] | 905 | DISPLAYLEVEL(3, "test%3i : decompress without dictionary : ", testNb++); |
| 906 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize); |
| 907 | if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */ |
| 908 | DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); |
Nick Terrell | 62ecad3 | 2017-04-03 20:56:39 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 911 | DISPLAYLEVEL(3, "test%3i : compress with ZSTD_CCtx_refPrefix : ", testNb++); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 912 | CHECK_Z( ZSTD_CCtx_refPrefix(zc, dictionary.start, dictionary.filled) ); |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 913 | outBuff.dst = compressedBuffer; |
| 914 | outBuff.size = compressedBufferSize; |
| 915 | outBuff.pos = 0; |
| 916 | inBuff.src = CNBuffer; |
| 917 | inBuff.size = CNBufferSize; |
| 918 | inBuff.pos = 0; |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 919 | CHECK_Z( ZSTD_compressStream2(zc, &outBuff, &inBuff, ZSTD_e_end) ); |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 920 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 921 | cSize = outBuff.pos; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 922 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBufferSize*100); |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 923 | |
Yann Collet | 6cda8c9 | 2018-03-20 16:16:13 -0700 | [diff] [blame] | 924 | DISPLAYLEVEL(3, "test%3i : decompress with ZSTD_DCtx_refPrefix : ", testNb++); |
| 925 | CHECK_Z( ZSTD_DCtx_refPrefix(zd, dictionary.start, dictionary.filled) ); |
| 926 | outBuff.dst = decodedBuffer; |
| 927 | outBuff.size = CNBufferSize; |
| 928 | outBuff.pos = 0; |
| 929 | inBuff.src = compressedBuffer; |
| 930 | inBuff.size = cSize; |
| 931 | inBuff.pos = 0; |
Yann Collet | 34e146f | 2018-12-04 10:28:36 -0800 | [diff] [blame] | 932 | CHECK_Z( ZSTD_decompressStream(zd, &outBuff, &inBuff) ); |
Yann Collet | 6cda8c9 | 2018-03-20 16:16:13 -0700 | [diff] [blame] | 933 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 934 | if (outBuff.pos != CNBufferSize) goto _output_error; /* must regenerate whole input */ |
| 935 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 936 | |
| 937 | DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should fail): ", testNb++); |
| 938 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize); |
| 939 | if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */ |
| 940 | DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); |
| 941 | } |
| 942 | |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 943 | DISPLAYLEVEL(3, "test%3i : compress again with ZSTD_compressStream2 : ", testNb++); |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 944 | outBuff.dst = compressedBuffer; |
| 945 | outBuff.size = compressedBufferSize; |
| 946 | outBuff.pos = 0; |
| 947 | inBuff.src = CNBuffer; |
| 948 | inBuff.size = CNBufferSize; |
| 949 | inBuff.pos = 0; |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 950 | CHECK_Z( ZSTD_compressStream2(zc, &outBuff, &inBuff, ZSTD_e_end) ); |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 951 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 952 | cSize = outBuff.pos; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 953 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (unsigned)cSize, (double)cSize/CNBufferSize*100); |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 954 | |
| 955 | DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should work): ", testNb++); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 956 | CHECK_Z( ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize) ); |
| 957 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 958 | |
Yann Collet | 0bb381d | 2017-04-18 15:08:52 -0700 | [diff] [blame] | 959 | /* Empty srcSize */ |
| 960 | DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_advanced with pledgedSrcSize=0 and dict : ", testNb++); |
| 961 | { ZSTD_parameters params = ZSTD_getParams(5, 0, 0); |
| 962 | params.fParams.contentSizeFlag = 1; |
Yann Collet | 213ef3b | 2017-10-13 19:01:58 -0700 | [diff] [blame] | 963 | CHECK_Z( ZSTD_initCStream_advanced(zc, dictionary.start, dictionary.filled, params, 0 /* pledgedSrcSize==0 means "empty" when params.fParams.contentSizeFlag is set */) ); |
Yann Collet | 0bb381d | 2017-04-18 15:08:52 -0700 | [diff] [blame] | 964 | } /* cstream advanced shall write content size = 0 */ |
Yann Collet | 8a104fd | 2017-12-12 12:51:49 -0800 | [diff] [blame] | 965 | outBuff.dst = compressedBuffer; |
| 966 | outBuff.size = compressedBufferSize; |
| 967 | outBuff.pos = 0; |
Yann Collet | 03832b7 | 2017-12-12 14:01:54 -0800 | [diff] [blame] | 968 | inBuff.src = CNBuffer; |
| 969 | inBuff.size = 0; |
| 970 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 971 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 0bb381d | 2017-04-18 15:08:52 -0700 | [diff] [blame] | 972 | if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error; |
| 973 | cSize = outBuff.pos; |
| 974 | if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error; |
| 975 | DISPLAYLEVEL(3, "OK \n"); |
| 976 | |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 977 | DISPLAYLEVEL(3, "test%3i : pledgedSrcSize == 0 behaves properly : ", testNb++); |
| 978 | { ZSTD_parameters params = ZSTD_getParams(5, 0, 0); |
| 979 | params.fParams.contentSizeFlag = 1; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 980 | CHECK_Z( ZSTD_initCStream_advanced(zc, NULL, 0, params, 0) ); |
Yann Collet | 4ee6b15 | 2017-04-11 11:59:44 -0700 | [diff] [blame] | 981 | } /* cstream advanced shall write content size = 0 */ |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 982 | inBuff.src = CNBuffer; |
| 983 | inBuff.size = 0; |
| 984 | inBuff.pos = 0; |
| 985 | outBuff.dst = compressedBuffer; |
| 986 | outBuff.size = compressedBufferSize; |
| 987 | outBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 988 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 989 | if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error; |
| 990 | cSize = outBuff.pos; |
| 991 | if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error; |
| 992 | |
| 993 | ZSTD_resetCStream(zc, 0); /* resetCStream should treat 0 as unknown */ |
Yann Collet | 8a104fd | 2017-12-12 12:51:49 -0800 | [diff] [blame] | 994 | outBuff.dst = compressedBuffer; |
| 995 | outBuff.size = compressedBufferSize; |
| 996 | outBuff.pos = 0; |
Yann Collet | 03832b7 | 2017-12-12 14:01:54 -0800 | [diff] [blame] | 997 | inBuff.src = CNBuffer; |
| 998 | inBuff.size = 0; |
| 999 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 1000 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 1001 | if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error; |
| 1002 | cSize = outBuff.pos; |
| 1003 | if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error; |
| 1004 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 1005 | |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 1006 | /* Basic multithreading compression test */ |
| 1007 | DISPLAYLEVEL(3, "test%3i : compress %u bytes with multiple threads : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 1008 | { ZSTD_parameters const params = ZSTD_getParams(1, 0, 0); |
Yann Collet | 96adc84 | 2018-12-13 16:50:19 -0800 | [diff] [blame] | 1009 | int jobSize; |
Nick Terrell | b426bcc | 2018-06-25 15:21:08 -0700 | [diff] [blame] | 1010 | CHECK_Z( ZSTDMT_getMTCtxParameter(mtctx, ZSTDMT_p_jobSize, &jobSize)); |
| 1011 | CHECK(jobSize != 0, "job size non-zero"); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 1012 | CHECK_Z( ZSTDMT_initCStream_advanced(mtctx, CNBuffer, dictSize, params, CNBufferSize) ); |
Nick Terrell | b426bcc | 2018-06-25 15:21:08 -0700 | [diff] [blame] | 1013 | CHECK_Z( ZSTDMT_getMTCtxParameter(mtctx, ZSTDMT_p_jobSize, &jobSize)); |
| 1014 | CHECK(jobSize != 0, "job size non-zero"); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 1015 | } |
Yann Collet | 03832b7 | 2017-12-12 14:01:54 -0800 | [diff] [blame] | 1016 | outBuff.dst = compressedBuffer; |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 1017 | outBuff.size = compressedBufferSize; |
| 1018 | outBuff.pos = 0; |
| 1019 | inBuff.src = CNBuffer; |
| 1020 | inBuff.size = CNBufferSize; |
| 1021 | inBuff.pos = 0; |
Yann Collet | 6711396 | 2018-01-19 22:11:11 -0800 | [diff] [blame] | 1022 | { size_t const compressResult = ZSTDMT_compressStream_generic(mtctx, &outBuff, &inBuff, ZSTD_e_end); |
| 1023 | if (compressResult != 0) goto _output_error; /* compression must be completed in a single round */ |
| 1024 | } |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 1025 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
Yann Collet | 6711396 | 2018-01-19 22:11:11 -0800 | [diff] [blame] | 1026 | { size_t const compressedSize = ZSTD_findFrameCompressedSize(compressedBuffer, outBuff.pos); |
| 1027 | if (compressedSize != outBuff.pos) goto _output_error; /* must be a full valid frame */ |
| 1028 | } |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 1029 | DISPLAYLEVEL(3, "OK \n"); |
| 1030 | |
Yann Collet | 03832b7 | 2017-12-12 14:01:54 -0800 | [diff] [blame] | 1031 | /* Complex multithreading + dictionary test */ |
Yann Collet | 209df52 | 2018-02-01 19:29:30 -0800 | [diff] [blame] | 1032 | { U32 const nbWorkers = 2; |
Yann Collet | 03832b7 | 2017-12-12 14:01:54 -0800 | [diff] [blame] | 1033 | size_t const jobSize = 4 * 1 MB; |
Yann Collet | 209df52 | 2018-02-01 19:29:30 -0800 | [diff] [blame] | 1034 | size_t const srcSize = jobSize * nbWorkers; /* we want each job to have predictable size */ |
Yann Collet | 03832b7 | 2017-12-12 14:01:54 -0800 | [diff] [blame] | 1035 | size_t const segLength = 2 KB; |
| 1036 | size_t const offset = 600 KB; /* must be larger than window defined in cdict */ |
| 1037 | size_t const start = jobSize + (offset-1); |
| 1038 | const BYTE* const srcToCopy = (const BYTE*)CNBuffer + start; |
| 1039 | BYTE* const dst = (BYTE*)CNBuffer + start - offset; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1040 | DISPLAYLEVEL(3, "test%3i : compress %u bytes with multiple threads + dictionary : ", testNb++, (unsigned)srcSize); |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 1041 | CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_c_compressionLevel, 3) ); |
| 1042 | CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_c_nbWorkers, nbWorkers) ); |
| 1043 | CHECK_Z( ZSTD_CCtx_setParameter(zc, ZSTD_c_jobSize, jobSize) ); |
Yann Collet | 03832b7 | 2017-12-12 14:01:54 -0800 | [diff] [blame] | 1044 | assert(start > offset); |
| 1045 | assert(start + segLength < COMPRESSIBLE_NOISE_LENGTH); |
| 1046 | memcpy(dst, srcToCopy, segLength); /* create a long repetition at long distance for job 2 */ |
| 1047 | outBuff.dst = compressedBuffer; |
| 1048 | outBuff.size = compressedBufferSize; |
| 1049 | outBuff.pos = 0; |
| 1050 | inBuff.src = CNBuffer; |
| 1051 | inBuff.size = srcSize; assert(srcSize < COMPRESSIBLE_NOISE_LENGTH); |
| 1052 | inBuff.pos = 0; |
| 1053 | } |
Josh Soref | a880ca2 | 2019-04-12 14:18:11 -0400 | [diff] [blame] | 1054 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, 4 KB, dictionary.filled); /* intentionally lies on estimatedSrcSize, to push cdict into targeting a small window size */ |
Yann Collet | 6873fec | 2018-03-20 15:13:14 -0700 | [diff] [blame] | 1055 | ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dct_fullDict, cParams, ZSTD_defaultCMem); |
Yann Collet | 03832b7 | 2017-12-12 14:01:54 -0800 | [diff] [blame] | 1056 | DISPLAYLEVEL(5, "cParams.windowLog = %u : ", cParams.windowLog); |
Yann Collet | d23eb9a | 2017-12-13 15:35:49 -0800 | [diff] [blame] | 1057 | CHECK_Z( ZSTD_CCtx_refCDict(zc, cdict) ); |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 1058 | CHECK_Z( ZSTD_compressStream2(zc, &outBuff, &inBuff, ZSTD_e_end) ); |
Yann Collet | d23eb9a | 2017-12-13 15:35:49 -0800 | [diff] [blame] | 1059 | CHECK_Z( ZSTD_CCtx_refCDict(zc, NULL) ); /* do not keep a reference to cdict, as its lifetime ends */ |
Yann Collet | 03832b7 | 2017-12-12 14:01:54 -0800 | [diff] [blame] | 1060 | ZSTD_freeCDict(cdict); |
| 1061 | } |
| 1062 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 1063 | cSize = outBuff.pos; |
| 1064 | DISPLAYLEVEL(3, "OK \n"); |
| 1065 | |
| 1066 | DISPLAYLEVEL(3, "test%3i : decompress large frame created from multiple threads + dictionary : ", testNb++); |
| 1067 | { ZSTD_DStream* const dstream = ZSTD_createDCtx(); |
| 1068 | ZSTD_frameHeader zfh; |
| 1069 | ZSTD_getFrameHeader(&zfh, compressedBuffer, cSize); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1070 | DISPLAYLEVEL(5, "frame windowsize = %u : ", (unsigned)zfh.windowSize); |
Yann Collet | 03832b7 | 2017-12-12 14:01:54 -0800 | [diff] [blame] | 1071 | outBuff.dst = decodedBuffer; |
| 1072 | outBuff.size = CNBufferSize; |
| 1073 | outBuff.pos = 0; |
| 1074 | inBuff.src = compressedBuffer; |
| 1075 | inBuff.pos = 0; |
| 1076 | CHECK_Z( ZSTD_initDStream_usingDict(dstream, dictionary.start, dictionary.filled) ); |
| 1077 | inBuff.size = 1; /* avoid shortcut to single-pass mode */ |
| 1078 | CHECK_Z( ZSTD_decompressStream(dstream, &outBuff, &inBuff) ); |
| 1079 | inBuff.size = cSize; |
| 1080 | CHECK_Z( ZSTD_decompressStream(dstream, &outBuff, &inBuff) ); |
| 1081 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 1082 | ZSTD_freeDStream(dstream); |
| 1083 | } |
| 1084 | DISPLAYLEVEL(3, "OK \n"); |
| 1085 | |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 1086 | DISPLAYLEVEL(3, "test%3i : check dictionary FSE tables can represent every code : ", testNb++); |
| 1087 | { unsigned const kMaxWindowLog = 24; |
| 1088 | unsigned value; |
| 1089 | ZSTD_compressionParameters cParams = ZSTD_getCParams(3, 1U << kMaxWindowLog, 1024); |
| 1090 | ZSTD_CDict* cdict; |
| 1091 | ZSTD_DDict* ddict; |
| 1092 | SEQ_stream seq = SEQ_initStream(0x87654321); |
| 1093 | SEQ_gen_type type; |
| 1094 | XXH64_state_t xxh; |
| 1095 | |
| 1096 | XXH64_reset(&xxh, 0); |
| 1097 | cParams.windowLog = kMaxWindowLog; |
Yann Collet | 6873fec | 2018-03-20 15:13:14 -0700 | [diff] [blame] | 1098 | cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dct_fullDict, cParams, ZSTD_defaultCMem); |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 1099 | ddict = ZSTD_createDDict(dictionary.start, dictionary.filled); |
| 1100 | |
| 1101 | if (!cdict || !ddict) goto _output_error; |
| 1102 | |
Yann Collet | 5c68639 | 2018-11-15 16:12:39 -0800 | [diff] [blame] | 1103 | ZSTD_CCtx_reset(zc, ZSTD_reset_session_only); |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 1104 | ZSTD_resetDStream(zd); |
| 1105 | CHECK_Z(ZSTD_CCtx_refCDict(zc, cdict)); |
| 1106 | CHECK_Z(ZSTD_initDStream_usingDDict(zd, ddict)); |
Yann Collet | 3e042d5 | 2018-12-04 17:30:58 -0800 | [diff] [blame] | 1107 | CHECK_Z(ZSTD_DCtx_setParameter(zd, ZSTD_d_windowLogMax, kMaxWindowLog)); |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 1108 | /* Test all values < 300 */ |
| 1109 | for (value = 0; value < 300; ++value) { |
| 1110 | for (type = (SEQ_gen_type)0; type < SEQ_gen_max; ++type) { |
| 1111 | CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value)); |
| 1112 | } |
| 1113 | } |
| 1114 | /* Test values 2^8 to 2^17 */ |
| 1115 | for (value = (1 << 8); value < (1 << 17); value <<= 1) { |
| 1116 | for (type = (SEQ_gen_type)0; type < SEQ_gen_max; ++type) { |
| 1117 | CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value)); |
| 1118 | CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value + (value >> 2))); |
| 1119 | } |
| 1120 | } |
| 1121 | /* Test offset values up to the max window log */ |
| 1122 | for (value = 8; value <= kMaxWindowLog; ++value) { |
| 1123 | CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, SEQ_gen_of, (1U << value) - 1)); |
| 1124 | } |
| 1125 | |
| 1126 | CHECK_Z(SEQ_roundTrip(zc, zd, &xxh, NULL, 0, ZSTD_e_end)); |
| 1127 | CHECK(SEQ_digest(&seq) != XXH64_digest(&xxh), "SEQ XXH64 does not match"); |
| 1128 | |
| 1129 | ZSTD_freeCDict(cdict); |
| 1130 | ZSTD_freeDDict(ddict); |
| 1131 | } |
| 1132 | DISPLAYLEVEL(3, "OK \n"); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 1133 | |
Nick Terrell | 6d222c4 | 2018-07-12 17:56:58 -0700 | [diff] [blame] | 1134 | DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_srcSize sets requestedParams : ", testNb++); |
Yann Collet | d4d4e10 | 2018-11-21 15:37:26 -0800 | [diff] [blame] | 1135 | { int level; |
Nick Terrell | 6d222c4 | 2018-07-12 17:56:58 -0700 | [diff] [blame] | 1136 | CHECK_Z(ZSTD_initCStream_srcSize(zc, 11, ZSTD_CONTENTSIZE_UNKNOWN)); |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 1137 | CHECK_Z(ZSTD_CCtx_getParameter(zc, ZSTD_c_compressionLevel, &level)); |
Nick Terrell | 6d222c4 | 2018-07-12 17:56:58 -0700 | [diff] [blame] | 1138 | CHECK(level != 11, "Compression level does not match"); |
| 1139 | ZSTD_resetCStream(zc, ZSTD_CONTENTSIZE_UNKNOWN); |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 1140 | CHECK_Z(ZSTD_CCtx_getParameter(zc, ZSTD_c_compressionLevel, &level)); |
Nick Terrell | 6d222c4 | 2018-07-12 17:56:58 -0700 | [diff] [blame] | 1141 | CHECK(level != 11, "Compression level does not match"); |
| 1142 | } |
| 1143 | DISPLAYLEVEL(3, "OK \n"); |
| 1144 | |
| 1145 | DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_advanced sets requestedParams : ", testNb++); |
| 1146 | { ZSTD_parameters const params = ZSTD_getParams(9, 0, 0); |
| 1147 | CHECK_Z(ZSTD_initCStream_advanced(zc, NULL, 0, params, ZSTD_CONTENTSIZE_UNKNOWN)); |
| 1148 | CHECK(badParameters(zc, params), "Compression parameters do not match"); |
| 1149 | ZSTD_resetCStream(zc, ZSTD_CONTENTSIZE_UNKNOWN); |
| 1150 | CHECK(badParameters(zc, params), "Compression parameters do not match"); |
| 1151 | } |
| 1152 | DISPLAYLEVEL(3, "OK \n"); |
| 1153 | |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 1154 | /* Overlen overwriting window data bug */ |
| 1155 | DISPLAYLEVEL(3, "test%3i : wildcopy doesn't overwrite potential match data : ", testNb++); |
Sean Purcell | 0ed3901 | 2017-02-16 13:29:47 -0800 | [diff] [blame] | 1156 | { /* This test has a window size of 1024 bytes and consists of 3 blocks: |
| 1157 | 1. 'a' repeated 517 times |
| 1158 | 2. 'b' repeated 516 times |
| 1159 | 3. a compressed block with no literals and 3 sequence commands: |
| 1160 | litlength = 0, offset = 24, match length = 24 |
| 1161 | litlength = 0, offset = 24, match length = 3 (this one creates an overlength write of length 2*WILDCOPY_OVERLENGTH - 3) |
| 1162 | litlength = 0, offset = 1021, match length = 3 (this one will try to read from overwritten data if the buffer is too small) */ |
| 1163 | |
| 1164 | const char* testCase = |
| 1165 | "\x28\xB5\x2F\xFD\x04\x00\x4C\x00\x00\x10\x61\x61\x01\x00\x00\x2A" |
| 1166 | "\x80\x05\x44\x00\x00\x08\x62\x01\x00\x00\x2A\x20\x04\x5D\x00\x00" |
| 1167 | "\x00\x03\x40\x00\x00\x64\x60\x27\xB0\xE0\x0C\x67\x62\xCE\xE0"; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 1168 | ZSTD_DStream* const zds = ZSTD_createDStream(); |
| 1169 | if (zds==NULL) goto _output_error; |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 1170 | |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 1171 | CHECK_Z( ZSTD_initDStream(zds) ); |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 1172 | inBuff.src = testCase; |
Sean Purcell | 0ed3901 | 2017-02-16 13:29:47 -0800 | [diff] [blame] | 1173 | inBuff.size = 47; |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 1174 | inBuff.pos = 0; |
| 1175 | outBuff.dst = decodedBuffer; |
| 1176 | outBuff.size = CNBufferSize; |
| 1177 | outBuff.pos = 0; |
| 1178 | |
| 1179 | while (inBuff.pos < inBuff.size) { |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 1180 | CHECK_Z( ZSTD_decompressStream(zds, &outBuff, &inBuff) ); |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 1181 | } |
Przemyslaw Skibinski | 684858e | 2017-02-21 18:17:24 +0100 | [diff] [blame] | 1182 | |
| 1183 | ZSTD_freeDStream(zds); |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 1184 | } |
| 1185 | DISPLAYLEVEL(3, "OK \n"); |
| 1186 | |
Ephraim Park | 01e8384 | 2019-06-27 17:27:29 -0700 | [diff] [blame] | 1187 | /* Small Sequence Section bug */ |
| 1188 | DISPLAYLEVEL(3, "test%3i : decompress blocks with small sequences section : ", testNb++); |
| 1189 | { /* This test consists of 3 blocks. Each block has one sequence. |
| 1190 | The sequence has literal length of 10, match length of 10 and offset of 10. |
| 1191 | The sequence value and compression mode for the blocks are following: |
| 1192 | The order of values are ll, ml, of. |
| 1193 | - First block : (10, 7, 13) (rle, rle, rle) |
| 1194 | - size of sequences section: 6 bytes (1 byte for nbSeq, 1 byte for encoding mode, 3 bytes for rle, 1 byte bitstream) |
| 1195 | - Second block : (10, 7, 1) (repeat, repeat, rle) |
| 1196 | - size of sequences section: 4 bytes (1 byte for nbSeq, 1 byte for encoding mode, 1 bytes for rle, 1 byte bitstream) |
| 1197 | - Third block : (10, 7, 1) (repeat, repeat, repeat) |
| 1198 | - size of sequences section: 3 bytes (1 byte for nbSeq, 1 byte for encoding mode, 1 byte bitstream) */ |
| 1199 | |
| 1200 | unsigned char compressed[] = { |
| 1201 | 0x28, 0xb5, 0x2f, 0xfd, 0x24, 0x3c, 0x35, 0x01, 0x00, 0xf0, 0x85, 0x08, |
| 1202 | 0xc2, 0xc4, 0x70, 0xcf, 0xd7, 0xc0, 0x96, 0x7e, 0x4c, 0x6b, 0xa9, 0x8b, |
| 1203 | 0xbc, 0xc5, 0xb6, 0xd9, 0x7f, 0x4c, 0xf1, 0x05, 0xa6, 0x54, 0xef, 0xac, |
| 1204 | 0x69, 0x94, 0x89, 0x1c, 0x03, 0x44, 0x0a, 0x07, 0x00, 0xb4, 0x04, 0x80, |
| 1205 | 0x40, 0x0a, 0xa4 |
| 1206 | }; |
| 1207 | unsigned int compressedSize = 51; |
| 1208 | unsigned char decompressed[] = { |
| 1209 | 0x85, 0x08, 0xc2, 0xc4, 0x70, 0xcf, 0xd7, 0xc0, 0x96, 0x7e, 0x85, 0x08, |
| 1210 | 0xc2, 0xc4, 0x70, 0xcf, 0xd7, 0xc0, 0x96, 0x7e, 0x4c, 0x6b, 0xa9, 0x8b, |
| 1211 | 0xbc, 0xc5, 0xb6, 0xd9, 0x7f, 0x4c, 0x4c, 0x6b, 0xa9, 0x8b, 0xbc, 0xc5, |
| 1212 | 0xb6, 0xd9, 0x7f, 0x4c, 0xf1, 0x05, 0xa6, 0x54, 0xef, 0xac, 0x69, 0x94, |
| 1213 | 0x89, 0x1c, 0xf1, 0x05, 0xa6, 0x54, 0xef, 0xac, 0x69, 0x94, 0x89, 0x1c |
| 1214 | }; |
| 1215 | unsigned int decompressedSize = 60; |
| 1216 | |
| 1217 | ZSTD_DStream* const zds = ZSTD_createDStream(); |
| 1218 | if (zds==NULL) goto _output_error; |
| 1219 | |
| 1220 | CHECK_Z( ZSTD_initDStream(zds) ); |
| 1221 | inBuff.src = compressed; |
| 1222 | inBuff.size = compressedSize; |
| 1223 | inBuff.pos = 0; |
| 1224 | outBuff.dst = decodedBuffer; |
| 1225 | outBuff.size = CNBufferSize; |
| 1226 | outBuff.pos = 0; |
| 1227 | |
Ephraim Park | 2830952 | 2019-07-01 10:17:30 -0700 | [diff] [blame] | 1228 | CHECK(ZSTD_decompressStream(zds, &outBuff, &inBuff) != 0, |
| 1229 | "Decompress did not reach the end of frame"); |
| 1230 | CHECK(inBuff.pos != inBuff.size, "Decompress did not fully consume input"); |
| 1231 | CHECK(outBuff.pos != decompressedSize, "Decompressed size does not match"); |
| 1232 | CHECK(memcmp(outBuff.dst, decompressed, decompressedSize) != 0, |
| 1233 | "Decompressed data does not match"); |
Ephraim Park | 01e8384 | 2019-06-27 17:27:29 -0700 | [diff] [blame] | 1234 | |
| 1235 | ZSTD_freeDStream(zds); |
| 1236 | } |
| 1237 | DISPLAYLEVEL(3, "OK \n"); |
| 1238 | |
W. Felix Handte | db4c8d0 | 2018-08-24 14:30:21 -0700 | [diff] [blame] | 1239 | DISPLAYLEVEL(3, "test%3i : dictionary + uncompressible block + reusing tables checks offset table validity: ", testNb++); |
| 1240 | { ZSTD_CDict* const cdict = ZSTD_createCDict_advanced( |
| 1241 | dictionary.start, dictionary.filled, |
| 1242 | ZSTD_dlm_byRef, ZSTD_dct_fullDict, |
| 1243 | ZSTD_getCParams(3, 0, dictionary.filled), |
| 1244 | ZSTD_defaultCMem); |
| 1245 | const size_t inbufsize = 2 * 128 * 1024; /* 2 blocks */ |
| 1246 | const size_t outbufsize = ZSTD_compressBound(inbufsize); |
| 1247 | size_t inbufpos = 0; |
| 1248 | size_t cursegmentlen; |
| 1249 | BYTE *inbuf = (BYTE *)malloc(inbufsize); |
| 1250 | BYTE *outbuf = (BYTE *)malloc(outbufsize); |
| 1251 | BYTE *checkbuf = (BYTE *)malloc(inbufsize); |
| 1252 | size_t ret; |
| 1253 | |
| 1254 | CHECK(cdict == NULL, "failed to alloc cdict"); |
| 1255 | CHECK(inbuf == NULL, "failed to alloc input buffer"); |
Yann Collet | d195eec | 2018-09-13 12:29:52 -0700 | [diff] [blame] | 1256 | |
W. Felix Handte | db4c8d0 | 2018-08-24 14:30:21 -0700 | [diff] [blame] | 1257 | /* first block is uncompressible */ |
| 1258 | cursegmentlen = 128 * 1024; |
| 1259 | RDG_genBuffer(inbuf + inbufpos, cursegmentlen, 0., 0., seed); |
| 1260 | inbufpos += cursegmentlen; |
| 1261 | |
| 1262 | /* second block is compressible */ |
| 1263 | cursegmentlen = 128 * 1024 - 256; |
| 1264 | RDG_genBuffer(inbuf + inbufpos, cursegmentlen, 0.05, 0., seed); |
| 1265 | inbufpos += cursegmentlen; |
| 1266 | |
| 1267 | /* and includes a very long backref */ |
| 1268 | cursegmentlen = 128; |
| 1269 | memcpy(inbuf + inbufpos, dictionary.start + 256, cursegmentlen); |
| 1270 | inbufpos += cursegmentlen; |
| 1271 | |
| 1272 | /* and includes a very long backref */ |
| 1273 | cursegmentlen = 128; |
| 1274 | memcpy(inbuf + inbufpos, dictionary.start + 128, cursegmentlen); |
| 1275 | inbufpos += cursegmentlen; |
| 1276 | |
| 1277 | ret = ZSTD_compress_usingCDict(zc, outbuf, outbufsize, inbuf, inbufpos, cdict); |
| 1278 | CHECK_Z(ret); |
| 1279 | |
| 1280 | ret = ZSTD_decompress_usingDict(zd, checkbuf, inbufsize, outbuf, ret, dictionary.start, dictionary.filled); |
| 1281 | CHECK_Z(ret); |
| 1282 | |
| 1283 | CHECK(memcmp(inbuf, checkbuf, inbufpos), "start and finish buffers don't match"); |
| 1284 | |
| 1285 | ZSTD_freeCDict(cdict); |
| 1286 | free(inbuf); |
| 1287 | free(outbuf); |
| 1288 | free(checkbuf); |
| 1289 | } |
| 1290 | DISPLAYLEVEL(3, "OK \n"); |
| 1291 | |
Nick Terrell | 146049a | 2018-09-28 12:09:14 -0700 | [diff] [blame] | 1292 | DISPLAYLEVEL(3, "test%3i : dictionary + small blocks + reusing tables checks offset table validity: ", testNb++); |
| 1293 | { ZSTD_CDict* const cdict = ZSTD_createCDict_advanced( |
| 1294 | dictionary.start, dictionary.filled, |
| 1295 | ZSTD_dlm_byRef, ZSTD_dct_fullDict, |
| 1296 | ZSTD_getCParams(3, 0, dictionary.filled), |
| 1297 | ZSTD_defaultCMem); |
| 1298 | ZSTD_outBuffer out = {compressedBuffer, compressedBufferSize, 0}; |
| 1299 | int remainingInput = 256 * 1024; |
Nick Terrell | eb4423e | 2018-09-28 14:24:38 -0700 | [diff] [blame] | 1300 | int offset; |
Nick Terrell | 146049a | 2018-09-28 12:09:14 -0700 | [diff] [blame] | 1301 | |
Yann Collet | 5c68639 | 2018-11-15 16:12:39 -0800 | [diff] [blame] | 1302 | CHECK_Z(ZSTD_CCtx_reset(zc, ZSTD_reset_session_and_parameters)); |
Nick Terrell | 146049a | 2018-09-28 12:09:14 -0700 | [diff] [blame] | 1303 | CHECK_Z(ZSTD_CCtx_refCDict(zc, cdict)); |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 1304 | CHECK_Z(ZSTD_CCtx_setParameter(zc, ZSTD_c_checksumFlag, 1)); |
Nick Terrell | 146049a | 2018-09-28 12:09:14 -0700 | [diff] [blame] | 1305 | /* Write a bunch of 6 byte blocks */ |
| 1306 | while (remainingInput > 0) { |
Nick Terrell | 0e7a7f1 | 2018-09-28 12:14:24 -0700 | [diff] [blame] | 1307 | char testBuffer[6] = "\xAA\xAA\xAA\xAA\xAA\xAA"; |
| 1308 | const size_t kSmallBlockSize = sizeof(testBuffer); |
Nick Terrell | 146049a | 2018-09-28 12:09:14 -0700 | [diff] [blame] | 1309 | ZSTD_inBuffer in = {testBuffer, kSmallBlockSize, 0}; |
| 1310 | |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 1311 | CHECK_Z(ZSTD_compressStream2(zc, &out, &in, ZSTD_e_flush)); |
Nick Terrell | 146049a | 2018-09-28 12:09:14 -0700 | [diff] [blame] | 1312 | CHECK(in.pos != in.size, "input not fully consumed"); |
| 1313 | remainingInput -= kSmallBlockSize; |
| 1314 | } |
| 1315 | /* Write several very long offset matches into the dictionary */ |
Nick Terrell | eb4423e | 2018-09-28 14:24:38 -0700 | [diff] [blame] | 1316 | for (offset = 1024; offset >= 0; offset -= 128) { |
Nick Terrell | 146049a | 2018-09-28 12:09:14 -0700 | [diff] [blame] | 1317 | ZSTD_inBuffer in = {dictionary.start + offset, 128, 0}; |
| 1318 | ZSTD_EndDirective flush = offset > 0 ? ZSTD_e_continue : ZSTD_e_end; |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 1319 | CHECK_Z(ZSTD_compressStream2(zc, &out, &in, flush)); |
Nick Terrell | 146049a | 2018-09-28 12:09:14 -0700 | [diff] [blame] | 1320 | CHECK(in.pos != in.size, "input not fully consumed"); |
| 1321 | } |
| 1322 | /* Ensure decompression works */ |
| 1323 | CHECK_Z(ZSTD_decompress_usingDict(zd, decodedBuffer, CNBufferSize, out.dst, out.pos, dictionary.start, dictionary.filled)); |
| 1324 | |
| 1325 | ZSTD_freeCDict(cdict); |
| 1326 | } |
| 1327 | DISPLAYLEVEL(3, "OK \n"); |
| 1328 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1329 | _end: |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 1330 | FUZ_freeDictionary(dictionary); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1331 | ZSTD_freeCStream(zc); |
| 1332 | ZSTD_freeDStream(zd); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 1333 | ZSTDMT_freeCCtx(mtctx); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1334 | free(CNBuffer); |
| 1335 | free(compressedBuffer); |
| 1336 | free(decodedBuffer); |
| 1337 | return testResult; |
| 1338 | |
| 1339 | _output_error: |
| 1340 | testResult = 1; |
| 1341 | DISPLAY("Error detected in Unit tests ! \n"); |
| 1342 | goto _end; |
| 1343 | } |
| 1344 | |
| 1345 | |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 1346 | /* ====== Fuzzer tests ====== */ |
| 1347 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1348 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 1349 | { |
| 1350 | const BYTE* b1 = (const BYTE*)buf1; |
| 1351 | const BYTE* b2 = (const BYTE*)buf2; |
| 1352 | size_t u; |
| 1353 | for (u=0; u<max; u++) { |
| 1354 | if (b1[u] != b2[u]) break; |
| 1355 | } |
Yann Collet | 940634a | 2018-01-19 13:19:59 -0800 | [diff] [blame] | 1356 | if (u==max) { |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1357 | DISPLAY("=> No difference detected within %u bytes \n", (unsigned)max); |
Yann Collet | 940634a | 2018-01-19 13:19:59 -0800 | [diff] [blame] | 1358 | return u; |
| 1359 | } |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1360 | DISPLAY("Error at position %u / %u \n", (unsigned)u, (unsigned)max); |
Yann Collet | dc69623 | 2018-01-19 12:41:56 -0800 | [diff] [blame] | 1361 | if (u>=3) |
| 1362 | DISPLAY(" %02X %02X %02X ", |
| 1363 | b1[u-3], b1[u-2], b1[u-1]); |
| 1364 | DISPLAY(" :%02X: %02X %02X %02X %02X %02X \n", |
| 1365 | b1[u], b1[u+1], b1[u+2], b1[u+3], b1[u+4], b1[u+5]); |
| 1366 | if (u>=3) |
| 1367 | DISPLAY(" %02X %02X %02X ", |
| 1368 | b2[u-3], b2[u-2], b2[u-1]); |
| 1369 | DISPLAY(" :%02X: %02X %02X %02X %02X %02X \n", |
| 1370 | b2[u], b2[u+1], b2[u+2], b2[u+3], b2[u+4], b2[u+5]); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1371 | return u; |
| 1372 | } |
| 1373 | |
| 1374 | static size_t FUZ_rLogLength(U32* seed, U32 logLength) |
| 1375 | { |
| 1376 | size_t const lengthMask = ((size_t)1 << logLength) - 1; |
| 1377 | return (lengthMask+1) + (FUZ_rand(seed) & lengthMask); |
| 1378 | } |
| 1379 | |
| 1380 | static size_t FUZ_randomLength(U32* seed, U32 maxLog) |
| 1381 | { |
| 1382 | U32 const logLength = FUZ_rand(seed) % maxLog; |
| 1383 | return FUZ_rLogLength(seed, logLength); |
| 1384 | } |
| 1385 | |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 1386 | /* Return value in range minVal <= v <= maxVal */ |
| 1387 | static U32 FUZ_randomClampedLength(U32* seed, U32 minVal, U32 maxVal) |
| 1388 | { |
| 1389 | U32 const mod = maxVal < minVal ? 1 : (maxVal + 1) - minVal; |
| 1390 | return (U32)((FUZ_rand(seed) % mod) + minVal); |
| 1391 | } |
| 1392 | |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1393 | static int fuzzerTests(U32 seed, unsigned nbTests, unsigned startTest, double compressibility, int bigTests) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1394 | { |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 1395 | U32 const maxSrcLog = bigTests ? 24 : 22; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1396 | static const U32 maxSampleLog = 19; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1397 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1398 | BYTE* cNoiseBuffer[5]; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1399 | size_t const copyBufferSize = srcBufferSize + (1<<maxSampleLog); |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1400 | BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize); |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1401 | size_t const cBufferSize = ZSTD_compressBound(srcBufferSize); |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1402 | BYTE* const cBuffer = (BYTE*)malloc (cBufferSize); |
| 1403 | size_t const dstBufferSize = srcBufferSize; |
| 1404 | BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1405 | U32 result = 0; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1406 | unsigned testNb = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1407 | U32 coreSeed = seed; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1408 | ZSTD_CStream* zc = ZSTD_createCStream(); /* will be re-created sometimes */ |
| 1409 | ZSTD_DStream* zd = ZSTD_createDStream(); /* will be re-created sometimes */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1410 | ZSTD_DStream* const zd_noise = ZSTD_createDStream(); |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 1411 | UTIL_time_t const startClock = UTIL_getTime(); |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1412 | const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */ |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 1413 | size_t dictSize = 0; |
| 1414 | U32 oldTestLog = 0; |
Yann Collet | 49f8459 | 2017-06-21 18:43:39 -0700 | [diff] [blame] | 1415 | U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel() : g_cLevelMax_smallTests; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1416 | |
| 1417 | /* allocations */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1418 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 1419 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 1420 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 1421 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 1422 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1423 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1424 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise , |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1425 | "Not enough memory, fuzzer tests cancelled"); |
| 1426 | |
| 1427 | /* Create initial samples */ |
| 1428 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 1429 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 1430 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 1431 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 1432 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 1433 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1434 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1435 | |
| 1436 | /* catch up testNb */ |
| 1437 | for (testNb=1; testNb < startTest; testNb++) |
| 1438 | FUZ_rand(&coreSeed); |
| 1439 | |
| 1440 | /* test loop */ |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 1441 | for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1442 | U32 lseed; |
| 1443 | const BYTE* srcBuffer; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1444 | size_t totalTestSize, totalGenSize, cSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1445 | XXH64_state_t xxhState; |
| 1446 | U64 crcOrig; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1447 | U32 resetAllowed = 1; |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 1448 | size_t maxTestSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1449 | |
| 1450 | /* init */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1451 | FUZ_rand(&coreSeed); |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 1452 | lseed = coreSeed ^ prime32; |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1453 | if (nbTests >= testNb) { |
Yann Collet | 9c40ae7 | 2018-01-26 17:48:33 -0800 | [diff] [blame] | 1454 | DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1455 | } else { |
Yann Collet | 9c40ae7 | 2018-01-26 17:48:33 -0800 | [diff] [blame] | 1456 | DISPLAYUPDATE(2, "\r%6u ", testNb); |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1457 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1458 | |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 1459 | /* states full reset (deliberately not synchronized) */ |
| 1460 | /* some issues can only happen when reusing states */ |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1461 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { |
| 1462 | ZSTD_freeCStream(zc); |
| 1463 | zc = ZSTD_createCStream(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1464 | CHECK(zc==NULL, "ZSTD_createCStream : allocation error"); |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1465 | resetAllowed=0; |
| 1466 | } |
| 1467 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { |
| 1468 | ZSTD_freeDStream(zd); |
| 1469 | zd = ZSTD_createDStream(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1470 | CHECK(zd==NULL, "ZSTD_createDStream : allocation error"); |
| 1471 | CHECK_Z( ZSTD_initDStream_usingDict(zd, NULL, 0) ); /* ensure at least one init */ |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1472 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1473 | |
| 1474 | /* srcBuffer selection [0-4] */ |
| 1475 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 1476 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 1477 | else { |
| 1478 | buffNb >>= 3; |
| 1479 | if (buffNb & 7) { |
| 1480 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 1481 | buffNb = tnb[buffNb >> 3]; |
| 1482 | } else { |
| 1483 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 1484 | buffNb = tnb[buffNb >> 3]; |
| 1485 | } } |
| 1486 | srcBuffer = cNoiseBuffer[buffNb]; |
| 1487 | } |
| 1488 | |
| 1489 | /* compression init */ |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 1490 | if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */ |
| 1491 | && oldTestLog /* at least one test happened */ && resetAllowed) { |
| 1492 | maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2); |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1493 | maxTestSize = MIN(maxTestSize, srcBufferSize-16); |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 1494 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1495 | CHECK_Z( ZSTD_resetCStream(zc, pledgedSrcSize) ); |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 1496 | } |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1497 | } else { |
| 1498 | U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 1499 | U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 1500 | U32 const cLevelCandidate = ( FUZ_rand(&lseed) % |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 1501 | (ZSTD_maxCLevel() - |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 1502 | (MAX(testLog, dictLog) / 3))) |
Yann Collet | ce80098 | 2017-04-05 16:34:09 -0700 | [diff] [blame] | 1503 | + 1; |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 1504 | U32 const cLevel = MIN(cLevelCandidate, cLevelMax); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1505 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 1506 | oldTestLog = testLog; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1507 | /* random dictionary selection */ |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 1508 | dictSize = ((FUZ_rand(&lseed)&7)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1509 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 1510 | dict = srcBuffer + dictStart; |
| 1511 | } |
Yann Collet | 213ef3b | 2017-10-13 19:01:58 -0700 | [diff] [blame] | 1512 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize; |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 1513 | ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1514 | params.fParams.checksumFlag = FUZ_rand(&lseed) & 1; |
| 1515 | params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1; |
Yann Collet | 213ef3b | 2017-10-13 19:01:58 -0700 | [diff] [blame] | 1516 | params.fParams.contentSizeFlag = FUZ_rand(&lseed) & 1; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1517 | CHECK_Z ( ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) ); |
| 1518 | } } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1519 | |
| 1520 | /* multi-segments compression test */ |
| 1521 | XXH64_reset(&xxhState, 0); |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 1522 | { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1523 | U32 n; |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 1524 | for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) { |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 1525 | /* compress random chunks into randomly sized dst buffers */ |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 1526 | { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 150354c | 2017-11-01 16:57:48 -0700 | [diff] [blame] | 1527 | size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize); |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 1528 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1529 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1530 | size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 1531 | ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 }; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1532 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1533 | |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1534 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1535 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1536 | XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); |
| 1537 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); |
| 1538 | totalTestSize += inBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | /* random flush operation, to mess around */ |
| 1542 | if ((FUZ_rand(&lseed) & 15) == 0) { |
| 1543 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1544 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 1545 | outBuff.size = outBuff.pos + adjustedDstSize; |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1546 | CHECK_Z( ZSTD_flushStream(zc, &outBuff) ); |
| 1547 | } } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1548 | |
| 1549 | /* final frame epilogue */ |
| 1550 | { size_t remainingToFlush = (size_t)(-1); |
| 1551 | while (remainingToFlush) { |
| 1552 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1553 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1554 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 1555 | remainingToFlush = ZSTD_endStream(zc, &outBuff); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 1556 | CHECK (ZSTD_isError(remainingToFlush), "end error : %s", ZSTD_getErrorName(remainingToFlush)); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1557 | } } |
| 1558 | crcOrig = XXH64_digest(&xxhState); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1559 | cSize = outBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | /* multi - fragments decompression test */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1563 | if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) { |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1564 | CHECK_Z ( ZSTD_resetDStream(zd) ); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 1565 | } else { |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1566 | CHECK_Z ( ZSTD_initDStream_usingDict(zd, dict, dictSize) ); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 1567 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1568 | { size_t decompressionResult = 1; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1569 | ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1570 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1571 | for (totalGenSize = 0 ; decompressionResult ; ) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1572 | size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1573 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1574 | size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1575 | inBuff.size = inBuff.pos + readCSrcSize; |
Yann Collet | ca306c1 | 2017-09-27 00:39:41 -0700 | [diff] [blame] | 1576 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1577 | decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | b3f33cc | 2017-09-09 14:37:28 -0700 | [diff] [blame] | 1578 | if (ZSTD_getErrorCode(decompressionResult) == ZSTD_error_checksum_wrong) { |
| 1579 | DISPLAY("checksum error : \n"); |
| 1580 | findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 1581 | } |
| 1582 | CHECK( ZSTD_isError(decompressionResult), "decompression error : %s", |
| 1583 | ZSTD_getErrorName(decompressionResult) ); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1584 | } |
| 1585 | CHECK (decompressionResult != 0, "frame not fully decoded"); |
Yann Collet | b3f33cc | 2017-09-09 14:37:28 -0700 | [diff] [blame] | 1586 | CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1587 | (unsigned)outBuff.pos, (unsigned)totalTestSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1588 | CHECK (inBuff.pos != cSize, "compressed data should be fully read") |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1589 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 1590 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 1591 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); |
| 1592 | } } |
| 1593 | |
| 1594 | /*===== noisy/erroneous src decompression test =====*/ |
| 1595 | |
| 1596 | /* add some noise */ |
| 1597 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 1598 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
| 1599 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1600 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
| 1601 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 1602 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 1603 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 1604 | } } |
| 1605 | |
| 1606 | /* try decompression on noisy data */ |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1607 | CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1608 | { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1609 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1610 | while (outBuff.pos < dstBufferSize) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1611 | size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1612 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1613 | size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize); |
Yann Collet | 64bf8ff | 2017-01-27 17:25:07 -0800 | [diff] [blame] | 1614 | size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1615 | outBuff.size = outBuff.pos + adjustedDstSize; |
Yann Collet | 64bf8ff | 2017-01-27 17:25:07 -0800 | [diff] [blame] | 1616 | inBuff.size = inBuff.pos + adjustedCSrcSize; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1617 | { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1618 | if (ZSTD_isError(decompressError)) break; /* error correctly detected */ |
Yann Collet | 64bf8ff | 2017-01-27 17:25:07 -0800 | [diff] [blame] | 1619 | /* No forward progress possible */ |
| 1620 | if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1621 | } } } } |
| 1622 | DISPLAY("\r%u fuzzer tests completed \n", testNb); |
| 1623 | |
| 1624 | _cleanup: |
| 1625 | ZSTD_freeCStream(zc); |
| 1626 | ZSTD_freeDStream(zd); |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1627 | ZSTD_freeDStream(zd_noise); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1628 | free(cNoiseBuffer[0]); |
| 1629 | free(cNoiseBuffer[1]); |
| 1630 | free(cNoiseBuffer[2]); |
| 1631 | free(cNoiseBuffer[3]); |
| 1632 | free(cNoiseBuffer[4]); |
| 1633 | free(copyBuffer); |
| 1634 | free(cBuffer); |
| 1635 | free(dstBuffer); |
| 1636 | return result; |
| 1637 | |
| 1638 | _output_error: |
| 1639 | result = 1; |
| 1640 | goto _cleanup; |
| 1641 | } |
| 1642 | |
| 1643 | |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 1644 | /* fuzzing ZSTDMT_* interface */ |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1645 | static int fuzzerTests_MT(U32 seed, int nbTests, int startTest, |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 1646 | double compressibility, int bigTests) |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1647 | { |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 1648 | const U32 maxSrcLog = bigTests ? 24 : 22; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1649 | static const U32 maxSampleLog = 19; |
| 1650 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
| 1651 | BYTE* cNoiseBuffer[5]; |
| 1652 | size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog); |
| 1653 | BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize); |
| 1654 | size_t const cBufferSize = ZSTD_compressBound(srcBufferSize); |
| 1655 | BYTE* const cBuffer = (BYTE*)malloc (cBufferSize); |
| 1656 | size_t const dstBufferSize = srcBufferSize; |
| 1657 | BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize); |
| 1658 | U32 result = 0; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1659 | int testNb = 0; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1660 | U32 coreSeed = seed; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1661 | int nbThreads = 2; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1662 | ZSTDMT_CCtx* zc = ZSTDMT_createCCtx(nbThreads); /* will be reset sometimes */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1663 | ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */ |
| 1664 | ZSTD_DStream* const zd_noise = ZSTD_createDStream(); |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 1665 | UTIL_time_t const startClock = UTIL_getTime(); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1666 | const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */ |
| 1667 | size_t dictSize = 0; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1668 | int const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests; |
| 1669 | U32 const nbThreadsMax = bigTests ? 4 : 2; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1670 | |
| 1671 | /* allocations */ |
| 1672 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 1673 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 1674 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 1675 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 1676 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
| 1677 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
| 1678 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise , |
| 1679 | "Not enough memory, fuzzer tests cancelled"); |
| 1680 | |
| 1681 | /* Create initial samples */ |
| 1682 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 1683 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 1684 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 1685 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 1686 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 1687 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
| 1688 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1689 | DISPLAYLEVEL(6, "Creating initial context with %i threads \n", nbThreads); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1690 | |
| 1691 | /* catch up testNb */ |
| 1692 | for (testNb=1; testNb < startTest; testNb++) |
| 1693 | FUZ_rand(&coreSeed); |
| 1694 | |
| 1695 | /* test loop */ |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 1696 | for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) { |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1697 | U32 lseed; |
| 1698 | const BYTE* srcBuffer; |
| 1699 | size_t totalTestSize, totalGenSize, cSize; |
| 1700 | XXH64_state_t xxhState; |
| 1701 | U64 crcOrig; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1702 | size_t maxTestSize; |
| 1703 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1704 | FUZ_rand(&coreSeed); |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1705 | if (nbTests >= testNb) { |
Yann Collet | 8e128ea | 2018-01-26 10:20:38 -0800 | [diff] [blame] | 1706 | DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1707 | } else { |
Yann Collet | 8e128ea | 2018-01-26 10:20:38 -0800 | [diff] [blame] | 1708 | DISPLAYUPDATE(2, "\r%6u ", testNb); |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1709 | } |
Yann Collet | d7e3cb5 | 2017-01-20 16:44:50 -0800 | [diff] [blame] | 1710 | lseed = coreSeed ^ prime32; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1711 | |
| 1712 | /* states full reset (deliberately not synchronized) */ |
| 1713 | /* some issues can only happen when reusing states */ |
| 1714 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { |
Yann Collet | 9fe50ed | 2017-09-28 01:42:06 -0700 | [diff] [blame] | 1715 | nbThreads = (FUZ_rand(&lseed) % nbThreadsMax) + 1; |
Yann Collet | 30c7698 | 2017-03-31 18:27:03 -0700 | [diff] [blame] | 1716 | DISPLAYLEVEL(5, "Creating new context with %u threads \n", nbThreads); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1717 | ZSTDMT_freeCCtx(zc); |
| 1718 | zc = ZSTDMT_createCCtx(nbThreads); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1719 | CHECK(zc==NULL, "ZSTDMT_createCCtx allocation error") |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1720 | } |
| 1721 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { |
| 1722 | ZSTD_freeDStream(zd); |
| 1723 | zd = ZSTD_createDStream(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1724 | CHECK(zd==NULL, "ZSTDMT_createCCtx allocation error") |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1725 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
| 1726 | } |
| 1727 | |
| 1728 | /* srcBuffer selection [0-4] */ |
| 1729 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 1730 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 1731 | else { |
| 1732 | buffNb >>= 3; |
| 1733 | if (buffNb & 7) { |
| 1734 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 1735 | buffNb = tnb[buffNb >> 3]; |
| 1736 | } else { |
| 1737 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 1738 | buffNb = tnb[buffNb >> 3]; |
| 1739 | } } |
| 1740 | srcBuffer = cNoiseBuffer[buffNb]; |
| 1741 | } |
| 1742 | |
| 1743 | /* compression init */ |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1744 | { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 1745 | U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1746 | int const cLevelCandidate = ( FUZ_rand(&lseed) |
| 1747 | % (ZSTD_maxCLevel() - (MAX(testLog, dictLog) / 2)) ) |
| 1748 | + 1; |
| 1749 | int const cLevelThreadAdjusted = cLevelCandidate - (nbThreads * 2) + 2; /* reduce cLevel when multiple threads to reduce memory consumption */ |
| 1750 | int const cLevelMin = MAX(cLevelThreadAdjusted, 1); /* no negative cLevel yet */ |
| 1751 | int const cLevel = MIN(cLevelMin, cLevelMax); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1752 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1753 | |
| 1754 | if (FUZ_rand(&lseed)&1) { /* simple init */ |
| 1755 | int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1; |
| 1756 | DISPLAYLEVEL(5, "Init with compression level = %i \n", compressionLevel); |
| 1757 | CHECK_Z( ZSTDMT_initCStream(zc, compressionLevel) ); |
| 1758 | } else { /* advanced init */ |
| 1759 | /* random dictionary selection */ |
| 1760 | dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0; |
| 1761 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 1762 | dict = srcBuffer + dictStart; |
| 1763 | } |
| 1764 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize; |
| 1765 | ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize); |
| 1766 | DISPLAYLEVEL(5, "Init with windowLog = %u, pledgedSrcSize = %u, dictSize = %u \n", |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1767 | params.cParams.windowLog, (unsigned)pledgedSrcSize, (unsigned)dictSize); |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1768 | params.fParams.checksumFlag = FUZ_rand(&lseed) & 1; |
| 1769 | params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1; |
| 1770 | params.fParams.contentSizeFlag = FUZ_rand(&lseed) & 1; |
| 1771 | DISPLAYLEVEL(5, "checksumFlag : %u \n", params.fParams.checksumFlag); |
Yann Collet | eee789b | 2018-12-11 17:41:42 -0800 | [diff] [blame] | 1772 | CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_overlapLog, FUZ_rand(&lseed) % 12) ); |
Yann Collet | 5e6aaa3 | 2018-12-10 18:45:03 -0800 | [diff] [blame] | 1773 | CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_jobSize, FUZ_rand(&lseed) % (2*maxTestSize+1)) ); /* custom job size */ |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1774 | CHECK_Z( ZSTDMT_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) ); |
| 1775 | } } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1776 | |
| 1777 | /* multi-segments compression test */ |
| 1778 | XXH64_reset(&xxhState, 0); |
| 1779 | { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; |
| 1780 | U32 n; |
| 1781 | for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) { |
| 1782 | /* compress random chunks into randomly sized dst buffers */ |
| 1783 | { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1784 | size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize); |
| 1785 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize); |
| 1786 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1787 | size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
| 1788 | ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 }; |
| 1789 | outBuff.size = outBuff.pos + dstBuffSize; |
| 1790 | |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1791 | DISPLAYLEVEL(6, "Sending %u bytes to compress \n", (unsigned)srcSize); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1792 | CHECK_Z( ZSTDMT_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1793 | DISPLAYLEVEL(6, "%u bytes read by ZSTDMT_compressStream \n", (unsigned)inBuff.pos); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1794 | |
| 1795 | XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); |
| 1796 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); |
| 1797 | totalTestSize += inBuff.pos; |
| 1798 | } |
| 1799 | |
| 1800 | /* random flush operation, to mess around */ |
| 1801 | if ((FUZ_rand(&lseed) & 15) == 0) { |
| 1802 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1803 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 2e23333 | 2018-01-16 15:28:43 -0800 | [diff] [blame] | 1804 | size_t const previousPos = outBuff.pos; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1805 | outBuff.size = outBuff.pos + adjustedDstSize; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1806 | DISPLAYLEVEL(5, "Flushing into dst buffer of size %u \n", (unsigned)adjustedDstSize); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1807 | CHECK_Z( ZSTDMT_flushStream(zc, &outBuff) ); |
Yann Collet | 2e23333 | 2018-01-16 15:28:43 -0800 | [diff] [blame] | 1808 | assert(outBuff.pos >= previousPos); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1809 | DISPLAYLEVEL(6, "%u bytes flushed by ZSTDMT_flushStream \n", (unsigned)(outBuff.pos-previousPos)); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1810 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1811 | |
| 1812 | /* final frame epilogue */ |
| 1813 | { size_t remainingToFlush = (size_t)(-1); |
| 1814 | while (remainingToFlush) { |
| 1815 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1816 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 2e23333 | 2018-01-16 15:28:43 -0800 | [diff] [blame] | 1817 | size_t const previousPos = outBuff.pos; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1818 | outBuff.size = outBuff.pos + adjustedDstSize; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1819 | DISPLAYLEVEL(5, "Ending into dst buffer of size %u \n", (unsigned)adjustedDstSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1820 | remainingToFlush = ZSTDMT_endStream(zc, &outBuff); |
Yann Collet | 30c7698 | 2017-03-31 18:27:03 -0700 | [diff] [blame] | 1821 | CHECK (ZSTD_isError(remainingToFlush), "ZSTDMT_endStream error : %s", ZSTD_getErrorName(remainingToFlush)); |
Yann Collet | 2e23333 | 2018-01-16 15:28:43 -0800 | [diff] [blame] | 1822 | assert(outBuff.pos >= previousPos); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1823 | DISPLAYLEVEL(6, "%u bytes flushed by ZSTDMT_endStream \n", (unsigned)(outBuff.pos-previousPos)); |
| 1824 | DISPLAYLEVEL(5, "endStream : remainingToFlush : %u \n", (unsigned)remainingToFlush); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1825 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1826 | crcOrig = XXH64_digest(&xxhState); |
| 1827 | cSize = outBuff.pos; |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1828 | DISPLAYLEVEL(5, "Frame completed : %u bytes compressed into %u bytes \n", |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1829 | (unsigned)totalTestSize, (unsigned)cSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | /* multi - fragments decompression test */ |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1833 | assert(totalTestSize < dstBufferSize); |
| 1834 | memset(dstBuffer, 170, totalTestSize); /* init dest area */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1835 | if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) { |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1836 | CHECK_Z( ZSTD_resetDStream(zd) ); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1837 | } else { |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1838 | CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) ); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1839 | } |
| 1840 | { size_t decompressionResult = 1; |
| 1841 | ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1842 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1843 | for (totalGenSize = 0 ; decompressionResult ; ) { |
| 1844 | size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1845 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1846 | size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
| 1847 | inBuff.size = inBuff.pos + readCSrcSize; |
Yann Collet | bfabd1d | 2017-09-27 01:01:11 -0700 | [diff] [blame] | 1848 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1849 | DISPLAYLEVEL(6, "ZSTD_decompressStream input %u bytes into outBuff %u bytes \n", |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1850 | (unsigned)readCSrcSize, (unsigned)dstBuffSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1851 | decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | 940634a | 2018-01-19 13:19:59 -0800 | [diff] [blame] | 1852 | if (ZSTD_isError(decompressionResult)) { |
| 1853 | DISPLAY("ZSTD_decompressStream error : %s \n", ZSTD_getErrorName(decompressionResult)); |
Yann Collet | 6f7280f | 2018-01-18 16:20:26 -0800 | [diff] [blame] | 1854 | findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 1855 | } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1856 | CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult)); |
Yann Collet | 3ad7d49 | 2018-01-19 17:35:08 -0800 | [diff] [blame] | 1857 | DISPLAYLEVEL(6, "total ingested (inBuff.pos) = %u and produced (outBuff.pos) = %u \n", |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1858 | (unsigned)inBuff.pos, (unsigned)outBuff.pos); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1859 | } |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1860 | CHECK (outBuff.pos != totalTestSize, |
| 1861 | "decompressed data : wrong size (%u != %u)", |
| 1862 | (unsigned)outBuff.pos, (unsigned)totalTestSize ); |
| 1863 | CHECK (inBuff.pos != cSize, |
| 1864 | "compressed data should be fully read (%u != %u)", |
| 1865 | (unsigned)inBuff.pos, (unsigned)cSize ); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1866 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 1867 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 1868 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); |
| 1869 | } } |
| 1870 | |
| 1871 | /*===== noisy/erroneous src decompression test =====*/ |
| 1872 | |
| 1873 | /* add some noise */ |
| 1874 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 1875 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
| 1876 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1877 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
| 1878 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 1879 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 1880 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 1881 | } } |
| 1882 | |
| 1883 | /* try decompression on noisy data */ |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1884 | CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1885 | { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1886 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1887 | while (outBuff.pos < dstBufferSize) { |
| 1888 | size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1889 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1890 | size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize); |
Nick Terrell | d98bf49 | 2017-01-27 15:42:36 -0800 | [diff] [blame] | 1891 | size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1892 | outBuff.size = outBuff.pos + adjustedDstSize; |
Nick Terrell | d98bf49 | 2017-01-27 15:42:36 -0800 | [diff] [blame] | 1893 | inBuff.size = inBuff.pos + adjustedCSrcSize; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1894 | { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 1895 | if (ZSTD_isError(decompressError)) break; /* error correctly detected */ |
Nick Terrell | d98bf49 | 2017-01-27 15:42:36 -0800 | [diff] [blame] | 1896 | /* No forward progress possible */ |
| 1897 | if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1898 | } } } } |
| 1899 | DISPLAY("\r%u fuzzer tests completed \n", testNb); |
| 1900 | |
| 1901 | _cleanup: |
| 1902 | ZSTDMT_freeCCtx(zc); |
| 1903 | ZSTD_freeDStream(zd); |
| 1904 | ZSTD_freeDStream(zd_noise); |
| 1905 | free(cNoiseBuffer[0]); |
| 1906 | free(cNoiseBuffer[1]); |
| 1907 | free(cNoiseBuffer[2]); |
| 1908 | free(cNoiseBuffer[3]); |
| 1909 | free(cNoiseBuffer[4]); |
| 1910 | free(copyBuffer); |
| 1911 | free(cBuffer); |
| 1912 | free(dstBuffer); |
| 1913 | return result; |
| 1914 | |
| 1915 | _output_error: |
| 1916 | result = 1; |
| 1917 | goto _cleanup; |
| 1918 | } |
| 1919 | |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1920 | /** If useOpaqueAPI, sets param in cctxParams. |
| 1921 | * Otherwise, sets the param in zc. */ |
| 1922 | static size_t setCCtxParameter(ZSTD_CCtx* zc, ZSTD_CCtx_params* cctxParams, |
| 1923 | ZSTD_cParameter param, unsigned value, |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 1924 | int useOpaqueAPI) |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1925 | { |
| 1926 | if (useOpaqueAPI) { |
Nick Terrell | 7ad7ba3 | 2019-02-19 17:41:56 -0800 | [diff] [blame] | 1927 | return ZSTD_CCtxParams_setParameter(cctxParams, param, value); |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1928 | } else { |
| 1929 | return ZSTD_CCtx_setParameter(zc, param, value); |
| 1930 | } |
| 1931 | } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1932 | |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 1933 | /* Tests for ZSTD_compress_generic() API */ |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1934 | static int fuzzerTests_newAPI(U32 seed, int nbTests, int startTest, |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 1935 | double compressibility, int bigTests) |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1936 | { |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 1937 | U32 const maxSrcLog = bigTests ? 24 : 22; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1938 | static const U32 maxSampleLog = 19; |
| 1939 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
| 1940 | BYTE* cNoiseBuffer[5]; |
| 1941 | size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog); |
| 1942 | BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize); |
| 1943 | size_t const cBufferSize = ZSTD_compressBound(srcBufferSize); |
| 1944 | BYTE* const cBuffer = (BYTE*)malloc (cBufferSize); |
| 1945 | size_t const dstBufferSize = srcBufferSize; |
| 1946 | BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize); |
| 1947 | U32 result = 0; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1948 | int testNb = 0; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1949 | U32 coreSeed = seed; |
| 1950 | ZSTD_CCtx* zc = ZSTD_createCCtx(); /* will be reset sometimes */ |
| 1951 | ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */ |
| 1952 | ZSTD_DStream* const zd_noise = ZSTD_createDStream(); |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 1953 | UTIL_time_t const startClock = UTIL_getTime(); |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 1954 | const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1955 | size_t dictSize = 0; |
| 1956 | U32 oldTestLog = 0; |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 1957 | U32 windowLogMalus = 0; /* can survive between 2 loops */ |
| 1958 | U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests; |
| 1959 | U32 const nbThreadsMax = bigTests ? 4 : 2; |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1960 | ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams(); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1961 | |
| 1962 | /* allocations */ |
| 1963 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 1964 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 1965 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 1966 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 1967 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
| 1968 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
| 1969 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise , |
| 1970 | "Not enough memory, fuzzer tests cancelled"); |
| 1971 | |
| 1972 | /* Create initial samples */ |
| 1973 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 1974 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 1975 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 1976 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 1977 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 1978 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1979 | CHECK_Z( ZSTD_initDStream_usingDict(zd, NULL, 0) ); /* ensure at least one init */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1980 | |
| 1981 | /* catch up testNb */ |
| 1982 | for (testNb=1; testNb < startTest; testNb++) |
| 1983 | FUZ_rand(&coreSeed); |
| 1984 | |
| 1985 | /* test loop */ |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 1986 | for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) { |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1987 | U32 lseed; |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 1988 | int opaqueAPI; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1989 | const BYTE* srcBuffer; |
| 1990 | size_t totalTestSize, totalGenSize, cSize; |
| 1991 | XXH64_state_t xxhState; |
| 1992 | U64 crcOrig; |
| 1993 | U32 resetAllowed = 1; |
| 1994 | size_t maxTestSize; |
Nick Terrell | ca77822 | 2018-04-25 16:32:29 -0700 | [diff] [blame] | 1995 | ZSTD_parameters savedParams; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1996 | |
| 1997 | /* init */ |
| 1998 | if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } |
| 1999 | else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
| 2000 | FUZ_rand(&coreSeed); |
| 2001 | lseed = coreSeed ^ prime32; |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 2002 | DISPLAYLEVEL(5, " *** Test %u *** \n", testNb); |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 2003 | opaqueAPI = FUZ_rand(&lseed) & 1; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2004 | |
| 2005 | /* states full reset (deliberately not synchronized) */ |
| 2006 | /* some issues can only happen when reusing states */ |
| 2007 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { |
| 2008 | DISPLAYLEVEL(5, "Creating new context \n"); |
| 2009 | ZSTD_freeCCtx(zc); |
| 2010 | zc = ZSTD_createCCtx(); |
Yann Collet | 62469c9 | 2018-06-19 20:14:03 -0700 | [diff] [blame] | 2011 | CHECK(zc == NULL, "ZSTD_createCCtx allocation error"); |
| 2012 | resetAllowed = 0; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2013 | } |
| 2014 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { |
| 2015 | ZSTD_freeDStream(zd); |
| 2016 | zd = ZSTD_createDStream(); |
Yann Collet | 62469c9 | 2018-06-19 20:14:03 -0700 | [diff] [blame] | 2017 | CHECK(zd == NULL, "ZSTD_createDStream allocation error"); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2018 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
| 2019 | } |
| 2020 | |
| 2021 | /* srcBuffer selection [0-4] */ |
| 2022 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 2023 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 2024 | else { |
| 2025 | buffNb >>= 3; |
| 2026 | if (buffNb & 7) { |
| 2027 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 2028 | buffNb = tnb[buffNb >> 3]; |
| 2029 | } else { |
| 2030 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 2031 | buffNb = tnb[buffNb >> 3]; |
| 2032 | } } |
| 2033 | srcBuffer = cNoiseBuffer[buffNb]; |
| 2034 | } |
| 2035 | |
| 2036 | /* compression init */ |
Yann Collet | b737293 | 2017-06-27 15:49:12 -0700 | [diff] [blame] | 2037 | CHECK_Z( ZSTD_CCtx_loadDictionary(zc, NULL, 0) ); /* cancel previous dict /*/ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2038 | if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */ |
Yann Collet | 2108dec | 2018-06-01 15:18:32 -0700 | [diff] [blame] | 2039 | && oldTestLog /* at least one test happened */ |
| 2040 | && resetAllowed) { |
| 2041 | /* just set a compression level */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2042 | maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2); |
| 2043 | if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2044 | { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1; |
Yann Collet | 2108dec | 2018-06-01 15:18:32 -0700 | [diff] [blame] | 2045 | DISPLAYLEVEL(5, "t%u : compression level : %i \n", testNb, compressionLevel); |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 2046 | CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_c_compressionLevel, compressionLevel, opaqueAPI) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2047 | } |
| 2048 | } else { |
| 2049 | U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
| 2050 | U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 2051 | U32 const cLevelCandidate = (FUZ_rand(&lseed) % |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2052 | (ZSTD_maxCLevel() - |
Yann Collet | 9fe50ed | 2017-09-28 01:42:06 -0700 | [diff] [blame] | 2053 | (MAX(testLog, dictLog) / 2))) + |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2054 | 1; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2055 | int const cLevel = MIN(cLevelCandidate, cLevelMax); |
| 2056 | DISPLAYLEVEL(5, "t%i: base cLevel : %u \n", testNb, cLevel); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2057 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2058 | DISPLAYLEVEL(5, "t%i: maxTestSize : %u \n", testNb, (unsigned)maxTestSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2059 | oldTestLog = testLog; |
| 2060 | /* random dictionary selection */ |
| 2061 | dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0; |
| 2062 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 2063 | dict = srcBuffer + dictStart; |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 2064 | if (!dictSize) dict=NULL; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2065 | } |
| 2066 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize; |
| 2067 | ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, pledgedSrcSize, dictSize); |
Nick Terrell | 7ee910e | 2018-09-27 13:55:24 -0700 | [diff] [blame] | 2068 | const U32 windowLogMax = bigTests ? 24 : 20; |
| 2069 | const U32 searchLogMax = bigTests ? 15 : 13; |
Yann Collet | 2108dec | 2018-06-01 15:18:32 -0700 | [diff] [blame] | 2070 | if (dictSize) |
| 2071 | DISPLAYLEVEL(5, "t%u: with dictionary of size : %zu \n", testNb, dictSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2072 | |
| 2073 | /* mess with compression parameters */ |
| 2074 | cParams.windowLog += (FUZ_rand(&lseed) & 3) - 1; |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 2075 | cParams.windowLog = MIN(windowLogMax, cParams.windowLog); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2076 | cParams.hashLog += (FUZ_rand(&lseed) & 3) - 1; |
| 2077 | cParams.chainLog += (FUZ_rand(&lseed) & 3) - 1; |
| 2078 | cParams.searchLog += (FUZ_rand(&lseed) & 3) - 1; |
Nick Terrell | 7ee910e | 2018-09-27 13:55:24 -0700 | [diff] [blame] | 2079 | cParams.searchLog = MIN(searchLogMax, cParams.searchLog); |
Yann Collet | e874dac | 2018-11-20 14:56:07 -0800 | [diff] [blame] | 2080 | cParams.minMatch += (FUZ_rand(&lseed) & 3) - 1; |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 2081 | cParams.targetLength = (U32)((cParams.targetLength + 1 ) * (0.5 + ((double)(FUZ_rand(&lseed) & 127) / 128))); |
Yann Collet | 2108dec | 2018-06-01 15:18:32 -0700 | [diff] [blame] | 2082 | cParams = ZSTD_adjustCParams(cParams, pledgedSrcSize, dictSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2083 | |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 2084 | if (FUZ_rand(&lseed) & 1) { |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 2085 | DISPLAYLEVEL(5, "t%u: windowLog : %u \n", testNb, cParams.windowLog); |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 2086 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_windowLog, cParams.windowLog, opaqueAPI) ); |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 2087 | assert(cParams.windowLog >= ZSTD_WINDOWLOG_MIN); /* guaranteed by ZSTD_adjustCParams() */ |
| 2088 | windowLogMalus = (cParams.windowLog - ZSTD_WINDOWLOG_MIN) / 5; |
| 2089 | } |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 2090 | if (FUZ_rand(&lseed) & 1) { |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 2091 | DISPLAYLEVEL(5, "t%u: hashLog : %u \n", testNb, cParams.hashLog); |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 2092 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_hashLog, cParams.hashLog, opaqueAPI) ); |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 2093 | } |
| 2094 | if (FUZ_rand(&lseed) & 1) { |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 2095 | DISPLAYLEVEL(5, "t%u: chainLog : %u \n", testNb, cParams.chainLog); |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 2096 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_chainLog, cParams.chainLog, opaqueAPI) ); |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 2097 | } |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 2098 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_searchLog, cParams.searchLog, opaqueAPI) ); |
| 2099 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_minMatch, cParams.minMatch, opaqueAPI) ); |
| 2100 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_targetLength, cParams.targetLength, opaqueAPI) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2101 | |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 2102 | /* mess with long distance matching parameters */ |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 2103 | if (bigTests) { |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 2104 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_enableLongDistanceMatching, FUZ_rand(&lseed) & 63, opaqueAPI) ); |
| 2105 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_ldmHashLog, FUZ_randomClampedLength(&lseed, ZSTD_HASHLOG_MIN, 23), opaqueAPI) ); |
| 2106 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_ldmMinMatch, FUZ_randomClampedLength(&lseed, ZSTD_LDM_MINMATCH_MIN, ZSTD_LDM_MINMATCH_MAX), opaqueAPI) ); |
| 2107 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_ldmBucketSizeLog, FUZ_randomClampedLength(&lseed, ZSTD_LDM_BUCKETSIZELOG_MIN, ZSTD_LDM_BUCKETSIZELOG_MAX), opaqueAPI) ); |
| 2108 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_ldmHashRateLog, FUZ_randomClampedLength(&lseed, ZSTD_LDM_HASHRATELOG_MIN, ZSTD_LDM_HASHRATELOG_MAX), opaqueAPI) ); |
Nick Magerko | c7a24d7 | 2019-08-20 13:06:15 -0700 | [diff] [blame^] | 2109 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_srcSizeHint, FUZ_randomClampedLength(&lseed, ZSTD_SRCSIZEHINT_MIN, ZSTD_SRCSIZEHINT_MAX), opaqueAPI) ); |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 2110 | } |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 2111 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2112 | /* mess with frame parameters */ |
Yann Collet | ebd955e | 2018-01-23 13:12:40 -0800 | [diff] [blame] | 2113 | if (FUZ_rand(&lseed) & 1) { |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2114 | int const checksumFlag = FUZ_rand(&lseed) & 1; |
Yann Collet | ebd955e | 2018-01-23 13:12:40 -0800 | [diff] [blame] | 2115 | DISPLAYLEVEL(5, "t%u: frame checksum : %u \n", testNb, checksumFlag); |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 2116 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_checksumFlag, checksumFlag, opaqueAPI) ); |
Yann Collet | ebd955e | 2018-01-23 13:12:40 -0800 | [diff] [blame] | 2117 | } |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 2118 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_dictIDFlag, FUZ_rand(&lseed) & 1, opaqueAPI) ); |
| 2119 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_contentSizeFlag, FUZ_rand(&lseed) & 1, opaqueAPI) ); |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 2120 | if (FUZ_rand(&lseed) & 1) { |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2121 | DISPLAYLEVEL(5, "t%u: pledgedSrcSize : %u \n", testNb, (unsigned)pledgedSrcSize); |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 2122 | CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) ); |
| 2123 | } |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2124 | |
Josh Soref | a880ca2 | 2019-04-12 14:18:11 -0400 | [diff] [blame] | 2125 | /* multi-threading parameters. Only adjust occasionally for small tests. */ |
Nick Terrell | d8c73cd | 2018-09-27 15:49:31 -0700 | [diff] [blame] | 2126 | if (bigTests || (FUZ_rand(&lseed) & 0xF) == 0xF) { |
| 2127 | U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1; |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 2128 | U32 const nbThreadsAdjusted = (windowLogMalus < nbThreadsCandidate) ? nbThreadsCandidate - windowLogMalus : 1; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2129 | int const nbThreads = MIN(nbThreadsAdjusted, nbThreadsMax); |
| 2130 | DISPLAYLEVEL(5, "t%i: nbThreads : %u \n", testNb, nbThreads); |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 2131 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_nbWorkers, nbThreads, opaqueAPI) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2132 | if (nbThreads > 1) { |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 2133 | U32 const jobLog = FUZ_rand(&lseed) % (testLog+1); |
Yann Collet | 9b784de | 2018-12-11 16:55:33 -0800 | [diff] [blame] | 2134 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_overlapLog, FUZ_rand(&lseed) % 10, opaqueAPI) ); |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 2135 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog), opaqueAPI) ); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 2136 | } |
| 2137 | } |
Nick Terrell | 71fe78c | 2018-11-14 13:53:14 -0800 | [diff] [blame] | 2138 | /* Enable rsyncable mode 1 in 4 times. */ |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 2139 | setCCtxParameter(zc, cctxParams, ZSTD_c_rsyncable, (FUZ_rand(&lseed) % 4 == 0), opaqueAPI); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 2140 | |
Yann Collet | 3583d19 | 2018-12-05 17:26:02 -0800 | [diff] [blame] | 2141 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_c_forceMaxWindow, FUZ_rand(&lseed) & 1, opaqueAPI) ); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 2142 | |
| 2143 | /* Apply parameters */ |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 2144 | if (opaqueAPI) { |
Yann Collet | 2108dec | 2018-06-01 15:18:32 -0700 | [diff] [blame] | 2145 | DISPLAYLEVEL(5, "t%u: applying CCtxParams \n", testNb); |
Stella Lau | 82d636b | 2017-08-29 18:03:06 -0700 | [diff] [blame] | 2146 | CHECK_Z (ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams) ); |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 2147 | } |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 2148 | |
| 2149 | if (FUZ_rand(&lseed) & 1) { |
Stella Lau | eb7bbab | 2017-08-25 10:48:07 -0700 | [diff] [blame] | 2150 | if (FUZ_rand(&lseed) & 1) { |
| 2151 | CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) ); |
| 2152 | } else { |
| 2153 | CHECK_Z( ZSTD_CCtx_loadDictionary_byReference(zc, dict, dictSize) ); |
| 2154 | } |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 2155 | } else { |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 2156 | CHECK_Z( ZSTD_CCtx_refPrefix(zc, dict, dictSize) ); |
| 2157 | } |
| 2158 | } } |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2159 | |
Nick Terrell | ca77822 | 2018-04-25 16:32:29 -0700 | [diff] [blame] | 2160 | CHECK_Z(getCCtxParams(zc, &savedParams)); |
| 2161 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2162 | /* multi-segments compression test */ |
| 2163 | XXH64_reset(&xxhState, 0); |
| 2164 | { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; |
Yann Collet | 052a95f | 2017-07-11 17:18:26 -0700 | [diff] [blame] | 2165 | for (cSize=0, totalTestSize=0 ; (totalTestSize < maxTestSize) ; ) { |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2166 | /* compress random chunks into randomly sized dst buffers */ |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 2167 | size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 2168 | size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize); |
| 2169 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize); |
Yann Collet | 052a95f | 2017-07-11 17:18:26 -0700 | [diff] [blame] | 2170 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1); |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 2171 | size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
| 2172 | ZSTD_EndDirective const flush = (FUZ_rand(&lseed) & 15) ? ZSTD_e_continue : ZSTD_e_flush; |
| 2173 | ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 }; |
| 2174 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2175 | |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 2176 | CHECK_Z( ZSTD_compressStream2(zc, &outBuff, &inBuff, flush) ); |
Yann Collet | ebd955e | 2018-01-23 13:12:40 -0800 | [diff] [blame] | 2177 | DISPLAYLEVEL(6, "t%u: compress consumed %u bytes (total : %u) ; flush: %u (total : %u) \n", |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2178 | testNb, (unsigned)inBuff.pos, (unsigned)(totalTestSize + inBuff.pos), (unsigned)flush, (unsigned)outBuff.pos); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2179 | |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 2180 | XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); |
| 2181 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); |
| 2182 | totalTestSize += inBuff.pos; |
| 2183 | } |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2184 | |
| 2185 | /* final frame epilogue */ |
Yann Collet | ebd955e | 2018-01-23 13:12:40 -0800 | [diff] [blame] | 2186 | { size_t remainingToFlush = 1; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2187 | while (remainingToFlush) { |
| 2188 | ZSTD_inBuffer inBuff = { NULL, 0, 0 }; |
Yann Collet | 052a95f | 2017-07-11 17:18:26 -0700 | [diff] [blame] | 2189 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2190 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 2191 | outBuff.size = outBuff.pos + adjustedDstSize; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2192 | DISPLAYLEVEL(6, "t%u: End-flush into dst buffer of size %u \n", testNb, (unsigned)adjustedDstSize); |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 2193 | remainingToFlush = ZSTD_compressStream2(zc, &outBuff, &inBuff, ZSTD_e_end); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2194 | DISPLAYLEVEL(6, "t%u: Total flushed so far : %u bytes \n", testNb, (unsigned)outBuff.pos); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 2195 | CHECK( ZSTD_isError(remainingToFlush), |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 2196 | "ZSTD_compressStream2 w/ ZSTD_e_end error : %s", |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 2197 | ZSTD_getErrorName(remainingToFlush) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2198 | } } |
| 2199 | crcOrig = XXH64_digest(&xxhState); |
| 2200 | cSize = outBuff.pos; |
Yann Collet | 2108dec | 2018-06-01 15:18:32 -0700 | [diff] [blame] | 2201 | DISPLAYLEVEL(5, "Frame completed : %zu bytes \n", cSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
Nick Terrell | ca77822 | 2018-04-25 16:32:29 -0700 | [diff] [blame] | 2204 | CHECK(badParameters(zc, savedParams), "CCtx params are wrong"); |
| 2205 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2206 | /* multi - fragments decompression test */ |
| 2207 | if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) { |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2208 | DISPLAYLEVEL(5, "resetting DCtx (dict:%p) \n", dict); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 2209 | CHECK_Z( ZSTD_resetDStream(zd) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2210 | } else { |
Yann Collet | 2108dec | 2018-06-01 15:18:32 -0700 | [diff] [blame] | 2211 | if (dictSize) |
| 2212 | DISPLAYLEVEL(5, "using dictionary of size %zu \n", dictSize); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 2213 | CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2214 | } |
| 2215 | { size_t decompressionResult = 1; |
| 2216 | ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 2217 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 2218 | for (totalGenSize = 0 ; decompressionResult ; ) { |
| 2219 | size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 2220 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 2221 | size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
| 2222 | inBuff.size = inBuff.pos + readCSrcSize; |
Yann Collet | bfabd1d | 2017-09-27 01:01:11 -0700 | [diff] [blame] | 2223 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | 4f7c896 | 2018-01-23 18:00:51 -0800 | [diff] [blame] | 2224 | DISPLAYLEVEL(6, "decompression presented %u new bytes (pos:%u/%u)\n", |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2225 | (unsigned)readCSrcSize, (unsigned)inBuff.pos, (unsigned)cSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2226 | decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | 4f7c896 | 2018-01-23 18:00:51 -0800 | [diff] [blame] | 2227 | DISPLAYLEVEL(6, "so far: consumed = %u, produced = %u \n", |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2228 | (unsigned)inBuff.pos, (unsigned)outBuff.pos); |
Yann Collet | ebd955e | 2018-01-23 13:12:40 -0800 | [diff] [blame] | 2229 | if (ZSTD_isError(decompressionResult)) { |
| 2230 | DISPLAY("ZSTD_decompressStream error : %s \n", ZSTD_getErrorName(decompressionResult)); |
| 2231 | findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 2232 | } |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2233 | CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult)); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2234 | CHECK (inBuff.pos > cSize, "ZSTD_decompressStream consumes too much input : %u > %u ", (unsigned)inBuff.pos, (unsigned)cSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2235 | } |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2236 | CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (unsigned)inBuff.pos, (unsigned)cSize); |
| 2237 | CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (unsigned)outBuff.pos, (unsigned)totalTestSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2238 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 2239 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 2240 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); |
| 2241 | } } |
| 2242 | |
| 2243 | /*===== noisy/erroneous src decompression test =====*/ |
| 2244 | |
| 2245 | /* add some noise */ |
| 2246 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 2247 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
| 2248 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 2249 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
| 2250 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 2251 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 2252 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 2253 | } } |
| 2254 | |
| 2255 | /* try decompression on noisy data */ |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 2256 | CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2257 | { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 2258 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 2259 | while (outBuff.pos < dstBufferSize) { |
| 2260 | size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 2261 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 2262 | size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize); |
| 2263 | size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize); |
| 2264 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 2265 | inBuff.size = inBuff.pos + adjustedCSrcSize; |
| 2266 | { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 2267 | if (ZSTD_isError(decompressError)) break; /* error correctly detected */ |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 2268 | /* Good so far, but no more progress possible */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2269 | if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break; |
| 2270 | } } } } |
Yann Collet | 8dee0ec | 2017-06-18 23:25:15 -0700 | [diff] [blame] | 2271 | DISPLAY("\r%u fuzzer tests completed \n", testNb-1); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2272 | |
| 2273 | _cleanup: |
| 2274 | ZSTD_freeCCtx(zc); |
| 2275 | ZSTD_freeDStream(zd); |
| 2276 | ZSTD_freeDStream(zd_noise); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 2277 | ZSTD_freeCCtxParams(cctxParams); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2278 | free(cNoiseBuffer[0]); |
| 2279 | free(cNoiseBuffer[1]); |
| 2280 | free(cNoiseBuffer[2]); |
| 2281 | free(cNoiseBuffer[3]); |
| 2282 | free(cNoiseBuffer[4]); |
| 2283 | free(copyBuffer); |
| 2284 | free(cBuffer); |
| 2285 | free(dstBuffer); |
| 2286 | return result; |
| 2287 | |
| 2288 | _output_error: |
| 2289 | result = 1; |
| 2290 | goto _cleanup; |
| 2291 | } |
| 2292 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2293 | /*-******************************************************* |
| 2294 | * Command line |
| 2295 | *********************************************************/ |
Nick Terrell | f2d6db4 | 2018-09-27 15:13:43 -0700 | [diff] [blame] | 2296 | static int FUZ_usage(const char* programName) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2297 | { |
| 2298 | DISPLAY( "Usage :\n"); |
| 2299 | DISPLAY( " %s [args]\n", programName); |
| 2300 | DISPLAY( "\n"); |
| 2301 | DISPLAY( "Arguments :\n"); |
| 2302 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 2303 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 2304 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
| 2305 | DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); |
| 2306 | DISPLAY( " -v : verbose\n"); |
| 2307 | DISPLAY( " -p : pause at the end\n"); |
| 2308 | DISPLAY( " -h : display help and exit\n"); |
| 2309 | return 0; |
| 2310 | } |
| 2311 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2312 | typedef enum { simple_api, mt_api, advanced_api } e_api; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2313 | |
| 2314 | int main(int argc, const char** argv) |
| 2315 | { |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 2316 | U32 seed = 0; |
| 2317 | int seedset = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2318 | int nbTests = nbTestsDefault; |
| 2319 | int testNb = 0; |
| 2320 | int proba = FUZ_COMPRESSIBILITY_DEFAULT; |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 2321 | int result = 0; |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 2322 | int mainPause = 0; |
Yann Collet | afb0aca | 2017-06-29 18:19:09 -0700 | [diff] [blame] | 2323 | int bigTests = (sizeof(size_t) == 8); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2324 | e_api selected_api = simple_api; |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 2325 | const char* const programName = argv[0]; |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 2326 | int argNb; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2327 | |
| 2328 | /* Check command line */ |
| 2329 | for(argNb=1; argNb<argc; argNb++) { |
| 2330 | const char* argument = argv[argNb]; |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 2331 | assert(argument != NULL); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2332 | |
| 2333 | /* Parsing commands. Aggregated commands are allowed */ |
| 2334 | if (argument[0]=='-') { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2335 | |
Yann Collet | 47c6a95 | 2017-09-28 18:27:22 -0700 | [diff] [blame] | 2336 | if (!strcmp(argument, "--mt")) { selected_api=mt_api; testNb += !testNb; continue; } |
| 2337 | if (!strcmp(argument, "--newapi")) { selected_api=advanced_api; testNb += !testNb; continue; } |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 2338 | if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; } |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 2339 | |
| 2340 | argument++; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2341 | while (*argument!=0) { |
| 2342 | switch(*argument) |
| 2343 | { |
| 2344 | case 'h': |
| 2345 | return FUZ_usage(programName); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 2346 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2347 | case 'v': |
| 2348 | argument++; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 2349 | g_displayLevel++; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2350 | break; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 2351 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2352 | case 'q': |
| 2353 | argument++; |
| 2354 | g_displayLevel--; |
| 2355 | break; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 2356 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2357 | case 'p': /* pause at the end */ |
| 2358 | argument++; |
| 2359 | mainPause = 1; |
| 2360 | break; |
| 2361 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 2362 | case 'i': /* limit tests by nb of iterations (default) */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2363 | argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 2364 | nbTests=0; g_clockTime=0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2365 | while ((*argument>='0') && (*argument<='9')) { |
| 2366 | nbTests *= 10; |
| 2367 | nbTests += *argument - '0'; |
| 2368 | argument++; |
| 2369 | } |
| 2370 | break; |
| 2371 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 2372 | case 'T': /* limit tests by time */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2373 | argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 2374 | nbTests=0; g_clockTime=0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2375 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 2376 | g_clockTime *= 10; |
| 2377 | g_clockTime += *argument - '0'; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2378 | argument++; |
| 2379 | } |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 2380 | if (*argument=='m') { /* -T1m == -T60 */ |
| 2381 | g_clockTime *=60, argument++; |
| 2382 | if (*argument=='n') argument++; /* -T1mn == -T60 */ |
| 2383 | } else if (*argument=='s') argument++; /* -T10s == -T10 */ |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 2384 | g_clockTime *= SEC_TO_MICRO; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2385 | break; |
| 2386 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 2387 | case 's': /* manually select seed */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2388 | argument++; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2389 | seedset=1; |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 2390 | seed=0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2391 | while ((*argument>='0') && (*argument<='9')) { |
| 2392 | seed *= 10; |
| 2393 | seed += *argument - '0'; |
| 2394 | argument++; |
| 2395 | } |
| 2396 | break; |
| 2397 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 2398 | case 't': /* select starting test number */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2399 | argument++; |
| 2400 | testNb=0; |
| 2401 | while ((*argument>='0') && (*argument<='9')) { |
| 2402 | testNb *= 10; |
| 2403 | testNb += *argument - '0'; |
| 2404 | argument++; |
| 2405 | } |
| 2406 | break; |
| 2407 | |
| 2408 | case 'P': /* compressibility % */ |
| 2409 | argument++; |
| 2410 | proba=0; |
| 2411 | while ((*argument>='0') && (*argument<='9')) { |
| 2412 | proba *= 10; |
| 2413 | proba += *argument - '0'; |
| 2414 | argument++; |
| 2415 | } |
| 2416 | if (proba<0) proba=0; |
| 2417 | if (proba>100) proba=100; |
| 2418 | break; |
| 2419 | |
| 2420 | default: |
| 2421 | return FUZ_usage(programName); |
| 2422 | } |
| 2423 | } } } /* for(argNb=1; argNb<argc; argNb++) */ |
| 2424 | |
| 2425 | /* Get Seed */ |
Yann Collet | bb85581 | 2016-08-25 19:11:11 +0200 | [diff] [blame] | 2426 | DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2427 | |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 2428 | if (!seedset) { |
| 2429 | time_t const t = time(NULL); |
| 2430 | U32 const h = XXH32(&t, sizeof(t), 1); |
| 2431 | seed = h % 10000; |
| 2432 | } |
| 2433 | |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2434 | DISPLAY("Seed = %u\n", (unsigned)seed); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2435 | if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba); |
| 2436 | |
| 2437 | if (nbTests<=0) nbTests=1; |
| 2438 | |
| 2439 | if (testNb==0) { |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 2440 | result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */ |
Yann Collet | 4616fad | 2017-07-10 17:16:41 -0700 | [diff] [blame] | 2441 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2442 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2443 | if (!result) { |
| 2444 | switch(selected_api) |
| 2445 | { |
| 2446 | case simple_api : |
| 2447 | result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100, bigTests); |
| 2448 | break; |
| 2449 | case mt_api : |
| 2450 | result = fuzzerTests_MT(seed, nbTests, testNb, ((double)proba) / 100, bigTests); |
| 2451 | break; |
| 2452 | case advanced_api : |
Yann Collet | c9e8ee9 | 2018-06-18 19:20:37 -0700 | [diff] [blame] | 2453 | result = fuzzerTests_newAPI(seed, nbTests, testNb, ((double)proba) / 100, bigTests); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 2454 | break; |
| 2455 | default : |
| 2456 | assert(0); /* impossible */ |
| 2457 | } |
| 2458 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 2459 | |
| 2460 | if (mainPause) { |
| 2461 | int unused; |
| 2462 | DISPLAY("Press Enter \n"); |
| 2463 | unused = getchar(); |
| 2464 | (void)unused; |
| 2465 | } |
| 2466 | return result; |
| 2467 | } |