2 * wireshark's xml dissector .
4 * (C) 2005, Luis E. Garcia Ontanon.
8 * Refer to the AUTHORS file or the AUTHORS section in the man page
9 * for contacting the author(s) of this file.
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43 #include <wsutil/str_util.h>
45 #include <epan/emem.h>
46 #include <epan/packet.h>
47 #include <epan/tvbparse.h>
49 #include <epan/report_err.h>
50 #include <epan/filesystem.h>
51 #include <epan/prefs.h>
52 #include <epan/garrayfix.h>
54 #include "packet-xml.h"
56 struct _attr_reg_data {
62 static gint ett_dtd = -1;
63 static gint ett_xmpli = -1;
65 static int hf_unknowwn_attrib = -1;
66 static int hf_comment = -1;
67 static int hf_xmlpi = -1;
68 static int hf_dtd_tag = -1;
69 static int hf_doctype = -1;
71 /* dissector handles */
72 static dissector_handle_t xml_handle;
74 /* parser definitions */
75 static tvbparse_wanted_t* want;
76 static tvbparse_wanted_t* want_ignore;
77 static tvbparse_wanted_t* want_heur;
79 static GHashTable* xmpli_names;
80 static GHashTable* media_types;
82 static xml_ns_t xml_ns = {"xml","/",-1,-1,-1,NULL,NULL,NULL};
83 static xml_ns_t unknown_ns = {"unknown","?",-1,-1,-1,NULL,NULL,NULL};
84 static xml_ns_t* root_ns;
86 static gboolean pref_heuristic_media = FALSE;
87 static gboolean pref_heuristic_tcp = FALSE;
88 static gboolean pref_heuristic_udp = FALSE;
89 static gboolean pref_heuristic_media_save = FALSE;
90 static gboolean pref_heuristic_tcp_save = FALSE;
91 static gboolean pref_heuristic_udp_save = FALSE;
92 static range_t *global_xml_tcp_range = NULL;
93 static range_t *xml_tcp_range = NULL;
95 static gboolean pref_heuristic_unicode = FALSE;
97 #define XML_CDATA -1000
98 #define XML_SCOPED_NAME -1001
101 static GArray* hf_arr;
102 static GArray* ett_arr;
104 static const gchar* default_media_types[] = {
111 "application/auth-policy+xml",
112 "application/cpim-pidf+xml",
113 "application/cpl+xml",
114 "application/mathml+xml",
115 "application/media_control+xml",
116 "application/note+xml",
117 "application/pidf+xml",
118 "application/poc-settings+xml",
119 "application/rdf+xml",
120 "application/reginfo+xml",
121 "application/resource-lists+xml",
122 "application/rlmi+xml",
123 "application/rls-services+xml",
125 "application/simple-filter+xml",
126 "application/soap+xml",
127 "application/vnd.etsi.aoc+xml",
128 "application/vnd.etsi.cug+xml",
129 "application/vnd.etsi.iptvcommand+xml",
130 "application/vnd.etsi.iptvdiscovery+xml",
131 "application/vnd.etsi.iptvprofile+xml",
132 "application/vnd.etsi.iptvsad-bc+xml",
133 "application/vnd.etsi.iptvsad-cod+xml",
134 "application/vnd.etsi.iptvsad-npvr+xml",
135 "application/vnd.etsi.iptvueprofile+xml",
136 "application/vnd.etsi.mcid+xml",
137 "application/vnd.etsi.sci+xml",
138 "application/vnd.etsi.simservs+xml",
139 "application/vnd.wv.csp+xml",
140 "application/vnd.wv.csp.xml",
141 "application/watcherinfo+xml",
142 "application/xcap-att+xml",
143 "application/xcap-caps+xml",
144 "application/xcap-diff+xml",
145 "application/xcap-el+xml",
146 "application/xcap-error+xml",
147 "application/xcap-ns+xml",
149 "application/xml-dtd",
150 "application/xpidf+xml",
151 "application/xslt+xml",
152 "application/x-wms-logconnectstats",
153 "application/x-wms-logplaystats",
154 "application/x-wms-sendevent",
155 "application/rss+xml",
159 static void insert_xml_frame(xml_frame_t *parent, xml_frame_t *new_child) {
160 new_child->first_child = NULL;
161 new_child->last_child = NULL;
163 new_child->parent = parent;
164 new_child->next_sibling = NULL;
165 new_child->prev_sibling = NULL;
166 if (parent == NULL) return; /* root */
168 if (parent->first_child == NULL) { /* the 1st child */
169 parent->first_child = new_child;
170 } else { /* following children */
171 parent->last_child->next_sibling = new_child;
172 new_child->prev_sibling = parent->last_child;
174 parent->last_child = new_child;
178 dissect_xml(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
181 tvbparse_elem_t* tok = NULL;
182 static GPtrArray* stack = NULL;
183 xml_frame_t* current_frame;
187 g_ptr_array_free(stack,TRUE);
189 stack = g_ptr_array_new();
190 current_frame = ep_alloc(sizeof(xml_frame_t));
191 current_frame->type = XML_FRAME_ROOT;
192 current_frame->name = NULL;
193 current_frame->name_orig_case = NULL;
194 current_frame->value = NULL;
195 insert_xml_frame(NULL, current_frame);
196 g_ptr_array_add(stack,current_frame);
198 tt = tvbparse_init(tvb,0,-1,stack,want_ignore);
199 current_frame->start_offset = 0;
203 if (pinfo->match_string)
204 root_ns = g_hash_table_lookup(media_types,pinfo->match_string);
208 colinfo_str = "/XML";
210 colinfo_str = ep_strdup_printf("/%s",root_ns->name);
211 ascii_strup_inplace(colinfo_str);
214 col_append_str(pinfo->cinfo, COL_PROTOCOL, colinfo_str);
216 current_frame->ns = root_ns;
218 current_frame->item = proto_tree_add_item(tree,current_frame->ns->hf_tag,tvb,0,-1,FALSE);
219 current_frame->tree = proto_item_add_subtree(current_frame->item,current_frame->ns->ett);
220 current_frame->last_item = current_frame->item;
222 while(( tok = tvbparse_get(tt, want) )) ;
224 pinfo->private_data = current_frame; /* pass XML structure to the dissector calling XML */
227 static gboolean dissect_xml_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
228 if (pref_heuristic_media || pref_heuristic_tcp || pref_heuristic_udp) {
229 if (tvbparse_peek(tvbparse_init(tvb,0,-1,NULL,want_ignore), want_heur)) {
230 dissect_xml(tvb, pinfo, tree);
232 } else if (pref_heuristic_unicode) {
233 const guint8 *data = tvb_get_ephemeral_faked_unicode(tvb, 0, tvb_length(tvb)/2, TRUE);
234 tvbuff_t *unicode_tvb = tvb_new_real_data(data, tvb_length(tvb)/2, tvb_length(tvb)/2);
235 if (tvbparse_peek(tvbparse_init(unicode_tvb,0,-1,NULL,want_ignore), want_heur)) {
236 dissect_xml(unicode_tvb, pinfo, tree);
244 xml_frame_t *xml_get_tag(xml_frame_t *frame, const gchar *name) {
245 xml_frame_t *tag = NULL;
247 xml_frame_t *xml_item = frame->first_child;
249 if ((xml_item->type == XML_FRAME_TAG)) {
250 if (!name) { /* get the 1st tag */
253 } else if (xml_item->name_orig_case && !strcmp(xml_item->name_orig_case, name)) {
258 xml_item = xml_item->next_sibling;
264 xml_frame_t *xml_get_attrib(xml_frame_t *frame, const gchar *name) {
265 xml_frame_t *attr = NULL;
267 xml_frame_t *xml_item = frame->first_child;
269 if ((xml_item->type == XML_FRAME_ATTRIB) &&
270 xml_item->name_orig_case && !strcmp(xml_item->name_orig_case, name)) {
274 xml_item = xml_item->next_sibling;
280 xml_frame_t *xml_get_cdata(xml_frame_t *frame) {
281 xml_frame_t *cdata = NULL;
283 xml_frame_t *xml_item = frame->first_child;
285 if ((xml_item->type == XML_FRAME_CDATA)) {
289 xml_item = xml_item->next_sibling;
295 static void after_token(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok) {
296 GPtrArray* stack = tvbparse_data;
297 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
299 gboolean is_cdata = FALSE;
301 xml_frame_t* new_frame;
303 if (tok->id == XML_CDATA) {
304 hfid = current_frame->ns ? current_frame->ns->hf_cdata : xml_ns.hf_cdata;
306 } else if ( tok->id > 0) {
309 hfid = xml_ns.hf_cdata;
312 pi = proto_tree_add_item(current_frame->tree, hfid, tok->tvb, tok->offset, tok->len, FALSE);
314 proto_item_set_text(pi, "%s",
315 tvb_format_text(tok->tvb,tok->offset,tok->len));
318 new_frame = ep_alloc(sizeof(xml_frame_t));
319 new_frame->type = XML_FRAME_CDATA;
320 new_frame->name = NULL;
321 new_frame->name_orig_case = NULL;
322 new_frame->value = tvb_new_subset(tok->tvb, tok->offset, tok->len, tok->len);
323 insert_xml_frame(current_frame, new_frame);
324 new_frame->item = pi;
325 new_frame->last_item = pi;
326 new_frame->tree = NULL;
327 new_frame->start_offset = tok->offset;
328 new_frame->ns = NULL;
332 static void before_xmpli(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok) {
333 GPtrArray* stack = tvbparse_data;
334 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
337 tvbparse_elem_t* name_tok = tok->sub->next;
338 gchar* name = tvb_get_ephemeral_string(name_tok->tvb,name_tok->offset,name_tok->len);
339 xml_ns_t* ns = g_hash_table_lookup(xmpli_names,name);
340 xml_frame_t* new_frame;
345 ascii_strdown_inplace(name);
354 pi = proto_tree_add_item(current_frame->tree,hf_tag,tok->tvb,tok->offset,tok->len,FALSE);
356 proto_item_set_text(pi, "%s", tvb_format_text(tok->tvb,tok->offset,(name_tok->offset - tok->offset) + name_tok->len));
358 pt = proto_item_add_subtree(pi,ett);
360 new_frame = ep_alloc(sizeof(xml_frame_t));
361 new_frame->type = XML_FRAME_XMPLI;
362 new_frame->name = name;
363 new_frame->name_orig_case = name;
364 new_frame->value = NULL;
365 insert_xml_frame(current_frame, new_frame);
366 new_frame->item = pi;
367 new_frame->last_item = pi;
368 new_frame->tree = pt;
369 new_frame->start_offset = tok->offset;
372 g_ptr_array_add(stack,new_frame);
376 static void after_xmlpi(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok) {
377 GPtrArray* stack = tvbparse_data;
378 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
380 proto_tree_add_text(current_frame->tree,
381 tok->tvb, tok->offset, tok->len, "%s",
382 tvb_format_text(tok->tvb,tok->offset,tok->len));
384 if (stack->len > 1) {
385 g_ptr_array_remove_index_fast(stack,stack->len - 1);
387 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"[ ERROR: Closing an unopened xmpli tag ]");
391 static void before_tag(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok) {
392 GPtrArray* stack = tvbparse_data;
393 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
394 tvbparse_elem_t* name_tok = tok->sub->next;
396 gchar *name = NULL, *name_orig_case = NULL;
398 xml_frame_t* new_frame;
402 if (name_tok->sub->id == XML_SCOPED_NAME) {
403 tvbparse_elem_t* root_tok = name_tok->sub->sub;
404 tvbparse_elem_t* leaf_tok = name_tok->sub->sub->next->next;
405 xml_ns_t* nameroot_ns;
407 root_name = (gchar*)tvb_get_ephemeral_string(root_tok->tvb,root_tok->offset,root_tok->len);
408 name = (gchar*)tvb_get_ephemeral_string(leaf_tok->tvb,leaf_tok->offset,leaf_tok->len);
409 name_orig_case = name;
411 nameroot_ns = g_hash_table_lookup(xml_ns.elements,root_name);
414 ns = g_hash_table_lookup(nameroot_ns->elements,name);
423 name = tvb_get_ephemeral_string(name_tok->tvb,name_tok->offset,name_tok->len);
424 name_orig_case = ep_strdup(name);
425 ascii_strdown_inplace(name);
427 if(current_frame->ns) {
428 ns = g_hash_table_lookup(current_frame->ns->elements,name);
431 if (! ( ns = g_hash_table_lookup(root_ns->elements,name) ) ) {
440 pi = proto_tree_add_item(current_frame->tree,ns->hf_tag,tok->tvb,tok->offset,tok->len,FALSE);
441 proto_item_set_text(pi, "%s", tvb_format_text(tok->tvb,tok->offset,(name_tok->offset - tok->offset) + name_tok->len));
443 pt = proto_item_add_subtree(pi,ns->ett);
445 new_frame = ep_alloc(sizeof(xml_frame_t));
446 new_frame->type = XML_FRAME_TAG;
447 new_frame->name = name;
448 new_frame->name_orig_case = name_orig_case;
449 new_frame->value = NULL;
450 insert_xml_frame(current_frame, new_frame);
451 new_frame->item = pi;
452 new_frame->last_item = pi;
453 new_frame->tree = pt;
454 new_frame->start_offset = tok->offset;
457 g_ptr_array_add(stack,new_frame);
461 static void after_open_tag(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok _U_) {
462 GPtrArray* stack = tvbparse_data;
463 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
465 proto_item_append_text(current_frame->last_item,">");
468 static void after_closed_tag(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok) {
469 GPtrArray* stack = tvbparse_data;
470 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
472 proto_item_append_text(current_frame->last_item,"/>");
474 if (stack->len > 1) {
475 g_ptr_array_remove_index_fast(stack,stack->len - 1);
477 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"[ ERROR: Closing an unopened tag ]");
481 static void after_untag(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok){
482 GPtrArray* stack = tvbparse_data;
483 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
485 proto_item_set_len(current_frame->item, (tok->offset - current_frame->start_offset) + tok->len);
487 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"%s",
488 tvb_format_text(tok->tvb,tok->offset,tok->len));
490 if (stack->len > 1) {
491 g_ptr_array_remove_index_fast(stack,stack->len - 1);
493 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,
494 "[ ERROR: Closing an unopened tag ]");
498 static void before_dtd_doctype(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok){
499 GPtrArray* stack = tvbparse_data;
500 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
501 xml_frame_t* new_frame;
502 tvbparse_elem_t* name_tok = tok->sub->next->next->next->sub->sub;
503 proto_tree* dtd_item = proto_tree_add_item(current_frame->tree, hf_doctype,
504 name_tok->tvb, name_tok->offset, name_tok->len, FALSE);
506 proto_item_set_text(dtd_item,"%s",tvb_format_text(tok->tvb,tok->offset,tok->len));
508 new_frame = ep_alloc(sizeof(xml_frame_t));
509 new_frame->type = XML_FRAME_DTD_DOCTYPE;
510 new_frame->name = (gchar*)tvb_get_ephemeral_string(name_tok->tvb,name_tok->offset,name_tok->len);
511 new_frame->name_orig_case = new_frame->name;
512 new_frame->value = NULL;
513 insert_xml_frame(current_frame, new_frame);
514 new_frame->item = dtd_item;
515 new_frame->last_item = dtd_item;
516 new_frame->tree = proto_item_add_subtree(dtd_item,ett_dtd);
517 new_frame->start_offset = tok->offset;
518 new_frame->ns = NULL;
520 g_ptr_array_add(stack,new_frame);
523 static void pop_stack(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok _U_) {
524 GPtrArray* stack = tvbparse_data;
525 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
527 if (stack->len > 1) {
528 g_ptr_array_remove_index_fast(stack,stack->len - 1);
530 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,
531 "[ ERROR: Closing an unopened tag ]");
535 static void after_dtd_close(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok){
536 GPtrArray* stack = tvbparse_data;
537 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
539 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"%s",
540 tvb_format_text(tok->tvb,tok->offset,tok->len));
541 if (stack->len > 1) {
542 g_ptr_array_remove_index_fast(stack,stack->len - 1);
544 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"[ ERROR: Closing an unopened tag ]");
548 static void get_attrib_value(void* tvbparse_data _U_, const void* wanted_data _U_, tvbparse_elem_t* tok) {
549 tok->data = tok->sub;
552 static void after_attrib(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok) {
553 GPtrArray* stack = tvbparse_data;
554 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
555 gchar *name = NULL, *name_orig_case = NULL;
556 tvbparse_elem_t* value;
557 tvbparse_elem_t* value_part = tok->sub->next->next->data;
561 xml_frame_t* new_frame;
563 name = tvb_get_ephemeral_string(tok->sub->tvb,tok->sub->offset,tok->sub->len);
564 name_orig_case = ep_strdup(name);
565 ascii_strdown_inplace(name);
567 if(current_frame->ns && (hfidp = g_hash_table_lookup(current_frame->ns->attributes,name) )) {
571 hfid = hf_unknowwn_attrib;
575 pi = proto_tree_add_item(current_frame->tree,hfid,value->tvb,value->offset,value->len,FALSE);
576 proto_item_set_text(pi, "%s", tvb_format_text(tok->tvb,tok->offset,tok->len));
578 current_frame->last_item = pi;
580 new_frame = ep_alloc(sizeof(xml_frame_t));
581 new_frame->type = XML_FRAME_ATTRIB;
582 new_frame->name = name;
583 new_frame->name_orig_case = name_orig_case;
584 new_frame->value = tvb_new_subset(value_part->tvb, value_part->offset, value_part->len, value_part->len);
585 insert_xml_frame(current_frame, new_frame);
586 new_frame->item = pi;
587 new_frame->last_item = pi;
588 new_frame->tree = NULL;
589 new_frame->start_offset = tok->offset;
590 new_frame->ns = NULL;
594 static void unrecognized_token(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok _U_){
595 GPtrArray* stack = tvbparse_data;
596 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
598 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"[ ERROR: Unrecognized text ]");
604 static void init_xml_parser(void) {
605 tvbparse_wanted_t* want_name = tvbparse_chars(-1,1,0,"abcdefghijklmnopqrstuvwxyz.-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",NULL,NULL,NULL);
606 tvbparse_wanted_t* want_attr_name = tvbparse_chars(-1,1,0,"abcdefghijklmnopqrstuvwxyz.-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:",NULL,NULL,NULL);
608 tvbparse_wanted_t* want_scoped_name = tvbparse_set_seq(XML_SCOPED_NAME, NULL, NULL, NULL,
610 tvbparse_char(-1,":",NULL,NULL,NULL),
614 tvbparse_wanted_t* want_tag_name = tvbparse_set_oneof(0, NULL, NULL, NULL,
619 tvbparse_wanted_t* want_attrib_value = tvbparse_set_oneof(0, NULL, NULL, get_attrib_value,
620 tvbparse_quoted(-1, NULL, NULL, tvbparse_shrink_token_cb,'\"','\\'),
621 tvbparse_quoted(-1, NULL, NULL, tvbparse_shrink_token_cb,'\'','\\'),
622 tvbparse_chars(-1,1,0,"0123456789",NULL,NULL,NULL),
626 tvbparse_wanted_t* want_attributes = tvbparse_one_or_more(-1, NULL, NULL, NULL,
627 tvbparse_set_seq(-1, NULL, NULL, after_attrib,
629 tvbparse_char(-1,"=",NULL,NULL,NULL),
633 tvbparse_wanted_t* want_stoptag = tvbparse_set_oneof(-1,NULL,NULL,NULL,
634 tvbparse_char(-1, ">", NULL, NULL, after_open_tag),
635 tvbparse_string(-1, "/>", NULL, NULL, after_closed_tag),
638 tvbparse_wanted_t* want_stopxmlpi = tvbparse_string(-1,"?>",NULL,NULL,after_xmlpi);
640 tvbparse_wanted_t* want_comment = tvbparse_set_seq(hf_comment,NULL,NULL,after_token,
641 tvbparse_string(-1,"<!--",NULL,NULL,NULL),
642 tvbparse_until(-1,NULL,NULL,NULL,
643 tvbparse_string(-1,"-->",NULL,NULL,NULL),
647 tvbparse_wanted_t* want_xmlpi = tvbparse_set_seq(hf_xmlpi,NULL,before_xmpli,NULL,
648 tvbparse_string(-1,"<?",NULL,NULL,NULL),
650 tvbparse_set_oneof(-1,NULL,NULL,NULL,
652 tvbparse_set_seq(-1,NULL,NULL,NULL,
659 tvbparse_wanted_t* want_closing_tag = tvbparse_set_seq(0,NULL,NULL,after_untag,
660 tvbparse_char(-1, "<", NULL, NULL, NULL),
661 tvbparse_char(-1, "/", NULL, NULL, NULL),
663 tvbparse_char(-1, ">", NULL, NULL, NULL),
666 tvbparse_wanted_t* want_doctype_start = tvbparse_set_seq(-1,NULL,before_dtd_doctype,NULL,
667 tvbparse_char(-1,"<",NULL,NULL,NULL),
668 tvbparse_char(-1,"!",NULL,NULL,NULL),
669 tvbparse_casestring(-1,"DOCTYPE",NULL,NULL,NULL),
670 tvbparse_set_oneof(-1,NULL,NULL,NULL,
671 tvbparse_set_seq(-1,NULL,NULL,NULL,
673 tvbparse_char(-1,"[",NULL,NULL,NULL),
675 tvbparse_set_seq(-1,NULL,NULL,pop_stack,
677 tvbparse_set_oneof(-1,NULL,NULL,NULL,
678 tvbparse_casestring(-1,"PUBLIC",NULL,NULL,NULL),
679 tvbparse_casestring(-1,"SYSTEM",NULL,NULL,NULL),
681 tvbparse_until(-1,NULL,NULL,NULL,
682 tvbparse_char(-1,">",NULL,NULL,NULL),
688 tvbparse_wanted_t* want_dtd_tag = tvbparse_set_seq(hf_dtd_tag,NULL,NULL,after_token,
689 tvbparse_char(-1,"<",NULL,NULL,NULL),
690 tvbparse_char(-1,"!",NULL,NULL,NULL),
691 tvbparse_until(-1,NULL,NULL,NULL,
692 tvbparse_char(-1, ">", NULL, NULL, NULL),
696 tvbparse_wanted_t* want_tag = tvbparse_set_seq(-1, NULL, before_tag, NULL,
697 tvbparse_char(-1,"<",NULL,NULL,NULL),
699 tvbparse_set_oneof(-1,NULL,NULL,NULL,
700 tvbparse_set_seq(-1,NULL,NULL,NULL,
708 tvbparse_wanted_t* want_dtd_close = tvbparse_set_seq(-1,NULL,NULL,after_dtd_close,
709 tvbparse_char(-1,"]",NULL,NULL,NULL),
710 tvbparse_char(-1,">",NULL,NULL,NULL),
713 want_ignore = tvbparse_chars(-1,1,0," \t\r\n",NULL,NULL,NULL);
716 want = tvbparse_set_oneof(-1, NULL, NULL, NULL,
724 tvbparse_not_chars(XML_CDATA,1,0,"<",NULL,NULL,after_token),
725 tvbparse_not_chars(-1,1,0," \t\r\n",NULL,NULL,unrecognized_token),
728 want_heur = tvbparse_set_oneof(-1, NULL, NULL, NULL,
739 static xml_ns_t* xml_new_namespace(GHashTable* hash, gchar* name, ...) {
740 xml_ns_t* ns = g_malloc(sizeof(xml_ns_t));
744 ns->name = g_strdup(name);
748 ns->attributes = g_hash_table_new(g_str_hash,g_str_equal);
749 ns->elements = g_hash_table_new(g_str_hash,g_str_equal);
753 while(( attr_name = va_arg(ap,gchar*) )) {
754 int* hfp = g_malloc(sizeof(int));
756 g_hash_table_insert(ns->attributes,g_strdup(attr_name),hfp);
761 g_hash_table_insert(hash,ns->name,ns);
767 static void add_xml_field(GArray* hfs, int* p_id, gchar* name, gchar* fqn) {
768 hf_register_info hfri;
771 hfri.hfinfo.name = name;
772 hfri.hfinfo.abbrev = fqn;
773 hfri.hfinfo.type = FT_STRING;
774 hfri.hfinfo.display = BASE_NONE;
775 hfri.hfinfo.strings = NULL;
776 hfri.hfinfo.bitmask = 0x0;
777 hfri.hfinfo.blurb = NULL;
779 hfri.hfinfo.parent = 0;
780 hfri.hfinfo.ref_type = HF_REF_TYPE_NONE;
781 hfri.hfinfo.bitshift = 0;
782 hfri.hfinfo.same_name_next = NULL;
783 hfri.hfinfo.same_name_prev = NULL;
785 g_array_append_val(hfs,hfri);
788 static void add_xml_attribute_names(gpointer k, gpointer v, gpointer p) {
789 struct _attr_reg_data* d = p;
790 gchar* basename = g_strdup_printf("%s.%s",d->basename,(gchar*)k);
791 add_xml_field(d->hf, (int*) v, (gchar*)k, basename);
795 static void add_xmlpi_namespace(gpointer k _U_, gpointer v, gpointer p) {
797 gchar* basename = g_strdup_printf("%s.%s",(gchar*)p,ns->name);
798 gint* ett_p = &(ns->ett);
799 struct _attr_reg_data d;
801 add_xml_field(hf_arr, &(ns->hf_tag), basename, basename);
803 g_array_append_val(ett_arr,ett_p);
805 d.basename = basename;
808 g_hash_table_foreach(ns->attributes,add_xml_attribute_names,&d);
812 static void destroy_dtd_data(dtd_build_data_t* dtd_data) {
813 g_free(dtd_data->proto_name);
814 g_free(dtd_data->media_type);
815 g_free(dtd_data->description);
816 g_free(dtd_data->proto_root);
818 g_string_free(dtd_data->error,TRUE);
820 while(dtd_data->elements->len) {
821 dtd_named_list_t* nl = g_ptr_array_remove_index_fast(dtd_data->elements,0);
822 g_ptr_array_free(nl->list,TRUE);
826 g_ptr_array_free(dtd_data->elements,TRUE);
828 while(dtd_data->attributes->len) {
829 dtd_named_list_t* nl = g_ptr_array_remove_index_fast(dtd_data->attributes,0);
830 g_ptr_array_free(nl->list,TRUE);
834 g_ptr_array_free(dtd_data->attributes,TRUE);
839 static void copy_attrib_item(gpointer k, gpointer v _U_, gpointer p) {
840 gchar* key = g_strdup(k);
841 int* value = g_malloc(sizeof(int));
845 g_hash_table_insert(dst,key,value);
849 static GHashTable* copy_attributes_hash(GHashTable* src) {
850 GHashTable* dst = g_hash_table_new(g_str_hash,g_str_equal);
852 g_hash_table_foreach(src,copy_attrib_item,dst);
857 static xml_ns_t* duplicate_element(xml_ns_t* orig) {
858 xml_ns_t* new_item = g_malloc(sizeof(xml_ns_t));
861 new_item->name = g_strdup(orig->name);
862 new_item->hf_tag = -1;
863 new_item->hf_cdata = -1;
865 new_item->attributes = copy_attributes_hash(orig->attributes);
866 new_item->elements = g_hash_table_new(g_str_hash,g_str_equal);
867 new_item->element_names = g_ptr_array_new();
869 for(i=0; i < orig->element_names->len; i++) {
870 g_ptr_array_add(new_item->element_names,
871 g_ptr_array_index(orig->element_names,i));
877 static gchar* fully_qualified_name(GPtrArray* hier, gchar* name, gchar* proto_name) {
879 GString* s = g_string_new(proto_name);
881 g_string_append(s,".");
883 for (i = 1; i < hier->len; i++) {
884 g_string_append_printf(s, "%s.",(gchar*)g_ptr_array_index(hier,i));
887 g_string_append(s,name);
889 g_string_free(s,FALSE);
895 static xml_ns_t* make_xml_hier(gchar* elem_name,
897 GHashTable* elements,
907 struct _attr_reg_data d;
908 gboolean recurred = FALSE;
911 if ( g_str_equal(elem_name,root->name) ) {
915 if (! ( orig = g_hash_table_lookup(elements,elem_name) )) {
916 g_string_append_printf(error,"element '%s' is not defined\n", elem_name);
920 for (i = 0; i < hier->len; i++) {
921 if( strcmp(elem_name,(gchar*) g_ptr_array_index(hier,i) ) == 0 ) {
930 fqn = fully_qualified_name(hier,elem_name,proto_name);
932 new = duplicate_element(orig);
935 add_xml_field(hfs, &(new->hf_tag), g_strdup(elem_name), fqn);
936 add_xml_field(hfs, &(new->hf_cdata), g_strdup(elem_name), fqn);
939 g_array_append_val(etts,ett_p);
944 g_hash_table_foreach(new->attributes,add_xml_attribute_names,&d);
946 while(new->element_names->len) {
947 gchar* child_name = g_ptr_array_remove_index(new->element_names,0);
948 xml_ns_t* child_element = NULL;
950 g_ptr_array_add(hier,elem_name);
951 child_element = make_xml_hier(child_name, root, elements, hier,error,hfs,etts,proto_name);
952 g_ptr_array_remove_index_fast(hier,hier->len - 1);
955 g_hash_table_insert(new->elements,child_element->name,child_element);
959 g_ptr_array_free(new->element_names,TRUE);
960 new->element_names = NULL;
964 static gboolean free_both(gpointer k, gpointer v, gpointer p _U_) {
970 static gboolean free_elements(gpointer k _U_, gpointer v, gpointer p _U_) {
973 g_hash_table_foreach_remove(e->attributes,free_both,NULL);
974 g_hash_table_destroy(e->attributes);
975 g_hash_table_destroy(e->elements);
977 while (e->element_names->len) {
978 g_free(g_ptr_array_remove_index(e->element_names,0));
981 g_ptr_array_free(e->element_names,TRUE);
987 static void register_dtd(dtd_build_data_t* dtd_data, GString* errors) {
988 GHashTable* elements = g_hash_table_new(g_str_hash,g_str_equal);
989 gchar* root_name = NULL;
990 xml_ns_t* root_element = NULL;
995 GPtrArray* element_names = g_ptr_array_new();
997 /* we first populate elements with the those coming from the parser */
998 while(dtd_data->elements->len) {
999 dtd_named_list_t* nl = g_ptr_array_remove_index(dtd_data->elements,0);
1000 xml_ns_t* element = g_malloc(sizeof(xml_ns_t));
1002 /* we will use the first element found as root in case no other one was given. */
1003 if (root_name == NULL)
1004 root_name = g_strdup(nl->name);
1006 element->name = nl->name;
1007 element->element_names = nl->list;
1008 element->hf_tag = -1;
1009 element->hf_cdata = -1;
1011 element->attributes = g_hash_table_new(g_str_hash,g_str_equal);
1012 element->elements = g_hash_table_new(g_str_hash,g_str_equal);
1014 if( g_hash_table_lookup(elements,element->name) ) {
1015 g_string_append_printf(errors,"element %s defined more than once\n", element->name);
1016 free_elements(NULL,element,NULL);
1018 g_hash_table_insert(elements,element->name,element);
1019 g_ptr_array_add(element_names,g_strdup(element->name));
1025 /* then we add the attributes to its relative elements */
1026 while(dtd_data->attributes->len) {
1027 dtd_named_list_t* nl = g_ptr_array_remove_index(dtd_data->attributes,0);
1028 xml_ns_t* element = g_hash_table_lookup(elements,nl->name);
1031 while(nl->list->len) {
1032 gchar* name = g_ptr_array_remove_index(nl->list,0);
1033 int* id_p = g_malloc(sizeof(int));
1036 g_hash_table_insert(element->attributes,name,id_p);
1040 g_string_append_printf(errors,"element %s is not defined\n", nl->name);
1044 g_ptr_array_free(nl->list,TRUE);
1048 /* if a proto_root is defined in the dtd we'll use that as root */
1049 if( dtd_data->proto_root ) {
1051 root_name = g_strdup(dtd_data->proto_root);
1054 /* we use a stack with the names to avoid recurring infinitelly */
1055 hier = g_ptr_array_new();
1058 * if a proto name was given in the dtd the dtd will be used as a protocol
1059 * or else the dtd will be loaded as a branch of the xml namespace
1061 if( ! dtd_data->proto_name ) {
1064 g_ptr_array_add(hier,g_strdup("xml"));
1065 root_element = &xml_ns;
1068 * if we were given a proto_name the namespace will be registered
1069 * as an independent protocol with its own hf and ett arrays.
1071 hfs = g_array_new(FALSE,FALSE,sizeof(hf_register_info));
1072 etts = g_array_new(FALSE,FALSE,sizeof(gint*));
1075 /* the root element of the dtd's namespace */
1076 root_element = g_malloc(sizeof(xml_ns_t));
1077 root_element->name = g_strdup(root_name);
1078 root_element->fqn = dtd_data->proto_name ? g_strdup(dtd_data->proto_name) : root_element->name;
1079 root_element->hf_tag = -1;
1080 root_element->hf_cdata = -1;
1081 root_element->ett = -1;
1082 root_element->elements = g_hash_table_new(g_str_hash,g_str_equal);
1083 root_element->element_names = element_names;
1086 * we can either create a namespace as a flat namespace
1087 * in which all the elements are at the root level
1088 * or we can create a recursive namespace
1090 if (dtd_data->recursion) {
1091 xml_ns_t* orig_root;
1093 make_xml_hier(root_name, root_element, elements,hier,errors,hfs,etts,dtd_data->proto_name);
1095 g_hash_table_insert(root_element->elements,root_element->name,root_element);
1097 orig_root = g_hash_table_lookup(elements,root_name);
1099 /* if the root element was defined copy its attrlist to the child */
1101 struct _attr_reg_data d;
1103 d.basename = dtd_data->proto_name;
1106 root_element->attributes = copy_attributes_hash(orig_root->attributes);
1107 g_hash_table_foreach(root_element->attributes,add_xml_attribute_names,&d);
1109 root_element->attributes = g_hash_table_new(g_str_hash,g_str_equal);
1112 /* we then create all the sub hierachies to catch the recurred cases */
1113 g_ptr_array_add(hier,root_name);
1115 while(root_element->element_names->len) {
1116 curr_name = g_ptr_array_remove_index(root_element->element_names,0);
1118 if( ! g_hash_table_lookup(root_element->elements,curr_name) ) {
1119 xml_ns_t* new = make_xml_hier(curr_name, root_element, elements,hier,errors,hfs,etts,dtd_data->proto_name);
1120 g_hash_table_insert(root_element->elements,new->name,new);
1127 /* a flat namespace */
1128 g_ptr_array_add(hier,root_name);
1130 root_element->attributes = g_hash_table_new(g_str_hash,g_str_equal);
1132 while(root_element->element_names->len) {
1135 struct _attr_reg_data d;
1137 curr_name = g_ptr_array_remove_index(root_element->element_names,0);
1138 new = duplicate_element(g_hash_table_lookup(elements,curr_name));
1139 new->fqn = fully_qualified_name(hier, curr_name, root_name);
1141 add_xml_field(hfs, &(new->hf_tag), curr_name, new->fqn);
1142 add_xml_field(hfs, &(new->hf_cdata), curr_name, new->fqn);
1144 d.basename = new->fqn;
1147 g_hash_table_foreach(new->attributes,add_xml_attribute_names,&d);
1150 g_array_append_val(etts,ett_p);
1152 g_ptr_array_free(new->element_names,TRUE);
1154 g_hash_table_insert(root_element->elements,new->name,new);
1158 g_ptr_array_free(element_names,TRUE);
1160 g_ptr_array_free(hier,TRUE);
1163 * if we were given a proto_name the namespace will be registered
1164 * as an independent protocol.
1166 if( dtd_data->proto_name ) {
1169 if ( ! dtd_data->description) {
1170 dtd_data->description = g_strdup(root_name);
1173 ett_p = &root_element->ett;
1174 g_array_append_val(etts,ett_p);
1176 add_xml_field(hfs, &root_element->hf_cdata, root_element->name, root_element->fqn);
1178 root_element->hf_tag = proto_register_protocol(dtd_data->description, dtd_data->proto_name, dtd_data->proto_name);
1179 proto_register_field_array(root_element->hf_tag, (hf_register_info*)g_array_data(hfs), hfs->len);
1180 proto_register_subtree_array((gint**)g_array_data(etts), etts->len);
1182 if (dtd_data->media_type) {
1183 g_hash_table_insert(media_types,dtd_data->media_type,root_element);
1184 dtd_data->media_type = NULL;
1187 dtd_data->description = NULL;
1188 dtd_data->proto_name = NULL;
1189 g_array_free(hfs,FALSE);
1190 g_array_free(etts,TRUE);
1193 g_hash_table_insert(xml_ns.elements,root_element->name,root_element);
1195 g_hash_table_foreach_remove(elements,free_elements,NULL);
1196 g_hash_table_destroy(elements);
1198 destroy_dtd_data(dtd_data);
1202 # define DIRECTORY_T GDir
1203 # define FILE_T gchar
1204 # define OPENDIR_OP(name) g_dir_open(name, 0, dummy)
1205 # define DIRGETNEXT_OP(dir) g_dir_read_name(dir)
1206 # define GETFNAME_OP(file) (file);
1207 # define CLOSEDIR_OP(dir) g_dir_close(dir)
1209 static void init_xml_names(void) {
1210 xml_ns_t* xmlpi_xml_ns;
1214 const gchar* filename;
1217 GError** dummy = g_malloc(sizeof(GError *));
1220 xmpli_names = g_hash_table_new(g_str_hash,g_str_equal);
1221 media_types = g_hash_table_new(g_str_hash,g_str_equal);
1223 unknown_ns.elements = xml_ns.elements = g_hash_table_new(g_str_hash,g_str_equal);
1224 unknown_ns.attributes = xml_ns.attributes = g_hash_table_new(g_str_hash,g_str_equal);
1226 xmlpi_xml_ns = xml_new_namespace(xmpli_names,"xml","version","encoding","standalone",NULL);
1228 g_hash_table_destroy(xmlpi_xml_ns->elements);
1229 xmlpi_xml_ns->elements = NULL;
1232 dirname = get_persconffile_path("dtds", FALSE, FALSE);
1234 if (test_for_directory(dirname) != EISDIR) {
1235 /* Although dir isn't a directory it may still use memory */
1237 dirname = get_datafile_path("dtds");
1240 if (test_for_directory(dirname) == EISDIR) {
1242 if ((dir = OPENDIR_OP(dirname)) != NULL) {
1243 while ((file = DIRGETNEXT_OP(dir)) != NULL) {
1245 filename = GETFNAME_OP(file);
1247 namelen = (int)strlen(filename);
1248 if ( namelen > 4 && ( g_ascii_strcasecmp(filename+(namelen-4),".dtd") == 0 ) ) {
1249 GString* errors = g_string_new("");
1250 GString* preparsed = dtd_preparse(dirname, filename, errors);
1251 dtd_build_data_t* dtd_data;
1254 report_failure("Dtd Preparser in file %s%c%s: %s",dirname,G_DIR_SEPARATOR,filename,errors->str);
1258 dtd_data = dtd_parse(preparsed);
1260 g_string_free(preparsed,TRUE);
1262 if (dtd_data->error->len) {
1263 report_failure("Dtd Parser in file %s%c%s: %s",dirname,G_DIR_SEPARATOR,filename,dtd_data->error->str);
1264 destroy_dtd_data(dtd_data);
1268 register_dtd(dtd_data,errors);
1271 report_failure("Dtd Registration in file: %s%c%s: %s",dirname,G_DIR_SEPARATOR,filename,errors->str);
1272 g_string_free(errors,TRUE);
1284 for(i=0;i<array_length(default_media_types);i++) {
1285 if( ! g_hash_table_lookup(media_types,default_media_types[i]) ) {
1286 g_hash_table_insert(media_types,(gpointer)default_media_types[i],&xml_ns);
1290 g_hash_table_foreach(xmpli_names,add_xmlpi_namespace,"xml.xmlpi");
1295 static void range_delete_xml_tcp_callback(guint32 port) {
1296 dissector_delete_uint("tcp.port", port, xml_handle);
1299 static void range_add_xml_tcp_callback(guint32 port) {
1300 dissector_add_uint("tcp.port", port, xml_handle);
1303 static void apply_prefs(void) {
1304 if (pref_heuristic_media_save != pref_heuristic_media) {
1305 if (pref_heuristic_media) {
1306 heur_dissector_add("http", dissect_xml_heur, xml_ns.hf_tag);
1307 heur_dissector_add("sip", dissect_xml_heur, xml_ns.hf_tag);
1308 heur_dissector_add("media", dissect_xml_heur, xml_ns.hf_tag);
1309 pref_heuristic_media_save = TRUE;
1311 heur_dissector_delete("http", dissect_xml_heur, xml_ns.hf_tag);
1312 heur_dissector_delete("sip", dissect_xml_heur, xml_ns.hf_tag);
1313 heur_dissector_delete("media", dissect_xml_heur, xml_ns.hf_tag);
1314 pref_heuristic_media_save = FALSE;
1318 if (pref_heuristic_tcp_save != pref_heuristic_tcp ) {
1319 if (pref_heuristic_tcp) {
1320 heur_dissector_add("tcp", dissect_xml_heur, xml_ns.hf_tag);
1321 pref_heuristic_tcp_save = TRUE;
1323 heur_dissector_delete("tcp", dissect_xml_heur, xml_ns.hf_tag);
1324 pref_heuristic_tcp_save = FALSE;
1328 if (pref_heuristic_udp_save != pref_heuristic_udp ) {
1329 if (pref_heuristic_udp) {
1330 heur_dissector_add("udp", dissect_xml_heur, xml_ns.hf_tag);
1331 pref_heuristic_udp_save = TRUE;
1333 heur_dissector_delete("udp", dissect_xml_heur, xml_ns.hf_tag);
1334 pref_heuristic_udp_save = FALSE;
1338 range_foreach(xml_tcp_range, range_delete_xml_tcp_callback);
1339 g_free(xml_tcp_range);
1340 xml_tcp_range = range_copy(global_xml_tcp_range);
1341 range_foreach(xml_tcp_range, range_add_xml_tcp_callback);
1345 proto_register_xml(void) {
1346 static gint *ett_base[] = {
1353 static hf_register_info hf_base[] = {
1354 { &hf_xmlpi, {"XMLPI", "xml.xmlpi", FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL }},
1355 { &hf_comment, {"Comment", "xml.comment", FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL }},
1356 { &hf_unknowwn_attrib, {"Attribute", "xml.attribute", FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL }},
1357 { &hf_doctype, {"Doctype", "xml.doctype", FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL }},
1358 { &hf_dtd_tag, {"DTD Tag", "xml.dtdtag", FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL }},
1359 { &unknown_ns.hf_cdata, {"CDATA", "xml.cdata", FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL }},
1360 { &unknown_ns.hf_tag, {"Tag", "xml.tag", FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL }},
1361 { &xml_ns.hf_cdata, {"Unknown", "xml.unknown", FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL }}
1363 module_t* xml_module;
1365 hf_arr = g_array_new(FALSE,FALSE,sizeof(hf_register_info));
1366 ett_arr = g_array_new(FALSE,FALSE,sizeof(gint*));
1368 g_array_append_vals(hf_arr,hf_base,array_length(hf_base));
1369 g_array_append_vals(ett_arr,ett_base,array_length(ett_base));
1373 xml_ns.hf_tag = proto_register_protocol("eXtensible Markup Language", "XML", xml_ns.name);
1375 proto_register_field_array(xml_ns.hf_tag, (hf_register_info*)g_array_data(hf_arr), hf_arr->len);
1376 proto_register_subtree_array((gint**)g_array_data(ett_arr), ett_arr->len);
1378 xml_module = prefs_register_protocol(xml_ns.hf_tag,apply_prefs);
1379 prefs_register_bool_preference(xml_module, "heuristic", "Use Heuristics for media types",
1380 "Try to recognize XML for unknown media types",
1381 &pref_heuristic_media);
1382 prefs_register_bool_preference(xml_module, "heuristic_tcp", "Use Heuristics for TCP",
1383 "Try to recognize XML for unknown TCP ports",
1384 &pref_heuristic_tcp);
1385 prefs_register_range_preference(xml_module, "tcp.port", "TCP Ports",
1387 &global_xml_tcp_range, 65535);
1388 prefs_register_bool_preference(xml_module, "heuristic_udp", "Use Heuristics for UDP",
1389 "Try to recognize XML for unknown UDP ports",
1390 &pref_heuristic_udp);
1391 prefs_register_bool_preference(xml_module, "heuristic_unicode", "Use Unicode in heuristics",
1392 "Try to recognize XML encoded in Unicode (UCS-2)",
1393 &pref_heuristic_unicode);
1395 g_array_free(hf_arr,FALSE);
1396 g_array_free(ett_arr,TRUE);
1398 register_dissector("xml", dissect_xml, xml_ns.hf_tag);
1402 xml_tcp_range = range_empty();
1407 static void add_dissector_media(gpointer k, gpointer v _U_, gpointer p _U_) {
1408 dissector_add_string("media_type", (gchar*)k, xml_handle);
1412 proto_reg_handoff_xml(void)
1415 xml_handle = find_dissector("xml");
1417 g_hash_table_foreach(media_types,add_dissector_media,NULL);