Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under the BSD-style license found in the |
| 6 | * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 9 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 10 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 11 | /*-************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 12 | * Compiler specific |
| 13 | **************************************/ |
| 14 | #ifdef _MSC_VER /* Visual Studio */ |
Przemyslaw Skibinski | 97a258d | 2016-12-21 14:00:41 +0100 | [diff] [blame] | 15 | # define _CRT_SECURE_NO_WARNINGS /* fgets */ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 16 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
cyan | b880631 | 2016-05-30 18:20:46 +0200 | [diff] [blame] | 17 | # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 18 | #endif |
| 19 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 20 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 21 | /*-************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 22 | * Includes |
| 23 | **************************************/ |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 24 | #include <stdlib.h> /* free */ |
| 25 | #include <stdio.h> /* fgets, sscanf */ |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 26 | #include <string.h> /* strcmp */ |
| 27 | #include <time.h> /* clock_t */ |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 28 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressContinue, ZSTD_compressBlock */ |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 29 | #include "zstd.h" /* ZSTD_VERSION_STRING */ |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 30 | #include "zstd_errors.h" /* ZSTD_getErrorCode */ |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 31 | #include "zdict.h" /* ZDICT_trainFromBuffer */ |
| 32 | #include "datagen.h" /* RDG_genBuffer */ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 33 | #include "mem.h" |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 34 | #define XXH_STATIC_LINKING_ONLY |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 35 | #include "xxhash.h" /* XXH64 */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 36 | |
| 37 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 38 | /*-************************************ |
| 39 | * Constants |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 40 | **************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 41 | #define KB *(1U<<10) |
| 42 | #define MB *(1U<<20) |
| 43 | #define GB *(1U<<30) |
| 44 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 45 | static const U32 FUZ_compressibility_default = 50; |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 46 | static const U32 nbTestsDefault = 30000; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 47 | |
| 48 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 49 | /*-************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 50 | * Display Macros |
| 51 | **************************************/ |
| 52 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 53 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 54 | static U32 g_displayLevel = 2; |
| 55 | |
| 56 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 57 | if ((FUZ_clockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ |
| 58 | { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 59 | if (g_displayLevel>=4) fflush(stdout); } } |
Yann Collet | 64deef3 | 2016-09-14 00:16:07 +0200 | [diff] [blame] | 60 | static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6; |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 61 | static clock_t g_displayClock = 0; |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 62 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 63 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 64 | /*-******************************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 65 | * Fuzzer functions |
| 66 | *********************************************************/ |
Yann Collet | e93add0 | 2016-02-15 17:44:14 +0100 | [diff] [blame] | 67 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 68 | |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 69 | static clock_t FUZ_clockSpan(clock_t cStart) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 70 | { |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 71 | return clock() - cStart; /* works even when overflow; max span ~ 30mn */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 75 | #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 76 | static unsigned FUZ_rand(unsigned* src) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 77 | { |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 78 | static const U32 prime1 = 2654435761U; |
| 79 | static const U32 prime2 = 2246822519U; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 80 | U32 rand32 = *src; |
| 81 | rand32 *= prime1; |
| 82 | rand32 += prime2; |
| 83 | rand32 = FUZ_rotl32(rand32, 13); |
| 84 | *src = rand32; |
| 85 | return rand32 >> 5; |
| 86 | } |
| 87 | |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 88 | static unsigned FUZ_highbit32(U32 v32) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 89 | { |
| 90 | unsigned nbBits = 0; |
| 91 | if (v32==0) return 0; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 92 | while (v32) v32 >>= 1, nbBits++; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 93 | return nbBits; |
| 94 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 95 | |
| 96 | |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 97 | /*============================================= |
| 98 | * Basic Unit tests |
| 99 | =============================================*/ |
| 100 | |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 101 | #define CHECK_V(var, fn) size_t const var = fn; if (ZSTD_isError(var)) goto _output_error |
| 102 | #define CHECK(fn) { CHECK_V(err, fn); } |
| 103 | #define CHECKPLUS(var, fn, more) { CHECK_V(var, fn); more; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 104 | static int basicUnitTests(U32 seed, double compressibility) |
| 105 | { |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 106 | size_t const CNBuffSize = 5 MB; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 107 | void* const CNBuffer = malloc(CNBuffSize); |
| 108 | void* const compressedBuffer = malloc(ZSTD_compressBound(CNBuffSize)); |
| 109 | void* const decodedBuffer = malloc(CNBuffSize); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 110 | int testResult = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 111 | U32 testNb=0; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 112 | size_t cSize; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 113 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 114 | /* Create compressible noise */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 115 | if (!CNBuffer || !compressedBuffer || !decodedBuffer) { |
Yann Collet | 94f998b | 2015-07-04 23:10:40 -0800 | [diff] [blame] | 116 | DISPLAY("Not enough memory, aborting\n"); |
| 117 | testResult = 1; |
| 118 | goto _end; |
| 119 | } |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 120 | RDG_genBuffer(CNBuffer, CNBuffSize, compressibility, 0., seed); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 121 | |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 122 | /* Basic tests */ |
Yann Collet | 9a69ec4 | 2016-08-01 16:25:58 +0200 | [diff] [blame] | 123 | DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName : ", testNb++); |
| 124 | { const char* errorString = ZSTD_getErrorName(0); |
| 125 | DISPLAYLEVEL(4, "OK : %s \n", errorString); |
| 126 | } |
| 127 | |
| 128 | DISPLAYLEVEL(4, "test%3i : ZSTD_getErrorName with wrong value : ", testNb++); |
| 129 | { const char* errorString = ZSTD_getErrorName(499); |
| 130 | DISPLAYLEVEL(4, "OK : %s \n", errorString); |
| 131 | } |
| 132 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 133 | DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, (U32)CNBuffSize); |
| 134 | CHECKPLUS(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 135 | CNBuffer, CNBuffSize, 1), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 136 | cSize=r ); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 137 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 138 | |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 139 | DISPLAYLEVEL(4, "test%3i : decompressed size test : ", testNb++); |
| 140 | { unsigned long long const rSize = ZSTD_getDecompressedSize(compressedBuffer, cSize); |
| 141 | if (rSize != CNBuffSize) goto _output_error; |
| 142 | } |
| 143 | DISPLAYLEVEL(4, "OK \n"); |
| 144 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 145 | DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)CNBuffSize); |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 146 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize); |
| 147 | if (r != CNBuffSize) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 148 | DISPLAYLEVEL(4, "OK \n"); |
| 149 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 150 | DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); |
| 151 | { size_t u; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 152 | for (u=0; u<CNBuffSize; u++) { |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 153 | if (((BYTE*)decodedBuffer)[u] != ((BYTE*)CNBuffer)[u]) goto _output_error;; |
| 154 | } } |
| 155 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 156 | |
| 157 | DISPLAYLEVEL(4, "test%3i : decompress with 1 missing byte : ", testNb++); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 158 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize-1); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 159 | if (!ZSTD_isError(r)) goto _output_error; |
| 160 | if (ZSTD_getErrorCode((size_t)r) != ZSTD_error_srcSize_wrong) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 161 | DISPLAYLEVEL(4, "OK \n"); |
| 162 | |
| 163 | DISPLAYLEVEL(4, "test%3i : decompress with 1 too much byte : ", testNb++); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 164 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize+1); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 165 | if (!ZSTD_isError(r)) goto _output_error; |
| 166 | if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 167 | DISPLAYLEVEL(4, "OK \n"); |
| 168 | |
Yann Collet | 389648c | 2016-04-12 19:13:08 +0200 | [diff] [blame] | 169 | /* Dictionary and CCtx Duplication tests */ |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 170 | { ZSTD_CCtx* const ctxOrig = ZSTD_createCCtx(); |
| 171 | ZSTD_CCtx* const ctxDuplicated = ZSTD_createCCtx(); |
| 172 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
| 173 | static const size_t dictSize = 551; |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 174 | |
Yann Collet | 887e7da | 2016-04-11 20:12:27 +0200 | [diff] [blame] | 175 | DISPLAYLEVEL(4, "test%3i : copy context too soon : ", testNb++); |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 176 | { size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig, 0); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 177 | if (!ZSTD_isError(copyResult)) goto _output_error; } /* error must be detected */ |
Yann Collet | 887e7da | 2016-04-11 20:12:27 +0200 | [diff] [blame] | 178 | DISPLAYLEVEL(4, "OK \n"); |
| 179 | |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 180 | DISPLAYLEVEL(4, "test%3i : load dictionary into context : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 181 | CHECK( ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2) ); |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 182 | CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, CNBuffSize - dictSize) ); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 183 | DISPLAYLEVEL(4, "OK \n"); |
| 184 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 185 | DISPLAYLEVEL(4, "test%3i : compress with flat dictionary : ", testNb++); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 186 | cSize = 0; |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 187 | CHECKPLUS(r, ZSTD_compressEnd(ctxOrig, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 188 | (const char*)CNBuffer + dictSize, CNBuffSize - dictSize), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 189 | cSize += r); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 190 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 191 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 192 | DISPLAYLEVEL(4, "test%3i : frame built with flat dictionary should be decompressible : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 193 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 194 | decodedBuffer, CNBuffSize, |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 195 | compressedBuffer, cSize, |
| 196 | CNBuffer, dictSize), |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 197 | if (r != CNBuffSize - dictSize) goto _output_error); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 198 | DISPLAYLEVEL(4, "OK \n"); |
| 199 | |
| 200 | DISPLAYLEVEL(4, "test%3i : compress with duplicated context : ", testNb++); |
Yann Collet | 389648c | 2016-04-12 19:13:08 +0200 | [diff] [blame] | 201 | { size_t const cSizeOrig = cSize; |
| 202 | cSize = 0; |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 203 | CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 204 | (const char*)CNBuffer + dictSize, CNBuffSize - dictSize), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 205 | cSize += r); |
Yann Collet | 227cc39 | 2016-07-15 11:27:09 +0200 | [diff] [blame] | 206 | if (cSize != cSizeOrig) goto _output_error; /* should be identical ==> same size */ |
Yann Collet | 389648c | 2016-04-12 19:13:08 +0200 | [diff] [blame] | 207 | } |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 208 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 209 | |
| 210 | DISPLAYLEVEL(4, "test%3i : frame built with duplicated context should be decompressible : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 211 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 212 | decodedBuffer, CNBuffSize, |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 213 | compressedBuffer, cSize, |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 214 | CNBuffer, dictSize), |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 215 | if (r != CNBuffSize - dictSize) goto _output_error); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 216 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | 541dc7c | 2016-04-12 18:00:20 +0200 | [diff] [blame] | 217 | |
| 218 | DISPLAYLEVEL(4, "test%3i : check content size on duplicated context : ", testNb++); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 219 | { size_t const testSize = CNBuffSize / 3; |
Yann Collet | 6c6e175 | 2016-06-27 15:28:45 +0200 | [diff] [blame] | 220 | { ZSTD_parameters p = ZSTD_getParams(2, testSize, dictSize); |
| 221 | p.fParams.contentSizeFlag = 1; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 222 | CHECK( ZSTD_compressBegin_advanced(ctxOrig, CNBuffer, dictSize, p, testSize-1) ); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 223 | } |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 224 | CHECK( ZSTD_copyCCtx(ctxDuplicated, ctxOrig, testSize) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 225 | |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 226 | CHECKPLUS(r, ZSTD_compressEnd(ctxDuplicated, compressedBuffer, ZSTD_compressBound(testSize), |
| 227 | (const char*)CNBuffer + dictSize, testSize), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 228 | cSize = r); |
Yann Collet | 541dc7c | 2016-04-12 18:00:20 +0200 | [diff] [blame] | 229 | { ZSTD_frameParams fp; |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 230 | if (ZSTD_getFrameParams(&fp, compressedBuffer, cSize)) goto _output_error; |
Yann Collet | 541dc7c | 2016-04-12 18:00:20 +0200 | [diff] [blame] | 231 | if ((fp.frameContentSize != testSize) && (fp.frameContentSize != 0)) goto _output_error; |
| 232 | } } |
| 233 | DISPLAYLEVEL(4, "OK \n"); |
| 234 | |
| 235 | ZSTD_freeCCtx(ctxOrig); |
| 236 | ZSTD_freeCCtx(ctxDuplicated); |
| 237 | ZSTD_freeDCtx(dctx); |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 240 | /* Dictionary and dictBuilder tests */ |
| 241 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
| 242 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
| 243 | size_t dictSize = 16 KB; |
| 244 | void* dictBuffer = malloc(dictSize); |
| 245 | size_t const totalSampleSize = 1 MB; |
| 246 | size_t const sampleUnitSize = 8 KB; |
Yann Collet | b81cbba | 2016-05-30 22:29:45 +0200 | [diff] [blame] | 247 | U32 const nbSamples = (U32)(totalSampleSize / sampleUnitSize); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 248 | size_t* const samplesSizes = (size_t*) malloc(nbSamples * sizeof(size_t)); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 249 | U32 dictID; |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 250 | |
| 251 | if (dictBuffer==NULL || samplesSizes==NULL) { |
| 252 | free(dictBuffer); |
| 253 | free(samplesSizes); |
| 254 | goto _output_error; |
| 255 | } |
| 256 | |
| 257 | DISPLAYLEVEL(4, "test%3i : dictBuilder : ", testNb++); |
| 258 | { U32 u; for (u=0; u<nbSamples; u++) samplesSizes[u] = sampleUnitSize; } |
| 259 | dictSize = ZDICT_trainFromBuffer(dictBuffer, dictSize, |
| 260 | CNBuffer, samplesSizes, nbSamples); |
| 261 | if (ZDICT_isError(dictSize)) goto _output_error; |
| 262 | DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize); |
| 263 | |
Yann Collet | da3fbcb | 2016-08-19 14:23:58 +0200 | [diff] [blame] | 264 | DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 265 | dictID = ZDICT_getDictID(dictBuffer, dictSize); |
| 266 | if (dictID==0) goto _output_error; |
| 267 | DISPLAYLEVEL(4, "OK : %u \n", dictID); |
Yann Collet | da3fbcb | 2016-08-19 14:23:58 +0200 | [diff] [blame] | 268 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 269 | DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++); |
| 270 | cSize = ZSTD_compress_usingDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 271 | CNBuffer, CNBuffSize, |
| 272 | dictBuffer, dictSize, 4); |
| 273 | if (ZSTD_isError(cSize)) goto _output_error; |
| 274 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 275 | |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 276 | DISPLAYLEVEL(4, "test%3i : retrieve dictID from dictionary : ", testNb++); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 277 | { U32 const did = ZSTD_getDictID_fromDict(dictBuffer, dictSize); |
| 278 | if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */ |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 279 | } |
| 280 | DISPLAYLEVEL(4, "OK \n"); |
| 281 | |
| 282 | DISPLAYLEVEL(4, "test%3i : retrieve dictID from frame : ", testNb++); |
Yann Collet | f586bdf | 2016-12-06 06:11:46 +0100 | [diff] [blame] | 283 | { U32 const did = ZSTD_getDictID_fromFrame(compressedBuffer, cSize); |
| 284 | if (did != dictID) goto _output_error; /* non-conformant (content-only) dictionary */ |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 285 | } |
| 286 | DISPLAYLEVEL(4, "OK \n"); |
| 287 | |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 288 | DISPLAYLEVEL(4, "test%3i : frame built with dictionary should be decompressible : ", testNb++); |
| 289 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
| 290 | decodedBuffer, CNBuffSize, |
| 291 | compressedBuffer, cSize, |
| 292 | dictBuffer, dictSize), |
| 293 | if (r != CNBuffSize) goto _output_error); |
| 294 | DISPLAYLEVEL(4, "OK \n"); |
| 295 | |
| 296 | DISPLAYLEVEL(4, "test%3i : compress without dictID : ", testNb++); |
Yann Collet | 6c6e175 | 2016-06-27 15:28:45 +0200 | [diff] [blame] | 297 | { ZSTD_parameters p = ZSTD_getParams(3, CNBuffSize, dictSize); |
| 298 | p.fParams.noDictIDFlag = 1; |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 299 | cSize = ZSTD_compress_advanced(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), |
| 300 | CNBuffer, CNBuffSize, |
| 301 | dictBuffer, dictSize, p); |
| 302 | if (ZSTD_isError(cSize)) goto _output_error; |
| 303 | } |
| 304 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBuffSize*100); |
| 305 | |
| 306 | DISPLAYLEVEL(4, "test%3i : frame built without dictID should be decompressible : ", testNb++); |
| 307 | CHECKPLUS(r, ZSTD_decompress_usingDict(dctx, |
| 308 | decodedBuffer, CNBuffSize, |
| 309 | compressedBuffer, cSize, |
| 310 | dictBuffer, dictSize), |
| 311 | if (r != CNBuffSize) goto _output_error); |
| 312 | DISPLAYLEVEL(4, "OK \n"); |
| 313 | |
| 314 | ZSTD_freeCCtx(cctx); |
| 315 | ZSTD_freeDCtx(dctx); |
| 316 | free(dictBuffer); |
| 317 | free(samplesSizes); |
| 318 | } |
| 319 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 320 | /* Decompression defense tests */ |
| 321 | DISPLAYLEVEL(4, "test%3i : Check input length for magic number : ", testNb++); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 322 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 3); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 323 | if (!ZSTD_isError(r)) goto _output_error; |
| 324 | if (r != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 325 | DISPLAYLEVEL(4, "OK \n"); |
| 326 | |
| 327 | DISPLAYLEVEL(4, "test%3i : Check magic Number : ", testNb++); |
| 328 | ((char*)(CNBuffer))[0] = 1; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 329 | { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, CNBuffer, 4); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 330 | if (!ZSTD_isError(r)) goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 331 | DISPLAYLEVEL(4, "OK \n"); |
| 332 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 333 | /* block API tests */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 334 | { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 335 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
Yann Collet | d3d2db5 | 2016-07-15 12:20:26 +0200 | [diff] [blame] | 336 | static const size_t dictSize = 65 KB; |
| 337 | static const size_t blockSize = 100 KB; /* won't cause pb with small dict size */ |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 338 | size_t cSize2; |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 339 | |
| 340 | /* basic block compression */ |
| 341 | DISPLAYLEVEL(4, "test%3i : Block compression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 342 | CHECK( ZSTD_compressBegin(cctx, 5) ); |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 343 | cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize); |
| 344 | if (ZSTD_isError(cSize)) goto _output_error; |
| 345 | DISPLAYLEVEL(4, "OK \n"); |
| 346 | |
| 347 | DISPLAYLEVEL(4, "test%3i : Block decompression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 348 | CHECK( ZSTD_decompressBegin(dctx) ); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 349 | { CHECK_V(r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) ); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 350 | if (r != blockSize) goto _output_error; } |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 351 | DISPLAYLEVEL(4, "OK \n"); |
| 352 | |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 353 | /* dictionary block compression */ |
| 354 | DISPLAYLEVEL(4, "test%3i : Dictionary Block compression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 355 | CHECK( ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5) ); |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 356 | cSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize); |
| 357 | if (ZSTD_isError(cSize)) goto _output_error; |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 358 | cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize+blockSize, blockSize); |
| 359 | if (ZSTD_isError(cSize2)) goto _output_error; |
Yann Collet | aa2628d | 2016-07-07 15:28:41 +0200 | [diff] [blame] | 360 | memcpy((char*)compressedBuffer+cSize, (char*)CNBuffer+dictSize+blockSize, blockSize); /* fake non-compressed block */ |
| 361 | cSize2 = ZSTD_compressBlock(cctx, (char*)compressedBuffer+cSize+blockSize, ZSTD_compressBound(blockSize), |
| 362 | (char*)CNBuffer+dictSize+2*blockSize, blockSize); |
| 363 | if (ZSTD_isError(cSize2)) goto _output_error; |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 364 | DISPLAYLEVEL(4, "OK \n"); |
| 365 | |
| 366 | DISPLAYLEVEL(4, "test%3i : Dictionary Block decompression test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 367 | CHECK( ZSTD_decompressBegin_usingDict(dctx, CNBuffer, dictSize) ); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 368 | { CHECK_V( r, ZSTD_decompressBlock(dctx, decodedBuffer, CNBuffSize, compressedBuffer, cSize) ); |
| 369 | if (r != blockSize) goto _output_error; } |
Yann Collet | aa2628d | 2016-07-07 15:28:41 +0200 | [diff] [blame] | 370 | ZSTD_insertBlock(dctx, (char*)decodedBuffer+blockSize, blockSize); /* insert non-compressed block into dctx history */ |
| 371 | { CHECK_V( r, ZSTD_decompressBlock(dctx, (char*)decodedBuffer+2*blockSize, CNBuffSize, (char*)compressedBuffer+cSize+blockSize, cSize2) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 372 | if (r != blockSize) goto _output_error; } |
Yann Collet | b012510 | 2016-01-09 02:00:10 +0100 | [diff] [blame] | 373 | DISPLAYLEVEL(4, "OK \n"); |
| 374 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 375 | ZSTD_freeCCtx(cctx); |
| 376 | ZSTD_freeDCtx(dctx); |
| 377 | } |
| 378 | |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 379 | /* long rle test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 380 | { size_t sampleSize = 0; |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 381 | DISPLAYLEVEL(4, "test%3i : Long RLE test : ", testNb++); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 382 | RDG_genBuffer(CNBuffer, sampleSize, compressibility, 0., seed+1); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 383 | memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1); |
| 384 | sampleSize += 256 KB - 1; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 385 | RDG_genBuffer((char*)CNBuffer+sampleSize, 96 KB, compressibility, 0., seed+2); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 386 | sampleSize += 96 KB; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 387 | cSize = ZSTD_compress(compressedBuffer, ZSTD_compressBound(sampleSize), CNBuffer, sampleSize, 1); |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 388 | if (ZSTD_isError(cSize)) goto _output_error; |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 389 | { CHECK_V(regenSize, ZSTD_decompress(decodedBuffer, sampleSize, compressedBuffer, cSize)); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 390 | if (regenSize!=sampleSize) goto _output_error; } |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 391 | DISPLAYLEVEL(4, "OK \n"); |
| 392 | } |
| 393 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 394 | /* All zeroes test (test bug #137) */ |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 395 | #define ZEROESLENGTH 100 |
| 396 | DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH); |
| 397 | memset(CNBuffer, 0, ZEROESLENGTH); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 398 | { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 399 | cSize = r; } |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 400 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100); |
| 401 | |
| 402 | DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 403 | { CHECK_V(r, ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 404 | if (r != ZEROESLENGTH) goto _output_error; } |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 405 | DISPLAYLEVEL(4, "OK \n"); |
| 406 | |
| 407 | /* nbSeq limit test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 408 | #define _3BYTESTESTLENGTH 131000 |
| 409 | #define NB3BYTESSEQLOG 9 |
| 410 | #define NB3BYTESSEQ (1 << NB3BYTESSEQLOG) |
| 411 | #define NB3BYTESSEQMASK (NB3BYTESSEQ-1) |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 412 | /* creates a buffer full of 3-bytes sequences */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 413 | { BYTE _3BytesSeqs[NB3BYTESSEQ][3]; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 414 | U32 rSeed = 1; |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 415 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 416 | /* create batch of 3-bytes sequences */ |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 417 | { int i; |
| 418 | for (i=0; i < NB3BYTESSEQ; i++) { |
| 419 | _3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 420 | _3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 421 | _3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&rSeed) & 255); |
| 422 | } } |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 423 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 424 | /* randomly fills CNBuffer with prepared 3-bytes sequences */ |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 425 | { int i; |
| 426 | for (i=0; i < _3BYTESTESTLENGTH; i += 3) { /* note : CNBuffer size > _3BYTESTESTLENGTH+3 */ |
| 427 | U32 const id = FUZ_rand(&rSeed) & NB3BYTESSEQMASK; |
| 428 | ((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0]; |
| 429 | ((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1]; |
| 430 | ((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2]; |
| 431 | } } } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 432 | DISPLAYLEVEL(4, "test%3i : compress lots 3-bytes sequences : ", testNb++); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 433 | { CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH), |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 434 | CNBuffer, _3BYTESTESTLENGTH, 19) ); |
| 435 | cSize = r; } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 436 | DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/_3BYTESTESTLENGTH*100); |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 437 | |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 438 | DISPLAYLEVEL(4, "test%3i : decompress lots 3-bytes sequence : ", testNb++); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 439 | { CHECK_V(r, ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize) ); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 440 | if (r != _3BYTESTESTLENGTH) goto _output_error; } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 441 | DISPLAYLEVEL(4, "OK \n"); |
Yann Collet | 4ba8534 | 2016-03-07 20:01:45 +0100 | [diff] [blame] | 442 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 443 | _end: |
| 444 | free(CNBuffer); |
| 445 | free(compressedBuffer); |
| 446 | free(decodedBuffer); |
| 447 | return testResult; |
| 448 | |
| 449 | _output_error: |
| 450 | testResult = 1; |
| 451 | DISPLAY("Error detected in Unit tests ! \n"); |
| 452 | goto _end; |
| 453 | } |
| 454 | |
| 455 | |
| 456 | static size_t findDiff(const void* buf1, const void* buf2, size_t max) |
| 457 | { |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 458 | const BYTE* b1 = (const BYTE*)buf1; |
| 459 | const BYTE* b2 = (const BYTE*)buf2; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 460 | size_t u; |
| 461 | for (u=0; u<max; u++) { |
| 462 | if (b1[u] != b2[u]) break; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 463 | } |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 464 | return u; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 467 | |
| 468 | static size_t FUZ_rLogLength(U32* seed, U32 logLength) |
| 469 | { |
| 470 | size_t const lengthMask = ((size_t)1 << logLength) - 1; |
| 471 | return (lengthMask+1) + (FUZ_rand(seed) & lengthMask); |
| 472 | } |
| 473 | |
| 474 | static size_t FUZ_randomLength(U32* seed, U32 maxLog) |
| 475 | { |
| 476 | U32 const logLength = FUZ_rand(seed) % maxLog; |
| 477 | return FUZ_rLogLength(seed, logLength); |
| 478 | } |
| 479 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 480 | #undef CHECK |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 481 | #define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ |
| 482 | DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 483 | |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 484 | static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxDurationS, double compressibility) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 485 | { |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 486 | static const U32 maxSrcLog = 23; |
| 487 | static const U32 maxSampleLog = 22; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 488 | size_t const srcBufferSize = (size_t)1<<maxSrcLog; |
| 489 | size_t const dstBufferSize = (size_t)1<<maxSampleLog; |
| 490 | size_t const cBufferSize = ZSTD_compressBound(dstBufferSize); |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 491 | BYTE* cNoiseBuffer[5]; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 492 | BYTE* srcBuffer; /* jumping pointer */ |
| 493 | BYTE* const cBuffer = (BYTE*) malloc (cBufferSize); |
| 494 | BYTE* const dstBuffer = (BYTE*) malloc (dstBufferSize); |
| 495 | BYTE* const mirrorBuffer = (BYTE*) malloc (dstBufferSize); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 496 | ZSTD_CCtx* const refCtx = ZSTD_createCCtx(); |
| 497 | ZSTD_CCtx* const ctx = ZSTD_createCCtx(); |
| 498 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 499 | U32 result = 0; |
| 500 | U32 testNb = 0; |
| 501 | U32 coreSeed = seed, lseed = 0; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 502 | clock_t const startClock = clock(); |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 503 | clock_t const maxClockSpan = maxDurationS * CLOCKS_PER_SEC; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 504 | |
| 505 | /* allocation */ |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 506 | cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); |
| 507 | cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize); |
| 508 | cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize); |
| 509 | cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize); |
| 510 | cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 511 | CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 512 | || !dstBuffer || !mirrorBuffer || !cBuffer || !refCtx || !ctx || !dctx, |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 513 | "Not enough memory, fuzzer tests cancelled"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 514 | |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 515 | /* Create initial samples */ |
| 516 | RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */ |
| 517 | RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */ |
| 518 | RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); |
| 519 | RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ |
| 520 | RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ |
| 521 | srcBuffer = cNoiseBuffer[2]; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 522 | |
| 523 | /* catch up testNb */ |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 524 | for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 525 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 526 | /* main test loop */ |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 527 | for ( ; (testNb <= nbTests) || (FUZ_clockSpan(startClock) < maxClockSpan); testNb++ ) { |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 528 | size_t sampleSize, maxTestSize, totalTestSize; |
| 529 | size_t cSize, totalCSize, totalGenSize; |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 530 | XXH64_state_t xxhState; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 531 | U64 crcOrig; |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 532 | BYTE* sampleBuffer; |
Yann Collet | 4bfe415 | 2015-12-06 13:18:37 +0100 | [diff] [blame] | 533 | const BYTE* dict; |
| 534 | size_t dictSize; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 535 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 536 | /* notification */ |
| 537 | if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); } |
Yann Collet | 4c0b44f | 2016-11-01 11:13:22 -0700 | [diff] [blame] | 538 | else { DISPLAYUPDATE(2, "\r%6u ", testNb); } |
Yann Collet | 1c2ddba | 2015-12-04 17:45:35 +0100 | [diff] [blame] | 539 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 540 | FUZ_rand(&coreSeed); |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 541 | { U32 const prime1 = 2654435761U; lseed = coreSeed ^ prime1; } |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 542 | |
| 543 | /* srcBuffer selection [0-4] */ |
| 544 | { U32 buffNb = FUZ_rand(&lseed) & 0x7F; |
| 545 | if (buffNb & 7) buffNb=2; /* most common : compressible (P) */ |
| 546 | else { |
| 547 | buffNb >>= 3; |
| 548 | if (buffNb & 7) { |
| 549 | const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */ |
| 550 | buffNb = tnb[buffNb >> 3]; |
| 551 | } else { |
| 552 | const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */ |
| 553 | buffNb = tnb[buffNb >> 3]; |
| 554 | } } |
| 555 | srcBuffer = cNoiseBuffer[buffNb]; |
| 556 | } |
| 557 | |
| 558 | /* select src segment */ |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 559 | sampleSize = FUZ_randomLength(&lseed, maxSampleLog); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 560 | |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 561 | /* create sample buffer (to catch read error with valgrind & sanitizers) */ |
| 562 | sampleBuffer = (BYTE*)malloc(sampleSize); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 563 | CHECK(sampleBuffer==NULL, "not enough memory for sample buffer"); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 564 | { size_t const sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); |
| 565 | memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); } |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 566 | crcOrig = XXH64(sampleBuffer, sampleSize, 0); |
| 567 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 568 | /* compression tests */ |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 569 | { unsigned const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (FUZ_highbit32((U32)sampleSize)/3))) + 1; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 570 | cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 571 | CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed : %s", ZSTD_getErrorName(cSize)); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 572 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 573 | /* compression failure test : too small dest buffer */ |
| 574 | if (cSize > 3) { |
| 575 | const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 576 | const size_t tooSmallSize = cSize - missing; |
| 577 | const U32 endMark = 0x4DC2B1A9; |
| 578 | memcpy(dstBuffer+tooSmallSize, &endMark, 4); |
| 579 | { size_t const errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel); |
| 580 | CHECK(!ZSTD_isError(errorCode), "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (U32)tooSmallSize, (U32)cSize); } |
| 581 | { U32 endCheck; memcpy(&endCheck, dstBuffer+tooSmallSize, 4); |
| 582 | CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow"); } |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 583 | } } |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 584 | |
Yann Collet | f323bf7 | 2016-07-07 13:14:21 +0200 | [diff] [blame] | 585 | /* Decompressed size test */ |
| 586 | { unsigned long long const rSize = ZSTD_getDecompressedSize(cBuffer, cSize); |
| 587 | CHECK(rSize != sampleSize, "decompressed size incorrect"); |
| 588 | } |
| 589 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 590 | /* frame header decompression test */ |
Yann Collet | 346bffb | 2016-03-15 15:24:52 +0100 | [diff] [blame] | 591 | { ZSTD_frameParams dParams; |
| 592 | size_t const check = ZSTD_getFrameParams(&dParams, cBuffer, cSize); |
| 593 | CHECK(ZSTD_isError(check), "Frame Parameters extraction failed"); |
| 594 | CHECK(dParams.frameContentSize != sampleSize, "Frame content size incorrect"); |
| 595 | } |
| 596 | |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 597 | /* successful decompression test */ |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 598 | { size_t const margin = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1; |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 599 | size_t const dSize = ZSTD_decompress(dstBuffer, sampleSize + margin, cBuffer, cSize); |
Yann Collet | 546c9b1 | 2016-03-19 12:47:52 +0100 | [diff] [blame] | 600 | CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s) (srcSize : %u ; cSize : %u)", ZSTD_getErrorName(dSize), (U32)sampleSize, (U32)cSize); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 601 | { U64 const crcDest = XXH64(dstBuffer, sampleSize, 0); |
| 602 | CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (U32)findDiff(sampleBuffer, dstBuffer, sampleSize), (U32)sampleSize); |
| 603 | } } |
Yann Collet | 110cc14 | 2015-11-19 12:02:28 +0100 | [diff] [blame] | 604 | |
| 605 | free(sampleBuffer); /* no longer useful after this point */ |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 606 | |
| 607 | /* truncated src decompression test */ |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 608 | { size_t const missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 609 | size_t const tooSmallSize = cSize - missing; |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 610 | void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch read overflows */ |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 611 | CHECK(cBufferTooSmall == NULL, "not enough memory !"); |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 612 | memcpy(cBufferTooSmall, cBuffer, tooSmallSize); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 613 | { size_t const errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize); |
| 614 | CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); } |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 615 | free(cBufferTooSmall); |
| 616 | } |
| 617 | |
| 618 | /* too small dst decompression test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 619 | if (sampleSize > 3) { |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 620 | size_t const missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ |
| 621 | size_t const tooSmallSize = sampleSize - missing; |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 622 | static const BYTE token = 0xA9; |
| 623 | dstBuffer[tooSmallSize] = token; |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 624 | { size_t const errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize); |
| 625 | CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (U32)errorCode, (U32)tooSmallSize); } |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 626 | CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow"); |
| 627 | } |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 628 | |
| 629 | /* noisy src decompression test */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 630 | if (cSize > 6) { |
| 631 | /* insert noise into src */ |
| 632 | { U32 const maxNbBits = FUZ_highbit32((U32)(cSize-4)); |
| 633 | size_t pos = 4; /* preserve magic number (too easy to detect) */ |
| 634 | for (;;) { |
| 635 | /* keep some original src */ |
| 636 | { U32 const nbBits = FUZ_rand(&lseed) % maxNbBits; |
| 637 | size_t const mask = (1<<nbBits) - 1; |
| 638 | size_t const skipLength = FUZ_rand(&lseed) & mask; |
| 639 | pos += skipLength; |
| 640 | } |
| 641 | if (pos <= cSize) break; |
| 642 | /* add noise */ |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 643 | { U32 const nbBitsCodes = FUZ_rand(&lseed) % maxNbBits; |
| 644 | U32 const nbBits = nbBitsCodes ? nbBitsCodes-1 : 0; |
| 645 | size_t const mask = (1<<nbBits) - 1; |
| 646 | size_t const rNoiseLength = (FUZ_rand(&lseed) & mask) + 1; |
| 647 | size_t const noiseLength = MIN(rNoiseLength, cSize-pos); |
| 648 | size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength); |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 649 | memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength); |
| 650 | pos += noiseLength; |
| 651 | } } } |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 652 | |
| 653 | /* decompress noisy source */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 654 | { U32 const endMark = 0xA9B1C3D6; |
Yann Collet | 997f9ee | 2015-08-21 02:44:20 +0100 | [diff] [blame] | 655 | memcpy(dstBuffer+sampleSize, &endMark, 4); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 656 | { size_t const decompressResult = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize); |
| 657 | /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */ |
| 658 | CHECK((!ZSTD_isError(decompressResult)) && (decompressResult>sampleSize), |
| 659 | "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (U32)decompressResult, (U32)sampleSize); |
| 660 | } |
| 661 | { U32 endCheck; memcpy(&endCheck, dstBuffer+sampleSize, 4); |
| 662 | CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow"); |
| 663 | } } } /* noisy src decompression test */ |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 664 | |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 665 | /*===== Streaming compression test, scattered segments and dictionary =====*/ |
| 666 | |
| 667 | { U32 const testLog = FUZ_rand(&lseed) % maxSrcLog; |
| 668 | int const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1; |
| 669 | maxTestSize = FUZ_rLogLength(&lseed, testLog); |
| 670 | if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1; |
| 671 | |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 672 | dictSize = FUZ_randomLength(&lseed, maxSampleLog); /* needed also for decompression */ |
| 673 | dict = srcBuffer + (FUZ_rand(&lseed) % (srcBufferSize - dictSize)); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 674 | |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 675 | if (FUZ_rand(&lseed) & 0xF) { |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 676 | size_t const errorCode = ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, cLevel); |
| 677 | CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode)); |
| 678 | } else { |
cyan | b880631 | 2016-05-30 18:20:46 +0200 | [diff] [blame] | 679 | ZSTD_compressionParameters const cPar = ZSTD_getCParams(cLevel, 0, dictSize); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 680 | ZSTD_frameParameters const fpar = { FUZ_rand(&lseed)&1 /* contentSizeFlag */, |
| 681 | !(FUZ_rand(&lseed)&3) /* contentChecksumFlag*/, |
| 682 | 0 /*NodictID*/ }; /* note : since dictionary is fake, dictIDflag has no impact */ |
cyan | b880631 | 2016-05-30 18:20:46 +0200 | [diff] [blame] | 683 | ZSTD_parameters p; |
| 684 | size_t errorCode; |
| 685 | p.cParams = cPar; p.fParams = fpar; |
| 686 | errorCode = ZSTD_compressBegin_advanced(refCtx, dict, dictSize, p, 0); |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 687 | CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_advanced error : %s", ZSTD_getErrorName(errorCode)); |
| 688 | } |
Yann Collet | 97b378a | 2016-09-21 17:20:19 +0200 | [diff] [blame] | 689 | { size_t const errorCode = ZSTD_copyCCtx(ctx, refCtx, 0); |
| 690 | CHECK (ZSTD_isError(errorCode), "ZSTD_copyCCtx error : %s", ZSTD_getErrorName(errorCode)); |
| 691 | } } |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 692 | XXH64_reset(&xxhState, 0); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 693 | { U32 const nbChunks = (FUZ_rand(&lseed) & 127) + 2; |
| 694 | U32 n; |
| 695 | for (totalTestSize=0, cSize=0, n=0 ; n<nbChunks ; n++) { |
| 696 | size_t const segmentSize = FUZ_randomLength(&lseed, maxSampleLog); |
| 697 | size_t const segmentStart = FUZ_rand(&lseed) % (srcBufferSize - segmentSize); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 698 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 699 | if (cBufferSize-cSize < ZSTD_compressBound(segmentSize)) break; /* avoid invalid dstBufferTooSmall */ |
| 700 | if (totalTestSize+segmentSize > maxTestSize) break; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 701 | |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 702 | { size_t const compressResult = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+segmentStart, segmentSize); |
| 703 | CHECK (ZSTD_isError(compressResult), "multi-segments compression error : %s", ZSTD_getErrorName(compressResult)); |
| 704 | cSize += compressResult; |
| 705 | } |
| 706 | XXH64_update(&xxhState, srcBuffer+segmentStart, segmentSize); |
| 707 | memcpy(mirrorBuffer + totalTestSize, srcBuffer+segmentStart, segmentSize); |
| 708 | totalTestSize += segmentSize; |
| 709 | } } |
| 710 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 711 | { size_t const flushResult = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize, NULL, 0); |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 712 | CHECK (ZSTD_isError(flushResult), "multi-segments epilogue error : %s", ZSTD_getErrorName(flushResult)); |
| 713 | cSize += flushResult; |
| 714 | } |
Yann Collet | 6c903a8 | 2016-05-28 13:34:07 +0200 | [diff] [blame] | 715 | crcOrig = XXH64_digest(&xxhState); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 716 | |
| 717 | /* streaming decompression test */ |
Yann Collet | 33341de | 2016-05-29 23:09:51 +0200 | [diff] [blame] | 718 | if (dictSize<8) dictSize=0, dict=NULL; /* disable dictionary */ |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 719 | { size_t const errorCode = ZSTD_decompressBegin_usingDict(dctx, dict, dictSize); |
Yann Collet | 3000952 | 2016-05-30 16:17:33 +0200 | [diff] [blame] | 720 | CHECK (ZSTD_isError(errorCode), "ZSTD_decompressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode)); } |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 721 | totalCSize = 0; |
| 722 | totalGenSize = 0; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 723 | while (totalCSize < cSize) { |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 724 | size_t const inSize = ZSTD_nextSrcSizeToDecompress(dctx); |
| 725 | size_t const genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize); |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 726 | CHECK (ZSTD_isError(genSize), "ZSTD_decompressContinue error : %s", ZSTD_getErrorName(genSize)); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 727 | totalGenSize += genSize; |
| 728 | totalCSize += inSize; |
| 729 | } |
| 730 | CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded"); |
Yann Collet | c0a9bf3 | 2016-05-30 01:56:08 +0200 | [diff] [blame] | 731 | CHECK (totalGenSize != totalTestSize, "streaming decompressed data : wrong size") |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 732 | CHECK (totalCSize != cSize, "compressed data should be fully read") |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 733 | { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0); |
| 734 | if (crcDest!=crcOrig) { |
| 735 | size_t const errorPos = findDiff(mirrorBuffer, dstBuffer, totalTestSize); |
Yann Collet | d2858e9 | 2016-05-30 15:10:09 +0200 | [diff] [blame] | 736 | CHECK (1, "streaming decompressed data corrupted : byte %u / %u (%02X!=%02X)", |
Yann Collet | 1fce6e0 | 2016-04-08 20:26:33 +0200 | [diff] [blame] | 737 | (U32)errorPos, (U32)totalTestSize, dstBuffer[errorPos], mirrorBuffer[errorPos]); |
| 738 | } } |
| 739 | } /* for ( ; (testNb <= nbTests) */ |
Yann Collet | 1c2ddba | 2015-12-04 17:45:35 +0100 | [diff] [blame] | 740 | DISPLAY("\r%u fuzzer tests completed \n", testNb-1); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 741 | |
| 742 | _cleanup: |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 743 | ZSTD_freeCCtx(refCtx); |
Yann Collet | 2f648e5 | 2015-10-29 18:23:38 +0100 | [diff] [blame] | 744 | ZSTD_freeCCtx(ctx); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 745 | ZSTD_freeDCtx(dctx); |
Yann Collet | d5d9bc3 | 2015-08-23 23:13:49 +0100 | [diff] [blame] | 746 | free(cNoiseBuffer[0]); |
| 747 | free(cNoiseBuffer[1]); |
| 748 | free(cNoiseBuffer[2]); |
| 749 | free(cNoiseBuffer[3]); |
| 750 | free(cNoiseBuffer[4]); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 751 | free(cBuffer); |
| 752 | free(dstBuffer); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 753 | free(mirrorBuffer); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 754 | return result; |
| 755 | |
| 756 | _output_error: |
| 757 | result = 1; |
| 758 | goto _cleanup; |
| 759 | } |
| 760 | |
| 761 | |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 762 | /*_******************************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 763 | * Command line |
| 764 | *********************************************************/ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 765 | int FUZ_usage(const char* programName) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 766 | { |
| 767 | DISPLAY( "Usage :\n"); |
| 768 | DISPLAY( " %s [args]\n", programName); |
| 769 | DISPLAY( "\n"); |
| 770 | DISPLAY( "Arguments :\n"); |
| 771 | DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); |
| 772 | DISPLAY( " -s# : Select seed (default:prompt user)\n"); |
| 773 | DISPLAY( " -t# : Select starting test number (default:0)\n"); |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 774 | DISPLAY( " -P# : Select compressibility in %% (default:%u%%)\n", FUZ_compressibility_default); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 775 | DISPLAY( " -v : verbose\n"); |
Yann Collet | e9853b2 | 2015-08-07 19:07:32 +0100 | [diff] [blame] | 776 | DISPLAY( " -p : pause at the end\n"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 777 | DISPLAY( " -h : display help and exit\n"); |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 782 | int main(int argc, const char** argv) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 783 | { |
| 784 | U32 seed=0; |
| 785 | int seedset=0; |
| 786 | int argNb; |
| 787 | int nbTests = nbTestsDefault; |
| 788 | int testNb = 0; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 789 | U32 proba = FUZ_compressibility_default; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 790 | int result=0; |
| 791 | U32 mainPause = 0; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 792 | U32 maxDuration = 0; |
Yann Collet | ea63bb7 | 2016-04-08 15:25:32 +0200 | [diff] [blame] | 793 | const char* programName = argv[0]; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 794 | |
| 795 | /* Check command line */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 796 | for (argNb=1; argNb<argc; argNb++) { |
| 797 | const char* argument = argv[argNb]; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 798 | if(!argument) continue; /* Protection if argument empty */ |
| 799 | |
| 800 | /* Handle commands. Aggregated commands are allowed */ |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 801 | if (argument[0]=='-') { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 802 | argument++; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 803 | while (*argument!=0) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 804 | switch(*argument) |
| 805 | { |
| 806 | case 'h': |
| 807 | return FUZ_usage(programName); |
| 808 | case 'v': |
| 809 | argument++; |
| 810 | g_displayLevel=4; |
| 811 | break; |
| 812 | case 'q': |
| 813 | argument++; |
| 814 | g_displayLevel--; |
| 815 | break; |
| 816 | case 'p': /* pause at the end */ |
| 817 | argument++; |
| 818 | mainPause = 1; |
| 819 | break; |
| 820 | |
| 821 | case 'i': |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 822 | argument++; maxDuration=0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 823 | nbTests=0; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 824 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 825 | nbTests *= 10; |
| 826 | nbTests += *argument - '0'; |
| 827 | argument++; |
| 828 | } |
| 829 | break; |
| 830 | |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 831 | case 'T': |
| 832 | argument++; |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 833 | nbTests=0; maxDuration=0; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 834 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 835 | maxDuration *= 10; |
| 836 | maxDuration += *argument - '0'; |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 837 | argument++; |
| 838 | } |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 839 | if (*argument=='m') maxDuration *=60, argument++; |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 840 | if (*argument=='n') argument++; |
Yann Collet | 553cf6a | 2015-12-04 17:25:26 +0100 | [diff] [blame] | 841 | break; |
| 842 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 843 | case 's': |
| 844 | argument++; |
| 845 | seed=0; |
| 846 | seedset=1; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 847 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 848 | seed *= 10; |
| 849 | seed += *argument - '0'; |
| 850 | argument++; |
| 851 | } |
| 852 | break; |
| 853 | |
| 854 | case 't': |
| 855 | argument++; |
| 856 | testNb=0; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 857 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 858 | testNb *= 10; |
| 859 | testNb += *argument - '0'; |
| 860 | argument++; |
| 861 | } |
| 862 | break; |
| 863 | |
| 864 | case 'P': /* compressibility % */ |
| 865 | argument++; |
| 866 | proba=0; |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 867 | while ((*argument>='0') && (*argument<='9')) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 868 | proba *= 10; |
| 869 | proba += *argument - '0'; |
| 870 | argument++; |
| 871 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 872 | if (proba>100) proba=100; |
| 873 | break; |
| 874 | |
| 875 | default: |
| 876 | return FUZ_usage(programName); |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 877 | } } } } /* for (argNb=1; argNb<argc; argNb++) */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 878 | |
| 879 | /* Get Seed */ |
Yann Collet | 45f84ab | 2016-05-20 12:34:40 +0200 | [diff] [blame] | 880 | DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 881 | |
Yann Collet | 3f01c88 | 2016-06-16 13:38:10 +0200 | [diff] [blame] | 882 | if (!seedset) { |
| 883 | time_t const t = time(NULL); |
| 884 | U32 const h = XXH32(&t, sizeof(t), 1); |
| 885 | seed = h % 10000; |
| 886 | } |
| 887 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 888 | DISPLAY("Seed = %u\n", seed); |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 889 | if (proba!=FUZ_compressibility_default) DISPLAY("Compressibility : %u%%\n", proba); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 890 | |
Yann Collet | 803c05e | 2016-06-16 11:32:57 +0200 | [diff] [blame] | 891 | if (nbTests < testNb) nbTests = testNb; |
| 892 | |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 893 | if (testNb==0) |
| 894 | result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 895 | if (!result) |
Yann Collet | 0d9ce04 | 2016-03-19 13:21:08 +0100 | [diff] [blame] | 896 | result = fuzzerTests(seed, nbTests, testNb, maxDuration, ((double)proba) / 100); |
Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 897 | if (mainPause) { |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 898 | int unused; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 899 | DISPLAY("Press Enter \n"); |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 900 | unused = getchar(); |
| 901 | (void)unused; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 902 | } |
| 903 | return result; |
| 904 | } |