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 "version_info.h"
44 #include <wsutil/file_util.h>
45 #include <epan/charsets.h>
46 #include <epan/dissectors/packet-data.h>
47 #include <epan/dissectors/packet-frame.h>
49 #define PDML_VERSION "0"
50 #define PSML_VERSION "0"
54 print_stream_t *stream;
57 print_dissections_e print_dissections;
58 gboolean print_hex_for_data;
59 packet_char_enc encoding;
71 output_fields_t* fields;
75 struct _output_fields {
76 gboolean print_header;
81 GHashTable* field_indicies;
82 emem_strbuf_t** field_values;
86 static gboolean write_headers = FALSE;
88 static const gchar* get_field_hex_value(GSList* src_list, field_info *fi);
89 static void proto_tree_print_node(proto_node *node, gpointer data);
90 static void proto_tree_write_node_pdml(proto_node *node, gpointer data);
91 static const guint8 *get_field_data(GSList *src_list, field_info *fi);
92 static void write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi);
93 static gboolean print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
94 guint length, packet_char_enc encoding);
95 static void ps_clean_string(unsigned char *out, const unsigned char *in,
97 static void print_escaped_xml(FILE *fh, const char *unescaped_string);
99 static void print_pdml_geninfo(proto_tree *tree, FILE *fh);
101 static void proto_tree_get_node_field_values(proto_node *node, gpointer data);
104 open_print_dest(int to_file, const char *dest)
108 /* Open the file or command for output */
110 fh = ws_fopen(dest, "w");
112 fh = popen(dest, "w");
118 close_print_dest(int to_file, FILE *fh)
120 /* Close the file or command */
122 return (fclose(fh) == 0);
124 return (pclose(fh) == 0);
127 #define MAX_PS_LINE_LENGTH 256
130 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
131 print_stream_t *stream)
135 /* Create the output */
137 data.stream = stream;
139 data.src_list = edt->pi.data_src;
140 data.encoding = edt->pi.fd->flags.encoding;
141 data.print_dissections = print_args->print_dissections;
142 /* If we're printing the entire packet in hex, don't
143 print uninterpreted data fields in hex as well. */
144 data.print_hex_for_data = !print_args->print_hex;
147 proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
151 #define MAX_INDENT 160
153 /* Print a tree's data, and any child nodes. */
155 void proto_tree_print_node(proto_node *node, gpointer data)
157 field_info *fi = PNODE_FINFO(node);
158 print_data *pdata = (print_data*) data;
160 gchar label_str[ITEM_LABEL_LENGTH];
163 g_assert(fi && "dissection with an invisible proto tree?");
165 /* Don't print invisible entries. */
166 if (PROTO_ITEM_IS_HIDDEN(node))
169 /* Give up if we've already gotten an error. */
173 /* was a free format label produced? */
175 label_ptr = fi->rep->representation;
177 else { /* no, make a generic label */
178 label_ptr = label_str;
179 proto_item_fill_label(fi, label_str);
182 if (PROTO_ITEM_IS_GENERATED(node)) {
183 label_ptr = g_strdup_printf("[%s]", label_ptr);
186 if (!print_line(pdata->stream, pdata->level, label_ptr)) {
187 pdata->success = FALSE;
191 if (PROTO_ITEM_IS_GENERATED(node)) {
195 /* If it's uninterpreted data, dump it (unless our caller will
196 be printing the entire packet in hex). */
197 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
199 * Find the data for this field.
201 pd = get_field_data(pdata->src_list, fi);
203 if (!print_hex_data_buffer(pdata->stream, pd,
204 fi->length, pdata->encoding)) {
205 pdata->success = FALSE;
211 /* If we're printing all levels, or if this node is one with a
212 subtree and its subtree is expanded, recurse into the subtree,
214 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
215 if (pdata->print_dissections == print_dissections_expanded ||
216 (pdata->print_dissections == print_dissections_as_displayed &&
217 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
218 if (node->first_child != NULL) {
220 proto_tree_children_foreach(node,
221 proto_tree_print_node, pdata);
230 write_pdml_preamble(FILE *fh)
232 fputs("<?xml version=\"1.0\"?>\n", fh);
233 fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
234 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
238 proto_tree_write_pdml(epan_dissect_t *edt, FILE *fh)
240 write_pdml_data data;
242 /* Create the output */
245 data.src_list = edt->pi.data_src;
248 fprintf(fh, "<packet>\n");
250 /* Print a "geninfo" protocol as required by PDML */
251 print_pdml_geninfo(edt->tree, fh);
253 proto_tree_children_foreach(edt->tree, proto_tree_write_node_pdml,
256 fprintf(fh, "</packet>\n\n");
259 /* Write out a tree's data, and any child nodes, as PDML */
261 proto_tree_write_node_pdml(proto_node *node, gpointer data)
263 field_info *fi = PNODE_FINFO(node);
264 write_pdml_data *pdata = (write_pdml_data*) data;
265 const gchar *label_ptr;
266 gchar label_str[ITEM_LABEL_LENGTH];
267 char *dfilter_string;
270 gboolean wrap_in_fake_protocol;
272 g_assert(fi && "dissection with an invisible proto tree?");
274 /* Will wrap up top-level field items inside a fake protocol wrapper to
275 preserve the PDML schema */
276 wrap_in_fake_protocol =
277 (((fi->hfinfo->type != FT_PROTOCOL) ||
278 (fi->hfinfo->id == proto_data)) &&
279 (pdata->level == 0));
281 /* Indent to the correct level */
282 for (i = -1; i < pdata->level; i++) {
283 fputs(" ", pdata->fh);
286 if (wrap_in_fake_protocol) {
287 /* Open fake protocol wrapper */
288 fputs("<proto name=\"fake-field-wrapper\">\n", pdata->fh);
290 /* Indent to increased level before writint out field */
292 for (i = -1; i < pdata->level; i++) {
293 fputs(" ", pdata->fh);
297 /* Text label. It's printed as a field with no name. */
298 if (fi->hfinfo->id == hf_text_only) {
301 label_ptr = fi->rep->representation;
307 /* Show empty name since it is a required field */
308 fputs("<field name=\"", pdata->fh);
309 fputs("\" show=\"", pdata->fh);
310 print_escaped_xml(pdata->fh, label_ptr);
312 fprintf(pdata->fh, "\" size=\"%d", fi->length);
313 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
315 fputs("\" value=\"", pdata->fh);
316 write_pdml_field_hex_value(pdata, fi);
318 if (node->first_child != NULL) {
319 fputs("\">\n", pdata->fh);
322 fputs("\"/>\n", pdata->fh);
326 /* Uninterpreted data, i.e., the "Data" protocol, is
327 * printed as a field instead of a protocol. */
328 else if (fi->hfinfo->id == proto_data) {
330 /* Write out field with data */
331 fputs("<field name=\"data\" value=\"", pdata->fh);
332 write_pdml_field_hex_value(pdata, fi);
333 fputs("\"/>\n", pdata->fh);
335 /* Normal protocols and fields */
337 if (fi->hfinfo->type == FT_PROTOCOL) {
338 fputs("<proto name=\"", pdata->fh);
341 fputs("<field name=\"", pdata->fh);
343 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
347 * http://www.nbee.org/doku.php?id=netpdl:pdml_specification
349 * the show fields contains things in 'human readable' format
350 * showname: contains only the name of the field
351 * show: contains only the data of the field
352 * showdtl: contains additional details of the field data
353 * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
355 * XXX - the showname shouldn't contain the field data itself
356 * (like it's contained in the fi->rep->representation).
357 * Unfortunately, we don't have the field data representation for
358 * all fields, so this isn't currently possible */
359 fputs("\" showname=\"", pdata->fh);
360 print_escaped_xml(pdata->fh, fi->hfinfo->name);
364 fputs("\" showname=\"", pdata->fh);
365 print_escaped_xml(pdata->fh, fi->rep->representation);
368 label_ptr = label_str;
369 proto_item_fill_label(fi, label_str);
370 fputs("\" showname=\"", pdata->fh);
371 print_escaped_xml(pdata->fh, label_ptr);
374 if (PROTO_ITEM_IS_HIDDEN(node))
375 fprintf(pdata->fh, "\" hide=\"yes");
377 fprintf(pdata->fh, "\" size=\"%d", fi->length);
378 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
379 /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
381 /* show, value, and unmaskedvalue attributes */
382 switch (fi->hfinfo->type)
387 fputs("\" show=\"\" value=\"", pdata->fh);
390 /* XXX - this is a hack until we can just call
391 * fvalue_to_string_repr() for *all* FT_* types. */
392 dfilter_string = proto_construct_match_selected_string(fi,
394 if (dfilter_string != NULL) {
395 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
397 /* XXX - Remove double-quotes. Again, once we
398 * can call fvalue_to_string_repr(), we can
399 * ask it not to produce the version for
400 * display-filters, and thus, no
402 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
403 dfilter_string[strlen(dfilter_string)-1] = '\0';
407 fputs("\" show=\"", pdata->fh);
408 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
412 * XXX - should we omit "value" for any fields?
413 * What should we do for fields whose length is 0?
414 * They might come from a pseudo-header or from
415 * the capture header (e.g., time stamps), or
416 * they might be generated fields.
418 if (fi->length > 0) {
419 fputs("\" value=\"", pdata->fh);
421 if (fi->hfinfo->bitmask!=0) {
422 fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
423 fputs("\" unmaskedvalue=\"", pdata->fh);
424 write_pdml_field_hex_value(pdata, fi);
427 write_pdml_field_hex_value(pdata, fi);
432 if (node->first_child != NULL) {
433 fputs("\">\n", pdata->fh);
435 else if (fi->hfinfo->id == proto_data) {
436 fputs("\">\n", pdata->fh);
439 fputs("\"/>\n", pdata->fh);
443 /* We always print all levels for PDML. Recurse here. */
444 if (node->first_child != NULL) {
446 proto_tree_children_foreach(node,
447 proto_tree_write_node_pdml, pdata);
451 /* Take back the extra level we added for fake wrapper protocol */
452 if (wrap_in_fake_protocol) {
456 if (node->first_child != NULL) {
457 /* Indent to correct level */
458 for (i = -1; i < pdata->level; i++) {
459 fputs(" ", pdata->fh);
461 /* Close off current element */
462 if (fi->hfinfo->id != proto_data) { /* Data protocol uses simple tags */
463 if (fi->hfinfo->type == FT_PROTOCOL) {
464 fputs("</proto>\n", pdata->fh);
467 fputs("</field>\n", pdata->fh);
472 /* Close off fake wrapper protocol */
473 if (wrap_in_fake_protocol) {
474 fputs("</proto>\n", pdata->fh);
478 /* Print info for a 'geninfo' pseudo-protocol. This is required by
479 * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
480 * but we produce a 'geninfo' protocol in the PDML to conform to spec.
481 * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
483 print_pdml_geninfo(proto_tree *tree, FILE *fh)
485 guint32 num, len, caplen;
487 GPtrArray *finfo_array;
488 field_info *frame_finfo;
490 /* Get frame protocol's finfo. */
491 finfo_array = proto_find_finfo(tree, proto_frame);
492 if (g_ptr_array_len(finfo_array) < 1) {
495 frame_finfo = (field_info *)finfo_array->pdata[0];
496 g_ptr_array_free(finfo_array, TRUE);
498 /* frame.number --> geninfo.num */
499 finfo_array = proto_find_finfo(tree, hf_frame_number);
500 if (g_ptr_array_len(finfo_array) < 1) {
503 num = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
504 g_ptr_array_free(finfo_array, TRUE);
506 /* frame.frame_len --> geninfo.len */
507 finfo_array = proto_find_finfo(tree, hf_frame_len);
508 if (g_ptr_array_len(finfo_array) < 1) {
511 len = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
512 g_ptr_array_free(finfo_array, TRUE);
514 /* frame.cap_len --> geninfo.caplen */
515 finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
516 if (g_ptr_array_len(finfo_array) < 1) {
519 caplen = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
520 g_ptr_array_free(finfo_array, TRUE);
522 /* frame.time --> geninfo.timestamp */
523 finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
524 if (g_ptr_array_len(finfo_array) < 1) {
527 timestamp = (nstime_t *)fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
528 g_ptr_array_free(finfo_array, TRUE);
530 /* Print geninfo start */
532 " <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
533 frame_finfo->length);
535 /* Print geninfo.num */
537 " <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
538 num, num, frame_finfo->length);
540 /* Print geninfo.len */
542 " <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Frame Length\" value=\"%x\" size=\"%u\"/>\n",
543 len, len, frame_finfo->length);
545 /* Print geninfo.caplen */
547 " <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
548 caplen, caplen, frame_finfo->length);
550 /* Print geninfo.timestamp */
552 " <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
553 abs_time_to_str(timestamp, ABSOLUTE_TIME_LOCAL, TRUE), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
555 /* Print geninfo end */
561 write_pdml_finale(FILE *fh)
563 fputs("</pdml>\n", fh);
567 write_psml_preamble(FILE *fh)
569 fputs("<?xml version=\"1.0\"?>\n", fh);
570 fputs("<psml version=\"" PSML_VERSION "\" ", fh);
571 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
572 write_headers = TRUE;
576 proto_tree_write_psml(epan_dissect_t *edt, FILE *fh)
580 /* if this is the first packet, we have to create the PSML structure output */
582 fprintf(fh, "<structure>\n");
584 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
585 fprintf(fh, "<section>");
586 print_escaped_xml(fh, edt->pi.cinfo->col_title[i]);
587 fprintf(fh, "</section>\n");
590 fprintf(fh, "</structure>\n\n");
592 write_headers = FALSE;
595 fprintf(fh, "<packet>\n");
597 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
598 fprintf(fh, "<section>");
599 print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
600 fprintf(fh, "</section>\n");
603 fprintf(fh, "</packet>\n\n");
607 write_psml_finale(FILE *fh)
609 fputs("</psml>\n", fh);
613 write_csv_preamble(FILE *fh _U_)
615 write_headers = TRUE;
619 proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
623 /* if this is the first packet, we have to write the CSV header */
625 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
626 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_title[i]);
628 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_title[i]);
630 write_headers = FALSE;
633 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
634 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_data[i]);
636 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_data[i]);
640 write_csv_finale(FILE *fh _U_)
646 write_carrays_preamble(FILE *fh _U_)
652 proto_tree_write_carrays(const guint8 *pd, guint32 len, guint32 num, FILE *fh)
659 fprintf(fh, "char pkt%u[] = {\n", num);
661 for (i = 0; i < len; i++) {
663 fprintf(fh, "0x%02x", *(pd + i));
665 if (i == (len - 1)) {
666 fprintf(fh, " };\n\n");
670 if (!((i + 1) % 8)) {
679 write_carrays_finale(FILE *fh _U_)
685 * Find the data source for a specified field, and return a pointer
686 * to the data in it. Returns NULL if the data is out of bounds.
688 static const guint8 *
689 get_field_data(GSList *src_list, field_info *fi)
694 gint length, tvbuff_length;
696 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
697 src = (data_source *)src_le->data;
699 if (fi->ds_tvb == src_tvb) {
703 * XXX - a field can have a length that runs past
704 * the end of the tvbuff. Ideally, that should
705 * be fixed when adding an item to the protocol
706 * tree, but checking the length when doing
707 * that could be expensive. Until we fix that,
708 * we'll do the check here.
710 tvbuff_length = tvb_length_remaining(src_tvb,
712 if (tvbuff_length < 0) {
716 if (length > tvbuff_length)
717 length = tvbuff_length;
718 return tvb_get_ptr(src_tvb, fi->start, length);
721 g_assert_not_reached();
722 return NULL; /* not found */
725 /* Print a string, escaping out certain characters that need to
726 * escaped out for XML. */
728 print_escaped_xml(FILE *fh, const char *unescaped_string)
733 for (p = unescaped_string; *p != '\0'; p++) {
751 if (g_ascii_isprint(*p))
754 g_snprintf(temp_str, sizeof(temp_str), "\\x%x", (guint8)*p);
762 write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi)
770 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
771 fprintf(pdata->fh, "field length invalid!");
775 /* Find the data for this field. */
776 pd = get_field_data(pdata->src_list, fi);
779 /* Print a simple hex dump */
780 for (i = 0 ; i < fi->length; i++) {
781 fprintf(pdata->fh, "%02x", pd[i]);
787 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
789 gboolean multiple_sources;
799 * Set "multiple_sources" iff this frame has more than one
800 * data source; if it does, we need to print the name of
801 * the data source before printing the data from the
804 multiple_sources = (edt->pi.data_src->next != NULL);
806 for (src_le = edt->pi.data_src; src_le != NULL;
807 src_le = src_le->next) {
808 src = (data_source *)src_le->data;
810 if (multiple_sources) {
811 name = get_data_source_name(src);
812 print_line(stream, 0, "");
813 line = g_strdup_printf("%s:", name);
814 print_line(stream, 0, line);
817 length = tvb_length(tvb);
820 cp = tvb_get_ptr(tvb, 0, length);
821 if (!print_hex_data_buffer(stream, cp, length,
822 edt->pi.fd->flags.encoding))
829 * This routine is based on a routine created by Dan Lasley
830 * <DLASLEY@PROMUS.com>.
832 * It was modified for Wireshark by Gilbert Ramirez and others.
835 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
836 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
837 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
838 /* max number of characters hex dump takes -
839 2 digits plus trailing blank */
840 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
841 /* number of characters those bytes take;
842 3 characters per byte of hex dump,
843 2 blanks separating hex from ASCII,
844 1 character per byte of ASCII dump */
845 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
846 /* number of characters per line;
847 offset, 2 blanks separating offset
848 from data dump, data dump */
851 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
852 guint length, packet_char_enc encoding)
854 register unsigned int ad, i, j, k, l;
856 guchar line[MAX_LINE_LEN + 1];
857 unsigned int use_digits;
858 static guchar binhex[16] = {
859 '0', '1', '2', '3', '4', '5', '6', '7',
860 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
862 if (!print_line(stream, 0, ""))
866 * How many of the leading digits of the offset will we supply?
867 * We always supply at least 4 digits, but if the maximum offset
868 * won't fit in 4 digits, we use as many digits as will be needed.
870 if (((length - 1) & 0xF0000000) != 0)
871 use_digits = 8; /* need all 8 digits */
872 else if (((length - 1) & 0x0F000000) != 0)
873 use_digits = 7; /* need 7 digits */
874 else if (((length - 1) & 0x00F00000) != 0)
875 use_digits = 6; /* need 6 digits */
876 else if (((length - 1) & 0x000F0000) != 0)
877 use_digits = 5; /* need 5 digits */
879 use_digits = 4; /* we'll supply 4 digits */
888 * Start of a new line.
894 c = (ad >> (l*4)) & 0xF;
895 line[j++] = binhex[c];
899 memset(line+j, ' ', DATA_DUMP_LEN);
902 * Offset in line of ASCII dump.
904 k = j + HEX_DUMP_LEN + 2;
907 line[j++] = binhex[c>>4];
908 line[j++] = binhex[c&0xf];
910 if (encoding == PACKET_CHAR_ENC_CHAR_EBCDIC) {
911 c = EBCDIC_to_ASCII1(c);
913 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
915 if ((i & 15) == 0 || i == length) {
917 * We'll be starting a new line, or
918 * we're finished printing this buffer;
919 * dump out the line we've constructed,
920 * and advance the offset.
923 if (!print_line(stream, 0, line))
932 void ps_clean_string(unsigned char *out, const unsigned char *in,
943 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
964 /* Some formats need stuff at the beginning of the output */
966 print_preamble(print_stream_t *self, gchar *filename)
968 return (self->ops->print_preamble)(self, filename);
972 print_line(print_stream_t *self, int indent, const char *line)
974 return (self->ops->print_line)(self, indent, line);
977 /* Insert bookmark */
979 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
981 return (self->ops->print_bookmark)(self, name, title);
985 new_page(print_stream_t *self)
987 return (self->ops->new_page)(self);
990 /* Some formats need stuff at the end of the output */
992 print_finale(print_stream_t *self)
994 return (self->ops->print_finale)(self);
998 destroy_print_stream(print_stream_t *self)
1000 return (self->ops->destroy)(self);
1009 print_preamble_text(print_stream_t *self _U_, gchar *filename _U_)
1012 return TRUE; /* always succeeds */
1016 print_line_text(print_stream_t *self, int indent, const char *line)
1018 output_text *output = (output_text *)self->data;
1019 char space[MAX_INDENT+1];
1023 /* Prepare the tabs for printing, depending on tree level */
1024 num_spaces = indent * 4;
1025 if (num_spaces > MAX_INDENT) {
1026 num_spaces = MAX_INDENT;
1028 for (i = 0; i < num_spaces; i++) {
1031 /* The string is NUL-terminated */
1032 space[num_spaces] = '\0';
1034 fputs(space, output->fh);
1035 fputs(line, output->fh);
1036 putc('\n', output->fh);
1037 return !ferror(output->fh);
1041 print_bookmark_text(print_stream_t *self _U_, const gchar *name _U_,
1042 const gchar *title _U_)
1049 new_page_text(print_stream_t *self)
1051 output_text *output = (output_text *)self->data;
1053 fputs("\f", output->fh);
1054 return !ferror(output->fh);
1058 print_finale_text(print_stream_t *self _U_)
1061 return TRUE; /* always succeeds */
1065 destroy_text(print_stream_t *self)
1067 output_text *output = (output_text *)self->data;
1070 ret = close_print_dest(output->to_file, output->fh);
1076 static const print_stream_ops_t print_text_ops = {
1077 print_preamble_text,
1079 print_bookmark_text,
1085 static print_stream_t *
1086 print_stream_text_alloc(int to_file, FILE *fh)
1088 print_stream_t *stream;
1089 output_text *output;
1091 output = (output_text *)g_malloc(sizeof *output);
1092 output->to_file = to_file;
1094 stream = (print_stream_t *)g_malloc(sizeof (print_stream_t));
1095 stream->ops = &print_text_ops;
1096 stream->data = output;
1102 print_stream_text_new(int to_file, const char *dest)
1106 fh = open_print_dest(to_file, dest);
1110 return print_stream_text_alloc(to_file, fh);
1114 print_stream_text_stdio_new(FILE *fh)
1116 return print_stream_text_alloc(TRUE, fh);
1125 print_preamble_ps(print_stream_t *self, gchar *filename)
1127 output_ps *output = (output_ps *)self->data;
1128 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1130 print_ps_preamble(output->fh);
1132 fputs("%% the page title\n", output->fh);
1133 ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
1134 fprintf(output->fh, "/ws_pagetitle (%s - Wireshark " VERSION "%s) def\n", psbuffer, wireshark_svnversion);
1135 fputs("\n", output->fh);
1136 return !ferror(output->fh);
1140 print_line_ps(print_stream_t *self, int indent, const char *line)
1142 output_ps *output = (output_ps *)self->data;
1143 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1145 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
1146 fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
1147 return !ferror(output->fh);
1151 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
1153 output_ps *output = (output_ps *)self->data;
1154 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1157 * See the Adobe "pdfmark reference":
1159 * http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
1161 * The pdfmark stuff tells code that turns PostScript into PDF
1162 * things that it should do.
1164 * The /OUT stuff creates a bookmark that goes to the
1165 * destination with "name" as the name and "title" as the title.
1167 * The "/DEST" creates the destination.
1169 ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
1170 fprintf(output->fh, "[/Dest /%s /Title (%s) /OUT pdfmark\n", name,
1172 fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
1174 fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
1176 fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
1177 return !ferror(output->fh);
1181 new_page_ps(print_stream_t *self)
1183 output_ps *output = (output_ps *)self->data;
1185 fputs("formfeed\n", output->fh);
1186 return !ferror(output->fh);
1190 print_finale_ps(print_stream_t *self)
1192 output_ps *output = (output_ps *)self->data;
1194 print_ps_finale(output->fh);
1195 return !ferror(output->fh);
1199 destroy_ps(print_stream_t *self)
1201 output_ps *output = (output_ps *)self->data;
1204 ret = close_print_dest(output->to_file, output->fh);
1210 static const print_stream_ops_t print_ps_ops = {
1219 static print_stream_t *
1220 print_stream_ps_alloc(int to_file, FILE *fh)
1222 print_stream_t *stream;
1225 output = (output_ps *)g_malloc(sizeof *output);
1226 output->to_file = to_file;
1228 stream = (print_stream_t *)g_malloc(sizeof (print_stream_t));
1229 stream->ops = &print_ps_ops;
1230 stream->data = output;
1236 print_stream_ps_new(int to_file, const char *dest)
1240 fh = open_print_dest(to_file, dest);
1244 return print_stream_ps_alloc(to_file, fh);
1248 print_stream_ps_stdio_new(FILE *fh)
1250 return print_stream_ps_alloc(TRUE, fh);
1253 output_fields_t* output_fields_new()
1255 output_fields_t* fields = g_new(output_fields_t, 1);
1256 fields->print_header = FALSE;
1257 fields->separator = '\t';
1258 fields->occurrence = 'a';
1259 fields->aggregator = ',';
1260 fields->fields = NULL; /*Do lazy initialisation */
1261 fields->field_indicies = NULL;
1262 fields->field_values = NULL;
1267 gsize output_fields_num_fields(output_fields_t* fields)
1271 if(NULL == fields->fields) {
1274 return fields->fields->len;
1278 void output_fields_free(output_fields_t* fields)
1282 if(NULL != fields->field_indicies) {
1283 /* Keys are stored in fields->fields, values are
1286 g_hash_table_destroy(fields->field_indicies);
1288 if(NULL != fields->fields) {
1290 for(i = 0; i < fields->fields->len; ++i) {
1291 gchar* field = (gchar *)g_ptr_array_index(fields->fields,i);
1294 g_ptr_array_free(fields->fields, TRUE);
1300 void output_fields_add(output_fields_t* fields, const gchar* field)
1308 if(NULL == fields->fields) {
1309 fields->fields = g_ptr_array_new();
1312 field_copy = g_strdup(field);
1314 g_ptr_array_add(fields->fields, field_copy);
1317 gboolean output_fields_set_option(output_fields_t* info, gchar* option)
1319 const gchar* option_name;
1320 const gchar* option_value;
1325 if('\0' == *option) {
1326 return FALSE; /* Is this guarded against by option parsing? */
1328 option_name = strtok(option,"=");
1329 option_value = option + strlen(option_name) + 1;
1330 if(0 == strcmp(option_name, "header")) {
1331 switch(NULL == option_value ? '\0' : *option_value) {
1333 info->print_header = FALSE;
1336 info->print_header = TRUE;
1344 if(0 == strcmp(option_name,"separator")) {
1345 switch(NULL == option_value ? '\0' : *option_value) {
1349 switch(*++option_value) {
1351 info->separator = '\t';
1354 info->separator = ' ';
1357 info->separator = '\\';
1361 info->separator = *option_value;
1367 if(0 == strcmp(option_name, "occurrence")) {
1368 switch(NULL == option_value ? '\0' : *option_value) {
1372 info->occurrence = *option_value;
1380 if(0 == strcmp(option_name,"aggregator")) {
1381 switch(NULL == option_value ? '\0' : *option_value) {
1385 switch(*++option_value) {
1387 info->aggregator = ' ';
1390 info->aggregator = '\\';
1394 info->aggregator = *option_value;
1400 if(0 == strcmp(option_name, "quote")) {
1401 switch(NULL == option_value ? '\0' : *option_value) {
1402 default: /* Fall through */
1422 void output_fields_list_options(FILE *fh)
1424 fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
1425 fputs("header=y|n Print field abbreviations as first line of output (def: N: no)\n", fh);
1426 fputs("separator=/t|/s|<character> Set the separator to use;\n \"/t\" = tab, \"/s\" = space (def: /t: tab)\n", fh);
1427 fputs("occurrence=f|l|a Select the occurrence of a field to use;\n \"f\" = first, \"l\" = last, \"a\" = all (def: a: all)\n", fh);
1428 fputs("aggregator=,|/s|<character> Set the aggregator to use;\n \",\" = comma, \"/s\" = space (def: ,: comma)\n", fh);
1429 fputs("quote=d|s|n Print either d: double-quotes, s: single quotes or \n n: no quotes around field values (def: n: none)\n", fh);
1433 void write_fields_preamble(output_fields_t* fields, FILE *fh)
1440 if(!fields->print_header) {
1444 for(i = 0; i < fields->fields->len; ++i) {
1445 const gchar* field = (const gchar *)g_ptr_array_index(fields->fields,i);
1447 fputc(fields->separator, fh);
1454 static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
1456 write_field_data_t *call_data;
1458 gpointer field_index;
1460 call_data = (write_field_data_t *)data;
1461 fi = PNODE_FINFO(node);
1463 g_assert(fi && "dissection with an invisible proto tree?");
1465 field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
1466 if(NULL != field_index) {
1469 value = get_node_field_value(fi, call_data->edt); /* ep_alloced string */
1471 if(NULL != value && '\0' != *value) {
1473 actual_index = GPOINTER_TO_UINT(field_index);
1474 /* Unwrap change made to disambiguiate zero / null */
1475 if ( call_data->fields->field_values[actual_index - 1] == NULL ) {
1476 call_data->fields->field_values[actual_index - 1] = ep_strbuf_new(value);
1477 } else if ( call_data->fields->occurrence == 'l' ) {
1478 /* print only the value of the last occurrence of the field */
1479 ep_strbuf_printf(call_data->fields->field_values[actual_index - 1],"%s",value);
1480 } else if ( call_data->fields->occurrence == 'a' ) {
1481 /* print the value of all accurrences of the field */
1482 ep_strbuf_append_printf(call_data->fields->field_values[actual_index - 1],
1483 "%c%s",call_data->fields->aggregator,value);
1489 if (node->first_child != NULL) {
1490 proto_tree_children_foreach(node, proto_tree_get_node_field_values,
1495 void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, FILE *fh)
1499 write_field_data_t data;
1505 data.fields = fields;
1508 if(NULL == fields->field_indicies) {
1509 /* Prepare a lookup table from string abbreviation for field to its index. */
1510 fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
1513 while( i < fields->fields->len) {
1514 gchar* field = (gchar *)g_ptr_array_index(fields->fields, i);
1515 /* Store field indicies +1 so that zero is not a valid value,
1516 * and can be distinguished from NULL as a pointer.
1519 g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
1523 /* Buffer to store values for this packet */
1524 fields->field_values = ep_alloc_array0(emem_strbuf_t*, fields->fields->len);
1526 proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
1529 for(i = 0; i < fields->fields->len; ++i) {
1531 fputc(fields->separator, fh);
1533 if(NULL != fields->field_values[i]) {
1534 if(fields->quote != '\0') {
1535 fputc(fields->quote, fh);
1537 fputs(fields->field_values[i]->str, fh);
1538 if(fields->quote != '\0') {
1539 fputc(fields->quote, fh);
1545 void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
1550 /* Returns an ep_alloced string or a static constant*/
1551 const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
1553 if (fi->hfinfo->id == hf_text_only) {
1557 return fi->rep->representation;
1560 return get_field_hex_value(edt->pi.data_src, fi);
1563 else if (fi->hfinfo->id == proto_data) {
1564 /* Uninterpreted data, i.e., the "Data" protocol, is
1565 * printed as a field instead of a protocol. */
1566 return get_field_hex_value(edt->pi.data_src, fi);
1569 /* Normal protocols and fields */
1570 gchar *dfilter_string;
1573 switch (fi->hfinfo->type)
1576 /* Print out the full details for the protocol. */
1578 return fi->rep->representation;
1580 /* Just print out the protocol abbreviation */
1581 return fi->hfinfo->abbrev;;
1584 /* Return "1" so that the presence of a field of type
1585 * FT_NONE can be checked when using -T fields */
1588 /* XXX - this is a hack until we can just call
1589 * fvalue_to_string_repr() for *all* FT_* types. */
1590 dfilter_string = proto_construct_match_selected_string(fi,
1592 if (dfilter_string != NULL) {
1593 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
1595 /* XXX - Remove double-quotes. Again, once we
1596 * can call fvalue_to_string_repr(), we can
1597 * ask it not to produce the version for
1598 * display-filters, and thus, no
1600 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
1601 dfilter_string[strlen(dfilter_string)-1] = '\0';
1605 return &(dfilter_string[chop_len]);
1607 return get_field_hex_value(edt->pi.data_src, fi);
1614 get_field_hex_value(GSList* src_list, field_info *fi)
1621 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
1622 return "field length invalid!";
1625 /* Find the data for this field. */
1626 pd = get_field_data(src_list, fi);
1633 const int chars_per_byte = 2;
1635 len = chars_per_byte * fi->length;
1636 buffer = ep_alloc_array(gchar, len + 1);
1637 buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
1639 /* Print a simple hex dump */
1640 for (i = 0 ; i < fi->length; i++) {
1641 g_snprintf(p, chars_per_byte+1, "%02x", pd[i]);
1642 p += chars_per_byte;