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