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 const gchar* get_field_hex_value(GSList* src_list, field_info *fi);
87 static void proto_tree_print_node(proto_node *node, gpointer data);
88 static void proto_tree_write_node_pdml(proto_node *node, gpointer data);
89 static const guint8 *get_field_data(GSList *src_list, field_info *fi);
90 static void write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi);
91 static gboolean print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
92 guint length, packet_char_enc encoding);
93 static void ps_clean_string(unsigned char *out, const unsigned char *in,
95 static void print_escaped_xml(FILE *fh, const char *unescaped_string);
97 static void print_pdml_geninfo(proto_tree *tree, FILE *fh);
99 static void proto_tree_get_node_field_values(proto_node *node, gpointer data);
102 open_print_dest(int to_file, const char *dest)
106 /* Open the file or command for output */
108 fh = ws_fopen(dest, "w");
110 fh = popen(dest, "w");
116 close_print_dest(int to_file, FILE *fh)
118 /* Close the file or command */
120 return (fclose(fh) == 0);
122 return (pclose(fh) == 0);
125 #define MAX_PS_LINE_LENGTH 256
128 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
129 print_stream_t *stream)
133 /* Create the output */
135 data.stream = stream;
137 data.src_list = edt->pi.data_src;
138 data.encoding = edt->pi.fd->flags.encoding;
139 data.print_dissections = print_args->print_dissections;
140 /* If we're printing the entire packet in hex, don't
141 print uninterpreted data fields in hex as well. */
142 data.print_hex_for_data = !print_args->print_hex;
145 proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
149 #define MAX_INDENT 160
151 /* Print a tree's data, and any child nodes. */
153 void proto_tree_print_node(proto_node *node, gpointer data)
155 field_info *fi = PNODE_FINFO(node);
156 print_data *pdata = (print_data*) data;
158 gchar label_str[ITEM_LABEL_LENGTH];
161 g_assert(fi && "dissection with an invisible proto tree?");
163 /* Don't print invisible entries. */
164 if (PROTO_ITEM_IS_HIDDEN(node))
167 /* Give up if we've already gotten an error. */
171 /* was a free format label produced? */
173 label_ptr = fi->rep->representation;
175 else { /* no, make a generic label */
176 label_ptr = label_str;
177 proto_item_fill_label(fi, label_str);
180 if (PROTO_ITEM_IS_GENERATED(node)) {
181 label_ptr = g_strdup_printf("[%s]", label_ptr);
184 if (!print_line(pdata->stream, pdata->level, label_ptr)) {
185 pdata->success = FALSE;
189 if (PROTO_ITEM_IS_GENERATED(node)) {
193 /* If it's uninterpreted data, dump it (unless our caller will
194 be printing the entire packet in hex). */
195 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
197 * Find the data for this field.
199 pd = get_field_data(pdata->src_list, fi);
201 if (!print_hex_data_buffer(pdata->stream, pd,
202 fi->length, pdata->encoding)) {
203 pdata->success = FALSE;
209 /* If we're printing all levels, or if this node is one with a
210 subtree and its subtree is expanded, recurse into the subtree,
212 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
213 if (pdata->print_dissections == print_dissections_expanded ||
214 (pdata->print_dissections == print_dissections_as_displayed &&
215 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
216 if (node->first_child != NULL) {
218 proto_tree_children_foreach(node,
219 proto_tree_print_node, pdata);
228 write_pdml_preamble(FILE *fh)
230 fputs("<?xml version=\"1.0\"?>\n", fh);
231 fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
232 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
236 proto_tree_write_pdml(epan_dissect_t *edt, FILE *fh)
238 write_pdml_data data;
240 /* Create the output */
243 data.src_list = edt->pi.data_src;
246 /* We shouldn't be called with a NULL pointer here because we've
247 * created a visible protocol tree */
248 g_assert(data.src_list);
250 fprintf(fh, "<packet>\n");
252 /* Print a "geninfo" protocol as required by PDML */
253 print_pdml_geninfo(edt->tree, fh);
255 proto_tree_children_foreach(edt->tree, proto_tree_write_node_pdml,
258 fprintf(fh, "</packet>\n\n");
261 /* Write out a tree's data, and any child nodes, as PDML */
263 proto_tree_write_node_pdml(proto_node *node, gpointer data)
265 field_info *fi = PNODE_FINFO(node);
266 write_pdml_data *pdata = (write_pdml_data*) data;
267 const gchar *label_ptr;
268 gchar label_str[ITEM_LABEL_LENGTH];
269 char *dfilter_string;
272 gboolean wrap_in_fake_protocol;
274 g_assert(fi && "dissection with an invisible proto tree?");
276 /* Will wrap up top-level field items inside a fake protocol wrapper to
277 preserve the PDML schema */
278 wrap_in_fake_protocol =
279 (((fi->hfinfo->type != FT_PROTOCOL) ||
280 (fi->hfinfo->id == proto_data)) &&
281 (pdata->level == 0));
283 /* Indent to the correct level */
284 for (i = -1; i < pdata->level; i++) {
285 fputs(" ", pdata->fh);
288 if (wrap_in_fake_protocol) {
289 /* Open fake protocol wrapper */
290 fputs("<proto name=\"fake-field-wrapper\">\n", pdata->fh);
292 /* Indent to increased level before writint out field */
294 for (i = -1; i < pdata->level; i++) {
295 fputs(" ", pdata->fh);
299 /* Text label. It's printed as a field with no name. */
300 if (fi->hfinfo->id == hf_text_only) {
303 label_ptr = fi->rep->representation;
309 /* Show empty name since it is a required field */
310 fputs("<field name=\"", pdata->fh);
311 fputs("\" show=\"", pdata->fh);
312 print_escaped_xml(pdata->fh, label_ptr);
314 fprintf(pdata->fh, "\" size=\"%d", fi->length);
315 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
317 fputs("\" value=\"", pdata->fh);
318 write_pdml_field_hex_value(pdata, fi);
320 if (node->first_child != NULL) {
321 fputs("\">\n", pdata->fh);
324 fputs("\"/>\n", pdata->fh);
328 /* Uninterpreted data, i.e., the "Data" protocol, is
329 * printed as a field instead of a protocol. */
330 else if (fi->hfinfo->id == proto_data) {
332 /* Write out field with data */
333 fputs("<field name=\"data\" value=\"", pdata->fh);
334 write_pdml_field_hex_value(pdata, fi);
335 fputs("\"/>\n", pdata->fh);
337 /* Normal protocols and fields */
339 if (fi->hfinfo->type == FT_PROTOCOL) {
340 fputs("<proto name=\"", pdata->fh);
343 fputs("<field name=\"", pdata->fh);
345 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
349 * http://www.nbee.org/doku.php?id=netpdl:pdml_specification
351 * the show fields contains things in 'human readable' format
352 * showname: contains only the name of the field
353 * show: contains only the data of the field
354 * showdtl: contains additional details of the field data
355 * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
357 * XXX - the showname shouldn't contain the field data itself
358 * (like it's contained in the fi->rep->representation).
359 * Unfortunately, we don't have the field data representation for
360 * all fields, so this isn't currently possible */
361 fputs("\" showname=\"", pdata->fh);
362 print_escaped_xml(pdata->fh, fi->hfinfo->name);
366 fputs("\" showname=\"", pdata->fh);
367 print_escaped_xml(pdata->fh, fi->rep->representation);
370 label_ptr = label_str;
371 proto_item_fill_label(fi, label_str);
372 fputs("\" showname=\"", pdata->fh);
373 print_escaped_xml(pdata->fh, label_ptr);
376 if (PROTO_ITEM_IS_HIDDEN(node))
377 fprintf(pdata->fh, "\" hide=\"yes");
379 fprintf(pdata->fh, "\" size=\"%d", fi->length);
380 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
381 /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
383 /* show, value, and unmaskedvalue attributes */
384 switch (fi->hfinfo->type)
389 fputs("\" show=\"\" value=\"", pdata->fh);
392 /* XXX - this is a hack until we can just call
393 * fvalue_to_string_repr() for *all* FT_* types. */
394 dfilter_string = proto_construct_match_selected_string(fi,
396 if (dfilter_string != NULL) {
397 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
399 /* XXX - Remove double-quotes. Again, once we
400 * can call fvalue_to_string_repr(), we can
401 * ask it not to produce the version for
402 * display-filters, and thus, no
404 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
405 dfilter_string[strlen(dfilter_string)-1] = '\0';
409 fputs("\" show=\"", pdata->fh);
410 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
414 * XXX - should we omit "value" for any fields?
415 * What should we do for fields whose length is 0?
416 * They might come from a pseudo-header or from
417 * the capture header (e.g., time stamps), or
418 * they might be generated fields.
420 if (fi->length > 0) {
421 fputs("\" value=\"", pdata->fh);
423 if (fi->hfinfo->bitmask!=0) {
424 fprintf(pdata->fh, "%X", fvalue_get_uinteger(&fi->value));
425 fputs("\" unmaskedvalue=\"", pdata->fh);
426 write_pdml_field_hex_value(pdata, fi);
429 write_pdml_field_hex_value(pdata, fi);
434 if (node->first_child != NULL) {
435 fputs("\">\n", pdata->fh);
437 else if (fi->hfinfo->id == proto_data) {
438 fputs("\">\n", pdata->fh);
441 fputs("\"/>\n", pdata->fh);
445 /* We always print all levels for PDML. Recurse here. */
446 if (node->first_child != NULL) {
448 proto_tree_children_foreach(node,
449 proto_tree_write_node_pdml, pdata);
453 /* Take back the extra level we added for fake wrapper protocol */
454 if (wrap_in_fake_protocol) {
458 if (node->first_child != NULL) {
459 /* Indent to correct level */
460 for (i = -1; i < pdata->level; i++) {
461 fputs(" ", pdata->fh);
463 /* Close off current element */
464 if (fi->hfinfo->id != proto_data) { /* Data protocol uses simple tags */
465 if (fi->hfinfo->type == FT_PROTOCOL) {
466 fputs("</proto>\n", pdata->fh);
469 fputs("</field>\n", pdata->fh);
474 /* Close off fake wrapper protocol */
475 if (wrap_in_fake_protocol) {
476 fputs("</proto>\n", pdata->fh);
480 /* Print info for a 'geninfo' pseudo-protocol. This is required by
481 * the PDML spec. The information is contained in Wireshark's 'frame' protocol,
482 * but we produce a 'geninfo' protocol in the PDML to conform to spec.
483 * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
485 print_pdml_geninfo(proto_tree *tree, FILE *fh)
487 guint32 num, len, caplen;
489 GPtrArray *finfo_array;
490 field_info *frame_finfo;
492 /* Get frame protocol's finfo. */
493 finfo_array = proto_find_finfo(tree, proto_frame);
494 if (g_ptr_array_len(finfo_array) < 1) {
497 frame_finfo = (field_info *)finfo_array->pdata[0];
498 g_ptr_array_free(finfo_array, TRUE);
500 /* frame.number --> geninfo.num */
501 finfo_array = proto_find_finfo(tree, hf_frame_number);
502 if (g_ptr_array_len(finfo_array) < 1) {
505 num = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
506 g_ptr_array_free(finfo_array, TRUE);
508 /* frame.frame_len --> geninfo.len */
509 finfo_array = proto_find_finfo(tree, hf_frame_len);
510 if (g_ptr_array_len(finfo_array) < 1) {
513 len = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
514 g_ptr_array_free(finfo_array, TRUE);
516 /* frame.cap_len --> geninfo.caplen */
517 finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
518 if (g_ptr_array_len(finfo_array) < 1) {
521 caplen = fvalue_get_uinteger(&((field_info*)finfo_array->pdata[0])->value);
522 g_ptr_array_free(finfo_array, TRUE);
524 /* frame.time --> geninfo.timestamp */
525 finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
526 if (g_ptr_array_len(finfo_array) < 1) {
529 timestamp = (nstime_t *)fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
530 g_ptr_array_free(finfo_array, TRUE);
532 /* Print geninfo start */
534 " <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
535 frame_finfo->length);
537 /* Print geninfo.num */
539 " <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
540 num, num, frame_finfo->length);
542 /* Print geninfo.len */
544 " <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Frame Length\" value=\"%x\" size=\"%u\"/>\n",
545 len, len, frame_finfo->length);
547 /* Print geninfo.caplen */
549 " <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
550 caplen, caplen, frame_finfo->length);
552 /* Print geninfo.timestamp */
554 " <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
555 abs_time_to_str(timestamp, ABSOLUTE_TIME_LOCAL, TRUE), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
557 /* Print geninfo end */
563 write_pdml_finale(FILE *fh)
565 fputs("</pdml>\n", fh);
569 write_psml_preamble(FILE *fh)
571 fputs("<?xml version=\"1.0\"?>\n", fh);
572 fputs("<psml version=\"" PSML_VERSION "\" ", fh);
573 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
577 proto_tree_write_psml(epan_dissect_t *edt, FILE *fh)
580 static gboolean structure_written = FALSE;
582 /* if this is the first packet, we have to create the PSML structure output */
583 if(!structure_written) {
584 fprintf(fh, "<structure>\n");
586 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
587 fprintf(fh, "<section>");
588 print_escaped_xml(fh, edt->pi.cinfo->col_title[i]);
589 fprintf(fh, "</section>\n");
592 fprintf(fh, "</structure>\n\n");
594 structure_written = TRUE;
597 fprintf(fh, "<packet>\n");
599 for(i=0; i < edt->pi.cinfo->num_cols; i++) {
600 fprintf(fh, "<section>");
601 print_escaped_xml(fh, edt->pi.cinfo->col_data[i]);
602 fprintf(fh, "</section>\n");
605 fprintf(fh, "</packet>\n\n");
609 write_psml_finale(FILE *fh)
611 fputs("</psml>\n", fh);
615 write_csv_preamble(FILE *fh _U_)
621 proto_tree_write_csv(epan_dissect_t *edt, FILE *fh)
625 /* if this is the first packet, we have to write the CSV header */
626 if(edt->pi.fd->num == 1) {
627 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
628 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_title[i]);
630 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_title[i]);
633 for(i=0; i < edt->pi.cinfo->num_cols - 1; i++)
634 fprintf(fh, "\"%s\",", edt->pi.cinfo->col_data[i]);
636 fprintf(fh, "\"%s\"\n", edt->pi.cinfo->col_data[i]);
640 write_csv_finale(FILE *fh _U_)
646 write_carrays_preamble(FILE *fh _U_)
652 proto_tree_write_carrays(const guint8 *pd, guint32 len, guint32 num, FILE *fh)
659 fprintf(fh, "char pkt%u[] = {\n", num);
661 for (i = 0; i < len; i++) {
663 fprintf(fh, "0x%02x", *(pd + i));
665 if (i == (len - 1)) {
666 fprintf(fh, " };\n\n");
670 if (!((i + 1) % 8)) {
679 write_carrays_finale(FILE *fh _U_)
685 * Find the data source for a specified field, and return a pointer
686 * to the data in it. Returns NULL if the data is out of bounds.
688 static const guint8 *
689 get_field_data(GSList *src_list, field_info *fi)
694 gint length, tvbuff_length;
696 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
697 src = (data_source *)src_le->data;
699 if (fi->ds_tvb == src_tvb) {
703 * XXX - a field can have a length that runs past
704 * the end of the tvbuff. Ideally, that should
705 * be fixed when adding an item to the protocol
706 * tree, but checking the length when doing
707 * that could be expensive. Until we fix that,
708 * we'll do the check here.
710 tvbuff_length = tvb_length_remaining(src_tvb,
712 if (tvbuff_length < 0) {
716 if (length > tvbuff_length)
717 length = tvbuff_length;
718 return tvb_get_ptr(src_tvb, fi->start, length);
721 g_assert_not_reached();
722 return NULL; /* not found */
725 /* Print a string, escaping out certain characters that need to
726 * escaped out for XML. */
728 print_escaped_xml(FILE *fh, const char *unescaped_string)
733 for (p = unescaped_string; *p != '\0'; p++) {
751 if (g_ascii_isprint(*p))
754 g_snprintf(temp_str, sizeof(temp_str), "\\x%x", (guint8)*p);
762 write_pdml_field_hex_value(write_pdml_data *pdata, field_info *fi)
770 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
771 fprintf(pdata->fh, "field length invalid!");
775 /* Find the data for this field. */
776 pd = get_field_data(pdata->src_list, fi);
779 /* Print a simple hex dump */
780 for (i = 0 ; i < fi->length; i++) {
781 fprintf(pdata->fh, "%02x", pd[i]);
787 print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
789 gboolean multiple_sources;
798 /* We shouldn't be called with a NULL pointer here because we've
799 * created a visible protocol tree */
800 g_assert(edt->pi.data_src);
803 * Set "multiple_sources" iff this frame has more than one
804 * data source; if it does, we need to print the name of
805 * the data source before printing the data from the
808 multiple_sources = (edt->pi.data_src->next != NULL);
810 for (src_le = edt->pi.data_src; src_le != NULL;
811 src_le = src_le->next) {
812 src = (data_source *)src_le->data;
814 if (multiple_sources) {
815 name = get_data_source_name(src);
816 print_line(stream, 0, "");
817 line = g_strdup_printf("%s:", name);
818 print_line(stream, 0, line);
821 length = tvb_length(tvb);
824 cp = tvb_get_ptr(tvb, 0, length);
825 if (!print_hex_data_buffer(stream, cp, length,
826 edt->pi.fd->flags.encoding))
833 * This routine is based on a routine created by Dan Lasley
834 * <DLASLEY@PROMUS.com>.
836 * It was modified for Wireshark by Gilbert Ramirez and others.
839 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
840 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
841 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
842 /* max number of characters hex dump takes -
843 2 digits plus trailing blank */
844 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
845 /* number of characters those bytes take;
846 3 characters per byte of hex dump,
847 2 blanks separating hex from ASCII,
848 1 character per byte of ASCII dump */
849 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
850 /* number of characters per line;
851 offset, 2 blanks separating offset
852 from data dump, data dump */
855 print_hex_data_buffer(print_stream_t *stream, const guchar *cp,
856 guint length, packet_char_enc encoding)
858 register unsigned int ad, i, j, k, l;
860 guchar line[MAX_LINE_LEN + 1];
861 unsigned int use_digits;
862 static guchar binhex[16] = {
863 '0', '1', '2', '3', '4', '5', '6', '7',
864 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
866 if (!print_line(stream, 0, ""))
870 * How many of the leading digits of the offset will we supply?
871 * We always supply at least 4 digits, but if the maximum offset
872 * won't fit in 4 digits, we use as many digits as will be needed.
874 if (((length - 1) & 0xF0000000) != 0)
875 use_digits = 8; /* need all 8 digits */
876 else if (((length - 1) & 0x0F000000) != 0)
877 use_digits = 7; /* need 7 digits */
878 else if (((length - 1) & 0x00F00000) != 0)
879 use_digits = 6; /* need 6 digits */
880 else if (((length - 1) & 0x000F0000) != 0)
881 use_digits = 5; /* need 5 digits */
883 use_digits = 4; /* we'll supply 4 digits */
892 * Start of a new line.
898 c = (ad >> (l*4)) & 0xF;
899 line[j++] = binhex[c];
903 memset(line+j, ' ', DATA_DUMP_LEN);
906 * Offset in line of ASCII dump.
908 k = j + HEX_DUMP_LEN + 2;
911 line[j++] = binhex[c>>4];
912 line[j++] = binhex[c&0xf];
914 if (encoding == PACKET_CHAR_ENC_CHAR_EBCDIC) {
915 c = EBCDIC_to_ASCII1(c);
917 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
919 if ((i & 15) == 0 || i == length) {
921 * We'll be starting a new line, or
922 * we're finished printing this buffer;
923 * dump out the line we've constructed,
924 * and advance the offset.
927 if (!print_line(stream, 0, line))
936 void ps_clean_string(unsigned char *out, const unsigned char *in,
947 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
968 /* Some formats need stuff at the beginning of the output */
970 print_preamble(print_stream_t *self, gchar *filename)
972 return (self->ops->print_preamble)(self, filename);
976 print_line(print_stream_t *self, int indent, const char *line)
978 return (self->ops->print_line)(self, indent, line);
981 /* Insert bookmark */
983 print_bookmark(print_stream_t *self, const gchar *name, const gchar *title)
985 return (self->ops->print_bookmark)(self, name, title);
989 new_page(print_stream_t *self)
991 return (self->ops->new_page)(self);
994 /* Some formats need stuff at the end of the output */
996 print_finale(print_stream_t *self)
998 return (self->ops->print_finale)(self);
1002 destroy_print_stream(print_stream_t *self)
1004 return (self->ops->destroy)(self);
1013 print_preamble_text(print_stream_t *self _U_, gchar *filename _U_)
1016 return TRUE; /* always succeeds */
1020 print_line_text(print_stream_t *self, int indent, const char *line)
1022 output_text *output = (output_text *)self->data;
1023 char space[MAX_INDENT+1];
1027 /* Prepare the tabs for printing, depending on tree level */
1028 num_spaces = indent * 4;
1029 if (num_spaces > MAX_INDENT) {
1030 num_spaces = MAX_INDENT;
1032 for (i = 0; i < num_spaces; i++) {
1035 /* The string is NUL-terminated */
1036 space[num_spaces] = '\0';
1038 fputs(space, output->fh);
1039 fputs(line, output->fh);
1040 putc('\n', output->fh);
1041 return !ferror(output->fh);
1045 print_bookmark_text(print_stream_t *self _U_, const gchar *name _U_,
1046 const gchar *title _U_)
1053 new_page_text(print_stream_t *self)
1055 output_text *output = (output_text *)self->data;
1057 fputs("\f", output->fh);
1058 return !ferror(output->fh);
1062 print_finale_text(print_stream_t *self _U_)
1065 return TRUE; /* always succeeds */
1069 destroy_text(print_stream_t *self)
1071 output_text *output = (output_text *)self->data;
1074 ret = close_print_dest(output->to_file, output->fh);
1080 static const print_stream_ops_t print_text_ops = {
1081 print_preamble_text,
1083 print_bookmark_text,
1089 static print_stream_t *
1090 print_stream_text_alloc(int to_file, FILE *fh)
1092 print_stream_t *stream;
1093 output_text *output;
1095 output = (output_text *)g_malloc(sizeof *output);
1096 output->to_file = to_file;
1098 stream = (print_stream_t *)g_malloc(sizeof (print_stream_t));
1099 stream->ops = &print_text_ops;
1100 stream->data = output;
1106 print_stream_text_new(int to_file, const char *dest)
1110 fh = open_print_dest(to_file, dest);
1114 return print_stream_text_alloc(to_file, fh);
1118 print_stream_text_stdio_new(FILE *fh)
1120 return print_stream_text_alloc(TRUE, fh);
1129 print_preamble_ps(print_stream_t *self, gchar *filename)
1131 output_ps *output = (output_ps *)self->data;
1132 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1134 print_ps_preamble(output->fh);
1136 fputs("%% the page title\n", output->fh);
1137 ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
1138 fprintf(output->fh, "/ws_pagetitle (%s - Wireshark " VERSION "%s) def\n", psbuffer, wireshark_svnversion);
1139 fputs("\n", output->fh);
1140 return !ferror(output->fh);
1144 print_line_ps(print_stream_t *self, int indent, const char *line)
1146 output_ps *output = (output_ps *)self->data;
1147 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1149 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
1150 fprintf(output->fh, "%d (%s) putline\n", indent, psbuffer);
1151 return !ferror(output->fh);
1155 print_bookmark_ps(print_stream_t *self, const gchar *name, const gchar *title)
1157 output_ps *output = (output_ps *)self->data;
1158 unsigned char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
1161 * See the Adobe "pdfmark reference":
1163 * http://partners.adobe.com/asn/acrobat/docs/pdfmark.pdf
1165 * The pdfmark stuff tells code that turns PostScript into PDF
1166 * things that it should do.
1168 * The /OUT stuff creates a bookmark that goes to the
1169 * destination with "name" as the name and "title" as the title.
1171 * The "/DEST" creates the destination.
1173 ps_clean_string(psbuffer, title, MAX_PS_LINE_LENGTH);
1174 fprintf(output->fh, "[/Dest /%s /Title (%s) /OUT pdfmark\n", name,
1176 fputs("[/View [/XYZ -4 currentpoint matrix currentmatrix matrix defaultmatrix\n",
1178 fputs("matrix invertmatrix matrix concatmatrix transform exch pop 20 add null]\n",
1180 fprintf(output->fh, "/Dest /%s /DEST pdfmark\n", name);
1181 return !ferror(output->fh);
1185 new_page_ps(print_stream_t *self)
1187 output_ps *output = (output_ps *)self->data;
1189 fputs("formfeed\n", output->fh);
1190 return !ferror(output->fh);
1194 print_finale_ps(print_stream_t *self)
1196 output_ps *output = (output_ps *)self->data;
1198 print_ps_finale(output->fh);
1199 return !ferror(output->fh);
1203 destroy_ps(print_stream_t *self)
1205 output_ps *output = (output_ps *)self->data;
1208 ret = close_print_dest(output->to_file, output->fh);
1214 static const print_stream_ops_t print_ps_ops = {
1223 static print_stream_t *
1224 print_stream_ps_alloc(int to_file, FILE *fh)
1226 print_stream_t *stream;
1229 output = (output_ps *)g_malloc(sizeof *output);
1230 output->to_file = to_file;
1232 stream = (print_stream_t *)g_malloc(sizeof (print_stream_t));
1233 stream->ops = &print_ps_ops;
1234 stream->data = output;
1240 print_stream_ps_new(int to_file, const char *dest)
1244 fh = open_print_dest(to_file, dest);
1248 return print_stream_ps_alloc(to_file, fh);
1252 print_stream_ps_stdio_new(FILE *fh)
1254 return print_stream_ps_alloc(TRUE, fh);
1257 output_fields_t* output_fields_new()
1259 output_fields_t* fields = g_new(output_fields_t, 1);
1260 fields->print_header = FALSE;
1261 fields->separator = '\t';
1262 fields->occurrence = 'a';
1263 fields->aggregator = ',';
1264 fields->fields = NULL; /*Do lazy initialisation */
1265 fields->field_indicies = NULL;
1266 fields->field_values = NULL;
1271 gsize output_fields_num_fields(output_fields_t* fields)
1275 if(NULL == fields->fields) {
1278 return fields->fields->len;
1282 void output_fields_free(output_fields_t* fields)
1286 if(NULL != fields->field_indicies) {
1287 /* Keys are stored in fields->fields, values are
1290 g_hash_table_destroy(fields->field_indicies);
1292 if(NULL != fields->fields) {
1294 for(i = 0; i < fields->fields->len; ++i) {
1295 gchar* field = (gchar *)g_ptr_array_index(fields->fields,i);
1298 g_ptr_array_free(fields->fields, TRUE);
1304 void output_fields_add(output_fields_t* fields, const gchar* field)
1312 if(NULL == fields->fields) {
1313 fields->fields = g_ptr_array_new();
1316 field_copy = g_strdup(field);
1318 g_ptr_array_add(fields->fields, field_copy);
1321 gboolean output_fields_set_option(output_fields_t* info, gchar* option)
1323 const gchar* option_name;
1324 const gchar* option_value;
1329 if('\0' == *option) {
1330 return FALSE; /* Is this guarded against by option parsing? */
1332 option_name = strtok(option,"=");
1333 option_value = option + strlen(option_name) + 1;
1334 if(0 == strcmp(option_name, "header")) {
1335 switch(NULL == option_value ? '\0' : *option_value) {
1337 info->print_header = FALSE;
1340 info->print_header = TRUE;
1348 if(0 == strcmp(option_name,"separator")) {
1349 switch(NULL == option_value ? '\0' : *option_value) {
1353 switch(*++option_value) {
1355 info->separator = '\t';
1358 info->separator = ' ';
1361 info->separator = '\\';
1365 info->separator = *option_value;
1371 if(0 == strcmp(option_name, "occurrence")) {
1372 switch(NULL == option_value ? '\0' : *option_value) {
1376 info->occurrence = *option_value;
1384 if(0 == strcmp(option_name,"aggregator")) {
1385 switch(NULL == option_value ? '\0' : *option_value) {
1389 switch(*++option_value) {
1391 info->aggregator = ' ';
1394 info->aggregator = '\\';
1398 info->aggregator = *option_value;
1404 if(0 == strcmp(option_name, "quote")) {
1405 switch(NULL == option_value ? '\0' : *option_value) {
1406 default: /* Fall through */
1426 void output_fields_list_options(FILE *fh)
1428 fprintf(fh, "TShark: The available options for field output \"E\" are:\n");
1429 fputs("header=y|n Print field abbreviations as first line of output (def: N: no)\n", fh);
1430 fputs("separator=/t|/s|<character> Set the separator to use;\n \"/t\" = tab, \"/s\" = space (def: /t: tab)\n", fh);
1431 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);
1432 fputs("aggregator=,|/s|<character> Set the aggregator to use;\n \",\" = comma, \"/s\" = space (def: ,: comma)\n", fh);
1433 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);
1437 void write_fields_preamble(output_fields_t* fields, FILE *fh)
1444 if(!fields->print_header) {
1448 for(i = 0; i < fields->fields->len; ++i) {
1449 const gchar* field = (const gchar *)g_ptr_array_index(fields->fields,i);
1451 fputc(fields->separator, fh);
1458 static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
1460 write_field_data_t *call_data;
1462 gpointer field_index;
1464 call_data = (write_field_data_t *)data;
1465 fi = PNODE_FINFO(node);
1467 g_assert(fi && "dissection with an invisible proto tree?");
1469 field_index = g_hash_table_lookup(call_data->fields->field_indicies, fi->hfinfo->abbrev);
1470 if(NULL != field_index) {
1473 value = get_node_field_value(fi, call_data->edt); /* ep_alloced string */
1475 if(NULL != value && '\0' != *value) {
1477 actual_index = GPOINTER_TO_UINT(field_index);
1478 /* Unwrap change made to disambiguiate zero / null */
1479 if ( call_data->fields->field_values[actual_index - 1] == NULL ) {
1480 call_data->fields->field_values[actual_index - 1] = ep_strbuf_new(value);
1481 } else if ( call_data->fields->occurrence == 'l' ) {
1482 /* print only the value of the last occurrence of the field */
1483 ep_strbuf_printf(call_data->fields->field_values[actual_index - 1],"%s",value);
1484 } else if ( call_data->fields->occurrence == 'a' ) {
1485 /* print the value of all accurrences of the field */
1486 ep_strbuf_append_printf(call_data->fields->field_values[actual_index - 1],
1487 "%c%s",call_data->fields->aggregator,value);
1493 if (node->first_child != NULL) {
1494 proto_tree_children_foreach(node, proto_tree_get_node_field_values,
1499 void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, FILE *fh)
1503 write_field_data_t data;
1509 data.fields = fields;
1512 if(NULL == fields->field_indicies) {
1513 /* Prepare a lookup table from string abbreviation for field to its index. */
1514 fields->field_indicies = g_hash_table_new(g_str_hash, g_str_equal);
1517 while( i < fields->fields->len) {
1518 gchar* field = (gchar *)g_ptr_array_index(fields->fields, i);
1519 /* Store field indicies +1 so that zero is not a valid value,
1520 * and can be distinguished from NULL as a pointer.
1523 g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
1527 /* Buffer to store values for this packet */
1528 fields->field_values = ep_alloc_array0(emem_strbuf_t*, fields->fields->len);
1530 proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
1533 for(i = 0; i < fields->fields->len; ++i) {
1535 fputc(fields->separator, fh);
1537 if(NULL != fields->field_values[i]) {
1538 if(fields->quote != '\0') {
1539 fputc(fields->quote, fh);
1541 fputs(fields->field_values[i]->str, fh);
1542 if(fields->quote != '\0') {
1543 fputc(fields->quote, fh);
1549 void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
1554 /* Returns an ep_alloced string or a static constant*/
1555 const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
1557 if (fi->hfinfo->id == hf_text_only) {
1561 return fi->rep->representation;
1564 return get_field_hex_value(edt->pi.data_src, fi);
1567 else if (fi->hfinfo->id == proto_data) {
1568 /* Uninterpreted data, i.e., the "Data" protocol, is
1569 * printed as a field instead of a protocol. */
1570 return get_field_hex_value(edt->pi.data_src, fi);
1573 /* Normal protocols and fields */
1574 gchar *dfilter_string;
1577 switch (fi->hfinfo->type)
1580 /* Print out the full details for the protocol. */
1582 return fi->rep->representation;
1584 /* Just print out the protocol abbreviation */
1585 return fi->hfinfo->abbrev;;
1588 /* Return "1" so that the presence of a field of type
1589 * FT_NONE can be checked when using -T fields */
1592 /* XXX - this is a hack until we can just call
1593 * fvalue_to_string_repr() for *all* FT_* types. */
1594 dfilter_string = proto_construct_match_selected_string(fi,
1596 if (dfilter_string != NULL) {
1597 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
1599 /* XXX - Remove double-quotes. Again, once we
1600 * can call fvalue_to_string_repr(), we can
1601 * ask it not to produce the version for
1602 * display-filters, and thus, no
1604 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
1605 dfilter_string[strlen(dfilter_string)-1] = '\0';
1609 return &(dfilter_string[chop_len]);
1611 return get_field_hex_value(edt->pi.data_src, fi);
1618 get_field_hex_value(GSList* src_list, field_info *fi)
1625 if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
1626 return "field length invalid!";
1629 /* Find the data for this field. */
1630 pd = get_field_data(src_list, fi);
1637 const int chars_per_byte = 2;
1639 len = chars_per_byte * fi->length;
1640 buffer = ep_alloc_array(gchar, len + 1);
1641 buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
1643 /* Print a simple hex dump */
1644 for (i = 0 ; i < fi->length; i++) {
1645 g_snprintf(p, chars_per_byte+1, "%02x", pd[i]);
1646 p += chars_per_byte;