from Frederic Peters: bring debian package generation .deb up to date
[obnox/wireshark/wip.git] / ethereal_gen.py
index 83a622d6db505ee81301c266b3d73ea9cf47a3b7..14f8440b398be29ea4f6d8933a349ac862a37efa 100644 (file)
@@ -1,6 +1,6 @@
 # -*- python -*-
 #
-# $Id: ethereal_gen.py,v 1.12 2001/10/12 17:14:41 guy Exp $
+# $Id$
 #                           
 # ethereal_gen.py (part of idl2eth)           
 #
@@ -37,7 +37,7 @@
 #   
 #   Omniidl Back-end which parses an IDL list of "Operation" nodes
 #   passed from ethereal_be2.py and generates "C" code for compiling
-#   as a dissector in Ethereal IP protocol anlayser.
+#   as a plugin for the  Ethereal IP Protocol Analyser.
 #
 #
 # Strategy (sneaky but ...)
@@ -94,8 +94,10 @@ import tempfile
 # 12. Implement IDL "union" code [done]
 # 13. Implement support for plugins [done]
 # 14. Dont generate code for empty operations (cf: exceptions without members)
-# 15. Generate code to display Enums numerically ans symbolically [done]
-# 16. Place structs in subtrees
+# 15. Generate code to display Enums numerically and symbolically [done]
+# 16. Place structs/unions in subtrees
+# 17. Recursive struct and union handling [done ]
+# 18. Improve variable naming for display (eg: structs, unions etc)
 #
 # Also test, Test, TEST
 #
@@ -169,9 +171,10 @@ class ethereal_gen_C:
     #
     #
         
-    def genCode(self,oplist, atlist, enlist):   # operation and attribute lists
+    def genCode(self,oplist, atlist, enlist, stlist, unlist):   # operation,attribute,enums,struct and union lists
 
-        self.genHelpers(oplist)         # sneaky .. call it now, to populate the fn_hash
+
+        self.genHelpers(oplist,stlist,unlist)  # sneaky .. call it now, to populate the fn_hash
                                         # so when I come to that operation later, I have the variables to
                                         # declare already.
                                         
@@ -191,7 +194,7 @@ class ethereal_gen_C:
         self.genEthCopyright()          # Ethereal Copyright comments.
         self.genGPL()                   # GPL license
         self.genIncludes()
-        self.genDeclares(oplist)
+        self.genDeclares(oplist,atlist,enlist,stlist,unlist)
         self.genProtocol()
         self.genRegisteredFields()
         self.genOpList(oplist)          # string constant declares for operation names
@@ -204,7 +207,7 @@ class ethereal_gen_C:
         self.genExceptionDelegator(oplist) # finds the helper function to decode a user exception
         self.genAttributeHelpers(atlist)   # helper function to decode "attributes"
 
-        self.genHelpers(oplist)
+        self.genHelpers(oplist,stlist,unlist)  # operation, struct and union decode helper functions
 
         self.genMainEntryStart(oplist)
         self.genOpDelegator(oplist)
@@ -213,7 +216,7 @@ class ethereal_gen_C:
 
         self.gen_proto_register()
         self.gen_proto_reg_handoff(oplist)
-        self.gen_plugin_init()
+        self.gen_plugin_register()
 
         #self.dumpvars()                 # debug
         
@@ -273,15 +276,39 @@ class ethereal_gen_C:
             
         self.st.out(self.template_Includes)
                                 
+
     #
     # denDeclares
     #
+    # generate function prototypes if required
     #
+    # Currently this is used for struct and union helper function declarations.
+    #
+    
     
-    def genDeclares(self,oplist):
+    def genDeclares(self,oplist,atlist,enlist,stlist,unlist):
         if self.DEBUG:
             print "XXX genDeclares"
 
+        # struct prototypes
+        
+        self.st.out(self.template_prototype_struct_start)        
+        for st in stlist:
+            #print st.repoId()
+            sname = self.namespace(st, "_")   
+
+            self.st.out(self.template_prototype_struct_body, stname=st.repoId(),name=sname)        
+        self.st.out(self.template_prototype_struct_end)        
+
+        # union prototypes
+
+        self.st.out(self.template_prototype_union_start)        
+        for un in unlist:
+            sname = self.namespace(un, "_")   
+            self.st.out(self.template_prototype_union_body, unname=un.repoId(),name=sname)        
+        self.st.out(self.template_prototype_union_end)        
+                        
+
 
                                 
     #
