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