2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.45 2002/04/02 05:07:36 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/packet.h>
42 #include <epan/tvbuff.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(print_args_t *print_args,
100 GNode *protocol_tree, frame_data *fd, FILE *fh)
104 /* Create the output */
107 data.src_list = fd->data_src;
108 data.encoding = fd->flags.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);
124 * Find the data source for a specified field, and return a pointer
127 static const guint8 *
128 get_field_data(GSList *src_list, field_info *fi)
134 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
137 if (fi->ds_tvb == src_tvb) {
141 return tvb_get_ptr(src_tvb, fi->start, fi->length);
144 g_assert_not_reached();
145 return NULL; /* not found */
148 #define MAX_INDENT 160
150 /* Print a tree's data, and any child nodes, in plain text */
152 void proto_tree_print_node_text(GNode *node, gpointer data)
154 field_info *fi = PITEM_FINFO(node);
155 print_data *pdata = (print_data*) data;
158 char space[MAX_INDENT+1];
160 gchar label_str[ITEM_LABEL_LENGTH];
163 /* Don't print invisible entries. */
167 /* was a free format label produced? */
168 if (fi->representation) {
169 label_ptr = fi->representation;
171 else { /* no, make a generic label */
172 label_ptr = label_str;
173 proto_item_fill_label(fi, label_str);
176 /* Prepare the tabs for printing, depending on tree level */
177 num_spaces = pdata->level * 4;
178 if (num_spaces > MAX_INDENT) {
179 num_spaces = MAX_INDENT;
181 for (i = 0; i < num_spaces; i++) {
184 /* The string is NUL-terminated */
185 space[num_spaces] = '\0';
188 fprintf(pdata->fh, "%s%s\n", space, label_ptr);
190 /* If it's uninterpreted data, dump it (unless our caller will
191 be printing the entire packet in hex). */
192 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
194 * Find the data for this field.
196 pd = get_field_data(pdata->src_list, fi);
197 print_hex_data_text(pdata->fh, pd, fi->length, pdata->encoding);
200 /* If we're printing all levels, or if this node is one with a
201 subtree and its subtree is expanded, recurse into the subtree,
203 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
204 if (pdata->print_all_levels ||
205 (fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
206 if (g_node_n_children(node) > 0) {
208 g_node_children_foreach(node, G_TRAVERSE_ALL,
209 proto_tree_print_node_text, pdata);
215 void print_hex_data(FILE *fh, gint format, frame_data *fd)
217 gboolean multiple_sources;
227 * Set "multiple_sources" iff this frame has more than one
228 * data source; if it does, we need to print the name of
229 * the data source before printing the data from the
232 multiple_sources = (fd->data_src->next != NULL);
234 for (src_le = fd->data_src; src_le != NULL; src_le = src_le->next) {
237 if (multiple_sources) {
239 print_line(fh, format, "\n");
240 line = g_malloc(strlen(name) + 3); /* <name>:\n\0 */
243 print_line(fh, format, line);
246 length = tvb_length(tvb);
247 cp = tvb_get_ptr(tvb, 0, length);
248 if (format == PR_FMT_PS)
249 print_hex_data_ps(fh, cp, length, fd->flags.encoding);
251 print_hex_data_text(fh, cp, length, fd->flags.encoding);
255 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
256 only slightly modified for ethereal by Gilbert Ramirez. */
258 void print_hex_data_text(FILE *fh, register const u_char *cp,
259 register u_int length, char_enc encoding)
261 register unsigned int ad, i, j, k;
263 u_char line[50+16+1];
264 static u_char binhex[16] = {
265 '0', '1', '2', '3', '4', '5', '6', '7',
266 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
268 memset (line, ' ', sizeof line);
269 line[sizeof (line)-1] = 0;
270 for (ad=i=j=k=0; i<length; i++) {
272 line[j++] = binhex[c>>4];
273 line[j++] = binhex[c&0xf];
275 if (encoding == CHAR_EBCDIC) {
276 c = EBCDIC_to_ASCII1(c);
278 line[50+k++] = c >= ' ' && c < 0x7f ? c : '.';
279 if ((i & 15) == 15) {
280 fprintf (fh, "\n%04x %s", ad, line);
281 /*if (i==15) printf (" %d", length);*/
282 memset (line, ' ', sizeof line);
283 line[sizeof (line)-1] = j = k = 0;
288 if (line[0] != ' ') fprintf (fh, "\n%04x %s", ad, line);
294 #define MAX_LINE_LENGTH 256
296 /* Print a node's data, and any child nodes, in PostScript */
298 void proto_tree_print_node_ps(GNode *node, gpointer data)
300 field_info *fi = PITEM_FINFO(node);
301 print_data *pdata = (print_data*) data;
302 gchar label_str[ITEM_LABEL_LENGTH];
305 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
310 /* was a free format label produced? */
311 if (fi->representation) {
312 label_ptr = fi->representation;
314 else { /* no, make a generic label */
315 label_ptr = label_str;
316 proto_item_fill_label(fi, label_str);
320 ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
321 fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
323 /* If it's uninterpreted data, dump it (unless our caller will
324 be printing the entire packet in hex). */
325 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
327 * Find the data for this field.
329 pd = get_field_data(pdata->src_list, fi);
330 print_hex_data_ps(pdata->fh, pd, fi->length, pdata->encoding);
333 /* Recurse into the subtree, if it exists */
334 if (g_node_n_children(node) > 0) {
336 g_node_children_foreach(node, G_TRAVERSE_ALL,
337 proto_tree_print_node_ps, pdata);
343 void ps_clean_string(unsigned char *out, const unsigned char *in,
349 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
371 void print_hex_data_ps(FILE *fh, register const u_char *cp,
372 register u_int length, char_enc encoding)
374 register unsigned int ad, i, j, k;
377 static u_char binhex[16] = {
378 '0', '1', '2', '3', '4', '5', '6', '7',
379 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
380 u_char psline[MAX_LINE_LENGTH];
383 memset (line, ' ', sizeof line);
384 line[sizeof (line)-1] = 0;
385 for (ad=i=j=k=0; i<length; i++) {
387 line[j++] = binhex[c>>4];
388 line[j++] = binhex[c&0xf];
390 if (encoding == CHAR_EBCDIC) {
391 c = EBCDIC_to_ASCII1(c);
393 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
394 if ((i & 15) == 15) {
395 ps_clean_string(psline, line, MAX_LINE_LENGTH);
396 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
397 memset (line, ' ', sizeof line);
398 line[sizeof (line)-1] = j = k = 0;
403 if (line[0] != ' ') {
404 ps_clean_string(psline, line, MAX_LINE_LENGTH);
405 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
411 void print_line(FILE *fh, gint format, char *line)
413 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
415 if (format == PR_FMT_PS) {
416 ps_clean_string(psbuffer, line, MAX_LINE_LENGTH);
417 fprintf(fh, "(%s) hexdump\n", psbuffer);