2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.46 2002/05/10 23:20:38 guy Exp $
6 * Gilbert Ramirez <gram@alumni.rice.edu>
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
10 * Copyright 1998 Gerald Combs
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.
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.
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.
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
38 #include <epan/packet.h>
42 #include <epan/tvbuff.h>
43 #include "packet-data.h"
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,
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);
58 gboolean print_all_levels;
59 gboolean print_hex_for_data;
63 FILE *open_print_dest(int to_file, const char *dest)
67 /* Open the file or command for output */
69 fh = fopen(dest, "w");
71 fh = popen(dest, "w");
76 void close_print_dest(int to_file, FILE *fh)
78 /* Close the file or command */
85 void print_preamble(FILE *fh, gint format)
87 if (format == PR_FMT_PS)
88 print_ps_preamble(fh);
91 void print_finale(FILE *fh, gint format)
93 if (format == PR_FMT_PS)
97 void proto_tree_print(print_args_t *print_args,
98 GNode *protocol_tree, frame_data *fd, FILE *fh)
102 /* Create the output */
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. */
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);
116 g_node_children_foreach((GNode*) protocol_tree, G_TRAVERSE_ALL,
117 proto_tree_print_node_ps, &data);
122 * Find the data source for a specified field, and return a pointer
125 static const guint8 *
126 get_field_data(GSList *src_list, field_info *fi)
132 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
135 if (fi->ds_tvb == src_tvb) {
139 return tvb_get_ptr(src_tvb, fi->start, fi->length);
142 g_assert_not_reached();
143 return NULL; /* not found */
146 #define MAX_INDENT 160
148 /* Print a tree's data, and any child nodes, in plain text */
150 void proto_tree_print_node_text(GNode *node, gpointer data)
152 field_info *fi = PITEM_FINFO(node);
153 print_data *pdata = (print_data*) data;
156 char space[MAX_INDENT+1];
158 gchar label_str[ITEM_LABEL_LENGTH];
161 /* Don't print invisible entries. */
165 /* was a free format label produced? */
166 if (fi->representation) {
167 label_ptr = fi->representation;
169 else { /* no, make a generic label */
170 label_ptr = label_str;
171 proto_item_fill_label(fi, label_str);
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;
179 for (i = 0; i < num_spaces; i++) {
182 /* The string is NUL-terminated */
183 space[num_spaces] = '\0';
186 fprintf(pdata->fh, "%s%s\n", space, label_ptr);
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) {
192 * Find the data for this field.
194 pd = get_field_data(pdata->src_list, fi);
195 print_hex_data_text(pdata->fh, pd, fi->length, pdata->encoding);
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,
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) {
206 g_node_children_foreach(node, G_TRAVERSE_ALL,
207 proto_tree_print_node_text, pdata);
213 void print_hex_data(FILE *fh, gint format, frame_data *fd)
215 gboolean multiple_sources;
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
230 multiple_sources = (fd->data_src->next != NULL);
232 for (src_le = fd->data_src; src_le != NULL; src_le = src_le->next) {
235 if (multiple_sources) {
237 print_line(fh, format, "\n");
238 line = g_malloc(strlen(name) + 3); /* <name>:\n\0 */
241 print_line(fh, format, line);
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);
249 print_hex_data_text(fh, cp, length, fd->flags.encoding);
253 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
254 only slightly modified for ethereal by Gilbert Ramirez. */
256 void print_hex_data_text(FILE *fh, register const u_char *cp,
257 register u_int length, char_enc encoding)
259 register unsigned int ad, i, j, k;
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'};
266 memset (line, ' ', sizeof line);
267 line[sizeof (line)-1] = 0;
268 for (ad=i=j=k=0; i<length; i++) {
270 line[j++] = binhex[c>>4];
271 line[j++] = binhex[c&0xf];
273 if (encoding == CHAR_EBCDIC) {
274 c = EBCDIC_to_ASCII1(c);
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;
286 if (line[0] != ' ') fprintf (fh, "\n%04x %s", ad, line);
292 #define MAX_LINE_LENGTH 256
294 /* Print a node's data, and any child nodes, in PostScript */
296 void proto_tree_print_node_ps(GNode *node, gpointer data)
298 field_info *fi = PITEM_FINFO(node);
299 print_data *pdata = (print_data*) data;
300 gchar label_str[ITEM_LABEL_LENGTH];
303 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
308 /* was a free format label produced? */
309 if (fi->representation) {
310 label_ptr = fi->representation;
312 else { /* no, make a generic label */
313 label_ptr = label_str;
314 proto_item_fill_label(fi, label_str);
318 ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
319 fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
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) {
325 * Find the data for this field.
327 pd = get_field_data(pdata->src_list, fi);
328 print_hex_data_ps(pdata->fh, pd, fi->length, pdata->encoding);
331 /* Recurse into the subtree, if it exists */
332 if (g_node_n_children(node) > 0) {
334 g_node_children_foreach(node, G_TRAVERSE_ALL,
335 proto_tree_print_node_ps, pdata);
341 void ps_clean_string(unsigned char *out, const unsigned char *in,
347 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
369 void print_hex_data_ps(FILE *fh, register const u_char *cp,
370 register u_int length, char_enc encoding)
372 register unsigned int ad, i, j, k;
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];
381 memset (line, ' ', sizeof line);
382 line[sizeof (line)-1] = 0;
383 for (ad=i=j=k=0; i<length; i++) {
385 line[j++] = binhex[c>>4];
386 line[j++] = binhex[c&0xf];
388 if (encoding == CHAR_EBCDIC) {
389 c = EBCDIC_to_ASCII1(c);
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;
401 if (line[0] != ' ') {
402 ps_clean_string(psline, line, MAX_LINE_LENGTH);
403 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
409 void print_line(FILE *fh, gint format, char *line)
411 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
413 if (format == PR_FMT_PS) {
414 ps_clean_string(psbuffer, line, MAX_LINE_LENGTH);
415 fprintf(fh, "(%s) hexdump\n", psbuffer);