do not call proto_tree_add_...() inside PROTO_ITEM_SET_HIDDEN() macro
[metze/wireshark/wip.git] / print.c
diff --git a/print.c b/print.c
index 9b39dd07e99e595b784655e242849f0cd8ecaeb1..bdd72a2609bb9921ed143400b2eddf193bef5754 100644 (file)
--- a/print.c
+++ b/print.c
@@ -40,7 +40,8 @@
 #include "packet-range.h"
 #include "print.h"
 #include "ps.h"
-#include "file_util.h"
+#include "version_info.h"
+#include <wsutil/file_util.h>
 #include <epan/charsets.h>
 #include <epan/dissectors/packet-data.h>
 #include <epan/dissectors/packet-frame.h>
@@ -103,7 +104,7 @@ open_print_dest(int to_file, const char *dest)
 
        /* Open the file or command for output */
        if (to_file)
-               fh = eth_fopen(dest, "w");
+               fh = ws_fopen(dest, "w");
        else
                fh = popen(dest, "w");
 
@@ -262,10 +263,29 @@ proto_tree_write_node_pdml(proto_node *node, gpointer data)
        int             chop_len;
        int             i;
 
+       /* Will wrap up top-level field items inside a fake protocol wrapper to
+          preserve the PDML schema */
+       gboolean wrap_in_fake_protocol =
+           (((fi->hfinfo->type != FT_PROTOCOL) ||
+            (fi->hfinfo->id == proto_data)) &&
+           (pdata->level == 0));
+
+       /* Indent to the correct level */
        for (i = -1; i < pdata->level; i++) {
                fputs("  ", pdata->fh);
        }
 
+       if (wrap_in_fake_protocol) {
+               /* Open fake protocol wrapper */
+               fputs("<proto name=\"fake-field-wrapper\">\n", pdata->fh);
+
+               /* Indent to increased level before writint out field */
+               pdata->level++;
+               for (i = -1; i < pdata->level; i++) {
+                       fputs("  ", pdata->fh);
+               }
+       }
+
        /* Text label. It's printed as a field with no name. */
        if (fi->hfinfo->id == hf_text_only) {
                /* Get the text */
@@ -294,16 +314,15 @@ proto_tree_write_node_pdml(proto_node *node, gpointer data)
                        fputs("\"/>\n", pdata->fh);
                }
        }
+
        /* Uninterpreted data, i.e., the "Data" protocol, is
         * printed as a field instead of a protocol. */
        else if (fi->hfinfo->id == proto_data) {
 
+               /* Write out field with data */
                fputs("<field name=\"data\" value=\"", pdata->fh);
-
                write_pdml_field_hex_value(pdata, fi);
-
                fputs("\"/>\n", pdata->fh);
-
        }
        /* Normal protocols and fields */
        else {
@@ -316,7 +335,7 @@ proto_tree_write_node_pdml(proto_node *node, gpointer data)
                print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
 
 #if 0
-       /* PDML spec, see: 
+       /* PDML spec, see:
         * http://analyzer.polito.it/30alpha/docs/dissectors/PDMLSpec.htm
         *
         * the show fields contains things in 'human readable' format
@@ -325,9 +344,9 @@ proto_tree_write_node_pdml(proto_node *node, gpointer data)
         * showdtl: contains additional details of the field data
         * showmap: contains mappings of the field data (e.g. the hostname to an IP address)
         *
-        * XXX - the showname shouldn't contain the field data itself 
-        * (like it's contained in the fi->rep->representation). 
-        * Unfortunately, we don't have the field data representation for 
+        * XXX - the showname shouldn't contain the field data itself
+        * (like it's contained in the fi->rep->representation).
+        * Unfortunately, we don't have the field data representation for
         * all fields, so this isn't currently possible */
                fputs("\" showname=\"", pdata->fh);
                print_escaped_xml(pdata->fh, fi->hfinfo->name);
@@ -421,17 +440,31 @@ proto_tree_write_node_pdml(proto_node *node, gpointer data)
                pdata->level--;
        }
 
