diff options
author | Luis Ontanon <luis.ontanon@gmail.com> | 2005-09-28 21:32:47 +0000 |
---|---|---|
committer | Luis Ontanon <luis.ontanon@gmail.com> | 2005-09-28 21:32:47 +0000 |
commit | c6ed07b117f951075a231bc9c1172c3c4c1f4507 (patch) | |
tree | aa13ac94b5a45b3402f3f5729b0e362b76f7f022 | |
parent | 84e00aa0f35c3be94f3cb95234e9d1f47d164571 (diff) | |
download | wireshark-c6ed07b117f951075a231bc9c1172c3c4c1f4507.tar.gz wireshark-c6ed07b117f951075a231bc9c1172c3c4c1f4507.tar.bz2 wireshark-c6ed07b117f951075a231bc9c1172c3c4c1f4507.zip |
tvbparse.[ch]:
- add a tvbparse_handle() (for recursion)
- change tvbparse_until() to allow more control when parsing
- make the wanted control an union so that different types of data can be used
packet-xml.c:
- change the parser definition to match changes to tvbparse_until()
svn path=/trunk/; revision=16045
-rw-r--r-- | epan/dissectors/packet-xml.c | 6 | ||||
-rw-r--r-- | epan/tvbparse.c | 93 | ||||
-rw-r--r-- | epan/tvbparse.h | 19 |
3 files changed, 88 insertions, 30 deletions
diff --git a/epan/dissectors/packet-xml.c b/epan/dissectors/packet-xml.c index fdf7da966a..2015146a4d 100644 --- a/epan/dissectors/packet-xml.c +++ b/epan/dissectors/packet-xml.c @@ -483,7 +483,7 @@ void init_xml_parser(void) { tvbparse_string(-1,"<!--",NULL,NULL,NULL), tvbparse_until(-1,NULL,NULL,NULL, tvbparse_string(-1,"-->",NULL,NULL,NULL), - TRUE), + TP_UNTIL_INCLUDE), NULL); tvbparse_wanted_t* want_xmlpi = tvbparse_set_seq(hf_xmlpi,NULL,before_xmpli,NULL, @@ -522,7 +522,7 @@ void init_xml_parser(void) { NULL), tvbparse_until(-1,NULL,NULL,NULL, tvbparse_char(-1,">",NULL,NULL,NULL), - TRUE), + TP_UNTIL_INCLUDE), NULL), NULL), NULL); @@ -532,7 +532,7 @@ void init_xml_parser(void) { tvbparse_char(-1,"!",NULL,NULL,NULL), tvbparse_until(-1,NULL,NULL,NULL, tvbparse_char(-1, ">", NULL, NULL, NULL), - TRUE), + TP_UNTIL_INCLUDE), NULL); tvbparse_wanted_t* want_tag = tvbparse_set_seq(-1, NULL, before_tag, NULL, diff --git a/epan/tvbparse.c b/epan/tvbparse.c index 0007104817..3c90230f09 100644 --- a/epan/tvbparse.c +++ b/epan/tvbparse.c @@ -50,7 +50,8 @@ typedef enum _tvbparse_wanted_type_t { /* composed tokens */ TVBPARSE_WANTED_SET_ONEOF, /* one of the given types */ TVBPARSE_WANTED_SET_SEQ, /* an exact sequence of tokens of the given types */ - TVBPARSE_WANTED_CARDINALITY /* one or more tokens of the given type */ + TVBPARSE_WANTED_CARDINALITY, /* one or more tokens of the given type */ + TVBPARSE_WANTED_HANDLE, /* a handle to another one */ } tvbparse_type_t; @@ -67,7 +68,12 @@ struct _tvbparse_wanted_t { int id; tvbparse_type_t type; - const gchar* ctl; + union { + const gchar* str; + guint val; + struct _tvbparse_wanted_t** handle; + } control; + int len; guint min; @@ -90,7 +96,7 @@ tvbparse_wanted_t* tvbparse_char(int id, w->id = id; w->type = TVBPARSE_WANTED_SIMPLE_CHAR; - w->ctl = chr; + w->control.str = chr; w->len = 1; w->min = 0; w->max = 0; @@ -113,7 +119,7 @@ tvbparse_wanted_t* tvbparse_chars(int id, w->id = id; w->type = TVBPARSE_WANTED_SIMPLE_CHARS; - w->ctl = chr; + w->control.str = chr; w->len = 0; w->min = min_len ? min_len : 1; w->max = max_len ? max_len : G_MAXINT; @@ -134,7 +140,7 @@ tvbparse_wanted_t* tvbparse_not_char(int id, w->id = id; w->type = TVBPARSE_WANTED_SIMPLE_NOT_CHAR; - w->ctl = chr; + w->control.str = chr; w->len = 0; w->min = 0; w->max = 0; @@ -157,7 +163,7 @@ tvbparse_wanted_t* tvbparse_not_chars(int id, w->id = id; w->type = TVBPARSE_WANTED_SIMPLE_NOT_CHARS; - w->ctl = chr; + w->control.str = chr; w->len = 0; w->min = min_len ? min_len : 1; w->max = max_len ? max_len : G_MAXINT; @@ -179,7 +185,7 @@ tvbparse_wanted_t* tvbparse_string(int id, w->id = id; w->type = TVBPARSE_WANTED_SIMPLE_STRING; - w->ctl = str; + w->control.str = str; w->len = strlen(str); w->min = 0; w->max = 0; @@ -200,7 +206,7 @@ tvbparse_wanted_t* tvbparse_casestring(int id, w->id = id; w->type = TVBPARSE_WANTED_SIMPLE_CASESTRING; - w->ctl = str; + w->control.str = str; w->len = strlen(str); w->min = 0; w->max = 0; @@ -224,7 +230,7 @@ tvbparse_wanted_t* tvbparse_set_oneof(int id, w->id = id; w->type = TVBPARSE_WANTED_SET_ONEOF; - w->ctl = NULL; + w->control.val = 0; w->len = 0; w->min = 0; w->max = 0; @@ -255,7 +261,7 @@ tvbparse_wanted_t* tvbparse_set_seq(int id, w->id = id; w->type = TVBPARSE_WANTED_SET_SEQ; - w->ctl = NULL; + w->control.val = 0; w->len = 0; w->min = 0; w->max = 0; @@ -289,7 +295,7 @@ tvbparse_wanted_t* tvbparse_some(int id, w->id = id; w->type = TVBPARSE_WANTED_CARDINALITY; - w->ctl = NULL; + w->control.val = 0; w->len = 0; w->min = from; w->max = to; @@ -308,14 +314,13 @@ tvbparse_wanted_t* tvbparse_until(int id, tvbparse_action_t before_cb, tvbparse_action_t after_cb, const tvbparse_wanted_t* el, - gboolean include_term) { + int op_mode) { tvbparse_wanted_t* w = g_malloc(sizeof(tvbparse_wanted_t)); w->id = id; w->type = TVBPARSE_WANTED_UNTIL; - /* XXX this is ugly */ - w->ctl = include_term ? "include" : "do not include"; + w->control.val = op_mode; w->len = 0; w->min = 0; @@ -330,6 +335,25 @@ tvbparse_wanted_t* tvbparse_until(int id, return w; } +tvbparse_wanted_t* tvbparse_handle(tvbparse_wanted_t** handle) { + tvbparse_wanted_t* w = g_malloc(sizeof(tvbparse_wanted_t)); + + w->id = 0; + w->type = TVBPARSE_WANTED_HANDLE; + + w->control.handle = handle; + + w->len = 0; + w->min = 0; + w->max = 0; + w->data = NULL; + w->before = NULL; + w->after = NULL; + w->elems = NULL; + + return w; +} + tvbparse_wanted_t* tvbparse_quoted(int id, const void* data, @@ -459,7 +483,7 @@ tvbparse_elem_t* tvbparse_get(tvbparse_t* tt, t = (gchar) tvb_get_guint8(tt->tvb,tt->offset); - for(i = 0; (c = wanted->ctl[i]) && tt->max_len; i++) { + for(i = 0; (c = wanted->control.str[i]) && tt->max_len; i++) { if ( c == t ) { not_matched = TRUE; } @@ -484,7 +508,7 @@ tvbparse_elem_t* tvbparse_get(tvbparse_t* tt, t = (gchar) tvb_get_guint8(tt->tvb,tt->offset); - for(i = 0; (c = wanted->ctl[i]) && tt->max_len; i++) { + for(i = 0; (c = wanted->control.str[i]) && tt->max_len; i++) { if ( c == t ) { tt->offset++; tt->max_len--; @@ -506,7 +530,7 @@ tvbparse_elem_t* tvbparse_get(tvbparse_t* tt, t = (gchar) tvb_get_guint8(tt->tvb,tt->offset); i = 0; - while ( (c = wanted->ctl[i]) && tt->max_len ) { + while ( (c = wanted->control.str[i]) && tt->max_len ) { if (c == t) { not_matched = TRUE; @@ -542,7 +566,7 @@ tvbparse_elem_t* tvbparse_get(tvbparse_t* tt, t = (gchar) tvb_get_guint8(tt->tvb,tt->offset); i = 0; - while ( (c = wanted->ctl[i]) && tt->max_len ) { + while ( (c = wanted->control.str[i]) && tt->max_len ) { if (c == t) { matched = TRUE; @@ -569,7 +593,7 @@ tvbparse_elem_t* tvbparse_get(tvbparse_t* tt, } case TVBPARSE_WANTED_SIMPLE_STRING: { - if ( tvb_strneql(tt->tvb, tt->offset, wanted->ctl, wanted->len) == 0 ) { + if ( tvb_strneql(tt->tvb, tt->offset, wanted->control.str, wanted->len) == 0 ) { int offset = tt->offset; tt->offset += wanted->len; tt->max_len -= wanted->len; @@ -581,7 +605,7 @@ tvbparse_elem_t* tvbparse_get(tvbparse_t* tt, } case TVBPARSE_WANTED_SIMPLE_CASESTRING: { - if ( tvb_strncaseeql(tt->tvb, tt->offset, wanted->ctl, wanted->len) == 0 ) { + if ( tvb_strncaseeql(tt->tvb, tt->offset, wanted->control.str, wanted->len) == 0 ) { int offset = tt->offset; tt->offset += wanted->len; tt->max_len -= wanted->len; @@ -675,11 +699,19 @@ tvbparse_elem_t* tvbparse_get(tvbparse_t* tt, if (new) { tok = new; - /* XXX this is ugly */ - if (*(wanted->ctl) == 'i' ) { - tok->len = (tok->offset - offset) + tok->len; - } else { - tok->len = (tok->offset - offset); + switch (wanted->control.val) { + case TP_UNTIL_INCLUDE: + tok->len = (tok->offset - offset) + tok->len; + break; + case TP_UNTIL_LEAVE: + tt->offset -= tok->len; + tt->max_len += tok->len; + /* fall through */ + case TP_UNTIL_SPEND: + tok->len = (tok->offset - offset); + break; + default: + DISSECTOR_ASSERT_NOT_REACHED(); } tok->offset = offset; @@ -693,6 +725,15 @@ tvbparse_elem_t* tvbparse_get(tvbparse_t* tt, goto reject; } } + case TVBPARSE_WANTED_HANDLE: + { + tok = tvbparse_get(tt, *(wanted->control.handle)); + if (tok) { + goto accept; + } else { + goto reject; + } + } } DISSECTOR_ASSERT_NOT_REACHED(); @@ -728,7 +769,7 @@ accept: } - g_ptr_array_free(stack,FALSE); + g_ptr_array_free(stack,TRUE); } tt->depth--; diff --git a/epan/tvbparse.h b/epan/tvbparse.h index 78a300740d..536f31e25c 100644 --- a/epan/tvbparse.h +++ b/epan/tvbparse.h @@ -218,8 +218,16 @@ tvbparse_wanted_t* tvbparse_until(int id, tvbparse_action_t before_cb, tvbparse_action_t after_cb, const tvbparse_wanted_t* ending, - gboolean include_ending); + int op_mode); +/* + * op_mode values determine how the terminating element and the current offset + * of the parser are handled + */ + +#define TP_UNTIL_INCLUDE 0 /* elem is included, its span is spent by the parser */ +#define TP_UNTIL_SPEND 1 /* elem is not included, but its span is spent by the parser */ +#define TP_UNTIL_LEAVE 2 /* elem is not included, neither its span is spent by the parser */ /* * one_of @@ -270,6 +278,15 @@ tvbparse_wanted_t* tvbparse_some(int id, #define tvbparse_one_or_more(id, private_data, before_cb, after_cb, wanted)\ tvbparse_some(id, 1, G_MAXINT, private_data, before_cb, after_cb, wanted) + +/* + * handle + * + * this is a pointer to a pointer to a wanted element (that might have not + * been initialized yet) so that recursive structures + */ +tvbparse_wanted_t* tvbparse_handle(tvbparse_wanted_t** handle); + /* quoted * this is a composed candidate, that will try to match a quoted string * (included the quotes) including into it every escaped quote. |