The function pointer in a "per_choice_t" or a "per_sequence_t" is to a
[obnox/wireshark/wip.git] / print.c
1 /* print.c
2  * Routines for printing packet analysis trees.
3  *
4  * $Id: print.c,v 1.82 2004/05/01 21:04:27 obiot Exp $
5  *
6  * Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
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 "range.h"
40 #include "print.h"
41 #include "ps.h"
42 #include "util.h"
43 #include "packet-data.h"
44 #include "packet-frame.h"
45
46 #define PDML_VERSION "0"
47 #define PSML_VERSION "0"
48
49 static void proto_tree_print_node(proto_node *node, gpointer data);
50 static void proto_tree_print_node_pdml(proto_node *node, gpointer data);
51 static void print_hex_data_buffer(FILE *fh, register const guchar *cp,
52     register guint length, char_enc encoding, print_format_e format);
53 static void ps_clean_string(unsigned char *out, const unsigned char *in,
54                         int outbuf_size);
55 static void print_escaped_xml(FILE *fh, char *unescaped_string);
56
57 typedef struct {
58         int                         level;
59         FILE                    *fh;
60         GSList                  *src_list;
61         print_dissections_e print_dissections;
62         gboolean                print_hex_for_data;
63         char_enc                encoding;
64         gint                    format;
65         epan_dissect_t      *edt;
66 } print_data;
67
68 static void print_pdml_geninfo(proto_tree *tree, print_data *pdata);
69
70 FILE *open_print_dest(int to_file, const char *dest)
71 {
72         FILE    *fh;
73
74         /* Open the file or command for output */
75         if (to_file)
76                 fh = fopen(dest, "w");
77         else
78                 fh = popen(dest, "w");
79
80         return fh;
81 }
82
83 gboolean close_print_dest(int to_file, FILE *fh)
84 {
85         /* Close the file or command */
86         if (to_file)
87                 return (fclose(fh) == 0);
88         else
89                 return (pclose(fh) == 0);
90 }
91
92
93 void
94 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
95     FILE *fh)
96 {
97         print_data data;
98         gint    i;
99
100         /* Create the output */
101         data.level = 0;
102         data.fh = fh;
103         data.src_list = edt->pi.data_src;
104         data.encoding = edt->pi.fd->flags.encoding;
105         data.print_dissections = print_args->print_dissections;
106         data.print_hex_for_data = !print_args->print_hex;
107             /* If we're printing the entire packet in hex, don't
108                print uninterpreted data fields in hex as well. */
109         data.format = print_args->format;
110         data.edt = edt;
111
112     switch(data.format) {
113     case(PR_FMT_TEXT):  /* fall through */
114     case(PR_FMT_PS):
115                 proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
116         break;
117     case(PR_FMT_PDML):
118                 fprintf(fh, "<packet>\n");
119
120                 /* Print a "geninfo" protocol as required by PDML */
121                 print_pdml_geninfo(edt->tree, &data);
122
123                 proto_tree_children_foreach(edt->tree, proto_tree_print_node_pdml, &data);
124
125                 fprintf(fh, "</packet>\n\n");
126         break;
127     case(PR_FMT_PSML):
128         /* if this is the first packet, we have to create the PSML structure output */
129         if(edt->pi.fd->num == 1) {
130             fprintf(fh, "<structure>\n");
131
132             for(i=0; i < edt->pi.cinfo->num_cols; i++) {
133                 fprintf(fh, "<section>");
134                 print_escaped_xml(fh, edt->pi.cinfo->col_title[i]);
135                 fprintf(fh, "</section>\n");
136             }
137
138             fprintf(fh, "</structure>\n\n");
139         }
140
141                 fprintf(fh, "<packet>\n");
142
143         for(i=0; i < edt->pi.cinfo->num_cols; i++) {
144             fprintf(fh, "<section>");
145             print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
146             fprintf(fh, "</section>\n");
147         }
148
149                 fprintf(fh, "</packet>\n\n");
150         break;
151     default:
152         g_assert_not_reached();
153         }
154 }
155
156 /*
157  * Find the data source for a specified field, and return a pointer
158  * to the data in it. Returns NULL if the data is out of bounds.
159  */
160 static const guint8 *
161 get_field_data(GSList *src_list, field_info *fi)
162 {
163         GSList *src_le;
164         data_source *src;
165         tvbuff_t *src_tvb;
166         gint length, tvbuff_length;
167
168         for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
169                 src = src_le->data;
170                 src_tvb = src->tvb;
171                 if (fi->ds_tvb == src_tvb) {
172                         /*
173                          * Found it.
174                          *
175                          * XXX - a field can have a length that runs past
176                          * the end of the tvbuff.  Ideally, that should
177                          * be fixed when adding an item to the protocol
178                          * tree, but checking the length when doing
179                          * that could be expensive.  Until we fix that,
180                          * we'll do the check here.
181                          */
182                         tvbuff_length = tvb_length_remaining(src_tvb,
183                             fi->start);
184                         if (tvbuff_length < 0) {
185                                 return NULL;
186                         }
187                         length = fi->length;
188                         if (length > tvbuff_length)
189                                 length = tvbuff_length;
190                         return tvb_get_ptr(src_tvb, fi->start, length);
191                 }
192         }
193         g_assert_not_reached();
194         return NULL;    /* not found */
195 }
196
197 #define MAX_INDENT      160
198
199 #define MAX_PS_LINE_LENGTH 256
200
201 /* Print a tree's data, and any child nodes. */
202 static
203 void proto_tree_print_node(proto_node *node, gpointer data)
204 {
205         field_info      *fi = PITEM_FINFO(node);
206         print_data      *pdata = (print_data*) data;
207         const guint8    *pd;
208         gchar           label_str[ITEM_LABEL_LENGTH];
209         gchar           *label_ptr;
210
211         /* Don't print invisible entries. */
212         if (PROTO_ITEM_IS_HIDDEN(node))
213                 return;
214
215     /* was a free format label produced? */
216         if (fi->rep) {
217                 label_ptr = fi->rep->representation;
218         }
219         else { /* no, make a generic label */
220                 label_ptr = label_str;
221                 proto_item_fill_label(fi, label_str);
222         }
223
224         print_line(pdata->fh, pdata->level, pdata->format, label_ptr);
225
226         /* If it's uninterpreted data, dump it (unless our caller will
227            be printing the entire packet in hex). */
228         if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
229                 /*
230                  * Find the data for this field.
231                  */
232                 pd = get_field_data(pdata->src_list, fi);
233                 if (pd) {
234                         print_hex_data_buffer(pdata->fh, pd, fi->length,
235                             pdata->encoding, pdata->format);
236                 }
237         }
238
239         /* If we're printing all levels, or if this node is one with a
240            subtree and its subtree is expanded, recurse into the subtree,
241            if it exists. */
242         g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
243         if (pdata->print_dissections == print_dissections_expanded ||
244             (pdata->print_dissections == print_dissections_as_displayed &&
245         fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
246                 if (node->first_child != NULL) {
247                         pdata->level++;
248                         proto_tree_children_foreach(node,
249                                 proto_tree_print_node, pdata);
250                         pdata->level--;
251                 }
252         }
253 }
254
255 /* Print a string, escaping out certain characters that need to
256  * escaped out for XML. */
257 static void
258 print_escaped_xml(FILE *fh, char *unescaped_string)
259 {
260         unsigned char *p;
261
262         for (p = unescaped_string; *p != '\0'; p++) {
263                 switch (*p) {
264                         case '&':
265                                 fputs("&amp;", fh);
266                                 break;
267                         case '<':
268                                 fputs("&lt;", fh);
269                                 break;
270                         case '>':
271                                 fputs("&gt;", fh);
272                                 break;
273                         case '"':
274                                 fputs("&quot;", fh);
275                                 break;
276                         case '\'':
277                                 fputs("&apos;", fh);
278                                 break;
279                         default:
280                                 fputc(*p, fh);
281                 }
282         }
283 }
284
285 static void
286 print_field_hex_value(print_data *pdata, field_info *fi)
287 {
288         int i;
289         const guint8 *pd;
290
291         /* Find the data for this field. */
292         pd = get_field_data(pdata->src_list, fi);
293
294         if (pd) {
295                 /* Print a simple hex dump */
296                 for (i = 0 ; i < fi->length; i++) {
297                         fprintf(pdata->fh, "%02x", pd[i]);
298                 }
299         }
300 }
301
302
303 /* Print a tree's data, and any child nodes, as PDML */
304 static void
305 proto_tree_print_node_pdml(proto_node *node, gpointer data)
306 {
307         field_info      *fi = PITEM_FINFO(node);
308         print_data      *pdata = (print_data*) data;
309         gchar           *label_ptr;
310         gchar           label_str[ITEM_LABEL_LENGTH];
311         char            *dfilter_string;
312         int             chop_len;
313         int             i;
314
315         for (i = -1; i < pdata->level; i++) {
316                 fputs("  ", pdata->fh);
317         }
318
319         /* Text label. It's printed as a field with no name. */
320         if (fi->hfinfo->id == hf_text_only) {
321                 /* Get the text */
322                 if (fi->rep) {
323                         label_ptr = fi->rep->representation;
324                 }
325                 else {
326                         label_ptr = "";
327                 }
328
329                 fputs("<field show=\"", pdata->fh);
330                 print_escaped_xml(pdata->fh, label_ptr);
331
332                 fprintf(pdata->fh, "\" size=\"%d", fi->length);
333                 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
334
335                 fputs("\" value=\"", pdata->fh);
336                 print_field_hex_value(pdata, fi);
337
338                 if (node->first_child != NULL) {
339                         fputs("\">\n", pdata->fh);
340                 }
341                 else {
342                         fputs("\"/>\n", pdata->fh);
343                 }
344         }
345         /* Uninterpreted data, i.e., the "Data" protocol, is
346          * printed as a field instead of a protocol. */
347         else if (fi->hfinfo->id == proto_data) {
348
349                 fputs("<field name=\"data\" value=\"", pdata->fh);
350
351                 print_field_hex_value(pdata, fi);
352
353                 fputs("\"/>\n", pdata->fh);
354
355         }
356         /* Normal protocols and fields */
357         else {
358                 if (fi->hfinfo->type == FT_PROTOCOL) {
359                         fputs("<proto name=\"", pdata->fh);
360                 }
361                 else {
362                         fputs("<field name=\"", pdata->fh);
363                 }
364                 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
365
366 #if 0
367         /* PDML spec, see: 
368          * http://analyzer.polito.it/30alpha/docs/dissectors/PDMLSpec.htm
369          *
370          * the show fields contains things in 'human readable' format
371          * showname: contains only the name of the field
372          * show: contains only the data of the field
373          * showdtl: contains additional details of the field data
374          * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
375          *
376          * XXX - the showname shouldn't contain the field data itself 
377          * (like it's contained in the fi->rep->representation). 
378          * Unfortunately, we don't have the field data representation for 
379          * all fields, so this isn't currently possible */
380                 fputs("\" showname=\"", pdata->fh);
381                 print_escaped_xml(pdata->fh, fi->hfinfo->name);
382 #endif
383
384                 if (fi->rep) {
385                         fputs("\" showname=\"", pdata->fh);
386                         print_escaped_xml(pdata->fh, fi->rep->representation);
387                 }
388                 else {
389                         label_ptr = label_str;
390                         proto_item_fill_label(fi, label_str);
391                         fputs("\" showname=\"", pdata->fh);
392                         print_escaped_xml(pdata->fh, label_ptr);
393                 }
394
395         if(PROTO_ITEM_IS_HIDDEN(node)) {
396                 fprintf(pdata->fh, "\" hide=\"yes");
397         }
398
399                 fprintf(pdata->fh, "\" size=\"%d", fi->length);
400                 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
401 /*              fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
402
403                 if (fi->hfinfo->type != FT_PROTOCOL) {
404                         /* Field */
405
406                         /* XXX - this is a hack until we can just call
407                          * fvalue_to_string_repr() for *all* FT_* types. */
408                         dfilter_string = proto_construct_dfilter_string(fi,
409                                         pdata->edt);
410                         if (dfilter_string != NULL) {
411                                 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
412
413                                 /* XXX - Remove double-quotes. Again, once we
414                                  * can call fvalue_to_string_repr(), we can
415                                  * ask it not to produce the version for
416                                  * display-filters, and thus, no
417                                  * double-quotes. */
418                                 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
419                                         dfilter_string[strlen(dfilter_string)-1] = '\0';
420                                         chop_len++;
421                                 }
422
423                                 fputs("\" show=\"", pdata->fh);
424                                 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
425                         }
426                         if (fi->length > 0) {
427                                 fputs("\" value=\"", pdata->fh);
428                                 print_field_hex_value(pdata, fi);
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_print_node_pdml, pdata);
448                 pdata->level--;
449         }
450
451         if (node->first_child != NULL) {
452                 for (i = -1; i < pdata->level; i++) {
453                         fputs("  ", pdata->fh);
454                 }
455                 if (fi->hfinfo->type == FT_PROTOCOL) {
456                         fputs("</proto>\n", pdata->fh);
457                 }
458                 else {
459                         fputs("</field>\n", pdata->fh);
460                 }
461         }
462 }
463
464 /* Print info for a 'geninfo' pseudo-protocol. This is required by
465  * the PDML spec. The information is contained in Ethereal's 'frame' protocol,
466  * but we produce a 'geninfo' protocol in the PDML to conform to spec.
467  * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
468 static void
469 print_pdml_geninfo(proto_tree *tree, print_data *pdata)
470 {
471         guint32 num, len, caplen;
472         nstime_t *timestamp;
473         GPtrArray *finfo_array;
474         field_info *frame_finfo;
475
476         /* Get frame protocol's finfo. */
477         finfo_array = proto_find_finfo(tree, proto_frame);
478         if (g_ptr_array_len(finfo_array) < 1) {
479                 return;
480         }
481         frame_finfo = finfo_array->pdata[0];
482         g_ptr_array_free(finfo_array, FALSE);
483
484         /* frame.number --> geninfo.num */
485         finfo_array = proto_find_finfo(tree, hf_frame_number);
486         if (g_ptr_array_len(finfo_array) < 1) {
487                 return;
488         }
489         num = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
490         g_ptr_array_free(finfo_array, FALSE);
491
492         /* frame.pkt_len --> geninfo.len */
493         finfo_array = proto_find_finfo(tree, hf_frame_packet_len);
494         if (g_ptr_array_len(finfo_array) < 1) {
495                 return;
496         }
497         len = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
498         g_ptr_array_free(finfo_array, FALSE);
499
500         /* frame.cap_len --> geninfo.caplen */
501         finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
502         if (g_ptr_array_len(finfo_array) < 1) {
503                 return;
504         }
505         caplen = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
506         g_ptr_array_free(finfo_array, FALSE);
507
508         /* frame.time --> geninfo.timestamp */
509         finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
510         if (g_ptr_array_len(finfo_array) < 1) {
511                 return;
512         }
513         timestamp = fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
514         g_ptr_array_free(finfo_array, FALSE);
515
516         /* Print geninfo start */
517         fprintf(pdata->fh,
518 "  <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
519                 frame_finfo->length);
520
521         /* Print geninfo.num */
522         fprintf(pdata->fh,
523 "    <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
524                 num, num, frame_finfo->length);
525
526         /* Print geninfo.len */
527         fprintf(pdata->fh,
528 "    <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Packet Length\" value=\"%x\" size=\"%u\"/>\n",
529                 len, len, frame_finfo->length);
530
531         /* Print geninfo.caplen */
532         fprintf(pdata->fh,
533 "    <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
534                 caplen, caplen, frame_finfo->length);
535
536         /* Print geninfo.timestamp */
537         fprintf(pdata->fh,
538 "    <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
539                 abs_time_to_str(timestamp), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
540
541         /* Print geninfo end */
542         fprintf(pdata->fh,
543 "  </proto>\n");
544 }
545
546
547 void print_hex_data(FILE *fh, print_format_e format, epan_dissect_t *edt)
548 {
549         gboolean multiple_sources;
550         GSList *src_le;
551         data_source *src;
552         tvbuff_t *tvb;
553         char *name;
554         char *line;
555         const guchar *cp;
556         guint length;
557
558         /*
559          * Set "multiple_sources" iff this frame has more than one
560          * data source; if it does, we need to print the name of
561          * the data source before printing the data from the
562          * data source.
563          */
564         multiple_sources = (edt->pi.data_src->next != NULL);
565
566         for (src_le = edt->pi.data_src; src_le != NULL;
567             src_le = src_le->next) {
568                 src = src_le->data;
569                 tvb = src->tvb;
570                 if (multiple_sources) {
571                         name = src->name;
572                         print_line(fh, 0, format, "");
573                         line = g_malloc(strlen(name) + 2);      /* <name>:\0 */
574                         strcpy(line, name);
575                         strcat(line, ":");
576                         print_line(fh, 0, format, line);
577                         g_free(line);
578                 }
579                 length = tvb_length(tvb);
580                 cp = tvb_get_ptr(tvb, 0, length);
581                 print_hex_data_buffer(fh, cp, length,
582                     edt->pi.fd->flags.encoding, format);
583         }
584 }
585
586 /*
587  * This routine is based on a routine created by Dan Lasley
588  * <DLASLEY@PROMUS.com>.
589  *
590  * It was modified for Ethereal by Gilbert Ramirez and others.
591  */
592
593 #define MAX_OFFSET_LEN  8       /* max length of hex offset of bytes */
594 #define BYTES_PER_LINE  16      /* max byte values printed on a line */
595 #define HEX_DUMP_LEN    (BYTES_PER_LINE*3)
596                                 /* max number of characters hex dump takes -
597                                    2 digits plus trailing blank */
598 #define DATA_DUMP_LEN   (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
599                                 /* number of characters those bytes take;
600                                    3 characters per byte of hex dump,
601                                    2 blanks separating hex from ASCII,
602                                    1 character per byte of ASCII dump */
603 #define MAX_LINE_LEN    (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
604                                 /* number of characters per line;
605                                    offset, 2 blanks separating offset
606                                    from data dump, data dump */
607
608 static void
609 print_hex_data_buffer(FILE *fh, register const guchar *cp,
610     register guint length, char_enc encoding, print_format_e format)
611 {
612         register unsigned int ad, i, j, k, l;
613         guchar c;
614         guchar line[MAX_LINE_LEN + 1];
615         unsigned int use_digits;
616         static guchar binhex[16] = {
617                 '0', '1', '2', '3', '4', '5', '6', '7',
618                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
619
620         print_line(fh, 0, format, "");
621
622         /*
623          * How many of the leading digits of the offset will we supply?
624          * We always supply at least 4 digits, but if the maximum offset
625          * won't fit in 4 digits, we use as many digits as will be needed.
626          */
627         if (((length - 1) & 0xF0000000) != 0)
628                 use_digits = 8; /* need all 8 digits */
629         else if (((length - 1) & 0x0F000000) != 0)
630                 use_digits = 7; /* need 7 digits */
631         else if (((length - 1) & 0x00F00000) != 0)
632                 use_digits = 6; /* need 6 digits */
633         else if (((length - 1) & 0x000F0000) != 0)
634                 use_digits = 5; /* need 5 digits */
635         else
636                 use_digits = 4; /* we'll supply 4 digits */
637
638         ad = 0;
639         i = 0;
640         j = 0;
641         k = 0;
642         while (i < length) {
643                 if ((i & 15) == 0) {
644                         /*
645                          * Start of a new line.
646                          */
647                         j = 0;
648                         k = 0;
649                         l = use_digits;
650                         do {
651                                 l--;
652                                 c = (ad >> (l*4)) & 0xF;
653                                 line[j++] = binhex[c];
654                         } while (l != 0);
655                         line[j++] = ' ';
656                         line[j++] = ' ';
657                         memset(line+j, ' ', DATA_DUMP_LEN);
658
659                         /*
660                          * Offset in line of ASCII dump.
661                          */
662                         k = j + HEX_DUMP_LEN + 2;
663                 }
664                 c = *cp++;
665                 line[j++] = binhex[c>>4];
666                 line[j++] = binhex[c&0xf];
667                 j++;
668                 if (encoding == CHAR_EBCDIC) {
669                         c = EBCDIC_to_ASCII1(c);
670                 }
671                 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
672                 i++;
673                 if ((i & 15) == 0 || i == length) {
674                         /*
675                          * We'll be starting a new line, or
676                          * we're finished printing this buffer;
677                          * dump out the line we've constructed,
678                          * and advance the offset.
679                          */
680                         line[k] = '\0';
681                         print_line(fh, 0, format, line);
682                         ad += 16;
683                 }
684         }
685 }
686
687 static
688 void ps_clean_string(unsigned char *out, const unsigned char *in,
689                         int outbuf_size)
690 {
691         int rd, wr;
692         char c;
693
694         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
695                 c = in[rd];
696                 switch (c) {
697                         case '(':
698                         case ')':
699                         case '\\':
700                                 out[wr] = '\\';
701                                 out[++wr] = c;
702                                 break;
703
704                         default:
705                                 out[wr] = c;
706                                 break;
707                 }
708
709                 if (c == 0) {
710                         break;
711                 }
712         }
713 }
714
715 /* Some formats need stuff at the beginning of the output */
716 void
717 print_preamble(FILE *fh, print_format_e format, gchar *filename)
718 {
719         char            psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
720
721     
722     switch(format) {
723     case(PR_FMT_TEXT):
724         /* do nothing */
725         break;
726     case(PR_FMT_PS):
727                 print_ps_preamble(fh);
728
729                 fputs("%% Set the font to 10 point\n", fh);
730                 fputs("/Courier findfont 10 scalefont setfont\n", fh);
731                 fputs("\n", fh);
732                 fputs("%% the page title\n", fh);
733                 ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
734                 fprintf(fh, "/eth_pagetitle (%s - Ethereal) def\n", psbuffer);
735                 fputs("\n", fh);
736         break;
737     case(PR_FMT_PDML):
738                 fputs("<?xml version=\"1.0\"?>\n", fh);
739                 fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
740                 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
741         break;
742     case(PR_FMT_PSML):
743                 fputs("<?xml version=\"1.0\"?>\n", fh);
744                 fputs("<psml version=\"" PSML_VERSION "\" ", fh);
745                 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
746         break;
747     default:
748                 g_assert_not_reached();
749         }
750 }
751
752 void
753 print_packet_header(FILE *fh, print_format_e format, guint32 number, gchar *summary) {
754         char            psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
755
756     
757     switch(format) {
758     case(PR_FMT_TEXT):
759         /* do nothing */
760         break;
761     case(PR_FMT_PS):
762                 ps_clean_string(psbuffer, summary, MAX_PS_LINE_LENGTH);
763         fprintf(fh, "[/Dest /__frame%u__ /Title (%s)   /OUT pdfmark\n", number, psbuffer);
764         fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n", fh);
765         fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n", fh);
766         fprintf(fh, "/Dest /__frame%u__ /DEST pdfmark\n", number);
767         break;
768     case(PR_FMT_PDML):
769         /* do nothing */
770         break;
771     case(PR_FMT_PSML):
772         /* do nothing */
773         break;
774     default:
775                 g_assert_not_reached();
776         }
777 }
778
779 void
780 print_formfeed(FILE *fh, print_format_e format) {
781     switch(format) {
782     case(PR_FMT_TEXT):
783                 fputs("\f", fh);
784         break;
785     case(PR_FMT_PS):
786                 fputs("formfeed\n", fh);
787         break;
788     case(PR_FMT_PDML):
789         /* do nothing */
790         break;
791     case(PR_FMT_PSML):
792         /* do nothing */
793         break;
794     default:
795                 g_assert_not_reached();
796         }
797 }
798
799 /* Some formats need stuff at the end of the output */
800 void
801 print_finale(FILE *fh, print_format_e format)
802 {
803     switch(format) {
804     case(PR_FMT_TEXT):
805         /* do nothing */
806         break;
807     case(PR_FMT_PS):
808                 print_ps_finale(fh);
809         break;
810     case(PR_FMT_PDML):
811                 fputs("</pdml>\n", fh);
812         break;
813     case(PR_FMT_PSML):
814                 fputs("</psml>\n", fh);
815         break;
816     default:
817                 g_assert_not_reached();
818         }
819 }
820
821 void
822 print_line(FILE *fh, int indent, print_format_e format, char *line)
823 {
824         char            space[MAX_INDENT+1];
825         int             i;
826         int             num_spaces;
827         char            psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
828
829     switch(format) {
830     case(PR_FMT_TEXT):
831                 /* Prepare the tabs for printing, depending on tree level */
832                 num_spaces = indent * 4;
833                 if (num_spaces > MAX_INDENT) {
834                         num_spaces = MAX_INDENT;
835                 }
836                 for (i = 0; i < num_spaces; i++) {
837                         space[i] = ' ';
838                 }
839                 /* The string is NUL-terminated */
840                 space[num_spaces] = '\0';
841
842                 fputs(space, fh);
843                 fputs(line, fh);
844                 putc('\n', fh);
845         break;
846     case(PR_FMT_PS):
847                 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
848                 fprintf(fh, "%d (%s) putline\n", indent, psbuffer);
849         break;
850     case(PR_FMT_PDML):
851         /* do nothing */
852         break;
853     case(PR_FMT_PSML):
854         /* do nothing */
855         break;
856     default:
857                 g_assert_not_reached();
858     }
859 }