2 * Routines for printing packet analysis trees.
6 * Gilbert Ramirez <gram@alumni.rice.edu>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include <epan/epan.h>
35 #include <epan/epan_dissect.h>
36 #include <epan/tvbuff.h>
37 #include <epan/packet.h>
38 #include <epan/emem.h>
40 #include "packet-range.h"
43 #include "version_info.h"
44 #include <wsutil/file_util.h>
45 #include <epan/charsets.h>
46 #include <epan/dissectors/packet-data.h>
47 #include <epan/dissectors/packet-frame.h>
49 #define PDML_VERSION "0"
50 #define PSML_VERSION "0"
54 print_stream_t *stream;
57 print_dissections_e print_dissections;
58 gboolean print_hex_for_data;
59 packet_char_enc encoding;
71 output_fields_t* fields;
75 struct _output_fields {
76 gboolean print_header;
81 GHashTable* field_indicies;
82 emem_strbuf_t** field_values;
86 static gboolean write_headers = FALSE;
88 static const gchar* get_field_hex_value(GSList* src_list, field_info *fi);
89 static void proto_tree_print_node(proto_node *node, gpointer data);
90 static void proto_tree_write_node_pdml(proto_node *node, gpointer data);
91 static const guint8 *get_field_data(GSList *src_list, field_info *fi);
92 static void write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi);
93 static gboolean print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
94 guint length, packet_char_enc encoding);
95 static void ps_clean_string(unsigned char *out, const unsigned char *in,
97 static void print_escaped_xml(FILE *fh, const char *unescaped_string);
99 static void print_pdml_geninfo(proto_tree *tree, FILE *fh);
101 static void proto_tree_get_node_field_values(proto_node *node, gpointer data);
104 open_print_dest(int to_file, const char *dest)
108 /* Open the file or command for output */
110 fh = ws_fopen(dest, "w");
112 fh = popen(dest, "w");
118 close_print_dest(int to_file, FILE *fh)
120 /* Close the file or command */
122 return (fclose(fh) == 0);
124 return (pclose(fh) == 0);
127 #define MAX_PS_LINE_LENGTH 256
130 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
131 print_stream_t *stream)
135 /* Create the output */
137 data.stream = stream;
139 data.src_list = edt->pi.data_src;
140 data.encoding = edt->pi.fd->flags.encoding;
141 data.print_dissections = print_args->print_dissections;
142 /* If we're printing the entire packet in hex, don't
143 print uninterpreted data fields in hex as well. */
144 data.print_hex_for_data = !print_args->print_hex;
147 proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
151 #define MAX_INDENT 160
153 /* Print a tree's data, and any child nodes. */
155 void proto_tree_print_node(proto_node *node, gpointer data)
157 field_info *fi = PNODE_FINFO(node);
158 print_data *pdata = (print_data*) data;
160 gchar label_str[ITEM_LABEL_LENGTH];
163 g_assert(fi && "dissection with an invisible proto tree?");
165 /* Don't print invisible entries. */
166 if (PROTO_ITEM_IS_HIDDEN(node))
169 /* Give up if we've already gotten an error. */
173 /* was a free format label produced? */
175 label_ptr = fi->rep->representation;
177 else { /* no, make a generic label */
178 label_ptr = label_str;
179 proto_item_fill_label(fi, label_str);
182 if (PROTO_ITEM_IS_GENERATED(node)) {
183 label_ptr = g_strdup_printf("[%s]", label_ptr);
186 if (!print_line(pdata->stream, pdata->level, label_ptr)) {
187 pdata->success = FALSE;
191 if (PROTO_ITEM_IS_GENERATED(node)) {
195 /* If it's uninterpreted data, dump it (unless our caller will
196 be printing the entire packet in hex). */
197 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
199 * Find the data for this field.
201 pd = get_field_data(pdata->src_list, fi);
203 if (!print_hex_data_buffer(pdata->stream, pd,
204 fi->length, pdata->encoding)) {
205 pdata->success = FALSE;
211 /* If we're printing all levels, or if this node is one with a
212 subtree and its subtree is expanded, recurse into the subtree,
214 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
215 if (pdata->print_dissections == print_dissections_expanded ||
216 (pdata->print_dissections == print_dissections_as_displayed &&
217 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
218 if (node->first_child != NULL) {
220 proto_tree_children_foreach(node,
221 proto_tree_print_node, pdata);
230 write_pdml_preamble(FILE *fh)
232 fputs("<?xml version=\"1.0\"?>\n", fh);
233 fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
234 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
238 proto_tree_write_pdml(epan_dissect_t *edt, FILE *fh)
240 write_pdml_data data;
242 /* Create the output */
245 data.src_list = edt->pi.data_src;
248 /* We shouldn't be called with a NULL pointer here because we've
249 * created a visible protocol tree */
250 g_assert(data.src_list);
252 fprintf(fh, "<packet>\n");
254 /* Print a "geninfo" protocol as required by PDML */
255 print_pdml_geninfo(edt->tree, fh);
257 proto_tree_children_foreach(edt->tree, proto_tree_write_node_pdml,
260 fprintf(fh, "</packet>\n\n");
263 /* Write out a tree's data, and any child nodes, as PDML */
265 proto_tree_write_node_pdml(proto_node *node, gpointer data)
267 field_info *fi = PNODE_FINFO(node);
268 write_pdml_data *pdata = (write_pdml_data*) data;
269 const gchar *label_ptr;
270 gchar label_str[ITEM_LABEL_LENGTH];
271 char *dfilter_string;
274 gboolean wrap_in_fake_protocol;
276 g_assert(fi && "dissection with an invisible proto tree?");
278 /* Will wrap up top-level field items inside a fake protocol wrapper to
279 preserve the PDML schema */
280 wrap_in_fake_protocol =
281 (((fi->hfinfo->type != FT_PROTOCOL) ||
282 (fi->hfinfo->id == proto_data)) &&
283 (pdata->level == 0));
285 /* Indent to the correct level */
286 for (i = -1; i < pdata->level; i++) {
287 fputs(" ", pdata->fh);
290 if (wrap_in_fake_protocol) {
291 /* Open fake protocol wrapper */
292 fputs("<proto name=\"fake-field-wrapper\">\n", pdata->fh);
294 /* Indent to increased level before writint out field */
296 for (i = -1; i < pdata->level; i++) {
297 fputs(" ", pdata->fh);
301 /* Text label. It's printed as a field with no name. */
302 if (fi->hfinfo->id == hf_text_only) {
305 label_ptr = fi->rep->representation;
311 /* Show empty name since it is a required field */
312 fputs("<field name=\"", pdata->fh);
313 fputs("\" show=\"", pdata->fh);
314 print_escaped_xml(pdata->fh, label_ptr);
316 fprintf(pdata->fh, "\" size=\"%d", fi->length);
317 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
319 fputs("\" value=\"", pdata->fh);
320 write_pdml_field_hex_value(pdata, fi);
322 if (node->first_child != NULL) {
323 fputs("\">\n", pdata->fh);
326 fputs("\"/>\n", pdata->fh);
330 /* Uninterpreted data, i.e., the "Data" protocol, is
331 * printed as a field instead of a protocol. */
332 else if (fi->hfinfo->id == proto_data) {
334 /* Write out field with data */
335 fputs("<field name=\"data\" value=\"", pdata->fh);
336 write_pdml_field_hex_value(pdata, fi);
337 fputs("\"/>\n", pdata->fh);
339 /* Normal protocols and fields */
341 if (fi->hfinfo->type == FT_PROTOCOL) {
342 fputs("<proto name=\"", pdata->fh);
345 fputs("<field name=\"", pdata->fh);
347 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
351 * http://www.nbee.org/doku.php?id=netpdl:pdml_specification
353 * the show fields contains things in 'human readable' format
354 * showname: contains only the name of the field
355 * show: contains only the data of the field
356 * showdtl: contains additional details of the field data
357 * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
359 * XXX - the showname shouldn't contain the field data itself
360 * (like it's contained in the fi->rep->representation).
361 * Unfortunately, we don't have the field data representation for
362 * all fields, so this isn't currently possible */
363 fputs("\" showname=\"", pdata->fh);
364 print_escaped_xml(pdata->fh, fi->hfinfo->name);
368 fputs("\" showname=\"", pdata->fh);
369 print_escaped_xml(pdata->fh, fi->rep->representation);
372 label_ptr = label_str;
373 proto_item_fill_label(fi, label_str);
374 fputs("\" showname=\"", pdata->fh);
375 print_escaped_xml(pdata->fh, label_ptr);
378 if (PROTO_ITEM_IS_HIDDEN(node))
379 fprintf(pdata->fh, "\" hide=\"yes");
381 fprintf(pdata->fh, "\" size=\"%d", fi->length);
382 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
383 /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
385 /* show, value, and unmaskedvalue attributes */
386 switch (fi->hfinfo->type)
391 fputs("\" show=\"\" value=\"", pdata->fh);
394 /* XXX - this is a hack until we can just call
395 * fvalue_to_string_repr() for *all* FT_* types. */
396 dfilter_string = proto_construct_match_selected_string(fi,
398 if (dfilter_string != NULL) {
399 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
401 /* XXX - Remove double-quotes. Again, once we
402 * can call fvalue_to_string_repr(), we can
403 * ask it not to produce the version for
404 * display-filters, and thus, no
406 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
407 dfilter_string[strlen(dfilter_string)-1] = '\0';
411 fputs("\" show=\"", pdata->fh);
412 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
416 * XXX - should we omit "value" for any fields?
417 * What should we do for fields whose length is 0?
418 * They might come from a pseudo-header or from
419 * the capture header (e.g., time stamps), or
420 * they might be generated fields.
422 if (fi->length > 0) {
423 fputs("\" value=\"", pdata->fh);
425 if (fi->hfinfo->bitmask!=0) {
426 fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
427 fputs("\" unmaskedvalue=\"", pdata->fh);
428 write_pdml_field_hex_value(pdata, fi);
431 write_pdml_field_hex_value(pdata, fi);
436 if (node->first_child != NULL) {
437 fputs("\">\n", pdata->fh);
439 else if (fi->hfinfo->id == proto_data) {
440 fputs("\">\n", pdata->fh);
443 fputs("\"/>\n", pdata->fh);
447 /* We always print all levels for PDML. Recurse here. */
448 if (node->first_child != NULL) {
450 proto_tree_children_foreach(node,
451 proto_tree_write_node_pdml, pdata);
455 /* Take back the extra level we added for fake wrapper protocol */
456 if (wrap_in_fake_protocol) {
460 if (node->first_child != NULL) {
461 /* Indent to correct level */
462 for (i = -1; i < pdata->level; i++) {
463 fputs(" ", pdata->fh);
465 /* Close off current element */
466 if (fi->hfinfo->id != proto_data) { /* Data protocol uses simple tags */
467 if (fi->hfinfo->type == FT_PROTOCOL) {
468 fputs("</proto>\n", pdata->fh);
471 fputs("</field>\n", pdata->fh);
476 /* Close off fake wrapper protocol */
477 if (wrap_in_fake_protocol) {
478 fputs("</proto>\n", pdata->fh);
482 /* Print info for a 'geninfo' pseudo-protocol. This is required by
483 * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
484 * but we produce a 'geninfo' protocol in the PDML to conform to spec.
485 * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
487 print_pdml_geninfo(proto_tree *tree, FILE *fh)
489 guint32 num, len, caplen;
491 GPtrArray *finfo_array;
492 field_info *frame_finfo;
494 /* Get frame protocol's finfo. */
495 finfo_array = proto_find_finfo(tree, proto_frame);
496 if (g_ptr_array_len(finfo_array) < 1) {
499 frame_finfo = (field_info *)finfo_array->pdata[0];
500 g_ptr_array_free(finfo_array, TRUE);
502 /* frame.number --> geninfo.num */
503 finfo_array = proto_find_finfo(tree, hf_frame_number);
504 if (g_ptr_array_len(finfo_array) < 1) {
507 num = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
508 g_ptr_array_free(finfo_array, TRUE);
510 /* frame.frame_len --> geninfo.len */
511 finfo_array = proto_find_finfo(tree, hf_frame_len);
512 if (g_ptr_array_len(finfo_array) < 1) {
515 len = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
516 g_ptr_array_free(finfo_array, TRUE);
518 /* frame.cap_len --> geninfo.caplen */
519 finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
520 if (g_ptr_array_len(finfo_array) < 1) {
523 caplen = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
524 g_ptr_array_free(finfo_array, TRUE);
526 /* frame.time --> geninfo.timestamp */
527 finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
528 if (g_ptr_array_len(finfo_array) < 1) {
531 timestamp = (nstime_t *)fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
532 g_ptr_array_free(finfo_array, TRUE);
534 /* Print geninfo start */
536 " <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
537 frame_finfo->length);
539 /* Print geninfo.num */
541 " <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
542 num, num, frame_finfo->length);
544 /* Print geninfo.len */
546 " <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Frame Length\" value=\"%x\" size=\"%u\"/>\n",
547 len, len, frame_finfo->length);
549 /* Print geninfo.caplen */
551 " <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
552 caplen, caplen, frame_finfo->length);
554 /* Print geninfo.timestamp */
556 " <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
557 abs_time_to_str(timestamp, ABSOLUTE_TIME_LOCAL, TRUE), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
559 /* Print geninfo end */
565 write_pdml_finale(FILE *fh)
567 fputs("</pdml>\n", fh);
571 write_psml_preamble(FILE *fh)
573 fputs("<?xml version=\"1.0\"?>\n", fh);
574 fputs("<psml version=\"" PSML_VERSION "\" ", fh);
575 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
576 write_headers = TRUE;
580 proto_tree_write_psml(epan_dissect_t *edt, FILE *fh)
584 /* if this is the first packet, we have to create the PSML structure output */
586 fprintf(fh, "<structure>\n");
588 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
589 fprintf(fh, "<section>");
590 print_escaped_xml(fh, edt->pi.cinfo->col_title[i]);
591 fprintf(fh, "</section>\n");
594 fprintf(fh, "</structure>\n\n");
596 write_headers = FALSE;
599 fprintf(fh, "<packet>\n");
601 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
602 fprintf(fh, "<section>");
603 print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
604 fprintf(fh, "</section>\n");
607 fprintf(fh, "</packet>\n\n");
611 write_psml_finale(FILE *fh)
613 fputs("</psml>\n", fh);
617 write_csv_preamble(FILE *fh _U_)
619 write_headers = TRUE;
623 proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
627 /* if this is the first packet, we have to write the CSV header */
629 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
630 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_title[i]);
632 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_title[i]);
634 write_headers = FALSE;
637 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
638 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_data[i]);
640 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_data[i]);
644 write_csv_finale(FILE *fh _U_)
650 write_carrays_preamble(FILE *fh _U_)
656 proto_tree_write_carrays(const guint8 *pd, guint32 len, guint32 num, FILE *fh)
663 fprintf(fh, "char pkt%u[] = {\n", num);
665 for (i = 0; i < len; i++) {
667 fprintf(fh, "0x%02x", *(pd + i));
669 if (i == (len - 1)) {
670 fprintf(fh, " };\n\n");
674 if (!((i + 1) % 8)) {
683 write_carrays_finale(FILE *fh _U_)
689 * Find the data source for a specified field, and return a pointer
690 * to the data in it. Returns NULL if the data is out of bounds.
692 static const guint8 *
693 get_field_data(GSList *src_list, field_info *fi)
698 gint length, tvbuff_length;
700 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
701 src = (data_source *)src_le->data;
703 if (fi->ds_tvb == src_tvb) {
707 * XXX - a field can have a length that runs past
708 * the end of the tvbuff. Ideally, that should
709 * be fixed when adding an item to the protocol
710 * tree, but checking the length when doing
711 * that could be expensive. Until we fix that,
712 * we'll do the check here.
714 tvbuff_length = tvb_length_remaining(src_tvb,
716 if (tvbuff_length < 0) {
720 if (length > tvbuff_length)
721 length = tvbuff_length;
722 return tvb_get_ptr(src_tvb, fi->start, length);
725 g_assert_not_reached();
726 return NULL; /* not found */
729 /* Print a string, escaping out certain characters that need to
730 * escaped out for XML. */
732 print_escaped_xml(FILE *fh, const char *unescaped_string)
737 for (p = unescaped_string; *p != '\0'; p++) {
755 if (g_ascii_isprint(*p))
758 g_snprintf(temp_str, sizeof(temp_str), "\\x%x", (guint8)*p);
766 write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi)
774 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
775 fprintf(pdata->fh, "field length invalid!");
779 /* Find the data for this field. */
780 pd = get_field_data(pdata->src_list, fi);
783 /* Print a simple hex dump */
784 for (i = 0 ; i < fi->length; i++) {
785 fprintf(pdata->fh, "%02x", pd[i]);
791 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
793 gboolean multiple_sources;
802 /* We shouldn't be called with a NULL pointer here because we've
803 * created a visible protocol tree */
804 g_assert(edt->pi.data_src);
807 * Set "multiple_sources" iff this frame has more than one
808 * data source; if it does, we need to print the name of
809 * the data source before printing the data from the
812 multiple_sources = (edt->pi.data_src->next != NULL);
814 for (src_le = edt->pi.data_src; src_le != NULL;
815 src_le = src_le->next) {
816 src = (data_source *)src_le->data;
818 if (multiple_sources) {
819 name = get_data_source_name(src);
820 print_line(stream, 0, "");
821 line = g_strdup_printf("%s:", name);
822 print_line(stream, 0, line);
825 length = tvb_length(tvb);
828 cp = tvb_get_ptr(tvb, 0, length);
829 if (!print_hex_data_buffer(stream, cp, length,
830 edt->pi.fd->flags.encoding))
837 * This routine is based on a routine created by Dan Lasley
838 * <DLASLEY@PROMUS.com>.
840 * It was modified for Wireshark by Gilbert Ramirez and others.
843 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
844 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
845 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
846 /* max number of characters hex dump takes -
847 2 digits plus trailing blank */
848 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
849 /* number of characters those bytes take;
850 3 characters per byte of hex dump,
851 2 blanks separating hex from ASCII,
852 1 character per byte of ASCII dump */
853 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
854 /* number of characters per line;
855 offset, 2 blanks separating offset
856 from data dump, data dump */
859 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
860 guint length, packet_char_enc encoding)
862 register unsigned int ad, i, j, k, l;
864 guchar line[MAX_LINE_LEN + 1];
865 unsigned int use_digits;
866 static guchar binhex[16] = {
867 '0', '1', '2', '3', '4', '5', '6', '7',
868 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
870 if (!print_line(stream, 0, ""))
874 * How many of the leading digits of the offset will we supply?
875 * We always supply at least 4 digits, but if the maximum offset
876 * won't fit in 4 digits, we use as many digits as will be needed.
878 if (((length - 1) & 0xF0000000) != 0)
879 use_digits = 8; /* need all 8 digits */
880 else if (((length - 1) & 0x0F000000) != 0)
881 use_digits = 7; /* need 7 digits */
882 else if (((length - 1) & 0x00F00000) != 0)
883 use_digits = 6; /* need 6 digits */
884 else if (((length - 1) & 0x000F0000) != 0)
885 use_digits = 5; /* need 5 digits */
887 use_digits = 4; /* we'll supply 4 digits */
896 * Start of a new line.
902 c = (ad >> (l*4)) & 0xF;
903 line[j++] = binhex[c];
907 memset(line+j, ' ', DATA_DUMP_LEN);
910 * Offset in line of ASCII dump.
912 k = j + HEX_DUMP_LEN + 2;
915 line[j++] = binhex[c>>4];
916 line[j++] = binhex[c&0xf];
918 if (encoding == PACKET_CHAR_ENC_CHAR_EBCDIC) {
919 c = EBCDIC_to_ASCII1(c);
921 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
923 if ((i & 15) == 0 || i == length) {
925 * We'll be starting a new line, or
926 * we're finished printing this buffer;
927 * dump out the line we've constructed,
928 * and advance the offset.
931 if (!print_line(stream, 0, line))
940 void ps_clean_string(unsigned char *out, const unsigned char *in,
951 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
972 /* Some formats need stuff at the beginning of the output */
974 print_preamble(print_stream_t *self, gchar *filename)
976 return (self->ops->print_preamble)(self, filename);
980 print_line(print_stream_t *self, int indent, const char *line)
982 return (self->ops->print_line)(self, indent, line);
985 /* Insert bookmark */
987 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
989 return (self->ops->print_bookmark)(self, name, title);
993 new_page(print_stream_t *self)
995 return (self->ops->new_page)(self);
998 /* Some formats need stuff at the end of the output */
1000 print_finale(print_stream_t *self)
1002 return (self->ops->print_finale)(self);
1006 destroy_print_stream(print_stream_t *self)
1008 return (self->ops->destroy)(self);
1017 print_preamble_text(print_stream_t *self _U_, gchar *filename _U_)
1020 return TRUE; /* always succeeds */
1024 print_line_text(print_stream_t *self, int indent, const char *line)
1026 output_text *output = (output_text *)self->data;
1027 char space[MAX_INDENT+1];
1031 /* Prepare the tabs for printing, depending on tree level */
1032 num_spaces = indent * 4;
1033 if (num_spaces > MAX_INDENT) {
1034 num_spaces = MAX_INDENT;
1036 for (i = 0; i < num_spaces; i++) {
1039 /* The string is NUL-terminated */
1040 space[num_spaces] = '\0';
1042 fputs(space, output->fh);
1043 fputs(line, output->fh);
1044 putc('\n', output->fh);
1045 return !ferror(output->fh);
1049 print_bookmark_text(print_stream_t *self _U_, const gchar *name _U_,
1050 const gchar *title _U_)
1057 new_page_text(print_stream_t *self)
1059 output_text *output = (output_text *)self->data;
1061 fputs("\f", output->fh);
1062 return !ferror(output->fh);
1066 print_finale_text(print_stream_t *self _U_)
1069 return TRUE; /* always succeeds */
1073 destroy_text(print_stream_t *self)
1075 output_text *output = (output_text *)self->data;
1078 ret = close_print_dest(output->to_file, output->fh);
1084 static const print_stream_ops_t print_text_ops = {
1085 print_preamble_text,
1087 print_bookmark_text,
1093 static print_stream_t *
1094 print_stream_text_alloc(int to_file, FILE *fh)
1096 print_stream_t *stream;
1097 output_text *output;
1099 output = (output_text *)g_malloc(sizeof *output);
1100 output->to_file = to_file;
1102 stream = (print_stream_t *)g_malloc(sizeof (print_stream_t));
1103 stream->ops = &print_text_ops;
1104 stream->data = output;
1110 print_stream_text_new(int to_file, const char *dest)
1114 fh = open_print_dest(to_file, dest);
1118 return print_stream_text_alloc(to_file, fh);
1122 print_stream_text_stdio_new(FILE *fh)
1124 return print_stream_text_alloc(TRUE, fh);
1133 print_preamble_ps(print_stream_t *self, gchar *filename)
1135 output_ps *output = (output_ps *)self->data;
1136 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1138 print_ps_preamble(output->fh);
1140 fputs("%% the page title\n", output->fh);
1141 ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
1142 fprintf(output->fh, "/ws_pagetitle (%s - Wireshark " VERSION "%s) def\n", psbuffer, wireshark_svnversion);
1143 fputs("\n", output->fh);
1144 return !ferror(output->fh);
1148 print_line_ps(print_stream_t *self, int indent, const char *line)
1150 output_ps *output = (output_ps *)self->data;
1151 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1153 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
1154 fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
1155 return !ferror(output->fh);
1159 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
1161 output_ps *output = (output_ps *)self->data;
1162 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1165 * See the Adobe "pdfmark reference":
1167 * http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
1169 * The pdfmark stuff tells code that turns PostScript into PDF
1170 * things that it should do.
1172 * The /OUT stuff creates a bookmark that goes to the
1173 * destination with "name" as the name and "title" as the title.
1175 * The "/DEST" creates the destination.
1177 ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
1178 fprintf(output->fh, "[/Dest /%s /Title (%s) /OUT pdfmark\n", name,
1180 fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
1182 fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
1184 fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
1185 return !ferror(output->fh);
1189 new_page_ps(print_stream_t *self)
1191 output_ps *output = (output_ps *)self->data;
1193 fputs("formfeed\n", output->fh);
1194 return !ferror(output->fh);
1198 print_finale_ps(print_stream_t *self)
1200 output_ps *output = (output_ps *)self->data;
1202 print_ps_finale(output->fh);
1203 return !ferror(output->fh);
1207 destroy_ps(print_stream_t *self)
1209 output_ps *output = (output_ps *)self->data;
1212 ret = close_print_dest(output->to_file, output->fh);
1218 static const print_stream_ops_t print_ps_ops = {
1227 static print_stream_t *
1228 print_stream_ps_alloc(int to_file, FILE *fh)
1230 print_stream_t *stream;
1233 output = (output_ps *)g_malloc(sizeof *output);
1234 output->to_file = to_file;
1236 stream = (print_stream_t *)g_malloc(sizeof (print_stream_t));
1237 stream->ops = &print_ps_ops;
1238 stream->data = output;
1244 print_stream_ps_new(int to_file, const char *dest)
1248 fh = open_print_dest(to_file, dest);
1252 return print_stream_ps_alloc(to_file, fh);
1256 print_stream_ps_stdio_new(FILE *fh)
1258 return print_stream_ps_alloc(TRUE, fh);
1261 output_fields_t* output_fields_new()
1263 output_fields_t* fields = g_new(output_fields_t, 1);
1264 fields->print_header = FALSE;
1265 fields->separator = '\t';
1266 fields->occurrence = 'a';
1267 fields->aggregator = ',';
1268 fields->fields = NULL; /*Do lazy initialisation */
1269 fields->field_indicies = NULL;
1270 fields->field_values = NULL;
1275 gsize output_fields_num_fields(output_fields_t* fields)
1279 if(NULL == fields->fields) {
1282 return fields->fields->len;
1286 void output_fields_free(output_fields_t* fields)
1290 if(NULL != fields->field_indicies) {
1291 /* Keys are stored in fields->fields, values are
1294 g_hash_table_destroy(fields->field_indicies);
1296 if(NULL != fields->fields) {
1298 for(i = 0; i < fields->fields->len; ++i) {
1299 gchar* field = (gchar *)g_ptr_array_index(fields->fields,i);
1302 g_ptr_array_free(fields->fields, TRUE);
1308 void output_fields_add(output_fields_t* fields, const gchar* field)
1316 if(NULL == fields->fields) {
1317 fields->fields = g_ptr_array_new();
1320 field_copy = g_strdup(field);
1322 g_ptr_array_add(fields->fields, field_copy);
1325 gboolean output_fields_set_option(output_fields_t* info, gchar* option)
1327 const gchar* option_name;
1328 const gchar* option_value;
1333 if('\0' == *option) {
1334 return FALSE; /* Is this guarded against by option parsing? */
1336 option_name = strtok(option,"=");
1337 option_value = option + strlen(option_name) + 1;
1338 if(0 == strcmp(option_name, "header")) {
1339 switch(NULL == option_value ? '\0' : *option_value) {
1341 info->print_header = FALSE;
1344 info->print_header = TRUE;
1352 if(0 == strcmp(option_name,"separator")) {
1353 switch(NULL == option_value ? '\0' : *option_value) {
1357 switch(*++option_value) {
1359 info->separator = '\t';
1362 info->separator = ' ';
1365 info->separator = '\\';
1369 info->separator = *option_value;
1375 if(0 == strcmp(option_name, "occurrence")) {
1376 switch(NULL == option_value ? '\0' : *option_value) {
1380 info->occurrence = *option_value;
1388 if(0 == strcmp(option_name,"aggregator")) {
1389 switch(NULL == option_value ? '\0' : *option_value) {
1393 switch(*++option_value) {
1395 info->aggregator = ' ';
1398 info->aggregator = '\\';
1402 info->aggregator = *option_value;
1408 if(0 == strcmp(option_name, "quote")) {
1409 switch(NULL == option_value ? '\0' : *option_value) {
1410 default: /* Fall through */
1430 void output_fields_list_options(FILE *fh)
1432 fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
1433 fputs("header=y|n Print field abbreviations as first line of output (def: N: no)\n", fh);
1434 fputs("separator=/t|/s|<character> Set the separator to use;\n \"/t\" = tab, \"/s\" = space (def: /t: tab)\n", fh);
1435 fputs("occurrence=f|l|a Select the occurrence of a field to use;\n \"f\" = first, \"l\" = last, \"a\" = all (def: a: all)\n", fh);
1436 fputs("aggregator=,|/s|<character> Set the aggregator to use;\n \",\" = comma, \"/s\" = space (def: ,: comma)\n", fh);
1437 fputs("quote=d|s|n Print either d: double-quotes, s: single quotes or \n n: no quotes around field values (def: n: none)\n", fh);
1441 void write_fields_preamble(output_fields_t* fields, FILE *fh)
1448 if(!fields->print_header) {
1452 for(i = 0; i < fields->fields->len; ++i) {
1453 const gchar* field = (const gchar *)g_ptr_array_index(fields->fields,i);
1455 fputc(fields->separator, fh);
1462 static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
1464 write_field_data_t *call_data;
1466 gpointer field_index;
1468 call_data = (write_field_data_t *)data;
1469 fi = PNODE_FINFO(node);
1471 g_assert(fi && "dissection with an invisible proto tree?");
1473 field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
1474 if(NULL != field_index) {
1477 value = get_node_field_value(fi, call_data->edt); /* ep_alloced string */
1479 if(NULL != value && '\0' != *value) {
1481 actual_index = GPOINTER_TO_UINT(field_index);
1482 /* Unwrap change made to disambiguiate zero / null */
1483 if ( call_data->fields->field_values[actual_index - 1] == NULL ) {
1484 call_data->fields->field_values[actual_index - 1] = ep_strbuf_new(value);
1485 } else if ( call_data->fields->occurrence == 'l' ) {
1486 /* print only the value of the last occurrence of the field */
1487 ep_strbuf_printf(call_data->fields->field_values[actual_index - 1],"%s",value);
1488 } else if ( call_data->fields->occurrence == 'a' ) {
1489 /* print the value of all accurrences of the field */
1490 ep_strbuf_append_printf(call_data->fields->field_values[actual_index - 1],
1491 "%c%s",call_data->fields->aggregator,value);
1497 if (node->first_child != NULL) {
1498 proto_tree_children_foreach(node, proto_tree_get_node_field_values,
1503 void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, FILE *fh)
1507 write_field_data_t data;
1513 data.fields = fields;
1516 if(NULL == fields->field_indicies) {
1517 /* Prepare a lookup table from string abbreviation for field to its index. */
1518 fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
1521 while( i < fields->fields->len) {
1522 gchar* field = (gchar *)g_ptr_array_index(fields->fields, i);
1523 /* Store field indicies +1 so that zero is not a valid value,
1524 * and can be distinguished from NULL as a pointer.
1527 g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
1531 /* Buffer to store values for this packet */
1532 fields->field_values = ep_alloc_array0(emem_strbuf_t*, fields->fields->len);
1534 proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
1537 for(i = 0; i < fields->fields->len; ++i) {
1539 fputc(fields->separator, fh);
1541 if(NULL != fields->field_values[i]) {
1542 if(fields->quote != '\0') {
1543 fputc(fields->quote, fh);
1545 fputs(fields->field_values[i]->str, fh);
1546 if(fields->quote != '\0') {
1547 fputc(fields->quote, fh);
1553 void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
1558 /* Returns an ep_alloced string or a static constant*/
1559 const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
1561 if (fi->hfinfo->id == hf_text_only) {
1565 return fi->rep->representation;
1568 return get_field_hex_value(edt->pi.data_src, fi);
1571 else if (fi->hfinfo->id == proto_data) {
1572 /* Uninterpreted data, i.e., the "Data" protocol, is
1573 * printed as a field instead of a protocol. */
1574 return get_field_hex_value(edt->pi.data_src, fi);
1577 /* Normal protocols and fields */
1578 gchar *dfilter_string;
1581 switch (fi->hfinfo->type)
1584 /* Print out the full details for the protocol. */
1586 return fi->rep->representation;
1588 /* Just print out the protocol abbreviation */
1589 return fi->hfinfo->abbrev;;
1592 /* Return "1" so that the presence of a field of type
1593 * FT_NONE can be checked when using -T fields */
1596 /* XXX - this is a hack until we can just call
1597 * fvalue_to_string_repr() for *all* FT_* types. */
1598 dfilter_string = proto_construct_match_selected_string(fi,
1600 if (dfilter_string != NULL) {
1601 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
1603 /* XXX - Remove double-quotes. Again, once we
1604 * can call fvalue_to_string_repr(), we can
1605 * ask it not to produce the version for
1606 * display-filters, and thus, no
1608 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
1609 dfilter_string[strlen(dfilter_string)-1] = '\0';
1613 return &(dfilter_string[chop_len]);
1615 return get_field_hex_value(edt->pi.data_src, fi);
1622 get_field_hex_value(GSList* src_list, field_info *fi)
1629 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
1630 return "field length invalid!";
1633 /* Find the data for this field. */
1634 pd = get_field_data(src_list, fi);
1641 const int chars_per_byte = 2;
1643 len = chars_per_byte * fi->length;
1644 buffer = ep_alloc_array(gchar, len + 1);
1645 buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
1647 /* Print a simple hex dump */
1648 for (i = 0 ; i < fi->length; i++) {
1649 g_snprintf(p, chars_per_byte+1, "%02x", pd[i]);
1650 p += chars_per_byte;