@@ -326,7 +353,6 @@ class ethereal_gen_C:
         self.st.dec_indent()
         self.st.out(self.template_main_dissector_switch_msgtype_all_other_msgtype)             
         self.st.dec_indent()
-        #self.st.out(self.template_main_dissector_switch_msgtype_end)              
         self.st.out(self.template_main_dissector_end)        
         
 
@@ -667,15 +693,21 @@ class ethereal_gen_C:
     # genHelpers()
     #
     # Generate private helper functions for each IDL operation.
+    # Generate private helper functions for each IDL struct.
+    # Generate private helper functions for each IDL union.
     #
-    # in: oplist
+    #
+    # in: oplist, stlist, unlist
     #
     
         
-    def genHelpers(self,oplist):
+    def genHelpers(self,oplist,stlist,unlist):
         for op in oplist:
             self.genOperation(op)
-
+        for st in stlist:
+            self.genStructHelper(st)
+        for un in unlist:
+            self.genUnionHelper(un)
 
     #
     # genOperation()
@@ -755,7 +787,6 @@ class ethereal_gen_C:
     #
     # Decode function parameters for a GIOP request message
     #
-    # TODO check for enum
     #
     
     def genOperationRequest(self,opnode):
@@ -772,7 +803,6 @@ class ethereal_gen_C:
     #
     # Decode function parameters for a GIOP reply message
     #
-    # TODO check for enum
 
     
     def genOperationReply(self,opnode):
@@ -966,7 +996,7 @@ class ethereal_gen_C:
         elif pt ==  idltype.tk_struct:
             self.get_CDR_struct(type,pn)
         elif pt ==  idltype.tk_TypeCode: # will I ever get here ?
-            self.get_CDR_TypeCode(type,pn)
+            self.get_CDR_TypeCode(pn)
         elif pt == idltype.tk_sequence:
             self.get_CDR_sequence(type,pn)
         elif pt == idltype.tk_objref:
@@ -1047,7 +1077,7 @@ class ethereal_gen_C:
 
     def get_CDR_enum(self,pn,type):
         #self.st.out(self.template_get_CDR_enum, varname=pn)
-        sname = self.namespace(type, "_")
+        sname = self.namespace(type.unalias(), "_")
         self.st.out(self.template_get_CDR_enum_symbolic, valstringarray=sname)
 
 
@@ -1079,6 +1109,8 @@ class ethereal_gen_C:
         self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
         self.addvar(self.c_u_octet4)
 
+
+            
     def get_CDR_union(self,type,pn):
         if self.DEBUG:
             print "XXX Union type =" , type, " pn = ",pn
@@ -1095,25 +1127,53 @@ class ethereal_gen_C:
         if self.DEBUG:    
             print "XXX Union ntype =" , ntype
 
-        st = ntype.switchType()
+        sname = self.namespace(ntype, "_")
+        self.st.out(self.template_union_start, name=sname )
+
+        # Output a call to the union helper function so I can handle recursive union also.
+        
+        self.st.out(self.template_decode_union,name=sname)    
 
+        self.st.out(self.template_union_end, name=sname )
             
 
-        #std = st.decl()
+    #
+    # Code to generate Union Helper functions
+    #
+    # in: un - a union node
+    #
+    #
 
-        if self.DEBUG:    
-            print "XXX Union ntype =" , ntype
-            print "XXX Union ntype.switchType =" , st
-            #print "XXX Union ntype.switchType().decl() = " , std
-            #print "XXX Union  std.repoId() = ", std.repoId()
 
+    def genUnionHelper(self,un):
+        if self.DEBUG:
+            print "XXX Union type =" , un
+            print "XXX Union type.decl()" , un.decl()
+            print "XXX Union Scoped Name" , un.scopedName()
 
-        self.st.out(self.template_comment_union_code_start, uname=ntype.repoId() )
+        sname = self.namespace(un, "_")
+        self.curr_sname = sname         # update current opnode/exnode/stnode/unnode scoped name
+        if not self.fn_hash_built:
+            self.fn_hash[sname] = []        # init empty list as val for this sname key
+                                            # but only if the fn_hash is not already built
 
