summaryrefslogtreecommitdiff
path: root/tools/yara2rost/tokens.l
blob: 34e61d07ecae6ddb0c3dfe691ff569f2d3909a95 (plain)
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

%top {

#include "grammar.h"

}


%{

#include "decl.h"

#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>


#define PUSH_STATE(s) yy_push_state(s, yyscanner)
#define POP_STATE     yy_pop_state(yyscanner)

%}


%option bison-bridge reentrant
%option stack
%option nounput
%option noinput
%option noyywrap
%option noyy_top_state
%option yylineno
%option never-interactive


%x regexp
%x comment


str_not_escaped [^\"\\]
str_escaped \\a|\\b|\\t|\\n|\\v|\\f|\\r|\\e|\\\"|\\\\|\\x{hbyte}
str_mixed ({str_not_escaped}|{str_escaped})

hbyte [0-9a-fA-F]{2}

digit         [0-9]
letter        [a-zA-Z]
hexdigit      [a-fA-F0-9]
octdigit      [0-7]


%%


":"                     { return COLON; }
"{"                     { return CURLY_BRACKET_O; }
"}"                     { return CURLY_BRACKET_C; }
"="                     { return EQUAL; }
"("                     { return PAREN_O; }
")"                     { return PAREN_C; }
".."                    { return DOT_DOT; }
","                     { return COMMA; }
"["                     { return BRACKET_O; }
"]"                     { return BRACKET_C; }
"%"                     { return PERCENT; }
"."                     { return DOT; }

"+"                     { return ADD_OP; }
"-"                     { return SUB_OP; }
"*"                     { return MUL_OP; }
"\\"                    { return DIV_OP; }
"^"                     { return EOR_OP; }
"&"                     { return AND_OP; }
"|"                     { return OR_OP; }
"~"                     { return INV_OP; }
"<<"                    { return SHIFT_LEFT_OP; }
">>"                    { return SHIFT_RIGHT_OP; }

"<"                     { return LT; }
">"                     { return GT; }
"<="                    { return LE; }
">="                    { return GE; }
"=="                    { return EQ; }
"!="                    { return NEQ; }

"all"                   { return ALL; }
"and"                   { return AND; }
"any"                   { return ANY; }
"ascii"                 { return ASCII; }
"at"                    { return AT; }
"base64"                { return BASE64; }
"base64wide"            { return BASE64WIDE; }
"condition"             { return CONDITION; }
"contains"              { return CONTAINS; }
"defined"               { return DEFINED; }
"endswith"              { return ENDSWITH; }
"entrypoint"            { return ENTRYPOINT; }
"filesize"              { return FILESIZE; }
"for"                   { return FOR; }
"fullword"              { return FULLWORD; }
"global"                { return GLOBAL; }
"icontains"             { return ICONTAINS; }
"iendswith"             { return IENDSWITH; }
"iequals"               { return IEQUALS; }
"import"                { return IMPORT; }
"in"                    { return IN; }
"include"               { return INCLUDE; }
"istartswith"           { return ISTARTSWITH; }
"matches"               { return MATCHES; }
"meta"                  { return META; }
"nocase"                { return NOCASE; }
"none"                  { return NONE; }
"not"                   { return NOT; }
"of"                    { return OF; }
"or"                    { return OR; }
"private"               { return PRIVATE; }
"rule"                  { return RULE; }
"startswith"            { return STARTSWITH; }
"strings"               { return STRINGS; }
"them"                  { return THEM; }
"wide"                  { return WIDE; }
"xor"                   { return XOR; }

"false"                 { return _FALSE; }
"true"                  { return _TRUE; }


%{ /* Commentaires */ %}

"/*"                    { PUSH_STATE(comment); }
<comment>"*/"           { POP_STATE; }
<comment>(.|\n)         { }

"//"[^\n]*              { }


%{ /* Blocs de texte */ %}

$({letter}|{digit}|_)*"*" {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return STRING_IDENTIFIER_WITH_WILDCARD;

}

$({letter}|{digit}|_)* {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return STRING_IDENTIFIER;

}

#({letter}|{digit}|_)* {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return STRING_COUNT;

}

@({letter}|{digit}|_)* {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return STRING_OFFSET;

}

!({letter}|{digit}|_)* {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return STRING_LENGTH;

}

u?int(8|16|32)(be)? {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return INTEGER_FUNCTION;

}

({letter}|_)({letter}|{digit}|_)* {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return IDENTIFIER;

}

{digit}+(MB|KB){0,1} {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return NUMBER;

}

{digit}+"."{digit}+ {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return DOUBLE;

}

0x{hexdigit}+ {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return NUMBER;

}

0o{octdigit}+ {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return NUMBER;

}

\"{str_mixed}*\" {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return TEXT_STRING;

}

"/" {

    PUSH_STATE(regexp);

}

<regexp>(\\\/|\\.|[^/\n\\])+\/i?s? {

    POP_STATE;

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return REGEXP;

}

\{(({hexdigit}|[ \-|\~\?\[\]\(\)\n\r\t]|\/\*(\/|\**[^*/])*\*+\/)+|\/\/.*\n)+\} {

    yylval->cstring.data = yytext;
    yylval->cstring.len = yyleng;

    return HEX_STRING;

}


%{ /* Actions par défaut */ %}

<*>[ \t\r]+             { }

<*>[\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);
                            }
                        }


%%