2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.47 2002/06/04 07:03:47 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_text(GNode *node, gpointer data);
49 static void proto_tree_print_node_ps(GNode *node, gpointer data);
50 static void ps_clean_string(unsigned char *out, const unsigned char *in,
52 static void print_hex_data_text(FILE *fh, register const u_char *cp,
53 register u_int length, char_enc encoding);
54 static void print_hex_data_ps(FILE *fh, register const u_char *cp,
55 register u_int length, char_enc encoding);
61 gboolean print_all_levels;
62 gboolean print_hex_for_data;
66 FILE *open_print_dest(int to_file, const char *dest)
70 /* Open the file or command for output */
72 fh = fopen(dest, "w");
74 fh = popen(dest, "w");
79 void close_print_dest(int to_file, FILE *fh)
81 /* Close the file or command */
88 void print_preamble(FILE *fh, gint format)
90 if (format == PR_FMT_PS)
91 print_ps_preamble(fh);
94 void print_finale(FILE *fh, gint format)
96 if (format == PR_FMT_PS)
100 void proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
105 /* Create the output */
108 data.src_list = edt->pi.data_src;
109 data.encoding = edt->pi.fd->flags.encoding;
110 data.print_all_levels = print_args->expand_all;
111 data.print_hex_for_data = !print_args->print_hex;
112 /* If we're printing the entire packet in hex, don't
113 print uninterpreted data fields in hex as well. */
115 if (print_args->format == PR_FMT_TEXT) {
116 g_node_children_foreach((GNode*) edt->tree, G_TRAVERSE_ALL,
117 proto_tree_print_node_text, &data);
119 g_node_children_foreach((GNode*) edt->tree, G_TRAVERSE_ALL,
120 proto_tree_print_node_ps, &data);
125 * Find the data source for a specified field, and return a pointer
128 static const guint8 *
129 get_field_data(GSList *src_list, field_info *fi)
135 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
138 if (fi->ds_tvb == src_tvb) {
142 return tvb_get_ptr(src_tvb, fi->start, fi->length);
145 g_assert_not_reached();
146 return NULL; /* not found */
149 #define MAX_INDENT 160
151 /* Print a tree's data, and any child nodes, in plain text */
153 void proto_tree_print_node_text(GNode *node, gpointer data)
155 field_info *fi = PITEM_FINFO(node);
156 print_data *pdata = (print_data*) data;
159 char space[MAX_INDENT+1];
161 gchar label_str[ITEM_LABEL_LENGTH];
164 /* Don't print invisible entries. */
168 /* was a free format label produced? */
169 if (fi->representation) {
170 label_ptr = fi->representation;
172 else { /* no, make a generic label */
173 label_ptr = label_str;
174 proto_item_fill_label(fi, label_str);
177 /* Prepare the tabs for printing, depending on tree level */
178 num_spaces = pdata->level * 4;
179 if (num_spaces > MAX_INDENT) {
180 num_spaces = MAX_INDENT;
182 for (i = 0; i < num_spaces; i++) {
185 /* The string is NUL-terminated */
186 space[num_spaces] = '\0';
189 fprintf(pdata->fh, "%s%s\n", space, label_ptr);
191 /* If it's uninterpreted data, dump it (unless our caller will
192 be printing the entire packet in hex). */
193 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
195 * Find the data for this field.
197 pd = get_field_data(pdata->src_list, fi);
198 print_hex_data_text(pdata->fh, pd, fi->length, pdata->encoding);
201 /* If we're printing all levels, or if this node is one with a
202 subtree and its subtree is expanded, recurse into the subtree,
204 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
205 if (pdata->print_all_levels ||
206 (fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
207 if (g_node_n_children(node) > 0) {
209 g_node_children_foreach(node, G_TRAVERSE_ALL,
210 proto_tree_print_node_text, pdata);
216 void print_hex_data(FILE *fh, gint format, epan_dissect_t *edt)
218 gboolean multiple_sources;
228 * Set "multiple_sources" iff this frame has more than one
229 * data source; if it does, we need to print the name of
230 * the data source before printing the data from the
233 multiple_sources = (edt->pi.data_src->next != NULL);
235 for (src_le = edt->pi.data_src; src_le != NULL;
236 src_le = src_le->next) {
239 if (multiple_sources) {
241 print_line(fh, format, "\n");
242 line = g_malloc(strlen(name) + 3); /* <name>:\n\0 */
245 print_line(fh, format, line);
248 length = tvb_length(tvb);
249 cp = tvb_get_ptr(tvb, 0, length);
250 if (format == PR_FMT_PS) {
251 print_hex_data_ps(fh, cp, length,
252 edt->pi.fd->flags.encoding);
254 print_hex_data_text(fh, cp, length,
255 edt->pi.fd->flags.encoding);
260 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
261 only slightly modified for ethereal by Gilbert Ramirez. */
263 void print_hex_data_text(FILE *fh, register const u_char *cp,
264 register u_int length, char_enc encoding)
266 register unsigned int ad, i, j, k;
268 u_char line[50+16+1];
269 static u_char binhex[16] = {
270 '0', '1', '2', '3', '4', '5', '6', '7',
271 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
273 memset (line, ' ', sizeof line);
274 line[sizeof (line)-1] = 0;
275 for (ad=i=j=k=0; i<length; i++) {
277 line[j++] = binhex[c>>4];
278 line[j++] = binhex[c&0xf];
280 if (encoding == CHAR_EBCDIC) {
281 c = EBCDIC_to_ASCII1(c);
283 line[50+k++] = c >= ' ' && c < 0x7f ? c : '.';
284 if ((i & 15) == 15) {
285 fprintf (fh, "\n%04x %s", ad, line);
286 /*if (i==15) printf (" %d", length);*/
287 memset (line, ' ', sizeof line);
288 line[sizeof (line)-1] = j = k = 0;
293 if (line[0] != ' ') fprintf (fh, "\n%04x %s", ad, line);
299 #define MAX_LINE_LENGTH 256
301 /* Print a node's data, and any child nodes, in PostScript */
303 void proto_tree_print_node_ps(GNode *node, gpointer data)
305 field_info *fi = PITEM_FINFO(node);
306 print_data *pdata = (print_data*) data;
307 gchar label_str[ITEM_LABEL_LENGTH];
310 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
315 /* was a free format label produced? */
316 if (fi->representation) {
317 label_ptr = fi->representation;
319 else { /* no, make a generic label */
320 label_ptr = label_str;
321 proto_item_fill_label(fi, label_str);
325 ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
326 fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
328 /* If it's uninterpreted data, dump it (unless our caller will
329 be printing the entire packet in hex). */
330 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
332 * Find the data for this field.
334 pd = get_field_data(pdata->src_list, fi);
335 print_hex_data_ps(pdata->fh, pd, fi->length, pdata->encoding);
338 /* Recurse into the subtree, if it exists */
339 if (g_node_n_children(node) > 0) {
341 g_node_children_foreach(node, G_TRAVERSE_ALL,
342 proto_tree_print_node_ps, pdata);
348 void ps_clean_string(unsigned char *out, const unsigned char *in,
354 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
376 void print_hex_data_ps(FILE *fh, register const u_char *cp,
377 register u_int length, char_enc encoding)
379 register unsigned int ad, i, j, k;
382 static u_char binhex[16] = {
383 '0', '1', '2', '3', '4', '5', '6', '7',
384 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
385 u_char psline[MAX_LINE_LENGTH];
388 memset (line, ' ', sizeof line);
389 line[sizeof (line)-1] = 0;
390 for (ad=i=j=k=0; i<length; i++) {
392 line[j++] = binhex[c>>4];
393 line[j++] = binhex[c&0xf];
395 if (encoding == CHAR_EBCDIC) {
396 c = EBCDIC_to_ASCII1(c);
398 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
399 if ((i & 15) == 15) {
400 ps_clean_string(psline, line, MAX_LINE_LENGTH);
401 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
402 memset (line, ' ', sizeof line);
403 line[sizeof (line)-1] = j = k = 0;
408 if (line[0] != ' ') {
409 ps_clean_string(psline, line, MAX_LINE_LENGTH);
410 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
416 void print_line(FILE *fh, gint format, char *line)
418 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
420 if (format == PR_FMT_PS) {
421 ps_clean_string(psbuffer, line, MAX_LINE_LENGTH);
422 fprintf(fh, "(%s) hexdump\n", psbuffer);