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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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>
39 #include <epan/expert.h>
41 #include "packet-range.h"
45 #include "version_info.h"
46 #include <wsutil/file_util.h>
47 #include <epan/charsets.h>
48 #include <epan/dissectors/packet-data.h>
49 #include <epan/dissectors/packet-frame.h>
50 #include <epan/filesystem.h>
52 #define PDML_VERSION "0"
53 #define PSML_VERSION "0"
57 print_stream_t *stream;
60 print_dissections_e print_dissections;
61 gboolean print_hex_for_data;
62 packet_char_enc encoding;
74 output_fields_t* fields;
78 struct _output_fields {
79 gboolean print_header;
84 GHashTable* field_indicies;
85 emem_strbuf_t** field_values;
87 gboolean includes_col_fields;
90 GHashTable *output_only_tables = NULL;
92 static gboolean write_headers = FALSE;
94 static const gchar* get_field_hex_value(GSList* src_list, field_info *fi);
95 static void proto_tree_print_node(proto_node *node, gpointer data);
96 static void proto_tree_write_node_pdml(proto_node *node, gpointer data);
97 static const guint8 *get_field_data(GSList *src_list, field_info *fi);
98 static void write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi);
99 static gboolean print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
100 guint length, packet_char_enc encoding);
101 static void ps_clean_string(char *out, const char *in,
103 static void print_escaped_xml(FILE *fh, const char *unescaped_string);
105 static void print_pdml_geninfo(proto_tree *tree, FILE *fh);
107 static void proto_tree_get_node_field_values(proto_node *node, gpointer data);
110 open_print_dest(gboolean to_file, const char *dest)
114 /* Open the file or command for output */
116 fh = ws_fopen(dest, "w");
118 fh = popen(dest, "w");
124 close_print_dest(gboolean to_file, FILE *fh)
126 /* Close the file or command */
128 return (fclose(fh) == 0);
130 return (pclose(fh) == 0);
133 #define MAX_PS_LINE_LENGTH 256
136 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
137 print_stream_t *stream)
141 /* Create the output */
143 data.stream = stream;
145 data.src_list = edt->pi.data_src;
146 data.encoding = edt->pi.fd->flags.encoding;
147 data.print_dissections = print_args->print_dissections;
148 /* If we're printing the entire packet in hex, don't
149 print uninterpreted data fields in hex as well. */
150 data.print_hex_for_data = !print_args->print_hex;
153 proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
157 #define MAX_INDENT 160
159 /* Print a tree's data, and any child nodes. */
161 void proto_tree_print_node(proto_node *node, gpointer data)
163 field_info *fi = PNODE_FINFO(node);
164 print_data *pdata = (print_data*) data;
166 gchar label_str[ITEM_LABEL_LENGTH];
169 /* dissection with an invisible proto tree? */
172 /* Don't print invisible entries. */
173 if (PROTO_ITEM_IS_HIDDEN(node))
176 /* Give up if we've already gotten an error. */
180 /* was a free format label produced? */
182 label_ptr = fi->rep->representation;
184 else { /* no, make a generic label */
185 label_ptr = label_str;
186 proto_item_fill_label(fi, label_str);
189 if (PROTO_ITEM_IS_GENERATED(node)) {
190 label_ptr = g_strdup_printf("[%s]", label_ptr);
193 if (!print_line(pdata->stream, pdata->level, label_ptr)) {
194 pdata->success = FALSE;
199 * If -O is specified, only display the protocols which are in the
200 * lookup table. Only check on the first level: once we start printing
201 * a tree, print the rest of the subtree. Otherwise we won't print
202 * subitems whose abbreviation doesn't match the protocol--for example
203 * text items (whose abbreviation is simply "text").
205 if (output_only_tables != NULL && pdata->level == 0
206 && g_hash_table_lookup(output_only_tables, fi->hfinfo->abbrev) == NULL) {
207 pdata->success = TRUE;
211 if (PROTO_ITEM_IS_GENERATED(node)) {
215 /* If it's uninterpreted data, dump it (unless our caller will
216 be printing the entire packet in hex). */
217 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
219 * Find the data for this field.
221 pd = get_field_data(pdata->src_list, fi);
223 if (!print_line(pdata->stream, 0, "")) {
224 pdata->success = FALSE;
227 if (!print_hex_data_buffer(pdata->stream, pd,
228 fi->length, pdata->encoding)) {
229 pdata->success = FALSE;
235 /* If we're printing all levels, or if this node is one with a
236 subtree and its subtree is expanded, recurse into the subtree,
238 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
239 if (pdata->print_dissections == print_dissections_expanded ||
240 (pdata->print_dissections == print_dissections_as_displayed &&
241 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
242 if (node->first_child != NULL) {
244 proto_tree_children_foreach(node,
245 proto_tree_print_node, pdata);
253 #define PDML2HTML_XSL "pdml2html.xsl"
255 write_pdml_preamble(FILE *fh, const gchar* filename)
258 char *ts=asctime(localtime(&t));
259 ts[strlen(ts)-1]=0; /* overwrite \n */
261 fputs("<?xml version=\"1.0\"?>\n", fh);
262 fputs("<?xml-stylesheet type=\"text/xsl\" href=\"" PDML2HTML_XSL "\"?>\n", fh);
263 fprintf(fh, "<!-- You can find " PDML2HTML_XSL " in %s or at http://anonsvn.wireshark.org/trunk/wireshark/" PDML2HTML_XSL ". -->\n", get_datafile_dir());
264 fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
265 fprintf(fh, "creator=\"%s/%s\" time=\"%s\" capture_file=\"%s\">\n", PACKAGE, VERSION, ts, filename ? filename : "");
269 proto_tree_write_pdml(epan_dissect_t *edt, FILE *fh)
271 write_pdml_data data;
273 /* Create the output */
276 data.src_list = edt->pi.data_src;
279 fprintf(fh, "<packet>\n");
281 /* Print a "geninfo" protocol as required by PDML */
282 print_pdml_geninfo(edt->tree, fh);
284 proto_tree_children_foreach(edt->tree, proto_tree_write_node_pdml,
287 fprintf(fh, "</packet>\n\n");
290 /* Write out a tree's data, and any child nodes, as PDML */
292 proto_tree_write_node_pdml(proto_node *node, gpointer data)
294 field_info *fi = PNODE_FINFO(node);
295 write_pdml_data *pdata = (write_pdml_data*) data;
296 const gchar *label_ptr;
297 gchar label_str[ITEM_LABEL_LENGTH];
298 char *dfilter_string;
301 gboolean wrap_in_fake_protocol;
303 /* dissection with an invisible proto tree? */
306 /* Will wrap up top-level field items inside a fake protocol wrapper to
307 preserve the PDML schema */
308 wrap_in_fake_protocol =
309 (((fi->hfinfo->type != FT_PROTOCOL) ||
310 (fi->hfinfo->id == proto_data)) &&
311 (pdata->level == 0));
313 /* Indent to the correct level */
314 for (i = -1; i < pdata->level; i++) {
315 fputs(" ", pdata->fh);
318 if (wrap_in_fake_protocol) {
319 /* Open fake protocol wrapper */
320 fputs("<proto name=\"fake-field-wrapper\">\n", pdata->fh);
322 /* Indent to increased level before writing out field */
324 for (i = -1; i < pdata->level; i++) {
325 fputs(" ", pdata->fh);
329 /* Text label. It's printed as a field with no name. */
330 if (fi->hfinfo->id == hf_text_only) {
333 label_ptr = fi->rep->representation;
339 /* Show empty name since it is a required field */
340 fputs("<field name=\"", pdata->fh);
341 fputs("\" show=\"", pdata->fh);
342 print_escaped_xml(pdata->fh, label_ptr);
344 fprintf(pdata->fh, "\" size=\"%d", fi->length);
345 if (node->parent && node->parent->finfo && (fi->start < node->parent->finfo->start)) {
346 fprintf(pdata->fh, "\" pos=\"%d", node->parent->finfo->start + fi->start);
348 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
351 fputs("\" value=\"", pdata->fh);
352 write_pdml_field_hex_value(pdata, fi);
354 if (node->first_child != NULL) {
355 fputs("\">\n", pdata->fh);
358 fputs("\"/>\n", pdata->fh);
362 /* Uninterpreted data, i.e., the "Data" protocol, is
363 * printed as a field instead of a protocol. */
364 else if (fi->hfinfo->id == proto_data) {
366 /* Write out field with data */
367 fputs("<field name=\"data\" value=\"", pdata->fh);
368 write_pdml_field_hex_value(pdata, fi);
369 fputs("\">\n", pdata->fh);
371 /* Normal protocols and fields */
373 if (fi->hfinfo->type == FT_PROTOCOL && fi->hfinfo->id != proto_expert) {
374 fputs("<proto name=\"", pdata->fh);
377 fputs("<field name=\"", pdata->fh);
379 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
383 * http://www.nbee.org/doku.php?id=netpdl:pdml_specification
385 * the show fields contains things in 'human readable' format
386 * showname: contains only the name of the field
387 * show: contains only the data of the field
388 * showdtl: contains additional details of the field data
389 * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
391 * XXX - the showname shouldn't contain the field data itself
392 * (like it's contained in the fi->rep->representation).
393 * Unfortunately, we don't have the field data representation for
394 * all fields, so this isn't currently possible */
395 fputs("\" showname=\"", pdata->fh);
396 print_escaped_xml(pdata->fh, fi->hfinfo->name);
400 fputs("\" showname=\"", pdata->fh);
401 print_escaped_xml(pdata->fh, fi->rep->representation);
404 label_ptr = label_str;
405 proto_item_fill_label(fi, label_str);
406 fputs("\" showname=\"", pdata->fh);
407 print_escaped_xml(pdata->fh, label_ptr);
410 if (PROTO_ITEM_IS_HIDDEN(node))
411 fprintf(pdata->fh, "\" hide=\"yes");
413 fprintf(pdata->fh, "\" size=\"%d", fi->length);
414 if (node->parent && node->parent->finfo && (fi->start < node->parent->finfo->start)) {
415 fprintf(pdata->fh, "\" pos=\"%d", node->parent->finfo->start + fi->start);
417 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
419 /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
421 /* show, value, and unmaskedvalue attributes */
422 switch (fi->hfinfo->type)
427 fputs("\" show=\"\" value=\"", pdata->fh);
430 /* XXX - this is a hack until we can just call
431 * fvalue_to_string_repr() for *all* FT_* types. */
432 dfilter_string = proto_construct_match_selected_string(fi,
434 if (dfilter_string != NULL) {
435 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
437 /* XXX - Remove double-quotes. Again, once we
438 * can call fvalue_to_string_repr(), we can
439 * ask it not to produce the version for
440 * display-filters, and thus, no
442 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
443 dfilter_string[strlen(dfilter_string)-1] = '\0';
447 fputs("\" show=\"", pdata->fh);
448 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
452 * XXX - should we omit "value" for any fields?
453 * What should we do for fields whose length is 0?
454 * They might come from a pseudo-header or from
455 * the capture header (e.g., time stamps), or
456 * they might be generated fields.
458 if (fi->length > 0) {
459 fputs("\" value=\"", pdata->fh);
461 if (fi->hfinfo->bitmask!=0) {
462 fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
463 fputs("\" unmaskedvalue=\"", pdata->fh);
464 write_pdml_field_hex_value(pdata, fi);
467 write_pdml_field_hex_value(pdata, fi);
472 if (node->first_child != NULL) {
473 fputs("\">\n", pdata->fh);
475 else if (fi->hfinfo->id == proto_data) {
476 fputs("\">\n", pdata->fh);
479 fputs("\"/>\n", pdata->fh);
483 /* We always print all levels for PDML. Recurse here. */
484 if (node->first_child != NULL) {
486 proto_tree_children_foreach(node,
487 proto_tree_write_node_pdml, pdata);
491 /* Take back the extra level we added for fake wrapper protocol */
492 if (wrap_in_fake_protocol) {
496 if (node->first_child != NULL) {
497 /* Indent to correct level */
498 for (i = -1; i < pdata->level; i++) {
499 fputs(" ", pdata->fh);
501 /* Close off current element */
502 /* Data and expert "protocols" use simple tags */
503 if (fi->hfinfo->id != proto_data && fi->hfinfo->id != proto_expert) {
504 if (fi->hfinfo->type == FT_PROTOCOL) {
505 fputs("</proto>\n", pdata->fh);
508 fputs("</field>\n", pdata->fh);
511 fputs("</field>\n", pdata->fh);
515 /* Close off fake wrapper protocol */
516 if (wrap_in_fake_protocol) {
517 fputs("</proto>\n", pdata->fh);
521 /* Print info for a 'geninfo' pseudo-protocol. This is required by
522 * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
523 * but we produce a 'geninfo' protocol in the PDML to conform to spec.
524 * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
526 print_pdml_geninfo(proto_tree *tree, FILE *fh)
528 guint32 num, len, caplen;
530 GPtrArray *finfo_array;
531 field_info *frame_finfo;
533 /* Get frame protocol's finfo. */
534 finfo_array = proto_find_finfo(tree, proto_frame);
535 if (g_ptr_array_len(finfo_array) < 1) {
538 frame_finfo = (field_info *)finfo_array->pdata[0];
539 g_ptr_array_free(finfo_array, TRUE);
541 /* frame.number --> geninfo.num */
542 finfo_array = proto_find_finfo(tree, hf_frame_number);
543 if (g_ptr_array_len(finfo_array) < 1) {
546 num = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
547 g_ptr_array_free(finfo_array, TRUE);
549 /* frame.frame_len --> geninfo.len */
550 finfo_array = proto_find_finfo(tree, hf_frame_len);
551 if (g_ptr_array_len(finfo_array) < 1) {
554 len = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
555 g_ptr_array_free(finfo_array, TRUE);
557 /* frame.cap_len --> geninfo.caplen */
558 finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
559 if (g_ptr_array_len(finfo_array) < 1) {
562 caplen = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
563 g_ptr_array_free(finfo_array, TRUE);
565 /* frame.time --> geninfo.timestamp */
566 finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
567 if (g_ptr_array_len(finfo_array) < 1) {
570 timestamp = (nstime_t *)fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
571 g_ptr_array_free(finfo_array, TRUE);
573 /* Print geninfo start */
575 " <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
576 frame_finfo->length);
578 /* Print geninfo.num */
580 " <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
581 num, num, frame_finfo->length);
583 /* Print geninfo.len */
585 " <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Frame Length\" value=\"%x\" size=\"%u\"/>\n",
586 len, len, frame_finfo->length);
588 /* Print geninfo.caplen */
590 " <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
591 caplen, caplen, frame_finfo->length);
593 /* Print geninfo.timestamp */
595 " <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
596 abs_time_to_str(timestamp, ABSOLUTE_TIME_LOCAL, TRUE), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
598 /* Print geninfo end */
604 write_pdml_finale(FILE *fh)
606 fputs("</pdml>\n", fh);
610 write_psml_preamble(FILE *fh)
612 fputs("<?xml version=\"1.0\"?>\n", fh);
613 fputs("<psml version=\"" PSML_VERSION "\" ", fh);
614 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
615 write_headers = TRUE;
619 proto_tree_write_psml(epan_dissect_t *edt, FILE *fh)
623 /* if this is the first packet, we have to create the PSML structure output */
625 fprintf(fh, "<structure>\n");
627 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
628 fprintf(fh, "<section>");
629 print_escaped_xml(fh, edt->pi.cinfo->col_title[i]);
630 fprintf(fh, "</section>\n");
633 fprintf(fh, "</structure>\n\n");
635 write_headers = FALSE;
638 fprintf(fh, "<packet>\n");
640 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
641 fprintf(fh, "<section>");
642 print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
643 fprintf(fh, "</section>\n");
646 fprintf(fh, "</packet>\n\n");
650 write_psml_finale(FILE *fh)
652 fputs("</psml>\n", fh);
656 write_csv_preamble(FILE *fh _U_)
658 write_headers = TRUE;
661 static gchar *csv_massage_str(const gchar *source, const gchar *exceptions)
666 csv_str = g_strescape(source, exceptions);
668 while ( (tmp_str = strstr(tmp_str, "\\\"")) != NULL )
673 static void csv_write_str(const char *str, char sep, FILE *fh)
677 csv_str = csv_massage_str(str, NULL);
678 fprintf(fh, "\"%s\"%c", csv_str, sep);
683 proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
687 /* if this is the first packet, we have to write the CSV header */
689 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
690 csv_write_str(edt->pi.cinfo->col_title[i], ',', fh);
691 csv_write_str(edt->pi.cinfo->col_title[i], '\n', fh);
692 write_headers = FALSE;
695 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
696 csv_write_str(edt->pi.cinfo->col_data[i], ',', fh);
697 csv_write_str(edt->pi.cinfo->col_data[i], '\n', fh);
701 write_csv_finale(FILE *fh _U_)
707 write_carrays_preamble(FILE *fh _U_)
713 proto_tree_write_carrays(guint32 num, FILE *fh, epan_dissect_t *edt)
715 guint32 i = 0, src_num = 0;
717 struct data_source *src;
724 for (src_le = edt->pi.data_src; src_le != NULL; src_le = src_le->next) {
725 memset(ascii, 0, sizeof(ascii));
726 src = (struct data_source *)src_le->data;
727 tvb = get_data_source_tvb(src);
728 length = tvb_length(tvb);
732 cp = tvb_get_ptr(tvb, 0, length);
734 name = get_data_source_name(src);
736 fprintf(fh, "/* %s */\n", name);
738 fprintf(fh, "static const unsigned char pkt%u_%u[%u] = {\n",
739 num, src_num, length);
741 fprintf(fh, "static const unsigned char pkt%u[%u] = {\n",
746 for (i = 0; i < length; i++) {
747 fprintf(fh, "0x%02x", *(cp + i));
748 ascii[i % 8] = isprint(*(cp + i)) ? *(cp + i) : '.';
750 if (i == (length - 1)) {
755 for ( j = 0; j < 8 - rem; j++ )
758 fprintf(fh, " /* %s */\n};\n\n", ascii);
762 if (!((i + 1) % 8)) {
763 fprintf(fh, ", /* %s */\n", ascii);
764 memset(ascii, 0, sizeof(ascii));
774 write_carrays_finale(FILE *fh _U_)
780 * Find the data source for a specified field, and return a pointer
781 * to the data in it. Returns NULL if the data is out of bounds.
783 static const guint8 *
784 get_field_data(GSList *src_list, field_info *fi)
787 struct data_source *src;
789 gint length, tvbuff_length;
791 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
792 src = (struct data_source *)src_le->data;
793 src_tvb = get_data_source_tvb(src);
794 if (fi->ds_tvb == src_tvb) {
798 * XXX - a field can have a length that runs past
799 * the end of the tvbuff. Ideally, that should
800 * be fixed when adding an item to the protocol
801 * tree, but checking the length when doing
802 * that could be expensive. Until we fix that,
803 * we'll do the check here.
805 tvbuff_length = tvb_length_remaining(src_tvb,
807 if (tvbuff_length < 0) {
811 if (length > tvbuff_length)
812 length = tvbuff_length;
813 return tvb_get_ptr(src_tvb, fi->start, length);
816 g_assert_not_reached();
817 return NULL; /* not found */
820 /* Print a string, escaping out certain characters that need to
821 * escaped out for XML. */
823 print_escaped_xml(FILE *fh, const char *unescaped_string)
828 for (p = unescaped_string; *p != '\0'; p++) {
846 if (g_ascii_isprint(*p))
849 g_snprintf(temp_str, sizeof(temp_str), "\\x%x", (guint8)*p);
857 write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi)
865 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
866 fprintf(pdata->fh, "field length invalid!");
870 /* Find the data for this field. */
871 pd = get_field_data(pdata->src_list, fi);
874 /* Print a simple hex dump */
875 for (i = 0 ; i < fi->length; i++) {
876 fprintf(pdata->fh, "%02x", pd[i]);
882 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
884 gboolean multiple_sources;
886 struct data_source *src;
894 * Set "multiple_sources" iff this frame has more than one
895 * data source; if it does, we need to print the name of
896 * the data source before printing the data from the
899 multiple_sources = (edt->pi.data_src->next != NULL);
901 for (src_le = edt->pi.data_src; src_le != NULL;
902 src_le = src_le->next) {
903 src = (struct data_source *)src_le->data;
904 tvb = get_data_source_tvb(src);
905 if (multiple_sources) {
906 name = get_data_source_name(src);
907 line = g_strdup_printf("%s:", name);
908 print_line(stream, 0, line);
911 length = tvb_length(tvb);
914 cp = tvb_get_ptr(tvb, 0, length);
915 if (!print_hex_data_buffer(stream, cp, length,
916 edt->pi.fd->flags.encoding))
923 * This routine is based on a routine created by Dan Lasley
924 * <DLASLEY@PROMUS.com>.
926 * It was modified for Wireshark by Gilbert Ramirez and others.
929 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
930 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
931 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
932 /* max number of characters hex dump takes -
933 2 digits plus trailing blank */
934 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
935 /* number of characters those bytes take;
936 3 characters per byte of hex dump,
937 2 blanks separating hex from ASCII,
938 1 character per byte of ASCII dump */
939 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
940 /* number of characters per line;
941 offset, 2 blanks separating offset
942 from data dump, data dump */
945 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
946 guint length, packet_char_enc encoding)
948 register unsigned int ad, i, j, k, l;
950 gchar line[MAX_LINE_LEN + 1];
951 unsigned int use_digits;
952 static gchar binhex[16] = {
953 '0', '1', '2', '3', '4', '5', '6', '7',
954 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
957 * How many of the leading digits of the offset will we supply?
958 * We always supply at least 4 digits, but if the maximum offset
959 * won't fit in 4 digits, we use as many digits as will be needed.
961 if (((length - 1) & 0xF0000000) != 0)
962 use_digits = 8; /* need all 8 digits */
963 else if (((length - 1) & 0x0F000000) != 0)
964 use_digits = 7; /* need 7 digits */
965 else if (((length - 1) & 0x00F00000) != 0)
966 use_digits = 6; /* need 6 digits */
967 else if (((length - 1) & 0x000F0000) != 0)
968 use_digits = 5; /* need 5 digits */
970 use_digits = 4; /* we'll supply 4 digits */
979 * Start of a new line.
985 c = (ad >> (l*4)) & 0xF;
986 line[j++] = binhex[c];
990 memset(line+j, ' ', DATA_DUMP_LEN);
993 * Offset in line of ASCII dump.
995 k = j + HEX_DUMP_LEN + 2;
998 line[j++] = binhex[c>>4];
999 line[j++] = binhex[c&0xf];
1001 if (encoding == PACKET_CHAR_ENC_CHAR_EBCDIC) {
1002 c = EBCDIC_to_ASCII1(c);
1004 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
1006 if ((i & 15) == 0 || i == length) {
1008 * We'll be starting a new line, or
1009 * we're finished printing this buffer;
1010 * dump out the line we've constructed,
1011 * and advance the offset.
1014 if (!print_line(stream, 0, line))
1023 void ps_clean_string(char *out, const char *in, int outbuf_size)
1033 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
1054 /* Some formats need stuff at the beginning of the output */
1056 print_preamble(print_stream_t *self, gchar *filename)
1058 return self->ops->print_preamble ? (self->ops->print_preamble)(self, filename) : TRUE;
1062 print_line(print_stream_t *self, int indent, const char *line)
1064 return (self->ops->print_line)(self, indent, line);
1067 /* Insert bookmark */
1069 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
1071 return self->ops->print_bookmark ? (self->ops->print_bookmark)(self, name, title) : TRUE;
1075 new_page(print_stream_t *self)
1077 return self->ops->new_page ? (self->ops->new_page)(self) : TRUE;
1080 /* Some formats need stuff at the end of the output */
1082 print_finale(print_stream_t *self)
1084 return self->ops->print_finale ? (self->ops->print_finale)(self) : TRUE;
1088 destroy_print_stream(print_stream_t *self)
1090 return self->ops->destroy ? (self->ops->destroy)(self) : TRUE;
1099 print_line_text(print_stream_t *self, int indent, const char *line)
1101 output_text *output = (output_text *)self->data;
1102 char space[MAX_INDENT+1];
1106 /* Prepare the tabs for printing, depending on tree level */
1107 num_spaces = indent * 4;
1108 if (num_spaces > MAX_INDENT) {
1109 num_spaces = MAX_INDENT;
1111 for (i = 0; i < num_spaces; i++) {
1114 /* The string is NUL-terminated */
1115 space[num_spaces] = '\0';
1117 fputs(space, output->fh);
1118 fputs(line, output->fh);
1119 putc('\n', output->fh);
1120 return !ferror(output->fh);
1124 new_page_text(print_stream_t *self)
1126 output_text *output = (output_text *)self->data;
1128 fputs("\f", output->fh);
1129 return !ferror(output->fh);
1133 destroy_text(print_stream_t *self)
1135 output_text *output = (output_text *)self->data;
1138 ret = close_print_dest(output->to_file, output->fh);
1144 static const print_stream_ops_t print_text_ops = {
1145 NULL, /* preamble */
1147 NULL, /* bookmark */
1153 static print_stream_t *
1154 print_stream_text_alloc(gboolean to_file, FILE *fh)
1156 print_stream_t *stream;
1157 output_text *output;
1159 output = (output_text *)g_malloc(sizeof *output);
1160 output->to_file = to_file;
1162 stream = (print_stream_t *)g_malloc(sizeof (print_stream_t));
1163 stream->ops = &print_text_ops;
1164 stream->data = output;
1170 print_stream_text_new(gboolean to_file, const char *dest)
1174 fh = open_print_dest(to_file, dest);
1178 return print_stream_text_alloc(to_file, fh);
1182 print_stream_text_stdio_new(FILE *fh)
1184 return print_stream_text_alloc(TRUE, fh);
1193 print_preamble_ps(print_stream_t *self, gchar *filename)
1195 output_ps *output = (output_ps *)self->data;
1196 char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1198 print_ps_preamble(output->fh);
1200 fputs("%% the page title\n", output->fh);
1201 ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
1202 fprintf(output->fh, "/ws_pagetitle (%s - Wireshark " VERSION "%s) def\n", psbuffer, wireshark_svnversion);
1203 fputs("\n", output->fh);
1204 return !ferror(output->fh);
1208 print_line_ps(print_stream_t *self, int indent, const char *line)
1210 output_ps *output = (output_ps *)self->data;
1211 char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1213 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
1214 fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
1215 return !ferror(output->fh);
1219 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
1221 output_ps *output = (output_ps *)self->data;
1222 char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1225 * See the Adobe "pdfmark reference":
1227 * http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
1229 * The pdfmark stuff tells code that turns PostScript into PDF
1230 * things that it should do.
1232 * The /OUT stuff creates a bookmark that goes to the
1233 * destination with "name" as the name and "title" as the title.
1235 * The "/DEST" creates the destination.
1237 ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
1238 fprintf(output->fh, "[/Dest /%s /Title (%s) /OUT pdfmark\n", name,
1240 fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
1242 fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
1244 fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
1245 return !ferror(output->fh);
1249 new_page_ps(print_stream_t *self)
1251 output_ps *output = (output_ps *)self->data;
1253 fputs("formfeed\n", output->fh);
1254 return !ferror(output->fh);
1258 print_finale_ps(print_stream_t *self)
1260 output_ps *output = (output_ps *)self->data;
1262 print_ps_finale(output->fh);
1263 return !ferror(output->fh);
1267 destroy_ps(print_stream_t *self)
1269 output_ps *output = (output_ps *)self->data;
1272 ret = close_print_dest(output->to_file, output->fh);
1278 static const print_stream_ops_t print_ps_ops = {
1287 static print_stream_t *
1288 print_stream_ps_alloc(gboolean to_file, FILE *fh)
1290 print_stream_t *stream;
1293 output = (output_ps *)g_malloc(sizeof *output);
1294 output->to_file = to_file;
1296 stream = (print_stream_t *)g_malloc(sizeof (print_stream_t));
1297 stream->ops = &print_ps_ops;
1298 stream->data = output;
1304 print_stream_ps_new(gboolean to_file, const char *dest)
1308 fh = open_print_dest(to_file, dest);
1312 return print_stream_ps_alloc(to_file, fh);
1316 print_stream_ps_stdio_new(FILE *fh)
1318 return print_stream_ps_alloc(TRUE, fh);
1321 output_fields_t* output_fields_new(void)
1323 output_fields_t* fields = g_new(output_fields_t, 1);
1324 fields->print_header = FALSE;
1325 fields->separator = '\t';
1326 fields->occurrence = 'a';
1327 fields->aggregator = ',';
1328 fields->fields = NULL; /*Do lazy initialisation */
1329 fields->field_indicies = NULL;
1330 fields->field_values = NULL;
1332 fields->includes_col_fields = FALSE;
1336 gsize output_fields_num_fields(output_fields_t* fields)
1340 if(NULL == fields->fields) {
1343 return fields->fields->len;
1347 void output_fields_free(output_fields_t* fields)
1351 if(NULL != fields->field_indicies) {
1352 /* Keys are stored in fields->fields, values are
1355 g_hash_table_destroy(fields->field_indicies);
1357 if(NULL != fields->fields) {
1359 for(i = 0; i < fields->fields->len; ++i) {
1360 gchar* field = (gchar *)g_ptr_array_index(fields->fields,i);
1363 g_ptr_array_free(fields->fields, TRUE);
1369 #define COLUMN_FIELD_FILTER "col."
1371 void output_fields_add(output_fields_t* fields, const gchar* field)
1379 if(NULL == fields->fields) {
1380 fields->fields = g_ptr_array_new();
1383 field_copy = g_strdup(field);
1385 g_ptr_array_add(fields->fields, field_copy);
1387 /* See if we have a column as a field entry */
1388 if (!strncmp(field, COLUMN_FIELD_FILTER, strlen(COLUMN_FIELD_FILTER)))
1389 fields->includes_col_fields = TRUE;
1393 gboolean output_fields_set_option(output_fields_t* info, gchar* option)
1395 const gchar* option_name;
1396 const gchar* option_value;
1401 if('\0' == *option) {
1402 return FALSE; /* Is this guarded against by option parsing? */
1404 option_name = strtok(option,"=");
1408 option_value = option + strlen(option_name) + 1;
1409 if(0 == strcmp(option_name, "header")) {
1410 switch(NULL == option_value ? '\0' : *option_value) {
1412 info->print_header = FALSE;
1415 info->print_header = TRUE;
1423 if(0 == strcmp(option_name,"separator")) {
1424 switch(NULL == option_value ? '\0' : *option_value) {
1428 switch(*++option_value) {
1430 info->separator = '\t';
1433 info->separator = ' ';
1436 info->separator = '\\';
1440 info->separator = *option_value;
1446 if(0 == strcmp(option_name, "occurrence")) {
1447 switch(NULL == option_value ? '\0' : *option_value) {
1451 info->occurrence = *option_value;
1459 if(0 == strcmp(option_name,"aggregator")) {
1460 switch(NULL == option_value ? '\0' : *option_value) {
1464 switch(*++option_value) {
1466 info->aggregator = ' ';
1469 info->aggregator = '\\';
1473 info->aggregator = *option_value;
1479 if(0 == strcmp(option_name, "quote")) {
1480 switch(NULL == option_value ? '\0' : *option_value) {
1481 default: /* Fall through */
1501 void output_fields_list_options(FILE *fh)
1503 fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
1504 fputs("header=y|n Print field abbreviations as first line of output (def: N: no)\n", fh);
1505 fputs("separator=/t|/s|<character> Set the separator to use;\n \"/t\" = tab, \"/s\" = space (def: /t: tab)\n", fh);
1506 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);
1507 fputs("aggregator=,|/s|<character> Set the aggregator to use;\n \",\" = comma, \"/s\" = space (def: ,: comma)\n", fh);
1508 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);
1511 gboolean output_fields_has_cols(output_fields_t* fields)
1514 return fields->includes_col_fields;
1517 void write_fields_preamble(output_fields_t* fields, FILE *fh)
1524 if(!fields->print_header) {
1528 for(i = 0; i < fields->fields->len; ++i) {
1529 const gchar* field = (const gchar *)g_ptr_array_index(fields->fields,i);
1531 fputc(fields->separator, fh);
1538 static void format_field_values(output_fields_t* fields, gpointer field_index, const gchar* value)
1540 if(NULL != value && '\0' != *value) {
1542 actual_index = GPOINTER_TO_UINT(field_index);
1543 /* Unwrap change made to disambiguiate zero / null */
1544 if (fields->field_values[actual_index - 1] == NULL ) {
1545 fields->field_values[actual_index - 1] = ep_strbuf_new(value);
1546 } else if (fields->occurrence == 'l' ) {
1547 /* print only the value of the last occurrence of the field */
1548 ep_strbuf_printf(fields->field_values[actual_index - 1],"%s",value);
1549 } else if (fields->occurrence == 'a' ) {
1550 /* print the value of all accurrences of the field */
1551 ep_strbuf_append_printf(fields->field_values[actual_index - 1],
1552 "%c%s",fields->aggregator,value);
1557 static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
1559 write_field_data_t *call_data;
1561 gpointer field_index;
1563 call_data = (write_field_data_t *)data;
1564 fi = PNODE_FINFO(node);
1566 /* dissection with an invisible proto tree? */
1569 field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
1570 if(NULL != field_index) {
1571 format_field_values(call_data->fields, field_index,
1572 get_node_field_value(fi, call_data->edt) /* ep_alloced string */
1577 if (node->first_child != NULL) {
1578 proto_tree_children_foreach(node, proto_tree_get_node_field_values,
1583 void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, column_info *cinfo, FILE *fh)
1588 gpointer field_index;
1590 write_field_data_t data;
1596 data.fields = fields;
1599 if(NULL == fields->field_indicies) {
1600 /* Prepare a lookup table from string abbreviation for field to its index. */
1601 fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
1604 while( i < fields->fields->len) {
1605 gchar* field = (gchar *)g_ptr_array_index(fields->fields, i);
1606 /* Store field indicies +1 so that zero is not a valid value,
1607 * and can be distinguished from NULL as a pointer.
1610 g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
1614 /* Buffer to store values for this packet */
1615 fields->field_values = ep_alloc_array0(emem_strbuf_t*, fields->fields->len);
1617 proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
1620 if (fields->includes_col_fields) {
1621 for (col = 0; col < cinfo->num_cols; col++) {
1622 /* Prepend COLUMN_FIELD_FILTER as the field name */
1623 col_name = ep_strdup_printf("%s%s", COLUMN_FIELD_FILTER, cinfo->col_title[col]);
1624 field_index = g_hash_table_lookup(fields->field_indicies, col_name);
1626 if(NULL != field_index) {
1627 format_field_values(fields, field_index, cinfo->col_data[col]);
1632 for(i = 0; i < fields->fields->len; ++i) {
1634 fputc(fields->separator, fh);
1636 if(NULL != fields->field_values[i]) {
1637 if(fields->quote != '\0') {
1638 fputc(fields->quote, fh);
1640 fputs(fields->field_values[i]->str, fh);
1641 if(fields->quote != '\0') {
1642 fputc(fields->quote, fh);
1648 void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
1653 /* Returns an ep_alloced string or a static constant*/
1654 const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
1656 if (fi->hfinfo->id == hf_text_only) {
1660 return fi->rep->representation;
1663 return get_field_hex_value(edt->pi.data_src, fi);
1666 else if (fi->hfinfo->id == proto_data) {
1667 /* Uninterpreted data, i.e., the "Data" protocol, is
1668 * printed as a field instead of a protocol. */
1669 return get_field_hex_value(edt->pi.data_src, fi);
1672 /* Normal protocols and fields */
1673 gchar *dfilter_string;
1676 switch (fi->hfinfo->type)
1679 /* Print out the full details for the protocol. */
1681 return fi->rep->representation;
1683 /* Just print out the protocol abbreviation */
1684 return fi->hfinfo->abbrev;
1687 /* Return "1" so that the presence of a field of type
1688 * FT_NONE can be checked when using -T fields */
1691 /* XXX - this is a hack until we can just call
1692 * fvalue_to_string_repr() for *all* FT_* types. */
1693 dfilter_string = proto_construct_match_selected_string(fi,
1695 if (dfilter_string != NULL) {
1696 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
1698 /* XXX - Remove double-quotes. Again, once we
1699 * can call fvalue_to_string_repr(), we can
1700 * ask it not to produce the version for
1701 * display-filters, and thus, no
1703 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
1704 dfilter_string[strlen(dfilter_string)-1] = '\0';
1708 return &(dfilter_string[chop_len]);
1710 return get_field_hex_value(edt->pi.data_src, fi);
1717 get_field_hex_value(GSList* src_list, field_info *fi)
1724 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
1725 return "field length invalid!";
1728 /* Find the data for this field. */
1729 pd = get_field_data(src_list, fi);
1736 const int chars_per_byte = 2;
1738 len = chars_per_byte * fi->length;
1739 buffer = ep_alloc_array(gchar, len + 1);
1740 buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
1742 /* Print a simple hex dump */
1743 for (i = 0 ; i < fi->length; i++) {
1744 g_snprintf(p, chars_per_byte+1, "%02x", pd[i]);
1745 p += chars_per_byte;