Fix crash bug by allocating correct size of struct (used with SR tracking).
[obnox/wireshark/wip.git] / print.c
1 /* print.c
2  * Routines for printing packet analysis trees.
3  *
4  * $Id$
5  *
6  * Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <string.h>
33
34 #include <epan/epan.h>
35 #include <epan/epan_dissect.h>
36 #include <epan/tvbuff.h>
37 #include <epan/packet.h>
38 #include <epan/emem.h>
39
40 #include "packet-range.h"
41 #include "print.h"
42 #include "ps.h"
43 #include "version_info.h"
44 #include <wsutil/file_util.h>
45 #include <epan/charsets.h>
46 #include <epan/dissectors/packet-data.h>
47 #include <epan/dissectors/packet-frame.h>
48
49 #define PDML_VERSION "0"
50 #define PSML_VERSION "0"
51
52 typedef struct {
53         int                     level;
54         print_stream_t          *stream;
55         gboolean                success;
56         GSList                  *src_list;
57         print_dissections_e     print_dissections;
58         gboolean                print_hex_for_data;
59         char_enc                encoding;
60         epan_dissect_t          *edt;
61 } print_data;
62
63 typedef struct {
64         int                     level;
65         FILE                    *fh;
66         GSList                  *src_list;
67         epan_dissect_t          *edt;
68 } write_pdml_data;
69
70 typedef struct {
71     output_fields_t* fields;
72         epan_dissect_t          *edt;
73 } write_field_data_t;
74
75 struct _output_fields {
76     gboolean print_header;
77     gchar separator;
78     GPtrArray* fields;
79     GHashTable* field_indicies;
80     const gchar** field_values;
81     gchar quote;
82 };
83
84 static const gchar* get_field_hex_value(GSList* src_list, field_info *fi);
85 static void proto_tree_print_node(proto_node *node, gpointer data);
86 static void proto_tree_write_node_pdml(proto_node *node, gpointer data);
87 static const guint8 *get_field_data(GSList *src_list, field_info *fi);
88 static void write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi);
89 static gboolean print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
90     guint length, char_enc encoding);
91 static void ps_clean_string(unsigned char *out, const unsigned char *in,
92                         int outbuf_size);
93 static void print_escaped_xml(FILE *fh, const char *unescaped_string);
94
95 static void print_pdml_geninfo(proto_tree *tree, FILE *fh);
96
97 static void proto_tree_get_node_field_values(proto_node *node, gpointer data);
98
99 static FILE *
100 open_print_dest(int to_file, const char *dest)
101 {
102         FILE    *fh;
103
104         /* Open the file or command for output */
105         if (to_file)
106                 fh = ws_fopen(dest, "w");
107         else
108                 fh = popen(dest, "w");
109
110         return fh;
111 }
112
113 static gboolean
114 close_print_dest(int to_file, FILE *fh)
115 {
116         /* Close the file or command */
117         if (to_file)
118                 return (fclose(fh) == 0);
119         else
120                 return (pclose(fh) == 0);
121 }
122
123 #define MAX_PS_LINE_LENGTH 256
124
125 gboolean
126 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
127     print_stream_t *stream)
128 {
129         print_data data;
130
131         /* Create the output */
132         data.level = 0;
133         data.stream = stream;
134         data.success = TRUE;
135         data.src_list = edt->pi.data_src;
136         data.encoding = edt->pi.fd->flags.encoding;
137         data.print_dissections = print_args->print_dissections;
138         /* If we're printing the entire packet in hex, don't
139            print uninterpreted data fields in hex as well. */
140         data.print_hex_for_data = !print_args->print_hex;
141         data.edt = edt;
142
143         proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
144         return data.success;
145 }
146
147 #define MAX_INDENT      160
148
149 /* Print a tree's data, and any child nodes. */
150 static
151 void proto_tree_print_node(proto_node *node, gpointer data)
152 {
153         field_info      *fi = PNODE_FINFO(node);
154         print_data      *pdata = (print_data*) data;
155         const guint8    *pd;
156         gchar           label_str[ITEM_LABEL_LENGTH];
157         gchar           *label_ptr;
158
159         g_assert(fi && "dissection with an invisible proto tree?");
160
161         /* Don't print invisible entries. */
162         if (PROTO_ITEM_IS_HIDDEN(node))
163                 return;
164
165         /* Give up if we've already gotten an error. */
166         if (!pdata->success)
167                 return;
168
169         /* was a free format label produced? */
170         if (fi->rep) {
171                 label_ptr = fi->rep->representation;
172         }
173         else { /* no, make a generic label */
174                 label_ptr = label_str;
175                 proto_item_fill_label(fi, label_str);
176         }
177
178         if (PROTO_ITEM_IS_GENERATED(node)) {
179                 label_ptr = g_strdup_printf("[%s]", label_ptr);
180         }
181
182         if (!print_line(pdata->stream, pdata->level, label_ptr)) {
183                 pdata->success = FALSE;
184                 return;
185         }
186
187         if (PROTO_ITEM_IS_GENERATED(node)) {
188                 g_free(label_ptr);
189         }
190
191         /* If it's uninterpreted data, dump it (unless our caller will
192            be printing the entire packet in hex). */
193         if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
194                 /*
195                  * Find the data for this field.
196                  */
197                 pd = get_field_data(pdata->src_list, fi);
198                 if (pd) {
199                         if (!print_hex_data_buffer(pdata->stream, pd,
200                             fi->length, pdata->encoding)) {
201                                 pdata->success = FALSE;
202                                 return;
203                         }
204                 }
205         }
206
207         /* If we're printing all levels, or if this node is one with a
208            subtree and its subtree is expanded, recurse into the subtree,
209            if it exists. */
210         g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
211         if (pdata->print_dissections == print_dissections_expanded ||
212             (pdata->print_dissections == print_dissections_as_displayed &&
213                 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
214                 if (node->first_child != NULL) {
215                         pdata->level++;
216                         proto_tree_children_foreach(node,
217                                 proto_tree_print_node, pdata);
218                         pdata->level--;
219                         if (!pdata->success)
220                                 return;
221                 }
222         }
223 }
224
225 void
226 write_pdml_preamble(FILE *fh)
227 {
228         fputs("<?xml version=\"1.0\"?>\n", fh);
229         fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
230         fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
231 }
232
233 void
234 proto_tree_write_pdml(epan_dissect_t *edt, FILE *fh)
235 {
236         write_pdml_data data;
237
238         /* Create the output */
239         data.level = 0;
240         data.fh = fh;
241         data.src_list = edt->pi.data_src;
242         data.edt = edt;
243
244         /* We shouldn't be called with a NULL pointer here because we've
245          * created a visible protocol tree */
246         g_assert(data.src_list);
247
248         fprintf(fh, "<packet>\n");
249
250         /* Print a "geninfo" protocol as required by PDML */
251         print_pdml_geninfo(edt->tree, fh);
252
253         proto_tree_children_foreach(edt->tree, proto_tree_write_node_pdml,
254             &data);
255
256         fprintf(fh, "</packet>\n\n");
257 }
258
259 /* Write out a tree's data, and any child nodes, as PDML */
260 static void
261 proto_tree_write_node_pdml(proto_node *node, gpointer data)
262 {
263         field_info      *fi = PNODE_FINFO(node);
264         write_pdml_data *pdata = (write_pdml_data*) data;
265         const gchar     *label_ptr;
266         gchar           label_str[ITEM_LABEL_LENGTH];
267         char            *dfilter_string;
268         size_t          chop_len;
269         int             i;
270         gboolean wrap_in_fake_protocol;
271
272         g_assert(fi && "dissection with an invisible proto tree?");
273
274         /* Will wrap up top-level field items inside a fake protocol wrapper to
275            preserve the PDML schema */
276         wrap_in_fake_protocol =
277             (((fi->hfinfo->type != FT_PROTOCOL) ||
278              (fi->hfinfo->id == proto_data)) &&
279             (pdata->level == 0));
280
281         /* Indent to the correct level */
282         for (i = -1; i < pdata->level; i++) {
283                 fputs("  ", pdata->fh);
284         }
285
286         if (wrap_in_fake_protocol) {
287                 /* Open fake protocol wrapper */
288                 fputs("<proto name=\"fake-field-wrapper\">\n", pdata->fh);
289
290                 /* Indent to increased level before writint out field */
291                 pdata->level++;
292                 for (i = -1; i < pdata->level; i++) {
293                         fputs("  ", pdata->fh);
294                 }
295         }
296
297         /* Text label. It's printed as a field with no name. */
298         if (fi->hfinfo->id == hf_text_only) {
299                 /* Get the text */
300                 if (fi->rep) {
301                         label_ptr = fi->rep->representation;
302                 }
303                 else {
304                         label_ptr = "";
305                 }
306
307                 /* Show empty name since it is a required field */
308                 fputs("<field name=\"", pdata->fh);
309                 fputs("\" show=\"", pdata->fh);
310                 print_escaped_xml(pdata->fh, label_ptr);
311
312                 fprintf(pdata->fh, "\" size=\"%d", fi->length);
313                 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
314
315                 fputs("\" value=\"", pdata->fh);
316                 write_pdml_field_hex_value(pdata, fi);
317
318                 if (node->first_child != NULL) {
319                         fputs("\">\n", pdata->fh);
320                 }
321                 else {
322                         fputs("\"/>\n", pdata->fh);
323                 }
324         }
325
326         /* Uninterpreted data, i.e., the "Data" protocol, is
327          * printed as a field instead of a protocol. */
328         else if (fi->hfinfo->id == proto_data) {
329
330                 /* Write out field with data */
331                 fputs("<field name=\"data\" value=\"", pdata->fh);
332                 write_pdml_field_hex_value(pdata, fi);
333                 fputs("\"/>\n", pdata->fh);
334         }
335         /* Normal protocols and fields */
336         else {
337                 if (fi->hfinfo->type == FT_PROTOCOL) {
338                         fputs("<proto name=\"", pdata->fh);
339                 }
340                 else {
341                         fputs("<field name=\"", pdata->fh);
342                 }
343                 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
344
345 #if 0
346         /* PDML spec, see:
347          * http://analyzer.polito.it/30alpha/docs/dissectors/PDMLSpec.htm
348          *
349          * the show fields contains things in 'human readable' format
350          * showname: contains only the name of the field
351          * show: contains only the data of the field
352          * showdtl: contains additional details of the field data
353          * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
354          *
355          * XXX - the showname shouldn't contain the field data itself
356          * (like it's contained in the fi->rep->representation).
357          * Unfortunately, we don't have the field data representation for
358          * all fields, so this isn't currently possible */
359                 fputs("\" showname=\"", pdata->fh);
360                 print_escaped_xml(pdata->fh, fi->hfinfo->name);
361 #endif
362
363                 if (fi->rep) {
364                         fputs("\" showname=\"", pdata->fh);
365                         print_escaped_xml(pdata->fh, fi->rep->representation);
366                 }
367                 else {
368                         label_ptr = label_str;
369                         proto_item_fill_label(fi, label_str);
370                         fputs("\" showname=\"", pdata->fh);
371                         print_escaped_xml(pdata->fh, label_ptr);
372                 }
373
374                 if (PROTO_ITEM_IS_HIDDEN(node))
375                         fprintf(pdata->fh, "\" hide=\"yes");
376
377                 fprintf(pdata->fh, "\" size=\"%d", fi->length);
378                 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
379 /*              fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
380
381                 /* show, value, and unmaskedvalue attributes */
382                 switch (fi->hfinfo->type)
383                 {
384                 case FT_PROTOCOL:
385                         break;
386                 case FT_NONE:
387                         fputs("\" show=\"\" value=\"",  pdata->fh);
388                         break;
389                 default:
390                         /* XXX - this is a hack until we can just call
391                          * fvalue_to_string_repr() for *all* FT_* types. */
392                         dfilter_string = proto_construct_match_selected_string(fi,
393                             pdata->edt);
394                         if (dfilter_string != NULL) {
395                                 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
396
397                                 /* XXX - Remove double-quotes. Again, once we
398                                  * can call fvalue_to_string_repr(), we can
399                                  * ask it not to produce the version for
400                                  * display-filters, and thus, no
401                                  * double-quotes. */
402                                 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
403                                         dfilter_string[strlen(dfilter_string)-1] = '\0';
404                                         chop_len++;
405                                 }
406
407                                 fputs("\" show=\"", pdata->fh);
408                                 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
409                         }
410
411                         /*
412                          * XXX - should we omit "value" for any fields?
413                          * What should we do for fields whose length is 0?
414                          * They might come from a pseudo-header or from
415                          * the capture header (e.g., time stamps), or
416                          * they might be generated fields.
417                          */
418                         if (fi->length > 0) {
419                                 fputs("\" value=\"", pdata->fh);
420
421                                 if (fi->hfinfo->bitmask!=0) {
422                                         fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
423                                         fputs("\" unmaskedvalue=\"", pdata->fh);
424                                         write_pdml_field_hex_value(pdata, fi);
425                                 }
426                                 else {
427                                         write_pdml_field_hex_value(pdata, fi);
428                                 }
429                         }
430                 }
431
432                 if (node->first_child != NULL) {
433                         fputs("\">\n", pdata->fh);
434                 }
435                 else if (fi->hfinfo->id == proto_data) {
436                         fputs("\">\n", pdata->fh);
437                 }
438                 else {
439                         fputs("\"/>\n", pdata->fh);
440                 }
441         }
442
443         /* We always print all levels for PDML. Recurse here. */
444         if (node->first_child != NULL) {
445                 pdata->level++;
446                 proto_tree_children_foreach(node,
447                                 proto_tree_write_node_pdml, pdata);
448                 pdata->level--;
449         }
450
451         /* Take back the extra level we added for fake wrapper protocol */
452         if (wrap_in_fake_protocol) {
453                 pdata->level--;
454         }
455
456         if (node->first_child != NULL) {
457                 /* Indent to correct level */
458                 for (i = -1; i < pdata->level; i++) {
459                         fputs("  ", pdata->fh);
460                 }
461                 /* Close off current element */
462                 if (fi->hfinfo->id != proto_data) {   /* Data protocol uses simple tags */
463                         if (fi->hfinfo->type == FT_PROTOCOL) {
464                                 fputs("</proto>\n", pdata->fh);
465                         }
466                         else {
467                                 fputs("</field>\n", pdata->fh);
468                         }
469                 }
470         }
471
472         /* Close off fake wrapper protocol */
473         if (wrap_in_fake_protocol) {
474                 fputs("</proto>\n", pdata->fh);
475         }
476 }
477
478 /* Print info for a 'geninfo' pseudo-protocol. This is required by
479  * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
480  * but we produce a 'geninfo' protocol in the PDML to conform to spec.
481  * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
482 static void
483 print_pdml_geninfo(proto_tree *tree, FILE *fh)
484 {
485         guint32 num, len, caplen;
486         nstime_t *timestamp;
487         GPtrArray *finfo_array;
488         field_info *frame_finfo;
489
490         /* Get frame protocol's finfo. */
491         finfo_array = proto_find_finfo(tree, proto_frame);
492         if (g_ptr_array_len(finfo_array) < 1) {
493                 return;
494         }
495         frame_finfo = finfo_array->pdata[0];
496         g_ptr_array_free(finfo_array, TRUE);
497
498         /* frame.number --> geninfo.num */
499         finfo_array = proto_find_finfo(tree, hf_frame_number);
500         if (g_ptr_array_len(finfo_array) < 1) {
501                 return;
502         }
503         num = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
504         g_ptr_array_free(finfo_array, TRUE);
505
506         /* frame.frame_len --> geninfo.len */
507         finfo_array = proto_find_finfo(tree, hf_frame_len);
508         if (g_ptr_array_len(finfo_array) < 1) {
509                 return;
510         }
511         len = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
512         g_ptr_array_free(finfo_array, TRUE);
513
514         /* frame.cap_len --> geninfo.caplen */
515         finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
516         if (g_ptr_array_len(finfo_array) < 1) {
517                 return;
518         }
519         caplen = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
520         g_ptr_array_free(finfo_array, TRUE);
521
522         /* frame.time --> geninfo.timestamp */
523         finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
524         if (g_ptr_array_len(finfo_array) < 1) {
525                 return;
526         }
527         timestamp = fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
528         g_ptr_array_free(finfo_array, TRUE);
529
530         /* Print geninfo start */
531         fprintf(fh,
532 "  <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
533                 frame_finfo->length);
534
535         /* Print geninfo.num */
536         fprintf(fh,
537 "    <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
538                 num, num, frame_finfo->length);
539
540         /* Print geninfo.len */
541         fprintf(fh,
542 "    <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Frame Length\" value=\"%x\" size=\"%u\"/>\n",
543                 len, len, frame_finfo->length);
544
545         /* Print geninfo.caplen */
546         fprintf(fh,
547 "    <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
548                 caplen, caplen, frame_finfo->length);
549
550         /* Print geninfo.timestamp */
551         fprintf(fh,
552 "    <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
553                 abs_time_to_str(timestamp, FALSE), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
554
555         /* Print geninfo end */
556         fprintf(fh,
557 "  </proto>\n");
558 }
559
560 void
561 write_pdml_finale(FILE *fh)
562 {
563         fputs("</pdml>\n", fh);
564 }
565
566 void
567 write_psml_preamble(FILE *fh)
568 {
569         fputs("<?xml version=\"1.0\"?>\n", fh);
570         fputs("<psml version=\"" PSML_VERSION "\" ", fh);
571         fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
572 }
573
574 void
575 proto_tree_write_psml(epan_dissect_t *edt, FILE *fh)
576 {
577         gint    i;
578
579         /* if this is the first packet, we have to create the PSML structure output */
580         if(edt->pi.fd->num == 1) {
581             fprintf(fh, "<structure>\n");
582
583             for(i=0; i < edt->pi.cinfo->num_cols; i++) {
584                 fprintf(fh, "<section>");
585                 print_escaped_xml(fh, edt->pi.cinfo->col_title[i]);
586                 fprintf(fh, "</section>\n");
587             }
588
589             fprintf(fh, "</structure>\n\n");
590         }
591
592         fprintf(fh, "<packet>\n");
593
594         for(i=0; i < edt->pi.cinfo->num_cols; i++) {
595             fprintf(fh, "<section>");
596             print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
597             fprintf(fh, "</section>\n");
598         }
599
600         fprintf(fh, "</packet>\n\n");
601 }
602
603 void
604 write_psml_finale(FILE *fh)
605 {
606         fputs("</psml>\n", fh);
607 }
608
609 void
610 write_csv_preamble(FILE *fh _U_)
611 {
612
613 }
614
615 void
616 proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
617 {
618         gint    i;
619
620         /* if this is the first packet, we have to write the CSV header */
621         if(edt->pi.fd->num == 1) {
622             for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
623                 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_title[i]);
624
625             fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_title[i]);
626         }
627
628         for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
629             fprintf(fh, "\"%s\",", edt->pi.cinfo->col_data[i]);
630
631         fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_data[i]);
632 }
633
634 void
635 write_csv_finale(FILE *fh _U_)
636 {
637
638 }
639
640 void
641 write_carrays_preamble(FILE *fh _U_)
642 {
643
644 }
645
646 void
647 proto_tree_write_carrays(const guint8 *pd, guint32 len, guint32 num, FILE *fh)
648 {
649         guint32 i = 0;
650
651         if (!len)
652                 return;
653
654         fprintf(fh, "char pkt%u[] = {\n", num);
655
656         for (i = 0; i < len; i++) {
657
658                 fprintf(fh, "0x%02x", *(pd + i));
659
660                 if (i == (len - 1)) {
661                         fprintf(fh, " };\n\n");
662                         break;
663                 }
664
665                 if (!((i + 1) % 8)) {
666                         fprintf(fh, ", \n");
667                 } else {
668                         fprintf(fh, ", ");
669                 }
670         }
671 }
672
673 void
674 write_carrays_finale(FILE *fh _U_)
675 {
676
677 }
678
679 /*
680  * Find the data source for a specified field, and return a pointer
681  * to the data in it. Returns NULL if the data is out of bounds.
682  */
683 static const guint8 *
684 get_field_data(GSList *src_list, field_info *fi)
685 {
686         GSList *src_le;
687         data_source *src;
688         tvbuff_t *src_tvb;
689         gint length, tvbuff_length;
690
691         for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
692                 src = src_le->data;
693                 src_tvb = src->tvb;
694                 if (fi->ds_tvb == src_tvb) {
695                         /*
696                          * Found it.
697                          *
698                          * XXX - a field can have a length that runs past
699                          * the end of the tvbuff.  Ideally, that should
700                          * be fixed when adding an item to the protocol
701                          * tree, but checking the length when doing
702                          * that could be expensive.  Until we fix that,
703                          * we'll do the check here.
704                          */
705                         tvbuff_length = tvb_length_remaining(src_tvb,
706                             fi->start);
707                         if (tvbuff_length < 0) {
708                                 return NULL;
709                         }
710                         length = fi->length;
711                         if (length > tvbuff_length)
712                                 length = tvbuff_length;
713                         return tvb_get_ptr(src_tvb, fi->start, length);
714                 }
715         }
716         g_assert_not_reached();
717         return NULL;    /* not found */
718 }
719
720 /* Print a string, escaping out certain characters that need to
721  * escaped out for XML. */
722 static void
723 print_escaped_xml(FILE *fh, const char *unescaped_string)
724 {
725         const char *p;
726
727         for (p = unescaped_string; *p != '\0'; p++) {
728                 switch (*p) {
729                         case '&':
730                                 fputs("&amp;", fh);
731                                 break;
732                         case '<':
733                                 fputs("&lt;", fh);
734                                 break;
735                         case '>':
736                                 fputs("&gt;", fh);
737                                 break;
738                         case '"':
739                                 fputs("&quot;", fh);
740                                 break;
741                         case '\'':
742                                 fputs("&apos;", fh);
743                                 break;
744                         default:
745                                 fputc(*p, fh);
746                 }
747         }
748 }
749
750 static void
751 write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi)
752 {
753         int i;
754         const guint8 *pd;
755
756         if (!fi->ds_tvb)
757                 return;
758
759         if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
760                 fprintf(pdata->fh, "field length invalid!");
761                 return;
762         }
763
764         /* Find the data for this field. */
765         pd = get_field_data(pdata->src_list, fi);
766
767         if (pd) {
768                 /* Print a simple hex dump */
769                 for (i = 0 ; i < fi->length; i++) {
770                         fprintf(pdata->fh, "%02x", pd[i]);
771                 }
772         }
773 }
774
775 gboolean
776 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
777 {
778         gboolean multiple_sources;
779         GSList *src_le;
780         data_source *src;
781         tvbuff_t *tvb;
782         const char *name;
783         char *line;
784         const guchar *cp;
785         guint length;
786
787         /* We shouldn't be called with a NULL pointer here because we've
788          * created a visible protocol tree */
789         g_assert(edt->pi.data_src);
790
791         /*
792          * Set "multiple_sources" iff this frame has more than one
793          * data source; if it does, we need to print the name of
794          * the data source before printing the data from the
795          * data source.
796          */
797         multiple_sources = (edt->pi.data_src->next != NULL);
798
799         for (src_le = edt->pi.data_src; src_le != NULL;
800             src_le = src_le->next) {
801                 src = src_le->data;
802                 tvb = src->tvb;
803                 if (multiple_sources) {
804                         name = get_data_source_name(src);
805                         print_line(stream, 0, "");
806                         line = g_strdup_printf("%s:", name);
807                         print_line(stream, 0, line);
808                         g_free(line);
809                 }
810                 length = tvb_length(tvb);
811                 if (length == 0)
812                     return TRUE;
813                 cp = tvb_get_ptr(tvb, 0, length);
814                 if (!print_hex_data_buffer(stream, cp, length,
815                     edt->pi.fd->flags.encoding))
816                         return FALSE;
817         }
818         return TRUE;
819 }
820
821 /*
822  * This routine is based on a routine created by Dan Lasley
823  * <DLASLEY@PROMUS.com>.
824  *
825  * It was modified for Wireshark by Gilbert Ramirez and others.
826  */
827
828 #define MAX_OFFSET_LEN  8       /* max length of hex offset of bytes */
829 #define BYTES_PER_LINE  16      /* max byte values printed on a line */
830 #define HEX_DUMP_LEN    (BYTES_PER_LINE*3)
831                                 /* max number of characters hex dump takes -
832                                    2 digits plus trailing blank */
833 #define DATA_DUMP_LEN   (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
834                                 /* number of characters those bytes take;
835                                    3 characters per byte of hex dump,
836                                    2 blanks separating hex from ASCII,
837                                    1 character per byte of ASCII dump */
838 #define MAX_LINE_LEN    (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
839                                 /* number of characters per line;
840                                    offset, 2 blanks separating offset
841                                    from data dump, data dump */
842
843 static gboolean
844 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
845     guint length, char_enc encoding)
846 {
847         register unsigned int ad, i, j, k, l;
848         guchar c;
849         guchar line[MAX_LINE_LEN + 1];
850         unsigned int use_digits;
851         static guchar binhex[16] = {
852                 '0', '1', '2', '3', '4', '5', '6', '7',
853                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
854
855         if (!print_line(stream, 0, ""))
856                 return FALSE;
857
858         /*
859          * How many of the leading digits of the offset will we supply?
860          * We always supply at least 4 digits, but if the maximum offset
861          * won't fit in 4 digits, we use as many digits as will be needed.
862          */
863         if (((length - 1) & 0xF0000000) != 0)
864                 use_digits = 8; /* need all 8 digits */
865         else if (((length - 1) & 0x0F000000) != 0)
866                 use_digits = 7; /* need 7 digits */
867         else if (((length - 1) & 0x00F00000) != 0)
868                 use_digits = 6; /* need 6 digits */
869         else if (((length - 1) & 0x000F0000) != 0)
870                 use_digits = 5; /* need 5 digits */
871         else
872                 use_digits = 4; /* we'll supply 4 digits */
873
874         ad = 0;
875         i = 0;
876         j = 0;
877         k = 0;
878         while (i < length) {
879                 if ((i & 15) == 0) {
880                         /*
881                          * Start of a new line.
882                          */
883                         j = 0;
884                         l = use_digits;
885                         do {
886                                 l--;
887                                 c = (ad >> (l*4)) & 0xF;
888                                 line[j++] = binhex[c];
889                         } while (l != 0);
890                         line[j++] = ' ';
891                         line[j++] = ' ';
892                         memset(line+j, ' ', DATA_DUMP_LEN);
893
894                         /*
895                          * Offset in line of ASCII dump.
896                          */
897                         k = j + HEX_DUMP_LEN + 2;
898                 }
899                 c = *cp++;
900                 line[j++] = binhex[c>>4];
901                 line[j++] = binhex[c&0xf];
902                 j++;
903                 if (encoding == CHAR_EBCDIC) {
904                         c = EBCDIC_to_ASCII1(c);
905                 }
906                 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
907                 i++;
908                 if ((i & 15) == 0 || i == length) {
909                         /*
910                          * We'll be starting a new line, or
911                          * we're finished printing this buffer;
912                          * dump out the line we've constructed,
913                          * and advance the offset.
914                          */
915                         line[k] = '\0';
916                         if (!print_line(stream, 0, line))
917                                 return FALSE;
918                         ad += 16;
919                 }
920         }
921         return TRUE;
922 }
923
924 static
925 void ps_clean_string(unsigned char *out, const unsigned char *in,
926                         int outbuf_size)
927 {
928         int rd, wr;
929         char c;
930
931         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
932                 c = in[rd];
933                 switch (c) {
934                         case '(':
935                         case ')':
936                         case '\\':
937                                 out[wr] = '\\';
938                                 out[++wr] = c;
939                                 break;
940
941                         default:
942                                 out[wr] = c;
943                                 break;
944                 }
945
946                 if (c == 0) {
947                         break;
948                 }
949         }
950 }
951
952 /* Some formats need stuff at the beginning of the output */
953 gboolean
954 print_preamble(print_stream_t *self, gchar *filename)
955 {
956         return (self->ops->print_preamble)(self, filename);
957 }
958
959 gboolean
960 print_line(print_stream_t *self, int indent, const char *line)
961 {
962         return (self->ops->print_line)(self, indent, line);
963 }
964
965 /* Insert bookmark */
966 gboolean
967 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
968 {
969         return (self->ops->print_bookmark)(self, name, title);
970 }
971
972 gboolean
973 new_page(print_stream_t *self)
974 {
975         return (self->ops->new_page)(self);
976 }
977
978 /* Some formats need stuff at the end of the output */
979 gboolean
980 print_finale(print_stream_t *self)
981 {
982         return (self->ops->print_finale)(self);
983 }
984
985 gboolean
986 destroy_print_stream(print_stream_t *self)
987 {
988         return (self->ops->destroy)(self);
989 }
990
991 typedef struct {
992         int to_file;
993         FILE *fh;
994 } output_text;
995
996 static gboolean
997 print_preamble_text(print_stream_t *self _U_, gchar *filename _U_)
998 {
999         /* do nothing */
1000         return TRUE;    /* always succeeds */
1001 }
1002
1003 static gboolean
1004 print_line_text(print_stream_t *self, int indent, const char *line)
1005 {
1006         output_text *output = self->data;
1007         char space[MAX_INDENT+1];
1008         int i;
1009         int num_spaces;
1010
1011         /* Prepare the tabs for printing, depending on tree level */
1012         num_spaces = indent * 4;
1013         if (num_spaces > MAX_INDENT) {
1014                 num_spaces = MAX_INDENT;
1015         }
1016         for (i = 0; i < num_spaces; i++) {
1017                 space[i] = ' ';
1018         }
1019         /* The string is NUL-terminated */
1020         space[num_spaces] = '\0';
1021
1022         fputs(space, output->fh);
1023         fputs(line, output->fh);
1024         putc('\n', output->fh);
1025         return !ferror(output->fh);
1026 }
1027
1028 static gboolean
1029 print_bookmark_text(print_stream_t *self _U_, const gchar *name _U_,
1030     const gchar *title _U_)
1031 {
1032         /* do nothing */
1033         return TRUE;
1034 }
1035
1036 static gboolean
1037 new_page_text(print_stream_t *self)
1038 {
1039         output_text *output = self->data;
1040
1041         fputs("\f", output->fh);
1042         return !ferror(output->fh);
1043 }
1044
1045 static gboolean
1046 print_finale_text(print_stream_t *self _U_)
1047 {
1048         /* do nothing */
1049         return TRUE;    /* always succeeds */
1050 }
1051
1052 static gboolean
1053 destroy_text(print_stream_t *self)
1054 {
1055         output_text *output = self->data;
1056         gboolean ret;
1057
1058         ret = close_print_dest(output->to_file, output->fh);
1059         g_free(output);
1060         g_free(self);
1061         return ret;
1062 }
1063
1064 static const print_stream_ops_t print_text_ops = {
1065         print_preamble_text,
1066         print_line_text,
1067         print_bookmark_text,
1068         new_page_text,
1069         print_finale_text,
1070         destroy_text
1071 };
1072
1073 static print_stream_t *
1074 print_stream_text_alloc(int to_file, FILE *fh)
1075 {
1076         print_stream_t *stream;
1077         output_text *output;
1078
1079         output = g_malloc(sizeof *output);
1080         output->to_file = to_file;
1081         output->fh = fh;
1082         stream = g_malloc(sizeof (print_stream_t));
1083         stream->ops = &print_text_ops;
1084         stream->data = output;
1085
1086         return stream;
1087 }
1088
1089 print_stream_t *
1090 print_stream_text_new(int to_file, const char *dest)
1091 {
1092         FILE *fh;
1093
1094         fh = open_print_dest(to_file, dest);
1095         if (fh == NULL)
1096                 return NULL;
1097
1098         return print_stream_text_alloc(to_file, fh);
1099 }
1100
1101 print_stream_t *
1102 print_stream_text_stdio_new(FILE *fh)
1103 {
1104         return print_stream_text_alloc(TRUE, fh);
1105 }
1106
1107 typedef struct {
1108         int to_file;
1109         FILE *fh;
1110 } output_ps;
1111
1112 static gboolean
1113 print_preamble_ps(print_stream_t *self, gchar *filename)
1114 {
1115         output_ps *output = self->data;
1116         unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1117
1118         print_ps_preamble(output->fh);
1119
1120         fputs("%% Set the font to 8 point\n", output->fh);
1121         fputs("/Courier findfont 8 scalefont setfont\n", output->fh);
1122         fputs("\n", output->fh);
1123         fputs("%% the page title\n", output->fh);
1124         ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
1125         fprintf(output->fh, "/ws_pagetitle (%s - Wireshark " VERSION "%s) def\n", psbuffer, wireshark_svnversion);
1126         fputs("\n", output->fh);
1127         return !ferror(output->fh);
1128 }
1129
1130 static gboolean
1131 print_line_ps(print_stream_t *self, int indent, const char *line)
1132 {
1133         output_ps *output = self->data;
1134         unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1135
1136         ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
1137         fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
1138         return !ferror(output->fh);
1139 }
1140
1141 static gboolean
1142 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
1143 {
1144         output_ps *output = self->data;
1145         unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1146
1147         /*
1148          * See the Adobe "pdfmark reference":
1149          *
1150          *      http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
1151          *
1152          * The pdfmark stuff tells code that turns PostScript into PDF
1153          * things that it should do.
1154          *
1155          * The /OUT stuff creates a bookmark that goes to the
1156          * destination with "name" as the name and "title" as the title.
1157          *
1158          * The "/DEST" creates the destination.
1159          */
1160         ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
1161         fprintf(output->fh, "[/Dest /%s /Title (%s)   /OUT pdfmark\n", name,
1162             psbuffer);
1163         fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
1164             output->fh);
1165         fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
1166             output->fh);
1167         fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
1168         return !ferror(output->fh);
1169 }
1170
1171 static gboolean
1172 new_page_ps(print_stream_t *self)
1173 {
1174         output_ps *output = self->data;
1175
1176         fputs("formfeed\n", output->fh);
1177         return !ferror(output->fh);
1178 }
1179
1180 static gboolean
1181 print_finale_ps(print_stream_t *self)
1182 {
1183         output_ps *output = self->data;
1184
1185         print_ps_finale(output->fh);
1186         return !ferror(output->fh);
1187 }
1188
1189 static gboolean
1190 destroy_ps(print_stream_t *self)
1191 {
1192         output_ps *output = self->data;
1193         gboolean ret;
1194
1195         ret = close_print_dest(output->to_file, output->fh);
1196         g_free(output);
1197         g_free(self);
1198         return ret;
1199 }
1200
1201 static const print_stream_ops_t print_ps_ops = {
1202         print_preamble_ps,
1203         print_line_ps,
1204         print_bookmark_ps,
1205         new_page_ps,
1206         print_finale_ps,
1207         destroy_ps
1208 };
1209
1210 static print_stream_t *
1211 print_stream_ps_alloc(int to_file, FILE *fh)
1212 {
1213         print_stream_t *stream;
1214         output_ps *output;
1215
1216         output = g_malloc(sizeof *output);
1217         output->to_file = to_file;
1218         output->fh = fh;
1219         stream = g_malloc(sizeof (print_stream_t));
1220         stream->ops = &print_ps_ops;
1221         stream->data = output;
1222
1223         return stream;
1224 }
1225
1226 print_stream_t *
1227 print_stream_ps_new(int to_file, const char *dest)
1228 {
1229         FILE *fh;
1230
1231         fh = open_print_dest(to_file, dest);
1232         if (fh == NULL)
1233                 return NULL;
1234
1235         return print_stream_ps_alloc(to_file, fh);
1236 }
1237
1238 print_stream_t *
1239 print_stream_ps_stdio_new(FILE *fh)
1240 {
1241         return print_stream_ps_alloc(TRUE, fh);
1242 }
1243
1244 output_fields_t* output_fields_new()
1245 {
1246     output_fields_t* fields = g_new(output_fields_t, 1);
1247     fields->print_header = FALSE;
1248     fields->separator = '\t';
1249     fields->fields = NULL; /*Do lazy initialisation */
1250     fields->field_indicies = NULL;
1251     fields->field_values = NULL;
1252     fields->quote='\0';
1253     return fields;
1254 }
1255
1256 gsize output_fields_num_fields(output_fields_t* fields)
1257 {
1258     g_assert(fields);
1259
1260     if(NULL == fields->fields) {
1261         return 0;
1262     } else {
1263         return fields->fields->len;
1264     }
1265 }
1266
1267 void output_fields_free(output_fields_t* fields)
1268 {
1269     g_assert(fields);
1270
1271     if(NULL != fields->field_indicies) {
1272         /* Keys are stored in fields->fields, values are
1273          * integers.
1274          */
1275         g_hash_table_destroy(fields->field_indicies);
1276     }
1277     if(NULL != fields->fields) {
1278         gsize i;
1279         for(i = 0; i < fields->fields->len; ++i) {
1280             gchar* field = g_ptr_array_index(fields->fields,i);
1281             g_free(field);
1282         }
1283         g_ptr_array_free(fields->fields, TRUE);
1284     }
1285
1286     g_free(fields);
1287 }
1288
1289 void output_fields_add(output_fields_t* fields, const gchar* field)
1290 {
1291     gchar* field_copy;
1292
1293     g_assert(fields);
1294     g_assert(field);
1295
1296
1297     if(NULL == fields->fields) {
1298         fields->fields = g_ptr_array_new();
1299     }
1300
1301     field_copy = g_strdup(field);
1302
1303     g_ptr_array_add(fields->fields, field_copy);
1304 }
1305
1306 gboolean output_fields_set_option(output_fields_t* info, gchar* option)
1307 {
1308     const gchar* option_name;
1309     const gchar* option_value;
1310
1311     g_assert(info);
1312     g_assert(option);
1313
1314     if('\0' == *option) {
1315         return FALSE; /* Is this guarded against by option parsing? */
1316     }
1317     option_name = strtok(option,"=");
1318     option_value = option + strlen(option_name) + 1;
1319     if(0 == strcmp(option_name, "header")) {
1320         switch(NULL == option_value ? '\0' : *option_value) {
1321         case 'n':
1322             info->print_header = FALSE;
1323             break;
1324         case 'y':
1325             info->print_header = TRUE;
1326             break;
1327         default:
1328             return FALSE;
1329         }
1330         return TRUE;
1331     }
1332
1333     if(0 == strcmp(option_name,"separator")) {
1334         switch(NULL == option_value ? '\0' : *option_value) {
1335         case '\0':
1336             return FALSE;
1337         case '/':
1338             switch(*++option_value) {
1339             case 't':
1340                 info->separator = '\t';
1341                 break;
1342             case 's':
1343                 info->separator = ' ';
1344                 break;
1345             default:
1346                 info->separator = '\\';
1347             }
1348             break;
1349         default:
1350             info->separator = *option_value;
1351             break;
1352         }
1353         return TRUE;
1354     }
1355
1356     if(0 == strcmp(option_name, "quote")) {
1357         switch(NULL == option_value ? '\0' : *option_value) {
1358         default: /* Fall through */
1359         case '\0':
1360             info->quote='\0';
1361             return FALSE;
1362         case 'd':
1363             info->quote='"';
1364             break;
1365         case 's':
1366             info->quote='\'';
1367             break;
1368         case 'n':
1369             info->quote='\0';
1370             break;
1371         }
1372         return TRUE;
1373     }
1374
1375     return FALSE;
1376 }
1377
1378 void output_fields_list_options(FILE *fh)
1379 {
1380     fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
1381     fputs("header=y|n   Print field abbreviations as first line of output (def: N: no)\n", fh);
1382     fputs("separator=/t|/s|<character>   Set the separator to use; \"/t\" = tab,\n \"/s\" = space (def: /t: tab)\n", fh);
1383     fputs("quote=d|s|n   Print either d: double-quotes, s: single quotes or n: no quotes around field values (def: n: none)\n", fh);
1384 }
1385
1386
1387 void write_fields_preamble(output_fields_t* fields, FILE *fh)
1388 {
1389     gsize i;
1390
1391     g_assert(fields);
1392     g_assert(fh);
1393
1394     if(!fields->print_header) {
1395         return;
1396     }
1397
1398     for(i = 0; i < fields->fields->len; ++i) {
1399         const gchar* field = g_ptr_array_index(fields->fields,i);
1400         if(i != 0 ) {
1401             fputc(fields->separator, fh);
1402         }
1403         fputs(field, fh);
1404     }
1405     fputc('\n', fh);
1406 }
1407
1408 static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
1409 {
1410     write_field_data_t *call_data;
1411     field_info *fi;
1412     gpointer field_index;
1413
1414     call_data = data;
1415     fi = PNODE_FINFO(node);
1416
1417     g_assert(fi && "dissection with an invisible proto tree?");
1418
1419     field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
1420     if(NULL != field_index) {
1421         const gchar* value;
1422
1423         value = get_node_field_value(fi, call_data->edt); /* ep_alloced string */
1424
1425         if(NULL != value && '\0' != *value) {
1426             guint actual_index;
1427             actual_index = GPOINTER_TO_UINT(field_index);
1428             /* Unwrap change made to disambiguiate zero / null */
1429             call_data->fields->field_values[actual_index - 1] = value;
1430         }
1431     }
1432
1433     /* Recurse here. */
1434     if (node->first_child != NULL) {
1435         proto_tree_children_foreach(node, proto_tree_get_node_field_values,
1436                                     call_data);
1437     }
1438 }
1439
1440 void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, FILE *fh)
1441 {
1442     gsize i;
1443
1444     write_field_data_t data;
1445
1446     g_assert(fields);
1447     g_assert(edt);
1448     g_assert(fh);
1449
1450     data.fields = fields;
1451     data.edt = edt;
1452
1453     if(NULL == fields->field_indicies) {
1454         /* Prepare a lookup table from string abbreviation for field to its index. */
1455         fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
1456
1457         i = 0;
1458         while( i < fields->fields->len) {
1459             gchar* field = g_ptr_array_index(fields->fields, i);
1460              /* Store field indicies +1 so that zero is not a valid value,
1461               * and can be distinguished from NULL as a pointer.
1462               */
1463             ++i;
1464             g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
1465         }
1466     }
1467
1468     /* Buffer to store values for this packet */
1469     fields->field_values = ep_alloc_array0(const gchar*, fields->fields->len);
1470
1471     proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
1472                                 &data);
1473
1474     for(i = 0; i < fields->fields->len; ++i) {
1475         if(0 != i) {
1476             fputc(fields->separator, fh);
1477         }
1478         if(NULL != fields->field_values[i]) {
1479             if(fields->quote != '\0') {
1480                 fputc(fields->quote, fh);
1481             }
1482             fputs(fields->field_values[i], fh);
1483             if(fields->quote != '\0') {
1484                 fputc(fields->quote, fh);
1485             }
1486         }
1487     }
1488 }
1489
1490 void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
1491 {
1492     /* Nothing to do */
1493 }
1494
1495 /* Returns an ep_alloced string or a static constant*/
1496 const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
1497 {
1498     if (fi->hfinfo->id == hf_text_only) {
1499         /* Text label.
1500          * Get the text */
1501         if (fi->rep) {
1502             return fi->rep->representation;
1503         }
1504         else {
1505             return get_field_hex_value(edt->pi.data_src, fi);
1506         }
1507     }
1508     else if (fi->hfinfo->id == proto_data) {
1509         /* Uninterpreted data, i.e., the "Data" protocol, is
1510          * printed as a field instead of a protocol. */
1511         return get_field_hex_value(edt->pi.data_src, fi);
1512     }
1513     else {
1514         /* Normal protocols and fields */
1515         gchar      *dfilter_string;
1516         size_t      chop_len;
1517
1518         switch (fi->hfinfo->type)
1519         {
1520         case FT_PROTOCOL:
1521             /* Print out the full details for the protocol. */
1522             if (fi->rep) {
1523                 return fi->rep->representation;
1524             } else {
1525                 /* Just print out the protocol abbreviation */
1526                 return fi->hfinfo->abbrev;;
1527             }
1528         case FT_NONE:
1529             /* Return "1" so that the presence of a field of type
1530              * FT_NONE can be checked when using -T fields */
1531             return "1";
1532         default:
1533             /* XXX - this is a hack until we can just call
1534              * fvalue_to_string_repr() for *all* FT_* types. */
1535             dfilter_string = proto_construct_match_selected_string(fi,
1536                 edt);
1537             if (dfilter_string != NULL) {
1538                 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
1539
1540                 /* XXX - Remove double-quotes. Again, once we
1541                  * can call fvalue_to_string_repr(), we can
1542                  * ask it not to produce the version for
1543                  * display-filters, and thus, no
1544                  * double-quotes. */
1545                 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
1546                     dfilter_string[strlen(dfilter_string)-1] = '\0';
1547                     chop_len++;
1548                 }
1549
1550                 return &(dfilter_string[chop_len]);
1551             } else {
1552                 return get_field_hex_value(edt->pi.data_src, fi);
1553             }
1554         }
1555     }
1556 }
1557
1558 static const gchar*
1559 get_field_hex_value(GSList* src_list, field_info *fi)
1560 {
1561     const guint8 *pd;
1562
1563     if (!fi->ds_tvb)
1564         return NULL;
1565
1566     if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
1567         return "field length invalid!";
1568     }
1569
1570     /* Find the data for this field. */
1571     pd = get_field_data(src_list, fi);
1572
1573     if (pd) {
1574         int i;
1575         gchar* buffer;
1576         gchar* p;
1577         int len;
1578         const int chars_per_byte = 2;
1579
1580         len = chars_per_byte * fi->length;
1581         buffer = ep_alloc_array(gchar, len + 1);
1582         buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
1583         p = buffer;
1584         /* Print a simple hex dump */
1585         for (i = 0 ; i < fi->length; i++) {
1586             g_snprintf(p, chars_per_byte+1, "%02x", pd[i]);
1587             p += chars_per_byte;
1588         }
1589         return buffer;
1590     } else {
1591         return NULL;
1592     }
1593 }