2 * Routines for printing packet analysis trees.
6 * Gilbert Ramirez <gram@alumni.rice.edu>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include <epan/epan.h>
35 #include <epan/epan_dissect.h>
36 #include <epan/tvbuff.h>
37 #include <epan/packet.h>
38 #include <epan/emem.h>
40 #include "packet-range.h"
43 #include "file_util.h"
44 #include <epan/charsets.h>
45 #include <epan/dissectors/packet-data.h>
46 #include <epan/dissectors/packet-frame.h>
48 #define PDML_VERSION "0"
49 #define PSML_VERSION "0"
53 print_stream_t *stream;
56 print_dissections_e print_dissections;
57 gboolean print_hex_for_data;
70 output_fields_t* fields;
74 struct _output_fields {
75 gboolean print_header;
78 GHashTable* field_indicies;
79 const gchar** field_values;
83 static const gchar* get_field_hex_value(GSList* src_list, field_info *fi);
84 static const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt);
85 static void proto_tree_print_node(proto_node *node, gpointer data);
86 static void proto_tree_write_node_pdml(proto_node *node, gpointer data);
87 static const guint8 *get_field_data(GSList *src_list, field_info *fi);
88 static void write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi);
89 static gboolean print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
90 guint length, char_enc encoding);
91 static void ps_clean_string(unsigned char *out, const unsigned char *in,
93 static void print_escaped_xml(FILE *fh, const char *unescaped_string);
95 static void print_pdml_geninfo(proto_tree *tree, FILE *fh);
97 static void proto_tree_get_node_field_values(proto_node *node, gpointer data);
100 open_print_dest(int to_file, const char *dest)
104 /* Open the file or command for output */
106 fh = eth_fopen(dest, "w");
108 fh = popen(dest, "w");
114 close_print_dest(int to_file, FILE *fh)
116 /* Close the file or command */
118 return (fclose(fh) == 0);
120 return (pclose(fh) == 0);
123 #define MAX_PS_LINE_LENGTH 256
126 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
127 print_stream_t *stream)
131 /* Create the output */
133 data.stream = stream;
135 data.src_list = edt->pi.data_src;
136 data.encoding = edt->pi.fd->flags.encoding;
137 data.print_dissections = print_args->print_dissections;
138 /* If we're printing the entire packet in hex, don't
139 print uninterpreted data fields in hex as well. */
140 data.print_hex_for_data = !print_args->print_hex;
143 proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
147 #define MAX_INDENT 160
149 /* Print a tree's data, and any child nodes. */
151 void proto_tree_print_node(proto_node *node, gpointer data)
153 field_info *fi = PITEM_FINFO(node);
154 print_data *pdata = (print_data*) data;
156 gchar label_str[ITEM_LABEL_LENGTH];
159 /* Don't print invisible entries. */
160 if (PROTO_ITEM_IS_HIDDEN(node))
163 /* Give up if we've already gotten an error. */
167 /* was a free format label produced? */
169 label_ptr = fi->rep->representation;
171 else { /* no, make a generic label */
172 label_ptr = label_str;
173 proto_item_fill_label(fi, label_str);
176 if (PROTO_ITEM_IS_GENERATED(node)) {
177 label_ptr = g_strdup_printf("[%s]", label_ptr);
180 if (!print_line(pdata->stream, pdata->level, label_ptr)) {
181 pdata->success = FALSE;
185 if (PROTO_ITEM_IS_GENERATED(node)) {
189 /* If it's uninterpreted data, dump it (unless our caller will
190 be printing the entire packet in hex). */
191 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
193 * Find the data for this field.
195 pd = get_field_data(pdata->src_list, fi);
197 if (!print_hex_data_buffer(pdata->stream, pd,
198 fi->length, pdata->encoding)) {
199 pdata->success = FALSE;
205 /* If we're printing all levels, or if this node is one with a
206 subtree and its subtree is expanded, recurse into the subtree,
208 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
209 if (pdata->print_dissections == print_dissections_expanded ||
210 (pdata->print_dissections == print_dissections_as_displayed &&
211 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
212 if (node->first_child != NULL) {
214 proto_tree_children_foreach(node,
215 proto_tree_print_node, pdata);
224 write_pdml_preamble(FILE *fh)
226 fputs("<?xml version=\"1.0\"?>\n", fh);
227 fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
228 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
232 proto_tree_write_pdml(epan_dissect_t *edt, FILE *fh)
234 write_pdml_data data;
236 /* Create the output */
239 data.src_list = edt->pi.data_src;
242 fprintf(fh, "<packet>\n");
244 /* Print a "geninfo" protocol as required by PDML */
245 print_pdml_geninfo(edt->tree, fh);
247 proto_tree_children_foreach(edt->tree, proto_tree_write_node_pdml,
250 fprintf(fh, "</packet>\n\n");
253 /* Write out a tree's data, and any child nodes, as PDML */
255 proto_tree_write_node_pdml(proto_node *node, gpointer data)
257 field_info *fi = PITEM_FINFO(node);
258 write_pdml_data *pdata = (write_pdml_data*) data;
259 const gchar *label_ptr;
260 gchar label_str[ITEM_LABEL_LENGTH];
261 char *dfilter_string;
265 /* Will wrap up top-level field items inside a fake protocol wrapper to
266 preserve the PDML schema */
267 gboolean wrap_in_fake_protocol =
268 (((fi->hfinfo->type != FT_PROTOCOL) ||
269 (fi->hfinfo->id == proto_data)) &&
270 (pdata->level == 0));
272 /* Indent to the correct level */
273 for (i = -1; i < pdata->level; i++) {
274 fputs(" ", pdata->fh);
277 if (wrap_in_fake_protocol) {
278 /* Open fake protocol wrapper */
279 fputs("<proto name=\"fake-field-wrapper\">\n", pdata->fh);
281 /* Indent to increased level before writint out field */
283 for (i = -1; i < pdata->level; i++) {
284 fputs(" ", pdata->fh);
288 /* Text label. It's printed as a field with no name. */
289 if (fi->hfinfo->id == hf_text_only) {
292 label_ptr = fi->rep->representation;
298 /* Show empty name since it is a required field */
299 fputs("<field name=\"", pdata->fh);
300 fputs("\" show=\"", pdata->fh);
301 print_escaped_xml(pdata->fh, label_ptr);
303 fprintf(pdata->fh, "\" size=\"%d", fi->length);
304 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
306 fputs("\" value=\"", pdata->fh);
307 write_pdml_field_hex_value(pdata, fi);
309 if (node->first_child != NULL) {
310 fputs("\">\n", pdata->fh);
313 fputs("\"/>\n", pdata->fh);
317 /* Uninterpreted data, i.e., the "Data" protocol, is
318 * printed as a field instead of a protocol. */
319 else if (fi->hfinfo->id == proto_data) {
321 /* Write out field with data */
322 fputs("<field name=\"data\" value=\"", pdata->fh);
323 write_pdml_field_hex_value(pdata, fi);
324 fputs("\"/>\n", pdata->fh);
326 /* Normal protocols and fields */
328 if (fi->hfinfo->type == FT_PROTOCOL) {
329 fputs("<proto name=\"", pdata->fh);
332 fputs("<field name=\"", pdata->fh);
334 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
338 * http://analyzer.polito.it/30alpha/docs/dissectors/PDMLSpec.htm
340 * the show fields contains things in 'human readable' format
341 * showname: contains only the name of the field
342 * show: contains only the data of the field
343 * showdtl: contains additional details of the field data
344 * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
346 * XXX - the showname shouldn't contain the field data itself
347 * (like it's contained in the fi->rep->representation).
348 * Unfortunately, we don't have the field data representation for
349 * all fields, so this isn't currently possible */
350 fputs("\" showname=\"", pdata->fh);
351 print_escaped_xml(pdata->fh, fi->hfinfo->name);
355 fputs("\" showname=\"", pdata->fh);
356 print_escaped_xml(pdata->fh, fi->rep->representation);
359 label_ptr = label_str;
360 proto_item_fill_label(fi, label_str);
361 fputs("\" showname=\"", pdata->fh);
362 print_escaped_xml(pdata->fh, label_ptr);
365 if (PROTO_ITEM_IS_HIDDEN(node))
366 fprintf(pdata->fh, "\" hide=\"yes");
368 fprintf(pdata->fh, "\" size=\"%d", fi->length);
369 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
370 /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
372 /* show, value, and unmaskedvalue attributes */
373 switch (fi->hfinfo->type)
378 fputs("\" show=\"\" value=\"", pdata->fh);
381 /* XXX - this is a hack until we can just call
382 * fvalue_to_string_repr() for *all* FT_* types. */
383 dfilter_string = proto_construct_match_selected_string(fi,
385 if (dfilter_string != NULL) {
386 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
388 /* XXX - Remove double-quotes. Again, once we
389 * can call fvalue_to_string_repr(), we can
390 * ask it not to produce the version for
391 * display-filters, and thus, no
393 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
394 dfilter_string[strlen(dfilter_string)-1] = '\0';
398 fputs("\" show=\"", pdata->fh);
399 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
403 * XXX - should we omit "value" for any fields?
404 * What should we do for fields whose length is 0?
405 * They might come from a pseudo-header or from
406 * the capture header (e.g., time stamps), or
407 * they might be generated fields.
409 if (fi->length > 0) {
410 fputs("\" value=\"", pdata->fh);
412 if (fi->hfinfo->bitmask!=0) {
413 fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
414 fputs("\" unmaskedvalue=\"", pdata->fh);
415 write_pdml_field_hex_value(pdata, fi);
418 write_pdml_field_hex_value(pdata, fi);
423 if (node->first_child != NULL) {
424 fputs("\">\n", pdata->fh);
426 else if (fi->hfinfo->id == proto_data) {
427 fputs("\">\n", pdata->fh);
430 fputs("\"/>\n", pdata->fh);
434 /* We always print all levels for PDML. Recurse here. */
435 if (node->first_child != NULL) {
437 proto_tree_children_foreach(node,
438 proto_tree_write_node_pdml, pdata);
442 /* Take back the extra level we added for fake wrapper protocol */
443 if (wrap_in_fake_protocol) {
447 if (node->first_child != NULL) {
448 /* Indent to correct level */
449 for (i = -1; i < pdata->level; i++) {
450 fputs(" ", pdata->fh);
452 /* Close off current element */
453 if (fi->hfinfo->id != proto_data) { /* Data protocol uses simple tags */
454 if (fi->hfinfo->type == FT_PROTOCOL) {
455 fputs("</proto>\n", pdata->fh);
458 fputs("</field>\n", pdata->fh);
463 /* Close off fake wrapper protocol */
464 if (wrap_in_fake_protocol) {
465 fputs("</proto>\n", pdata->fh);
469 /* Print info for a 'geninfo' pseudo-protocol. This is required by
470 * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
471 * but we produce a 'geninfo' protocol in the PDML to conform to spec.
472 * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
474 print_pdml_geninfo(proto_tree *tree, FILE *fh)
476 guint32 num, len, caplen;
478 GPtrArray *finfo_array;
479 field_info *frame_finfo;
481 /* Get frame protocol's finfo. */
482 finfo_array = proto_find_finfo(tree, proto_frame);
483 if (g_ptr_array_len(finfo_array) < 1) {
486 frame_finfo = finfo_array->pdata[0];
487 g_ptr_array_free(finfo_array, FALSE);
489 /* frame.number --> geninfo.num */
490 finfo_array = proto_find_finfo(tree, hf_frame_number);
491 if (g_ptr_array_len(finfo_array) < 1) {
494 num = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
495 g_ptr_array_free(finfo_array, FALSE);
497 /* frame.pkt_len --> geninfo.len */
498 finfo_array = proto_find_finfo(tree, hf_frame_packet_len);
499 if (g_ptr_array_len(finfo_array) < 1) {
502 len = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
503 g_ptr_array_free(finfo_array, FALSE);
505 /* frame.cap_len --> geninfo.caplen */
506 finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
507 if (g_ptr_array_len(finfo_array) < 1) {
510 caplen = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
511 g_ptr_array_free(finfo_array, FALSE);
513 /* frame.time --> geninfo.timestamp */
514 finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
515 if (g_ptr_array_len(finfo_array) < 1) {
518 timestamp = fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
519 g_ptr_array_free(finfo_array, FALSE);
521 /* Print geninfo start */
523 " <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
524 frame_finfo->length);
526 /* Print geninfo.num */
528 " <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
529 num, num, frame_finfo->length);
531 /* Print geninfo.len */
533 " <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Packet Length\" value=\"%x\" size=\"%u\"/>\n",
534 len, len, frame_finfo->length);
536 /* Print geninfo.caplen */
538 " <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
539 caplen, caplen, frame_finfo->length);
541 /* Print geninfo.timestamp */
543 " <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
544 abs_time_to_str(timestamp), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
546 /* Print geninfo end */
552 write_pdml_finale(FILE *fh)
554 fputs("</pdml>\n", fh);
558 write_psml_preamble(FILE *fh)
560 fputs("<?xml version=\"1.0\"?>\n", fh);
561 fputs("<psml version=\"" PSML_VERSION "\" ", fh);
562 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
566 proto_tree_write_psml(epan_dissect_t *edt, FILE *fh)
570 /* if this is the first packet, we have to create the PSML structure output */
571 if(edt->pi.fd->num == 1) {
572 fprintf(fh, "<structure>\n");
574 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
575 fprintf(fh, "<section>");
576 print_escaped_xml(fh, edt->pi.cinfo->col_title[i]);
577 fprintf(fh, "</section>\n");
580 fprintf(fh, "</structure>\n\n");
583 fprintf(fh, "<packet>\n");
585 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
586 fprintf(fh, "<section>");
587 print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
588 fprintf(fh, "</section>\n");
591 fprintf(fh, "</packet>\n\n");
595 write_psml_finale(FILE *fh)
597 fputs("</psml>\n", fh);
601 write_csv_preamble(FILE *fh _U_)
607 proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
611 /* if this is the first packet, we have to write the CSV header */
612 if(edt->pi.fd->num == 1) {
613 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
614 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_title[i]);
616 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_title[i]);
619 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
620 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_data[i]);
622 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_data[i]);
626 write_csv_finale(FILE *fh _U_)
632 * Find the data source for a specified field, and return a pointer
633 * to the data in it. Returns NULL if the data is out of bounds.
635 static const guint8 *
636 get_field_data(GSList *src_list, field_info *fi)
641 gint length, tvbuff_length;
643 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
646 if (fi->ds_tvb == src_tvb) {
650 * XXX - a field can have a length that runs past
651 * the end of the tvbuff. Ideally, that should
652 * be fixed when adding an item to the protocol
653 * tree, but checking the length when doing
654 * that could be expensive. Until we fix that,
655 * we'll do the check here.
657 tvbuff_length = tvb_length_remaining(src_tvb,
659 if (tvbuff_length < 0) {
663 if (length > tvbuff_length)
664 length = tvbuff_length;
665 return tvb_get_ptr(src_tvb, fi->start, length);
668 g_assert_not_reached();
669 return NULL; /* not found */
672 /* Print a string, escaping out certain characters that need to
673 * escaped out for XML. */
675 print_escaped_xml(FILE *fh, const char *unescaped_string)
679 for (p = unescaped_string; *p != '\0'; p++) {
703 write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi)
708 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
709 fprintf(pdata->fh, "field length invalid!");
713 /* Find the data for this field. */
714 pd = get_field_data(pdata->src_list, fi);
717 /* Print a simple hex dump */
718 for (i = 0 ; i < fi->length; i++) {
719 fprintf(pdata->fh, "%02x", pd[i]);
725 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
727 gboolean multiple_sources;
737 * Set "multiple_sources" iff this frame has more than one
738 * data source; if it does, we need to print the name of
739 * the data source before printing the data from the
742 multiple_sources = (edt->pi.data_src->next != NULL);
744 for (src_le = edt->pi.data_src; src_le != NULL;
745 src_le = src_le->next) {
748 if (multiple_sources) {
750 print_line(stream, 0, "");
751 line = g_strdup_printf("%s:", name);
752 print_line(stream, 0, line);
755 length = tvb_length(tvb);
758 cp = tvb_get_ptr(tvb, 0, length);
759 if (!print_hex_data_buffer(stream, cp, length,
760 edt->pi.fd->flags.encoding))
767 * This routine is based on a routine created by Dan Lasley
768 * <DLASLEY@PROMUS.com>.
770 * It was modified for Wireshark by Gilbert Ramirez and others.
773 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
774 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
775 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
776 /* max number of characters hex dump takes -
777 2 digits plus trailing blank */
778 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
779 /* number of characters those bytes take;
780 3 characters per byte of hex dump,
781 2 blanks separating hex from ASCII,
782 1 character per byte of ASCII dump */
783 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
784 /* number of characters per line;
785 offset, 2 blanks separating offset
786 from data dump, data dump */
789 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
790 guint length, char_enc encoding)
792 register unsigned int ad, i, j, k, l;
794 guchar line[MAX_LINE_LEN + 1];
795 unsigned int use_digits;
796 static guchar binhex[16] = {
797 '0', '1', '2', '3', '4', '5', '6', '7',
798 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
800 if (!print_line(stream, 0, ""))
804 * How many of the leading digits of the offset will we supply?
805 * We always supply at least 4 digits, but if the maximum offset
806 * won't fit in 4 digits, we use as many digits as will be needed.
808 if (((length - 1) & 0xF0000000) != 0)
809 use_digits = 8; /* need all 8 digits */
810 else if (((length - 1) & 0x0F000000) != 0)
811 use_digits = 7; /* need 7 digits */
812 else if (((length - 1) & 0x00F00000) != 0)
813 use_digits = 6; /* need 6 digits */
814 else if (((length - 1) & 0x000F0000) != 0)
815 use_digits = 5; /* need 5 digits */
817 use_digits = 4; /* we'll supply 4 digits */
826 * Start of a new line.
833 c = (ad >> (l*4)) & 0xF;
834 line[j++] = binhex[c];
838 memset(line+j, ' ', DATA_DUMP_LEN);
841 * Offset in line of ASCII dump.
843 k = j + HEX_DUMP_LEN + 2;
846 line[j++] = binhex[c>>4];
847 line[j++] = binhex[c&0xf];
849 if (encoding == CHAR_EBCDIC) {
850 c = EBCDIC_to_ASCII1(c);
852 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
854 if ((i & 15) == 0 || i == length) {
856 * We'll be starting a new line, or
857 * we're finished printing this buffer;
858 * dump out the line we've constructed,
859 * and advance the offset.
862 if (!print_line(stream, 0, line))
871 void ps_clean_string(unsigned char *out, const unsigned char *in,
877 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
898 /* Some formats need stuff at the beginning of the output */
900 print_preamble(print_stream_t *self, gchar *filename)
902 return (self->ops->print_preamble)(self, filename);
906 print_line(print_stream_t *self, int indent, const char *line)
908 return (self->ops->print_line)(self, indent, line);
911 /* Insert bookmark */
913 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
915 return (self->ops->print_bookmark)(self, name, title);
919 new_page(print_stream_t *self)
921 return (self->ops->new_page)(self);
924 /* Some formats need stuff at the end of the output */
926 print_finale(print_stream_t *self)
928 return (self->ops->print_finale)(self);
932 destroy_print_stream(print_stream_t *self)
934 return (self->ops->destroy)(self);
943 print_preamble_text(print_stream_t *self _U_, gchar *filename _U_)
946 return TRUE; /* always succeeds */
950 print_line_text(print_stream_t *self, int indent, const char *line)
952 output_text *output = self->data;
953 char space[MAX_INDENT+1];
957 /* Prepare the tabs for printing, depending on tree level */
958 num_spaces = indent * 4;
959 if (num_spaces > MAX_INDENT) {
960 num_spaces = MAX_INDENT;
962 for (i = 0; i < num_spaces; i++) {
965 /* The string is NUL-terminated */
966 space[num_spaces] = '\0';
968 fputs(space, output->fh);
969 fputs(line, output->fh);
970 putc('\n', output->fh);
971 return !ferror(output->fh);
975 print_bookmark_text(print_stream_t *self _U_, const gchar *name _U_,
976 const gchar *title _U_)
983 new_page_text(print_stream_t *self)
985 output_text *output = self->data;
987 fputs("\f", output->fh);
988 return !ferror(output->fh);
992 print_finale_text(print_stream_t *self _U_)
995 return TRUE; /* always succeeds */
999 destroy_text(print_stream_t *self)
1001 output_text *output = self->data;
1004 ret = close_print_dest(output->to_file, output->fh);
1010 static const print_stream_ops_t print_text_ops = {
1011 print_preamble_text,
1013 print_bookmark_text,
1020 print_stream_text_new(int to_file, const char *dest)
1023 print_stream_t *stream;
1024 output_text *output;
1026 fh = open_print_dest(to_file, dest);
1030 output = g_malloc(sizeof *output);
1031 output->to_file = to_file;
1033 stream = g_malloc(sizeof (print_stream_t));
1034 stream->ops = &print_text_ops;
1035 stream->data = output;
1041 print_stream_text_stdio_new(FILE *fh)
1043 print_stream_t *stream;
1044 output_text *output;
1046 output = g_malloc(sizeof *output);
1047 output->to_file = TRUE;
1049 stream = g_malloc(sizeof (print_stream_t));
1050 stream->ops = &print_text_ops;
1051 stream->data = output;
1062 print_preamble_ps(print_stream_t *self, gchar *filename)
1064 output_ps *output = self->data;
1065 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1067 print_ps_preamble(output->fh);
1069 fputs("%% Set the font to 10 point\n", output->fh);
1070 fputs("/Courier findfont 10 scalefont setfont\n", output->fh);
1071 fputs("\n", output->fh);
1072 fputs("%% the page title\n", output->fh);
1073 ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
1074 fprintf(output->fh, "/eth_pagetitle (%s - Wireshark) def\n", psbuffer);
1075 fputs("\n", output->fh);
1076 return !ferror(output->fh);
1080 print_line_ps(print_stream_t *self, int indent, const char *line)
1082 output_ps *output = self->data;
1083 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1085 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
1086 fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
1087 return !ferror(output->fh);
1091 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
1093 output_ps *output = self->data;
1094 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1097 * See the Adobe "pdfmark reference":
1099 * http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
1101 * The pdfmark stuff tells code that turns PostScript into PDF
1102 * things that it should do.
1104 * The /OUT stuff creates a bookmark that goes to the
1105 * destination with "name" as the name and "title" as the title.
1107 * The "/DEST" creates the destination.
1109 ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
1110 fprintf(output->fh, "[/Dest /%s /Title (%s) /OUT pdfmark\n", name,
1112 fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
1114 fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
1116 fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
1117 return !ferror(output->fh);
1121 new_page_ps(print_stream_t *self)
1123 output_ps *output = self->data;
1125 fputs("formfeed\n", output->fh);
1126 return !ferror(output->fh);
1130 print_finale_ps(print_stream_t *self)
1132 output_ps *output = self->data;
1134 print_ps_finale(output->fh);
1135 return !ferror(output->fh);
1139 destroy_ps(print_stream_t *self)
1141 output_ps *output = self->data;
1144 ret = close_print_dest(output->to_file, output->fh);
1150 static const print_stream_ops_t print_ps_ops = {
1160 print_stream_ps_new(int to_file, const char *dest)
1163 print_stream_t *stream;
1166 fh = open_print_dest(to_file, dest);
1170 output = g_malloc(sizeof *output);
1171 output->to_file = to_file;
1173 stream = g_malloc(sizeof (print_stream_t));
1174 stream->ops = &print_ps_ops;
1175 stream->data = output;
1181 print_stream_ps_stdio_new(FILE *fh)
1183 print_stream_t *stream;
1186 output = g_malloc(sizeof *output);
1187 output->to_file = TRUE;
1189 stream = g_malloc(sizeof (print_stream_t));
1190 stream->ops = &print_ps_ops;
1191 stream->data = output;
1196 output_fields_t* output_fields_new()
1198 output_fields_t* fields = g_new(output_fields_t, 1);
1199 fields->print_header = FALSE;
1200 fields->separator = '\t';
1201 fields->fields = NULL; /*Do lazy initialisation */
1202 fields->field_indicies = NULL;
1203 fields->field_values = NULL;
1208 gsize output_fields_num_fields(output_fields_t* fields)
1212 if(NULL == fields->fields) {
1215 return fields->fields->len;
1219 void output_fields_free(output_fields_t* fields)
1223 if(NULL != fields->field_indicies) {
1224 /* Keys are stored in fields->fields, values are
1227 g_hash_table_destroy(fields->field_indicies);
1229 if(NULL != fields->fields) {
1231 for(i = 0; i < fields->fields->len; ++i) {
1232 gchar* field = g_ptr_array_index(fields->fields,i);
1235 g_ptr_array_free(fields->fields, TRUE);
1241 void output_fields_add(output_fields_t* fields, const gchar* field)
1249 if(NULL == fields->fields) {
1250 fields->fields = g_ptr_array_new();
1253 field_copy = g_strdup(field);
1255 g_ptr_array_add(fields->fields, field_copy);
1258 gboolean output_fields_set_option(output_fields_t* info, gchar* option)
1260 const gchar* option_name;
1261 const gchar* option_value;
1266 if('\0' == *option) {
1267 return FALSE; /* Is this guarded against by option parsing? */
1269 option_name = strtok(option,"=");
1270 option_value = option + strlen(option_name) + 1;
1271 if(0 == strcmp(option_name, "header")) {
1272 switch(NULL == option_value ? '\0' : *option_value) {
1274 info->print_header = FALSE;
1277 info->print_header = TRUE;
1285 if(0 == strcmp(option_name,"separator")) {
1286 switch(NULL == option_value ? '\0' : *option_value) {
1290 switch(*++option_value) {
1292 info->separator = '\t';
1295 info->separator = ' ';
1298 info->separator = '\\';
1302 info->separator = *option_value;
1308 if(0 == strcmp(option_name, "quote")) {
1309 switch(NULL == option_value ? '\0' : *option_value) {
1310 default: /* Fall through */
1330 void output_fields_list_options(FILE *fh)
1332 fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
1333 fputs("header=y|n Print field abbreviations as first line of output (def: N: no)\n", fh);
1334 fputs("separator=/t|/s|<character> Set the separator to use; \"/t\" = tab,\n \"/s\" = space (def: /t: tab)\n", fh);
1335 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);
1339 void write_fields_preamble(output_fields_t* fields, FILE *fh)
1346 if(!fields->print_header) {
1350 for(i = 0; i < fields->fields->len; ++i) {
1351 const gchar* field = g_ptr_array_index(fields->fields,i);
1353 fputc(fields->separator, fh);
1360 static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
1362 write_field_data_t *call_data;
1364 gpointer field_index;
1367 fi = PITEM_FINFO(node);
1369 field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
1370 if(NULL != field_index) {
1373 value = get_node_field_value(fi, call_data->edt); /* ep_alloced string */
1375 if(NULL != value && '\0' != *value) {
1377 actual_index = GPOINTER_TO_UINT(field_index);
1378 /* Unwrap change made to disambiguiate zero / null */
1379 call_data->fields->field_values[actual_index - 1] = value;
1384 if (node->first_child != NULL) {
1385 proto_tree_children_foreach(node, proto_tree_get_node_field_values,
1390 void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, FILE *fh)
1394 write_field_data_t data;
1400 data.fields = fields;
1403 if(NULL == fields->field_indicies) {
1404 /* Prepare a lookup table from string abbreviation for field to its index. */
1405 fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
1408 while( i < fields->fields->len) {
1409 gchar* field = g_ptr_array_index(fields->fields, i);
1410 /* Store field indicies +1 so that zero is not a valid value,
1411 * and can be distinguished from NULL as a pointer.
1414 g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
1418 /* Buffer to store values for this packet */
1419 fields->field_values = ep_alloc_array0(const gchar*, fields->fields->len);
1421 proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
1424 for(i = 0; i < fields->fields->len; ++i) {
1426 fputc(fields->separator, fh);
1428 if(NULL != fields->field_values[i]) {
1429 if(fields->quote != '\0') {
1430 fputc(fields->quote, fh);
1432 fputs(fields->field_values[i], fh);
1433 if(fields->quote != '\0') {
1434 fputc(fields->quote, fh);
1440 void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
1445 /* Returns an ep_alloced string or a static constant*/
1446 static const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
1448 if (fi->hfinfo->id == hf_text_only) {
1452 return fi->rep->representation;
1455 return get_field_hex_value(edt->pi.data_src, fi);
1458 else if (fi->hfinfo->id == proto_data) {
1459 /* Uninterpreted data, i.e., the "Data" protocol, is
1460 * printed as a field instead of a protocol. */
1461 return get_field_hex_value(edt->pi.data_src, fi);
1464 /* Normal protocols and fields */
1465 gchar *dfilter_string;
1468 switch (fi->hfinfo->type)
1471 /* Print out the full details for the protocol. */
1473 return fi->rep->representation;
1475 /* Just print out the protocol abbreviation */
1476 return fi->hfinfo->abbrev;;
1479 /* Return "1" so that the presence of a field of type
1480 * FT_NONE can be checked when using -T fields */
1483 /* XXX - this is a hack until we can just call
1484 * fvalue_to_string_repr() for *all* FT_* types. */
1485 dfilter_string = proto_construct_match_selected_string(fi,
1487 if (dfilter_string != NULL) {
1488 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
1490 /* XXX - Remove double-quotes. Again, once we
1491 * can call fvalue_to_string_repr(), we can
1492 * ask it not to produce the version for
1493 * display-filters, and thus, no
1495 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
1496 dfilter_string[strlen(dfilter_string)-1] = '\0';
1500 return &(dfilter_string[chop_len]);
1502 return get_field_hex_value(edt->pi.data_src, fi);
1509 get_field_hex_value(GSList* src_list, field_info *fi)
1513 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
1514 return "field length invalid!";
1517 /* Find the data for this field. */
1518 pd = get_field_data(src_list, fi);
1525 const int chars_per_byte = 2;
1527 len = chars_per_byte * fi->length;
1528 buffer = ep_alloc_array(gchar, len + 1);
1529 buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
1531 /* Print a simple hex dump */
1532 for (i = 0 ; i < fi->length; i++) {
1533 g_snprintf(p, len, "%02x", pd[i]);
1534 p += chars_per_byte;
1535 len -= chars_per_byte;