-        self.getCDR3(st,pn);
+        self.st.out(self.template_union_helper_function_start, sname=sname, unname=un.repoId())
+        self.st.inc_indent()
+
+        self.st.out(self.template_helper_function_vars_start)
+        self.dumpCvars(sname)
+        self.st.out(self.template_helper_function_vars_end )
+        
+        self.st.out(self.template_union_helper_function_get_endianess)
+
+        st = un.switchType().unalias() # may be typedef switch type, so find real type
+
+        self.st.out(self.template_comment_union_code_start, uname=un.repoId() )
+
+        self.getCDR3(st,un.identifier());
 
         # Depending on what kind of discriminant I come accross (enum,integer,char,
-        # boolean), make sure I cast the return value of the get_XXX accessor
+        # short, boolean), make sure I cast the return value of the get_XXX accessor
         # to an appropriate value. Omniidl idlast.CaseLabel.value() accessor will
         # return an integer, or an Enumerator object that is then converted to its
         # integer equivalent.
@@ -1125,20 +1185,32 @@ class ethereal_gen_C:
         if (st.kind() == idltype.tk_enum):
             std = st.decl()            
             self.st.out(self.template_comment_union_code_discriminant, uname=std.repoId() )
-            self.st.out(self.template_union_code_save_discriminant_enum, discname=pn )
-            self.addvar(self.c_s_disc + pn + ";")            
+            self.st.out(self.template_union_code_save_discriminant_enum, discname=un.identifier() )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
 
         elif (st.kind() == idltype.tk_long):
-            self.st.out(self.template_union_code_save_discriminant_integer, discname=pn )
-            self.addvar(self.c_s_disc + pn + ";")            
+            self.st.out(self.template_union_code_save_discriminant_long, discname=un.identifier() )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
+
+        elif (st.kind() == idltype.tk_ulong):
+            self.st.out(self.template_union_code_save_discriminant_ulong, discname=un.identifier() )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
+
+        elif (st.kind() == idltype.tk_short):
+            self.st.out(self.template_union_code_save_discriminant_short, discname=un.identifier() )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
+
+        elif (st.kind() == idltype.tk_ushort):
+            self.st.out(self.template_union_code_save_discriminant_ushort, discname=un.identifier() )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
 
         elif (st.kind() == idltype.tk_boolean):
-            self.st.out(self.template_union_code_save_discriminant_boolean, discname=pn )
-            self.addvar(self.c_s_disc + pn + ";")            
+            self.st.out(self.template_union_code_save_discriminant_boolean, discname=un.identifier()  )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
             
         elif (st.kind() == idltype.tk_char):
-            self.st.out(self.template_union_code_save_discriminant_char, discname=pn )
-            self.addvar(self.c_s_disc + pn + ";")            
+            self.st.out(self.template_union_code_save_discriminant_char, discname=un.identifier() )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
 
         else:
             print "XXX Unknown st.kind() = ", st.kind()
@@ -1147,8 +1219,8 @@ class ethereal_gen_C:
         # Loop over all cases in this union
         #
         
-        for uc in ntype.cases():        # for all UnionCase objects in this union
-            for cl in uc.labels():       # for all Caselabel objects in this UnionCase
+        for uc in un.cases():           # for all UnionCase objects in this union
+            for cl in uc.labels():      # for all Caselabel objects in this UnionCase
 
                 # get integer value, even if discriminant is
                 # an Enumerator node
@@ -1172,9 +1244,18 @@ class ethereal_gen_C:
                 #    
                 # if char, dont convert to int, but put inside single quotes so that it is understood by C.
                 # eg: if (disc == 'b')..
+                #
+                # TODO : handle \xxx chars generically from a function or table lookup rather than
+                #        a whole bunch of "if" statements. -- FS
+                
                                           
-                if (st.kind() == idltype.tk_char):      
-                    string_clv = "'" + clv + "'"
+                if (st.kind() == idltype.tk_char):
+                    if (clv == '\n'):          # newline
+                        string_clv = "'\\n'" 
+                    elif (clv == '\t'):        # tab
+                        string_clv = "'\\t'"
+                    else:
+                        string_clv = "'" + clv + "'"
                 else:                    
                     string_clv = '%i ' % clv
 
@@ -1183,7 +1264,7 @@ class ethereal_gen_C:
                 #
                 
                 if not cl.default():
