Put a blank line before the line identifying the data source 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.31 2001/03/24 09:39:47 guy Exp $
5  *
6  * Gilbert Ramirez <gram@xiexie.org>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <string.h>
34
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
37 #endif
38
39 #include "packet.h"
40 #include "print.h"
41 #include "ps.h"
42 #include "util.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         const guint8    *pd;
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, const u_char *pd, 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.pd = pd;
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 /* Print a tree's data, and any child nodes, in plain text */
124 static
125 void proto_tree_print_node_text(GNode *node, gpointer data)
126 {
127         field_info      *fi = (field_info*) (node->data);
128         print_data      *pdata = (print_data*) data;
129         int             i;
130         int             num_spaces;
131         char            space[41];
132         gchar           label_str[ITEM_LABEL_LENGTH];
133         gchar           *label_ptr;
134
135         /* Don't print invisible entries. */
136         if (!fi->visible)
137                 return;
138
139         /* was a free format label produced? */
140         if (fi->representation) {
141                 label_ptr = fi->representation;
142         }
143         else { /* no, make a generic label */
144                 label_ptr = label_str;
145                 proto_item_fill_label(fi, label_str);
146         }
147                 
148         /* Prepare the tabs for printing, depending on tree level */
149         num_spaces = pdata->level * 4;
150         if (num_spaces > 40) {
151                 num_spaces = 40;
152         }
153         for (i = 0; i < num_spaces; i++) {
154                 space[i] = ' ';
155         }
156         /* The string is NUL-terminated */
157         space[num_spaces] = 0;
158
159         /* Print the text */
160         fprintf(pdata->fh, "%s%s\n", space, label_ptr);
161
162         /* If it's uninterpreted data, dump it (unless our caller will
163            be printing the entire packet in hex). */
164         if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data)
165                 print_hex_data_text(pdata->fh, &pdata->pd[fi->start],
166                                 fi->length, pdata->encoding);
167
168         /* If we're printing all levels, or if this level is expanded,
169            recurse into the subtree, if it exists. */
170         if (pdata->print_all_levels || tree_is_expanded[fi->tree_type]) {
171                 if (g_node_n_children(node) > 0) {
172                         pdata->level++;
173                         g_node_children_foreach(node, G_TRAVERSE_ALL,
174                                 proto_tree_print_node_text, pdata);
175                         pdata->level--;
176                 }
177         }
178 }
179
180 void print_hex_data(FILE *fh, gint format, frame_data *fd)
181 {
182         gboolean multiple_sources;
183         GSList *src;
184         tvbuff_t *tvb;
185         char *name;
186         char *line;
187         const u_char *cp;
188         guint length;
189
190         /*
191          * Set "multiple_sources" iff this frame has more than one
192          * data source; if it does, we need to print the name of
193          * the data source before printing the data from the
194          * data source.
195          */
196         multiple_sources = (fd->data_src->next != NULL);
197
198         for (src = fd->data_src; src != NULL; src = src->next) {
199                 tvb = src->data;
200                 if (multiple_sources) {
201                         name = tvb_get_name(tvb);
202                         print_line(fh, format, "\n");
203                         line = g_malloc(strlen(name) + 3);      /* <name>:\n\0 */
204                         strcpy(line, name);
205                         strcat(line, ":");
206                         print_line(fh, format, line);
207                         g_free(line);
208                         print_line(fh, format, "\n");
209                 }
210                 length = tvb_length(tvb);
211                 cp = tvb_get_ptr(tvb, 0, length);
212                 if (format == PR_FMT_PS)
213                         print_hex_data_ps(fh, cp, length, fd->flags.encoding);
214                 else
215                         print_hex_data_text(fh, cp, length, fd->flags.encoding);
216         }
217 }
218
219 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
220 only slightly modified for ethereal by Gilbert Ramirez. */
221 static
222 void print_hex_data_text(FILE *fh, register const u_char *cp,
223                 register u_int length, char_enc encoding)
224 {
225         register int ad, i, j, k;
226         u_char c;
227         u_char line[60];
228         static u_char binhex[16] = {
229                 '0', '1', '2', '3', '4', '5', '6', '7',
230                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
231
232         memset (line, ' ', sizeof line);
233         line[sizeof (line)-1] = 0;
234         for (ad=i=j=k=0; i<length; i++) {
235                 c = *cp++;
236                 line[j++] = binhex[c>>4];
237                 line[j++] = binhex[c&0xf];
238                 if (i&1) j++;
239                 if (encoding == CHAR_EBCDIC) {
240                         c = EBCDIC_to_ASCII1(c);
241                 }
242                 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
243                 if ((i & 15) == 15) {
244                         fprintf (fh, "\n%4x  %s", ad, line);
245                         /*if (i==15) printf (" %d", length);*/
246                         memset (line, ' ', sizeof line);
247                         line[sizeof (line)-1] = j = k = 0;
248                         ad += 16;
249                 }
250         }
251
252         if (line[0] != ' ') fprintf (fh, "\n%4x  %s", ad, line);
253         fprintf(fh, "\n");
254         return;
255
256 }
257
258 #define MAX_LINE_LENGTH 256
259
260 /* Print a node's data, and any child nodes, in PostScript */
261 static
262 void proto_tree_print_node_ps(GNode *node, gpointer data)
263 {
264         field_info      *fi = (field_info*) (node->data);
265         print_data      *pdata = (print_data*) data;
266         gchar           label_str[ITEM_LABEL_LENGTH];
267         gchar           *label_ptr;
268         char            psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
269
270         if (!fi->visible)
271                 return;
272
273         /* was a free format label produced? */
274         if (fi->representation) {
275                 label_ptr = fi->representation;
276         }
277         else { /* no, make a generic label */
278                 label_ptr = label_str;
279                 proto_item_fill_label(fi, label_str);
280         }
281                 
282         /* Print the text */
283         ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
284         fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
285
286         /* If it's uninterpreted data, dump it (unless our caller will
287            be printing the entire packet in hex). */
288         if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
289                 print_hex_data_ps(pdata->fh, &pdata->pd[fi->start], fi->length,
290                                 pdata->encoding);
291         }
292
293         /* Recurse into the subtree, if it exists */
294         if (g_node_n_children(node) > 0) {
295                 pdata->level++;
296                 g_node_children_foreach(node, G_TRAVERSE_ALL,
297                         proto_tree_print_node_ps, pdata);
298                 pdata->level--;
299         }
300 }
301
302 static
303 void ps_clean_string(unsigned char *out, const unsigned char *in,
304                         int outbuf_size)
305 {
306         int rd, wr;
307         char c;
308
309         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
310                 c = in[rd];
311                 switch (c) {
312                         case '(':
313                         case ')':
314                         case '\\':
315                                 out[wr] = '\\';
316                                 out[++wr] = c;
317                                 break;
318
319                         default:
320                                 out[wr] = c;
321                                 break;
322                 }
323
324                 if (c == 0) {
325                         break;
326                 }
327         }
328 }
329
330 static
331 void print_hex_data_ps(FILE *fh, register const u_char *cp,
332                 register u_int length, char_enc encoding)
333 {
334         register int ad, i, j, k;
335         u_char c;
336         u_char line[60];
337         static u_char binhex[16] = {
338                 '0', '1', '2', '3', '4', '5', '6', '7',
339                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
340         u_char psline[MAX_LINE_LENGTH];
341
342         print_ps_hex(fh);
343         memset (line, ' ', sizeof line);
344         line[sizeof (line)-1] = 0;
345         for (ad=i=j=k=0; i<length; i++) {
346                 c = *cp++;
347                 line[j++] = binhex[c>>4];
348                 line[j++] = binhex[c&0xf];
349                 if (i&1) j++;
350                 if (encoding == CHAR_EBCDIC) {
351                         c = EBCDIC_to_ASCII1(c);
352                 }
353                 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
354                 if ((i & 15) == 15) {
355                         ps_clean_string(psline, line, MAX_LINE_LENGTH);
356                         fprintf (fh, "(%4x  %s) hexdump\n", ad, psline);
357                         memset (line, ' ', sizeof line);
358                         line[sizeof (line)-1] = j = k = 0;
359                         ad += 16;
360                 }
361         }
362
363         if (line[0] != ' ') {
364                 ps_clean_string(psline, line, MAX_LINE_LENGTH);
365                 fprintf (fh, "(%4x  %s) hexdump\n", ad, psline);
366         }
367         return;
368
369 }
370
371 void print_line(FILE *fh, gint format, char *line)
372 {
373         char            psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
374
375         if (format == PR_FMT_PS) {
376                 ps_clean_string(psbuffer, line, MAX_LINE_LENGTH);
377                 fprintf(fh, "(%s) hexdump\n", psbuffer);
378         } else
379                 fputs(line, fh);
380 }