29 #define _CRT_SECURE_NO_WARNINGS // fgets
37 #include <sys/timeb.h>
44 #define NB_ATTEMPTS (1<<18)
47 #define NUM_SEQ (1 << SEQ_POW)
48 #define SEQ_MSK ((NUM_SEQ) - 1)
49 #define MOD_SEQ(x) ((((x) >> 8) & 255) == 0)
50 #define NEW_SEQ(x) ((((x) >> 10) %10) == 0)
51 #define PAGE_SIZE 4096
52 #define ROUND_PAGE(x) (((x) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
53 #define PRIME1 2654435761U
54 #define PRIME2 2246822519U
55 #define PRIME3 3266489917U
67 nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000);
75 nSpan += 0x100000 * 1000;
89 for (i = 0; i < 2048; i++)
90 if (buf[i] != buf[i + 2048])
101 printf(
"Starting security tests...");
102 input = (
char*) malloc (20<<20);
103 output = (
char*) malloc (20<<20);
107 for(i = 3; i < 16840000; i++)
113 printf(
" Completed (r=%i)\n",r);
120 unsigned long long bytes = 0;
121 unsigned long long cbytes = 0;
122 unsigned char buf[
LEN];
123 unsigned char testOut[
LEN+1];
124 # define FUZ_max LZ4_COMPRESSBOUND(LEN)
125 # define FUZ_avail ROUND_PAGE(FUZ_max)
129 int i, j, k, ret, len;
130 char userInput[30] = {0};
132 printf(
"starting LZ4 fuzzer\n");
133 printf(
"Select an Initialisation number (default : random) : ");
135 if ( fgets(userInput,
sizeof userInput, stdin) )
137 if ( sscanf(userInput,
"%d", &seed) == 1 ) {}
140 printf(
"Seed = %u\n", seed);
144 for (i = 0; i < 2048; i++)
149 printf(
"\r%7i /%7i\r", i, NB_ATTEMPTS);
152 for (j = 0; j <
NUM_SEQ; j++) {
154 seeds[j] ^= (
FUZ_rand(&seed) >> 8) & 65535;
156 for (j = 0; j <
LEN; j++) {
163 seeds[k] ^= (
FUZ_rand(&seed) >> 8) & 65535;
170 if (ret == 0) { printf(
"compression failed despite sufficient space: seed %u, len %d\n", seed, LEN);
goto _output_error; }
174 ret =
LZ4_uncompress((
char*)&cbuf[off_full], (
char*)testOut, LEN);
175 if (ret<0) { printf(
"decompression failed despite correct space: seed %u, len %d\n", seed, LEN);
goto _output_error; }
178 ret =
LZ4_uncompress((
char*)&cbuf[off_full], (
char*)testOut, LEN-1);
179 if (ret>=0) { printf(
"decompression should have failed, due to Output Size being too small : seed %u, len %d\n", seed, LEN);
goto _output_error; }
182 ret =
LZ4_uncompress((
char*)&cbuf[off_full], (
char*)testOut, LEN+1);
183 if (ret>=0) { printf(
"decompression should have failed, due to Output Size being too large : seed %u, len %d\n", seed, LEN);
goto _output_error; }
187 if (ret<0) { printf(
"decompression failed despite sufficient space: seed %u, len %d\n", seed, LEN);
goto _output_error; }
191 if (ret<0) { printf(
"decompression failed despite sufficient space: seed %u, len %d\n", seed, LEN);
goto _output_error; }
195 if (ret>=0) { printf(
"decompression should have failed, due to Output Size being too small : seed %u, len %d\n", seed, LEN);
goto _output_error; }
199 if (ret>=0) { printf(
"decompression should have failed, due to input size being too small : seed %u, len %d\n", seed, LEN);
goto _output_error; }
203 if (ret>=0) { printf(
"decompression should have failed, due to input size being too large : seed %u, len %d\n", seed, LEN);
goto _output_error; }
207 if (!
test_canary(&cbuf[
FUZ_avail])) { printf(
"compression overran output buffer: seed %u, len %d, olen %d\n", seed, LEN, len);
goto _output_error; }
208 if (ret == 0) { printf(
"compression failed despite sufficient space: seed %u, len %d\n", seed, LEN);
goto _output_error; }
212 if (ret) { printf(
"compression overran output buffer: seed %u, len %d, olen %d => ret %d", seed, LEN, len-1, ret);
goto _output_error; }
213 if (!
test_canary(&cbuf[FUZ_avail])) { printf(
"compression overran output buffer: seed %u, len %d, olen %d", seed, LEN, len-1);
goto _output_error; }
219 printf(
"all tests completed successfully \n");
220 printf(
"compression ratio: %0.3f%%\n", (
double)cbytes/bytes*100);