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