2 * We don't use unput, so don't generate code for it.
7 * We don't read from the terminal.
9 %option never-interactive
12 * The language we're scanning is case-insensitive.
17 * We use start condition stacks.
22 * Prefix scanner routines with "WimaxasncpDict" rather than "yy", so this
23 * scanner can coexist with other scanners.
25 %option prefix="WimaxasncpDict"
27 %option outfile="wimaxasncp_dict.c"
32 ** WIMAXASNCP Dictionary Import Routines
36 ** (c) 2007, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
37 ** (c) 2007, Stephen Croll <stephen.d.croll@gmail.com>
39 ** This library is free software; you can redistribute it and/or
40 ** modify it under the terms of the GNU Library General Public
41 ** License as published by the Free Software Foundation; either
42 ** version 2 of the License, or (at your option) any later version.
44 ** This library is distributed in the hope that it will be useful,
45 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
46 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
47 ** Library General Public License for more details.
49 ** You should have received a copy of the GNU Library General Public
50 ** License along with this library; if not, write to the Free Software
51 ** Foundation, Inc., 51 Franklin Street, Fifth Floor,
52 ** Boston, MA 02110-1301, USA.
65 #include <epan/emem.h>
66 #include <epan/value_string.h>
67 #include <epan/packet.h> /* array_length */
68 #include <wsutil/file_util.h>
70 #include "wimaxasncp_dict.h"
71 #include "wimaxasncp_dict_lex.h"
73 typedef struct entity_t {
76 struct entity_t *next;
79 #define ATTR_UINT(cont) do { D(("attr_uint " #cont "\t" )); attr_uint = &(cont); yy_push_state(GET_UINT_ATTR); } while(0)
80 #define ATTR_UINT16(cont) do { D(("attr_uint16 " #cont "\t" )); attr_uint16 = &(cont); yy_push_state(GET_UINT16_ATTR); } while(0)
81 #define ATTR_STR(cont) do { D(("attr_str " #cont "\t" )); attr_str = &(cont); yy_push_state(GET_ATTR); } while(0)
82 #define ATTR_DECODER(cont) do { D(("attr_decoder " #cont "\t" )); attr_uint = &(cont); yy_push_state(GET_DECODER_ATTR); } while(0)
83 #define WIMAXASNCP_IGNORE() do { D(("ignore: %s\t",yytext)); yy_push_state(IGNORE_ATTR); } while(0)
85 #define D(args) wimaxasncp_dict_debug args
87 #define MAX_INCLUDE_DEPTH 10
88 #define YY_INPUT(buf,result,max_size) { result = current_yyinput(buf,max_size); }
90 #define APPEND(txt,len) append_to_buffer(txt,len)
93 static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
94 static int include_stack_ptr = 0;
95 static size_t (*current_yyinput)(gchar*,size_t);
96 static const gchar *sys_dir;
97 static wimaxasncp_dict_t *dict;
98 static wimaxasncp_dict_tlv_t *tlv;
99 static wimaxasncp_dict_enum_t *enumitem;
100 static wimaxasncp_dict_xmlpi_t *xmlpi;
102 static wimaxasncp_dict_tlv_t *last_tlv;
103 static wimaxasncp_dict_enum_t *last_enumitem;
104 static wimaxasncp_dict_xmlpi_t *last_xmlpi;
106 static gchar **attr_str;
107 static guint *attr_uint;
108 static gint16 *attr_uint16;
110 static guint wimaxasncp_bits(guint bits, char *n);
111 static gint wimaxasncp_decode_type(const gchar *name);
112 static void wimaxasncp_dict_debug(const gchar *fmt, ...);
113 static void append_to_buffer(gchar *txt, int len);
114 static FILE *wimaxasncp_dict_open(const gchar*, const gchar*);
116 static GString *dict_error = NULL;
121 xmlpi_start [[:blank:] \r\n]*<\?[[:blank:] \r\n]*
122 xmlpi_end [[:blank:] \r\n]*\?>[[:blank:] \r\n]*
123 xmlpi_key_attr [[:blank:] \r\n]*key[[:blank:] \r\n]*=[[:blank:] \r\n]*\042
124 xmlpi_value_attr [[:blank:] \r\n]*value[[:blank:] \r\n]*=[[:blank:] \r\n]*\042
126 comment_start [[:blank:] \r\n]*<!--[[:blank:] \r\n]*
127 comment_end [[:blank:] \r\n]*-->[[:blank:] \r\n]*
128 open_tag [[:blank:] \r\n]*<[[:blank:] \r\n]*
129 end_tag [[:blank:] \r\n]*\/>[[:blank:] \r\n]*
130 close_tag [[:blank:] \r\n]*>[[:blank:] \r\n]*
131 open_closetag [[:blank:] \r\n]*<\/[[:blank:] \r\n]*
132 equals [[:blank:] \r\n]*=[[:blank:] \r\n]*
133 whitespace [[:blank:] \r\n]*
134 dquoted \042[^\042]*\042
136 doctype [[:blank:] \r\n]*<!DOCTYPE[^\[]*\[[[:blank:] \r\n]*
137 doctype_end [[:blank:] \r\n]*\][[:blank:] \r\n]*>[[:blank:] \r\n]*
139 start_entity [[:blank:] \r\n]*<\!ENTITY[[:blank:] \r\n]*
140 system [[:blank:] \r\n]*SYSTEM[[:blank:] \r\n]*\042
141 entityname [a-z0-9-]+
143 end_entity \042[[:blank:] \r\n]*>[[:blank:] \r\n]*
155 number [-]?[0-9]*|(0x)?[0-9a-fA-F]*
157 dictionary_start <dictionary>
158 dictionary_end <\/dictionary>
166 ignored_attr [a-z0-9-]+=
167 ignored_quoted \042[^\042]*\042
172 typename_attr type-name=\042
173 description_attr description=\042
174 decoder_attr decoder=\042
177 %S LOADING LOADING_COMMENT LOADING_XMLPI ENTITY GET_SYSTEM GET_FILE END_ENTITY
178 %S GET_ATTR GET_UINT_ATTR GET_UINT16_ATTR
179 %S BIT32 BIT16 BIT8 GET_DECODER_ATTR END_ATTR
180 %S OUTSIDE IN_DICT IN_APPL IN_TLV IGNORE_ATTR
181 %S ENUM_ATTRS TLV_ATTRS
182 %S XMLPI_ATTRS XMLPI_GETKEY XMLPI_GETVAL XMLPI_ENDATTR
185 <LOADING>{doctype_end} ;
187 <LOADING>{comment_start} BEGIN LOADING_COMMENT;
189 <LOADING_COMMENT>{comment_end} BEGIN LOADING;
191 <LOADING>{xmlpi_start} BEGIN LOADING_XMLPI;
192 <LOADING_XMLPI>{whitespace} ;
193 <LOADING_XMLPI>{entityname} {
194 xmlpi = g_malloc(sizeof(wimaxasncp_dict_xmlpi_t));
195 xmlpi->name = g_strdup(yytext);
200 if (!dict->xmlpis) last_xmlpi = dict->xmlpis = xmlpi;
201 else last_xmlpi = last_xmlpi->next = xmlpi;
206 <XMLPI_ATTRS>{xmlpi_key_attr} BEGIN XMLPI_GETKEY;
207 <XMLPI_GETKEY>{ndquot} { xmlpi->key = g_strdup(yytext); BEGIN XMLPI_ATTRS; }
209 <XMLPI_ATTRS>{xmlpi_value_attr} BEGIN XMLPI_GETVAL;
210 <XMLPI_GETVAL>{ndquot} { xmlpi->value = g_strdup(yytext); BEGIN XMLPI_ATTRS; }
213 <XMLPI_ATTRS>{xmlpi_end} BEGIN LOADING;
216 <LOADING>{start_entity} BEGIN ENTITY;
217 <ENTITY>{entityname} {
218 entity_t *e = g_malloc(sizeof(entity_t));
219 D(("ENTITY: %s\n",yytext));
220 e->name = g_strdup(yytext);
225 <GET_SYSTEM>{system} BEGIN GET_FILE;
227 D(("GET_FILE: %s\n",yytext));
228 ents.next->file = g_strdup(yytext);
231 <END_ENTITY>{end_entity} BEGIN LOADING;
233 <LOADING>{open_tag} APPEND("<",1);
235 <LOADING>{close_tag} APPEND(">",1);
237 <LOADING>{end_tag} APPEND("/>",2);
239 <LOADING>{open_closetag} APPEND("</",2);
241 <LOADING>{whitespace} APPEND(" ",1);
243 <LOADING>{dquoted} APPEND(yytext,yyleng);
245 <LOADING>{equals} APPEND("=",1);
247 <LOADING>{any} APPEND(yytext,yyleng);
249 <LOADING,IN_DICT>{entity} {
250 gchar *p = ++yytext, *temp_str;
253 while(*p != ';') p++;
257 D(("looking for entity: %s\n",yytext));
259 if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
260 dict_error = g_string_append(
261 dict_error, "included files nested too deeply\n");
265 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
268 for (e = ents.next; e; e = e->next) {
269 if (strcmp(e->name,yytext) == 0) {
270 yyin = wimaxasncp_dict_open(sys_dir,e->file);
271 D(("entity: %s filename: %s yyin: %p\n",e->name,e->file,yyin));
275 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
282 temp_str = g_strdup_printf(
283 "cannot find entity: '%s'\n", e->name);
284 dict_error = g_string_append(dict_error, temp_str);
292 if (!yyin) yyterminate();
295 D(("closing: %p %i\n",yyin,include_stack_ptr));
297 if ( --include_stack_ptr < 0 ) {
298 D(("DONE READING\n"));
302 yy_delete_buffer( YY_CURRENT_BUFFER );
303 yy_switch_to_buffer(include_stack[include_stack_ptr]);
310 *attr_str = g_strdup(yytext);
316 <GET_UINT_ATTR>{number} {
317 *attr_uint = strtoul(yytext,NULL,0);
323 <GET_UINT16_ATTR>{number} {
324 *attr_uint16 = (gint16) strtol(yytext,NULL,0);
330 <GET_UINT_ATTR>"WIMAXASNCP_BIT32"[ \t]*"(" { BEGIN BIT32; }
333 *attr_uint = wimaxasncp_bits(32, yytext);
334 D(("WIMAXASNCP_BIT32(%s)\n",yytext););
338 <GET_UINT_ATTR>"WIMAXASNCP_BIT16"[ \t]*"(" { BEGIN BIT16; }
341 *attr_uint = wimaxasncp_bits(16, yytext);
342 D(("WIMAXASNCP_BIT16(%s)\n",yytext););
346 <GET_UINT_ATTR>"WIMAXASNCP_BIT8"[ \t]*"(" { BEGIN BIT8; }
349 *attr_uint = wimaxasncp_bits(8, yytext);
350 D(("WIMAXASNCP_BIT8(%s)\n",yytext););
354 <BIT32,BIT16,BIT8>[ \t]*")" { BEGIN END_ATTR; }
356 <GET_DECODER_ATTR>{ndquot} {
357 *attr_uint = wimaxasncp_decode_type(yytext);
363 <END_ATTR>{dquot} { yy_pop_state(); }
370 <IGNORE_ATTR>{ignored_quoted} {
371 D(("=>%s<=\n",yytext));
375 <OUTSIDE>{dictionary_start} {
376 D(("dictionary_start\n"));
381 <IN_DICT>{tlv_start} {
384 tlv = g_malloc(sizeof(wimaxasncp_dict_tlv_t));
387 tlv->description = NULL;
394 tlv->hf_protocol = -1;
395 tlv->hf_port_low = -1;
396 tlv->hf_port_high = -1;
397 tlv->hf_ipv4_mask = -1;
398 tlv->hf_ipv6_mask = -1;
399 tlv->hf_vendor_id = -1;
400 tlv->hf_vendor_rest_of_info = -1;
405 if (! dict->tlvs ) last_tlv = dict->tlvs = tlv;
406 else last_tlv = last_tlv->next = tlv;
411 <TLV_ATTRS>{name_attr} { ATTR_STR(tlv->name); }
412 <TLV_ATTRS>{description_attr} { ATTR_STR(tlv->description); }
413 <TLV_ATTRS>{type_attr} { ATTR_UINT16(tlv->type); }
414 <TLV_ATTRS>{decoder_attr} { ATTR_DECODER(tlv->decoder); }
415 <TLV_ATTRS>{stop} { BEGIN IN_TLV; }
416 <TLV_ATTRS>{stop_end} { BEGIN IN_DICT; }
419 <IN_TLV>{enum_start} {
422 enumitem = g_malloc(sizeof(wimaxasncp_dict_enum_t));
423 enumitem->name = NULL;
425 enumitem->next = NULL;
427 if (!tlv->enums) last_enumitem = tlv->enums = enumitem;
428 else last_enumitem = last_enumitem->next = enumitem;
434 <ENUM_ATTRS>{name_attr} { ATTR_STR(enumitem->name); }
435 <ENUM_ATTRS>{code_attr} { ATTR_UINT(enumitem->code); }
437 <ENUM_ATTRS>{stop} { BEGIN IN_TLV; }
438 <ENUM_ATTRS>{stop_end} { BEGIN IN_TLV; }
440 <IN_TLV>{tlv_end} { D(("tlv_end")); BEGIN IN_DICT; }
442 <IN_DICT>{dictionary_end} {
446 <TLV_ATTRS,ENUM_ATTRS>{ignored_attr} WIMAXASNCP_IGNORE();
457 static int debugging = 0;
459 static void wimaxasncp_dict_debug(const gchar *fmt, ...) {
463 if (debugging) vfprintf(stderr, fmt, ap);
469 static guint wimaxasncp_bits(guint bits, char *n)
471 return 1 << ((bits - 1) - (strtoul(n, NULL, 10)));
474 static const value_string wimaxasncp_decode_type_vals[] =
476 { WIMAXASNCP_TLV_TBD, "WIMAXASNCP_TLV_TBD"},
477 { WIMAXASNCP_TLV_COMPOUND, "WIMAXASNCP_TLV_COMPOUND"},
478 { WIMAXASNCP_TLV_BYTES, "WIMAXASNCP_TLV_BYTES"},
479 { WIMAXASNCP_TLV_ENUM8, "WIMAXASNCP_TLV_ENUM8"},
480 { WIMAXASNCP_TLV_ENUM16, "WIMAXASNCP_TLV_ENUM16"},
481 { WIMAXASNCP_TLV_ENUM32, "WIMAXASNCP_TLV_ENUM32"},
482 { WIMAXASNCP_TLV_ETHER, "WIMAXASNCP_TLV_ETHER"},
483 { WIMAXASNCP_TLV_ASCII_STRING, "WIMAXASNCP_TLV_ASCII_STRING"},
484 { WIMAXASNCP_TLV_FLAG0, "WIMAXASNCP_TLV_FLAG0"},
485 { WIMAXASNCP_TLV_BITFLAGS16, "WIMAXASNCP_TLV_BITFLAGS16"},
486 { WIMAXASNCP_TLV_BITFLAGS32, "WIMAXASNCP_TLV_BITFLAGS32"},
487 { WIMAXASNCP_TLV_ID, "WIMAXASNCP_TLV_ID"},
488 { WIMAXASNCP_TLV_HEX8, "WIMAXASNCP_TLV_HEX8"},
489 { WIMAXASNCP_TLV_HEX16, "WIMAXASNCP_TLV_HEX16"},
490 { WIMAXASNCP_TLV_HEX32, "WIMAXASNCP_TLV_HEX32"},
491 { WIMAXASNCP_TLV_DEC8, "WIMAXASNCP_TLV_DEC8"},
492 { WIMAXASNCP_TLV_DEC16, "WIMAXASNCP_TLV_DEC16"},
493 { WIMAXASNCP_TLV_DEC32, "WIMAXASNCP_TLV_DEC32"},
494 { WIMAXASNCP_TLV_IP_ADDRESS, "WIMAXASNCP_TLV_IP_ADDRESS"},
495 { WIMAXASNCP_TLV_IPV4_ADDRESS, "WIMAXASNCP_TLV_IPV4_ADDRESS"},
496 { WIMAXASNCP_TLV_PROTOCOL_LIST, "WIMAXASNCP_TLV_PROTOCOL_LIST"},
497 { WIMAXASNCP_TLV_PORT_RANGE_LIST, "WIMAXASNCP_TLV_PORT_RANGE_LIST"},
498 { WIMAXASNCP_TLV_IP_ADDRESS_MASK_LIST,"WIMAXASNCP_TLV_IP_ADDRESS_MASK_LIST"},
499 { WIMAXASNCP_TLV_EAP, "WIMAXASNCP_TLV_EAP"},
500 { WIMAXASNCP_TLV_VENDOR_SPECIFIC, "WIMAXASNCP_TLV_VENDOR_SPECIFIC"},
504 static gint wimaxasncp_decode_type(const gchar *name)
507 for (i = 0; i < array_length(wimaxasncp_decode_type_vals) - 1; ++i)
509 if (strcmp(name, wimaxasncp_decode_type_vals[i].strptr) == 0)
511 return wimaxasncp_decode_type_vals[i].value;
515 /* not found, emit some sort of error here? */
517 return WIMAXASNCP_TLV_TBD;
520 static gchar *strbuf = NULL;
521 static gchar *write_ptr = NULL;
522 static gchar *read_ptr = NULL;
524 static guint size_strbuf = 8192;
525 static guint len_strbuf = 0;
527 extern void wimaxasncp_dict_unused(void);
528 void wimaxasncp_dict_unused(void) {
532 static void append_to_buffer(gchar *txt, int len) {
534 if (strbuf == NULL) {
535 read_ptr = write_ptr = strbuf = g_malloc(size_strbuf);
538 if ( (len_strbuf + len + 1) >= size_strbuf ) {
539 read_ptr = strbuf = g_realloc(strbuf,size_strbuf *= 2);
542 write_ptr = strbuf + len_strbuf;
543 strncpy(write_ptr,txt,len);
545 strbuf[len_strbuf] = '\0';
548 static size_t file_input(gchar *buf, size_t max) {
551 read = fread(buf,1,max,yyin);
555 } else if (read > 0) {
563 static size_t string_input(gchar *buf, size_t max) {
564 if (read_ptr >= write_ptr ) {
566 } else if ( read_ptr + max > write_ptr ) {
567 max = write_ptr - read_ptr;
570 memcpy(buf,read_ptr,max);
576 static FILE *wimaxasncp_dict_open(
577 const gchar *system_directory,
578 const gchar *filename)
582 if (system_directory)
584 int len = strlen(system_directory) + strlen(filename)
585 + strlen(G_DIR_SEPARATOR_S) + 1;
586 fname = g_malloc(len);
587 g_snprintf(fname, len, "%s%s%s",
588 system_directory, G_DIR_SEPARATOR_S,filename);
592 fname = g_strdup(filename);
595 fh = ws_fopen(fname,"r");
597 D(("fname: %s fh: %p\n",fname,fh));
605 wimaxasncp_dict_t *wimaxasncp_dict_scan(
606 const gchar *system_directory, const gchar *filename, int dbg,
611 dict_error = g_string_new("");
615 sys_dir = system_directory;
622 wimaxasncp_dict_free(dict);
625 dict = g_malloc(sizeof(wimaxasncp_dict_t));
634 last_enumitem = NULL;
639 yyin = wimaxasncp_dict_open(sys_dir,filename);
643 current_yyinput = file_input;
647 D(("\n---------------\n%s\n------- %d -------\n",
648 strbuf, len_strbuf));
650 current_yyinput = string_input;
659 entity_t *next = e->next;
670 if (dict_error->len > 0)
672 *error = dict_error->str;
673 g_string_free(dict_error, FALSE);
678 g_string_free(dict_error, TRUE);
684 void wimaxasncp_dict_free(wimaxasncp_dict_t *d) {
685 wimaxasncp_dict_tlv_t *t, *tn;
687 #define FREE_NAMEANDOBJ(n) do { if(n->name) g_free(n->name); g_free(n); } while(0)
689 for (t = d->tlvs; t; t = tn) {
690 wimaxasncp_dict_enum_t *e, *en;
693 for (e = t->enums; e; e = en) {
698 if (!t->description) g_free(t->description);
705 void wimaxasncp_dict_print(FILE *fh, wimaxasncp_dict_t *d) {
706 wimaxasncp_dict_tlv_t *tlv;
710 for (tlv = d->tlvs; tlv; tlv = tlv->next) {
711 wimaxasncp_dict_enum_t *e;
712 fprintf(fh,"TLV: %s[%u] %s[%d] %s\n",
713 tlv->name ? tlv->name : "-",
715 val_to_str(tlv->decoder,
716 wimaxasncp_decode_type_vals,
719 tlv->description ? tlv->description : "");
721 for (e = tlv->enums; e; e = e->next) {
722 fprintf(fh,"\tEnum: %s[%u]\n",
723 e->name ? e->name : "-",
730 * We want to stop processing when we get to the end of the input.
731 * (%option noyywrap is not used because if used then
732 * some flex versions (eg: 2.5.35) generate code which causes
733 * warnings by the Windows VC compiler).
740 #ifdef TEST_WIMAXASNCP_DICT_STANDALONE
741 int main(int argc, char **argv) {
742 wimaxasncp_dict_t *d;
754 fprintf(stderr,"%s: usage [dictionary_dir] dictionary_filename\n",argv[0]);
758 d = wimaxasncp_dict_scan(dname,fname,1);
762 printf("wimaxasncp - %s", dict_error);
766 wimaxasncp_dict_print(stdout, d);