tvb_get_nstringz() needs to terminate a string with a NUL if the
[metze/wireshark/wip.git] / print.c
1 /* print.c
2  * Routines for printing packet analysis trees.
3  *
4  * $Id: print.c,v 1.41 2002/02/18 01:08: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
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 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 /* 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[41];
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 > 40) {
177                 num_spaces = 40;
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 level is expanded,
199            recurse into the subtree, if it exists. */
200         if (pdata->print_all_levels || tree_is_expanded[fi->tree_type]) {
201                 if (g_node_n_children(node) > 0) {
202                         pdata->level++;
203                         g_node_children_foreach(node, G_TRAVERSE_ALL,
204                                 proto_tree_print_node_text, pdata);
205                         pdata->level--;
206                 }
207         }
208 }
209
210 void print_hex_data(FILE *fh, gint format, frame_data *fd)
211 {
212         gboolean multiple_sources;
213         GSList *src_le;
214         data_source *src;
215         tvbuff_t *tvb;
216         char *name;
217         char *line;
218         const u_char *cp;
219         guint length;
220
221         /*
222          * Set "multiple_sources" iff this frame has more than one
223          * data source; if it does, we need to print the name of
224          * the data source before printing the data from the
225          * data source.
226          */
227         multiple_sources = (fd->data_src->next != NULL);
228
229         for (src_le = fd->data_src; src_le != NULL; src_le = src_le->next) {
230                 src = src_le->data;
231                 tvb = src->tvb;
232                 if (multiple_sources) {
233                         name = src->name;
234                         print_line(fh, format, "\n");
235                         line = g_malloc(strlen(name) + 3);      /* <name>:\n\0 */
236                         strcpy(line, name);
237                         strcat(line, ":\n");
238                         print_line(fh, format, line);
239                         g_free(line);
240                 }
241                 length = tvb_length(tvb);
242                 cp = tvb_get_ptr(tvb, 0, length);
243                 if (format == PR_FMT_PS)
244                         print_hex_data_ps(fh, cp, length, fd->flags.encoding);
245                 else
246                         print_hex_data_text(fh, cp, length, fd->flags.encoding);
247         }
248 }
249
250 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
251 only slightly modified for ethereal by Gilbert Ramirez. */
252 static
253 void print_hex_data_text(FILE *fh, register const u_char *cp,
254                 register u_int length, char_enc encoding)
255 {
256         register unsigned int ad, i, j, k;
257         u_char c;
258         u_char line[50+16+1];
259         static u_char binhex[16] = {
260                 '0', '1', '2', '3', '4', '5', '6', '7',
261                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
262
263         memset (line, ' ', sizeof line);
264         line[sizeof (line)-1] = 0;
265         for (ad=i=j=k=0; i<length; i++) {
266                 c = *cp++;
267                 line[j++] = binhex[c>>4];
268                 line[j++] = binhex[c&0xf];
269                 j++;
270                 if (encoding == CHAR_EBCDIC) {
271                         c = EBCDIC_to_ASCII1(c);
272                 }
273                 line[50+k++] = c >= ' ' && c < 0x7f ? c : '.';
274                 if ((i & 15) == 15) {
275                         fprintf (fh, "\n%04x  %s", ad, line);
276                         /*if (i==15) printf (" %d", length);*/
277                         memset (line, ' ', sizeof line);
278                         line[sizeof (line)-1] = j = k = 0;
279                         ad += 16;
280                 }
281         }
282
283         if (line[0] != ' ') fprintf (fh, "\n%04x  %s", ad, line);
284         fprintf(fh, "\n");
285         return;
286
287 }
288
289 #define MAX_LINE_LENGTH 256
290
291 /* Print a node's data, and any child nodes, in PostScript */
292 static
293 void proto_tree_print_node_ps(GNode *node, gpointer data)
294 {
295         field_info      *fi = PITEM_FINFO(node);
296         print_data      *pdata = (print_data*) data;
297         gchar           label_str[ITEM_LABEL_LENGTH];
298         gchar           *label_ptr;
299         const guint8    *pd;
300         char            psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
301
302         if (!fi->visible)
303                 return;
304
305         /* was a free format label produced? */
306         if (fi->representation) {
307                 label_ptr = fi->representation;
308         }
309         else { /* no, make a generic label */
310                 label_ptr = label_str;
311                 proto_item_fill_label(fi, label_str);
312         }
313                 
314         /* Print the text */
315         ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
316         fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
317
318         /* If it's uninterpreted data, dump it (unless our caller will
319            be printing the entire packet in hex). */
320         if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
321                 /*
322                  * Find the data for this field.
323                  */
324                 pd = get_field_data(pdata->src_list, fi);
325                 print_hex_data_ps(pdata->fh, pd, fi->length, pdata->encoding);
326         }
327
328         /* Recurse into the subtree, if it exists */
329         if (g_node_n_children(node) > 0) {
330                 pdata->level++;
331                 g_node_children_foreach(node, G_TRAVERSE_ALL,
332                         proto_tree_print_node_ps, pdata);
333                 pdata->level--;
334         }
335 }
336
337 static
338 void ps_clean_string(unsigned char *out, const unsigned char *in,
339                         int outbuf_size)
340 {
341         int rd, wr;
342         char c;
343
344         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
345                 c = in[rd];
346                 switch (c) {
347                         case '(':
348                         case ')':
349                         case '\\':
350                                 out[wr] = '\\';
351                                 out[++wr] = c;
352                                 break;
353
354                         default:
355                                 out[wr] = c;
356                                 break;
357                 }
358
359                 if (c == 0) {
360                         break;
361                 }
362         }
363 }
364
365 static
366 void print_hex_data_ps(FILE *fh, register const u_char *cp,
367                 register u_int length, char_enc encoding)
368 {
369         register unsigned int ad, i, j, k;
370         u_char c;
371         u_char line[60];
372         static u_char binhex[16] = {
373                 '0', '1', '2', '3', '4', '5', '6', '7',
374                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
375         u_char psline[MAX_LINE_LENGTH];
376
377         print_ps_hex(fh);
378         memset (line, ' ', sizeof line);
379         line[sizeof (line)-1] = 0;
380         for (ad=i=j=k=0; i<length; i++) {
381                 c = *cp++;
382                 line[j++] = binhex[c>>4];
383                 line[j++] = binhex[c&0xf];
384                 if (i&1) j++;
385                 if (encoding == CHAR_EBCDIC) {
386                         c = EBCDIC_to_ASCII1(c);
387                 }
388                 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
389                 if ((i & 15) == 15) {
390                         ps_clean_string(psline, line, MAX_LINE_LENGTH);
391                         fprintf (fh, "(%4x  %s) hexdump\n", ad, psline);
392                         memset (line, ' ', sizeof line);
393                         line[sizeof (line)-1] = j = k = 0;
394                         ad += 16;
395                 }
396         }
397
398         if (line[0] != ' ') {
399                 ps_clean_string(psline, line, MAX_LINE_LENGTH);
400                 fprintf (fh, "(%4x  %s) hexdump\n", ad, psline);
401         }
402         return;
403
404 }
405
406 void print_line(FILE *fh, gint format, char *line)
407 {
408         char            psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
409
410         if (format == PR_FMT_PS) {
411                 ps_clean_string(psbuffer, line, MAX_LINE_LENGTH);
412                 fprintf(fh, "(%s) hexdump\n", psbuffer);
413         } else
414                 fputs(line, fh);
415 }