2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.52 2002/06/22 01:43:57 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/epan.h>
39 #include <epan/epan_dissect.h>
40 #include <epan/tvbuff.h>
41 #include <epan/packet.h>
46 #include "packet-data.h"
48 static void proto_tree_print_node(GNode *node, gpointer data);
49 static void print_hex_data_buffer(FILE *fh, register const u_char *cp,
50 register u_int length, char_enc encoding, gint format);
51 static void ps_clean_string(unsigned char *out, const unsigned char *in,
58 gboolean print_all_levels;
59 gboolean print_hex_for_data;
61 gint format; /* text or PostScript */
64 FILE *open_print_dest(int to_file, const char *dest)
68 /* Open the file or command for output */
70 fh = fopen(dest, "w");
72 fh = popen(dest, "w");
77 void close_print_dest(int to_file, FILE *fh)
79 /* Close the file or command */
86 void print_preamble(FILE *fh, gint format)
88 if (format == PR_FMT_PS)
89 print_ps_preamble(fh);
92 void print_finale(FILE *fh, gint format)
94 if (format == PR_FMT_PS)
98 void proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
103 /* Create the output */
106 data.src_list = edt->pi.data_src;
107 data.encoding = edt->pi.fd->flags.encoding;
108 data.print_all_levels = print_args->expand_all;
109 data.print_hex_for_data = !print_args->print_hex;
110 /* If we're printing the entire packet in hex, don't
111 print uninterpreted data fields in hex as well. */
112 data.format = print_args->format;
114 g_node_children_foreach((GNode*) edt->tree, G_TRAVERSE_ALL,
115 proto_tree_print_node, &data);
119 * Find the data source for a specified field, and return a pointer
122 static const guint8 *
123 get_field_data(GSList *src_list, field_info *fi)
129 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
132 if (fi->ds_tvb == src_tvb) {
136 return tvb_get_ptr(src_tvb, fi->start, fi->length);
139 g_assert_not_reached();
140 return NULL; /* not found */
143 #define MAX_INDENT 160
145 #define MAX_PS_LINE_LENGTH 256
147 /* Print a tree's data, and any child nodes. */
149 void proto_tree_print_node(GNode *node, gpointer data)
151 field_info *fi = PITEM_FINFO(node);
152 print_data *pdata = (print_data*) data;
155 char space[MAX_INDENT+1];
157 gchar label_str[ITEM_LABEL_LENGTH];
159 char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
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 if (pdata->format == PR_FMT_PS) {
175 /* Print the text, as PostScript */
176 ps_clean_string(psbuffer, label_ptr, MAX_PS_LINE_LENGTH);
177 fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
179 /* Prepare the tabs for printing, depending on tree level */
180 num_spaces = pdata->level * 4;
181 if (num_spaces > MAX_INDENT) {
182 num_spaces = MAX_INDENT;
184 for (i = 0; i < num_spaces; i++) {
187 /* The string is NUL-terminated */
188 space[num_spaces] = '\0';
191 fprintf(pdata->fh, "%s%s\n", space, label_ptr);
194 /* If it's uninterpreted data, dump it (unless our caller will
195 be printing the entire packet in hex). */
196 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
198 * Find the data for this field.
200 pd = get_field_data(pdata->src_list, fi);
201 print_hex_data_buffer(pdata->fh, pd, fi->length,
202 pdata->encoding, pdata->format);
205 /* If we're printing all levels, or if this node is one with a
206 subtree and its subtree is expanded, recurse into the subtree,
208 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
209 if (pdata->print_all_levels ||
210 (fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
211 if (g_node_n_children(node) > 0) {
213 g_node_children_foreach(node, G_TRAVERSE_ALL,
214 proto_tree_print_node, pdata);
220 void print_hex_data(FILE *fh, gint format, epan_dissect_t *edt)
222 gboolean multiple_sources;
232 * Set "multiple_sources" iff this frame has more than one
233 * data source; if it does, we need to print the name of
234 * the data source before printing the data from the
237 multiple_sources = (edt->pi.data_src->next != NULL);
239 for (src_le = edt->pi.data_src; src_le != NULL;
240 src_le = src_le->next) {
243 if (multiple_sources) {
245 print_line(fh, format, "");
246 line = g_malloc(strlen(name) + 2); /* <name>:\0 */
249 print_line(fh, format, line);
252 length = tvb_length(tvb);
253 cp = tvb_get_ptr(tvb, 0, length);
254 print_hex_data_buffer(fh, cp, length,
255 edt->pi.fd->flags.encoding, format);
260 * This routine is based on a routine created by Dan Lasley
261 * <DLASLEY@PROMUS.com>.
263 * It was modified for Ethereal by Gilbert Ramirez and others.
266 print_hex_data_buffer(FILE *fh, register const u_char *cp,
267 register u_int length, char_enc encoding, gint format)
269 register unsigned int ad, i, j, k;
271 u_char line[6+50+16+1];
272 static u_char binhex[16] = {
273 '0', '1', '2', '3', '4', '5', '6', '7',
274 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
276 if (format == PR_FMT_PS)
278 print_line(fh, format, "");
286 * Start of a new line.
290 sprintf(line, "%04x ", ad);
291 memset(line+6, ' ', sizeof line-6);
292 line[sizeof (line)-1] = '\0';
295 line[6+j++] = binhex[c>>4];
296 line[6+j++] = binhex[c&0xf];
298 if (encoding == CHAR_EBCDIC) {
299 c = EBCDIC_to_ASCII1(c);
301 line[6+50+k++] = c >= ' ' && c < 0x7f ? c : '.';
303 if ((i & 15) == 0 || i == length) {
305 * We'll be starting a new line, or
306 * we're finished printing this buffer;
307 * dump out the line we've constructed,
308 * and advance the offset.
310 print_line(fh, format, line);
317 void ps_clean_string(unsigned char *out, const unsigned char *in,
323 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
344 void print_line(FILE *fh, gint format, char *line)
346 char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
348 if (format == PR_FMT_PS) {
349 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
350 fprintf(fh, "(%s) hexdump\n", psbuffer);