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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
%top {
/* Include this before everything else, for various large-file definitions */
#include "config.h"
#include <wireshark.h>
#define WS_LOG_DOMAIN LOG_DOMAIN_UAT
/* #define DEBUG_UAT_LOAD 1 */
}
/*
* We want a reentrant scanner.
*/
%option reentrant
/*
* We don't use input, so don't generate code for it.
*/
%option noinput
/*
* We don't use unput, so don't generate code for it.
*/
%option nounput
/*
* We don't read interactively from the terminal.
*/
%option never-interactive
/*
* We want to stop processing when we get to the end of the input.
*/
%option noyywrap
/*
* The type for the state we keep for a scanner.
*/
%option extra-type="uat_load_scanner_state_t *"
/*
* We have to override the memory allocators so that we don't get
* "unused argument" warnings from the yyscanner argument (which
* we don't use, as we have a global memory allocator).
*
* We provide, as macros, our own versions of the routines generated by Flex,
* which just call malloc()/realloc()/free() (as the Flex versions do),
* discarding the extra argument.
*/
%option noyyalloc
%option noyyrealloc
%option noyyfree
/*
* Prefix scanner routines with "uat_load_" rather than "yy", so this scanner
* can coexist with other scanners.
*/
%option prefix="uat_load_"
%{
/*
* uat_load.l
*
* User Accessible Tables
* Maintain an array of user accessible data strucures
* One parser to fit them all
*
* (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 2001 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <glib.h>
#include "uat-int.h"
#include <wsutil/file_util.h>
/*
* Disable diagnostics in the code generated by Flex.
*/
DIAG_OFF_FLEX()
typedef struct {
uat_t* uat;
char *parse_str;
char* error;
bool valid_record;
unsigned colnum;
char* ptrx;
unsigned len;
void* record;
unsigned linenum;
size_t parse_str_pos;
} uat_load_scanner_state_t;
/*
* Signal a fatal error and stops parsing.
* Since the record is internal to the parsing process, its contents must also
* be cleared before terminating. Any values that are not handled yet (ptrx)
* must also be freed.
*/
#define ERROR(fmtd) do { \
char* fmt_str = ws_strdup_printf fmtd; \
g_free(yyextra->error); \
yyextra->error = ws_strdup_printf("%s:%d: %s",yyextra->uat->filename,yyextra->linenum,fmt_str); \
g_free(fmt_str); \
if (yyextra->uat->free_cb) { \
yyextra->uat->free_cb(yyextra->record); \
} \
g_free(yyextra->ptrx); \
yyterminate(); \
} while(0)
/*
* Sets the field of the current (scanner-internal) record, using the parsed
* value. If the field validation function exists and returns an error, then
* the record is marked as invalid and an error message is stored such that it
* can be shown after parsing. (If other errors occur after this issue, then
* this message will be overwritten though.)
*/
#define SET_FIELD() \
{ char* errx; \
if (yyextra->uat->fields[yyextra->colnum].cb.chk) { \
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) ) { \
g_free(yyextra->error); \
yyextra->error = ws_strdup_printf("%s:%d: %s",yyextra->uat->filename,yyextra->linenum,errx); \
g_free(errx); \
yyextra->valid_record = false; \
}\
}\
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);\
g_free(yyextra->ptrx);\
yyextra->ptrx = NULL;\
yyextra->colnum++; \
} while(0)
#ifdef DEBUG_UAT_LOAD
#define DUMP_FIELD(str) \
{ unsigned 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 ",((uint8_t*)yyextra->ptrx)[i]); } else putc(yyextra->ptrx[i],stdout); printf("'[%d]\n",yyextra->len); }
#define DUMP(str) printf("%s\n",str)
#else
#define DUMP_FIELD(s)
#define DUMP(s)
#endif
/* Modified version of YY_INPUT generated by Flex 2.91 */
#define YY_INPUT(buf,result,max_size) \
if ( yyextra->parse_str ) \
{ \
size_t n = 0; \
size_t pslen = strlen(yyextra->parse_str); \
if (yyextra->parse_str_pos < pslen) \
{ \
n = pslen - yyextra->parse_str_pos; \
if (n > max_size) n = max_size; \
memcpy(buf, yyextra->parse_str + yyextra->parse_str_pos, n); \
yyextra->parse_str_pos += n; \
} \
result = n; \
} \
else \
{ \
errno=0; \
while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
{ \
if( errno != EINTR) \
{ \
YY_FATAL_ERROR( "input in flex scanner failed" ); \
break; \
} \
errno=0; \
clearerr(yyin); \
} \
}
/*
* XXX
* quoted_string below fails badly on "...\\"
* workarround in uat_save(), using /x5c and /x22
*/
#define YY_USER_INIT BEGIN START_OF_LINE;
/*
* Sleazy hack to suppress compiler warnings in yy_fatal_error().
*/
#define YY_EXIT_FAILURE ((void)yyscanner, 2)
/*
* Macros for the allocators, to discard the extra argument.
*/
#define uat_load_alloc(size, yyscanner) (void *)malloc(size)
#define uat_load_realloc(ptr, size, yyscanner) (void *)realloc((char *)(ptr), (size))
#define uat_load_free(ptr, yyscanner) free((char *)ptr)
%}
quoted_string \042([^\042]|\134\042)*\042
extra_records_string (\042.*\042)
binstring ([0-9a-zA-Z][0-9a-zA-Z])*
separator [ \t]*,
newline [ \t]*[\r]?\n
ws [ \t]+
comment #[^\n]*\n
%x START_OF_LINE NEXT_FIELD SEPARATOR END_OF_RECORD ERRORED
%%
<START_OF_LINE,NEXT_FIELD>{ws} ;
<START_OF_LINE>{newline} yyextra->linenum++;
<START_OF_LINE>{comment} yyextra->linenum++;
<START_OF_LINE,NEXT_FIELD>{separator} {
yyextra->ptrx = g_strdup("");
yyextra->len = 0;
DUMP_FIELD("empty->next");
SET_FIELD();
if ( yyextra->colnum >= yyextra->uat->ncols ) {
ERROR(("more fields than required"));
}
BEGIN NEXT_FIELD;
}
<START_OF_LINE,NEXT_FIELD>{newline} {
yyextra->ptrx = g_strdup("");
yyextra->len = 0;
BEGIN END_OF_RECORD;
yyless((int) yyleng);
}
<START_OF_LINE,NEXT_FIELD>{quoted_string} {
yyextra->ptrx = uat_undquote(yytext, (unsigned) yyleng, &yyextra->len);
if (yyextra->colnum < yyextra->uat->ncols - 1) {
DUMP("quoted_str->s");
BEGIN SEPARATOR;
} else {
DUMP("quoted_str->eor");
BEGIN END_OF_RECORD;
}
}
<START_OF_LINE,NEXT_FIELD>{binstring} {
yyextra->ptrx = uat_unbinstring(yytext, (unsigned) yyleng, &yyextra->len);
if (!yyextra->ptrx) {
ERROR(("uneven hexstring for field %s",yyextra->uat->fields[yyextra->colnum].name));
}
if ( yyextra->colnum < yyextra->uat->ncols - 1 ) {
DUMP("binstring->s");
BEGIN SEPARATOR;
} else {
DUMP("binstring->eor");
BEGIN END_OF_RECORD;
}
}
<SEPARATOR>{separator} {
DUMP_FIELD("separator->next");
SET_FIELD();
if ( yyextra->colnum >= yyextra->uat->ncols ) {
ERROR(("more fields than required"));
}
BEGIN NEXT_FIELD;
}
<SEPARATOR>{newline} {
DUMP("separator->newline");
/*
* We've run out of fields. Check to see if we have any default
* values that we can fill in. The field for the current column
* will be filled in below in <END_OF_RECORD>{newline}.
*/
unsigned save_colnum = yyextra->colnum;
yyextra->colnum++;
while (yyextra->colnum < yyextra->uat->ncols) {
if (!yyextra->uat->default_values) {
break;
}
const char *default_value = yyextra->uat->default_values[yyextra->colnum];
if (!default_value) {
break;
}
yyextra->uat->fields[yyextra->colnum].cb.set(yyextra->record, default_value, (unsigned) strlen(default_value), yyextra->uat->fields[yyextra->colnum].cbdata.chk, yyextra->uat->fields[yyextra->colnum].fld_data);
ws_log(WS_LOG_DOMAIN, LOG_LEVEL_INFO, "%s:%d: Set %s to %s.",
yyextra->uat->filename, yyextra->linenum, yyextra->uat->fields[yyextra->colnum].name, default_value);
yyextra->colnum++;
}
if (yyextra->colnum < yyextra->uat->ncols) {
ERROR(("expecting field %s", yyextra->uat->fields[yyextra->colnum].name));
}
yyextra->colnum = save_colnum;
yyextra->linenum++;
BEGIN END_OF_RECORD;
yyless(0);
}
<SEPARATOR>. {
ERROR(("unexpected char '%s' while looking for field %s",yytext,yyextra->uat->fields[yyextra->colnum].name));
}
<END_OF_RECORD>{separator}{extra_records_string} {
/* If we wanted to be really fancy we could retain the extra data. */
ws_log(WS_LOG_DOMAIN, LOG_LEVEL_WARNING, "%s:%d: More fields than required. Discarding '%s'.",
yyextra->uat->filename, yyextra->linenum, yytext);
}
<END_OF_RECORD>{newline} {
void* rec;
char* err = NULL;
yyextra->linenum++;
DUMP_FIELD("newline->start");
SET_FIELD();
/* Last field was processed, try to store the full record in the UAT. */
rec = uat_add_record(yyextra->uat, yyextra->record, yyextra->valid_record);
if ((yyextra->uat->update_cb) && (rec != NULL)) {
if (!yyextra->uat->update_cb(rec,&err)) {
g_free(yyextra->error);
yyextra->error = err;
yyterminate();
}
}
/* The record was duplicated to the UAT above, now free our fields. */
if (yyextra->uat->free_cb) {
yyextra->uat->free_cb(yyextra->record);
}
memset(yyextra->record, 0, yyextra->uat->record_size);
yyextra->valid_record = true;
yyextra->colnum = 0;
yyextra->ptrx = NULL;
yyextra->len = 0;
BEGIN START_OF_LINE;
}
<END_OF_RECORD>. {
ERROR(("unexpected char %s while looking for end of line", yytext));
}
<ERRORED>{newline} { yyextra->linenum++; BEGIN START_OF_LINE; }
<ERRORED>. ;
{newline} { yyextra->linenum++; ERROR(("incomplete record")); }
. { ERROR(("unexpected input")); }
%%
/*
* Turn diagnostics back on, so we check the code that we've written.
*/
DIAG_ON_FLEX()
bool
uat_load(uat_t *uat, const char *filename, char **errx)
{
char *fname;
FILE *in;
yyscan_t scanner;
uat_load_scanner_state_t state;
if (filename) {
fname = g_strdup(filename);
} else {
fname = uat_get_actual_filename(uat, false);
}
if (!fname) {
UAT_UPDATE(uat);
if (uat->post_update_cb)
uat->post_update_cb();
return true;
}
if (!(in = ws_fopen(fname,"r"))) {
*errx = g_strdup(g_strerror(errno));
g_free(fname);
return false;
}
if (uat_load_lex_init(&scanner) != 0) {
*errx = g_strdup(g_strerror(errno));
fclose(in);
g_free(fname);
return false;
}
uat_load_set_in(in, scanner);
state.uat = uat;
state.parse_str = NULL; /* we're reading from a file */
state.error = NULL;
state.valid_record = true;
state.colnum = 0;
state.ptrx = NULL;
state.len = 0;
state.record = g_malloc0(uat->record_size);
state.linenum = 1;
state.parse_str_pos = 0;
DUMP(fname);
g_free(fname); /* we're done with the file name now */
/* Associate the state with the scanner */
uat_load_set_extra(&state, scanner);
uat_load_lex(scanner);
uat_load_lex_destroy(scanner);
g_free(state.record);
fclose(in);
uat->changed = false;
uat->loaded = true;
UAT_UPDATE(uat);
if (state.error) {
*errx = state.error;
return false;
}
if (uat->post_update_cb)
uat->post_update_cb();
*errx = NULL;
return true;
}
bool
uat_load_str(uat_t *uat, char *entry, char **err)
{
yyscan_t scanner;
uat_load_scanner_state_t state;
state.uat = uat;
state.parse_str = ws_strdup_printf("%s\n", entry); /* Records must end with a newline */
state.error = NULL;
state.valid_record = true;
state.colnum = 0;
state.ptrx = NULL;
state.len = 0;
state.record = g_malloc0(uat->record_size);
state.linenum = 1;
state.parse_str_pos = 0;
if (uat_load_lex_init(&scanner) != 0) {
*err = g_strdup(g_strerror(errno));
g_free(state.parse_str);
g_free(state.record);
return false;
}
DUMP(entry);
/* Associate the state with the scanner */
uat_load_set_extra(&state, scanner);
uat_load_lex(scanner);
uat_load_lex_destroy(scanner);
g_free(state.record);
g_free(state.parse_str);
uat->changed = true;
uat->loaded = true;
UAT_UPDATE(uat);
if (state.error) {
*err = state.error;
return false;
}
if (uat->post_update_cb)
uat->post_update_cb();
*err = NULL;
return true;
}
|