-                    self.st.out(self.template_comment_union_code_label_compare_start, discname=pn,labelval=string_clv )
+                    self.st.out(self.template_comment_union_code_label_compare_start, discname=un.identifier(),labelval=string_clv )
                     self.st.inc_indent()       
                 else:
                     self.st.out(self.template_comment_union_code_label_default_start  )
@@ -1196,7 +1277,11 @@ class ethereal_gen_C:
                     self.st.out(self.template_comment_union_code_label_compare_end )
                 else:
                     self.st.out(self.template_comment_union_code_label_default_end  )
-            
+
+        self.st.dec_indent()
+        self.st.out(self.template_union_helper_function_end)
+
+
 
     #
     # Currently, get_CDR_alias is geared to finding typdef 
@@ -1236,8 +1321,11 @@ class ethereal_gen_C:
         
         
 
+    #
+    # Handle structs, including recursive
+    #
+    
     def get_CDR_struct(self,type,pn):       
-        self.st.out(self.template_structure_start, name=type.name() )
 
         #  If I am a typedef struct {..}; node then find the struct node
         
@@ -1245,8 +1333,44 @@ class ethereal_gen_C:
             ntype = type.decl().alias().aliasType().decl()           
         else:
             ntype = type.decl()         # I am a struct node
-                        
-        for m in ntype.members():
+
+        sname = self.namespace(ntype, "_")
+        self.st.out(self.template_structure_start, name=sname )
+
+        # Output a call to the struct helper function so I can handle recursive structs also.
+        
+        self.st.out(self.template_decode_struct,name=sname)    
+
+        self.st.out(self.template_structure_end, name=sname )
+
+    #
+    # genStructhelper() 
+    #
+    # Generate private helper functions to decode a struct
+    #
+    # in: stnode ( a struct node)
+    #
+    
+    def genStructHelper(self,st):
+        if self.DEBUG:
+            print "XXX genStructHelper"
+            
+        sname = self.namespace(st, "_")
+        self.curr_sname = sname         # update current opnode/exnode/stnode scoped name
+        if not self.fn_hash_built:
+            self.fn_hash[sname] = []        # init empty list as val for this sname key
+                                            # but only if the fn_hash is not already built
+
+        self.st.out(self.template_struct_helper_function_start, sname=sname, stname=st.repoId())
+        self.st.inc_indent()
+
+        self.st.out(self.template_helper_function_vars_start)
+        self.dumpCvars(sname)
+        self.st.out(self.template_helper_function_vars_end )
+        
+        self.st.out(self.template_struct_helper_function_get_endianess)
+
+        for m in st.members():
             for decl in m.declarators():
                 if decl.sizes():        # an array
                     indices = self.get_indices_from_sizes(decl.sizes())
@@ -1256,21 +1380,25 @@ class ethereal_gen_C:
                     self.addvar(self.c_i + decl.identifier() + ";")
                     
                     self.st.inc_indent()       
-                    self.getCDR3(m.memberType(), type.name() + "_" + decl.identifier() )                    
+                    self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )                    
                     self.st.dec_indent()
                     self.st.out(self.template_get_CDR_array_end)
                     
                     
-                else:    
-                    self.getCDR3(m.memberType(), type.name() + "_" + decl.identifier() )
+                else:                            
+                    self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
+                            
+        self.st.dec_indent()
+        self.st.out(self.template_struct_helper_function_end)
+
 
-        self.st.out(self.template_structure_end, name=type.name())
 
 
 
     #
     # Generate code to access a sequence of a type
     #
+
     
     def get_CDR_sequence(self,type, pn):
         self.st.out(self.template_get_CDR_sequence_length, seqname=pn )
@@ -1322,8 +1450,8 @@ class ethereal_gen_C:
     # generate code for plugin initialisation
     #
 
