Don't hand off the stub body of a Fault PDU to the subdissector for the
[obnox/wireshark/wip.git] / print.c
1 /* print.c
2  * Routines for printing packet analysis trees.
3  *
4  * $Id: print.c,v 1.47 2002/06/04 07:03:47 guy 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 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #include <epan/epan.h>
39 #include <epan/epan_dissect.h>
40 #include <epan/tvbuff.h>
41 #include <epan/packet.h>
42
43 #include "print.h"
44 #include "ps.h"
45 #include "util.h"
46 #include "packet-data.h"
47
48 static void proto_tree_print_node_text(GNode *node, gpointer data);
49 static void proto_tree_print_node_ps(GNode *node, gpointer data);
50 static void ps_clean_string(unsigned char *out, const unsigned char *in,
51                         int outbuf_size);
52 static void print_hex_data_text(FILE *fh, register const u_char *cp,
53                 register u_int length, char_enc encoding);
54 static void print_hex_data_ps(FILE *fh, register const u_char *cp,
55                 register u_int length, char_enc encoding);
56
57 typedef struct {
58         int             level;
59         FILE            *fh;
60         GSList          *src_list;
61         gboolean        print_all_levels;
62         gboolean        print_hex_for_data;
63         char_enc        encoding;
64 } print_data;
65
66 FILE *open_print_dest(int to_file, const char *dest)
67 {
68         FILE    *fh;
69
70         /* Open the file or command for output */
71         if (to_file)
72                 fh = fopen(dest, "w");
73         else
74                 fh = popen(dest, "w");
75
76         return fh;
77 }
78
79 void close_print_dest(int to_file, FILE *fh)
80 {
81         /* Close the file or command */
82         if (to_file)
83                 fclose(fh);
84         else
85                 pclose(fh);
86 }
87
88 void print_preamble(FILE *fh, gint format)
89 {
90         if (format == PR_FMT_PS)
91                 print_ps_preamble(fh);
92 }
93
94 void print_finale(FILE *fh, gint format)
95 {
96         if (format == PR_FMT_PS)
97                 print_ps_finale(fh);
98 }
99
100 void proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
101     FILE *fh)
102 {
103         print_data data;
104
105         /* Create the output */
106         data.level = 0;
107         data.fh = fh;
108         data.src_list = edt->pi.data_src;
109         data.encoding = edt->pi.fd->flags.encoding;
110         data.print_all_levels = print_args->expand_all;
111         data.print_hex_for_data = !print_args->print_hex;
112             /* If we're printing the entire packet in hex, don't
113                print uninterpreted data fields in hex as well. */
114
115         if (print_args->format == PR_FMT_TEXT) {
116                 g_node_children_foreach((GNode*) edt->tree, G_TRAVERSE_ALL,
117                         proto_tree_print_node_text, &data);
118         } else {
119                 g_node_children_foreach((GNode*) edt->tree, G_TRAVERSE_ALL,
120                         proto_tree_print_node_ps, &data);
121         }
122 }
123
124 /*
125  * Find the data source for a specified field, and return a pointer
126  * to the data in it.
127  */
128 static const guint8 *
129 get_field_data(GSList *src_list, field_info *fi)
130 {
131         GSList *src_le;
132         data_source *src;
133         tvbuff_t *src_tvb;
134
135         for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
136                 src = src_le->data;
137                 src_tvb = src->tvb;
138                 if (fi->ds_tvb == src_tvb) {
139                         /*
140                          * Found it.
141                          */
142                         return tvb_get_ptr(src_tvb, fi->start, fi->length);
143                 }
144         }
145         g_assert_not_reached();
146         return NULL;    /* not found */
147 }
148
149 #define MAX_INDENT      160
150
151 /* Print a tree's data, and any child nodes, in plain text */
152 static
153 void proto_tree_print_node_text(GNode *node, gpointer data)
154 {
155         field_info      *fi = PITEM_FINFO(node);
156         print_data      *pdata = (print_data*) data;
157         int             i;
158         int             num_spaces;
159         char            space[MAX_INDENT+1];
160         const guint8    *pd;
161         gchar           label_str[ITEM_LABEL_LENGTH];
162         gchar           *label_ptr;
163
164         /* Don't print invisible entries. */
165         if (!fi->visible)
166                 return;
167
168         /* was a free format label produced? */
169         if (fi->representation) {
170                 label_ptr = fi->representation;
171         }
172         else { /* no, make a generic label */
173                 label_ptr = label_str;
174                 proto_item_fill_label(fi, label_str);
175         }
176                 
177         /* Prepare the tabs for printing, depending on tree level */
178         num_spaces = pdata->level * 4;
179         if (num_spaces > MAX_INDENT) {
180                 num_spaces = MAX_INDENT;
181         }
182         for (i = 0; i < num_spaces; i++) {
183                 space[i] = ' ';
184         }
185         /* The string is NUL-terminated */
186         space[num_spaces] = '\0';
187
188         /* Print the text */
189         fprintf(pdata->fh, "%s%s\n", space, label_ptr);
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                 print_hex_data_text(pdata->fh, pd, fi->length, pdata->encoding);
199         }
200
201         /* If we're printing all levels, or if this node is one with a
202            subtree and its subtree is expanded, recurse into the subtree,
203            if it exists. */
204         g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
205         if (pdata->print_all_levels ||
206             (fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
207                 if (g_node_n_children(node) > 0) {
208                         pdata->level++;
209                         g_node_children_foreach(node, G_TRAVERSE_ALL,
210                                 proto_tree_print_node_text, pdata);
211                         pdata->level--;
212                 }
213         }
214 }
215
216 void print_hex_data(FILE *fh, gint format, epan_dissect_t *edt)
217 {
218         gboolean multiple_sources;
219         GSList *src_le;
220         data_source *src;
221         tvbuff_t *tvb;
222         char *name;
223         char *line;
224         const u_char *cp;
225         guint length;
226
227         /*
228          * Set "multiple_sources" iff this frame has more than one
229          * data source; if it does, we need to print the name of
230          * the data source before printing the data from the
231          * data source.
232          */
233         multiple_sources = (edt->pi.data_src->next != NULL);
234
235         for (src_le = edt->pi.data_src; src_le != NULL;
236             src_le = src_le->next) {
237                 src = src_le->data;
238                 tvb = src->tvb;
239                 if (multiple_sources) {
240                         name = src->name;
241                         print_line(fh, format, "\n");
242                         line = g_malloc(strlen(name) + 3);      /* <name>:\n\0 */
243                         strcpy(line, name);
244                         strcat(line, ":\n");
245                         print_line(fh, format, line);
246                         g_free(line);
247                 }
248                 length = tvb_length(tvb);
249                 cp = tvb_get_ptr(tvb, 0, length);
250                 if (format == PR_FMT_PS) {
251                         print_hex_data_ps(fh, cp, length,
252                             edt->pi.fd->flags.encoding);
253                 } else {
254                         print_hex_data_text(fh, cp, length,
255                             edt->pi.fd->flags.encoding);
256                 }
257         }
258 }
259
260 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
261 only slightly modified for ethereal by Gilbert Ramirez. */
262 static
263 void print_hex_data_text(FILE *fh, register const u_char *cp,
264                 register u_int length, char_enc encoding)
265 {
266         register unsigned int ad, i, j, k;
267         u_char c;
268         u_char line[50+16+1];
269         static u_char binhex[16] = {
270                 '0', '1', '2', '3', '4', '5', '6', '7',
271                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
272
273         memset (line, ' ', sizeof line);
274         line[sizeof (line)-1] = 0;
275         for (ad=i=j=k=0; i<length; i++) {
276                 c = *cp++;
277                 line[j++] = binhex[c>>4];
278                 line[j++] = binhex[c&0xf];
279                 j++;
280                 if (encoding == CHAR_EBCDIC) {
281                         c = EBCDIC_to_ASCII1(c);
282                 }
283                 line[50+k++] = c >= ' ' && c < 0x7f ? c : '.';
284                 if ((i & 15) == 15) {
285                         fprintf (fh, "\n%04x  %s", ad, line);
286                         /*if (i==15) printf (" %d", length);*/
287                         memset (line, ' ', sizeof line);
288                         line[sizeof (line)-1] = j = k = 0;
289                         ad += 16;
290                 }
291         }
292
293         if (line[0] != ' ') fprintf (fh, "\n%04x  %s", ad, line);
294         fprintf(fh, "\n");
295         return;
296
297 }
298
299 #define MAX_LINE_LENGTH 256
300
301 /* Print a node's data, and any child nodes, in PostScript */
302 static
303 void proto_tree_print_node_ps(GNode *node, gpointer data)
304 {
305         field_info      *fi = PITEM_FINFO(node);
306         print_data      *pdata = (print_data*) data;
307         gchar           label_str[ITEM_LABEL_LENGTH];
308         gchar           *label_ptr;
309         const guint8    *pd;
310         char            psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
311
312         if (!fi->visible)
313                 return;
314
315         /* was a free format label produced? */
316         if (fi->representation) {
317                 label_ptr = fi->representation;
318         }
319         else { /* no, make a generic label */
320                 label_ptr = label_str;
321                 proto_item_fill_label(fi, label_str);
322         }
323                 
324         /* Print the text */
325         ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
326         fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
327
328         /* If it's uninterpreted data, dump it (unless our caller will
329            be printing the entire packet in hex). */
330         if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
331                 /*
332                  * Find the data for this field.
333                  */
334                 pd = get_field_data(pdata->src_list, fi);
335                 print_hex_data_ps(pdata->fh, pd, fi->length, pdata->encoding);
336         }
337
338         /* Recurse into the subtree, if it exists */
339         if (g_node_n_children(node) > 0) {
340                 pdata->level++;
341                 g_node_children_foreach(node, G_TRAVERSE_ALL,
342                         proto_tree_print_node_ps, pdata);
343                 pdata->level--;
344         }
345 }
346
347 static
348 void ps_clean_string(unsigned char *out, const unsigned char *in,
349                         int outbuf_size)
350 {
351         int rd, wr;
352         char c;
353
354         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
355                 c = in[rd];
356                 switch (c) {
357                         case '(':
358                         case ')':
359                         case '\\':
360                                 out[wr] = '\\';
361                                 out[++wr] = c;
362                                 break;
363
364                         default:
365                                 out[wr] = c;
366                                 break;
367                 }
368
369                 if (c == 0) {
370                         break;
371                 }
372         }
373 }
374
375 static
376 void print_hex_data_ps(FILE *fh, register const u_char *cp,
377                 register u_int length, char_enc encoding)
378 {
379         register unsigned int ad, i, j, k;
380         u_char c;
381         u_char line[60];
382         static u_char binhex[16] = {
383                 '0', '1', '2', '3', '4', '5', '6', '7',
384                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
385         u_char psline[MAX_LINE_LENGTH];
386
387         print_ps_hex(fh);
388         memset (line, ' ', sizeof line);
389         line[sizeof (line)-1] = 0;
390         for (ad=i=j=k=0; i<length; i++) {
391                 c = *cp++;
392                 line[j++] = binhex[c>>4];
393                 line[j++] = binhex[c&0xf];
394                 if (i&1) j++;
395                 if (encoding == CHAR_EBCDIC) {
396                         c = EBCDIC_to_ASCII1(c);
397                 }
398                 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
399                 if ((i & 15) == 15) {
400                         ps_clean_string(psline, line, MAX_LINE_LENGTH);
401                         fprintf (fh, "(%4x  %s) hexdump\n", ad, psline);
402                         memset (line, ' ', sizeof line);
403                         line[sizeof (line)-1] = j = k = 0;
404                         ad += 16;
405                 }
406         }
407
408         if (line[0] != ' ') {
409                 ps_clean_string(psline, line, MAX_LINE_LENGTH);
410                 fprintf (fh, "(%4x  %s) hexdump\n", ad, psline);
411         }
412         return;
413
414 }
415
416 void print_line(FILE *fh, gint format, char *line)
417 {
418         char            psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
419
420         if (format == PR_FMT_PS) {
421                 ps_clean_string(psbuffer, line, MAX_LINE_LENGTH);
422                 fprintf(fh, "(%s) hexdump\n", psbuffer);
423         } else
424                 fputs(line, fh);
425 }