]> git.samba.org - obnox/wireshark/wip.git/blob - print.c
Use a common routine to print protocol tree nodes as text or PostScript,
[obnox/wireshark/wip.git] / print.c
1 /* print.c
2  * Routines for printing packet analysis trees.
3  *
4  * $Id: print.c,v 1.52 2002/06/22 01:43:57 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(GNode *node, gpointer data);
49 static void print_hex_data_buffer(FILE *fh, register const u_char *cp,
50     register u_int length, char_enc encoding, gint format);
51 static void ps_clean_string(unsigned char *out, const unsigned char *in,
52                         int outbuf_size);
53
54 typedef struct {
55         int             level;
56         FILE            *fh;
57         GSList          *src_list;
58         gboolean        print_all_levels;
59         gboolean        print_hex_for_data;
60         char_enc        encoding;
61         gint            format;         /* text or PostScript */
62 } print_data;
63
64 FILE *open_print_dest(int to_file, const char *dest)
65 {
66         FILE    *fh;
67
68         /* Open the file or command for output */
69         if (to_file)
70                 fh = fopen(dest, "w");
71         else
72                 fh = popen(dest, "w");
73
74         return fh;
75 }
76
77 void close_print_dest(int to_file, FILE *fh)
78 {
79         /* Close the file or command */
80         if (to_file)
81                 fclose(fh);
82         else
83                 pclose(fh);
84 }
85
86 void print_preamble(FILE *fh, gint format)
87 {
88         if (format == PR_FMT_PS)
89                 print_ps_preamble(fh);
90 }
91
92 void print_finale(FILE *fh, gint format)
93 {
94         if (format == PR_FMT_PS)
95                 print_ps_finale(fh);
96 }
97
98 void proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
99     FILE *fh)
100 {
101         print_data data;
102
103         /* Create the output */
104         data.level = 0;
105         data.fh = fh;
106         data.src_list = edt->pi.data_src;
107         data.encoding = edt->pi.fd->flags.encoding;
108         data.print_all_levels = print_args->expand_all;
109         data.print_hex_for_data = !print_args->print_hex;
110             /* If we're printing the entire packet in hex, don't
111                print uninterpreted data fields in hex as well. */
112         data.format = print_args->format;
113
114         g_node_children_foreach((GNode*) edt->tree, G_TRAVERSE_ALL,
115                 proto_tree_print_node, &data);
116 }
117
118 /*
119  * Find the data source for a specified field, and return a pointer
120  * to the data in it.
121  */
122 static const guint8 *
123 get_field_data(GSList *src_list, field_info *fi)
124 {
125         GSList *src_le;
126         data_source *src;
127         tvbuff_t *src_tvb;
128
129         for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
130                 src = src_le->data;
131                 src_tvb = src->tvb;
132                 if (fi->ds_tvb == src_tvb) {
133                         /*
134                          * Found it.
135                          */
136                         return tvb_get_ptr(src_tvb, fi->start, fi->length);
137                 }
138         }
139         g_assert_not_reached();
140         return NULL;    /* not found */
141 }
142
143 #define MAX_INDENT      160
144
145 #define MAX_PS_LINE_LENGTH 256
146
147 /* Print a tree's data, and any child nodes. */
148 static
149 void proto_tree_print_node(GNode *node, gpointer data)
150 {
151         field_info      *fi = PITEM_FINFO(node);
152         print_data      *pdata = (print_data*) data;
153         int             i;
154         int             num_spaces;
155         char            space[MAX_INDENT+1];
156         const guint8    *pd;
157         gchar           label_str[ITEM_LABEL_LENGTH];
158         gchar           *label_ptr;
159         char            psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
160
161         /* Don't print invisible entries. */
162         if (!fi->visible)
163                 return;
164
165         /* was a free format label produced? */
166         if (fi->representation) {
167                 label_ptr = fi->representation;
168         }
169         else { /* no, make a generic label */
170                 label_ptr = label_str;
171                 proto_item_fill_label(fi, label_str);
172         }
173                 
174         if (pdata->format == PR_FMT_PS) {
175                 /* Print the text, as PostScript */
176                 ps_clean_string(psbuffer, label_ptr, MAX_PS_LINE_LENGTH);
177                 fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
178         } else {
179                 /* Prepare the tabs for printing, depending on tree level */
180                 num_spaces = pdata->level * 4;
181                 if (num_spaces > MAX_INDENT) {
182                         num_spaces = MAX_INDENT;
183                 }
184                 for (i = 0; i < num_spaces; i++) {
185                         space[i] = ' ';
186                 }
187                 /* The string is NUL-terminated */
188                 space[num_spaces] = '\0';
189
190                 /* Print the text */
191                 fprintf(pdata->fh, "%s%s\n", space, label_ptr);
192         }
193
194         /* If it's uninterpreted data, dump it (unless our caller will
195            be printing the entire packet in hex). */
196         if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
197                 /*
198                  * Find the data for this field.
199                  */
200                 pd = get_field_data(pdata->src_list, fi);
201                 print_hex_data_buffer(pdata->fh, pd, fi->length,
202                     pdata->encoding, pdata->format);
203         }
204
205         /* If we're printing all levels, or if this node is one with a
206            subtree and its subtree is expanded, recurse into the subtree,
207            if it exists. */
208         g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
209         if (pdata->print_all_levels ||
210             (fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
211                 if (g_node_n_children(node) > 0) {
212                         pdata->level++;
213                         g_node_children_foreach(node, G_TRAVERSE_ALL,
214                                 proto_tree_print_node, pdata);
215                         pdata->level--;
216                 }
217         }
218 }
219
220 void print_hex_data(FILE *fh, gint format, epan_dissect_t *edt)
221 {
222         gboolean multiple_sources;
223         GSList *src_le;
224         data_source *src;
225         tvbuff_t *tvb;
226         char *name;
227         char *line;
228         const u_char *cp;
229         guint length;
230
231         /*
232          * Set "multiple_sources" iff this frame has more than one
233          * data source; if it does, we need to print the name of
234          * the data source before printing the data from the
235          * data source.
236          */
237         multiple_sources = (edt->pi.data_src->next != NULL);
238
239         for (src_le = edt->pi.data_src; src_le != NULL;
240             src_le = src_le->next) {
241                 src = src_le->data;
242                 tvb = src->tvb;
243                 if (multiple_sources) {
244                         name = src->name;
245                         print_line(fh, format, "");
246                         line = g_malloc(strlen(name) + 2);      /* <name>:\0 */
247                         strcpy(line, name);
248                         strcat(line, ":");
249                         print_line(fh, format, line);
250                         g_free(line);
251                 }
252                 length = tvb_length(tvb);
253                 cp = tvb_get_ptr(tvb, 0, length);
254                 print_hex_data_buffer(fh, cp, length,
255                     edt->pi.fd->flags.encoding, format);
256         }
257 }
258
259 /*
260  * This routine is based on a routine created by Dan Lasley
261  * <DLASLEY@PROMUS.com>.
262  *
263  * It was modified for Ethereal by Gilbert Ramirez and others.
264  */
265 static void
266 print_hex_data_buffer(FILE *fh, register const u_char *cp,
267     register u_int length, char_enc encoding, gint format)
268 {
269         register unsigned int ad, i, j, k;
270         u_char c;
271         u_char line[6+50+16+1];
272         static u_char binhex[16] = {
273                 '0', '1', '2', '3', '4', '5', '6', '7',
274                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
275
276         if (format == PR_FMT_PS)
277                 print_ps_hex(fh);
278         print_line(fh, format, "");
279         ad = 0;
280         i = 0;
281         j = 0;
282         k = 0;
283         while (i < length) {
284                 if ((i & 15) == 0) {
285                         /*
286                          * Start of a new line.
287                          */
288                         j = 0;
289                         k = 0;
290                         sprintf(line, "%04x  ", ad);
291                         memset(line+6, ' ', sizeof line-6);
292                         line[sizeof (line)-1] = '\0';
293                 }
294                 c = *cp++;
295                 line[6+j++] = binhex[c>>4];
296                 line[6+j++] = binhex[c&0xf];
297                 j++;
298                 if (encoding == CHAR_EBCDIC) {
299                         c = EBCDIC_to_ASCII1(c);
300                 }
301                 line[6+50+k++] = c >= ' ' && c < 0x7f ? c : '.';
302                 i++;
303                 if ((i & 15) == 0 || i == length) {
304                         /*
305                          * We'll be starting a new line, or
306                          * we're finished printing this buffer;
307                          * dump out the line we've constructed,
308                          * and advance the offset.
309                          */
310                         print_line(fh, format, line);
311                         ad += 16;
312                 }
313         }
314 }
315
316 static
317 void ps_clean_string(unsigned char *out, const unsigned char *in,
318                         int outbuf_size)
319 {
320         int rd, wr;
321         char c;
322
323         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
324                 c = in[rd];
325                 switch (c) {
326                         case '(':
327                         case ')':
328                         case '\\':
329                                 out[wr] = '\\';
330                                 out[++wr] = c;
331                                 break;
332
333                         default:
334                                 out[wr] = c;
335                                 break;
336                 }
337
338                 if (c == 0) {
339                         break;
340                 }
341         }
342 }
343
344 void print_line(FILE *fh, gint format, char *line)
345 {
346         char            psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
347
348         if (format == PR_FMT_PS) {
349                 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
350                 fprintf(fh, "(%s) hexdump\n", psbuffer);
351         } else {
352                 fputs(line, fh);
353                 putc('\n', fh);
354         }
355 }