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