-    def gen_plugin_init(self):
-        self.st.out(self.template_plugin_init, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
+    def gen_plugin_register(self):
+        self.st.out(self.template_plugin_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
 
     #
     # generate  register_giop_user_module code, and register only
@@ -1489,7 +1617,7 @@ class ethereal_gen_C:
 
 
     template_helper_function_start = """\
-static void decode_@sname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
+static void decode_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header, gchar *operation _U_) {
 
     gboolean stream_is_big_endian;          /* big endianess */
 """
@@ -1577,27 +1705,26 @@ static guint32  boundary = GIOP_HEADER_SIZE;  /* initial value */
 """
 
     #
-    # plugin_init and plugin_reg_handoff templates
+    # plugin_register and plugin_reg_handoff templates
     #
 
-    template_plugin_init = """
-
-#ifndef __ETHEREAL_STATIC__
+    template_plugin_register = """
 
-G_MODULE_EXPORT void
-plugin_reg_handoff(void){
-   proto_register_handoff_giop_@dissector_name@();
-}
+#ifndef ENABLE_STATIC
 
 G_MODULE_EXPORT void
-plugin_init(plugin_address_table_t *pat){
-   /* initialise the table of pointers needed in Win32 DLLs */
-   plugin_address_table_init(pat);
+plugin_register(void)
+{
    if (proto_@dissector_name@ == -1) {
      proto_register_giop_@dissector_name@();
    }
 }
 
+G_MODULE_EXPORT void
+plugin_reg_handoff(void){
+   proto_register_handoff_giop_@dissector_name@();
+}
+
 #endif
 
 """
@@ -1978,6 +2105,15 @@ for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
 /*  End struct \"@name@\"  */
 """
 
+    template_union_start = """\
+/*  Begin union \"@name@\"  */
+"""
+
+
+    template_union_end = """\
+/*  End union \"@name@\"  */
+"""
+
 
 
 
@@ -2040,36 +2176,21 @@ for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
 # include "config.h"
 #endif
 
-#include "plugins/plugin_api.h"
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <gmodule.h>
 
-#ifdef HAVE_SYS_TYPES_H
-# include <sys/types.h>
-#endif
-
-#ifdef HAVE_NETINET_IN_H
-# include <netinet/in.h>
-#endif
-
 #ifdef NEED_SNPRINTF_H
-# ifdef HAVE_STDARG_H
-#  include <stdarg.h>
-# else
-#  include <varargs.h>
-# endif
 # include "snprintf.h"
 #endif
 
 #include <string.h>
 #include <glib.h>
-#include "packet.h"
-#include "proto.h"
+#include <epan/packet.h>
+#include <epan/proto.h>
 #include "packet-giop.h"
 
-#ifndef __ETHEREAL_STATIC__
+#ifndef ENABLE_STATIC
 G_MODULE_EXPORT const gchar version[] = "0.0.1";
 #endif
 
@@ -2081,21 +2202,26 @@ G_MODULE_EXPORT const gchar version[] = "0.0.1";
 #
 
     template_main_dissector_start = """\
-static gboolean dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, gchar *operation, gchar *idlname) {
+static gboolean dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, gchar *operation, gchar *idlname _U_) {
 
     proto_item *ti = NULL;
     proto_tree *tree = NULL;            /* init later, inside if(tree) */
     
     gboolean be;                        /* big endianess */
-    guint32  offset_saved = (*offset);  /* save in case we must back out */
 
-    pinfo->current_proto = \"@disprot@\";
+    if (check_col(pinfo->cinfo, COL_PROTOCOL))
+       col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
 
-    if (check_col(pinfo->fd, COL_PROTOCOL))
-       col_add_str(pinfo->fd, COL_PROTOCOL, \"@disprot@\");
+/* 
+ * Do not clear COL_INFO, as nothing is being written there by 
+ * this dissector yet. So leave it as is from the GIOP dissector.
+ * TODO: add something useful to COL_INFO 
+ *  if (check_col(pinfo->cinfo, COL_INFO))
+ *     col_clear(pinfo->cinfo, COL_INFO);
+ */
 
     if (ptree) {
-       ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, tvb_length(tvb) - *offset, FALSE);
+       ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, -1, FALSE);
        tree = proto_item_add_subtree(ti, ett_@dissname@);
     }  
 
@@ -2132,23 +2258,6 @@ break;
 """
 
 
-    template_main_dissector_switch_msgtype_end = """\
-
-
-/*
- * We failed to match ANY operations, so perhaps this is not for us !
- */
-
-(*offset) = offset_saved;       /* be nice */
-
-return FALSE;
-
-
-"""
-
-
-
-    
     template_main_dissector_switch_msgtype_all_other_msgtype = """\
 case CancelRequest:
 case LocateRequest:
@@ -2166,14 +2275,6 @@ default:
 """
 
 
-    template_main_dissector_switch_msgtype_end = """\
-
-   return TRUE;
-
-} /* switch */
-
-"""
-
     template_main_dissector_end = """\
 
     return FALSE;
