From Anders: Fix wrong tag numbers.
[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 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         size_t          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.frame_len --> geninfo.len */
499         finfo_array = proto_find_finfo(tree, hf_frame_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=\"Frame 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->ds_tvb)
749                 return;
750
751         if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
752                 fprintf(pdata->fh, "field length invalid!");
753                 return;
754         }
755
756         /* Find the data for this field. */
757         pd = get_field_data(pdata->src_list, fi);
758
759         if (pd) {
760                 /* Print a simple hex dump */
761                 for (i = 0 ; i < fi->length; i++) {
762                         fprintf(pdata->fh, "%02x", pd[i]);
763                 }
764         }
765 }
766
767 gboolean
768 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
769 {
770         gboolean multiple_sources;
771         GSList *src_le;
772         data_source *src;
773         tvbuff_t *tvb;
774         char *name;
775         char *line;
776         const guchar *cp;
777         guint length;
778
779         /*
780          * Set "multiple_sources" iff this frame has more than one
781          * data source; if it does, we need to print the name of
782          * the data source before printing the data from the
783          * data source.
784          */
785         multiple_sources = (edt->pi.data_src->next != NULL);
786
787         for (src_le = edt->pi.data_src; src_le != NULL;
788             src_le = src_le->next) {
789                 src = src_le->data;
790                 tvb = src->tvb;
791                 if (multiple_sources) {
792                         name = src->name;
793                         print_line(stream, 0, "");
794                         line = g_strdup_printf("%s:", name);
795                         print_line(stream, 0, line);
796                         g_free(line);
797                 }
798                 length = tvb_length(tvb);
799                 if (length == 0)
800                     return TRUE;
801                 cp = tvb_get_ptr(tvb, 0, length);
802                 if (!print_hex_data_buffer(stream, cp, length,
803                     edt->pi.fd->flags.encoding))
804                         return FALSE;
805         }
806         return TRUE;
807 }
808
809 /*
810  * This routine is based on a routine created by Dan Lasley
811  * <DLASLEY@PROMUS.com>.
812  *
813  * It was modified for Wireshark by Gilbert Ramirez and others.
814  */
815
816 #define MAX_OFFSET_LEN  8       /* max length of hex offset of bytes */
817 #define BYTES_PER_LINE  16      /* max byte values printed on a line */
818 #define HEX_DUMP_LEN    (BYTES_PER_LINE*3)
819                                 /* max number of characters hex dump takes -
820                                    2 digits plus trailing blank */
821 #define DATA_DUMP_LEN   (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
822                                 /* number of characters those bytes take;
823                                    3 characters per byte of hex dump,
824                                    2 blanks separating hex from ASCII,
825                                    1 character per byte of ASCII dump */
826 #define MAX_LINE_LEN    (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
827                                 /* number of characters per line;
828                                    offset, 2 blanks separating offset
829                                    from data dump, data dump */
830
831 static gboolean
832 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
833     guint length, char_enc encoding)
834 {
835         register unsigned int ad, i, j, k, l;
836         guchar c;
837         guchar line[MAX_LINE_LEN + 1];
838         unsigned int use_digits;
839         static guchar binhex[16] = {
840                 '0', '1', '2', '3', '4', '5', '6', '7',
841                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
842
843         if (!print_line(stream, 0, ""))
844                 return FALSE;
845
846         /*
847          * How many of the leading digits of the offset will we supply?
848          * We always supply at least 4 digits, but if the maximum offset
849          * won't fit in 4 digits, we use as many digits as will be needed.
850          */
851         if (((length - 1) & 0xF0000000) != 0)
852                 use_digits = 8; /* need all 8 digits */
853         else if (((length - 1) & 0x0F000000) != 0)
854                 use_digits = 7; /* need 7 digits */
855         else if (((length - 1) & 0x00F00000) != 0)
856                 use_digits = 6; /* need 6 digits */
857         else if (((length - 1) & 0x000F0000) != 0)
858                 use_digits = 5; /* need 5 digits */
859         else
860                 use_digits = 4; /* we'll supply 4 digits */
861
862         ad = 0;
863         i = 0;
864         j = 0;
865         k = 0;
866         while (i < length) {
867                 if ((i & 15) == 0) {
868                         /*
869                          * Start of a new line.
870                          */
871                         j = 0;
872                         k = 0;
873                         l = use_digits;
874                         do {
875                                 l--;
876                                 c = (ad >> (l*4)) & 0xF;
877                                 line[j++] = binhex[c];
878                         } while (l != 0);
879                         line[j++] = ' ';
880                         line[j++] = ' ';
881                         memset(line+j, ' ', DATA_DUMP_LEN);
882
883                         /*
884                          * Offset in line of ASCII dump.
885                          */
886                         k = j + HEX_DUMP_LEN + 2;
887                 }
888                 c = *cp++;
889                 line[j++] = binhex[c>>4];
890                 line[j++] = binhex[c&0xf];
891                 j++;
892                 if (encoding == CHAR_EBCDIC) {
893                         c = EBCDIC_to_ASCII1(c);
894                 }
895                 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
896                 i++;
897                 if ((i & 15) == 0 || i == length) {
898                         /*
899                          * We'll be starting a new line, or
900                          * we're finished printing this buffer;
901                          * dump out the line we've constructed,
902                          * and advance the offset.
903                          */
904                         line[k] = '\0';
905                         if (!print_line(stream, 0, line))
906                                 return FALSE;
907                         ad += 16;
908                 }
909         }
910         return TRUE;
911 }
912
913 static
914 void ps_clean_string(unsigned char *out, const unsigned char *in,
915                         int outbuf_size)
916 {
917         int rd, wr;
918         char c;
919
920         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
921                 c = in[rd];
922                 switch (c) {
923                         case '(':
924                         case ')':
925                         case '\\':
926                                 out[wr] = '\\';
927                                 out[++wr] = c;
928                                 break;
929
930                         default:
931                                 out[wr] = c;
932                                 break;
933                 }
934
935                 if (c == 0) {
936                         break;
937                 }
938         }
939 }
940
941 /* Some formats need stuff at the beginning of the output */
942 gboolean
943 print_preamble(print_stream_t *self, gchar *filename)
944 {
945         return (self->ops->print_preamble)(self, filename);
946 }
947
948 gboolean
949 print_line(print_stream_t *self, int indent, const char *line)
950 {
951         return (self->ops->print_line)(self, indent, line);
952 }
953
954 /* Insert bookmark */
955 gboolean
956 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
957 {
958         return (self->ops->print_bookmark)(self, name, title);
959 }
960
961 gboolean
962 new_page(print_stream_t *self)
963 {
964         return (self->ops->new_page)(self);
965 }
966
967 /* Some formats need stuff at the end of the output */
968 gboolean
969 print_finale(print_stream_t *self)
970 {
971         return (self->ops->print_finale)(self);
972 }
973
974 gboolean
975 destroy_print_stream(print_stream_t *self)
976 {
977         return (self->ops->destroy)(self);
978 }
979
980 typedef struct {
981         int to_file;
982         FILE *fh;
983 } output_text;
984
985 static gboolean
986 print_preamble_text(print_stream_t *self _U_, gchar *filename _U_)
987 {
988         /* do nothing */
989         return TRUE;    /* always succeeds */
990 }
991
992 static gboolean
993 print_line_text(print_stream_t *self, int indent, const char *line)
994 {
995         output_text *output = self->data;
996         char space[MAX_INDENT+1];
997         int i;
998         int num_spaces;
999
1000         /* Prepare the tabs for printing, depending on tree level */
1001         num_spaces = indent * 4;
1002         if (num_spaces > MAX_INDENT) {
1003                 num_spaces = MAX_INDENT;
1004         }
1005         for (i = 0; i < num_spaces; i++) {
1006                 space[i] = ' ';
1007         }
1008         /* The string is NUL-terminated */
1009         space[num_spaces] = '\0';
1010
1011         fputs(space, output->fh);
1012         fputs(line, output->fh);
1013         putc('\n', output->fh);
1014         return !ferror(output->fh);
1015 }
1016
1017 static gboolean
1018 print_bookmark_text(print_stream_t *self _U_, const gchar *name _U_,
1019     const gchar *title _U_)
1020 {
1021         /* do nothing */
1022         return TRUE;
1023 }
1024
1025 static gboolean
1026 new_page_text(print_stream_t *self)
1027 {
1028         output_text *output = self->data;
1029
1030         fputs("\f", output->fh);
1031         return !ferror(output->fh);
1032 }
1033
1034 static gboolean
1035 print_finale_text(print_stream_t *self _U_)
1036 {
1037         /* do nothing */
1038         return TRUE;    /* always succeeds */
1039 }
1040
1041 static gboolean
1042 destroy_text(print_stream_t *self)
1043 {
1044         output_text *output = self->data;
1045         gboolean ret;
1046
1047         ret = close_print_dest(output->to_file, output->fh);
1048         g_free(output);
1049         g_free(self);
1050         return ret;
1051 }
1052
1053 static const print_stream_ops_t print_text_ops = {
1054         print_preamble_text,
1055         print_line_text,
1056         print_bookmark_text,
1057         new_page_text,
1058         print_finale_text,
1059         destroy_text
1060 };
1061
1062 print_stream_t *
1063 print_stream_text_new(int to_file, const char *dest)
1064 {
1065         FILE *fh;
1066         print_stream_t *stream;
1067         output_text *output;
1068
1069         fh = open_print_dest(to_file, dest);
1070         if (fh == NULL)
1071                 return NULL;
1072
1073         output = g_malloc(sizeof *output);
1074         output->to_file = to_file;
1075         output->fh = fh;
1076         stream = g_malloc(sizeof (print_stream_t));
1077         stream->ops = &print_text_ops;
1078         stream->data = output;
1079
1080         return stream;
1081 }
1082
1083 print_stream_t *
1084 print_stream_text_stdio_new(FILE *fh)
1085 {
1086         print_stream_t *stream;
1087         output_text *output;
1088
1089         output = g_malloc(sizeof *output);
1090         output->to_file = TRUE;
1091         output->fh = fh;
1092         stream = g_malloc(sizeof (print_stream_t));
1093         stream->ops = &print_text_ops;
1094         stream->data = output;
1095
1096         return stream;
1097 }
1098
1099 typedef struct {
1100         int to_file;
1101         FILE *fh;
1102 } output_ps;
1103
1104 static gboolean
1105 print_preamble_ps(print_stream_t *self, gchar *filename)
1106 {
1107         output_ps *output = self->data;
1108         unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1109
1110         print_ps_preamble(output->fh);
1111
1112         fputs("%% Set the font to 8 point\n", output->fh);
1113         fputs("/Courier findfont 8 scalefont setfont\n", output->fh);
1114         fputs("\n", output->fh);
1115         fputs("%% the page title\n", output->fh);
1116         ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
1117         fprintf(output->fh, "/ws_pagetitle (%s - Wireshark " VERSION "%s) def\n", psbuffer, wireshark_svnversion);
1118         fputs("\n", output->fh);
1119         return !ferror(output->fh);
1120 }
1121
1122 static gboolean
1123 print_line_ps(print_stream_t *self, int indent, const char *line)
1124 {
1125         output_ps *output = self->data;
1126         unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1127
1128         ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
1129         fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
1130         return !ferror(output->fh);
1131 }
1132
1133 static gboolean
1134 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
1135 {
1136         output_ps *output = self->data;
1137         unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1138
1139         /*
1140          * See the Adobe "pdfmark reference":
1141          *
1142          *      http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
1143          *
1144          * The pdfmark stuff tells code that turns PostScript into PDF
1145          * things that it should do.
1146          *
1147          * The /OUT stuff creates a bookmark that goes to the
1148          * destination with "name" as the name and "title" as the title.
1149          *
1150          * The "/DEST" creates the destination.
1151          */
1152         ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
1153         fprintf(output->fh, "[/Dest /%s /Title (%s)   /OUT pdfmark\n", name,
1154             psbuffer);
1155         fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
1156             output->fh);
1157         fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
1158             output->fh);
1159         fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
1160         return !ferror(output->fh);
1161 }
1162
1163 static gboolean
1164 new_page_ps(print_stream_t *self)
1165 {
1166         output_ps *output = self->data;
1167
1168         fputs("formfeed\n", output->fh);
1169         return !ferror(output->fh);
1170 }
1171
1172 static gboolean
1173 print_finale_ps(print_stream_t *self)
1174 {
1175         output_ps *output = self->data;
1176
1177         print_ps_finale(output->fh);
1178         return !ferror(output->fh);
1179 }
1180
1181 static gboolean
1182 destroy_ps(print_stream_t *self)
1183 {
1184         output_ps *output = self->data;
1185         gboolean ret;
1186
1187         ret = close_print_dest(output->to_file, output->fh);
1188         g_free(output);
1189         g_free(self);
1190         return ret;
1191 }
1192
1193 static const print_stream_ops_t print_ps_ops = {
1194         print_preamble_ps,
1195         print_line_ps,
1196         print_bookmark_ps,
1197         new_page_ps,
1198         print_finale_ps,
1199         destroy_ps
1200 };
1201
1202 print_stream_t *
1203 print_stream_ps_new(int to_file, const char *dest)
1204 {
1205         FILE *fh;
1206         print_stream_t *stream;
1207         output_ps *output;
1208
1209         fh = open_print_dest(to_file, dest);
1210         if (fh == NULL)
1211                 return NULL;
1212
1213         output = g_malloc(sizeof *output);
1214         output->to_file = to_file;
1215         output->fh = fh;
1216         stream = g_malloc(sizeof (print_stream_t));
1217         stream->ops = &print_ps_ops;
1218         stream->data = output;
1219
1220         return stream;
1221 }
1222
1223 print_stream_t *
1224 print_stream_ps_stdio_new(FILE *fh)
1225 {
1226         print_stream_t *stream;
1227         output_ps *output;
1228
1229         output = g_malloc(sizeof *output);
1230         output->to_file = TRUE;
1231         output->fh = fh;
1232         stream = g_malloc(sizeof (print_stream_t));
1233         stream->ops = &print_ps_ops;
1234         stream->data = output;
1235
1236         return stream;
1237 }
1238
1239 output_fields_t* output_fields_new()
1240 {
1241     output_fields_t* fields = g_new(output_fields_t, 1);
1242     fields->print_header = FALSE;
1243     fields->separator = '\t';
1244     fields->fields = NULL; /*Do lazy initialisation */
1245     fields->field_indicies = NULL;
1246     fields->field_values = NULL;
1247     fields->quote='\0';
1248     return fields;
1249 }
1250
1251 gsize output_fields_num_fields(output_fields_t* fields)
1252 {
1253     g_assert(fields);
1254
1255     if(NULL == fields->fields) {
1256         return 0;
1257     } else {
1258         return fields->fields->len;
1259     }
1260 }
1261
1262 void output_fields_free(output_fields_t* fields)
1263 {
1264     g_assert(fields);
1265
1266     if(NULL != fields->field_indicies) {
1267         /* Keys are stored in fields->fields, values are
1268          * integers.
1269          */
1270         g_hash_table_destroy(fields->field_indicies);
1271     }
1272     if(NULL != fields->fields) {
1273         gsize i;
1274         for(i = 0; i < fields->fields->len; ++i) {
1275             gchar* field = g_ptr_array_index(fields->fields,i);
1276             g_free(field);
1277         }
1278         g_ptr_array_free(fields->fields, TRUE);
1279     }
1280
1281     g_free(fields);
1282 }
1283
1284 void output_fields_add(output_fields_t* fields, const gchar* field)
1285 {
1286     gchar* field_copy;
1287
1288     g_assert(fields);
1289     g_assert(field);
1290
1291
1292     if(NULL == fields->fields) {
1293         fields->fields = g_ptr_array_new();
1294     }
1295
1296     field_copy = g_strdup(field);
1297
1298     g_ptr_array_add(fields->fields, field_copy);
1299 }
1300
1301 gboolean output_fields_set_option(output_fields_t* info, gchar* option)
1302 {
1303     const gchar* option_name;
1304     const gchar* option_value;
1305
1306     g_assert(info);
1307     g_assert(option);
1308
1309     if('\0' == *option) {
1310         return FALSE; /* Is this guarded against by option parsing? */
1311     }
1312     option_name = strtok(option,"=");
1313     option_value = option + strlen(option_name) + 1;
1314     if(0 == strcmp(option_name, "header")) {
1315         switch(NULL == option_value ? '\0' : *option_value) {
1316         case 'n':
1317             info->print_header = FALSE;
1318             break;
1319         case 'y':
1320             info->print_header = TRUE;
1321             break;
1322         default:
1323             return FALSE;
1324         }
1325         return TRUE;
1326     }
1327
1328     if(0 == strcmp(option_name,"separator")) {
1329         switch(NULL == option_value ? '\0' : *option_value) {
1330         case '\0':
1331             return FALSE;
1332         case '/':
1333             switch(*++option_value) {
1334             case 't':
1335                 info->separator = '\t';
1336                 break;
1337             case 's':
1338                 info->separator = ' ';
1339                 break;
1340             default:
1341                 info->separator = '\\';
1342             }
1343             break;
1344         default:
1345             info->separator = *option_value;
1346             break;
1347         }
1348         return TRUE;
1349     }
1350
1351     if(0 == strcmp(option_name, "quote")) {
1352         switch(NULL == option_value ? '\0' : *option_value) {
1353         default: /* Fall through */
1354         case '\0':
1355             info->quote='\0';
1356             return FALSE;
1357         case 'd':
1358             info->quote='"';
1359             break;
1360         case 's':
1361             info->quote='\'';
1362             break;
1363         case 'n':
1364             info->quote='\0';
1365             break;
1366         }
1367         return TRUE;
1368     }
1369
1370     return FALSE;
1371 }
1372
1373 void output_fields_list_options(FILE *fh)
1374 {
1375     fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
1376     fputs("header=y|n   Print field abbreviations as first line of output (def: N: no)\n", fh);
1377     fputs("separator=/t|/s|<character>   Set the separator to use; \"/t\" = tab,\n \"/s\" = space (def: /t: tab)\n", fh);
1378     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);
1379 }
1380
1381
1382 void write_fields_preamble(output_fields_t* fields, FILE *fh)
1383 {
1384     gsize i;
1385
1386     g_assert(fields);
1387     g_assert(fh);
1388
1389     if(!fields->print_header) {
1390         return;
1391     }
1392
1393     for(i = 0; i < fields->fields->len; ++i) {
1394         const gchar* field = g_ptr_array_index(fields->fields,i);
1395         if(i != 0 ) {
1396             fputc(fields->separator, fh);
1397         }
1398         fputs(field, fh);
1399     }
1400     fputc('\n', fh);
1401 }
1402
1403 static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
1404 {
1405     write_field_data_t *call_data;
1406     field_info *fi;
1407     gpointer field_index;
1408
1409     call_data = data;
1410     fi = PITEM_FINFO(node);
1411
1412     field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
1413     if(NULL != field_index) {
1414         const gchar* value;
1415
1416         value = get_node_field_value(fi, call_data->edt); /* ep_alloced string */
1417
1418         if(NULL != value && '\0' != *value) {
1419             guint actual_index;
1420             actual_index = GPOINTER_TO_UINT(field_index);
1421             /* Unwrap change made to disambiguiate zero / null */
1422             call_data->fields->field_values[actual_index - 1] = value;
1423         }
1424     }
1425
1426     /* Recurse here. */
1427     if (node->first_child != NULL) {
1428         proto_tree_children_foreach(node, proto_tree_get_node_field_values,
1429                                     call_data);
1430     }
1431 }
1432
1433 void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, FILE *fh)
1434 {
1435     gsize i;
1436
1437     write_field_data_t data;
1438
1439     g_assert(fields);
1440     g_assert(edt);
1441     g_assert(fh);
1442
1443     data.fields = fields;
1444     data.edt = edt;
1445
1446     if(NULL == fields->field_indicies) {
1447         /* Prepare a lookup table from string abbreviation for field to its index. */
1448         fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
1449
1450         i = 0;
1451         while( i < fields->fields->len) {
1452             gchar* field = g_ptr_array_index(fields->fields, i);
1453              /* Store field indicies +1 so that zero is not a valid value,
1454               * and can be distinguished from NULL as a pointer.
1455               */
1456             ++i;
1457             g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
1458         }
1459     }
1460
1461     /* Buffer to store values for this packet */
1462     fields->field_values = ep_alloc_array0(const gchar*, fields->fields->len);
1463
1464     proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
1465                                 &data);
1466
1467     for(i = 0; i < fields->fields->len; ++i) {
1468         if(0 != i) {
1469             fputc(fields->separator, fh);
1470         }
1471         if(NULL != fields->field_values[i]) {
1472             if(fields->quote != '\0') {
1473                 fputc(fields->quote, fh);
1474             }
1475             fputs(fields->field_values[i], fh);
1476             if(fields->quote != '\0') {
1477                 fputc(fields->quote, fh);
1478             }
1479         }
1480     }
1481 }
1482
1483 void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
1484 {
1485     /* Nothing to do */
1486 }
1487
1488 /* Returns an ep_alloced string or a static constant*/
1489 const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
1490 {
1491     if (fi->hfinfo->id == hf_text_only) {
1492         /* Text label.
1493          * Get the text */
1494         if (fi->rep) {
1495             return fi->rep->representation;
1496         }
1497         else {
1498             return get_field_hex_value(edt->pi.data_src, fi);
1499         }
1500     }
1501     else if (fi->hfinfo->id == proto_data) {
1502         /* Uninterpreted data, i.e., the "Data" protocol, is
1503          * printed as a field instead of a protocol. */
1504         return get_field_hex_value(edt->pi.data_src, fi);
1505     }
1506     else {
1507         /* Normal protocols and fields */
1508         gchar      *dfilter_string;
1509         size_t      chop_len;
1510
1511         switch (fi->hfinfo->type)
1512         {
1513         case FT_PROTOCOL:
1514             /* Print out the full details for the protocol. */
1515             if (fi->rep) {
1516                 return fi->rep->representation;
1517             } else {
1518                 /* Just print out the protocol abbreviation */
1519                 return fi->hfinfo->abbrev;;
1520             }
1521         case FT_NONE:
1522             /* Return "1" so that the presence of a field of type
1523              * FT_NONE can be checked when using -T fields */
1524             return "1";
1525         default:
1526             /* XXX - this is a hack until we can just call
1527              * fvalue_to_string_repr() for *all* FT_* types. */
1528             dfilter_string = proto_construct_match_selected_string(fi,
1529                 edt);
1530             if (dfilter_string != NULL) {
1531                 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
1532
1533                 /* XXX - Remove double-quotes. Again, once we
1534                  * can call fvalue_to_string_repr(), we can
1535                  * ask it not to produce the version for
1536                  * display-filters, and thus, no
1537                  * double-quotes. */
1538                 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
1539                     dfilter_string[strlen(dfilter_string)-1] = '\0';
1540                     chop_len++;
1541                 }
1542
1543                 return &(dfilter_string[chop_len]);
1544             } else {
1545                 return get_field_hex_value(edt->pi.data_src, fi);
1546             }
1547         }
1548     }
1549 }
1550
1551 static const gchar*
1552 get_field_hex_value(GSList* src_list, field_info *fi)
1553 {
1554     const guint8 *pd;
1555
1556     if (!fi->ds_tvb)
1557         return NULL;
1558
1559     if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
1560         return "field length invalid!";
1561     }
1562
1563     /* Find the data for this field. */
1564     pd = get_field_data(src_list, fi);
1565
1566     if (pd) {
1567         int i;
1568         gchar* buffer;
1569         gchar* p;
1570         int len;
1571         const int chars_per_byte = 2;
1572
1573         len = chars_per_byte * fi->length;
1574         buffer = ep_alloc_array(gchar, len + 1);
1575         buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
1576         p = buffer;
1577         /* Print a simple hex dump */
1578         for (i = 0 ; i < fi->length; i++) {
1579             g_snprintf(p, chars_per_byte+1, "%02x", pd[i]);
1580             p += chars_per_byte;
1581         }
1582         return buffer;
1583     } else {
1584         return NULL;
1585     }
1586 }