2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.28 2000/01/22 06:22:18 guy Exp $
6 * Gilbert Ramirez <gram@xiexie.org>
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.
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
44 static void proto_tree_print_node_text(GNode *node, gpointer data);
45 static void proto_tree_print_node_ps(GNode *node, gpointer data);
46 static void ps_clean_string(unsigned char *out, const unsigned char *in,
48 static void print_hex_data_text(FILE *fh, register const u_char *cp,
49 register u_int length, char_enc encoding);
50 static void print_hex_data_ps(FILE *fh, register const u_char *cp,
51 register u_int length, char_enc encoding);
53 extern int proto_data; /* in packet-data.c */
60 gboolean print_all_levels;
61 gboolean print_hex_for_data;
65 FILE *open_print_dest(int to_file, const char *dest)
69 /* Open the file or command for output */
71 fh = fopen(dest, "w");
73 fh = popen(dest, "w");
78 void close_print_dest(int to_file, FILE *fh)
80 /* Close the file or command */
87 void print_preamble(FILE *fh, gint format)
89 if (format == PR_FMT_PS)
90 print_ps_preamble(fh);
93 void print_finale(FILE *fh, gint format)
95 if (format == PR_FMT_PS)
99 void proto_tree_print(gboolean print_one_packet, print_args_t *print_args,
100 GNode *protocol_tree, const u_char *pd, frame_data *fd, FILE *fh)
104 /* Create the output */
108 data.encoding = fd->encoding;
109 data.print_all_levels = print_args->expand_all;
110 data.print_hex_for_data = !print_args->print_hex;
111 /* If we're printing the entire packet in hex, don't
112 print uninterpreted data fields in hex as well. */
114 if (print_args->format == PR_FMT_TEXT) {
115 g_node_children_foreach((GNode*) protocol_tree, G_TRAVERSE_ALL,
116 proto_tree_print_node_text, &data);
118 g_node_children_foreach((GNode*) protocol_tree, G_TRAVERSE_ALL,
119 proto_tree_print_node_ps, &data);
123 /* Print a tree's data, and any child nodes, in plain text */
125 void proto_tree_print_node_text(GNode *node, gpointer data)
127 field_info *fi = (field_info*) (node->data);
128 print_data *pdata = (print_data*) data;
132 gchar label_str[ITEM_LABEL_LENGTH];
135 /* Don't print invisible entries. */
139 /* was a free format label produced? */
140 if (fi->representation) {
141 label_ptr = fi->representation;
143 else { /* no, make a generic label */
144 label_ptr = label_str;
145 proto_item_fill_label(fi, label_str);
148 /* Prepare the tabs for printing, depending on tree level */
149 num_spaces = pdata->level * 4;
150 if (num_spaces > 40) {
153 for (i = 0; i < num_spaces; i++) {
156 /* The string is NUL-terminated */
157 space[num_spaces] = 0;
160 fprintf(pdata->fh, "%s%s\n", space, label_ptr);
162 /* If it's uninterpreted data, dump it (unless our caller will
163 be printing the entire packet in hex). */
164 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data)
165 print_hex_data_text(pdata->fh, &pdata->pd[fi->start],
166 fi->length, pdata->encoding);
168 /* If we're printing all levels, or if this level is expanded,
169 recurse into the subtree, if it exists. */
170 if (pdata->print_all_levels || tree_is_expanded[fi->tree_type]) {
171 if (g_node_n_children(node) > 0) {
173 g_node_children_foreach(node, G_TRAVERSE_ALL,
174 proto_tree_print_node_text, pdata);
180 void print_hex_data(FILE *fh, gint format, register const u_char *cp,
181 register u_int length, char_enc encoding)
183 if (format == PR_FMT_PS)
184 print_hex_data_ps(fh, cp, length, encoding);
186 print_hex_data_text(fh, cp, length, encoding);
189 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
190 only slightly modified for ethereal by Gilbert Ramirez. */
192 void print_hex_data_text(FILE *fh, register const u_char *cp,
193 register u_int length, char_enc encoding)
195 register int ad, i, j, k;
198 static u_char binhex[16] = {
199 '0', '1', '2', '3', '4', '5', '6', '7',
200 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
202 memset (line, ' ', sizeof line);
203 line[sizeof (line)-1] = 0;
204 for (ad=i=j=k=0; i<length; i++) {
206 line[j++] = binhex[c>>4];
207 line[j++] = binhex[c&0xf];
209 if (encoding == CHAR_EBCDIC) {
210 c = EBCDIC_to_ASCII1(c);
212 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
213 if ((i & 15) == 15) {
214 fprintf (fh, "\n%4x %s", ad, line);
215 /*if (i==15) printf (" %d", length);*/
216 memset (line, ' ', sizeof line);
217 line[sizeof (line)-1] = j = k = 0;
222 if (line[0] != ' ') fprintf (fh, "\n%4x %s", ad, line);
228 #define MAX_LINE_LENGTH 256
230 /* Print a node's data, and any child nodes, in PostScript */
232 void proto_tree_print_node_ps(GNode *node, gpointer data)
234 field_info *fi = (field_info*) (node->data);
235 print_data *pdata = (print_data*) data;
236 gchar label_str[ITEM_LABEL_LENGTH];
238 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
243 /* was a free format label produced? */
244 if (fi->representation) {
245 label_ptr = fi->representation;
247 else { /* no, make a generic label */
248 label_ptr = label_str;
249 proto_item_fill_label(fi, label_str);
253 ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
254 fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
256 /* If it's uninterpreted data, dump it (unless our caller will
257 be printing the entire packet in hex). */
258 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
259 print_hex_data_ps(pdata->fh, &pdata->pd[fi->start], fi->length,
263 /* Recurse into the subtree, if it exists */
264 if (g_node_n_children(node) > 0) {
266 g_node_children_foreach(node, G_TRAVERSE_ALL,
267 proto_tree_print_node_ps, pdata);
273 void ps_clean_string(unsigned char *out, const unsigned char *in,
279 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
301 void print_hex_data_ps(FILE *fh, register const u_char *cp,
302 register u_int length, char_enc encoding)
304 register int ad, i, j, k;
307 static u_char binhex[16] = {
308 '0', '1', '2', '3', '4', '5', '6', '7',
309 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
310 u_char psline[MAX_LINE_LENGTH];
313 memset (line, ' ', sizeof line);
314 line[sizeof (line)-1] = 0;
315 for (ad=i=j=k=0; i<length; i++) {
317 line[j++] = binhex[c>>4];
318 line[j++] = binhex[c&0xf];
320 if (encoding == CHAR_EBCDIC) {
321 c = EBCDIC_to_ASCII1(c);
323 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
324 if ((i & 15) == 15) {
325 ps_clean_string(psline, line, MAX_LINE_LENGTH);
326 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
327 memset (line, ' ', sizeof line);
328 line[sizeof (line)-1] = j = k = 0;
333 if (line[0] != ' ') {
334 ps_clean_string(psline, line, MAX_LINE_LENGTH);
335 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
341 void print_line(FILE *fh, gint format, char *line)
343 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
345 if (format == PR_FMT_PS) {
346 ps_clean_string(psbuffer, line, MAX_LINE_LENGTH);
347 fprintf(fh, "(%s) hexdump\n", psbuffer);