2 * Routines for printing packet analysis trees.
6 * Gilbert Ramirez <gram@alumni.rice.edu>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
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 #include <epan/epan.h>
35 #include <epan/epan_dissect.h>
36 #include <epan/tvbuff.h>
37 #include <epan/packet.h>
38 #include <epan/emem.h>
40 #include "packet-range.h"
43 #include "file_util.h"
44 #include <epan/charsets.h>
45 #include <epan/dissectors/packet-data.h>
46 #include <epan/dissectors/packet-frame.h>
48 #define PDML_VERSION "0"
49 #define PSML_VERSION "0"
53 print_stream_t *stream;
56 print_dissections_e print_dissections;
57 gboolean print_hex_for_data;
70 output_fields_t* fields;
74 struct _output_fields {
75 gboolean print_header;
78 GHashTable* field_indicies;
79 const gchar** field_values;
83 static const gchar* get_field_hex_value(GSList* src_list, field_info *fi);
84 static const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt);
85 static void proto_tree_print_node(proto_node *node, gpointer data);
86 static void proto_tree_write_node_pdml(proto_node *node, gpointer data);
87 static const guint8 *get_field_data(GSList *src_list, field_info *fi);
88 static void write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi);
89 static gboolean print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
90 guint length, char_enc encoding);
91 static void ps_clean_string(unsigned char *out, const unsigned char *in,
93 static void print_escaped_xml(FILE *fh, const char *unescaped_string);
95 static void print_pdml_geninfo(proto_tree *tree, FILE *fh);
97 static void proto_tree_get_node_field_values(proto_node *node, gpointer data);
100 open_print_dest(int to_file, const char *dest)
104 /* Open the file or command for output */
106 fh = eth_fopen(dest, "w");
108 fh = popen(dest, "w");
114 close_print_dest(int to_file, FILE *fh)
116 /* Close the file or command */
118 return (fclose(fh) == 0);
120 return (pclose(fh) == 0);
123 #define MAX_PS_LINE_LENGTH 256
126 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
127 print_stream_t *stream)
131 /* Create the output */
133 data.stream = stream;
135 data.src_list = edt->pi.data_src;
136 data.encoding = edt->pi.fd->flags.encoding;
137 data.print_dissections = print_args->print_dissections;
138 /* If we're printing the entire packet in hex, don't
139 print uninterpreted data fields in hex as well. */
140 data.print_hex_for_data = !print_args->print_hex;
143 proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
147 #define MAX_INDENT 160
149 /* Print a tree's data, and any child nodes. */
151 void proto_tree_print_node(proto_node *node, gpointer data)
153 field_info *fi = PITEM_FINFO(node);
154 print_data *pdata = (print_data*) data;
156 gchar label_str[ITEM_LABEL_LENGTH];
159 /* Don't print invisible entries. */
160 if (PROTO_ITEM_IS_HIDDEN(node))
163 /* Give up if we've already gotten an error. */
167 /* was a free format label produced? */
169 label_ptr = fi->rep->representation;
171 else { /* no, make a generic label */
172 label_ptr = label_str;
173 proto_item_fill_label(fi, label_str);
176 if (PROTO_ITEM_IS_GENERATED(node)) {
177 label_ptr = g_strdup_printf("[%s]", label_ptr);
180 if (!print_line(pdata->stream, pdata->level, label_ptr)) {
181 pdata->success = FALSE;
185 if (PROTO_ITEM_IS_GENERATED(node)) {
189 /* If it's uninterpreted data, dump it (unless our caller will
190 be printing the entire packet in hex). */
191 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
193 * Find the data for this field.
195 pd = get_field_data(pdata->src_list, fi);
197 if (!print_hex_data_buffer(pdata->stream, pd,
198 fi->length, pdata->encoding)) {
199 pdata->success = FALSE;
205 /* If we're printing all levels, or if this node is one with a
206 subtree and its subtree is expanded, recurse into the subtree,
208 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
209 if (pdata->print_dissections == print_dissections_expanded ||
210 (pdata->print_dissections == print_dissections_as_displayed &&
211 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
212 if (node->first_child != NULL) {
214 proto_tree_children_foreach(node,
215 proto_tree_print_node, pdata);
224 write_pdml_preamble(FILE *fh)
226 fputs("<?xml version=\"1.0\"?>\n", fh);
227 fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
228 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
232 proto_tree_write_pdml(epan_dissect_t *edt, FILE *fh)
234 write_pdml_data data;
236 /* Create the output */
239 data.src_list = edt->pi.data_src;
242 fprintf(fh, "<packet>\n");
244 /* Print a "geninfo" protocol as required by PDML */
245 print_pdml_geninfo(edt->tree, fh);
247 proto_tree_children_foreach(edt->tree, proto_tree_write_node_pdml,
250 fprintf(fh, "</packet>\n\n");
253 /* Write out a tree's data, and any child nodes, as PDML */
255 proto_tree_write_node_pdml(proto_node *node, gpointer data)
257 field_info *fi = PITEM_FINFO(node);
258 write_pdml_data *pdata = (write_pdml_data*) data;
259 const gchar *label_ptr;
260 gchar label_str[ITEM_LABEL_LENGTH];
261 char *dfilter_string;
265 for (i = -1; i < pdata->level; i++) {
266 fputs(" ", pdata->fh);
269 /* Text label. It's printed as a field with no name. */
270 if (fi->hfinfo->id == hf_text_only) {
273 label_ptr = fi->rep->representation;
279 /* Show empty name since it is a required field */
280 fputs("<field name=\"", pdata->fh);
281 fputs("\" show=\"", pdata->fh);
282 print_escaped_xml(pdata->fh, label_ptr);
284 fprintf(pdata->fh, "\" size=\"%d", fi->length);
285 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
287 fputs("\" value=\"", pdata->fh);
288 write_pdml_field_hex_value(pdata, fi);
290 if (node->first_child != NULL) {
291 fputs("\">\n", pdata->fh);
294 fputs("\"/>\n", pdata->fh);
297 /* Uninterpreted data, i.e., the "Data" protocol, is
298 * printed as a field instead of a protocol. */
299 else if (fi->hfinfo->id == proto_data) {
301 fputs("<field name=\"data\" value=\"", pdata->fh);
303 write_pdml_field_hex_value(pdata, fi);
305 fputs("\"/>\n", pdata->fh);
308 /* Normal protocols and fields */
310 if (fi->hfinfo->type == FT_PROTOCOL) {
311 fputs("<proto name=\"", pdata->fh);
314 fputs("<field name=\"", pdata->fh);
316 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
320 * http://analyzer.polito.it/30alpha/docs/dissectors/PDMLSpec.htm
322 * the show fields contains things in 'human readable' format
323 * showname: contains only the name of the field
324 * show: contains only the data of the field
325 * showdtl: contains additional details of the field data
326 * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
328 * XXX - the showname shouldn't contain the field data itself
329 * (like it's contained in the fi->rep->representation).
330 * Unfortunately, we don't have the field data representation for
331 * all fields, so this isn't currently possible */
332 fputs("\" showname=\"", pdata->fh);
333 print_escaped_xml(pdata->fh, fi->hfinfo->name);
337 fputs("\" showname=\"", pdata->fh);
338 print_escaped_xml(pdata->fh, fi->rep->representation);
341 label_ptr = label_str;
342 proto_item_fill_label(fi, label_str);
343 fputs("\" showname=\"", pdata->fh);
344 print_escaped_xml(pdata->fh, label_ptr);
347 if (PROTO_ITEM_IS_HIDDEN(node))
348 fprintf(pdata->fh, "\" hide=\"yes");
350 fprintf(pdata->fh, "\" size=\"%d", fi->length);
351 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
352 /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
354 /* show, value, and unmaskedvalue attributes */
355 switch (fi->hfinfo->type)
360 fputs("\" show=\"\" value=\"", pdata->fh);
363 /* XXX - this is a hack until we can just call
364 * fvalue_to_string_repr() for *all* FT_* types. */
365 dfilter_string = proto_construct_match_selected_string(fi,
367 if (dfilter_string != NULL) {
368 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
370 /* XXX - Remove double-quotes. Again, once we
371 * can call fvalue_to_string_repr(), we can
372 * ask it not to produce the version for
373 * display-filters, and thus, no
375 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
376 dfilter_string[strlen(dfilter_string)-1] = '\0';
380 fputs("\" show=\"", pdata->fh);
381 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
385 * XXX - should we omit "value" for any fields?
386 * What should we do for fields whose length is 0?
387 * They might come from a pseudo-header or from
388 * the capture header (e.g., time stamps), or
389 * they might be generated fields.
391 if (fi->length > 0) {
392 fputs("\" value=\"", pdata->fh);
394 if (fi->hfinfo->bitmask!=0) {
395 fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
396 fputs("\" unmaskedvalue=\"", pdata->fh);
397 write_pdml_field_hex_value(pdata, fi);
400 write_pdml_field_hex_value(pdata, fi);
405 if (node->first_child != NULL) {
406 fputs("\">\n", pdata->fh);
408 else if (fi->hfinfo->id == proto_data) {
409 fputs("\">\n", pdata->fh);
412 fputs("\"/>\n", pdata->fh);
416 /* We always print all levels for PDML. Recurse here. */
417 if (node->first_child != NULL) {
419 proto_tree_children_foreach(node,
420 proto_tree_write_node_pdml, pdata);
424 if (node->first_child != NULL) {
425 for (i = -1; i < pdata->level; i++) {
426 fputs(" ", pdata->fh);
428 if (fi->hfinfo->id != proto_data) { /* Data protocol uses simple tags */
429 if (fi->hfinfo->type == FT_PROTOCOL) {
430 fputs("</proto>\n", pdata->fh);
433 fputs("</field>\n", pdata->fh);
439 /* Print info for a 'geninfo' pseudo-protocol. This is required by
440 * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
441 * but we produce a 'geninfo' protocol in the PDML to conform to spec.
442 * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
444 print_pdml_geninfo(proto_tree *tree, FILE *fh)
446 guint32 num, len, caplen;
448 GPtrArray *finfo_array;
449 field_info *frame_finfo;
451 /* Get frame protocol's finfo. */
452 finfo_array = proto_find_finfo(tree, proto_frame);
453 if (g_ptr_array_len(finfo_array) < 1) {
456 frame_finfo = finfo_array->pdata[0];
457 g_ptr_array_free(finfo_array, FALSE);
459 /* frame.number --> geninfo.num */
460 finfo_array = proto_find_finfo(tree, hf_frame_number);
461 if (g_ptr_array_len(finfo_array) < 1) {
464 num = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
465 g_ptr_array_free(finfo_array, FALSE);
467 /* frame.pkt_len --> geninfo.len */
468 finfo_array = proto_find_finfo(tree, hf_frame_packet_len);
469 if (g_ptr_array_len(finfo_array) < 1) {
472 len = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
473 g_ptr_array_free(finfo_array, FALSE);
475 /* frame.cap_len --> geninfo.caplen */
476 finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
477 if (g_ptr_array_len(finfo_array) < 1) {
480 caplen = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
481 g_ptr_array_free(finfo_array, FALSE);
483 /* frame.time --> geninfo.timestamp */
484 finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
485 if (g_ptr_array_len(finfo_array) < 1) {
488 timestamp = fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
489 g_ptr_array_free(finfo_array, FALSE);
491 /* Print geninfo start */
493 " <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
494 frame_finfo->length);
496 /* Print geninfo.num */
498 " <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
499 num, num, frame_finfo->length);
501 /* Print geninfo.len */
503 " <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Packet Length\" value=\"%x\" size=\"%u\"/>\n",
504 len, len, frame_finfo->length);
506 /* Print geninfo.caplen */
508 " <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
509 caplen, caplen, frame_finfo->length);
511 /* Print geninfo.timestamp */
513 " <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
514 abs_time_to_str(timestamp), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
516 /* Print geninfo end */
522 write_pdml_finale(FILE *fh)
524 fputs("</pdml>\n", fh);
528 write_psml_preamble(FILE *fh)
530 fputs("<?xml version=\"1.0\"?>\n", fh);
531 fputs("<psml version=\"" PSML_VERSION "\" ", fh);
532 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
536 proto_tree_write_psml(epan_dissect_t *edt, FILE *fh)
540 /* if this is the first packet, we have to create the PSML structure output */
541 if(edt->pi.fd->num == 1) {
542 fprintf(fh, "<structure>\n");
544 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
545 fprintf(fh, "<section>");
546 print_escaped_xml(fh, edt->pi.cinfo->col_title[i]);
547 fprintf(fh, "</section>\n");
550 fprintf(fh, "</structure>\n\n");
553 fprintf(fh, "<packet>\n");
555 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
556 fprintf(fh, "<section>");
557 print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
558 fprintf(fh, "</section>\n");
561 fprintf(fh, "</packet>\n\n");
565 write_psml_finale(FILE *fh)
567 fputs("</psml>\n", fh);
571 write_csv_preamble(FILE *fh _U_)
577 proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
581 /* if this is the first packet, we have to write the CSV header */
582 if(edt->pi.fd->num == 1) {
583 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
584 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_title[i]);
586 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_title[i]);
589 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
590 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_data[i]);
592 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_data[i]);
596 write_csv_finale(FILE *fh _U_)
602 * Find the data source for a specified field, and return a pointer
603 * to the data in it. Returns NULL if the data is out of bounds.
605 static const guint8 *
606 get_field_data(GSList *src_list, field_info *fi)
611 gint length, tvbuff_length;
613 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
616 if (fi->ds_tvb == src_tvb) {
620 * XXX - a field can have a length that runs past
621 * the end of the tvbuff. Ideally, that should
622 * be fixed when adding an item to the protocol
623 * tree, but checking the length when doing
624 * that could be expensive. Until we fix that,
625 * we'll do the check here.
627 tvbuff_length = tvb_length_remaining(src_tvb,
629 if (tvbuff_length < 0) {
633 if (length > tvbuff_length)
634 length = tvbuff_length;
635 return tvb_get_ptr(src_tvb, fi->start, length);
638 g_assert_not_reached();
639 return NULL; /* not found */
642 /* Print a string, escaping out certain characters that need to
643 * escaped out for XML. */
645 print_escaped_xml(FILE *fh, const char *unescaped_string)
649 for (p = unescaped_string; *p != '\0'; p++) {
673 write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi)
678 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
679 fprintf(pdata->fh, "field length invalid!");
683 /* Find the data for this field. */
684 pd = get_field_data(pdata->src_list, fi);
687 /* Print a simple hex dump */
688 for (i = 0 ; i < fi->length; i++) {
689 fprintf(pdata->fh, "%02x", pd[i]);
695 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
697 gboolean multiple_sources;
707 * Set "multiple_sources" iff this frame has more than one
708 * data source; if it does, we need to print the name of
709 * the data source before printing the data from the
712 multiple_sources = (edt->pi.data_src->next != NULL);
714 for (src_le = edt->pi.data_src; src_le != NULL;
715 src_le = src_le->next) {
718 if (multiple_sources) {
720 print_line(stream, 0, "");
721 line = g_malloc(strlen(name) + 2); /* <name>:\0 */
724 print_line(stream, 0, line);
727 length = tvb_length(tvb);
730 cp = tvb_get_ptr(tvb, 0, length);
731 if (!print_hex_data_buffer(stream, cp, length,
732 edt->pi.fd->flags.encoding))
739 * This routine is based on a routine created by Dan Lasley
740 * <DLASLEY@PROMUS.com>.
742 * It was modified for Wireshark by Gilbert Ramirez and others.
745 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
746 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
747 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
748 /* max number of characters hex dump takes -
749 2 digits plus trailing blank */
750 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
751 /* number of characters those bytes take;
752 3 characters per byte of hex dump,
753 2 blanks separating hex from ASCII,
754 1 character per byte of ASCII dump */
755 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
756 /* number of characters per line;
757 offset, 2 blanks separating offset
758 from data dump, data dump */
761 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
762 guint length, char_enc encoding)
764 register unsigned int ad, i, j, k, l;
766 guchar line[MAX_LINE_LEN + 1];
767 unsigned int use_digits;
768 static guchar binhex[16] = {
769 '0', '1', '2', '3', '4', '5', '6', '7',
770 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
772 if (!print_line(stream, 0, ""))
776 * How many of the leading digits of the offset will we supply?
777 * We always supply at least 4 digits, but if the maximum offset
778 * won't fit in 4 digits, we use as many digits as will be needed.
780 if (((length - 1) & 0xF0000000) != 0)
781 use_digits = 8; /* need all 8 digits */
782 else if (((length - 1) & 0x0F000000) != 0)
783 use_digits = 7; /* need 7 digits */
784 else if (((length - 1) & 0x00F00000) != 0)
785 use_digits = 6; /* need 6 digits */
786 else if (((length - 1) & 0x000F0000) != 0)
787 use_digits = 5; /* need 5 digits */
789 use_digits = 4; /* we'll supply 4 digits */
798 * Start of a new line.
805 c = (ad >> (l*4)) & 0xF;
806 line[j++] = binhex[c];
810 memset(line+j, ' ', DATA_DUMP_LEN);
813 * Offset in line of ASCII dump.
815 k = j + HEX_DUMP_LEN + 2;
818 line[j++] = binhex[c>>4];
819 line[j++] = binhex[c&0xf];
821 if (encoding == CHAR_EBCDIC) {
822 c = EBCDIC_to_ASCII1(c);
824 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
826 if ((i & 15) == 0 || i == length) {
828 * We'll be starting a new line, or
829 * we're finished printing this buffer;
830 * dump out the line we've constructed,
831 * and advance the offset.
834 if (!print_line(stream, 0, line))
843 void ps_clean_string(unsigned char *out, const unsigned char *in,
849 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
870 /* Some formats need stuff at the beginning of the output */
872 print_preamble(print_stream_t *self, gchar *filename)
874 return (self->ops->print_preamble)(self, filename);
878 print_line(print_stream_t *self, int indent, const char *line)
880 return (self->ops->print_line)(self, indent, line);
883 /* Insert bookmark */
885 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
887 return (self->ops->print_bookmark)(self, name, title);
891 new_page(print_stream_t *self)
893 return (self->ops->new_page)(self);
896 /* Some formats need stuff at the end of the output */
898 print_finale(print_stream_t *self)
900 return (self->ops->print_finale)(self);
904 destroy_print_stream(print_stream_t *self)
906 return (self->ops->destroy)(self);
915 print_preamble_text(print_stream_t *self _U_, gchar *filename _U_)
918 return TRUE; /* always succeeds */
922 print_line_text(print_stream_t *self, int indent, const char *line)
924 output_text *output = self->data;
925 char space[MAX_INDENT+1];
929 /* Prepare the tabs for printing, depending on tree level */
930 num_spaces = indent * 4;
931 if (num_spaces > MAX_INDENT) {
932 num_spaces = MAX_INDENT;
934 for (i = 0; i < num_spaces; i++) {
937 /* The string is NUL-terminated */
938 space[num_spaces] = '\0';
940 fputs(space, output->fh);
941 fputs(line, output->fh);
942 putc('\n', output->fh);
943 return !ferror(output->fh);
947 print_bookmark_text(print_stream_t *self _U_, const gchar *name _U_,
948 const gchar *title _U_)
955 new_page_text(print_stream_t *self)
957 output_text *output = self->data;
959 fputs("\f", output->fh);
960 return !ferror(output->fh);
964 print_finale_text(print_stream_t *self _U_)
967 return TRUE; /* always succeeds */
971 destroy_text(print_stream_t *self)
973 output_text *output = self->data;
976 ret = close_print_dest(output->to_file, output->fh);
982 static const print_stream_ops_t print_text_ops = {
992 print_stream_text_new(int to_file, const char *dest)
995 print_stream_t *stream;
998 fh = open_print_dest(to_file, dest);
1002 output = g_malloc(sizeof *output);
1003 output->to_file = to_file;
1005 stream = g_malloc(sizeof (print_stream_t));
1006 stream->ops = &print_text_ops;
1007 stream->data = output;
1013 print_stream_text_stdio_new(FILE *fh)
1015 print_stream_t *stream;
1016 output_text *output;
1018 output = g_malloc(sizeof *output);
1019 output->to_file = TRUE;
1021 stream = g_malloc(sizeof (print_stream_t));
1022 stream->ops = &print_text_ops;
1023 stream->data = output;
1034 print_preamble_ps(print_stream_t *self, gchar *filename)
1036 output_ps *output = self->data;
1037 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1039 print_ps_preamble(output->fh);
1041 fputs("%% Set the font to 10 point\n", output->fh);
1042 fputs("/Courier findfont 10 scalefont setfont\n", output->fh);
1043 fputs("\n", output->fh);
1044 fputs("%% the page title\n", output->fh);
1045 ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
1046 fprintf(output->fh, "/eth_pagetitle (%s - Wireshark) def\n", psbuffer);
1047 fputs("\n", output->fh);
1048 return !ferror(output->fh);
1052 print_line_ps(print_stream_t *self, int indent, const char *line)
1054 output_ps *output = self->data;
1055 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1057 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
1058 fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
1059 return !ferror(output->fh);
1063 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
1065 output_ps *output = self->data;
1066 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1069 * See the Adobe "pdfmark reference":
1071 * http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
1073 * The pdfmark stuff tells code that turns PostScript into PDF
1074 * things that it should do.
1076 * The /OUT stuff creates a bookmark that goes to the
1077 * destination with "name" as the name and "title" as the title.
1079 * The "/DEST" creates the destination.
1081 ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
1082 fprintf(output->fh, "[/Dest /%s /Title (%s) /OUT pdfmark\n", name,
1084 fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
1086 fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
1088 fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
1089 return !ferror(output->fh);
1093 new_page_ps(print_stream_t *self)
1095 output_ps *output = self->data;
1097 fputs("formfeed\n", output->fh);
1098 return !ferror(output->fh);
1102 print_finale_ps(print_stream_t *self)
1104 output_ps *output = self->data;
1106 print_ps_finale(output->fh);
1107 return !ferror(output->fh);
1111 destroy_ps(print_stream_t *self)
1113 output_ps *output = self->data;
1116 ret = close_print_dest(output->to_file, output->fh);
1122 static const print_stream_ops_t print_ps_ops = {
1132 print_stream_ps_new(int to_file, const char *dest)
1135 print_stream_t *stream;
1138 fh = open_print_dest(to_file, dest);
1142 output = g_malloc(sizeof *output);
1143 output->to_file = to_file;
1145 stream = g_malloc(sizeof (print_stream_t));
1146 stream->ops = &print_ps_ops;
1147 stream->data = output;
1153 print_stream_ps_stdio_new(FILE *fh)
1155 print_stream_t *stream;
1158 output = g_malloc(sizeof *output);
1159 output->to_file = TRUE;
1161 stream = g_malloc(sizeof (print_stream_t));
1162 stream->ops = &print_ps_ops;
1163 stream->data = output;
1168 output_fields_t* output_fields_new()
1170 output_fields_t* fields = g_new(output_fields_t, 1);
1171 fields->print_header = FALSE;
1172 fields->separator = '\t';
1173 fields->fields = NULL; /*Do lazy initialisation */
1174 fields->field_indicies = NULL;
1175 fields->field_values = NULL;
1180 gsize output_fields_num_fields(output_fields_t* fields)
1184 if(NULL == fields->fields) {
1187 return fields->fields->len;
1191 void output_fields_free(output_fields_t* fields)
1195 if(NULL != fields->field_indicies) {
1196 /* Keys are stored in fields->fields, values are
1199 g_hash_table_destroy(fields->field_indicies);
1201 if(NULL != fields->fields) {
1203 for(i = 0; i < fields->fields->len; ++i) {
1204 gchar* field = g_ptr_array_index(fields->fields,i);
1207 g_ptr_array_free(fields->fields, TRUE);
1213 void output_fields_add(output_fields_t* fields, const gchar* field)
1221 if(NULL == fields->fields) {
1222 fields->fields = g_ptr_array_new();
1225 field_copy = g_strdup(field);
1227 g_ptr_array_add(fields->fields, field_copy);
1230 gboolean output_fields_set_option(output_fields_t* info, gchar* option)
1232 const gchar* option_name;
1233 const gchar* option_value;
1238 if('\0' == *option) {
1239 return FALSE; /* Is this guarded against by option parsing? */
1241 option_name = strtok(option,"=");
1242 option_value = option + strlen(option_name) + 1;
1243 if(0 == strcmp(option_name, "header")) {
1244 switch(NULL == option_value ? '\0' : *option_value) {
1246 info->print_header = FALSE;
1249 info->print_header = TRUE;
1257 if(0 == strcmp(option_name,"separator")) {
1258 switch(NULL == option_value ? '\0' : *option_value) {
1262 switch(*++option_value) {
1264 info->separator = '\t';
1267 info->separator = ' ';
1270 info->separator = '\\';
1274 info->separator = *option_value;
1280 if(0 == strcmp(option_name, "quote")) {
1281 switch(NULL == option_value ? '\0' : *option_value) {
1282 default: /* Fall through */
1302 void output_fields_list_options(FILE *fh)
1304 fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
1305 fputs("header=y|n Print field abbreviations as first line of output (def: N: no)\n", fh);
1306 fputs("separator=/t|/s|<character> Set the separator to use; \"/t\" = tab,\n \"/s\" = space (def: /t: tab)\n", fh);
1307 fputs("quote=d|s|n Print either d: double-quotes, s: single quotes or n: no quotes around field values (def: n: none)\n", fh);
1311 void write_fields_preamble(output_fields_t* fields, FILE *fh)
1318 if(!fields->print_header) {
1322 for(i = 0; i < fields->fields->len; ++i) {
1323 const gchar* field = g_ptr_array_index(fields->fields,i);
1325 fputc(fields->separator, fh);
1332 static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
1334 write_field_data_t *call_data;
1336 gpointer field_index;
1339 fi = PITEM_FINFO(node);
1341 field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
1342 if(NULL != field_index) {
1345 value = get_node_field_value(fi, call_data->edt); /* ep_alloced string */
1347 if(NULL != value && '\0' != *value) {
1349 actual_index = GPOINTER_TO_UINT(field_index);
1350 /* Unwrap change made to disambiguiate zero / null */
1351 call_data->fields->field_values[actual_index - 1] = value;
1356 if (node->first_child != NULL) {
1357 proto_tree_children_foreach(node, proto_tree_get_node_field_values,
1362 void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, FILE *fh)
1366 write_field_data_t data;
1372 data.fields = fields;
1375 if(NULL == fields->field_indicies) {
1376 /* Prepare a lookup table from string abbreviation for field to its index. */
1377 fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
1380 while( i < fields->fields->len) {
1381 gchar* field = g_ptr_array_index(fields->fields, i);
1382 /* Store field indicies +1 so that zero is not a valid value,
1383 * and can be distinguished from NULL as a pointer.
1386 g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
1390 /* Buffer to store values for this packet */
1391 fields->field_values = ep_alloc_array0(const gchar*, fields->fields->len);
1393 proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
1396 for(i = 0; i < fields->fields->len; ++i) {
1398 fputc(fields->separator, fh);
1400 if(NULL != fields->field_values[i]) {
1401 if(fields->quote != '\0') {
1402 fputc(fields->quote, fh);
1404 fputs(fields->field_values[i], fh);
1405 if(fields->quote != '\0') {
1406 fputc(fields->quote, fh);
1412 void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
1417 /* Returns an ep_alloced string or a static constant*/
1418 static const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
1420 if (fi->hfinfo->id == hf_text_only) {
1424 return fi->rep->representation;
1427 return get_field_hex_value(edt->pi.data_src, fi);
1430 else if (fi->hfinfo->id == proto_data) {
1431 /* Uninterpreted data, i.e., the "Data" protocol, is
1432 * printed as a field instead of a protocol. */
1433 return get_field_hex_value(edt->pi.data_src, fi);
1436 /* Normal protocols and fields */
1437 gchar *dfilter_string;
1440 switch (fi->hfinfo->type)
1443 /* Print out the full details for the protocol. */
1445 return fi->rep->representation;
1447 /* Just print out the protocol abbreviation */
1448 return fi->hfinfo->abbrev;;
1451 /* Return "1" so that the presence of a field of type
1452 * FT_NONE can be checked when using -T fields */
1455 /* XXX - this is a hack until we can just call
1456 * fvalue_to_string_repr() for *all* FT_* types. */
1457 dfilter_string = proto_construct_match_selected_string(fi,
1459 if (dfilter_string != NULL) {
1460 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
1462 /* XXX - Remove double-quotes. Again, once we
1463 * can call fvalue_to_string_repr(), we can
1464 * ask it not to produce the version for
1465 * display-filters, and thus, no
1467 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
1468 dfilter_string[strlen(dfilter_string)-1] = '\0';
1472 return &(dfilter_string[chop_len]);
1474 return get_field_hex_value(edt->pi.data_src, fi);
1481 get_field_hex_value(GSList* src_list, field_info *fi)
1485 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
1486 return "field length invalid!";
1489 /* Find the data for this field. */
1490 pd = get_field_data(src_list, fi);
1497 const int chars_per_byte = 2;
1499 len = chars_per_byte * fi->length;
1500 buffer = ep_alloc_array(gchar, len + 1);
1501 buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
1503 /* Print a simple hex dump */
1504 for (i = 0 ; i < fi->length; i++) {
1505 g_snprintf(p, len, "%02x", pd[i]);
1506 p += chars_per_byte;
1507 len -= chars_per_byte;