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 = PITEM_FINFO(node);
155 print_data *pdata = (print_data*) data;
157 gchar label_str[ITEM_LABEL_LENGTH];
160 /* Don't print invisible entries. */
161 if (PROTO_ITEM_IS_HIDDEN(node))
164 /* Give up if we've already gotten an error. */
168 /* was a free format label produced? */
170 label_ptr = fi->rep->representation;
172 else { /* no, make a generic label */
173 label_ptr = label_str;
174 proto_item_fill_label(fi, label_str);
177 if (PROTO_ITEM_IS_GENERATED(node)) {
178 label_ptr = g_strdup_printf("[%s]", label_ptr);
181 if (!print_line(pdata->stream, pdata->level, label_ptr)) {
182 pdata->success = FALSE;
186 if (PROTO_ITEM_IS_GENERATED(node)) {
190 /* If it's uninterpreted data, dump it (unless our caller will
191 be printing the entire packet in hex). */
192 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
194 * Find the data for this field.
196 pd = get_field_data(pdata->src_list, fi);
198 if (!print_hex_data_buffer(pdata->stream, pd,
199 fi->length, pdata->encoding)) {
200 pdata->success = FALSE;
206 /* If we're printing all levels, or if this node is one with a
207 subtree and its subtree is expanded, recurse into the subtree,
209 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
210 if (pdata->print_dissections == print_dissections_expanded ||
211 (pdata->print_dissections == print_dissections_as_displayed &&
212 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
213 if (node->first_child != NULL) {
215 proto_tree_children_foreach(node,
216 proto_tree_print_node, pdata);
225 write_pdml_preamble(FILE *fh)
227 fputs("<?xml version=\"1.0\"?>\n", fh);
228 fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
229 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
233 proto_tree_write_pdml(epan_dissect_t *edt, FILE *fh)
235 write_pdml_data data;
237 /* Create the output */
240 data.src_list = edt->pi.data_src;
243 fprintf(fh, "<packet>\n");
245 /* Print a "geninfo" protocol as required by PDML */
246 print_pdml_geninfo(edt->tree, fh);
248 proto_tree_children_foreach(edt->tree, proto_tree_write_node_pdml,
251 fprintf(fh, "</packet>\n\n");
254 /* Write out a tree's data, and any child nodes, as PDML */
256 proto_tree_write_node_pdml(proto_node *node, gpointer data)
258 field_info *fi = PITEM_FINFO(node);
259 write_pdml_data *pdata = (write_pdml_data*) data;
260 const gchar *label_ptr;
261 gchar label_str[ITEM_LABEL_LENGTH];
262 char *dfilter_string;
266 /* Will wrap up top-level field items inside a fake protocol wrapper to
267 preserve the PDML schema */
268 gboolean wrap_in_fake_protocol =
269 (((fi->hfinfo->type != FT_PROTOCOL) ||
270 (fi->hfinfo->id == proto_data)) &&
271 (pdata->level == 0));
273 /* Indent to the correct level */
274 for (i = -1; i < pdata->level; i++) {
275 fputs(" ", pdata->fh);
278 if (wrap_in_fake_protocol) {
279 /* Open fake protocol wrapper */
280 fputs("<proto name=\"fake-field-wrapper\">\n", pdata->fh);
282 /* Indent to increased level before writint out field */
284 for (i = -1; i < pdata->level; i++) {
285 fputs(" ", pdata->fh);
289 /* Text label. It's printed as a field with no name. */
290 if (fi->hfinfo->id == hf_text_only) {
293 label_ptr = fi->rep->representation;
299 /* Show empty name since it is a required field */
300 fputs("<field name=\"", pdata->fh);
301 fputs("\" show=\"", pdata->fh);
302 print_escaped_xml(pdata->fh, label_ptr);
304 fprintf(pdata->fh, "\" size=\"%d", fi->length);
305 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
307 fputs("\" value=\"", pdata->fh);
308 write_pdml_field_hex_value(pdata, fi);
310 if (node->first_child != NULL) {
311 fputs("\">\n", pdata->fh);
314 fputs("\"/>\n", pdata->fh);
318 /* Uninterpreted data, i.e., the "Data" protocol, is
319 * printed as a field instead of a protocol. */
320 else if (fi->hfinfo->id == proto_data) {
322 /* Write out field with data */
323 fputs("<field name=\"data\" value=\"", pdata->fh);
324 write_pdml_field_hex_value(pdata, fi);
325 fputs("\"/>\n", pdata->fh);
327 /* Normal protocols and fields */
329 if (fi->hfinfo->type == FT_PROTOCOL) {
330 fputs("<proto name=\"", pdata->fh);
333 fputs("<field name=\"", pdata->fh);
335 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
339 * http://analyzer.polito.it/30alpha/docs/dissectors/PDMLSpec.htm
341 * the show fields contains things in 'human readable' format
342 * showname: contains only the name of the field
343 * show: contains only the data of the field
344 * showdtl: contains additional details of the field data
345 * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
347 * XXX - the showname shouldn't contain the field data itself
348 * (like it's contained in the fi->rep->representation).
349 * Unfortunately, we don't have the field data representation for
350 * all fields, so this isn't currently possible */
351 fputs("\" showname=\"", pdata->fh);
352 print_escaped_xml(pdata->fh, fi->hfinfo->name);
356 fputs("\" showname=\"", pdata->fh);
357 print_escaped_xml(pdata->fh, fi->rep->representation);
360 label_ptr = label_str;
361 proto_item_fill_label(fi, label_str);
362 fputs("\" showname=\"", pdata->fh);
363 print_escaped_xml(pdata->fh, label_ptr);
366 if (PROTO_ITEM_IS_HIDDEN(node))
367 fprintf(pdata->fh, "\" hide=\"yes");
369 fprintf(pdata->fh, "\" size=\"%d", fi->length);
370 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
371 /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
373 /* show, value, and unmaskedvalue attributes */
374 switch (fi->hfinfo->type)
379 fputs("\" show=\"\" value=\"", pdata->fh);
382 /* XXX - this is a hack until we can just call
383 * fvalue_to_string_repr() for *all* FT_* types. */
384 dfilter_string = proto_construct_match_selected_string(fi,
386 if (dfilter_string != NULL) {
387 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
389 /* XXX - Remove double-quotes. Again, once we
390 * can call fvalue_to_string_repr(), we can
391 * ask it not to produce the version for
392 * display-filters, and thus, no
394 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
395 dfilter_string[strlen(dfilter_string)-1] = '\0';
399 fputs("\" show=\"", pdata->fh);
400 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
404 * XXX - should we omit "value" for any fields?
405 * What should we do for fields whose length is 0?
406 * They might come from a pseudo-header or from
407 * the capture header (e.g., time stamps), or
408 * they might be generated fields.
410 if (fi->length > 0) {
411 fputs("\" value=\"", pdata->fh);
413 if (fi->hfinfo->bitmask!=0) {
414 fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
415 fputs("\" unmaskedvalue=\"", pdata->fh);
416 write_pdml_field_hex_value(pdata, fi);
419 write_pdml_field_hex_value(pdata, fi);
424 if (node->first_child != NULL) {
425 fputs("\">\n", pdata->fh);
427 else if (fi->hfinfo->id == proto_data) {
428 fputs("\">\n", pdata->fh);
431 fputs("\"/>\n", pdata->fh);
435 /* We always print all levels for PDML. Recurse here. */
436 if (node->first_child != NULL) {
438 proto_tree_children_foreach(node,
439 proto_tree_write_node_pdml, pdata);
443 /* Take back the extra level we added for fake wrapper protocol */
444 if (wrap_in_fake_protocol) {
448 if (node->first_child != NULL) {
449 /* Indent to correct level */
450 for (i = -1; i < pdata->level; i++) {
451 fputs(" ", pdata->fh);
453 /* Close off current element */
454 if (fi->hfinfo->id != proto_data) { /* Data protocol uses simple tags */
455 if (fi->hfinfo->type == FT_PROTOCOL) {
456 fputs("</proto>\n", pdata->fh);
459 fputs("</field>\n", pdata->fh);
464 /* Close off fake wrapper protocol */
465 if (wrap_in_fake_protocol) {
466 fputs("</proto>\n", pdata->fh);
470 /* Print info for a 'geninfo' pseudo-protocol. This is required by
471 * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
472 * but we produce a 'geninfo' protocol in the PDML to conform to spec.
473 * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
475 print_pdml_geninfo(proto_tree *tree, FILE *fh)
477 guint32 num, len, caplen;
479 GPtrArray *finfo_array;
480 field_info *frame_finfo;
482 /* Get frame protocol's finfo. */
483 finfo_array = proto_find_finfo(tree, proto_frame);
484 if (g_ptr_array_len(finfo_array) < 1) {
487 frame_finfo = finfo_array->pdata[0];
488 g_ptr_array_free(finfo_array, FALSE);
490 /* frame.number --> geninfo.num */
491 finfo_array = proto_find_finfo(tree, hf_frame_number);
492 if (g_ptr_array_len(finfo_array) < 1) {
495 num = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
496 g_ptr_array_free(finfo_array, FALSE);
498 /* frame.frame_len --> geninfo.len */
499 finfo_array = proto_find_finfo(tree, hf_frame_len);
500 if (g_ptr_array_len(finfo_array) < 1) {
503 len = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
504 g_ptr_array_free(finfo_array, FALSE);
506 /* frame.cap_len --> geninfo.caplen */
507 finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
508 if (g_ptr_array_len(finfo_array) < 1) {
511 caplen = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
512 g_ptr_array_free(finfo_array, FALSE);
514 /* frame.time --> geninfo.timestamp */
515 finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
516 if (g_ptr_array_len(finfo_array) < 1) {
519 timestamp = fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
520 g_ptr_array_free(finfo_array, FALSE);
522 /* Print geninfo start */
524 " <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
525 frame_finfo->length);
527 /* Print geninfo.num */
529 " <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
530 num, num, frame_finfo->length);
532 /* Print geninfo.len */
534 " <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Frame Length\" value=\"%x\" size=\"%u\"/>\n",
535 len, len, frame_finfo->length);
537 /* Print geninfo.caplen */
539 " <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
540 caplen, caplen, frame_finfo->length);
542 /* Print geninfo.timestamp */
544 " <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
545 abs_time_to_str(timestamp), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
547 /* Print geninfo end */
553 write_pdml_finale(FILE *fh)
555 fputs("</pdml>\n", fh);
559 write_psml_preamble(FILE *fh)
561 fputs("<?xml version=\"1.0\"?>\n", fh);
562 fputs("<psml version=\"" PSML_VERSION "\" ", fh);
563 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
567 proto_tree_write_psml(epan_dissect_t *edt, FILE *fh)
571 /* if this is the first packet, we have to create the PSML structure output */
572 if(edt->pi.fd->num == 1) {
573 fprintf(fh, "<structure>\n");
575 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
576 fprintf(fh, "<section>");
577 print_escaped_xml(fh, edt->pi.cinfo->col_title[i]);
578 fprintf(fh, "</section>\n");
581 fprintf(fh, "</structure>\n\n");
584 fprintf(fh, "<packet>\n");
586 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
587 fprintf(fh, "<section>");
588 print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
589 fprintf(fh, "</section>\n");
592 fprintf(fh, "</packet>\n\n");
596 write_psml_finale(FILE *fh)
598 fputs("</psml>\n", fh);
602 write_csv_preamble(FILE *fh _U_)
608 proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
612 /* if this is the first packet, we have to write the CSV header */
613 if(edt->pi.fd->num == 1) {
614 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
615 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_title[i]);
617 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_title[i]);
620 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
621 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_data[i]);
623 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_data[i]);
627 write_csv_finale(FILE *fh _U_)
633 write_carrays_preamble(FILE *fh _U_)
639 proto_tree_write_carrays(const guint8 *pd, guint32 len, guint32 num, FILE *fh)
646 fprintf(fh, "char pkt%u[] = {\n", num);
648 for (i = 0; i < len; i++) {
650 fprintf(fh, "0x%02x", *(pd + i));
652 if (i == (len - 1)) {
653 fprintf(fh, " };\n\n");
657 if (!((i + 1) % 8)) {
666 write_carrays_finale(FILE *fh _U_)
672 * Find the data source for a specified field, and return a pointer
673 * to the data in it. Returns NULL if the data is out of bounds.
675 static const guint8 *
676 get_field_data(GSList *src_list, field_info *fi)
681 gint length, tvbuff_length;
683 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
686 if (fi->ds_tvb == src_tvb) {
690 * XXX - a field can have a length that runs past
691 * the end of the tvbuff. Ideally, that should
692 * be fixed when adding an item to the protocol
693 * tree, but checking the length when doing
694 * that could be expensive. Until we fix that,
695 * we'll do the check here.
697 tvbuff_length = tvb_length_remaining(src_tvb,
699 if (tvbuff_length < 0) {
703 if (length > tvbuff_length)
704 length = tvbuff_length;
705 return tvb_get_ptr(src_tvb, fi->start, length);
708 g_assert_not_reached();
709 return NULL; /* not found */
712 /* Print a string, escaping out certain characters that need to
713 * escaped out for XML. */
715 print_escaped_xml(FILE *fh, const char *unescaped_string)
719 for (p = unescaped_string; *p != '\0'; p++) {
743 write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi)
751 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
752 fprintf(pdata->fh, "field length invalid!");
756 /* Find the data for this field. */
757 pd = get_field_data(pdata->src_list, fi);
760 /* Print a simple hex dump */
761 for (i = 0 ; i < fi->length; i++) {
762 fprintf(pdata->fh, "%02x", pd[i]);
768 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
770 gboolean multiple_sources;
780 * Set "multiple_sources" iff this frame has more than one
781 * data source; if it does, we need to print the name of
782 * the data source before printing the data from the
785 multiple_sources = (edt->pi.data_src->next != NULL);
787 for (src_le = edt->pi.data_src; src_le != NULL;
788 src_le = src_le->next) {
791 if (multiple_sources) {
793 print_line(stream, 0, "");
794 line = g_strdup_printf("%s:", name);
795 print_line(stream, 0, line);
798 length = tvb_length(tvb);
801 cp = tvb_get_ptr(tvb, 0, length);
802 if (!print_hex_data_buffer(stream, cp, length,
803 edt->pi.fd->flags.encoding))
810 * This routine is based on a routine created by Dan Lasley
811 * <DLASLEY@PROMUS.com>.
813 * It was modified for Wireshark by Gilbert Ramirez and others.
816 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
817 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
818 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
819 /* max number of characters hex dump takes -
820 2 digits plus trailing blank */
821 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
822 /* number of characters those bytes take;
823 3 characters per byte of hex dump,
824 2 blanks separating hex from ASCII,
825 1 character per byte of ASCII dump */
826 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
827 /* number of characters per line;
828 offset, 2 blanks separating offset
829 from data dump, data dump */
832 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
833 guint length, char_enc encoding)
835 register unsigned int ad, i, j, k, l;
837 guchar line[MAX_LINE_LEN + 1];
838 unsigned int use_digits;
839 static guchar binhex[16] = {
840 '0', '1', '2', '3', '4', '5', '6', '7',
841 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
843 if (!print_line(stream, 0, ""))
847 * How many of the leading digits of the offset will we supply?
848 * We always supply at least 4 digits, but if the maximum offset
849 * won't fit in 4 digits, we use as many digits as will be needed.
851 if (((length - 1) & 0xF0000000) != 0)
852 use_digits = 8; /* need all 8 digits */
853 else if (((length - 1) & 0x0F000000) != 0)
854 use_digits = 7; /* need 7 digits */
855 else if (((length - 1) & 0x00F00000) != 0)
856 use_digits = 6; /* need 6 digits */
857 else if (((length - 1) & 0x000F0000) != 0)
858 use_digits = 5; /* need 5 digits */
860 use_digits = 4; /* we'll supply 4 digits */
869 * Start of a new line.
876 c = (ad >> (l*4)) & 0xF;
877 line[j++] = binhex[c];
881 memset(line+j, ' ', DATA_DUMP_LEN);
884 * Offset in line of ASCII dump.
886 k = j + HEX_DUMP_LEN + 2;
889 line[j++] = binhex[c>>4];
890 line[j++] = binhex[c&0xf];
892 if (encoding == CHAR_EBCDIC) {
893 c = EBCDIC_to_ASCII1(c);
895 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
897 if ((i & 15) == 0 || i == length) {
899 * We'll be starting a new line, or
900 * we're finished printing this buffer;
901 * dump out the line we've constructed,
902 * and advance the offset.
905 if (!print_line(stream, 0, line))
914 void ps_clean_string(unsigned char *out, const unsigned char *in,
920 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
941 /* Some formats need stuff at the beginning of the output */
943 print_preamble(print_stream_t *self, gchar *filename)
945 return (self->ops->print_preamble)(self, filename);
949 print_line(print_stream_t *self, int indent, const char *line)
951 return (self->ops->print_line)(self, indent, line);
954 /* Insert bookmark */
956 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
958 return (self->ops->print_bookmark)(self, name, title);
962 new_page(print_stream_t *self)
964 return (self->ops->new_page)(self);
967 /* Some formats need stuff at the end of the output */
969 print_finale(print_stream_t *self)
971 return (self->ops->print_finale)(self);
975 destroy_print_stream(print_stream_t *self)
977 return (self->ops->destroy)(self);
986 print_preamble_text(print_stream_t *self _U_, gchar *filename _U_)
989 return TRUE; /* always succeeds */
993 print_line_text(print_stream_t *self, int indent, const char *line)
995 output_text *output = self->data;
996 char space[MAX_INDENT+1];
1000 /* Prepare the tabs for printing, depending on tree level */
1001 num_spaces = indent * 4;
1002 if (num_spaces > MAX_INDENT) {
1003 num_spaces = MAX_INDENT;
1005 for (i = 0; i < num_spaces; i++) {
1008 /* The string is NUL-terminated */
1009 space[num_spaces] = '\0';
1011 fputs(space, output->fh);
1012 fputs(line, output->fh);
1013 putc('\n', output->fh);
1014 return !ferror(output->fh);
1018 print_bookmark_text(print_stream_t *self _U_, const gchar *name _U_,
1019 const gchar *title _U_)
1026 new_page_text(print_stream_t *self)
1028 output_text *output = self->data;
1030 fputs("\f", output->fh);
1031 return !ferror(output->fh);
1035 print_finale_text(print_stream_t *self _U_)
1038 return TRUE; /* always succeeds */
1042 destroy_text(print_stream_t *self)
1044 output_text *output = self->data;
1047 ret = close_print_dest(output->to_file, output->fh);
1053 static const print_stream_ops_t print_text_ops = {
1054 print_preamble_text,
1056 print_bookmark_text,
1063 print_stream_text_new(int to_file, const char *dest)
1066 print_stream_t *stream;
1067 output_text *output;
1069 fh = open_print_dest(to_file, dest);
1073 output = g_malloc(sizeof *output);
1074 output->to_file = to_file;
1076 stream = g_malloc(sizeof (print_stream_t));
1077 stream->ops = &print_text_ops;
1078 stream->data = output;
1084 print_stream_text_stdio_new(FILE *fh)
1086 print_stream_t *stream;
1087 output_text *output;
1089 output = g_malloc(sizeof *output);
1090 output->to_file = TRUE;
1092 stream = g_malloc(sizeof (print_stream_t));
1093 stream->ops = &print_text_ops;
1094 stream->data = output;
1105 print_preamble_ps(print_stream_t *self, gchar *filename)
1107 output_ps *output = self->data;
1108 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1110 print_ps_preamble(output->fh);
1112 fputs("%% Set the font to 8 point\n", output->fh);
1113 fputs("/Courier findfont 8 scalefont setfont\n", output->fh);
1114 fputs("\n", output->fh);
1115 fputs("%% the page title\n", output->fh);
1116 ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
1117 fprintf(output->fh, "/ws_pagetitle (%s - Wireshark " VERSION "%s) def\n", psbuffer, wireshark_svnversion);
1118 fputs("\n", output->fh);
1119 return !ferror(output->fh);
1123 print_line_ps(print_stream_t *self, int indent, const char *line)
1125 output_ps *output = self->data;
1126 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1128 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
1129 fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
1130 return !ferror(output->fh);
1134 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
1136 output_ps *output = self->data;
1137 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1140 * See the Adobe "pdfmark reference":
1142 * http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
1144 * The pdfmark stuff tells code that turns PostScript into PDF
1145 * things that it should do.
1147 * The /OUT stuff creates a bookmark that goes to the
1148 * destination with "name" as the name and "title" as the title.
1150 * The "/DEST" creates the destination.
1152 ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
1153 fprintf(output->fh, "[/Dest /%s /Title (%s) /OUT pdfmark\n", name,
1155 fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
1157 fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
1159 fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
1160 return !ferror(output->fh);
1164 new_page_ps(print_stream_t *self)
1166 output_ps *output = self->data;
1168 fputs("formfeed\n", output->fh);
1169 return !ferror(output->fh);
1173 print_finale_ps(print_stream_t *self)
1175 output_ps *output = self->data;
1177 print_ps_finale(output->fh);
1178 return !ferror(output->fh);
1182 destroy_ps(print_stream_t *self)
1184 output_ps *output = self->data;
1187 ret = close_print_dest(output->to_file, output->fh);
1193 static const print_stream_ops_t print_ps_ops = {
1203 print_stream_ps_new(int to_file, const char *dest)
1206 print_stream_t *stream;
1209 fh = open_print_dest(to_file, dest);
1213 output = g_malloc(sizeof *output);
1214 output->to_file = to_file;
1216 stream = g_malloc(sizeof (print_stream_t));
1217 stream->ops = &print_ps_ops;
1218 stream->data = output;
1224 print_stream_ps_stdio_new(FILE *fh)
1226 print_stream_t *stream;
1229 output = g_malloc(sizeof *output);
1230 output->to_file = TRUE;
1232 stream = g_malloc(sizeof (print_stream_t));
1233 stream->ops = &print_ps_ops;
1234 stream->data = output;
1239 output_fields_t* output_fields_new()
1241 output_fields_t* fields = g_new(output_fields_t, 1);
1242 fields->print_header = FALSE;
1243 fields->separator = '\t';
1244 fields->fields = NULL; /*Do lazy initialisation */
1245 fields->field_indicies = NULL;
1246 fields->field_values = NULL;
1251 gsize output_fields_num_fields(output_fields_t* fields)
1255 if(NULL == fields->fields) {
1258 return fields->fields->len;
1262 void output_fields_free(output_fields_t* fields)
1266 if(NULL != fields->field_indicies) {
1267 /* Keys are stored in fields->fields, values are
1270 g_hash_table_destroy(fields->field_indicies);
1272 if(NULL != fields->fields) {
1274 for(i = 0; i < fields->fields->len; ++i) {
1275 gchar* field = g_ptr_array_index(fields->fields,i);
1278 g_ptr_array_free(fields->fields, TRUE);
1284 void output_fields_add(output_fields_t* fields, const gchar* field)
1292 if(NULL == fields->fields) {
1293 fields->fields = g_ptr_array_new();
1296 field_copy = g_strdup(field);
1298 g_ptr_array_add(fields->fields, field_copy);
1301 gboolean output_fields_set_option(output_fields_t* info, gchar* option)
1303 const gchar* option_name;
1304 const gchar* option_value;
1309 if('\0' == *option) {
1310 return FALSE; /* Is this guarded against by option parsing? */
1312 option_name = strtok(option,"=");
1313 option_value = option + strlen(option_name) + 1;
1314 if(0 == strcmp(option_name, "header")) {
1315 switch(NULL == option_value ? '\0' : *option_value) {
1317 info->print_header = FALSE;
1320 info->print_header = TRUE;
1328 if(0 == strcmp(option_name,"separator")) {
1329 switch(NULL == option_value ? '\0' : *option_value) {
1333 switch(*++option_value) {
1335 info->separator = '\t';
1338 info->separator = ' ';
1341 info->separator = '\\';
1345 info->separator = *option_value;
1351 if(0 == strcmp(option_name, "quote")) {
1352 switch(NULL == option_value ? '\0' : *option_value) {
1353 default: /* Fall through */
1373 void output_fields_list_options(FILE *fh)
1375 fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
1376 fputs("header=y|n Print field abbreviations as first line of output (def: N: no)\n", fh);
1377 fputs("separator=/t|/s|<character> Set the separator to use; \"/t\" = tab,\n \"/s\" = space (def: /t: tab)\n", fh);
1378 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);
1382 void write_fields_preamble(output_fields_t* fields, FILE *fh)
1389 if(!fields->print_header) {
1393 for(i = 0; i < fields->fields->len; ++i) {
1394 const gchar* field = g_ptr_array_index(fields->fields,i);
1396 fputc(fields->separator, fh);
1403 static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
1405 write_field_data_t *call_data;
1407 gpointer field_index;
1410 fi = PITEM_FINFO(node);
1412 field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
1413 if(NULL != field_index) {
1416 value = get_node_field_value(fi, call_data->edt); /* ep_alloced string */
1418 if(NULL != value && '\0' != *value) {
1420 actual_index = GPOINTER_TO_UINT(field_index);
1421 /* Unwrap change made to disambiguiate zero / null */
1422 call_data->fields->field_values[actual_index - 1] = value;
1427 if (node->first_child != NULL) {
1428 proto_tree_children_foreach(node, proto_tree_get_node_field_values,
1433 void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, FILE *fh)
1437 write_field_data_t data;
1443 data.fields = fields;
1446 if(NULL == fields->field_indicies) {
1447 /* Prepare a lookup table from string abbreviation for field to its index. */
1448 fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
1451 while( i < fields->fields->len) {
1452 gchar* field = g_ptr_array_index(fields->fields, i);
1453 /* Store field indicies +1 so that zero is not a valid value,
1454 * and can be distinguished from NULL as a pointer.
1457 g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
1461 /* Buffer to store values for this packet */
1462 fields->field_values = ep_alloc_array0(const gchar*, fields->fields->len);
1464 proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
1467 for(i = 0; i < fields->fields->len; ++i) {
1469 fputc(fields->separator, fh);
1471 if(NULL != fields->field_values[i]) {
1472 if(fields->quote != '\0') {
1473 fputc(fields->quote, fh);
1475 fputs(fields->field_values[i], fh);
1476 if(fields->quote != '\0') {
1477 fputc(fields->quote, fh);
1483 void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
1488 /* Returns an ep_alloced string or a static constant*/
1489 const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
1491 if (fi->hfinfo->id == hf_text_only) {
1495 return fi->rep->representation;
1498 return get_field_hex_value(edt->pi.data_src, fi);
1501 else if (fi->hfinfo->id == proto_data) {
1502 /* Uninterpreted data, i.e., the "Data" protocol, is
1503 * printed as a field instead of a protocol. */
1504 return get_field_hex_value(edt->pi.data_src, fi);
1507 /* Normal protocols and fields */
1508 gchar *dfilter_string;
1511 switch (fi->hfinfo->type)
1514 /* Print out the full details for the protocol. */
1516 return fi->rep->representation;
1518 /* Just print out the protocol abbreviation */
1519 return fi->hfinfo->abbrev;;
1522 /* Return "1" so that the presence of a field of type
1523 * FT_NONE can be checked when using -T fields */
1526 /* XXX - this is a hack until we can just call
1527 * fvalue_to_string_repr() for *all* FT_* types. */
1528 dfilter_string = proto_construct_match_selected_string(fi,
1530 if (dfilter_string != NULL) {
1531 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
1533 /* XXX - Remove double-quotes. Again, once we
1534 * can call fvalue_to_string_repr(), we can
1535 * ask it not to produce the version for
1536 * display-filters, and thus, no
1538 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
1539 dfilter_string[strlen(dfilter_string)-1] = '\0';
1543 return &(dfilter_string[chop_len]);
1545 return get_field_hex_value(edt->pi.data_src, fi);
1552 get_field_hex_value(GSList* src_list, field_info *fi)
1559 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
1560 return "field length invalid!";
1563 /* Find the data for this field. */
1564 pd = get_field_data(src_list, fi);
1571 const int chars_per_byte = 2;
1573 len = chars_per_byte * fi->length;
1574 buffer = ep_alloc_array(gchar, len + 1);
1575 buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
1577 /* Print a simple hex dump */
1578 for (i = 0 ; i < fi->length; i++) {
1579 g_snprintf(p, chars_per_byte+1, "%02x", pd[i]);
1580 p += chars_per_byte;