tshark JSON and Elasticsearch output fix
[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 <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     gchar         **filter;
64 } write_pdml_data;
65
66 typedef struct {
67     int             level;
68     FILE           *fh;
69     GSList         *src_list;
70     epan_dissect_t *edt;
71     gchar         **filter;
72     gboolean        print_hex;
73 } write_json_data;
74
75 typedef struct {
76     output_fields_t *fields;
77     epan_dissect_t  *edt;
78 } write_field_data_t;
79
80 struct _output_fields {
81     gboolean     print_bom;
82     gboolean     print_header;
83     gchar        separator;
84     gchar        occurrence;
85     gchar        aggregator;
86     GPtrArray   *fields;
87     GHashTable  *field_indicies;
88     GPtrArray  **field_values;
89     gchar        quote;
90     gboolean     includes_col_fields;
91 };
92
93 static gchar *get_field_hex_value(GSList *src_list, field_info *fi);
94 static void proto_tree_print_node(proto_node *node, gpointer data);
95 static void proto_tree_write_node_pdml(proto_node *node, gpointer data);
96 static void proto_tree_write_node_json(proto_node *node, gpointer data);
97 static void proto_tree_write_node_ek(proto_node *node, gpointer data);
98 static const guint8 *get_field_data(GSList *src_list, field_info *fi);
99 static void pdml_write_field_hex_value(write_pdml_data *pdata, field_info *fi);
100 static void json_write_field_hex_value(write_json_data *pdata, field_info *fi);
101 static gboolean print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
102                                       guint length, packet_char_enc encoding);
103 static void print_escaped_xml(FILE *fh, const char *unescaped_string);
104 static void print_escaped_json(FILE *fh, const char *unescaped_string);
105 static void print_escaped_ek(FILE *fh, const char *unescaped_string);
106
107 static void print_pdml_geninfo(proto_tree *tree, FILE *fh);
108
109 static void proto_tree_get_node_field_values(proto_node *node, gpointer data);
110
111 /* Cache the protocols and field handles that the print functionality needs
112    This helps break explicit dependency on the dissectors. */
113 static int proto_data = -1;
114 static int proto_frame = -1;
115 static int hf_frame_arrival_time = -1;
116 static int hf_frame_number = -1;
117 static int hf_frame_len = -1;
118 static int hf_frame_capture_len = -1;
119
120 void print_cache_field_handles(void)
121 {
122     proto_data = proto_get_id_by_short_name("Data");
123     proto_frame = proto_get_id_by_short_name("Frame");
124     hf_frame_arrival_time = proto_registrar_get_id_byname("frame.time");
125     hf_frame_number = proto_registrar_get_id_byname("frame.number");
126     hf_frame_len = proto_registrar_get_id_byname("frame.len");
127     hf_frame_capture_len = proto_registrar_get_id_byname("frame.cap_len");
128 }
129
130 gboolean
131 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
132                  GHashTable *output_only_tables, print_stream_t *stream)
133 {
134     print_data data;
135
136     /* Create the output */
137     data.level              = 0;
138     data.stream             = stream;
139     data.success            = TRUE;
140     data.src_list           = edt->pi.data_src;
141     data.encoding           = (packet_char_enc)edt->pi.fd->flags.encoding;
142     data.print_dissections  = print_args->print_dissections;
143     /* If we're printing the entire packet in hex, don't
144        print uninterpreted data fields in hex as well. */
145     data.print_hex_for_data = !print_args->print_hex;
146     data.edt                = edt;
147     data.output_only_tables = output_only_tables;
148
149     proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
150     return data.success;
151 }
152
153 /* Print a tree's data, and any child nodes. */
154 static void
155 proto_tree_print_node(proto_node *node, gpointer data)
156 {
157     field_info   *fi    = PNODE_FINFO(node);
158     print_data   *pdata = (print_data*) data;
159     const guint8 *pd;
160     gchar         label_str[ITEM_LABEL_LENGTH];
161     gchar        *label_ptr;
162
163     /* dissection with an invisible proto tree? */
164     g_assert(fi);
165
166     /* Don't print invisible entries. */
167     if (PROTO_ITEM_IS_HIDDEN(node))
168         return;
169
170     /* Give up if we've already gotten an error. */
171     if (!pdata->success)
172         return;
173
174     /* was a free format label produced? */
175     if (fi->rep) {
176         label_ptr = fi->rep->representation;
177     }
178     else { /* no, make a generic label */
179         label_ptr = label_str;
180         proto_item_fill_label(fi, label_str);
181     }
182
183     if (PROTO_ITEM_IS_GENERATED(node))
184         label_ptr = g_strconcat("[", label_ptr, "]", NULL);
185
186     pdata->success = print_line(pdata->stream, pdata->level, label_ptr);
187
188     if (PROTO_ITEM_IS_GENERATED(node))
189         g_free(label_ptr);
190
191     if (!pdata->success)
192         return;
193
194     /*
195      * If -O is specified, only display the protocols which are in the
196      * lookup table.  Only check on the first level: once we start printing
197      * a tree, print the rest of the subtree.  Otherwise we won't print
198      * subitems whose abbreviation doesn't match the protocol--for example
199      * text items (whose abbreviation is simply "text").
200      */
201     if ((pdata->output_only_tables != NULL) && (pdata->level == 0)
202         && (g_hash_table_lookup(pdata->output_only_tables, fi->hfinfo->abbrev) == NULL)) {
203         return;
204     }
205
206     /* If it's uninterpreted data, dump it (unless our caller will
207        be printing the entire packet in hex). */
208     if ((fi->hfinfo->id == proto_data) && (pdata->print_hex_for_data)) {
209         /*
210          * Find the data for this field.
211          */
212         pd = get_field_data(pdata->src_list, fi);
213         if (pd) {
214             if (!print_line(pdata->stream, 0, "")) {
215                 pdata->success = FALSE;
216                 return;
217             }
218             if (!print_hex_data_buffer(pdata->stream, pd,
219                                        fi->length, pdata->encoding)) {
220                 pdata->success = FALSE;
221                 return;
222             }
223         }
224     }
225
226     /* If we're printing all levels, or if this node is one with a
227        subtree and its subtree is expanded, recurse into the subtree,
228        if it exists. */
229     g_assert((fi->tree_type >= -1) && (fi->tree_type < num_tree_types));
230     if ((pdata->print_dissections == print_dissections_expanded) ||
231         ((pdata->print_dissections == print_dissections_as_displayed) &&
232          (fi->tree_type >= 0) && tree_expanded(fi->tree_type))) {
233         if (node->first_child != NULL) {
234             pdata->level++;
235             proto_tree_children_foreach(node,
236                                         proto_tree_print_node, pdata);
237             pdata->level--;
238             if (!pdata->success)
239                 return;
240         }
241     }
242 }
243
244 #define PDML2HTML_XSL "pdml2html.xsl"
245 void
246 write_pdml_preamble(FILE *fh, const gchar *filename)
247 {
248     time_t t = time(NULL);
249     char *ts = asctime(localtime(&t));
250
251     ts[strlen(ts)-1] = 0; /* overwrite \n */
252
253     fputs("<?xml version=\"1.0\"?>\n", fh);
254     fputs("<?xml-stylesheet type=\"text/xsl\" href=\"" PDML2HTML_XSL "\"?>\n", fh);
255     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());
256     fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
257     fprintf(fh, "creator=\"%s/%s\" time=\"%s\" capture_file=\"%s\">\n", PACKAGE, VERSION, ts, filename ? filename : "");
258 }
259
260 void
261 write_json_preamble(FILE *fh)
262 {
263     fputs("[\n", fh);
264 }
265
266 /* Check if the str match the protocolfilter. json_filter is space
267    delimited string and str need to exact-match to one of the value. */
268 gboolean check_protocolfilter(gchar **protocolfilter, const char *str)
269 {
270     gboolean res = FALSE;
271     gchar **ptr;
272
273     if (str == NULL || protocolfilter == NULL) {
274         return FALSE;
275     }
276
277     for (ptr = protocolfilter; *ptr; ptr++) {
278         if (strcmp(*ptr, str) == 0) {
279             res = TRUE;
280             break;
281         }
282     }
283
284     return res;
285 }
286
287 void
288 write_pdml_proto_tree(gchar **protocolfilter, epan_dissect_t *edt, FILE *fh)
289 {
290     write_pdml_data data;
291
292     /* Create the output */
293     data.level    = 0;
294     data.fh       = fh;
295     data.src_list = edt->pi.data_src;
296     data.edt      = edt;
297     data.filter   = protocolfilter;
298
299     fprintf(fh, "<packet>\n");
300
301     /* Print a "geninfo" protocol as required by PDML */
302     print_pdml_geninfo(edt->tree, fh);
303
304     proto_tree_children_foreach(edt->tree, proto_tree_write_node_pdml,
305                                 &data);
306
307     fprintf(fh, "</packet>\n\n");
308 }
309
310 void
311 write_json_proto_tree(print_args_t *print_args, gchar **protocolfilter, epan_dissect_t *edt, FILE *fh)
312 {
313     write_json_data data;
314     char ts[30];
315     time_t t = time(NULL);
316     struct tm * timeinfo;
317     static gboolean is_first = TRUE;
318
319     /* Create the output */
320     data.level    = 1;
321     data.fh       = fh;
322     data.src_list = edt->pi.data_src;
323     data.edt      = edt;
324     data.filter   = protocolfilter;
325     data.print_hex = print_args->print_hex;
326
327     timeinfo = localtime(&t);
328     strftime(ts, 30, "%Y-%m-%d", timeinfo);
329
330     if (!is_first)
331         fputs("  ,\n", fh);
332     else
333         is_first = FALSE;
334
335     fputs("  {\n", fh);
336     fprintf(fh, "    \"_index\": \"packets-%s\",\n", ts);
337     fputs("    \"_type\": \"pcap_file\",\n", fh);
338     fputs("    \"_score\": null,\n", fh);
339     fputs("    \"_source\": {\n", fh);
340     fputs("      \"layers\": {\n", fh);
341
342     proto_tree_children_foreach(edt->tree, proto_tree_write_node_json,
343                                 &data);
344
345     fputs("      }\n", fh);
346     fputs("    }\n", fh);
347     fputs("  }", fh);
348
349 }
350
351 void
352 write_ek_proto_tree(print_args_t *print_args, gchar **protocolfilter, epan_dissect_t *edt, FILE *fh)
353 {
354     write_json_data data;
355     char ts[30];
356     time_t t = time(NULL);
357     struct tm  *timeinfo;
358     nstime_t   *timestamp;
359     GPtrArray  *finfo_array;
360
361     /* Create the output */
362     data.level    = 0;
363     data.fh       = fh;
364     data.src_list = edt->pi.data_src;
365     data.edt      = edt;
366     data.filter   = protocolfilter;
367     data.print_hex = print_args->print_hex;
368
369
370     timeinfo = localtime(&t);
371     strftime(ts, 30, "%Y-%m-%d", timeinfo);
372
373
374     /* Get frame protocol's finfo. */
375     finfo_array = proto_find_finfo(edt->tree, proto_frame);
376     if (g_ptr_array_len(finfo_array) < 1) {
377         return;
378     }
379     /* frame.time --> geninfo.timestamp */
380     finfo_array = proto_find_finfo(edt->tree, hf_frame_arrival_time);
381     if (g_ptr_array_len(finfo_array) < 1) {
382         return;
383     }
384     timestamp = (nstime_t *)fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
385     g_ptr_array_free(finfo_array, TRUE);
386
387
388     fprintf(fh, "{\"index\" : {\"_index\": \"packets-%s\", \"_type\": \"pcap_file\", \"_score\": null}}\n", ts);
389     /* Timestamp added for time indexing in Elasticsearch */
390     fprintf(fh, "{\"timestamp\" : \"%" G_GUINT64_FORMAT "%03d\", \"layers\" : {", (guint64)timestamp->secs, timestamp->nsecs/1000000);
391
392
393     proto_tree_children_foreach(edt->tree, proto_tree_write_node_ek,
394                                 &data);
395     fputs("}}\n", fh);
396 }
397
398 /* Write out a tree's data, and any child nodes, as PDML */
399 static void
400 proto_tree_write_node_pdml(proto_node *node, gpointer data)
401 {
402     field_info      *fi    = PNODE_FINFO(node);
403     write_pdml_data *pdata = (write_pdml_data*) data;
404     const gchar     *label_ptr;
405     gchar            label_str[ITEM_LABEL_LENGTH];
406     char            *dfilter_string;
407     int              i;
408     gboolean         wrap_in_fake_protocol;
409
410     /* dissection with an invisible proto tree? */
411     g_assert(fi);
412
413     /* Will wrap up top-level field items inside a fake protocol wrapper to
414        preserve the PDML schema */
415     wrap_in_fake_protocol =
416         (((fi->hfinfo->type != FT_PROTOCOL) ||
417           (fi->hfinfo->id == proto_data)) &&
418          (pdata->level == 0));
419
420     /* Indent to the correct level */
421     for (i = -1; i < pdata->level; i++) {
422         fputs("  ", pdata->fh);
423     }
424
425     if (wrap_in_fake_protocol) {
426         /* Open fake protocol wrapper */
427         fputs("<proto name=\"fake-field-wrapper\">\n", pdata->fh);
428
429         /* Indent to increased level before writing out field */
430         pdata->level++;
431         for (i = -1; i < pdata->level; i++) {
432             fputs("  ", pdata->fh);
433         }
434     }
435
436     /* Text label. It's printed as a field with no name. */
437     if (fi->hfinfo->id == hf_text_only) {
438         /* Get the text */
439         if (fi->rep) {
440             label_ptr = fi->rep->representation;
441         }
442         else {
443             label_ptr = "";
444         }
445
446         /* Show empty name since it is a required field */
447         fputs("<field name=\"", pdata->fh);
448         fputs("\" show=\"", pdata->fh);
449         print_escaped_xml(pdata->fh, label_ptr);
450
451         fprintf(pdata->fh, "\" size=\"%d", fi->length);
452         if (node->parent && node->parent->finfo && (fi->start < node->parent->finfo->start)) {
453             fprintf(pdata->fh, "\" pos=\"%d", node->parent->finfo->start + fi->start);
454         } else {
455             fprintf(pdata->fh, "\" pos=\"%d", fi->start);
456         }
457
458         if (fi->length > 0) {
459             fputs("\" value=\"", pdata->fh);
460             pdml_write_field_hex_value(pdata, fi);
461         }
462
463         if (node->first_child != NULL) {
464             fputs("\">\n", pdata->fh);
465         }
466         else {
467             fputs("\"/>\n", pdata->fh);
468         }
469     }
470
471     /* Uninterpreted data, i.e., the "Data" protocol, is
472      * printed as a field instead of a protocol. */
473     else if (fi->hfinfo->id == proto_data) {
474         /* Write out field with data */
475         fputs("<field name=\"data\" value=\"", pdata->fh);
476         pdml_write_field_hex_value(pdata, fi);
477         fputs("\">\n", pdata->fh);
478     }
479     /* Normal protocols and fields */
480     else {
481         if ((fi->hfinfo->type == FT_PROTOCOL) && (fi->hfinfo->id != proto_expert)) {
482             fputs("<proto name=\"", pdata->fh);
483         }
484         else {
485             fputs("<field name=\"", pdata->fh);
486         }
487         print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
488
489 #if 0
490         /* PDML spec, see:
491          * http://www.nbee.org/doku.php?id=netpdl:pdml_specification
492          *
493          * the show fields contains things in 'human readable' format
494          * showname: contains only the name of the field
495          * show: contains only the data of the field
496          * showdtl: contains additional details of the field data
497          * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
498          *
499          * XXX - the showname shouldn't contain the field data itself
500          * (like it's contained in the fi->rep->representation).
501          * Unfortunately, we don't have the field data representation for
502          * all fields, so this isn't currently possible */
503         fputs("\" showname=\"", pdata->fh);
504         print_escaped_xml(pdata->fh, fi->hfinfo->name);
505 #endif
506
507         if (fi->rep) {
508             fputs("\" showname=\"", pdata->fh);
509             print_escaped_xml(pdata->fh, fi->rep->representation);
510         }
511         else {
512             label_ptr = label_str;
513             proto_item_fill_label(fi, label_str);
514             fputs("\" showname=\"", pdata->fh);
515             print_escaped_xml(pdata->fh, label_ptr);
516         }
517
518         if (PROTO_ITEM_IS_HIDDEN(node))
519             fprintf(pdata->fh, "\" hide=\"yes");
520
521         fprintf(pdata->fh, "\" size=\"%d", fi->length);
522         if (node->parent && node->parent->finfo && (fi->start < node->parent->finfo->start)) {
523             fprintf(pdata->fh, "\" pos=\"%d", node->parent->finfo->start + fi->start);
524         } else {
525             fprintf(pdata->fh, "\" pos=\"%d", fi->start);
526         }
527 /*      fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
528
529         /* show, value, and unmaskedvalue attributes */
530         switch (fi->hfinfo->type)
531         {
532         case FT_PROTOCOL:
533             break;
534         case FT_NONE:
535             fputs("\" show=\"\" value=\"",  pdata->fh);
536             break;
537         default:
538             dfilter_string = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
539             if (dfilter_string != NULL) {
540
541                 fputs("\" show=\"", pdata->fh);
542                 print_escaped_xml(pdata->fh, dfilter_string);
543             }
544             wmem_free(NULL, dfilter_string);
545
546             /*
547              * XXX - should we omit "value" for any fields?
548              * What should we do for fields whose length is 0?
549              * They might come from a pseudo-header or from
550              * the capture header (e.g., time stamps), or
551              * they might be generated fields.
552              */
553             if (fi->length > 0) {
554                 fputs("\" value=\"", pdata->fh);
555
556                 if (fi->hfinfo->bitmask!=0) {
557                     switch (fi->value.ftype->ftype) {
558                         case FT_INT8:
559                         case FT_INT16:
560                         case FT_INT24:
561                         case FT_INT32:
562                             fprintf(pdata->fh, "%X", (guint) fvalue_get_sinteger(&fi->value));
563                             break;
564                         case FT_UINT8:
565                         case FT_UINT16:
566                         case FT_UINT24:
567                         case FT_UINT32:
568                             fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
569                             break;
570                         case FT_INT40:
571                         case FT_INT48:
572                         case FT_INT56:
573                         case FT_INT64:
574                             fprintf(pdata->fh, "%" G_GINT64_MODIFIER "X", fvalue_get_sinteger64(&fi->value));
575                             break;
576                         case FT_UINT40:
577                         case FT_UINT48:
578                         case FT_UINT56:
579                         case FT_UINT64:
580                         case FT_BOOLEAN:
581                             fprintf(pdata->fh, "%" G_GINT64_MODIFIER "X", fvalue_get_uinteger64(&fi->value));
582                             break;
583                         default:
584                             g_assert_not_reached();
585                     }
586                     fputs("\" unmaskedvalue=\"", pdata->fh);
587                     pdml_write_field_hex_value(pdata, fi);
588                 }
589                 else {
590                     pdml_write_field_hex_value(pdata, fi);
591                 }
592             }
593         }
594
595         if (node->first_child != NULL) {
596             fputs("\">\n", pdata->fh);
597         }
598         else if (fi->hfinfo->id == proto_data) {
599             fputs("\">\n", pdata->fh);
600         }
601         else {
602             fputs("\"/>\n", pdata->fh);
603         }
604     }
605
606     /* We print some levels for PDML. Recurse here. */
607     if (node->first_child != NULL) {
608         if(check_protocolfilter(pdata->filter, fi->hfinfo->abbrev)) {
609             pdata->level++;
610             proto_tree_children_foreach(node,
611                                         proto_tree_write_node_pdml, pdata);
612             pdata->level--;
613         } else {
614             /* Indent to the correct level */
615             for (i = -2; i < pdata->level; i++) {
616                 fputs("  ", pdata->fh);
617             }
618             /* print dummy field */
619             fputs("<field name=\"filtered\" value=\"", pdata->fh);
620             print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
621             fputs("\" />\n", pdata->fh);
622         }
623     }
624
625     /* Take back the extra level we added for fake wrapper protocol */
626     if (wrap_in_fake_protocol) {
627         pdata->level--;
628     }
629
630     if (node->first_child != NULL) {
631         /* Indent to correct level */
632         for (i = -1; i < pdata->level; i++) {
633             fputs("  ", pdata->fh);
634         }
635         /* Close off current element */
636         /* Data and expert "protocols" use simple tags */
637         if ((fi->hfinfo->id != proto_data) && (fi->hfinfo->id != proto_expert)) {
638             if (fi->hfinfo->type == FT_PROTOCOL) {
639                 fputs("</proto>\n", pdata->fh);
640             }
641             else {
642                 fputs("</field>\n", pdata->fh);
643             }
644         } else {
645             fputs("</field>\n", pdata->fh);
646         }
647     }
648
649     /* Close off fake wrapper protocol */
650     if (wrap_in_fake_protocol) {
651         fputs("</proto>\n", pdata->fh);
652     }
653 }
654
655
656 /* Write out a tree's data, and any child nodes, as JSON */
657 static void
658 proto_tree_write_node_json(proto_node *node, gpointer data)
659 {
660     field_info      *fi    = PNODE_FINFO(node);
661     write_json_data *pdata = (write_json_data*) data;
662     const gchar     *label_ptr;
663     char            *dfilter_string;
664     int              i;
665
666     /* dissection with an invisible proto tree? */
667     g_assert(fi);
668
669     /* Indent to the correct level */
670     for (i = -3; i < pdata->level; i++) {
671         fputs("  ", pdata->fh);
672     }
673
674     /* Text label. It's printed as a field with no name. */
675     if (fi->hfinfo->id == hf_text_only) {
676         /* Get the text */
677         if (fi->rep) {
678             label_ptr = fi->rep->representation;
679         }
680         else {
681             label_ptr = "";
682         }
683
684         /* Show empty name since it is a required field */
685         fputs("\"", pdata->fh);
686         print_escaped_json(pdata->fh, label_ptr);
687
688         if (node->first_child != NULL) {
689             fputs("\": {\n", pdata->fh);
690         }
691         else {
692             if (node->next == NULL) {
693               fputs("\": \"\"\n",  pdata->fh);
694             } else {
695               fputs("\": \"\",\n",  pdata->fh);
696             }
697         }
698     }
699
700     /* Normal protocols and fields */
701     else {
702         /*
703          * Hex dump -x
704          */
705         if (pdata->print_hex && fi->length > 0) {
706             fputs("\"", pdata->fh);
707             print_escaped_json(pdata->fh, fi->hfinfo->abbrev);
708             fputs("_raw", pdata->fh);
709             fputs("\": \"", pdata->fh);
710
711             if (fi->hfinfo->bitmask!=0) {
712                 switch (fi->value.ftype->ftype) {
713                     case FT_INT8:
714                     case FT_INT16:
715                     case FT_INT24:
716                     case FT_INT32:
717                         fprintf(pdata->fh, "%X", (guint) fvalue_get_sinteger(&fi->value));
718                         break;
719                     case FT_UINT8:
720                     case FT_UINT16:
721                     case FT_UINT24:
722                     case FT_UINT32:
723                         fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
724                         break;
725                     case FT_INT40:
726                     case FT_INT48:
727                     case FT_INT56:
728                     case FT_INT64:
729                         fprintf(pdata->fh, "%" G_GINT64_MODIFIER "X", fvalue_get_sinteger64(&fi->value));
730                         break;
731                     case FT_UINT40:
732                     case FT_UINT48:
733                     case FT_UINT56:
734                     case FT_UINT64:
735                     case FT_BOOLEAN:
736                         fprintf(pdata->fh, "%" G_GINT64_MODIFIER "X", fvalue_get_uinteger64(&fi->value));
737                         break;
738                     default:
739                         g_assert_not_reached();
740                 }
741                 fputs("\",\n", pdata->fh);
742             }
743             else {
744                 json_write_field_hex_value(pdata, fi);
745                 fputs("\",\n", pdata->fh);
746             }
747
748             /* Indent to the correct level */
749             for (i = -3; i < pdata->level; i++) {
750                 fputs("  ", pdata->fh);
751             }
752         }
753
754
755         fputs("\"", pdata->fh);
756
757         print_escaped_json(pdata->fh, fi->hfinfo->abbrev);
758
759         /* show, value, and unmaskedvalue attributes */
760         switch (fi->hfinfo->type)
761         {
762         case FT_PROTOCOL:
763             if (node->first_child != NULL) {
764                 fputs("\": {\n", pdata->fh);
765             }
766             break;
767         case FT_NONE:
768             if (node->first_child != NULL) {
769                 fputs("\": {\n", pdata->fh);
770             } else {
771                 if (node->next == NULL) {
772                   fputs("\": \"\"\n",  pdata->fh);
773                 } else {
774                   fputs("\": \"\",\n",  pdata->fh);
775                 }
776             }
777             break;
778         default:
779             dfilter_string = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
780             if (dfilter_string != NULL) {
781               if (node->first_child == NULL) {
782                 fputs("\": \"", pdata->fh);
783                 print_escaped_json(pdata->fh, dfilter_string);
784               } else {
785                 fputs("\": {\n", pdata->fh);
786               }
787             }
788             wmem_free(NULL, dfilter_string);
789
790             if (node->first_child == NULL) {
791               if (node->next == NULL) {
792                   fputs("\"\n", pdata->fh);
793               } else {
794                   fputs("\",\n", pdata->fh);
795               }
796             }
797         }
798
799     }
800
801     /* We print some levels for JSON. Recurse here. */
802     if (node->first_child != NULL) {
803         if (pdata->filter != NULL) {
804           if(check_protocolfilter(pdata->filter, fi->hfinfo->abbrev)) {
805             pdata->level++;
806             proto_tree_children_foreach(node,
807                                         proto_tree_write_node_json, pdata);
808             pdata->level--;
809           } else {
810               /* Indent to the correct level */
811               for (i = -4; i < pdata->level; i++) {
812                   fputs("  ", pdata->fh);
813               }
814               /* print dummy field */
815               fputs("\"filtered\": \"", pdata->fh);
816               print_escaped_ek(pdata->fh, fi->hfinfo->abbrev);
817               fputs("\"\n", pdata->fh);
818           }
819         } else {
820             pdata->level++;
821             proto_tree_children_foreach(node,
822                                         proto_tree_write_node_json, pdata);
823             pdata->level--;
824         }
825     }
826
827     if (node->first_child != NULL) {
828         /* Indent to correct level */
829         for (i = -3; i < pdata->level; i++) {
830             fputs("  ", pdata->fh);
831         }
832         /* Close off current element */
833         if (node->next == NULL) {
834             fputs("}\n", pdata->fh);
835         } else {
836             fputs("},\n", pdata->fh);
837         }
838     }
839 }
840
841 /* Write out a tree's data, and any child nodes, as JSON for EK */
842 static void
843 proto_tree_write_node_ek(proto_node *node, gpointer data)
844 {
845     field_info      *fi    = PNODE_FINFO(node);
846     field_info      *fi_parent    = PNODE_FINFO(node->parent);
847     write_json_data *pdata = (write_json_data*) data;
848     const gchar     *label_ptr;
849     char            *dfilter_string;
850     int              i;
851     gchar           *abbrev_escaped = NULL;
852
853     /* dissection with an invisible proto tree? */
854     g_assert(fi);
855
856     /* Text label. It's printed as a field with no name. */
857     if (fi->hfinfo->id == hf_text_only) {
858         /* Get the text */
859         if (fi->rep) {
860             label_ptr = fi->rep->representation;
861         }
862         else {
863             label_ptr = "";
864         }
865
866         /* Show empty name since it is a required field */
867         fputs("\"", pdata->fh);
868         if (fi_parent != NULL) {
869             print_escaped_ek(pdata->fh, fi_parent->hfinfo->abbrev);
870             fputs("_", pdata->fh);
871         }
872         print_escaped_ek(pdata->fh, fi->hfinfo->abbrev);
873
874         if (node->first_child != NULL) {
875             fputs("\": \"", pdata->fh);
876             print_escaped_json(pdata->fh, label_ptr);
877             fputs("\",", pdata->fh);
878
879         }
880         else {
881             if (node->next == NULL) {
882               fputs("\": \"",  pdata->fh);
883               print_escaped_json(pdata->fh, label_ptr);
884               fputs("\"", pdata->fh);
885             } else {
886               fputs("\": \"",  pdata->fh);
887               print_escaped_json(pdata->fh, label_ptr);
888                fputs("\",", pdata->fh);
889             }
890         }
891     }
892
893     /* Normal protocols and fields */
894     else {
895         /*
896          * Hex dump -x
897          */
898         if (pdata->print_hex && fi->length > 0) {
899             fputs("\"", pdata->fh);
900             if (fi_parent != NULL) {
901                 print_escaped_ek(pdata->fh, fi_parent->hfinfo->abbrev);
902                 fputs("_", pdata->fh);
903             }
904             print_escaped_ek(pdata->fh, fi->hfinfo->abbrev);
905             fputs("_raw", pdata->fh);
906             fputs("\": \"", pdata->fh);
907
908             if (fi->hfinfo->bitmask!=0) {
909                 switch (fi->value.ftype->ftype) {
910                     case FT_INT8:
911                     case FT_INT16:
912                     case FT_INT24:
913                     case FT_INT32:
914                         fprintf(pdata->fh, "%X", (guint) fvalue_get_sinteger(&fi->value));
915                         break;
916                     case FT_UINT8:
917                     case FT_UINT16:
918                     case FT_UINT24:
919                     case FT_UINT32:
920                         fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
921                         break;
922                     case FT_INT40:
923                     case FT_INT48:
924                     case FT_INT56:
925                     case FT_INT64:
926                         fprintf(pdata->fh, "%" G_GINT64_MODIFIER "X", fvalue_get_sinteger64(&fi->value));
927                         break;
928                     case FT_UINT40:
929                     case FT_UINT48:
930                     case FT_UINT56:
931                     case FT_UINT64:
932                     case FT_BOOLEAN:
933                         fprintf(pdata->fh, "%" G_GINT64_MODIFIER "X", fvalue_get_uinteger64(&fi->value));
934                         break;
935                     default:
936                         g_assert_not_reached();
937                 }
938                 fputs("\",", pdata->fh);
939             }
940             else {
941                 json_write_field_hex_value(pdata, fi);
942                 fputs("\",", pdata->fh);
943             }
944         }
945
946
947
948         fputs("\"", pdata->fh);
949
950         if (fi_parent != NULL) {
951             print_escaped_ek(pdata->fh, fi_parent->hfinfo->abbrev);
952             fputs("_", pdata->fh);
953         }
954         print_escaped_ek(pdata->fh, fi->hfinfo->abbrev);
955
956         /* show, value, and unmaskedvalue attributes */
957         switch (fi->hfinfo->type)
958         {
959         case FT_PROTOCOL:
960             if (node->first_child != NULL) {
961                 fputs("\": {", pdata->fh);
962             }
963             break;
964         case FT_NONE:
965             if (node->first_child != NULL) {
966                 fputs("\": \"\",",  pdata->fh);
967             } else {
968                 if (node->next == NULL) {
969                   fputs("\": \"\"",  pdata->fh);
970                 } else {
971                   fputs("\": \"\",",  pdata->fh);
972                 }
973             }
974             break;
975         default:
976             dfilter_string = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
977             if (dfilter_string != NULL) {
978               if (node->first_child == NULL) {
979                 fputs("\": \"", pdata->fh);
980                 print_escaped_json(pdata->fh, dfilter_string);
981               } else {
982                   fputs("\": \"\",", pdata->fh);
983               }
984             }
985             wmem_free(NULL, dfilter_string);
986
987             if (node->first_child == NULL) {
988               if (node->next == NULL) {
989                   fputs("\"", pdata->fh);
990               } else {
991                   fputs("\",", pdata->fh);
992               }
993             }
994         }
995
996     }
997
998     /* We print some levels for JSON. Recurse here. */
999     if (node->first_child != NULL) {
1000
1001         if (pdata->filter != NULL) {
1002
1003           /* to to thread the '.' and '_' equally. The '.' is replace by print_escaped_ek for '_' */
1004           if (fi->hfinfo->abbrev != NULL) {
1005             if (strlen(fi->hfinfo->abbrev) > 0) {
1006                 abbrev_escaped = g_strdup(fi->hfinfo->abbrev);
1007
1008                 i = 0;
1009                 while(abbrev_escaped[i]!='\0') {
1010                    if(abbrev_escaped[i]=='.')
1011                    {
1012                        abbrev_escaped[i]='_';
1013                    }
1014                    i++;
1015                  }
1016             }
1017           }
1018
1019           if(check_protocolfilter(pdata->filter, fi->hfinfo->abbrev) || check_protocolfilter(pdata->filter, abbrev_escaped)) {
1020             pdata->level++;
1021             proto_tree_children_foreach(node,
1022                                         proto_tree_write_node_ek, pdata);
1023             pdata->level--;
1024           } else {
1025               /* print dummy field */
1026               fputs("\"filtered\": \"", pdata->fh);
1027               print_escaped_ek(pdata->fh, fi->hfinfo->abbrev);
1028               fputs("\"", pdata->fh);
1029           }
1030
1031           /* release abbrev_escaped string */
1032           if (abbrev_escaped != NULL) {
1033               g_free(abbrev_escaped);
1034           }
1035
1036         } else {
1037             pdata->level++;
1038             proto_tree_children_foreach(node,
1039                                         proto_tree_write_node_ek, pdata);
1040             pdata->level--;
1041         }
1042     }
1043
1044     if (node->first_child != NULL) {
1045       if (fi->hfinfo->type == FT_PROTOCOL) {
1046         /* Close off current element */
1047           if (node->next == NULL) {
1048               fputs("}", pdata->fh);
1049           } else {
1050               fputs("},", pdata->fh);
1051           }
1052       } else {
1053           if (node->next != NULL) {
1054               fputs(",", pdata->fh);
1055           }
1056       }
1057     }
1058 }
1059
1060 /* Print info for a 'geninfo' pseudo-protocol. This is required by
1061  * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
1062  * but we produce a 'geninfo' protocol in the PDML to conform to spec.
1063  * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
1064 static void
1065 print_pdml_geninfo(proto_tree *tree, FILE *fh)
1066 {
1067     guint32     num, len, caplen;
1068     nstime_t   *timestamp;
1069     GPtrArray  *finfo_array;
1070     field_info *frame_finfo;
1071     gchar      *tmp;
1072
1073     /* Get frame protocol's finfo. */
1074     finfo_array = proto_find_finfo(tree, proto_frame);
1075     if (g_ptr_array_len(finfo_array) < 1) {
1076         return;
1077     }
1078     frame_finfo = (field_info *)finfo_array->pdata[0];
1079     g_ptr_array_free(finfo_array, TRUE);
1080
1081     /* frame.number --> geninfo.num */
1082     finfo_array = proto_find_finfo(tree, hf_frame_number);
1083     if (g_ptr_array_len(finfo_array) < 1) {
1084         return;
1085     }
1086     num = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
1087     g_ptr_array_free(finfo_array, TRUE);
1088
1089     /* frame.frame_len --> geninfo.len */
1090     finfo_array = proto_find_finfo(tree, hf_frame_len);
1091     if (g_ptr_array_len(finfo_array) < 1) {
1092         return;
1093     }
1094     len = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
1095     g_ptr_array_free(finfo_array, TRUE);
1096
1097     /* frame.cap_len --> geninfo.caplen */
1098     finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
1099     if (g_ptr_array_len(finfo_array) < 1) {
1100         return;
1101     }
1102     caplen = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
1103     g_ptr_array_free(finfo_array, TRUE);
1104
1105     /* frame.time --> geninfo.timestamp */
1106     finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
1107     if (g_ptr_array_len(finfo_array) < 1) {
1108         return;
1109     }
1110     timestamp = (nstime_t *)fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
1111     g_ptr_array_free(finfo_array, TRUE);
1112
1113     /* Print geninfo start */
1114     fprintf(fh,
1115             "  <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%d\">\n",
1116             frame_finfo->length);
1117
1118     /* Print geninfo.num */
1119     fprintf(fh,
1120             "    <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%d\"/>\n",
1121             num, num, frame_finfo->length);
1122
1123     /* Print geninfo.len */
1124     fprintf(fh,
1125             "    <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Frame Length\" value=\"%x\" size=\"%d\"/>\n",
1126             len, len, frame_finfo->length);
1127
1128     /* Print geninfo.caplen */
1129     fprintf(fh,
1130             "    <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%d\"/>\n",
1131             caplen, caplen, frame_finfo->length);
1132
1133     tmp = abs_time_to_str(NULL, timestamp, ABSOLUTE_TIME_LOCAL, TRUE);
1134
1135     /* Print geninfo.timestamp */
1136     fprintf(fh,
1137             "    <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%d\"/>\n",
1138             tmp, (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
1139
1140     wmem_free(NULL, tmp);
1141
1142     /* Print geninfo end */
1143     fprintf(fh,
1144             "  </proto>\n");
1145 }
1146
1147 void
1148 write_pdml_finale(FILE *fh)
1149 {
1150     fputs("</pdml>\n", fh);
1151 }
1152
1153 void
1154 write_json_finale(FILE *fh)
1155 {
1156     fputs("]\n", fh);
1157 }
1158
1159 void
1160 write_psml_preamble(column_info *cinfo, FILE *fh)
1161 {
1162     gint i;
1163
1164     fputs("<?xml version=\"1.0\"?>\n", fh);
1165     fputs("<psml version=\"" PSML_VERSION "\" ", fh);
1166     fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
1167     fprintf(fh, "<structure>\n");
1168
1169     for (i = 0; i < cinfo->num_cols; i++) {
1170         fprintf(fh, "<section>");
1171         print_escaped_xml(fh, cinfo->columns[i].col_title);
1172         fprintf(fh, "</section>\n");
1173     }
1174
1175     fprintf(fh, "</structure>\n\n");
1176 }
1177
1178 void
1179 write_psml_columns(epan_dissect_t *edt, FILE *fh)
1180 {
1181     gint i;
1182
1183     fprintf(fh, "<packet>\n");
1184
1185     for (i = 0; i < edt->pi.cinfo->num_cols; i++) {
1186         fprintf(fh, "<section>");
1187         print_escaped_xml(fh, edt->pi.cinfo->columns[i].col_data);
1188         fprintf(fh, "</section>\n");
1189     }
1190
1191     fprintf(fh, "</packet>\n\n");
1192 }
1193
1194 void
1195 write_psml_finale(FILE *fh)
1196 {
1197     fputs("</psml>\n", fh);
1198 }
1199
1200 static gchar *csv_massage_str(const gchar *source, const gchar *exceptions)
1201 {
1202     gchar *csv_str;
1203     gchar *tmp_str;
1204
1205     /* In general, our output for any field can contain Unicode characters,
1206        so g_strescape (which escapes any non-ASCII) is the wrong thing to do.
1207        Unfortunately glib doesn't appear to provide g_unicode_strescape()... */
1208     csv_str = g_strescape(source, exceptions);
1209     tmp_str = csv_str;
1210     /* Locate the UTF-8 right arrow character and replace it by an ASCII equivalent */
1211     while ( (tmp_str = strstr(tmp_str, UTF8_RIGHTWARDS_ARROW)) != NULL ) {
1212         tmp_str[0] = ' ';
1213         tmp_str[1] = '>';
1214         tmp_str[2] = ' ';
1215     }
1216     tmp_str = csv_str;
1217     while ( (tmp_str = strstr(tmp_str, "\\\"")) != NULL )
1218         *tmp_str = '\"';
1219     return csv_str;
1220 }
1221
1222 static void csv_write_str(const char *str, char sep, FILE *fh)
1223 {
1224     gchar *csv_str;
1225
1226     /* Do not escape the UTF-8 right arrow character */
1227     csv_str = csv_massage_str(str, UTF8_RIGHTWARDS_ARROW);
1228     fprintf(fh, "\"%s\"%c", csv_str, sep);
1229     g_free(csv_str);
1230 }
1231
1232 void
1233 write_csv_column_titles(column_info *cinfo, FILE *fh)
1234 {
1235     gint i;
1236
1237     for (i = 0; i < cinfo->num_cols - 1; i++)
1238         csv_write_str(cinfo->columns[i].col_title, ',', fh);
1239     csv_write_str(cinfo->columns[i].col_title, '\n', fh);
1240 }
1241
1242 void
1243 write_csv_columns(epan_dissect_t *edt, FILE *fh)
1244 {
1245     gint i;
1246
1247     for (i = 0; i < edt->pi.cinfo->num_cols - 1; i++)
1248         csv_write_str(edt->pi.cinfo->columns[i].col_data, ',', fh);
1249     csv_write_str(edt->pi.cinfo->columns[i].col_data, '\n', fh);
1250 }
1251
1252 void
1253 write_carrays_hex_data(guint32 num, FILE *fh, epan_dissect_t *edt)
1254 {
1255     guint32       i = 0, src_num = 0;
1256     GSList       *src_le;
1257     tvbuff_t     *tvb;
1258     char         *name;
1259     const guchar *cp;
1260     guint         length;
1261     char          ascii[9];
1262     struct data_source *src;
1263
1264     for (src_le = edt->pi.data_src; src_le != NULL; src_le = src_le->next) {
1265         memset(ascii, 0, sizeof(ascii));
1266         src = (struct data_source *)src_le->data;
1267         tvb = get_data_source_tvb(src);
1268         length = tvb_captured_length(tvb);
1269         if (length == 0)
1270             continue;
1271
1272         cp = tvb_get_ptr(tvb, 0, length);
1273
1274         name = get_data_source_name(src);
1275         if (name) {
1276             fprintf(fh, "/* %s */\n", name);
1277             wmem_free(NULL, name);
1278         }
1279         if (src_num) {
1280             fprintf(fh, "static const unsigned char pkt%u_%u[%u] = {\n",
1281                     num, src_num, length);
1282         } else {
1283             fprintf(fh, "static const unsigned char pkt%u[%u] = {\n",
1284                     num, length);
1285         }
1286         src_num++;
1287
1288         for (i = 0; i < length; i++) {
1289             fprintf(fh, "0x%02x", *(cp + i));
1290             ascii[i % 8] = g_ascii_isprint(*(cp + i)) ? *(cp + i) : '.';
1291
1292             if (i == (length - 1)) {
1293                 guint rem;
1294                 rem = length % 8;
1295                 if (rem) {
1296                     guint j;
1297                     for ( j = 0; j < 8 - rem; j++ )
1298                         fprintf(fh, "      ");
1299                 }
1300                 fprintf(fh, "  /* %s */\n};\n\n", ascii);
1301                 break;
1302             }
1303
1304             if (!((i + 1) % 8)) {
1305                 fprintf(fh, ", /* %s */\n", ascii);
1306                 memset(ascii, 0, sizeof(ascii));
1307             }
1308             else {
1309                 fprintf(fh, ", ");
1310             }
1311         }
1312     }
1313 }
1314
1315 /*
1316  * Find the data source for a specified field, and return a pointer
1317  * to the data in it. Returns NULL if the data is out of bounds.
1318  */
1319 /* XXX: What am I missing ?
1320  *      Why bother searching for fi->ds_tvb for the matching tvb
1321  *       in the data_source list ?
1322  *      IOW: Why not just use fi->ds_tvb for the arg to tvb_get_ptr() ?
1323  */
1324
1325 static const guint8 *
1326 get_field_data(GSList *src_list, field_info *fi)
1327 {
1328     GSList   *src_le;
1329     tvbuff_t *src_tvb;
1330     gint      length, tvbuff_length;
1331     struct data_source *src;
1332
1333     for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
1334         src = (struct data_source *)src_le->data;
1335         src_tvb = get_data_source_tvb(src);
1336         if (fi->ds_tvb == src_tvb) {
1337             /*
1338              * Found it.
1339              *
1340              * XXX - a field can have a length that runs past
1341              * the end of the tvbuff.  Ideally, that should
1342              * be fixed when adding an item to the protocol
1343              * tree, but checking the length when doing
1344              * that could be expensive.  Until we fix that,
1345              * we'll do the check here.
1346              */
1347             tvbuff_length = tvb_captured_length_remaining(src_tvb,
1348                                                  fi->start);
1349             if (tvbuff_length < 0) {
1350                 return NULL;
1351             }
1352             length = fi->length;
1353             if (length > tvbuff_length)
1354                 length = tvbuff_length;
1355             return tvb_get_ptr(src_tvb, fi->start, length);
1356         }
1357     }
1358     g_assert_not_reached();
1359     return NULL;  /* not found */
1360 }
1361
1362 /* Print a string, escaping out certain characters that need to
1363  * escaped out for XML. */
1364 static void
1365 print_escaped_xml(FILE *fh, const char *unescaped_string)
1366 {
1367     const char *p;
1368     char        temp_str[8];
1369
1370     for (p = unescaped_string; *p != '\0'; p++) {
1371         switch (*p) {
1372         case '&':
1373             fputs("&amp;", fh);
1374             break;
1375         case '<':
1376             fputs("&lt;", fh);
1377             break;
1378         case '>':
1379             fputs("&gt;", fh);
1380             break;
1381         case '"':
1382             fputs("&quot;", fh);
1383             break;
1384         case '\'':
1385             fputs("&#x27;", fh);
1386             break;
1387         default:
1388             if (g_ascii_isprint(*p))
1389                 fputc(*p, fh);
1390             else {
1391                 g_snprintf(temp_str, sizeof(temp_str), "\\x%x", (guint8)*p);
1392                 fputs(temp_str, fh);
1393             }
1394         }
1395     }
1396 }
1397
1398 /* Print a string, escaping out certain characters that need to
1399  * escaped out for JSON. */
1400 static void
1401 print_escaped_json(FILE *fh, const char *unescaped_string)
1402 {
1403     const char *p;
1404     char        temp_str[8];
1405
1406     for (p = unescaped_string; *p != '\0'; p++) {
1407         switch (*p) {
1408         case '"':
1409             fputs("\\\"", fh);
1410             break;
1411         case '\\':
1412             fputs("\\\\", fh);
1413             break;
1414         case '/':
1415             fputs("\\/", fh);
1416             break;
1417         case '\b':
1418             fputs("\\b", fh);
1419             break;
1420         case '\f':
1421             fputs("\\f", fh);
1422             break;
1423         case '\n':
1424             fputs("\\n", fh);
1425             break;
1426         case '\r':
1427             fputs("\\r", fh);
1428             break;
1429         case '\t':
1430             fputs("\\t", fh);
1431             break;
1432         default:
1433             if (g_ascii_isprint(*p))
1434                 fputc(*p, fh);
1435             else {
1436                 g_snprintf(temp_str, sizeof(temp_str), "\\u00%u", (guint8)*p);
1437                 fputs(temp_str, fh);
1438             }
1439         }
1440     }
1441 }
1442
1443 /* Print a string, escaping out certain characters that need to
1444  * escaped out for Elasticsearch title. */
1445 static void
1446 print_escaped_ek(FILE *fh, const char *unescaped_string)
1447 {
1448     const char *p;
1449     char        temp_str[8];
1450
1451     for (p = unescaped_string; *p != '\0'; p++) {
1452         switch (*p) {
1453         case '"':
1454             fputs("\\\"", fh);
1455             break;
1456         case '\\':
1457             fputs("\\\\", fh);
1458             break;
1459         case '/':
1460             fputs("\\/", fh);
1461             break;
1462         case '\b':
1463             fputs("\\b", fh);
1464             break;
1465         case '\f':
1466             fputs("\\f", fh);
1467             break;
1468         case '\n':
1469             fputs("\\n", fh);
1470             break;
1471         case '\r':
1472             fputs("\\r", fh);
1473             break;
1474         case '\t':
1475             fputs("\\t", fh);
1476             break;
1477         case '.':
1478             fputs("_", fh);
1479             break;
1480         default:
1481             if (g_ascii_isprint(*p))
1482                 fputc(*p, fh);
1483             else {
1484                 g_snprintf(temp_str, sizeof(temp_str), "\\u00%u", (guint8)*p);
1485                 fputs(temp_str, fh);
1486             }
1487         }
1488     }
1489 }
1490
1491 static void
1492 pdml_write_field_hex_value(write_pdml_data *pdata, field_info *fi)
1493 {
1494     int           i;
1495     const guint8 *pd;
1496
1497     if (!fi->ds_tvb)
1498         return;
1499
1500     if (fi->length > tvb_captured_length_remaining(fi->ds_tvb, fi->start)) {
1501         fprintf(pdata->fh, "field length invalid!");
1502         return;
1503     }
1504
1505     /* Find the data for this field. */
1506     pd = get_field_data(pdata->src_list, fi);
1507
1508     if (pd) {
1509         /* Print a simple hex dump */
1510         for (i = 0 ; i < fi->length; i++) {
1511             fprintf(pdata->fh, "%02x", pd[i]);
1512         }
1513     }
1514 }
1515
1516 static void
1517 json_write_field_hex_value(write_json_data *pdata, field_info *fi)
1518 {
1519     int           i;
1520     const guint8 *pd;
1521
1522     if (!fi->ds_tvb)
1523         return;
1524
1525     if (fi->length > tvb_captured_length_remaining(fi->ds_tvb, fi->start)) {
1526         fprintf(pdata->fh, "field length invalid!");
1527         return;
1528     }
1529
1530     /* Find the data for this field. */
1531     pd = get_field_data(pdata->src_list, fi);
1532
1533     if (pd) {
1534         /* Print a simple hex dump */
1535         for (i = 0 ; i < fi->length; i++) {
1536             fprintf(pdata->fh, "%02x", pd[i]);
1537         }
1538     }
1539 }
1540
1541 gboolean
1542 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
1543 {
1544     gboolean      multiple_sources;
1545     GSList       *src_le;
1546     tvbuff_t     *tvb;
1547     char         *line, *name;
1548     const guchar *cp;
1549     guint         length;
1550     struct data_source *src;
1551
1552     /*
1553      * Set "multiple_sources" iff this frame has more than one
1554      * data source; if it does, we need to print the name of
1555      * the data source before printing the data from the
1556      * data source.
1557      */
1558     multiple_sources = (edt->pi.data_src->next != NULL);
1559
1560     for (src_le = edt->pi.data_src; src_le != NULL;
1561          src_le = src_le->next) {
1562         src = (struct data_source *)src_le->data;
1563         tvb = get_data_source_tvb(src);
1564         if (multiple_sources) {
1565             name = get_data_source_name(src);
1566             line = g_strdup_printf("%s:", name);
1567             wmem_free(NULL, name);
1568             print_line(stream, 0, line);
1569             g_free(line);
1570         }
1571         length = tvb_captured_length(tvb);
1572         if (length == 0)
1573             return TRUE;
1574         cp = tvb_get_ptr(tvb, 0, length);
1575         if (!print_hex_data_buffer(stream, cp, length,
1576                                    (packet_char_enc)edt->pi.fd->flags.encoding))
1577             return FALSE;
1578     }
1579     return TRUE;
1580 }
1581
1582 /*
1583  * This routine is based on a routine created by Dan Lasley
1584  * <DLASLEY@PROMUS.com>.
1585  *
1586  * It was modified for Wireshark by Gilbert Ramirez and others.
1587  */
1588
1589 #define MAX_OFFSET_LEN   8       /* max length of hex offset of bytes */
1590 #define BYTES_PER_LINE  16      /* max byte values printed on a line */
1591 #define HEX_DUMP_LEN    (BYTES_PER_LINE*3)
1592                                 /* max number of characters hex dump takes -
1593                                    2 digits plus trailing blank */
1594 #define DATA_DUMP_LEN   (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
1595                                 /* number of characters those bytes take;
1596                                    3 characters per byte of hex dump,
1597                                    2 blanks separating hex from ASCII,
1598                                    1 character per byte of ASCII dump */
1599 #define MAX_LINE_LEN    (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
1600                                 /* number of characters per line;
1601                                    offset, 2 blanks separating offset
1602                                    from data dump, data dump */
1603
1604 static gboolean
1605 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
1606                       guint length, packet_char_enc encoding)
1607 {
1608     register unsigned int ad, i, j, k, l;
1609     guchar                c;
1610     gchar                 line[MAX_LINE_LEN + 1];
1611     unsigned int          use_digits;
1612
1613     static gchar binhex[16] = {
1614         '0', '1', '2', '3', '4', '5', '6', '7',
1615         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
1616
1617     /*
1618      * How many of the leading digits of the offset will we supply?
1619      * We always supply at least 4 digits, but if the maximum offset
1620      * won't fit in 4 digits, we use as many digits as will be needed.
1621      */
1622     if (((length - 1) & 0xF0000000) != 0)
1623         use_digits = 8; /* need all 8 digits */
1624     else if (((length - 1) & 0x0F000000) != 0)
1625         use_digits = 7; /* need 7 digits */
1626     else if (((length - 1) & 0x00F00000) != 0)
1627         use_digits = 6; /* need 6 digits */
1628     else if (((length - 1) & 0x000F0000) != 0)
1629         use_digits = 5; /* need 5 digits */
1630     else
1631         use_digits = 4; /* we'll supply 4 digits */
1632
1633     ad = 0;
1634     i = 0;
1635     j = 0;
1636     k = 0;
1637     while (i < length) {
1638         if ((i & 15) == 0) {
1639             /*
1640              * Start of a new line.
1641              */
1642             j = 0;
1643             l = use_digits;
1644             do {
1645                 l--;
1646                 c = (ad >> (l*4)) & 0xF;
1647                 line[j++] = binhex[c];
1648             } while (l != 0);
1649             line[j++] = ' ';
1650             line[j++] = ' ';
1651             memset(line+j, ' ', DATA_DUMP_LEN);
1652
1653             /*
1654              * Offset in line of ASCII dump.
1655              */
1656             k = j + HEX_DUMP_LEN + 2;
1657         }
1658         c = *cp++;
1659         line[j++] = binhex[c>>4];
1660         line[j++] = binhex[c&0xf];
1661         j++;
1662         if (encoding == PACKET_CHAR_ENC_CHAR_EBCDIC) {
1663             c = EBCDIC_to_ASCII1(c);
1664         }
1665         line[k++] = ((c >= ' ') && (c < 0x7f)) ? c : '.';
1666         i++;
1667         if (((i & 15) == 0) || (i == length)) {
1668             /*
1669              * We'll be starting a new line, or
1670              * we're finished printing this buffer;
1671              * dump out the line we've constructed,
1672              * and advance the offset.
1673              */
1674             line[k] = '\0';
1675             if (!print_line(stream, 0, line))
1676                 return FALSE;
1677             ad += 16;
1678         }
1679     }
1680     return TRUE;
1681 }
1682
1683 gsize output_fields_num_fields(output_fields_t* fields)
1684 {
1685     g_assert(fields);
1686
1687     if (NULL == fields->fields) {
1688         return 0;
1689     } else {
1690         return fields->fields->len;
1691     }
1692 }
1693
1694 void output_fields_free(output_fields_t* fields)
1695 {
1696     g_assert(fields);
1697
1698     if (NULL != fields->fields) {
1699         gsize i;
1700
1701         if (NULL != fields->field_indicies) {
1702             /* Keys are stored in fields->fields, values are
1703              * integers.
1704              */
1705             g_hash_table_destroy(fields->field_indicies);
1706         }
1707
1708         if (NULL != fields->field_values) {
1709             g_free(fields->field_values);
1710         }
1711
1712         for(i = 0; i < fields->fields->len; ++i) {
1713             gchar* field = (gchar *)g_ptr_array_index(fields->fields,i);
1714             g_free(field);
1715         }
1716         g_ptr_array_free(fields->fields, TRUE);
1717     }
1718
1719     g_free(fields);
1720 }
1721
1722 #define COLUMN_FIELD_FILTER  "_ws.col."
1723
1724 void output_fields_add(output_fields_t *fields, const gchar *field)
1725 {
1726     gchar *field_copy;
1727
1728     g_assert(fields);
1729     g_assert(field);
1730
1731
1732     if (NULL == fields->fields) {
1733         fields->fields = g_ptr_array_new();
1734     }
1735
1736     field_copy = g_strdup(field);
1737
1738     g_ptr_array_add(fields->fields, field_copy);
1739
1740     /* See if we have a column as a field entry */
1741     if (!strncmp(field, COLUMN_FIELD_FILTER, strlen(COLUMN_FIELD_FILTER)))
1742         fields->includes_col_fields = TRUE;
1743
1744 }
1745
1746 static void
1747 output_field_check(void *data, void *user_data)
1748 {
1749     gchar *field = (gchar *)data;
1750     GSList **invalid_fields = (GSList **)user_data;
1751
1752     if (!strncmp(field, COLUMN_FIELD_FILTER, strlen(COLUMN_FIELD_FILTER)))
1753         return;
1754
1755     if (!proto_registrar_get_byname(field)) {
1756         *invalid_fields = g_slist_prepend(*invalid_fields, field);
1757     }
1758
1759 }
1760
1761 GSList *
1762 output_fields_valid(output_fields_t *fields)
1763 {
1764     GSList *invalid_fields = NULL;
1765     if (fields->fields == NULL) {
1766         return NULL;
1767     }
1768
1769     g_ptr_array_foreach(fields->fields, output_field_check, &invalid_fields);
1770
1771     return invalid_fields;
1772 }
1773
1774 gboolean output_fields_set_option(output_fields_t *info, gchar *option)
1775 {
1776     const gchar *option_name;
1777     const gchar *option_value;
1778
1779     g_assert(info);
1780     g_assert(option);
1781
1782     if ('\0' == *option) {
1783         return FALSE; /* this happens if we're called from tshark -E '' */
1784     }
1785     option_name = strtok(option, "=");
1786     if (!option_name) {
1787         return FALSE;
1788     }
1789     option_value = option + strlen(option_name) + 1;
1790     if (*option_value == '\0') {
1791         return FALSE;
1792     }
1793
1794     if (0 == strcmp(option_name, "header")) {
1795         switch (*option_value) {
1796         case 'n':
1797             info->print_header = FALSE;
1798             break;
1799         case 'y':
1800             info->print_header = TRUE;
1801             break;
1802         default:
1803             return FALSE;
1804         }
1805         return TRUE;
1806     }
1807     else if (0 == strcmp(option_name, "separator")) {
1808         switch (*option_value) {
1809         case '/':
1810             switch (*++option_value) {
1811             case 't':
1812                 info->separator = '\t';
1813                 break;
1814             case 's':
1815                 info->separator = ' ';
1816                 break;
1817             default:
1818                 info->separator = '\\';
1819             }
1820             break;
1821         default:
1822             info->separator = *option_value;
1823             break;
1824         }
1825         return TRUE;
1826     }
1827     else if (0 == strcmp(option_name, "occurrence")) {
1828         switch (*option_value) {
1829         case 'f':
1830         case 'l':
1831         case 'a':
1832             info->occurrence = *option_value;
1833             break;
1834         default:
1835             return FALSE;
1836         }
1837         return TRUE;
1838     }
1839     else if (0 == strcmp(option_name, "aggregator")) {
1840         switch (*option_value) {
1841         case '/':
1842             switch (*++option_value) {
1843             case 's':
1844                 info->aggregator = ' ';
1845                 break;
1846             default:
1847                 info->aggregator = '\\';
1848             }
1849             break;
1850         default:
1851             info->aggregator = *option_value;
1852             break;
1853         }
1854         return TRUE;
1855     }
1856     else if (0 == strcmp(option_name, "quote")) {
1857         switch (*option_value) {
1858         case 'd':
1859             info->quote = '"';
1860             break;
1861         case 's':
1862             info->quote = '\'';
1863             break;
1864         case 'n':
1865             info->quote = '\0';
1866             break;
1867         default:
1868             info->quote = '\0';
1869             return FALSE;
1870         }
1871         return TRUE;
1872     }
1873     else if (0 == strcmp(option_name, "bom")) {
1874         switch (*option_value) {
1875         case 'n':
1876             info->print_bom = FALSE;
1877             break;
1878         case 'y':
1879             info->print_bom = TRUE;
1880             break;
1881         default:
1882             return FALSE;
1883         }
1884         return TRUE;
1885     }
1886
1887     return FALSE;
1888 }
1889
1890 void output_fields_list_options(FILE *fh)
1891 {
1892     fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
1893     fputs("bom=y|n    Prepend output with the UTF-8 BOM (def: N: no)\n", fh);
1894     fputs("header=y|n    Print field abbreviations as first line of output (def: N: no)\n", fh);
1895     fputs("separator=/t|/s|<character>   Set the separator to use;\n     \"/t\" = tab, \"/s\" = space (def: /t: tab)\n", fh);
1896     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);
1897     fputs("aggregator=,|/s|<character>   Set the aggregator to use;\n     \",\" = comma, \"/s\" = space (def: ,: comma)\n", fh);
1898     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);
1899 }
1900
1901 gboolean output_fields_has_cols(output_fields_t* fields)
1902 {
1903     g_assert(fields);
1904     return fields->includes_col_fields;
1905 }
1906
1907 void write_fields_preamble(output_fields_t* fields, FILE *fh)
1908 {
1909     gsize i;
1910
1911     g_assert(fields);
1912     g_assert(fh);
1913     g_assert(fields->fields);
1914
1915     if (fields->print_bom) {
1916         fputs(UTF8_BOM, fh);
1917     }
1918
1919
1920     if (!fields->print_header) {
1921         return;
1922     }
1923
1924     for(i = 0; i < fields->fields->len; ++i) {
1925         const gchar* field = (const gchar *)g_ptr_array_index(fields->fields,i);
1926         if (i != 0 ) {
1927             fputc(fields->separator, fh);
1928         }
1929         fputs(field, fh);
1930     }
1931     fputc('\n', fh);
1932 }
1933
1934 static void format_field_values(output_fields_t* fields, gpointer field_index, const gchar* value)
1935 {
1936     guint      indx;
1937     GPtrArray* fv_p;
1938
1939     if (NULL == value)
1940         return;
1941
1942     /* Unwrap change made to disambiguiate zero / null */
1943     indx = GPOINTER_TO_UINT(field_index) - 1;
1944
1945     if (fields->field_values[indx] == NULL) {
1946         fields->field_values[indx] = g_ptr_array_new();
1947     }
1948
1949     /* Essentially: fieldvalues[indx] is a 'GPtrArray *' with each array entry */
1950     /*  pointing to a string which is (part of) the final output string.       */
1951
1952     fv_p = fields->field_values[indx];
1953
1954     switch (fields->occurrence) {
1955     case 'f':
1956         /* print the value of only the first occurrence of the field */
1957         if (g_ptr_array_len(fv_p) != 0)
1958             return;
1959         break;
1960     case 'l':
1961         /* print the value of only the last occurrence of the field */
1962         g_ptr_array_set_size(fv_p, 0);
1963         break;
1964     case 'a':
1965         /* print the value of all accurrences of the field */
1966         /* If not the first, add the 'aggregator' */
1967         if (g_ptr_array_len(fv_p) > 0) {
1968             g_ptr_array_add(fv_p, (gpointer)g_strdup_printf("%c", fields->aggregator));
1969         }
1970         break;
1971     default:
1972         g_assert_not_reached();
1973         break;
1974     }
1975
1976     g_ptr_array_add(fv_p, (gpointer)value);
1977 }
1978
1979 static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
1980 {
1981     write_field_data_t *call_data;
1982     field_info *fi;
1983     gpointer    field_index;
1984
1985     call_data = (write_field_data_t *)data;
1986     fi = PNODE_FINFO(node);
1987
1988     /* dissection with an invisible proto tree? */
1989     g_assert(fi);
1990
1991     field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
1992     if (NULL != field_index) {
1993         format_field_values(call_data->fields, field_index,
1994                             get_node_field_value(fi, call_data->edt) /* g_ alloc'd string */
1995             );
1996     }
1997
1998     /* Recurse here. */
1999     if (node->first_child != NULL) {
2000         proto_tree_children_foreach(node, proto_tree_get_node_field_values,
2001                                     call_data);
2002     }
2003 }
2004
2005 void write_fields_proto_tree(output_fields_t *fields, epan_dissect_t *edt, column_info *cinfo, FILE *fh)
2006 {
2007     gsize     i;
2008     gint      col;
2009     gchar    *col_name;
2010     gpointer  field_index;
2011
2012     write_field_data_t data;
2013
2014     g_assert(fields);
2015     g_assert(fields->fields);
2016     g_assert(edt);
2017     g_assert(fh);
2018
2019     data.fields = fields;
2020     data.edt = edt;
2021
2022     if (NULL == fields->field_indicies) {
2023         /* Prepare a lookup table from string abbreviation for field to its index. */
2024         fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
2025
2026         i = 0;
2027         while (i < fields->fields->len) {
2028             gchar *field = (gchar *)g_ptr_array_index(fields->fields, i);
2029             /* Store field indicies +1 so that zero is not a valid value,
2030              * and can be distinguished from NULL as a pointer.
2031              */
2032             ++i;
2033             g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
2034         }
2035     }
2036
2037     /* Array buffer to store values for this packet              */
2038     /*  Allocate an array for the 'GPtrarray *' the first time   */
2039     /*   ths function is invoked for a file;                     */
2040     /*  Any and all 'GPtrArray *' are freed (after use) each     */
2041     /*   time (each packet) this function is invoked for a flle. */
2042     /* XXX: ToDo: use packet-scope'd memory & (if/when implemented) wmem ptr_array */
2043     if (NULL == fields->field_values)
2044         fields->field_values = g_new0(GPtrArray*, fields->fields->len);  /* free'd in output_fields_free() */
2045
2046     proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
2047                                 &data);
2048
2049     if (fields->includes_col_fields) {
2050         for (col = 0; col < cinfo->num_cols; col++) {
2051             /* Prepend COLUMN_FIELD_FILTER as the field name */
2052             col_name = g_strdup_printf("%s%s", COLUMN_FIELD_FILTER, cinfo->columns[col].col_title);
2053             field_index = g_hash_table_lookup(fields->field_indicies, col_name);
2054             g_free(col_name);
2055
2056             if (NULL != field_index) {
2057                 format_field_values(fields, field_index, g_strdup(cinfo->columns[col].col_data));
2058             }
2059         }
2060     }
2061
2062     for(i = 0; i < fields->fields->len; ++i) {
2063         if (0 != i) {
2064             fputc(fields->separator, fh);
2065         }
2066         if (NULL != fields->field_values[i]) {
2067             GPtrArray *fv_p;
2068             gchar * str;
2069             gsize j;
2070             fv_p = fields->field_values[i];
2071             if (fields->quote != '\0') {
2072                 fputc(fields->quote, fh);
2073             }
2074
2075             /* Output the array of (partial) field values */
2076             for (j = 0; j < g_ptr_array_len(fv_p); j++ ) {
2077                 str = (gchar *)g_ptr_array_index(fv_p, j);
2078                 fputs(str, fh);
2079                 g_free(str);
2080             }
2081             if (fields->quote != '\0') {
2082                 fputc(fields->quote, fh);
2083             }
2084             g_ptr_array_free(fv_p, TRUE);  /* get ready for the next packet */
2085             fields->field_values[i] = NULL;
2086         }
2087     }
2088 }
2089
2090 void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
2091 {
2092     /* Nothing to do */
2093 }
2094
2095 /* Returns an g_malloced string */
2096 gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
2097 {
2098     if (fi->hfinfo->id == hf_text_only) {
2099         /* Text label.
2100          * Get the text */
2101         if (fi->rep) {
2102             return g_strdup(fi->rep->representation);
2103         }
2104         else {
2105             return get_field_hex_value(edt->pi.data_src, fi);
2106         }
2107     }
2108     else if (fi->hfinfo->id == proto_data) {
2109         /* Uninterpreted data, i.e., the "Data" protocol, is
2110          * printed as a field instead of a protocol. */
2111         return get_field_hex_value(edt->pi.data_src, fi);
2112     }
2113     else {
2114         /* Normal protocols and fields */
2115         gchar      *dfilter_string;
2116
2117         switch (fi->hfinfo->type)
2118         {
2119         case FT_PROTOCOL:
2120             /* Print out the full details for the protocol. */
2121             if (fi->rep) {
2122                 return g_strdup(fi->rep->representation);
2123             } else {
2124                 /* Just print out the protocol abbreviation */
2125                 return g_strdup(fi->hfinfo->abbrev);
2126             }
2127         case FT_NONE:
2128             /* Return "1" so that the presence of a field of type
2129              * FT_NONE can be checked when using -T fields */
2130             return g_strdup("1");
2131         default:
2132             dfilter_string = fvalue_to_string_repr(NULL, &fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
2133             if (dfilter_string != NULL) {
2134                 gchar* ret = g_strdup(dfilter_string);
2135                 wmem_free(NULL, dfilter_string);
2136                 return ret;
2137             } else {
2138                 return get_field_hex_value(edt->pi.data_src, fi);
2139             }
2140         }
2141     }
2142 }
2143
2144 static gchar*
2145 get_field_hex_value(GSList *src_list, field_info *fi)
2146 {
2147     const guint8 *pd;
2148
2149     if (!fi->ds_tvb)
2150         return NULL;
2151
2152     if (fi->length > tvb_captured_length_remaining(fi->ds_tvb, fi->start)) {
2153         return g_strdup("field length invalid!");
2154     }
2155
2156     /* Find the data for this field. */
2157     pd = get_field_data(src_list, fi);
2158
2159     if (pd) {
2160         int        i;
2161         gchar     *buffer;
2162         gchar     *p;
2163         int        len;
2164         const int  chars_per_byte = 2;
2165
2166         len    = chars_per_byte * fi->length;
2167         buffer = (gchar *)g_malloc(sizeof(gchar)*(len + 1));
2168         buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
2169         p = buffer;
2170         /* Print a simple hex dump */
2171         for (i = 0 ; i < fi->length; i++) {
2172             g_snprintf(p, chars_per_byte+1, "%02x", pd[i]);
2173             p += chars_per_byte;
2174         }
2175         return buffer;
2176     } else {
2177         return NULL;
2178     }
2179 }
2180
2181 output_fields_t* output_fields_new(void)
2182 {
2183     output_fields_t* fields     = g_new(output_fields_t, 1);
2184     fields->print_bom           = FALSE;
2185     fields->print_header        = FALSE;
2186     fields->separator           = '\t';
2187     fields->occurrence          = 'a';
2188     fields->aggregator          = ',';
2189     fields->fields              = NULL; /*Do lazy initialisation */
2190     fields->field_indicies      = NULL;
2191     fields->field_values        = NULL;
2192     fields->quote               ='\0';
2193     fields->includes_col_fields = FALSE;
2194     return fields;
2195 }
2196
2197 /*
2198  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
2199  *
2200  * Local variables:
2201  * c-basic-offset: 4
2202  * tab-width: 8
2203  * indent-tabs-mode: nil
2204  * End:
2205  *
2206  * vi: set shiftwidth=4 tabstop=8 expandtab:
2207  * :indentSize=4:tabSize=8:noTabs=true:
2208  */