3 # $Id: ethereal_gen.py,v 1.28 2002/08/02 23:35:46 jmayer Exp $
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_init()
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"
295 self.st.out(self.template_prototype_struct_start)
298 sname = self.namespace(st, "_")
300 self.st.out(self.template_prototype_struct_body, stname=st.repoId(),name=sname)
301 self.st.out(self.template_prototype_struct_end)
305 self.st.out(self.template_prototype_union_start)
307 sname = self.namespace(un, "_")
308 self.st.out(self.template_prototype_union_body, unname=un.repoId(),name=sname)
309 self.st.out(self.template_prototype_union_end)
319 def genProtocol(self):
320 self.st.out(self.template_protocol, dissector_name=self.dissname)
321 self.st.out(self.template_init_boundary)
325 # genProtoAndRegisteredFields
329 def genRegisteredFields(self):
330 self.st.out(self.template_registered_fields )
338 def genMainEntryStart(self,oplist):
339 self.st.out(self.template_main_dissector_start, dissname=self.dissname, disprot=self.protoname)
341 self.st.out(self.template_main_dissector_switch_msgtype_start)
342 self.st.out(self.template_main_dissector_switch_msgtype_start_request_reply)
350 def genMainEntryEnd(self):
352 self.st.out(self.template_main_dissector_switch_msgtype_end_request_reply)
354 self.st.out(self.template_main_dissector_switch_msgtype_all_other_msgtype)
356 self.st.out(self.template_main_dissector_end)
364 # out: C code for IDL operations
368 # static const char Penguin_Echo_echoShort_op[] = "echoShort" ;
371 def genOpList(self,oplist):
372 self.st.out(self.template_comment_operations_start)
375 sname = self.namespace(n, "_")
376 opname = n.identifier()
377 self.st.out(self.template_operations_declare, sname=sname, opname=opname)
379 self.st.out(self.template_comment_operations_end)
386 # out: C code for IDL User Exceptions that contain members
390 # static const char user_exception_tux_bad_value[] = "IDL:tux/bad_value:1.0" ;
393 def genExList(self,oplist):
395 self.st.out(self.template_comment_user_exceptions_string_declare_start)
397 exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
401 print "XXX Exception " , ex.repoId()
402 print "XXX Exception Identifier" , ex.identifier()
403 print "XXX Exception Scoped Name" , ex.scopedName()
405 if (ex.members()): # only if has members
406 sname = self.namespace(ex, "_")
408 self.st.out(self.template_user_exceptions_declare, sname=sname, exname=ex.repoId())
410 self.st.out(self.template_comment_user_exceptions_string_declare_end)
417 # out: C code for IDL attribute decalarations.
419 # NOTE: Mapping of attributes to operation(function) names is tricky.
421 # The actual accessor function names are language-mapping specific. The attribute name
422 # is subject to OMG IDL's name scoping rules; the accessor function names are
423 # guaranteed not to collide with any legal operation names specifiable in OMG IDL.
427 # static const char get_Penguin_Echo_get_width_at[] = "get_width" ;
428 # static const char set_Penguin_Echo_set_width_at[] = "set_width" ;
432 # static const char get_Penguin_Echo_get_width_at[] = "_get_width" ;
433 # static const char set_Penguin_Echo_set_width_at[] = "_set_width" ;
435 # TODO: Implement some language dependant templates to handle naming conventions
436 # language <=> attribute. for C, C++. Java etc
438 # OR, just add a runtime GUI option to select language binding for attributes -- FS
442 # ie: def genAtlist(self,atlist,language)
447 def genAtList(self,atlist):
448 self.st.out(self.template_comment_attributes_start)
451 for i in n.declarators(): #
452 sname = self.namespace(i, "_")
453 atname = i.identifier()
454 self.st.out(self.template_attributes_declare_Java_get, sname=sname, atname=atname)
456 self.st.out(self.template_attributes_declare_Java_set, sname=sname, atname=atname)
458 self.st.out(self.template_comment_attributes_end)
466 # out: C code for IDL Enum decalarations using "static const value_string" template
471 def genEnList(self,enlist):
473 self.st.out(self.template_comment_enums_start)
476 sname = self.namespace(enum, "_")
478 self.st.out(self.template_comment_enum_comment, ename=enum.repoId())
479 self.st.out(self.template_value_string_start, valstringname=sname)
480 for enumerator in enum.enumerators():
481 self.st.out(self.template_value_string_entry, intval=str(self.valFromEnum(enum,enumerator)), description=enumerator.identifier())
484 #atname = n.identifier()
485 self.st.out(self.template_value_string_end, valstringname=sname)
487 self.st.out(self.template_comment_enums_end)
499 # genExceptionDelegator
503 # out: C code for User exception delegator
509 def genExceptionDelegator(self,oplist):
511 self.st.out(self.template_main_exception_delegator_start)
514 exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
518 print "XXX Exception " , ex.repoId()
519 print "XXX Exception Identifier" , ex.identifier()
520 print "XXX Exception Scoped Name" , ex.scopedName()
522 if (ex.members()): # only if has members
523 sname = self.namespace(ex, "_")
525 self.st.out(self.template_ex_delegate_code, sname=sname, exname=ex.repoId())
528 self.st.out(self.template_main_exception_delegator_end)
532 # genAttribueHelpers()
534 # Generate private helper functions to decode Attributes.
538 # For readonly attribute - generate get_xxx()
539 # If NOT readonly attribute - also generate set_xxx()
542 def genAttributeHelpers(self,atlist):
544 print "XXX genAttributeHelpers: atlist = ", atlist
546 self.st.out(self.template_attribute_helpers_start)
548 for attrib in atlist:
549 for decl in attrib.declarators():
550 self.genAtHelper(attrib,decl,"get") # get accessor
551 if not attrib.readonly():
552 self.genAtHelper(attrib,decl,"set") # set accessor
554 self.st.out(self.template_attribute_helpers_end)
559 # Generate private helper functions to decode an attribute
561 # in: at - attribute node
562 # in: decl - declarator belonging to this attribute
563 # in: order - to generate a "get" or "set" helper
565 def genAtHelper(self,attrib,decl,order):
567 print "XXX genAtHelper"
569 sname = order + "_" + self.namespace(decl, "_") # must use set or get prefix to avoid collision
570 self.curr_sname = sname # update current opnode/exnode scoped name
572 if not self.fn_hash_built:
573 self.fn_hash[sname] = [] # init empty list as val for this sname key
574 # but only if the fn_hash is not already built
576 self.st.out(self.template_attribute_helper_function_start, sname=sname, atname=decl.repoId())
579 self.st.out(self.template_helper_function_vars_start)
580 self.dumpCvars(sname)
581 self.st.out(self.template_helper_function_vars_end )
583 self.st.out(self.template_exception_helper_function_get_endianess)
586 # TODO - attributes are simple types, so remove array handling
589 if decl.sizes(): # an array
590 indices = self.get_indices_from_sizes(decl.sizes())
591 string_indices = '%i ' % indices # convert int to string
592 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
593 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
594 self.addvar(self.c_i + decl.identifier() + ";")
598 self.getCDR3(attrib.attrType(), decl.identifier() )
601 self.st.out(self.template_get_CDR_array_end)
606 self.getCDR3(attrib.attrType(), decl.identifier() )
609 self.st.out(self.template_attribute_helper_function_end)
614 # genExceptionHelpers()
616 # Generate private helper functions to decode Exceptions used
623 def genExceptionHelpers(self,oplist):
624 exlist = self.get_exceptionList(oplist) # grab list of exception nodes
626 print "XXX genExceptionHelpers: exlist = ", exlist
628 self.st.out(self.template_exception_helpers_start)
630 if (ex.members()): # only if has members
631 #print "XXX Exception = " + ex.identifier()
634 self.st.out(self.template_exception_helpers_end)
640 # Generate private helper functions to decode User Exceptions
642 # in: exnode ( an exception node)
645 def genExHelper(self,ex):
647 print "XXX genExHelper"
649 sname = self.namespace(ex, "_")
650 self.curr_sname = sname # update current opnode/exnode scoped name
651 if not self.fn_hash_built:
652 self.fn_hash[sname] = [] # init empty list as val for this sname key
653 # but only if the fn_hash is not already built
655 self.st.out(self.template_exception_helper_function_start, sname=sname, exname=ex.repoId())
658 self.st.out(self.template_helper_function_vars_start)
659 self.dumpCvars(sname)
660 self.st.out(self.template_helper_function_vars_end )
662 self.st.out(self.template_exception_helper_function_get_endianess)
665 for m in ex.members():
666 #print "XXX genExhelper, member = ", m, "member type = ", m.memberType()
669 for decl in m.declarators():
670 #print "XXX genExhelper, d = ", decl
671 if decl.sizes(): # an array
672 indices = self.get_indices_from_sizes(decl.sizes())
673 string_indices = '%i ' % indices # convert int to string
674 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
675 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
676 self.addvar(self.c_i + decl.identifier() + ";")
679 self.getCDR3(m.memberType(), ex.identifier() + "_" + decl.identifier() )
682 self.st.out(self.template_get_CDR_array_end)
686 self.getCDR3(m.memberType(), ex.identifier() + "_" + decl.identifier() )
689 self.st.out(self.template_exception_helper_function_end)
695 # Generate private helper functions for each IDL operation.
696 # Generate private helper functions for each IDL struct.
697 # Generate private helper functions for each IDL union.
700 # in: oplist, stlist, unlist
704 def genHelpers(self,oplist,stlist,unlist):
706 self.genOperation(op)
708 self.genStructHelper(st)
710 self.genUnionHelper(un)
715 # Generate private helper functions for a specificIDL operation.
720 def genOperation(self,opnode):
722 print "XXX genOperation called"
724 sname = self.namespace(opnode, "_")
725 if not self.fn_hash_built:
726 self.fn_hash[sname] = [] # init empty list as val for this sname key
727 # but only if the fn_hash is not already built
729 self.curr_sname = sname # update current opnode's scoped name
730 opname = opnode.identifier()
732 self.st.out(self.template_helper_function_comment, repoid=opnode.repoId() )
734 self.st.out(self.template_helper_function_start, sname=sname)
737 self.st.out(self.template_helper_function_vars_start)
738 self.dumpCvars(sname)
739 self.st.out(self.template_helper_function_vars_end )
742 self.st.out(self.template_helper_switch_msgtype_start)
744 self.st.out(self.template_helper_switch_msgtype_request_start)
746 self.genOperationRequest(opnode)
747 self.st.out(self.template_helper_switch_msgtype_request_end)
750 self.st.out(self.template_helper_switch_msgtype_reply_start)
753 self.st.out(self.template_helper_switch_rep_status_start)
756 self.st.out(self.template_helper_switch_msgtype_reply_no_exception_start)
758 self.genOperationReply(opnode)
759 self.st.out(self.template_helper_switch_msgtype_reply_no_exception_end)
762 self.st.out(self.template_helper_switch_msgtype_reply_user_exception_start)
764 self.genOpExceptions(opnode)
765 self.st.out(self.template_helper_switch_msgtype_reply_user_exception_end)
768 self.st.out(self.template_helper_switch_msgtype_reply_default_start)
769 self.st.out(self.template_helper_switch_msgtype_reply_default_end)
771 self.st.out(self.template_helper_switch_rep_status_end)
775 self.st.out(self.template_helper_switch_msgtype_default_start)
776 self.st.out(self.template_helper_switch_msgtype_default_end)
778 self.st.out(self.template_helper_switch_msgtype_end)
782 self.st.out(self.template_helper_function_end, sname=sname)
788 # Decode function parameters for a GIOP request message
792 def genOperationRequest(self,opnode):
793 for p in opnode.parameters():
796 print "XXX parameter = " ,p
797 print "XXX parameter type = " ,p.paramType()
798 print "XXX parameter type kind = " ,p.paramType().kind()
800 self.getCDR3(p.paramType(),p.identifier())
804 # Decode function parameters for a GIOP reply message
808 def genOperationReply(self,opnode):
810 rt = opnode.returnType() # get return type
812 print "XXX opnode = " , opnode
813 print "XXX return type = " , rt
814 print "XXX return type.unalias = " , rt.unalias()
815 print "XXX return type.kind() = " , rt.kind();
818 if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
819 #self.getCDR3(rt.decl().alias().aliasType(),"dummy") # return value maybe a typedef
820 #self.get_CDR_alias(rt, "Operation_Return_Value" )
821 self.get_CDR_alias(rt, rt.name() )
824 self.getCDR3(rt, "Operation_Return_Value") # return value is NOT an alias
826 for p in opnode.parameters():
827 if p.is_out(): # out or inout
828 self.getCDR3(p.paramType(),p.identifier())
830 #self.st.dec_indent()
832 def genOpExceptions(self,opnode):
833 for ex in opnode.raises():
836 for m in ex.members():
838 #print m.memberType(), m.memberType().kind()
840 # Delegator for Operations
843 def genOpDelegator(self,oplist):
845 opname = op.identifier()
846 sname = self.namespace(op, "_")
847 self.st.out(self.template_op_delegate_code, sname=sname)
850 # Delegator for Attributes
853 def genAtDelegator(self,atlist):
855 for i in a.declarators():
856 atname = i.identifier()
857 sname = self.namespace(i, "_")
858 self.st.out(self.template_at_delegate_code_get, sname=sname)
860 self.st.out(self.template_at_delegate_code_set, sname=sname)
864 # Add a variable declaration to the hash of list
867 def addvar(self, var):
868 if not ( var in self.fn_hash[self.curr_sname] ):
869 self.fn_hash[self.curr_sname].append(var)
872 # Print the variable declaration from the hash of list
877 for fn in self.fn_hash.keys():
879 for v in self.fn_hash[fn]:
882 # Print the "C" variable declaration from the hash of list
883 # for a given scoped operation name (eg: tux_penguin_eat)
887 def dumpCvars(self, sname):
888 for v in self.fn_hash[sname]:
893 # Given an enum node, and a enumerator node, return
894 # the enumerator's numerical value.
896 # eg: enum Color {red,green,blue} should return
900 def valFromEnum(self,enumNode, enumeratorNode):
902 print "XXX valFromEnum, enumNode = ", enumNode, " from ", enumNode.repoId()
903 print "XXX valFromEnum, enumeratorNode = ", enumeratorNode, " from ", enumeratorNode.repoId()
905 if isinstance(enumeratorNode,idlast.Enumerator):
906 value = enumNode.enumerators().index(enumeratorNode)
935 ## tk_longdouble = 25
942 ## tk_abstract_interface = 32
948 # This is the main "iterator" function. It takes a node, and tries to output
949 # a get_CDR_XXX accessor method(s). It can call itself multiple times
950 # if I find nested structures etc.
953 def getCDR3(self,type,name="fred"):
955 pt = type.unalias().kind() # param CDR type
956 pn = name # param name
959 print "XXX getCDR3: kind = " , pt
961 if pt == idltype.tk_ulong:
962 self.get_CDR_ulong(pn)
963 elif pt == idltype.tk_void:
964 self.get_CDR_void(pn)
965 elif pt == idltype.tk_short:
966 self.get_CDR_short(pn)
967 elif pt == idltype.tk_long:
968 self.get_CDR_long(pn)
969 elif pt == idltype.tk_ushort:
970 self.get_CDR_ushort(pn)
971 elif pt == idltype.tk_float:
972 self.get_CDR_float(pn)
973 elif pt == idltype.tk_double:
974 self.get_CDR_double(pn)
975 elif pt == idltype.tk_fixed:
976 self.get_CDR_fixed(type.unalias(),pn)
977 elif pt == idltype.tk_boolean:
978 self.get_CDR_boolean(pn)
979 elif pt == idltype.tk_char:
980 self.get_CDR_char(pn)
981 elif pt == idltype.tk_octet:
982 self.get_CDR_octet(pn)
983 elif pt == idltype.tk_any:
985 elif pt == idltype.tk_string:
986 self.get_CDR_string(pn)
987 elif pt == idltype.tk_wstring:
988 self.get_CDR_wstring(pn)
989 elif pt == idltype.tk_wchar:
990 self.get_CDR_wchar(pn)
991 elif pt == idltype.tk_enum:
993 self.get_CDR_enum(pn,type)
994 #self.get_CDR_enum(pn)
996 elif pt == idltype.tk_struct:
997 self.get_CDR_struct(type,pn)
998 elif pt == idltype.tk_TypeCode: # will I ever get here ?
999 self.get_CDR_TypeCode(pn)
1000 elif pt == idltype.tk_sequence:
1001 self.get_CDR_sequence(type,pn)
1002 elif pt == idltype.tk_objref:
1003 self.get_CDR_objref(type,pn)
1004 elif pt == idltype.tk_array:
1005 self.get_CDR_array(type,pn)
1006 elif pt == idltype.tk_union:
1007 self.get_CDR_union(type,pn)
1008 elif pt == idltype.tk_alias:
1010 print "XXXXX Alias type XXXXX " , type
1011 self.get_CDR_alias(type,pn)
1013 self.genWARNING("Unknown typecode = " + '%i ' % pt) # put comment in source code
1017 # get_CDR_XXX methods are here ..
1022 def get_CDR_ulong(self,pn):
1023 self.st.out(self.template_get_CDR_ulong, varname=pn)
1024 self.addvar(self.c_u_octet4)
1026 def get_CDR_short(self,pn):
1027 self.st.out(self.template_get_CDR_short, varname=pn)
1028 self.addvar(self.c_s_octet2)
1030 def get_CDR_void(self,pn):
1031 self.st.out(self.template_get_CDR_void, varname=pn)
1033 def get_CDR_long(self,pn):
1034 self.st.out(self.template_get_CDR_long, varname=pn)
1035 self.addvar(self.c_s_octet4)
1037 def get_CDR_ushort(self,pn):
1038 self.st.out(self.template_get_CDR_ushort, varname=pn)
1039 self.addvar(self.c_u_octet2)
1041 def get_CDR_float(self,pn):
1042 self.st.out(self.template_get_CDR_float, varname=pn)
1043 self.addvar(self.c_float)
1045 def get_CDR_double(self,pn):
1046 self.st.out(self.template_get_CDR_double, varname=pn)
1047 self.addvar(self.c_double)
1049 def get_CDR_boolean(self,pn):
1050 self.st.out(self.template_get_CDR_boolean, varname=pn)
1051 self.addvar(self.c_u_octet1)
1053 def get_CDR_fixed(self,type,pn):
1055 print "XXXX calling get_CDR_fixed, type = ", type
1056 print "XXXX calling get_CDR_fixed, type.digits() = ", type.digits()
1057 print "XXXX calling get_CDR_fixed, type.scale() = ", type.scale()
1059 string_digits = '%i ' % type.digits() # convert int to string
1060 string_scale = '%i ' % type.scale() # convert int to string
1061 string_length = '%i ' % self.dig_to_len(type.digits()) # how many octets to hilight for a number of digits
1063 self.st.out(self.template_get_CDR_fixed, varname=pn, digits=string_digits, scale=string_scale, length=string_length )
1064 self.addvar(self.c_seq)
1067 def get_CDR_char(self,pn):
1068 self.st.out(self.template_get_CDR_char, varname=pn)
1069 self.addvar(self.c_u_octet1)
1071 def get_CDR_octet(self,pn):
1072 self.st.out(self.template_get_CDR_octet, varname=pn)
1073 self.addvar(self.c_u_octet1)
1075 def get_CDR_any(self,pn):
1076 self.st.out(self.template_get_CDR_any, varname=pn)
1078 def get_CDR_enum(self,pn,type):
1079 #self.st.out(self.template_get_CDR_enum, varname=pn)
1080 sname = self.namespace(type.unalias(), "_")
1081 self.st.out(self.template_get_CDR_enum_symbolic, valstringarray=sname)
1084 self.addvar(self.c_u_octet4)
1086 def get_CDR_string(self,pn):
1087 self.st.out(self.template_get_CDR_string, varname=pn)
1088 self.addvar(self.c_u_octet4)
1089 self.addvar(self.c_seq)
1091 def get_CDR_wstring(self,pn):
1092 self.st.out(self.template_get_CDR_wstring, varname=pn)
1093 self.addvar(self.c_u_octet4)
1094 self.addvar(self.c_seq)
1096 def get_CDR_wchar(self,pn):
1097 self.st.out(self.template_get_CDR_wchar, varname=pn)
1098 self.addvar(self.c_s_octet1)
1099 self.addvar(self.c_seq)
1101 def get_CDR_TypeCode(self,pn):
1102 self.st.out(self.template_get_CDR_TypeCode, varname=pn)
1103 self.addvar(self.c_u_octet4)
1105 def get_CDR_objref(self,type,pn):
1106 self.st.out(self.template_get_CDR_object)
1108 def get_CDR_sequence_len(self,pn):
1109 self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1110 self.addvar(self.c_u_octet4)
1114 def get_CDR_union(self,type,pn):
1116 print "XXX Union type =" , type, " pn = ",pn
1117 print "XXX Union type.decl()" , type.decl()
1118 print "XXX Union Scoped Name" , type.scopedName()
1120 # If I am a typedef union {..}; node then find the union node
1122 if isinstance(type.decl(), idlast.Declarator):
1123 ntype = type.decl().alias().aliasType().decl()
1125 ntype = type.decl() # I am a union node
1128 print "XXX Union ntype =" , ntype
1130 sname = self.namespace(ntype, "_")
1131 self.st.out(self.template_union_start, name=sname )
1133 # Output a call to the union helper function so I can handle recursive union also.
1135 self.st.out(self.template_decode_union,name=sname)
1137 self.st.out(self.template_union_end, name=sname )
1141 # Code to generate Union Helper functions
1143 # in: un - a union node
1148 def genUnionHelper(self,un):
1150 print "XXX Union type =" , un
1151 print "XXX Union type.decl()" , un.decl()
1152 print "XXX Union Scoped Name" , un.scopedName()
1154 sname = self.namespace(un, "_")
1155 self.curr_sname = sname # update current opnode/exnode/stnode/unnode scoped name
1156 if not self.fn_hash_built:
1157 self.fn_hash[sname] = [] # init empty list as val for this sname key
1158 # but only if the fn_hash is not already built
1160 self.st.out(self.template_union_helper_function_start, sname=sname, unname=un.repoId())
1161 self.st.inc_indent()
1163 self.st.out(self.template_helper_function_vars_start)
1164 self.dumpCvars(sname)
1165 self.st.out(self.template_helper_function_vars_end )
1167 self.st.out(self.template_union_helper_function_get_endianess)
1169 st = un.switchType().unalias() # may be typedef switch type, so find real type
1171 self.st.out(self.template_comment_union_code_start, uname=un.repoId() )
1173 self.getCDR3(st,un.identifier());
1175 # Depending on what kind of discriminant I come accross (enum,integer,char,
1176 # short, boolean), make sure I cast the return value of the get_XXX accessor
1177 # to an appropriate value. Omniidl idlast.CaseLabel.value() accessor will
1178 # return an integer, or an Enumerator object that is then converted to its
1179 # integer equivalent.
1182 # NOTE - May be able to skip some of this stuff, but leave it in for now -- FS
1185 if (st.kind() == idltype.tk_enum):
1187 self.st.out(self.template_comment_union_code_discriminant, uname=std.repoId() )
1188 self.st.out(self.template_union_code_save_discriminant_enum, discname=un.identifier() )
1189 self.addvar(self.c_s_disc + un.identifier() + ";")
1191 elif (st.kind() == idltype.tk_long):
1192 self.st.out(self.template_union_code_save_discriminant_long, discname=un.identifier() )
1193 self.addvar(self.c_s_disc + un.identifier() + ";")
1195 elif (st.kind() == idltype.tk_short):
1196 self.st.out(self.template_union_code_save_discriminant_short, discname=un.identifier() )
1197 self.addvar(self.c_s_disc + un.identifier() + ";")
1199 elif (st.kind() == idltype.tk_boolean):
1200 self.st.out(self.template_union_code_save_discriminant_boolean, discname=un.identifier() )
1201 self.addvar(self.c_s_disc + un.identifier() + ";")
1203 elif (st.kind() == idltype.tk_char):
1204 self.st.out(self.template_union_code_save_discriminant_char, discname=un.identifier() )
1205 self.addvar(self.c_s_disc + un.identifier() + ";")
1208 print "XXX Unknown st.kind() = ", st.kind()
1211 # Loop over all cases in this union
1214 for uc in un.cases(): # for all UnionCase objects in this union
1215 for cl in uc.labels(): # for all Caselabel objects in this UnionCase
1217 # get integer value, even if discriminant is
1218 # an Enumerator node
1220 if isinstance(cl.value(),idlast.Enumerator):
1222 print "XXX clv.identifier()", cl.value().identifier()
1223 print "XXX clv.repoId()", cl.value().repoId()
1224 print "XXX clv.scopedName()", cl.value().scopedName()
1226 # find index of enumerator in enum declaration
1227 # eg: RED is index 0 in enum Colors { RED, BLUE, GREEN }
1229 clv = self.valFromEnum(std,cl.value())
1234 #print "XXX clv = ",clv
1237 # if char, dont convert to int, but put inside single quotes so that it is understood by C.
1238 # eg: if (disc == 'b')..
1240 # TODO : handle \xxx chars generically from a function or table lookup rather than
1241 # a whole bunch of "if" statements. -- FS
1244 if (st.kind() == idltype.tk_char):
1245 if (clv == '\n'): # newline
1246 string_clv = "'\\n'"
1247 elif (clv == '\t'): # tab
1248 string_clv = "'\\t'"
1250 string_clv = "'" + clv + "'"
1252 string_clv = '%i ' % clv
1255 # If default case, then skp comparison with discriminator
1258 if not cl.default():
1259 self.st.out(self.template_comment_union_code_label_compare_start, discname=un.identifier(),labelval=string_clv )
1260 self.st.inc_indent()
1262 self.st.out(self.template_comment_union_code_label_default_start )
1265 self.getCDR3(uc.caseType(),uc.declarator().identifier())
1267 if not cl.default():
1268 self.st.dec_indent()
1269 self.st.out(self.template_comment_union_code_label_compare_end )
1271 self.st.out(self.template_comment_union_code_label_default_end )
1273 self.st.dec_indent()
1274 self.st.out(self.template_union_helper_function_end)
1279 # Currently, get_CDR_alias is geared to finding typdef
1282 def get_CDR_alias(self,type,pn):
1284 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1285 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1286 print "XXX get_CDR_alias, type.decl().alias() = " ,type.decl().alias()
1288 decl = type.decl() # get declarator object
1290 if (decl.sizes()): # a typedef array
1291 indices = self.get_indices_from_sizes(decl.sizes())
1292 string_indices = '%i ' % indices # convert int to string
1293 self.st.out(self.template_get_CDR_array_comment, aname=pn, asize=string_indices)
1295 self.st.out(self.template_get_CDR_array_start, aname=pn, aval=string_indices)
1296 self.addvar(self.c_i + pn + ";")
1297 self.st.inc_indent()
1298 self.getCDR3(type.decl().alias().aliasType(), pn )
1300 self.st.dec_indent()
1301 self.st.out(self.template_get_CDR_array_end)
1304 else: # a simple typdef
1306 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1307 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1309 self.getCDR3(type, decl.identifier() )
1317 # Handle structs, including recursive
1320 def get_CDR_struct(self,type,pn):
1322 # If I am a typedef struct {..}; node then find the struct node
1324 if isinstance(type.decl(), idlast.Declarator):
1325 ntype = type.decl().alias().aliasType().decl()
1327 ntype = type.decl() # I am a struct node
1329 sname = self.namespace(ntype, "_")
1330 self.st.out(self.template_structure_start, name=sname )
1332 # Output a call to the struct helper function so I can handle recursive structs also.
1334 self.st.out(self.template_decode_struct,name=sname)
1336 self.st.out(self.template_structure_end, name=sname )
1341 # Generate private helper functions to decode a struct
1343 # in: stnode ( a struct node)
1346 def genStructHelper(self,st):
1348 print "XXX genStructHelper"
1350 sname = self.namespace(st, "_")
1351 self.curr_sname = sname # update current opnode/exnode/stnode scoped name
1352 if not self.fn_hash_built:
1353 self.fn_hash[sname] = [] # init empty list as val for this sname key
1354 # but only if the fn_hash is not already built
1356 self.st.out(self.template_struct_helper_function_start, sname=sname, stname=st.repoId())
1357 self.st.inc_indent()
1359 self.st.out(self.template_helper_function_vars_start)
1360 self.dumpCvars(sname)
1361 self.st.out(self.template_helper_function_vars_end )
1363 self.st.out(self.template_struct_helper_function_get_endianess)
1365 for m in st.members():
1366 for decl in m.declarators():
1367 if decl.sizes(): # an array
1368 indices = self.get_indices_from_sizes(decl.sizes())
1369 string_indices = '%i ' % indices # convert int to string
1370 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
1371 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
1372 self.addvar(self.c_i + decl.identifier() + ";")
1374 self.st.inc_indent()
1375 self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
1376 self.st.dec_indent()
1377 self.st.out(self.template_get_CDR_array_end)
1381 self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
1383 self.st.dec_indent()
1384 self.st.out(self.template_struct_helper_function_end)
1391 # Generate code to access a sequence of a type
1395 def get_CDR_sequence(self,type, pn):
1396 self.st.out(self.template_get_CDR_sequence_length, seqname=pn )
1397 self.st.out(self.template_get_CDR_sequence_loop_start, seqname=pn )
1398 self.addvar(self.c_i_lim + pn + ";" )
1399 self.addvar(self.c_i + pn + ";")
1401 self.st.inc_indent()
1402 self.getCDR3(type.unalias().seqType(), pn ) # and start all over with the type
1403 self.st.dec_indent()
1405 self.st.out(self.template_get_CDR_sequence_loop_end)
1410 # Generate code to access arrays,
1412 # This is handled elsewhere. Arrays are either typedefs or in
1415 # TODO - Remove this
1418 def get_CDR_array(self,type, decl):
1420 print "XXX get_CDR_array called "
1421 print "XXX array size = " ,decl.sizes()
1429 # out - scoped operation name, using sep character instead of "::"
1431 # eg: Penguin::Echo::echoWString => Penguin_Echo_echoWString if sep = "_"
1435 def namespace(self,node,sep):
1436 sname = string.replace(idlutil.ccolonName(node.scopedName()), '::', sep)
1437 #print "XXX namespace: sname = " + sname
1442 # generate code for plugin initialisation
1445 def gen_plugin_init(self):
1446 self.st.out(self.template_plugin_init, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1449 # generate register_giop_user_module code, and register only
1450 # unique interfaces that contain operations. Also output
1451 # a heuristic register in case we want to use that.
1453 # TODO - make this a command line option
1461 def gen_proto_reg_handoff(self, oplist):
1463 self.st.out(self.template_proto_reg_handoff_start, dissector_name=self.dissname)
1464 self.st.inc_indent()
1466 for iname in self.get_intlist(oplist):
1467 self.st.out(self.template_proto_reg_handoff_body, dissector_name=self.dissname, protocol_name=self.protoname, interface=iname )
1469 self.st.out(self.template_proto_reg_handoff_heuristic, dissector_name=self.dissname, protocol_name=self.protoname)
1470 self.st.dec_indent()
1472 self.st.out(self.template_proto_reg_handoff_end)
1477 # generate proto_register_<protoname> code,
1481 def gen_proto_register(self):
1482 self.st.out(self.template_proto_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1488 # out - a list of unique interface names. This will be used in
1489 # register_giop_user_module(dissect_giop_auto, "TEST IDL", "Penguin/Echo" ); so the operation
1490 # name must be removed from the scope. And we also only want unique interfaces.
1493 def get_intlist(self,oplist):
1494 int_hash = {} # holds a hash of unique interfaces
1496 sc = op.scopedName() # eg: penguin,tux,bite
1497 sc1 = sc[:-1] # drop last entry
1498 sn = idlutil.slashName(sc1) # penguin/tux
1499 if not int_hash.has_key(sn):
1500 int_hash[sn] = 0; # dummy val, but at least key is unique
1502 return int_hash.keys()
1509 # out - a list of exception nodes (unique). This will be used in
1510 # to generate dissect_exception_XXX functions.
1515 def get_exceptionList(self,oplist):
1516 ex_hash = {} # holds a hash of unique exceptions.
1518 for ex in op.raises():
1519 if not ex_hash.has_key(ex):
1520 ex_hash[ex] = 0; # dummy val, but at least key is unique
1522 print "XXX Exception = " + ex.identifier()
1524 return ex_hash.keys()
1529 # Simple function to take a list of array sizes and find the
1530 # total number of elements
1533 # eg: temp[4][3] = 12 elements
1536 def get_indices_from_sizes(self,sizelist):
1544 # Determine how many octets contain requested number
1545 # of digits for an "fixed" IDL type "on the wire"
1548 def dig_to_len(self,dignum):
1549 return (dignum/2) + 1
1554 # Output some TODO comment
1558 def genTODO(self,message):
1559 self.st.out(self.template_debug_TODO, message=message)
1562 # Output some WARNING comment
1566 def genWARNING(self,message):
1567 self.st.out(self.template_debug_WARNING, message=message)
1570 # Templates for C code
1575 template_comment_operations_start = """\
1577 * IDL Operations Start
1583 template_operations_declare = """static const char @sname@_op[] = \"@opname@\" ;"""
1586 template_comment_operations_end = """
1588 * IDL Operations End
1593 template_helper_function_comment = """\
1601 template_helper_function_vars_start = """
1602 /* Operation specific Variable declarations Begin */
1605 template_helper_function_vars_end = """
1606 /* Operation specific Variable declarations End */
1611 template_helper_function_start = """\
1612 static void decode_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header, gchar *operation _U_) {
1614 gboolean stream_is_big_endian; /* big endianess */
1617 template_helper_function_end = """\
1622 # proto_reg_handoff() templates
1626 template_proto_reg_handoff_start = """
1627 /* register me as handler for these interfaces */
1629 void proto_register_handoff_giop_@dissector_name@(void) {
1633 template_proto_reg_handoff_body = """
1636 /* Register for Explicit Dissection */
1638 register_giop_user_module(dissect_@dissector_name@, \"@protocol_name@\", \"@interface@\", proto_@dissector_name@ ); /* explicit dissector */
1644 template_proto_reg_handoff_heuristic = """
1646 /* Register for Heuristic Dissection */
1648 register_giop_user(dissect_@dissector_name@, \"@protocol_name@\" ,proto_@dissector_name@); /* heuristic dissector */
1652 template_proto_reg_handoff_end = """
1658 # Initialize the protocol
1661 template_protocol = """
1662 /* Initialise the protocol and subtree pointers */
1664 static int proto_@dissector_name@ = -1;
1666 static gint ett_@dissector_name@ = -1;
1673 # Initialize the boundary Alignment
1676 template_init_boundary = """
1677 /* Initialise the initial Alignment */
1679 static guint32 boundary = GIOP_HEADER_SIZE; /* initial value */
1688 # Initialize the Registered fields
1691 template_registered_fields = """
1693 /* Initialise the Registered fields */
1695 /* TODO - Use registered fields */
1700 # plugin_init and plugin_reg_handoff templates
1703 template_plugin_init = """
1705 #ifndef __ETHEREAL_STATIC__
1707 G_MODULE_EXPORT void
1708 plugin_reg_handoff(void){
1709 proto_register_handoff_giop_@dissector_name@();
1712 G_MODULE_EXPORT void
1713 plugin_init(plugin_address_table_t *pat
1714 #ifndef PLUGINS_NEED_ADDRESS_TABLE
1718 /* initialise the table of pointers needed in Win32 DLLs */
1719 plugin_address_table_init(pat);
1720 if (proto_@dissector_name@ == -1) {
1721 proto_register_giop_@dissector_name@();
1730 # proto_register_<dissector name>(void) templates
1734 template_proto_register = """
1736 /* Register the protocol with Ethereal */
1738 void proto_register_giop_@dissector_name@(void) {
1740 /* setup list of header fields */
1743 static hf_register_info hf[] = {
1750 /* setup protocol subtree array */
1752 static gint *ett[] = {
1753 &ett_@dissector_name@,
1756 /* Register the protocol name and description */
1758 proto_@dissector_name@ = proto_register_protocol(\"@description@\" , \"@protocol_name@\", \"giop-@dissector_name@\" );
1761 proto_register_field_array(proto_@dissector_name@, hf, array_length(hf));
1763 proto_register_subtree_array(ett,array_length(ett));
1771 # template for delegation code
1774 template_op_delegate_code = """\
1775 if (!strcmp(operation, @sname@_op )) {
1776 decode_@sname@(tvb, pinfo, tree, offset, header, operation);
1782 # Templates for the helper functions
1788 template_helper_switch_msgtype_start = """\
1790 stream_is_big_endian = is_big_endian(header);
1792 switch(header->message_type) {
1795 template_helper_switch_msgtype_default_start = """\
1798 /* Unknown GIOP Exception */
1800 g_warning("Unknown GIOP Message");
1803 template_helper_switch_msgtype_default_end = """\
1809 template_helper_switch_msgtype_end = """\
1811 } /* switch(header->message_type) */
1814 template_helper_switch_msgtype_request_start = """\
1818 template_helper_switch_msgtype_request_end = """\
1822 template_helper_switch_msgtype_reply_start = """\
1826 template_helper_switch_msgtype_reply_no_exception_start = """\
1830 template_helper_switch_msgtype_reply_no_exception_end = """\
1835 template_helper_switch_msgtype_reply_user_exception_start = """\
1836 case USER_EXCEPTION:
1839 template_helper_switch_msgtype_reply_user_exception_end = """\
1843 template_helper_switch_msgtype_reply_default_start = """\
1846 /* Unknown Exception */
1848 g_warning("Unknown Exception ");
1853 template_helper_switch_msgtype_reply_default_end = """\
1858 template_helper_switch_msgtype_reply_end = """\
1862 template_helper_switch_msgtype_default_start = """\
1865 /* Unknown GIOP Message */
1867 g_warning("Unknown GIOP Message");
1871 template_helper_switch_msgtype_default_end = """\
1877 template_helper_switch_rep_status_start = """\
1878 switch(header->rep_status) {
1881 template_helper_switch_rep_status_default_start = """\
1884 /* Unknown Reply Status */
1886 g_warning("Unknown Reply Status");
1890 template_helper_switch_rep_status_default_end = """\
1894 template_helper_switch_rep_status_end = """\
1896 } /* switch(header->message_type) */
1906 # Templates for get_CDR_xxx accessors
1909 template_get_CDR_ulong = """\
1910 u_octet4 = get_CDR_ulong(tvb,offset,stream_is_big_endian, boundary);
1912 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %u",u_octet4);
1916 template_get_CDR_short = """\
1917 s_octet2 = get_CDR_short(tvb,offset,stream_is_big_endian, boundary);
1919 proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %i",s_octet2);
1923 template_get_CDR_void = """\
1925 /* Function returns void */
1929 template_get_CDR_long = """\
1930 s_octet4 = get_CDR_long(tvb,offset,stream_is_big_endian, boundary);
1932 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %i",s_octet4);
1936 template_get_CDR_ushort = """\
1937 u_octet2 = get_CDR_ushort(tvb,offset,stream_is_big_endian, boundary);
1939 proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %u",u_octet2);
1942 template_get_CDR_float = """\
1943 my_float = get_CDR_float(tvb,offset,stream_is_big_endian, boundary);
1945 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %.6e",my_float);
1949 template_get_CDR_double = """\
1950 my_double = get_CDR_double(tvb,offset,stream_is_big_endian, boundary);
1952 proto_tree_add_text(tree,tvb,*offset-8,8,"@varname@ = %.15e",my_double);
1956 template_get_CDR_boolean = """\
1957 u_octet1 = get_CDR_boolean(tvb,offset);
1959 proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1963 template_get_CDR_char = """\
1964 u_octet1 = get_CDR_char(tvb,offset);
1966 proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1970 template_get_CDR_octet = """\
1971 u_octet1 = get_CDR_octet(tvb,offset);
1973 proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1979 template_get_CDR_any = """\
1980 get_CDR_any(tvb,tree,offset,stream_is_big_endian, boundary, header);
1984 template_get_CDR_fixed = """\
1985 get_CDR_fixed(tvb, &seq, offset, @digits@, @scale@);
1987 proto_tree_add_text(tree,tvb,*offset-@length@, @length@, "@varname@ < @digits@, @scale@> = %s",seq);
1990 g_free(seq); /* free buffer */
1996 template_get_CDR_enum_symbolic = """\
1998 u_octet4 = get_CDR_enum(tvb,offset,stream_is_big_endian, boundary);
2000 proto_tree_add_text(tree,tvb,*offset-4,4,"Enum value = %u (%s)",u_octet4,val_to_str(u_octet4,@valstringarray@,"Unknown Enum Value"));
2006 template_get_CDR_string = """\
2007 u_octet4 = get_CDR_string(tvb, &seq, offset, stream_is_big_endian, boundary);
2009 proto_tree_add_text(tree,tvb,*offset-4-u_octet4,4,"length = %u",u_octet4);
2011 proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ = %s",seq);
2015 g_free(seq); /* free buffer */
2020 template_get_CDR_wstring = """\
2021 u_octet4 = get_CDR_wstring(tvb, &seq, offset, stream_is_big_endian, boundary, header);
2023 proto_tree_add_text(tree,tvb,*offset-4-u_octet4,4,"length = %u",u_octet4);
2025 proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ = %s",seq);
2029 g_free(seq); /* free buffer */
2034 template_get_CDR_wchar = """\
2035 s_octet1 = get_CDR_wchar(tvb, &seq, offset, header);
2038 proto_tree_add_text(tree,tvb,*offset-1-s_octet1,1,"length = %u",s_octet1);
2041 s_octet1 = -s_octet1;
2044 proto_tree_add_text(tree,tvb,*offset-s_octet1,s_octet1,"@varname@ = %s",seq);
2048 g_free(seq); /* free buffer */
2055 template_get_CDR_TypeCode = """\
2056 u_octet4 = get_CDR_typeCode(tvb, tree, offset, stream_is_big_endian, boundary, header);
2060 template_get_CDR_object = """\
2061 get_CDR_object(tvb, pinfo, tree, offset, stream_is_big_endian, boundary);
2066 template_get_CDR_sequence_length = """\
2067 u_octet4_loop_@seqname@ = get_CDR_ulong(tvb, offset, stream_is_big_endian, boundary);
2069 proto_tree_add_text(tree,tvb,*offset-4, 4 ,"Seq length of @seqname@ = %u",u_octet4_loop_@seqname@);
2073 template_get_CDR_sequence_loop_start = """\
2074 for (i_@seqname@=0; i_@seqname@ < u_octet4_loop_@seqname@; i_@seqname@++) {
2076 template_get_CDR_sequence_loop_end = """\
2082 template_get_CDR_array_start = """\
2083 for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
2085 template_get_CDR_array_end = """\
2089 template_get_CDR_array_comment = """\
2090 /* Array: @aname@[ @asize@] */
2096 template_structure_start = """\
2097 /* Begin struct \"@name@\" */
2101 template_structure_end = """\
2102 /* End struct \"@name@\" */
2105 template_union_start = """\
2106 /* Begin union \"@name@\" */
2110 template_union_end = """\
2111 /* End union \"@name@\" */
2118 # Program Header Template
2121 template_Header = """\
2122 /* packet-@dissector_name@.c
2123 * Routines for IDL dissection
2125 * Autogenerated from idl2eth
2126 * Copyright 2001 Frank Singleton <frank.singleton@@ericsson.com>
2131 template_ethereal_copyright = """\
2133 * Ethereal - Network traffic analyzer
2135 * Copyright 1999 Gerald Combs
2149 * This program is free software; you can redistribute it and/or
2150 * modify it under the terms of the GNU General Public License
2151 * as published by the Free Software Foundation; either version 2
2152 * of the License, or (at your option) any later version.
2154 * This program is distributed in the hope that it will be useful,
2155 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2156 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2157 * GNU General Public License for more details.
2159 * You should have received a copy of the GNU General Public License
2160 * along with this program; if not, write to the Free Software
2161 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2170 template_Includes = """\
2172 #ifdef HAVE_CONFIG_H
2173 # include "config.h"
2176 #include "plugins/plugin_api.h"
2180 #include <gmodule.h>
2182 #ifdef NEED_SNPRINTF_H
2183 # include "snprintf.h"
2188 #include <epan/packet.h>
2189 #include <epan/proto.h>
2190 #include "packet-giop.h"
2192 #include "plugins/plugin_api_defs.h"
2194 #ifndef __ETHEREAL_STATIC__
2195 G_MODULE_EXPORT const gchar version[] = "0.0.1";
2202 # Main dissector entry templates
2205 template_main_dissector_start = """\
2206 static gboolean dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, gchar *operation, gchar *idlname _U_) {
2208 proto_item *ti = NULL;
2209 proto_tree *tree = NULL; /* init later, inside if(tree) */
2211 gboolean be; /* big endianess */
2213 if (check_col(pinfo->cinfo, COL_PROTOCOL))
2214 col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
2217 * Do not clear COL_INFO, as nothing is being written there by
2218 * this dissector yet. So leave it as is from the GIOP dissector.
2219 * TODO: add something useful to COL_INFO
2220 * if (check_col(pinfo->cinfo, COL_INFO))
2221 * col_clear(pinfo->cinfo, COL_INFO);
2225 ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, tvb_length(tvb) - *offset, FALSE);
2226 tree = proto_item_add_subtree(ti, ett_@dissname@);
2230 be = is_big_endian(header); /* get endianess - TODO use passed in stream_is_big_endian instead ? */
2232 /* If we have a USER Exception, then decode it and return */
2234 if ((header->message_type == Reply) && (header->rep_status == USER_EXCEPTION)) {
2236 return decode_user_exception(tvb, pinfo, tree, offset, header, operation);
2246 template_main_dissector_switch_msgtype_start = """\
2247 switch(header->message_type) {
2250 template_main_dissector_switch_msgtype_start_request_reply = """\
2256 template_main_dissector_switch_msgtype_end_request_reply = """\
2262 template_main_dissector_switch_msgtype_all_other_msgtype = """\
2266 case CloseConnection:
2269 return FALSE; /* not handled yet */
2272 return FALSE; /* not handled yet */
2279 template_main_dissector_end = """\
2283 } /* End of main dissector */
2293 #-------------------------------------------------------------#
2294 # Exception handling templates #
2295 #-------------------------------------------------------------#
2303 template_exception_helpers_start = """\
2304 /* Begin Exception Helper Functions */
2308 template_exception_helpers_end = """\
2310 /* End Exception Helper Functions */
2317 # Templates for declaration of string constants for user exceptions.
2320 template_comment_user_exceptions_string_declare_start = """\
2321 /* Begin Exception (containing members) String Declare */
2325 template_user_exceptions_declare = """static const char user_exception_@sname@[] = \"@exname@\" ; """
2328 template_comment_user_exceptions_string_declare_end = """\
2330 /* End Exception (containing members) String Declare */
2338 # template for Main delegator for exception handling
2341 template_main_exception_delegator_start = """\
2344 * Main delegator for exception handling
2348 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_ ) {
2350 gboolean be _U_; /* big endianess */
2357 # template for exception delegation code body
2359 template_ex_delegate_code = """\
2360 if (!strcmp(header->exception_id, user_exception_@sname@ )) {
2361 decode_ex_@sname@(tvb, pinfo, tree, offset, header, operation); /* @exname@ */
2369 # End of Main delegator for exception handling
2372 template_main_exception_delegator_end = """\
2375 return FALSE; /* user exception not found */
2382 # template for exception helper code
2386 template_exception_helper_function_start = """\
2388 /* Exception = @exname@ */
2390 static void decode_ex_@sname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2392 gboolean stream_is_big_endian; /* big endianess */
2398 # Template for the helper function
2399 # to get stream endianess from header
2402 template_exception_helper_function_get_endianess = """\
2404 stream_is_big_endian = is_big_endian(header); /* get stream endianess */
2409 template_exception_helper_function_end = """\
2415 # template for struct helper code
2419 template_struct_helper_function_start = """\
2421 /* Struct = @stname@ */
2423 static void decode_@sname@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2425 gboolean stream_is_big_endian; /* big endianess */
2432 # Template for the helper function
2433 # to get stream endianess from header
2436 template_struct_helper_function_get_endianess = """\
2438 stream_is_big_endian = is_big_endian(header); /* get stream endianess */
2443 template_struct_helper_function_end = """\
2448 # template for union helper code
2452 template_union_helper_function_start = """\
2454 /* Union = @unname@ */
2456 static void decode_@sname@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2458 gboolean stream_is_big_endian; /* big endianess */
2465 # Template for the helper function
2466 # to get stream endianess from header
2469 template_union_helper_function_get_endianess = """\
2471 stream_is_big_endian = is_big_endian(header); /* get stream endianess */
2476 template_union_helper_function_end = """\
2482 #-------------------------------------------------------------#
2483 # Value string templates #
2484 #-------------------------------------------------------------#
2486 template_value_string_start = """\
2487 static const value_string @valstringname@[] = {
2490 template_value_string_entry = """\
2491 { @intval@, \"@description@\" }, """
2493 template_value_string_end = """\
2501 #-------------------------------------------------------------#
2502 # Enum handling templates #
2503 #-------------------------------------------------------------#
2505 template_comment_enums_start = """\
2512 template_comment_enums_end = """\
2519 template_comment_enum_comment = """\
2528 #-------------------------------------------------------------#
2529 # Attribute handling templates #
2530 #-------------------------------------------------------------#
2533 template_comment_attributes_start = """\
2535 * IDL Attributes Start
2541 # get/set accessor method names are language mapping dependant.
2544 template_attributes_declare_Java_get = """static const char get_@sname@_at[] = \"_get_@atname@\" ;"""
2545 template_attributes_declare_Java_set = """static const char set_@sname@_at[] = \"_set_@atname@\" ;"""
2548 template_comment_attributes_end = """
2550 * IDL Attributes End
2557 # template for Attribute delegation code
2559 # Note: _get_xxx() should only be called for Reply with NO_EXCEPTION
2560 # Note: _set_xxx() should only be called for Request
2564 template_at_delegate_code_get = """\
2565 if (!strcmp(operation, get_@sname@_at ) && (header->message_type == Reply) && (header->rep_status == NO_EXCEPTION) ) {
2566 decode_get_@sname@_at(tvb, pinfo, tree, offset, header, operation);
2571 template_at_delegate_code_set = """\
2572 if (!strcmp(operation, set_@sname@_at ) && (header->message_type == Request) ) {
2573 decode_set_@sname@_at(tvb, pinfo, tree, offset, header, operation);
2580 template_attribute_helpers_start = """\
2581 /* Begin Attribute Helper Functions */
2585 template_attribute_helpers_end = """\
2587 /* End Attribute Helper Functions */
2592 # template for attribute helper code
2596 template_attribute_helper_function_start = """\
2598 /* Attribute = @atname@ */
2600 static void decode_@sname@_at(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2602 gboolean stream_is_big_endian; /* big endianess */
2608 # Template for the helper function
2609 # to get stream endianess from header
2612 template_attribute_helper_function_get_endianess = """\
2614 stream_is_big_endian = is_big_endian(header); /* get stream endianess */
2619 template_attribute_helper_function_end = """\
2624 #-------------------------------------------------------------#
2625 # Debugging templates #
2626 #-------------------------------------------------------------#
2630 # Template for outputting TODO "C" comments
2631 # so user know I need ti improve something.
2634 template_debug_TODO = """\
2636 /* TODO - @message@ */
2640 # Template for outputting WARNING "C" comments
2641 # so user know if I have found a problem.
2644 template_debug_WARNING = """\
2646 /* WARNING - @message@ */
2651 #-------------------------------------------------------------#
2652 # IDL Union templates #
2653 #-------------------------------------------------------------#
2657 template_comment_union_code_start = """\
2659 * IDL Union Start - @uname@
2665 template_comment_union_code_end = """
2667 * IDL union End - @uname@
2672 template_comment_union_code_discriminant = """\
2674 * IDL Union - Discriminant - @uname@
2680 # Cast Unions types to something appropriate
2681 # Enum value cast to guint32, all others cast to gint32
2682 # as omniidl accessor returns integer or Enum.
2685 template_union_code_save_discriminant_enum = """\
2686 disc_s_@discname@ = (gint32) u_octet4; /* save Enum Value discriminant and cast to gint32 */
2689 template_union_code_save_discriminant_long = """\
2690 disc_s_@discname@ = (gint32) s_octet4; /* save gint32 discriminant and cast to gint32 */
2693 template_union_code_save_discriminant_short = """\
2694 disc_s_@discname@ = (gint32) s_octet2; /* save gint16 discriminant and cast to gint32 */
2697 template_union_code_save_discriminant_char = """\
2698 disc_s_@discname@ = (gint32) u_octet1; /* save guint1 discriminant and cast to gint32 */
2701 template_union_code_save_discriminant_boolean = """\
2702 disc_s_@discname@ = (gint32) u_octet1; /* save guint1 discriminant and cast to gint32 */
2706 template_comment_union_code_label_compare_start = """\
2707 if (disc_s_@discname@ == @labelval@) {
2710 template_comment_union_code_label_compare_end = """\
2711 return; /* End Compare for this discriminant type */
2717 template_comment_union_code_label_default_start = """
2718 /* Default Union Case Start */
2722 template_comment_union_code_label_default_end = """\
2723 /* Default Union Case End */
2728 # Templates for function prototypes.
2729 # This is used in genDeclares() for declaring function prototypes
2730 # for structs and union helper functions.
2733 template_prototype_struct_start = """
2734 /* Struct prototype declaration Start */
2738 template_prototype_struct_end = """
2739 /* Struct prototype declaration End */
2743 template_prototype_struct_body = """
2745 /* Struct = @stname@ */
2747 static void decode_@name@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
2751 template_decode_struct = """
2753 decode_@name@_st(tvb, pinfo, tree, offset, header, operation);
2760 template_prototype_union_start = """
2761 /* Union prototype declaration Start */
2765 template_prototype_union_end = """
2766 /* Union prototype declaration End */
2770 template_prototype_union_body = """
2772 /* Union = @unname@ */
2774 static void decode_@name@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
2778 template_decode_union = """
2780 decode_@name@_un(tvb, pinfo, tree, offset, header, operation);