Based on a change from Paul Blankenbaker, make the "show" attribute for
[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
39 #include "packet-range.h"
40 #include "print.h"
41 #include "ps.h"
42 #include "file_util.h"
43 #include <epan/charsets.h>
44 #include <epan/dissectors/packet-data.h>
45 #include <epan/dissectors/packet-frame.h>
46
47 #define PDML_VERSION "0"
48 #define PSML_VERSION "0"
49
50 typedef struct {
51         int                     level;
52         print_stream_t          *stream;
53         gboolean                success;
54         GSList                  *src_list;
55         print_dissections_e     print_dissections;
56         gboolean                print_hex_for_data;
57         char_enc                encoding;
58         epan_dissect_t          *edt;
59 } print_data;
60
61 typedef struct {
62         int                     level;
63         FILE                    *fh;
64         GSList                  *src_list;
65         epan_dissect_t          *edt;
66 } write_pdml_data;
67
68 static void proto_tree_print_node(proto_node *node, gpointer data);
69 static void proto_tree_write_node_pdml(proto_node *node, gpointer data);
70 static const guint8 *get_field_data(GSList *src_list, field_info *fi);
71 static void write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi);
72 static gboolean print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
73     guint length, char_enc encoding);
74 static void ps_clean_string(unsigned char *out, const unsigned char *in,
75                         int outbuf_size);
76 static void print_escaped_xml(FILE *fh, const char *unescaped_string);
77
78 static void print_pdml_geninfo(proto_tree *tree, FILE *fh);
79
80 static FILE *
81 open_print_dest(int to_file, const char *dest)
82 {
83         FILE    *fh;
84
85         /* Open the file or command for output */
86         if (to_file)
87                 fh = eth_fopen(dest, "w");
88         else
89                 fh = popen(dest, "w");
90
91         return fh;
92 }
93
94 static gboolean
95 close_print_dest(int to_file, FILE *fh)
96 {
97         /* Close the file or command */
98         if (to_file)
99                 return (fclose(fh) == 0);
100         else
101                 return (pclose(fh) == 0);
102 }
103
104 #define MAX_PS_LINE_LENGTH 256
105
106 gboolean
107 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
108     print_stream_t *stream)
109 {
110         print_data data;
111
112         /* Create the output */
113         data.level = 0;
114         data.stream = stream;
115         data.success = TRUE;
116         data.src_list = edt->pi.data_src;
117         data.encoding = edt->pi.fd->flags.encoding;
118         data.print_dissections = print_args->print_dissections;
119         /* If we're printing the entire packet in hex, don't
120            print uninterpreted data fields in hex as well. */
121         data.print_hex_for_data = !print_args->print_hex;
122         data.edt = edt;
123
124         proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
125         return data.success;
126 }
127
128 #define MAX_INDENT      160
129
130 /* Print a tree's data, and any child nodes. */
131 static
132 void proto_tree_print_node(proto_node *node, gpointer data)
133 {
134         field_info      *fi = PITEM_FINFO(node);
135         print_data      *pdata = (print_data*) data;
136         const guint8    *pd;
137         gchar           label_str[ITEM_LABEL_LENGTH];
138         gchar           *label_ptr;
139
140         /* Don't print invisible entries. */
141         if (PROTO_ITEM_IS_HIDDEN(node))
142                 return;
143
144         /* Give up if we've already gotten an error. */
145         if (!pdata->success)
146                 return;
147
148         /* was a free format label produced? */
149         if (fi->rep) {
150                 label_ptr = fi->rep->representation;
151         }
152         else { /* no, make a generic label */
153                 label_ptr = label_str;
154                 proto_item_fill_label(fi, label_str);
155         }
156
157     if(PROTO_ITEM_IS_GENERATED(node)) {
158         label_ptr = g_strdup_printf("[%s]", label_ptr);
159     }
160
161         if (!print_line(pdata->stream, pdata->level, label_ptr)) {
162                 pdata->success = FALSE;
163                 return;
164         }
165
166     if(PROTO_ITEM_IS_GENERATED(node)) {
167         g_free(label_ptr);
168     }
169
170         /* If it's uninterpreted data, dump it (unless our caller will
171            be printing the entire packet in hex). */
172         if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
173                 /*
174                  * Find the data for this field.
175                  */
176                 pd = get_field_data(pdata->src_list, fi);
177                 if (pd) {
178                         if (!print_hex_data_buffer(pdata->stream, pd,
179                             fi->length, pdata->encoding)) {
180                                 pdata->success = FALSE;
181                                 return;
182                         }
183                 }
184         }
185
186         /* If we're printing all levels, or if this node is one with a
187            subtree and its subtree is expanded, recurse into the subtree,
188            if it exists. */
189         g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
190         if (pdata->print_dissections == print_dissections_expanded ||
191             (pdata->print_dissections == print_dissections_as_displayed &&
192                 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
193                 if (node->first_child != NULL) {
194                         pdata->level++;
195                         proto_tree_children_foreach(node,
196                                 proto_tree_print_node, pdata);
197                         pdata->level--;
198                         if (!pdata->success)
199                                 return;
200                 }
201         }
202 }
203
204 void
205 write_pdml_preamble(FILE *fh)
206 {
207         fputs("<?xml version=\"1.0\"?>\n", fh);
208         fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
209         fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
210 }
211
212 void
213 proto_tree_write_pdml(epan_dissect_t *edt, FILE *fh)
214 {
215         write_pdml_data data;
216
217         /* Create the output */
218         data.level = 0;
219         data.fh = fh;
220         data.src_list = edt->pi.data_src;
221         data.edt = edt;
222
223         fprintf(fh, "<packet>\n");
224
225         /* Print a "geninfo" protocol as required by PDML */
226         print_pdml_geninfo(edt->tree, fh);
227
228         proto_tree_children_foreach(edt->tree, proto_tree_write_node_pdml,
229             &data);
230
231         fprintf(fh, "</packet>\n\n");
232 }
233
234 /* Write out a tree's data, and any child nodes, as PDML */
235 static void
236 proto_tree_write_node_pdml(proto_node *node, gpointer data)
237 {
238         field_info      *fi = PITEM_FINFO(node);
239         write_pdml_data *pdata = (write_pdml_data*) data;
240         const gchar     *label_ptr;
241         gchar           label_str[ITEM_LABEL_LENGTH];
242         char            *dfilter_string;
243         int             chop_len;
244         int             i;
245
246         for (i = -1; i < pdata->level; i++) {
247                 fputs("  ", pdata->fh);
248         }
249
250         /* Text label. It's printed as a field with no name. */
251         if (fi->hfinfo->id == hf_text_only) {
252                 /* Get the text */
253                 if (fi->rep) {
254                         label_ptr = fi->rep->representation;
255                 }
256                 else {
257                         label_ptr = "";
258                 }
259
260                 /* Show empty name since it is a required field */
261                 fputs("<field name=\"", pdata->fh);
262                 fputs("\" show=\"", pdata->fh);
263                 print_escaped_xml(pdata->fh, label_ptr);
264
265                 fprintf(pdata->fh, "\" size=\"%d", fi->length);
266                 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
267
268                 fputs("\" value=\"", pdata->fh);
269                 write_pdml_field_hex_value(pdata, fi);
270
271                 if (node->first_child != NULL) {
272                         fputs("\">\n", pdata->fh);
273                 }
274                 else {
275                         fputs("\"/>\n", pdata->fh);
276                 }
277         }
278         /* Uninterpreted data, i.e., the "Data" protocol, is
279          * printed as a field instead of a protocol. */
280         else if (fi->hfinfo->id == proto_data) {
281
282                 fputs("<field name=\"data\" value=\"", pdata->fh);
283
284                 write_pdml_field_hex_value(pdata, fi);
285
286                 fputs("\"/>\n", pdata->fh);
287
288         }
289         /* Normal protocols and fields */
290         else {
291                 if (fi->hfinfo->type == FT_PROTOCOL) {
292                         fputs("<proto name=\"", pdata->fh);
293                 }
294                 else {
295                         fputs("<field name=\"", pdata->fh);
296                 }
297                 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
298
299 #if 0
300         /* PDML spec, see: 
301          * http://analyzer.polito.it/30alpha/docs/dissectors/PDMLSpec.htm
302          *
303          * the show fields contains things in 'human readable' format
304          * showname: contains only the name of the field
305          * show: contains only the data of the field
306          * showdtl: contains additional details of the field data
307          * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
308          *
309          * XXX - the showname shouldn't contain the field data itself 
310          * (like it's contained in the fi->rep->representation). 
311          * Unfortunately, we don't have the field data representation for 
312          * all fields, so this isn't currently possible */
313                 fputs("\" showname=\"", pdata->fh);
314                 print_escaped_xml(pdata->fh, fi->hfinfo->name);
315 #endif
316
317                 if (fi->rep) {
318                         fputs("\" showname=\"", pdata->fh);
319                         print_escaped_xml(pdata->fh, fi->rep->representation);
320                 }
321                 else {
322                         label_ptr = label_str;
323                         proto_item_fill_label(fi, label_str);
324                         fputs("\" showname=\"", pdata->fh);
325                         print_escaped_xml(pdata->fh, label_ptr);
326                 }
327
328                 if (PROTO_ITEM_IS_HIDDEN(node))
329                         fprintf(pdata->fh, "\" hide=\"yes");
330
331                 fprintf(pdata->fh, "\" size=\"%d", fi->length);
332                 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
333 /*              fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
334
335                 /* show, value, and unmaskedvalue attributes */
336                 switch (fi->hfinfo->type)
337                 {
338                 case FT_PROTOCOL:
339                         break;
340                 case FT_NONE:
341                         fputs("\" show=\"\" value=\"",  pdata->fh);
342                         break;
343                 default:
344                         /*
345                          * Set the 'show' attribute to empty string
346                          * if the field is zero-length.
347                          *
348                          * XXX - why does the code in the else clause
349                          * not do that correctly?
350                          */
351                         if (fi->length == 0) {
352                                 fputs("\" show=\"",  pdata->fh);
353                         } else {
354                                 /* XXX - this is a hack until we can just call
355                                  * fvalue_to_string_repr() for *all* FT_*
356                                  * types. */
357                                 dfilter_string = proto_construct_dfilter_string(fi,
358                                         pdata->edt);
359                                 if (dfilter_string != NULL) {
360                                         chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
361
362                                         /* XXX - Remove double-quotes. Again,
363                                          * once we can call fvalue_to_string_repr(), we can
364                                          * ask it not to produce the version
365                                          * for display-filters, and thus, no
366                                          * double-quotes. */
367                                         if (dfilter_string[strlen(dfilter_string)-1] == '"') {
368                                                 dfilter_string[strlen(dfilter_string)-1] = '\0';
369                                                 chop_len++;
370                                         }
371
372                                         fputs("\" show=\"", pdata->fh);
373                                         print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
374                                 }
375                         }
376                         fputs("\" value=\"", pdata->fh);
377
378                         if (fi->hfinfo->bitmask!=0) {
379                                 fprintf(pdata->fh, "%X", fvalue_get_integer(&fi->value));
380                                 fputs("\" unmaskedvalue=\"", pdata->fh);
381                                 write_pdml_field_hex_value(pdata, fi);
382                         }
383                         else {
384                                 write_pdml_field_hex_value(pdata, fi);
385                         }
386                 }
387
388                 if (node->first_child != NULL) {
389                         fputs("\">\n", pdata->fh);
390                 }
391                 else if (fi->hfinfo->id == proto_data) {
392                         fputs("\">\n", pdata->fh);
393                 }
394                 else {
395                         fputs("\"/>\n", pdata->fh);
396                 }
397         }
398
399         /* We always print all levels for PDML. Recurse here. */
400         if (node->first_child != NULL) {
401                 pdata->level++;
402                 proto_tree_children_foreach(node,
403                                 proto_tree_write_node_pdml, pdata);
404                 pdata->level--;
405         }
406
407         if (node->first_child != NULL) {
408                 for (i = -1; i < pdata->level; i++) {
409                         fputs("  ", pdata->fh);
410                 }
411                 if (fi->hfinfo->type == FT_PROTOCOL) {
412                         fputs("</proto>\n", pdata->fh);
413                 }
414                 else {
415                         fputs("</field>\n", pdata->fh);
416                 }
417         }
418 }
419
420 /* Print info for a 'geninfo' pseudo-protocol. This is required by
421  * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
422  * but we produce a 'geninfo' protocol in the PDML to conform to spec.
423  * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
424 static void
425 print_pdml_geninfo(proto_tree *tree, FILE *fh)
426 {
427         guint32 num, len, caplen;
428         nstime_t *timestamp;
429         GPtrArray *finfo_array;
430         field_info *frame_finfo;
431
432         /* Get frame protocol's finfo. */
433         finfo_array = proto_find_finfo(tree, proto_frame);
434         if (g_ptr_array_len(finfo_array) < 1) {
435                 return;
436         }
437         frame_finfo = finfo_array->pdata[0];
438         g_ptr_array_free(finfo_array, FALSE);
439
440         /* frame.number --> geninfo.num */
441         finfo_array = proto_find_finfo(tree, hf_frame_number);
442         if (g_ptr_array_len(finfo_array) < 1) {
443                 return;
444         }
445         num = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
446         g_ptr_array_free(finfo_array, FALSE);
447
448         /* frame.pkt_len --> geninfo.len */
449         finfo_array = proto_find_finfo(tree, hf_frame_packet_len);
450         if (g_ptr_array_len(finfo_array) < 1) {
451                 return;
452         }
453         len = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
454         g_ptr_array_free(finfo_array, FALSE);
455
456         /* frame.cap_len --> geninfo.caplen */
457         finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
458         if (g_ptr_array_len(finfo_array) < 1) {
459                 return;
460         }
461         caplen = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
462         g_ptr_array_free(finfo_array, FALSE);
463
464         /* frame.time --> geninfo.timestamp */
465         finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
466         if (g_ptr_array_len(finfo_array) < 1) {
467                 return;
468         }
469         timestamp = fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
470         g_ptr_array_free(finfo_array, FALSE);
471
472         /* Print geninfo start */
473         fprintf(fh,
474 "  <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
475                 frame_finfo->length);
476
477         /* Print geninfo.num */
478         fprintf(fh,
479 "    <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
480                 num, num, frame_finfo->length);
481
482         /* Print geninfo.len */
483         fprintf(fh,
484 "    <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Packet Length\" value=\"%x\" size=\"%u\"/>\n",
485                 len, len, frame_finfo->length);
486
487         /* Print geninfo.caplen */
488         fprintf(fh,
489 "    <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
490                 caplen, caplen, frame_finfo->length);
491
492         /* Print geninfo.timestamp */
493         fprintf(fh,
494 "    <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
495                 abs_time_to_str(timestamp), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
496
497         /* Print geninfo end */
498         fprintf(fh,
499 "  </proto>\n");
500 }
501
502 void
503 write_pdml_finale(FILE *fh)
504 {
505         fputs("</pdml>\n", fh);
506 }
507
508 void
509 write_psml_preamble(FILE *fh)
510 {
511         fputs("<?xml version=\"1.0\"?>\n", fh);
512         fputs("<psml version=\"" PSML_VERSION "\" ", fh);
513         fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
514 }
515
516 void
517 proto_tree_write_psml(epan_dissect_t *edt, FILE *fh)
518 {
519         gint    i;
520
521         /* if this is the first packet, we have to create the PSML structure output */
522         if(edt->pi.fd->num == 1) {
523             fprintf(fh, "<structure>\n");
524
525             for(i=0; i < edt->pi.cinfo->num_cols; i++) {
526                 fprintf(fh, "<section>");
527                 print_escaped_xml(fh, edt->pi.cinfo->col_title[i]);
528                 fprintf(fh, "</section>\n");
529             }
530
531             fprintf(fh, "</structure>\n\n");
532         }
533
534         fprintf(fh, "<packet>\n");
535
536         for(i=0; i < edt->pi.cinfo->num_cols; i++) {
537             fprintf(fh, "<section>");
538             print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
539             fprintf(fh, "</section>\n");
540         }
541
542         fprintf(fh, "</packet>\n\n");
543 }
544
545 void
546 write_psml_finale(FILE *fh)
547 {
548         fputs("</psml>\n", fh);
549 }
550
551 void
552 write_csv_preamble(FILE *fh _U_)
553 {
554
555 }
556
557 void
558 proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
559 {
560         gint    i;
561
562         /* if this is the first packet, we have to write the CSV header */
563         if(edt->pi.fd->num == 1) {
564             for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
565                 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_title[i]);
566
567             fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_title[i]);
568         }
569
570         for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
571             fprintf(fh, "\"%s\",", edt->pi.cinfo->col_data[i]);
572
573         fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_data[i]);
574 }
575
576 void
577 write_csv_finale(FILE *fh _U_)
578 {
579
580 }
581
582 /*
583  * Find the data source for a specified field, and return a pointer
584  * to the data in it. Returns NULL if the data is out of bounds.
585  */
586 static const guint8 *
587 get_field_data(GSList *src_list, field_info *fi)
588 {
589         GSList *src_le;
590         data_source *src;
591         tvbuff_t *src_tvb;
592         gint length, tvbuff_length;
593
594         for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
595                 src = src_le->data;
596                 src_tvb = src->tvb;
597                 if (fi->ds_tvb == src_tvb) {
598                         /*
599                          * Found it.
600                          *
601                          * XXX - a field can have a length that runs past
602                          * the end of the tvbuff.  Ideally, that should
603                          * be fixed when adding an item to the protocol
604                          * tree, but checking the length when doing
605                          * that could be expensive.  Until we fix that,
606                          * we'll do the check here.
607                          */
608                         tvbuff_length = tvb_length_remaining(src_tvb,
609                             fi->start);
610                         if (tvbuff_length < 0) {
611                                 return NULL;
612                         }
613                         length = fi->length;
614                         if (length > tvbuff_length)
615                                 length = tvbuff_length;
616                         return tvb_get_ptr(src_tvb, fi->start, length);
617                 }
618         }
619         g_assert_not_reached();
620         return NULL;    /* not found */
621 }
622
623 /* Print a string, escaping out certain characters that need to
624  * escaped out for XML. */
625 static void
626 print_escaped_xml(FILE *fh, const char *unescaped_string)
627 {
628         const unsigned char *p;
629
630         for (p = unescaped_string; *p != '\0'; p++) {
631                 switch (*p) {
632                         case '&':
633                                 fputs("&amp;", fh);
634                                 break;
635                         case '<':
636                                 fputs("&lt;", fh);
637                                 break;
638                         case '>':
639                                 fputs("&gt;", fh);
640                                 break;
641                         case '"':
642                                 fputs("&quot;", fh);
643                                 break;
644                         case '\'':
645                                 fputs("&apos;", fh);
646                                 break;
647                         default:
648                                 fputc(*p, fh);
649                 }
650         }
651 }
652
653 static void
654 write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi)
655 {
656         int i;
657         const guint8 *pd;
658
659         if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
660                 fprintf(pdata->fh, "field length invalid!");
661                 return;
662         }
663
664         /* Find the data for this field. */
665         pd = get_field_data(pdata->src_list, fi);
666
667         if (pd) {
668                 /* Print a simple hex dump */
669                 for (i = 0 ; i < fi->length; i++) {
670                         fprintf(pdata->fh, "%02x", pd[i]);
671                 }
672         }
673 }
674
675 gboolean
676 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
677 {
678         gboolean multiple_sources;
679         GSList *src_le;
680         data_source *src;
681         tvbuff_t *tvb;
682         char *name;
683         char *line;
684         const guchar *cp;
685         guint length;
686
687         /*
688          * Set "multiple_sources" iff this frame has more than one
689          * data source; if it does, we need to print the name of
690          * the data source before printing the data from the
691          * data source.
692          */
693         multiple_sources = (edt->pi.data_src->next != NULL);
694
695         for (src_le = edt->pi.data_src; src_le != NULL;
696             src_le = src_le->next) {
697                 src = src_le->data;
698                 tvb = src->tvb;
699                 if (multiple_sources) {
700                         name = src->name;
701                         print_line(stream, 0, "");
702                         line = g_malloc(strlen(name) + 2);      /* <name>:\0 */
703                         strcpy(line, name);
704                         strcat(line, ":");
705                         print_line(stream, 0, line);
706                         g_free(line);
707                 }
708                 length = tvb_length(tvb);
709                 if (length == 0)
710                     return TRUE;
711                 cp = tvb_get_ptr(tvb, 0, length);
712                 if (!print_hex_data_buffer(stream, cp, length,
713                     edt->pi.fd->flags.encoding))
714                         return FALSE;
715         }
716         return TRUE;
717 }
718
719 /*
720  * This routine is based on a routine created by Dan Lasley
721  * <DLASLEY@PROMUS.com>.
722  *
723  * It was modified for Wireshark by Gilbert Ramirez and others.
724  */
725
726 #define MAX_OFFSET_LEN  8       /* max length of hex offset of bytes */
727 #define BYTES_PER_LINE  16      /* max byte values printed on a line */
728 #define HEX_DUMP_LEN    (BYTES_PER_LINE*3)
729                                 /* max number of characters hex dump takes -
730                                    2 digits plus trailing blank */
731 #define DATA_DUMP_LEN   (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
732                                 /* number of characters those bytes take;
733                                    3 characters per byte of hex dump,
734                                    2 blanks separating hex from ASCII,
735                                    1 character per byte of ASCII dump */
736 #define MAX_LINE_LEN    (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
737                                 /* number of characters per line;
738                                    offset, 2 blanks separating offset
739                                    from data dump, data dump */
740
741 static gboolean
742 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
743     guint length, char_enc encoding)
744 {
745         register unsigned int ad, i, j, k, l;
746         guchar c;
747         guchar line[MAX_LINE_LEN + 1];
748         unsigned int use_digits;
749         static guchar binhex[16] = {
750                 '0', '1', '2', '3', '4', '5', '6', '7',
751                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
752
753         if (!print_line(stream, 0, ""))
754                 return FALSE;
755
756         /*
757          * How many of the leading digits of the offset will we supply?
758          * We always supply at least 4 digits, but if the maximum offset
759          * won't fit in 4 digits, we use as many digits as will be needed.
760          */
761         if (((length - 1) & 0xF0000000) != 0)
762                 use_digits = 8; /* need all 8 digits */
763         else if (((length - 1) & 0x0F000000) != 0)
764                 use_digits = 7; /* need 7 digits */
765         else if (((length - 1) & 0x00F00000) != 0)
766                 use_digits = 6; /* need 6 digits */
767         else if (((length - 1) & 0x000F0000) != 0)
768                 use_digits = 5; /* need 5 digits */
769         else
770                 use_digits = 4; /* we'll supply 4 digits */
771
772         ad = 0;
773         i = 0;
774         j = 0;
775         k = 0;
776         while (i < length) {
777                 if ((i & 15) == 0) {
778                         /*
779                          * Start of a new line.
780                          */
781                         j = 0;
782                         k = 0;
783                         l = use_digits;
784                         do {
785                                 l--;
786                                 c = (ad >> (l*4)) & 0xF;
787                                 line[j++] = binhex[c];
788                         } while (l != 0);
789                         line[j++] = ' ';
790                         line[j++] = ' ';
791                         memset(line+j, ' ', DATA_DUMP_LEN);
792
793                         /*
794                          * Offset in line of ASCII dump.
795                          */
796                         k = j + HEX_DUMP_LEN + 2;
797                 }
798                 c = *cp++;
799                 line[j++] = binhex[c>>4];
800                 line[j++] = binhex[c&0xf];
801                 j++;
802                 if (encoding == CHAR_EBCDIC) {
803                         c = EBCDIC_to_ASCII1(c);
804                 }
805                 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
806                 i++;
807                 if ((i & 15) == 0 || i == length) {
808                         /*
809                          * We'll be starting a new line, or
810                          * we're finished printing this buffer;
811                          * dump out the line we've constructed,
812                          * and advance the offset.
813                          */
814                         line[k] = '\0';
815                         if (!print_line(stream, 0, line))
816                                 return FALSE;
817                         ad += 16;
818                 }
819         }
820         return TRUE;
821 }
822
823 static
824 void ps_clean_string(unsigned char *out, const unsigned char *in,
825                         int outbuf_size)
826 {
827         int rd, wr;
828         char c;
829
830         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
831                 c = in[rd];
832                 switch (c) {
833                         case '(':
834                         case ')':
835                         case '\\':
836                                 out[wr] = '\\';
837                                 out[++wr] = c;
838                                 break;
839
840                         default:
841                                 out[wr] = c;
842                                 break;
843                 }
844
845                 if (c == 0) {
846                         break;
847                 }
848         }
849 }
850
851 /* Some formats need stuff at the beginning of the output */
852 gboolean
853 print_preamble(print_stream_t *self, gchar *filename)
854 {
855         return (self->ops->print_preamble)(self, filename);
856 }
857
858 gboolean
859 print_line(print_stream_t *self, int indent, const char *line)
860 {
861         return (self->ops->print_line)(self, indent, line);
862 }
863
864 /* Insert bookmark */
865 gboolean
866 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
867 {
868         return (self->ops->print_bookmark)(self, name, title);
869 }
870
871 gboolean
872 new_page(print_stream_t *self)
873 {
874         return (self->ops->new_page)(self);
875 }
876
877 /* Some formats need stuff at the end of the output */
878 gboolean
879 print_finale(print_stream_t *self)
880 {
881         return (self->ops->print_finale)(self);
882 }
883
884 gboolean
885 destroy_print_stream(print_stream_t *self)
886 {
887         return (self->ops->destroy)(self);
888 }
889
890 typedef struct {
891         int to_file;
892         FILE *fh;
893 } output_text;
894
895 static gboolean
896 print_preamble_text(print_stream_t *self _U_, gchar *filename _U_)
897 {
898         /* do nothing */
899         return TRUE;    /* always succeeds */
900 }
901
902 static gboolean
903 print_line_text(print_stream_t *self, int indent, const char *line)
904 {
905         output_text *output = self->data;
906         char space[MAX_INDENT+1];
907         int i;
908         int num_spaces;
909
910         /* Prepare the tabs for printing, depending on tree level */
911         num_spaces = indent * 4;
912         if (num_spaces > MAX_INDENT) {
913                 num_spaces = MAX_INDENT;
914         }
915         for (i = 0; i < num_spaces; i++) {
916                 space[i] = ' ';
917         }
918         /* The string is NUL-terminated */
919         space[num_spaces] = '\0';
920
921         fputs(space, output->fh);
922         fputs(line, output->fh);
923         putc('\n', output->fh);
924         return !ferror(output->fh);
925 }
926
927 static gboolean
928 print_bookmark_text(print_stream_t *self _U_, const gchar *name _U_,
929     const gchar *title _U_)
930 {
931         /* do nothing */
932         return TRUE;
933 }
934
935 static gboolean
936 new_page_text(print_stream_t *self)
937 {
938         output_text *output = self->data;
939
940         fputs("\f", output->fh);
941         return !ferror(output->fh);
942 }
943
944 static gboolean
945 print_finale_text(print_stream_t *self _U_)
946 {
947         /* do nothing */
948         return TRUE;    /* always succeeds */
949 }
950
951 static gboolean
952 destroy_text(print_stream_t *self)
953 {
954         output_text *output = self->data;
955         gboolean ret;
956
957         ret = close_print_dest(output->to_file, output->fh);
958         g_free(output);
959         g_free(self);
960         return ret;
961 }
962
963 static const print_stream_ops_t print_text_ops = {
964         print_preamble_text,
965         print_line_text,
966         print_bookmark_text,
967         new_page_text,
968         print_finale_text,
969         destroy_text
970 };
971
972 print_stream_t *
973 print_stream_text_new(int to_file, const char *dest)
974 {
975         FILE *fh;
976         print_stream_t *stream;
977         output_text *output;
978
979         fh = open_print_dest(to_file, dest);
980         if (fh == NULL)
981                 return NULL;
982
983         output = g_malloc(sizeof *output);
984         output->to_file = to_file;
985         output->fh = fh;
986         stream = g_malloc(sizeof (print_stream_t));
987         stream->ops = &print_text_ops;
988         stream->data = output;
989
990         return stream;
991 }
992
993 print_stream_t *
994 print_stream_text_stdio_new(FILE *fh)
995 {
996         print_stream_t *stream;
997         output_text *output;
998
999         output = g_malloc(sizeof *output);
1000         output->to_file = TRUE;
1001         output->fh = fh;
1002         stream = g_malloc(sizeof (print_stream_t));
1003         stream->ops = &print_text_ops;
1004         stream->data = output;
1005
1006         return stream;
1007 }
1008
1009 typedef struct {
1010         int to_file;
1011         FILE *fh;
1012 } output_ps;
1013
1014 static gboolean
1015 print_preamble_ps(print_stream_t *self, gchar *filename)
1016 {
1017         output_ps *output = self->data;
1018         char            psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1019
1020         print_ps_preamble(output->fh);
1021
1022         fputs("%% Set the font to 10 point\n", output->fh);
1023         fputs("/Courier findfont 10 scalefont setfont\n", output->fh);
1024         fputs("\n", output->fh);
1025         fputs("%% the page title\n", output->fh);
1026         ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
1027         fprintf(output->fh, "/eth_pagetitle (%s - Wireshark) def\n", psbuffer);
1028         fputs("\n", output->fh);
1029         return !ferror(output->fh);
1030 }
1031
1032 static gboolean
1033 print_line_ps(print_stream_t *self, int indent, const char *line)
1034 {
1035         output_ps *output = self->data;
1036         char            psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1037
1038         ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
1039         fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
1040         return !ferror(output->fh);
1041 }
1042
1043 static gboolean
1044 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
1045 {
1046         output_ps *output = self->data;
1047         char            psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1048
1049         /*
1050          * See the Adobe "pdfmark reference":
1051          *
1052          *      http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
1053          *
1054          * The pdfmark stuff tells code that turns PostScript into PDF
1055          * things that it should do.
1056          *
1057          * The /OUT stuff creates a bookmark that goes to the
1058          * destination with "name" as the name and "title" as the title.
1059          *
1060          * The "/DEST" creates the destination.
1061          */
1062         ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
1063         fprintf(output->fh, "[/Dest /%s /Title (%s)   /OUT pdfmark\n", name,
1064             psbuffer);
1065         fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
1066             output->fh);
1067         fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
1068             output->fh);
1069         fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
1070         return !ferror(output->fh);
1071 }
1072
1073 static gboolean
1074 new_page_ps(print_stream_t *self)
1075 {
1076         output_ps *output = self->data;
1077
1078         fputs("formfeed\n", output->fh);
1079         return !ferror(output->fh);
1080 }
1081
1082 static gboolean
1083 print_finale_ps(print_stream_t *self)
1084 {
1085         output_ps *output = self->data;
1086
1087         print_ps_finale(output->fh);
1088         return !ferror(output->fh);
1089 }
1090
1091 static gboolean
1092 destroy_ps(print_stream_t *self)
1093 {
1094         output_ps *output = self->data;
1095         gboolean ret;
1096
1097         ret = close_print_dest(output->to_file, output->fh);
1098         g_free(output);
1099         g_free(self);
1100         return ret;
1101 }
1102
1103 static const print_stream_ops_t print_ps_ops = {
1104         print_preamble_ps,
1105         print_line_ps,
1106         print_bookmark_ps,
1107         new_page_ps,
1108         print_finale_ps,
1109         destroy_ps
1110 };
1111
1112 print_stream_t *
1113 print_stream_ps_new(int to_file, const char *dest)
1114 {
1115         FILE *fh;
1116         print_stream_t *stream;
1117         output_ps *output;
1118
1119         fh = open_print_dest(to_file, dest);
1120         if (fh == NULL)
1121                 return NULL;
1122
1123         output = g_malloc(sizeof *output);
1124         output->to_file = to_file;
1125         output->fh = fh;
1126         stream = g_malloc(sizeof (print_stream_t));
1127         stream->ops = &print_ps_ops;
1128         stream->data = output;
1129
1130         return stream;
1131 }
1132
1133 print_stream_t *
1134 print_stream_ps_stdio_new(FILE *fh)
1135 {
1136         print_stream_t *stream;
1137         output_ps *output;
1138
1139         output = g_malloc(sizeof *output);
1140         output->to_file = TRUE;
1141         output->fh = fh;
1142         stream = g_malloc(sizeof (print_stream_t));
1143         stream->ops = &print_ps_ops;
1144         stream->data = output;
1145
1146         return stream;
1147 }