trafgen: parser: add possibility for negative numbers
[netsniff-ng.git] / trafgen_lexer.l
blobbe6917fcf94ff79d00a0d3de73998814812fbcb2
1 /*
2  * netsniff-ng - the packet sniffing beast
3  * By Daniel Borkmann <daniel@netsniff-ng.org>
4  * Copyright 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5  * Swiss federal institute of technology (ETH Zurich)
6  * Subject to the GPL, version 2.
7  */
9 /* lex-func-prefix: yy */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
18 #include "trafgen_parser.tab.h"
19 #include "xmalloc.h"
21 extern void yyerror(const char *);
23 static char *try_convert_shellcode(char *sstr)
25         int j = 0;
26         char *bstr, *ostr = sstr, *hay;
27         size_t blen, slen = strlen(sstr), tot = 0;
28         const char *needle = "\\x";
30         sstr++;
31         slen -= 2;
33         if (slen % 4 != 0)
34                 return sstr;
36         blen = slen / 4;
37         hay = sstr;
38         while ((hay = strstr(hay, needle)) != NULL ) {
39                 hay += strlen(needle) + 2;
40                 tot++;
41         }
43         if (blen != tot) {
44                 printf("Warning: mixed shellcode with strings, "
45                        "using strings!\n");
46                 return sstr;
47         }
49         blen += 2;
50         bstr = xzmalloc(blen);
52         bstr[j++] = '\"';
53         while (j < blen - 1)
54                 bstr[j++] = (uint8_t) strtoul(sstr + 2, &sstr, 16);
55         bstr[j++] = '\"';
57         xfree(ostr);
58         return bstr;
63 %option align
64 %option nounput
65 %option noyywrap
66 %option noreject
67 %option 8bit
68 %option caseless
69 %option noinput
70 %option nodefault
72 number_oct      ([0][0-9]+)
73 number_hex      ([0][x][a-fA-F0-9]+)
74 number_bin      ([0][b][0-1]+)
75 number_dec      (([0])|([1-9][0-9]*))
76 number_ascii    ([a-zA-Z])
80 "cpu"           { return K_CPU; }
81 "fill"          { return K_FILL; }
82 "rnd"           { return K_RND; }
83 "csumip"        { return K_CSUMIP; }
84 "csumip4"       { return K_CSUMIP; }
85 "csumicmp"      { return K_CSUMIP; }
86 "csumicmp4"     { return K_CSUMIP; }
87 "csumudp"       { return K_CSUMUDP; }
88 "csumtcp"       { return K_CSUMTCP; }
89 "drnd"          { return K_DRND; }
90 "dinc"          { return K_DINC; }
91 "ddec"          { return K_DDEC; }
92 "seqinc"        { return K_SEQINC; }
93 "seqdec"        { return K_SEQDEC; }
94 "const8"|"c8"   { return K_CONST8; }
95 "const16"|"c16" { return K_CONST16; }
96 "const32"|"c32" { return K_CONST32; }
97 "const64"|"c64" { return K_CONST64; }
99 [ ]*"-"[ ]*     { return '-'; }
100 [ ]*"+"[ ]*     { return '+'; }
101 [ ]*"*"[ ]*     { return '*'; }
102 [ ]*"/"[ ]*     { return '/'; }
103 [ ]*"%"[ ]*     { return '%'; }
104 [ ]*"&"[ ]*     { return '&'; }
105 [ ]*"|"[ ]*     { return '|'; }
106 [ ]*"<"[ ]*     { return '<'; }
107 [ ]*">"[ ]*     { return '>'; }
108 [ ]*"^"[ ]*     { return '^'; }
109 "{"             { return '{'; }
110 "}"             { return '}'; }
111 "("             { return '('; }
112 ")"             { return ')'; }
113 "["             { return '['; }
114 "]"             { return ']'; }
115 ","             { return ','; }
116 ":"             { return ':'; }
118 "\n"            { yylineno++; }
120 "\""[^\"]+"\""  { yylval.str = try_convert_shellcode(xstrdup(yytext));
121                   return string; }
123 ([ \t\n]+)?     { return K_WHITE; }
125 "/*"([^\*]|\*[^/])*"*/" { return K_COMMENT; }
127 "#"[^\n]*       { return K_COMMENT; }
129 {number_hex}    { yylval.number = strtoul(yytext, NULL, 16);
130                   return number; }
132 {number_dec}    { yylval.number = strtol(yytext, NULL, 10);
133                   return number; }
135 {number_oct}    { yylval.number = strtol(yytext + 1, NULL, 8);
136                   return number; }
138 {number_bin}    { yylval.number = strtol(yytext + 2, NULL, 2);
139                   return number; }
141 {number_ascii}  { yylval.number = (uint8_t) (*yytext);
142                   return number; }
144 "'\\x"[a-fA-F0-9]{2}"'" { yylval.number = strtol(yytext + 3, NULL, 16);
145                   return number; }
147 "'"."'"         { yylval.number = (uint8_t) (*(yytext + 1));
148                   return number; }
150 ";"[^\n]*       {/* NOP */}
151 .               { printf("Unknown character '%s'", yytext);
152                   yyerror("lex Unknown character"); }