+       /* Take back the extra level we added for fake wrapper protocol */
+       if (wrap_in_fake_protocol) {
+               pdata->level--;
+       }
+
        if (node->first_child != NULL) {
+               /* Indent to correct level */
                for (i = -1; i < pdata->level; i++) {
                        fputs("  ", pdata->fh);
                }
-               if (fi->hfinfo->type == FT_PROTOCOL) {
-                       fputs("</proto>\n", pdata->fh);
-               }
-               else {
-                       fputs("</field>\n", pdata->fh);
+               /* Close off current element */
+               if (fi->hfinfo->id != proto_data) {   /* Data protocol uses simple tags */
+                       if (fi->hfinfo->type == FT_PROTOCOL) {
+                               fputs("</proto>\n", pdata->fh);
+                       }
+                       else {
+                               fputs("</field>\n", pdata->fh);
+                       }
                }
        }
+
+       /* Close off fake wrapper protocol */
+       if (wrap_in_fake_protocol) {
+               fputs("</proto>\n", pdata->fh);
+       }
 }
 
 /* Print info for a 'geninfo' pseudo-protocol. This is required by
@@ -596,6 +629,45 @@ write_csv_finale(FILE *fh _U_)
 
 }
 
+void
+write_carrays_preamble(FILE *fh _U_)
+{
+
+}
+
+void
+proto_tree_write_carrays(const guint8 *pd, guint32 len, guint32 num, FILE *fh)
+{
+        guint32 i = 0;
+
+       if (!len)
+               return;
+
+       fprintf(fh, "char pkt%u[] = {\n", num);
+
+        for (i = 0; i < len; i++) {
+
+               fprintf(fh, "0x%02x", *(pd + i));
+
+               if (i == (len - 1)) {
+                       fprintf(fh, " };\n\n");
+                       break;
+               }
+
+               if (!((i + 1) % 8)) {
+                       fprintf(fh, ", \n");
+               } else {
+                       fprintf(fh, ", ");
+               }
+       }
+}
+
+void
+write_carrays_finale(FILE *fh _U_)
+{
+
+}
+
 /*
  * Find the data source for a specified field, and return a pointer
  * to the data in it. Returns NULL if the data is out of bounds.
@@ -716,9 +788,7 @@ print_hex_data(print_stream_t *stream, epan_dissect_t *edt)
                if (multiple_sources) {
                        name = src->name;
                        print_line(stream, 0, "");
-                       line = g_malloc(strlen(name) + 2);      /* <name>:\0 */
-                       strcpy(line, name);
-                       strcat(line, ":");
+                       line = g_strdup_printf("%s:", name);
                        print_line(stream, 0, line);
                        g_free(line);
                }
@@ -1036,12 +1106,12 @@ print_preamble_ps(print_stream_t *self, gchar *filename)
 
        print_ps_preamble(output->fh);
 
-       fputs("%% Set the font to 10 point\n", output->fh);
-       fputs("/Courier findfont 10 scalefont setfont\n", output->fh);
+       fputs("%% Set the font to 8 point\n", output->fh);
+       fputs("/Courier findfont 8 scalefont setfont\n", output->fh);
        fputs("\n", output->fh);
        fputs("%% the page title\n", output->fh);
        ps_clean_string(psbuffer, filename, MAX_PS_LINE_LENGTH);
-       fprintf(output->fh, "/eth_pagetitle (%s - Wireshark) def\n", psbuffer);
+       fprintf(output->fh, "/eth_pagetitle (%s - Wireshark " VERSION "%s) def\n", psbuffer, wireshark_svnversion);
        fputs("\n", output->fh);
        return !ferror(output->fh);
 }
@@ -1171,7 +1241,7 @@ output_fields_t* output_fields_new()
     fields->fields = NULL; /*Do lazy initialisation */
     fields->field_indicies = NULL;
     fields->field_values = NULL;
-    fields->quote='\0'; 
+    fields->quote='\0';
     return fields;
 }
 
