blob: c21b9de9b6fc9224aac0e13be0c6722ba7215615 [file] [log] [blame]
Yann Collet4ded9e52016-08-30 10:04:33 -07001/**
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 Colletd7883a22016-08-12 16:48:02 +02009
Yann Colletd7883a22016-08-12 16:48:02 +020010
11/*-************************************
12* Compiler specific
13**************************************/
14#ifdef _MSC_VER /* Visual Studio */
15# define _CRT_SECURE_NO_WARNINGS /* fgets */
16# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
17# pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */
18#endif
19
20
21/*-************************************
22* Includes
23**************************************/
24#include <stdlib.h> /* free */
25#include <stdio.h> /* fgets, sscanf */
Yann Colletef9999f2016-09-01 16:44:48 -070026#include <time.h> /* clock_t, clock() */
Yann Colletd7883a22016-08-12 16:48:02 +020027#include <string.h> /* strcmp */
28#include "mem.h"
Yann Colletef9999f2016-09-01 16:44:48 -070029#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel, ZSTD_customMem */
Yann Colletd7883a22016-08-12 16:48:02 +020030#include "zstd.h" /* ZSTD_compressBound */
31#include "datagen.h" /* RDG_genBuffer */
Yann Colletef9999f2016-09-01 16:44:48 -070032#define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */
Yann Colletd7883a22016-08-12 16:48:02 +020033#include "xxhash.h" /* XXH64_* */
34
35
36/*-************************************
37* Constants
38**************************************/
39#define KB *(1U<<10)
40#define MB *(1U<<20)
41#define GB *(1U<<30)
42
43static const U32 nbTestsDefault = 10000;
44#define COMPRESSIBLE_NOISE_LENGTH (10 MB)
45#define FUZ_COMPRESSIBILITY_DEFAULT 50
46static const U32 prime1 = 2654435761U;
47static const U32 prime2 = 2246822519U;
48
49
Yann Colletd7883a22016-08-12 16:48:02 +020050/*-************************************
51* Display Macros
52**************************************/
53#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
54#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
55static U32 g_displayLevel = 2;
56
57#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletef9999f2016-09-01 16:44:48 -070058 if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
59 { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \
Yann Colletd7883a22016-08-12 16:48:02 +020060 if (g_displayLevel>=4) fflush(stdout); } }
Yann Colletb3060f72016-09-09 16:44:16 +020061static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6;
Yann Colletef9999f2016-09-01 16:44:48 -070062static clock_t g_displayClock = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020063
Yann Colletef9999f2016-09-01 16:44:48 -070064static clock_t g_clockTime = 0;
Yann Colletd7883a22016-08-12 16:48:02 +020065
66
67/*-*******************************************************
68* Fuzzer functions
69*********************************************************/
70#define MAX(a,b) ((a)>(b)?(a):(b))
71
Yann Colletef9999f2016-09-01 16:44:48 -070072static clock_t FUZ_GetClockSpan(clock_t clockStart)
Yann Colletd7883a22016-08-12 16:48:02 +020073{
Yann Colletef9999f2016-09-01 16:44:48 -070074 return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */
Yann Colletd7883a22016-08-12 16:48:02 +020075}
76
77/*! FUZ_rand() :
78 @return : a 27 bits random value, from a 32-bits `seed`.
79 `seed` is also modified */
Yann Collet95162342016-10-25 16:19:52 -070080#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
Yann Colletd7883a22016-08-12 16:48:02 +020081unsigned int FUZ_rand(unsigned int* seedPtr)
82{
83 U32 rand32 = *seedPtr;
84 rand32 *= prime1;
85 rand32 += prime2;
86 rand32 = FUZ_rotl32(rand32, 13);
87 *seedPtr = rand32;
88 return rand32 >> 5;
89}
90
Yann Colletd7883a22016-08-12 16:48:02 +020091/*
92static unsigned FUZ_highbit32(U32 v32)
93{
94 unsigned nbBits = 0;
95 if (v32==0) return 0;
96 for ( ; v32 ; v32>>=1) nbBits++;
97 return nbBits;
98}
99*/
100
101static void* allocFunction(void* opaque, size_t size)
102{
103 void* address = malloc(size);
104 (void)opaque;
105 return address;
106}
107
108static void freeFunction(void* opaque, void* address)
109{
110 (void)opaque;
111 free(address);
112}
113
Yann Colletcb327632016-08-23 00:30:31 +0200114
115/*======================================================
116* Basic Unit tests
117======================================================*/
118
Yann Colletd7883a22016-08-12 16:48:02 +0200119static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem customMem)
120{
Yann Colletb3060f72016-09-09 16:44:16 +0200121 size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
Yann Colletd7883a22016-08-12 16:48:02 +0200122 void* CNBuffer = malloc(CNBufferSize);
123 size_t const skippableFrameSize = 11;
124 size_t const compressedBufferSize = (8 + skippableFrameSize) + ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
125 void* compressedBuffer = malloc(compressedBufferSize);
126 size_t const decodedBufferSize = CNBufferSize;
127 void* decodedBuffer = malloc(decodedBufferSize);
128 size_t cSize;
Yann Colletb3060f72016-09-09 16:44:16 +0200129 int testResult = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200130 U32 testNb=0;
131 ZSTD_CStream* zc = ZSTD_createCStream_advanced(customMem);
132 ZSTD_DStream* zd = ZSTD_createDStream_advanced(customMem);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800133 ZSTD_inBuffer inBuff, inBuff2;
Yann Collet53e17fb2016-08-17 01:39:22 +0200134 ZSTD_outBuffer outBuff;
Yann Colletd7883a22016-08-12 16:48:02 +0200135
136 /* Create compressible test buffer */
137 if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
138 DISPLAY("Not enough memory, aborting\n");
139 goto _output_error;
140 }
141 RDG_genBuffer(CNBuffer, CNBufferSize, compressibility, 0., seed);
142
143 /* generate skippable frame */
144 MEM_writeLE32(compressedBuffer, ZSTD_MAGIC_SKIPPABLE_START);
145 MEM_writeLE32(((char*)compressedBuffer)+4, (U32)skippableFrameSize);
146 cSize = skippableFrameSize + 8;
147
148 /* Basic compression test */
149 DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
150 ZSTD_initCStream_usingDict(zc, CNBuffer, 128 KB, 1);
Yann Collet53e17fb2016-08-17 01:39:22 +0200151 outBuff.dst = (char*)(compressedBuffer)+cSize;
152 outBuff.size = compressedBufferSize;
153 outBuff.pos = 0;
154 inBuff.src = CNBuffer;
155 inBuff.size = CNBufferSize;
156 inBuff.pos = 0;
157 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200158 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200159 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
160 { size_t const r = ZSTD_endStream(zc, &outBuff);
Yann Collet9a021c12016-08-26 09:05:06 +0200161 if (r != 0) goto _output_error; } /* error, or some data not flushed */
Yann Collet53e17fb2016-08-17 01:39:22 +0200162 cSize += outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200163 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
164
Yann Colletcb327632016-08-23 00:30:31 +0200165 DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200166 { size_t const s = ZSTD_sizeof_CStream(zc);
Yann Colletcb327632016-08-23 00:30:31 +0200167 if (ZSTD_isError(s)) goto _output_error;
168 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
169 }
170
Yann Colletd7883a22016-08-12 16:48:02 +0200171 /* skippable frame test */
172 DISPLAYLEVEL(4, "test%3i : decompress skippable frame : ", testNb++);
173 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200174 inBuff.src = compressedBuffer;
175 inBuff.size = cSize;
176 inBuff.pos = 0;
177 outBuff.dst = decodedBuffer;
178 outBuff.size = CNBufferSize;
179 outBuff.pos = 0;
180 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200181 if (r != 0) goto _output_error; }
Yann Collet53e17fb2016-08-17 01:39:22 +0200182 if (outBuff.pos != 0) goto _output_error; /* skippable frame len is 0 */
Yann Colletd7883a22016-08-12 16:48:02 +0200183 DISPLAYLEVEL(4, "OK \n");
184
185 /* Basic decompression test */
Yann Collet9ffbeea2016-12-02 18:37:38 -0800186 inBuff2 = inBuff;
Yann Colletd7883a22016-08-12 16:48:02 +0200187 DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
188 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet17e482e2016-08-23 16:58:10 +0200189 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000000000); /* large limit */
190 if (ZSTD_isError(r)) goto _output_error; }
Yann Collet9ffbeea2016-12-02 18:37:38 -0800191 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff);
192 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
193 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
194 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
195 DISPLAYLEVEL(4, "OK \n");
196
197 /* Re-use without init */
198 DISPLAYLEVEL(4, "test%3i : decompress again without init (re-use previous settings): ", testNb++);
199 outBuff.pos = 0;
200 { size_t const remaining = ZSTD_decompressStream(zd, &outBuff, &inBuff2);
201 if (remaining != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
Yann Collet53e17fb2016-08-17 01:39:22 +0200202 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
203 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200204 DISPLAYLEVEL(4, "OK \n");
205
206 /* check regenerated data is byte exact */
207 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
208 { size_t i;
209 for (i=0; i<CNBufferSize; i++) {
210 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;
211 } }
212 DISPLAYLEVEL(4, "OK \n");
213
Yann Colletcb327632016-08-23 00:30:31 +0200214 DISPLAYLEVEL(4, "test%3i : check DStream size : ", testNb++);
Yann Collet70e3b312016-08-23 01:18:06 +0200215 { size_t const s = ZSTD_sizeof_DStream(zd);
Yann Colletcb327632016-08-23 00:30:31 +0200216 if (ZSTD_isError(s)) goto _output_error;
217 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
218 }
219
Yann Colletd7883a22016-08-12 16:48:02 +0200220 /* Byte-by-byte decompression test */
221 DISPLAYLEVEL(4, "test%3i : decompress byte-by-byte : ", testNb++);
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200222 { /* skippable frame */
223 size_t r = 1;
Yann Colletd7883a22016-08-12 16:48:02 +0200224 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
Yann Collet53e17fb2016-08-17 01:39:22 +0200225 inBuff.src = compressedBuffer;
226 outBuff.dst = decodedBuffer;
227 inBuff.pos = 0;
228 outBuff.pos = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200229 while (r) { /* skippable frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200230 inBuff.size = inBuff.pos + 1;
231 outBuff.size = outBuff.pos + 1;
232 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200233 if (ZSTD_isError(r)) goto _output_error;
234 }
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200235 /* normal frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200236 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
237 r=1;
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200238 while (r) {
Yann Collet53e17fb2016-08-17 01:39:22 +0200239 inBuff.size = inBuff.pos + 1;
240 outBuff.size = outBuff.pos + 1;
241 r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200242 if (ZSTD_isError(r)) goto _output_error;
243 }
244 }
Yann Collet53e17fb2016-08-17 01:39:22 +0200245 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
246 if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
Yann Colletd7883a22016-08-12 16:48:02 +0200247 DISPLAYLEVEL(4, "OK \n");
248
249 /* check regenerated data is byte exact */
250 DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
251 { size_t i;
252 for (i=0; i<CNBufferSize; i++) {
253 if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
254 } }
255 DISPLAYLEVEL(4, "OK \n");
256
Yann Collet12083a42016-09-06 15:01:51 +0200257 /* Complex context re-use scenario */
258 DISPLAYLEVEL(4, "test%3i : context re-use : ", testNb++);
259 ZSTD_freeCStream(zc);
260 zc = ZSTD_createCStream_advanced(customMem);
261 if (zc==NULL) goto _output_error; /* memory allocation issue */
262 /* use 1 */
263 { size_t const inSize = 513;
264 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
265 inBuff.src = CNBuffer;
266 inBuff.size = inSize;
267 inBuff.pos = 0;
268 outBuff.dst = (char*)(compressedBuffer)+cSize;
269 outBuff.size = ZSTD_compressBound(inSize);
270 outBuff.pos = 0;
271 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
272 if (ZSTD_isError(r)) goto _output_error; }
273 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
274 { size_t const r = ZSTD_endStream(zc, &outBuff);
275 if (r != 0) goto _output_error; } /* error, or some data not flushed */
276 }
277 /* use 2 */
278 { size_t const inSize = 1025; /* will not continue, because tables auto-adjust and are therefore different size */
279 ZSTD_initCStream_advanced(zc, NULL, 0, ZSTD_getParams(19, inSize, 0), inSize); /* needs btopt + search3 to trigger hashLog3 */
280 inBuff.src = CNBuffer;
281 inBuff.size = inSize;
282 inBuff.pos = 0;
283 outBuff.dst = (char*)(compressedBuffer)+cSize;
284 outBuff.size = ZSTD_compressBound(inSize);
285 outBuff.pos = 0;
286 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
287 if (ZSTD_isError(r)) goto _output_error; }
288 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
289 { size_t const r = ZSTD_endStream(zc, &outBuff);
290 if (r != 0) goto _output_error; } /* error, or some data not flushed */
291 }
292 DISPLAYLEVEL(4, "OK \n");
293
Yann Collet95162342016-10-25 16:19:52 -0700294 /* CDict scenario */
295 DISPLAYLEVEL(4, "test%3i : digested dictionary : ", testNb++);
296 { ZSTD_CDict* const cdict = ZSTD_createCDict(CNBuffer, 128 KB, 1);
297 size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
298 if (ZSTD_isError(initError)) goto _output_error;
299 cSize = 0;
300 outBuff.dst = compressedBuffer;
301 outBuff.size = compressedBufferSize;
302 outBuff.pos = 0;
303 inBuff.src = CNBuffer;
304 inBuff.size = CNBufferSize;
305 inBuff.pos = 0;
306 { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
307 if (ZSTD_isError(r)) goto _output_error; }
308 if (inBuff.pos != inBuff.size) goto _output_error; /* entire input should be consumed */
309 { size_t const r = ZSTD_endStream(zc, &outBuff);
310 if (r != 0) goto _output_error; } /* error, or some data not flushed */
311 cSize = outBuff.pos;
312 ZSTD_freeCDict(cdict);
313 DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
314 }
315
Yann Collet12083a42016-09-06 15:01:51 +0200316 DISPLAYLEVEL(4, "test%3i : check CStream size : ", testNb++);
317 { size_t const s = ZSTD_sizeof_CStream(zc);
318 if (ZSTD_isError(s)) goto _output_error;
319 DISPLAYLEVEL(4, "OK (%u bytes) \n", (U32)s);
320 }
321
Yann Collet335ad5d2016-10-25 17:47:02 -0700322 /* DDict scenario */
323 DISPLAYLEVEL(4, "test%3i : decompress %u bytes with digested dictionary : ", testNb++, (U32)CNBufferSize);
324 { ZSTD_DDict* const ddict = ZSTD_createDDict(CNBuffer, 128 KB);
325 size_t const initError = ZSTD_initDStream_usingDDict(zd, ddict);
326 if (ZSTD_isError(initError)) goto _output_error;
327 inBuff.src = compressedBuffer;
328 inBuff.size = cSize;
329 inBuff.pos = 0;
330 outBuff.dst = decodedBuffer;
331 outBuff.size = CNBufferSize;
332 outBuff.pos = 0;
333 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
334 if (r != 0) goto _output_error; } /* should reach end of frame == 0; otherwise, some data left, or an error */
335 if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
336 if (inBuff.pos != inBuff.size) goto _output_error; /* should have read the entire frame */
337 ZSTD_freeDDict(ddict);
338 DISPLAYLEVEL(4, "OK \n");
339 }
340
Yann Collet12083a42016-09-06 15:01:51 +0200341 /* test ZSTD_setDStreamParameter() resilience */
Yann Collet17e482e2016-08-23 16:58:10 +0200342 DISPLAYLEVEL(4, "test%3i : wrong parameter for ZSTD_setDStreamParameter(): ", testNb++);
343 { size_t const r = ZSTD_setDStreamParameter(zd, (ZSTD_DStreamParameter_e)999, 1); /* large limit */
344 if (!ZSTD_isError(r)) goto _output_error; }
345 DISPLAYLEVEL(4, "OK \n");
346
347 /* Memory restriction */
348 DISPLAYLEVEL(4, "test%3i : maxWindowSize < frame requirement : ", testNb++);
349 ZSTD_initDStream_usingDict(zd, CNBuffer, 128 KB);
350 { size_t const r = ZSTD_setDStreamParameter(zd, ZSTDdsp_maxWindowSize, 1000); /* too small limit */
351 if (ZSTD_isError(r)) goto _output_error; }
352 inBuff.src = compressedBuffer;
353 inBuff.size = cSize;
354 inBuff.pos = 0;
355 outBuff.dst = decodedBuffer;
356 outBuff.size = CNBufferSize;
357 outBuff.pos = 0;
358 { size_t const r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
359 if (!ZSTD_isError(r)) goto _output_error; /* must fail : frame requires > 100 bytes */
360 DISPLAYLEVEL(4, "OK (%s)\n", ZSTD_getErrorName(r)); }
361
362
Yann Colletd7883a22016-08-12 16:48:02 +0200363_end:
364 ZSTD_freeCStream(zc);
365 ZSTD_freeDStream(zd);
366 free(CNBuffer);
367 free(compressedBuffer);
368 free(decodedBuffer);
369 return testResult;
370
371_output_error:
372 testResult = 1;
373 DISPLAY("Error detected in Unit tests ! \n");
374 goto _end;
375}
376
377
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200378/* ====== Fuzzer tests ====== */
379
Yann Colletd7883a22016-08-12 16:48:02 +0200380static size_t findDiff(const void* buf1, const void* buf2, size_t max)
381{
382 const BYTE* b1 = (const BYTE*)buf1;
383 const BYTE* b2 = (const BYTE*)buf2;
384 size_t u;
385 for (u=0; u<max; u++) {
386 if (b1[u] != b2[u]) break;
387 }
388 return u;
389}
390
391static size_t FUZ_rLogLength(U32* seed, U32 logLength)
392{
393 size_t const lengthMask = ((size_t)1 << logLength) - 1;
394 return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
395}
396
397static size_t FUZ_randomLength(U32* seed, U32 maxLog)
398{
399 U32 const logLength = FUZ_rand(seed) % maxLog;
400 return FUZ_rLogLength(seed, logLength);
401}
402
403#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
404
405#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
406 DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
407
408static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
409{
410 static const U32 maxSrcLog = 24;
411 static const U32 maxSampleLog = 19;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200412 size_t const srcBufferSize = (size_t)1<<maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200413 BYTE* cNoiseBuffer[5];
Yann Collet58d5dfe2016-09-25 01:34:03 +0200414 size_t const copyBufferSize= srcBufferSize + (1<<maxSampleLog);
415 BYTE* const copyBuffer = (BYTE*)malloc (copyBufferSize);
416 size_t const cBufferSize = ZSTD_compressBound(srcBufferSize);
417 BYTE* const cBuffer = (BYTE*)malloc (cBufferSize);
418 size_t const dstBufferSize = srcBufferSize;
419 BYTE* const dstBuffer = (BYTE*)malloc (dstBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200420 U32 result = 0;
421 U32 testNb = 0;
422 U32 coreSeed = seed;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200423 ZSTD_CStream* zc = ZSTD_createCStream(); /* will be reset sometimes */
424 ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */
425 ZSTD_DStream* const zd_noise = ZSTD_createDStream();
426 clock_t const startClock = clock();
427 const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */
Yann Colletcf409a72016-09-26 16:41:05 +0200428 size_t dictSize = 0;
429 U32 oldTestLog = 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200430
431 /* allocations */
Yann Colletd7883a22016-08-12 16:48:02 +0200432 cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
433 cNoiseBuffer[1] = (BYTE*)malloc (srcBufferSize);
434 cNoiseBuffer[2] = (BYTE*)malloc (srcBufferSize);
435 cNoiseBuffer[3] = (BYTE*)malloc (srcBufferSize);
436 cNoiseBuffer[4] = (BYTE*)malloc (srcBufferSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200437 CHECK (!cNoiseBuffer[0] || !cNoiseBuffer[1] || !cNoiseBuffer[2] || !cNoiseBuffer[3] || !cNoiseBuffer[4] ||
Yann Collet58d5dfe2016-09-25 01:34:03 +0200438 !copyBuffer || !dstBuffer || !cBuffer || !zc || !zd || !zd_noise ,
Yann Colletd7883a22016-08-12 16:48:02 +0200439 "Not enough memory, fuzzer tests cancelled");
440
441 /* Create initial samples */
442 RDG_genBuffer(cNoiseBuffer[0], srcBufferSize, 0.00, 0., coreSeed); /* pure noise */
443 RDG_genBuffer(cNoiseBuffer[1], srcBufferSize, 0.05, 0., coreSeed); /* barely compressible */
444 RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
445 RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
446 RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
447 memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200448 ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
Yann Colletd7883a22016-08-12 16:48:02 +0200449
450 /* catch up testNb */
451 for (testNb=1; testNb < startTest; testNb++)
452 FUZ_rand(&coreSeed);
453
454 /* test loop */
Yann Colletef9999f2016-09-01 16:44:48 -0700455 for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200456 U32 lseed;
457 const BYTE* srcBuffer;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200458 size_t totalTestSize, totalGenSize, cSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200459 XXH64_state_t xxhState;
460 U64 crcOrig;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200461 U32 resetAllowed = 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200462 size_t maxTestSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200463
464 /* init */
Yann Collet4c0b44f2016-11-01 11:13:22 -0700465 if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
466 else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
Yann Colletd7883a22016-08-12 16:48:02 +0200467 FUZ_rand(&coreSeed);
468 lseed = coreSeed ^ prime1;
469
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200470 /* states full reset (deliberately not synchronized) */
471 /* some issues can only happen when reusing states */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200472 if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZSTD_freeCStream(zc); zc = ZSTD_createCStream(); resetAllowed=0; }
473 if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZSTD_freeDStream(zd); zd = ZSTD_createDStream(); ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */ }
Yann Colletd7883a22016-08-12 16:48:02 +0200474
475 /* srcBuffer selection [0-4] */
476 { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
477 if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
478 else {
479 buffNb >>= 3;
480 if (buffNb & 7) {
481 const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
482 buffNb = tnb[buffNb >> 3];
483 } else {
484 const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
485 buffNb = tnb[buffNb >> 3];
486 } }
487 srcBuffer = cNoiseBuffer[buffNb];
488 }
489
490 /* compression init */
Yann Colletcf409a72016-09-26 16:41:05 +0200491 if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
492 && oldTestLog /* at least one test happened */ && resetAllowed) {
493 maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
494 if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
495 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
496 size_t const resetError = ZSTD_resetCStream(zc, pledgedSrcSize);
497 CHECK(ZSTD_isError(resetError), "ZSTD_resetCStream error : %s", ZSTD_getErrorName(resetError));
498 }
Yann Collet58d5dfe2016-09-25 01:34:03 +0200499 } else {
500 U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200501 U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
502 maxTestSize = FUZ_rLogLength(&lseed, testLog);
Yann Colletcf409a72016-09-26 16:41:05 +0200503 oldTestLog = testLog;
Yann Colletd7883a22016-08-12 16:48:02 +0200504 /* random dictionary selection */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200505 dictSize = ((FUZ_rand(&lseed)&63)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0;
Yann Colletd7883a22016-08-12 16:48:02 +0200506 { size_t const dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
507 dict = srcBuffer + dictStart;
508 }
Yann Colletcf409a72016-09-26 16:41:05 +0200509 { U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? 0 : maxTestSize;
510 ZSTD_parameters params = ZSTD_getParams(cLevel, pledgedSrcSize, dictSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200511 params.fParams.checksumFlag = FUZ_rand(&lseed) & 1;
512 params.fParams.noDictIDFlag = FUZ_rand(&lseed) & 1;
Yann Colletcf409a72016-09-26 16:41:05 +0200513 { size_t const initError = ZSTD_initCStream_advanced(zc, dict, dictSize, params, pledgedSrcSize);
Yann Colletb3060f72016-09-09 16:44:16 +0200514 CHECK (ZSTD_isError(initError),"ZSTD_initCStream_advanced error : %s", ZSTD_getErrorName(initError));
Yann Colletd7883a22016-08-12 16:48:02 +0200515 } } }
516
517 /* multi-segments compression test */
518 XXH64_reset(&xxhState, 0);
Yann Collet2f263942016-09-26 14:06:08 +0200519 { ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
Yann Collet58d5dfe2016-09-25 01:34:03 +0200520 U32 n;
Yann Collet2f263942016-09-26 14:06:08 +0200521 for (n=0, cSize=0, totalTestSize=0 ; totalTestSize < maxTestSize ; n++) {
Yann Colletd7883a22016-08-12 16:48:02 +0200522 /* compress random chunk into random size dst buffer */
Yann Collet2f263942016-09-26 14:06:08 +0200523 { size_t const randomSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
524 size_t const srcSize = MIN (maxTestSize-totalTestSize, randomSrcSize);
525 size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - srcSize);
Yann Colletd7883a22016-08-12 16:48:02 +0200526 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
527 size_t const dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
Yann Collet2f263942016-09-26 14:06:08 +0200528 ZSTD_inBuffer inBuff = { srcBuffer+srcStart, srcSize, 0 };
Yann Collet53e17fb2016-08-17 01:39:22 +0200529 outBuff.size = outBuff.pos + dstBuffSize;
Yann Colletd7883a22016-08-12 16:48:02 +0200530
Yann Collet53e17fb2016-08-17 01:39:22 +0200531 { size_t const compressionError = ZSTD_compressStream(zc, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200532 CHECK (ZSTD_isError(compressionError), "compression error : %s", ZSTD_getErrorName(compressionError)); }
Yann Colletd7883a22016-08-12 16:48:02 +0200533
Yann Collet53e17fb2016-08-17 01:39:22 +0200534 XXH64_update(&xxhState, srcBuffer+srcStart, inBuff.pos);
535 memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, inBuff.pos);
536 totalTestSize += inBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200537 }
538
539 /* random flush operation, to mess around */
540 if ((FUZ_rand(&lseed) & 15) == 0) {
541 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200542 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
543 outBuff.size = outBuff.pos + adjustedDstSize;
544 { size_t const flushError = ZSTD_flushStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200545 CHECK (ZSTD_isError(flushError), "flush error : %s", ZSTD_getErrorName(flushError));
546 } } }
547
548 /* final frame epilogue */
549 { size_t remainingToFlush = (size_t)(-1);
550 while (remainingToFlush) {
551 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
552 size_t const adjustedDstSize = MIN(cBufferSize - cSize, randomDstSize);
553 U32 const enoughDstSize = (adjustedDstSize >= remainingToFlush);
Yann Collet53e17fb2016-08-17 01:39:22 +0200554 outBuff.size = outBuff.pos + adjustedDstSize;
555 remainingToFlush = ZSTD_endStream(zc, &outBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200556 CHECK (ZSTD_isError(remainingToFlush), "flush error : %s", ZSTD_getErrorName(remainingToFlush));
557 CHECK (enoughDstSize && remainingToFlush, "ZSTD_endStream() not fully flushed (%u remaining), but enough space available", (U32)remainingToFlush);
558 } }
559 crcOrig = XXH64_digest(&xxhState);
Yann Collet53e17fb2016-08-17 01:39:22 +0200560 cSize = outBuff.pos;
Yann Colletd7883a22016-08-12 16:48:02 +0200561 }
562
563 /* multi - fragments decompression test */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200564 if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
Yann Collet95162342016-10-25 16:19:52 -0700565 CHECK (ZSTD_isError(ZSTD_resetDStream(zd)), "ZSTD_resetDStream failed");
Yann Collet9ffbeea2016-12-02 18:37:38 -0800566 } else {
Yann Collet58d5dfe2016-09-25 01:34:03 +0200567 ZSTD_initDStream_usingDict(zd, dict, dictSize);
Yann Collet9ffbeea2016-12-02 18:37:38 -0800568 }
Yann Colletd7883a22016-08-12 16:48:02 +0200569 { size_t decompressionResult = 1;
Yann Collet53e17fb2016-08-17 01:39:22 +0200570 ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
571 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
572 for (totalGenSize = 0 ; decompressionResult ; ) {
Yann Colletd7883a22016-08-12 16:48:02 +0200573 size_t const readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
574 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
575 size_t const dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
Yann Collet53e17fb2016-08-17 01:39:22 +0200576 inBuff.size = inBuff.pos + readCSrcSize;
577 outBuff.size = inBuff.pos + dstBuffSize;
578 decompressionResult = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200579 CHECK (ZSTD_isError(decompressionResult), "decompression error : %s", ZSTD_getErrorName(decompressionResult));
Yann Colletd7883a22016-08-12 16:48:02 +0200580 }
581 CHECK (decompressionResult != 0, "frame not fully decoded");
Yann Collet53e17fb2016-08-17 01:39:22 +0200582 CHECK (outBuff.pos != totalTestSize, "decompressed data : wrong size")
583 CHECK (inBuff.pos != cSize, "compressed data should be fully read")
Yann Colletd7883a22016-08-12 16:48:02 +0200584 { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
585 if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
586 CHECK (crcDest!=crcOrig, "decompressed data corrupted");
587 } }
588
589 /*===== noisy/erroneous src decompression test =====*/
590
591 /* add some noise */
592 { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
593 U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
594 size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
595 size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
596 size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
597 size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
598 memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
599 } }
600
601 /* try decompression on noisy data */
Yann Collet58d5dfe2016-09-25 01:34:03 +0200602 ZSTD_initDStream(zd_noise); /* note : no dictionary */
Yann Collet53e17fb2016-08-17 01:39:22 +0200603 { ZSTD_inBuffer inBuff = { cBuffer, cSize, 0 };
604 ZSTD_outBuffer outBuff= { dstBuffer, dstBufferSize, 0 };
605 while (outBuff.pos < dstBufferSize) {
Yann Colletd7883a22016-08-12 16:48:02 +0200606 size_t const randomCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
607 size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
Yann Collet53e17fb2016-08-17 01:39:22 +0200608 size_t const adjustedDstSize = MIN(dstBufferSize - outBuff.pos, randomDstSize);
609 outBuff.size = outBuff.pos + adjustedDstSize;
610 inBuff.size = inBuff.pos + randomCSrcSize;
611 { size_t const decompressError = ZSTD_decompressStream(zd, &outBuff, &inBuff);
Yann Colletd7883a22016-08-12 16:48:02 +0200612 if (ZSTD_isError(decompressError)) break; /* error correctly detected */
613 } } } }
614 DISPLAY("\r%u fuzzer tests completed \n", testNb);
615
616_cleanup:
617 ZSTD_freeCStream(zc);
618 ZSTD_freeDStream(zd);
Yann Collet58d5dfe2016-09-25 01:34:03 +0200619 ZSTD_freeDStream(zd_noise);
Yann Colletd7883a22016-08-12 16:48:02 +0200620 free(cNoiseBuffer[0]);
621 free(cNoiseBuffer[1]);
622 free(cNoiseBuffer[2]);
623 free(cNoiseBuffer[3]);
624 free(cNoiseBuffer[4]);
625 free(copyBuffer);
626 free(cBuffer);
627 free(dstBuffer);
628 return result;
629
630_output_error:
631 result = 1;
632 goto _cleanup;
633}
634
635
636/*-*******************************************************
637* Command line
638*********************************************************/
639int FUZ_usage(const char* programName)
640{
641 DISPLAY( "Usage :\n");
642 DISPLAY( " %s [args]\n", programName);
643 DISPLAY( "\n");
644 DISPLAY( "Arguments :\n");
645 DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
646 DISPLAY( " -s# : Select seed (default:prompt user)\n");
647 DISPLAY( " -t# : Select starting test number (default:0)\n");
648 DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT);
649 DISPLAY( " -v : verbose\n");
650 DISPLAY( " -p : pause at the end\n");
651 DISPLAY( " -h : display help and exit\n");
652 return 0;
653}
654
655
656int main(int argc, const char** argv)
657{
658 U32 seed=0;
659 int seedset=0;
660 int argNb;
661 int nbTests = nbTestsDefault;
662 int testNb = 0;
663 int proba = FUZ_COMPRESSIBILITY_DEFAULT;
664 int result=0;
665 U32 mainPause = 0;
666 const char* programName = argv[0];
667 ZSTD_customMem customMem = { allocFunction, freeFunction, NULL };
668 ZSTD_customMem customNULL = { NULL, NULL, NULL };
669
670 /* Check command line */
671 for(argNb=1; argNb<argc; argNb++) {
672 const char* argument = argv[argNb];
673 if(!argument) continue; /* Protection if argument empty */
674
675 /* Parsing commands. Aggregated commands are allowed */
676 if (argument[0]=='-') {
677 argument++;
678
679 while (*argument!=0) {
680 switch(*argument)
681 {
682 case 'h':
683 return FUZ_usage(programName);
684 case 'v':
685 argument++;
686 g_displayLevel=4;
687 break;
688 case 'q':
689 argument++;
690 g_displayLevel--;
691 break;
692 case 'p': /* pause at the end */
693 argument++;
694 mainPause = 1;
695 break;
696
697 case 'i':
698 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700699 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200700 while ((*argument>='0') && (*argument<='9')) {
701 nbTests *= 10;
702 nbTests += *argument - '0';
703 argument++;
704 }
705 break;
706
707 case 'T':
708 argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700709 nbTests=0; g_clockTime=0;
Yann Colletd7883a22016-08-12 16:48:02 +0200710 while ((*argument>='0') && (*argument<='9')) {
Yann Colletef9999f2016-09-01 16:44:48 -0700711 g_clockTime *= 10;
712 g_clockTime += *argument - '0';
Yann Colletd7883a22016-08-12 16:48:02 +0200713 argument++;
714 }
Yann Colletef9999f2016-09-01 16:44:48 -0700715 if (*argument=='m') g_clockTime *=60, argument++;
Yann Colletd7883a22016-08-12 16:48:02 +0200716 if (*argument=='n') argument++;
Yann Colletef9999f2016-09-01 16:44:48 -0700717 g_clockTime *= CLOCKS_PER_SEC;
Yann Colletd7883a22016-08-12 16:48:02 +0200718 break;
719
720 case 's':
721 argument++;
722 seed=0;
723 seedset=1;
724 while ((*argument>='0') && (*argument<='9')) {
725 seed *= 10;
726 seed += *argument - '0';
727 argument++;
728 }
729 break;
730
731 case 't':
732 argument++;
733 testNb=0;
734 while ((*argument>='0') && (*argument<='9')) {
735 testNb *= 10;
736 testNb += *argument - '0';
737 argument++;
738 }
739 break;
740
741 case 'P': /* compressibility % */
742 argument++;
743 proba=0;
744 while ((*argument>='0') && (*argument<='9')) {
745 proba *= 10;
746 proba += *argument - '0';
747 argument++;
748 }
749 if (proba<0) proba=0;
750 if (proba>100) proba=100;
751 break;
752
753 default:
754 return FUZ_usage(programName);
755 }
756 } } } /* for(argNb=1; argNb<argc; argNb++) */
757
758 /* Get Seed */
Yann Colletbb855812016-08-25 19:11:11 +0200759 DISPLAY("Starting zstream tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION_STRING);
Yann Colletd7883a22016-08-12 16:48:02 +0200760
Yann Colletef9999f2016-09-01 16:44:48 -0700761 if (!seedset) {
762 time_t const t = time(NULL);
763 U32 const h = XXH32(&t, sizeof(t), 1);
764 seed = h % 10000;
765 }
766
Yann Colletd7883a22016-08-12 16:48:02 +0200767 DISPLAY("Seed = %u\n", seed);
768 if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba);
769
770 if (nbTests<=0) nbTests=1;
771
772 if (testNb==0) {
773 result = basicUnitTests(0, ((double)proba) / 100, customNULL); /* constant seed for predictability */
774 if (!result) {
775 DISPLAYLEVEL(4, "Unit tests using customMem :\n")
776 result = basicUnitTests(0, ((double)proba) / 100, customMem); /* use custom memory allocation functions */
777 } }
778
779 if (!result)
780 result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100);
781
782 if (mainPause) {
783 int unused;
784 DISPLAY("Press Enter \n");
785 unused = getchar();
786 (void)unused;
787 }
788 return result;
789}