Get rid of get_ber_last_reated_item() and fix dissection of wIN-TriggerList.
[obnox/wireshark/wip.git] / epan / dissectors / packet-xml.c
1 /* packet-xml.c
2  * wireshark's xml dissector .
3  *
4  * (C) 2005, Luis E. Garcia Ontanon.
5  *
6  * $Id$
7  *
8  * Refer to the AUTHORS file or the AUTHORS section in the man page
9  * for contacting the author(s) of this file.
10  *
11  * Wireshark - Network traffic analyzer
12  * By Gerald Combs <gerald@wireshark.org>
13  * Copyright 1998 Gerald Combs
14  *
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.
19  *
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.
24  *
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.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #ifdef HAVE_DIRENT_H
35 #include <dirent.h>
36 #endif
37
38 #include <ctype.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdarg.h>
42 #include <errno.h>
43
44 #include <stdio.h>
45
46 #include <glib.h>
47 #include <epan/emem.h>
48 #include <epan/packet.h>
49 #include <epan/strutil.h>
50 #include <epan/tvbparse.h>
51 #include <epan/dtd.h>
52 #include <epan/report_err.h>
53 #include <epan/filesystem.h>
54 #include <epan/prefs.h>
55 #include <epan/garrayfix.h>
56
57 typedef struct _xml_ns_t {
58     /* the name of this namespace */
59         gchar* name;
60
61     /* its fully qualified name */
62         gchar* fqn;
63
64         /* the contents of the whole element from <> to </> */
65         int hf_tag;
66
67         /* chunks of cdata from <> to </> excluding sub tags */
68         int hf_cdata;
69
70     /* the subtree for its sub items  */
71         gint ett;
72
73         GHashTable* attributes;
74     /*  key:   the attribute name
75         value: hf_id of what's between quotes */
76
77     /* the namespace's namespaces */
78     GHashTable* elements;
79     /*  key:   the element name
80         value: the child namespace */
81
82         GPtrArray* element_names;
83     /* imported directly from the parser and used while building the namespace */
84
85 } xml_ns_t;
86
87 typedef struct {
88         proto_tree* tree;
89         proto_item* item;
90         proto_item* last_item;
91         xml_ns_t* ns;
92         int start_offset;
93 } xml_frame_t;
94
95 struct _attr_reg_data {
96         GArray* hf;
97         gchar* basename;
98 };
99
100
101 static gint ett_dtd = -1;
102 static gint ett_xmpli = -1;
103
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;
109
110 /* dissector handles */
111 static dissector_handle_t xml_handle;
112
113 /* parser definitions */
114 static tvbparse_wanted_t* want;
115 static tvbparse_wanted_t* want_ignore;
116 static tvbparse_wanted_t* want_heur;
117
118 static GHashTable* xmpli_names;
119 static GHashTable* media_types;
120
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;
124
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;
131
132 #define XML_CDATA -1000
133 #define XML_SCOPED_NAME -1001
134
135
136 GArray* hf_arr;
137 GArray* ett_arr;
138
139 static const gchar* default_media_types[] = {
140         "text/xml",
141         "text/vnd.wap.wml",
142         "text/vnd.wap.si",
143         "text/vnd.wap.sl",
144         "text/vnd.wap.co",
145         "text/vnd.wap.emn",
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",
159         "application/smil",
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",
171         "application/xml",
172         "application/xml-dtd",
173         "application/xpidf+xml",
174         "application/xslt+xml",
175         "image/svg+xml",
176 };
177
178 static void
179 dissect_xml(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
180 {
181         tvbparse_t* tt;
182         tvbparse_elem_t* tok = NULL;
183         static GPtrArray* stack = NULL;
184         xml_frame_t* current_frame;
185         char* colinfo_str;
186         
187         if(!tree) return;
188
189         if (stack != NULL)
190                 g_ptr_array_free(stack,TRUE);
191
192         stack = g_ptr_array_new();
193         current_frame = ep_alloc(sizeof(xml_frame_t));
194         g_ptr_array_add(stack,current_frame);
195
196         tt = tvbparse_init(tvb,0,-1,stack,want_ignore);
197         current_frame->start_offset = 0;
198
199         root_ns = NULL;
200
201         if (pinfo->match_string)
202                 root_ns = g_hash_table_lookup(media_types,pinfo->match_string);
203
204         if (! root_ns ) {
205                 root_ns = &xml_ns;
206                 colinfo_str = "/XML";
207         } else {
208                 colinfo_str = ep_strdup_printf("/%s",root_ns->name);
209                 g_strup(colinfo_str);
210         }
211
212         if (check_col(pinfo->cinfo, COL_PROTOCOL))
213                 col_append_str(pinfo->cinfo, COL_PROTOCOL, colinfo_str);
214
215         current_frame->ns = root_ns;
216
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;
220
221         while(( tok = tvbparse_get(tt, want) )) ;
222 }
223
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);
228         return TRUE;
229     } else {
230         return FALSE;
231     }
232 }
233
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);
237         int hfid;
238         proto_item* pi;
239
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) {
243                 hfid = tok->id;
244         } else {
245                 hfid = xml_ns.hf_cdata;
246         }
247
248         pi = proto_tree_add_item(current_frame->tree, hfid, tok->tvb, tok->offset, tok->len, FALSE);
249
250         proto_item_set_text(pi, "%s",
251                                                 tvb_format_text(tok->tvb,tok->offset,tok->len));
252 }
253
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);
257         proto_item* pi;
258         proto_tree* pt;
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);
262
263         int hf_tag;
264         gint ett;
265
266         g_strdown(name);
267         if (!ns) {
268                 hf_tag = hf_xmlpi;
269                 ett = ett_xmpli;
270         } else {
271                 hf_tag = ns->hf_tag;
272                 ett = ns->ett;
273         }
274
275         pi = proto_tree_add_item(current_frame->tree,hf_tag,tok->tvb,tok->offset,tok->len,FALSE);
276
277         proto_item_set_text(pi, "%s", tvb_format_text(tok->tvb,tok->offset,(name_tok->offset - tok->offset) + name_tok->len));
278
279         pt = proto_item_add_subtree(pi,ett);
280
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;
287
288         g_ptr_array_add(stack,current_frame);
289
290 }
291
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);
295
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));
299
300         if (stack->len > 1) {
301                 g_ptr_array_remove_index_fast(stack,stack->len - 1);
302         } else {
303                 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"[ ERROR: Closing an unopened xmpli tag ]");
304         }
305 }
306
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;
311     gchar* root_name;
312         gchar* name;
313         xml_ns_t* ns;
314         xml_frame_t* new_frame;
315         proto_item* pi;
316         proto_tree* pt;
317
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;
322
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);
325
326         nameroot_ns = g_hash_table_lookup(xml_ns.elements,root_name);
327
328         if(nameroot_ns) {
329             ns = g_hash_table_lookup(nameroot_ns->elements,name);
330             if (!ns) {
331                 ns = &unknown_ns;
332             }
333         } else {
334             ns = &unknown_ns;
335         }
336
337     } else {
338         name = (gchar*)tvb_get_ephemeral_string(name_tok->tvb,name_tok->offset,name_tok->len);
339         g_strdown(name);
340
341         if(current_frame->ns) {
342                         ns = g_hash_table_lookup(current_frame->ns->elements,name);
343
344                         if (!ns) {
345                                 if (! ( ns = g_hash_table_lookup(root_ns->elements,name) ) ) {
346                                         ns = &unknown_ns;
347                                 }
348                         }
349                 } else {
350                         ns = &unknown_ns;
351                 }
352     }
353
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));
356
357         pt = proto_item_add_subtree(pi,ns->ett);
358
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;
364         new_frame->ns = ns;
365
366         g_ptr_array_add(stack,new_frame);
367
368 }
369
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);
373
374         proto_item_append_text(current_frame->last_item,">");
375 }
376
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);
380
381         proto_item_append_text(current_frame->last_item,"/>");
382
383         if (stack->len > 1) {
384                 g_ptr_array_remove_index_fast(stack,stack->len - 1);
385         } else {
386                 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"[ ERROR: Closing an unopened tag ]");
387         }
388 }
389
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);
393
394         proto_item_set_len(current_frame->item, (tok->offset - current_frame->start_offset) + tok->len);
395
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));
398
399         if (stack->len > 1) {
400                 g_ptr_array_remove_index_fast(stack,stack->len - 1);
401         } else {
402                 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,
403                                                         "[ ERROR: Closing an unopened tag ]");
404         }
405 }
406
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);
413
414         proto_item_set_text(dtd_item,"%s",tvb_format_text(tok->tvb,tok->offset,tok->len));
415
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;
422
423         g_ptr_array_add(stack,current_frame);
424 }
425
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);
429
430         if (stack->len > 1) {
431                 g_ptr_array_remove_index_fast(stack,stack->len - 1);
432         } else {
433                 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,
434                                                         "[ ERROR: Closing an unopened tag ]");
435         }
436 }
437
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);
441
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);
446         } else {
447                 proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"[ ERROR: Closing an unopened tag ]");
448         }
449 }
450
451 static void get_attrib_value(void* tvbparse_data _U_, const void* wanted_data _U_, tvbparse_elem_t* tok) {
452         tok->data = tok->sub;
453 }
454
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;
460         int* hfidp;
461         int hfid;
462
463         g_strdown(name);
464         if(current_frame->ns && (hfidp = g_hash_table_lookup(current_frame->ns->attributes,name) )) {
465                 hfid = *hfidp;
466         } else {
467                 hfid = hf_unknowwn_attrib;
468                 value = tok;
469         }
470
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));
473
474 }
475
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);
479
480         proto_tree_add_text(current_frame->tree,tok->tvb,tok->offset,tok->len,"[ ERROR: Unrecognized text ]");
481
482 }
483
484
485
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);
489
490     tvbparse_wanted_t* want_scoped_name = tvbparse_set_seq(XML_SCOPED_NAME, NULL, NULL, NULL,
491                                                            want_name,
492                                                            tvbparse_char(-1,":",NULL,NULL,NULL),
493                                                            want_name,
494                                                            NULL);
495
496     tvbparse_wanted_t* want_tag_name = tvbparse_set_oneof(0, NULL, NULL, NULL,
497                                                           want_scoped_name,
498                                                           want_name,
499                                                           NULL);
500
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),
505                                                               want_name,
506                                                               NULL);
507
508         tvbparse_wanted_t* want_attributes = tvbparse_one_or_more(-1, NULL, NULL, NULL,
509                                                                                                                           tvbparse_set_seq(-1, NULL, NULL, after_attrib,
510                                                                                want_attr_name,
511                                                                                                                                                            tvbparse_char(-1,"=",NULL,NULL,NULL),
512                                                                                                                                                            want_attrib_value,
513                                                                                                                                                            NULL));
514
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),
518                                                                                                                  NULL);
519
520         tvbparse_wanted_t* want_stopxmlpi = tvbparse_string(-1,"?>",NULL,NULL,after_xmlpi);
521
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),
526                                                                       TP_UNTIL_INCLUDE),
527                                                        NULL);
528
529     tvbparse_wanted_t* want_xmlpi = tvbparse_set_seq(hf_xmlpi,NULL,before_xmpli,NULL,
530                                                      tvbparse_string(-1,"<?",NULL,NULL,NULL),
531                                                      want_name,
532                                                      tvbparse_set_oneof(-1,NULL,NULL,NULL,
533                                                                         want_stopxmlpi,
534                                                                         tvbparse_set_seq(-1,NULL,NULL,NULL,
535                                                                                          want_attributes,
536                                                                                          want_stopxmlpi,
537                                                                                          NULL),
538                                                                         NULL),
539                                                      NULL);
540
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),
544                                                            want_tag_name,
545                                                            tvbparse_char(-1, ">", NULL, NULL, NULL),
546                                                            NULL);
547
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,
554                                                                                                  want_name,
555                                                                                                  tvbparse_char(-1,"[",NULL,NULL,NULL),
556                                                                                                  NULL),
557                                                                                 tvbparse_set_seq(-1,NULL,NULL,pop_stack,
558                                                                                                  want_name,
559                                                                                                  tvbparse_set_oneof(-1,NULL,NULL,NULL,
560                                                                                                                     tvbparse_casestring(-1,"PUBLIC",NULL,NULL,NULL),
561                                                                                                                     tvbparse_casestring(-1,"SYSTEM",NULL,NULL,NULL),
562                                                                                                                     NULL),
563                                                                                                  tvbparse_until(-1,NULL,NULL,NULL,
564                                                                                                                 tvbparse_char(-1,">",NULL,NULL,NULL),
565                                                                                                                 TP_UNTIL_INCLUDE),
566                                                                                                  NULL),
567                                                                                 NULL),
568                                                              NULL);
569
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),
575                                                                       TP_UNTIL_INCLUDE),
576                                                        NULL);
577
578     tvbparse_wanted_t* want_tag = tvbparse_set_seq(-1, NULL, before_tag, NULL,
579                                                    tvbparse_char(-1,"<",NULL,NULL,NULL),
580                                                    want_tag_name,
581                                                    tvbparse_set_oneof(-1,NULL,NULL,NULL,
582                                                                       tvbparse_set_seq(-1,NULL,NULL,NULL,
583                                                                                        want_attributes,
584                                                                                        want_stoptag,
585                                                                                        NULL),
586                                                                       want_stoptag,
587                                                                       NULL),
588                                                    NULL);
589
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),
593                                                          NULL);
594
595     want_ignore = tvbparse_chars(-1,1,0," \t\r\n",NULL,NULL,NULL);
596
597
598         want = tvbparse_set_oneof(-1, NULL, NULL, NULL,
599                                                           want_comment,
600                                                           want_xmlpi,
601                                                           want_closing_tag,
602                                                           want_doctype_start,
603                                                           want_dtd_close,
604                                                           want_dtd_tag,
605                                                           want_tag,
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),
608                                                           NULL);
609
610     want_heur = tvbparse_set_oneof(-1, NULL, NULL, NULL,
611                                    want_comment,
612                                    want_xmlpi,
613                                    want_doctype_start,
614                                    want_dtd_tag,
615                                    want_tag,
616                                    NULL);
617
618 }
619
620
621 static xml_ns_t* xml_new_namespace(GHashTable* hash, gchar* name, ...) {
622         xml_ns_t* ns = g_malloc(sizeof(xml_ns_t));
623         va_list ap;
624         gchar* attr_name;
625
626         ns->name = g_strdup(name);
627         ns->hf_tag = -1;
628         ns->hf_cdata = -1;
629         ns->ett = -1;
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);
632
633         va_start(ap,name);
634
635         while(( attr_name = va_arg(ap,gchar*) )) {
636                 int* hfp = g_malloc(sizeof(int));
637                 *hfp = -1;
638                 g_hash_table_insert(ns->attributes,g_strdup(attr_name),hfp);
639         };
640
641         va_end(ap);
642
643         g_hash_table_insert(hash,ns->name,ns);
644
645         return ns;
646 }
647
648
649 static void add_xml_field(GArray* hfs, int* p_id, gchar* name, gchar* fqn) {
650         hf_register_info hfri;
651
652         hfri.p_id = p_id;
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 = "";
660         hfri.hfinfo.id = 0;
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;
666
667         g_array_append_val(hfs,hfri);
668 }
669
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);
674 }
675
676
677 static void add_xmlpi_namespace(gpointer k _U_, gpointer v, gpointer p) {
678         xml_ns_t* ns = v;
679         gchar* basename = g_strdup_printf("%s.%s",(gchar*)p,ns->name);
680         gint* ett_p = &(ns->ett);
681         struct _attr_reg_data d;
682
683         add_xml_field(hf_arr, &(ns->hf_tag), basename, basename);
684
685         g_array_append_val(ett_arr,ett_p);
686
687         d.basename = basename;
688         d.hf = hf_arr;
689
690         g_hash_table_foreach(ns->attributes,add_xml_attribute_names,&d);
691
692 }
693
694 static void destroy_dtd_data(dtd_build_data_t* dtd_data) {
695
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);
700
701     g_string_free(dtd_data->error,TRUE);
702
703
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);
707                 g_free(nl);
708         }
709
710         g_ptr_array_free(dtd_data->elements,TRUE);
711
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);
715                 g_free(nl);
716         }
717
718         g_ptr_array_free(dtd_data->attributes,TRUE);
719
720         g_free(dtd_data);
721
722 }
723
724
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));
728         GHashTable* dst = p;
729
730         *value = -1;
731         g_hash_table_insert(dst,key,value);
732
733 }
734
735 static GHashTable* copy_attributes_hash(GHashTable* src) {
736         GHashTable* dst = g_hash_table_new(g_str_hash,g_str_equal);
737
738         g_hash_table_foreach(src,copy_attrib_item,dst);
739
740         return dst;
741 }
742
743 static xml_ns_t* duplicate_element(xml_ns_t* orig) {
744         xml_ns_t* new_item  = g_malloc(sizeof(xml_ns_t));
745         guint i;
746
747         new_item->name = g_strdup(orig->name);
748         new_item->hf_tag = -1;
749         new_item->hf_cdata = -1;
750         new_item->ett = -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();
754
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));
758         }
759
760         return new_item;
761 }
762
763 static gchar* fully_qualified_name(GPtrArray* hier, gchar* name, gchar* proto_name) {
764         guint i;
765         GString* s = g_string_new(proto_name);
766         gchar* str;
767         g_string_append(s,".");
768         
769         for (i = 1; i < hier->len; i++) {
770                 g_string_sprintfa(s, "%s.",(gchar*)g_ptr_array_index(hier,i));
771         }
772
773         g_string_append(s,name);
774         str = s->str;
775         g_string_free(s,FALSE);
776
777         return str;
778 }
779
780
781 static xml_ns_t* make_xml_hier(gchar* elem_name,
782                                                                   xml_ns_t* root,
783                                                                   GHashTable* elements,
784                                                                   GPtrArray* hier,
785                                                                   GString* error,
786                                                                   GArray* hfs,
787                                                                   GArray* etts,
788                                                                   char* proto_name) {
789         xml_ns_t* new;
790         xml_ns_t* orig;
791         gchar* fqn;
792         gint* ett_p;
793         struct _attr_reg_data d;
794     gboolean recurred = FALSE;
795     guint i;
796
797     if ( g_str_equal(elem_name,root->name) ) {
798         return NULL;
799     }
800
801         if (! ( orig = g_hash_table_lookup(elements,elem_name) )) {
802                 g_string_sprintfa(error,"element '%s' is not defined\n", elem_name);
803                 return NULL;
804         }
805
806     for (i = 0; i < hier->len; i++) {
807         if( strcmp(elem_name,(gchar*) g_ptr_array_index(hier,i) ) == 0 ) {
808             recurred = TRUE;
809         }
810     }
811
812     if (recurred) {
813         return NULL;
814     }
815
816         fqn = fully_qualified_name(hier,elem_name,proto_name);
817
818         new = duplicate_element(orig);
819         new->fqn = fqn;
820
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);
823
824         ett_p = &new->ett;
825         g_array_append_val(etts,ett_p);
826
827         d.basename = fqn;
828         d.hf = hfs;
829
830         g_hash_table_foreach(new->attributes,add_xml_attribute_names,&d);
831
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;
835
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);
839
840                 if (child_element) {
841                         g_hash_table_insert(new->elements,child_element->name,child_element);
842                 }
843     }
844
845         g_ptr_array_free(new->element_names,TRUE);
846         new->element_names = NULL;
847         return new;
848 }
849
850 static gboolean free_both(gpointer k, gpointer v, gpointer p _U_) {
851     g_free(k);
852     g_free(v);
853     return TRUE;
854 }
855
856 static gboolean free_elements(gpointer k _U_, gpointer v, gpointer p _U_) {
857     xml_ns_t* e = v;
858     g_free(e->name);
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);
862
863     while (e->element_names->len) {
864         g_free(g_ptr_array_remove_index(e->element_names,0));
865     }
866
867     g_ptr_array_free(e->element_names,TRUE);
868     g_free(e);
869
870     return TRUE;
871 }
872
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;
877         GArray* hfs;
878         GArray* etts;
879         GPtrArray* hier;
880         gchar* curr_name;
881     GPtrArray* element_names = g_ptr_array_new();
882
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));
887
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);
891
892                 element->name = nl->name;
893                 element->element_names = nl->list;
894                 element->hf_tag = -1;
895                 element->hf_cdata = -1;
896                 element->ett = -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);
899
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);
903         } else {
904             g_hash_table_insert(elements,element->name,element);
905             g_ptr_array_add(element_names,g_strdup(element->name));
906                 }
907
908                 g_free(nl);
909         }
910
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);
915
916                 if (!element) {
917                         g_string_sprintfa(errors,"element %s is not defined\n", nl->name);
918
919             goto next_attribute;
920                 }
921
922                 while(nl->list->len) {
923                         gchar* name = g_ptr_array_remove_index(nl->list,0);
924                         int* id_p = g_malloc(sizeof(int));
925
926                         *id_p = -1;
927                         g_hash_table_insert(element->attributes,name,id_p);
928                 }
929
930 next_attribute:
931                 g_free(nl->name);
932                 g_ptr_array_free(nl->list,TRUE);
933                 g_free(nl);
934         }
935
936     /* if a proto_root is defined in the dtd we'll use that as root */
937         if( dtd_data->proto_root ) {
938         if(root_name)
939             g_free(root_name);
940         root_name = g_strdup(dtd_data->proto_root);
941         }
942
943     /* we use a stack with the names to avoid recurring infinitelly */
944         hier = g_ptr_array_new();
945
946     /*
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
949      */
950         if( ! dtd_data->proto_name ) {
951                 hfs = hf_arr;
952                 etts = ett_arr;
953                 g_ptr_array_add(hier,g_strdup("xml"));
954                 root_element = &xml_ns;
955         } else {
956         /*
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.
959          */
960             hfs = g_array_new(FALSE,FALSE,sizeof(hf_register_info));
961             etts = g_array_new(FALSE,FALSE,sizeof(gint*));
962         }
963
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;
973
974     /*
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
978      */
979     if (dtd_data->recursion) {
980         xml_ns_t* orig_root;
981
982         make_xml_hier(root_name, root_element, elements,hier,errors,hfs,etts,dtd_data->proto_name);
983
984         g_hash_table_insert(root_element->elements,root_element->name,root_element);
985
986         orig_root = g_hash_table_lookup(elements,root_name);
987
988         /* if the root element was defined copy its attrlist to the child */
989         if(orig_root) {
990             struct _attr_reg_data d;
991
992             d.basename = dtd_data->proto_name;
993             d.hf = hfs;
994
995             root_element->attributes = copy_attributes_hash(orig_root->attributes);
996             g_hash_table_foreach(root_element->attributes,add_xml_attribute_names,&d);
997         } else {
998             root_element->attributes = g_hash_table_new(g_str_hash,g_str_equal);
999         }
1000
1001         /* we then create all the sub hierachies to catch the recurred cases */
1002         g_ptr_array_add(hier,root_name);
1003
1004         while(root_element->element_names->len) {
1005             curr_name = g_ptr_array_remove_index(root_element->element_names,0);
1006
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);
1010             }
1011
1012             g_free(curr_name);
1013         }
1014
1015     } else {
1016         /* a flat namespace */
1017         g_ptr_array_add(hier,root_name);
1018
1019         root_element->attributes = g_hash_table_new(g_str_hash,g_str_equal);
1020
1021         while(root_element->element_names->len) {
1022             xml_ns_t* new;
1023             gint* ett_p;
1024             struct _attr_reg_data d;
1025
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);
1029
1030             add_xml_field(hfs, &(new->hf_tag), curr_name, new->fqn);
1031             add_xml_field(hfs, &(new->hf_cdata), curr_name, new->fqn);
1032
1033             d.basename = new->fqn;
1034             d.hf = hfs;
1035
1036             g_hash_table_foreach(new->attributes,add_xml_attribute_names,&d);
1037
1038             ett_p = &new->ett;
1039             g_array_append_val(etts,ett_p);
1040
1041             g_ptr_array_free(new->element_names,TRUE);
1042
1043             g_hash_table_insert(root_element->elements,new->name,new);
1044         }
1045     }
1046
1047     g_ptr_array_free(element_names,TRUE);
1048
1049         g_ptr_array_free(hier,TRUE);
1050
1051     /*
1052      * if we were given a proto_name the namespace will be registered
1053      * as an indipendent protocol.
1054      */
1055         if( dtd_data->proto_name ) {
1056         gint* ett_p;
1057
1058                 if ( ! dtd_data->description) {
1059                         dtd_data->description = g_strdup(root_name);
1060                 }
1061
1062         ett_p = &root_element->ett;
1063         g_array_append_val(etts,ett_p);
1064
1065         add_xml_field(hfs, &root_element->hf_cdata, root_element->name, root_element->fqn);
1066
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);
1070
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;
1074                 }
1075
1076                 dtd_data->description = NULL;
1077                 dtd_data->proto_name = NULL;
1078                 g_array_free(hfs,FALSE);
1079                 g_array_free(etts,TRUE);
1080         }
1081
1082     g_hash_table_insert(xml_ns.elements,root_element->name,root_element);
1083
1084     g_hash_table_foreach_remove(elements,free_elements,NULL);
1085     g_hash_table_destroy(elements);
1086
1087         destroy_dtd_data(dtd_data);
1088
1089     if (root_name)
1090         g_free(root_name);
1091 }
1092
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)
1100 #else /* GLIB 2 */
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)
1107 #endif
1108
1109 static void init_xml_names(void) {
1110         xml_ns_t* xmlpi_xml_ns;
1111         guint i;
1112         DIRECTORY_T* dir;
1113         const FILE_T* file;
1114         const gchar* filename;
1115         gchar* dirname;
1116
1117 #if GLIB_MAJOR_VERSION >= 2
1118         GError** dummy = g_malloc(sizeof(GError *));
1119         *dummy = NULL;
1120 #endif
1121
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);
1124
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);
1127
1128         xmlpi_xml_ns = xml_new_namespace(xmpli_names,"xml","version","encoding","standalone",NULL);
1129
1130         g_hash_table_destroy(xmlpi_xml_ns->elements);
1131         xmlpi_xml_ns->elements = NULL;
1132
1133
1134         dirname = get_persconffile_path("dtds", FALSE);
1135
1136         if (test_for_directory(dirname) != EISDIR) {
1137                 /* Although dir isn't a directory it may still use memory */
1138                 g_free(dirname);
1139                 dirname = get_datafile_path("dtds");
1140         }
1141
1142         if (test_for_directory(dirname) == EISDIR) {
1143
1144             if ((dir = OPENDIR_OP(dirname)) != NULL) {
1145                 while ((file = DIRGETNEXT_OP(dir)) != NULL) {
1146                     guint namelen;
1147                     filename = GETFNAME_OP(file);
1148
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;
1154
1155                         if (errors->len) {
1156                             report_failure("Dtd Preparser in file %s%c%s: %s",dirname,G_DIR_SEPARATOR,filename,errors->str);
1157                             continue;
1158                         }
1159
1160                         dtd_data = dtd_parse(preparsed);
1161
1162                         g_string_free(preparsed,TRUE);
1163
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);
1167                             continue;
1168                         }
1169
1170                         register_dtd(dtd_data,errors);
1171
1172                         if (errors->len) {
1173                             report_failure("Dtd Registration in file: %s%c%s: %s",dirname,G_DIR_SEPARATOR,filename,errors->str);
1174                             g_string_free(errors,TRUE);
1175                             continue;
1176                         }
1177                     }
1178                 }
1179
1180                 CLOSEDIR_OP(dir);
1181             }
1182         }
1183
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);
1187                 }
1188         }
1189
1190         g_hash_table_foreach(xmpli_names,add_xmlpi_namespace,"xml.xmlpi");
1191
1192 #if GLIB_MAJOR_VERSION >= 2
1193         g_free(dummy);
1194 #endif
1195 }
1196
1197 static void range_delete_xml_tcp_callback(guint32 port) {
1198         dissector_delete("tcp.port", port, xml_handle);
1199 }
1200
1201 static void range_add_xml_tcp_callback(guint32 port) {
1202         dissector_add("tcp.port", port, xml_handle);
1203 }
1204
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;
1212                 } else {
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;
1217                 }
1218         }
1219         
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;
1224                 } else {
1225                         heur_dissector_delete("tcp", dissect_xml_heur, xml_ns.hf_tag);
1226                         pref_heuristic_tcp_save = FALSE;
1227                 }
1228         }
1229         
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);       
1234 }
1235
1236 void
1237 proto_register_xml(void) {
1238         static gint *ett_base[] = {
1239                 &unknown_ns.ett,
1240                 &xml_ns.ett,
1241                 &ett_dtd,
1242                 &ett_xmpli
1243         };
1244
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 }}
1254     };
1255         module_t* xml_module;
1256
1257         hf_arr = g_array_new(FALSE,FALSE,sizeof(hf_register_info));
1258         ett_arr = g_array_new(FALSE,FALSE,sizeof(gint*));
1259
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));
1262
1263         init_xml_names();
1264
1265         xml_ns.hf_tag = proto_register_protocol("eXtensible Markup Language", "XML", xml_ns.name);
1266
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);
1269
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",
1278                                                                         "TCP Ports range",
1279                                                                         &global_xml_tcp_range, 65535);
1280         
1281     g_array_free(hf_arr,FALSE);
1282     g_array_free(ett_arr,TRUE);
1283
1284         register_dissector("xml", dissect_xml, xml_ns.hf_tag);
1285
1286         init_xml_parser();
1287
1288         xml_tcp_range = range_empty();
1289
1290
1291 }
1292
1293 static void add_dissector_media(gpointer k, gpointer v _U_, gpointer p _U_) {
1294         dissector_add_string("media_type", (gchar*)k, xml_handle);
1295 }
1296
1297 void
1298 proto_reg_handoff_xml(void)
1299 {
1300
1301         xml_handle = find_dissector("xml");
1302
1303         g_hash_table_foreach(media_types,add_dissector_media,NULL);
1304
1305 }