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