@@ -1205,7 +1275,7 @@ void output_fields_free(output_fields_t* fields)
         g_ptr_array_free(fields->fields, TRUE);
     }
 
-    g_free(fields);    
+    g_free(fields);
 }
 
 void output_fields_add(output_fields_t* fields, const gchar* field)
@@ -1272,7 +1342,7 @@ gboolean output_fields_set_option(output_fields_t* info, gchar* option)
             info->separator = *option_value;
             break;
         }
-        return TRUE;       
+        return TRUE;
     }
 
     if(0 == strcmp(option_name, "quote")) {
@@ -1291,7 +1361,7 @@ gboolean output_fields_set_option(output_fields_t* info, gchar* option)
             info->quote='\0';
             break;
         }
-        return TRUE;        
+        return TRUE;
     }
 
     return FALSE;
@@ -1323,17 +1393,14 @@ void write_fields_preamble(output_fields_t* fields, FILE *fh)
             fputc(fields->separator, fh);
         }
        fputs(field, fh);
-    }    
+    }
     fputc('\n', fh);
 }
 
-
-
-
-static void proto_tree_get_node_field_values(proto_node *node, gpointer data) 
+static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
 {
     write_field_data_t *call_data;
-       field_info      *fi;
+    field_info *fi;
     gpointer field_index;
 
     call_data = data;
@@ -1352,12 +1419,12 @@ static void proto_tree_get_node_field_values(proto_node *node, gpointer data)
             call_data->fields->field_values[actual_index - 1] = value;
         }
     }
-    
-       /* Recurse here. */
-       if (node->first_child != NULL) {
-               proto_tree_children_foreach(node,
-                               proto_tree_get_node_field_values, call_data);
-       }
+
+    /* Recurse here. */
+    if (node->first_child != NULL) {
+        proto_tree_children_foreach(node, proto_tree_get_node_field_values,
+                                    call_data);
+    }
 }
 
 void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, FILE *fh)
@@ -1380,19 +1447,19 @@ void proto_tree_write_fields(output_fields_t* fields, epan_dissect_t *edt, FILE
         i = 0;
         while( i < fields->fields->len) {
             gchar* field = g_ptr_array_index(fields->fields, i);
-             /* Store field indicies +1 so that zero is not a valid value, 
+             /* Store field indicies +1 so that zero is not a valid value,
               * and can be distinguished from NULL as a pointer.
               */
             ++i;
-            g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));            
+            g_hash_table_insert(fields->field_indicies, field, GUINT_TO_POINTER(i));
         }
     }
 
     /* Buffer to store values for this packet */
     fields->field_values = ep_alloc_array0(const gchar*, fields->fields->len);
