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