Fuzz: Fix the capinfos check (again).
[metze/wireshark/wip.git] / epan / uat_load.l
1 %top {
2 /* Include this before everything else, for various large-file definitions */
3 #include "config.h"
4 }
5
6 /*
7  * We want a reentrant scanner.
8  */
9 %option reentrant
10
11 /*
12  * We don't use input, so don't generate code for it.
13  */
14 %option noinput
15
16 /*
17  * We don't use unput, so don't generate code for it.
18  */
19 %option nounput
20
21 /*
22  * We don't read interactively from the terminal.
23  */
24 %option never-interactive
25
26 /*
27  * We want to stop processing when we get to the end of the input.
28  */
29 %option noyywrap
30
31 /*
32  * The type for the state we keep for a scanner.
33  */
34 %option extra-type="uat_load_scanner_state_t *"
35
36 /*
37  * We have to override the memory allocators so that we don't get
38  * "unused argument" warnings from the yyscanner argument (which
39  * we don't use, as we have a global memory allocator).
40  *
41  * We provide, as macros, our own versions of the routines generated by Flex,
42  * which just call malloc()/realloc()/free() (as the Flex versions do),
43  * discarding the extra argument.
44  */
45 %option noyyalloc
46 %option noyyrealloc
47 %option noyyfree
48
49 /*
50  * Prefix scanner routines with "uat_load_" rather than "yy", so this scanner
51  * can coexist with other scanners.
52  */
53 %option prefix="uat_load_"
54
55 %{
56         /*
57          * uat_load.l
58          *
59          *  User Accessible Tables
60          *  Maintain an array of user accessible data strucures
61          *  One parser to fit them all
62          *
63          * (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
64          *
65          * Wireshark - Network traffic analyzer
66          * By Gerald Combs <gerald@wireshark.org>
67          * Copyright 2001 Gerald Combs
68          *
69          * This program is free software; you can redistribute it and/or
70          * modify it under the terms of the GNU General Public License
71          * as published by the Free Software Foundation; either version 2
72          * of the License, or (at your option) any later version.
73          *
74          * This program is distributed in the hope that it will be useful,
75          * but WITHOUT ANY WARRANTY; without even the implied warranty of
76          * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
77          * GNU General Public License for more details.
78          *
79          * You should have received a copy of the GNU General Public License
80          * along with this program; if not, write to the Free Software
81          * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
82          */
83 #include <stdlib.h>
84 #include <stdio.h>
85 #include <string.h>
86 #include <errno.h>
87
88 #include <glib.h>
89
90 #include "uat-int.h"
91 #include <wsutil/file_util.h>
92
93 #ifdef _WIN32
94 /* disable Windows VC compiler warning "signed/unsigned mismatch" associated  */
95 /* with YY_INPUT code generated by flex versions such as 2.5.35.              */
96 #pragma warning (disable:4018)
97 #endif
98
99 typedef struct {
100         uat_t* uat;
101         gchar *parse_str;
102
103         gchar* error;
104         gboolean valid_record;
105         guint colnum;
106         gchar* ptrx;
107         guint len;
108         void* record;
109         guint linenum;
110         size_t parse_str_pos;
111 } uat_load_scanner_state_t;
112
113 /*
114  * Signal a fatal error and stops parsing.
115  * Since the record is internal to the parsing process, its contents must also
116  * be cleared before terminating. Any values that are not handled yet (ptrx)
117  * must also be freed.
118  */
119 #define ERROR(fmtd) do { \
120         char* fmt_str = g_strdup_printf fmtd; \
121         g_free(yyextra->error); \
122         yyextra->error = g_strdup_printf("%s:%d: %s",yyextra->uat->filename,yyextra->linenum,fmt_str); \
123         g_free(fmt_str); \
124         if (yyextra->uat->free_cb) { \
125                 yyextra->uat->free_cb(yyextra->record); \
126         } \
127         g_free(yyextra->ptrx); \
128         yyterminate(); \
129 } while(0)
130
131 /*
132  * Sets the field of the current (scanner-internal) record, using the parsed
133  * value. If the field validation function exists and returns an error, then
134  * the record is marked as invalid and an error message is stored such that it
135  * can be shown after parsing. (If other errors occur after this issue, then
136  * this message will be overwritten though.)
137  */
138 #define SET_FIELD() \
139         { gchar* errx; \
140         if (yyextra->uat->fields[yyextra->colnum].cb.chk) { \
141                 if ( ! yyextra->uat->fields[yyextra->colnum].cb.chk(yyextra->record, yyextra->ptrx, yyextra->len, yyextra->uat->fields[yyextra->colnum].cbdata.chk, yyextra->uat->fields[yyextra->colnum].fld_data, &errx) ) { \
142                         g_free(yyextra->error); \
143                         yyextra->error = g_strdup_printf("%s:%d: %s",yyextra->uat->filename,yyextra->linenum,errx); \
144                         g_free(errx); \
145                         yyextra->valid_record = FALSE; \
146                 }\
147         }\
148         yyextra->uat->fields[yyextra->colnum].cb.set(yyextra->record, yyextra->ptrx, yyextra->len, yyextra->uat->fields[yyextra->colnum].cbdata.chk, yyextra->uat->fields[yyextra->colnum].fld_data);\
149         g_free(yyextra->ptrx);\
150         yyextra->ptrx = NULL;\
151         yyextra->colnum++; \
152         } while(0)
153
154 #ifdef DEBUG_UAT_LOAD
155 #define DUMP_FIELD(str) \
156                 { guint i; printf("%s: %s='",str,yyextra->uat->fields[yyextra->colnum].name); for(i=0;i<yyextra->len;i++) if (yyextra->uat->fields[yyextra->colnum].mode == PT_TXTMOD_HEXBYTES) { printf("%.2x ",((guint8*)yyextra->ptrx)[i]); } else putc(yyextra->ptrx[i],stdout); printf("'[%d]\n",yyextra->len); }
157
158 #define DUMP(str) printf("%s\n",str)
159 #else
160 #define DUMP_FIELD(s)
161 #define DUMP(s)
162 #endif
163
164 /* Modified version of YY_INPUT generated by Flex 2.91 */
165 #define YY_INPUT(buf,result,max_size) \
166         if ( yyextra->parse_str ) \
167                 { \
168                 size_t n = 0; \
169                 size_t pslen = strlen(yyextra->parse_str); \
170                 if (yyextra->parse_str_pos < pslen) \
171                         { \
172                         n = pslen - yyextra->parse_str_pos; \
173                         if (n > max_size) n = max_size; \
174                         memcpy(buf, yyextra->parse_str + yyextra->parse_str_pos, n); \
175                         yyextra->parse_str_pos += n; \
176                         } \
177                 result = n; \
178                 } \
179         else \
180                 { \
181                 errno=0; \
182                 while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
183                         { \
184                         if( errno != EINTR) \
185                                 { \
186                                 YY_FATAL_ERROR( "input in flex scanner failed" ); \
187                                 break; \
188                                 } \
189                         errno=0; \
190                         clearerr(yyin); \
191                         } \
192                 }
193
194                 /*
195                  * XXX
196                  * quoted_string below fails badly on "...\\"
197                  * workarround in uat_save(), using /x5c and /x22
198                  */
199
200 #define YY_USER_INIT BEGIN START_OF_LINE;
201
202 /*
203  * Sleazy hack to suppress compiler warnings in yy_fatal_error().
204  */
205 #define YY_EXIT_FAILURE ((void)yyscanner, 2)
206
207 /*
208  * Macros for the allocators, to discard the extra argument.
209  */
210 #define uat_load_alloc(size, yyscanner)         (void *)malloc(size)
211 #define uat_load_realloc(ptr, size, yyscanner)  (void *)realloc((char *)(ptr), (size))
212 #define uat_load_free(ptr, yyscanner)           free((char *)ptr)
213
214 %}
215
216 quoted_string \042([^\042]|\134\042)*\042
217 binstring ([0-9a-zA-Z][0-9a-zA-Z])*
218 separator [ \t]*,
219 newline [ \t]*[\r]?\n
220 ws [ \t]+
221 comment #[^\n]*\n
222
223 %x START_OF_LINE NEXT_FIELD SEPARATOR END_OF_RECORD ERRORED
224 %%
225 <START_OF_LINE,NEXT_FIELD>{ws} ;
226 <START_OF_LINE>{newline} yyextra->linenum++;
227 <START_OF_LINE>{comment} yyextra->linenum++;
228
229 <START_OF_LINE,NEXT_FIELD>{separator} {
230         yyextra->ptrx = g_strdup("");
231         yyextra->len = 0;
232
233         DUMP_FIELD("empty->next");
234
235         SET_FIELD();
236
237         if ( yyextra->colnum >= yyextra->uat->ncols ) {
238                 ERROR(("more fields than required"));
239         }
240
241         BEGIN NEXT_FIELD;
242 }
243
244 <START_OF_LINE,NEXT_FIELD>{newline}   {
245         yyextra->ptrx = g_strdup("");
246         yyextra->len = 0;
247
248         BEGIN END_OF_RECORD;
249
250         yyless((int) yyleng);
251 }
252
253 <START_OF_LINE,NEXT_FIELD>{quoted_string} {
254         yyextra->ptrx = uat_undquote(yytext, (guint) yyleng, &yyextra->len);
255
256
257         if (yyextra->colnum < yyextra->uat->ncols - 1) {
258                 DUMP("quoted_str->s");
259                 BEGIN SEPARATOR;
260         } else {
261                 DUMP("quoted_str->eor");
262                 BEGIN END_OF_RECORD;
263         }
264 }
265
266 <START_OF_LINE,NEXT_FIELD>{binstring} {
267         yyextra->ptrx = uat_unbinstring(yytext,  (guint) yyleng, &yyextra->len);
268
269         if (!yyextra->ptrx) {
270                 ERROR(("uneven hexstring for field %s",yyextra->uat->fields[yyextra->colnum].name));
271         }
272
273         if ( yyextra->colnum < yyextra->uat->ncols - 1 ) {
274                 DUMP("binstring->s");
275                 BEGIN SEPARATOR;
276         } else {
277                 DUMP("binstring->eor");
278                 BEGIN END_OF_RECORD;
279         }
280 }
281
282 <SEPARATOR>{separator} {
283
284         DUMP_FIELD("separator->next");
285
286         SET_FIELD();
287
288         if ( yyextra->colnum >= yyextra->uat->ncols ) {
289                 ERROR(("more fields than required"));
290         }
291
292         BEGIN NEXT_FIELD;
293 }
294
295 <SEPARATOR>{newline} {
296         yyextra->linenum++;
297         ERROR(("expecting field %s in previous line",yyextra->uat->fields[yyextra->colnum].name));
298 }
299
300 <SEPARATOR>. {
301         ERROR(("unexpected char '%s' while looking for field %s",yytext,yyextra->uat->fields[yyextra->colnum].name));
302 }
303
304 <END_OF_RECORD>{separator} {
305         ERROR(("more fields than required"));
306 }
307
308 <END_OF_RECORD>{newline} {
309         void* rec;
310         char* err = NULL;
311
312         yyextra->linenum++;
313
314         DUMP_FIELD("newline->start");
315
316         SET_FIELD();
317
318         /* Last field was processed, try to store the full record in the UAT. */
319         rec = uat_add_record(yyextra->uat, yyextra->record, yyextra->valid_record);
320
321         if ((yyextra->uat->update_cb) && (rec != NULL)) {
322                 if (!yyextra->uat->update_cb(rec,&err)) {
323                         g_free(yyextra->error);
324                         yyextra->error = err;
325                         yyterminate();
326                 }
327         }
328
329         /* The record was duplicated to the UAT above, now free our fields. */
330         if (yyextra->uat->free_cb) {
331                 yyextra->uat->free_cb(yyextra->record);
332         }
333         memset(yyextra->record, 0, yyextra->uat->record_size);
334
335         yyextra->valid_record = TRUE;
336         yyextra->colnum = 0;
337         yyextra->ptrx = NULL;
338         yyextra->len = 0;
339
340         BEGIN START_OF_LINE;
341  }
342
343 <END_OF_RECORD>. {
344         ERROR(("unexpected char while looking for end of line"));
345 }
346
347 <ERRORED>{newline} { yyextra->linenum++; BEGIN START_OF_LINE; }
348 <ERRORED>. ;
349
350 {newline} { yyextra->linenum++; ERROR(("incomplete record")); }
351 . { ERROR(("unexpected input")); }
352
353 %%
354
355
356
357
358 gboolean
359 uat_load(uat_t *uat, char **errx)
360 {
361         gchar *fname = uat_get_actual_filename(uat, FALSE);
362         FILE *in;
363         yyscan_t scanner;
364         uat_load_scanner_state_t state;
365
366         if (!fname) {
367                 UAT_UPDATE(uat);
368
369                 if (uat->post_update_cb)
370                         uat->post_update_cb();
371
372                 return TRUE;
373         }
374
375
376         if (!(in = ws_fopen(fname,"r"))) {
377                 *errx = g_strdup(g_strerror(errno));
378                 g_free(fname);
379                 return FALSE;
380         }
381
382         if (uat_load_lex_init(&scanner) != 0) {
383                 *errx = g_strdup(g_strerror(errno));
384                 fclose(in);
385                 g_free(fname);
386                 return FALSE;
387         }
388
389         uat_load_set_in(in, scanner);
390
391         state.uat = uat;
392         state.parse_str = NULL; /* we're reading from a file */
393
394         state.error = NULL;
395         state.valid_record = TRUE;
396         state.colnum = 0;
397         state.ptrx = NULL;
398         state.len = 0;
399         state.record = g_malloc0(uat->record_size);
400         state.linenum = 1;
401         state.parse_str_pos = 0;
402
403         DUMP(fname);
404         g_free(fname);  /* we're done with the file name now */
405
406         /* Associate the state with the scanner */
407         uat_load_set_extra(&state, scanner);
408
409         uat_load_lex(scanner);
410
411         uat_load_lex_destroy(scanner);
412         g_free(state.record);
413         fclose(in);
414
415         uat->changed = FALSE;
416         uat->loaded = TRUE;
417         UAT_UPDATE(uat);
418
419         if (state.error) {
420                 *errx = state.error;
421                 return FALSE;
422         }
423
424         if (uat->post_update_cb)
425                 uat->post_update_cb();
426
427         *errx = NULL;
428         return TRUE;
429 }
430
431 gboolean
432 uat_load_str(uat_t *uat, char *entry, char **err)
433 {
434         yyscan_t scanner;
435         uat_load_scanner_state_t state;
436
437         state.uat = uat;
438         state.parse_str = g_strdup_printf("%s\n", entry); /* Records must end with a newline */
439
440         state.error = NULL;
441         state.valid_record = TRUE;
442         state.colnum = 0;
443         state.ptrx = NULL;
444         state.len = 0;
445         state.record = g_malloc0(uat->record_size);
446         state.linenum = 1;
447         state.parse_str_pos = 0;
448
449         if (uat_load_lex_init(&scanner) != 0) {
450                 *err = g_strdup(g_strerror(errno));
451                 g_free(state.parse_str);
452                 g_free(state.record);
453                 return FALSE;
454         }
455
456         DUMP(entry);
457
458         /* Associate the state with the scanner */
459         uat_load_set_extra(&state, scanner);
460
461         uat_load_lex(scanner);
462
463         uat_load_lex_destroy(scanner);
464         g_free(state.record);
465         g_free(state.parse_str);
466
467         uat->changed = TRUE;
468         uat->loaded = TRUE;
469         UAT_UPDATE(uat);
470
471         if (state.error) {
472                 *err = state.error;
473                 return FALSE;
474         }
475
476         if (uat->post_update_cb)
477                 uat->post_update_cb();
478
479         *err = NULL;
480         return TRUE;
481 }