5 # ethereal_gen.py (part of idl2eth)
7 # Author : Frank Singleton (frank.singleton@ericsson.com)
9 # Copyright (C) 2001 Frank Singleton, Ericsson Inc.
11 # This file is a backend to "omniidl", used to generate "Ethereal"
12 # dissectors from CORBA IDL descriptions. The output language generated
13 # is "C". It will generate code to use the GIOP/IIOP get_CDR_XXX API.
15 # Please see packet-giop.h in Ethereal distro for API description.
16 # Ethereal is available at http://www.ethereal.com/
18 # Omniidl is part of the OmniOrb distribution, and is available at
19 # http://www.uk.research.att.com/omniORB/omniORB.html
21 # This program is free software; you can redistribute it and/or modify it
22 # under the terms of the GNU General Public License as published by
23 # the Free Software Foundation; either version 2 of the License, or
24 # (at your option) any later version.
26 # This program is distributed in the hope that it will be useful,
27 # but WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 # General Public License for more details.
31 # You should have received a copy of the GNU General Public License
32 # along with this program; if not, write to the Free Software
33 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
38 # Omniidl Back-end which parses an IDL list of "Operation" nodes
39 # passed from ethereal_be2.py and generates "C" code for compiling
40 # as a plugin for the Ethereal IP Protocol Analyser.
43 # Strategy (sneaky but ...)
45 # problem: I dont know what variables to declare until AFTER the helper functions
46 # have been built, so ...
48 # There are 2 passes through genHelpers, the first one is there just to
49 # make sure the fn_hash data struct is populated properly.
50 # The second pass is the real thing, generating code and declaring
51 # variables (from the 1st pass) properly.
55 """Ethereal IDL compiler back-end."""
57 from omniidl import idlast, idltype, idlutil, output
62 # Output class, generates "C" src code for the sub-dissector
69 # node - a reference to an Operations object.
70 # name - scoped name (Module::Module::Interface:: .. ::Operation
78 # 1. generate hf[] data for searchable fields (but what is searchable?)
79 # 2. add item instead of add_text()
80 # 3. sequence handling [done]
81 # 4. User Exceptions [done]
82 # 5. Fix arrays, and structs containing arrays [done]
84 # 7. Exception can be common to many operations, so handle them outside the
85 # operation helper functions [done]
86 # 8. Automatic variable declaration [done, improve, still get some collisions.add variable delegator function ]
87 # For example, mutlidimensional arrays.
88 # 9. wchar and wstring handling [giop API needs improving]
89 # 10. Support Fixed [done]
90 # 11. Support attributes (get/set) [started, needs language mapping option, perhaps ethereal GUI option
91 # to set the attribute function prefix or suffix ? ] For now the prefix is "_get" and "_set"
92 # eg: attribute string apple => _get_apple and _set_apple
94 # 12. Implement IDL "union" code [done]
95 # 13. Implement support for plugins [done]
96 # 14. Dont generate code for empty operations (cf: exceptions without members)
97 # 15. Generate code to display Enums numerically and symbolically [done]
98 # 16. Place structs/unions in subtrees
99 # 17. Recursive struct and union handling [done ]
100 # 18. Improve variable naming for display (eg: structs, unions etc)
102 # Also test, Test, TEST
109 # For every operation and attribute do
110 # For return val and all parameters do
111 # find basic IDL type for each parameter
113 # output exception handling code
114 # output attribute handling code
118 class ethereal_gen_C:
122 # Turn DEBUG stuff on/off
128 # Some string constants for our templates
131 c_u_octet4 = "guint32 u_octet4;"
132 c_s_octet4 = "gint32 s_octet4;"
133 c_u_octet2 = "guint16 u_octet2;"
134 c_s_octet2 = "gint16 s_octet2;"
135 c_u_octet1 = "guint8 u_octet1;"
136 c_s_octet1 = "gint8 s_octet1;"
138 c_float = "gfloat my_float;"
139 c_double = "gdouble my_double;"
141 c_seq = "gchar *seq = NULL;" # pointer to buffer of gchars
142 c_i = "guint32 i_"; # loop index
143 c_i_lim = "guint32 u_octet4_loop_"; # loop limit
144 c_u_disc = "guint32 disc_u_"; # unsigned int union discriminant variable name (enum)
145 c_s_disc = "gint32 disc_s_"; # signed int union discriminant variable name (other cases, except Enum)
151 def __init__(self, st, protocol_name, dissector_name ,description):
152 self.st = output.Stream(tempfile.TemporaryFile(),4) # for first pass only
154 self.st_save = st # where 2nd pass should go
155 self.protoname = protocol_name # Protocol Name (eg: ECHO)
156 self.dissname = dissector_name # Dissector name (eg: echo)
157 self.description = description # Detailed Protocol description (eg: Echo IDL Example)
158 self.exlist = [] # list of exceptions used in operations.
159 #self.curr_sname # scoped name of current opnode or exnode I am visiting, used for generating "C" var declares
160 self.fn_hash = {} # top level hash to contain key = function/exception and val = list of variable declarations
162 self.fn_hash_built = 0 # flag to indicate the 1st pass is complete, and the fn_hash is correctly
163 # populated with operations/vars and exceptions/vars
169 # Main entry point, controls sequence of
174 def genCode(self,oplist, atlist, enlist, stlist, unlist): # operation,attribute,enums,struct and union lists
177 self.genHelpers(oplist,stlist,unlist) # sneaky .. call it now, to populate the fn_hash
178 # so when I come to that operation later, I have the variables to
181 self.genExceptionHelpers(oplist) # sneaky .. call it now, to populate the fn_hash
182 # so when I come to that exception later, I have the variables to
185 self.genAttributeHelpers(atlist) # sneaky .. call it now, to populate the fn_hash
186 # so when I come to that exception later, I have the variables to
190 self.fn_hash_built = 1 # DONE, so now I know , see genOperation()
192 self.st = self.st_save
193 self.genHeader() # initial dissector comments
194 self.genEthCopyright() # Ethereal Copyright comments.
195 self.genGPL() # GPL license
197 self.genDeclares(oplist,atlist,enlist,stlist,unlist)
199 self.genRegisteredFields()
200 self.genOpList(oplist) # string constant declares for operation names
201 self.genExList(oplist) # string constant declares for user exceptions
202 self.genAtList(atlist) # string constant declares for Attributes
203 self.genEnList(enlist) # string constant declares for Enums
206 self.genExceptionHelpers(oplist) # helper function to decode user exceptions that have members
207 self.genExceptionDelegator(oplist) # finds the helper function to decode a user exception
208 self.genAttributeHelpers(atlist) # helper function to decode "attributes"
210 self.genHelpers(oplist,stlist,unlist) # operation, struct and union decode helper functions
212 self.genMainEntryStart(oplist)
213 self.genOpDelegator(oplist)
214 self.genAtDelegator(atlist)
215 self.genMainEntryEnd()
217 self.gen_proto_register()
218 self.gen_proto_reg_handoff(oplist)
219 self.gen_plugin_register()
221 #self.dumpvars() # debug
228 # Generate Standard Ethereal Header Comments
233 self.st.out(self.template_Header,dissector_name=self.dissname)
235 print "XXX genHeader"
243 # Ethereal Copyright Info
247 def genEthCopyright(self):
249 print "XXX genEthCopyright"
250 self.st.out(self.template_ethereal_copyright)
264 self.st.out(self.template_GPL)
273 def genIncludes(self):
275 print "XXX genIncludes"
277 self.st.out(self.template_Includes)
283 # generate function prototypes if required
285 # Currently this is used for struct and union helper function declarations.
289 def genDeclares(self,oplist,atlist,enlist,stlist,unlist):
291 print "XXX genDeclares"
293 # prototype for start_dissecting()
295 self.st.out(self.template_prototype_start_dissecting)
299 self.st.out(self.template_prototype_struct_start)
302 sname = self.namespace(st, "_")
304 self.st.out(self.template_prototype_struct_body, stname=st.repoId(),name=sname)
305 self.st.out(self.template_prototype_struct_end)
309 self.st.out(self.template_prototype_union_start)
311 sname = self.namespace(un, "_")
312 self.st.out(self.template_prototype_union_body, unname=un.repoId(),name=sname)
313 self.st.out(self.template_prototype_union_end)
323 def genProtocol(self):
324 self.st.out(self.template_protocol, dissector_name=self.dissname)
325 self.st.out(self.template_init_boundary)
329 # genProtoAndRegisteredFields
333 def genRegisteredFields(self):
334 self.st.out(self.template_registered_fields )
342 def genMainEntryStart(self,oplist):
343 self.st.out(self.template_main_dissector_start, dissname=self.dissname, disprot=self.protoname)
345 self.st.out(self.template_main_dissector_switch_msgtype_start)
346 self.st.out(self.template_main_dissector_switch_msgtype_start_request_reply)
354 def genMainEntryEnd(self):
356 self.st.out(self.template_main_dissector_switch_msgtype_end_request_reply)
358 self.st.out(self.template_main_dissector_switch_msgtype_all_other_msgtype)
360 self.st.out(self.template_main_dissector_end)
368 # out: C code for IDL operations
372 # static const char Penguin_Echo_echoShort_op[] = "echoShort" ;
375 def genOpList(self,oplist):
376 self.st.out(self.template_comment_operations_start)
379 sname = self.namespace(n, "_")
380 opname = n.identifier()
381 self.st.out(self.template_operations_declare, sname=sname, opname=opname)
383 self.st.out(self.template_comment_operations_end)
390 # out: C code for IDL User Exceptions that contain members
394 # static const char user_exception_tux_bad_value[] = "IDL:tux/bad_value:1.0" ;
397 def genExList(self,oplist):
399 self.st.out(self.template_comment_user_exceptions_string_declare_start)
401 exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
405 print "XXX Exception " , ex.repoId()
406 print "XXX Exception Identifier" , ex.identifier()
407 print "XXX Exception Scoped Name" , ex.scopedName()
409 if (ex.members()): # only if has members
410 sname = self.namespace(ex, "_")
412 self.st.out(self.template_user_exceptions_declare, sname=sname, exname=ex.repoId())
414 self.st.out(self.template_comment_user_exceptions_string_declare_end)
421 # out: C code for IDL attribute decalarations.
423 # NOTE: Mapping of attributes to operation(function) names is tricky.
425 # The actual accessor function names are language-mapping specific. The attribute name
426 # is subject to OMG IDL's name scoping rules; the accessor function names are
427 # guaranteed not to collide with any legal operation names specifiable in OMG IDL.
431 # static const char get_Penguin_Echo_get_width_at[] = "get_width" ;
432 # static const char set_Penguin_Echo_set_width_at[] = "set_width" ;
436 # static const char get_Penguin_Echo_get_width_at[] = "_get_width" ;
437 # static const char set_Penguin_Echo_set_width_at[] = "_set_width" ;
439 # TODO: Implement some language dependant templates to handle naming conventions
440 # language <=> attribute. for C, C++. Java etc
442 # OR, just add a runtime GUI option to select language binding for attributes -- FS
446 # ie: def genAtlist(self,atlist,language)
451 def genAtList(self,atlist):
452 self.st.out(self.template_comment_attributes_start)
455 for i in n.declarators(): #
456 sname = self.namespace(i, "_")
457 atname = i.identifier()
458 self.st.out(self.template_attributes_declare_Java_get, sname=sname, atname=atname)
460 self.st.out(self.template_attributes_declare_Java_set, sname=sname, atname=atname)
462 self.st.out(self.template_comment_attributes_end)
470 # out: C code for IDL Enum decalarations using "static const value_string" template
475 def genEnList(self,enlist):
477 self.st.out(self.template_comment_enums_start)
480 sname = self.namespace(enum, "_")
482 self.st.out(self.template_comment_enum_comment, ename=enum.repoId())
483 self.st.out(self.template_value_string_start, valstringname=sname)
484 for enumerator in enum.enumerators():
485 self.st.out(self.template_value_string_entry, intval=str(self.valFromEnum(enum,enumerator)), description=enumerator.identifier())
488 #atname = n.identifier()
489 self.st.out(self.template_value_string_end, valstringname=sname)
491 self.st.out(self.template_comment_enums_end)
503 # genExceptionDelegator
507 # out: C code for User exception delegator
513 def genExceptionDelegator(self,oplist):
515 self.st.out(self.template_main_exception_delegator_start)
518 exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
522 print "XXX Exception " , ex.repoId()
523 print "XXX Exception Identifier" , ex.identifier()
524 print "XXX Exception Scoped Name" , ex.scopedName()
526 if (ex.members()): # only if has members
527 sname = self.namespace(ex, "_")
529 self.st.out(self.template_ex_delegate_code, sname=sname, exname=ex.repoId())
532 self.st.out(self.template_main_exception_delegator_end)
536 # genAttribueHelpers()
538 # Generate private helper functions to decode Attributes.
542 # For readonly attribute - generate get_xxx()
543 # If NOT readonly attribute - also generate set_xxx()
546 def genAttributeHelpers(self,atlist):
548 print "XXX genAttributeHelpers: atlist = ", atlist
550 self.st.out(self.template_attribute_helpers_start)
552 for attrib in atlist:
553 for decl in attrib.declarators():
554 self.genAtHelper(attrib,decl,"get") # get accessor
555 if not attrib.readonly():
556 self.genAtHelper(attrib,decl,"set") # set accessor
558 self.st.out(self.template_attribute_helpers_end)
563 # Generate private helper functions to decode an attribute
565 # in: at - attribute node
566 # in: decl - declarator belonging to this attribute
567 # in: order - to generate a "get" or "set" helper
569 def genAtHelper(self,attrib,decl,order):
571 print "XXX genAtHelper"
573 sname = order + "_" + self.namespace(decl, "_") # must use set or get prefix to avoid collision
574 self.curr_sname = sname # update current opnode/exnode scoped name
576 if not self.fn_hash_built:
577 self.fn_hash[sname] = [] # init empty list as val for this sname key
578 # but only if the fn_hash is not already built
580 self.st.out(self.template_attribute_helper_function_start, sname=sname, atname=decl.repoId())
583 self.st.out(self.template_helper_function_vars_start)
584 self.dumpCvars(sname)
585 self.st.out(self.template_helper_function_vars_end )
587 self.st.out(self.template_exception_helper_function_get_endianess)
590 # TODO - attributes are simple types, so remove array handling
593 if decl.sizes(): # an array
594 indices = self.get_indices_from_sizes(decl.sizes())
595 string_indices = '%i ' % indices # convert int to string
596 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
597 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
598 self.addvar(self.c_i + decl.identifier() + ";")
602 self.getCDR3(attrib.attrType(), decl.identifier() )
605 self.st.out(self.template_get_CDR_array_end)
610 self.getCDR3(attrib.attrType(), decl.identifier() )
613 self.st.out(self.template_attribute_helper_function_end)
618 # genExceptionHelpers()
620 # Generate private helper functions to decode Exceptions used
627 def genExceptionHelpers(self,oplist):
628 exlist = self.get_exceptionList(oplist) # grab list of exception nodes
630 print "XXX genExceptionHelpers: exlist = ", exlist
632 self.st.out(self.template_exception_helpers_start)
634 if (ex.members()): # only if has members
635 #print "XXX Exception = " + ex.identifier()
638 self.st.out(self.template_exception_helpers_end)
644 # Generate private helper functions to decode User Exceptions
646 # in: exnode ( an exception node)
649 def genExHelper(self,ex):
651 print "XXX genExHelper"
653 sname = self.namespace(ex, "_")
654 self.curr_sname = sname # update current opnode/exnode scoped name
655 if not self.fn_hash_built:
656 self.fn_hash[sname] = [] # init empty list as val for this sname key
657 # but only if the fn_hash is not already built
659 self.st.out(self.template_exception_helper_function_start, sname=sname, exname=ex.repoId())
662 self.st.out(self.template_helper_function_vars_start)
663 self.dumpCvars(sname)
664 self.st.out(self.template_helper_function_vars_end )
666 self.st.out(self.template_exception_helper_function_get_endianess)
669 for m in ex.members():
670 #print "XXX genExhelper, member = ", m, "member type = ", m.memberType()
673 for decl in m.declarators():
674 #print "XXX genExhelper, d = ", decl
675 if decl.sizes(): # an array
676 indices = self.get_indices_from_sizes(decl.sizes())
677 string_indices = '%i ' % indices # convert int to string
678 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
679 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
680 self.addvar(self.c_i + decl.identifier() + ";")
683 self.getCDR3(m.memberType(), ex.identifier() + "_" + decl.identifier() )
686 self.st.out(self.template_get_CDR_array_end)
690 self.getCDR3(m.memberType(), ex.identifier() + "_" + decl.identifier() )
693 self.st.out(self.template_exception_helper_function_end)
699 # Generate private helper functions for each IDL operation.
700 # Generate private helper functions for each IDL struct.
701 # Generate private helper functions for each IDL union.
704 # in: oplist, stlist, unlist
708 def genHelpers(self,oplist,stlist,unlist):
710 self.genOperation(op)
712 self.genStructHelper(st)
714 self.genUnionHelper(un)
719 # Generate private helper functions for a specificIDL operation.
724 def genOperation(self,opnode):
726 print "XXX genOperation called"
728 sname = self.namespace(opnode, "_")
729 if not self.fn_hash_built:
730 self.fn_hash[sname] = [] # init empty list as val for this sname key
731 # but only if the fn_hash is not already built
733 self.curr_sname = sname # update current opnode's scoped name
734 opname = opnode.identifier()
736 self.st.out(self.template_helper_function_comment, repoid=opnode.repoId() )
738 self.st.out(self.template_helper_function_start, sname=sname)
741 self.st.out(self.template_helper_function_vars_start)
742 self.dumpCvars(sname)
743 self.st.out(self.template_helper_function_vars_end )
746 self.st.out(self.template_helper_switch_msgtype_start)
748 self.st.out(self.template_helper_switch_msgtype_request_start)
750 self.genOperationRequest(opnode)
751 self.st.out(self.template_helper_switch_msgtype_request_end)
754 self.st.out(self.template_helper_switch_msgtype_reply_start)
757 self.st.out(self.template_helper_switch_rep_status_start)
760 self.st.out(self.template_helper_switch_msgtype_reply_no_exception_start)
762 self.genOperationReply(opnode)
763 self.st.out(self.template_helper_switch_msgtype_reply_no_exception_end)
766 self.st.out(self.template_helper_switch_msgtype_reply_user_exception_start)
768 self.genOpExceptions(opnode)
769 self.st.out(self.template_helper_switch_msgtype_reply_user_exception_end)
772 self.st.out(self.template_helper_switch_msgtype_reply_default_start)
773 self.st.out(self.template_helper_switch_msgtype_reply_default_end)
775 self.st.out(self.template_helper_switch_rep_status_end)
779 self.st.out(self.template_helper_switch_msgtype_default_start)
780 self.st.out(self.template_helper_switch_msgtype_default_end)
782 self.st.out(self.template_helper_switch_msgtype_end)
786 self.st.out(self.template_helper_function_end, sname=sname)
792 # Decode function parameters for a GIOP request message
796 def genOperationRequest(self,opnode):
797 for p in opnode.parameters():
800 print "XXX parameter = " ,p
801 print "XXX parameter type = " ,p.paramType()
802 print "XXX parameter type kind = " ,p.paramType().kind()
804 self.getCDR3(p.paramType(),p.identifier())
808 # Decode function parameters for a GIOP reply message
812 def genOperationReply(self,opnode):
814 rt = opnode.returnType() # get return type
816 print "XXX opnode = " , opnode
817 print "XXX return type = " , rt
818 print "XXX return type.unalias = " , rt.unalias()
819 print "XXX return type.kind() = " , rt.kind();
822 if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
823 #self.getCDR3(rt.decl().alias().aliasType(),"dummy") # return value maybe a typedef
824 #self.get_CDR_alias(rt, "Operation_Return_Value" )
825 self.get_CDR_alias(rt, rt.name() )
828 self.getCDR3(rt, "Operation_Return_Value") # return value is NOT an alias
830 for p in opnode.parameters():
831 if p.is_out(): # out or inout
832 self.getCDR3(p.paramType(),p.identifier())
834 #self.st.dec_indent()
836 def genOpExceptions(self,opnode):
837 for ex in opnode.raises():
840 for m in ex.members():
842 #print m.memberType(), m.memberType().kind()
844 # Delegator for Operations
847 def genOpDelegator(self,oplist):
849 opname = op.identifier()
850 sname = self.namespace(op, "_")
851 self.st.out(self.template_op_delegate_code, sname=sname)
854 # Delegator for Attributes
857 def genAtDelegator(self,atlist):
859 for i in a.declarators():
860 atname = i.identifier()
861 sname = self.namespace(i, "_")
862 self.st.out(self.template_at_delegate_code_get, sname=sname)
864 self.st.out(self.template_at_delegate_code_set, sname=sname)
868 # Add a variable declaration to the hash of list
871 def addvar(self, var):
872 if not ( var in self.fn_hash[self.curr_sname] ):
873 self.fn_hash[self.curr_sname].append(var)
876 # Print the variable declaration from the hash of list
881 for fn in self.fn_hash.keys():
883 for v in self.fn_hash[fn]:
886 # Print the "C" variable declaration from the hash of list
887 # for a given scoped operation name (eg: tux_penguin_eat)
891 def dumpCvars(self, sname):
892 for v in self.fn_hash[sname]:
897 # Given an enum node, and a enumerator node, return
898 # the enumerator's numerical value.
900 # eg: enum Color {red,green,blue} should return
904 def valFromEnum(self,enumNode, enumeratorNode):
906 print "XXX valFromEnum, enumNode = ", enumNode, " from ", enumNode.repoId()
907 print "XXX valFromEnum, enumeratorNode = ", enumeratorNode, " from ", enumeratorNode.repoId()
909 if isinstance(enumeratorNode,idlast.Enumerator):
910 value = enumNode.enumerators().index(enumeratorNode)
939 ## tk_longdouble = 25
946 ## tk_abstract_interface = 32
952 # This is the main "iterator" function. It takes a node, and tries to output
953 # a get_CDR_XXX accessor method(s). It can call itself multiple times
954 # if I find nested structures etc.
957 def getCDR3(self,type,name="fred"):
959 pt = type.unalias().kind() # param CDR type
960 pn = name # param name
963 print "XXX getCDR3: kind = " , pt
965 if pt == idltype.tk_ulong:
966 self.get_CDR_ulong(pn)
967 elif pt == idltype.tk_void:
968 self.get_CDR_void(pn)
969 elif pt == idltype.tk_short:
970 self.get_CDR_short(pn)
971 elif pt == idltype.tk_long:
972 self.get_CDR_long(pn)
973 elif pt == idltype.tk_ushort:
974 self.get_CDR_ushort(pn)
975 elif pt == idltype.tk_float:
976 self.get_CDR_float(pn)
977 elif pt == idltype.tk_double:
978 self.get_CDR_double(pn)
979 elif pt == idltype.tk_fixed:
980 self.get_CDR_fixed(type.unalias(),pn)
981 elif pt == idltype.tk_boolean:
982 self.get_CDR_boolean(pn)
983 elif pt == idltype.tk_char:
984 self.get_CDR_char(pn)
985 elif pt == idltype.tk_octet:
986 self.get_CDR_octet(pn)
987 elif pt == idltype.tk_any:
989 elif pt == idltype.tk_string:
990 self.get_CDR_string(pn)
991 elif pt == idltype.tk_wstring:
992 self.get_CDR_wstring(pn)
993 elif pt == idltype.tk_wchar:
994 self.get_CDR_wchar(pn)
995 elif pt == idltype.tk_enum:
997 self.get_CDR_enum(pn,type)
998 #self.get_CDR_enum(pn)
1000 elif pt == idltype.tk_struct:
1001 self.get_CDR_struct(type,pn)
1002 elif pt == idltype.tk_TypeCode: # will I ever get here ?
1003 self.get_CDR_TypeCode(pn)
1004 elif pt == idltype.tk_sequence and \
1005 type.unalias().seqType().kind() == idltype.tk_octet:
1006 self.get_CDR_sequence_octet(type,pn)
1007 elif pt == idltype.tk_sequence:
1008 self.get_CDR_sequence(type,pn)
1009 elif pt == idltype.tk_objref:
1010 self.get_CDR_objref(type,pn)
1011 elif pt == idltype.tk_array:
1012 self.get_CDR_array(type,pn)
1013 elif pt == idltype.tk_union:
1014 self.get_CDR_union(type,pn)
1015 elif pt == idltype.tk_alias:
1017 print "XXXXX Alias type XXXXX " , type
1018 self.get_CDR_alias(type,pn)
1020 self.genWARNING("Unknown typecode = " + '%i ' % pt) # put comment in source code
1024 # get_CDR_XXX methods are here ..
1029 def get_CDR_ulong(self,pn):
1030 self.st.out(self.template_get_CDR_ulong, varname=pn)
1031 self.addvar(self.c_u_octet4)
1033 def get_CDR_short(self,pn):
1034 self.st.out(self.template_get_CDR_short, varname=pn)
1035 self.addvar(self.c_s_octet2)
1037 def get_CDR_void(self,pn):
1038 self.st.out(self.template_get_CDR_void, varname=pn)
1040 def get_CDR_long(self,pn):
1041 self.st.out(self.template_get_CDR_long, varname=pn)
1042 self.addvar(self.c_s_octet4)
1044 def get_CDR_ushort(self,pn):
1045 self.st.out(self.template_get_CDR_ushort, varname=pn)
1046 self.addvar(self.c_u_octet2)
1048 def get_CDR_float(self,pn):
1049 self.st.out(self.template_get_CDR_float, varname=pn)
1050 self.addvar(self.c_float)
1052 def get_CDR_double(self,pn):
1053 self.st.out(self.template_get_CDR_double, varname=pn)
1054 self.addvar(self.c_double)
1056 def get_CDR_boolean(self,pn):
1057 self.st.out(self.template_get_CDR_boolean, varname=pn)
1058 self.addvar(self.c_u_octet1)
1060 def get_CDR_fixed(self,type,pn):
1062 print "XXXX calling get_CDR_fixed, type = ", type
1063 print "XXXX calling get_CDR_fixed, type.digits() = ", type.digits()
1064 print "XXXX calling get_CDR_fixed, type.scale() = ", type.scale()
1066 string_digits = '%i ' % type.digits() # convert int to string
1067 string_scale = '%i ' % type.scale() # convert int to string
1068 string_length = '%i ' % self.dig_to_len(type.digits()) # how many octets to hilight for a number of digits
1070 self.st.out(self.template_get_CDR_fixed, varname=pn, digits=string_digits, scale=string_scale, length=string_length )
1071 self.addvar(self.c_seq)
1074 def get_CDR_char(self,pn):
1075 self.st.out(self.template_get_CDR_char, varname=pn)
1076 self.addvar(self.c_u_octet1)
1078 def get_CDR_octet(self,pn):
1079 self.st.out(self.template_get_CDR_octet, varname=pn)
1080 self.addvar(self.c_u_octet1)
1082 def get_CDR_any(self,pn):
1083 self.st.out(self.template_get_CDR_any, varname=pn)
1085 def get_CDR_enum(self,pn,type):
1086 #self.st.out(self.template_get_CDR_enum, varname=pn)
1087 sname = self.namespace(type.unalias(), "_")
1088 self.st.out(self.template_get_CDR_enum_symbolic, valstringarray=sname)
1091 self.addvar(self.c_u_octet4)
1093 def get_CDR_string(self,pn):
1094 self.st.out(self.template_get_CDR_string, varname=pn)
1095 self.addvar(self.c_u_octet4)
1096 self.addvar(self.c_seq)
1098 def get_CDR_wstring(self,pn):
1099 self.st.out(self.template_get_CDR_wstring, varname=pn)
1100 self.addvar(self.c_u_octet4)
1101 self.addvar(self.c_seq)
1103 def get_CDR_wchar(self,pn):
1104 self.st.out(self.template_get_CDR_wchar, varname=pn)
1105 self.addvar(self.c_s_octet1)
1106 self.addvar(self.c_seq)
1108 def get_CDR_TypeCode(self,pn):
1109 self.st.out(self.template_get_CDR_TypeCode, varname=pn)
1110 self.addvar(self.c_u_octet4)
1112 def get_CDR_objref(self,type,pn):
1113 self.st.out(self.template_get_CDR_object)
1115 def get_CDR_sequence_len(self,pn):
1116 self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1117 self.addvar(self.c_u_octet4)
1121 def get_CDR_union(self,type,pn):
1123 print "XXX Union type =" , type, " pn = ",pn
1124 print "XXX Union type.decl()" , type.decl()
1125 print "XXX Union Scoped Name" , type.scopedName()
1127 # If I am a typedef union {..}; node then find the union node
1129 if isinstance(type.decl(), idlast.Declarator):
1130 ntype = type.decl().alias().aliasType().decl()
1132 ntype = type.decl() # I am a union node
1135 print "XXX Union ntype =" , ntype
1137 sname = self.namespace(ntype, "_")
1138 self.st.out(self.template_union_start, name=sname )
1140 # Output a call to the union helper function so I can handle recursive union also.
1142 self.st.out(self.template_decode_union,name=sname)
1144 self.st.out(self.template_union_end, name=sname )
1148 # Code to generate Union Helper functions
1150 # in: un - a union node
1155 def genUnionHelper(self,un):
1157 print "XXX Union type =" , un
1158 print "XXX Union type.decl()" , un.decl()
1159 print "XXX Union Scoped Name" , un.scopedName()
1161 sname = self.namespace(un, "_")
1162 self.curr_sname = sname # update current opnode/exnode/stnode/unnode scoped name
1163 if not self.fn_hash_built:
1164 self.fn_hash[sname] = [] # init empty list as val for this sname key
1165 # but only if the fn_hash is not already built
1167 self.st.out(self.template_union_helper_function_start, sname=sname, unname=un.repoId())
1168 self.st.inc_indent()
1170 self.st.out(self.template_helper_function_vars_start)
1171 self.dumpCvars(sname)
1172 self.st.out(self.template_helper_function_vars_end )
1174 self.st.out(self.template_union_helper_function_get_endianess)
1176 st = un.switchType().unalias() # may be typedef switch type, so find real type
1178 self.st.out(self.template_comment_union_code_start, uname=un.repoId() )
1180 self.getCDR3(st,un.identifier());
1182 # Depending on what kind of discriminant I come accross (enum,integer,char,
1183 # short, boolean), make sure I cast the return value of the get_XXX accessor
1184 # to an appropriate value. Omniidl idlast.CaseLabel.value() accessor will
1185 # return an integer, or an Enumerator object that is then converted to its
1186 # integer equivalent.
1189 # NOTE - May be able to skip some of this stuff, but leave it in for now -- FS
1192 if (st.kind() == idltype.tk_enum):
1194 self.st.out(self.template_comment_union_code_discriminant, uname=std.repoId() )
1195 self.st.out(self.template_union_code_save_discriminant_enum, discname=un.identifier() )
1196 self.addvar(self.c_s_disc + un.identifier() + ";")
1198 elif (st.kind() == idltype.tk_long):
1199 self.st.out(self.template_union_code_save_discriminant_long, discname=un.identifier() )
1200 self.addvar(self.c_s_disc + un.identifier() + ";")
1202 elif (st.kind() == idltype.tk_ulong):
1203 self.st.out(self.template_union_code_save_discriminant_ulong, discname=un.identifier() )
1204 self.addvar(self.c_s_disc + un.identifier() + ";")
1206 elif (st.kind() == idltype.tk_short):
1207 self.st.out(self.template_union_code_save_discriminant_short, discname=un.identifier() )
1208 self.addvar(self.c_s_disc + un.identifier() + ";")
1210 elif (st.kind() == idltype.tk_ushort):
1211 self.st.out(self.template_union_code_save_discriminant_ushort, discname=un.identifier() )
1212 self.addvar(self.c_s_disc + un.identifier() + ";")
1214 elif (st.kind() == idltype.tk_boolean):
1215 self.st.out(self.template_union_code_save_discriminant_boolean, discname=un.identifier() )
1216 self.addvar(self.c_s_disc + un.identifier() + ";")
1218 elif (st.kind() == idltype.tk_char):
1219 self.st.out(self.template_union_code_save_discriminant_char, discname=un.identifier() )
1220 self.addvar(self.c_s_disc + un.identifier() + ";")
1223 print "XXX Unknown st.kind() = ", st.kind()
1226 # Loop over all cases in this union
1229 for uc in un.cases(): # for all UnionCase objects in this union
1230 for cl in uc.labels(): # for all Caselabel objects in this UnionCase
1232 # get integer value, even if discriminant is
1233 # an Enumerator node
1235 if isinstance(cl.value(),idlast.Enumerator):
1237 print "XXX clv.identifier()", cl.value().identifier()
1238 print "XXX clv.repoId()", cl.value().repoId()
1239 print "XXX clv.scopedName()", cl.value().scopedName()
1241 # find index of enumerator in enum declaration
1242 # eg: RED is index 0 in enum Colors { RED, BLUE, GREEN }
1244 clv = self.valFromEnum(std,cl.value())
1249 #print "XXX clv = ",clv
1252 # if char, dont convert to int, but put inside single quotes so that it is understood by C.
1253 # eg: if (disc == 'b')..
1255 # TODO : handle \xxx chars generically from a function or table lookup rather than
1256 # a whole bunch of "if" statements. -- FS
1259 if (st.kind() == idltype.tk_char):
1260 if (clv == '\n'): # newline
1261 string_clv = "'\\n'"
1262 elif (clv == '\t'): # tab
1263 string_clv = "'\\t'"
1265 string_clv = "'" + clv + "'"
1267 string_clv = '%i ' % clv
1270 # If default case, then skp comparison with discriminator
1273 if not cl.default():
1274 self.st.out(self.template_comment_union_code_label_compare_start, discname=un.identifier(),labelval=string_clv )
1275 self.st.inc_indent()
1277 self.st.out(self.template_comment_union_code_label_default_start )
1280 self.getCDR3(uc.caseType(),uc.declarator().identifier())
1282 if not cl.default():
1283 self.st.dec_indent()
1284 self.st.out(self.template_comment_union_code_label_compare_end )
1286 self.st.out(self.template_comment_union_code_label_default_end )
1288 self.st.dec_indent()
1289 self.st.out(self.template_union_helper_function_end)
1294 # Currently, get_CDR_alias is geared to finding typdef
1297 def get_CDR_alias(self,type,pn):
1299 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1300 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1301 print "XXX get_CDR_alias, type.decl().alias() = " ,type.decl().alias()
1303 decl = type.decl() # get declarator object
1305 if (decl.sizes()): # a typedef array
1306 indices = self.get_indices_from_sizes(decl.sizes())
1307 string_indices = '%i ' % indices # convert int to string
1308 self.st.out(self.template_get_CDR_array_comment, aname=pn, asize=string_indices)
1310 self.st.out(self.template_get_CDR_array_start, aname=pn, aval=string_indices)
1311 self.addvar(self.c_i + pn + ";")
1312 self.st.inc_indent()
1313 self.getCDR3(type.decl().alias().aliasType(), pn )
1315 self.st.dec_indent()
1316 self.st.out(self.template_get_CDR_array_end)
1319 else: # a simple typdef
1321 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1322 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1324 self.getCDR3(type, decl.identifier() )
1332 # Handle structs, including recursive
1335 def get_CDR_struct(self,type,pn):
1337 # If I am a typedef struct {..}; node then find the struct node
1339 if isinstance(type.decl(), idlast.Declarator):
1340 ntype = type.decl().alias().aliasType().decl()
1342 ntype = type.decl() # I am a struct node
1344 sname = self.namespace(ntype, "_")
1345 self.st.out(self.template_structure_start, name=sname )
1347 # Output a call to the struct helper function so I can handle recursive structs also.
1349 self.st.out(self.template_decode_struct,name=sname)
1351 self.st.out(self.template_structure_end, name=sname )
1356 # Generate private helper functions to decode a struct
1358 # in: stnode ( a struct node)
1361 def genStructHelper(self,st):
1363 print "XXX genStructHelper"
1365 sname = self.namespace(st, "_")
1366 self.curr_sname = sname # update current opnode/exnode/stnode scoped name
1367 if not self.fn_hash_built:
1368 self.fn_hash[sname] = [] # init empty list as val for this sname key
1369 # but only if the fn_hash is not already built
1371 self.st.out(self.template_struct_helper_function_start, sname=sname, stname=st.repoId())
1372 self.st.inc_indent()
1374 self.st.out(self.template_helper_function_vars_start)
1375 self.dumpCvars(sname)
1376 self.st.out(self.template_helper_function_vars_end )
1378 self.st.out(self.template_struct_helper_function_get_endianess)
1380 for m in st.members():
1381 for decl in m.declarators():
1382 if decl.sizes(): # an array
1383 indices = self.get_indices_from_sizes(decl.sizes())
1384 string_indices = '%i ' % indices # convert int to string
1385 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
1386 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
1387 self.addvar(self.c_i + decl.identifier() + ";")
1389 self.st.inc_indent()
1390 self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
1391 self.st.dec_indent()
1392 self.st.out(self.template_get_CDR_array_end)
1396 self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
1398 self.st.dec_indent()
1399 self.st.out(self.template_struct_helper_function_end)
1406 # Generate code to access a sequence of a type
1410 def get_CDR_sequence(self,type, pn):
1411 self.st.out(self.template_get_CDR_sequence_length, seqname=pn )
1412 self.st.out(self.template_get_CDR_sequence_loop_start, seqname=pn )
1413 self.addvar(self.c_i_lim + pn + ";" )
1414 self.addvar(self.c_i + pn + ";")
1416 self.st.inc_indent()
1417 self.getCDR3(type.unalias().seqType(), pn ) # and start all over with the type
1418 self.st.dec_indent()
1420 self.st.out(self.template_get_CDR_sequence_loop_end)
1424 # Generate code to access a sequence of octet
1427 def get_CDR_sequence_octet(self,type, pn):
1428 self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1429 self.st.out(self.template_get_CDR_sequence_octet, seqname=pn)
1430 self.addvar(self.c_i_lim + pn + ";")
1431 self.addvar("gchar * binary_seq_" + pn + ";")
1432 self.addvar("gchar * text_seq_" + pn + ";")
1436 # Generate code to access arrays,
1438 # This is handled elsewhere. Arrays are either typedefs or in
1441 # TODO - Remove this
1444 def get_CDR_array(self,type, decl):
1446 print "XXX get_CDR_array called "
1447 print "XXX array size = " ,decl.sizes()
1455 # out - scoped operation name, using sep character instead of "::"
1457 # eg: Penguin::Echo::echoWString => Penguin_Echo_echoWString if sep = "_"
1461 def namespace(self,node,sep):
1462 sname = string.replace(idlutil.ccolonName(node.scopedName()), '::', sep)
1463 #print "XXX namespace: sname = " + sname
1468 # generate code for plugin initialisation
1471 def gen_plugin_register(self):
1472 self.st.out(self.template_plugin_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1475 # generate register_giop_user_module code, and register only
1476 # unique interfaces that contain operations. Also output
1477 # a heuristic register in case we want to use that.
1479 # TODO - make this a command line option
1487 def gen_proto_reg_handoff(self, oplist):
1489 self.st.out(self.template_proto_reg_handoff_start, dissector_name=self.dissname)
1490 self.st.inc_indent()
1492 for iname in self.get_intlist(oplist):
1493 self.st.out(self.template_proto_reg_handoff_body, dissector_name=self.dissname, protocol_name=self.protoname, interface=iname )
1495 self.st.out(self.template_proto_reg_handoff_heuristic, dissector_name=self.dissname, protocol_name=self.protoname)
1496 self.st.dec_indent()
1498 self.st.out(self.template_proto_reg_handoff_end)
1503 # generate proto_register_<protoname> code,
1507 def gen_proto_register(self):
1508 self.st.out(self.template_proto_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1514 # out - a list of unique interface names. This will be used in
1515 # register_giop_user_module(dissect_giop_auto, "TEST IDL", "Penguin/Echo" ); so the operation
1516 # name must be removed from the scope. And we also only want unique interfaces.
1519 def get_intlist(self,oplist):
1520 int_hash = {} # holds a hash of unique interfaces
1522 sc = op.scopedName() # eg: penguin,tux,bite
1523 sc1 = sc[:-1] # drop last entry
1524 sn = idlutil.slashName(sc1) # penguin/tux
1525 if not int_hash.has_key(sn):
1526 int_hash[sn] = 0; # dummy val, but at least key is unique
1528 return int_hash.keys()
1535 # out - a list of exception nodes (unique). This will be used in
1536 # to generate dissect_exception_XXX functions.
1541 def get_exceptionList(self,oplist):
1542 ex_hash = {} # holds a hash of unique exceptions.
1544 for ex in op.raises():
1545 if not ex_hash.has_key(ex):
1546 ex_hash[ex] = 0; # dummy val, but at least key is unique
1548 print "XXX Exception = " + ex.identifier()
1550 return ex_hash.keys()
1555 # Simple function to take a list of array sizes and find the
1556 # total number of elements
1559 # eg: temp[4][3] = 12 elements
1562 def get_indices_from_sizes(self,sizelist):
1570 # Determine how many octets contain requested number
1571 # of digits for an "fixed" IDL type "on the wire"
1574 def dig_to_len(self,dignum):
1575 return (dignum/2) + 1
1580 # Output some TODO comment
1584 def genTODO(self,message):
1585 self.st.out(self.template_debug_TODO, message=message)
1588 # Output some WARNING comment
1592 def genWARNING(self,message):
1593 self.st.out(self.template_debug_WARNING, message=message)
1596 # Templates for C code
1601 template_comment_operations_start = """\
1603 * IDL Operations Start
1609 template_operations_declare = """static const char @sname@_op[] = \"@opname@\" ;"""
1612 template_comment_operations_end = """
1614 * IDL Operations End
1619 template_helper_function_comment = """\
1627 template_helper_function_vars_start = """
1628 /* Operation specific Variable declarations Begin */
1631 template_helper_function_vars_end = """
1632 /* Operation specific Variable declarations End */
1637 template_helper_function_start = """\
1638 static void decode_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header, gchar *operation _U_) {
1640 gboolean stream_is_big_endian; /* big endianess */
1643 template_helper_function_end = """\
1648 # proto_reg_handoff() templates
1652 template_proto_reg_handoff_start = """
1653 /* register me as handler for these interfaces */
1655 void proto_register_handoff_giop_@dissector_name@(void) {
1659 template_proto_reg_handoff_body = """
1662 /* Register for Explicit Dissection */
1664 register_giop_user_module(dissect_@dissector_name@, \"@protocol_name@\", \"@interface@\", proto_@dissector_name@ ); /* explicit dissector */
1670 template_proto_reg_handoff_heuristic = """
1672 /* Register for Heuristic Dissection */
1674 register_giop_user(dissect_@dissector_name@, \"@protocol_name@\" ,proto_@dissector_name@); /* heuristic dissector */
1678 template_proto_reg_handoff_end = """
1684 # Initialize the protocol
1687 template_protocol = """
1688 /* Initialise the protocol and subtree pointers */
1690 static int proto_@dissector_name@ = -1;
1692 static gint ett_@dissector_name@ = -1;
1699 # Initialize the boundary Alignment
1702 template_init_boundary = """
1703 /* Initialise the initial Alignment */
1705 static guint32 boundary = GIOP_HEADER_SIZE; /* initial value */
1714 # Initialize the Registered fields
1717 template_registered_fields = """
1719 /* Initialise the Registered fields */
1721 /* TODO - Use registered fields */
1726 # plugin_register and plugin_reg_handoff templates
1729 template_plugin_register = """
1731 #ifndef ENABLE_STATIC
1733 G_MODULE_EXPORT void
1734 plugin_register(void)
1736 if (proto_@dissector_name@ == -1) {
1737 proto_register_giop_@dissector_name@();
1741 G_MODULE_EXPORT void
1742 plugin_reg_handoff(void){
1743 proto_register_handoff_giop_@dissector_name@();
1751 # proto_register_<dissector name>(void) templates
1755 template_proto_register = """
1757 /* Register the protocol with Ethereal */
1759 void proto_register_giop_@dissector_name@(void) {
1761 /* setup list of header fields */
1764 static hf_register_info hf[] = {
1771 /* setup protocol subtree array */
1773 static gint *ett[] = {
1774 &ett_@dissector_name@,
1777 /* Register the protocol name and description */
1779 proto_@dissector_name@ = proto_register_protocol(\"@description@\" , \"@protocol_name@\", \"giop-@dissector_name@\" );
1782 proto_register_field_array(proto_@dissector_name@, hf, array_length(hf));
1784 proto_register_subtree_array(ett,array_length(ett));
1792 # template for delegation code
1795 template_op_delegate_code = """\
1796 if (strcmp(operation, @sname@_op) == 0) {
1797 tree = start_dissecting(tvb, pinfo, ptree, offset);
1798 decode_@sname@(tvb, pinfo, tree, offset, header, operation);
1804 # Templates for the helper functions
1810 template_helper_switch_msgtype_start = """\
1812 stream_is_big_endian = is_big_endian(header);
1814 switch(header->message_type) {
1817 template_helper_switch_msgtype_default_start = """\
1820 /* Unknown GIOP Exception */
1822 g_warning("Unknown GIOP Message");
1825 template_helper_switch_msgtype_default_end = """\
1831 template_helper_switch_msgtype_end = """\
1833 } /* switch(header->message_type) */
1836 template_helper_switch_msgtype_request_start = """\
1840 template_helper_switch_msgtype_request_end = """\
1844 template_helper_switch_msgtype_reply_start = """\
1848 template_helper_switch_msgtype_reply_no_exception_start = """\
1852 template_helper_switch_msgtype_reply_no_exception_end = """\
1857 template_helper_switch_msgtype_reply_user_exception_start = """\
1858 case USER_EXCEPTION:
1861 template_helper_switch_msgtype_reply_user_exception_end = """\
1865 template_helper_switch_msgtype_reply_default_start = """\
1868 /* Unknown Exception */
1870 g_warning("Unknown Exception ");
1875 template_helper_switch_msgtype_reply_default_end = """\
1880 template_helper_switch_msgtype_reply_end = """\
1884 template_helper_switch_msgtype_default_start = """\
1887 /* Unknown GIOP Message */
1889 g_warning("Unknown GIOP Message");
1893 template_helper_switch_msgtype_default_end = """\
1899 template_helper_switch_rep_status_start = """\
1900 switch(header->rep_status) {
1903 template_helper_switch_rep_status_default_start = """\
1906 /* Unknown Reply Status */
1908 g_warning("Unknown Reply Status");
1912 template_helper_switch_rep_status_default_end = """\
1916 template_helper_switch_rep_status_end = """\
1918 } /* switch(header->message_type) */
1928 # Templates for get_CDR_xxx accessors
1931 template_get_CDR_ulong = """\
1932 u_octet4 = get_CDR_ulong(tvb,offset,stream_is_big_endian, boundary);
1934 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %u",u_octet4);
1938 template_get_CDR_short = """\
1939 s_octet2 = get_CDR_short(tvb,offset,stream_is_big_endian, boundary);
1941 proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %i",s_octet2);
1945 template_get_CDR_void = """\
1947 /* Function returns void */
1951 template_get_CDR_long = """\
1952 s_octet4 = get_CDR_long(tvb,offset,stream_is_big_endian, boundary);
1954 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %i",s_octet4);
1958 template_get_CDR_ushort = """\
1959 u_octet2 = get_CDR_ushort(tvb,offset,stream_is_big_endian, boundary);
1961 proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %u",u_octet2);
1964 template_get_CDR_float = """\
1965 my_float = get_CDR_float(tvb,offset,stream_is_big_endian, boundary);
1967 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %.6e",my_float);
1971 template_get_CDR_double = """\
1972 my_double = get_CDR_double(tvb,offset,stream_is_big_endian, boundary);
1974 proto_tree_add_text(tree,tvb,*offset-8,8,"@varname@ = %.15e",my_double);
1978 template_get_CDR_boolean = """\
1979 u_octet1 = get_CDR_boolean(tvb,offset);
1981 proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1985 template_get_CDR_char = """\
1986 u_octet1 = get_CDR_char(tvb,offset);
1988 proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1992 template_get_CDR_octet = """\
1993 u_octet1 = get_CDR_octet(tvb,offset);
1995 proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
2001 template_get_CDR_any = """\
2002 get_CDR_any(tvb,tree,offset,stream_is_big_endian, boundary, header);
2006 template_get_CDR_fixed = """\
2007 get_CDR_fixed(tvb, &seq, offset, @digits@, @scale@);
2009 proto_tree_add_text(tree,tvb,*offset-@length@, @length@, "@varname@ < @digits@, @scale@> = %s",seq);
2012 g_free(seq); /* free buffer */
2018 template_get_CDR_enum_symbolic = """\
2020 u_octet4 = get_CDR_enum(tvb,offset,stream_is_big_endian, boundary);
2022 proto_tree_add_text(tree,tvb,*offset-4,4,"Enum value = %u (%s)",u_octet4,val_to_str(u_octet4,@valstringarray@,"Unknown Enum Value"));
2028 template_get_CDR_string = """\
2029 u_octet4 = get_CDR_string(tvb, &seq, offset, stream_is_big_endian, boundary);
2031 proto_tree_add_text(tree,tvb,*offset-4-u_octet4,4,"length = %u",u_octet4);
2033 proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ = %s",seq);
2037 g_free(seq); /* free buffer */
2042 template_get_CDR_wstring = """\
2043 u_octet4 = get_CDR_wstring(tvb, &seq, offset, stream_is_big_endian, boundary, header);
2045 proto_tree_add_text(tree,tvb,*offset-4-u_octet4,4,"length = %u",u_octet4);
2047 proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ = %s",seq);
2051 g_free(seq); /* free buffer */
2056 template_get_CDR_wchar = """\
2057 s_octet1 = get_CDR_wchar(tvb, &seq, offset, header);
2060 proto_tree_add_text(tree,tvb,*offset-1-s_octet1,1,"length = %u",s_octet1);
2063 s_octet1 = -s_octet1;
2066 proto_tree_add_text(tree,tvb,*offset-s_octet1,s_octet1,"@varname@ = %s",seq);
2070 g_free(seq); /* free buffer */
2077 template_get_CDR_TypeCode = """\
2078 u_octet4 = get_CDR_typeCode(tvb, tree, offset, stream_is_big_endian, boundary, header);
2082 template_get_CDR_object = """\
2083 get_CDR_object(tvb, pinfo, tree, offset, stream_is_big_endian, boundary);
2088 template_get_CDR_sequence_length = """\
2089 u_octet4_loop_@seqname@ = get_CDR_ulong(tvb, offset, stream_is_big_endian, boundary);
2091 proto_tree_add_text(tree,tvb,*offset-4, 4 ,"Seq length of @seqname@ = %u",u_octet4_loop_@seqname@);
2095 template_get_CDR_sequence_loop_start = """\
2096 for (i_@seqname@=0; i_@seqname@ < u_octet4_loop_@seqname@; i_@seqname@++) {
2098 template_get_CDR_sequence_loop_end = """\
2102 template_get_CDR_sequence_octet = """\
2103 if (u_octet4_loop_@seqname@ > 0 && tree) {
2104 get_CDR_octet_seq(tvb, &binary_seq_@seqname@, offset,
2105 u_octet4_loop_@seqname@);
2106 text_seq_@seqname@ = make_printable_string(binary_seq_@seqname@,
2107 u_octet4_loop_@seqname@);
2108 proto_tree_add_text(tree, tvb, *offset - u_octet4_loop_@seqname@,
2109 u_octet4_loop_@seqname@, \"@seqname@: %s\", text_seq_@seqname@);
2110 g_free(binary_seq_@seqname@);
2111 g_free(text_seq_@seqname@);
2115 template_get_CDR_array_start = """\
2116 for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
2118 template_get_CDR_array_end = """\
2122 template_get_CDR_array_comment = """\
2123 /* Array: @aname@[ @asize@] */
2129 template_structure_start = """\
2130 /* Begin struct \"@name@\" */
2134 template_structure_end = """\
2135 /* End struct \"@name@\" */
2138 template_union_start = """\
2139 /* Begin union \"@name@\" */
2143 template_union_end = """\
2144 /* End union \"@name@\" */
2151 # Program Header Template
2154 template_Header = """\
2155 /* packet-@dissector_name@.c
2156 * Routines for IDL dissection
2158 * Autogenerated from idl2eth
2159 * Copyright 2001 Frank Singleton <frank.singleton@@ericsson.com>
2164 template_ethereal_copyright = """\
2166 * Ethereal - Network traffic analyzer
2168 * Copyright 1999 Gerald Combs
2182 * This program is free software; you can redistribute it and/or
2183 * modify it under the terms of the GNU General Public License
2184 * as published by the Free Software Foundation; either version 2
2185 * of the License, or (at your option) any later version.
2187 * This program is distributed in the hope that it will be useful,
2188 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2189 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2190 * GNU General Public License for more details.
2192 * You should have received a copy of the GNU General Public License
2193 * along with this program; if not, write to the Free Software
2194 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2202 template_Includes = """\
2204 #ifdef HAVE_CONFIG_H
2205 # include "config.h"
2210 #include <gmodule.h>
2214 #include <epan/packet.h>
2215 #include <epan/proto.h>
2216 #include <epan/dissectors/packet-giop.h>
2218 #ifndef ENABLE_STATIC
2219 G_MODULE_EXPORT const gchar version[] = "0.0.1";
2226 # Main dissector entry templates
2229 template_main_dissector_start = """\
2232 * Called once we accept the packet as being for us; it sets the
2233 * Protocol and Info columns and creates the top-level protocol
2236 static proto_tree *start_dissecting(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset) {
2238 proto_item *ti = NULL;
2239 proto_tree *tree = NULL; /* init later, inside if(tree) */
2241 if (check_col(pinfo->cinfo, COL_PROTOCOL))
2242 col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
2245 * Do not clear COL_INFO, as nothing is being written there by
2246 * this dissector yet. So leave it as is from the GIOP dissector.
2247 * TODO: add something useful to COL_INFO
2248 * if (check_col(pinfo->cinfo, COL_INFO))
2249 * col_clear(pinfo->cinfo, COL_INFO);
2253 ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, -1, FALSE);
2254 tree = proto_item_add_subtree(ti, ett_@dissname@);
2259 static gboolean dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, gchar *operation, gchar *idlname _U_) {
2261 gboolean be; /* big endianess */
2262 proto_tree *tree _U_;
2264 be = is_big_endian(header); /* get endianess - TODO use passed in stream_is_big_endian instead ? */
2266 /* If we have a USER Exception, then decode it and return */
2268 if ((header->message_type == Reply) && (header->rep_status == USER_EXCEPTION)) {
2270 return decode_user_exception(tvb, pinfo, ptree, offset, header, operation);
2280 template_main_dissector_switch_msgtype_start = """\
2281 switch(header->message_type) {
2284 template_main_dissector_switch_msgtype_start_request_reply = """\
2290 template_main_dissector_switch_msgtype_end_request_reply = """\
2296 template_main_dissector_switch_msgtype_all_other_msgtype = """\
2300 case CloseConnection:
2303 return FALSE; /* not handled yet */
2306 return FALSE; /* not handled yet */
2313 template_main_dissector_end = """\
2317 } /* End of main dissector */
2327 #-------------------------------------------------------------#
2328 # Exception handling templates #
2329 #-------------------------------------------------------------#
2337 template_exception_helpers_start = """\
2338 /* Begin Exception Helper Functions */
2342 template_exception_helpers_end = """\
2344 /* End Exception Helper Functions */
2351 # Templates for declaration of string constants for user exceptions.
2354 template_comment_user_exceptions_string_declare_start = """\
2355 /* Begin Exception (containing members) String Declare */
2359 template_user_exceptions_declare = """static const char user_exception_@sname@[] = \"@exname@\" ; """
2362 template_comment_user_exceptions_string_declare_end = """\
2364 /* End Exception (containing members) String Declare */
2372 # template for Main delegator for exception handling
2375 template_main_exception_delegator_start = """\
2378 * Main delegator for exception handling
2382 static gboolean decode_user_exception(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *ptree _U_, int *offset _U_, MessageHeader *header, gchar *operation _U_ ) {
2384 gboolean be _U_; /* big endianess */
2385 proto_tree *tree _U_;
2387 if (!header->exception_id)
2394 # template for exception delegation code body
2396 template_ex_delegate_code = """\
2397 if (strcmp(header->exception_id, user_exception_@sname@) == 0) {
2398 tree = start_dissecting(tvb, pinfo, ptree, offset);
2399 decode_ex_@sname@(tvb, pinfo, tree, offset, header, operation); /* @exname@ */
2407 # End of Main delegator for exception handling
2410 template_main_exception_delegator_end = """\
2413 return FALSE; /* user exception not found */
2420 # template for exception helper code
2424 template_exception_helper_function_start = """\
2426 /* Exception = @exname@ */
2428 static void decode_ex_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_) {
2430 gboolean stream_is_big_endian; /* big endianess */
2436 # Template for the helper function
2437 # to get stream endianess from header
2440 template_exception_helper_function_get_endianess = """\
2442 stream_is_big_endian = is_big_endian(header); /* get stream endianess */
2447 template_exception_helper_function_end = """\
2453 # template for struct helper code
2457 template_struct_helper_function_start = """\
2459 /* Struct = @stname@ */
2461 static void decode_@sname@_st(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_) {
2463 gboolean stream_is_big_endian; /* big endianess */
2470 # Template for the helper function
2471 # to get stream endianess from header
2474 template_struct_helper_function_get_endianess = """\
2476 stream_is_big_endian = is_big_endian(header); /* get stream endianess */
2481 template_struct_helper_function_end = """\
2486 # template for union helper code
2490 template_union_helper_function_start = """\
2492 /* Union = @unname@ */
2494 static void decode_@sname@_un(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_) {
2496 gboolean stream_is_big_endian; /* big endianess */
2503 # Template for the helper function
2504 # to get stream endianess from header
2507 template_union_helper_function_get_endianess = """\
2509 stream_is_big_endian = is_big_endian(header); /* get stream endianess */
2514 template_union_helper_function_end = """\
2520 #-------------------------------------------------------------#
2521 # Value string templates #
2522 #-------------------------------------------------------------#
2524 template_value_string_start = """\
2525 static const value_string @valstringname@[] = {
2528 template_value_string_entry = """\
2529 { @intval@, \"@description@\" }, """
2531 template_value_string_end = """\
2539 #-------------------------------------------------------------#
2540 # Enum handling templates #
2541 #-------------------------------------------------------------#
2543 template_comment_enums_start = """\
2550 template_comment_enums_end = """\
2557 template_comment_enum_comment = """\
2566 #-------------------------------------------------------------#
2567 # Attribute handling templates #
2568 #-------------------------------------------------------------#
2571 template_comment_attributes_start = """\
2573 * IDL Attributes Start
2579 # get/set accessor method names are language mapping dependant.
2582 template_attributes_declare_Java_get = """static const char get_@sname@_at[] = \"_get_@atname@\" ;"""
2583 template_attributes_declare_Java_set = """static const char set_@sname@_at[] = \"_set_@atname@\" ;"""
2586 template_comment_attributes_end = """
2588 * IDL Attributes End
2595 # template for Attribute delegation code
2597 # Note: _get_xxx() should only be called for Reply with NO_EXCEPTION
2598 # Note: _set_xxx() should only be called for Request
2602 template_at_delegate_code_get = """\
2603 if (strcmp(operation, get_@sname@_at) == 0 && (header->message_type == Reply) && (header->rep_status == NO_EXCEPTION) ) {
2604 tree = start_dissecting(tvb, pinfo, ptree, offset);
2605 decode_get_@sname@_at(tvb, pinfo, tree, offset, header, operation);
2610 template_at_delegate_code_set = """\
2611 if (strcmp(operation, set_@sname@_at) == 0 && (header->message_type == Request) ) {
2612 tree = start_dissecting(tvb, pinfo, ptree, offset);
2613 decode_set_@sname@_at(tvb, pinfo, tree, offset, header, operation);
2620 template_attribute_helpers_start = """\
2621 /* Begin Attribute Helper Functions */
2625 template_attribute_helpers_end = """\
2627 /* End Attribute Helper Functions */
2632 # template for attribute helper code
2636 template_attribute_helper_function_start = """\
2638 /* Attribute = @atname@ */
2640 static void decode_@sname@_at(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_) {
2642 gboolean stream_is_big_endian; /* big endianess */
2648 # Template for the helper function
2649 # to get stream endianess from header
2652 template_attribute_helper_function_get_endianess = """\
2654 stream_is_big_endian = is_big_endian(header); /* get stream endianess */
2659 template_attribute_helper_function_end = """\
2664 #-------------------------------------------------------------#
2665 # Debugging templates #
2666 #-------------------------------------------------------------#
2670 # Template for outputting TODO "C" comments
2671 # so user know I need ti improve something.
2674 template_debug_TODO = """\
2676 /* TODO - @message@ */
2680 # Template for outputting WARNING "C" comments
2681 # so user know if I have found a problem.
2684 template_debug_WARNING = """\
2686 /* WARNING - @message@ */
2691 #-------------------------------------------------------------#
2692 # IDL Union templates #
2693 #-------------------------------------------------------------#
2697 template_comment_union_code_start = """\
2699 * IDL Union Start - @uname@
2705 template_comment_union_code_end = """
2707 * IDL union End - @uname@
2712 template_comment_union_code_discriminant = """\
2714 * IDL Union - Discriminant - @uname@
2720 # Cast Unions types to something appropriate
2721 # Enum value cast to guint32, all others cast to gint32
2722 # as omniidl accessor returns integer or Enum.
2725 template_union_code_save_discriminant_enum = """\
2726 disc_s_@discname@ = (gint32) u_octet4; /* save Enum Value discriminant and cast to gint32 */
2729 template_union_code_save_discriminant_long = """\
2730 disc_s_@discname@ = (gint32) s_octet4; /* save gint32 discriminant and cast to gint32 */
2733 template_union_code_save_discriminant_ulong = """\
2734 disc_s_@discname@ = (gint32) u_octet4; /* save guint32 discriminant and cast to gint32 */
2737 template_union_code_save_discriminant_short = """\
2738 disc_s_@discname@ = (gint32) s_octet2; /* save gint16 discriminant and cast to gint32 */
2741 template_union_code_save_discriminant_ushort = """\
2742 disc_s_@discname@ = (gint32) u_octet2; /* save guint16 discriminant and cast to gint32 */
2745 template_union_code_save_discriminant_char = """\
2746 disc_s_@discname@ = (gint32) u_octet1; /* save guint1 discriminant and cast to gint32 */
2749 template_union_code_save_discriminant_boolean = """\
2750 disc_s_@discname@ = (gint32) u_octet1; /* save guint1 discriminant and cast to gint32 */
2754 template_comment_union_code_label_compare_start = """\
2755 if (disc_s_@discname@ == @labelval@) {
2758 template_comment_union_code_label_compare_end = """\
2759 return; /* End Compare for this discriminant type */
2765 template_comment_union_code_label_default_start = """
2766 /* Default Union Case Start */
2770 template_comment_union_code_label_default_end = """\
2771 /* Default Union Case End */
2776 # Templates for function prototypes.
2777 # This is used in genDeclares() for declaring function prototypes
2778 # for structs and union helper functions.
2781 template_prototype_start_dissecting = """
2782 static proto_tree *start_dissecting(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset);
2786 template_prototype_struct_start = """
2787 /* Struct prototype declaration Start */
2791 template_prototype_struct_end = """
2792 /* Struct prototype declaration End */
2796 template_prototype_struct_body = """
2798 /* Struct = @stname@ */
2800 static void decode_@name@_st(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_);
2804 template_decode_struct = """
2806 decode_@name@_st(tvb, pinfo, tree, offset, header, operation);
2813 template_prototype_union_start = """
2814 /* Union prototype declaration Start */
2818 template_prototype_union_end = """
2819 /* Union prototype declaration End */
2823 template_prototype_union_body = """
2825 /* Union = @unname@ */
2827 static void decode_@name@_un(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_);
2831 template_decode_union = """
2833 decode_@name@_un(tvb, pinfo, tree, offset, header, operation);