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