-
Notifications
You must be signed in to change notification settings - Fork 7
/
nope_list.c
164 lines (132 loc) · 2.57 KB
/
nope_list.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
/*
* The code is distributed under terms of the BSD license.
* Copyright (c) 2016 Alex Pankratov. All rights reserved.
*
* http://swapped.cc/bsd-license
*/
#include "nope_list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
static
void * zalloc(size_t bytes)
{
void * ptr = calloc(1, bytes);
if (! ptr)
printf("calloc(%u) failed\n", bytes);
return ptr;
}
nope_list * load_nope_list(const char * filename, size_t max_size)
{
nope_list * nl = NULL;
struct stat st;
int fd = -1;
int r;
byte_range temp;
size_t i;
if (stat(filename, &st) < 0)
{
printf("stat(%s) failed with %d\n", filename, errno);
goto err;
}
if (st.st_size > max_size)
{
printf("%s is too big\n", filename);
goto err;
}
fd = open(filename, O_RDONLY);
if (fd < 0)
{
printf("open(%s) failed with %d\n", filename, errno);
goto err;
}
nl = zalloc(sizeof(*nl));
if (! nl)
goto err;
nl->raw.ptr = zalloc(st.st_size);
if (! nl->raw.ptr)
goto err;
r = read(fd, (void*)nl->raw.ptr, st.st_size);
if (r != st.st_size)
{
printf("read(%s) returned %d, wanted %d\n", filename, r, st.st_size);
goto err;
}
nl->raw.end = nl->raw.ptr + st.st_size;
close(fd);
fd = -1;
/*
*
*/
for (temp = nl->raw; temp.ptr < temp.end; )
{
byte_range line;
br_get_line(&temp, &line);
if (line.ptr == line.end || line.ptr[0] == '#')
continue;
br_trim(&line);
if (line.ptr == line.end)
continue;
nl->size++;
}
if (! nl->size)
{
printf("%s has no usable entries\n", filename);
goto err;
}
nl->items = zalloc(sizeof(*nl->items) * nl->size);
if (! nl->items)
goto err;
for (i = 0, temp = nl->raw; temp.ptr < temp.end; )
{
byte_range line;
br_get_line(&temp, &line);
if (line.ptr == line.end || line.ptr[0] == '#')
continue;
br_trim(&line);
if (line.ptr == line.end)
continue;
nl->items[i++] = line;
}
return nl;
err:
free_nope_list(nl);
if (fd != -1)
close(fd);
return NULL;
}
void free_nope_list(nope_list * nl)
{
if (! nl)
return;
free(nl->raw.ptr);
free(nl->items);
free(nl);
}
byte_range * match_nope_list(nope_list * nl, const char * _what)
{
byte_range what;
size_t i;
what.ptr = (uint8_t*)_what;
what.end = (uint8_t*)_what + strlen(_what);
for (i=0; i<nl->size; i++)
{
byte_range * rule = nl->items+i;
if (br_front(rule) == '~')
{
byte_range meat = { rule->ptr+1, rule->end };
if (br_compare(&what, &meat) == 0)
return rule;
}
else
{
if (br_search(&what, nl->items+i))
return rule;
}
}
return NULL;
}