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