-    
-       proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
-           &data);
+
+    proto_tree_children_foreach(edt->tree, proto_tree_get_node_field_values,
+                                &data);
 
     for(i = 0; i < fields->fields->len; ++i) {
         if(0 != i) {
@@ -1418,79 +1485,80 @@ void write_fields_finale(output_fields_t* fields _U_ , FILE *fh _U_)
 /* Returns an ep_alloced string or a static constant*/
 static const gchar* get_node_field_value(field_info* fi, epan_dissect_t* edt)
 {
-       /* Text label. */
-       if (fi->hfinfo->id == hf_text_only) {
-               /* Get the text */
-               if (fi->rep) {
+    if (fi->hfinfo->id == hf_text_only) {
+        /* Text label.
+         * Get the text */
+        if (fi->rep) {
             return fi->rep->representation;
-               }
-               else {
-                       return get_field_hex_value(edt->pi.data_src, fi);
-               }
-       }
-       /* Uninterpreted data, i.e., the "Data" protocol, is
-        * printed as a field instead of a protocol. */
-       else if (fi->hfinfo->id == proto_data) {
+        }
+        else {
+            return get_field_hex_value(edt->pi.data_src, fi);
+        }
+    }
+    else if (fi->hfinfo->id == proto_data) {
+        /* Uninterpreted data, i.e., the "Data" protocol, is
+         * printed as a field instead of a protocol. */
         return get_field_hex_value(edt->pi.data_src, fi);
-       }
-
-       /* Normal protocols and fields */
-       else {
-       gchar           *dfilter_string;
-           gint                chop_len;
-
-               switch (fi->hfinfo->type)
-               {
-               case FT_PROTOCOL:
+    }
+    else {
+        /* Normal protocols and fields */
+        gchar      *dfilter_string;
+        gint        chop_len;
+
+        switch (fi->hfinfo->type)
+        {
+        case FT_PROTOCOL:
             /* Print out the full details for the protocol. */
-                   if (fi->rep) {
+            if (fi->rep) {
                 return fi->rep->representation;
-                   } else {
+            } else {
                 /* Just print out the protocol abbreviation */
                 return fi->hfinfo->abbrev;;
             }
-               case FT_NONE:
-                       return NULL;
-               default:
-                       /* XXX - this is a hack until we can just call
-                        * fvalue_to_string_repr() for *all* FT_* types. */
-                       dfilter_string = proto_construct_match_selected_string(fi,
-                           edt);
-                       if (dfilter_string != NULL) {
-                               chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
-
-                               /* XXX - Remove double-quotes. Again, once we
-                                * can call fvalue_to_string_repr(), we can
-                                * ask it not to produce the version for
-                                * display-filters, and thus, no
-                                * double-quotes. */
-                               if (dfilter_string[strlen(dfilter_string)-1] == '"') {
-                                       dfilter_string[strlen(dfilter_string)-1] = '\0';
-                                       chop_len++;
-                               }
+        case FT_NONE:
+            /* Return "1" so that the presence of a field of type
+             * FT_NONE can be checked when using -T fields */
+            return "1";
+        default:
+            /* XXX - this is a hack until we can just call
+             * fvalue_to_string_repr() for *all* FT_* types. */
+            dfilter_string = proto_construct_match_selected_string(fi,
+                edt);
+            if (dfilter_string != NULL) {
+                chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
+
+                /* XXX - Remove double-quotes. Again, once we
+                 * can call fvalue_to_string_repr(), we can
+                 * ask it not to produce the version for
+                 * display-filters, and thus, no
+                 * double-quotes. */
+                if (dfilter_string[strlen(dfilter_string)-1] == '"') {
+                    dfilter_string[strlen(dfilter_string)-1] = '\0';
+                    chop_len++;
+                }
 
                 return &(dfilter_string[chop_len]);
-                       } else {
-                       return get_field_hex_value(edt->pi.data_src, fi);
+            } else {
+                return get_field_hex_value(edt->pi.data_src, fi);
             }
-               }
+        }
     }
 }
 
 static const gchar*
 get_field_hex_value(GSList* src_list, field_info *fi)
 {
-       const guint8 *pd;
+    const guint8 *pd;
 
-       if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
-               return "field length invalid!";
-       }
+    if (fi->length > tvb_length_remaining(fi->ds_tvb, fi->start)) {
+        return "field length invalid!";
+    }
 
-       /* Find the data for this field. */
-       pd = get_field_data(src_list, fi);
+    /* Find the data for this field. */
+    pd = get_field_data(src_list, fi);
 
-       if (pd) {
-       int i;
+    if (pd) {
+        int i;
         gchar* buffer;
         gchar* p;
         int len;
@@ -1500,14 +1568,13 @@ get_field_hex_value(GSList* src_list, field_info *fi)
         buffer = ep_alloc_array(gchar, len + 1);
         buffer[len] = '\0'; /* Ensure NULL termination in bad cases */
         p = buffer;
-               /* Print a simple hex dump */
-               for (i = 0 ; i < fi->length; i++) {
-                       g_snprintf(p, len, "%02x", pd[i]);
+        /* Print a simple hex dump */
+        for (i = 0 ; i < fi->length; i++) {
+            g_snprintf(p, chars_per_byte+1, "%02x", pd[i]);
             p += chars_per_byte;
-            len -= chars_per_byte;
-               }
+        }
         return buffer;
-       } else {
+    } else {
         return NULL;
     }
 }