2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.57 2002/08/28 21:00:40 jmayer 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 #include <epan/epan.h>
35 #include <epan/epan_dissect.h>
36 #include <epan/tvbuff.h>
37 #include <epan/packet.h>
42 #include "packet-data.h"
44 static void proto_tree_print_node(GNode *node, gpointer data);
45 static void print_hex_data_buffer(FILE *fh, register const guchar *cp,
46 register guint length, char_enc encoding, gint format);
47 static void ps_clean_string(unsigned char *out, const unsigned char *in,
54 gboolean print_all_levels;
55 gboolean print_hex_for_data;
57 gint format; /* text or PostScript */
60 FILE *open_print_dest(int to_file, const char *dest)
64 /* Open the file or command for output */
66 fh = fopen(dest, "w");
68 fh = popen(dest, "w");
73 void close_print_dest(int to_file, FILE *fh)
75 /* Close the file or command */
82 void proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
87 /* Create the output */
90 data.src_list = edt->pi.data_src;
91 data.encoding = edt->pi.fd->flags.encoding;
92 data.print_all_levels = print_args->expand_all;
93 data.print_hex_for_data = !print_args->print_hex;
94 /* If we're printing the entire packet in hex, don't
95 print uninterpreted data fields in hex as well. */
96 data.format = print_args->format;
98 g_node_children_foreach((GNode*) edt->tree, G_TRAVERSE_ALL,
99 proto_tree_print_node, &data);
103 * Find the data source for a specified field, and return a pointer
106 static const guint8 *
107 get_field_data(GSList *src_list, field_info *fi)
113 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
116 if (fi->ds_tvb == src_tvb) {
120 return tvb_get_ptr(src_tvb, fi->start, fi->length);
123 g_assert_not_reached();
124 return NULL; /* not found */
127 #define MAX_INDENT 160
129 #define MAX_PS_LINE_LENGTH 256
131 /* Print a tree's data, and any child nodes. */
133 void proto_tree_print_node(GNode *node, gpointer data)
135 field_info *fi = PITEM_FINFO(node);
136 print_data *pdata = (print_data*) data;
138 gchar label_str[ITEM_LABEL_LENGTH];
141 /* Don't print invisible entries. */
145 /* was a free format label produced? */
146 if (fi->representation) {
147 label_ptr = fi->representation;
149 else { /* no, make a generic label */
150 label_ptr = label_str;
151 proto_item_fill_label(fi, label_str);
154 print_line(pdata->fh, pdata->level, pdata->format, label_ptr);
156 /* If it's uninterpreted data, dump it (unless our caller will
157 be printing the entire packet in hex). */
158 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
160 * Find the data for this field.
162 pd = get_field_data(pdata->src_list, fi);
163 print_hex_data_buffer(pdata->fh, pd, fi->length,
164 pdata->encoding, pdata->format);
167 /* If we're printing all levels, or if this node is one with a
168 subtree and its subtree is expanded, recurse into the subtree,
170 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
171 if (pdata->print_all_levels ||
172 (fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
173 if (g_node_n_children(node) > 0) {
175 g_node_children_foreach(node, G_TRAVERSE_ALL,
176 proto_tree_print_node, pdata);
182 void print_hex_data(FILE *fh, gint format, epan_dissect_t *edt)
184 gboolean multiple_sources;
194 * Set "multiple_sources" iff this frame has more than one
195 * data source; if it does, we need to print the name of
196 * the data source before printing the data from the
199 multiple_sources = (edt->pi.data_src->next != NULL);
201 for (src_le = edt->pi.data_src; src_le != NULL;
202 src_le = src_le->next) {
205 if (multiple_sources) {
207 print_line(fh, 0, format, "");
208 line = g_malloc(strlen(name) + 2); /* <name>:\0 */
211 print_line(fh, 0, format, line);
214 length = tvb_length(tvb);
215 cp = tvb_get_ptr(tvb, 0, length);
216 print_hex_data_buffer(fh, cp, length,
217 edt->pi.fd->flags.encoding, format);
222 * This routine is based on a routine created by Dan Lasley
223 * <DLASLEY@PROMUS.com>.
225 * It was modified for Ethereal by Gilbert Ramirez and others.
228 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
229 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
230 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
231 /* max number of characters hex dump takes -
232 2 digits plus trailing blank */
233 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
234 /* number of characters those bytes take;
235 3 characters per byte of hex dump,
236 2 blanks separating hex from ASCII,
237 1 character per byte of ASCII dump */
238 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
239 /* number of characters per line;
240 offset, 2 blanks separating offset
241 from data dump, data dump */
244 print_hex_data_buffer(FILE *fh, register const guchar *cp,
245 register guint length, char_enc encoding, gint format)
247 register unsigned int ad, i, j, k, l;
249 guchar line[MAX_LINE_LEN + 1];
250 unsigned int use_digits;
251 static guchar binhex[16] = {
252 '0', '1', '2', '3', '4', '5', '6', '7',
253 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
255 print_line(fh, 0, format, "");
258 * How many of the leading digits of the offset will we supply?
259 * We always supply at least 4 digits, but if the maximum offset
260 * won't fit in 4 digits, we use as many digits as will be needed.
262 if (((length - 1) & 0xF0000000) != 0)
263 use_digits = 8; /* need all 8 digits */
264 else if (((length - 1) & 0x0F000000) != 0)
265 use_digits = 7; /* need 7 digits */
266 else if (((length - 1) & 0x00F00000) != 0)
267 use_digits = 6; /* need 6 digits */
268 else if (((length - 1) & 0x000F0000) != 0)
269 use_digits = 5; /* need 5 digits */
271 use_digits = 4; /* we'll supply 4 digits */
280 * Start of a new line.
287 c = (ad >> (l*4)) & 0xF;
288 line[j++] = binhex[c];
292 memset(line+j, ' ', DATA_DUMP_LEN);
295 * Offset in line of ASCII dump.
297 k = j + HEX_DUMP_LEN + 2;
300 line[j++] = binhex[c>>4];
301 line[j++] = binhex[c&0xf];
303 if (encoding == CHAR_EBCDIC) {
304 c = EBCDIC_to_ASCII1(c);
306 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
308 if ((i & 15) == 0 || i == length) {
310 * We'll be starting a new line, or
311 * we're finished printing this buffer;
312 * dump out the line we've constructed,
313 * and advance the offset.
316 print_line(fh, 0, format, line);
323 void ps_clean_string(unsigned char *out, const unsigned char *in,
329 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
350 void print_preamble(FILE *fh, gint format)
352 if (format == PR_FMT_PS)
353 print_ps_preamble(fh);
356 void print_finale(FILE *fh, gint format)
358 if (format == PR_FMT_PS)
362 void print_line(FILE *fh, int indent, gint format, char *line)
364 char space[MAX_INDENT+1];
367 char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
369 if (format == PR_FMT_PS) {
370 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
371 fprintf(fh, "%d (%s) putline\n", indent, psbuffer);
373 /* Prepare the tabs for printing, depending on tree level */
374 num_spaces = indent * 4;
375 if (num_spaces > MAX_INDENT) {
376 num_spaces = MAX_INDENT;
378 for (i = 0; i < num_spaces; i++) {
381 /* The string is NUL-terminated */
382 space[num_spaces] = '\0';