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