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.
47 #include <epan/emem.h>
48 #include <epan/packet.h>
49 #include <epan/strutil.h>
50 #include <epan/tvbparse.h>
52 #include <epan/report_err.h>
53 #include <epan/filesystem.h>
54 #include <epan/prefs.h>
55 #include <epan/garrayfix.h>
57 typedef struct _xml_ns_t {
58 /* the name of this namespace */
61 /* its fully qualified name */
64 /* the contents of the whole element from <> to </> */
67 /* chunks of cdata from <> to </> excluding sub tags */
70 /* the subtree for its sub items */
73 GHashTable* attributes;
74 /* key: the attribute name
75 value: hf_id of what's between quotes */
77 /* the namespace's namespaces */
79 /* key: the element name
80 value: the child namespace */
82 GPtrArray* element_names;
83 /* imported directly from the parser and used while building the namespace */
90 proto_item* last_item;
95 struct _attr_reg_data {
101 static gint ett_dtd = -1;
102 static gint ett_xmpli = -1;
104 static int hf_unknowwn_attrib = -1;
105 static int hf_comment = -1;
106 static int hf_xmlpi = -1;
107 static int hf_dtd_tag = -1;
108 static int hf_doctype = -1;
110 /* dissector handles */
111 static dissector_handle_t xml_handle;
113 /* parser definitions */
114 static tvbparse_wanted_t* want;
115 static tvbparse_wanted_t* want_ignore;
116 static tvbparse_wanted_t* want_heur;
118 static GHashTable* xmpli_names;
119 static GHashTable* media_types;
121 static xml_ns_t xml_ns = {"xml","/",-1,-1,-1,NULL,NULL,NULL};
122 static xml_ns_t unknown_ns = {"unknown","?",-1,-1,-1,NULL,NULL,NULL};
123 static xml_ns_t* root_ns;
125 static gboolean pref_heuristic_media = FALSE;
126 static gboolean pref_heuristic_tcp = FALSE;
127 static gboolean pref_heuristic_media_save = FALSE;
128 static gboolean pref_heuristic_tcp_save = FALSE;
129 static range_t *global_xml_tcp_range = NULL;
130 static range_t *xml_tcp_range = NULL;
132 #define XML_CDATA -1000
133 #define XML_SCOPED_NAME -1001
139 static const gchar* default_media_types[] = {
146 "application/auth-policy+xml",
147 "application/cpim-pidf+xml",
148 "application/cpl+xml",
149 "application/mathml+xml",
150 "application/media_control+xml",
151 "application/note+xml",
152 "application/pidf+xml",
153 "application/poc-settings+xml",
154 "application/rdf+xml",
155 "application/reginfo+xml",
156 "application/resource-lists+xml",
157 "application/rlmi+xml",
158 "application/rls-services+xml",
160 "application/simple-filter+xml",
161 "application/soap+xml",
162 "application/vnd.wv.csp+xml",
163 "application/vnd.wv.csp.xml",
164 "application/watcherinfo+xml",
165 "application/xcap-att+xml",
166 "application/xcap-caps+xml",
167 "application/xcap-diff+xml",
168 "application/xcap-el+xml",
169 "application/xcap-error+xml",
170 "application/xcap-ns+xml",
172 "application/xml-dtd",
173 "application/xpidf+xml",
174 "application/xslt+xml",
179 dissect_xml(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
182 tvbparse_elem_t* tok = NULL;
183 static GPtrArray* stack = NULL;
184 xml_frame_t* current_frame;
190 g_ptr_array_free(stack,TRUE);
192 stack = g_ptr_array_new();
193 current_frame = ep_alloc(sizeof(xml_frame_t));
194 g_ptr_array_add(stack,current_frame);
196 tt = tvbparse_init(tvb,0,-1,stack,want_ignore);
197 current_frame->start_offset = 0;
201 if (pinfo->match_string)
202 root_ns = g_hash_table_lookup(media_types,pinfo->match_string);
206 colinfo_str = "/XML";
208 colinfo_str = ep_strdup_printf("/%s",root_ns->name);
209 g_strup(colinfo_str);
212 if (check_col(pinfo->cinfo, COL_PROTOCOL))
213 col_append_str(pinfo->cinfo, COL_PROTOCOL, colinfo_str);
215 current_frame->ns = root_ns;
217 current_frame->item = proto_tree_add_item(tree,current_frame->ns->hf_tag,tvb,0,-1,FALSE);
218 current_frame->tree = proto_item_add_subtree(current_frame->item,current_frame->ns->ett);
219 current_frame->last_item = current_frame->item;
221 while(( tok = tvbparse_get(tt, want) )) ;
224 static gboolean dissect_xml_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
225 if ( (pref_heuristic_media || pref_heuristic_tcp)
226 && tvbparse_peek(tvbparse_init(tvb,0,-1,NULL,want_ignore), want_heur)) {
227 dissect_xml(tvb, pinfo, tree);
234 static void after_token(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok) {
235 GPtrArray* stack = tvbparse_data;
236 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
240 if (tok->id == XML_CDATA) {
241 hfid = current_frame->ns ? current_frame->ns->hf_cdata : xml_ns.hf_cdata;
242 } else if ( tok->id > 0) {
245 hfid = xml_ns.hf_cdata;
248 pi = proto_tree_add_item(current_frame->tree, hfid, tok->tvb, tok->offset, tok->len, FALSE);
250 proto_item_set_text(pi, "%s",
251 tvb_format_text(tok->tvb,tok->offset,tok->len));
254 static void before_xmpli(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok) {
255 GPtrArray* stack = tvbparse_data;
256 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
259 tvbparse_elem_t* name_tok = tok->sub->next;
260 gchar* name = (gchar*)tvb_get_ephemeral_string(name_tok->tvb,name_tok->offset,name_tok->len);
261 xml_ns_t* ns = g_hash_table_lookup(xmpli_names,name);
275 pi = proto_tree_add_item(current_frame->tree,hf_tag,tok->tvb,tok->offset,tok->len,FALSE);
277 proto_item_set_text(pi, "%s", tvb_format_text(tok->tvb,tok->offset,(name_tok->offset - tok->offset) + name_tok->len));
279 pt = proto_item_add_subtree(pi,ett);
281 current_frame = ep_alloc(sizeof(xml_frame_t));
282 current_frame->item = pi;
283 current_frame->last_item = pi;
284 current_frame->tree = pt;
285 current_frame->start_offset = tok->offset;
286 current_frame->ns = ns;
288 g_ptr_array_add(stack,current_frame);
292 static void after_xmlpi(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok) {
293 GPtrArray* stack = tvbparse_data;
294 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
296 proto_tree_add_text(current_frame->tree,
297 tok->tvb, tok->offset, tok->len,
298 tvb_format_text(tok->tvb,tok->offset,tok->len));
300 if (stack->len > 1) {
301 g_ptr_array_remove_index_fast(stack,stack->len - 1);
303 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"[ ERROR: Closing an unopened xmpli tag ]");
307 static void before_tag(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok) {
308 GPtrArray* stack = tvbparse_data;
309 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
310 tvbparse_elem_t* name_tok = tok->sub->next;
314 xml_frame_t* new_frame;
318 if (name_tok->sub->id == XML_SCOPED_NAME) {
319 tvbparse_elem_t* root_tok = name_tok->sub->sub;
320 tvbparse_elem_t* leaf_tok = name_tok->sub->sub->next->next;
321 xml_ns_t* nameroot_ns;
323 root_name = (gchar*)tvb_get_ephemeral_string(root_tok->tvb,root_tok->offset,root_tok->len);
324 name = (gchar*)tvb_get_ephemeral_string(leaf_tok->tvb,leaf_tok->offset,leaf_tok->len);
326 nameroot_ns = g_hash_table_lookup(xml_ns.elements,root_name);
329 ns = g_hash_table_lookup(nameroot_ns->elements,name);
338 name = (gchar*)tvb_get_ephemeral_string(name_tok->tvb,name_tok->offset,name_tok->len);
341 if(current_frame->ns) {
342 ns = g_hash_table_lookup(current_frame->ns->elements,name);
345 if (! ( ns = g_hash_table_lookup(root_ns->elements,name) ) ) {
354 pi = proto_tree_add_item(current_frame->tree,ns->hf_tag,tok->tvb,tok->offset,tok->len,FALSE);
355 proto_item_set_text(pi, "%s", tvb_format_text(tok->tvb,tok->offset,(name_tok->offset - tok->offset) + name_tok->len));
357 pt = proto_item_add_subtree(pi,ns->ett);
359 new_frame = ep_alloc(sizeof(xml_frame_t));
360 new_frame->item = pi;
361 new_frame->last_item = pi;
362 new_frame->tree = pt;
363 new_frame->start_offset = tok->offset;
366 g_ptr_array_add(stack,new_frame);
370 static void after_open_tag(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok _U_) {
371 GPtrArray* stack = tvbparse_data;
372 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
374 proto_item_append_text(current_frame->last_item,">");
377 static void after_closed_tag(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok) {
378 GPtrArray* stack = tvbparse_data;
379 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
381 proto_item_append_text(current_frame->last_item,"/>");
383 if (stack->len > 1) {
384 g_ptr_array_remove_index_fast(stack,stack->len - 1);
386 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"[ ERROR: Closing an unopened tag ]");
390 static void after_untag(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok){
391 GPtrArray* stack = tvbparse_data;
392 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
394 proto_item_set_len(current_frame->item, (tok->offset - current_frame->start_offset) + tok->len);
396 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"%s",
397 tvb_format_text(tok->tvb,tok->offset,tok->len));
399 if (stack->len > 1) {
400 g_ptr_array_remove_index_fast(stack,stack->len - 1);
402 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,
403 "[ ERROR: Closing an unopened tag ]");
407 static void before_dtd_doctype(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok){
408 GPtrArray* stack = tvbparse_data;
409 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
410 tvbparse_elem_t* name_tok = tok->sub->next->next->next->sub->sub;
411 proto_tree* dtd_item = proto_tree_add_item(current_frame->tree, hf_doctype,
412 name_tok->tvb, name_tok->offset, name_tok->len, FALSE);
414 proto_item_set_text(dtd_item,"%s",tvb_format_text(tok->tvb,tok->offset,tok->len));
416 current_frame = ep_alloc(sizeof(xml_frame_t));
417 current_frame->item = dtd_item;
418 current_frame->last_item = dtd_item;
419 current_frame->tree = proto_item_add_subtree(dtd_item,ett_dtd);
420 current_frame->start_offset = tok->offset;
421 current_frame->ns = NULL;
423 g_ptr_array_add(stack,current_frame);
426 static void pop_stack(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok _U_) {
427 GPtrArray* stack = tvbparse_data;
428 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
430 if (stack->len > 1) {
431 g_ptr_array_remove_index_fast(stack,stack->len - 1);
433 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,
434 "[ ERROR: Closing an unopened tag ]");
438 static void after_dtd_close(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok){
439 GPtrArray* stack = tvbparse_data;
440 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
442 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"%s",
443 tvb_format_text(tok->tvb,tok->offset,tok->len));
444 if (stack->len > 1) {
445 g_ptr_array_remove_index_fast(stack,stack->len - 1);
447 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"[ ERROR: Closing an unopened tag ]");
451 static void get_attrib_value(void* tvbparse_data _U_, const void* wanted_data _U_, tvbparse_elem_t* tok) {
452 tok->data = tok->sub;
455 static void after_attrib(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok) {
456 GPtrArray* stack = tvbparse_data;
457 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
458 gchar* name = (gchar*)tvb_get_ephemeral_string(tok->sub->tvb,tok->sub->offset,tok->sub->len);
459 tvbparse_elem_t* value = tok->sub->next->next->data;
464 if(current_frame->ns && (hfidp = g_hash_table_lookup(current_frame->ns->attributes,name) )) {
467 hfid = hf_unknowwn_attrib;
471 current_frame->last_item = proto_tree_add_item(current_frame->tree,hfid,value->tvb,value->offset,value->len,FALSE);
472 proto_item_set_text(current_frame->last_item, "%s", tvb_format_text(tok->tvb,tok->offset,tok->len));
476 static void unrecognized_token(void* tvbparse_data, const void* wanted_data _U_, tvbparse_elem_t* tok _U_){
477 GPtrArray* stack = tvbparse_data;
478 xml_frame_t* current_frame = g_ptr_array_index(stack,stack->len - 1);
480 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"[ ERROR: Unrecognized text ]");
486 static void init_xml_parser(void) {
487 tvbparse_wanted_t* want_name = tvbparse_chars(-1,1,0,"abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",NULL,NULL,NULL);
488 tvbparse_wanted_t* want_attr_name = tvbparse_chars(-1,1,0,"abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:",NULL,NULL,NULL);
490 tvbparse_wanted_t* want_scoped_name = tvbparse_set_seq(XML_SCOPED_NAME, NULL, NULL, NULL,
492 tvbparse_char(-1,":",NULL,NULL,NULL),
496 tvbparse_wanted_t* want_tag_name = tvbparse_set_oneof(0, NULL, NULL, NULL,
501 tvbparse_wanted_t* want_attrib_value = tvbparse_set_oneof(0, NULL, NULL, get_attrib_value,
502 tvbparse_quoted(-1, NULL, NULL, tvbparse_shrink_token_cb,'\"','\\'),
503 tvbparse_quoted(-1, NULL, NULL, tvbparse_shrink_token_cb,'\'','\\'),
504 tvbparse_chars(-1,1,0,"0123456789",NULL,NULL,NULL),
508 tvbparse_wanted_t* want_attributes = tvbparse_one_or_more(-1, NULL, NULL, NULL,
509 tvbparse_set_seq(-1, NULL, NULL, after_attrib,
511 tvbparse_char(-1,"=",NULL,NULL,NULL),
515 tvbparse_wanted_t* want_stoptag = tvbparse_set_oneof(-1,NULL,NULL,NULL,
516 tvbparse_char(-1, ">", NULL, NULL, after_open_tag),
517 tvbparse_string(-1, "/>", NULL, NULL, after_closed_tag),
520 tvbparse_wanted_t* want_stopxmlpi = tvbparse_string(-1,"?>",NULL,NULL,after_xmlpi);
522 tvbparse_wanted_t* want_comment = tvbparse_set_seq(hf_comment,NULL,NULL,after_token,
523 tvbparse_string(-1,"<!--",NULL,NULL,NULL),
524 tvbparse_until(-1,NULL,NULL,NULL,
525 tvbparse_string(-1,"-->",NULL,NULL,NULL),
529 tvbparse_wanted_t* want_xmlpi = tvbparse_set_seq(hf_xmlpi,NULL,before_xmpli,NULL,
530 tvbparse_string(-1,"<?",NULL,NULL,NULL),
532 tvbparse_set_oneof(-1,NULL,NULL,NULL,
534 tvbparse_set_seq(-1,NULL,NULL,NULL,
541 tvbparse_wanted_t* want_closing_tag = tvbparse_set_seq(0,NULL,NULL,after_untag,
542 tvbparse_char(-1, "<", NULL, NULL, NULL),
543 tvbparse_char(-1, "/", NULL, NULL, NULL),
545 tvbparse_char(-1, ">", NULL, NULL, NULL),
548 tvbparse_wanted_t* want_doctype_start = tvbparse_set_seq(-1,NULL,before_dtd_doctype,NULL,
549 tvbparse_char(-1,"<",NULL,NULL,NULL),
550 tvbparse_char(-1,"!",NULL,NULL,NULL),
551 tvbparse_casestring(-1,"DOCTYPE",NULL,NULL,NULL),
552 tvbparse_set_oneof(-1,NULL,NULL,NULL,
553 tvbparse_set_seq(-1,NULL,NULL,NULL,
555 tvbparse_char(-1,"[",NULL,NULL,NULL),
557 tvbparse_set_seq(-1,NULL,NULL,pop_stack,
559 tvbparse_set_oneof(-1,NULL,NULL,NULL,
560 tvbparse_casestring(-1,"PUBLIC",NULL,NULL,NULL),
561 tvbparse_casestring(-1,"SYSTEM",NULL,NULL,NULL),
563 tvbparse_until(-1,NULL,NULL,NULL,
564 tvbparse_char(-1,">",NULL,NULL,NULL),
570 tvbparse_wanted_t* want_dtd_tag = tvbparse_set_seq(hf_dtd_tag,NULL,NULL,after_token,
571 tvbparse_char(-1,"<",NULL,NULL,NULL),
572 tvbparse_char(-1,"!",NULL,NULL,NULL),
573 tvbparse_until(-1,NULL,NULL,NULL,
574 tvbparse_char(-1, ">", NULL, NULL, NULL),
578 tvbparse_wanted_t* want_tag = tvbparse_set_seq(-1, NULL, before_tag, NULL,
579 tvbparse_char(-1,"<",NULL,NULL,NULL),
581 tvbparse_set_oneof(-1,NULL,NULL,NULL,
582 tvbparse_set_seq(-1,NULL,NULL,NULL,
590 tvbparse_wanted_t* want_dtd_close = tvbparse_set_seq(-1,NULL,NULL,after_dtd_close,
591 tvbparse_char(-1,"]",NULL,NULL,NULL),
592 tvbparse_char(-1,">",NULL,NULL,NULL),
595 want_ignore = tvbparse_chars(-1,1,0," \t\r\n",NULL,NULL,NULL);
598 want = tvbparse_set_oneof(-1, NULL, NULL, NULL,
606 tvbparse_not_chars(XML_CDATA,1,0,"<",NULL,NULL,after_token),
607 tvbparse_not_chars(-1,1,0," \t\r\n",NULL,NULL,unrecognized_token),
610 want_heur = tvbparse_set_oneof(-1, NULL, NULL, NULL,
621 static xml_ns_t* xml_new_namespace(GHashTable* hash, gchar* name, ...) {
622 xml_ns_t* ns = g_malloc(sizeof(xml_ns_t));
626 ns->name = g_strdup(name);
630 ns->attributes = g_hash_table_new(g_str_hash,g_str_equal);
631 ns->elements = g_hash_table_new(g_str_hash,g_str_equal);
635 while(( attr_name = va_arg(ap,gchar*) )) {
636 int* hfp = g_malloc(sizeof(int));
638 g_hash_table_insert(ns->attributes,g_strdup(attr_name),hfp);
643 g_hash_table_insert(hash,ns->name,ns);
649 static void add_xml_field(GArray* hfs, int* p_id, gchar* name, gchar* fqn) {
650 hf_register_info hfri;
653 hfri.hfinfo.name = name;
654 hfri.hfinfo.abbrev = fqn;
655 hfri.hfinfo.type = FT_STRING;
656 hfri.hfinfo.display = BASE_NONE;
657 hfri.hfinfo.strings = NULL;
658 hfri.hfinfo.bitmask = 0x0;
659 hfri.hfinfo.blurb = "";
661 hfri.hfinfo.parent = 0;
662 hfri.hfinfo.ref_count = 0;
663 hfri.hfinfo.bitshift = 0;
664 hfri.hfinfo.same_name_next = NULL;
665 hfri.hfinfo.same_name_prev = NULL;
667 g_array_append_val(hfs,hfri);
670 static void add_xml_attribute_names(gpointer k, gpointer v, gpointer p) {
671 struct _attr_reg_data* d = p;
672 gchar* basename = g_strdup_printf("%s.%s",d->basename,(gchar*)k);
673 add_xml_field(d->hf, (int*) v, (gchar*)k, basename);
677 static void add_xmlpi_namespace(gpointer k _U_, gpointer v, gpointer p) {
679 gchar* basename = g_strdup_printf("%s.%s",(gchar*)p,ns->name);
680 gint* ett_p = &(ns->ett);
681 struct _attr_reg_data d;
683 add_xml_field(hf_arr, &(ns->hf_tag), basename, basename);
685 g_array_append_val(ett_arr,ett_p);
687 d.basename = basename;
690 g_hash_table_foreach(ns->attributes,add_xml_attribute_names,&d);
694 static void destroy_dtd_data(dtd_build_data_t* dtd_data) {
696 if(dtd_data->proto_name) g_free(dtd_data->proto_name);
697 if(dtd_data->media_type) g_free(dtd_data->media_type);
698 if(dtd_data->description) g_free(dtd_data->description);
699 if(dtd_data->proto_root) g_free(dtd_data->proto_root);
701 g_string_free(dtd_data->error,TRUE);
704 while(dtd_data->elements->len) {
705 dtd_named_list_t* nl = g_ptr_array_remove_index_fast(dtd_data->elements,0);
706 g_ptr_array_free(nl->list,TRUE);
710 g_ptr_array_free(dtd_data->elements,TRUE);
712 while(dtd_data->attributes->len) {
713 dtd_named_list_t* nl = g_ptr_array_remove_index_fast(dtd_data->attributes,0);
714 g_ptr_array_free(nl->list,TRUE);
718 g_ptr_array_free(dtd_data->attributes,TRUE);
725 static void copy_attrib_item(gpointer k, gpointer v _U_, gpointer p) {
726 gchar* key = g_strdup(k);
727 int* value = g_malloc(sizeof(int));
731 g_hash_table_insert(dst,key,value);
735 static GHashTable* copy_attributes_hash(GHashTable* src) {
736 GHashTable* dst = g_hash_table_new(g_str_hash,g_str_equal);
738 g_hash_table_foreach(src,copy_attrib_item,dst);
743 static xml_ns_t* duplicate_element(xml_ns_t* orig) {
744 xml_ns_t* new_item = g_malloc(sizeof(xml_ns_t));
747 new_item->name = g_strdup(orig->name);
748 new_item->hf_tag = -1;
749 new_item->hf_cdata = -1;
751 new_item->attributes = copy_attributes_hash(orig->attributes);
752 new_item->elements = g_hash_table_new(g_str_hash,g_str_equal);
753 new_item->element_names = g_ptr_array_new();
755 for(i=0; i < orig->element_names->len; i++) {
756 g_ptr_array_add(new_item->element_names,
757 g_ptr_array_index(orig->element_names,i));
763 static gchar* fully_qualified_name(GPtrArray* hier, gchar* name, gchar* proto_name) {
765 GString* s = g_string_new(proto_name);
767 g_string_append(s,".");
769 for (i = 1; i < hier->len; i++) {
770 g_string_sprintfa(s, "%s.",(gchar*)g_ptr_array_index(hier,i));
773 g_string_append(s,name);
775 g_string_free(s,FALSE);
781 static xml_ns_t* make_xml_hier(gchar* elem_name,
783 GHashTable* elements,
793 struct _attr_reg_data d;
794 gboolean recurred = FALSE;
797 if ( g_str_equal(elem_name,root->name) ) {
801 if (! ( orig = g_hash_table_lookup(elements,elem_name) )) {
802 g_string_sprintfa(error,"element '%s' is not defined\n", elem_name);
806 for (i = 0; i < hier->len; i++) {
807 if( strcmp(elem_name,(gchar*) g_ptr_array_index(hier,i) ) == 0 ) {
816 fqn = fully_qualified_name(hier,elem_name,proto_name);
818 new = duplicate_element(orig);
821 add_xml_field(hfs, &(new->hf_tag), g_strdup(elem_name), fqn);
822 add_xml_field(hfs, &(new->hf_cdata), g_strdup(elem_name), fqn);
825 g_array_append_val(etts,ett_p);
830 g_hash_table_foreach(new->attributes,add_xml_attribute_names,&d);
832 while(new->element_names->len) {
833 gchar* child_name = g_ptr_array_remove_index(new->element_names,0);
834 xml_ns_t* child_element = NULL;
836 g_ptr_array_add(hier,elem_name);
837 child_element = make_xml_hier(child_name, root, elements, hier,error,hfs,etts,proto_name);
838 g_ptr_array_remove_index_fast(hier,hier->len - 1);
841 g_hash_table_insert(new->elements,child_element->name,child_element);
845 g_ptr_array_free(new->element_names,TRUE);
846 new->element_names = NULL;
850 static gboolean free_both(gpointer k, gpointer v, gpointer p _U_) {
856 static gboolean free_elements(gpointer k _U_, gpointer v, gpointer p _U_) {
859 g_hash_table_foreach_remove(e->attributes,free_both,NULL);
860 g_hash_table_destroy(e->attributes);
861 g_hash_table_destroy(e->elements);
863 while (e->element_names->len) {
864 g_free(g_ptr_array_remove_index(e->element_names,0));
867 g_ptr_array_free(e->element_names,TRUE);
873 static void register_dtd(dtd_build_data_t* dtd_data, GString* errors) {
874 GHashTable* elements = g_hash_table_new(g_str_hash,g_str_equal);
875 gchar* root_name = NULL;
876 xml_ns_t* root_element = NULL;
881 GPtrArray* element_names = g_ptr_array_new();
883 /* we first populate elements with the those coming from the parser */
884 while(dtd_data->elements->len) {
885 dtd_named_list_t* nl = g_ptr_array_remove_index(dtd_data->elements,0);
886 xml_ns_t* element = g_malloc(sizeof(xml_ns_t));
888 /* we will use the first element found as root in case no other one was given. */
889 if (root_name == NULL)
890 root_name = g_strdup(nl->name);
892 element->name = nl->name;
893 element->element_names = nl->list;
894 element->hf_tag = -1;
895 element->hf_cdata = -1;
897 element->attributes = g_hash_table_new(g_str_hash,g_str_equal);
898 element->elements = g_hash_table_new(g_str_hash,g_str_equal);
900 if( g_hash_table_lookup(elements,element->name) ) {
901 g_string_sprintfa(errors,"element %s defined more than once\n", element->name);
902 free_elements(NULL,element,NULL);
904 g_hash_table_insert(elements,element->name,element);
905 g_ptr_array_add(element_names,g_strdup(element->name));
911 /* then we add the attributes to its relative elements */
912 while(dtd_data->attributes->len) {
913 dtd_named_list_t* nl = g_ptr_array_remove_index(dtd_data->attributes,0);
914 xml_ns_t* element = g_hash_table_lookup(elements,nl->name);
917 g_string_sprintfa(errors,"element %s is not defined\n", nl->name);
922 while(nl->list->len) {
923 gchar* name = g_ptr_array_remove_index(nl->list,0);
924 int* id_p = g_malloc(sizeof(int));
927 g_hash_table_insert(element->attributes,name,id_p);
932 g_ptr_array_free(nl->list,TRUE);
936 /* if a proto_root is defined in the dtd we'll use that as root */
937 if( dtd_data->proto_root ) {
940 root_name = g_strdup(dtd_data->proto_root);
943 /* we use a stack with the names to avoid recurring infinitelly */
944 hier = g_ptr_array_new();
947 * if a proto name was given in the dtd the dtd will be used as a protocol
948 * or else the dtd will be loaded as a branch of the xml namespace
950 if( ! dtd_data->proto_name ) {
953 g_ptr_array_add(hier,g_strdup("xml"));
954 root_element = &xml_ns;
957 * if we were given a proto_name the namespace will be registered
958 * as an indipendent protocol with its own hf and ett arrays.
960 hfs = g_array_new(FALSE,FALSE,sizeof(hf_register_info));
961 etts = g_array_new(FALSE,FALSE,sizeof(gint*));
964 /* the root element of the dtd's namespace */
965 root_element = g_malloc(sizeof(xml_ns_t));
966 root_element->name = g_strdup(root_name);
967 root_element->fqn = dtd_data->proto_name ? g_strdup(dtd_data->proto_name) : root_element->name;
968 root_element->hf_tag = -1;
969 root_element->hf_cdata = -1;
970 root_element->ett = -1;
971 root_element->elements = g_hash_table_new(g_str_hash,g_str_equal);
972 root_element->element_names = element_names;
975 * we can either create a namespace as a flat namespace
976 * in which all the elements are at the root level
977 * or we can create a recursive namespace
979 if (dtd_data->recursion) {
982 make_xml_hier(root_name, root_element, elements,hier,errors,hfs,etts,dtd_data->proto_name);
984 g_hash_table_insert(root_element->elements,root_element->name,root_element);
986 orig_root = g_hash_table_lookup(elements,root_name);
988 /* if the root element was defined copy its attrlist to the child */
990 struct _attr_reg_data d;
992 d.basename = dtd_data->proto_name;
995 root_element->attributes = copy_attributes_hash(orig_root->attributes);
996 g_hash_table_foreach(root_element->attributes,add_xml_attribute_names,&d);
998 root_element->attributes = g_hash_table_new(g_str_hash,g_str_equal);
1001 /* we then create all the sub hierachies to catch the recurred cases */
1002 g_ptr_array_add(hier,root_name);
1004 while(root_element->element_names->len) {
1005 curr_name = g_ptr_array_remove_index(root_element->element_names,0);
1007 if( ! g_hash_table_lookup(root_element->elements,curr_name) ) {
1008 xml_ns_t* new = make_xml_hier(curr_name, root_element, elements,hier,errors,hfs,etts,dtd_data->proto_name);
1009 g_hash_table_insert(root_element->elements,new->name,new);
1016 /* a flat namespace */
1017 g_ptr_array_add(hier,root_name);
1019 root_element->attributes = g_hash_table_new(g_str_hash,g_str_equal);
1021 while(root_element->element_names->len) {
1024 struct _attr_reg_data d;
1026 curr_name = g_ptr_array_remove_index(root_element->element_names,0);
1027 new = duplicate_element(g_hash_table_lookup(elements,curr_name));
1028 new->fqn = fully_qualified_name(hier, curr_name, root_name);
1030 add_xml_field(hfs, &(new->hf_tag), curr_name, new->fqn);
1031 add_xml_field(hfs, &(new->hf_cdata), curr_name, new->fqn);
1033 d.basename = new->fqn;
1036 g_hash_table_foreach(new->attributes,add_xml_attribute_names,&d);
1039 g_array_append_val(etts,ett_p);
1041 g_ptr_array_free(new->element_names,TRUE);
1043 g_hash_table_insert(root_element->elements,new->name,new);
1047 g_ptr_array_free(element_names,TRUE);
1049 g_ptr_array_free(hier,TRUE);
1052 * if we were given a proto_name the namespace will be registered
1053 * as an indipendent protocol.
1055 if( dtd_data->proto_name ) {
1058 if ( ! dtd_data->description) {
1059 dtd_data->description = g_strdup(root_name);
1062 ett_p = &root_element->ett;
1063 g_array_append_val(etts,ett_p);
1065 add_xml_field(hfs, &root_element->hf_cdata, root_element->name, root_element->fqn);
1067 root_element->hf_tag = proto_register_protocol(dtd_data->description, dtd_data->proto_name, dtd_data->proto_name);
1068 proto_register_field_array(root_element->hf_tag, (hf_register_info*)g_array_data(hfs), hfs->len);
1069 proto_register_subtree_array((gint**)g_array_data(etts), etts->len);
1071 if (dtd_data->media_type) {
1072 g_hash_table_insert(media_types,dtd_data->media_type,root_element);
1073 dtd_data->media_type = NULL;
1076 dtd_data->description = NULL;
1077 dtd_data->proto_name = NULL;
1078 g_array_free(hfs,FALSE);
1079 g_array_free(etts,TRUE);
1082 g_hash_table_insert(xml_ns.elements,root_element->name,root_element);
1084 g_hash_table_foreach_remove(elements,free_elements,NULL);
1085 g_hash_table_destroy(elements);
1087 destroy_dtd_data(dtd_data);
1093 #if GLIB_MAJOR_VERSION < 2
1094 # define DIRECTORY_T DIR
1095 # define FILE_T struct dirent
1096 # define OPENDIR_OP(name) opendir(name)
1097 # define DIRGETNEXT_OP(dir) readdir(dir)
1098 # define GETFNAME_OP(file) (gchar *)file->d_name
1099 # define CLOSEDIR_OP(dir) closedir(dir)
1101 # define DIRECTORY_T GDir
1102 # define FILE_T gchar
1103 # define OPENDIR_OP(name) g_dir_open(name, 0, dummy)
1104 # define DIRGETNEXT_OP(dir) g_dir_read_name(dir)
1105 # define GETFNAME_OP(file) (file);
1106 # define CLOSEDIR_OP(dir) g_dir_close(dir)
1109 static void init_xml_names(void) {
1110 xml_ns_t* xmlpi_xml_ns;
1114 const gchar* filename;
1117 #if GLIB_MAJOR_VERSION >= 2
1118 GError** dummy = g_malloc(sizeof(GError *));
1122 xmpli_names = g_hash_table_new(g_str_hash,g_str_equal);
1123 media_types = g_hash_table_new(g_str_hash,g_str_equal);
1125 unknown_ns.elements = xml_ns.elements = g_hash_table_new(g_str_hash,g_str_equal);
1126 unknown_ns.attributes = xml_ns.attributes = g_hash_table_new(g_str_hash,g_str_equal);
1128 xmlpi_xml_ns = xml_new_namespace(xmpli_names,"xml","version","encoding","standalone",NULL);
1130 g_hash_table_destroy(xmlpi_xml_ns->elements);
1131 xmlpi_xml_ns->elements = NULL;
1134 dirname = get_persconffile_path("dtds", FALSE);
1136 if (test_for_directory(dirname) != EISDIR) {
1137 /* Although dir isn't a directory it may still use memory */
1139 dirname = get_datafile_path("dtds");
1142 if (test_for_directory(dirname) == EISDIR) {
1144 if ((dir = OPENDIR_OP(dirname)) != NULL) {
1145 while ((file = DIRGETNEXT_OP(dir)) != NULL) {
1147 filename = GETFNAME_OP(file);
1149 namelen = strlen(filename);
1150 if ( namelen > 4 && ( g_strcasecmp(filename+(namelen-4),".dtd") == 0 ) ) {
1151 GString* errors = g_string_new("");
1152 GString* preparsed = dtd_preparse(dirname, filename, errors);
1153 dtd_build_data_t* dtd_data;
1156 report_failure("Dtd Preparser in file %s%c%s: %s",dirname,G_DIR_SEPARATOR,filename,errors->str);
1160 dtd_data = dtd_parse(preparsed);
1162 g_string_free(preparsed,TRUE);
1164 if (dtd_data->error->len) {
1165 report_failure("Dtd Parser in file %s%c%s: %s",dirname,G_DIR_SEPARATOR,filename,dtd_data->error->str);
1166 destroy_dtd_data(dtd_data);
1170 register_dtd(dtd_data,errors);
1173 report_failure("Dtd Registration in file: %s%c%s: %s",dirname,G_DIR_SEPARATOR,filename,errors->str);
1174 g_string_free(errors,TRUE);
1184 for(i=0;i<array_length(default_media_types);i++) {
1185 if( ! g_hash_table_lookup(media_types,default_media_types[i]) ) {
1186 g_hash_table_insert(media_types,(gpointer)default_media_types[i],&xml_ns);
1190 g_hash_table_foreach(xmpli_names,add_xmlpi_namespace,"xml.xmlpi");
1192 #if GLIB_MAJOR_VERSION >= 2
1197 static void range_delete_xml_tcp_callback(guint32 port) {
1198 dissector_delete("tcp.port", port, xml_handle);
1201 static void range_add_xml_tcp_callback(guint32 port) {
1202 dissector_add("tcp.port", port, xml_handle);
1205 static void apply_prefs(void) {
1206 if (pref_heuristic_media_save != pref_heuristic_media) {
1207 if (pref_heuristic_media) {
1208 heur_dissector_add("http", dissect_xml_heur, xml_ns.hf_tag);
1209 heur_dissector_add("sip", dissect_xml_heur, xml_ns.hf_tag);
1210 heur_dissector_add("media", dissect_xml_heur, xml_ns.hf_tag);
1211 pref_heuristic_media_save = TRUE;
1213 heur_dissector_delete("http", dissect_xml_heur, xml_ns.hf_tag);
1214 heur_dissector_delete("sip", dissect_xml_heur, xml_ns.hf_tag);
1215 heur_dissector_delete("media", dissect_xml_heur, xml_ns.hf_tag);
1216 pref_heuristic_media_save = FALSE;
1220 if (pref_heuristic_tcp_save != pref_heuristic_tcp ) {
1221 if (pref_heuristic_tcp) {
1222 heur_dissector_add("tcp", dissect_xml_heur, xml_ns.hf_tag);
1223 pref_heuristic_tcp_save = TRUE;
1225 heur_dissector_delete("tcp", dissect_xml_heur, xml_ns.hf_tag);
1226 pref_heuristic_tcp_save = FALSE;
1230 range_foreach(xml_tcp_range, range_delete_xml_tcp_callback);
1231 g_free(xml_tcp_range);
1232 xml_tcp_range = range_copy(global_xml_tcp_range);
1233 range_foreach(xml_tcp_range, range_add_xml_tcp_callback);
1237 proto_register_xml(void) {
1238 static gint *ett_base[] = {
1245 static hf_register_info hf_base[] = {
1246 { &hf_xmlpi, {"XMLPI", "xml.xmlpi", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
1247 { &hf_comment, {"Comment", "xml.comment", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
1248 { &hf_unknowwn_attrib, {"Attribute", "xml.attribute", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
1249 { &hf_doctype, {"Doctype", "xml.doctype", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
1250 { &hf_dtd_tag, {"DTD Tag", "xml.dtdtag", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
1251 { &unknown_ns.hf_cdata, {"CDATA", "xml.cdata", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
1252 { &unknown_ns.hf_tag, {"Tag", "xml.tag", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }},
1253 { &xml_ns.hf_cdata, {"Unknown", "xml.unknown", FT_STRING, BASE_NONE, NULL, 0, "", HFILL }}
1255 module_t* xml_module;
1257 hf_arr = g_array_new(FALSE,FALSE,sizeof(hf_register_info));
1258 ett_arr = g_array_new(FALSE,FALSE,sizeof(gint*));
1260 g_array_append_vals(hf_arr,hf_base,array_length(hf_base));
1261 g_array_append_vals(ett_arr,ett_base,array_length(ett_base));
1265 xml_ns.hf_tag = proto_register_protocol("eXtensible Markup Language", "XML", xml_ns.name);
1267 proto_register_field_array(xml_ns.hf_tag, (hf_register_info*)g_array_data(hf_arr), hf_arr->len);
1268 proto_register_subtree_array((gint**)g_array_data(ett_arr), ett_arr->len);
1270 xml_module = prefs_register_protocol(xml_ns.hf_tag,apply_prefs);
1271 prefs_register_bool_preference(xml_module, "heuristic", "Use Heuristics for media types",
1272 "Try to recognize XML for unknown media types",
1273 &pref_heuristic_media);
1274 prefs_register_bool_preference(xml_module, "heuristic_tcp", "Use Heuristics for tcp",
1275 "Try to recognize XML for unknown TCP ports",
1276 &pref_heuristic_tcp);
1277 prefs_register_range_preference(xml_module, "tcp.port", "TCP Ports",
1279 &global_xml_tcp_range, 65535);
1281 g_array_free(hf_arr,FALSE);
1282 g_array_free(ett_arr,TRUE);
1284 register_dissector("xml", dissect_xml, xml_ns.hf_tag);
1288 xml_tcp_range = range_empty();
1293 static void add_dissector_media(gpointer k, gpointer v _U_, gpointer p _U_) {
1294 dissector_add_string("media_type", (gchar*)k, xml_handle);
1298 proto_reg_handoff_xml(void)
1301 xml_handle = find_dissector("xml");
1303 g_hash_table_foreach(media_types,add_dissector_media,NULL);