2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.20 1999/09/12 20:23:33 guy 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>
45 static void proto_tree_print_node_text(GNode *node, gpointer data);
46 static void dumpit (FILE *fh, register const u_char *cp, register u_int length);
47 static void proto_tree_print_node_ps(GNode *node, gpointer data);
48 static void ps_clean_string(unsigned char *out, const unsigned char *in,
50 static void dumpit_ps (FILE *fh, register const u_char *cp, register u_int length);
52 extern int proto_data; /* in packet-data.c */
59 gboolean print_all_levels;
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(gboolean print_one_packet, gboolean print_all_levels,
97 GNode *protocol_tree, const u_char *pd, frame_data *fd, FILE *fh)
101 /* Create the output */
105 data.print_all_levels = print_all_levels;
107 /* XXX - printing multiple frames in PostScript looks as if it's
108 tricky - you have to deal with page boundaries, I think -
109 and I'll have to spend some time learning enough about
110 PostScript to figure it out, so, for now, we only print
111 multiple frames as text. */
112 if (prefs.pr_format == PR_FMT_TEXT || !print_one_packet) {
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);
121 /* Print a tree's data, and any child nodes, in plain text */
123 void proto_tree_print_node_text(GNode *node, gpointer data)
125 field_info *fi = (field_info*) (node->data);
126 print_data *pdata = (print_data*) data;
130 gchar label_str[ITEM_LABEL_LENGTH];
133 /* Don't print invisible entries. */
137 /* was a free format label produced? */
138 if (fi->representation) {
139 label_ptr = fi->representation;
141 else { /* no, make a generic label */
142 label_ptr = label_str;
143 proto_item_fill_label(fi, label_str);
146 /* Prepare the tabs for printing, depending on tree level */
147 num_spaces = pdata->level * 4;
148 if (num_spaces > 40) {
151 for (i = 0; i < num_spaces; i++) {
154 /* The string is NUL-terminated */
155 space[num_spaces] = 0;
158 fprintf(pdata->fh, "%s%s\n", space, label_ptr);
160 /* If it's uninterpreted data, dump it. */
161 if (fi->hfinfo->id == proto_data)
162 dumpit(pdata->fh, &pdata->pd[fi->start], fi->length);
164 /* If we're printing all levels, or if this level is expanded,
165 recurse into the subtree, if it exists. */
166 if (pdata->print_all_levels || tree_is_expanded[fi->tree_type]) {
167 if (g_node_n_children(node) > 0) {
169 g_node_children_foreach(node, G_TRAVERSE_ALL,
170 proto_tree_print_node_text, pdata);
176 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
177 only slightly modified for ethereal by Gilbert Ramirez. */
179 void dumpit (FILE *fh, register const u_char *cp, register u_int length)
181 register int ad, i, j, k;
184 static u_char binhex[16] = {
185 '0', '1', '2', '3', '4', '5', '6', '7',
186 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
188 memset (line, ' ', sizeof line);
189 line[sizeof (line)-1] = 0;
190 for (ad=i=j=k=0; i<length; i++) {
192 line[j++] = binhex[c>>4];
193 line[j++] = binhex[c&0xf];
195 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
196 if ((i & 15) == 15) {
197 fprintf (fh, "\n%4x %s", ad, line);
198 /*if (i==15) printf (" %d", length);*/
199 memset (line, ' ', sizeof line);
200 line[sizeof (line)-1] = j = k = 0;
205 if (line[0] != ' ') fprintf (fh, "\n%4x %s", ad, line);
211 #define MAX_LINE_LENGTH 256
213 /* Print a node's data, and any child nodes, in PostScript */
215 void proto_tree_print_node_ps(GNode *node, gpointer data)
217 field_info *fi = (field_info*) (node->data);
218 print_data *pdata = (print_data*) data;
219 gchar label_str[ITEM_LABEL_LENGTH];
221 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
226 /* was a free format label produced? */
227 if (fi->representation) {
228 label_ptr = fi->representation;
230 else { /* no, make a generic label */
231 label_ptr = label_str;
232 proto_item_fill_label(fi, label_str);
236 ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
237 fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
239 /* If it's uninterpreted data, dump it. */
240 if (fi->hfinfo->id == proto_data) {
241 print_ps_hex(pdata->fh);
242 dumpit_ps(pdata->fh, &pdata->pd[fi->start], fi->length);
245 /* Recurse into the subtree, if it exists */
246 if (g_node_n_children(node) > 0) {
248 g_node_children_foreach(node, G_TRAVERSE_ALL,
249 proto_tree_print_node_ps, pdata);
255 void ps_clean_string(unsigned char *out, const unsigned char *in,
261 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
283 void dumpit_ps (FILE *fh, register const u_char *cp, register u_int length)
285 register int ad, i, j, k;
288 static u_char binhex[16] = {
289 '0', '1', '2', '3', '4', '5', '6', '7',
290 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
291 u_char psline[MAX_LINE_LENGTH];
293 memset (line, ' ', sizeof line);
294 line[sizeof (line)-1] = 0;
295 for (ad=i=j=k=0; i<length; i++) {
297 line[j++] = binhex[c>>4];
298 line[j++] = binhex[c&0xf];
300 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
301 if ((i & 15) == 15) {
302 ps_clean_string(psline, line, MAX_LINE_LENGTH);
303 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
304 memset (line, ' ', sizeof line);
305 line[sizeof (line)-1] = j = k = 0;
310 if (line[0] != ' ') {
311 ps_clean_string(psline, line, MAX_LINE_LENGTH);
312 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);