-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
78 lines (69 loc) · 1.48 KB
/
utils.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "utils.h"
void parse_arguements(int argc, char **argv, char **inputfname, char **fuzzfname)
{
int option;
option = getopt(argc, argv, ":i:f:");
while(option != -1)
{
switch(option)
{
case 'i':
*inputfname = strdup(optarg);
break;
case 'f':
*fuzzfname = strdup(optarg);
break;
/*
default:
fprintf(stderr, "Unknown option %c\n", optopt);
exit(1);
*/
}
option = getopt(argc, argv, ":i:f:");
}
if (*inputfname == NULL)
{
fputs("Please specify an inputfile", stderr);
exit(1);
}
if (*fuzzfname == NULL)
{
fputs("Please specify a file to fuzz", stderr);
exit(1);
}
}
size_t readFile(char *fname, char **inpBuf)
{
size_t size;
FILE* inputfile = fopen(fname, "r");
fseek(inputfile, 0, SEEK_END);
size = ftell(inputfile);
rewind(inputfile);
*inpBuf = (char *)calloc(size, 1);
fread(*inpBuf, 1, size, inputfile);
fclose(inputfile);
return size;
}
size_t writeFile(char *fname, char *buf, size_t size)
{
FILE* outFile = fopen(fname, "w");
size_t sz = fwrite(buf, 1, size, outFile);
fclose(outFile);
return sz;
}
void p8(char *buf, size_t idx, size_t val)
{
*(unsigned char *)(buf + idx)= (unsigned char)(val & 0xff);
}
void p16(char *buf, size_t idx, size_t val)
{
*(unsigned short *)(buf + idx) = (unsigned short)(val & 0xffff);
}
void p32(char *buf, size_t idx, size_t val)
{
*(unsigned int *)(buf + idx) = (unsigned int)(val & 0xffffffff);
}