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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
%top {
#include "grammar.h"
}
%{
//#include "manual.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#define read_block(tmp) \
({ \
unsigned int __depth; \
bool __is_string; \
char *__iter; \
\
__depth = 1; \
__is_string = false; \
\
for (__iter = temp; __depth > 0; __iter += (__depth > 0 ? 1 : 0)) \
{ \
*__iter = input(); \
\
switch (*__iter) \
{ \
case '"': \
__is_string = !__is_string; \
break; \
\
case '{': \
if (!__is_string) __depth++; \
break; \
\
case '}': \
if (!__is_string) \
{ \
__depth--; \
if (__depth == 0) unput('}'); \
} \
break; \
\
} \
\
} \
\
*__iter = '\0'; \
\
})
#define PUSH_STATE(s) yy_push_state(s, yyscanner)
#define POP_STATE yy_pop_state(yyscanner)
#define EXTEND_BUFFER_IF_NEEDED(extra) \
if ((*used + extra) > *allocated) \
{ \
*allocated *= 2; \
*buf = realloc(*buf, *allocated); \
}
%}
%option bison-bridge reentrant
%option stack
%option nounput
%option noinput
%option noyywrap
%option noyy_top_state
%option yylineno
%option never-interactive
%x rule_intro
%x raw_block
%x strings
%x strval
%x strval_raw
%x strval_hex
%x condition
%x strlit
%x wait_for_colon
%x comment
%%
"rule" { PUSH_STATE(rule_intro); return RAW_RULE; }
<rule_intro>[A-Za-z0-9_]+ { yylval->cstring = yytext; return RULE_NAME; }
<rule_intro>[ \t]* { }
<rule_intro>"{" { POP_STATE; PUSH_STATE(raw_block); return BRACE_IN; }
<raw_block>"strings" { PUSH_STATE(strings); PUSH_STATE(wait_for_colon); return STRINGS; }
<raw_block,strings>"condition" { PUSH_STATE(condition); PUSH_STATE(wait_for_colon); return CONDITION; }
<condition>"true" { return TRUE_; }
<condition>"false" { return FALSE_; }
<condition>(0|[1-9][0-9]*) { yylval->integer = strtoull(yytext, NULL, 10); return INTEGER; }
<condition>0x[0-9a-f]+ { yylval->integer = strtoull(yytext, NULL, 16); return INTEGER; }
<condition>[kK][bB] { return KB; }
<condition>[mM][bB] { return MB; }
<condition>[gG][bB] { return GB; }
<condition>"\"" {
*used = 0;
PUSH_STATE(strlit);
}
<strlit>"\"" {
POP_STATE;
EXTEND_BUFFER_IF_NEEDED(1);
(*buf)[(*used)++] = '\0';
yylval->cstring = *buf;
return STRING;
}
<strlit>"\\\"" { EXTEND_BUFFER_IF_NEEDED(1); (*buf)[(*used)++] = '"'; }
<strlit>"\\t" { EXTEND_BUFFER_IF_NEEDED(1); (*buf)[(*used)++] = '\t'; }
<strlit>"\\r" { EXTEND_BUFFER_IF_NEEDED(1); (*buf)[(*used)++] = '\r'; }
<strlit>"\\n" { EXTEND_BUFFER_IF_NEEDED(1); (*buf)[(*used)++] = '\n'; }
<strlit>"\\\\" { EXTEND_BUFFER_IF_NEEDED(1); (*buf)[(*used)++] = '\\'; }
<strlit>\\x[0-9a-fA-F]{2} {
char __ch;
__ch = strtol(yytext + 2, NULL, 16);
EXTEND_BUFFER_IF_NEEDED(1);
(*buf)[(*used)++] = __ch;
}
<strlit>[^\\\"]+ {
size_t __len;
__len = strlen(yytext);
EXTEND_BUFFER_IF_NEEDED(__len);
strcpy(&(*buf)[*used], yytext);
*used += __len;
}
<condition>"and" { return AND; }
<condition>"or" { return OR; }
<condition>"not" { return NOT; }
<condition>"<" { return LT; }
<condition>"<=" { return LE; }
<condition>"==" { return EQ; }
<condition>"!=" { return NE; }
<condition>">" { return GT; }
<condition>">=" { return GE; }
<condition>"contains" { return CONTAINS; }
<condition>"startswith" { return STARTSWITH; }
<condition>"endswith" { return ENDSWITH; }
<condition>"matches" { return MATCHES; }
<condition>"icontains" { return ICONTAINS; }
<condition>"istartswith" { return ISTARTSWITH; }
<condition>"iendswith" { return IENDSWITH; }
<condition>"iequals" { return IEQUALS; }
<condition>"+" { return PLUS; }
<condition>"-" { return MINUS; }
<condition>"*" { return MUL; }
<condition>"\\" { return DIV; }
<condition>"%" { return MOD; }
<condition>"(" { return PAREN_O; }
<condition>")" { return PAREN_C; }
<condition>"," { return COMMA; }
<condition>"." { return DOT; }
<condition>"none" { return NONE; }
<condition>"any" { return ANY; }
<condition>"all" { return ALL; }
<condition>"of" { return OF; }
<condition>"them" { return THEM; }
<strings,condition>$[A-Za-z0-9_]* {
yylval->sized_cstring.cstring = yytext + 1;
yylval->sized_cstring.len = yyleng - 1;
return IDENTIFIER;
}
<condition>[A-Za-z_][A-Za-z0-9_]* {
yylval->sized_cstring.cstring = yytext;
yylval->sized_cstring.len = yyleng;
return NAME;
}
<strings>"=" { PUSH_STATE(strval); return ASSIGN; }
<strval>\"[^\"\\]+\" {
POP_STATE;
yylval->sized_cstring.cstring = yytext + 1;
yylval->sized_cstring.len = yyleng - 2;
return PLAIN_STRING;
}
<strval>"\"" {
POP_STATE;
// *built_pattern = g_bytes_pattern_new();
PUSH_STATE(strval_raw);
}
<strval>"{" {
POP_STATE;
// *built_pattern = g_bytes_pattern_new();
PUSH_STATE(strval_hex);
}
<strval_raw>"\"" { POP_STATE; /*yylval->pattern = *built_pattern*/; return MASKED_STRING; }
<strval_raw>"\\\"" { }//g_bytes_pattern_append_data(*built_pattern, '"', 0xff); }
<strval_raw>"\\t" { }//g_bytes_pattern_append_data(*built_pattern, '\t', 0xff); }
<strval_raw>"\\r" { }//g_bytes_pattern_append_data(*built_pattern, '\r', 0xff); }
<strval_raw>"\\n" { }//g_bytes_pattern_append_data(*built_pattern, '\n', 0xff); }
<strval_raw>"\\\\" { }//g_bytes_pattern_append_data(*built_pattern, '\\', 0xff); }
<strval_raw>\\x[0-9a-fA-F]{2} {
uint8_t __ch;
__ch = strtol(yytext + 2, NULL, 16);
//g_bytes_pattern_append_data(*built_pattern, __ch, 0xff);
}
<strval_raw>. { }//g_bytes_pattern_append_data(*built_pattern, *yytext, 0xff); }
<strval_hex>"}" { POP_STATE; /*yylval->pattern = *built_pattern;*/ return MASKED_STRING; }
<strval_hex>[0-9a-fA-F]{2} {
uint8_t __ch;
__ch = strtol(yytext, NULL, 16);
//g_bytes_pattern_append_data(*built_pattern, __ch, 0xff);
}
<strval_hex>"??" { /*g_bytes_pattern_insert_space(*built_pattern, 1, 1);*/ }
<wait_for_colon>":" { POP_STATE; return COLON; }
<raw_block,strings,condition>"}" { POP_STATE; return BRACE_OUT; }
%{ /* Commentaires */ %}
<*>"/*" { PUSH_STATE(comment); }
<comment>"*/" { POP_STATE; }
<comment>(.|\n) { }
<*>"//"[^\n]* { }
%{ /* Actions par défaut */ %}
<*>[ \t\n]+ { }
<*>. {
char *msg;
int ret;
ret = asprintf(&msg, "Unhandled token in rule definition: '%s'", yytext);
if (ret == -1)
YY_FATAL_ERROR("Unhandled token in undisclosed rule definition");
else
{
YY_FATAL_ERROR(msg);
free(msg);
}
}
%%
|