@@ -2243,9 +2344,9 @@ default:
  *
  */
  
-static gboolean decode_user_exception(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation ) {
+static gboolean decode_user_exception(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_ ) {
     
-    gboolean be;                        /* big endianess */
+    gboolean be _U_;                        /* big endianess */
 
     
 """
@@ -2308,6 +2409,75 @@ stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
 }
 """
 
+    
+#
+# template for struct helper code
+#
+
+
+    template_struct_helper_function_start = """\
+
+/* Struct = @stname@ */
+
+static void decode_@sname@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
+
+    gboolean stream_is_big_endian;          /* big endianess */
+"""
+
+
+
+
+    #
+    # Template for the helper function
+    # to get stream endianess from header
+    #
+
+    template_struct_helper_function_get_endianess = """\
+
+stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
+
+"""
+
+    
+    template_struct_helper_function_end = """\
+}
+"""
+
+#
+# template for union helper code
+#
+
+
+    template_union_helper_function_start = """\
+
+/* Union = @unname@ */
+
+static void decode_@sname@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
+
+    gboolean stream_is_big_endian;          /* big endianess */
+"""
+
+
+
+
+    #
+    # Template for the helper function
+    # to get stream endianess from header
+    #
+
+    template_union_helper_function_get_endianess = """\
+
+stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
+
+"""
+
+    
+    template_union_helper_function_end = """\
+}
+"""
+
+
+
 #-------------------------------------------------------------#
 #             Value string  templates                         #
 #-------------------------------------------------------------#
@@ -2515,9 +2685,21 @@ stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
 disc_s_@discname@ = (gint32) u_octet4;     /* save Enum Value  discriminant and cast to gint32 */
 """
     
-    template_union_code_save_discriminant_integer = """\
+    template_union_code_save_discriminant_long = """\
 disc_s_@discname@ = (gint32) s_octet4;     /* save gint32 discriminant and cast to gint32 */
 """
+
+    template_union_code_save_discriminant_ulong = """\
+disc_s_@discname@ = (gint32) u_octet4;     /* save guint32 discriminant and cast to gint32 */
+"""    
+
+    template_union_code_save_discriminant_short = """\
+disc_s_@discname@ = (gint32) s_octet2;     /* save gint16 discriminant and cast to gint32 */
+"""    
+
+    template_union_code_save_discriminant_ushort = """\
+disc_s_@discname@ = (gint32) u_octet2;     /* save guint16 discriminant and cast to gint32 */
+"""    
     
     template_union_code_save_discriminant_char = """\
 disc_s_@discname@ = (gint32) u_octet1;     /* save guint1 discriminant and cast to gint32 */
@@ -2533,7 +2715,7 @@ if (disc_s_@discname@ == @labelval@) {
 
  """
     template_comment_union_code_label_compare_end = """\
-    break;
+    return;     /* End Compare for this discriminant type */
 }
 
  """
@@ -2548,3 +2730,62 @@ if (disc_s_@discname@ == @labelval@) {
 /* Default Union Case End */
  
  """
+
+    #
+    # Templates for function prototypes.
+    # This is used in genDeclares() for declaring function prototypes
+    # for structs and union helper functions.
+    #
+
+    template_prototype_struct_start = """
+/* Struct prototype declaration Start */
+"""
+    
+    template_prototype_struct_end = """
+/* Struct prototype declaration End */
+"""
+    
+    template_prototype_struct_body = """
+        
+/* Struct = @stname@ */
+
+static void decode_@name@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
+
+"""
+
+    template_decode_struct = """
+        
+decode_@name@_st(tvb, pinfo, tree, offset, header, operation);
+
+"""
+
+
+    
+    
+    template_prototype_union_start = """
+/* Union prototype declaration Start */
+"""
+    
+    template_prototype_union_end = """
+/* Union prototype declaration End */
+"""
+    
+    template_prototype_union_body = """
+        
+/* Union = @unname@ */
+
+static void decode_@name@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
+
+"""
+
+    template_decode_union = """
+        
+decode_@name@_un(tvb, pinfo, tree, offset, header, operation);
+
+"""
+    
+