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
130 c_u_octet8 = "guint64 u_octet8;"
131 c_s_octet8 = "gint64 s_octet8;"
132 c_u_octet4 = "guint32 u_octet4;"
133 c_s_octet4 = "gint32 s_octet4;"
134 c_u_octet2 = "guint16 u_octet2;"
135 c_s_octet2 = "gint16 s_octet2;"
136 c_u_octet1 = "guint8 u_octet1;"
137 c_s_octet1 = "gint8 s_octet1;"
139 c_float = "gfloat my_float;"
140 c_double = "gdouble my_double;"
142 c_seq = "gchar *seq = NULL;" # pointer to buffer of gchars
143 c_i = "guint32 i_"; # loop index
144 c_i_lim = "guint32 u_octet4_loop_"; # loop limit
145 c_u_disc = "guint32 disc_u_"; # unsigned int union discriminant variable name (enum)
146 c_s_disc = "gint32 disc_s_"; # signed int union discriminant variable name (other cases, except Enum)
152 def __init__(self, st, protocol_name, dissector_name ,description):
153 self.st = output.Stream(tempfile.TemporaryFile(),4) # for first pass only
155 self.st_save = st # where 2nd pass should go
156 self.protoname = protocol_name # Protocol Name (eg: ECHO)
157 self.dissname = dissector_name # Dissector name (eg: echo)
158 self.description = description # Detailed Protocol description (eg: Echo IDL Example)
159 self.exlist = [] # list of exceptions used in operations.
160 #self.curr_sname # scoped name of current opnode or exnode I am visiting, used for generating "C" var declares
161 self.fn_hash = {} # top level hash to contain key = function/exception and val = list of variable declarations
163 self.fn_hash_built = 0 # flag to indicate the 1st pass is complete, and the fn_hash is correctly
164 # populated with operations/vars and exceptions/vars
170 # Main entry point, controls sequence of
175 def genCode(self,oplist, atlist, enlist, stlist, unlist): # operation,attribute,enums,struct and union lists
178 self.genHelpers(oplist,stlist,unlist) # sneaky .. call it now, to populate the fn_hash
179 # so when I come to that operation later, I have the variables to
182 self.genExceptionHelpers(oplist) # sneaky .. call it now, to populate the fn_hash
183 # so when I come to that exception later, I have the variables to
186 self.genAttributeHelpers(atlist) # sneaky .. call it now, to populate the fn_hash
187 # so when I come to that exception later, I have the variables to
191 self.fn_hash_built = 1 # DONE, so now I know , see genOperation()
193 self.st = self.st_save
194 self.genHeader() # initial dissector comments
195 self.genEthCopyright() # Ethereal Copyright comments.
196 self.genGPL() # GPL license
198 self.genDeclares(oplist,atlist,enlist,stlist,unlist)
200 self.genRegisteredFields()
201 self.genOpList(oplist) # string constant declares for operation names
202 self.genExList(oplist) # string constant declares for user exceptions
203 self.genAtList(atlist) # string constant declares for Attributes
204 self.genEnList(enlist) # string constant declares for Enums
207 self.genExceptionHelpers(oplist) # helper function to decode user exceptions that have members
208 self.genExceptionDelegator(oplist) # finds the helper function to decode a user exception
209 self.genAttributeHelpers(atlist) # helper function to decode "attributes"
211 self.genHelpers(oplist,stlist,unlist) # operation, struct and union decode helper functions
213 self.genMainEntryStart(oplist)
214 self.genOpDelegator(oplist)
215 self.genAtDelegator(atlist)
216 self.genMainEntryEnd()
218 self.gen_proto_register()
219 self.gen_proto_reg_handoff(oplist)
220 self.gen_plugin_register()
222 #self.dumpvars() # debug
229 # Generate Standard Ethereal Header Comments
234 self.st.out(self.template_Header,dissector_name=self.dissname)
236 print "XXX genHeader"
244 # Ethereal Copyright Info
248 def genEthCopyright(self):
250 print "XXX genEthCopyright"
251 self.st.out(self.template_ethereal_copyright)
265 self.st.out(self.template_GPL)
274 def genIncludes(self):
276 print "XXX genIncludes"
278 self.st.out(self.template_Includes)
284 # generate function prototypes if required
286 # Currently this is used for struct and union helper function declarations.
290 def genDeclares(self,oplist,atlist,enlist,stlist,unlist):
292 print "XXX genDeclares"
294 # prototype for start_dissecting()
296 self.st.out(self.template_prototype_start_dissecting)
300 self.st.out(self.template_prototype_struct_start)
303 sname = self.namespace(st, "_")
305 self.st.out(self.template_prototype_struct_body, stname=st.repoId(),name=sname)
306 self.st.out(self.template_prototype_struct_end)
310 self.st.out(self.template_prototype_union_start)
312 sname = self.namespace(un, "_")
313 self.st.out(self.template_prototype_union_body, unname=un.repoId(),name=sname)
314 self.st.out(self.template_prototype_union_end)
324 def genProtocol(self):
325 self.st.out(self.template_protocol, dissector_name=self.dissname)
326 self.st.out(self.template_init_boundary)
330 # genProtoAndRegisteredFields
334 def genRegisteredFields(self):
335 self.st.out(self.template_registered_fields )
343 def genMainEntryStart(self,oplist):
344 self.st.out(self.template_main_dissector_start, dissname=self.dissname, disprot=self.protoname)
346 self.st.out(self.template_main_dissector_switch_msgtype_start)
347 self.st.out(self.template_main_dissector_switch_msgtype_start_request_reply)
355 def genMainEntryEnd(self):
357 self.st.out(self.template_main_dissector_switch_msgtype_end_request_reply)
359 self.st.out(self.template_main_dissector_switch_msgtype_all_other_msgtype)
361 self.st.out(self.template_main_dissector_end)
369 # out: C code for IDL operations
373 # static const char Penguin_Echo_echoShort_op[] = "echoShort" ;
376 def genOpList(self,oplist):
377 self.st.out(self.template_comment_operations_start)
380 sname = self.namespace(n, "_")
381 opname = n.identifier()
382 self.st.out(self.template_operations_declare, sname=sname, opname=opname)
384 self.st.out(self.template_comment_operations_end)
391 # out: C code for IDL User Exceptions that contain members
395 # static const char user_exception_tux_bad_value[] = "IDL:tux/bad_value:1.0" ;
398 def genExList(self,oplist):
400 self.st.out(self.template_comment_user_exceptions_string_declare_start)
402 exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
406 print "XXX Exception " , ex.repoId()
407 print "XXX Exception Identifier" , ex.identifier()
408 print "XXX Exception Scoped Name" , ex.scopedName()
410 if (ex.members()): # only if has members
411 sname = self.namespace(ex, "_")
413 self.st.out(self.template_user_exceptions_declare, sname=sname, exname=ex.repoId())
415 self.st.out(self.template_comment_user_exceptions_string_declare_end)
422 # out: C code for IDL attribute decalarations.
424 # NOTE: Mapping of attributes to operation(function) names is tricky.
426 # The actual accessor function names are language-mapping specific. The attribute name
427 # is subject to OMG IDL's name scoping rules; the accessor function names are
428 # guaranteed not to collide with any legal operation names specifiable in OMG IDL.
432 # static const char get_Penguin_Echo_get_width_at[] = "get_width" ;
433 # static const char set_Penguin_Echo_set_width_at[] = "set_width" ;
437 # static const char get_Penguin_Echo_get_width_at[] = "_get_width" ;
438 # static const char set_Penguin_Echo_set_width_at[] = "_set_width" ;
440 # TODO: Implement some language dependant templates to handle naming conventions
441 # language <=> attribute. for C, C++. Java etc
443 # OR, just add a runtime GUI option to select language binding for attributes -- FS
447 # ie: def genAtlist(self,atlist,language)
452 def genAtList(self,atlist):
453 self.st.out(self.template_comment_attributes_start)
456 for i in n.declarators(): #
457 sname = self.namespace(i, "_")
458 atname = i.identifier()
459 self.st.out(self.template_attributes_declare_Java_get, sname=sname, atname=atname)
461 self.st.out(self.template_attributes_declare_Java_set, sname=sname, atname=atname)
463 self.st.out(self.template_comment_attributes_end)
471 # out: C code for IDL Enum decalarations using "static const value_string" template
476 def genEnList(self,enlist):
478 self.st.out(self.template_comment_enums_start)
481 sname = self.namespace(enum, "_")
483 self.st.out(self.template_comment_enum_comment, ename=enum.repoId())
484 self.st.out(self.template_value_string_start, valstringname=sname)
485 for enumerator in enum.enumerators():
486 self.st.out(self.template_value_string_entry, intval=str(self.valFromEnum(enum,enumerator)), description=enumerator.identifier())
489 #atname = n.identifier()
490 self.st.out(self.template_value_string_end, valstringname=sname)
492 self.st.out(self.template_comment_enums_end)
504 # genExceptionDelegator
508 # out: C code for User exception delegator
514 def genExceptionDelegator(self,oplist):
516 self.st.out(self.template_main_exception_delegator_start)
519 exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
523 print "XXX Exception " , ex.repoId()
524 print "XXX Exception Identifier" , ex.identifier()
525 print "XXX Exception Scoped Name" , ex.scopedName()
527 if (ex.members()): # only if has members
528 sname = self.namespace(ex, "_")
530 self.st.out(self.template_ex_delegate_code, sname=sname, exname=ex.repoId())
533 self.st.out(self.template_main_exception_delegator_end)
537 # genAttribueHelpers()
539 # Generate private helper functions to decode Attributes.
543 # For readonly attribute - generate get_xxx()
544 # If NOT readonly attribute - also generate set_xxx()
547 def genAttributeHelpers(self,atlist):
549 print "XXX genAttributeHelpers: atlist = ", atlist
551 self.st.out(self.template_attribute_helpers_start)
553 for attrib in atlist:
554 for decl in attrib.declarators():
555 self.genAtHelper(attrib,decl,"get") # get accessor
556 if not attrib.readonly():
557 self.genAtHelper(attrib,decl,"set") # set accessor
559 self.st.out(self.template_attribute_helpers_end)
564 # Generate private helper functions to decode an attribute
566 # in: at - attribute node
567 # in: decl - declarator belonging to this attribute
568 # in: order - to generate a "get" or "set" helper
570 def genAtHelper(self,attrib,decl,order):
572 print "XXX genAtHelper"
574 sname = order + "_" + self.namespace(decl, "_") # must use set or get prefix to avoid collision
575 self.curr_sname = sname # update current opnode/exnode scoped name
577 if not self.fn_hash_built:
578 self.fn_hash[sname] = [] # init empty list as val for this sname key
579 # but only if the fn_hash is not already built
581 self.st.out(self.template_attribute_helper_function_start, sname=sname, atname=decl.repoId())
584 self.st.out(self.template_helper_function_vars_start)
585 self.dumpCvars(sname)
586 self.st.out(self.template_helper_function_vars_end )
588 self.st.out(self.template_exception_helper_function_get_endianess)
591 # TODO - attributes are simple types, so remove array handling
594 if decl.sizes(): # an array
595 indices = self.get_indices_from_sizes(decl.sizes())
596 string_indices = '%i ' % indices # convert int to string
597 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
598 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
599 self.addvar(self.c_i + decl.identifier() + ";")
603 self.getCDR3(attrib.attrType(), decl.identifier() )
606 self.st.out(self.template_get_CDR_array_end)
611 self.getCDR3(attrib.attrType(), decl.identifier() )
614 self.st.out(self.template_attribute_helper_function_end)
619 # genExceptionHelpers()
621 # Generate private helper functions to decode Exceptions used
628 def genExceptionHelpers(self,oplist):
629 exlist = self.get_exceptionList(oplist) # grab list of exception nodes
631 print "XXX genExceptionHelpers: exlist = ", exlist
633 self.st.out(self.template_exception_helpers_start)
635 if (ex.members()): # only if has members
636 #print "XXX Exception = " + ex.identifier()
639 self.st.out(self.template_exception_helpers_end)
645 # Generate private helper functions to decode User Exceptions
647 # in: exnode ( an exception node)
650 def genExHelper(self,ex):
652 print "XXX genExHelper"
654 sname = self.namespace(ex, "_")
655 self.curr_sname = sname # update current opnode/exnode scoped name
656 if not self.fn_hash_built:
657 self.fn_hash[sname] = [] # init empty list as val for this sname key
658 # but only if the fn_hash is not already built
660 self.st.out(self.template_exception_helper_function_start, sname=sname, exname=ex.repoId())
663 self.st.out(self.template_helper_function_vars_start)
664 self.dumpCvars(sname)
665 self.st.out(self.template_helper_function_vars_end )
667 self.st.out(self.template_exception_helper_function_get_endianess)
670 for m in ex.members():
671 #print "XXX genExhelper, member = ", m, "member type = ", m.memberType()
674 for decl in m.declarators():
675 #print "XXX genExhelper, d = ", decl
676 if decl.sizes(): # an array
677 indices = self.get_indices_from_sizes(decl.sizes())
678 string_indices = '%i ' % indices # convert int to string
679 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
680 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
681 self.addvar(self.c_i + decl.identifier() + ";")
684 self.getCDR3(m.memberType(), ex.identifier() + "_" + decl.identifier() )
687 self.st.out(self.template_get_CDR_array_end)
691 self.getCDR3(m.memberType(), ex.identifier() + "_" + decl.identifier() )
694 self.st.out(self.template_exception_helper_function_end)
700 # Generate private helper functions for each IDL operation.
701 # Generate private helper functions for each IDL struct.
702 # Generate private helper functions for each IDL union.
705 # in: oplist, stlist, unlist
709 def genHelpers(self,oplist,stlist,unlist):
711 self.genOperation(op)
713 self.genStructHelper(st)
715 self.genUnionHelper(un)
720 # Generate private helper functions for a specificIDL operation.
725 def genOperation(self,opnode):
727 print "XXX genOperation called"
729 sname = self.namespace(opnode, "_")
730 if not self.fn_hash_built:
731 self.fn_hash[sname] = [] # init empty list as val for this sname key
732 # but only if the fn_hash is not already built
734 self.curr_sname = sname # update current opnode's scoped name
735 opname = opnode.identifier()
737 self.st.out(self.template_helper_function_comment, repoid=opnode.repoId() )
739 self.st.out(self.template_helper_function_start, sname=sname)
742 self.st.out(self.template_helper_function_vars_start)
743 self.dumpCvars(sname)
744 self.st.out(self.template_helper_function_vars_end )
747 self.st.out(self.template_helper_switch_msgtype_start)
749 self.st.out(self.template_helper_switch_msgtype_request_start)
751 self.genOperationRequest(opnode)
752 self.st.out(self.template_helper_switch_msgtype_request_end)
755 self.st.out(self.template_helper_switch_msgtype_reply_start)
758 self.st.out(self.template_helper_switch_rep_status_start)
761 self.st.out(self.template_helper_switch_msgtype_reply_no_exception_start)
763 self.genOperationReply(opnode)
764 self.st.out(self.template_helper_switch_msgtype_reply_no_exception_end)
767 self.st.out(self.template_helper_switch_msgtype_reply_user_exception_start)
769 self.genOpExceptions(opnode)
770 self.st.out(self.template_helper_switch_msgtype_reply_user_exception_end)
773 self.st.out(self.template_helper_switch_msgtype_reply_default_start)
774 self.st.out(self.template_helper_switch_msgtype_reply_default_end)
776 self.st.out(self.template_helper_switch_rep_status_end)
780 self.st.out(self.template_helper_switch_msgtype_default_start)
781 self.st.out(self.template_helper_switch_msgtype_default_end)
783 self.st.out(self.template_helper_switch_msgtype_end)
787 self.st.out(self.template_helper_function_end, sname=sname)
793 # Decode function parameters for a GIOP request message
797 def genOperationRequest(self,opnode):
798 for p in opnode.parameters():
801 print "XXX parameter = " ,p
802 print "XXX parameter type = " ,p.paramType()
803 print "XXX parameter type kind = " ,p.paramType().kind()
805 self.getCDR3(p.paramType(),p.identifier())
809 # Decode function parameters for a GIOP reply message
813 def genOperationReply(self,opnode):
815 rt = opnode.returnType() # get return type
817 print "XXX opnode = " , opnode
818 print "XXX return type = " , rt
819 print "XXX return type.unalias = " , rt.unalias()
820 print "XXX return type.kind() = " , rt.kind();
823 if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
824 #self.getCDR3(rt.decl().alias().aliasType(),"dummy") # return value maybe a typedef
825 #self.get_CDR_alias(rt, "Operation_Return_Value" )
826 self.get_CDR_alias(rt, rt.name() )
829 self.getCDR3(rt, "Operation_Return_Value") # return value is NOT an alias
831 for p in opnode.parameters():
832 if p.is_out(): # out or inout
833 self.getCDR3(p.paramType(),p.identifier())
835 #self.st.dec_indent()
837 def genOpExceptions(self,opnode):
838 for ex in opnode.raises():
841 for m in ex.members():
843 #print m.memberType(), m.memberType().kind()
845 # Delegator for Operations
848 def genOpDelegator(self,oplist):
850 iname = "/".join(op.scopedName()[:-1])
851 opname = op.identifier()
852 sname = self.namespace(op, "_")
853 self.st.out(self.template_op_delegate_code, interface=iname, sname=sname)
856 # Delegator for Attributes
859 def genAtDelegator(self,atlist):
861 for i in a.declarators():
862 atname = i.identifier()
863 sname = self.namespace(i, "_")
864 self.st.out(self.template_at_delegate_code_get, sname=sname)
866 self.st.out(self.template_at_delegate_code_set, sname=sname)
870 # Add a variable declaration to the hash of list
873 def addvar(self, var):
874 if not ( var in self.fn_hash[self.curr_sname] ):
875 self.fn_hash[self.curr_sname].append(var)
878 # Print the variable declaration from the hash of list
883 for fn in self.fn_hash.keys():
885 for v in self.fn_hash[fn]:
888 # Print the "C" variable declaration from the hash of list
889 # for a given scoped operation name (eg: tux_penguin_eat)
893 def dumpCvars(self, sname):
894 for v in self.fn_hash[sname]:
899 # Given an enum node, and a enumerator node, return
900 # the enumerator's numerical value.
902 # eg: enum Color {red,green,blue} should return
906 def valFromEnum(self,enumNode, enumeratorNode):
908 print "XXX valFromEnum, enumNode = ", enumNode, " from ", enumNode.repoId()
909 print "XXX valFromEnum, enumeratorNode = ", enumeratorNode, " from ", enumeratorNode.repoId()
911 if isinstance(enumeratorNode,idlast.Enumerator):
912 value = enumNode.enumerators().index(enumeratorNode)
941 ## tk_longdouble = 25
948 ## tk_abstract_interface = 32
954 # This is the main "iterator" function. It takes a node, and tries to output
955 # a get_CDR_XXX accessor method(s). It can call itself multiple times
956 # if I find nested structures etc.
959 def getCDR3(self,type,name="fred"):
961 pt = type.unalias().kind() # param CDR type
962 pn = name # param name
965 print "XXX getCDR3: kind = " , pt
967 if pt == idltype.tk_ulong:
968 self.get_CDR_ulong(pn)
969 elif pt == idltype.tk_longlong:
970 self.get_CDR_longlong(pn)
971 elif pt == idltype.tk_ulonglong:
972 self.get_CDR_ulonglong(pn)
973 elif pt == idltype.tk_void:
974 self.get_CDR_void(pn)
975 elif pt == idltype.tk_short:
976 self.get_CDR_short(pn)
977 elif pt == idltype.tk_long:
978 self.get_CDR_long(pn)
979 elif pt == idltype.tk_ushort:
980 self.get_CDR_ushort(pn)
981 elif pt == idltype.tk_float:
982 self.get_CDR_float(pn)
983 elif pt == idltype.tk_double:
984 self.get_CDR_double(pn)
985 elif pt == idltype.tk_fixed:
986 self.get_CDR_fixed(type.unalias(),pn)
987 elif pt == idltype.tk_boolean:
988 self.get_CDR_boolean(pn)
989 elif pt == idltype.tk_char:
990 self.get_CDR_char(pn)
991 elif pt == idltype.tk_octet:
992 self.get_CDR_octet(pn)
993 elif pt == idltype.tk_any:
995 elif pt == idltype.tk_string:
996 self.get_CDR_string(pn)
997 elif pt == idltype.tk_wstring:
998 self.get_CDR_wstring(pn)
999 elif pt == idltype.tk_wchar:
1000 self.get_CDR_wchar(pn)
1001 elif pt == idltype.tk_enum:
1003 self.get_CDR_enum(pn,type)
1004 #self.get_CDR_enum(pn)
1006 elif pt == idltype.tk_struct:
1007 self.get_CDR_struct(type,pn)
1008 elif pt == idltype.tk_TypeCode: # will I ever get here ?
1009 self.get_CDR_TypeCode(pn)
1010 elif pt == idltype.tk_sequence and \
1011 type.unalias().seqType().kind() == idltype.tk_octet:
1012 self.get_CDR_sequence_octet(type,pn)
1013 elif pt == idltype.tk_sequence:
1014 self.get_CDR_sequence(type,pn)
1015 elif pt == idltype.tk_objref:
1016 self.get_CDR_objref(type,pn)
1017 elif pt == idltype.tk_array:
1018 self.get_CDR_array(type,pn)
1019 elif pt == idltype.tk_union:
1020 self.get_CDR_union(type,pn)
1021 elif pt == idltype.tk_alias:
1023 print "XXXXX Alias type XXXXX " , type
1024 self.get_CDR_alias(type,pn)
1026 self.genWARNING("Unknown typecode = " + '%i ' % pt) # put comment in source code
1030 # get_CDR_XXX methods are here ..
1035 def get_CDR_ulong(self,pn):
1036 self.st.out(self.template_get_CDR_ulong, varname=pn)
1037 self.addvar(self.c_u_octet4)
1039 def get_CDR_short(self,pn):
1040 self.st.out(self.template_get_CDR_short, varname=pn)
1041 self.addvar(self.c_s_octet2)
1043 def get_CDR_void(self,pn):
1044 self.st.out(self.template_get_CDR_void, varname=pn)
1046 def get_CDR_long(self,pn):
1047 self.st.out(self.template_get_CDR_long, varname=pn)
1048 self.addvar(self.c_s_octet4)
1050 def get_CDR_ushort(self,pn):
1051 self.st.out(self.template_get_CDR_ushort, varname=pn)
1052 self.addvar(self.c_u_octet2)
1054 def get_CDR_float(self,pn):
1055 self.st.out(self.template_get_CDR_float, varname=pn)
1056 self.addvar(self.c_float)
1058 def get_CDR_double(self,pn):
1059 self.st.out(self.template_get_CDR_double, varname=pn)
1060 self.addvar(self.c_double)
1062 def get_CDR_longlong(self,pn):
1063 self.st.out(self.template_get_CDR_longlong, varname=pn)
1064 self.addvar(self.c_s_octet8)
1066 def get_CDR_ulonglong(self,pn):
1067 self.st.out(self.template_get_CDR_ulonglong, varname=pn)
1068 self.addvar(self.c_u_octet8)
1070 def get_CDR_boolean(self,pn):
1071 self.st.out(self.template_get_CDR_boolean, varname=pn)
1072 self.addvar(self.c_u_octet1)
1074 def get_CDR_fixed(self,type,pn):
1076 print "XXXX calling get_CDR_fixed, type = ", type
1077 print "XXXX calling get_CDR_fixed, type.digits() = ", type.digits()
1078 print "XXXX calling get_CDR_fixed, type.scale() = ", type.scale()
1080 string_digits = '%i ' % type.digits() # convert int to string
1081 string_scale = '%i ' % type.scale() # convert int to string
1082 string_length = '%i ' % self.dig_to_len(type.digits()) # how many octets to hilight for a number of digits
1084 self.st.out(self.template_get_CDR_fixed, varname=pn, digits=string_digits, scale=string_scale, length=string_length )
1085 self.addvar(self.c_seq)
1088 def get_CDR_char(self,pn):
1089 self.st.out(self.template_get_CDR_char, varname=pn)
1090 self.addvar(self.c_u_octet1)
1092 def get_CDR_octet(self,pn):
1093 self.st.out(self.template_get_CDR_octet, varname=pn)
1094 self.addvar(self.c_u_octet1)
1096 def get_CDR_any(self,pn):
1097 self.st.out(self.template_get_CDR_any, varname=pn)
1099 def get_CDR_enum(self,pn,type):
1100 #self.st.out(self.template_get_CDR_enum, varname=pn)
1101 sname = self.namespace(type.unalias(), "_")
1102 self.st.out(self.template_get_CDR_enum_symbolic, valstringarray=sname)
1105 self.addvar(self.c_u_octet4)
1107 def get_CDR_string(self,pn):
1108 self.st.out(self.template_get_CDR_string, varname=pn)
1109 self.addvar(self.c_u_octet4)
1110 self.addvar(self.c_seq)
1112 def get_CDR_wstring(self,pn):
1113 self.st.out(self.template_get_CDR_wstring, varname=pn)
1114 self.addvar(self.c_u_octet4)
1115 self.addvar(self.c_seq)
1117 def get_CDR_wchar(self,pn):
1118 self.st.out(self.template_get_CDR_wchar, varname=pn)
1119 self.addvar(self.c_s_octet1)
1120 self.addvar(self.c_seq)
1122 def get_CDR_TypeCode(self,pn):
1123 self.st.out(self.template_get_CDR_TypeCode, varname=pn)
1124 self.addvar(self.c_u_octet4)
1126 def get_CDR_objref(self,type,pn):
1127 self.st.out(self.template_get_CDR_object)
1129 def get_CDR_sequence_len(self,pn):
1130 self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1131 self.addvar(self.c_u_octet4)
1135 def get_CDR_union(self,type,pn):
1137 print "XXX Union type =" , type, " pn = ",pn
1138 print "XXX Union type.decl()" , type.decl()
1139 print "XXX Union Scoped Name" , type.scopedName()
1141 # If I am a typedef union {..}; node then find the union node
1143 if isinstance(type.decl(), idlast.Declarator):
1144 ntype = type.decl().alias().aliasType().decl()
1146 ntype = type.decl() # I am a union node
1149 print "XXX Union ntype =" , ntype
1151 sname = self.namespace(ntype, "_")
1152 self.st.out(self.template_union_start, name=sname )
1154 # Output a call to the union helper function so I can handle recursive union also.
1156 self.st.out(self.template_decode_union,name=sname)
1158 self.st.out(self.template_union_end, name=sname )
1162 # Code to generate Union Helper functions
1164 # in: un - a union node
1169 def genUnionHelper(self,un):
1171 print "XXX Union type =" , un
1172 print "XXX Union type.decl()" , un.decl()
1173 print "XXX Union Scoped Name" , un.scopedName()
1175 sname = self.namespace(un, "_")
1176 self.curr_sname = sname # update current opnode/exnode/stnode/unnode scoped name
1177 if not self.fn_hash_built:
1178 self.fn_hash[sname] = [] # init empty list as val for this sname key
1179 # but only if the fn_hash is not already built
1181 self.st.out(self.template_union_helper_function_start, sname=sname, unname=un.repoId())
1182 self.st.inc_indent()
1184 self.st.out(self.template_helper_function_vars_start)
1185 self.dumpCvars(sname)
1186 self.st.out(self.template_helper_function_vars_end )
1188 self.st.out(self.template_union_helper_function_get_endianess)
1190 st = un.switchType().unalias() # may be typedef switch type, so find real type
1192 self.st.out(self.template_comment_union_code_start, uname=un.repoId() )
1194 self.getCDR3(st,un.identifier());
1196 # Depending on what kind of discriminant I come accross (enum,integer,char,
1197 # short, boolean), make sure I cast the return value of the get_XXX accessor
1198 # to an appropriate value. Omniidl idlast.CaseLabel.value() accessor will
1199 # return an integer, or an Enumerator object that is then converted to its
1200 # integer equivalent.
1203 # NOTE - May be able to skip some of this stuff, but leave it in for now -- FS
1206 if (st.kind() == idltype.tk_enum):
1208 self.st.out(self.template_comment_union_code_discriminant, uname=std.repoId() )
1209 self.st.out(self.template_union_code_save_discriminant_enum, discname=un.identifier() )
1210 self.addvar(self.c_s_disc + un.identifier() + ";")
1212 elif (st.kind() == idltype.tk_long):
1213 self.st.out(self.template_union_code_save_discriminant_long, discname=un.identifier() )
1214 self.addvar(self.c_s_disc + un.identifier() + ";")
1216 elif (st.kind() == idltype.tk_ulong):
1217 self.st.out(self.template_union_code_save_discriminant_ulong, discname=un.identifier() )
1218 self.addvar(self.c_s_disc + un.identifier() + ";")
1220 elif (st.kind() == idltype.tk_short):
1221 self.st.out(self.template_union_code_save_discriminant_short, discname=un.identifier() )
1222 self.addvar(self.c_s_disc + un.identifier() + ";")
1224 elif (st.kind() == idltype.tk_ushort):
1225 self.st.out(self.template_union_code_save_discriminant_ushort, discname=un.identifier() )
1226 self.addvar(self.c_s_disc + un.identifier() + ";")
1228 elif (st.kind() == idltype.tk_boolean):
1229 self.st.out(self.template_union_code_save_discriminant_boolean, discname=un.identifier() )
1230 self.addvar(self.c_s_disc + un.identifier() + ";")
1232 elif (st.kind() == idltype.tk_char):
1233 self.st.out(self.template_union_code_save_discriminant_char, discname=un.identifier() )
1234 self.addvar(self.c_s_disc + un.identifier() + ";")
1237 print "XXX Unknown st.kind() = ", st.kind()
1240 # Loop over all cases in this union
1243 for uc in un.cases(): # for all UnionCase objects in this union
1244 for cl in uc.labels(): # for all Caselabel objects in this UnionCase
1246 # get integer value, even if discriminant is
1247 # an Enumerator node
1249 if isinstance(cl.value(),idlast.Enumerator):
1251 print "XXX clv.identifier()", cl.value().identifier()
1252 print "XXX clv.repoId()", cl.value().repoId()
1253 print "XXX clv.scopedName()", cl.value().scopedName()
1255 # find index of enumerator in enum declaration
1256 # eg: RED is index 0 in enum Colors { RED, BLUE, GREEN }
1258 clv = self.valFromEnum(std,cl.value())
1263 #print "XXX clv = ",clv
1266 # if char, dont convert to int, but put inside single quotes so that it is understood by C.
1267 # eg: if (disc == 'b')..
1269 # TODO : handle \xxx chars generically from a function or table lookup rather than
1270 # a whole bunch of "if" statements. -- FS
1273 if (st.kind() == idltype.tk_char):
1274 if (clv == '\n'): # newline
1275 string_clv = "'\\n'"
1276 elif (clv == '\t'): # tab
1277 string_clv = "'\\t'"
1279 string_clv = "'" + clv + "'"
1281 string_clv = '%i ' % clv
1284 # If default case, then skp comparison with discriminator
1287 if not cl.default():
1288 self.st.out(self.template_comment_union_code_label_compare_start, discname=un.identifier(),labelval=string_clv )
1289 self.st.inc_indent()
1291 self.st.out(self.template_comment_union_code_label_default_start )
1294 self.getCDR3(uc.caseType(),uc.declarator().identifier())
1296 if not cl.default():
1297 self.st.dec_indent()
1298 self.st.out(self.template_comment_union_code_label_compare_end )
1300 self.st.out(self.template_comment_union_code_label_default_end )
1302 self.st.dec_indent()
1303 self.st.out(self.template_union_helper_function_end)
1308 # Currently, get_CDR_alias is geared to finding typdef
1311 def get_CDR_alias(self,type,pn):
1313 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1314 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1315 print "XXX get_CDR_alias, type.decl().alias() = " ,type.decl().alias()
1317 decl = type.decl() # get declarator object
1319 if (decl.sizes()): # a typedef array
1320 indices = self.get_indices_from_sizes(decl.sizes())
1321 string_indices = '%i ' % indices # convert int to string
1322 self.st.out(self.template_get_CDR_array_comment, aname=pn, asize=string_indices)
1324 self.st.out(self.template_get_CDR_array_start, aname=pn, aval=string_indices)
1325 self.addvar(self.c_i + pn + ";")
1326 self.st.inc_indent()
1327 self.getCDR3(type.decl().alias().aliasType(), pn )
1329 self.st.dec_indent()
1330 self.st.out(self.template_get_CDR_array_end)
1333 else: # a simple typdef
1335 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1336 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1338 self.getCDR3(type, decl.identifier() )
1346 # Handle structs, including recursive
1349 def get_CDR_struct(self,type,pn):
1351 # If I am a typedef struct {..}; node then find the struct node
1353 if isinstance(type.decl(), idlast.Declarator):
1354 ntype = type.decl().alias().aliasType().decl()
1356 ntype = type.decl() # I am a struct node
1358 sname = self.namespace(ntype, "_")
1359 self.st.out(self.template_structure_start, name=sname )
1361 # Output a call to the struct helper function so I can handle recursive structs also.
1363 self.st.out(self.template_decode_struct,name=sname)
1365 self.st.out(self.template_structure_end, name=sname )
1370 # Generate private helper functions to decode a struct
1372 # in: stnode ( a struct node)
1375 def genStructHelper(self,st):
1377 print "XXX genStructHelper"
1379 sname = self.namespace(st, "_")
1380 self.curr_sname = sname # update current opnode/exnode/stnode scoped name
1381 if not self.fn_hash_built:
1382 self.fn_hash[sname] = [] # init empty list as val for this sname key
1383 # but only if the fn_hash is not already built
1385 self.st.out(self.template_struct_helper_function_start, sname=sname, stname=st.repoId())
1386 self.st.inc_indent()
1388 self.st.out(self.template_helper_function_vars_start)
1389 self.dumpCvars(sname)
1390 self.st.out(self.template_helper_function_vars_end )
1392 self.st.out(self.template_struct_helper_function_get_endianess)
1394 for m in st.members():
1395 for decl in m.declarators():
1396 if decl.sizes(): # an array
1397 indices = self.get_indices_from_sizes(decl.sizes())
1398 string_indices = '%i ' % indices # convert int to string
1399 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
1400 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
1401 self.addvar(self.c_i + decl.identifier() + ";")
1403 self.st.inc_indent()
1404 self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
1405 self.st.dec_indent()
1406 self.st.out(self.template_get_CDR_array_end)
1410 self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
1412 self.st.dec_indent()
1413 self.st.out(self.template_struct_helper_function_end)
1420 # Generate code to access a sequence of a type
1424 def get_CDR_sequence(self,type, pn):
1425 self.st.out(self.template_get_CDR_sequence_length, seqname=pn )
1426 self.st.out(self.template_get_CDR_sequence_loop_start, seqname=pn )
1427 self.addvar(self.c_i_lim + pn + ";" )
1428 self.addvar(self.c_i + pn + ";")
1430 self.st.inc_indent()
1431 self.getCDR3(type.unalias().seqType(), pn ) # and start all over with the type
1432 self.st.dec_indent()
1434 self.st.out(self.template_get_CDR_sequence_loop_end)
1438 # Generate code to access a sequence of octet
1441 def get_CDR_sequence_octet(self,type, pn):
1442 self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1443 self.st.out(self.template_get_CDR_sequence_octet, seqname=pn)
1444 self.addvar(self.c_i_lim + pn + ";")
1445 self.addvar("gchar * binary_seq_" + pn + ";")
1446 self.addvar("gchar * text_seq_" + pn + ";")
1450 # Generate code to access arrays,
1452 # This is handled elsewhere. Arrays are either typedefs or in
1455 # TODO - Remove this
1458 def get_CDR_array(self,type, decl):
1460 print "XXX get_CDR_array called "
1461 print "XXX array size = " ,decl.sizes()
1469 # out - scoped operation name, using sep character instead of "::"
1471 # eg: Penguin::Echo::echoWString => Penguin_Echo_echoWString if sep = "_"
1475 def namespace(self,node,sep):
1476 sname = string.replace(idlutil.ccolonName(node.scopedName()), '::', sep)
1477 #print "XXX namespace: sname = " + sname
1482 # generate code for plugin initialisation
1485 def gen_plugin_register(self):
1486 self.st.out(self.template_plugin_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1489 # generate register_giop_user_module code, and register only
1490 # unique interfaces that contain operations. Also output
1491 # a heuristic register in case we want to use that.
1493 # TODO - make this a command line option
1501 def gen_proto_reg_handoff(self, oplist):
1503 self.st.out(self.template_proto_reg_handoff_start, dissector_name=self.dissname)
1504 self.st.inc_indent()
1506 for iname in self.get_intlist(oplist):
1507 self.st.out(self.template_proto_reg_handoff_body, dissector_name=self.dissname, protocol_name=self.protoname, interface=iname )
1509 self.st.out(self.template_proto_reg_handoff_heuristic, dissector_name=self.dissname, protocol_name=self.protoname)
1510 self.st.dec_indent()
1512 self.st.out(self.template_proto_reg_handoff_end)
1517 # generate proto_register_<protoname> code,
1521 def gen_proto_register(self):
1522 self.st.out(self.template_proto_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1528 # out - a list of unique interface names. This will be used in
1529 # register_giop_user_module(dissect_giop_auto, "TEST IDL", "Penguin/Echo" ); so the operation
1530 # name must be removed from the scope. And we also only want unique interfaces.
1533 def get_intlist(self,oplist):
1534 int_hash = {} # holds a hash of unique interfaces
1536 sc = op.scopedName() # eg: penguin,tux,bite
1537 sc1 = sc[:-1] # drop last entry
1538 sn = idlutil.slashName(sc1) # penguin/tux
1539 if not int_hash.has_key(sn):
1540 int_hash[sn] = 0; # dummy val, but at least key is unique
1541 ret = int_hash.keys()
1550 # out - a list of exception nodes (unique). This will be used in
1551 # to generate dissect_exception_XXX functions.
1556 def get_exceptionList(self,oplist):
1557 ex_hash = {} # holds a hash of unique exceptions.
1559 for ex in op.raises():
1560 if not ex_hash.has_key(ex):
1561 ex_hash[ex] = 0; # dummy val, but at least key is unique
1563 print "XXX Exception = " + ex.identifier()
1564 ret = ex_hash.keys()
1571 # Simple function to take a list of array sizes and find the
1572 # total number of elements
1575 # eg: temp[4][3] = 12 elements
1578 def get_indices_from_sizes(self,sizelist):
1586 # Determine how many octets contain requested number
1587 # of digits for an "fixed" IDL type "on the wire"
1590 def dig_to_len(self,dignum):
1591 return (dignum/2) + 1
1596 # Output some TODO comment
1600 def genTODO(self,message):
1601 self.st.out(self.template_debug_TODO, message=message)
1604 # Output some WARNING comment
1608 def genWARNING(self,message):
1609 self.st.out(self.template_debug_WARNING, message=message)
1612 # Templates for C code
1615 template_comment_operations_start = """\
1617 * IDL Operations Start
1620 template_operations_declare = """static const char @sname@_op[] = \"@opname@\" ;"""
1622 template_comment_operations_end = """
1624 * IDL Operations End
1627 template_helper_function_comment = """\
1632 template_helper_function_vars_start = """
1633 /* Operation specific Variable declarations Begin */
1635 template_helper_function_vars_end = """
1636 /* Operation specific Variable declarations End */
1638 template_helper_function_start = """\
1639 static void decode_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header, gchar *operation _U_) {
1641 gboolean stream_is_big_endian; /* big endianess */
1643 template_helper_function_end = """\
1647 # proto_reg_handoff() templates
1650 template_proto_reg_handoff_start = """
1651 /* register me as handler for these interfaces */
1653 void proto_register_handoff_giop_@dissector_name@(void) {
1656 template_proto_reg_handoff_body = """
1657 /* Register for Explicit Dissection */
1659 register_giop_user_module(dissect_@dissector_name@, \"@protocol_name@\", \"@interface@\", proto_@dissector_name@ ); /* explicit dissector */
1661 template_proto_reg_handoff_heuristic = """
1662 /* Register for Heuristic Dissection */
1664 register_giop_user(dissect_@dissector_name@, \"@protocol_name@\" ,proto_@dissector_name@); /* heuristic dissector */
1666 template_proto_reg_handoff_end = """
1671 # Initialize the protocol
1674 template_protocol = """
1675 /* Initialise the protocol and subtree pointers */
1677 static int proto_@dissector_name@ = -1;
1679 static gint ett_@dissector_name@ = -1;
1682 # Initialize the boundary Alignment
1685 template_init_boundary = """
1686 /* Initialise the initial Alignment */
1688 static guint32 boundary = GIOP_HEADER_SIZE; /* initial value */
1691 # Initialize the Registered fields
1694 template_registered_fields = """
1696 /* Initialise the Registered fields */
1698 /* TODO - Use registered fields */
1701 # plugin_register and plugin_reg_handoff templates
1704 template_plugin_register = """
1705 #ifndef ENABLE_STATIC
1707 G_MODULE_EXPORT void
1708 plugin_register(void)
1710 if (proto_@dissector_name@ == -1) {
1711 proto_register_giop_@dissector_name@();
1715 G_MODULE_EXPORT void
1716 plugin_reg_handoff(void){
1717 proto_register_handoff_giop_@dissector_name@();
1722 # proto_register_<dissector name>(void) templates
1725 template_proto_register = """
1727 /* Register the protocol with Ethereal */
1729 void proto_register_giop_@dissector_name@(void) {
1731 /* setup list of header fields */
1734 static hf_register_info hf[] = {
1741 /* setup protocol subtree array */
1743 static gint *ett[] = {
1744 &ett_@dissector_name@,
1747 /* Register the protocol name and description */
1749 proto_@dissector_name@ = proto_register_protocol(\"@description@\" , \"@protocol_name@\", \"giop-@dissector_name@\" );
1752 proto_register_field_array(proto_@dissector_name@, hf, array_length(hf));
1754 proto_register_subtree_array(ett,array_length(ett));
1759 # template for delegation code
1762 template_op_delegate_code = """\
1763 if (strcmp(operation, @sname@_op) == 0
1764 && (!idlname || strcmp(idlname, \"@interface@\") == 0)) {
1765 tree = start_dissecting(tvb, pinfo, ptree, offset);
1766 decode_@sname@(tvb, pinfo, tree, offset, header, operation);
1771 # Templates for the helper functions
1776 template_helper_switch_msgtype_start = """\
1777 stream_is_big_endian = is_big_endian(header);
1779 switch(header->message_type) {
1781 template_helper_switch_msgtype_default_start = """\
1784 /* Unknown GIOP Exception */
1786 g_warning("Unknown GIOP Message");
1788 template_helper_switch_msgtype_default_end = """\
1791 template_helper_switch_msgtype_end = """\
1792 } /* switch(header->message_type) */
1794 template_helper_switch_msgtype_request_start = """\
1797 template_helper_switch_msgtype_request_end = """\
1800 template_helper_switch_msgtype_reply_start = """\
1803 template_helper_switch_msgtype_reply_no_exception_start = """\
1806 template_helper_switch_msgtype_reply_no_exception_end = """\
1809 template_helper_switch_msgtype_reply_user_exception_start = """\
1810 case USER_EXCEPTION:
1812 template_helper_switch_msgtype_reply_user_exception_end = """\
1815 template_helper_switch_msgtype_reply_default_start = """\
1818 /* Unknown Exception */
1820 g_warning("Unknown Exception ");
1822 template_helper_switch_msgtype_reply_default_end = """\
1825 template_helper_switch_msgtype_reply_end = """\
1828 template_helper_switch_msgtype_default_start = """\
1831 /* Unknown GIOP Message */
1833 g_warning("Unknown GIOP Message");
1835 template_helper_switch_msgtype_default_end = """\
1838 template_helper_switch_rep_status_start = """\
1839 switch(header->rep_status) {
1841 template_helper_switch_rep_status_default_start = """\
1844 /* Unknown Reply Status */
1846 g_warning("Unknown Reply Status");
1848 template_helper_switch_rep_status_default_end = """\
1851 template_helper_switch_rep_status_end = """\
1853 } /* switch(header->message_type) */
1859 # Templates for get_CDR_xxx accessors
1862 template_get_CDR_ulong = """\
1863 u_octet4 = get_CDR_ulong(tvb,offset,stream_is_big_endian, boundary);
1865 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %u",u_octet4);
1868 template_get_CDR_short = """\
1869 s_octet2 = get_CDR_short(tvb,offset,stream_is_big_endian, boundary);
1871 proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %i",s_octet2);
1874 template_get_CDR_void = """\
1875 /* Function returns void */
1877 template_get_CDR_long = """\
1878 s_octet4 = get_CDR_long(tvb,offset,stream_is_big_endian, boundary);
1880 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %i",s_octet4);
1883 template_get_CDR_ushort = """\
1884 u_octet2 = get_CDR_ushort(tvb,offset,stream_is_big_endian, boundary);
1886 proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %u",u_octet2);
1889 template_get_CDR_float = """\
1890 my_float = get_CDR_float(tvb,offset,stream_is_big_endian, boundary);
1892 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %.6e",my_float);
1895 template_get_CDR_double = """\
1896 my_double = get_CDR_double(tvb,offset,stream_is_big_endian, boundary);
1898 proto_tree_add_text(tree,tvb,*offset-8,8,"@varname@ = %.15e",my_double);
1901 template_get_CDR_longlong = """\
1902 s_octet8 = get_CDR_long_long(tvb,offset,stream_is_big_endian, boundary);
1904 proto_tree_add_text(tree,tvb,*offset-8,8,"@varname@ = PRIo64",s_octet8);
1907 template_get_CDR_ulonglong = """\
1908 u_octet8 = get_CDR_ulong_long(tvb,offset,stream_is_big_endian, boundary);
1910 proto_tree_add_text(tree,tvb,*offset-8,8,"@varname@ = PRIu64",u_octet8);
1913 template_get_CDR_boolean = """\
1914 u_octet1 = get_CDR_boolean(tvb,offset);
1916 proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1919 template_get_CDR_char = """\
1920 u_octet1 = get_CDR_char(tvb,offset);
1922 proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1925 template_get_CDR_octet = """\
1926 u_octet1 = get_CDR_octet(tvb,offset);
1928 proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1931 template_get_CDR_any = """\
1932 get_CDR_any(tvb,tree,offset,stream_is_big_endian, boundary, header);
1934 template_get_CDR_fixed = """\
1935 get_CDR_fixed(tvb, &seq, offset, @digits@, @scale@);
1937 proto_tree_add_text(tree,tvb,*offset-@length@, @length@, "@varname@ < @digits@, @scale@> = %s",seq);
1940 g_free(seq); /* free buffer */
1943 template_get_CDR_enum_symbolic = """\
1945 u_octet4 = get_CDR_enum(tvb,offset,stream_is_big_endian, boundary);
1947 proto_tree_add_text(tree,tvb,*offset-4,4,"Enum value = %u (%s)",u_octet4,val_to_str(u_octet4,@valstringarray@,"Unknown Enum Value"));
1950 template_get_CDR_string = """\
1951 u_octet4 = get_CDR_string(tvb, &seq, offset, stream_is_big_endian, boundary);
1953 proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ (%u) = %s",
1954 u_octet4, (u_octet4 > 0) ? seq : \"\");
1957 g_free(seq); /* free buffer */
1960 template_get_CDR_wstring = """\
1961 u_octet4 = get_CDR_wstring(tvb, &seq, offset, stream_is_big_endian, boundary, header);
1963 proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ (%u) = %s",
1964 u_octet4, (u_octet4 > 0) ? seq : \"\");
1967 g_free(seq); /* free buffer */
1970 template_get_CDR_wchar = """\
1971 s_octet1 = get_CDR_wchar(tvb, &seq, offset, header);
1974 proto_tree_add_text(tree,tvb,*offset-1-s_octet1,1,"length = %u",s_octet1);
1977 s_octet1 = -s_octet1;
1980 proto_tree_add_text(tree,tvb,*offset-s_octet1,s_octet1,"@varname@ = %s",seq);
1984 g_free(seq); /* free buffer */
1987 template_get_CDR_TypeCode = """\
1988 u_octet4 = get_CDR_typeCode(tvb, tree, offset, stream_is_big_endian, boundary, header);
1992 template_get_CDR_object = """\
1993 get_CDR_object(tvb, pinfo, tree, offset, stream_is_big_endian, boundary);
1996 template_get_CDR_sequence_length = """\
1997 u_octet4_loop_@seqname@ = get_CDR_ulong(tvb, offset, stream_is_big_endian, boundary);
1999 proto_tree_add_text(tree,tvb,*offset-4, 4 ,"Seq length of @seqname@ = %u",u_octet4_loop_@seqname@);
2002 template_get_CDR_sequence_loop_start = """\
2003 for (i_@seqname@=0; i_@seqname@ < u_octet4_loop_@seqname@; i_@seqname@++) {
2005 template_get_CDR_sequence_loop_end = """\
2009 template_get_CDR_sequence_octet = """\
2010 if (u_octet4_loop_@seqname@ > 0 && tree) {
2011 get_CDR_octet_seq(tvb, &binary_seq_@seqname@, offset,
2012 u_octet4_loop_@seqname@);
2013 text_seq_@seqname@ = make_printable_string(binary_seq_@seqname@,
2014 u_octet4_loop_@seqname@);
2015 proto_tree_add_text(tree, tvb, *offset - u_octet4_loop_@seqname@,
2016 u_octet4_loop_@seqname@, \"@seqname@: %s\", text_seq_@seqname@);
2017 g_free(binary_seq_@seqname@);
2018 g_free(text_seq_@seqname@);
2021 template_get_CDR_array_start = """\
2022 for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
2024 template_get_CDR_array_end = """\
2027 template_get_CDR_array_comment = """\
2028 /* Array: @aname@[ @asize@] */
2030 template_structure_start = """\
2031 /* Begin struct \"@name@\" */
2033 template_structure_end = """\
2034 /* End struct \"@name@\" */
2037 template_union_start = """\
2038 /* Begin union \"@name@\" */
2040 template_union_end = """\
2041 /* End union \"@name@\" */
2045 # Program Header Template
2048 template_Header = """\
2049 /* packet-@dissector_name@.c
2050 * Routines for IDL dissection
2052 * Autogenerated from idl2eth
2053 * Copyright 2001 Frank Singleton <frank.singleton@@ericsson.com>
2058 template_ethereal_copyright = """\
2060 * Ethereal - Network traffic analyzer
2062 * Copyright 1999 Gerald Combs
2076 * This program is free software; you can redistribute it and/or
2077 * modify it under the terms of the GNU General Public License
2078 * as published by the Free Software Foundation; either version 2
2079 * of the License, or (at your option) any later version.
2081 * This program is distributed in the hope that it will be useful,
2082 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2083 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2084 * GNU General Public License for more details.
2086 * You should have received a copy of the GNU General Public License
2087 * along with this program; if not, write to the Free Software
2088 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2096 template_Includes = """\
2098 #ifdef HAVE_CONFIG_H
2099 # include "config.h"
2104 #include <gmodule.h>
2108 #include <epan/packet.h>
2109 #include <epan/proto.h>
2110 #include <epan/dissectors/packet-giop.h>
2112 #ifndef ENABLE_STATIC
2113 G_MODULE_EXPORT const gchar version[] = "0.0.1";
2120 # Main dissector entry templates
2123 template_main_dissector_start = """\
2125 * Called once we accept the packet as being for us; it sets the
2126 * Protocol and Info columns and creates the top-level protocol
2129 static proto_tree *start_dissecting(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset) {
2131 proto_item *ti = NULL;
2132 proto_tree *tree = NULL; /* init later, inside if(tree) */
2134 if (check_col(pinfo->cinfo, COL_PROTOCOL))
2135 col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
2138 * Do not clear COL_INFO, as nothing is being written there by
2139 * this dissector yet. So leave it as is from the GIOP dissector.
2140 * TODO: add something useful to COL_INFO
2141 * if (check_col(pinfo->cinfo, COL_INFO))
2142 * col_clear(pinfo->cinfo, COL_INFO);
2146 ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, -1, FALSE);
2147 tree = proto_item_add_subtree(ti, ett_@dissname@);
2152 static gboolean dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, gchar *operation, gchar *idlname) {
2154 gboolean be; /* big endianess */
2155 proto_tree *tree _U_;
2157 be = is_big_endian(header); /* get endianess - TODO use passed in stream_is_big_endian instead ? */
2159 /* If we have a USER Exception, then decode it and return */
2161 if ((header->message_type == Reply) && (header->rep_status == USER_EXCEPTION)) {
2163 return decode_user_exception(tvb, pinfo, ptree, offset, header, operation);
2167 template_main_dissector_switch_msgtype_start = """\
2168 switch(header->message_type) {
2170 template_main_dissector_switch_msgtype_start_request_reply = """\
2174 template_main_dissector_switch_msgtype_end_request_reply = """\
2178 template_main_dissector_switch_msgtype_all_other_msgtype = """\
2182 case CloseConnection:
2185 return FALSE; /* not handled yet */
2188 return FALSE; /* not handled yet */
2192 template_main_dissector_end = """\
2196 } /* End of main dissector */
2205 #-------------------------------------------------------------#
2206 # Exception handling templates #
2207 #-------------------------------------------------------------#
2215 template_exception_helpers_start = """\
2216 /* Begin Exception Helper Functions */
2219 template_exception_helpers_end = """\
2221 /* End Exception Helper Functions */
2227 # Templates for declaration of string constants for user exceptions.
2230 template_comment_user_exceptions_string_declare_start = """\
2231 /* Begin Exception (containing members) String Declare */
2234 template_user_exceptions_declare = """static const char user_exception_@sname@[] = \"@exname@\" ; """
2237 template_comment_user_exceptions_string_declare_end = """\
2239 /* End Exception (containing members) String Declare */
2246 # template for Main delegator for exception handling
2249 template_main_exception_delegator_start = """\
2251 * Main delegator for exception handling
2255 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_ ) {
2257 gboolean be _U_; /* big endianess */
2258 proto_tree *tree _U_;
2260 if (!header->exception_id)
2266 # template for exception delegation code body
2268 template_ex_delegate_code = """\
2269 if (strcmp(header->exception_id, user_exception_@sname@) == 0) {
2270 tree = start_dissecting(tvb, pinfo, ptree, offset);
2271 decode_ex_@sname@(tvb, pinfo, tree, offset, header, operation); /* @exname@ */
2278 # End of Main delegator for exception handling
2281 template_main_exception_delegator_end = """\
2284 return FALSE; /* user exception not found */
2290 # template for exception helper code
2294 template_exception_helper_function_start = """\
2295 /* Exception = @exname@ */
2297 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_) {
2299 gboolean stream_is_big_endian; /* big endianess */
2305 # Template for the helper function
2306 # to get stream endianess from header
2309 template_exception_helper_function_get_endianess = """\
2310 stream_is_big_endian = is_big_endian(header); /* get stream endianess */
2312 template_exception_helper_function_end = """\
2318 # template for struct helper code
2322 template_struct_helper_function_start = """\
2323 /* Struct = @stname@ */
2325 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_) {
2327 gboolean stream_is_big_endian; /* big endianess */
2334 # Template for the helper function
2335 # to get stream endianess from header
2338 template_struct_helper_function_get_endianess = """\
2339 stream_is_big_endian = is_big_endian(header); /* get stream endianess */
2341 template_struct_helper_function_end = """\
2346 # template for union helper code
2350 template_union_helper_function_start = """\
2351 /* Union = @unname@ */
2353 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_) {
2355 gboolean stream_is_big_endian; /* big endianess */
2362 # Template for the helper function
2363 # to get stream endianess from header
2366 template_union_helper_function_get_endianess = """\
2367 stream_is_big_endian = is_big_endian(header); /* get stream endianess */
2369 template_union_helper_function_end = """\
2375 #-------------------------------------------------------------#
2376 # Value string templates #
2377 #-------------------------------------------------------------#
2379 template_value_string_start = """\
2380 static const value_string @valstringname@[] = {
2382 template_value_string_entry = """\
2383 { @intval@, \"@description@\" }, """
2385 template_value_string_end = """\
2392 #-------------------------------------------------------------#
2393 # Enum handling templates #
2394 #-------------------------------------------------------------#
2396 template_comment_enums_start = """\
2401 template_comment_enums_end = """\
2406 template_comment_enum_comment = """\
2414 #-------------------------------------------------------------#
2415 # Attribute handling templates #
2416 #-------------------------------------------------------------#
2419 template_comment_attributes_start = """\
2421 * IDL Attributes Start
2426 # get/set accessor method names are language mapping dependant.
2429 template_attributes_declare_Java_get = """static const char get_@sname@_at[] = \"_get_@atname@\" ;"""
2430 template_attributes_declare_Java_set = """static const char set_@sname@_at[] = \"_set_@atname@\" ;"""
2432 template_comment_attributes_end = """
2434 * IDL Attributes End
2440 # template for Attribute delegation code
2442 # Note: _get_xxx() should only be called for Reply with NO_EXCEPTION
2443 # Note: _set_xxx() should only be called for Request
2447 template_at_delegate_code_get = """\
2448 if (strcmp(operation, get_@sname@_at) == 0 && (header->message_type == Reply) && (header->rep_status == NO_EXCEPTION) ) {
2449 tree = start_dissecting(tvb, pinfo, ptree, offset);
2450 decode_get_@sname@_at(tvb, pinfo, tree, offset, header, operation);
2454 template_at_delegate_code_set = """\
2455 if (strcmp(operation, set_@sname@_at) == 0 && (header->message_type == Request) ) {
2456 tree = start_dissecting(tvb, pinfo, ptree, offset);
2457 decode_set_@sname@_at(tvb, pinfo, tree, offset, header, operation);
2461 template_attribute_helpers_start = """\
2462 /* Begin Attribute Helper Functions */
2464 template_attribute_helpers_end = """\
2466 /* End Attribute Helper Functions */
2470 # template for attribute helper code
2474 template_attribute_helper_function_start = """\
2476 /* Attribute = @atname@ */
2478 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_) {
2480 gboolean stream_is_big_endian; /* big endianess */
2486 # Template for the helper function
2487 # to get stream endianess from header
2490 template_attribute_helper_function_get_endianess = """\
2491 stream_is_big_endian = is_big_endian(header); /* get stream endianess */
2493 template_attribute_helper_function_end = """\
2498 #-------------------------------------------------------------#
2499 # Debugging templates #
2500 #-------------------------------------------------------------#
2503 # Template for outputting TODO "C" comments
2504 # so user know I need ti improve something.
2507 template_debug_TODO = """\
2509 /* TODO - @message@ */
2512 # Template for outputting WARNING "C" comments
2513 # so user know if I have found a problem.
2516 template_debug_WARNING = """\
2517 /* WARNING - @message@ */
2522 #-------------------------------------------------------------#
2523 # IDL Union templates #
2524 #-------------------------------------------------------------#
2526 template_comment_union_code_start = """\
2528 * IDL Union Start - @uname@
2531 template_comment_union_code_end = """
2533 * IDL union End - @uname@
2536 template_comment_union_code_discriminant = """\
2538 * IDL Union - Discriminant - @uname@
2542 # Cast Unions types to something appropriate
2543 # Enum value cast to guint32, all others cast to gint32
2544 # as omniidl accessor returns integer or Enum.
2547 template_union_code_save_discriminant_enum = """\
2548 disc_s_@discname@ = (gint32) u_octet4; /* save Enum Value discriminant and cast to gint32 */
2550 template_union_code_save_discriminant_long = """\
2551 disc_s_@discname@ = (gint32) s_octet4; /* save gint32 discriminant and cast to gint32 */
2554 template_union_code_save_discriminant_ulong = """\
2555 disc_s_@discname@ = (gint32) u_octet4; /* save guint32 discriminant and cast to gint32 */
2557 template_union_code_save_discriminant_short = """\
2558 disc_s_@discname@ = (gint32) s_octet2; /* save gint16 discriminant and cast to gint32 */
2561 template_union_code_save_discriminant_ushort = """\
2562 disc_s_@discname@ = (gint32) u_octet2; /* save guint16 discriminant and cast to gint32 */
2564 template_union_code_save_discriminant_char = """\
2565 disc_s_@discname@ = (gint32) u_octet1; /* save guint1 discriminant and cast to gint32 */
2567 template_union_code_save_discriminant_boolean = """\
2568 disc_s_@discname@ = (gint32) u_octet1; /* save guint1 discriminant and cast to gint32 */
2570 template_comment_union_code_label_compare_start = """\
2571 if (disc_s_@discname@ == @labelval@) {
2573 template_comment_union_code_label_compare_end = """\
2574 return; /* End Compare for this discriminant type */
2579 template_comment_union_code_label_default_start = """
2580 /* Default Union Case Start */
2582 template_comment_union_code_label_default_end = """\
2583 /* Default Union Case End */
2587 # Templates for function prototypes.
2588 # This is used in genDeclares() for declaring function prototypes
2589 # for structs and union helper functions.
2592 template_prototype_start_dissecting = """
2593 static proto_tree *start_dissecting(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset);
2596 template_prototype_struct_start = """
2597 /* Struct prototype declaration Start */
2599 template_prototype_struct_end = """
2600 /* Struct prototype declaration End */
2602 template_prototype_struct_body = """
2603 /* Struct = @stname@ */
2605 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_);
2607 template_decode_struct = """
2609 decode_@name@_st(tvb, pinfo, tree, offset, header, operation);
2611 template_prototype_union_start = """
2612 /* Union prototype declaration Start */
2614 template_prototype_union_end = """
2615 /* Union prototype declaration End */
2618 template_prototype_union_body = """
2620 /* Union = @unname@ */
2622 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_);
2625 template_decode_union = """
2626 decode_@name@_un(tvb, pinfo, tree, offset, header, operation);