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 /* Will wrap up top-level field items inside a fake protocol wrapper to
266 preserve the PDML schema */
267 gboolean wrap_in_fake_protocol =
268 (((fi->hfinfo->type != FT_PROTOCOL) ||
269 (fi->hfinfo->id == proto_data)) &&
270 (pdata->level == 0));
272 /* Indent to the correct level */
273 for (i = -1; i < pdata->level; i++) {
274 fputs(" ", pdata->fh);
277 if (wrap_in_fake_protocol) {
278 /* Open fake protocol wrapper */
279 fputs("<proto name=\"fake-field-wrapper\">\n", pdata->fh);
281 /* Indent to increased level before writint out field */
283 for (i = -1; i < pdata->level; i++) {
284 fputs(" ", pdata->fh);
288 /* Text label. It's printed as a field with no name. */
289 if (fi->hfinfo->id == hf_text_only) {
292 label_ptr = fi->rep->representation;
298 /* Show empty name since it is a required field */
299 fputs("<field name=\"", pdata->fh);
300 fputs("\" show=\"", pdata->fh);
301 print_escaped_xml(pdata->fh, label_ptr);
303 fprintf(pdata->fh, "\" size=\"%d", fi->length);
304 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
306 fputs("\" value=\"", pdata->fh);
307 write_pdml_field_hex_value(pdata, fi);
309 if (node->first_child != NULL) {
310 fputs("\">\n", pdata->fh);
313 fputs("\"/>\n", pdata->fh);
317 /* Uninterpreted data, i.e., the "Data" protocol, is
318 * printed as a field instead of a protocol. */
319 else if (fi->hfinfo->id == proto_data) {
321 /* Write out field with data */
322 fputs("<field name=\"data\" value=\"", pdata->fh);
323 write_pdml_field_hex_value(pdata, fi);
324 fputs("\"/>\n", pdata->fh);
326 /* Normal protocols and fields */
328 if (fi->hfinfo->type == FT_PROTOCOL) {
329 fputs("<proto name=\"", pdata->fh);
332 fputs("<field name=\"", pdata->fh);
334 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
338 * http://analyzer.polito.it/30alpha/docs/dissectors/PDMLSpec.htm
340 * the show fields contains things in 'human readable' format
341 * showname: contains only the name of the field
342 * show: contains only the data of the field
343 * showdtl: contains additional details of the field data
344 * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
346 * XXX - the showname shouldn't contain the field data itself
347 * (like it's contained in the fi->rep->representation).
348 * Unfortunately, we don't have the field data representation for
349 * all fields, so this isn't currently possible */
350 fputs("\" showname=\"", pdata->fh);
351 print_escaped_xml(pdata->fh, fi->hfinfo->name);
355 fputs("\" showname=\"", pdata->fh);
356 print_escaped_xml(pdata->fh, fi->rep->representation);
359 label_ptr = label_str;
360 proto_item_fill_label(fi, label_str);
361 fputs("\" showname=\"", pdata->fh);
362 print_escaped_xml(pdata->fh, label_ptr);
365 if (PROTO_ITEM_IS_HIDDEN(node))
366 fprintf(pdata->fh, "\" hide=\"yes");
368 fprintf(pdata->fh, "\" size=\"%d", fi->length);
369 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
370 /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
372 /* show, value, and unmaskedvalue attributes */
373 switch (fi->hfinfo->type)
378 fputs("\" show=\"\" value=\"", pdata->fh);
381 /* XXX - this is a hack until we can just call
382 * fvalue_to_string_repr() for *all* FT_* types. */
383 dfilter_string = proto_construct_match_selected_string(fi,
385 if (dfilter_string != NULL) {
386 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
388 /* XXX - Remove double-quotes. Again, once we
389 * can call fvalue_to_string_repr(), we can
390 * ask it not to produce the version for
391 * display-filters, and thus, no
393 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
394 dfilter_string[strlen(dfilter_string)-1] = '\0';
398 fputs("\" show=\"", pdata->fh);
399 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
403 * XXX - should we omit "value" for any fields?
404 * What should we do for fields whose length is 0?
405 * They might come from a pseudo-header or from
406 * the capture header (e.g., time stamps), or
407 * they might be generated fields.
409 if (fi->length > 0) {
410 fputs("\" value=\"", pdata->fh);
412 if (fi->hfinfo->bitmask!=0) {
413 fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
414 fputs("\" unmaskedvalue=\"", pdata->fh);
415 write_pdml_field_hex_value(pdata, fi);
418 write_pdml_field_hex_value(pdata, fi);
423 if (node->first_child != NULL) {
424 fputs("\">\n", pdata->fh);
426 else if (fi->hfinfo->id == proto_data) {
427 fputs("\">\n", pdata->fh);
430 fputs("\"/>\n", pdata->fh);
434 /* We always print all levels for PDML. Recurse here. */
435 if (node->first_child != NULL) {
437 proto_tree_children_foreach(node,
438 proto_tree_write_node_pdml, pdata);
442 /* Take back the extra level we added for fake wrapper protocol */
443 if (wrap_in_fake_protocol) {
447 if (node->first_child != NULL) {
448 /* Indent to correct level */
449 for (i = -1; i < pdata->level; i++) {
450 fputs(" ", pdata->fh);
452 /* Close off current element */
453 if (fi->hfinfo->id != proto_data) { /* Data protocol uses simple tags */
454 if (fi->hfinfo->type == FT_PROTOCOL) {
455 fputs("</proto>\n", pdata->fh);
458 fputs("</field>\n", pdata->fh);
463 /* Close off fake wrapper protocol */
464 if (wrap_in_fake_protocol) {
465 fputs("</proto>\n", pdata->fh);
469 /* Print info for a 'geninfo' pseudo-protocol. This is required by
470 * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
471 * but we produce a 'geninfo' protocol in the PDML to conform to spec.
472 * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
474 print_pdml_geninfo(proto_tree *tree, FILE *fh)
476 guint32 num, len, caplen;
478 GPtrArray *finfo_array;
479 field_info *frame_finfo;
481 /* Get frame protocol's finfo. */
482 finfo_array = proto_find_finfo(tree, proto_frame);
483 if (g_ptr_array_len(finfo_array) < 1) {
486 frame_finfo = finfo_array->pdata[0];
487 g_ptr_array_free(finfo_array, FALSE);
489 /* frame.number --> geninfo.num */
490 finfo_array = proto_find_finfo(tree, hf_frame_number);
491 if (g_ptr_array_len(finfo_array) < 1) {
494 num = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
495 g_ptr_array_free(finfo_array, FALSE);
497 /* frame.pkt_len --> geninfo.len */
498 finfo_array = proto_find_finfo(tree, hf_frame_packet_len);
499 if (g_ptr_array_len(finfo_array) < 1) {
502 len = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
503 g_ptr_array_free(finfo_array, FALSE);
505 /* frame.cap_len --> geninfo.caplen */
506 finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
507 if (g_ptr_array_len(finfo_array) < 1) {
510 caplen = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
511 g_ptr_array_free(finfo_array, FALSE);
513 /* frame.time --> geninfo.timestamp */
514 finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
515 if (g_ptr_array_len(finfo_array) < 1) {
518 timestamp = fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
519 g_ptr_array_free(finfo_array, FALSE);
521 /* Print geninfo start */
523 " <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
524 frame_finfo->length);
526 /* Print geninfo.num */
528 " <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
529 num, num, frame_finfo->length);
531 /* Print geninfo.len */
533 " <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Packet Length\" value=\"%x\" size=\"%u\"/>\n",
534 len, len, frame_finfo->length);
536 /* Print geninfo.caplen */
538 " <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
539 caplen, caplen, frame_finfo->length);
541 /* Print geninfo.timestamp */
543 " <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
544 abs_time_to_str(timestamp), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
546 /* Print geninfo end */
552 write_pdml_finale(FILE *fh)
554 fputs("</pdml>\n", fh);
558 write_psml_preamble(FILE *fh)
560 fputs("<?xml version=\"1.0\"?>\n", fh);
561 fputs("<psml version=\"" PSML_VERSION "\" ", fh);
562 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
566 proto_tree_write_psml(epan_dissect_t *edt, FILE *fh)
570 /* if this is the first packet, we have to create the PSML structure output */
571 if(edt->pi.fd->num == 1) {
572 fprintf(fh, "<structure>\n");
574 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
575 fprintf(fh, "<section>");
576 print_escaped_xml(fh, edt->pi.cinfo->col_title[i]);
577 fprintf(fh, "</section>\n");
580 fprintf(fh, "</structure>\n\n");
583 fprintf(fh, "<packet>\n");
585 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
586 fprintf(fh, "<section>");
587 print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
588 fprintf(fh, "</section>\n");
591 fprintf(fh, "</packet>\n\n");
595 write_psml_finale(FILE *fh)
597 fputs("</psml>\n", fh);
601 write_csv_preamble(FILE *fh _U_)
607 proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
611 /* if this is the first packet, we have to write the CSV header */
612 if(edt->pi.fd->num == 1) {
613 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
614 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_title[i]);
616 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_title[i]);
619 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
620 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_data[i]);
622 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_data[i]);
626 write_csv_finale(FILE *fh _U_)
632 write_carrays_preamble(FILE *fh _U_)
638 proto_tree_write_carrays(const guint8 *pd, guint32 len, guint32 num, FILE *fh)
645 fprintf(fh, "char pkt%u[] = {\n", num);
647 for (i = 0; i < len; i++) {
649 fprintf(fh, "0x%02x", *(pd + i));
651 if (i == (len - 1)) {
652 fprintf(fh, " };\n\n");
656 if (!((i + 1) % 8)) {
665 write_carrays_finale(FILE *fh _U_)
671 * Find the data source for a specified field, and return a pointer
672 * to the data in it. Returns NULL if the data is out of bounds.
674 static const guint8 *
675 get_field_data(GSList *src_list, field_info *fi)
680 gint length, tvbuff_length;
682 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
685 if (fi->ds_tvb == src_tvb) {
689 * XXX - a field can have a length that runs past
690 * the end of the tvbuff. Ideally, that should
691 * be fixed when adding an item to the protocol
692 * tree, but checking the length when doing
693 * that could be expensive. Until we fix that,
694 * we'll do the check here.
696 tvbuff_length = tvb_length_remaining(src_tvb,
698 if (tvbuff_length < 0) {
702 if (length > tvbuff_length)
703 length = tvbuff_length;
704 return tvb_get_ptr(src_tvb, fi->start, length);
707 g_assert_not_reached();
708 return NULL; /* not found */
711 /* Print a string, escaping out certain characters that need to
712 * escaped out for XML. */
714 print_escaped_xml(FILE *fh, const char *unescaped_string)
718 for (p = unescaped_string; *p != '\0'; p++) {
742 write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi)
747 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
748 fprintf(pdata->fh, "field length invalid!");
752 /* Find the data for this field. */
753 pd = get_field_data(pdata->src_list, fi);
756 /* Print a simple hex dump */
757 for (i = 0 ; i < fi->length; i++) {
758 fprintf(pdata->fh, "%02x", pd[i]);
764 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
766 gboolean multiple_sources;
776 * Set "multiple_sources" iff this frame has more than one
777 * data source; if it does, we need to print the name of
778 * the data source before printing the data from the
781 multiple_sources = (edt->pi.data_src->next != NULL);
783 for (src_le = edt->pi.data_src; src_le != NULL;
784 src_le = src_le->next) {
787 if (multiple_sources) {
789 print_line(stream, 0, "");
790 line = g_strdup_printf("%s:", name);
791 print_line(stream, 0, line);
794 length = tvb_length(tvb);
797 cp = tvb_get_ptr(tvb, 0, length);
798 if (!print_hex_data_buffer(stream, cp, length,
799 edt->pi.fd->flags.encoding))
806 * This routine is based on a routine created by Dan Lasley
807 * <DLASLEY@PROMUS.com>.
809 * It was modified for Wireshark by Gilbert Ramirez and others.
812 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
813 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
814 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
815 /* max number of characters hex dump takes -
816 2 digits plus trailing blank */
817 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
818 /* number of characters those bytes take;
819 3 characters per byte of hex dump,
820 2 blanks separating hex from ASCII,
821 1 character per byte of ASCII dump */
822 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
823 /* number of characters per line;
824 offset, 2 blanks separating offset
825 from data dump, data dump */
828 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
829 guint length, char_enc encoding)
831 register unsigned int ad, i, j, k, l;
833 guchar line[MAX_LINE_LEN + 1];
834 unsigned int use_digits;
835 static guchar binhex[16] = {
836 '0', '1', '2', '3', '4', '5', '6', '7',
837 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
839 if (!print_line(stream, 0, ""))
843 * How many of the leading digits of the offset will we supply?
844 * We always supply at least 4 digits, but if the maximum offset
845 * won't fit in 4 digits, we use as many digits as will be needed.
847 if (((length - 1) & 0xF0000000) != 0)
848 use_digits = 8; /* need all 8 digits */
849 else if (((length - 1) & 0x0F000000) != 0)
850 use_digits = 7; /* need 7 digits */
851 else if (((length - 1) & 0x00F00000) != 0)
852 use_digits = 6; /* need 6 digits */
853 else if (((length - 1) & 0x000F0000) != 0)
854 use_digits = 5; /* need 5 digits */
856 use_digits = 4; /* we'll supply 4 digits */
865 * Start of a new line.
872 c = (ad >> (l*4)) & 0xF;
873 line[j++] = binhex[c];
877 memset(line+j, ' ', DATA_DUMP_LEN);
880 * Offset in line of ASCII dump.
882 k = j + HEX_DUMP_LEN + 2;
885 line[j++] = binhex[c>>4];
886 line[j++] = binhex[c&0xf];
888 if (encoding == CHAR_EBCDIC) {
889 c = EBCDIC_to_ASCII1(c);
891 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
893 if ((i & 15) == 0 || i == length) {
895 * We'll be starting a new line, or
896 * we're finished printing this buffer;
897 * dump out the line we've constructed,
898 * and advance the offset.
901 if (!print_line(stream, 0, line))
910 void ps_clean_string(unsigned char *out, const unsigned char *in,
916 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
937 /* Some formats need stuff at the beginning of the output */
939 print_preamble(print_stream_t *self, gchar *filename)
941 return (self->ops->print_preamble)(self, filename);
945 print_line(print_stream_t *self, int indent, const char *line)
947 return (self->ops->print_line)(self, indent, line);
950 /* Insert bookmark */
952 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
954 return (self->ops->print_bookmark)(self, name, title);
958 new_page(print_stream_t *self)
960 return (self->ops->new_page)(self);
963 /* Some formats need stuff at the end of the output */
965 print_finale(print_stream_t *self)
967 return (self->ops->print_finale)(self);
971 destroy_print_stream(print_stream_t *self)
973 return (self->ops->destroy)(self);
982 print_preamble_text(print_stream_t *self _U_, gchar *filename _U_)
985 return TRUE; /* always succeeds */
989 print_line_text(print_stream_t *self, int indent, const char *line)
991 output_text *output = self->data;
992 char space[MAX_INDENT+1];
996 /* Prepare the tabs for printing, depending on tree level */
997 num_spaces = indent * 4;
998 if (num_spaces > MAX_INDENT) {
999 num_spaces = MAX_INDENT;
1001 for (i = 0; i < num_spaces; i++) {
1004 /* The string is NUL-terminated */
1005 space[num_spaces] = '\0';
1007 fputs(space, output->fh);
1008 fputs(line, output->fh);
1009 putc('\n', output->fh);
1010 return !ferror(output->fh);
1014 print_bookmark_text(print_stream_t *self _U_, const gchar *name _U_,
1015 const gchar *title _U_)
1022 new_page_text(print_stream_t *self)
1024 output_text *output = self->data;
1026 fputs("\f", output->fh);
1027 return !ferror(output->fh);
1031 print_finale_text(print_stream_t *self _U_)
1034 return TRUE; /* always succeeds */
1038 destroy_text(print_stream_t *self)
1040 output_text *output = self->data;
1043 ret = close_print_dest(output->to_file, output->fh);
1049 static const print_stream_ops_t print_text_ops = {
1050 print_preamble_text,
1052 print_bookmark_text,
1059 print_stream_text_new(int to_file, const char *dest)
1062 print_stream_t *stream;
1063 output_text *output;
1065 fh = open_print_dest(to_file, dest);
1069 output = g_malloc(sizeof *output);
1070 output->to_file = to_file;
1072 stream = g_malloc(sizeof (print_stream_t));
1073 stream->ops = &print_text_ops;
1074 stream->data = output;
1080 print_stream_text_stdio_new(FILE *fh)
1082 print_stream_t *stream;
1083 output_text *output;
1085 output = g_malloc(sizeof *output);
1086 output->to_file = TRUE;
1088 stream = g_malloc(sizeof (print_stream_t));
1089 stream->ops = &print_text_ops;
1090 stream->data = output;
1101 print_preamble_ps(print_stream_t *self, gchar *filename)
1103 output_ps *output = self->data;
1104 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1106 print_ps_preamble(output->fh);
1108 fputs("%% Set the font to 10 point\n", output->fh);
1109 fputs("/Courier findfont 10 scalefont setfont\n", output->fh);
1110 fputs("\n", output->fh);
1111 fputs("%% the page title\n", output->fh);
1112 ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
1113 fprintf(output->fh, "/eth_pagetitle (%s - Wireshark) def\n", psbuffer);
1114 fputs("\n", output->fh);
1115 return !ferror(output->fh);
1119 print_line_ps(print_stream_t *self, int indent, const char *line)
1121 output_ps *output = self->data;
1122 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1124 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
1125 fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
1126 return !ferror(output->fh);
1130 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
1132 output_ps *output = self->data;
1133 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1136 * See the Adobe "pdfmark reference":
1138 * http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
1140 * The pdfmark stuff tells code that turns PostScript into PDF
1141 * things that it should do.
1143 * The /OUT stuff creates a bookmark that goes to the
1144 * destination with "name" as the name and "title" as the title.
1146 * The "/DEST" creates the destination.
1148 ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
1149 fprintf(output->fh, "[/Dest /%s /Title (%s) /OUT pdfmark\n", name,
1151 fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
1153 fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
1155 fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
1156 return !ferror(output->fh);
1160 new_page_ps(print_stream_t *self)
1162 output_ps *output = self->data;
1164 fputs("formfeed\n", output->fh);
1165 return !ferror(output->fh);
1169 print_finale_ps(print_stream_t *self)
1171 output_ps *output = self->data;
1173 print_ps_finale(output->fh);
1174 return !ferror(output->fh);
1178 destroy_ps(print_stream_t *self)
1180 output_ps *output = self->data;
1183 ret = close_print_dest(output->to_file, output->fh);
1189 static const print_stream_ops_t print_ps_ops = {
1199 print_stream_ps_new(int to_file, const char *dest)
1202 print_stream_t *stream;
1205 fh = open_print_dest(to_file, dest);
1209 output = g_malloc(sizeof *output);
1210 output->to_file = to_file;
1212 stream = g_malloc(sizeof (print_stream_t));
1213 stream->ops = &print_ps_ops;
1214 stream->data = output;
1220 print_stream_ps_stdio_new(FILE *fh)
1222 print_stream_t *stream;
1225 output = g_malloc(sizeof *output);
1226 output->to_file = TRUE;
1228 stream = g_malloc(sizeof (print_stream_t));
1229 stream->ops = &print_ps_ops;
1230 stream->data = output;
1235 output_fields_t* output_fields_new()
1237 output_fields_t* fields = g_new(output_fields_t, 1);
1238 fields->print_header = FALSE;
1239 fields->separator = '\t';
1240 fields->fields = NULL; /*Do lazy initialisation */
1241 fields->field_indicies = NULL;
1242 fields->field_values = NULL;
1247 gsize output_fields_num_fields(output_fields_t* fields)
1251 if(NULL == fields->fields) {
1254 return fields->fields->len;
1258 void output_fields_free(output_fields_t* fields)
1262 if(NULL != fields->field_indicies) {
1263 /* Keys are stored in fields->fields, values are
1266 g_hash_table_destroy(fields->field_indicies);
1268 if(NULL != fields->fields) {
1270 for(i = 0; i < fields->fields->len; ++i) {
1271 gchar* field = g_ptr_array_index(fields->fields,i);
1274 g_ptr_array_free(fields->fields, TRUE);
1280 void output_fields_add(output_fields_t* fields, const gchar* field)
1288 if(NULL == fields->fields) {
1289 fields->fields = g_ptr_array_new();
1292 field_copy = g_strdup(field);
1294 g_ptr_array_add(fields->fields, field_copy);
1297 gboolean output_fields_set_option(output_fields_t* info, gchar* option)
1299 const gchar* option_name;
1300 const gchar* option_value;
1305 if('\0' == *option) {
1306 return FALSE; /* Is this guarded against by option parsing? */
1308 option_name = strtok(option,"=");
1309 option_value = option + strlen(option_name) + 1;
1310 if(0 == strcmp(option_name, "header")) {
1311 switch(NULL == option_value ? '\0' : *option_value) {
1313 info->print_header = FALSE;
1316 info->print_header = TRUE;
1324 if(0 == strcmp(option_name,"separator")) {
1325 switch(NULL == option_value ? '\0' : *option_value) {
1329 switch(*++option_value) {
1331 info->separator = '\t';
1334 info->separator = ' ';
1337 info->separator = '\\';
1341 info->separator = *option_value;
1347 if(0 == strcmp(option_name, "quote")) {
1348 switch(NULL == option_value ? '\0' : *option_value) {
1349 default: /* Fall through */
1369 void output_fields_list_options(FILE *fh)
1371 fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
1372 fputs("header=y|n Print field abbreviations as first line of output (def: N: no)\n", fh);
1373 fputs("separator=/t|/s|<character> Set the separator to use; \"/t\" = tab,\n \"/s\" = space (def: /t: tab)\n", fh);
1374 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);
1378 void write_fields_preamble(output_fields_t* fields, FILE *fh)
1385 if(!fields->print_header) {
1389 for(i = 0; i < fields->fields->len; ++i) {
1390 const gchar* field = g_ptr_array_index(fields->fields,i);
1392 fputc(fields->separator, fh);
1399 static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
1401 write_field_data_t *call_data;
1403 gpointer field_index;
1406 fi = PITEM_FINFO(node);
1408 field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
1409 if(NULL != field_index) {
1412 value = get_node_field_value(fi, call_data->edt); /* ep_alloced string */
1414 if(NULL != value && '\0' != *value) {
1416 actual_index = GPOINTER_TO_UINT(field_index);
1417 /* Unwrap change made to disambiguiate zero / null */
1418 call_data->fields->field_values[actual_index - 1] = value;
1423 if (node->first_child != NULL) {
1424 proto_tree_children_foreach(node, proto_tree_get_node_field_values,
1429 void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, FILE *fh)
1433 write_field_data_t data;
1439 data.fields = fields;
1442 if(NULL == fields->field_indicies) {
1443 /* Prepare a lookup table from string abbreviation for field to its index. */
1444 fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
1447 while( i < fields->fields->len) {
1448 gchar* field = g_ptr_array_index(fields->fields, i);
1449 /* Store field indicies +1 so that zero is not a valid value,
1450 * and can be distinguished from NULL as a pointer.
1453 g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
1457 /* Buffer to store values for this packet */
1458 fields->field_values = ep_alloc_array0(const gchar*, fields->fields->len);
1460 proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
1463 for(i = 0; i < fields->fields->len; ++i) {
1465 fputc(fields->separator, fh);
1467 if(NULL != fields->field_values[i]) {
1468 if(fields->quote != '\0') {
1469 fputc(fields->quote, fh);
1471 fputs(fields->field_values[i], fh);
1472 if(fields->quote != '\0') {
1473 fputc(fields->quote, fh);
1479 void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
1484 /* Returns an ep_alloced string or a static constant*/
1485 static const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
1487 if (fi->hfinfo->id == hf_text_only) {
1491 return fi->rep->representation;
1494 return get_field_hex_value(edt->pi.data_src, fi);
1497 else if (fi->hfinfo->id == proto_data) {
1498 /* Uninterpreted data, i.e., the "Data" protocol, is
1499 * printed as a field instead of a protocol. */
1500 return get_field_hex_value(edt->pi.data_src, fi);
1503 /* Normal protocols and fields */
1504 gchar *dfilter_string;
1507 switch (fi->hfinfo->type)
1510 /* Print out the full details for the protocol. */
1512 return fi->rep->representation;
1514 /* Just print out the protocol abbreviation */
1515 return fi->hfinfo->abbrev;;
1518 /* Return "1" so that the presence of a field of type
1519 * FT_NONE can be checked when using -T fields */
1522 /* XXX - this is a hack until we can just call
1523 * fvalue_to_string_repr() for *all* FT_* types. */
1524 dfilter_string = proto_construct_match_selected_string(fi,
1526 if (dfilter_string != NULL) {
1527 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
1529 /* XXX - Remove double-quotes. Again, once we
1530 * can call fvalue_to_string_repr(), we can
1531 * ask it not to produce the version for
1532 * display-filters, and thus, no
1534 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
1535 dfilter_string[strlen(dfilter_string)-1] = '\0';
1539 return &(dfilter_string[chop_len]);
1541 return get_field_hex_value(edt->pi.data_src, fi);
1548 get_field_hex_value(GSList* src_list, field_info *fi)
1552 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
1553 return "field length invalid!";
1556 /* Find the data for this field. */
1557 pd = get_field_data(src_list, fi);
1564 const int chars_per_byte = 2;
1566 len = chars_per_byte * fi->length;
1567 buffer = ep_alloc_array(gchar, len + 1);
1568 buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
1570 /* Print a simple hex dump */
1571 for (i = 0 ; i < fi->length; i++) {
1572 g_snprintf(p, len, "%02x", pd[i]);
1573 p += chars_per_byte;
1574 len -= chars_per_byte;