File: Fuzzer.c

package info (click to toggle)
unar 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,664 kB
  • sloc: ansic: 52,939; objc: 39,563; cpp: 4,074; makefile: 99; perl: 10
file content (196 lines) | stat: -rw-r--r-- 4,316 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdio.h>

#include "VariantG.h"
#include "VariantH.h"
#include "VariantI.h"
#include "SubAllocatorVariantG.h"
#include "SubAllocatorVariantH.h"
#include "SubAllocatorVariantI.h"
#include "SubAllocatorBrimstone.h"

static int FuzzerFunction1(void *context);
static int FuzzerFunction2(void *context);
static int FuzzerFunction3(void *context);
static int FuzzerFunction4(void *context);
static int FuzzerFunction5(void *context);

static void SeedRandom(uint32_t seed);
static uint32_t Random();

int main(int argc,const char **argv)
{
	if(argc!=3&&argc!=4&&argc!=5)
	{
		fprintf(stderr,"Usage: %s variant mode [seed [length]]\n",argv[0]);
		fprintf(stderr,"       \"variant\" is g, h, i, b or 7.\n");
		fprintf(stderr,"       \"mode\" is 1-5.\n");
		fprintf(stderr,"       \"seed\" is the random seed to use (defaults to 0).\n");
		fprintf(stderr,"       \"length\" is the number of bytes to read (defaults to infinite).\n");
		return 1;
	}

	PPMdReadFunction *readfunc;
	switch(argv[2][0])
	{
		case '1': readfunc=FuzzerFunction1; break;
		case '2': readfunc=FuzzerFunction2; break;
		case '3': readfunc=FuzzerFunction3; break;
		case '4': readfunc=FuzzerFunction4; break;
		case '5': readfunc=FuzzerFunction5; break;
		default:
			fprintf(stderr,"Unknown mode.\n");
			return 1;
	}

	uint32_t seed=0;
	if(argc>3) seed=atoi(argv[3]);
	SeedRandom(seed);

	int length=0;
	if(argc>4) length=atoi(argv[4]);

	int lastbyte=0;
	int numbytes=0;

	switch(argv[1][0])
	{
		case 'g':
		case 'G':
		{
			PPMdSubAllocatorVariantG *alloc=CreateSubAllocatorVariantG(1024*1024);
			PPMdModelVariantG model;
			StartPPMdModelVariantG(&model,readfunc,NULL,&alloc->core,16,false);

			for(;;)
			{
				lastbyte=NextPPMdVariantGByte(&model);
				if(lastbyte<0) break;
				numbytes++;
				if(length && numbytes==length) break;
			}
		}
		break;

		case 'b':
		case 'B':
		{
			PPMdSubAllocatorBrimstone *alloc=CreateSubAllocatorBrimstone(1024*1024);
			PPMdModelVariantG model;
			StartPPMdModelVariantG(&model,readfunc,NULL,&alloc->core,16,true);

			for(;;)
			{
				lastbyte=NextPPMdVariantGByte(&model);
				if(lastbyte<0) break;
				numbytes++;
				if(length && numbytes==length) break;
			}
		}
		break;

		case 'h':
		case 'H':
		{
			PPMdSubAllocatorVariantH *alloc=CreateSubAllocatorVariantH(1024*1024);
			PPMdModelVariantH model;
			StartPPMdModelVariantH(&model,readfunc,NULL,alloc,16,false);

			for(;;)
			{
				lastbyte=NextPPMdVariantHByte(&model);
				if(lastbyte<0) break;
				numbytes++;
				if(length && numbytes==length) break;
			}
		}
		break;

		case '7':
		{
			PPMdSubAllocatorVariantH *alloc=CreateSubAllocatorVariantH(1024*1024);
			PPMdModelVariantH model;
			StartPPMdModelVariantH(&model,readfunc,NULL,alloc,16,true);

			for(;;)
			{
				lastbyte=NextPPMdVariantHByte(&model);
				if(lastbyte<0) break;
				numbytes++;
				if(length && numbytes==length) break;
			}
		}
		break;

		case 'i':
		case 'I':
		{
			PPMdSubAllocatorVariantI *alloc=CreateSubAllocatorVariantI(1024*1024);
			PPMdModelVariantI model;
			StartPPMdModelVariantI(&model,readfunc,NULL,alloc,16,0);

			for(;;)
			{
				lastbyte=NextPPMdVariantIByte(&model);
				if(lastbyte<0) break;
				numbytes++;
				if(length && numbytes==length) break;
			}
		}
		break;

		default:
			fprintf(stderr,"Unknown variant.\n");
			return 1;
	}

	if(lastbyte==-1) fprintf(stderr,"End of stream after %d bytes.\n",numbytes);
	else if(lastbyte==-2) fprintf(stderr,"Error after %d bytes.\n",numbytes);
	else fprintf(stderr,"Stopped after %d bytes.\n",numbytes);

	return 0;
}

static int FuzzerFunction1(void *context)
{
	return Random()&0xff;
}

static int FuzzerFunction2(void *context)
{
	return Random()&Random()&Random()&0xff;
}

static int FuzzerFunction3(void *context)
{
	return (Random()|Random()|Random())&0xff;
}

static int FuzzerFunction4(void *context)
{
	return 0x00;
}

static int FuzzerFunction5(void *context)
{
	return 0xff;
}




static uint32_t s1=0xc7ff5f16,s2=0x0dc556ae,s3=0x78010089;

static void SeedRandom(uint32_t seed)
{
	s1=seed*1664525+1013904223|0x10;
	s2=seed*1103515245+12345|0x1000;
	s3=seed*214013+2531011|0x100000;
}

static uint32_t Random()
{
	s1=((s1&0xfffffffe)<<12)^(((s1<<13)^s1)>>19);
	s2=((s2&0xfffffff8)<<4)^(((s2<<2)^s2)>>25);
	s3=((s3&0xfffffff0)<<17)^(((s3<<3)^s3)>>11);
	return s1^s2^s3;
}