Eliminate some unused variables.
[obnox/wireshark/wip.git] / print.c
1 /* print.c
2  * Routines for printing packet analysis trees.
3  *
4  * $Id: print.c,v 1.45 2002/04/02 05:07:36 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
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(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 for a specified field, and return a pointer
125  * to the data in it.
126  */
127 static const guint8 *
128 get_field_data(GSList *src_list, field_info *fi)
129 {
130         GSList *src_le;
131         data_source *src;
132         tvbuff_t *src_tvb;
133
134         for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
135                 src = src_le->data;
136                 src_tvb = src->tvb;
137                 if (fi->ds_tvb == src_tvb) {
138                         /*
139                          * Found it.
140                          */
141                         return tvb_get_ptr(src_tvb, fi->start, fi->length);
142                 }
143         }
144         g_assert_not_reached();
145         return NULL;    /* not found */
146 }
147
148 #define MAX_INDENT      160
149
150 /* Print a tree's data, and any child nodes, in plain text */
151 static
152 void proto_tree_print_node_text(GNode *node, gpointer data)
153 {
154         field_info      *fi = PITEM_FINFO(node);
155         print_data      *pdata = (print_data*) data;
156         int             i;
157         int             num_spaces;
158         char            space[MAX_INDENT+1];
159         const guint8    *pd;
160         gchar           label_str[ITEM_LABEL_LENGTH];
161         gchar           *label_ptr;
162
163         /* Don't print invisible entries. */
164         if (!fi->visible)
165                 return;
166
167         /* was a free format label produced? */
168         if (fi->representation) {
169                 label_ptr = fi->representation;
170         }
171         else { /* no, make a generic label */
172                 label_ptr = label_str;
173                 proto_item_fill_label(fi, label_str);
174         }
175                 
176         /* Prepare the tabs for printing, depending on tree level */
177         num_spaces = pdata->level * 4;
178         if (num_spaces > MAX_INDENT) {
179                 num_spaces = MAX_INDENT;
180         }
181         for (i = 0; i < num_spaces; i++) {
182                 space[i] = ' ';
183         }
184         /* The string is NUL-terminated */
185         space[num_spaces] = '\0';
186
187         /* Print the text */
188         fprintf(pdata->fh, "%s%s\n", space, label_ptr);
189
190         /* If it's uninterpreted data, dump it (unless our caller will
191            be printing the entire packet in hex). */
192         if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
193                 /*
194                  * Find the data for this field.
195                  */
196                 pd = get_field_data(pdata->src_list, fi);
197                 print_hex_data_text(pdata->fh, pd, fi->length, pdata->encoding);
198         }
199
200         /* If we're printing all levels, or if this node is one with a
201            subtree and its subtree is expanded, recurse into the subtree,
202            if it exists. */
203         g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
204         if (pdata->print_all_levels ||
205             (fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
206                 if (g_node_n_children(node) > 0) {
207                         pdata->level++;
208                         g_node_children_foreach(node, G_TRAVERSE_ALL,
209                                 proto_tree_print_node_text, pdata);
210                         pdata->level--;
211                 }
212         }
213 }
214
215 void print_hex_data(FILE *fh, gint format, frame_data *fd)
216 {
217         gboolean multiple_sources;
218         GSList *src_le;
219         data_source *src;
220         tvbuff_t *tvb;
221         char *name;
222         char *line;
223         const u_char *cp;
224         guint length;
225
226         /*
227          * Set "multiple_sources" iff this frame has more than one
228          * data source; if it does, we need to print the name of
229          * the data source before printing the data from the
230          * data source.
231          */
232         multiple_sources = (fd->data_src->next != NULL);
233
234         for (src_le = fd->data_src; src_le != NULL; src_le = src_le->next) {
235                 src = src_le->data;
236                 tvb = src->tvb;
237                 if (multiple_sources) {
238                         name = src->name;
239                         print_line(fh, format, "\n");
240                         line = g_malloc(strlen(name) + 3);      /* <name>:\n\0 */
241                         strcpy(line, name);
242                         strcat(line, ":\n");
243                         print_line(fh, format, line);
244                         g_free(line);
245                 }
246                 length = tvb_length(tvb);
247                 cp = tvb_get_ptr(tvb, 0, length);
248                 if (format == PR_FMT_PS)
249                         print_hex_data_ps(fh, cp, length, fd->flags.encoding);
250                 else
251                         print_hex_data_text(fh, cp, length, fd->flags.encoding);
252         }
253 }
254
255 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
256 only slightly modified for ethereal by Gilbert Ramirez. */
257 static
258 void print_hex_data_text(FILE *fh, register const u_char *cp,
259                 register u_int length, char_enc encoding)
260 {
261         register unsigned int ad, i, j, k;
262         u_char c;
263         u_char line[50+16+1];
264         static u_char binhex[16] = {
265                 '0', '1', '2', '3', '4', '5', '6', '7',
266                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
267
268         memset (line, ' ', sizeof line);
269         line[sizeof (line)-1] = 0;
270         for (ad=i=j=k=0; i<length; i++) {
271                 c = *cp++;
272                 line[j++] = binhex[c>>4];
273                 line[j++] = binhex[c&0xf];
274                 j++;
275                 if (encoding == CHAR_EBCDIC) {
276                         c = EBCDIC_to_ASCII1(c);
277                 }
278                 line[50+k++] = c >= ' ' && c < 0x7f ? c : '.';
279                 if ((i & 15) == 15) {
280                         fprintf (fh, "\n%04x  %s", ad, line);
281                         /*if (i==15) printf (" %d", length);*/
282                         memset (line, ' ', sizeof line);
283                         line[sizeof (line)-1] = j = k = 0;
284                         ad += 16;
285                 }
286         }
287
288         if (line[0] != ' ') fprintf (fh, "\n%04x  %s", ad, line);
289         fprintf(fh, "\n");
290         return;
291
292 }
293
294 #define MAX_LINE_LENGTH 256
295
296 /* Print a node's data, and any child nodes, in PostScript */
297 static
298 void proto_tree_print_node_ps(GNode *node, gpointer data)
299 {
300         field_info      *fi = PITEM_FINFO(node);
301         print_data      *pdata = (print_data*) data;
302         gchar           label_str[ITEM_LABEL_LENGTH];
303         gchar           *label_ptr;
304         const guint8    *pd;
305         char            psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
306
307         if (!fi->visible)
308                 return;
309
310         /* was a free format label produced? */
311         if (fi->representation) {
312                 label_ptr = fi->representation;
313         }
314         else { /* no, make a generic label */
315                 label_ptr = label_str;
316                 proto_item_fill_label(fi, label_str);
317         }
318                 
319         /* Print the text */
320         ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
321         fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
322
323         /* If it's uninterpreted data, dump it (unless our caller will
324            be printing the entire packet in hex). */
325         if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
326                 /*
327                  * Find the data for this field.
328                  */
329                 pd = get_field_data(pdata->src_list, fi);
330                 print_hex_data_ps(pdata->fh, pd, fi->length, pdata->encoding);
331         }
332
333         /* Recurse into the subtree, if it exists */
334         if (g_node_n_children(node) > 0) {
335                 pdata->level++;
336                 g_node_children_foreach(node, G_TRAVERSE_ALL,
337                         proto_tree_print_node_ps, pdata);
338                 pdata->level--;
339         }
340 }
341
342 static
343 void ps_clean_string(unsigned char *out, const unsigned char *in,
344                         int outbuf_size)
345 {
346         int rd, wr;
347         char c;
348
349         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
350                 c = in[rd];
351                 switch (c) {
352                         case '(':
353                         case ')':
354                         case '\\':
355                                 out[wr] = '\\';
356                                 out[++wr] = c;
357                                 break;
358
359                         default:
360                                 out[wr] = c;
361                                 break;
362                 }
363
364                 if (c == 0) {
365                         break;
366                 }
367         }
368 }
369
370 static
371 void print_hex_data_ps(FILE *fh, register const u_char *cp,
372                 register u_int length, char_enc encoding)
373 {
374         register unsigned int ad, i, j, k;
375         u_char c;
376         u_char line[60];
377         static u_char binhex[16] = {
378                 '0', '1', '2', '3', '4', '5', '6', '7',
379                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
380         u_char psline[MAX_LINE_LENGTH];
381
382         print_ps_hex(fh);
383         memset (line, ' ', sizeof line);
384         line[sizeof (line)-1] = 0;
385         for (ad=i=j=k=0; i<length; i++) {
386                 c = *cp++;
387                 line[j++] = binhex[c>>4];
388                 line[j++] = binhex[c&0xf];
389                 if (i&1) j++;
390                 if (encoding == CHAR_EBCDIC) {
391                         c = EBCDIC_to_ASCII1(c);
392                 }
393                 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
394                 if ((i & 15) == 15) {
395                         ps_clean_string(psline, line, MAX_LINE_LENGTH);
396                         fprintf (fh, "(%4x  %s) hexdump\n", ad, psline);
397                         memset (line, ' ', sizeof line);
398                         line[sizeof (line)-1] = j = k = 0;
399                         ad += 16;
400                 }
401         }
402
403         if (line[0] != ' ') {
404                 ps_clean_string(psline, line, MAX_LINE_LENGTH);
405                 fprintf (fh, "(%4x  %s) hexdump\n", ad, psline);
406         }
407         return;
408
409 }
410
411 void print_line(FILE *fh, gint format, char *line)
412 {
413         char            psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
414
415         if (format == PR_FMT_PS) {
416                 ps_clean_string(psbuffer, line, MAX_LINE_LENGTH);
417                 fprintf(fh, "(%s) hexdump\n", psbuffer);
418         } else
419                 fputs(line, fh);
420 }