3 # wireshark_gen.py (part of idl2wrs)
5 # Author : Frank Singleton (frank.singleton@ericsson.com)
7 # Copyright (C) 2001 Frank Singleton, Ericsson Inc.
9 # This file is a backend to "omniidl", used to generate "Wireshark"
10 # dissectors from CORBA IDL descriptions. The output language generated
11 # is "C". It will generate code to use the GIOP/IIOP get_CDR_XXX API.
13 # Please see packet-giop.h in Wireshark distro for API description.
14 # Wireshark is available at http://www.wireshark.org/
16 # Omniidl is part of the OmniOrb distribution, and is available at
17 # http://omniorb.sourceforge.net
19 # This program is free software; you can redistribute it and/or modify it
20 # under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 2 of the License, or
22 # (at your option) any later version.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 # General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to the Free Software
31 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
36 # Omniidl Back-end which parses an IDL list of "Operation" nodes
37 # passed from wireshark_be2.py and generates "C" code for compiling
38 # as a plugin for the Wireshark IP Protocol Analyser.
41 # Strategy (sneaky but ...)
43 # problem: I dont know what variables to declare until AFTER the helper functions
44 # have been built, so ...
46 # There are 2 passes through genHelpers, the first one is there just to
47 # make sure the fn_hash data struct is populated properly.
48 # The second pass is the real thing, generating code and declaring
49 # variables (from the 1st pass) properly.
53 """Wireshark IDL compiler back-end."""
55 from omniidl import idlast, idltype, idlutil, output
60 # Output class, generates "C" src code for the sub-dissector
67 # node - a reference to an Operations object.
68 # name - scoped name (Module::Module::Interface:: .. ::Operation
76 # 1. generate hf[] data for searchable fields (but what is searchable?) [done, could be improved]
77 # 2. add item instead of add_text() [done]
78 # 3. sequence handling [done]
79 # 4. User Exceptions [done]
80 # 5. Fix arrays, and structs containing arrays [done]
82 # 7. Exception can be common to many operations, so handle them outside the
83 # operation helper functions [done]
84 # 8. Automatic variable declaration [done, improve, still get some collisions.add variable delegator function ]
85 # For example, mutlidimensional arrays.
86 # 9. wchar and wstring handling [giop API needs improving]
87 # 10. Support Fixed [done]
88 # 11. Support attributes (get/set) [started, needs language mapping option, perhaps wireshark GUI option
89 # to set the attribute function prefix or suffix ? ] For now the prefix is "_get" and "_set"
90 # eg: attribute string apple => _get_apple and _set_apple
92 # 12. Implement IDL "union" code [done]
93 # 13. Implement support for plugins [done]
94 # 14. Don't generate code for empty operations (cf: exceptions without members)
95 # 15. Generate code to display Enums numerically and symbolically [done]
96 # 16. Place structs/unions in subtrees
97 # 17. Recursive struct and union handling [done]
98 # 18. Improve variable naming for display (eg: structs, unions etc) [done]
100 # Also test, Test, TEST
107 # For every operation and attribute do
108 # For return val and all parameters do
109 # find basic IDL type for each parameter
111 # output exception handling code
112 # output attribute handling code
116 class wireshark_gen_C:
120 # Turn DEBUG stuff on/off
126 # Some string constants for our templates
128 c_u_octet8 = "guint64 u_octet8;"
129 c_s_octet8 = "gint64 s_octet8;"
130 c_u_octet4 = "guint32 u_octet4;"
131 c_s_octet4 = "gint32 s_octet4;"
132 c_u_octet2 = "guint16 u_octet2;"
133 c_s_octet2 = "gint16 s_octet2;"
134 c_u_octet1 = "guint8 u_octet1;"
135 c_s_octet1 = "gint8 s_octet1;"
137 c_float = "gfloat my_float;"
138 c_double = "gdouble my_double;"
140 c_seq = "const gchar *seq = NULL;" # pointer to buffer of gchars
141 c_i = "guint32 i_"; # loop index
142 c_i_lim = "guint32 u_octet4_loop_"; # loop limit
143 c_u_disc = "guint32 disc_u_"; # unsigned int union discriminant variable name (enum)
144 c_s_disc = "gint32 disc_s_"; # signed int union discriminant variable name (other cases, except Enum)
150 def __init__(self, st, protocol_name, dissector_name ,description):
151 self.st = output.Stream(tempfile.TemporaryFile(),4) # for first pass only
153 self.st_save = st # where 2nd pass should go
154 self.protoname = protocol_name # Protocol Name (eg: ECHO)
155 self.dissname = dissector_name # Dissector name (eg: echo)
156 self.description = description # Detailed Protocol description (eg: Echo IDL Example)
157 self.exlist = [] # list of exceptions used in operations.
158 #self.curr_sname # scoped name of current opnode or exnode I am visiting, used for generating "C" var declares
159 self.fn_hash = {} # top level hash to contain key = function/exception and val = list of variable declarations
161 self.fn_hash_built = 0 # flag to indicate the 1st pass is complete, and the fn_hash is correctly
162 # populated with operations/vars and exceptions/vars
168 # Main entry point, controls sequence of
173 def genCode(self,oplist, atlist, enlist, stlist, unlist): # operation,attribute,enums,struct and union lists
176 self.genHelpers(oplist,stlist,unlist) # sneaky .. call it now, to populate the fn_hash
177 # so when I come to that operation later, I have the variables to
180 self.genExceptionHelpers(oplist) # sneaky .. call it now, to populate the fn_hash
181 # so when I come to that exception later, I have the variables to
184 self.genAttributeHelpers(atlist) # sneaky .. call it now, to populate the fn_hash
185 # so when I come to that exception later, I have the variables to
189 self.fn_hash_built = 1 # DONE, so now I know , see genOperation()
191 self.st = self.st_save
192 self.genHeader() # initial dissector comments
193 self.genEthCopyright() # Wireshark Copyright comments.
194 self.genGPL() # GPL license
198 self.genDeclares(oplist,atlist,enlist,stlist,unlist)
199 if (len(atlist) > 0):
200 self.genAtList(atlist) # string constant declares for Attributes
201 if (len(enlist) > 0):
202 self.genEnList(enlist) # string constant declares for Enums
205 self.genExceptionHelpers(oplist) # helper function to decode user exceptions that have members
206 self.genExceptionDelegator(oplist) # finds the helper function to decode a user exception
207 if (len(atlist) > 0):
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(oplist, atlist, stlist, unlist)
218 self.gen_proto_reg_handoff(oplist)
219 # All the dissectors are now built-in
220 #self.gen_plugin_register()
222 #self.dumpvars() # debug
230 # Generate Standard Wireshark Header Comments
235 self.st.out(self.template_Header,dissector_name=self.dissname)
237 print "XXX genHeader"
245 # Wireshark Copyright Info
249 def genEthCopyright(self):
251 print "XXX genEthCopyright"
252 self.st.out(self.template_wireshark_copyright)
262 def genModelines(self):
264 print "XXX genModelines"
266 self.st.out(self.template_Modelines)
280 self.st.out(self.template_GPL)
289 def genIncludes(self):
291 print "XXX genIncludes"
293 self.st.out(self.template_Includes)
298 # Generate hf variables for operation filters
300 # in: opnode ( an operation node)
303 def genOpDeclares(self, op):
305 print "XXX genOpDeclares"
306 print "XXX return type = " , op.returnType().kind()
308 sname = self.namespace(op, "_")
311 if (rt.kind() != idltype.tk_void):
312 if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
313 #self.get_CDR_alias(rt, rt.name() )
314 if (rt.unalias().kind() == idltype.tk_sequence):
315 self.st.out(self.template_hf, name=sname + "_return_loop")
316 if (self.isSeqNativeType(rt.unalias().seqType())):
317 self.st.out(self.template_hf, name=sname + "_return")
318 elif ((rt.unalias().kind() != idltype.tk_struct) and \
319 (rt.unalias().kind() != idltype.tk_objref) and \
320 (rt.unalias().kind() != idltype.tk_any)):
321 self.st.out(self.template_hf, name=sname + "_return")
323 elif ((rt.kind() != idltype.tk_struct) and \
324 (rt.kind() != idltype.tk_objref) and \
325 (rt.kind() != idltype.tk_union) and \
326 (rt.kind() != idltype.tk_any)):
327 self.st.out(self.template_hf, name=sname + "_return")
329 for p in op.parameters():
330 if (p.paramType().unalias().kind() == idltype.tk_sequence):
331 self.st.out(self.template_hf, name=sname + "_" + p.identifier() + "_loop")
332 if (self.isSeqNativeType(p.paramType().unalias().seqType())):
333 self.st.out(self.template_hf, name=sname + "_" + p.identifier())
334 elif ((p.paramType().unalias().kind() != idltype.tk_any) and \
335 (p.paramType().unalias().kind() != idltype.tk_struct) and \
336 (p.paramType().unalias().kind() != idltype.tk_objref) and \
337 (p.paramType().unalias().kind() != idltype.tk_union)):
338 if (p.paramType().unalias().kind() == idltype.tk_wchar):
339 self.st.out(self.template_hf, name=sname + "_" + p.identifier() + "_len")
340 self.st.out(self.template_hf, name=sname + "_" + p.identifier())
345 # Generate hf variables for attributes
347 # in: at ( an attribute)
350 def genAtDeclares(self, at):
352 print "XXX genAtDeclares"
354 for decl in at.declarators():
355 sname = self.namespace(decl, "_")
357 self.st.out(self.template_hf, name="get" + "_" + sname + "_" + decl.identifier())
358 if not at.readonly():
359 self.st.out(self.template_hf, name="set" + "_" + sname + "_" + decl.identifier())
364 # Generate hf variables for structs
369 def genStDeclares(self, st):
371 print "XXX genStDeclares"
373 sname = self.namespace(st, "_")
375 for m in st.members():
376 if ((self.isSeqNativeType(m.memberType())) or (m.memberType().unalias().kind() == idltype.tk_sequence) or \
377 (m.memberType().unalias().kind() == idltype.tk_alias)):
378 for decl in m.declarators():
379 if (m.memberType().unalias().kind() == idltype.tk_sequence):
380 self.st.out(self.template_hf, name=sname + "_" + decl.identifier() + "_loop")
381 if (self.isSeqNativeType(m.memberType().unalias().seqType())):
382 self.st.out(self.template_hf, name=sname + "_" + decl.identifier())
384 if (m.memberType().unalias().kind() == idltype.tk_wchar):
385 self.st.out(self.template_hf, name=sname + "_" + decl.identifier() + "_len")
386 self.st.out(self.template_hf, name=sname + "_" + decl.identifier())
391 # Generate hf variables for user exception filters
393 # in: exnode ( an exception node)
396 def genExDeclares(self,ex):
398 print "XXX genExDeclares"
400 sname = self.namespace(ex, "_")
402 for m in ex.members():
403 for decl in m.declarators():
404 if (m.memberType().unalias().kind() == idltype.tk_sequence):
405 if (self.isSeqNativeType(m.memberType().unalias().seqType())):
406 self.st.out(self.template_hf, name=sname + "_" + decl.identifier())
407 self.st.out(self.template_hf, name=sname + "_" + decl.identifier() + "_loop")
408 elif (m.memberType().unalias().kind() != idltype.tk_struct):
409 self.st.out(self.template_hf, name=sname + "_" + decl.identifier())
414 # Generate hf variables for union filters
419 def genUnionDeclares(self,un):
421 print "XXX genUnionDeclares"
423 sname = self.namespace(un, "_")
424 self.st.out(self.template_hf, name=sname + "_" + un.identifier())
426 for uc in un.cases(): # for all UnionCase objects in this union
427 for cl in uc.labels(): # for all Caselabel objects in this UnionCase
428 if (uc.caseType().unalias().kind() == idltype.tk_sequence):
429 self.st.out(self.template_hf, name=sname + "_" + uc.declarator().identifier() + "_loop")
430 if (self.isSeqNativeType(uc.caseType().unalias().seqType())):
431 self.st.out(self.template_hf, name=sname + "_" + uc.declarator().identifier())
432 elif (self.isSeqNativeType(uc.caseType())):
433 if (uc.caseType().unalias().kind() == idltype.tk_wchar):
434 self.st.out(self.template_hf, name=sname + "_" + uc.declarator().identifier() + "_len")
435 self.st.out(self.template_hf, name=sname + "_" + uc.declarator().identifier())
438 # genExpertInfoDeclares()
440 # Generate ei variables for expert info filters
443 def genExpertInfoDeclares(self):
445 print "XXX genExpertInfoDeclares"
447 self.st.out(self.template_proto_register_ei_filters, dissector_name=self.dissname)
452 # generate function prototypes if required
454 # Currently this is used for struct and union helper function declarations.
458 def genDeclares(self,oplist,atlist,enlist,stlist,unlist):
460 print "XXX genDeclares"
462 # prototype for operation filters
463 self.st.out(self.template_hf_operations)
465 #operation specific filters
466 if (len(oplist) > 0):
467 self.st.out(self.template_proto_register_op_filter_comment)
469 self.genOpDeclares(op)
472 if (len(atlist) > 0):
473 self.st.out(self.template_proto_register_at_filter_comment)
475 self.genAtDeclares(at)
478 if (len(stlist) > 0):
479 self.st.out(self.template_proto_register_st_filter_comment)
481 self.genStDeclares(st)
483 # exception List filters
484 exlist = self.get_exceptionList(oplist) # grab list of exception nodes
485 if (len(exlist) > 0):
486 self.st.out(self.template_proto_register_ex_filter_comment)
488 if (ex.members()): # only if has members
489 self.genExDeclares(ex)
492 if (len(unlist) > 0):
493 self.st.out(self.template_proto_register_un_filter_comment)
495 self.genUnionDeclares(un)
498 self.genExpertInfoDeclares()
500 # prototype for start_dissecting()
502 self.st.out(self.template_prototype_start_dissecting)
507 self.st.out(self.template_prototype_struct_start)
510 sname = self.namespace(st, "_")
511 self.st.out(self.template_prototype_struct_body, stname=st.repoId(),name=sname)
513 self.st.out(self.template_prototype_struct_end)
517 self.st.out(self.template_prototype_union_start)
519 sname = self.namespace(un, "_")
520 self.st.out(self.template_prototype_union_body, unname=un.repoId(),name=sname)
521 self.st.out(self.template_prototype_union_end)
529 def genPrototype(self):
530 self.st.out(self.template_prototype, dissector_name=self.dissname)
537 def genProtocol(self):
538 self.st.out(self.template_protocol, dissector_name=self.dissname)
539 self.st.out(self.template_init_boundary)
545 def genMainEntryStart(self,oplist):
546 self.st.out(self.template_main_dissector_start, dissname=self.dissname, disprot=self.protoname)
548 self.st.out(self.template_main_dissector_switch_msgtype_start)
549 self.st.out(self.template_main_dissector_switch_msgtype_start_request_reply)
557 def genMainEntryEnd(self):
559 self.st.out(self.template_main_dissector_switch_msgtype_end_request_reply)
561 self.st.out(self.template_main_dissector_switch_msgtype_all_other_msgtype)
563 self.st.out(self.template_main_dissector_end)
571 # out: C code for IDL attribute decalarations.
573 # NOTE: Mapping of attributes to operation(function) names is tricky.
575 # The actual accessor function names are language-mapping specific. The attribute name
576 # is subject to OMG IDL's name scoping rules; the accessor function names are
577 # guaranteed not to collide with any legal operation names specifiable in OMG IDL.
581 # static const char get_Penguin_Echo_get_width_at[] = "get_width" ;
582 # static const char set_Penguin_Echo_set_width_at[] = "set_width" ;
586 # static const char get_Penguin_Echo_get_width_at[] = "_get_width" ;
587 # static const char set_Penguin_Echo_set_width_at[] = "_set_width" ;
589 # TODO: Implement some language dependent templates to handle naming conventions
590 # language <=> attribute. for C, C++. Java etc
592 # OR, just add a runtime GUI option to select language binding for attributes -- FS
596 # ie: def genAtlist(self,atlist,language)
601 def genAtList(self,atlist):
602 self.st.out(self.template_comment_attributes_start)
605 for i in n.declarators(): #
606 sname = self.namespace(i, "_")
607 atname = i.identifier()
608 self.st.out(self.template_attributes_declare_Java_get, sname=sname, atname=atname)
610 self.st.out(self.template_attributes_declare_Java_set, sname=sname, atname=atname)
612 self.st.out(self.template_comment_attributes_end)
620 # out: C code for IDL Enum decalarations using "static const value_string" template
625 def genEnList(self,enlist):
627 self.st.out(self.template_comment_enums_start)
630 sname = self.namespace(enum, "_")
632 self.st.out(self.template_comment_enum_comment, ename=enum.repoId())
633 self.st.out(self.template_value_string_start, valstringname=sname)
634 for enumerator in enum.enumerators():
635 self.st.out(self.template_value_string_entry, intval=str(self.valFromEnum(enum,enumerator)), description=enumerator.identifier())
638 #atname = n.identifier()
639 self.st.out(self.template_value_string_end, valstringname=sname)
641 self.st.out(self.template_comment_enums_end)
653 # genExceptionDelegator
657 # out: C code for User exception delegator
663 def genExceptionDelegator(self,oplist):
665 self.st.out(self.template_main_exception_delegator_start)
668 exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
672 print "XXX Exception " , ex.repoId()
673 print "XXX Exception Identifier" , ex.identifier()
674 print "XXX Exception Scoped Name" , ex.scopedName()
676 if (ex.members()): # only if has members
677 sname = self.namespace(ex, "_")
679 self.st.out(self.template_ex_delegate_code, sname=sname, exname=ex.repoId())
682 self.st.out(self.template_main_exception_delegator_end)
686 # genAttribueHelpers()
688 # Generate private helper functions to decode Attributes.
692 # For readonly attribute - generate get_xxx()
693 # If NOT readonly attribute - also generate set_xxx()
696 def genAttributeHelpers(self,atlist):
698 print "XXX genAttributeHelpers: atlist = ", atlist
700 self.st.out(self.template_attribute_helpers_start)
702 for attrib in atlist:
703 for decl in attrib.declarators():
704 self.genAtHelper(attrib,decl,"get") # get accessor
705 if not attrib.readonly():
706 self.genAtHelper(attrib,decl,"set") # set accessor
708 self.st.out(self.template_attribute_helpers_end)
713 # Generate private helper functions to decode an attribute
715 # in: at - attribute node
716 # in: decl - declarator belonging to this attribute
717 # in: order - to generate a "get" or "set" helper
719 def genAtHelper(self,attrib,decl,order):
721 print "XXX genAtHelper"
723 sname = order + "_" + self.namespace(decl, "_") # must use set or get prefix to avoid collision
724 self.curr_sname = sname # update current opnode/exnode scoped name
726 if not self.fn_hash_built:
727 self.fn_hash[sname] = [] # init empty list as val for this sname key
728 # but only if the fn_hash is not already built
730 self.st.out(self.template_attribute_helper_function_start, sname=sname, atname=decl.repoId())
733 if (len(self.fn_hash[sname]) > 0):
734 self.st.out(self.template_helper_function_vars_start)
735 self.dumpCvars(sname)
736 self.st.out(self.template_helper_function_vars_end_item )
738 self.getCDR(attrib.attrType(), sname + "_" + decl.identifier() )
741 self.st.out(self.template_attribute_helper_function_end)
746 # genExceptionHelpers()
748 # Generate private helper functions to decode Exceptions used
755 def genExceptionHelpers(self,oplist):
756 exlist = self.get_exceptionList(oplist) # grab list of exception nodes
758 print "XXX genExceptionHelpers: exlist = ", exlist
760 self.st.out(self.template_exception_helpers_start)
762 if (ex.members()): # only if has members
763 #print "XXX Exception = " + ex.identifier()
766 self.st.out(self.template_exception_helpers_end)
772 # Generate private helper functions to decode User Exceptions
774 # in: exnode ( an exception node)
777 def genExHelper(self,ex):
779 print "XXX genExHelper"
781 # check to see if we need an item
783 for m in ex.members():
784 if (self.isItemVarType(m.memberType())):
789 sname = self.namespace(ex, "_")
790 self.curr_sname = sname # update current opnode/exnode scoped name
791 if not self.fn_hash_built:
792 self.fn_hash[sname] = [] # init empty list as val for this sname key
793 # but only if the fn_hash is not already built
796 self.st.out(self.template_exception_helper_function_start_item, sname=sname, exname=ex.repoId())
798 self.st.out(self.template_exception_helper_function_start_no_item, sname=sname, exname=ex.repoId())
801 if (len(self.fn_hash[sname]) > 0):
802 self.st.out(self.template_helper_function_vars_start)
803 self.dumpCvars(sname)
805 self.st.out(self.template_helper_function_vars_end_item )
807 self.st.out(self.template_helper_function_vars_end )
809 for m in ex.members():
811 print "XXX genExhelper, member = ", m, "member type = ", m.memberType()
813 for decl in m.declarators():
815 print "XXX genExhelper, d = ", decl
817 if decl.sizes(): # an array
818 indices = self.get_indices_from_sizes(decl.sizes())
819 string_indices = '%i ' % indices # convert int to string
820 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
821 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
822 self.addvar(self.c_i + decl.identifier() + ";")
825 self.getCDR(m.memberType(), sname + "_" + decl.identifier() )
828 self.st.out(self.template_get_CDR_array_end)
832 self.getCDR(m.memberType(), sname + "_" + decl.identifier() )
835 self.st.out(self.template_exception_helper_function_end)
841 # Generate private helper functions for each IDL operation.
842 # Generate private helper functions for each IDL struct.
843 # Generate private helper functions for each IDL union.
846 # in: oplist, stlist, unlist
850 def genHelpers(self,oplist,stlist,unlist):
852 self.genOperation(op)
854 self.genStructHelper(st)
856 self.genUnionHelper(un)
861 # Generate private helper functions for a specificIDL operation.
866 def genOperation(self,opnode):
868 print "XXX genOperation called"
870 sname = self.namespace(opnode, "_")
871 if not self.fn_hash_built:
872 self.fn_hash[sname] = [] # init empty list as val for this sname key
873 # but only if the fn_hash is not already built
875 self.curr_sname = sname # update current opnode's scoped name
876 opname = opnode.identifier()
878 self.st.out(self.template_helper_function_comment, repoid=opnode.repoId() )
880 self.st.out(self.template_helper_function_start, sname=sname)
883 if (len(self.fn_hash[sname]) > 0):
884 self.st.out(self.template_helper_function_vars_start)
885 self.dumpCvars(sname)
886 self.st.out(self.template_helper_function_vars_end_item )
888 self.st.out(self.template_helper_switch_msgtype_start)
890 self.st.out(self.template_helper_switch_msgtype_request_start)
892 self.genOperationRequest(opnode)
893 self.st.out(self.template_helper_switch_msgtype_request_end)
896 self.st.out(self.template_helper_switch_msgtype_reply_start)
899 self.st.out(self.template_helper_switch_rep_status_start)
902 self.st.out(self.template_helper_switch_msgtype_reply_no_exception_start)
904 self.genOperationReply(opnode)
905 self.st.out(self.template_helper_switch_msgtype_reply_no_exception_end)
908 self.st.out(self.template_helper_switch_msgtype_reply_user_exception_start)
910 self.genOpExceptions(opnode)
911 self.st.out(self.template_helper_switch_msgtype_reply_user_exception_end)
914 self.st.out(self.template_helper_switch_msgtype_reply_default_start, dissector_name=self.dissname)
915 self.st.out(self.template_helper_switch_msgtype_reply_default_end)
917 self.st.out(self.template_helper_switch_rep_status_end)
921 self.st.out(self.template_helper_switch_msgtype_default_start, dissector_name=self.dissname)
922 self.st.out(self.template_helper_switch_msgtype_default_end)
924 self.st.out(self.template_helper_switch_msgtype_end)
928 self.st.out(self.template_helper_function_end, sname=sname)
934 # Decode function parameters for a GIOP request message
938 def genOperationRequest(self,opnode):
939 for p in opnode.parameters():
942 print "XXX parameter = " ,p
943 print "XXX parameter type = " ,p.paramType()
944 print "XXX parameter type kind = " ,p.paramType().kind()
946 self.getCDR(p.paramType(), self.curr_sname + "_" + p.identifier())
950 # Decode function parameters for a GIOP reply message
954 def genOperationReply(self,opnode):
956 rt = opnode.returnType() # get return type
958 print "XXX genOperationReply"
959 print "XXX opnode = " , opnode
960 print "XXX return type = " , rt
961 print "XXX return type.unalias = " , rt.unalias()
962 print "XXX return type.kind() = " , rt.kind();
964 sname = self.namespace(opnode, "_")
966 if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
967 #self.getCDR(rt.decl().alias().aliasType(),"dummy") # return value maybe a typedef
968 self.get_CDR_alias(rt, sname + "_return" )
969 #self.get_CDR_alias(rt, rt.name() )
972 self.getCDR(rt, sname + "_return") # return value is NOT an alias
974 for p in opnode.parameters():
975 if p.is_out(): # out or inout
976 self.getCDR(p.paramType(), self.curr_sname + "_" + p.identifier())
978 #self.st.dec_indent()
980 def genOpExceptions(self,opnode):
981 for ex in opnode.raises():
984 for m in ex.members():
986 #print m.memberType(), m.memberType().kind()
988 # Delegator for Operations
991 def genOpDelegator(self,oplist):
993 iname = "/".join(op.scopedName()[:-1])
994 opname = op.identifier()
995 sname = self.namespace(op, "_")
996 self.st.out(self.template_op_delegate_code, interface=iname, sname=sname, opname=opname)
999 # Delegator for Attributes
1002 def genAtDelegator(self,atlist):
1004 for i in a.declarators():
1005 atname = i.identifier()
1006 sname = self.namespace(i, "_")
1007 self.st.out(self.template_at_delegate_code_get, sname=sname)
1008 if not a.readonly():
1009 self.st.out(self.template_at_delegate_code_set, sname=sname)
1013 # Add a variable declaration to the hash of list
1016 def addvar(self, var):
1017 if not ( var in self.fn_hash[self.curr_sname] ):
1018 self.fn_hash[self.curr_sname].append(var)
1021 # Print the variable declaration from the hash of list
1026 for fn in self.fn_hash.keys():
1028 for v in self.fn_hash[fn]:
1031 # Print the "C" variable declaration from the hash of list
1032 # for a given scoped operation name (eg: tux_penguin_eat)
1036 def dumpCvars(self, sname):
1037 for v in self.fn_hash[sname]:
1042 # Given an enum node, and a enumerator node, return
1043 # the enumerator's numerical value.
1045 # eg: enum Color {red,green,blue} should return
1049 def valFromEnum(self,enumNode, enumeratorNode):
1051 print "XXX valFromEnum, enumNode = ", enumNode, " from ", enumNode.repoId()
1052 print "XXX valFromEnum, enumeratorNode = ", enumeratorNode, " from ", enumeratorNode.repoId()
1054 if isinstance(enumeratorNode,idlast.Enumerator):
1055 value = enumNode.enumerators().index(enumeratorNode)
1072 ## tk_Principal = 13
1083 ## tk_ulonglong = 24
1084 ## tk_longdouble = 25
1089 ## tk_value_box = 30
1091 ## tk_abstract_interface = 32
1097 # Return true for "native" datatypes that will generate a direct proto_tree_add_xxx
1098 # call for a sequence. Used to determine if a separate hf variable is needed for
1099 # the loop over the sequence
1101 def isSeqNativeType(self,type):
1103 pt = type.unalias().kind() # param CDR type
1106 print "XXX isSeqNativeType: kind = " , pt
1108 if pt == idltype.tk_ulong:
1110 elif pt == idltype.tk_longlong:
1112 elif pt == idltype.tk_ulonglong:
1114 elif pt == idltype.tk_short:
1116 elif pt == idltype.tk_long:
1118 elif pt == idltype.tk_ushort:
1120 elif pt == idltype.tk_float:
1122 elif pt == idltype.tk_double:
1124 elif pt == idltype.tk_boolean:
1126 elif pt == idltype.tk_octet:
1128 elif pt == idltype.tk_enum:
1130 elif pt == idltype.tk_string:
1132 elif pt == idltype.tk_wstring:
1134 elif pt == idltype.tk_wchar:
1136 elif pt == idltype.tk_char:
1141 def isItemVarType(self,type):
1143 pt = type.unalias().kind() # param CDR type
1146 print "XXX isItemVarType: kind = " , pt
1148 if pt == idltype.tk_fixed:
1150 elif pt == idltype.tk_any:
1152 elif pt == idltype.tk_struct:
1154 elif pt == idltype.tk_sequence:
1163 # This is the main "iterator" function. It takes a node, and tries to output
1164 # a get_CDR_XXX accessor method(s). It can call itself multiple times
1165 # if I find nested structures etc.
1168 def getCDR(self,type,name="fred"):
1170 pt = type.unalias().kind() # param CDR type
1171 pn = name # param name
1174 print "XXX getCDR: kind = " , pt
1175 print "XXX getCDR: name = " , pn
1177 if pt == idltype.tk_ulong:
1178 self.get_CDR_ulong(pn)
1179 elif pt == idltype.tk_longlong:
1180 self.get_CDR_longlong(pn)
1181 elif pt == idltype.tk_ulonglong:
1182 self.get_CDR_ulonglong(pn)
1183 elif pt == idltype.tk_void:
1184 self.get_CDR_void(pn)
1185 elif pt == idltype.tk_short:
1186 self.get_CDR_short(pn)
1187 elif pt == idltype.tk_long:
1188 self.get_CDR_long(pn)
1189 elif pt == idltype.tk_ushort:
1190 self.get_CDR_ushort(pn)
1191 elif pt == idltype.tk_float:
1192 self.get_CDR_float(pn)
1193 elif pt == idltype.tk_double:
1194 self.get_CDR_double(pn)
1195 elif pt == idltype.tk_fixed:
1196 self.get_CDR_fixed(type.unalias(),pn)
1197 elif pt == idltype.tk_boolean:
1198 self.get_CDR_boolean(pn)
1199 elif pt == idltype.tk_char:
1200 self.get_CDR_char(pn)
1201 elif pt == idltype.tk_octet:
1202 self.get_CDR_octet(pn)
1203 elif pt == idltype.tk_any:
1204 self.get_CDR_any(pn)
1205 elif pt == idltype.tk_string:
1206 self.get_CDR_string(pn)
1207 elif pt == idltype.tk_wstring:
1208 self.get_CDR_wstring(pn)
1209 elif pt == idltype.tk_wchar:
1210 self.get_CDR_wchar(pn)
1211 elif pt == idltype.tk_enum:
1213 self.get_CDR_enum(pn,type)
1214 #self.get_CDR_enum(pn)
1216 elif pt == idltype.tk_struct:
1217 self.get_CDR_struct(type,pn)
1218 elif pt == idltype.tk_TypeCode: # will I ever get here ?
1219 self.get_CDR_TypeCode(pn)
1220 elif pt == idltype.tk_sequence:
1221 if type.unalias().seqType().kind() == idltype.tk_octet:
1222 self.get_CDR_sequence_octet(type,pn)
1224 self.get_CDR_sequence(type,pn)
1225 elif pt == idltype.tk_objref:
1226 self.get_CDR_objref(type,pn)
1227 elif pt == idltype.tk_array:
1228 pn = pn # Supported elsewhere
1229 elif pt == idltype.tk_union:
1230 self.get_CDR_union(type,pn)
1231 elif pt == idltype.tk_alias:
1233 print "XXXXX Alias type XXXXX " , type
1234 self.get_CDR_alias(type,pn)
1236 self.genWARNING("Unknown typecode = " + '%i ' % pt) # put comment in source code
1240 # get_CDR_XXX methods are here ..
1245 def get_CDR_ulong(self,pn):
1246 self.st.out(self.template_get_CDR_ulong, hfname=pn)
1248 def get_CDR_short(self,pn):
1249 self.st.out(self.template_get_CDR_short, hfname=pn)
1251 def get_CDR_void(self,pn):
1252 self.st.out(self.template_get_CDR_void, hfname=pn)
1254 def get_CDR_long(self,pn):
1255 self.st.out(self.template_get_CDR_long, hfname=pn)
1257 def get_CDR_ushort(self,pn):
1258 self.st.out(self.template_get_CDR_ushort, hfname=pn)
1260 def get_CDR_float(self,pn):
1261 self.st.out(self.template_get_CDR_float, hfname=pn)
1263 def get_CDR_double(self,pn):
1264 self.st.out(self.template_get_CDR_double, hfname=pn)
1266 def get_CDR_longlong(self,pn):
1267 self.st.out(self.template_get_CDR_longlong, hfname=pn)
1269 def get_CDR_ulonglong(self,pn):
1270 self.st.out(self.template_get_CDR_ulonglong, hfname=pn)
1272 def get_CDR_boolean(self,pn):
1273 self.st.out(self.template_get_CDR_boolean, hfname=pn)
1275 def get_CDR_fixed(self,type,pn):
1277 print "XXXX calling get_CDR_fixed, type = ", type
1278 print "XXXX calling get_CDR_fixed, type.digits() = ", type.digits()
1279 print "XXXX calling get_CDR_fixed, type.scale() = ", type.scale()
1281 string_digits = '%i ' % type.digits() # convert int to string
1282 string_scale = '%i ' % type.scale() # convert int to string
1283 string_length = '%i ' % self.dig_to_len(type.digits()) # how many octets to hilight for a number of digits
1285 self.st.out(self.template_get_CDR_fixed, hfname=pn, digits=string_digits, scale=string_scale, length=string_length )
1286 self.addvar(self.c_seq)
1289 def get_CDR_char(self,pn):
1290 self.st.out(self.template_get_CDR_char, hfname=pn)
1292 def get_CDR_octet(self,pn):
1293 self.st.out(self.template_get_CDR_octet, hfname=pn)
1295 def get_CDR_any(self,pn):
1296 self.st.out(self.template_get_CDR_any, varname=pn)
1298 def get_CDR_enum(self,pn,type):
1299 #self.st.out(self.template_get_CDR_enum, hfname=pn)
1300 sname = self.namespace(type.unalias(), "_")
1301 self.st.out(self.template_get_CDR_enum_symbolic, valstringarray=sname,hfname=pn)
1302 self.addvar(self.c_u_octet4)
1304 def get_CDR_string(self,pn):
1305 self.st.out(self.template_get_CDR_string, hfname=pn)
1307 def get_CDR_wstring(self,pn):
1308 self.st.out(self.template_get_CDR_wstring, hfname=pn)
1309 self.addvar(self.c_u_octet4)
1310 self.addvar(self.c_seq)
1312 def get_CDR_wchar(self,pn):
1313 self.st.out(self.template_get_CDR_wchar, hfname=pn)
1314 self.addvar(self.c_s_octet1)
1315 self.addvar(self.c_seq)
1317 def get_CDR_TypeCode(self,pn):
1318 self.st.out(self.template_get_CDR_TypeCode, varname=pn)
1319 self.addvar(self.c_u_octet4)
1321 def get_CDR_objref(self,type,pn):
1322 self.st.out(self.template_get_CDR_object)
1324 def get_CDR_union(self,type,pn):
1326 print "XXX Union type =" , type, " pn = ",pn
1327 print "XXX Union type.decl()" , type.decl()
1328 print "XXX Union Scoped Name" , type.scopedName()
1330 # If I am a typedef union {..}; node then find the union node
1332 if isinstance(type.decl(), idlast.Declarator):
1333 ntype = type.decl().alias().aliasType().decl()
1335 ntype = type.decl() # I am a union node
1338 print "XXX Union ntype =" , ntype
1340 sname = self.namespace(ntype, "_")
1341 self.st.out(self.template_union_start, name=sname )
1343 # Output a call to the union helper function so I can handle recursive union also.
1345 self.st.out(self.template_decode_union,name=sname)
1347 self.st.out(self.template_union_end, name=sname )
1352 # This takes a node, and tries to output the appropriate item for the
1356 def getCDR_hf(self,type,desc,filter,hf_name="fred"):
1358 pt = type.unalias().kind() # param CDR type
1359 pn = hf_name # param name
1362 print "XXX getCDR_hf: kind = " , pt
1363 print "XXX getCDR_hf: name = " , pn
1365 if pt == idltype.tk_ulong:
1366 self.get_CDR_ulong_hf(pn, desc, filter, self.dissname)
1367 elif pt == idltype.tk_longlong:
1368 self.get_CDR_longlong_hf(pn, desc, filter, self.dissname)
1369 elif pt == idltype.tk_ulonglong:
1370 self.get_CDR_ulonglong_hf(pn, desc, filter, self.dissname)
1371 elif pt == idltype.tk_void:
1372 pt = pt # no hf_ variables needed
1373 elif pt == idltype.tk_short:
1374 self.get_CDR_short_hf(pn, desc, filter, self.dissname)
1375 elif pt == idltype.tk_long:
1376 self.get_CDR_long_hf(pn, desc, filter, self.dissname)
1377 elif pt == idltype.tk_ushort:
1378 self.get_CDR_ushort_hf(pn, desc, filter, self.dissname)
1379 elif pt == idltype.tk_float:
1380 self.get_CDR_float_hf(pn, desc, filter, self.dissname)
1381 elif pt == idltype.tk_double:
1382 self.get_CDR_double_hf(pn, desc, filter, self.dissname)
1383 elif pt == idltype.tk_fixed:
1384 self.get_CDR_fixed_hf(pn, desc, filter, self.dissname)
1385 elif pt == idltype.tk_boolean:
1386 self.get_CDR_boolean_hf(pn, desc, filter, self.dissname)
1387 elif pt == idltype.tk_char:
1388 self.get_CDR_char_hf(pn, desc, filter, self.dissname)
1389 elif pt == idltype.tk_octet:
1390 self.get_CDR_octet_hf(pn, desc, filter, self.dissname)
1391 elif pt == idltype.tk_any:
1392 pt = pt # no hf_ variables needed
1393 elif pt == idltype.tk_string:
1394 self.get_CDR_string_hf(pn, desc, filter, self.dissname)
1395 elif pt == idltype.tk_wstring:
1396 self.get_CDR_wstring_hf(pn, desc, filter, self.dissname)
1397 elif pt == idltype.tk_wchar:
1398 self.get_CDR_wchar_hf(pn, desc, filter, self.dissname)
1399 elif pt == idltype.tk_enum:
1400 self.get_CDR_enum_hf(pn, type, desc, filter, self.dissname)
1401 elif pt == idltype.tk_struct:
1402 pt = pt # no hf_ variables needed (should be already contained in struct members)
1403 elif pt == idltype.tk_TypeCode: # will I ever get here ?
1404 self.get_CDR_TypeCode_hf(pn, desc, filter, self.dissname)
1405 elif pt == idltype.tk_sequence:
1406 if type.unalias().seqType().kind() == idltype.tk_octet:
1407 self.get_CDR_sequence_octet_hf(type, pn, desc, filter, self.dissname)
1409 self.get_CDR_sequence_hf(type, pn, desc, filter, self.dissname)
1410 elif pt == idltype.tk_objref:
1411 pt = pt # no object specific hf_ variables used, use generic ones from giop dissector
1412 elif pt == idltype.tk_array:
1413 pt = pt # Supported elsewhere
1414 elif pt == idltype.tk_union:
1415 pt = pt # no hf_ variables needed (should be already contained in union members)
1416 elif pt == idltype.tk_alias:
1418 print "XXXXX Alias type hf XXXXX " , type
1419 self.get_CDR_alias_hf(type,desc,filter,pn)
1421 self.genWARNING("Unknown typecode = " + '%i ' % pt) # put comment in source code
1424 # get_CDR_XXX_hf methods are here ..
1429 def get_CDR_ulong_hf(self,pn,desc,filter,diss):
1430 self.st.out(self.template_get_CDR_ulong_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1432 def get_CDR_short_hf(self,pn,desc,filter,diss):
1433 self.st.out(self.template_get_CDR_short_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1435 def get_CDR_long_hf(self,pn,desc,filter,diss):
1436 self.st.out(self.template_get_CDR_long_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1438 def get_CDR_ushort_hf(self,pn,desc,filter,diss):
1439 self.st.out(self.template_get_CDR_ushort_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1441 def get_CDR_float_hf(self,pn,desc,filter,diss):
1442 self.st.out(self.template_get_CDR_float_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1444 def get_CDR_double_hf(self,pn,desc,filter,diss):
1445 self.st.out(self.template_get_CDR_double_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1447 def get_CDR_fixed_hf(self,pn,desc,filter,diss):
1448 self.st.out(self.template_get_CDR_fixed_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1450 def get_CDR_longlong_hf(self,pn,desc,filter,diss):
1451 self.st.out(self.template_get_CDR_longlong_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1453 def get_CDR_ulonglong_hf(self,pn,desc,filter,diss):
1454 self.st.out(self.template_get_CDR_ulonglong_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1456 def get_CDR_boolean_hf(self,pn,desc,filter,diss):
1457 self.st.out(self.template_get_CDR_boolean_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1459 def get_CDR_char_hf(self,pn,desc,filter,diss):
1460 self.st.out(self.template_get_CDR_char_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1462 def get_CDR_octet_hf(self,pn,desc,filter,diss):
1463 self.st.out(self.template_get_CDR_octet_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1465 def get_CDR_enum_hf(self,pn,type,desc,filter,diss):
1466 sname = self.namespace(type.unalias(), "_")
1467 self.st.out(self.template_get_CDR_enum_symbolic_hf, valstringarray=sname,hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1469 def get_CDR_string_hf(self,pn,desc,filter,diss):
1470 self.st.out(self.template_get_CDR_string_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1472 def get_CDR_wstring_hf(self,pn,desc,filter,diss):
1473 self.st.out(self.template_get_CDR_wstring_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1474 # self.addvar(self.c_u_octet4)
1475 # self.addvar(self.c_seq)
1477 def get_CDR_wchar_hf(self,pn,desc,filter,diss):
1478 self.st.out(self.template_get_CDR_wchar_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1479 # self.addvar(self.c_s_octet1)
1480 # self.addvar(self.c_seq)
1482 def get_CDR_TypeCode_hf(self,pn,desc,filter,diss):
1483 self.st.out(self.template_get_CDR_TypeCode_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1485 def get_CDR_sequence_octet_hf(self,type,pn,desc,filter,diss):
1486 self.st.out(self.template_get_CDR_sequence_octet_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1488 def get_CDR_sequence_hf(self,type,pn,desc,filter,diss):
1489 self.st.out(self.template_get_CDR_sequence_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1490 if (self.isSeqNativeType(type.unalias().seqType())):
1491 self.getCDR_hf(type.unalias().seqType(),desc,filter,pn)
1493 def get_CDR_alias_hf(self,type,desc,filter,pn):
1495 print "XXX get_CDR_alias_hf, type = " ,type , " pn = " , pn
1496 print "XXX get_CDR_alias_hf, type.decl() = " ,type.decl()
1497 print "XXX get_CDR_alias_hf, type.decl().alias() = " ,type.decl().alias()
1499 decl = type.decl() # get declarator object
1501 if (decl.sizes()): # a typedef array
1502 #indices = self.get_indices_from_sizes(decl.sizes())
1503 #string_indices = '%i ' % indices # convert int to string
1504 #self.st.out(self.template_get_CDR_array_comment, aname=pn, asize=string_indices)
1506 #self.st.out(self.template_get_CDR_array_start, aname=pn, aval=string_indices)
1507 #self.addvar(self.c_i + pn + ";")
1508 #self.st.inc_indent()
1509 self.getCDR_hf(type.decl().alias().aliasType(), desc, filter, pn )
1511 #self.st.dec_indent()
1512 #self.st.out(self.template_get_CDR_array_end)
1515 else: # a simple typdef
1517 print "XXX get_CDR_alias_hf, type = " ,type , " pn = " , pn
1518 print "XXX get_CDR_alias_hf, type.decl() = " ,type.decl()
1520 self.getCDR_hf(type, desc, filter, decl.identifier() )
1524 # Code to generate Union Helper functions
1526 # in: un - a union node
1531 def genUnionHelper(self,un):
1533 print "XXX genUnionHelper called"
1534 print "XXX Union type =" , un
1535 print "XXX Union type.switchType()" , un.switchType()
1536 print "XXX Union Scoped Name" , un.scopedName()
1538 # check to see if we need an item
1540 if (un.switchType().unalias().kind() == idltype.tk_enum):
1541 for uc in un.cases(): # for all UnionCase objects in this union
1542 if ( self.isItemVarType(uc.caseType())):
1543 if (uc.caseType().unalias().kind() == idltype.tk_sequence):
1544 if (uc.caseType().unalias().seqType().kind() == idltype.tk_struct):
1549 sname = self.namespace(un, "_")
1550 self.curr_sname = sname # update current opnode/exnode/stnode/unnode scoped name
1551 if not self.fn_hash_built:
1552 self.fn_hash[sname] = [] # init empty list as val for this sname key
1553 # but only if the fn_hash is not already built
1556 self.st.out(self.template_union_helper_function_start_with_item, sname=sname, unname=un.repoId())
1558 self.st.out(self.template_union_helper_function_start, sname=sname, unname=un.repoId())
1559 self.st.inc_indent()
1561 if (len(self.fn_hash[sname]) > 0):
1562 self.st.out(self.template_helper_function_vars_start)
1563 self.dumpCvars(sname)
1564 self.st.out(self.template_helper_function_vars_end_item )
1566 st = un.switchType().unalias() # may be typedef switch type, so find real type
1568 self.st.out(self.template_comment_union_code_start, uname=un.repoId() )
1570 self.getCDR(st, sname + "_" + un.identifier());
1572 # Depending on what kind of discriminant I come accross (enum,integer,char,
1573 # short, boolean), make sure I cast the return value of the get_XXX accessor
1574 # to an appropriate value. Omniidl idlast.CaseLabel.value() accessor will
1575 # return an integer, or an Enumerator object that is then converted to its
1576 # integer equivalent.
1579 # NOTE - May be able to skip some of this stuff, but leave it in for now -- FS
1582 if (st.kind() == idltype.tk_enum):
1584 self.st.out(self.template_comment_union_code_discriminant, uname=std.repoId() )
1586 #count the number of cases to ensure variable is needed
1589 for uc in un.cases(): # for all UnionCase objects in this union
1590 num += len(uc.labels())
1591 for cl in uc.labels():
1595 if ((num != 1) or (num_defaults != 1)):
1596 self.st.out(self.template_union_code_save_discriminant_enum, discname=un.identifier() )
1597 self.addvar(self.c_s_disc + un.identifier() + ";")
1599 elif (st.kind() == idltype.tk_long):
1600 self.st.out(self.template_union_code_save_discriminant_long, discname=un.identifier() )
1601 self.addvar(self.c_s_disc + un.identifier() + ";")
1603 elif (st.kind() == idltype.tk_ulong):
1604 self.st.out(self.template_union_code_save_discriminant_ulong, discname=un.identifier() )
1605 self.addvar(self.c_s_disc + un.identifier() + ";")
1607 elif (st.kind() == idltype.tk_short):
1608 self.st.out(self.template_union_code_save_discriminant_short, discname=un.identifier() )
1609 self.addvar(self.c_s_disc + un.identifier() + ";")
1611 elif (st.kind() == idltype.tk_ushort):
1612 self.st.out(self.template_union_code_save_discriminant_ushort, discname=un.identifier() )
1613 self.addvar(self.c_s_disc + un.identifier() + ";")
1615 elif (st.kind() == idltype.tk_boolean):
1616 self.st.out(self.template_union_code_save_discriminant_boolean, discname=un.identifier() )
1617 self.addvar(self.c_s_disc + un.identifier() + ";")
1619 elif (st.kind() == idltype.tk_char):
1620 self.st.out(self.template_union_code_save_discriminant_char, discname=un.identifier() )
1621 self.addvar(self.c_s_disc + un.identifier() + ";")
1624 print "XXX Unknown st.kind() = ", st.kind()
1627 # Loop over all cases in this union
1630 for uc in un.cases(): # for all UnionCase objects in this union
1631 for cl in uc.labels(): # for all Caselabel objects in this UnionCase
1633 # get integer value, even if discriminant is
1634 # an Enumerator node
1636 if isinstance(cl.value(),idlast.Enumerator):
1638 print "XXX clv.identifier()", cl.value().identifier()
1639 print "XXX clv.repoId()", cl.value().repoId()
1640 print "XXX clv.scopedName()", cl.value().scopedName()
1642 # find index of enumerator in enum declaration
1643 # eg: RED is index 0 in enum Colors { RED, BLUE, GREEN }
1645 clv = self.valFromEnum(std,cl.value())
1650 #print "XXX clv = ",clv
1653 # if char, dont convert to int, but put inside single quotes so that it is understood by C.
1654 # eg: if (disc == 'b')..
1656 # TODO : handle \xxx chars generically from a function or table lookup rather than
1657 # a whole bunch of "if" statements. -- FS
1660 if (st.kind() == idltype.tk_char):
1661 if (clv == '\n'): # newline
1662 string_clv = "'\\n'"
1663 elif (clv == '\t'): # tab
1664 string_clv = "'\\t'"
1666 string_clv = "'" + clv + "'"
1668 string_clv = '%i ' % clv
1671 # If default case, then skp comparison with discriminator
1674 if not cl.default():
1675 self.st.out(self.template_comment_union_code_label_compare_start, discname=un.identifier(),labelval=string_clv )
1676 self.st.inc_indent()
1678 self.st.out(self.template_comment_union_code_label_default_start )
1681 self.getCDR(uc.caseType(),sname + "_" + uc.declarator().identifier())
1683 if not cl.default():
1684 self.st.dec_indent()
1685 self.st.out(self.template_comment_union_code_label_compare_end )
1687 self.st.out(self.template_comment_union_code_label_default_end )
1689 self.st.dec_indent()
1690 self.st.out(self.template_union_helper_function_end)
1695 # Currently, get_CDR_alias is geared to finding typdef
1698 def get_CDR_alias(self,type,pn):
1700 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1701 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1702 print "XXX get_CDR_alias, type.decl().alias() = " ,type.decl().alias()
1704 decl = type.decl() # get declarator object
1706 if (decl.sizes()): # a typedef array
1707 indices = self.get_indices_from_sizes(decl.sizes())
1708 string_indices = '%i ' % indices # convert int to string
1709 self.st.out(self.template_get_CDR_array_comment, aname=pn, asize=string_indices)
1711 self.st.out(self.template_get_CDR_array_start, aname=pn, aval=string_indices)
1712 self.addvar(self.c_i + pn + ";")
1713 self.st.inc_indent()
1714 self.getCDR(type.decl().alias().aliasType(), pn )
1716 self.st.dec_indent()
1717 self.st.out(self.template_get_CDR_array_end)
1720 else: # a simple typdef
1722 self.getCDR(type, pn )
1730 # Handle structs, including recursive
1733 def get_CDR_struct(self,type,pn):
1735 # If I am a typedef struct {..}; node then find the struct node
1737 if isinstance(type.decl(), idlast.Declarator):
1738 ntype = type.decl().alias().aliasType().decl()
1740 ntype = type.decl() # I am a struct node
1742 sname = self.namespace(ntype, "_")
1743 self.st.out(self.template_structure_start, name=sname )
1745 # Output a call to the struct helper function so I can handle recursive structs also.
1747 self.st.out(self.template_decode_struct,name=sname)
1749 self.st.out(self.template_structure_end, name=sname )
1754 # Generate private helper functions to decode a struct
1756 # in: stnode ( a struct node)
1759 def genStructHelper(self,st):
1761 print "XXX genStructHelper"
1763 sname = self.namespace(st, "_")
1764 self.curr_sname = sname # update current opnode/exnode/stnode scoped name
1765 if not self.fn_hash_built:
1766 self.fn_hash[sname] = [] # init empty list as val for this sname key
1767 # but only if the fn_hash is not already built
1769 self.st.out(self.template_struct_helper_function_start, sname=sname, stname=st.repoId())
1770 self.st.inc_indent()
1772 if (len(self.fn_hash[sname]) > 0):
1773 self.st.out(self.template_helper_function_vars_start)
1774 self.dumpCvars(sname)
1775 self.st.out(self.template_helper_function_vars_end_item )
1777 for m in st.members():
1778 for decl in m.declarators():
1779 if decl.sizes(): # an array
1780 indices = self.get_indices_from_sizes(decl.sizes())
1781 string_indices = '%i ' % indices # convert int to string
1782 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
1783 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
1784 self.addvar(self.c_i + decl.identifier() + ";")
1786 self.st.inc_indent()
1787 self.getCDR(m.memberType(), sname + "_" + decl.identifier() )
1788 self.st.dec_indent()
1789 self.st.out(self.template_get_CDR_array_end)
1793 self.getCDR(m.memberType(), sname + "_" + decl.identifier() )
1795 self.st.dec_indent()
1796 self.st.out(self.template_struct_helper_function_end)
1803 # Generate code to access a sequence of a type
1807 def get_CDR_sequence(self,type,pn):
1809 print "XXX get_CDR_sequence"
1810 self.st.out(self.template_get_CDR_sequence_length, seqname=pn )
1811 self.st.out(self.template_get_CDR_sequence_loop_start, seqname=pn )
1812 self.addvar(self.c_i_lim + pn + ";" )
1813 self.addvar(self.c_i + pn + ";")
1815 self.st.inc_indent()
1816 self.getCDR(type.unalias().seqType(), pn ) # and start all over with the type
1817 self.st.dec_indent()
1819 self.st.out(self.template_get_CDR_sequence_loop_end)
1823 # Generate code to access a sequence of octet
1826 def get_CDR_sequence_octet(self,type, pn):
1828 print "XXX get_CDR_sequence_octet"
1830 self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1831 self.st.out(self.template_get_CDR_sequence_octet, seqname=pn)
1832 self.addvar(self.c_i_lim + pn + ";")
1833 self.addvar("const gchar * binary_seq_" + pn + ";")
1834 self.addvar("gchar * text_seq_" + pn + ";")
1842 # out - scoped operation name, using sep character instead of "::"
1844 # eg: Penguin::Echo::echoWString => Penguin_Echo_echoWString if sep = "_"
1848 def namespace(self,node,sep):
1849 sname = string.replace(idlutil.ccolonName(node.scopedName()), '::', sep)
1850 #print "XXX namespace: sname = " + sname
1855 # generate code for plugin initialisation
1858 def gen_plugin_register(self):
1859 self.st.out(self.template_plugin_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1862 # generate register_giop_user_module code, and register only
1863 # unique interfaces that contain operations. Also output
1864 # a heuristic register in case we want to use that.
1866 # TODO - make this a command line option
1874 def gen_proto_reg_handoff(self, oplist):
1876 self.st.out(self.template_proto_reg_handoff_start, dissector_name=self.dissname)
1877 self.st.inc_indent()
1879 for iname in self.get_intlist(oplist):
1880 self.st.out(self.template_proto_reg_handoff_body, dissector_name=self.dissname, protocol_name=self.protoname, interface=iname )
1882 self.st.out(self.template_proto_reg_handoff_heuristic, dissector_name=self.dissname, protocol_name=self.protoname)
1883 self.st.dec_indent()
1885 self.st.out(self.template_proto_reg_handoff_end)
1888 # generate hf_ array element for operation, attribute, enums, struct and union lists
1891 def genOp_hf(self,op):
1892 sname = self.namespace(op, "_")
1893 opname = sname[string.find(sname, "_")+1:]
1894 opname = opname[:string.find(opname, "_")]
1895 rt = op.returnType()
1897 if (rt.kind() != idltype.tk_void):
1898 if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
1899 self.getCDR_hf(rt, rt.name(),\
1900 opname + "." + op.identifier() + ".return", sname + "_return")
1902 self.getCDR_hf(rt, "Return value",\
1903 opname + "." + op.identifier() + ".return", sname + "_return")
1905 for p in op.parameters():
1906 self.getCDR_hf(p.paramType(), p.identifier(),\
1907 opname + "." + op.identifier() + "." + p.identifier(), sname + "_" + p.identifier())
1909 def genAt_hf(self,at):
1910 for decl in at.declarators():
1911 sname = self.namespace(decl, "_")
1912 atname = sname[string.find(sname, "_")+1:]
1913 atname = atname[:string.find(atname, "_")]
1915 self.getCDR_hf(at.attrType(), decl.identifier(),\
1916 atname + "." + decl.identifier() + ".get", "get" + "_" + sname + "_" + decl.identifier())
1917 if not at.readonly():
1918 self.getCDR_hf(at.attrType(), decl.identifier(),\
1919 atname + "." + decl.identifier() + ".set", "set" + "_" + sname + "_" + decl.identifier())
1921 def genSt_hf(self,st):
1922 sname = self.namespace(st, "_")
1923 stname = sname[string.find(sname, "_")+1:]
1924 stname = stname[:string.find(stname, "_")]
1925 for m in st.members():
1926 for decl in m.declarators():
1927 self.getCDR_hf(m.memberType(), st.identifier() + "_" + decl.identifier(),\
1928 st.identifier() + "." + decl.identifier(), sname + "_" + decl.identifier())
1930 def genEx_hf(self,ex):
1931 sname = self.namespace(ex, "_")
1932 exname = sname[string.find(sname, "_")+1:]
1933 exname = exname[:string.find(exname, "_")]
1934 for m in ex.members():
1935 for decl in m.declarators():
1936 self.getCDR_hf(m.memberType(), ex.identifier() + "_" + decl.identifier(),\
1937 exname + "." + ex.identifier() + "_" + decl.identifier(), sname + "_" + decl.identifier())
1939 def genUnion_hf(self,un):
1940 sname = self.namespace(un, "_")
1941 unname = sname[:string.rfind(sname, "_")]
1942 unname = string.replace(unname, "_", ".")
1944 self.getCDR_hf(un.switchType().unalias(), un.identifier(),\
1945 unname + "." + un.identifier(), sname + "_" + un.identifier())
1947 for uc in un.cases(): # for all UnionCase objects in this union
1948 for cl in uc.labels(): # for all Caselabel objects in this UnionCase
1949 self.getCDR_hf(uc.caseType(), un.identifier() + "_" + uc.declarator().identifier(),\
1950 unname + "." + un.identifier() + "." + uc.declarator().identifier(),\
1951 sname + "_" + uc.declarator().identifier())
1954 # generate proto_register_<protoname> code,
1956 # in - oplist[], atlist[], stline[], unlist[]
1960 def gen_proto_register(self, oplist, atlist, stlist, unlist):
1961 self.st.out(self.template_proto_register_start, dissector_name=self.dissname)
1963 #operation specific filters
1964 self.st.out(self.template_proto_register_op_filter_comment)
1969 self.st.out(self.template_proto_register_at_filter_comment)
1974 self.st.out(self.template_proto_register_st_filter_comment)
1976 if (st.members()): # only if has members
1979 # exception List filters
1980 exlist = self.get_exceptionList(oplist) # grab list of exception nodes
1981 self.st.out(self.template_proto_register_ex_filter_comment)
1983 if (ex.members()): # only if has members
1987 self.st.out(self.template_proto_register_un_filter_comment)
1989 self.genUnion_hf(un)
1991 self.st.out(self.template_proto_register_end, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1997 # out - a list of unique interface names. This will be used in
1998 # register_giop_user_module(dissect_giop_auto, "TEST IDL", "Penguin/Echo" ); so the operation
1999 # name must be removed from the scope. And we also only want unique interfaces.
2002 def get_intlist(self,oplist):
2003 int_hash = {} # holds a hash of unique interfaces
2005 sc = op.scopedName() # eg: penguin,tux,bite
2006 sc1 = sc[:-1] # drop last entry
2007 sn = idlutil.slashName(sc1) # penguin/tux
2008 if not int_hash.has_key(sn):
2009 int_hash[sn] = 0; # dummy val, but at least key is unique
2010 ret = int_hash.keys()
2019 # out - a list of exception nodes (unique). This will be used in
2020 # to generate dissect_exception_XXX functions.
2025 def get_exceptionList(self,oplist):
2026 ex_hash = {} # holds a hash of unique exceptions.
2028 for ex in op.raises():
2029 if not ex_hash.has_key(ex):
2030 ex_hash[ex] = 0; # dummy val, but at least key is unique
2032 print "XXX Exception = " + ex.identifier()
2033 ret = ex_hash.keys()
2040 # Simple function to take a list of array sizes and find the
2041 # total number of elements
2044 # eg: temp[4][3] = 12 elements
2047 def get_indices_from_sizes(self,sizelist):
2055 # Determine how many octets contain requested number
2056 # of digits for an "fixed" IDL type "on the wire"
2059 def dig_to_len(self,dignum):
2060 return (dignum/2) + 1
2065 # Output some TODO comment
2069 def genTODO(self,message):
2070 self.st.out(self.template_debug_TODO, message=message)
2073 # Output some WARNING comment
2077 def genWARNING(self,message):
2078 self.st.out(self.template_debug_WARNING, message=message)
2081 # Templates for C code
2084 template_helper_function_comment = """\
2088 template_helper_function_vars_start = """\
2089 /* Operation specific Variable declarations Begin */"""
2091 template_helper_function_vars_end = """\
2092 /* Operation specific Variable declarations End */
2094 template_helper_function_vars_end_item = """\
2095 /* Operation specific Variable declarations End */
2098 template_helper_function_start = """\
2100 decode_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, proto_item *item _U_, int *offset _U_, MessageHeader *header, const gchar *operation _U_, gboolean stream_is_big_endian _U_)
2103 template_helper_function_end = """\
2107 # proto_reg_handoff() templates
2110 template_proto_reg_handoff_start = """\
2111 /* register me as handler for these interfaces */
2112 void proto_reg_handoff_giop_@dissector_name@(void)
2115 template_proto_reg_handoff_body = """\
2116 /* Register for Explicit Dissection */
2117 register_giop_user_module(dissect_@dissector_name@, \"@protocol_name@\", \"@interface@\", proto_@dissector_name@ ); /* explicit dissector */
2120 template_proto_reg_handoff_heuristic = """\
2121 /* Register for Heuristic Dissection */
2122 register_giop_user(dissect_@dissector_name@, \"@protocol_name@\" ,proto_@dissector_name@); /* heuristic dissector */
2125 template_proto_reg_handoff_end = """\
2133 template_prototype = """
2134 void proto_register_giop_@dissector_name@(void);
2135 void proto_reg_handoff_giop_@dissector_name@(void);"""
2138 # Initialize the protocol
2141 template_protocol = """
2142 /* Initialise the protocol and subtree pointers */
2143 static int proto_@dissector_name@ = -1;
2144 static gint ett_@dissector_name@ = -1;
2147 # Initialize the boundary Alignment
2150 template_init_boundary = """
2151 /* Initialise the initial Alignment */
2152 static guint32 boundary = GIOP_HEADER_SIZE; /* initial value */"""
2155 # plugin_register and plugin_reg_handoff templates
2158 template_plugin_register = """
2161 WS_DLL_PUBLIC_DEF void
2162 plugin_register(void)
2164 if (proto_@dissector_name@ == -1) {
2165 proto_register_giop_@dissector_name@();
2169 WS_DLL_PUBLIC_DEF void
2170 plugin_reg_handoff(void){
2171 proto_register_handoff_giop_@dissector_name@();
2176 # proto_register_<dissector name>(void) templates
2179 template_proto_register_start = """
2180 /* Register the protocol with Wireshark */
2181 void proto_register_giop_@dissector_name@(void)
2183 /* setup list of header fields */
2184 static hf_register_info hf[] = {
2185 /* field that indicates the currently ongoing request/reply exchange */
2186 {&hf_operationrequest, {"Request_Operation","giop-@dissector_name@.Request_Operation",FT_STRING,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2188 template_proto_register_end = """
2191 static ei_register_info ei[] = {
2192 { &ei_@dissector_name@_unknown_giop_msg, { "giop-@dissector_name@.unknown_giop_msg", PI_PROTOCOL, PI_WARN, "Unknown GIOP message", EXPFILL }},
2193 { &ei_@dissector_name@_unknown_exception, { "giop-@dissector_name@.unknown_exception", PI_PROTOCOL, PI_WARN, "Unknown exception", EXPFILL }},
2194 { &ei_@dissector_name@_unknown_reply_status, { "giop-@dissector_name@.unknown_reply_status", PI_PROTOCOL, PI_WARN, "Unknown reply status", EXPFILL }},
2197 /* setup protocol subtree array */
2199 static gint *ett[] = {
2200 &ett_@dissector_name@,
2203 expert_module_t* expert_@dissector_name@;
2206 /* Register the protocol name and description */
2207 proto_@dissector_name@ = proto_register_protocol(\"@description@\" , \"@protocol_name@\", \"giop-@dissector_name@\" );
2208 proto_register_field_array(proto_@dissector_name@, hf, array_length(hf));
2209 proto_register_subtree_array(ett, array_length(ett));
2211 expert_@dissector_name@ = expert_register_protocol(proto_@dissector_name@);
2212 expert_register_field_array(expert_@dissector_name@, ei, array_length(ei));
2216 template_proto_register_op_filter_comment = """\
2217 /* Operation filters */"""
2219 template_proto_register_at_filter_comment = """\
2220 /* Attribute filters */"""
2222 template_proto_register_st_filter_comment = """\
2223 /* Struct filters */"""
2225 template_proto_register_ex_filter_comment = """\
2226 /* User exception filters */"""
2228 template_proto_register_un_filter_comment = """\
2229 /* Union filters */"""
2231 template_proto_register_ei_filters = """\
2232 /* Expert info filters */
2233 static expert_field ei_@dissector_name@_unknown_giop_msg = EI_INIT;
2234 static expert_field ei_@dissector_name@_unknown_exception = EI_INIT;
2235 static expert_field ei_@dissector_name@_unknown_reply_status = EI_INIT;
2239 # template for delegation code
2242 template_op_delegate_code = """\
2243 if (strcmp(operation, "@opname@") == 0
2244 && (!idlname || strcmp(idlname, \"@interface@\") == 0)) {
2245 item = process_RequestOperation(tvb, pinfo, ptree, header, operation); /* fill-up Request_Operation field & info column */
2246 tree = start_dissecting(tvb, pinfo, ptree, offset);
2247 decode_@sname@(tvb, pinfo, tree, item, offset, header, operation, stream_is_big_endian);
2252 # Templates for the helper functions
2257 template_helper_switch_msgtype_start = """\
2258 switch(header->message_type) {"""
2260 template_helper_switch_msgtype_default_start = """\
2262 /* Unknown GIOP Message */
2263 expert_add_info_format(pinfo, item, &ei_@dissector_name@_unknown_giop_msg, "Unknown GIOP message %d", header->message_type);"""
2265 template_helper_switch_msgtype_default_end = """\
2268 template_helper_switch_msgtype_end = """\
2269 } /* switch(header->message_type) */"""
2271 template_helper_switch_msgtype_request_start = """\
2274 template_helper_switch_msgtype_request_end = """\
2277 template_helper_switch_msgtype_reply_start = """\
2280 template_helper_switch_msgtype_reply_no_exception_start = """\
2281 case NO_EXCEPTION:"""
2283 template_helper_switch_msgtype_reply_no_exception_end = """\
2286 template_helper_switch_msgtype_reply_user_exception_start = """\
2287 case USER_EXCEPTION:"""
2289 template_helper_switch_msgtype_reply_user_exception_end = """\
2292 template_helper_switch_msgtype_reply_default_start = """\
2294 /* Unknown Exception */
2295 expert_add_info_format(pinfo, item, &ei_@dissector_name@_unknown_exception, "Unknown exception %d", header->rep_status);"""
2297 template_helper_switch_msgtype_reply_default_end = """\
2300 template_helper_switch_msgtype_reply_end = """\
2303 template_helper_switch_msgtype_default_start = """\
2305 /* Unknown GIOP Message */
2306 expert_add_info_format(pinfo, item, &ei_@dissector_name@_unknown_giop_msg, "Unknown GIOP message %d", header->message_type);"""
2308 template_helper_switch_msgtype_default_end = """\
2311 template_helper_switch_rep_status_start = """\
2312 switch(header->rep_status) {"""
2314 template_helper_switch_rep_status_default_start = """\
2316 /* Unknown Reply Status */
2317 expert_add_info_format(pinfo, item, &ei_@dissector_name@_unknown_reply_status, "Unknown reply status %d", header->rep_status);"""
2319 template_helper_switch_rep_status_default_end = """\
2322 template_helper_switch_rep_status_end = """\
2323 } /* switch(header->rep_status) */
2328 # Templates for get_CDR_xxx accessors
2331 template_get_CDR_ulong = """\
2332 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-4, 4, get_CDR_ulong(tvb,offset,stream_is_big_endian, boundary));
2334 template_get_CDR_short = """\
2335 proto_tree_add_int(tree, hf_@hfname@, tvb, *offset-2, 2, get_CDR_short(tvb,offset,stream_is_big_endian, boundary));
2337 template_get_CDR_void = """\
2338 /* Function returns void */
2340 template_get_CDR_long = """\
2341 proto_tree_add_int(tree, hf_@hfname@, tvb, *offset-4, 4, get_CDR_long(tvb,offset,stream_is_big_endian, boundary));
2343 template_get_CDR_ushort = """\
2344 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-2, 2, get_CDR_ushort(tvb,offset,stream_is_big_endian, boundary));
2346 template_get_CDR_float = """\
2347 proto_tree_add_float(tree, hf_@hfname@, tvb, *offset-4, 4, get_CDR_float(tvb,offset,stream_is_big_endian, boundary));
2349 template_get_CDR_double = """\
2350 proto_tree_add_double(tree, hf_@hfname@, tvb, *offset-8, 8, get_CDR_double(tvb,offset,stream_is_big_endian, boundary));
2352 template_get_CDR_longlong = """\
2353 proto_tree_add_int64(tree, hf_@hfname@, tvb, *offset-8, 8, get_CDR_long_long(tvb,offset,stream_is_big_endian, boundary));
2355 template_get_CDR_ulonglong = """\
2356 proto_tree_add_uint64(tree, hf_@hfname@, tvb, *offset-8, 8, get_CDR_ulong_long(tvb,offset,stream_is_big_endian, boundary));
2358 template_get_CDR_boolean = """\
2359 proto_tree_add_boolean(tree, hf_@hfname@, tvb, *offset-1, 1, get_CDR_boolean(tvb,offset));
2361 template_get_CDR_char = """\
2362 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-1, 1, get_CDR_char(tvb,offset));
2364 template_get_CDR_octet = """\
2365 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-1, 1, get_CDR_octet(tvb,offset));
2367 template_get_CDR_any = """\
2368 get_CDR_any(tvb, pinfo, tree, item, offset, stream_is_big_endian, boundary, header);
2370 template_get_CDR_fixed = """\
2371 get_CDR_fixed(tvb, pinfo, item, &seq, offset, @digits@, @scale@);
2372 proto_tree_add_string_format_value(tree, hf_@hfname@, tvb, *offset-@length@, @length@, seq, "< @digits@, @scale@> = %s", seq);
2374 template_get_CDR_enum_symbolic = """\
2375 u_octet4 = get_CDR_enum(tvb,offset,stream_is_big_endian, boundary);
2376 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-4, 4, u_octet4);
2378 template_get_CDR_string = """\
2379 giop_add_CDR_string(tree, tvb, offset, stream_is_big_endian, boundary, hf_@hfname@);
2381 template_get_CDR_wstring = """\
2382 u_octet4 = get_CDR_wstring(tvb, &seq, offset, stream_is_big_endian, boundary, header);
2383 proto_tree_add_string(tree, hf_@hfname@, tvb, *offset-u_octet4, u_octet4, (u_octet4 > 0) ? seq : \"\");
2385 template_get_CDR_wchar = """\
2386 s_octet1 = get_CDR_wchar(tvb, &seq, offset, header);
2389 proto_tree_add_uint(tree, hf_@hfname@_len, tvb, *offset-1-s_octet1, 1, s_octet1);
2392 s_octet1 = -s_octet1;
2395 proto_tree_add_string(tree, hf_@hfname@, tvb, *offset-s_octet1, s_octet1, seq);
2398 template_get_CDR_TypeCode = """\
2399 u_octet4 = get_CDR_typeCode(tvb, pinfo, tree, offset, stream_is_big_endian, boundary, header);
2402 template_get_CDR_object = """\
2403 get_CDR_object(tvb, pinfo, tree, offset, stream_is_big_endian, boundary);
2406 template_get_CDR_sequence_length = """\
2407 u_octet4_loop_@seqname@ = get_CDR_ulong(tvb, offset, stream_is_big_endian, boundary);
2408 proto_tree_add_uint(tree, hf_@seqname@_loop, tvb,*offset-4, 4, u_octet4_loop_@seqname@);
2410 template_get_CDR_sequence_length_item = """\
2411 u_octet4_loop_@seqname@ = get_CDR_ulong(tvb, offset, stream_is_big_endian, boundary);
2412 item = proto_tree_add_uint(tree, hf_@seqname@_loop, tvb,*offset-4, 4, u_octet4_loop_@seqname@);
2414 template_get_CDR_sequence_loop_start = """\
2415 for (i_@seqname@=0; i_@seqname@ < u_octet4_loop_@seqname@; i_@seqname@++) {
2417 template_get_CDR_sequence_loop_end = """\
2421 template_get_CDR_sequence_octet = """\
2422 if (u_octet4_loop_@seqname@ > 0 && tree) {
2423 get_CDR_octet_seq(tvb, &binary_seq_@seqname@, offset,
2424 u_octet4_loop_@seqname@);
2425 text_seq_@seqname@ = make_printable_string(binary_seq_@seqname@,
2426 u_octet4_loop_@seqname@);
2427 proto_tree_add_bytes_format_value(tree, hf_@seqname@, tvb, *offset - u_octet4_loop_@seqname@,
2428 u_octet4_loop_@seqname@, binary_seq_@seqname@, \"%s\", text_seq_@seqname@);
2431 template_get_CDR_array_start = """\
2432 for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
2434 template_get_CDR_array_end = """\
2437 template_get_CDR_array_comment = """\
2438 /* Array: @aname@[ @asize@] */
2440 template_structure_start = """\
2441 /* Begin struct \"@name@\" */"""
2443 template_structure_end = """\
2444 /* End struct \"@name@\" */"""
2446 template_union_start = """\
2447 /* Begin union \"@name@\" */"""
2449 template_union_end = """\
2450 /* End union \"@name@\" */"""
2453 # Templates for get_CDR_xxx_hf accessors
2456 template_get_CDR_ulong_hf = """\
2457 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT32,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2459 template_get_CDR_short_hf = """\
2460 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_INT16,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2462 template_get_CDR_long_hf = """\
2463 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_INT32,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2465 template_get_CDR_ushort_hf = """\
2466 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT16,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2468 template_get_CDR_float_hf = """\
2469 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_FLOAT,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2471 template_get_CDR_double_hf = """\
2472 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_DOUBLE,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2474 template_get_CDR_fixed_hf = """\
2475 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_STRING,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2477 template_get_CDR_longlong_hf = """\
2478 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_INT64,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2480 template_get_CDR_ulonglong_hf = """\
2481 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT64,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2483 template_get_CDR_boolean_hf = """\
2484 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_BOOLEAN,8,NULL,0x01,NULL,HFILL}},"""
2486 template_get_CDR_char_hf = """\
2487 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT8,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2489 template_get_CDR_octet_hf = """\
2490 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT8,BASE_HEX,NULL,0x0,NULL,HFILL}},"""
2492 template_get_CDR_enum_symbolic_hf = """\
2493 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT32,BASE_DEC,VALS(@valstringarray@),0x0,NULL,HFILL}},"""
2495 template_get_CDR_string_hf = """\
2496 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_STRING,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2498 template_get_CDR_wstring_hf = """\
2499 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_STRING,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2501 template_get_CDR_wchar_hf = """\
2502 {&hf_@hfname@_len, {"@descname@ Length","giop-@dissector_name@.@filtername@.len",FT_UINT8,BASE_DEC,NULL,0x0,NULL,HFILL}},
2503 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_STRING,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2505 template_get_CDR_TypeCode_hf = """\
2506 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT32,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2508 template_get_CDR_sequence_hf = """\
2509 {&hf_@hfname@_loop, {"Seq length of @descname@","giop-@dissector_name@.@filtername@.size",FT_UINT32,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2511 template_get_CDR_sequence_octet_hf = """\
2512 {&hf_@hfname@_loop, {"Seq length of @descname@","giop-@dissector_name@.@filtername@.size",FT_UINT32,BASE_DEC,NULL,0x0,NULL,HFILL}},
2513 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_BYTES,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2516 # Program Header Template
2519 template_Header = """\
2520 /* packet-@dissector_name@.c
2522 * Routines for IDL dissection
2524 * Autogenerated from idl2wrs
2525 * Copyright 2001 Frank Singleton <frank.singleton@@ericsson.com>
2530 template_wireshark_copyright = """\
2532 * Wireshark - Network traffic analyzer
2534 * Copyright 1999 - 2012 Gerald Combs
2547 * SPDX-License-Identifier: GPL-2.0-or-later
2552 # Modelines Template
2556 template_Modelines = """\
2563 * indent-tabs-mode: nil
2566 * ex: set shiftwidth=4 tabstop=8 expandtab:
2567 * :indentSize=4:tabSize=8:noTabs=true:
2574 template_Includes = """\
2578 #include <gmodule.h>
2582 #include <epan/packet.h>
2583 #include <epan/proto.h>
2584 #include <epan/dissectors/packet-giop.h>
2585 #include <epan/expert.h>
2588 /* disable warning: "unreference local variable" */
2589 #pragma warning(disable:4101)
2592 #if defined(__GNUC__)
2593 #pragma GCC diagnostic ignored "-Wunused-function"
2594 #pragma GCC diagnostic ignored "-Wunused-variable"
2599 # Main dissector entry templates
2602 template_main_dissector_start = """\
2604 * Called once we accept the packet as being for us; it sets the
2605 * Protocol and Info columns and creates the top-level protocol
2609 start_dissecting(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset)
2612 proto_item *ti = NULL;
2613 proto_tree *tree = NULL; /* init later, inside if(tree) */
2615 col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
2618 * Do not clear COL_INFO, as nothing is being written there by
2619 * this dissector yet. So leave it as is from the GIOP dissector.
2620 * TODO: add something useful to COL_INFO
2621 * col_clear(pinfo->cinfo, COL_INFO);
2625 ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, tvb_reported_length_remaining(tvb, *offset), ENC_NA);
2626 tree = proto_item_add_subtree(ti, ett_@dissname@);
2632 process_RequestOperation(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, MessageHeader *header, const gchar *operation)
2635 if(header->message_type == Reply) {
2636 /* fill-up info column */
2637 col_append_fstr(pinfo->cinfo, COL_INFO, " op = %s",operation);
2639 /* fill-up the field */
2640 pi=proto_tree_add_string(ptree, hf_operationrequest, tvb, 0, 0, operation);
2641 PROTO_ITEM_SET_GENERATED(pi);
2646 dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, const gchar *operation, gchar *idlname)
2648 proto_item *item _U_;
2649 proto_tree *tree _U_;
2650 gboolean stream_is_big_endian = is_big_endian(header); /* get endianess */
2652 /* If we have a USER Exception, then decode it and return */
2653 if ((header->message_type == Reply) && (header->rep_status == USER_EXCEPTION)) {
2654 return decode_user_exception(tvb, pinfo, ptree, offset, header, operation, stream_is_big_endian);
2658 template_main_dissector_switch_msgtype_start = """\
2659 switch(header->message_type) {
2661 template_main_dissector_switch_msgtype_start_request_reply = """\
2665 template_main_dissector_switch_msgtype_end_request_reply = """\
2668 template_main_dissector_switch_msgtype_all_other_msgtype = """\
2672 case CloseConnection:
2675 return FALSE; /* not handled yet */
2678 return FALSE; /* not handled yet */
2682 template_main_dissector_end = """\
2686 } /* End of main dissector */
2695 #-------------------------------------------------------------#
2696 # Exception handling templates #
2697 #-------------------------------------------------------------#
2705 template_exception_helpers_start = """\
2706 /* Begin Exception Helper Functions */
2709 template_exception_helpers_end = """\
2711 /* End Exception Helper Functions */
2717 # template for Main delegator for exception handling
2720 template_main_exception_delegator_start = """\
2722 * Main delegator for exception handling
2726 decode_user_exception(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *ptree _U_, int *offset _U_, MessageHeader *header, const gchar *operation _U_, gboolean stream_is_big_endian _U_)
2728 proto_tree *tree _U_;
2730 if (!header->exception_id)
2736 # template for exception delegation code body
2738 template_ex_delegate_code = """\
2739 if (strcmp(header->exception_id, "@exname@") == 0) {
2740 tree = start_dissecting(tvb, pinfo, ptree, offset);
2741 decode_ex_@sname@(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian); /* @exname@ */
2748 # End of Main delegator for exception handling
2751 template_main_exception_delegator_end = """
2752 return FALSE; /* user exception not found */
2757 # template for exception helper code
2761 template_exception_helper_function_start_no_item = """\
2762 /* Exception = @exname@ */
2764 decode_ex_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, const gchar *operation _U_, gboolean stream_is_big_endian _U_)
2766 proto_item *item _U_;
2769 template_exception_helper_function_start_item = """\
2770 /* Exception = @exname@ */
2772 decode_ex_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, const gchar *operation _U_, gboolean stream_is_big_endian _U_)
2774 proto_item *item = NULL;
2777 template_exception_helper_function_end = """\
2783 # template for struct helper code
2787 template_struct_helper_function_start = """\
2788 /* Struct = @stname@ */
2790 decode_@sname@_st(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, proto_item *item _U_, int *offset _U_, MessageHeader *header _U_, const gchar *operation _U_, gboolean stream_is_big_endian _U_)
2794 template_struct_helper_function_end = """\
2799 # template for union helper code
2803 template_union_helper_function_start = """\
2804 /* Union = @unname@ */
2806 decode_@sname@_un(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, const gchar *operation _U_, gboolean stream_is_big_endian _U_)
2810 template_union_helper_function_start_with_item = """\
2811 /* Union = @unname@ */
2813 decode_@sname@_un(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, const gchar *operation _U_, gboolean stream_is_big_endian _U_)
2815 proto_item* item = NULL;
2818 template_union_helper_function_end = """\
2824 #-------------------------------------------------------------#
2825 # Value string templates #
2826 #-------------------------------------------------------------#
2828 template_value_string_start = """\
2829 static const value_string @valstringname@[] = {
2831 template_value_string_entry = """\
2832 { @intval@, \"@description@\" },"""
2834 template_value_string_end = """\
2841 #-------------------------------------------------------------#
2842 # Enum handling templates #
2843 #-------------------------------------------------------------#
2845 template_comment_enums_start = """\
2850 template_comment_enums_end = """\
2855 template_comment_enum_comment = """\
2862 #-------------------------------------------------------------#
2863 # Attribute handling templates #
2864 #-------------------------------------------------------------#
2867 template_comment_attributes_start = """\
2869 * IDL Attributes Start
2874 # get/set accessor method names are language mapping dependent.
2877 template_attributes_declare_Java_get = """static const char get_@sname@_at[] = \"_get_@atname@\" ;"""
2878 template_attributes_declare_Java_set = """static const char set_@sname@_at[] = \"_set_@atname@\" ;"""
2880 template_comment_attributes_end = """
2882 * IDL Attributes End
2888 # template for Attribute delegation code
2890 # Note: _get_xxx() should only be called for Reply with NO_EXCEPTION
2891 # Note: _set_xxx() should only be called for Request
2895 template_at_delegate_code_get = """\
2896 if (strcmp(operation, get_@sname@_at) == 0 && (header->message_type == Reply) && (header->rep_status == NO_EXCEPTION) ) {
2897 tree = start_dissecting(tvb, pinfo, ptree, offset);
2898 decode_get_@sname@_at(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);
2902 template_at_delegate_code_set = """\
2903 if (strcmp(operation, set_@sname@_at) == 0 && (header->message_type == Request) ) {
2904 tree = start_dissecting(tvb, pinfo, ptree, offset);
2905 decode_set_@sname@_at(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);
2909 template_attribute_helpers_start = """\
2910 /* Begin Attribute Helper Functions */
2912 template_attribute_helpers_end = """\
2914 /* End Attribute Helper Functions */
2918 # template for attribute helper code
2922 template_attribute_helper_function_start = """\
2924 /* Attribute = @atname@ */
2926 decode_@sname@_at(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, const gchar *operation _U_, gboolean stream_is_big_endian _U_)
2928 proto_item* item _U_;
2931 template_attribute_helper_function_end = """\
2936 #-------------------------------------------------------------#
2937 # Debugging templates #
2938 #-------------------------------------------------------------#
2941 # Template for outputting TODO "C" comments
2942 # so user know I need ti improve something.
2945 template_debug_TODO = """\
2947 /* TODO - @message@ */
2950 # Template for outputting WARNING "C" comments
2951 # so user know if I have found a problem.
2954 template_debug_WARNING = """\
2955 /* WARNING - @message@ */
2960 #-------------------------------------------------------------#
2961 # IDL Union templates #
2962 #-------------------------------------------------------------#
2964 template_comment_union_code_start = """\
2966 * IDL Union Start - @uname@
2969 template_comment_union_code_end = """
2971 * IDL union End - @uname@
2974 template_comment_union_code_discriminant = """\
2976 * IDL Union - Discriminant - @uname@
2980 # Cast Unions types to something appropriate
2981 # Enum value cast to guint32, all others cast to gint32
2982 # as omniidl accessor returns integer or Enum.
2985 template_union_code_save_discriminant_enum = """\
2986 disc_s_@discname@ = (gint32) u_octet4; /* save Enum Value discriminant and cast to gint32 */
2988 template_union_code_save_discriminant_long = """\
2989 disc_s_@discname@ = (gint32) s_octet4; /* save gint32 discriminant and cast to gint32 */
2992 template_union_code_save_discriminant_ulong = """\
2993 disc_s_@discname@ = (gint32) u_octet4; /* save guint32 discriminant and cast to gint32 */
2995 template_union_code_save_discriminant_short = """\
2996 disc_s_@discname@ = (gint32) s_octet2; /* save gint16 discriminant and cast to gint32 */
2999 template_union_code_save_discriminant_ushort = """\
3000 disc_s_@discname@ = (gint32) u_octet2; /* save guint16 discriminant and cast to gint32 */
3002 template_union_code_save_discriminant_char = """\
3003 disc_s_@discname@ = (gint32) u_octet1; /* save guint1 discriminant and cast to gint32 */
3005 template_union_code_save_discriminant_boolean = """\
3006 disc_s_@discname@ = (gint32) u_octet1; /* save guint1 discriminant and cast to gint32 */
3008 template_comment_union_code_label_compare_start = """\
3009 if (disc_s_@discname@ == @labelval@) {
3011 template_comment_union_code_label_compare_end = """\
3012 return; /* End Compare for this discriminant type */
3017 template_comment_union_code_label_default_start = """
3018 /* Default Union Case Start */
3020 template_comment_union_code_label_default_end = """\
3021 /* Default Union Case End */
3025 # Templates for function prototypes.
3026 # This is used in genDeclares() for declaring function prototypes
3027 # for structs and union helper functions.
3030 template_hf_operations = """
3031 static int hf_operationrequest = -1;/* Request_Operation field */
3035 static int hf_@name@ = -1;"""
3037 template_prototype_start_dissecting = """
3038 static proto_tree *start_dissecting(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset);
3041 template_prototype_struct_start = """\
3042 /* Struct prototype declaration Start */
3044 template_prototype_struct_end = """\
3045 /* Struct prototype declaration End */
3047 template_prototype_struct_body = """\
3048 /* Struct = @stname@ */
3049 static void decode_@name@_st(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, proto_item *item _U_, int *offset _U_, MessageHeader *header _U_, const gchar *operation _U_, gboolean stream_is_big_endian _U_);
3051 template_decode_struct = """\
3052 decode_@name@_st(tvb, pinfo, tree, item, offset, header, operation, stream_is_big_endian);"""
3054 template_prototype_union_start = """\
3055 /* Union prototype declaration Start */"""
3057 template_prototype_union_end = """\
3058 /* Union prototype declaration End */"""
3060 template_prototype_union_body = """
3061 /* Union = @unname@ */
3062 static void decode_@name@_un(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, const gchar *operation _U_, gboolean stream_is_big_endian _U_);
3064 template_decode_union = """\
3065 decode_@name@_un(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);
3069 # Editor modelines - http://www.wireshark.org/tools/modelines.html
3073 # indent-tabs-mode: nil
3076 # vi: set shiftwidth=4 expandtab:
3077 # :indentSize=4:noTabs=true: