2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.24 1999/11/22 08:03:31 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>
46 static void proto_tree_print_node_text(GNode *node, gpointer data);
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 print_hex_data_ps(FILE *fh, register const u_char *cp,
51 register u_int length, char_enc encoding);
52 static void print_ps_file(FILE* target_fh, FILE* source_fh);
53 static void print_text_file(FILE* target_fh, FILE* source_fh);
55 extern int proto_data; /* in packet-data.c */
62 gboolean print_all_levels;
63 gboolean print_hex_for_data;
67 FILE *open_print_dest(int to_file, const char *dest)
71 /* Open the file or command for output */
73 fh = fopen(dest, "w");
75 fh = popen(dest, "w");
80 void close_print_dest(int to_file, FILE *fh)
82 /* Close the file or command */
89 void print_preamble(FILE *fh)
91 if (prefs.pr_format == PR_FMT_PS)
92 print_ps_preamble(fh);
95 void print_finale(FILE *fh)
97 if (prefs.pr_format == PR_FMT_PS)
101 void print_file(FILE* fh, const char* filename)
103 FILE* fh2 = fopen(filename, "r");
105 fprintf(stderr, "Could not open file %s for reading.\n", filename);
109 if (prefs.pr_format == PR_FMT_PS)
110 print_ps_file(fh, fh2);
112 print_text_file(fh, fh2);
115 void proto_tree_print(gboolean print_one_packet, print_args_t *print_args,
116 GNode *protocol_tree, const u_char *pd, frame_data *fd, FILE *fh)
120 /* Create the output */
124 data.encoding = fd->encoding;
125 data.print_all_levels = print_args->expand_all;
126 data.print_hex_for_data = !print_args->print_hex;
127 /* If we're printing the entire packet in hex, don't
128 print uninterpreted data fields in hex as well. */
130 /* XXX - printing multiple frames in PostScript looks as if it's
131 tricky - you have to deal with page boundaries, I think -
132 and I'll have to spend some time learning enough about
133 PostScript to figure it out, so, for now, we only print
134 multiple frames as text. */
135 if (prefs.pr_format == PR_FMT_TEXT || !print_one_packet) {
136 g_node_children_foreach((GNode*) protocol_tree, G_TRAVERSE_ALL,
137 proto_tree_print_node_text, &data);
139 g_node_children_foreach((GNode*) protocol_tree, G_TRAVERSE_ALL,
140 proto_tree_print_node_ps, &data);
144 /* Print a tree's data, and any child nodes, in plain text */
146 void proto_tree_print_node_text(GNode *node, gpointer data)
148 field_info *fi = (field_info*) (node->data);
149 print_data *pdata = (print_data*) data;
153 gchar label_str[ITEM_LABEL_LENGTH];
156 /* Don't print invisible entries. */
160 /* was a free format label produced? */
161 if (fi->representation) {
162 label_ptr = fi->representation;
164 else { /* no, make a generic label */
165 label_ptr = label_str;
166 proto_item_fill_label(fi, label_str);
169 /* Prepare the tabs for printing, depending on tree level */
170 num_spaces = pdata->level * 4;
171 if (num_spaces > 40) {
174 for (i = 0; i < num_spaces; i++) {
177 /* The string is NUL-terminated */
178 space[num_spaces] = 0;
181 fprintf(pdata->fh, "%s%s\n", space, label_ptr);
183 /* If it's uninterpreted data, dump it (unless our caller will
184 be printing the entire packet in hex). */
185 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data)
186 print_hex_data(pdata->fh, &pdata->pd[fi->start], fi->length,
189 /* If we're printing all levels, or if this level is expanded,
190 recurse into the subtree, if it exists. */
191 if (pdata->print_all_levels || tree_is_expanded[fi->tree_type]) {
192 if (g_node_n_children(node) > 0) {
194 g_node_children_foreach(node, G_TRAVERSE_ALL,
195 proto_tree_print_node_text, pdata);
201 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
202 only slightly modified for ethereal by Gilbert Ramirez. */
203 void print_hex_data(FILE *fh, register const u_char *cp, register u_int length,
206 register int ad, i, j, k;
209 static u_char binhex[16] = {
210 '0', '1', '2', '3', '4', '5', '6', '7',
211 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
213 memset (line, ' ', sizeof line);
214 line[sizeof (line)-1] = 0;
215 for (ad=i=j=k=0; i<length; i++) {
217 line[j++] = binhex[c>>4];
218 line[j++] = binhex[c&0xf];
220 if (encoding == CHAR_EBCDIC) {
221 c = EBCDIC_to_ASCII1(c);
223 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
224 if ((i & 15) == 15) {
225 fprintf (fh, "\n%4x %s", ad, line);
226 /*if (i==15) printf (" %d", length);*/
227 memset (line, ' ', sizeof line);
228 line[sizeof (line)-1] = j = k = 0;
233 if (line[0] != ' ') fprintf (fh, "\n%4x %s", ad, line);
239 #define MAX_LINE_LENGTH 256
241 /* Print a node's data, and any child nodes, in PostScript */
243 void proto_tree_print_node_ps(GNode *node, gpointer data)
245 field_info *fi = (field_info*) (node->data);
246 print_data *pdata = (print_data*) data;
247 gchar label_str[ITEM_LABEL_LENGTH];
249 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
254 /* was a free format label produced? */
255 if (fi->representation) {
256 label_ptr = fi->representation;
258 else { /* no, make a generic label */
259 label_ptr = label_str;
260 proto_item_fill_label(fi, label_str);
264 ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
265 fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
267 /* If it's uninterpreted data, dump it. */
268 if (fi->hfinfo->id == proto_data) {
269 print_ps_hex(pdata->fh);
270 print_hex_data_ps(pdata->fh, &pdata->pd[fi->start], fi->length,
274 /* Recurse into the subtree, if it exists */
275 if (g_node_n_children(node) > 0) {
277 g_node_children_foreach(node, G_TRAVERSE_ALL,
278 proto_tree_print_node_ps, pdata);
284 void ps_clean_string(unsigned char *out, const unsigned char *in,
290 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
312 void print_hex_data_ps(FILE *fh, register const u_char *cp, register u_int length,
315 register int ad, i, j, k;
318 static u_char binhex[16] = {
319 '0', '1', '2', '3', '4', '5', '6', '7',
320 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
321 u_char psline[MAX_LINE_LENGTH];
323 memset (line, ' ', sizeof line);
324 line[sizeof (line)-1] = 0;
325 for (ad=i=j=k=0; i<length; i++) {
327 line[j++] = binhex[c>>4];
328 line[j++] = binhex[c&0xf];
330 if (encoding == CHAR_EBCDIC) {
331 c = EBCDIC_to_ASCII1(c);
333 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
334 if ((i & 15) == 15) {
335 ps_clean_string(psline, line, MAX_LINE_LENGTH);
336 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
337 memset (line, ' ', sizeof line);
338 line[sizeof (line)-1] = j = k = 0;
343 if (line[0] != ' ') {
344 ps_clean_string(psline, line, MAX_LINE_LENGTH);
345 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
352 void print_text_file(FILE* target_fh, FILE* source_fh)
354 gchar buffer[MAX_LINE_LENGTH];
355 while (fgets(buffer, sizeof(buffer), source_fh) != NULL) {
356 fputs(buffer, target_fh);
361 void print_ps_file(FILE* target_fh, FILE* source_fh)
363 gchar buffer[MAX_LINE_LENGTH];
364 gchar ps_buffer[MAX_LINE_LENGTH];
366 while (fgets(buffer, sizeof(buffer), source_fh) != NULL) {
367 ps_clean_string(ps_buffer, buffer, MAX_LINE_LENGTH);
368 fputs(ps_buffer, target_fh);