diff options
author | Luis Ontanon <luis.ontanon@gmail.com> | 2005-03-10 17:13:47 +0000 |
---|---|---|
committer | Luis Ontanon <luis.ontanon@gmail.com> | 2005-03-10 17:13:47 +0000 |
commit | 8ee0a507c7807e32e7bad409df068e43f0eb1e18 (patch) | |
tree | 58b517cd1eb5ae1b884fcfee3f2974f3c8421e29 /plugins | |
parent | 25967a2604f9fd0030da2474fa8edf06b84d4361 (diff) |
An optimization.
Instead of first running the lexer to create a list of items and then iterate through this items to create the tree, just create the tree from the lexer.
svn path=/trunk/; revision=13696
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/xml/packet-xml.c | 82 | ||||
-rw-r--r-- | plugins/xml/packet-xml.h | 30 | ||||
-rw-r--r-- | plugins/xml/xml_lexer.c | 187 | ||||
-rw-r--r-- | plugins/xml/xml_lexer.l | 147 |
4 files changed, 219 insertions, 227 deletions
diff --git a/plugins/xml/packet-xml.c b/plugins/xml/packet-xml.c index ff244b3969..13075c957f 100644 --- a/plugins/xml/packet-xml.c +++ b/plugins/xml/packet-xml.c @@ -28,32 +28,24 @@ # include "config.h" #endif -#include <string.h> -#include <ctype.h> -#include <stdlib.h> - -#include <glib.h> - -#include <epan/packet.h> -#include <epan/strutil.h> -#include <epan/prefs.h> -#include <epan/report_err.h> - #include "packet-xml.h" static int proto_xml = -1; + static int ett_xml = -1; + static int hf_xml_pi = -1; static int hf_xml_markup_decl = -1; static int hf_xml_tag = -1; static int hf_xml_text = -1; -static proto_item* proto_tree_add_xml_item(proto_tree* tree, tvbuff_t* tvb, xml_token_t* xi) { +extern proto_item* proto_tree_add_xml_item(proto_tree* tree, tvbuff_t* tvb, + xml_token_type_t type, guint offset, guint len) { proto_item* pi; gchar* txt; int hfid; - switch (xi->type) { + switch (type) { case XML_TAG: hfid = hf_xml_tag; break; case XML_MARKUPDECL: hfid = hf_xml_markup_decl; break; case XML_XMLPI: hfid = hf_xml_pi; break; @@ -61,12 +53,12 @@ static proto_item* proto_tree_add_xml_item(proto_tree* tree, tvbuff_t* tvb, xml_ default: hfid = 0; break; } - txt = tvb_get_string(tvb,xi->offset,xi->len); + txt = tvb_get_string(tvb,offset,len); if ( hfid ) { - pi = proto_tree_add_string_format(tree,hfid,tvb,xi->offset,xi->len,txt,format_text(txt, xi->len)); + pi = proto_tree_add_string_format(tree,hfid,tvb,offset,len,txt,format_text(txt, len)); } else { - pi = proto_tree_add_text(tree,tvb,xi->offset,xi->len,"%s",format_text(txt, xi->len)); + pi = proto_tree_add_text(tree,tvb,offset,len,"%s",format_text(txt, len)); } g_free(txt); @@ -74,56 +66,6 @@ static proto_item* proto_tree_add_xml_item(proto_tree* tree, tvbuff_t* tvb, xml_ return pi; } - -static void dissect_xml(tvbuff_t* tvb, packet_info* pinfo _U_, proto_tree* tree) { - xml_token_t* xml_items ; - xml_token_t* xi; - xml_token_t* next_xi; - proto_item* pi = NULL; - GPtrArray* stack = g_ptr_array_new(); - - if (tree) { - pi = proto_tree_add_item(tree, proto_xml, tvb, 0, tvb->length, FALSE); - - tree = proto_item_add_subtree(pi, ett_xml); - - xml_items = scan_tvb_for_xml_items(tvb, 0, tvb->length); - - for (xi = xml_items; xi; xi = next_xi) { - next_xi = xi->next; - - switch (xi->type) { - case XML_WHITESPACE: - break; - case XML_CLOSEDTAG: - case XML_TEXT: - case XML_MARKUPDECL: - case XML_XMLPI: - case XML_COMMENT: - proto_tree_add_xml_item(tree,tvb,xi); - break; - case XML_DOCTYPE_START: - case XML_TAG: - pi = proto_tree_add_xml_item(tree,tvb,xi); - g_ptr_array_add(stack,tree); - tree = proto_item_add_subtree(pi, ett_xml); - break; - case XML_CLOSE_TAG: - case XML_DOCTYPE_STOP: - pi = proto_tree_add_xml_item(tree,tvb,xi); - if ( stack->len ) - tree = g_ptr_array_remove_index(stack,stack->len - 1); - break; - } - - g_free(xi); - } - - g_ptr_array_free(stack,FALSE); - } - -} - void proto_register_xml(void) { @@ -150,20 +92,20 @@ proto_register_xml(void) &ett_xml }; - - proto_xml = proto_register_protocol("eXtensible Markup Language", "XML", "xml"); proto_register_field_array(proto_xml, hf, array_length(hf)); proto_register_subtree_array(ett, array_length(ett)); register_dissector("xml", dissect_xml, proto_xml); + } void proto_reg_handoff_xml(void) { + dissector_handle_t xml_handle; - + xml_handle = find_dissector("xml"); dissector_add_string("media_type", "text/xml", xml_handle); @@ -184,4 +126,6 @@ proto_reg_handoff_xml(void) dissector_add_string("media_type", "application/xslt+xml", xml_handle); dissector_add_string("media_type", "application/mathml+xml", xml_handle); dissector_add_string("media_type", "image/svg+xml", xml_handle); + + xml_lexer_init(proto_xml,ett_xml); } diff --git a/plugins/xml/packet-xml.h b/plugins/xml/packet-xml.h index 08467446ca..3915dd9bdb 100644 --- a/plugins/xml/packet-xml.h +++ b/plugins/xml/packet-xml.h @@ -28,7 +28,17 @@ #define __XML_H_ #include <epan/tvbuff.h> -typedef struct _xml_token_t xml_token_t; +#include <string.h> +#include <ctype.h> +#include <stdlib.h> + +#include <glib.h> + +#include <epan/packet.h> +#include <epan/strutil.h> +#include <epan/prefs.h> +#include <epan/report_err.h> + typedef enum _xml_token_type_t { XML_WHITESPACE, @@ -43,14 +53,16 @@ typedef enum _xml_token_type_t { XML_DOCTYPE_STOP } xml_token_type_t; -struct _xml_token_t { - xml_token_type_t type; - int offset; - int len; - xml_token_t* next; - xml_token_t* prev; -}; +extern proto_item* proto_tree_add_xml_item(proto_tree* tree, + tvbuff_t* tvb, + xml_token_type_t type, + guint offset, + guint len); + +extern void xml_lexer_init(int proto_hfid, int ett); -extern xml_token_t* scan_tvb_for_xml_items(tvbuff_t*, gint, gint); +extern void dissect_xml(tvbuff_t* tvb, + packet_info* pinfo, + proto_tree* tree); #endif diff --git a/plugins/xml/xml_lexer.c b/plugins/xml/xml_lexer.c index a630af42b9..c426ef288b 100644 --- a/plugins/xml/xml_lexer.c +++ b/plugins/xml/xml_lexer.c @@ -414,23 +414,32 @@ char *yytext; * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - -#include "packet-xml.h" +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif +#include "packet-xml.h" + static guint8* extracted = NULL; static gint offset; - static gint last_offset; static gint text_offset; static gint len; - xml_token_t* head; - xml_token_t* tail; -#define YY_INPUT(buff,result,max_size) ( (result) = tvb_yyinput((buff),(max_size)) ) + tvbuff_t* tvb; + proto_item* pi; + proto_tree* tree; + GPtrArray* stack; + static int ett_xml; + static int proto_xml; + +#define YY_INPUT(buff,result,max_size) \ + ( (result) = tvb_yyinput((buff),(max_size)) ) + #define ECHO ; - static void add_xml_item(xml_token_type_t type, gint len); - static int tvb_yyinput(char* buff, guint max_len); + static void add_xml_item(xml_token_type_t, guint); + static int tvb_yyinput(char*, guint); #define OUT 1 #define COMMENT 2 @@ -441,7 +450,7 @@ char *yytext; #define MARKUPDECL 7 #define TEXT 8 -#line 445 "xml_lexer.c" +#line 454 "xml_lexer.c" /* Macros after this point can all be overridden by user definitions in * section 1. @@ -592,10 +601,10 @@ YY_DECL register char *yy_cp, *yy_bp; register int yy_act; -#line 73 "xml_lexer.l" +#line 82 "xml_lexer.l" -#line 599 "xml_lexer.c" +#line 608 "xml_lexer.c" if ( yy_init ) { @@ -680,90 +689,90 @@ do_action: /* This label is used only to access EOF actions. */ case 1: YY_RULE_SETUP -#line 75 "xml_lexer.l" -{ add_xml_item(XML_WHITESPACE, yyleng); } +#line 84 "xml_lexer.l" +{ text_offset = offset - yyleng; add_xml_item(XML_WHITESPACE, yyleng); } YY_BREAK case 2: YY_RULE_SETUP -#line 76 "xml_lexer.l" -{ add_xml_item(XML_DOCTYPE_STOP, yyleng); } +#line 85 "xml_lexer.l" +{ text_offset = offset - yyleng; add_xml_item(XML_DOCTYPE_STOP, yyleng); } YY_BREAK case 3: YY_RULE_SETUP -#line 78 "xml_lexer.l" -{ text_offset = offset - yyleng -1; BEGIN TEXT; } +#line 87 "xml_lexer.l" +{ text_offset = offset - yyleng; BEGIN TEXT; } YY_BREAK case 4: YY_RULE_SETUP -#line 79 "xml_lexer.l" -{ add_xml_item(XML_TEXT, --offset - text_offset -1 ); BEGIN OUT; } +#line 88 "xml_lexer.l" +{ add_xml_item(XML_TEXT, --offset - text_offset ); BEGIN OUT; } YY_BREAK case 5: YY_RULE_SETUP -#line 81 "xml_lexer.l" -{ text_offset = offset - yyleng -1; BEGIN COMMENT; } +#line 90 "xml_lexer.l" +{ text_offset = offset - yyleng; BEGIN COMMENT; } YY_BREAK case 6: YY_RULE_SETUP -#line 82 "xml_lexer.l" -{ add_xml_item(XML_COMMENT, offset - text_offset -1); BEGIN OUT; } +#line 91 "xml_lexer.l" +{ add_xml_item(XML_COMMENT, offset - text_offset); BEGIN OUT; } YY_BREAK case 7: YY_RULE_SETUP -#line 84 "xml_lexer.l" -{ text_offset = offset - yyleng -1; BEGIN CLOSE_TAG; } +#line 93 "xml_lexer.l" +{ text_offset = offset - yyleng ; BEGIN CLOSE_TAG; } YY_BREAK case 8: YY_RULE_SETUP -#line 85 "xml_lexer.l" -{ add_xml_item(XML_CLOSE_TAG, offset - text_offset -1); BEGIN OUT; } +#line 94 "xml_lexer.l" +{ add_xml_item(XML_CLOSE_TAG, offset - text_offset ); BEGIN OUT; } YY_BREAK case 9: YY_RULE_SETUP -#line 87 "xml_lexer.l" -{ text_offset = offset - yyleng -1; BEGIN XMLPI; } +#line 96 "xml_lexer.l" +{ text_offset = offset - yyleng; BEGIN XMLPI; } YY_BREAK case 10: YY_RULE_SETUP -#line 88 "xml_lexer.l" -{ add_xml_item(XML_XMLPI, offset - text_offset -1); BEGIN OUT; } +#line 97 "xml_lexer.l" +{ add_xml_item(XML_XMLPI, offset - text_offset); BEGIN OUT; } YY_BREAK case 11: YY_RULE_SETUP -#line 90 "xml_lexer.l" +#line 99 "xml_lexer.l" { text_offset = offset - yyleng -1; BEGIN MARKUPDECL; } YY_BREAK case 12: YY_RULE_SETUP -#line 91 "xml_lexer.l" +#line 100 "xml_lexer.l" { add_xml_item(XML_DOCTYPE_START, offset - text_offset); BEGIN OUT; } YY_BREAK case 13: YY_RULE_SETUP -#line 92 "xml_lexer.l" +#line 101 "xml_lexer.l" { add_xml_item(XML_MARKUPDECL, offset - text_offset); BEGIN OUT; } YY_BREAK case 14: YY_RULE_SETUP -#line 94 "xml_lexer.l" +#line 103 "xml_lexer.l" { text_offset = offset - yyleng -1; BEGIN TAG; } YY_BREAK case 15: YY_RULE_SETUP -#line 95 "xml_lexer.l" +#line 104 "xml_lexer.l" { add_xml_item(XML_CLOSEDTAG, offset - text_offset); BEGIN OUT; } YY_BREAK case 16: YY_RULE_SETUP -#line 96 "xml_lexer.l" +#line 105 "xml_lexer.l" { add_xml_item(XML_TAG, offset - text_offset); BEGIN OUT; } YY_BREAK case 17: YY_RULE_SETUP -#line 100 "xml_lexer.l" +#line 107 "xml_lexer.l" ECHO; YY_BREAK -#line 767 "xml_lexer.c" +#line 776 "xml_lexer.c" case YY_STATE_EOF(INITIAL): case YY_STATE_EOF(OUT): case YY_STATE_EOF(COMMENT): @@ -1657,34 +1666,37 @@ int main() return 0; } #endif -#line 100 "xml_lexer.l" - - -static void add_xml_item(xml_token_type_t type, gint the_len) { - xml_token_t* xi = g_malloc(sizeof(xml_token_t)); - - xi->type = type; - xi->offset = last_offset; - xi->len = the_len; - xi->next = NULL; - xi->prev = tail; - - last_offset += the_len; - - if (!head) head = xi; - - if (!tail) { - tail = xi; - } else { - tail->next = xi; - } - - tail = xi; +#line 107 "xml_lexer.l" + + +static void add_xml_item(xml_token_type_t type, guint the_len) { + switch (type) { + case XML_WHITESPACE: + break; + case XML_CLOSEDTAG: + case XML_TEXT: + case XML_MARKUPDECL: + case XML_XMLPI: + case XML_COMMENT: + pi = proto_tree_add_xml_item(tree,tvb,type,text_offset,the_len); + break; + case XML_DOCTYPE_START: + case XML_TAG: + pi = proto_tree_add_xml_item(tree,tvb,type,text_offset,the_len); + g_ptr_array_add(stack,tree); + tree = proto_item_add_subtree(pi, ett_xml); + break; + case XML_CLOSE_TAG: + case XML_DOCTYPE_STOP: + pi = proto_tree_add_xml_item(tree,tvb,type,text_offset,the_len); + if ( stack->len ) + tree = g_ptr_array_remove_index(stack,stack->len - 1); + break; + } } - - static int tvb_yyinput(char* buff, guint max_len _U_) { + if ( offset < len ) { buff[0] = extracted[offset]; offset++; @@ -1694,25 +1706,32 @@ static int tvb_yyinput(char* buff, guint max_len _U_) { } } -extern xml_token_t* scan_tvb_for_xml_items(tvbuff_t* tvb, gint the_offset, gint the_len) { - - offset = the_offset; - last_offset = the_offset; - len = the_len; - text_offset = offset; - extracted = tvb_memdup(tvb,offset,len); +extern void xml_lexer_init(int proto_hfid, int ett) { - head = NULL; - tail = NULL; - - BEGIN OUT; - - yylex(); - - yyrestart(NULL); - - g_free(extracted); - - return head; + proto_xml = proto_hfid; + ett_xml = ett; +}; + +extern void dissect_xml(tvbuff_t* the_tvb, packet_info* pinfo _U_, proto_tree* the_tree) { + + if (the_tree) { + + tree = the_tree; + tvb = the_tvb; + text_offset = offset = 0; + len = tvb_length(tvb); + extracted = tvb_memdup(tvb,offset,len); + stack = g_ptr_array_new(); + + pi = proto_tree_add_item(tree, proto_xml, tvb, 0, -1, FALSE); + tree = proto_item_add_subtree(pi, ett_xml); + + BEGIN OUT; + yylex(); + yyrestart(NULL); + + g_free(extracted); + g_ptr_array_free(stack,FALSE); + + } } - diff --git a/plugins/xml/xml_lexer.l b/plugins/xml/xml_lexer.l index 047eaa5f3f..d4e5859824 100644 --- a/plugins/xml/xml_lexer.l +++ b/plugins/xml/xml_lexer.l @@ -30,23 +30,32 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - -#include "packet-xml.h" +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif +#include "packet-xml.h" + static guint8* extracted = NULL; static gint offset; - static gint last_offset; static gint text_offset; static gint len; - xml_token_t* head; - xml_token_t* tail; -#define YY_INPUT(buff,result,max_size) ( (result) = tvb_yyinput((buff),(max_size)) ) + tvbuff_t* tvb; + proto_item* pi; + proto_tree* tree; + GPtrArray* stack; + static int ett_xml; + static int proto_xml; + +#define YY_INPUT(buff,result,max_size) \ + ( (result) = tvb_yyinput((buff),(max_size)) ) + #define ECHO ; - static void add_xml_item(xml_token_type_t type, gint len); - static int tvb_yyinput(char* buff, guint max_len); + static void add_xml_item(xml_token_type_t, guint); + static int tvb_yyinput(char*, guint); %} xmlpi_start "<?" @@ -72,58 +81,59 @@ text_stop < %START OUT COMMENT TAG CLOSE_TAG XMLPI DOCTYPE MARKUPDECL TEXT %% -<OUT>{whitespace} { add_xml_item(XML_WHITESPACE, yyleng); } -<OUT>{dtd_doctype_stop} { add_xml_item(XML_DOCTYPE_STOP, yyleng); } +<OUT>{whitespace} { text_offset = offset - yyleng; add_xml_item(XML_WHITESPACE, yyleng); } +<OUT>{dtd_doctype_stop} { text_offset = offset - yyleng; add_xml_item(XML_DOCTYPE_STOP, yyleng); } -<OUT>{text_start} { text_offset = offset - yyleng -1; BEGIN TEXT; } -<TEXT>{text_stop} { add_xml_item(XML_TEXT, --offset - text_offset -1 ); BEGIN OUT; } +<OUT>{text_start} { text_offset = offset - yyleng; BEGIN TEXT; } +<TEXT>{text_stop} { add_xml_item(XML_TEXT, --offset - text_offset ); BEGIN OUT; } -<OUT>{comment_start} { text_offset = offset - yyleng -1; BEGIN COMMENT; } -<COMMENT>{comment_end} { add_xml_item(XML_COMMENT, offset - text_offset -1); BEGIN OUT; } +<OUT>{comment_start} { text_offset = offset - yyleng; BEGIN COMMENT; } +<COMMENT>{comment_end} { add_xml_item(XML_COMMENT, offset - text_offset); BEGIN OUT; } -<OUT>{closetag_start} { text_offset = offset - yyleng -1; BEGIN CLOSE_TAG; } -<CLOSE_TAG>{tag_end} { add_xml_item(XML_CLOSE_TAG, offset - text_offset -1); BEGIN OUT; } +<OUT>{closetag_start} { text_offset = offset - yyleng ; BEGIN CLOSE_TAG; } +<CLOSE_TAG>{tag_end} { add_xml_item(XML_CLOSE_TAG, offset - text_offset ); BEGIN OUT; } -<OUT>{xmlpi_start} { text_offset = offset - yyleng -1; BEGIN XMLPI; } -<XMLPI>{xmlpi_end} { add_xml_item(XML_XMLPI, offset - text_offset -1); BEGIN OUT; } +<OUT>{xmlpi_start} { text_offset = offset - yyleng; BEGIN XMLPI; } +<XMLPI>{xmlpi_end} { add_xml_item(XML_XMLPI, offset - text_offset); BEGIN OUT; } <OUT>{markupdecl_start} { text_offset = offset - yyleng -1; BEGIN MARKUPDECL; } <MARKUPDECL>{open_brace} { add_xml_item(XML_DOCTYPE_START, offset - text_offset); BEGIN OUT; } <MARKUPDECL>{tag_end} { add_xml_item(XML_MARKUPDECL, offset - text_offset); BEGIN OUT; } -<OUT>{tag_start} { text_offset = offset - yyleng -1; BEGIN TAG; } -<TAG>{closedtag_end} { add_xml_item(XML_CLOSEDTAG, offset - text_offset); BEGIN OUT; } -<TAG>{tag_end} { add_xml_item(XML_TAG, offset - text_offset); BEGIN OUT; } - - +<OUT>{tag_start} { text_offset = offset - yyleng -1; BEGIN TAG; } +<TAG>{closedtag_end} { add_xml_item(XML_CLOSEDTAG, offset - text_offset); BEGIN OUT; } +<TAG>{tag_end} { add_xml_item(XML_TAG, offset - text_offset); BEGIN OUT; } %% -static void add_xml_item(xml_token_type_t type, gint the_len) { - xml_token_t* xi = g_malloc(sizeof(xml_token_t)); - - xi->type = type; - xi->offset = last_offset; - xi->len = the_len; - xi->next = NULL; - xi->prev = tail; - - last_offset += the_len; - - if (!head) head = xi; - - if (!tail) { - tail = xi; - } else { - tail->next = xi; - } - - tail = xi; +static void add_xml_item(xml_token_type_t type, guint the_len) { + switch (type) { + case XML_WHITESPACE: + break; + case XML_CLOSEDTAG: + case XML_TEXT: + case XML_MARKUPDECL: + case XML_XMLPI: + case XML_COMMENT: + pi = proto_tree_add_xml_item(tree,tvb,type,text_offset,the_len); + break; + case XML_DOCTYPE_START: + case XML_TAG: + pi = proto_tree_add_xml_item(tree,tvb,type,text_offset,the_len); + g_ptr_array_add(stack,tree); + tree = proto_item_add_subtree(pi, ett_xml); + break; + case XML_CLOSE_TAG: + case XML_DOCTYPE_STOP: + pi = proto_tree_add_xml_item(tree,tvb,type,text_offset,the_len); + if ( stack->len ) + tree = g_ptr_array_remove_index(stack,stack->len - 1); + break; + } } - - static int tvb_yyinput(char* buff, guint max_len _U_) { + if ( offset < len ) { buff[0] = extracted[offset]; offset++; @@ -133,25 +143,32 @@ static int tvb_yyinput(char* buff, guint max_len _U_) { } } -extern xml_token_t* scan_tvb_for_xml_items(tvbuff_t* tvb, gint the_offset, gint the_len) { - - offset = the_offset; - last_offset = the_offset; - len = the_len; - text_offset = offset; - extracted = tvb_memdup(tvb,offset,len); - - head = NULL; - tail = NULL; - - BEGIN OUT; - - yylex(); - - yyrestart(NULL); +extern void xml_lexer_init(int proto_hfid, int ett) { - g_free(extracted); - - return head; + proto_xml = proto_hfid; + ett_xml = ett; +}; + +extern void dissect_xml(tvbuff_t* the_tvb, packet_info* pinfo _U_, proto_tree* the_tree) { + + if (the_tree) { + + tree = the_tree; + tvb = the_tvb; + text_offset = offset = 0; + len = tvb_length(tvb); + extracted = tvb_memdup(tvb,offset,len); + stack = g_ptr_array_new(); + + pi = proto_tree_add_item(tree, proto_xml, tvb, 0, -1, FALSE); + tree = proto_item_add_subtree(pi, ett_xml); + + BEGIN OUT; + yylex(); + yyrestart(NULL); + + g_free(extracted); + g_ptr_array_free(stack,FALSE); + + } } -
\ No newline at end of file |