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;
71 output_fields_t* fields;
75 struct _output_fields {
76 gboolean print_header;
79 GHashTable* field_indicies;
80 const gchar** field_values;
84 static const gchar* get_field_hex_value(GSList* src_list, field_info *fi);
85 const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt);
86 static void proto_tree_print_node(proto_node *node, gpointer data);
87 static void proto_tree_write_node_pdml(proto_node *node, gpointer data);
88 static const guint8 *get_field_data(GSList *src_list, field_info *fi);
89 static void write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi);
90 static gboolean print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
91 guint length, char_enc encoding);
92 static void ps_clean_string(unsigned char *out, const unsigned char *in,
94 static void print_escaped_xml(FILE *fh, const char *unescaped_string);
96 static void print_pdml_geninfo(proto_tree *tree, FILE *fh);
98 static void proto_tree_get_node_field_values(proto_node *node, gpointer data);
101 open_print_dest(int to_file, const char *dest)
105 /* Open the file or command for output */
107 fh = ws_fopen(dest, "w");
109 fh = popen(dest, "w");
115 close_print_dest(int to_file, FILE *fh)
117 /* Close the file or command */
119 return (fclose(fh) == 0);
121 return (pclose(fh) == 0);
124 #define MAX_PS_LINE_LENGTH 256
127 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
128 print_stream_t *stream)
132 /* Create the output */
134 data.stream = stream;
136 data.src_list = edt->pi.data_src;
137 data.encoding = edt->pi.fd->flags.encoding;
138 data.print_dissections = print_args->print_dissections;
139 /* If we're printing the entire packet in hex, don't
140 print uninterpreted data fields in hex as well. */
141 data.print_hex_for_data = !print_args->print_hex;
144 proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
148 #define MAX_INDENT 160
150 /* Print a tree's data, and any child nodes. */
152 void proto_tree_print_node(proto_node *node, gpointer data)
154 field_info *fi = PNODE_FINFO(node);
155 print_data *pdata = (print_data*) data;
157 gchar label_str[ITEM_LABEL_LENGTH];
160 g_assert(fi && "dissection with an invisible proto tree?");
162 /* Don't print invisible entries. */
163 if (PROTO_ITEM_IS_HIDDEN(node))
166 /* Give up if we've already gotten an error. */
170 /* was a free format label produced? */
172 label_ptr = fi->rep->representation;
174 else { /* no, make a generic label */
175 label_ptr = label_str;
176 proto_item_fill_label(fi, label_str);
179 if (PROTO_ITEM_IS_GENERATED(node)) {
180 label_ptr = g_strdup_printf("[%s]", label_ptr);
183 if (!print_line(pdata->stream, pdata->level, label_ptr)) {
184 pdata->success = FALSE;
188 if (PROTO_ITEM_IS_GENERATED(node)) {
192 /* If it's uninterpreted data, dump it (unless our caller will
193 be printing the entire packet in hex). */
194 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
196 * Find the data for this field.
198 pd = get_field_data(pdata->src_list, fi);
200 if (!print_hex_data_buffer(pdata->stream, pd,
201 fi->length, pdata->encoding)) {
202 pdata->success = FALSE;
208 /* If we're printing all levels, or if this node is one with a
209 subtree and its subtree is expanded, recurse into the subtree,
211 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
212 if (pdata->print_dissections == print_dissections_expanded ||
213 (pdata->print_dissections == print_dissections_as_displayed &&
214 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
215 if (node->first_child != NULL) {
217 proto_tree_children_foreach(node,
218 proto_tree_print_node, pdata);
227 write_pdml_preamble(FILE *fh)
229 fputs("<?xml version=\"1.0\"?>\n", fh);
230 fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
231 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
235 proto_tree_write_pdml(epan_dissect_t *edt, FILE *fh)
237 write_pdml_data data;
239 /* Create the output */
242 data.src_list = edt->pi.data_src;
245 /* We shouldn't be called with a NULL pointer here because we've
246 * created a visible protocol tree */
247 g_assert(data.src_list);
249 fprintf(fh, "<packet>\n");
251 /* Print a "geninfo" protocol as required by PDML */
252 print_pdml_geninfo(edt->tree, fh);
254 proto_tree_children_foreach(edt->tree, proto_tree_write_node_pdml,
257 fprintf(fh, "</packet>\n\n");
260 /* Write out a tree's data, and any child nodes, as PDML */
262 proto_tree_write_node_pdml(proto_node *node, gpointer data)
264 field_info *fi = PNODE_FINFO(node);
265 write_pdml_data *pdata = (write_pdml_data*) data;
266 const gchar *label_ptr;
267 gchar label_str[ITEM_LABEL_LENGTH];
268 char *dfilter_string;
271 gboolean wrap_in_fake_protocol;
273 g_assert(fi && "dissection with an invisible proto tree?");
275 /* Will wrap up top-level field items inside a fake protocol wrapper to
276 preserve the PDML schema */
277 wrap_in_fake_protocol =
278 (((fi->hfinfo->type != FT_PROTOCOL) ||
279 (fi->hfinfo->id == proto_data)) &&
280 (pdata->level == 0));
282 /* Indent to the correct level */
283 for (i = -1; i < pdata->level; i++) {
284 fputs(" ", pdata->fh);
287 if (wrap_in_fake_protocol) {
288 /* Open fake protocol wrapper */
289 fputs("<proto name=\"fake-field-wrapper\">\n", pdata->fh);
291 /* Indent to increased level before writint out field */
293 for (i = -1; i < pdata->level; i++) {
294 fputs(" ", pdata->fh);
298 /* Text label. It's printed as a field with no name. */
299 if (fi->hfinfo->id == hf_text_only) {
302 label_ptr = fi->rep->representation;
308 /* Show empty name since it is a required field */
309 fputs("<field name=\"", pdata->fh);
310 fputs("\" show=\"", pdata->fh);
311 print_escaped_xml(pdata->fh, label_ptr);
313 fprintf(pdata->fh, "\" size=\"%d", fi->length);
314 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
316 fputs("\" value=\"", pdata->fh);
317 write_pdml_field_hex_value(pdata, fi);
319 if (node->first_child != NULL) {
320 fputs("\">\n", pdata->fh);
323 fputs("\"/>\n", pdata->fh);
327 /* Uninterpreted data, i.e., the "Data" protocol, is
328 * printed as a field instead of a protocol. */
329 else if (fi->hfinfo->id == proto_data) {
331 /* Write out field with data */
332 fputs("<field name=\"data\" value=\"", pdata->fh);
333 write_pdml_field_hex_value(pdata, fi);
334 fputs("\"/>\n", pdata->fh);
336 /* Normal protocols and fields */
338 if (fi->hfinfo->type == FT_PROTOCOL) {
339 fputs("<proto name=\"", pdata->fh);
342 fputs("<field name=\"", pdata->fh);
344 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
348 * http://analyzer.polito.it/30alpha/docs/dissectors/PDMLSpec.htm
350 * the show fields contains things in 'human readable' format
351 * showname: contains only the name of the field
352 * show: contains only the data of the field
353 * showdtl: contains additional details of the field data
354 * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
356 * XXX - the showname shouldn't contain the field data itself
357 * (like it's contained in the fi->rep->representation).
358 * Unfortunately, we don't have the field data representation for
359 * all fields, so this isn't currently possible */
360 fputs("\" showname=\"", pdata->fh);
361 print_escaped_xml(pdata->fh, fi->hfinfo->name);
365 fputs("\" showname=\"", pdata->fh);
366 print_escaped_xml(pdata->fh, fi->rep->representation);
369 label_ptr = label_str;
370 proto_item_fill_label(fi, label_str);
371 fputs("\" showname=\"", pdata->fh);
372 print_escaped_xml(pdata->fh, label_ptr);
375 if (PROTO_ITEM_IS_HIDDEN(node))
376 fprintf(pdata->fh, "\" hide=\"yes");
378 fprintf(pdata->fh, "\" size=\"%d", fi->length);
379 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
380 /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
382 /* show, value, and unmaskedvalue attributes */
383 switch (fi->hfinfo->type)
388 fputs("\" show=\"\" value=\"", pdata->fh);
391 /* XXX - this is a hack until we can just call
392 * fvalue_to_string_repr() for *all* FT_* types. */
393 dfilter_string = proto_construct_match_selected_string(fi,
395 if (dfilter_string != NULL) {
396 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
398 /* XXX - Remove double-quotes. Again, once we
399 * can call fvalue_to_string_repr(), we can
400 * ask it not to produce the version for
401 * display-filters, and thus, no
403 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
404 dfilter_string[strlen(dfilter_string)-1] = '\0';
408 fputs("\" show=\"", pdata->fh);
409 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
413 * XXX - should we omit "value" for any fields?
414 * What should we do for fields whose length is 0?
415 * They might come from a pseudo-header or from
416 * the capture header (e.g., time stamps), or
417 * they might be generated fields.
419 if (fi->length > 0) {
420 fputs("\" value=\"", pdata->fh);
422 if (fi->hfinfo->bitmask!=0) {
423 fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
424 fputs("\" unmaskedvalue=\"", pdata->fh);
425 write_pdml_field_hex_value(pdata, fi);
428 write_pdml_field_hex_value(pdata, fi);
433 if (node->first_child != NULL) {
434 fputs("\">\n", pdata->fh);
436 else if (fi->hfinfo->id == proto_data) {
437 fputs("\">\n", pdata->fh);
440 fputs("\"/>\n", pdata->fh);
444 /* We always print all levels for PDML. Recurse here. */
445 if (node->first_child != NULL) {
447 proto_tree_children_foreach(node,
448 proto_tree_write_node_pdml, pdata);
452 /* Take back the extra level we added for fake wrapper protocol */
453 if (wrap_in_fake_protocol) {
457 if (node->first_child != NULL) {
458 /* Indent to correct level */
459 for (i = -1; i < pdata->level; i++) {
460 fputs(" ", pdata->fh);
462 /* Close off current element */
463 if (fi->hfinfo->id != proto_data) { /* Data protocol uses simple tags */
464 if (fi->hfinfo->type == FT_PROTOCOL) {
465 fputs("</proto>\n", pdata->fh);
468 fputs("</field>\n", pdata->fh);
473 /* Close off fake wrapper protocol */
474 if (wrap_in_fake_protocol) {
475 fputs("</proto>\n", pdata->fh);
479 /* Print info for a 'geninfo' pseudo-protocol. This is required by
480 * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
481 * but we produce a 'geninfo' protocol in the PDML to conform to spec.
482 * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
484 print_pdml_geninfo(proto_tree *tree, FILE *fh)
486 guint32 num, len, caplen;
488 GPtrArray *finfo_array;
489 field_info *frame_finfo;
491 /* Get frame protocol's finfo. */
492 finfo_array = proto_find_finfo(tree, proto_frame);
493 if (g_ptr_array_len(finfo_array) < 1) {
496 frame_finfo = finfo_array->pdata[0];
497 g_ptr_array_free(finfo_array, TRUE);
499 /* frame.number --> geninfo.num */
500 finfo_array = proto_find_finfo(tree, hf_frame_number);
501 if (g_ptr_array_len(finfo_array) < 1) {
504 num = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
505 g_ptr_array_free(finfo_array, TRUE);
507 /* frame.frame_len --> geninfo.len */
508 finfo_array = proto_find_finfo(tree, hf_frame_len);
509 if (g_ptr_array_len(finfo_array) < 1) {
512 len = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
513 g_ptr_array_free(finfo_array, TRUE);
515 /* frame.cap_len --> geninfo.caplen */
516 finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
517 if (g_ptr_array_len(finfo_array) < 1) {
520 caplen = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
521 g_ptr_array_free(finfo_array, TRUE);
523 /* frame.time --> geninfo.timestamp */
524 finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
525 if (g_ptr_array_len(finfo_array) < 1) {
528 timestamp = fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
529 g_ptr_array_free(finfo_array, TRUE);
531 /* Print geninfo start */
533 " <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
534 frame_finfo->length);
536 /* Print geninfo.num */
538 " <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
539 num, num, frame_finfo->length);
541 /* Print geninfo.len */
543 " <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Frame Length\" value=\"%x\" size=\"%u\"/>\n",
544 len, len, frame_finfo->length);
546 /* Print geninfo.caplen */
548 " <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
549 caplen, caplen, frame_finfo->length);
551 /* Print geninfo.timestamp */
553 " <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
554 abs_time_to_str(timestamp), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
556 /* Print geninfo end */
562 write_pdml_finale(FILE *fh)
564 fputs("</pdml>\n", fh);
568 write_psml_preamble(FILE *fh)
570 fputs("<?xml version=\"1.0\"?>\n", fh);
571 fputs("<psml version=\"" PSML_VERSION "\" ", fh);
572 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
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 */
581 if(edt->pi.fd->num == 1) {
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");
593 fprintf(fh, "<packet>\n");
595 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
596 fprintf(fh, "<section>");
597 print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
598 fprintf(fh, "</section>\n");
601 fprintf(fh, "</packet>\n\n");
605 write_psml_finale(FILE *fh)
607 fputs("</psml>\n", fh);
611 write_csv_preamble(FILE *fh _U_)
617 proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
621 /* if this is the first packet, we have to write the CSV header */
622 if(edt->pi.fd->num == 1) {
623 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
624 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_title[i]);
626 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_title[i]);
629 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
630 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_data[i]);
632 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_data[i]);
636 write_csv_finale(FILE *fh _U_)
642 write_carrays_preamble(FILE *fh _U_)
648 proto_tree_write_carrays(const guint8 *pd, guint32 len, guint32 num, FILE *fh)
655 fprintf(fh, "char pkt%u[] = {\n", num);
657 for (i = 0; i < len; i++) {
659 fprintf(fh, "0x%02x", *(pd + i));
661 if (i == (len - 1)) {
662 fprintf(fh, " };\n\n");
666 if (!((i + 1) % 8)) {
675 write_carrays_finale(FILE *fh _U_)
681 * Find the data source for a specified field, and return a pointer
682 * to the data in it. Returns NULL if the data is out of bounds.
684 static const guint8 *
685 get_field_data(GSList *src_list, field_info *fi)
690 gint length, tvbuff_length;
692 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
695 if (fi->ds_tvb == src_tvb) {
699 * XXX - a field can have a length that runs past
700 * the end of the tvbuff. Ideally, that should
701 * be fixed when adding an item to the protocol
702 * tree, but checking the length when doing
703 * that could be expensive. Until we fix that,
704 * we'll do the check here.
706 tvbuff_length = tvb_length_remaining(src_tvb,
708 if (tvbuff_length < 0) {
712 if (length > tvbuff_length)
713 length = tvbuff_length;
714 return tvb_get_ptr(src_tvb, fi->start, length);
717 g_assert_not_reached();
718 return NULL; /* not found */
721 /* Print a string, escaping out certain characters that need to
722 * escaped out for XML. */
724 print_escaped_xml(FILE *fh, const char *unescaped_string)
728 for (p = unescaped_string; *p != '\0'; p++) {
752 write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi)
760 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
761 fprintf(pdata->fh, "field length invalid!");
765 /* Find the data for this field. */
766 pd = get_field_data(pdata->src_list, fi);
769 /* Print a simple hex dump */
770 for (i = 0 ; i < fi->length; i++) {
771 fprintf(pdata->fh, "%02x", pd[i]);
777 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
779 gboolean multiple_sources;
788 /* We shouldn't be called with a NULL pointer here because we've
789 * created a visible protocol tree */
790 g_assert(edt->pi.data_src);
793 * Set "multiple_sources" iff this frame has more than one
794 * data source; if it does, we need to print the name of
795 * the data source before printing the data from the
798 multiple_sources = (edt->pi.data_src->next != NULL);
800 for (src_le = edt->pi.data_src; src_le != NULL;
801 src_le = src_le->next) {
804 if (multiple_sources) {
805 name = get_data_source_name(src);
806 print_line(stream, 0, "");
807 line = g_strdup_printf("%s:", name);
808 print_line(stream, 0, line);
811 length = tvb_length(tvb);
814 cp = tvb_get_ptr(tvb, 0, length);
815 if (!print_hex_data_buffer(stream, cp, length,
816 edt->pi.fd->flags.encoding))
823 * This routine is based on a routine created by Dan Lasley
824 * <DLASLEY@PROMUS.com>.
826 * It was modified for Wireshark by Gilbert Ramirez and others.
829 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
830 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
831 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
832 /* max number of characters hex dump takes -
833 2 digits plus trailing blank */
834 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
835 /* number of characters those bytes take;
836 3 characters per byte of hex dump,
837 2 blanks separating hex from ASCII,
838 1 character per byte of ASCII dump */
839 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
840 /* number of characters per line;
841 offset, 2 blanks separating offset
842 from data dump, data dump */
845 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
846 guint length, char_enc encoding)
848 register unsigned int ad, i, j, k, l;
850 guchar line[MAX_LINE_LEN + 1];
851 unsigned int use_digits;
852 static guchar binhex[16] = {
853 '0', '1', '2', '3', '4', '5', '6', '7',
854 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
856 if (!print_line(stream, 0, ""))
860 * How many of the leading digits of the offset will we supply?
861 * We always supply at least 4 digits, but if the maximum offset
862 * won't fit in 4 digits, we use as many digits as will be needed.
864 if (((length - 1) & 0xF0000000) != 0)
865 use_digits = 8; /* need all 8 digits */
866 else if (((length - 1) & 0x0F000000) != 0)
867 use_digits = 7; /* need 7 digits */
868 else if (((length - 1) & 0x00F00000) != 0)
869 use_digits = 6; /* need 6 digits */
870 else if (((length - 1) & 0x000F0000) != 0)
871 use_digits = 5; /* need 5 digits */
873 use_digits = 4; /* we'll supply 4 digits */
882 * Start of a new line.
889 c = (ad >> (l*4)) & 0xF;
890 line[j++] = binhex[c];
894 memset(line+j, ' ', DATA_DUMP_LEN);
897 * Offset in line of ASCII dump.
899 k = j + HEX_DUMP_LEN + 2;
902 line[j++] = binhex[c>>4];
903 line[j++] = binhex[c&0xf];
905 if (encoding == CHAR_EBCDIC) {
906 c = EBCDIC_to_ASCII1(c);
908 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
910 if ((i & 15) == 0 || i == length) {
912 * We'll be starting a new line, or
913 * we're finished printing this buffer;
914 * dump out the line we've constructed,
915 * and advance the offset.
918 if (!print_line(stream, 0, line))
927 void ps_clean_string(unsigned char *out, const unsigned char *in,
933 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
954 /* Some formats need stuff at the beginning of the output */
956 print_preamble(print_stream_t *self, gchar *filename)
958 return (self->ops->print_preamble)(self, filename);
962 print_line(print_stream_t *self, int indent, const char *line)
964 return (self->ops->print_line)(self, indent, line);
967 /* Insert bookmark */
969 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
971 return (self->ops->print_bookmark)(self, name, title);
975 new_page(print_stream_t *self)
977 return (self->ops->new_page)(self);
980 /* Some formats need stuff at the end of the output */
982 print_finale(print_stream_t *self)
984 return (self->ops->print_finale)(self);
988 destroy_print_stream(print_stream_t *self)
990 return (self->ops->destroy)(self);
999 print_preamble_text(print_stream_t *self _U_, gchar *filename _U_)
1002 return TRUE; /* always succeeds */
1006 print_line_text(print_stream_t *self, int indent, const char *line)
1008 output_text *output = self->data;
1009 char space[MAX_INDENT+1];
1013 /* Prepare the tabs for printing, depending on tree level */
1014 num_spaces = indent * 4;
1015 if (num_spaces > MAX_INDENT) {
1016 num_spaces = MAX_INDENT;
1018 for (i = 0; i < num_spaces; i++) {
1021 /* The string is NUL-terminated */
1022 space[num_spaces] = '\0';
1024 fputs(space, output->fh);
1025 fputs(line, output->fh);
1026 putc('\n', output->fh);
1027 return !ferror(output->fh);
1031 print_bookmark_text(print_stream_t *self _U_, const gchar *name _U_,
1032 const gchar *title _U_)
1039 new_page_text(print_stream_t *self)
1041 output_text *output = self->data;
1043 fputs("\f", output->fh);
1044 return !ferror(output->fh);
1048 print_finale_text(print_stream_t *self _U_)
1051 return TRUE; /* always succeeds */
1055 destroy_text(print_stream_t *self)
1057 output_text *output = self->data;
1060 ret = close_print_dest(output->to_file, output->fh);
1066 static const print_stream_ops_t print_text_ops = {
1067 print_preamble_text,
1069 print_bookmark_text,
1076 print_stream_text_new(int to_file, const char *dest)
1079 print_stream_t *stream;
1080 output_text *output;
1082 fh = open_print_dest(to_file, dest);
1086 output = g_malloc(sizeof *output);
1087 output->to_file = to_file;
1089 stream = g_malloc(sizeof (print_stream_t));
1090 stream->ops = &print_text_ops;
1091 stream->data = output;
1097 print_stream_text_stdio_new(FILE *fh)
1099 print_stream_t *stream;
1100 output_text *output;
1102 output = g_malloc(sizeof *output);
1103 output->to_file = TRUE;
1105 stream = g_malloc(sizeof (print_stream_t));
1106 stream->ops = &print_text_ops;
1107 stream->data = output;
1118 print_preamble_ps(print_stream_t *self, gchar *filename)
1120 output_ps *output = self->data;
1121 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1123 print_ps_preamble(output->fh);
1125 fputs("%% Set the font to 8 point\n", output->fh);
1126 fputs("/Courier findfont 8 scalefont setfont\n", output->fh);
1127 fputs("\n", output->fh);
1128 fputs("%% the page title\n", output->fh);
1129 ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
1130 fprintf(output->fh, "/ws_pagetitle (%s - Wireshark " VERSION "%s) def\n", psbuffer, wireshark_svnversion);
1131 fputs("\n", output->fh);
1132 return !ferror(output->fh);
1136 print_line_ps(print_stream_t *self, int indent, const char *line)
1138 output_ps *output = self->data;
1139 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1141 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
1142 fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
1143 return !ferror(output->fh);
1147 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
1149 output_ps *output = self->data;
1150 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1153 * See the Adobe "pdfmark reference":
1155 * http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
1157 * The pdfmark stuff tells code that turns PostScript into PDF
1158 * things that it should do.
1160 * The /OUT stuff creates a bookmark that goes to the
1161 * destination with "name" as the name and "title" as the title.
1163 * The "/DEST" creates the destination.
1165 ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
1166 fprintf(output->fh, "[/Dest /%s /Title (%s) /OUT pdfmark\n", name,
1168 fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
1170 fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
1172 fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
1173 return !ferror(output->fh);
1177 new_page_ps(print_stream_t *self)
1179 output_ps *output = self->data;
1181 fputs("formfeed\n", output->fh);
1182 return !ferror(output->fh);
1186 print_finale_ps(print_stream_t *self)
1188 output_ps *output = self->data;
1190 print_ps_finale(output->fh);
1191 return !ferror(output->fh);
1195 destroy_ps(print_stream_t *self)
1197 output_ps *output = self->data;
1200 ret = close_print_dest(output->to_file, output->fh);
1206 static const print_stream_ops_t print_ps_ops = {
1216 print_stream_ps_new(int to_file, const char *dest)
1219 print_stream_t *stream;
1222 fh = open_print_dest(to_file, dest);
1226 output = g_malloc(sizeof *output);
1227 output->to_file = to_file;
1229 stream = g_malloc(sizeof (print_stream_t));
1230 stream->ops = &print_ps_ops;
1231 stream->data = output;
1237 print_stream_ps_stdio_new(FILE *fh)
1239 print_stream_t *stream;
1242 output = g_malloc(sizeof *output);
1243 output->to_file = TRUE;
1245 stream = g_malloc(sizeof (print_stream_t));
1246 stream->ops = &print_ps_ops;
1247 stream->data = output;
1252 output_fields_t* output_fields_new()
1254 output_fields_t* fields = g_new(output_fields_t, 1);
1255 fields->print_header = FALSE;
1256 fields->separator = '\t';
1257 fields->fields = NULL; /*Do lazy initialisation */
1258 fields->field_indicies = NULL;
1259 fields->field_values = NULL;
1264 gsize output_fields_num_fields(output_fields_t* fields)
1268 if(NULL == fields->fields) {
1271 return fields->fields->len;
1275 void output_fields_free(output_fields_t* fields)
1279 if(NULL != fields->field_indicies) {
1280 /* Keys are stored in fields->fields, values are
1283 g_hash_table_destroy(fields->field_indicies);
1285 if(NULL != fields->fields) {
1287 for(i = 0; i < fields->fields->len; ++i) {
1288 gchar* field = g_ptr_array_index(fields->fields,i);
1291 g_ptr_array_free(fields->fields, TRUE);
1297 void output_fields_add(output_fields_t* fields, const gchar* field)
1305 if(NULL == fields->fields) {
1306 fields->fields = g_ptr_array_new();
1309 field_copy = g_strdup(field);
1311 g_ptr_array_add(fields->fields, field_copy);
1314 gboolean output_fields_set_option(output_fields_t* info, gchar* option)
1316 const gchar* option_name;
1317 const gchar* option_value;
1322 if('\0' == *option) {
1323 return FALSE; /* Is this guarded against by option parsing? */
1325 option_name = strtok(option,"=");
1326 option_value = option + strlen(option_name) + 1;
1327 if(0 == strcmp(option_name, "header")) {
1328 switch(NULL == option_value ? '\0' : *option_value) {
1330 info->print_header = FALSE;
1333 info->print_header = TRUE;
1341 if(0 == strcmp(option_name,"separator")) {
1342 switch(NULL == option_value ? '\0' : *option_value) {
1346 switch(*++option_value) {
1348 info->separator = '\t';
1351 info->separator = ' ';
1354 info->separator = '\\';
1358 info->separator = *option_value;
1364 if(0 == strcmp(option_name, "quote")) {
1365 switch(NULL == option_value ? '\0' : *option_value) {
1366 default: /* Fall through */
1386 void output_fields_list_options(FILE *fh)
1388 fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
1389 fputs("header=y|n Print field abbreviations as first line of output (def: N: no)\n", fh);
1390 fputs("separator=/t|/s|<character> Set the separator to use; \"/t\" = tab,\n \"/s\" = space (def: /t: tab)\n", fh);
1391 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);
1395 void write_fields_preamble(output_fields_t* fields, FILE *fh)
1402 if(!fields->print_header) {
1406 for(i = 0; i < fields->fields->len; ++i) {
1407 const gchar* field = g_ptr_array_index(fields->fields,i);
1409 fputc(fields->separator, fh);
1416 static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
1418 write_field_data_t *call_data;
1420 gpointer field_index;
1423 fi = PNODE_FINFO(node);
1425 g_assert(fi && "dissection with an invisible proto tree?");
1427 field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
1428 if(NULL != field_index) {
1431 value = get_node_field_value(fi, call_data->edt); /* ep_alloced string */
1433 if(NULL != value && '\0' != *value) {
1435 actual_index = GPOINTER_TO_UINT(field_index);
1436 /* Unwrap change made to disambiguiate zero / null */
1437 call_data->fields->field_values[actual_index - 1] = value;
1442 if (node->first_child != NULL) {
1443 proto_tree_children_foreach(node, proto_tree_get_node_field_values,
1448 void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, FILE *fh)
1452 write_field_data_t data;
1458 data.fields = fields;
1461 if(NULL == fields->field_indicies) {
1462 /* Prepare a lookup table from string abbreviation for field to its index. */
1463 fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
1466 while( i < fields->fields->len) {
1467 gchar* field = g_ptr_array_index(fields->fields, i);
1468 /* Store field indicies +1 so that zero is not a valid value,
1469 * and can be distinguished from NULL as a pointer.
1472 g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
1476 /* Buffer to store values for this packet */
1477 fields->field_values = ep_alloc_array0(const gchar*, fields->fields->len);
1479 proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
1482 for(i = 0; i < fields->fields->len; ++i) {
1484 fputc(fields->separator, fh);
1486 if(NULL != fields->field_values[i]) {
1487 if(fields->quote != '\0') {
1488 fputc(fields->quote, fh);
1490 fputs(fields->field_values[i], fh);
1491 if(fields->quote != '\0') {
1492 fputc(fields->quote, fh);
1498 void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
1503 /* Returns an ep_alloced string or a static constant*/
1504 const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
1506 if (fi->hfinfo->id == hf_text_only) {
1510 return fi->rep->representation;
1513 return get_field_hex_value(edt->pi.data_src, fi);
1516 else if (fi->hfinfo->id == proto_data) {
1517 /* Uninterpreted data, i.e., the "Data" protocol, is
1518 * printed as a field instead of a protocol. */
1519 return get_field_hex_value(edt->pi.data_src, fi);
1522 /* Normal protocols and fields */
1523 gchar *dfilter_string;
1526 switch (fi->hfinfo->type)
1529 /* Print out the full details for the protocol. */
1531 return fi->rep->representation;
1533 /* Just print out the protocol abbreviation */
1534 return fi->hfinfo->abbrev;;
1537 /* Return "1" so that the presence of a field of type
1538 * FT_NONE can be checked when using -T fields */
1541 /* XXX - this is a hack until we can just call
1542 * fvalue_to_string_repr() for *all* FT_* types. */
1543 dfilter_string = proto_construct_match_selected_string(fi,
1545 if (dfilter_string != NULL) {
1546 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
1548 /* XXX - Remove double-quotes. Again, once we
1549 * can call fvalue_to_string_repr(), we can
1550 * ask it not to produce the version for
1551 * display-filters, and thus, no
1553 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
1554 dfilter_string[strlen(dfilter_string)-1] = '\0';
1558 return &(dfilter_string[chop_len]);
1560 return get_field_hex_value(edt->pi.data_src, fi);
1567 get_field_hex_value(GSList* src_list, field_info *fi)
1574 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
1575 return "field length invalid!";
1578 /* Find the data for this field. */
1579 pd = get_field_data(src_list, fi);
1586 const int chars_per_byte = 2;
1588 len = chars_per_byte * fi->length;
1589 buffer = ep_alloc_array(gchar, len + 1);
1590 buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
1592 /* Print a simple hex dump */
1593 for (i = 0 ; i < fi->length; i++) {
1594 g_snprintf(p, chars_per_byte+1, "%02x", pd[i]);
1595 p += chars_per_byte;