-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__helpers.c
172 lines (152 loc) · 4.13 KB
/
__helpers.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
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
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include "helpers.h"
#if defined(RANDOM_PREGEN) && RANDOM_PREGEN > 0
static unsigned int *__random_data = NULL;
static unsigned int __random_idx = -1;
void generate_random(void) __attribute__((constructor));
void
generate_random(void)
{
unsigned int count = RANDOM_PREGEN;
generate_array(&__random_data, &count, "/dev/urandom");
}
unsigned int
get_random(void)
{
return __random_data[++__random_idx%RANDOM_PREGEN];
}
#else
unsigned int
get_random(void)
{
int v;
unsigned int r;
const char *path = "/dev/urandom";
if ((v = open(path, O_RDONLY)) == -1) {
fprintf(stderr, "Error opening to %s: %s", path, strerror(errno));
return -1;
}
if (read(v, &r, sizeof(unsigned int)) < sizeof(unsigned int)) {
close(v);
fprintf(stderr, "Error reading %d bytes from %s: %s", sizeof(unsigned int), path, strerror(errno));
return -1;
}
close(v);
return r%10000;
return generate_random();
}
#endif
int
generate_array(unsigned int **array,
int *count,
const char *path)
{
int v;
if ((v = open(path, O_RDONLY)) == -1) {
fprintf(stderr, "Error opening to %s: %s", path, strerror(errno));
return -1;
}
if (*count == 0) {
struct stat s;
if (fstat(v, &s) == -1) {
fprintf(stderr, "Error getting information about %s: %s", path, strerror(errno));
return -1;
}
*count = s.st_size/sizeof(unsigned int);
}
if (!(*array = malloc(*count*sizeof(unsigned int)))) {
fprintf(stderr, "Error allocating %d bytes: %s", *count*sizeof(unsigned int), strerror(errno));
close(v);
return -1;
}
if (read(v, *array, *count*sizeof(unsigned int)) < *count*sizeof(unsigned int)) {
fprintf(stderr, "Error reading %d bytes from %s: %s", *count*sizeof(unsigned int), path, strerror(errno));
close(v);
return -1;
}
close(v);
for (v=0;v<*count;++v)
(*array)[v] %= 10000;
return 0;
}
void
print_array(unsigned int *array,
int count)
{
int i;
printf("Array of %d elements\n", count);
for (i=0;i<count;++i) {
if (i!=0 && !(i%16))
printf("\n");
printf("%4u ", array[i]);
}
printf("\n");
}
int
generate_key_value(struct key_value **data,
int *count,
const char *path)
{
int v;
if ((v = open(path, O_RDONLY)) == -1) {
fprintf(stderr, "Error opening to %s: %s", path, strerror(errno));
return -1;
}
if (*count == 0) {
struct stat s;
if (fstat(v, &s) == -1) {
fprintf(stderr, "Error getting information about %s: %s", path, strerror(errno));
close(v);
return -1;
}
*count = s.st_size/sizeof(struct key_value);
}
if (!(*data = malloc(*count*sizeof(struct key_value)))) {
fprintf(stderr, "Error allocating %d bytes: %s", *count*sizeof(struct key_value), strerror(errno));
close(v);
return -1;
}
if (read(v, *data, *count*sizeof(struct key_value)) < *count*sizeof(struct key_value)) {
fprintf(stderr, "Error reading %d bytes from %s: %s", *count*sizeof(struct key_value), path, strerror(errno));
close(v);
return -1;
}
close(v);
for (v=0;v<*count;++v) {
unsigned char *d;
d = (*data)[v].key;
d[KEY_LEN-1] = '\0';
while (*d) {
if (*d >= 0x80)
*d -= 0x80;
if (*d < 0x20)
*d += 0x20;
else if (*d > 0x7e)
*d -= 0x7e - 0x20;
++d;
}
d = (*data)[v].value;
d[VALUE_LEN-1] = '\0';
while (*d) {
if (*d >= 0x80)
*d -= 0x80;
if (*d < 0x20)
*d += 0x20;
else if (*d > 0x7e)
*d -= 0x7e - 0x20;
++d;
}
}
return 0;
}
void
print_key_value(struct key_value *data,
int count)
{
printf("data of %d elements\n", count);
}