-
Notifications
You must be signed in to change notification settings - Fork 153
/
filters_worker.inc.h
194 lines (143 loc) · 3.43 KB
/
filters_worker.inc.h
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
#ifdef BINFILTER
struct bfiltervec filters;
#endif // BINFILTER
#ifdef INTFILTER
struct ifiltervec filters;
# ifdef OMITMASK
IFT ifiltermask;
# endif
#endif // INTFILTER
#ifdef PCRE2FILTER
struct pfiltervec filters;
#endif // PCRE2FILTER
void filters_init(void)
{
VEC_INIT(filters);
}
#include "filters_common.inc.h"
#ifdef INTFILTER
# ifndef BINSEARCH
#define MATCHFILTER(it,pk) \
((*(IFT *)(pk) & VEC_BUF(filters,it).m) == VEC_BUF(filters,it).f)
#define DOFILTER(it,pk,code) \
do { \
for (it = 0;it < VEC_LENGTH(filters);++it) { \
if (unlikely(MATCHFILTER(it,pk))) { \
code; \
break; \
} \
} \
} while (0)
# else // BINSEARCH
# ifdef OMITMASK
#define DOFILTER(it,pk,code) \
do { \
register IFT maskedpk = *(IFT *)(pk) & ifiltermask; \
for (size_t down = 0,up = VEC_LENGTH(filters);down < up;) { \
it = (up + down) / 2; \
if (maskedpk < VEC_BUF(filters,it).f) \
up = it; \
else if (maskedpk > VEC_BUF(filters,it).f) \
down = it + 1; \
else { \
code; \
break; \
} \
} \
} while (0)
# else // OMITMASK
#define DOFILTER(it,pk,code) \
do { \
for (size_t down = 0,up = VEC_LENGTH(filters);down < up;) { \
it = (up + down) / 2; \
IFT maskedpk = *(IFT *)(pk) & VEC_BUF(filters,it).m; \
register int cmp = memcmp(&maskedpk,&VEC_BUF(filters,it).f,sizeof(IFT)); \
if (cmp < 0) \
up = it; \
else if (cmp > 0) \
down = it + 1; \
else { \
code; \
break; \
} \
} \
} while (0)
# endif // OMITMASK
# endif // BINSEARCH
#define PREFILTER
#define POSTFILTER
#endif // INTFILTER
#ifdef BINFILTER
# ifndef BINSEARCH
#define MATCHFILTER(it,pk) ( \
memcmp(pk,VEC_BUF(filters,it).f,VEC_BUF(filters,it).len) == 0 && \
(pk[VEC_BUF(filters,it).len] & VEC_BUF(filters,it).mask) == VEC_BUF(filters,it).f[VEC_BUF(filters,it).len])
#define DOFILTER(it,pk,code) \
do { \
for (it = 0;it < VEC_LENGTH(filters);++it) { \
if (unlikely(MATCHFILTER(it,pk))) { \
code; \
break; \
} \
} \
} while (0)
# else // BINSEARCH
#define DOFILTER(it,pk,code) \
do { \
for (size_t down = 0,up = VEC_LENGTH(filters);down < up;) { \
it = (up + down) / 2; \
{ \
register int filterdiff = memcmp(pk,VEC_BUF(filters,it).f,VEC_BUF(filters,it).len); \
if (filterdiff < 0) { \
up = it; \
continue; \
} \
if (filterdiff > 0) { \
down = it + 1; \
continue; \
} \
} \
if ((pk[VEC_BUF(filters,it).len] & VEC_BUF(filters,it).mask) < \
VEC_BUF(filters,it).f[VEC_BUF(filters,it).len]) \
{ \
up = it; \
continue; \
} \
if ((pk[VEC_BUF(filters,it).len] & VEC_BUF(filters,it).mask) > \
VEC_BUF(filters,it).f[VEC_BUF(filters,it).len]) \
{ \
down = it + 1; \
continue; \
} \
{ \
code; \
break; \
} \
} \
} while (0)
# endif // BINSEARCH
#define PREFILTER
#define POSTFILTER
#endif // BINFILTER
#ifdef PCRE2FILTER
#define PREFILTER \
char pkconvbuf[BASE32_TO_LEN(PUBLIC_LEN) + 1]; \
pcre2_match_data *pcre2md = pcre2_match_data_create(128,0); \
PCRE2_SIZE *pcre2ovector = 0;
#define POSTFILTER \
pcre2_match_data_free(pcre2md);
#define DOFILTER(it,pk,code) \
do { \
base32_to(pkconvbuf,pk,PUBLIC_LEN); \
size_t __l = VEC_LENGTH(filters); \
for (it = 0;it < __l;++it) { \
int rc = pcre2_match(VEC_BUF(filters,it).re,(PCRE2_SPTR8)pkconvbuf,BASE32_TO_LEN(PUBLIC_LEN),0, \
PCRE2_NO_UTF_CHECK,pcre2md,0); \
if (unlikely(rc >= 0)) { \
pcre2ovector = pcre2_get_ovector_pointer(pcre2md); \
code; \
break; \
} \
} \
} while (0)
#endif // PCRE2FILTER