aboutsummaryrefslogtreecommitdiffstats
path: root/epan/uat_load.l
blob: b34ecc51f276b5da9aefe51688fbc169f6b1a3f7 (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
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
/*
 * We don't use unput, so don't generate code for it.
 */
%option nounput

/*
 * We don't read from the terminal.
 */
%option never-interactive

/*
 * Prefix scanner routines with "uat_load_" rather than "yy", so this scanner
 * can coexist with other scanners.
 */
%option prefix="uat_load_"

%{
	/*
	 * uat_load.l
	 *
	 * $Id$
	 *
	 *  User Accessible Tables
	 *  Mantain an array of user accessible data strucures
	 *  One parser to fit them all
	 *
	 * (c) 2007, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
	 *
	 * 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.
	 */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>

#include <glib.h>

#include <epan/emem.h>
#include "uat-int.h"
#include "uat_load_lex.h"
#include <wsutil/file_util.h>

#ifdef _WIN32
/* disable Windows VC compiler warning "signed/unsigned mismatch" associated  */
/* with YY_INPUT code generated by flex versions such as 2.5.35.              */
#pragma warning (disable:4018)
#endif

	static uat_t* uat;
	static guint colnum;
	static gchar* ptr;
	static guint len;
	static gchar* error;
	static void* record;
	static guint linenum;
	static gchar *parse_str;
	static guint parse_str_pos;

#define ERROR(fmtd) do { error = ep_strdup_printf("%s:%d: %s",uat->filename,linenum,ep_strdup_printf fmtd); yyterminate(); } while(0)

#define SET_FIELD() \
	{ const gchar* err; \
	if (uat->fields[colnum].cb.chk) { \
		if ( ! uat->fields[colnum].cb.chk(record, ptr, len, uat->fields[colnum].cbdata.chk, uat->fields[colnum].fld_data, &err) ) { \
			ERROR(("%s",err)); \
		}\
	}\
	uat->fields[colnum].cb.set(record, ptr, len, uat->fields[colnum].cbdata.chk, uat->fields[colnum].fld_data);\
	g_free(ptr);\
	colnum++; \
	} while(0)

#ifdef DEBUG_UAT_LOAD
#define DUMP_FIELD(str) \
		{ guint i; printf("%s: %s='",str,uat->fields[colnum].name); for(i=0;i<len;i++) if (uat->fields[colnum].mode == PT_TXTMOD_HEXBYTES) { printf("%.2x ",((guint8*)ptr)[i]); } else putc(ptr[i],stdout); printf("'[%d]\n",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 ( parse_str ) \
		{ \
		int n = 0; \
		guint pslen = strlen(parse_str); \
		if (parse_str_pos < pslen) \
			{ \
			n = pslen - parse_str_pos; \
			if (n > max_size) n = max_size; \
			memcpy(buf, parse_str + parse_str_pos, n); \
			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
		 */
%}

quoted_string \042([^\042]|\134\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} linenum++;
<START_OF_LINE>{comment} linenum++;

<START_OF_LINE,NEXT_FIELD>{separator} {
	ptr = g_strdup("");
	len = 0;

	DUMP_FIELD("empty->next");

	SET_FIELD();

	if ( colnum >= uat->ncols ) {
		ERROR(("more fields than required"));
	}

	BEGIN NEXT_FIELD;
}

<START_OF_LINE,NEXT_FIELD>{newline}   {
	ptr = "";
	len = 0;

	BEGIN END_OF_RECORD;

	yyless(yyleng);
}

<START_OF_LINE,NEXT_FIELD>{quoted_string} {
	ptr = uat_undquote(yytext,yyleng,&len);


	if (colnum < uat->ncols - 1) {
		DUMP("quoted_str->s");
		BEGIN SEPARATOR;
	} else {
		DUMP("quoted_str->eor");
		BEGIN END_OF_RECORD;
	}
}

<START_OF_LINE,NEXT_FIELD>{binstring} {
	ptr = uat_unbinstring(yytext,yyleng,&len);

	if (!ptr) {
		ERROR(("uneven hexstring for field %s",uat->fields[colnum].name));
	}

	if ( colnum < 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 ( colnum >= uat->ncols ) {
		ERROR(("more fields than required"));
	}

	BEGIN NEXT_FIELD;
}

<SEPARATOR>{newline} {
	linenum++;
	ERROR(("expecting field %s in previuos line",uat->fields[colnum].name));
	BEGIN START_OF_LINE;
}

<SEPARATOR>. {
	ERROR(("unexpected char '%s' while looking for field %s",yytext,uat->fields[colnum].name));
	BEGIN ERRORED;
}

<END_OF_RECORD>{separator} {
	ERROR(("more fields than required"));
	BEGIN ERRORED;
}

<END_OF_RECORD>{newline} {
	void* rec;
	const gchar* err = NULL;

	linenum++;

	DUMP_FIELD("newline->start");

	SET_FIELD();

	rec = uat_add_record(uat, record);

	if (uat->update_cb)
		uat->update_cb(rec,&err);

	if (err) {
		ERROR(("%s",err));
	}

	colnum = 0;;
	ptr = NULL;
	len = 0;
	memset(record,0,uat->record_size);

	BEGIN START_OF_LINE;
 }

<END_OF_RECORD>. {
	ERROR(("unexpected char while looking for end of line"));
	BEGIN ERRORED;
}

<ERRORED>{newline} { linenum++; BEGIN START_OF_LINE; }
<ERRORED>. ;

{newline} { linenum++; ERROR(("incomplete record")); BEGIN START_OF_LINE; }
. { ERROR(("unexpected input")); }

%%




gboolean uat_load(uat_t* uat_in, char** err) {
	gchar* fname = uat_get_actual_filename(uat_in, FALSE);

	uat = uat_in;
	parse_str = NULL;

	if (!fname) {
		UAT_UPDATE(uat);
		return TRUE;
	}


	if (!(yyin = ws_fopen(fname,"r"))) {
		*err = strerror(errno);
		g_free(fname);
		return FALSE;
	}

	error = NULL;
	colnum = 0;
	record = g_malloc0(uat->record_size);
	linenum = 1;

	BEGIN START_OF_LINE;
	DUMP(fname);
	g_free(fname);	/* we're done with the file name now */

	yylex();
	yyrestart(NULL);

	uat->changed = FALSE;
	uat->loaded = TRUE;
	if (error) {
		UAT_UPDATE(uat);
		*err = ep_strdup(error);
		return FALSE;
	} else {
		UAT_UPDATE(uat);
		*err = NULL;
		return TRUE;
	}
}

gboolean uat_load_str(uat_t* uat_in, char* entry, char** err) {
	uat = uat_in;
	parse_str = g_strdup_printf("%s\n", entry); /* Records must end with a newline */
	parse_str_pos = 0;
	yyin = NULL;

	error = NULL;
	colnum = 0;
	record = g_malloc0(uat->record_size);
	linenum = 1;

	BEGIN START_OF_LINE;
	DUMP(entry);

	yylex();
	yyrestart(NULL);
	g_free(parse_str);
	parse_str = NULL;

	uat->changed = TRUE;
	uat->loaded = TRUE;
	if (error) {
		UAT_UPDATE(uat);
		*err = ep_strdup(error);
		return FALSE;
	} else {
		UAT_UPDATE(uat);
		*err = NULL;
		return TRUE;
	}
}

/*
 * We want to stop processing when we get to the end of the input.
 * (%option noyywrap is not used because if used then
 * some flex versions (eg: 2.5.35) generate code which causes
 * warnings by the Windows VC compiler).
 */

int yywrap(void) {
    return 1;
}