2007-01-28 10:31:32 +00:00
|
|
|
%option noyywrap
|
|
|
|
%option nounput
|
|
|
|
%option prefix="uat_load_"
|
|
|
|
%option never-interactive
|
2007-01-29 04:47:58 +00:00
|
|
|
%option yylineno
|
2007-01-28 10:31:32 +00:00
|
|
|
|
|
|
|
%{
|
|
|
|
/*
|
2007-01-29 04:47:58 +00:00
|
|
|
* uat_load.c
|
|
|
|
*
|
|
|
|
* $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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
2007-01-28 10:31:32 +00:00
|
|
|
*/
|
|
|
|
#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"
|
2007-02-03 14:25:02 +00:00
|
|
|
|
2007-01-28 10:31:32 +00:00
|
|
|
static uat_t* uat;
|
2007-02-03 14:25:02 +00:00
|
|
|
static guint colnum;
|
2007-01-28 10:31:32 +00:00
|
|
|
static gchar* ptr;
|
|
|
|
static guint len;
|
|
|
|
static gchar* error;
|
|
|
|
static void* record;
|
|
|
|
|
|
|
|
static char* unbinstring(const char* si, guint in_len, guint* len_p);
|
|
|
|
static char* undquote(const char* si, guint in_len, guint* len_p);
|
|
|
|
|
2007-01-29 10:29:50 +00:00
|
|
|
#define ERROR(fmtd) do { error = ep_strdup_printf("%s:%d: %s",uat->filename,yylineno,ep_strdup_printf fmtd); yyterminate(); } while(0)
|
2007-01-28 10:31:32 +00:00
|
|
|
|
|
|
|
#define SET_FIELD() \
|
|
|
|
{ gchar* err; \
|
2007-02-03 14:25:02 +00:00
|
|
|
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) ) { \
|
2007-01-29 10:29:50 +00:00
|
|
|
ERROR(("%s",err)); \
|
2007-01-28 10:31:32 +00:00
|
|
|
}\
|
|
|
|
}\
|
2007-02-03 14:25:02 +00:00
|
|
|
uat->fields[colnum].cb.set(record, ptr, len,uat->fields[colnum].cbdata.chk,uat->fields[colnum].fld_data);\
|
2007-01-28 10:31:32 +00:00
|
|
|
g_free(ptr);\
|
2007-02-03 14:25:02 +00:00
|
|
|
colnum++; \
|
2007-01-28 10:31:32 +00:00
|
|
|
} while(0)
|
|
|
|
|
2007-01-29 10:29:50 +00:00
|
|
|
#ifdef DEBUG_UAT_LOAD
|
2007-01-29 04:47:58 +00:00
|
|
|
#define DUMP_FIELD(str) \
|
|
|
|
{ guint i; printf("%s: '",str); for(i=0;i<len;i++) putc(ptr[i],stdout); printf("'[%d]\n",len); }
|
2007-01-28 10:31:32 +00:00
|
|
|
|
2007-01-29 04:47:58 +00:00
|
|
|
#define DUMP(str) printf("%s\n",str)
|
|
|
|
#else
|
|
|
|
#define DUMP_FIELD(s)
|
|
|
|
#define DUMP(s)
|
|
|
|
#endif
|
2007-01-28 10:31:32 +00:00
|
|
|
%}
|
|
|
|
|
|
|
|
quoted_string \042([^\042]|\134\042)*\042
|
|
|
|
binstring ([0-9a-zA-Z][0-9a-zA-Z])+
|
2007-01-29 04:47:58 +00:00
|
|
|
separator [ \t]*,
|
2007-01-28 10:31:32 +00:00
|
|
|
newline [ \t]*[\r]?\n
|
2007-01-29 04:47:58 +00:00
|
|
|
ws [ \t]+
|
|
|
|
comment #[^\n]*\n
|
2007-01-28 10:31:32 +00:00
|
|
|
|
2007-01-30 00:05:22 +00:00
|
|
|
%x START_OF_LINE NEXT_FIELD SEPARATOR END_OF_RECORD ERRORED
|
2007-01-28 10:31:32 +00:00
|
|
|
%%
|
2007-01-29 04:47:58 +00:00
|
|
|
<START_OF_LINE,NEXT_FIELD>{ws} ;
|
|
|
|
<START_OF_LINE>{newline} ;
|
|
|
|
<START_OF_LINE>{comment} ;
|
|
|
|
<NEXT_FIELD>{newline} {
|
2007-02-03 14:25:02 +00:00
|
|
|
ERROR(("expecting %s field in previuos line",uat->fields[colnum].name));
|
2007-01-29 04:47:58 +00:00
|
|
|
BEGIN START_OF_LINE;
|
|
|
|
}
|
2007-01-28 10:31:32 +00:00
|
|
|
|
|
|
|
<START_OF_LINE,NEXT_FIELD>{quoted_string} {
|
|
|
|
ptr = undquote(yytext,yyleng,&len);
|
2007-01-29 04:47:58 +00:00
|
|
|
|
|
|
|
|
2007-02-03 14:25:02 +00:00
|
|
|
if (colnum < uat->ncols - 1) {
|
2007-01-29 04:47:58 +00:00
|
|
|
DUMP("quoted_str->s");
|
2007-01-28 10:31:32 +00:00
|
|
|
BEGIN SEPARATOR;
|
|
|
|
} else {
|
2007-01-29 04:47:58 +00:00
|
|
|
DUMP("quoted_str->eor");
|
2007-01-28 10:31:32 +00:00
|
|
|
BEGIN END_OF_RECORD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<START_OF_LINE,NEXT_FIELD>{binstring} {
|
|
|
|
ptr = unbinstring(yytext,yyleng,&len);
|
2007-01-29 04:47:58 +00:00
|
|
|
|
2007-01-28 10:31:32 +00:00
|
|
|
if (!ptr) {
|
2007-02-03 14:25:02 +00:00
|
|
|
ERROR(("uneven hexstring for field %s",uat->fields[colnum].name));
|
2007-01-28 10:31:32 +00:00
|
|
|
}
|
|
|
|
|
2007-02-03 14:25:02 +00:00
|
|
|
if ( colnum < uat->ncols - 1 ) {
|
2007-01-29 04:47:58 +00:00
|
|
|
DUMP("binstring->s");
|
2007-01-28 10:31:32 +00:00
|
|
|
BEGIN SEPARATOR;
|
|
|
|
} else {
|
2007-01-29 04:47:58 +00:00
|
|
|
DUMP("binstring->eor");
|
2007-01-28 10:31:32 +00:00
|
|
|
BEGIN END_OF_RECORD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<SEPARATOR>{separator} {
|
2007-01-29 04:47:58 +00:00
|
|
|
|
|
|
|
DUMP_FIELD("separator->next");
|
|
|
|
|
2007-01-28 10:31:32 +00:00
|
|
|
SET_FIELD();
|
2007-01-29 04:47:58 +00:00
|
|
|
|
2007-02-03 14:25:02 +00:00
|
|
|
if ( colnum >= uat->ncols ) {
|
2007-01-29 10:29:50 +00:00
|
|
|
ERROR(("more fields than required"));
|
2007-01-28 10:31:32 +00:00
|
|
|
}
|
2007-01-29 04:47:58 +00:00
|
|
|
|
|
|
|
BEGIN NEXT_FIELD;
|
|
|
|
}
|
|
|
|
|
|
|
|
<SEPARATOR>{newline} {
|
2007-02-03 14:25:02 +00:00
|
|
|
ERROR(("expecting field %s in previuos line",uat->fields[colnum].name));
|
2007-01-29 04:47:58 +00:00
|
|
|
BEGIN START_OF_LINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
<SEPARATOR>. {
|
2007-02-03 14:25:02 +00:00
|
|
|
ERROR(("unexpected char while looking for field %s",uat->fields[colnum].name));
|
2007-01-29 04:47:58 +00:00
|
|
|
BEGIN ERRORED;
|
|
|
|
}
|
|
|
|
|
|
|
|
<END_OF_RECORD>{separator} {
|
2007-01-29 10:29:50 +00:00
|
|
|
ERROR(("more fields than required"));
|
2007-01-29 04:47:58 +00:00
|
|
|
BEGIN ERRORED;
|
2007-01-28 10:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<END_OF_RECORD>{newline} {
|
|
|
|
void* rec;
|
|
|
|
gchar* err = NULL;
|
|
|
|
|
2007-01-29 04:47:58 +00:00
|
|
|
DUMP_FIELD("newline->start");
|
|
|
|
|
2007-01-28 10:31:32 +00:00
|
|
|
SET_FIELD();
|
|
|
|
|
|
|
|
rec = uat_add_record(uat, record);
|
2007-02-03 14:25:02 +00:00
|
|
|
|
2007-01-28 10:31:32 +00:00
|
|
|
if (uat->update_cb)
|
|
|
|
uat->update_cb(rec,&err);
|
|
|
|
|
|
|
|
if (err) {
|
2007-01-29 10:29:50 +00:00
|
|
|
ERROR(("%s",err));
|
2007-01-28 10:31:32 +00:00
|
|
|
}
|
|
|
|
|
2007-02-03 14:25:02 +00:00
|
|
|
colnum = 0;;
|
2007-01-28 10:31:32 +00:00
|
|
|
ptr = NULL;
|
|
|
|
len = 0;
|
|
|
|
memset(record,0,uat->record_size);
|
|
|
|
|
|
|
|
BEGIN START_OF_LINE;
|
|
|
|
}
|
|
|
|
|
2007-01-29 04:47:58 +00:00
|
|
|
<END_OF_RECORD>. {
|
2007-01-29 10:29:50 +00:00
|
|
|
ERROR(("unexpected char while looking for end of line"));
|
2007-01-29 04:47:58 +00:00
|
|
|
BEGIN ERRORED;
|
|
|
|
}
|
|
|
|
|
|
|
|
<ERRORED>{newline} BEGIN START_OF_LINE;
|
|
|
|
<ERRORED>. ;
|
|
|
|
|
2007-01-29 10:29:50 +00:00
|
|
|
{newline} { ERROR(("incomplete record")); }
|
|
|
|
. { ERROR(("unexpected input")); }
|
|
|
|
|
2007-01-28 10:31:32 +00:00
|
|
|
%%
|
|
|
|
|
|
|
|
static int xton(char d) {
|
|
|
|
switch(d) {
|
|
|
|
case '0': return 0;
|
|
|
|
case '1': return 1;
|
|
|
|
case '2': return 2;
|
|
|
|
case '3': return 3;
|
|
|
|
case '4': return 4;
|
|
|
|
case '5': return 5;
|
|
|
|
case '6': return 6;
|
|
|
|
case '7': return 7;
|
|
|
|
case '8': return 8;
|
|
|
|
case '9': return 9;
|
|
|
|
case 'a': case 'A': return 10;
|
|
|
|
case 'b': case 'B': return 11;
|
|
|
|
case 'c': case 'C': return 12;
|
|
|
|
case 'd': case 'D': return 13;
|
|
|
|
case 'e': case 'E': return 14;
|
|
|
|
case 'f': case 'F': return 15;
|
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* unbinstring(const char* si, guint in_len, guint* len_p) {
|
|
|
|
char* buf;
|
|
|
|
guint len = in_len/2;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (in_len%2) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf= g_malloc(len); /* wastes one byte for every '\\' in text */
|
|
|
|
*len_p = len;
|
|
|
|
|
|
|
|
while(in_len) {
|
|
|
|
int d1 = xton(*(si++));
|
|
|
|
int d0 = xton(*(si++));
|
|
|
|
|
|
|
|
buf[i++] = (d1 * 16) + d0;
|
|
|
|
|
|
|
|
in_len -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* undquote(const char* si, guint in_len, guint* len_p) {
|
|
|
|
char* buf = g_malloc(in_len); /* wastes one byte for every '\\' in text */
|
|
|
|
char* p = buf;
|
|
|
|
guint len = 0;
|
|
|
|
char* end = buf+in_len;
|
|
|
|
const guint8* s = (void*)si;
|
|
|
|
|
|
|
|
for (s++; p < end; s++) {
|
|
|
|
switch(*s) {
|
|
|
|
case '\0':
|
|
|
|
*(p-1) = '\0';
|
|
|
|
goto done;
|
|
|
|
case '\\':
|
|
|
|
switch(*(++s)) {
|
|
|
|
case 'a': *(p++) = '\a'; len++; break;
|
|
|
|
case 'b': *(p++) = '\b'; len++; break;
|
|
|
|
case 'e': *(p++) = '\e'; len++; break;
|
|
|
|
case 'f': *(p++) = '\f'; len++; break;
|
|
|
|
case 'n': *(p++) = '\n'; len++; break;
|
|
|
|
case 'r': *(p++) = '\r'; len++; break;
|
|
|
|
case 't': *(p++) = '\t'; len++; break;
|
|
|
|
case 'v': *(p++) = '\v'; len++; break;
|
|
|
|
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
{
|
|
|
|
int c0 = 0;
|
|
|
|
int c1 = 0;
|
|
|
|
int c2 = 0;
|
|
|
|
int c = 0;
|
|
|
|
|
|
|
|
c0 = (*s) - '0';
|
|
|
|
|
|
|
|
if ( s[1] >= '0' && s[1] <= '7' ) {
|
|
|
|
c1 = c0;
|
|
|
|
c0 = (*++s) - '0';
|
|
|
|
|
|
|
|
if ( s[1] >= '0' && s[1] <= '7' ) {
|
|
|
|
c2 = c1;
|
|
|
|
c1 = c0;
|
|
|
|
c0 = (*++s) - '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c = (64 * c2) + (8 * c1) + c0;
|
|
|
|
*(p++) = (char) (c > 255 ? 255 : c);
|
|
|
|
len++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
{
|
|
|
|
char c1 = *(s+1);
|
|
|
|
char c0 = *(s+2);
|
|
|
|
|
|
|
|
if (isxdigit(c1) && isxdigit(c0)) {
|
|
|
|
*(p++) = (xton(c1) * 0x10) + xton(c0);
|
|
|
|
s += 2;
|
|
|
|
} else {
|
|
|
|
*(p++) = *s;
|
|
|
|
}
|
|
|
|
len++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
*p++ = *s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*(p++) = *s;
|
|
|
|
len++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
|
|
|
while ( p < end ) *(p++) = '\0';
|
|
|
|
buf[len] = '\0';
|
|
|
|
len--;
|
|
|
|
if (len_p) *len_p = len;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2007-01-29 04:47:58 +00:00
|
|
|
gboolean uat_load(uat_t* uat_in, char** err) {
|
|
|
|
gchar* fname = uat_get_actual_filename(uat_in, FALSE);
|
2007-02-03 14:25:02 +00:00
|
|
|
|
2007-01-29 04:47:58 +00:00
|
|
|
uat = uat_in;
|
2007-02-03 14:25:02 +00:00
|
|
|
|
|
|
|
if (!fname) {
|
|
|
|
UAT_UPDATE(uat);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-01-28 10:31:32 +00:00
|
|
|
if (!(yyin = fopen(fname,"r"))) {
|
|
|
|
*err = strerror(errno);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = NULL;
|
2007-02-03 14:25:02 +00:00
|
|
|
colnum = 0;
|
2007-01-28 10:31:32 +00:00
|
|
|
record = g_malloc0(uat->record_size);
|
|
|
|
|
|
|
|
BEGIN START_OF_LINE;
|
|
|
|
|
|
|
|
yylex();
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
UAT_UPDATE(uat);
|
|
|
|
*err = ep_strdup(error);
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
UAT_UPDATE(uat);
|
|
|
|
*err = NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|