2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.17 1999/09/01 03:04:12 gram Exp $
6 * Gilbert Ramirez <gram@verdict.uthscsa.edu>
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@zing.org>
10 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
40 /*#include "ethereal.h"*/
46 static void proto_tree_print_node_text(GNode *node, gpointer data);
47 static void dumpit (FILE *fh, register const u_char *cp, register u_int length);
48 static void proto_tree_print_node_ps(GNode *node, gpointer data);
49 static void ps_clean_string(unsigned char *out, const unsigned char *in,
51 static void dumpit_ps (FILE *fh, register const u_char *cp, register u_int length);
53 extern int proto_data; /* in packet-data.c */
62 FILE *open_print_dest(int to_file, const char *dest)
66 /* Open the file or command for output */
68 fh = fopen(dest, "w");
70 fh = popen(dest, "w");
75 void close_print_dest(int to_file, FILE *fh)
77 /* Close the file or command */
84 void print_preamble(FILE *fh)
86 if (prefs.pr_format == PR_FMT_PS)
87 print_ps_preamble(fh);
90 void print_finale(FILE *fh)
92 if (prefs.pr_format == PR_FMT_PS)
96 void proto_tree_print(int frame_num, GNode *protocol_tree,
97 const u_char *pd, frame_data *fd, FILE *fh)
101 /* Create the output */
106 /* XXX - printing multiple frames in PostScript looks as if it's
107 tricky - you have to deal with page boundaries, I think -
108 and I'll have to spend some time learning enough about
109 PostScript to figure it out, so, for now, we only print
110 multiple frames as text. */
111 if (prefs.pr_format == PR_FMT_TEXT || frame_num != -1) {
113 fprintf(fh, "Frame %d:\n\n", frame_num);
114 g_node_children_foreach((GNode*) protocol_tree, G_TRAVERSE_ALL,
115 proto_tree_print_node_text, &data);
119 if (frame_num != -1) {
120 fprintf(fh, "0 (Frame %d:) putline\n", frame_num);
121 fprintf(fh, "0 () putline\n");
123 g_node_children_foreach((GNode*) protocol_tree, G_TRAVERSE_ALL,
124 proto_tree_print_node_ps, &data);
126 fprintf(fh, "0 () putline\n");
130 /* Print a tree's data, and any child nodes, in plain text */
132 void proto_tree_print_node_text(GNode *node, gpointer data)
134 field_info *fi = (field_info*) (node->data);
135 print_data *pdata = (print_data*) data;
139 gchar label_str[ITEM_LABEL_LENGTH];
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 /* Prepare the tabs for printing, depending on tree level */
155 num_spaces = pdata->level * 4;
156 if (num_spaces > 40) {
159 for (i = 0; i < num_spaces; i++) {
162 /* The string is NUL-terminated */
163 space[num_spaces] = 0;
166 fprintf(pdata->fh, "%s%s\n", space, label_ptr);
168 /* If it's uninterpreted data, dump it. */
169 if (fi->hfinfo->id == proto_data)
170 dumpit(pdata->fh, &pdata->pd[fi->start], fi->length);
172 /* Recurse into the subtree, if it exists */
173 if (g_node_n_children(node) > 0) {
175 g_node_children_foreach(node, G_TRAVERSE_ALL,
176 proto_tree_print_node_text, pdata);
181 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
182 only slightly modified for ethereal by Gilbert Ramirez. */
184 void dumpit (FILE *fh, register const u_char *cp, register u_int length)
186 register int ad, i, j, k;
189 static u_char binhex[16] = {
190 '0', '1', '2', '3', '4', '5', '6', '7',
191 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
193 memset (line, ' ', sizeof line);
194 line[sizeof (line)-1] = 0;
195 for (ad=i=j=k=0; i<length; i++) {
197 line[j++] = binhex[c>>4];
198 line[j++] = binhex[c&0xf];
200 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
201 if ((i & 15) == 15) {
202 fprintf (fh, "\n%4x %s", ad, line);
203 /*if (i==15) printf (" %d", length);*/
204 memset (line, ' ', sizeof line);
205 line[sizeof (line)-1] = j = k = 0;
210 if (line[0] != ' ') fprintf (fh, "\n%4x %s", ad, line);
216 #define MAX_LINE_LENGTH 256
218 /* Print a node's data, and any child nodes, in PostScript */
220 void proto_tree_print_node_ps(GNode *node, gpointer data)
222 field_info *fi = (field_info*) (node->data);
223 print_data *pdata = (print_data*) data;
224 gchar label_str[ITEM_LABEL_LENGTH];
226 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
231 /* was a free format label produced? */
232 if (fi->representation) {
233 label_ptr = fi->representation;
235 else { /* no, make a generic label */
236 label_ptr = label_str;
237 proto_item_fill_label(fi, label_str);
241 ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
242 fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
244 /* If it's uninterpreted data, dump it. */
245 if (fi->hfinfo->id == proto_data) {
246 print_ps_hex(pdata->fh);
247 dumpit_ps(pdata->fh, &pdata->pd[fi->start], fi->length);
250 /* Recurse into the subtree, if it exists */
251 if (g_node_n_children(node) > 0) {
253 g_node_children_foreach(node, G_TRAVERSE_ALL,
254 proto_tree_print_node_ps, pdata);
260 void ps_clean_string(unsigned char *out, const unsigned char *in,
266 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
288 void dumpit_ps (FILE *fh, register const u_char *cp, register u_int length)
290 register int ad, i, j, k;
293 static u_char binhex[16] = {
294 '0', '1', '2', '3', '4', '5', '6', '7',
295 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
296 u_char psline[MAX_LINE_LENGTH];
298 memset (line, ' ', sizeof line);
299 line[sizeof (line)-1] = 0;
300 for (ad=i=j=k=0; i<length; i++) {
302 line[j++] = binhex[c>>4];
303 line[j++] = binhex[c&0xf];
305 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
306 if ((i & 15) == 15) {
307 ps_clean_string(psline, line, MAX_LINE_LENGTH);
308 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
309 memset (line, ' ', sizeof line);
310 line[sizeof (line)-1] = j = k = 0;
315 if (line[0] != ' ') {
316 ps_clean_string(psline, line, MAX_LINE_LENGTH);
317 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);