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 | /*-************************************ |
| 13 | * Compiler specific |
| 14 | **************************************/ |
| 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 | /*-************************************ |
| 23 | * Includes |
| 24 | **************************************/ |
| 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 */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 29 | #include "mem.h" |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 30 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem, ZSTD_getDictID_fromFrame */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 31 | #include "zstd.h" /* ZSTD_compressBound */ |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 32 | #include "zstd_errors.h" /* ZSTD_error_srcSize_wrong */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 33 | #include "zstdmt_compress.h" |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 34 | #include "zdict.h" /* ZDICT_trainFromBuffer */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 35 | #include "datagen.h" /* RDG_genBuffer */ |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 36 | #define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 37 | #include "xxhash.h" /* XXH64_* */ |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 38 | #include "seqgen.h" |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 39 | #include "util.h" |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 40 | |
| 41 | |
| 42 | /*-************************************ |
| 43 | * Constants |
| 44 | **************************************/ |
| 45 | #define KB *(1U<<10) |
| 46 | #define MB *(1U<<20) |
| 47 | #define GB *(1U<<30) |
| 48 | |
| 49 | static const U32 nbTestsDefault = 10000; |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 50 | static const U32 g_cLevelMax_smallTests = 10; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 51 | #define COMPRESSIBLE_NOISE_LENGTH (10 MB) |
| 52 | #define FUZ_COMPRESSIBILITY_DEFAULT 50 |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 53 | static const U32 prime32 = 2654435761U; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 54 | |
| 55 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 56 | /*-************************************ |
| 57 | * Display Macros |
| 58 | **************************************/ |
| 59 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 60 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { \ |
| 61 | DISPLAY(__VA_ARGS__); \ |
| 62 | if (g_displayLevel>=4) fflush(stderr); } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 63 | static U32 g_displayLevel = 2; |
| 64 | |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 65 | static const U64 g_refreshRate = SEC_TO_MICRO / 6; |
| 66 | static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 67 | |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 68 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
| 69 | if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ |
| 70 | { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \ |
| 71 | if (g_displayLevel>=4) fflush(stderr); } } |
| 72 | |
| 73 | static U64 g_clockTime = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 74 | |
| 75 | |
| 76 | /*-******************************************************* |
| 77 | * Fuzzer functions |
| 78 | *********************************************************/ |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 79 | #undef MIN |
| 80 | #undef MAX |
| 81 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 82 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 83 | /*! FUZ_rand() : |
| 84 | @return : a 27 bits random value, from a 32-bits `seed`. |
| 85 | `seed` is also modified */ |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 86 | #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 87 | unsigned int FUZ_rand(unsigned int* seedPtr) |
| 88 | { |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 89 | static const U32 prime2 = 2246822519U; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 90 | U32 rand32 = *seedPtr; |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 91 | rand32 *= prime32; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 92 | rand32 += prime2; |
| 93 | rand32 = FUZ_rotl32(rand32, 13); |
| 94 | *seedPtr = rand32; |
| 95 | return rand32 >> 5; |
| 96 | } |
| 97 | |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 98 | #define CHECK(cond, ...) { \ |
| 99 | if (cond) { \ |
| 100 | DISPLAY("Error => "); \ |
| 101 | DISPLAY(__VA_ARGS__); \ |
| 102 | DISPLAY(" (seed %u, test nb %u, line %u) \n", \ |
| 103 | seed, testNb, __LINE__); \ |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 104 | goto _output_error; \ |
| 105 | } } |
| 106 | |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 107 | #define CHECK_Z(f) { \ |
| 108 | size_t const err = f; \ |
| 109 | CHECK(ZSTD_isError(err), "%s : %s ", \ |
| 110 | #f, ZSTD_getErrorName(err)); \ |
| 111 | } |
| 112 | |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 113 | |
| 114 | /*====================================================== |
| 115 | * Basic Unit tests |
| 116 | ======================================================*/ |
| 117 | |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 118 | typedef struct { |
| 119 | void* start; |
| 120 | size_t size; |
| 121 | size_t filled; |
| 122 | } buffer_t; |
| 123 | |
| 124 | static const buffer_t g_nullBuffer = { NULL, 0 , 0 }; |
| 125 | |
| 126 | static buffer_t FUZ_createDictionary(const void* src, size_t srcSize, size_t blockSize, size_t requestedDictSize) |
| 127 | { |
| 128 | buffer_t dict = { NULL, 0, 0 }; |
| 129 | size_t const nbBlocks = (srcSize + (blockSize-1)) / blockSize; |
| 130 | size_t* const blockSizes = (size_t*) malloc(nbBlocks * sizeof(size_t)); |
| 131 | if (!blockSizes) return dict; |
| 132 | dict.start = malloc(requestedDictSize); |
| 133 | if (!dict.start) { free(blockSizes); return dict; } |
| 134 | { size_t nb; |
| 135 | for (nb=0; nb<nbBlocks-1; nb++) blockSizes[nb] = blockSize; |
| 136 | blockSizes[nbBlocks-1] = srcSize - (blockSize * (nbBlocks-1)); |
| 137 | } |
| 138 | { size_t const dictSize = ZDICT_trainFromBuffer(dict.start, requestedDictSize, src, blockSizes, (unsigned)nbBlocks); |
| 139 | free(blockSizes); |
Yann Collet | 2c5514c | 2017-04-18 22:52:41 -0700 | [diff] [blame] | 140 | if (ZDICT_isError(dictSize)) { free(dict.start); return g_nullBuffer; } |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 141 | dict.size = requestedDictSize; |
| 142 | dict.filled = dictSize; |
| 143 | return dict; /* how to return dictSize ? */ |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static void FUZ_freeDictionary(buffer_t dict) |
| 148 | { |
| 149 | free(dict.start); |
| 150 | } |
| 151 | |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 152 | /* Round trips data and updates xxh with the decompressed data produced */ |
| 153 | static size_t SEQ_roundTrip(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx, |
| 154 | XXH64_state_t* xxh, void* data, size_t size, |
| 155 | ZSTD_EndDirective endOp) |
| 156 | { |
| 157 | static BYTE compressed[1024]; |
| 158 | static BYTE uncompressed[1024]; |
| 159 | |
| 160 | ZSTD_inBuffer cin = {data, size, 0}; |
| 161 | size_t cret; |
| 162 | |
| 163 | do { |
| 164 | ZSTD_outBuffer cout = {compressed, sizeof(compressed), 0}; |
| 165 | ZSTD_inBuffer din = {compressed, 0, 0}; |
| 166 | ZSTD_outBuffer dout = {uncompressed, 0, 0}; |
| 167 | |
| 168 | cret = ZSTD_compress_generic(cctx, &cout, &cin, endOp); |
| 169 | if (ZSTD_isError(cret)) |
| 170 | return cret; |
| 171 | |
| 172 | din.size = cout.pos; |
| 173 | while (din.pos < din.size || (endOp == ZSTD_e_end && cret == 0)) { |
| 174 | size_t dret; |
| 175 | |
| 176 | dout.pos = 0; |
| 177 | dout.size = sizeof(uncompressed); |
| 178 | dret = ZSTD_decompressStream(dctx, &dout, &din); |
| 179 | if (ZSTD_isError(dret)) |
| 180 | return dret; |
| 181 | XXH64_update(xxh, dout.dst, dout.pos); |
| 182 | if (dret == 0) |
| 183 | break; |
| 184 | } |
| 185 | } while (cin.pos < cin.size || (endOp != ZSTD_e_continue && cret != 0)); |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /* Generates some data and round trips it */ |
| 190 | static size_t SEQ_generateRoundTrip(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx, |
| 191 | XXH64_state_t* xxh, SEQ_stream* seq, |
| 192 | SEQ_gen_type type, unsigned value) |
| 193 | { |
| 194 | static BYTE data[1024]; |
| 195 | size_t gen; |
| 196 | |
| 197 | do { |
| 198 | SEQ_outBuffer sout = {data, sizeof(data), 0}; |
| 199 | size_t ret; |
| 200 | gen = SEQ_gen(seq, type, value, &sout); |
| 201 | |
| 202 | ret = SEQ_roundTrip(cctx, dctx, xxh, sout.dst, sout.pos, ZSTD_e_continue); |
| 203 | if (ZSTD_isError(ret)) |
| 204 | return ret; |
| 205 | } while (gen != 0); |
| 206 | |
| 207 | return 0; |
| 208 | } |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 209 | |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 210 | static int basicUnitTests(U32 seed, double compressibility) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 211 | { |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 212 | size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 213 | void* CNBuffer = malloc(CNBufferSize); |
Nick Terrell | e19b082 | 2017-11-01 13:10:03 -0700 | [diff] [blame] | 214 | size_t const skippableFrameSize = 200 KB; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 215 | size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH); |
| 216 | void* compressedBuffer = malloc(compressedBufferSize); |
| 217 | size_t const decodedBufferSize = CNBufferSize; |
| 218 | void* decodedBuffer = malloc(decodedBufferSize); |
| 219 | size_t cSize; |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 220 | int testResult = 0; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 221 | U32 testNb = 1; |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 222 | ZSTD_CStream* zc = ZSTD_createCStream(); |
| 223 | ZSTD_DStream* zd = ZSTD_createDStream(); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 224 | ZSTDMT_CCtx* mtctx = ZSTDMT_createCCtx(2); |
| 225 | |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 226 | ZSTD_inBuffer inBuff, inBuff2; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 227 | ZSTD_outBuffer outBuff; |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 228 | buffer_t dictionary = g_nullBuffer; |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 229 | size_t const dictSize = 128 KB; |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 230 | unsigned dictID = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 231 | |
| 232 | /* Create compressible test buffer */ |
| 233 | if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) { |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 234 | DISPLAY("Not enough memory, aborting \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 235 | goto _output_error; |
| 236 | } |
| 237 | RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed); |
| 238 | |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 239 | /* Create dictionary */ |
Yann Collet | e0065cf | 2017-09-28 18:34:38 -0700 | [diff] [blame] | 240 | DISPLAYLEVEL(3, "creating dictionary for unit tests \n"); |
| 241 | dictionary = FUZ_createDictionary(CNBuffer, CNBufferSize / 2, 8 KB, 40 KB); |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 242 | if (!dictionary.start) { |
| 243 | DISPLAY("Error creating dictionary, aborting \n"); |
| 244 | goto _output_error; |
| 245 | } |
| 246 | dictID = ZDICT_getDictID(dictionary.start, dictionary.filled); |
| 247 | |
Yann Collet | 21f7672 | 2017-12-07 03:06:01 -0500 | [diff] [blame] | 248 | /* Basic compression test */ |
| 249 | DISPLAYLEVEL(3, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
| 250 | CHECK_Z( ZSTD_initCStream(zc, 1 /* cLevel */) ); |
| 251 | outBuff.dst = (char*)(compressedBuffer); |
| 252 | outBuff.size = compressedBufferSize; |
| 253 | outBuff.pos = 0; |
| 254 | inBuff.src = CNBuffer; |
| 255 | inBuff.size = CNBufferSize; |
| 256 | inBuff.pos = 0; |
| 257 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
| 258 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 259 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 260 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 261 | DISPLAYLEVEL(3, "OK (%u bytes)\n", (U32)outBuff.pos); |
| 262 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 263 | /* generate skippable frame */ |
| 264 | MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START); |
| 265 | MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize); |
| 266 | cSize = skippableFrameSize + 8; |
| 267 | |
Yann Collet | 21f7672 | 2017-12-07 03:06:01 -0500 | [diff] [blame] | 268 | /* Basic compression test using dict */ |
| 269 | DISPLAYLEVEL(3, "test%3i : skipframe + compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
Yann Collet | 3c1e3f8 | 2017-10-13 18:32:06 -0700 | [diff] [blame] | 270 | CHECK_Z( ZSTD_initCStream_usingDict(zc, CNBuffer, dictSize, 1 /* cLevel */) ); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 271 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
Yann Collet | 3c1e3f8 | 2017-10-13 18:32:06 -0700 | [diff] [blame] | 272 | assert(compressedBufferSize > cSize); |
| 273 | outBuff.size = compressedBufferSize - cSize; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 274 | outBuff.pos = 0; |
| 275 | inBuff.src = CNBuffer; |
| 276 | inBuff.size = CNBufferSize; |
| 277 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 278 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 279 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 280 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
Yann Collet | 9a021c1 | 2016-08-26 09:05:06 +0200 | [diff] [blame] | 281 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 282 | cSize += outBuff.pos; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 283 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 284 | |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 285 | /* context size functions */ |
| 286 | DISPLAYLEVEL(3, "test%3i : estimate CStream size : ", testNb++); |
| 287 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictSize); |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 288 | size_t const cstreamSize = ZSTD_estimateCStreamSize_usingCParams(cParams); |
| 289 | size_t const cdictSize = ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy); /* uses ZSTD_initCStream_usingDict() */ |
| 290 | if (ZSTD_isError(cstreamSize)) goto _output_error; |
| 291 | if (ZSTD_isError(cdictSize)) goto _output_error; |
| 292 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)(cstreamSize + cdictSize)); |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | DISPLAYLEVEL(3, "test%3i : check actual CStream size : ", testNb++); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 296 | { size_t const s = ZSTD_sizeof_CStream(zc); |
| 297 | if (ZSTD_isError(s)) goto _output_error; |
| 298 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 301 | /* Attempt bad compression parameters */ |
| 302 | DISPLAYLEVEL(3, "test%3i : use bad compression parameters : ", testNb++); |
| 303 | { size_t r; |
| 304 | ZSTD_parameters params = ZSTD_getParams(1, 0, 0); |
| 305 | params.cParams.searchLength = 2; |
| 306 | r = ZSTD_initCStream_advanced(zc, NULL, 0, params, 0); |
| 307 | if (!ZSTD_isError(r)) goto _output_error; |
| 308 | DISPLAYLEVEL(3, "init error : %s \n", ZSTD_getErrorName(r)); |
| 309 | } |
| 310 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 311 | /* skippable frame test */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 312 | DISPLAYLEVEL(3, "test%3i : decompress skippable frame : ", testNb++); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 313 | CHECK_Z( ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize) ); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 314 | inBuff.src = compressedBuffer; |
| 315 | inBuff.size = cSize; |
| 316 | inBuff.pos = 0; |
| 317 | outBuff.dst = decodedBuffer; |
| 318 | outBuff.size = CNBufferSize; |
| 319 | outBuff.pos = 0; |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 320 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 321 | DISPLAYLEVEL(5, " ( ZSTD_decompressStream => %u ) ", (U32)r); |
| 322 | if (r != 0) goto _output_error; |
| 323 | } |
Yann Collet | a33ae64 | 2017-02-28 01:15:28 -0800 | [diff] [blame] | 324 | 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] | 325 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 326 | |
| 327 | /* Basic decompression test */ |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 328 | inBuff2 = inBuff; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 329 | DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 330 | ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 331 | CHECK_Z( ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000000000) ); /* large limit */ |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 332 | { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 333 | if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
| 334 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 335 | 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] | 336 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 337 | |
| 338 | /* Re-use without init */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 339 | 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] | 340 | outBuff.pos = 0; |
| 341 | { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2); |
| 342 | 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] | 343 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 344 | 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] | 345 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 346 | |
| 347 | /* check regenerated data is byte exact */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 348 | DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 349 | { size_t i; |
| 350 | for (i=0; i<CNBufferSize; i++) { |
| 351 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error; |
| 352 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 353 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 354 | |
Yann Collet | f16f449 | 2017-05-09 16:18:17 -0700 | [diff] [blame] | 355 | /* context size functions */ |
| 356 | DISPLAYLEVEL(3, "test%3i : estimate DStream size : ", testNb++); |
| 357 | { ZSTD_frameHeader fhi; |
| 358 | const void* cStart = (char*)compressedBuffer + (skippableFrameSize + 8); |
| 359 | size_t const gfhError = ZSTD_getFrameHeader(&fhi, cStart, cSize); |
| 360 | if (gfhError!=0) goto _output_error; |
Yann Collet | dde10b2 | 2017-06-26 17:44:26 -0700 | [diff] [blame] | 361 | DISPLAYLEVEL(5, " (windowSize : %u) ", (U32)fhi.windowSize); |
| 362 | { size_t const s = ZSTD_estimateDStreamSize(fhi.windowSize) |
Yann Collet | 25989e3 | 2017-05-25 15:07:37 -0700 | [diff] [blame] | 363 | /* uses ZSTD_initDStream_usingDict() */ |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 364 | + ZSTD_estimateDDictSize(dictSize, ZSTD_dlm_byCopy); |
Yann Collet | f16f449 | 2017-05-09 16:18:17 -0700 | [diff] [blame] | 365 | if (ZSTD_isError(s)) goto _output_error; |
| 366 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s); |
| 367 | } } |
| 368 | |
| 369 | DISPLAYLEVEL(3, "test%3i : check actual DStream size : ", testNb++); |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 370 | { size_t const s = ZSTD_sizeof_DStream(zd); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 371 | if (ZSTD_isError(s)) goto _output_error; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 372 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 375 | /* Byte-by-byte decompression test */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 376 | DISPLAYLEVEL(3, "test%3i : decompress byte-by-byte : ", testNb++); |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 377 | { /* skippable frame */ |
| 378 | size_t r = 1; |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 379 | ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 380 | inBuff.src = compressedBuffer; |
| 381 | outBuff.dst = decodedBuffer; |
| 382 | inBuff.pos = 0; |
| 383 | outBuff.pos = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 384 | while (r) { /* skippable frame */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 385 | inBuff.size = inBuff.pos + 1; |
| 386 | outBuff.size = outBuff.pos + 1; |
| 387 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 388 | if (ZSTD_isError(r)) goto _output_error; |
| 389 | } |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 390 | /* normal frame */ |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 391 | ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 392 | r=1; |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 393 | while (r) { |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 394 | inBuff.size = inBuff.pos + 1; |
| 395 | outBuff.size = outBuff.pos + 1; |
| 396 | r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 397 | if (ZSTD_isError(r)) goto _output_error; |
| 398 | } |
| 399 | } |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 400 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 401 | 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] | 402 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 403 | |
| 404 | /* check regenerated data is byte exact */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 405 | DISPLAYLEVEL(3, "test%3i : check decompressed result : ", testNb++); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 406 | { size_t i; |
| 407 | for (i=0; i<CNBufferSize; i++) { |
| 408 | if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; |
| 409 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 410 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 411 | |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 412 | /* _srcSize compression test */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 413 | DISPLAYLEVEL(3, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 414 | ZSTD_initCStream_srcSize(zc, 1, CNBufferSize); |
Yann Collet | d564faa | 2016-12-18 21:39:15 +0100 | [diff] [blame] | 415 | outBuff.dst = (char*)(compressedBuffer); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 416 | outBuff.size = compressedBufferSize; |
| 417 | outBuff.pos = 0; |
| 418 | inBuff.src = CNBuffer; |
| 419 | inBuff.size = CNBufferSize; |
| 420 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 421 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 422 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 423 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 424 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
Sean Purcell | 4e70971 | 2017-02-07 13:50:09 -0800 | [diff] [blame] | 425 | { unsigned long long origSize = ZSTD_findDecompressedSize(outBuff.dst, outBuff.pos); |
Yann Collet | d564faa | 2016-12-18 21:39:15 +0100 | [diff] [blame] | 426 | if ((size_t)origSize != CNBufferSize) goto _output_error; } /* exact original size must be present */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 427 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 428 | |
| 429 | /* wrong _srcSize compression test */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 430 | DISPLAYLEVEL(3, "test%3i : wrong srcSize : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 431 | ZSTD_initCStream_srcSize(zc, 1, CNBufferSize-1); |
Yann Collet | d564faa | 2016-12-18 21:39:15 +0100 | [diff] [blame] | 432 | outBuff.dst = (char*)(compressedBuffer); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 433 | outBuff.size = compressedBufferSize; |
| 434 | outBuff.pos = 0; |
| 435 | inBuff.src = CNBuffer; |
| 436 | inBuff.size = CNBufferSize; |
| 437 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 438 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 439 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 440 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 441 | 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] | 442 | DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r)); } |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 443 | |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 444 | /* Complex context re-use scenario */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 445 | DISPLAYLEVEL(3, "test%3i : context re-use : ", testNb++); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 446 | ZSTD_freeCStream(zc); |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 447 | zc = ZSTD_createCStream(); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 448 | if (zc==NULL) goto _output_error; /* memory allocation issue */ |
| 449 | /* use 1 */ |
| 450 | { size_t const inSize = 513; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 451 | DISPLAYLEVEL(5, "use1 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 452 | ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */ |
| 453 | inBuff.src = CNBuffer; |
| 454 | inBuff.size = inSize; |
| 455 | inBuff.pos = 0; |
| 456 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 457 | outBuff.size = ZSTD_compressBound(inSize); |
| 458 | outBuff.pos = 0; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 459 | DISPLAYLEVEL(5, "compress1 "); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 460 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 461 | 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] | 462 | DISPLAYLEVEL(5, "end1 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 463 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 464 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 465 | } |
| 466 | /* use 2 */ |
| 467 | { 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] | 468 | DISPLAYLEVEL(5, "use2 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 469 | ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */ |
| 470 | inBuff.src = CNBuffer; |
| 471 | inBuff.size = inSize; |
| 472 | inBuff.pos = 0; |
| 473 | outBuff.dst = (char*)(compressedBuffer)+cSize; |
| 474 | outBuff.size = ZSTD_compressBound(inSize); |
| 475 | outBuff.pos = 0; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 476 | DISPLAYLEVEL(5, "compress2 "); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 477 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 478 | 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] | 479 | DISPLAYLEVEL(5, "end2 "); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 480 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 481 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 482 | } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 483 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 484 | |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 485 | /* CDict scenario */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 486 | DISPLAYLEVEL(3, "test%3i : digested dictionary : ", testNb++); |
Yann Collet | 2e42742 | 2017-06-27 17:09:12 -0700 | [diff] [blame] | 487 | { ZSTD_CDict* const cdict = ZSTD_createCDict(dictionary.start, dictionary.filled, 1 /*byRef*/ ); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 488 | size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict); |
Yann Collet | 15768ca | 2017-11-16 15:02:28 -0800 | [diff] [blame] | 489 | DISPLAYLEVEL(5, "ZSTD_initCStream_usingCDict result : %u ", (U32)initError); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 490 | if (ZSTD_isError(initError)) goto _output_error; |
| 491 | cSize = 0; |
| 492 | outBuff.dst = compressedBuffer; |
| 493 | outBuff.size = compressedBufferSize; |
| 494 | outBuff.pos = 0; |
| 495 | inBuff.src = CNBuffer; |
| 496 | inBuff.size = CNBufferSize; |
| 497 | inBuff.pos = 0; |
Yann Collet | 15768ca | 2017-11-16 15:02:28 -0800 | [diff] [blame] | 498 | DISPLAYLEVEL(5, "- starting ZSTD_compressStream "); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 499 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 500 | 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] | 501 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 502 | DISPLAYLEVEL(5, "- ZSTD_endStream result : %u ", (U32)r); |
| 503 | if (r != 0) goto _output_error; /* error, or some data not flushed */ |
| 504 | } |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 505 | cSize = outBuff.pos; |
| 506 | ZSTD_freeCDict(cdict); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 507 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 510 | DISPLAYLEVEL(3, "test%3i : check CStream size : ", testNb++); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 511 | { size_t const s = ZSTD_sizeof_CStream(zc); |
| 512 | if (ZSTD_isError(s)) goto _output_error; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 513 | DISPLAYLEVEL(3, "OK (%u bytes) \n", (U32)s); |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 516 | DISPLAYLEVEL(4, "test%3i : check Dictionary ID : ", testNb++); |
| 517 | { unsigned const dID = ZSTD_getDictID_fromFrame(compressedBuffer, cSize); |
| 518 | if (dID != dictID) goto _output_error; |
| 519 | DISPLAYLEVEL(4, "OK (%u) \n", dID); |
| 520 | } |
| 521 | |
Yann Collet | 335ad5d | 2016-10-25 17:47:02 -0700 | [diff] [blame] | 522 | /* DDict scenario */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 523 | DISPLAYLEVEL(3, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize); |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 524 | { ZSTD_DDict* const ddict = ZSTD_createDDict(dictionary.start, dictionary.filled); |
Yann Collet | 335ad5d | 2016-10-25 17:47:02 -0700 | [diff] [blame] | 525 | size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict); |
| 526 | if (ZSTD_isError(initError)) goto _output_error; |
Yann Collet | 5098d1f | 2017-12-12 12:48:31 -0800 | [diff] [blame] | 527 | inBuff.src = compressedBuffer; |
| 528 | inBuff.size = cSize; |
| 529 | inBuff.pos = 0; |
Yann Collet | 8a104fd | 2017-12-12 12:51:49 -0800 | [diff] [blame^] | 530 | outBuff.dst = decodedBuffer; |
| 531 | outBuff.size = CNBufferSize; |
| 532 | outBuff.pos = 0; |
Yann Collet | 335ad5d | 2016-10-25 17:47:02 -0700 | [diff] [blame] | 533 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 534 | if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */ |
| 535 | if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */ |
| 536 | if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */ |
| 537 | ZSTD_freeDDict(ddict); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 538 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 335ad5d | 2016-10-25 17:47:02 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Yann Collet | 12083a4 | 2016-09-06 15:01:51 +0200 | [diff] [blame] | 541 | /* test ZSTD_setDStreamParameter() resilience */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 542 | DISPLAYLEVEL(3, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++); |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 543 | { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */ |
| 544 | if (!ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 545 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 546 | |
| 547 | /* Memory restriction */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 548 | DISPLAYLEVEL(3, "test%3i : maxWindowSize < frame requirement : ", testNb++); |
Yann Collet | 30ab64e | 2017-05-10 11:30:19 -0700 | [diff] [blame] | 549 | ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 550 | CHECK_Z( ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1000) ); /* too small limit */ |
Yann Collet | 5098d1f | 2017-12-12 12:48:31 -0800 | [diff] [blame] | 551 | inBuff.src = compressedBuffer; |
| 552 | inBuff.size = cSize; |
| 553 | inBuff.pos = 0; |
Yann Collet | 8a104fd | 2017-12-12 12:51:49 -0800 | [diff] [blame^] | 554 | outBuff.dst = decodedBuffer; |
| 555 | outBuff.size = CNBufferSize; |
| 556 | outBuff.pos = 0; |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 557 | { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 558 | 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] | 559 | DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); } |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 560 | |
Yann Collet | 7d283cd | 2017-04-27 14:48:34 -0700 | [diff] [blame] | 561 | DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_usingCDict_advanced with masked dictID : ", testNb++); |
| 562 | { ZSTD_compressionParameters const cParams = ZSTD_getCParams(1, CNBufferSize, dictionary.filled); |
| 563 | ZSTD_frameParameters const fParams = { 1 /* contentSize */, 1 /* checksum */, 1 /* noDictID */}; |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 564 | ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dm_auto, cParams, ZSTD_defaultCMem); |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 565 | size_t const initError = ZSTD_initCStream_usingCDict_advanced(zc, cdict, fParams, CNBufferSize); |
Yann Collet | 7d283cd | 2017-04-27 14:48:34 -0700 | [diff] [blame] | 566 | if (ZSTD_isError(initError)) goto _output_error; |
Nick Terrell | 62ecad3 | 2017-04-03 20:56:39 -0700 | [diff] [blame] | 567 | cSize = 0; |
| 568 | outBuff.dst = compressedBuffer; |
| 569 | outBuff.size = compressedBufferSize; |
| 570 | outBuff.pos = 0; |
| 571 | inBuff.src = CNBuffer; |
| 572 | inBuff.size = CNBufferSize; |
| 573 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 574 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 575 | 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] | 576 | { size_t const r = ZSTD_endStream(zc, &outBuff); |
| 577 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 578 | cSize = outBuff.pos; |
| 579 | ZSTD_freeCDict(cdict); |
| 580 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100); |
Yann Collet | 4ee6b15 | 2017-04-11 11:59:44 -0700 | [diff] [blame] | 581 | } |
Nick Terrell | 62ecad3 | 2017-04-03 20:56:39 -0700 | [diff] [blame] | 582 | |
Yann Collet | a92cbb7 | 2017-04-27 15:08:56 -0700 | [diff] [blame] | 583 | DISPLAYLEVEL(3, "test%3i : try retrieving dictID from frame : ", testNb++); |
| 584 | { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize); |
| 585 | if (did != 0) goto _output_error; |
| 586 | } |
| 587 | DISPLAYLEVEL(3, "OK (not detected) \n"); |
| 588 | |
Yann Collet | 4ee6b15 | 2017-04-11 11:59:44 -0700 | [diff] [blame] | 589 | DISPLAYLEVEL(3, "test%3i : decompress without dictionary : ", testNb++); |
| 590 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize); |
| 591 | if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */ |
| 592 | DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); |
Nick Terrell | 62ecad3 | 2017-04-03 20:56:39 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 595 | DISPLAYLEVEL(3, "test%3i : compress with ZSTD_CCtx_refPrefix : ", testNb++); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 596 | CHECK_Z( ZSTD_CCtx_refPrefix(zc, dictionary.start, dictionary.filled) ); |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 597 | outBuff.dst = compressedBuffer; |
| 598 | outBuff.size = compressedBufferSize; |
| 599 | outBuff.pos = 0; |
| 600 | inBuff.src = CNBuffer; |
| 601 | inBuff.size = CNBufferSize; |
| 602 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 603 | CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end) ); |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 604 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 605 | cSize = outBuff.pos; |
| 606 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100); |
| 607 | |
| 608 | DISPLAYLEVEL(3, "test%3i : decompress with dictionary : ", testNb++); |
| 609 | { size_t const r = ZSTD_decompress_usingDict(zd, |
| 610 | decodedBuffer, CNBufferSize, |
| 611 | compressedBuffer, cSize, |
| 612 | dictionary.start, dictionary.filled); |
| 613 | if (ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */ |
| 614 | DISPLAYLEVEL(3, "OK \n"); |
| 615 | } |
| 616 | |
| 617 | DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should fail): ", testNb++); |
| 618 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize); |
| 619 | if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */ |
| 620 | DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); |
| 621 | } |
| 622 | |
| 623 | DISPLAYLEVEL(3, "test%3i : compress again with ZSTD_compress_generic : ", testNb++); |
| 624 | outBuff.dst = compressedBuffer; |
| 625 | outBuff.size = compressedBufferSize; |
| 626 | outBuff.pos = 0; |
| 627 | inBuff.src = CNBuffer; |
| 628 | inBuff.size = CNBufferSize; |
| 629 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 630 | CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end) ); |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 631 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 632 | cSize = outBuff.pos; |
| 633 | DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100); |
| 634 | |
| 635 | DISPLAYLEVEL(3, "test%3i : decompress without dictionary (should work): ", testNb++); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 636 | CHECK_Z( ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize) ); |
| 637 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 62f7efc | 2017-06-28 16:25:13 -0700 | [diff] [blame] | 638 | |
Yann Collet | 0bb381d | 2017-04-18 15:08:52 -0700 | [diff] [blame] | 639 | /* Empty srcSize */ |
| 640 | DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_advanced with pledgedSrcSize=0 and dict : ", testNb++); |
| 641 | { ZSTD_parameters params = ZSTD_getParams(5, 0, 0); |
| 642 | params.fParams.contentSizeFlag = 1; |
Yann Collet | 213ef3b | 2017-10-13 19:01:58 -0700 | [diff] [blame] | 643 | 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] | 644 | } /* cstream advanced shall write content size = 0 */ |
Yann Collet | 5098d1f | 2017-12-12 12:48:31 -0800 | [diff] [blame] | 645 | inBuff.src = CNBuffer; |
| 646 | inBuff.size = 0; |
| 647 | inBuff.pos = 0; |
Yann Collet | 8a104fd | 2017-12-12 12:51:49 -0800 | [diff] [blame^] | 648 | outBuff.dst = compressedBuffer; |
| 649 | outBuff.size = compressedBufferSize; |
| 650 | outBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 651 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 0bb381d | 2017-04-18 15:08:52 -0700 | [diff] [blame] | 652 | if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error; |
| 653 | cSize = outBuff.pos; |
| 654 | if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error; |
| 655 | DISPLAYLEVEL(3, "OK \n"); |
| 656 | |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 657 | DISPLAYLEVEL(3, "test%3i : pledgedSrcSize == 0 behaves properly : ", testNb++); |
| 658 | { ZSTD_parameters params = ZSTD_getParams(5, 0, 0); |
| 659 | params.fParams.contentSizeFlag = 1; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 660 | CHECK_Z( ZSTD_initCStream_advanced(zc, NULL, 0, params, 0) ); |
Yann Collet | 4ee6b15 | 2017-04-11 11:59:44 -0700 | [diff] [blame] | 661 | } /* cstream advanced shall write content size = 0 */ |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 662 | inBuff.src = CNBuffer; |
| 663 | inBuff.size = 0; |
| 664 | inBuff.pos = 0; |
| 665 | outBuff.dst = compressedBuffer; |
| 666 | outBuff.size = compressedBufferSize; |
| 667 | outBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 668 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 669 | if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error; |
| 670 | cSize = outBuff.pos; |
| 671 | if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != 0) goto _output_error; |
| 672 | |
| 673 | ZSTD_resetCStream(zc, 0); /* resetCStream should treat 0 as unknown */ |
Yann Collet | 5098d1f | 2017-12-12 12:48:31 -0800 | [diff] [blame] | 674 | inBuff.src = CNBuffer; |
| 675 | inBuff.size = 0; |
| 676 | inBuff.pos = 0; |
Yann Collet | 8a104fd | 2017-12-12 12:51:49 -0800 | [diff] [blame^] | 677 | outBuff.dst = compressedBuffer; |
| 678 | outBuff.size = compressedBufferSize; |
| 679 | outBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 680 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 681 | if (ZSTD_endStream(zc, &outBuff) != 0) goto _output_error; |
| 682 | cSize = outBuff.pos; |
| 683 | if (ZSTD_findDecompressedSize(compressedBuffer, cSize) != ZSTD_CONTENTSIZE_UNKNOWN) goto _output_error; |
| 684 | DISPLAYLEVEL(3, "OK \n"); |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 685 | |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 686 | /* Basic multithreading compression test */ |
| 687 | 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] | 688 | { ZSTD_parameters const params = ZSTD_getParams(1, 0, 0); |
| 689 | CHECK_Z( ZSTDMT_initCStream_advanced(mtctx, CNBuffer, dictSize, params, CNBufferSize) ); |
| 690 | } |
Yann Collet | 8a104fd | 2017-12-12 12:51:49 -0800 | [diff] [blame^] | 691 | outBuff.dst = (char*)(compressedBuffer); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 692 | outBuff.size = compressedBufferSize; |
| 693 | outBuff.pos = 0; |
| 694 | inBuff.src = CNBuffer; |
| 695 | inBuff.size = CNBufferSize; |
| 696 | inBuff.pos = 0; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 697 | CHECK_Z( ZSTDMT_compressStream_generic(mtctx, &outBuff, &inBuff, ZSTD_e_end) ); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 698 | if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */ |
| 699 | { size_t const r = ZSTDMT_endStream(mtctx, &outBuff); |
| 700 | if (r != 0) goto _output_error; } /* error, or some data not flushed */ |
| 701 | DISPLAYLEVEL(3, "OK \n"); |
| 702 | |
Nick Terrell | e600b5d | 2017-10-16 17:18:43 -0700 | [diff] [blame] | 703 | DISPLAYLEVEL(3, "test%3i : check dictionary FSE tables can represent every code : ", testNb++); |
| 704 | { unsigned const kMaxWindowLog = 24; |
| 705 | unsigned value; |
| 706 | ZSTD_compressionParameters cParams = ZSTD_getCParams(3, 1U << kMaxWindowLog, 1024); |
| 707 | ZSTD_CDict* cdict; |
| 708 | ZSTD_DDict* ddict; |
| 709 | SEQ_stream seq = SEQ_initStream(0x87654321); |
| 710 | SEQ_gen_type type; |
| 711 | XXH64_state_t xxh; |
| 712 | |
| 713 | XXH64_reset(&xxh, 0); |
| 714 | cParams.windowLog = kMaxWindowLog; |
| 715 | cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dm_fullDict, cParams, ZSTD_defaultCMem); |
| 716 | ddict = ZSTD_createDDict(dictionary.start, dictionary.filled); |
| 717 | |
| 718 | if (!cdict || !ddict) goto _output_error; |
| 719 | |
| 720 | ZSTD_CCtx_reset(zc); |
| 721 | ZSTD_resetDStream(zd); |
| 722 | CHECK_Z(ZSTD_CCtx_refCDict(zc, cdict)); |
| 723 | CHECK_Z(ZSTD_initDStream_usingDDict(zd, ddict)); |
| 724 | CHECK_Z(ZSTD_setDStreamParameter(zd, DStream_p_maxWindowSize, 1U << kMaxWindowLog)); |
| 725 | /* Test all values < 300 */ |
| 726 | for (value = 0; value < 300; ++value) { |
| 727 | for (type = (SEQ_gen_type)0; type < SEQ_gen_max; ++type) { |
| 728 | CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value)); |
| 729 | } |
| 730 | } |
| 731 | /* Test values 2^8 to 2^17 */ |
| 732 | for (value = (1 << 8); value < (1 << 17); value <<= 1) { |
| 733 | for (type = (SEQ_gen_type)0; type < SEQ_gen_max; ++type) { |
| 734 | CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value)); |
| 735 | CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, type, value + (value >> 2))); |
| 736 | } |
| 737 | } |
| 738 | /* Test offset values up to the max window log */ |
| 739 | for (value = 8; value <= kMaxWindowLog; ++value) { |
| 740 | CHECK_Z(SEQ_generateRoundTrip(zc, zd, &xxh, &seq, SEQ_gen_of, (1U << value) - 1)); |
| 741 | } |
| 742 | |
| 743 | CHECK_Z(SEQ_roundTrip(zc, zd, &xxh, NULL, 0, ZSTD_e_end)); |
| 744 | CHECK(SEQ_digest(&seq) != XXH64_digest(&xxh), "SEQ XXH64 does not match"); |
| 745 | |
| 746 | ZSTD_freeCDict(cdict); |
| 747 | ZSTD_freeDDict(ddict); |
| 748 | } |
| 749 | DISPLAYLEVEL(3, "OK \n"); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 750 | |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 751 | /* Overlen overwriting window data bug */ |
| 752 | DISPLAYLEVEL(3, "test%3i : wildcopy doesn't overwrite potential match data : ", testNb++); |
Sean Purcell | 0ed3901 | 2017-02-16 13:29:47 -0800 | [diff] [blame] | 753 | { /* This test has a window size of 1024 bytes and consists of 3 blocks: |
| 754 | 1. 'a' repeated 517 times |
| 755 | 2. 'b' repeated 516 times |
| 756 | 3. a compressed block with no literals and 3 sequence commands: |
| 757 | litlength = 0, offset = 24, match length = 24 |
| 758 | litlength = 0, offset = 24, match length = 3 (this one creates an overlength write of length 2*WILDCOPY_OVERLENGTH - 3) |
| 759 | litlength = 0, offset = 1021, match length = 3 (this one will try to read from overwritten data if the buffer is too small) */ |
| 760 | |
| 761 | const char* testCase = |
| 762 | "\x28\xB5\x2F\xFD\x04\x00\x4C\x00\x00\x10\x61\x61\x01\x00\x00\x2A" |
| 763 | "\x80\x05\x44\x00\x00\x08\x62\x01\x00\x00\x2A\x20\x04\x5D\x00\x00" |
| 764 | "\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] | 765 | ZSTD_DStream* const zds = ZSTD_createDStream(); |
| 766 | if (zds==NULL) goto _output_error; |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 767 | |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 768 | CHECK_Z( ZSTD_initDStream(zds) ); |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 769 | inBuff.src = testCase; |
Sean Purcell | 0ed3901 | 2017-02-16 13:29:47 -0800 | [diff] [blame] | 770 | inBuff.size = 47; |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 771 | inBuff.pos = 0; |
| 772 | outBuff.dst = decodedBuffer; |
| 773 | outBuff.size = CNBufferSize; |
| 774 | outBuff.pos = 0; |
| 775 | |
| 776 | while (inBuff.pos < inBuff.size) { |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 777 | CHECK_Z( ZSTD_decompressStream(zds, &outBuff, &inBuff) ); |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 778 | } |
Przemyslaw Skibinski | 684858e | 2017-02-21 18:17:24 +0100 | [diff] [blame] | 779 | |
| 780 | ZSTD_freeDStream(zds); |
Sean Purcell | 887eaa9 | 2017-02-15 16:43:45 -0800 | [diff] [blame] | 781 | } |
| 782 | DISPLAYLEVEL(3, "OK \n"); |
| 783 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 784 | _end: |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 785 | FUZ_freeDictionary(dictionary); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 786 | ZSTD_freeCStream(zc); |
| 787 | ZSTD_freeDStream(zd); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 788 | ZSTDMT_freeCCtx(mtctx); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 789 | free(CNBuffer); |
| 790 | free(compressedBuffer); |
| 791 | free(decodedBuffer); |
| 792 | return testResult; |
| 793 | |
| 794 | _output_error: |
| 795 | testResult = 1; |
| 796 | DISPLAY("Error detected in Unit tests ! \n"); |
| 797 | goto _end; |
| 798 | } |
| 799 | |
| 800 | |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 801 | /* ====== Fuzzer tests ====== */ |
| 802 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 803 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 804 | { |
| 805 | const BYTE* b1 = (const BYTE*)buf1; |
| 806 | const BYTE* b2 = (const BYTE*)buf2; |
| 807 | size_t u; |
| 808 | for (u=0; u<max; u++) { |
| 809 | if (b1[u] != b2[u]) break; |
| 810 | } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 811 | DISPLAY("Error at position %u / %u \n", (U32)u, (U32)max); |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 812 | DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n", |
| 813 | b1[u-3], b1[u-2], b1[u-1], b1[u-0], b1[u+1], b1[u+2], b1[u+3], b1[u+4], b1[u+5]); |
| 814 | DISPLAY(" %02X %02X %02X :%02X: %02X %02X %02X %02X %02X \n", |
| 815 | b2[u-3], b2[u-2], b2[u-1], b2[u-0], 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] | 816 | return u; |
| 817 | } |
| 818 | |
| 819 | static size_t FUZ_rLogLength(U32* seed, U32 logLength) |
| 820 | { |
| 821 | size_t const lengthMask = ((size_t)1 << logLength) - 1; |
| 822 | return (lengthMask+1) + (FUZ_rand(seed) & lengthMask); |
| 823 | } |
| 824 | |
| 825 | static size_t FUZ_randomLength(U32* seed, U32 maxLog) |
| 826 | { |
| 827 | U32 const logLength = FUZ_rand(seed) % maxLog; |
| 828 | return FUZ_rLogLength(seed, logLength); |
| 829 | } |
| 830 | |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 831 | /* Return value in range minVal <= v <= maxVal */ |
| 832 | static U32 FUZ_randomClampedLength(U32* seed, U32 minVal, U32 maxVal) |
| 833 | { |
| 834 | U32 const mod = maxVal < minVal ? 1 : (maxVal + 1) - minVal; |
| 835 | return (U32)((FUZ_rand(seed) % mod) + minVal); |
| 836 | } |
| 837 | |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 838 | static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests) |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 839 | { |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 840 | U32 const maxSrcLog = bigTests ? 24 : 22; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 841 | static const U32 maxSampleLog = 19; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 842 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 843 | BYTE* cNoiseBuffer[5]; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 844 | size_t const copyBufferSize = srcBufferSize + (1<<maxSampleLog); |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 845 | BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize); |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 846 | size_t const cBufferSize = ZSTD_compressBound(srcBufferSize); |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 847 | BYTE* const cBuffer = (BYTE*)malloc (cBufferSize); |
| 848 | size_t const dstBufferSize = srcBufferSize; |
| 849 | BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 850 | U32 result = 0; |
| 851 | U32 testNb = 0; |
| 852 | U32 coreSeed = seed; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 853 | ZSTD_CStream* zc = ZSTD_createCStream(); /* will be re-created sometimes */ |
| 854 | ZSTD_DStream* zd = ZSTD_createDStream(); /* will be re-created sometimes */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 855 | ZSTD_DStream* const zd_noise = ZSTD_createDStream(); |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 856 | UTIL_time_t const startClock = UTIL_getTime(); |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 857 | const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */ |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 858 | size_t dictSize = 0; |
| 859 | U32 oldTestLog = 0; |
Yann Collet | 49f8459 | 2017-06-21 18:43:39 -0700 | [diff] [blame] | 860 | U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel() : g_cLevelMax_smallTests; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 861 | |
| 862 | /* allocations */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 863 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 864 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 865 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 866 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 867 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 868 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 869 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise , |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 870 | "Not enough memory, fuzzer tests cancelled"); |
| 871 | |
| 872 | /* Create initial samples */ |
| 873 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 874 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 875 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 876 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 877 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 878 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 879 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 880 | |
| 881 | /* catch up testNb */ |
| 882 | for (testNb=1; testNb < startTest; testNb++) |
| 883 | FUZ_rand(&coreSeed); |
| 884 | |
| 885 | /* test loop */ |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 886 | for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 887 | U32 lseed; |
| 888 | const BYTE* srcBuffer; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 889 | size_t totalTestSize, totalGenSize, cSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 890 | XXH64_state_t xxhState; |
| 891 | U64 crcOrig; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 892 | U32 resetAllowed = 1; |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 893 | size_t maxTestSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 894 | |
| 895 | /* init */ |
Yann Collet | 4c0b44f | 2016-11-01 11:13:22 -0700 | [diff] [blame] | 896 | if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } |
| 897 | else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 898 | FUZ_rand(&coreSeed); |
Yann Collet | 33fce03 | 2017-01-16 19:46:22 -0800 | [diff] [blame] | 899 | lseed = coreSeed ^ prime32; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 900 | |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 901 | /* states full reset (deliberately not synchronized) */ |
| 902 | /* some issues can only happen when reusing states */ |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 903 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { |
| 904 | ZSTD_freeCStream(zc); |
| 905 | zc = ZSTD_createCStream(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 906 | CHECK(zc==NULL, "ZSTD_createCStream : allocation error"); |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 907 | resetAllowed=0; |
| 908 | } |
| 909 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { |
| 910 | ZSTD_freeDStream(zd); |
| 911 | zd = ZSTD_createDStream(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 912 | CHECK(zd==NULL, "ZSTD_createDStream : allocation error"); |
| 913 | 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] | 914 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 915 | |
| 916 | /* srcBuffer selection [0-4] */ |
| 917 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 918 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 919 | else { |
| 920 | buffNb >>= 3; |
| 921 | if (buffNb & 7) { |
| 922 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 923 | buffNb = tnb[buffNb >> 3]; |
| 924 | } else { |
| 925 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 926 | buffNb = tnb[buffNb >> 3]; |
| 927 | } } |
| 928 | srcBuffer = cNoiseBuffer[buffNb]; |
| 929 | } |
| 930 | |
| 931 | /* compression init */ |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 932 | if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */ |
| 933 | && oldTestLog /* at least one test happened */ && resetAllowed) { |
| 934 | maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2); |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 935 | maxTestSize = MIN(maxTestSize, srcBufferSize-16); |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 936 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 937 | CHECK_Z( ZSTD_resetCStream(zc, pledgedSrcSize) ); |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 938 | } |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 939 | } else { |
| 940 | U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 941 | U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 942 | U32 const cLevelCandidate = ( FUZ_rand(&lseed) % |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 943 | (ZSTD_maxCLevel() - |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 944 | (MAX(testLog, dictLog) / 3))) |
Yann Collet | ce80098 | 2017-04-05 16:34:09 -0700 | [diff] [blame] | 945 | + 1; |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 946 | U32 const cLevel = MIN(cLevelCandidate, cLevelMax); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 947 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 948 | oldTestLog = testLog; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 949 | /* random dictionary selection */ |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 950 | dictSize = ((FUZ_rand(&lseed)&7)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 951 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 952 | dict = srcBuffer + dictStart; |
| 953 | } |
Yann Collet | 213ef3b | 2017-10-13 19:01:58 -0700 | [diff] [blame] | 954 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize; |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 955 | ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 956 | params.fParams.checksumFlag = FUZ_rand(&lseed) & 1; |
| 957 | params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1; |
Yann Collet | 213ef3b | 2017-10-13 19:01:58 -0700 | [diff] [blame] | 958 | params.fParams.contentSizeFlag = FUZ_rand(&lseed) & 1; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 959 | CHECK_Z ( ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) ); |
| 960 | } } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 961 | |
| 962 | /* multi-segments compression test */ |
| 963 | XXH64_reset(&xxhState, 0); |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 964 | { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 965 | U32 n; |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 966 | for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) { |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 967 | /* compress random chunks into randomly sized dst buffers */ |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 968 | { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 150354c | 2017-11-01 16:57:48 -0700 | [diff] [blame] | 969 | size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize); |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 970 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 971 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 972 | size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 2f26394 | 2016-09-26 14:06:08 +0200 | [diff] [blame] | 973 | ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 }; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 974 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 975 | |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 976 | CHECK_Z( ZSTD_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 977 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 978 | XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); |
| 979 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); |
| 980 | totalTestSize += inBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /* random flush operation, to mess around */ |
| 984 | if ((FUZ_rand(&lseed) & 15) == 0) { |
| 985 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 986 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 987 | outBuff.size = outBuff.pos + adjustedDstSize; |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 988 | CHECK_Z( ZSTD_flushStream(zc, &outBuff) ); |
| 989 | } } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 990 | |
| 991 | /* final frame epilogue */ |
| 992 | { size_t remainingToFlush = (size_t)(-1); |
| 993 | while (remainingToFlush) { |
| 994 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 995 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 996 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 997 | remainingToFlush = ZSTD_endStream(zc, &outBuff); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 998 | CHECK (ZSTD_isError(remainingToFlush), "end error : %s", ZSTD_getErrorName(remainingToFlush)); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 999 | } } |
| 1000 | crcOrig = XXH64_digest(&xxhState); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1001 | cSize = outBuff.pos; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* multi - fragments decompression test */ |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1005 | 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] | 1006 | CHECK_Z ( ZSTD_resetDStream(zd) ); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 1007 | } else { |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1008 | CHECK_Z ( ZSTD_initDStream_usingDict(zd, dict, dictSize) ); |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 1009 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1010 | { size_t decompressionResult = 1; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1011 | ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1012 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1013 | for (totalGenSize = 0 ; decompressionResult ; ) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1014 | size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1015 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1016 | size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1017 | inBuff.size = inBuff.pos + readCSrcSize; |
Yann Collet | ca306c1 | 2017-09-27 00:39:41 -0700 | [diff] [blame] | 1018 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1019 | decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | b3f33cc | 2017-09-09 14:37:28 -0700 | [diff] [blame] | 1020 | if (ZSTD_getErrorCode(decompressionResult) == ZSTD_error_checksum_wrong) { |
| 1021 | DISPLAY("checksum error : \n"); |
| 1022 | findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 1023 | } |
| 1024 | CHECK( ZSTD_isError(decompressionResult), "decompression error : %s", |
| 1025 | ZSTD_getErrorName(decompressionResult) ); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1026 | } |
| 1027 | CHECK (decompressionResult != 0, "frame not fully decoded"); |
Yann Collet | b3f33cc | 2017-09-09 14:37:28 -0700 | [diff] [blame] | 1028 | CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", |
| 1029 | (U32)outBuff.pos, (U32)totalTestSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1030 | CHECK (inBuff.pos != cSize, "compressed data should be fully read") |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1031 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 1032 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 1033 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); |
| 1034 | } } |
| 1035 | |
| 1036 | /*===== noisy/erroneous src decompression test =====*/ |
| 1037 | |
| 1038 | /* add some noise */ |
| 1039 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 1040 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
| 1041 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1042 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
| 1043 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 1044 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 1045 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 1046 | } } |
| 1047 | |
| 1048 | /* try decompression on noisy data */ |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1049 | CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1050 | { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1051 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1052 | while (outBuff.pos < dstBufferSize) { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1053 | size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1054 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1055 | size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize); |
Yann Collet | 64bf8ff | 2017-01-27 17:25:07 -0800 | [diff] [blame] | 1056 | size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1057 | outBuff.size = outBuff.pos + adjustedDstSize; |
Yann Collet | 64bf8ff | 2017-01-27 17:25:07 -0800 | [diff] [blame] | 1058 | inBuff.size = inBuff.pos + adjustedCSrcSize; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 1059 | { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1060 | if (ZSTD_isError(decompressError)) break; /* error correctly detected */ |
Yann Collet | 64bf8ff | 2017-01-27 17:25:07 -0800 | [diff] [blame] | 1061 | /* No forward progress possible */ |
| 1062 | if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1063 | } } } } |
| 1064 | DISPLAY("\r%u fuzzer tests completed \n", testNb); |
| 1065 | |
| 1066 | _cleanup: |
| 1067 | ZSTD_freeCStream(zc); |
| 1068 | ZSTD_freeDStream(zd); |
Yann Collet | 58d5dfe | 2016-09-25 01:34:03 +0200 | [diff] [blame] | 1069 | ZSTD_freeDStream(zd_noise); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1070 | free(cNoiseBuffer[0]); |
| 1071 | free(cNoiseBuffer[1]); |
| 1072 | free(cNoiseBuffer[2]); |
| 1073 | free(cNoiseBuffer[3]); |
| 1074 | free(cNoiseBuffer[4]); |
| 1075 | free(copyBuffer); |
| 1076 | free(cBuffer); |
| 1077 | free(dstBuffer); |
| 1078 | return result; |
| 1079 | |
| 1080 | _output_error: |
| 1081 | result = 1; |
| 1082 | goto _cleanup; |
| 1083 | } |
| 1084 | |
| 1085 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1086 | /* Multi-threading version of fuzzer Tests */ |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 1087 | static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests) |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1088 | { |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 1089 | const U32 maxSrcLog = bigTests ? 24 : 22; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1090 | static const U32 maxSampleLog = 19; |
| 1091 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
| 1092 | BYTE* cNoiseBuffer[5]; |
| 1093 | size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog); |
| 1094 | BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize); |
| 1095 | size_t const cBufferSize = ZSTD_compressBound(srcBufferSize); |
| 1096 | BYTE* const cBuffer = (BYTE*)malloc (cBufferSize); |
| 1097 | size_t const dstBufferSize = srcBufferSize; |
| 1098 | BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize); |
| 1099 | U32 result = 0; |
| 1100 | U32 testNb = 0; |
| 1101 | U32 coreSeed = seed; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1102 | U32 nbThreads = 2; |
| 1103 | ZSTDMT_CCtx* zc = ZSTDMT_createCCtx(nbThreads); /* will be reset sometimes */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1104 | ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */ |
| 1105 | ZSTD_DStream* const zd_noise = ZSTD_createDStream(); |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 1106 | UTIL_time_t const startClock = UTIL_getTime(); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1107 | const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */ |
| 1108 | size_t dictSize = 0; |
| 1109 | U32 oldTestLog = 0; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1110 | int const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests; |
| 1111 | U32 const nbThreadsMax = bigTests ? 4 : 2; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1112 | |
| 1113 | /* allocations */ |
| 1114 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 1115 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 1116 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 1117 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 1118 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
| 1119 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
| 1120 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise , |
| 1121 | "Not enough memory, fuzzer tests cancelled"); |
| 1122 | |
| 1123 | /* Create initial samples */ |
| 1124 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 1125 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 1126 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 1127 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 1128 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 1129 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
| 1130 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
| 1131 | |
| 1132 | /* catch up testNb */ |
| 1133 | for (testNb=1; testNb < startTest; testNb++) |
| 1134 | FUZ_rand(&coreSeed); |
| 1135 | |
| 1136 | /* test loop */ |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 1137 | for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) { |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1138 | U32 lseed; |
| 1139 | const BYTE* srcBuffer; |
| 1140 | size_t totalTestSize, totalGenSize, cSize; |
| 1141 | XXH64_state_t xxhState; |
| 1142 | U64 crcOrig; |
| 1143 | U32 resetAllowed = 1; |
| 1144 | size_t maxTestSize; |
| 1145 | |
| 1146 | /* init */ |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1147 | if (testNb < nbTests) { |
| 1148 | DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); |
| 1149 | } else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1150 | FUZ_rand(&coreSeed); |
Yann Collet | d7e3cb5 | 2017-01-20 16:44:50 -0800 | [diff] [blame] | 1151 | lseed = coreSeed ^ prime32; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1152 | |
| 1153 | /* states full reset (deliberately not synchronized) */ |
| 1154 | /* some issues can only happen when reusing states */ |
| 1155 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { |
Yann Collet | 9fe50ed | 2017-09-28 01:42:06 -0700 | [diff] [blame] | 1156 | nbThreads = (FUZ_rand(&lseed) % nbThreadsMax) + 1; |
Yann Collet | 30c7698 | 2017-03-31 18:27:03 -0700 | [diff] [blame] | 1157 | DISPLAYLEVEL(5, "Creating new context with %u threads \n", nbThreads); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1158 | ZSTDMT_freeCCtx(zc); |
| 1159 | zc = ZSTDMT_createCCtx(nbThreads); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1160 | CHECK(zc==NULL, "ZSTDMT_createCCtx allocation error") |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1161 | resetAllowed=0; |
| 1162 | } |
| 1163 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { |
| 1164 | ZSTD_freeDStream(zd); |
| 1165 | zd = ZSTD_createDStream(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1166 | CHECK(zd==NULL, "ZSTDMT_createCCtx allocation error") |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1167 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
| 1168 | } |
| 1169 | |
| 1170 | /* srcBuffer selection [0-4] */ |
| 1171 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 1172 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 1173 | else { |
| 1174 | buffNb >>= 3; |
| 1175 | if (buffNb & 7) { |
| 1176 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 1177 | buffNb = tnb[buffNb >> 3]; |
| 1178 | } else { |
| 1179 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 1180 | buffNb = tnb[buffNb >> 3]; |
| 1181 | } } |
| 1182 | srcBuffer = cNoiseBuffer[buffNb]; |
| 1183 | } |
| 1184 | |
| 1185 | /* compression init */ |
| 1186 | if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */ |
| 1187 | && oldTestLog /* at least one test happened */ && resetAllowed) { |
| 1188 | maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2); |
| 1189 | if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1; |
| 1190 | { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1; |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1191 | CHECK_Z( ZSTDMT_initCStream(zc, compressionLevel) ); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1192 | } |
| 1193 | } else { |
| 1194 | U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 1195 | U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; |
Yann Collet | bc32b40 | 2017-09-27 17:27:38 -0700 | [diff] [blame] | 1196 | int const cLevelCandidate = ( FUZ_rand(&lseed) |
| 1197 | % (ZSTD_maxCLevel() - (MAX(testLog, dictLog) / 2)) ) |
| 1198 | + 1; |
| 1199 | int const cLevelThreadAdjusted = cLevelCandidate - (nbThreads * 2) + 2; /* reduce cLevel when multiple threads to reduce memory consumption */ |
| 1200 | int const cLevelMin = MAX(cLevelThreadAdjusted, 1); /* no negative cLevel yet */ |
| 1201 | int const cLevel = MIN(cLevelMin, cLevelMax); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1202 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
| 1203 | oldTestLog = testLog; |
| 1204 | /* random dictionary selection */ |
Sean Purcell | f5e5051 | 2017-03-15 15:04:54 -0700 | [diff] [blame] | 1205 | dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0; |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1206 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 1207 | dict = srcBuffer + dictStart; |
| 1208 | } |
Yann Collet | beb9b4b | 2017-10-13 19:09:30 -0700 | [diff] [blame] | 1209 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize; |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1210 | ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize); |
Yann Collet | ce80098 | 2017-04-05 16:34:09 -0700 | [diff] [blame] | 1211 | DISPLAYLEVEL(5, "Init with windowLog = %u and pledgedSrcSize = %u \n", |
| 1212 | params.cParams.windowLog, (U32)pledgedSrcSize); |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1213 | params.fParams.checksumFlag = FUZ_rand(&lseed) & 1; |
| 1214 | params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1; |
Yann Collet | beb9b4b | 2017-10-13 19:09:30 -0700 | [diff] [blame] | 1215 | params.fParams.contentSizeFlag = FUZ_rand(&lseed) & 1; |
Yann Collet | 2c5514c | 2017-04-18 22:52:41 -0700 | [diff] [blame] | 1216 | DISPLAYLEVEL(5, "checksumFlag : %u \n", params.fParams.checksumFlag); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1217 | CHECK_Z( ZSTDMT_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize) ); |
| 1218 | CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_overlapSectionLog, FUZ_rand(&lseed) % 12) ); |
| 1219 | CHECK_Z( ZSTDMT_setMTCtxParameter(zc, ZSTDMT_p_sectionSize, FUZ_rand(&lseed) % (2*maxTestSize+1)) ); |
Yann Collet | cd23dd2 | 2017-01-30 12:46:35 -0800 | [diff] [blame] | 1220 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1221 | |
| 1222 | /* multi-segments compression test */ |
| 1223 | XXH64_reset(&xxhState, 0); |
| 1224 | { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; |
| 1225 | U32 n; |
| 1226 | for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) { |
| 1227 | /* compress random chunks into randomly sized dst buffers */ |
| 1228 | { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1229 | size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize); |
| 1230 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize); |
| 1231 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1232 | size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
| 1233 | ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 }; |
| 1234 | outBuff.size = outBuff.pos + dstBuffSize; |
| 1235 | |
| 1236 | DISPLAYLEVEL(5, "Sending %u bytes to compress \n", (U32)srcSize); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1237 | CHECK_Z( ZSTDMT_compressStream(zc, &outBuff, &inBuff) ); |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1238 | DISPLAYLEVEL(5, "%u bytes read by ZSTDMT_compressStream \n", (U32)inBuff.pos); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1239 | |
| 1240 | XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); |
| 1241 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); |
| 1242 | totalTestSize += inBuff.pos; |
| 1243 | } |
| 1244 | |
| 1245 | /* random flush operation, to mess around */ |
| 1246 | if ((FUZ_rand(&lseed) & 15) == 0) { |
| 1247 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1248 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 1249 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 1250 | DISPLAYLEVEL(5, "Flushing into dst buffer of size %u \n", (U32)adjustedDstSize); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1251 | CHECK_Z( ZSTDMT_flushStream(zc, &outBuff) ); |
| 1252 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1253 | |
| 1254 | /* final frame epilogue */ |
| 1255 | { size_t remainingToFlush = (size_t)(-1); |
| 1256 | while (remainingToFlush) { |
| 1257 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1258 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 1259 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 1260 | DISPLAYLEVEL(5, "Ending into dst buffer of size %u \n", (U32)adjustedDstSize); |
| 1261 | remainingToFlush = ZSTDMT_endStream(zc, &outBuff); |
Yann Collet | 30c7698 | 2017-03-31 18:27:03 -0700 | [diff] [blame] | 1262 | CHECK (ZSTD_isError(remainingToFlush), "ZSTDMT_endStream error : %s", ZSTD_getErrorName(remainingToFlush)); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1263 | DISPLAYLEVEL(5, "endStream : remainingToFlush : %u \n", (U32)remainingToFlush); |
| 1264 | } } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1265 | crcOrig = XXH64_digest(&xxhState); |
| 1266 | cSize = outBuff.pos; |
Yann Collet | 2c5514c | 2017-04-18 22:52:41 -0700 | [diff] [blame] | 1267 | DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | /* multi - fragments decompression test */ |
| 1271 | 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] | 1272 | CHECK_Z( ZSTD_resetDStream(zd) ); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1273 | } else { |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1274 | CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) ); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1275 | } |
| 1276 | { size_t decompressionResult = 1; |
| 1277 | ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1278 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1279 | for (totalGenSize = 0 ; decompressionResult ; ) { |
| 1280 | size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1281 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1282 | size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
| 1283 | inBuff.size = inBuff.pos + readCSrcSize; |
Yann Collet | bfabd1d | 2017-09-27 01:01:11 -0700 | [diff] [blame] | 1284 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | ce80098 | 2017-04-05 16:34:09 -0700 | [diff] [blame] | 1285 | DISPLAYLEVEL(5, "ZSTD_decompressStream input %u bytes \n", (U32)readCSrcSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1286 | decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 1287 | CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult)); |
Yann Collet | 2c5514c | 2017-04-18 22:52:41 -0700 | [diff] [blame] | 1288 | DISPLAYLEVEL(5, "inBuff.pos = %u \n", (U32)readCSrcSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1289 | } |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 1290 | CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize); |
| 1291 | CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1292 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 1293 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 1294 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); |
| 1295 | } } |
| 1296 | |
| 1297 | /*===== noisy/erroneous src decompression test =====*/ |
| 1298 | |
| 1299 | /* add some noise */ |
| 1300 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 1301 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
| 1302 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1303 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
| 1304 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 1305 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 1306 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 1307 | } } |
| 1308 | |
| 1309 | /* try decompression on noisy data */ |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1310 | CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */ |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1311 | { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1312 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1313 | while (outBuff.pos < dstBufferSize) { |
| 1314 | size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1315 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1316 | size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize); |
Nick Terrell | d98bf49 | 2017-01-27 15:42:36 -0800 | [diff] [blame] | 1317 | size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1318 | outBuff.size = outBuff.pos + adjustedDstSize; |
Nick Terrell | d98bf49 | 2017-01-27 15:42:36 -0800 | [diff] [blame] | 1319 | inBuff.size = inBuff.pos + adjustedCSrcSize; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1320 | { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 1321 | if (ZSTD_isError(decompressError)) break; /* error correctly detected */ |
Nick Terrell | d98bf49 | 2017-01-27 15:42:36 -0800 | [diff] [blame] | 1322 | /* No forward progress possible */ |
| 1323 | if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1324 | } } } } |
| 1325 | DISPLAY("\r%u fuzzer tests completed \n", testNb); |
| 1326 | |
| 1327 | _cleanup: |
| 1328 | ZSTDMT_freeCCtx(zc); |
| 1329 | ZSTD_freeDStream(zd); |
| 1330 | ZSTD_freeDStream(zd_noise); |
| 1331 | free(cNoiseBuffer[0]); |
| 1332 | free(cNoiseBuffer[1]); |
| 1333 | free(cNoiseBuffer[2]); |
| 1334 | free(cNoiseBuffer[3]); |
| 1335 | free(cNoiseBuffer[4]); |
| 1336 | free(copyBuffer); |
| 1337 | free(cBuffer); |
| 1338 | free(dstBuffer); |
| 1339 | return result; |
| 1340 | |
| 1341 | _output_error: |
| 1342 | result = 1; |
| 1343 | goto _cleanup; |
| 1344 | } |
| 1345 | |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1346 | /** If useOpaqueAPI, sets param in cctxParams. |
| 1347 | * Otherwise, sets the param in zc. */ |
| 1348 | static size_t setCCtxParameter(ZSTD_CCtx* zc, ZSTD_CCtx_params* cctxParams, |
| 1349 | ZSTD_cParameter param, unsigned value, |
| 1350 | U32 useOpaqueAPI) |
| 1351 | { |
| 1352 | if (useOpaqueAPI) { |
| 1353 | return ZSTD_CCtxParam_setParameter(cctxParams, param, value); |
| 1354 | } else { |
| 1355 | return ZSTD_CCtx_setParameter(zc, param, value); |
| 1356 | } |
| 1357 | } |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1358 | |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 1359 | /* Tests for ZSTD_compress_generic() API */ |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1360 | static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests, U32 const useOpaqueAPI) |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1361 | { |
Yann Collet | f99c2c1 | 2017-06-21 23:35:58 -0700 | [diff] [blame] | 1362 | U32 const maxSrcLog = bigTests ? 24 : 22; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1363 | static const U32 maxSampleLog = 19; |
| 1364 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
| 1365 | BYTE* cNoiseBuffer[5]; |
| 1366 | size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog); |
| 1367 | BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize); |
| 1368 | size_t const cBufferSize = ZSTD_compressBound(srcBufferSize); |
| 1369 | BYTE* const cBuffer = (BYTE*)malloc (cBufferSize); |
| 1370 | size_t const dstBufferSize = srcBufferSize; |
| 1371 | BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize); |
| 1372 | U32 result = 0; |
| 1373 | U32 testNb = 0; |
| 1374 | U32 coreSeed = seed; |
| 1375 | ZSTD_CCtx* zc = ZSTD_createCCtx(); /* will be reset sometimes */ |
| 1376 | ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */ |
| 1377 | ZSTD_DStream* const zd_noise = ZSTD_createDStream(); |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 1378 | UTIL_time_t const startClock = UTIL_getTime(); |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 1379 | const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1380 | size_t dictSize = 0; |
| 1381 | U32 oldTestLog = 0; |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 1382 | U32 windowLogMalus = 0; /* can survive between 2 loops */ |
| 1383 | U32 const cLevelMax = bigTests ? (U32)ZSTD_maxCLevel()-1 : g_cLevelMax_smallTests; |
| 1384 | U32 const nbThreadsMax = bigTests ? 4 : 2; |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1385 | ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams(); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1386 | |
| 1387 | /* allocations */ |
| 1388 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 1389 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 1390 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 1391 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 1392 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
| 1393 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] || |
| 1394 | !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise , |
| 1395 | "Not enough memory, fuzzer tests cancelled"); |
| 1396 | |
| 1397 | /* Create initial samples */ |
| 1398 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 1399 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 1400 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 1401 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 1402 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 1403 | memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */ |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1404 | 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] | 1405 | |
| 1406 | /* catch up testNb */ |
| 1407 | for (testNb=1; testNb < startTest; testNb++) |
| 1408 | FUZ_rand(&coreSeed); |
| 1409 | |
| 1410 | /* test loop */ |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 1411 | for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) { |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1412 | U32 lseed; |
| 1413 | const BYTE* srcBuffer; |
| 1414 | size_t totalTestSize, totalGenSize, cSize; |
| 1415 | XXH64_state_t xxhState; |
| 1416 | U64 crcOrig; |
| 1417 | U32 resetAllowed = 1; |
| 1418 | size_t maxTestSize; |
| 1419 | |
| 1420 | /* init */ |
| 1421 | if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } |
| 1422 | else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
| 1423 | FUZ_rand(&coreSeed); |
| 1424 | lseed = coreSeed ^ prime32; |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 1425 | DISPLAYLEVEL(5, " *** Test %u *** \n", testNb); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1426 | |
| 1427 | /* states full reset (deliberately not synchronized) */ |
| 1428 | /* some issues can only happen when reusing states */ |
| 1429 | if ((FUZ_rand(&lseed) & 0xFF) == 131) { |
| 1430 | DISPLAYLEVEL(5, "Creating new context \n"); |
| 1431 | ZSTD_freeCCtx(zc); |
| 1432 | zc = ZSTD_createCCtx(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1433 | CHECK(zc==NULL, "ZSTD_createCCtx allocation error"); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1434 | resetAllowed=0; |
| 1435 | } |
| 1436 | if ((FUZ_rand(&lseed) & 0xFF) == 132) { |
| 1437 | ZSTD_freeDStream(zd); |
| 1438 | zd = ZSTD_createDStream(); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1439 | CHECK(zd==NULL, "ZSTD_createDStream allocation error"); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1440 | ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ |
| 1441 | } |
| 1442 | |
| 1443 | /* srcBuffer selection [0-4] */ |
| 1444 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 1445 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 1446 | else { |
| 1447 | buffNb >>= 3; |
| 1448 | if (buffNb & 7) { |
| 1449 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 1450 | buffNb = tnb[buffNb >> 3]; |
| 1451 | } else { |
| 1452 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 1453 | buffNb = tnb[buffNb >> 3]; |
| 1454 | } } |
| 1455 | srcBuffer = cNoiseBuffer[buffNb]; |
| 1456 | } |
| 1457 | |
| 1458 | /* compression init */ |
Yann Collet | b737293 | 2017-06-27 15:49:12 -0700 | [diff] [blame] | 1459 | CHECK_Z( ZSTD_CCtx_loadDictionary(zc, NULL, 0) ); /* cancel previous dict /*/ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1460 | if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */ |
| 1461 | && oldTestLog /* at least one test happened */ && resetAllowed) { |
| 1462 | maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2); |
| 1463 | if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1464 | { int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1; |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1465 | CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_compressionLevel, compressionLevel, useOpaqueAPI) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1466 | } |
| 1467 | } else { |
| 1468 | U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
| 1469 | U32 const dictLog = FUZ_rand(&lseed) % maxSrcLog; |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 1470 | U32 const cLevelCandidate = (FUZ_rand(&lseed) % |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1471 | (ZSTD_maxCLevel() - |
Yann Collet | 9fe50ed | 2017-09-28 01:42:06 -0700 | [diff] [blame] | 1472 | (MAX(testLog, dictLog) / 2))) + |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1473 | 1; |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 1474 | U32 const cLevel = MIN(cLevelCandidate, cLevelMax); |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 1475 | DISPLAYLEVEL(5, "t%u: cLevel : %u \n", testNb, cLevel); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1476 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 1477 | DISPLAYLEVEL(5, "t%u: maxTestSize : %u \n", testNb, (U32)maxTestSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1478 | oldTestLog = testLog; |
| 1479 | /* random dictionary selection */ |
| 1480 | dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_rLogLength(&lseed, dictLog) : 0; |
| 1481 | { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize); |
| 1482 | dict = srcBuffer + dictStart; |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 1483 | if (!dictSize) dict=NULL; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1484 | } |
| 1485 | { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize; |
| 1486 | ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, pledgedSrcSize, dictSize); |
Yann Collet | bbef058 | 2017-09-28 11:48:45 -0700 | [diff] [blame] | 1487 | static const U32 windowLogMax = 24; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1488 | |
| 1489 | /* mess with compression parameters */ |
| 1490 | cParams.windowLog += (FUZ_rand(&lseed) & 3) - 1; |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 1491 | cParams.windowLog = MIN(windowLogMax, cParams.windowLog); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1492 | cParams.hashLog += (FUZ_rand(&lseed) & 3) - 1; |
| 1493 | cParams.chainLog += (FUZ_rand(&lseed) & 3) - 1; |
| 1494 | cParams.searchLog += (FUZ_rand(&lseed) & 3) - 1; |
| 1495 | cParams.searchLength += (FUZ_rand(&lseed) & 3) - 1; |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 1496 | cParams.targetLength = (U32)((cParams.targetLength + 1 ) * (0.5 + ((double)(FUZ_rand(&lseed) & 127) / 128))); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1497 | cParams = ZSTD_adjustCParams(cParams, 0, 0); |
| 1498 | |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 1499 | if (FUZ_rand(&lseed) & 1) { |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 1500 | DISPLAYLEVEL(5, "t%u: windowLog : %u \n", testNb, cParams.windowLog); |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 1501 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_windowLog, cParams.windowLog, useOpaqueAPI) ); |
| 1502 | assert(cParams.windowLog >= ZSTD_WINDOWLOG_MIN); /* guaranteed by ZSTD_adjustCParams() */ |
| 1503 | windowLogMalus = (cParams.windowLog - ZSTD_WINDOWLOG_MIN) / 5; |
| 1504 | } |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 1505 | if (FUZ_rand(&lseed) & 1) { |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 1506 | DISPLAYLEVEL(5, "t%u: hashLog : %u \n", testNb, cParams.hashLog); |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 1507 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_hashLog, cParams.hashLog, useOpaqueAPI) ); |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 1508 | } |
| 1509 | if (FUZ_rand(&lseed) & 1) { |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 1510 | DISPLAYLEVEL(5, "t%u: chainLog : %u \n", testNb, cParams.chainLog); |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 1511 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_chainLog, cParams.chainLog, useOpaqueAPI) ); |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 1512 | } |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1513 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_searchLog, cParams.searchLog, useOpaqueAPI) ); |
| 1514 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_minMatch, cParams.searchLength, useOpaqueAPI) ); |
| 1515 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_targetLength, cParams.targetLength, useOpaqueAPI) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1516 | |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 1517 | /* mess with long distance matching parameters */ |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 1518 | if (bigTests) { |
| 1519 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_enableLongDistanceMatching, FUZ_rand(&lseed) & 63, useOpaqueAPI) ); |
| 1520 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashLog, FUZ_randomClampedLength(&lseed, ZSTD_HASHLOG_MIN, 23), useOpaqueAPI) ); |
| 1521 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmMinMatch, FUZ_randomClampedLength(&lseed, ZSTD_LDM_MINMATCH_MIN, ZSTD_LDM_MINMATCH_MAX), useOpaqueAPI) ); |
| 1522 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmBucketSizeLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_LDM_BUCKETSIZELOG_MAX), useOpaqueAPI) ); |
| 1523 | if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashEveryLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN), useOpaqueAPI) ); |
| 1524 | } |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 1525 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1526 | /* mess with frame parameters */ |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1527 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_checksumFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); |
| 1528 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); |
| 1529 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 1530 | if (FUZ_rand(&lseed) & 1) { |
| 1531 | DISPLAYLEVEL(5, "t%u: pledgedSrcSize : %u \n", testNb, (U32)pledgedSrcSize); |
| 1532 | CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) ); |
| 1533 | } |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1534 | |
| 1535 | /* multi-threading parameters */ |
Yann Collet | bfc2f00 | 2017-06-21 17:57:14 -0700 | [diff] [blame] | 1536 | { U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1; |
Yann Collet | aa800c4 | 2017-09-27 18:00:15 -0700 | [diff] [blame] | 1537 | U32 const nbThreadsAdjusted = (windowLogMalus < nbThreadsCandidate) ? nbThreadsCandidate - windowLogMalus : 1; |
| 1538 | U32 const nbThreads = MIN(nbThreadsAdjusted, nbThreadsMax); |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 1539 | DISPLAYLEVEL(5, "t%u: nbThreads : %u \n", testNb, nbThreads); |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 1540 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_nbThreads, nbThreads, useOpaqueAPI) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1541 | if (nbThreads > 1) { |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1542 | U32 const jobLog = FUZ_rand(&lseed) % (testLog+1); |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1543 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10, useOpaqueAPI) ); |
| 1544 | CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog), useOpaqueAPI) ); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 1548 | if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1, useOpaqueAPI) ); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1549 | |
| 1550 | /* Apply parameters */ |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1551 | if (useOpaqueAPI) { |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 1552 | DISPLAYLEVEL(6," t%u: applying CCtxParams \n", testNb); |
Stella Lau | 82d636b | 2017-08-29 18:03:06 -0700 | [diff] [blame] | 1553 | CHECK_Z (ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams) ); |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1554 | } |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1555 | |
| 1556 | if (FUZ_rand(&lseed) & 1) { |
Stella Lau | eb7bbab | 2017-08-25 10:48:07 -0700 | [diff] [blame] | 1557 | if (FUZ_rand(&lseed) & 1) { |
| 1558 | CHECK_Z( ZSTD_CCtx_loadDictionary(zc, dict, dictSize) ); |
| 1559 | } else { |
| 1560 | CHECK_Z( ZSTD_CCtx_loadDictionary_byReference(zc, dict, dictSize) ); |
| 1561 | } |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1562 | if (dict && dictSize) { |
| 1563 | /* test that compression parameters are rejected (correctly) after loading a non-NULL dictionary */ |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1564 | if (useOpaqueAPI) { |
Stella Lau | 82d636b | 2017-08-29 18:03:06 -0700 | [diff] [blame] | 1565 | size_t const setError = ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams); |
| 1566 | CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParametersUsingCCtxParams should have failed"); |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1567 | } else { |
| 1568 | size_t const setError = ZSTD_CCtx_setParameter(zc, ZSTD_p_windowLog, cParams.windowLog-1); |
| 1569 | CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParameter should have failed"); |
| 1570 | } |
| 1571 | } |
| 1572 | } else { |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1573 | CHECK_Z( ZSTD_CCtx_refPrefix(zc, dict, dictSize) ); |
| 1574 | } |
| 1575 | } } |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1576 | |
| 1577 | /* multi-segments compression test */ |
| 1578 | XXH64_reset(&xxhState, 0); |
| 1579 | { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ; |
Yann Collet | 052a95f | 2017-07-11 17:18:26 -0700 | [diff] [blame] | 1580 | for (cSize=0, totalTestSize=0 ; (totalTestSize < maxTestSize) ; ) { |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1581 | /* compress random chunks into randomly sized dst buffers */ |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1582 | size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1583 | size_t const srcSize = MIN(maxTestSize-totalTestSize, randomSrcSize); |
| 1584 | size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize); |
Yann Collet | 052a95f | 2017-07-11 17:18:26 -0700 | [diff] [blame] | 1585 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1); |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1586 | size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize); |
| 1587 | ZSTD_EndDirective const flush = (FUZ_rand(&lseed) & 15) ? ZSTD_e_continue : ZSTD_e_flush; |
| 1588 | ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 }; |
| 1589 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1590 | |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1591 | CHECK_Z( ZSTD_compress_generic(zc, &outBuff, &inBuff, flush) ); |
Yann Collet | d3c59ed | 2017-11-29 16:42:20 -0800 | [diff] [blame] | 1592 | DISPLAYLEVEL(6, "t%u: compress consumed %u bytes (total : %u) \n", |
| 1593 | testNb, (U32)inBuff.pos, (U32)(totalTestSize + inBuff.pos)); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1594 | |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1595 | XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos); |
| 1596 | memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos); |
| 1597 | totalTestSize += inBuff.pos; |
| 1598 | } |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1599 | |
| 1600 | /* final frame epilogue */ |
| 1601 | { size_t remainingToFlush = (size_t)(-1); |
| 1602 | while (remainingToFlush) { |
| 1603 | ZSTD_inBuffer inBuff = { NULL, 0, 0 }; |
Yann Collet | 052a95f | 2017-07-11 17:18:26 -0700 | [diff] [blame] | 1604 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog+1); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1605 | size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize); |
| 1606 | outBuff.size = outBuff.pos + adjustedDstSize; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 1607 | DISPLAYLEVEL(6, "End-flush into dst buffer of size %u \n", (U32)adjustedDstSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1608 | remainingToFlush = ZSTD_compress_generic(zc, &outBuff, &inBuff, ZSTD_e_end); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 1609 | CHECK( ZSTD_isError(remainingToFlush), |
| 1610 | "ZSTD_compress_generic w/ ZSTD_e_end error : %s", |
| 1611 | ZSTD_getErrorName(remainingToFlush) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1612 | } } |
| 1613 | crcOrig = XXH64_digest(&xxhState); |
| 1614 | cSize = outBuff.pos; |
| 1615 | DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize); |
| 1616 | } |
| 1617 | |
| 1618 | /* multi - fragments decompression test */ |
| 1619 | if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) { |
Yann Collet | 33a6639 | 2017-06-28 11:09:43 -0700 | [diff] [blame] | 1620 | DISPLAYLEVEL(5, "resetting DCtx (dict:%08X) \n", (U32)(size_t)dict); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1621 | CHECK_Z( ZSTD_resetDStream(zd) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1622 | } else { |
Yann Collet | 33a6639 | 2017-06-28 11:09:43 -0700 | [diff] [blame] | 1623 | DISPLAYLEVEL(5, "using dict of size %u \n", (U32)dictSize); |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1624 | CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) ); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1625 | } |
| 1626 | { size_t decompressionResult = 1; |
| 1627 | ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1628 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1629 | for (totalGenSize = 0 ; decompressionResult ; ) { |
| 1630 | size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1631 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1632 | size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize); |
| 1633 | inBuff.size = inBuff.pos + readCSrcSize; |
Yann Collet | bfabd1d | 2017-09-27 01:01:11 -0700 | [diff] [blame] | 1634 | outBuff.size = outBuff.pos + dstBuffSize; |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 1635 | DISPLAYLEVEL(6, "ZSTD_decompressStream input %u bytes (pos:%u/%u)\n", |
Yann Collet | 33a6639 | 2017-06-28 11:09:43 -0700 | [diff] [blame] | 1636 | (U32)readCSrcSize, (U32)inBuff.pos, (U32)cSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1637 | decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 1638 | CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult)); |
Yann Collet | d6770f8 | 2017-09-28 02:14:48 -0700 | [diff] [blame] | 1639 | DISPLAYLEVEL(6, "inBuff.pos = %u \n", (U32)readCSrcSize); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1640 | } |
| 1641 | CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size (%u != %u)", (U32)outBuff.pos, (U32)totalTestSize); |
| 1642 | CHECK (inBuff.pos != cSize, "compressed data should be fully read (%u != %u)", (U32)inBuff.pos, (U32)cSize); |
| 1643 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 1644 | if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); |
| 1645 | CHECK (crcDest!=crcOrig, "decompressed data corrupted"); |
| 1646 | } } |
| 1647 | |
| 1648 | /*===== noisy/erroneous src decompression test =====*/ |
| 1649 | |
| 1650 | /* add some noise */ |
| 1651 | { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2; |
| 1652 | U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) { |
| 1653 | size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1654 | size_t const noiseSize = MIN((cSize/3) , randomNoiseSize); |
| 1655 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize); |
| 1656 | size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize); |
| 1657 | memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize); |
| 1658 | } } |
| 1659 | |
| 1660 | /* try decompression on noisy data */ |
Yann Collet | dce7892 | 2017-06-21 15:53:42 -0700 | [diff] [blame] | 1661 | CHECK_Z( ZSTD_initDStream(zd_noise) ); /* note : no dictionary */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1662 | { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 }; |
| 1663 | ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 }; |
| 1664 | while (outBuff.pos < dstBufferSize) { |
| 1665 | size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1666 | size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 1667 | size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize); |
| 1668 | size_t const adjustedCSrcSize = MIN(cSize - inBuff.pos, randomCSrcSize); |
| 1669 | outBuff.size = outBuff.pos + adjustedDstSize; |
| 1670 | inBuff.size = inBuff.pos + adjustedCSrcSize; |
| 1671 | { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff); |
| 1672 | if (ZSTD_isError(decompressError)) break; /* error correctly detected */ |
Yann Collet | ed1d039 | 2017-06-19 11:07:33 -0700 | [diff] [blame] | 1673 | /* Good so far, but no more progress possible */ |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1674 | if (outBuff.pos < outBuff.size && inBuff.pos == cSize) break; |
| 1675 | } } } } |
Yann Collet | 8dee0ec | 2017-06-18 23:25:15 -0700 | [diff] [blame] | 1676 | DISPLAY("\r%u fuzzer tests completed \n", testNb-1); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1677 | |
| 1678 | _cleanup: |
| 1679 | ZSTD_freeCCtx(zc); |
| 1680 | ZSTD_freeDStream(zd); |
| 1681 | ZSTD_freeDStream(zd_noise); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 1682 | ZSTD_freeCCtxParams(cctxParams); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1683 | free(cNoiseBuffer[0]); |
| 1684 | free(cNoiseBuffer[1]); |
| 1685 | free(cNoiseBuffer[2]); |
| 1686 | free(cNoiseBuffer[3]); |
| 1687 | free(cNoiseBuffer[4]); |
| 1688 | free(copyBuffer); |
| 1689 | free(cBuffer); |
| 1690 | free(dstBuffer); |
| 1691 | return result; |
| 1692 | |
| 1693 | _output_error: |
| 1694 | result = 1; |
| 1695 | goto _cleanup; |
| 1696 | } |
| 1697 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1698 | /*-******************************************************* |
| 1699 | * Command line |
| 1700 | *********************************************************/ |
| 1701 | int FUZ_usage(const char* programName) |
| 1702 | { |
| 1703 | DISPLAY( "Usage :\n"); |
| 1704 | DISPLAY( " %s [args]\n", programName); |
| 1705 | DISPLAY( "\n"); |
| 1706 | DISPLAY( "Arguments :\n"); |
| 1707 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 1708 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 1709 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
| 1710 | DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); |
| 1711 | DISPLAY( " -v : verbose\n"); |
| 1712 | DISPLAY( " -p : pause at the end\n"); |
| 1713 | DISPLAY( " -h : display help and exit\n"); |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1717 | typedef enum { simple_api, mt_api, advanced_api } e_api; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1718 | |
| 1719 | int main(int argc, const char** argv) |
| 1720 | { |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 1721 | U32 seed = 0; |
| 1722 | int seedset = 0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1723 | int nbTests = nbTestsDefault; |
| 1724 | int testNb = 0; |
| 1725 | int proba = FUZ_COMPRESSIBILITY_DEFAULT; |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 1726 | int result = 0; |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1727 | int mainPause = 0; |
Yann Collet | afb0aca | 2017-06-29 18:19:09 -0700 | [diff] [blame] | 1728 | int bigTests = (sizeof(size_t) == 8); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1729 | e_api selected_api = simple_api; |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1730 | const char* const programName = argv[0]; |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1731 | U32 useOpaqueAPI = 0; |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 1732 | int argNb; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1733 | |
| 1734 | /* Check command line */ |
| 1735 | for(argNb=1; argNb<argc; argNb++) { |
| 1736 | const char* argument = argv[argNb]; |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 1737 | assert(argument != NULL); |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1738 | |
| 1739 | /* Parsing commands. Aggregated commands are allowed */ |
| 1740 | if (argument[0]=='-') { |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1741 | |
Yann Collet | 47c6a95 | 2017-09-28 18:27:22 -0700 | [diff] [blame] | 1742 | if (!strcmp(argument, "--mt")) { selected_api=mt_api; testNb += !testNb; continue; } |
| 1743 | if (!strcmp(argument, "--newapi")) { selected_api=advanced_api; testNb += !testNb; continue; } |
| 1744 | if (!strcmp(argument, "--opaqueapi")) { selected_api=advanced_api; testNb += !testNb; useOpaqueAPI = 1; continue; } |
Sean Purcell | 7ebf2de | 2017-03-20 11:25:00 -0700 | [diff] [blame] | 1745 | if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; } |
Yann Collet | 19d670b | 2017-01-19 15:32:07 -0800 | [diff] [blame] | 1746 | |
| 1747 | argument++; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1748 | while (*argument!=0) { |
| 1749 | switch(*argument) |
| 1750 | { |
| 1751 | case 'h': |
| 1752 | return FUZ_usage(programName); |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1753 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1754 | case 'v': |
| 1755 | argument++; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1756 | g_displayLevel++; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1757 | break; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1758 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1759 | case 'q': |
| 1760 | argument++; |
| 1761 | g_displayLevel--; |
| 1762 | break; |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1763 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1764 | case 'p': /* pause at the end */ |
| 1765 | argument++; |
| 1766 | mainPause = 1; |
| 1767 | break; |
| 1768 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1769 | case 'i': /* limit tests by nb of iterations (default) */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1770 | argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 1771 | nbTests=0; g_clockTime=0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1772 | while ((*argument>='0') && (*argument<='9')) { |
| 1773 | nbTests *= 10; |
| 1774 | nbTests += *argument - '0'; |
| 1775 | argument++; |
| 1776 | } |
| 1777 | break; |
| 1778 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1779 | case 'T': /* limit tests by time */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1780 | argument++; |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 1781 | nbTests=0; g_clockTime=0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1782 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 1783 | g_clockTime *= 10; |
| 1784 | g_clockTime += *argument - '0'; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1785 | argument++; |
| 1786 | } |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 1787 | if (*argument=='m') { /* -T1m == -T60 */ |
| 1788 | g_clockTime *=60, argument++; |
| 1789 | if (*argument=='n') argument++; /* -T1mn == -T60 */ |
| 1790 | } else if (*argument=='s') argument++; /* -T10s == -T10 */ |
Nick Terrell | 9a2f6f4 | 2017-11-29 19:11:12 -0800 | [diff] [blame] | 1791 | g_clockTime *= SEC_TO_MICRO; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1792 | break; |
| 1793 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1794 | case 's': /* manually select seed */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1795 | argument++; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1796 | seedset=1; |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 1797 | seed=0; |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1798 | while ((*argument>='0') && (*argument<='9')) { |
| 1799 | seed *= 10; |
| 1800 | seed += *argument - '0'; |
| 1801 | argument++; |
| 1802 | } |
| 1803 | break; |
| 1804 | |
Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 1805 | case 't': /* select starting test number */ |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1806 | argument++; |
| 1807 | testNb=0; |
| 1808 | while ((*argument>='0') && (*argument<='9')) { |
| 1809 | testNb *= 10; |
| 1810 | testNb += *argument - '0'; |
| 1811 | argument++; |
| 1812 | } |
| 1813 | break; |
| 1814 | |
| 1815 | case 'P': /* compressibility % */ |
| 1816 | argument++; |
| 1817 | proba=0; |
| 1818 | while ((*argument>='0') && (*argument<='9')) { |
| 1819 | proba *= 10; |
| 1820 | proba += *argument - '0'; |
| 1821 | argument++; |
| 1822 | } |
| 1823 | if (proba<0) proba=0; |
| 1824 | if (proba>100) proba=100; |
| 1825 | break; |
| 1826 | |
| 1827 | default: |
| 1828 | return FUZ_usage(programName); |
| 1829 | } |
| 1830 | } } } /* for(argNb=1; argNb<argc; argNb++) */ |
| 1831 | |
| 1832 | /* Get Seed */ |
Yann Collet | bb85581 | 2016-08-25 19:11:11 +0200 | [diff] [blame] | 1833 | 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] | 1834 | |
Yann Collet | ef9999f | 2016-09-01 16:44:48 -0700 | [diff] [blame] | 1835 | if (!seedset) { |
| 1836 | time_t const t = time(NULL); |
| 1837 | U32 const h = XXH32(&t, sizeof(t), 1); |
| 1838 | seed = h % 10000; |
| 1839 | } |
| 1840 | |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1841 | DISPLAY("Seed = %u\n", seed); |
| 1842 | if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba); |
| 1843 | |
| 1844 | if (nbTests<=0) nbTests=1; |
| 1845 | |
| 1846 | if (testNb==0) { |
Yann Collet | bd88f63 | 2017-11-27 12:15:23 -0800 | [diff] [blame] | 1847 | result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */ |
Yann Collet | 4616fad | 2017-07-10 17:16:41 -0700 | [diff] [blame] | 1848 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1849 | |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1850 | if (!result) { |
| 1851 | switch(selected_api) |
| 1852 | { |
| 1853 | case simple_api : |
| 1854 | result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100, bigTests); |
| 1855 | break; |
| 1856 | case mt_api : |
| 1857 | result = fuzzerTests_MT(seed, nbTests, testNb, ((double)proba) / 100, bigTests); |
| 1858 | break; |
| 1859 | case advanced_api : |
Stella Lau | 73c73bf | 2017-08-21 12:41:19 -0700 | [diff] [blame] | 1860 | result = fuzzerTests_newAPI(seed, nbTests, testNb, ((double)proba) / 100, bigTests, useOpaqueAPI); |
Yann Collet | 01743a3 | 2017-06-16 17:56:41 -0700 | [diff] [blame] | 1861 | break; |
| 1862 | default : |
| 1863 | assert(0); /* impossible */ |
| 1864 | } |
| 1865 | } |
Yann Collet | d7883a2 | 2016-08-12 16:48:02 +0200 | [diff] [blame] | 1866 | |
| 1867 | if (mainPause) { |
| 1868 | int unused; |
| 1869 | DISPLAY("Press Enter \n"); |
| 1870 | unused = getchar(); |
| 1871 | (void)unused; |
| 1872 | } |
| 1873 | return result; |
| 1874 | } |