Witness: enum witness_notifyResponse_type
[wireshark-wip.git] / tools / wireshark_gen.py
blob8be4e44a8a1f198d67232089e4ff9c7e818a6c71
1 # -*- python -*-
3 # $Id$
5 # wireshark_gen.py (part of idl2wrs)
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 "Wireshark"
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 Wireshark distro for API description.
16 # Wireshark is available at http://www.wireshark.org/
18 # Omniidl is part of the OmniOrb distribution, and is available at
19 # http://omniorb.sourceforge.net
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
34 # 02111-1307, USA.
36 # Description:
38 # Omniidl Back-end which parses an IDL list of "Operation" nodes
39 # passed from wireshark_be2.py and generates "C" code for compiling
40 # as a plugin for the Wireshark 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 """Wireshark IDL compiler back-end."""
57 from omniidl import idlast, idltype, idlutil, output
58 import sys, string
59 import tempfile
62 # Output class, generates "C" src code for the sub-dissector
64 # in:
67 # self - me
68 # st - output stream
69 # node - a reference to an Operations object.
70 # name - scoped name (Module::Module::Interface:: .. ::Operation
76 # TODO -- FS
78 # 1. generate hf[] data for searchable fields (but what is searchable?) [done, could be improved]
79 # 2. add item instead of add_text() [done]
80 # 3. sequence handling [done]
81 # 4. User Exceptions [done]
82 # 5. Fix arrays, and structs containing arrays [done]
83 # 6. Handle pragmas.
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 wireshark 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) [done]
102 # Also test, Test, TEST
108 # Strategy:
109 # For every operation and attribute do
110 # For return val and all parameters do
111 # find basic IDL type for each parameter
112 # output get_CDR_xxx
113 # output exception handling code
114 # output attribute handling code
118 class wireshark_gen_C:
122 # Turn DEBUG stuff on/off
125 DEBUG = 0
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)
149 # Constructor
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
162 # ie a hash of lists
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
168 # genCode()
170 # Main entry point, controls sequence of
171 # generated code.
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
180 # declare already.
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
184 # declare already.
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
188 # declare already.
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() # Wireshark Copyright comments.
196 self.genGPL() # GPL license
197 self.genIncludes()
198 self.genProtocol()
199 self.genDeclares(oplist,atlist,enlist,stlist,unlist)
200 if (len(atlist) > 0):
201 self.genAtList(atlist) # string constant declares for Attributes
202 if (len(enlist) > 0):
203 self.genEnList(enlist) # string constant declares for Enums
206 self.genExceptionHelpers(oplist) # helper function to decode user exceptions that have members
207 self.genExceptionDelegator(oplist) # finds the helper function to decode a user exception
208 if (len(atlist) > 0):
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(oplist, atlist, stlist, unlist)
219 self.gen_proto_reg_handoff(oplist)
220 # All the dissectors are now built-in
221 #self.gen_plugin_register()
223 #self.dumpvars() # debug
228 # genHeader
230 # Generate Standard Wireshark Header Comments
234 def genHeader(self):
235 self.st.out(self.template_Header,dissector_name=self.dissname)
236 if self.DEBUG:
237 print "XXX genHeader"
243 # genEthCopyright
245 # Wireshark Copyright Info
249 def genEthCopyright(self):
250 if self.DEBUG:
251 print "XXX genEthCopyright"
252 self.st.out(self.template_wireshark_copyright)
256 # genGPL
258 # GPL licencse
262 def genGPL(self):
263 if self.DEBUG:
264 print "XXX genGPL"
266 self.st.out(self.template_GPL)
269 # genIncludes
271 # GPL licencse
275 def genIncludes(self):
276 if self.DEBUG:
277 print "XXX genIncludes"
279 self.st.out(self.template_Includes)
282 # genOpDeclares()
284 # Generate hf variables for operation filters
286 # in: opnode ( an operation node)
289 def genOpDeclares(self, op):
290 if self.DEBUG:
291 print "XXX genOpDeclares"
292 print "XXX return type = " , op.returnType().kind()
294 sname = self.namespace(op, "_")
295 rt = op.returnType()
297 if (rt.kind() != idltype.tk_void):
298 if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
299 #self.get_CDR_alias(rt, rt.name() )
300 self.st.out(self.template_hf, name=sname + "_return")
301 else:
302 self.st.out(self.template_hf, name=sname + "_return")
304 for p in op.parameters():
305 self.st.out(self.template_hf, name=sname + "_" + p.identifier())
308 # genAtDeclares()
310 # Generate hf variables for attributes
312 # in: at ( an attribute)
315 def genAtDeclares(self, at):
316 if self.DEBUG:
317 print "XXX genAtDeclares"
319 for decl in at.declarators():
320 sname = self.namespace(decl, "_")
322 self.st.out(self.template_hf, name="get" + "_" + sname + "_" + decl.identifier())
323 if not at.readonly():
324 self.st.out(self.template_hf, name="set" + "_" + sname + "_" + decl.identifier())
327 # genStDeclares()
329 # Generate hf variables for structs
331 # in: st ( a struct)
334 def genStDeclares(self, st):
335 if self.DEBUG:
336 print "XXX genStDeclares"
338 sname = self.namespace(st, "_")
340 for m in st.members():
341 for decl in m.declarators():
342 self.st.out(self.template_hf, name=sname + "_" + decl.identifier())
345 # genExDeclares()
347 # Generate hf variables for user exception filters
349 # in: exnode ( an exception node)
352 def genExDeclares(self,ex):
353 if self.DEBUG:
354 print "XXX genExDeclares"
356 sname = self.namespace(ex, "_")
358 for m in ex.members():
359 for decl in m.declarators():
360 self.st.out(self.template_hf, name=sname + "_" + decl.identifier())
363 # genUnionDeclares()
365 # Generate hf variables for union filters
367 # in: un ( an union)
370 def genUnionDeclares(self,un):
371 if self.DEBUG:
372 print "XXX genUnionDeclares"
374 sname = self.namespace(un, "_")
375 self.st.out(self.template_hf, name=sname + "_" + un.identifier())
377 for uc in un.cases(): # for all UnionCase objects in this union
378 for cl in uc.labels(): # for all Caselabel objects in this UnionCase
379 self.st.out(self.template_hf, name=sname + "_" + uc.declarator().identifier())
383 # genExpertInfoDeclares()
385 # Generate ei variables for expert info filters
388 def genExpertInfoDeclares(self):
389 if self.DEBUG:
390 print "XXX genExpertInfoDeclares"
392 self.st.out(self.template_proto_register_ei_filters, dissector_name=self.dissname)
395 # genDeclares
397 # generate function prototypes if required
399 # Currently this is used for struct and union helper function declarations.
403 def genDeclares(self,oplist,atlist,enlist,stlist,unlist):
404 if self.DEBUG:
405 print "XXX genDeclares"
407 # prototype for operation filters
408 self.st.out(self.template_hf_operations)
410 #operation specific filters
411 if (len(oplist) > 0):
412 self.st.out(self.template_proto_register_op_filter_comment)
413 for op in oplist:
414 self.genOpDeclares(op)
416 #attribute filters
417 if (len(atlist) > 0):
418 self.st.out(self.template_proto_register_at_filter_comment)
419 for at in atlist:
420 self.genAtDeclares(at)
422 #struct filters
423 if (len(stlist) > 0):
424 self.st.out(self.template_proto_register_st_filter_comment)
425 for st in stlist:
426 self.genStDeclares(st)
428 # exception List filters
429 exlist = self.get_exceptionList(oplist) # grab list of exception nodes
430 if (len(exlist) > 0):
431 self.st.out(self.template_proto_register_ex_filter_comment)
432 for ex in exlist:
433 if (ex.members()): # only if has members
434 self.genExDeclares(ex)
436 #union filters
437 if (len(unlist) > 0):
438 self.st.out(self.template_proto_register_un_filter_comment)
439 for un in unlist:
440 self.genUnionDeclares(un)
442 #expert info filters
443 self.genExpertInfoDeclares()
445 # prototype for start_dissecting()
447 self.st.out(self.template_prototype_start_dissecting)
449 # struct prototypes
451 if len(stlist):
452 self.st.out(self.template_prototype_struct_start)
453 for st in stlist:
454 #print st.repoId()
455 sname = self.namespace(st, "_")
456 self.st.out(self.template_prototype_struct_body, stname=st.repoId(),name=sname)
458 self.st.out(self.template_prototype_struct_end)
460 # union prototypes
461 if len(unlist):
462 self.st.out(self.template_prototype_union_start)
463 for un in unlist:
464 sname = self.namespace(un, "_")
465 self.st.out(self.template_prototype_union_body, unname=un.repoId(),name=sname)
466 self.st.out(self.template_prototype_union_end)
470 # genProtocol
474 def genProtocol(self):
475 self.st.out(self.template_protocol, dissector_name=self.dissname)
476 self.st.out(self.template_init_boundary)
479 # genMainEntryStart
482 def genMainEntryStart(self,oplist):
483 self.st.out(self.template_main_dissector_start, dissname=self.dissname, disprot=self.protoname)
484 self.st.inc_indent()
485 self.st.out(self.template_main_dissector_switch_msgtype_start)
486 self.st.out(self.template_main_dissector_switch_msgtype_start_request_reply)
487 self.st.inc_indent()
491 # genMainEntryEnd
494 def genMainEntryEnd(self):
496 self.st.out(self.template_main_dissector_switch_msgtype_end_request_reply)
497 self.st.dec_indent()
498 self.st.out(self.template_main_dissector_switch_msgtype_all_other_msgtype)
499 self.st.dec_indent()
500 self.st.out(self.template_main_dissector_end)
504 # genAtList
506 # in: atlist
508 # out: C code for IDL attribute decalarations.
510 # NOTE: Mapping of attributes to operation(function) names is tricky.
512 # The actual accessor function names are language-mapping specific. The attribute name
513 # is subject to OMG IDL's name scoping rules; the accessor function names are
514 # guaranteed not to collide with any legal operation names specifiable in OMG IDL.
516 # eg:
518 # static const char get_Penguin_Echo_get_width_at[] = "get_width" ;
519 # static const char set_Penguin_Echo_set_width_at[] = "set_width" ;
521 # or:
523 # static const char get_Penguin_Echo_get_width_at[] = "_get_width" ;
524 # static const char set_Penguin_Echo_set_width_at[] = "_set_width" ;
526 # TODO: Implement some language dependant templates to handle naming conventions
527 # language <=> attribute. for C, C++. Java etc
529 # OR, just add a runtime GUI option to select language binding for attributes -- FS
533 # ie: def genAtlist(self,atlist,language)
538 def genAtList(self,atlist):
539 self.st.out(self.template_comment_attributes_start)
541 for n in atlist:
542 for i in n.declarators(): #
543 sname = self.namespace(i, "_")
544 atname = i.identifier()
545 self.st.out(self.template_attributes_declare_Java_get, sname=sname, atname=atname)
546 if not n.readonly():
547 self.st.out(self.template_attributes_declare_Java_set, sname=sname, atname=atname)
549 self.st.out(self.template_comment_attributes_end)
553 # genEnList
555 # in: enlist
557 # out: C code for IDL Enum decalarations using "static const value_string" template
562 def genEnList(self,enlist):
564 self.st.out(self.template_comment_enums_start)
566 for enum in enlist:
567 sname = self.namespace(enum, "_")
569 self.st.out(self.template_comment_enum_comment, ename=enum.repoId())
570 self.st.out(self.template_value_string_start, valstringname=sname)
571 for enumerator in enum.enumerators():
572 self.st.out(self.template_value_string_entry, intval=str(self.valFromEnum(enum,enumerator)), description=enumerator.identifier())
575 #atname = n.identifier()
576 self.st.out(self.template_value_string_end, valstringname=sname)
578 self.st.out(self.template_comment_enums_end)
590 # genExceptionDelegator
592 # in: oplist
594 # out: C code for User exception delegator
596 # eg:
600 def genExceptionDelegator(self,oplist):
602 self.st.out(self.template_main_exception_delegator_start)
603 self.st.inc_indent()
605 exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
607 for ex in exlist:
608 if self.DEBUG:
609 print "XXX Exception " , ex.repoId()
610 print "XXX Exception Identifier" , ex.identifier()
611 print "XXX Exception Scoped Name" , ex.scopedName()
613 if (ex.members()): # only if has members
614 sname = self.namespace(ex, "_")
615 exname = ex.repoId()
616 self.st.out(self.template_ex_delegate_code, sname=sname, exname=ex.repoId())
618 self.st.dec_indent()
619 self.st.out(self.template_main_exception_delegator_end)
623 # genAttribueHelpers()
625 # Generate private helper functions to decode Attributes.
627 # in: atlist
629 # For readonly attribute - generate get_xxx()
630 # If NOT readonly attribute - also generate set_xxx()
633 def genAttributeHelpers(self,atlist):
634 if self.DEBUG:
635 print "XXX genAttributeHelpers: atlist = ", atlist
637 self.st.out(self.template_attribute_helpers_start)
639 for attrib in atlist:
640 for decl in attrib.declarators():
641 self.genAtHelper(attrib,decl,"get") # get accessor
642 if not attrib.readonly():
643 self.genAtHelper(attrib,decl,"set") # set accessor
645 self.st.out(self.template_attribute_helpers_end)
648 # genAtHelper()
650 # Generate private helper functions to decode an attribute
652 # in: at - attribute node
653 # in: decl - declarator belonging to this attribute
654 # in: order - to generate a "get" or "set" helper
656 def genAtHelper(self,attrib,decl,order):
657 if self.DEBUG:
658 print "XXX genAtHelper"
660 sname = order + "_" + self.namespace(decl, "_") # must use set or get prefix to avoid collision
661 self.curr_sname = sname # update current opnode/exnode scoped name
663 if not self.fn_hash_built:
664 self.fn_hash[sname] = [] # init empty list as val for this sname key
665 # but only if the fn_hash is not already built
667 self.st.out(self.template_attribute_helper_function_start, sname=sname, atname=decl.repoId())
668 self.st.inc_indent()
670 if (len(self.fn_hash[sname]) > 0):
671 self.st.out(self.template_helper_function_vars_start)
672 self.dumpCvars(sname)
673 self.st.out(self.template_helper_function_vars_end )
675 self.getCDR(attrib.attrType(), sname + "_" + decl.identifier() )
677 self.st.dec_indent()
678 self.st.out(self.template_attribute_helper_function_end)
683 # genExceptionHelpers()
685 # Generate private helper functions to decode Exceptions used
686 # within operations
688 # in: oplist
692 def genExceptionHelpers(self,oplist):
693 exlist = self.get_exceptionList(oplist) # grab list of exception nodes
694 if self.DEBUG:
695 print "XXX genExceptionHelpers: exlist = ", exlist
697 self.st.out(self.template_exception_helpers_start)
698 for ex in exlist:
699 if (ex.members()): # only if has members
700 #print "XXX Exception = " + ex.identifier()
701 self.genExHelper(ex)
703 self.st.out(self.template_exception_helpers_end)
707 # genExhelper()
709 # Generate private helper functions to decode User Exceptions
711 # in: exnode ( an exception node)
714 def genExHelper(self,ex):
715 if self.DEBUG:
716 print "XXX genExHelper"
718 sname = self.namespace(ex, "_")
719 self.curr_sname = sname # update current opnode/exnode scoped name
720 if not self.fn_hash_built:
721 self.fn_hash[sname] = [] # init empty list as val for this sname key
722 # but only if the fn_hash is not already built
724 self.st.out(self.template_exception_helper_function_start, sname=sname, exname=ex.repoId())
725 self.st.inc_indent()
727 if (len(self.fn_hash[sname]) > 0):
728 self.st.out(self.template_helper_function_vars_start)
729 self.dumpCvars(sname)
730 self.st.out(self.template_helper_function_vars_end )
732 for m in ex.members():
733 if self.DEBUG:
734 print "XXX genExhelper, member = ", m, "member type = ", m.memberType()
736 for decl in m.declarators():
737 if self.DEBUG:
738 print "XXX genExhelper, d = ", decl
740 if decl.sizes(): # an array
741 indices = self.get_indices_from_sizes(decl.sizes())
742 string_indices = '%i ' % indices # convert int to string
743 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
744 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
745 self.addvar(self.c_i + decl.identifier() + ";")
747 self.st.inc_indent()
748 self.getCDR(m.memberType(), sname + "_" + decl.identifier() )
750 self.st.dec_indent()
751 self.st.out(self.template_get_CDR_array_end)
754 else:
755 self.getCDR(m.memberType(), sname + "_" + decl.identifier() )
757 self.st.dec_indent()
758 self.st.out(self.template_exception_helper_function_end)
762 # genHelpers()
764 # Generate private helper functions for each IDL operation.
765 # Generate private helper functions for each IDL struct.
766 # Generate private helper functions for each IDL union.
769 # in: oplist, stlist, unlist
773 def genHelpers(self,oplist,stlist,unlist):
774 for op in oplist:
775 self.genOperation(op)
776 for st in stlist:
777 self.genStructHelper(st)
778 for un in unlist:
779 self.genUnionHelper(un)
782 # genOperation()
784 # Generate private helper functions for a specificIDL operation.
786 # in: opnode
789 def genOperation(self,opnode):
790 if self.DEBUG:
791 print "XXX genOperation called"
793 sname = self.namespace(opnode, "_")
794 if not self.fn_hash_built:
795 self.fn_hash[sname] = [] # init empty list as val for this sname key
796 # but only if the fn_hash is not already built
798 self.curr_sname = sname # update current opnode's scoped name
799 opname = opnode.identifier()
801 self.st.out(self.template_helper_function_comment, repoid=opnode.repoId() )
803 self.st.out(self.template_helper_function_start, sname=sname)
804 self.st.inc_indent()
806 if (len(self.fn_hash[sname]) > 0):
807 self.st.out(self.template_helper_function_vars_start)
808 self.dumpCvars(sname)
809 self.st.out(self.template_helper_function_vars_end )
811 self.st.out(self.template_helper_switch_msgtype_start)
813 self.st.out(self.template_helper_switch_msgtype_request_start)
814 self.st.inc_indent()
815 self.genOperationRequest(opnode)
816 self.st.out(self.template_helper_switch_msgtype_request_end)
817 self.st.dec_indent()
819 self.st.out(self.template_helper_switch_msgtype_reply_start)
820 self.st.inc_indent()
822 self.st.out(self.template_helper_switch_rep_status_start)
825 self.st.out(self.template_helper_switch_msgtype_reply_no_exception_start)
826 self.st.inc_indent()
827 self.genOperationReply(opnode)
828 self.st.out(self.template_helper_switch_msgtype_reply_no_exception_end)
829 self.st.dec_indent()
831 self.st.out(self.template_helper_switch_msgtype_reply_user_exception_start)
832 self.st.inc_indent()
833 self.genOpExceptions(opnode)
834 self.st.out(self.template_helper_switch_msgtype_reply_user_exception_end)
835 self.st.dec_indent()
837 self.st.out(self.template_helper_switch_msgtype_reply_default_start, dissector_name=self.dissname)
838 self.st.out(self.template_helper_switch_msgtype_reply_default_end)
840 self.st.out(self.template_helper_switch_rep_status_end)
842 self.st.dec_indent()
844 self.st.out(self.template_helper_switch_msgtype_default_start, dissector_name=self.dissname)
845 self.st.out(self.template_helper_switch_msgtype_default_end)
847 self.st.out(self.template_helper_switch_msgtype_end)
848 self.st.dec_indent()
851 self.st.out(self.template_helper_function_end, sname=sname)
857 # Decode function parameters for a GIOP request message
861 def genOperationRequest(self,opnode):
862 for p in opnode.parameters():
863 if p.is_in():
864 if self.DEBUG:
865 print "XXX parameter = " ,p
866 print "XXX parameter type = " ,p.paramType()
867 print "XXX parameter type kind = " ,p.paramType().kind()
869 self.getCDR(p.paramType(), self.curr_sname + "_" + p.identifier())
873 # Decode function parameters for a GIOP reply message
877 def genOperationReply(self,opnode):
879 rt = opnode.returnType() # get return type
880 if self.DEBUG:
881 print "XXX genOperationReply"
882 print "XXX opnode = " , opnode
883 print "XXX return type = " , rt
884 print "XXX return type.unalias = " , rt.unalias()
885 print "XXX return type.kind() = " , rt.kind();
887 sname = self.namespace(opnode, "_")
889 if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
890 #self.getCDR(rt.decl().alias().aliasType(),"dummy") # return value maybe a typedef
891 self.get_CDR_alias(rt, sname + "_return" )
892 #self.get_CDR_alias(rt, rt.name() )
894 else:
895 self.getCDR(rt, sname + "_return") # return value is NOT an alias
897 for p in opnode.parameters():
898 if p.is_out(): # out or inout
899 self.getCDR(p.paramType(), self.curr_sname + "_" + p.identifier())
901 #self.st.dec_indent()
903 def genOpExceptions(self,opnode):
904 for ex in opnode.raises():
905 if ex.members():
906 #print ex.members()
907 for m in ex.members():
909 #print m.memberType(), m.memberType().kind()
911 # Delegator for Operations
914 def genOpDelegator(self,oplist):
915 for op in oplist:
916 iname = "/".join(op.scopedName()[:-1])
917 opname = op.identifier()
918 sname = self.namespace(op, "_")
919 self.st.out(self.template_op_delegate_code, interface=iname, sname=sname, opname=opname)
922 # Delegator for Attributes
925 def genAtDelegator(self,atlist):
926 for a in atlist:
927 for i in a.declarators():
928 atname = i.identifier()
929 sname = self.namespace(i, "_")
930 self.st.out(self.template_at_delegate_code_get, sname=sname)
931 if not a.readonly():
932 self.st.out(self.template_at_delegate_code_set, sname=sname)
936 # Add a variable declaration to the hash of list
939 def addvar(self, var):
940 if not ( var in self.fn_hash[self.curr_sname] ):
941 self.fn_hash[self.curr_sname].append(var)
944 # Print the variable declaration from the hash of list
948 def dumpvars(self):
949 for fn in self.fn_hash.keys():
950 print "FN = " + fn
951 for v in self.fn_hash[fn]:
952 print "-> " + v
954 # Print the "C" variable declaration from the hash of list
955 # for a given scoped operation name (eg: tux_penguin_eat)
959 def dumpCvars(self, sname):
960 for v in self.fn_hash[sname]:
961 self.st.out(v)
965 # Given an enum node, and a enumerator node, return
966 # the enumerator's numerical value.
968 # eg: enum Color {red,green,blue} should return
969 # val = 1 for green
972 def valFromEnum(self,enumNode, enumeratorNode):
973 if self.DEBUG:
974 print "XXX valFromEnum, enumNode = ", enumNode, " from ", enumNode.repoId()
975 print "XXX valFromEnum, enumeratorNode = ", enumeratorNode, " from ", enumeratorNode.repoId()
977 if isinstance(enumeratorNode,idlast.Enumerator):
978 value = enumNode.enumerators().index(enumeratorNode)
979 return value
982 ## tk_null = 0
983 ## tk_void = 1
984 ## tk_short = 2
985 ## tk_long = 3
986 ## tk_ushort = 4
987 ## tk_ulong = 5
988 ## tk_float = 6
989 ## tk_double = 7
990 ## tk_boolean = 8
991 ## tk_char = 9
992 ## tk_octet = 10
993 ## tk_any = 11
994 ## tk_TypeCode = 12
995 ## tk_Principal = 13
996 ## tk_objref = 14
997 ## tk_struct = 15
998 ## tk_union = 16
999 ## tk_enum = 17
1000 ## tk_string = 18
1001 ## tk_sequence = 19
1002 ## tk_array = 20
1003 ## tk_alias = 21
1004 ## tk_except = 22
1005 ## tk_longlong = 23
1006 ## tk_ulonglong = 24
1007 ## tk_longdouble = 25
1008 ## tk_wchar = 26
1009 ## tk_wstring = 27
1010 ## tk_fixed = 28
1011 ## tk_value = 29
1012 ## tk_value_box = 30
1013 ## tk_native = 31
1014 ## tk_abstract_interface = 32
1018 # getCDR()
1020 # This is the main "iterator" function. It takes a node, and tries to output
1021 # a get_CDR_XXX accessor method(s). It can call itself multiple times
1022 # if I find nested structures etc.
1025 def getCDR(self,type,name="fred"):
1027 pt = type.unalias().kind() # param CDR type
1028 pn = name # param name
1030 if self.DEBUG:
1031 print "XXX getCDR: kind = " , pt
1032 print "XXX getCDR: name = " , pn
1034 if pt == idltype.tk_ulong:
1035 self.get_CDR_ulong(pn)
1036 elif pt == idltype.tk_longlong:
1037 self.get_CDR_longlong(pn)
1038 elif pt == idltype.tk_ulonglong:
1039 self.get_CDR_ulonglong(pn)
1040 elif pt == idltype.tk_void:
1041 self.get_CDR_void(pn)
1042 elif pt == idltype.tk_short:
1043 self.get_CDR_short(pn)
1044 elif pt == idltype.tk_long:
1045 self.get_CDR_long(pn)
1046 elif pt == idltype.tk_ushort:
1047 self.get_CDR_ushort(pn)
1048 elif pt == idltype.tk_float:
1049 self.get_CDR_float(pn)
1050 elif pt == idltype.tk_double:
1051 self.get_CDR_double(pn)
1052 elif pt == idltype.tk_fixed:
1053 self.get_CDR_fixed(type.unalias(),pn)
1054 elif pt == idltype.tk_boolean:
1055 self.get_CDR_boolean(pn)
1056 elif pt == idltype.tk_char:
1057 self.get_CDR_char(pn)
1058 elif pt == idltype.tk_octet:
1059 self.get_CDR_octet(pn)
1060 elif pt == idltype.tk_any:
1061 self.get_CDR_any(pn)
1062 elif pt == idltype.tk_string:
1063 self.get_CDR_string(pn)
1064 elif pt == idltype.tk_wstring:
1065 self.get_CDR_wstring(pn)
1066 elif pt == idltype.tk_wchar:
1067 self.get_CDR_wchar(pn)
1068 elif pt == idltype.tk_enum:
1069 #print type.decl()
1070 self.get_CDR_enum(pn,type)
1071 #self.get_CDR_enum(pn)
1073 elif pt == idltype.tk_struct:
1074 self.get_CDR_struct(type,pn)
1075 elif pt == idltype.tk_TypeCode: # will I ever get here ?
1076 self.get_CDR_TypeCode(pn)
1077 elif pt == idltype.tk_sequence:
1078 if type.unalias().seqType().kind() == idltype.tk_octet:
1079 self.get_CDR_sequence_octet(type,pn)
1080 else:
1081 self.get_CDR_sequence(type,pn)
1082 elif pt == idltype.tk_objref:
1083 self.get_CDR_objref(type,pn)
1084 elif pt == idltype.tk_array:
1085 pn = pn # Supported elsewhere
1086 elif pt == idltype.tk_union:
1087 self.get_CDR_union(type,pn)
1088 elif pt == idltype.tk_alias:
1089 if self.DEBUG:
1090 print "XXXXX Alias type XXXXX " , type
1091 self.get_CDR_alias(type,pn)
1092 else:
1093 self.genWARNING("Unknown typecode = " + '%i ' % pt) # put comment in source code
1097 # get_CDR_XXX methods are here ..
1102 def get_CDR_ulong(self,pn):
1103 self.st.out(self.template_get_CDR_ulong, hfname=pn)
1105 def get_CDR_short(self,pn):
1106 self.st.out(self.template_get_CDR_short, hfname=pn)
1108 def get_CDR_void(self,pn):
1109 self.st.out(self.template_get_CDR_void, hfname=pn)
1111 def get_CDR_long(self,pn):
1112 self.st.out(self.template_get_CDR_long, hfname=pn)
1114 def get_CDR_ushort(self,pn):
1115 self.st.out(self.template_get_CDR_ushort, hfname=pn)
1117 def get_CDR_float(self,pn):
1118 self.st.out(self.template_get_CDR_float, hfname=pn)
1120 def get_CDR_double(self,pn):
1121 self.st.out(self.template_get_CDR_double, hfname=pn)
1123 def get_CDR_longlong(self,pn):
1124 self.st.out(self.template_get_CDR_longlong, hfname=pn)
1126 def get_CDR_ulonglong(self,pn):
1127 self.st.out(self.template_get_CDR_ulonglong, hfname=pn)
1129 def get_CDR_boolean(self,pn):
1130 self.st.out(self.template_get_CDR_boolean, hfname=pn)
1132 def get_CDR_fixed(self,type,pn):
1133 if self.DEBUG:
1134 print "XXXX calling get_CDR_fixed, type = ", type
1135 print "XXXX calling get_CDR_fixed, type.digits() = ", type.digits()
1136 print "XXXX calling get_CDR_fixed, type.scale() = ", type.scale()
1138 string_digits = '%i ' % type.digits() # convert int to string
1139 string_scale = '%i ' % type.scale() # convert int to string
1140 string_length = '%i ' % self.dig_to_len(type.digits()) # how many octets to hilight for a number of digits
1142 self.st.out(self.template_get_CDR_fixed, varname=pn, digits=string_digits, scale=string_scale, length=string_length )
1143 self.addvar(self.c_seq)
1146 def get_CDR_char(self,pn):
1147 self.st.out(self.template_get_CDR_char, hfname=pn)
1149 def get_CDR_octet(self,pn):
1150 self.st.out(self.template_get_CDR_octet, hfname=pn)
1152 def get_CDR_any(self,pn):
1153 self.st.out(self.template_get_CDR_any, varname=pn)
1155 def get_CDR_enum(self,pn,type):
1156 #self.st.out(self.template_get_CDR_enum, hfname=pn)
1157 sname = self.namespace(type.unalias(), "_")
1158 self.st.out(self.template_get_CDR_enum_symbolic, valstringarray=sname,hfname=pn)
1159 self.addvar(self.c_u_octet4)
1161 def get_CDR_string(self,pn):
1162 self.st.out(self.template_get_CDR_string, hfname=pn)
1164 def get_CDR_wstring(self,pn):
1165 self.st.out(self.template_get_CDR_wstring, varname=pn)
1166 self.addvar(self.c_u_octet4)
1167 self.addvar(self.c_seq)
1169 def get_CDR_wchar(self,pn):
1170 self.st.out(self.template_get_CDR_wchar, varname=pn)
1171 self.addvar(self.c_s_octet1)
1172 self.addvar(self.c_seq)
1174 def get_CDR_TypeCode(self,pn):
1175 self.st.out(self.template_get_CDR_TypeCode, varname=pn)
1176 self.addvar(self.c_u_octet4)
1178 def get_CDR_objref(self,type,pn):
1179 self.st.out(self.template_get_CDR_object)
1181 def get_CDR_sequence_len(self,pn):
1182 self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1185 def get_CDR_union(self,type,pn):
1186 if self.DEBUG:
1187 print "XXX Union type =" , type, " pn = ",pn
1188 print "XXX Union type.decl()" , type.decl()
1189 print "XXX Union Scoped Name" , type.scopedName()
1191 # If I am a typedef union {..}; node then find the union node
1193 if isinstance(type.decl(), idlast.Declarator):
1194 ntype = type.decl().alias().aliasType().decl()
1195 else:
1196 ntype = type.decl() # I am a union node
1198 if self.DEBUG:
1199 print "XXX Union ntype =" , ntype
1201 sname = self.namespace(ntype, "_")
1202 self.st.out(self.template_union_start, name=sname )
1204 # Output a call to the union helper function so I can handle recursive union also.
1206 self.st.out(self.template_decode_union,name=sname)
1208 self.st.out(self.template_union_end, name=sname )
1211 # getCDR_hf()
1213 # This takes a node, and tries to output the appropriate item for the
1214 # hf array.
1217 def getCDR_hf(self,type,desc,filter,hf_name="fred"):
1219 pt = type.unalias().kind() # param CDR type
1220 pn = hf_name # param name
1222 if self.DEBUG:
1223 print "XXX getCDR_hf: kind = " , pt
1224 print "XXX getCDR_hf: name = " , pn
1226 if pt == idltype.tk_ulong:
1227 self.get_CDR_ulong_hf(pn, desc, filter, self.dissname)
1228 elif pt == idltype.tk_longlong:
1229 self.get_CDR_longlong_hf(pn, desc, filter, self.dissname)
1230 elif pt == idltype.tk_ulonglong:
1231 self.get_CDR_ulonglong_hf(pn, desc, filter, self.dissname)
1232 elif pt == idltype.tk_void:
1233 pt = pt # no hf_ variables needed
1234 elif pt == idltype.tk_short:
1235 self.get_CDR_short_hf(pn, desc, filter, self.dissname)
1236 elif pt == idltype.tk_long:
1237 self.get_CDR_long_hf(pn, desc, filter, self.dissname)
1238 elif pt == idltype.tk_ushort:
1239 self.get_CDR_ushort_hf(pn, desc, filter, self.dissname)
1240 elif pt == idltype.tk_float:
1241 self.get_CDR_float_hf(pn, desc, filter, self.dissname)
1242 elif pt == idltype.tk_double:
1243 self.get_CDR_double_hf(pn, desc, filter, self.dissname)
1244 elif pt == idltype.tk_fixed:
1245 pt = pt # no hf_ variables needed
1246 elif pt == idltype.tk_boolean:
1247 self.get_CDR_boolean_hf(pn, desc, filter, self.dissname)
1248 elif pt == idltype.tk_char:
1249 self.get_CDR_char_hf(pn, desc, filter, self.dissname)
1250 elif pt == idltype.tk_octet:
1251 self.get_CDR_octet_hf(pn, desc, filter, self.dissname)
1252 elif pt == idltype.tk_any:
1253 pt = pt # no hf_ variables needed
1254 elif pt == idltype.tk_string:
1255 self.get_CDR_string_hf(pn, desc, filter, self.dissname)
1256 elif pt == idltype.tk_wstring:
1257 self.get_CDR_wstring_hf(pn, desc, filter, self.dissname)
1258 elif pt == idltype.tk_wchar:
1259 self.get_CDR_wchar_hf(pn, desc, filter, self.dissname)
1260 elif pt == idltype.tk_enum:
1261 self.get_CDR_enum_hf(pn, type, desc, filter, self.dissname)
1262 elif pt == idltype.tk_struct:
1263 pt = pt # no hf_ variables needed (should be already contained in struct members)
1264 elif pt == idltype.tk_TypeCode: # will I ever get here ?
1265 self.get_CDR_TypeCode_hf(pn, desc, filter, self.dissname)
1266 elif pt == idltype.tk_sequence:
1267 if type.unalias().seqType().kind() == idltype.tk_octet:
1268 self.get_CDR_sequence_octet_hf(type, pn, desc, filter, self.dissname)
1269 else:
1270 self.get_CDR_sequence_hf(type, pn, desc, filter, self.dissname)
1271 elif pt == idltype.tk_objref:
1272 pt = pt # no object specific hf_ variables used, use generic ones from giop dissector
1273 elif pt == idltype.tk_array:
1274 pt = pt # Supported elsewhere
1275 elif pt == idltype.tk_union:
1276 pt = pt # no hf_ variables needed (should be already contained in union members)
1277 elif pt == idltype.tk_alias:
1278 if self.DEBUG:
1279 print "XXXXX Alias type hf XXXXX " , type
1280 self.get_CDR_alias_hf(type,pn)
1281 else:
1282 self.genWARNING("Unknown typecode = " + '%i ' % pt) # put comment in source code
1285 # get_CDR_XXX_hf methods are here ..
1290 def get_CDR_ulong_hf(self,pn,desc,filter,diss):
1291 self.st.out(self.template_get_CDR_ulong_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1293 def get_CDR_short_hf(self,pn,desc,filter,diss):
1294 self.st.out(self.template_get_CDR_short_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1296 def get_CDR_long_hf(self,pn,desc,filter,diss):
1297 self.st.out(self.template_get_CDR_long_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1299 def get_CDR_ushort_hf(self,pn,desc,filter,diss):
1300 self.st.out(self.template_get_CDR_ushort_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1302 def get_CDR_float_hf(self,pn,desc,filter,diss):
1303 self.st.out(self.template_get_CDR_float_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1305 def get_CDR_double_hf(self,pn,desc,filter,diss):
1306 self.st.out(self.template_get_CDR_double_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1308 def get_CDR_longlong_hf(self,pn,desc,filter,diss):
1309 self.st.out(self.template_get_CDR_longlong_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1311 def get_CDR_ulonglong_hf(self,pn,desc,filter,diss):
1312 self.st.out(self.template_get_CDR_ulonglong_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1314 def get_CDR_boolean_hf(self,pn,desc,filter,diss):
1315 self.st.out(self.template_get_CDR_boolean_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1317 def get_CDR_char_hf(self,pn,desc,filter,diss):
1318 self.st.out(self.template_get_CDR_char_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1320 def get_CDR_octet_hf(self,pn,desc,filter,diss):
1321 self.st.out(self.template_get_CDR_octet_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1323 def get_CDR_enum_hf(self,pn,type,desc,filter,diss):
1324 sname = self.namespace(type.unalias(), "_")
1325 self.st.out(self.template_get_CDR_enum_symbolic_hf, valstringarray=sname,hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1327 def get_CDR_string_hf(self,pn,desc,filter,diss):
1328 self.st.out(self.template_get_CDR_string_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1330 def get_CDR_wstring_hf(self,pn,desc,filter,diss):
1331 self.st.out(self.template_get_CDR_wstring_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1332 # self.addvar(self.c_u_octet4)
1333 # self.addvar(self.c_seq)
1335 def get_CDR_wchar_hf(self,pn,desc,filter,diss):
1336 self.st.out(self.template_get_CDR_wchar_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1337 # self.addvar(self.c_s_octet1)
1338 # self.addvar(self.c_seq)
1340 def get_CDR_TypeCode_hf(self,pn,desc,filter,diss):
1341 self.st.out(self.template_get_CDR_TypeCode_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1343 def get_CDR_sequence_octet_hf(self,type,pn,desc,filter,diss):
1344 self.st.out(self.template_get_CDR_sequence_octet_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1346 def get_CDR_sequence_hf(self,type,pn,desc,filter,diss):
1347 self.st.out(self.template_get_CDR_sequence_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1349 def get_CDR_alias_hf(self,type,pn):
1350 if self.DEBUG:
1351 print "XXX get_CDR_alias_hf, type = " ,type , " pn = " , pn
1352 print "XXX get_CDR_alias_hf, type.decl() = " ,type.decl()
1353 print "XXX get_CDR_alias_hf, type.decl().alias() = " ,type.decl().alias()
1355 decl = type.decl() # get declarator object
1357 if (decl.sizes()): # a typedef array
1358 #indices = self.get_indices_from_sizes(decl.sizes())
1359 #string_indices = '%i ' % indices # convert int to string
1360 #self.st.out(self.template_get_CDR_array_comment, aname=pn, asize=string_indices)
1362 #self.st.out(self.template_get_CDR_array_start, aname=pn, aval=string_indices)
1363 #self.addvar(self.c_i + pn + ";")
1364 #self.st.inc_indent()
1365 self.getCDR_hf(type.decl().alias().aliasType(), pn )
1367 #self.st.dec_indent()
1368 #self.st.out(self.template_get_CDR_array_end)
1371 else: # a simple typdef
1372 if self.DEBUG:
1373 print "XXX get_CDR_alias_hf, type = " ,type , " pn = " , pn
1374 print "XXX get_CDR_alias_hf, type.decl() = " ,type.decl()
1376 self.getCDR_hf(type, decl.identifier() )
1380 # Code to generate Union Helper functions
1382 # in: un - a union node
1387 def genUnionHelper(self,un):
1388 if self.DEBUG:
1389 print "XXX genUnionHelper called"
1390 print "XXX Union type =" , un
1391 print "XXX Union type.switchType()" , un.switchType()
1392 print "XXX Union Scoped Name" , un.scopedName()
1394 sname = self.namespace(un, "_")
1395 self.curr_sname = sname # update current opnode/exnode/stnode/unnode scoped name
1396 if not self.fn_hash_built:
1397 self.fn_hash[sname] = [] # init empty list as val for this sname key
1398 # but only if the fn_hash is not already built
1400 self.st.out(self.template_union_helper_function_start, sname=sname, unname=un.repoId())
1401 self.st.inc_indent()
1403 if (len(self.fn_hash[sname]) > 0):
1404 self.st.out(self.template_helper_function_vars_start)
1405 self.dumpCvars(sname)
1406 self.st.out(self.template_helper_function_vars_end )
1408 st = un.switchType().unalias() # may be typedef switch type, so find real type
1410 self.st.out(self.template_comment_union_code_start, uname=un.repoId() )
1412 self.getCDR(st, sname + "_" + un.identifier());
1414 # Depending on what kind of discriminant I come accross (enum,integer,char,
1415 # short, boolean), make sure I cast the return value of the get_XXX accessor
1416 # to an appropriate value. Omniidl idlast.CaseLabel.value() accessor will
1417 # return an integer, or an Enumerator object that is then converted to its
1418 # integer equivalent.
1421 # NOTE - May be able to skip some of this stuff, but leave it in for now -- FS
1424 if (st.kind() == idltype.tk_enum):
1425 std = st.decl()
1426 self.st.out(self.template_comment_union_code_discriminant, uname=std.repoId() )
1427 self.st.out(self.template_union_code_save_discriminant_enum, discname=un.identifier() )
1428 self.addvar(self.c_s_disc + un.identifier() + ";")
1430 elif (st.kind() == idltype.tk_long):
1431 self.st.out(self.template_union_code_save_discriminant_long, discname=un.identifier() )
1432 self.addvar(self.c_s_disc + un.identifier() + ";")
1434 elif (st.kind() == idltype.tk_ulong):
1435 self.st.out(self.template_union_code_save_discriminant_ulong, discname=un.identifier() )
1436 self.addvar(self.c_s_disc + un.identifier() + ";")
1438 elif (st.kind() == idltype.tk_short):
1439 self.st.out(self.template_union_code_save_discriminant_short, discname=un.identifier() )
1440 self.addvar(self.c_s_disc + un.identifier() + ";")
1442 elif (st.kind() == idltype.tk_ushort):
1443 self.st.out(self.template_union_code_save_discriminant_ushort, discname=un.identifier() )
1444 self.addvar(self.c_s_disc + un.identifier() + ";")
1446 elif (st.kind() == idltype.tk_boolean):
1447 self.st.out(self.template_union_code_save_discriminant_boolean, discname=un.identifier() )
1448 self.addvar(self.c_s_disc + un.identifier() + ";")
1450 elif (st.kind() == idltype.tk_char):
1451 self.st.out(self.template_union_code_save_discriminant_char, discname=un.identifier() )
1452 self.addvar(self.c_s_disc + un.identifier() + ";")
1454 else:
1455 print "XXX Unknown st.kind() = ", st.kind()
1458 # Loop over all cases in this union
1461 for uc in un.cases(): # for all UnionCase objects in this union
1462 for cl in uc.labels(): # for all Caselabel objects in this UnionCase
1464 # get integer value, even if discriminant is
1465 # an Enumerator node
1467 if isinstance(cl.value(),idlast.Enumerator):
1468 if self.DEBUG:
1469 print "XXX clv.identifier()", cl.value().identifier()
1470 print "XXX clv.repoId()", cl.value().repoId()
1471 print "XXX clv.scopedName()", cl.value().scopedName()
1473 # find index of enumerator in enum declaration
1474 # eg: RED is index 0 in enum Colors { RED, BLUE, GREEN }
1476 clv = self.valFromEnum(std,cl.value())
1478 else:
1479 clv = cl.value()
1481 #print "XXX clv = ",clv
1484 # if char, dont convert to int, but put inside single quotes so that it is understood by C.
1485 # eg: if (disc == 'b')..
1487 # TODO : handle \xxx chars generically from a function or table lookup rather than
1488 # a whole bunch of "if" statements. -- FS
1491 if (st.kind() == idltype.tk_char):
1492 if (clv == '\n'): # newline
1493 string_clv = "'\\n'"
1494 elif (clv == '\t'): # tab
1495 string_clv = "'\\t'"
1496 else:
1497 string_clv = "'" + clv + "'"
1498 else:
1499 string_clv = '%i ' % clv
1502 # If default case, then skp comparison with discriminator
1505 if not cl.default():
1506 self.st.out(self.template_comment_union_code_label_compare_start, discname=un.identifier(),labelval=string_clv )
1507 self.st.inc_indent()
1508 else:
1509 self.st.out(self.template_comment_union_code_label_default_start )
1512 self.getCDR(uc.caseType(),sname + "_" + uc.declarator().identifier())
1514 if not cl.default():
1515 self.st.dec_indent()
1516 self.st.out(self.template_comment_union_code_label_compare_end )
1517 else:
1518 self.st.out(self.template_comment_union_code_label_default_end )
1520 self.st.dec_indent()
1521 self.st.out(self.template_union_helper_function_end)
1526 # Currently, get_CDR_alias is geared to finding typdef
1529 def get_CDR_alias(self,type,pn):
1530 if self.DEBUG:
1531 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1532 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1533 print "XXX get_CDR_alias, type.decl().alias() = " ,type.decl().alias()
1535 decl = type.decl() # get declarator object
1537 if (decl.sizes()): # a typedef array
1538 indices = self.get_indices_from_sizes(decl.sizes())
1539 string_indices = '%i ' % indices # convert int to string
1540 self.st.out(self.template_get_CDR_array_comment, aname=pn, asize=string_indices)
1542 self.st.out(self.template_get_CDR_array_start, aname=pn, aval=string_indices)
1543 self.addvar(self.c_i + pn + ";")
1544 self.st.inc_indent()
1545 self.getCDR(type.decl().alias().aliasType(), pn )
1547 self.st.dec_indent()
1548 self.st.out(self.template_get_CDR_array_end)
1551 else: # a simple typdef
1552 if self.DEBUG:
1553 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1554 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1556 self.getCDR(type, pn )
1564 # Handle structs, including recursive
1567 def get_CDR_struct(self,type,pn):
1569 # If I am a typedef struct {..}; node then find the struct node
1571 if isinstance(type.decl(), idlast.Declarator):
1572 ntype = type.decl().alias().aliasType().decl()
1573 else:
1574 ntype = type.decl() # I am a struct node
1576 sname = self.namespace(ntype, "_")
1577 self.st.out(self.template_structure_start, name=sname )
1579 # Output a call to the struct helper function so I can handle recursive structs also.
1581 self.st.out(self.template_decode_struct,name=sname)
1583 self.st.out(self.template_structure_end, name=sname )
1586 # genStructhelper()
1588 # Generate private helper functions to decode a struct
1590 # in: stnode ( a struct node)
1593 def genStructHelper(self,st):
1594 if self.DEBUG:
1595 print "XXX genStructHelper"
1597 sname = self.namespace(st, "_")
1598 self.curr_sname = sname # update current opnode/exnode/stnode scoped name
1599 if not self.fn_hash_built:
1600 self.fn_hash[sname] = [] # init empty list as val for this sname key
1601 # but only if the fn_hash is not already built
1603 self.st.out(self.template_struct_helper_function_start, sname=sname, stname=st.repoId())
1604 self.st.inc_indent()
1606 if (len(self.fn_hash[sname]) > 0):
1607 self.st.out(self.template_helper_function_vars_start)
1608 self.dumpCvars(sname)
1609 self.st.out(self.template_helper_function_vars_end )
1611 for m in st.members():
1612 for decl in m.declarators():
1613 if decl.sizes(): # an array
1614 indices = self.get_indices_from_sizes(decl.sizes())
1615 string_indices = '%i ' % indices # convert int to string
1616 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
1617 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
1618 self.addvar(self.c_i + decl.identifier() + ";")
1620 self.st.inc_indent()
1621 self.getCDR(m.memberType(), sname + "_" + decl.identifier() )
1622 self.st.dec_indent()
1623 self.st.out(self.template_get_CDR_array_end)
1626 else:
1627 self.getCDR(m.memberType(), sname + "_" + decl.identifier() )
1629 self.st.dec_indent()
1630 self.st.out(self.template_struct_helper_function_end)
1637 # Generate code to access a sequence of a type
1641 def get_CDR_sequence(self,type, pn):
1642 self.st.out(self.template_get_CDR_sequence_length, seqname=pn )
1643 self.st.out(self.template_get_CDR_sequence_loop_start, seqname=pn )
1644 self.addvar(self.c_i_lim + pn + ";" )
1645 self.addvar(self.c_i + pn + ";")
1647 self.st.inc_indent()
1648 self.getCDR(type.unalias().seqType(), pn ) # and start all over with the type
1649 self.st.dec_indent()
1651 self.st.out(self.template_get_CDR_sequence_loop_end)
1655 # Generate code to access a sequence of octet
1658 def get_CDR_sequence_octet(self,type, pn):
1659 self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1660 self.st.out(self.template_get_CDR_sequence_octet, seqname=pn)
1661 self.addvar(self.c_i_lim + pn + ";")
1662 self.addvar("gchar * binary_seq_" + pn + ";")
1663 self.addvar("gchar * text_seq_" + pn + ";")
1667 # namespace()
1669 # in - op node
1671 # out - scoped operation name, using sep character instead of "::"
1673 # eg: Penguin::Echo::echoWString => Penguin_Echo_echoWString if sep = "_"
1677 def namespace(self,node,sep):
1678 sname = string.replace(idlutil.ccolonName(node.scopedName()), '::', sep)
1679 #print "XXX namespace: sname = " + sname
1680 return sname
1684 # generate code for plugin initialisation
1687 def gen_plugin_register(self):
1688 self.st.out(self.template_plugin_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1691 # generate register_giop_user_module code, and register only
1692 # unique interfaces that contain operations. Also output
1693 # a heuristic register in case we want to use that.
1695 # TODO - make this a command line option
1697 # -e explicit
1698 # -h heuristic
1703 def gen_proto_reg_handoff(self, oplist):
1705 self.st.out(self.template_proto_reg_handoff_start, dissector_name=self.dissname)
1706 self.st.inc_indent()
1708 for iname in self.get_intlist(oplist):
1709 self.st.out(self.template_proto_reg_handoff_body, dissector_name=self.dissname, protocol_name=self.protoname, interface=iname )
1711 self.st.out(self.template_proto_reg_handoff_heuristic, dissector_name=self.dissname, protocol_name=self.protoname)
1712 self.st.dec_indent()
1714 self.st.out(self.template_proto_reg_handoff_end)
1717 # generate hf_ array element for operation, attribute, enums, struct and union lists
1720 def genOp_hf(self,op):
1721 sname = self.namespace(op, "_")
1722 opname = sname[string.find(sname, "_")+1:]
1723 opname = opname[:string.find(opname, "_")]
1724 rt = op.returnType()
1726 if (rt.kind() != idltype.tk_void):
1727 if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
1728 self.getCDR_hf(rt, rt.name(),\
1729 opname + "." + op.identifier() + ".return", sname + "_return")
1730 else:
1731 self.getCDR_hf(rt, "Return value",\
1732 opname + "." + op.identifier() + ".return", sname + "_return")
1734 for p in op.parameters():
1735 self.getCDR_hf(p.paramType(), p.identifier(),\
1736 opname + "." + op.identifier() + "." + p.identifier(), sname + "_" + p.identifier())
1738 def genAt_hf(self,at):
1739 for decl in at.declarators():
1740 sname = self.namespace(decl, "_")
1741 atname = sname[string.find(sname, "_")+1:]
1742 atname = atname[:string.find(atname, "_")]
1744 self.getCDR_hf(at.attrType(), decl.identifier(),\
1745 atname + "." + decl.identifier() + ".get", "get" + "_" + sname + "_" + decl.identifier())
1746 if not at.readonly():
1747 self.getCDR_hf(at.attrType(), decl.identifier(),\
1748 atname + "." + decl.identifier() + ".set", "set" + "_" + sname + "_" + decl.identifier())
1750 def genSt_hf(self,st):
1751 sname = self.namespace(st, "_")
1752 stname = sname[string.find(sname, "_")+1:]
1753 stname = stname[:string.find(stname, "_")]
1754 for m in st.members():
1755 for decl in m.declarators():
1756 self.getCDR_hf(m.memberType(), st.identifier() + "_" + decl.identifier(),\
1757 st.identifier() + "." + decl.identifier(), sname + "_" + decl.identifier())
1759 def genEx_hf(self,ex):
1760 sname = self.namespace(ex, "_")
1761 exname = sname[string.find(sname, "_")+1:]
1762 exname = exname[:string.find(exname, "_")]
1763 for m in ex.members():
1764 for decl in m.declarators():
1765 self.getCDR_hf(m.memberType(), ex.identifier() + "_" + decl.identifier(),\
1766 exname + "." + ex.identifier() + "_" + decl.identifier(), sname + "_" + decl.identifier())
1768 def genUnion_hf(self,un):
1769 sname = self.namespace(un, "_")
1770 unname = sname[:string.rfind(sname, "_")]
1771 unname = string.replace(unname, "_", ".")
1773 self.getCDR_hf(un.switchType().unalias(), un.identifier(),\
1774 unname + "." + un.identifier(), sname + "_" + un.identifier())
1776 for uc in un.cases(): # for all UnionCase objects in this union
1777 for cl in uc.labels(): # for all Caselabel objects in this UnionCase
1778 self.getCDR_hf(uc.caseType(), un.identifier() + "_" + uc.declarator().identifier(),\
1779 unname + "." + un.identifier() + "." + uc.declarator().identifier(),\
1780 sname + "_" + uc.declarator().identifier())
1783 # generate proto_register_<protoname> code,
1785 # in - oplist[], atlist[], stline[], unlist[]
1789 def gen_proto_register(self, oplist, atlist, stlist, unlist):
1790 self.st.out(self.template_proto_register_start, dissector_name=self.dissname)
1792 #operation specific filters
1793 self.st.out(self.template_proto_register_op_filter_comment)
1794 for op in oplist:
1795 self.genOp_hf(op)
1797 #attribute filters
1798 self.st.out(self.template_proto_register_at_filter_comment)
1799 for at in atlist:
1800 self.genAt_hf(at)
1802 #struct filters
1803 self.st.out(self.template_proto_register_st_filter_comment)
1804 for st in stlist:
1805 if (st.members()): # only if has members
1806 self.genSt_hf(st)
1808 # exception List filters
1809 exlist = self.get_exceptionList(oplist) # grab list of exception nodes
1810 self.st.out(self.template_proto_register_ex_filter_comment)
1811 for ex in exlist:
1812 if (ex.members()): # only if has members
1813 self.genEx_hf(ex)
1815 # Union filters
1816 self.st.out(self.template_proto_register_un_filter_comment)
1817 for un in unlist:
1818 self.genUnion_hf(un)
1820 self.st.out(self.template_proto_register_end, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1824 # in - oplist[]
1826 # out - a list of unique interface names. This will be used in
1827 # register_giop_user_module(dissect_giop_auto, "TEST IDL", "Penguin/Echo" ); so the operation
1828 # name must be removed from the scope. And we also only want unique interfaces.
1831 def get_intlist(self,oplist):
1832 int_hash = {} # holds a hash of unique interfaces
1833 for op in oplist:
1834 sc = op.scopedName() # eg: penguin,tux,bite
1835 sc1 = sc[:-1] # drop last entry
1836 sn = idlutil.slashName(sc1) # penguin/tux
1837 if not int_hash.has_key(sn):
1838 int_hash[sn] = 0; # dummy val, but at least key is unique
1839 ret = int_hash.keys()
1840 ret.sort()
1841 return ret
1846 # in - oplist[]
1848 # out - a list of exception nodes (unique). This will be used in
1849 # to generate dissect_exception_XXX functions.
1854 def get_exceptionList(self,oplist):
1855 ex_hash = {} # holds a hash of unique exceptions.
1856 for op in oplist:
1857 for ex in op.raises():
1858 if not ex_hash.has_key(ex):
1859 ex_hash[ex] = 0; # dummy val, but at least key is unique
1860 if self.DEBUG:
1861 print "XXX Exception = " + ex.identifier()
1862 ret = ex_hash.keys()
1863 ret.sort()
1864 return ret
1869 # Simple function to take a list of array sizes and find the
1870 # total number of elements
1873 # eg: temp[4][3] = 12 elements
1876 def get_indices_from_sizes(self,sizelist):
1877 val = 1;
1878 for i in sizelist:
1879 val = val * i
1881 return val
1884 # Determine how many octets contain requested number
1885 # of digits for an "fixed" IDL type "on the wire"
1888 def dig_to_len(self,dignum):
1889 return (dignum/2) + 1
1894 # Output some TODO comment
1898 def genTODO(self,message):
1899 self.st.out(self.template_debug_TODO, message=message)
1902 # Output some WARNING comment
1906 def genWARNING(self,message):
1907 self.st.out(self.template_debug_WARNING, message=message)
1910 # Templates for C code
1913 template_helper_function_comment = """\
1915 * @repoid@
1916 */"""
1917 template_helper_function_vars_start = """\
1918 /* Operation specific Variable declarations Begin */"""
1920 template_helper_function_vars_end = """\
1921 /* Operation specific Variable declarations End */
1923 (void)item; /* Avoid coverity param_set_but_unused parse warning */
1926 template_helper_function_start = """\
1927 static void
1928 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_)
1929 {"""
1931 template_helper_function_end = """\
1935 # proto_reg_handoff() templates
1938 template_proto_reg_handoff_start = """\
1939 /* register me as handler for these interfaces */
1940 void proto_reg_handoff_giop_@dissector_name@(void)
1941 {"""
1943 template_proto_reg_handoff_body = """
1944 /* Register for Explicit Dissection */
1945 register_giop_user_module(dissect_@dissector_name@, \"@protocol_name@\", \"@interface@\", proto_@dissector_name@ ); /* explicit dissector */"""
1947 template_proto_reg_handoff_heuristic = """
1948 /* Register for Heuristic Dissection */
1949 register_giop_user(dissect_@dissector_name@, \"@protocol_name@\" ,proto_@dissector_name@); /* heuristic dissector */"""
1951 template_proto_reg_handoff_end = """\
1956 # Initialize the protocol
1959 template_protocol = """
1960 /* Initialise the protocol and subtree pointers */
1961 static int proto_@dissector_name@ = -1;
1962 static gint ett_@dissector_name@ = -1;
1965 # Initialize the boundary Alignment
1968 template_init_boundary = """
1969 /* Initialise the initial Alignment */
1970 static guint32 boundary = GIOP_HEADER_SIZE; /* initial value */"""
1973 # plugin_register and plugin_reg_handoff templates
1976 template_plugin_register = """
1977 #if 0
1979 WS_DLL_PUBLIC_DEF void
1980 plugin_register(void)
1982 if (proto_@dissector_name@ == -1) {
1983 proto_register_giop_@dissector_name@();
1987 WS_DLL_PUBLIC_DEF void
1988 plugin_reg_handoff(void){
1989 proto_register_handoff_giop_@dissector_name@();
1991 #endif
1994 # proto_register_<dissector name>(void) templates
1997 template_proto_register_start = """
1998 /* Register the protocol with Wireshark */
1999 void proto_register_giop_@dissector_name@(void)
2001 /* setup list of header fields */
2002 static hf_register_info hf[] = {
2003 /* field that indicates the currently ongoing request/reply exchange */
2004 {&hf_operationrequest, {"Request_Operation","giop-@dissector_name@.Request_Operation",FT_STRING,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2006 template_proto_register_end = """
2009 static ei_register_info ei[] = {
2010 { &ei_@dissector_name@_unknown_giop_msg, { "giop-@dissector_name@.unknown_giop_msg", PI_PROTOCOL, PI_WARN, "Unknown GIOP message", EXPFILL }},
2011 { &ei_@dissector_name@_unknown_exception, { "giop-@dissector_name@.unknown_exception", PI_PROTOCOL, PI_WARN, "Unknown exception", EXPFILL }},
2012 { &ei_@dissector_name@_unknown_reply_status, { "giop-@dissector_name@.unknown_reply_status", PI_PROTOCOL, PI_WARN, "Unknown reply status", EXPFILL }},
2015 /* setup protocol subtree array */
2017 static gint *ett[] = {
2018 &ett_@dissector_name@,
2021 expert_module_t* expert_@dissector_name@;
2024 /* Register the protocol name and description */
2025 proto_@dissector_name@ = proto_register_protocol(\"@description@\" , \"@protocol_name@\", \"giop-@dissector_name@\" );
2026 proto_register_field_array(proto_@dissector_name@, hf, array_length(hf));
2027 proto_register_subtree_array(ett, array_length(ett));
2029 expert_@dissector_name@ = expert_register_protocol(proto_@dissector_name@);
2030 expert_register_field_array(expert_@dissector_name@, ei, array_length(ei));
2034 template_proto_register_op_filter_comment = """\
2035 /* Operation filters */"""
2037 template_proto_register_at_filter_comment = """\
2038 /* Attribute filters */"""
2040 template_proto_register_st_filter_comment = """\
2041 /* Struct filters */"""
2043 template_proto_register_ex_filter_comment = """\
2044 /* User exception filters */"""
2046 template_proto_register_un_filter_comment = """\
2047 /* Union filters */"""
2049 template_proto_register_ei_filters = """\
2050 /* Expert info filters */
2051 static expert_field ei_@dissector_name@_unknown_giop_msg = EI_INIT;
2052 static expert_field ei_@dissector_name@_unknown_exception = EI_INIT;
2053 static expert_field ei_@dissector_name@_unknown_reply_status = EI_INIT;
2057 # template for delegation code
2060 template_op_delegate_code = """\
2061 if (strcmp(operation, "@opname@") == 0
2062 && (!idlname || strcmp(idlname, \"@interface@\") == 0)) {
2063 item = process_RequestOperation(tvb, pinfo, ptree, header, operation); /* fill-up Request_Operation field & info column */
2064 tree = start_dissecting(tvb, pinfo, ptree, offset);
2065 decode_@sname@(tvb, pinfo, tree, item, offset, header, operation, stream_is_big_endian);
2066 return TRUE;
2070 # Templates for the helper functions
2075 template_helper_switch_msgtype_start = """\
2076 switch(header->message_type) {"""
2078 template_helper_switch_msgtype_default_start = """\
2079 default:
2080 /* Unknown GIOP Message */
2081 expert_add_info_format(pinfo, item, &ei_@dissector_name@_unknown_giop_msg, "Unknown GIOP message %d", header->message_type);"""
2083 template_helper_switch_msgtype_default_end = """\
2084 break;"""
2086 template_helper_switch_msgtype_end = """\
2087 } /* switch(header->message_type) */"""
2089 template_helper_switch_msgtype_request_start = """\
2090 case Request:"""
2092 template_helper_switch_msgtype_request_end = """\
2093 break;"""
2095 template_helper_switch_msgtype_reply_start = """\
2096 case Reply:"""
2098 template_helper_switch_msgtype_reply_no_exception_start = """\
2099 case NO_EXCEPTION:"""
2101 template_helper_switch_msgtype_reply_no_exception_end = """\
2102 break;"""
2104 template_helper_switch_msgtype_reply_user_exception_start = """\
2105 case USER_EXCEPTION:"""
2107 template_helper_switch_msgtype_reply_user_exception_end = """\
2108 break;"""
2110 template_helper_switch_msgtype_reply_default_start = """\
2111 default:
2112 /* Unknown Exception */
2113 expert_add_info_format(pinfo, item, &ei_@dissector_name@_unknown_exception, "Unknown exception %d", header->rep_status);"""
2115 template_helper_switch_msgtype_reply_default_end = """\
2116 break;"""
2118 template_helper_switch_msgtype_reply_end = """\
2119 break;"""
2121 template_helper_switch_msgtype_default_start = """\
2122 default:
2123 /* Unknown GIOP Message */
2124 expert_add_info_format(pinfo, item, &ei_@dissector_name@_unknown_giop_msg, "Unknown GIOP message %d", header->message_type);"""
2126 template_helper_switch_msgtype_default_end = """\
2127 break;"""
2129 template_helper_switch_rep_status_start = """\
2130 switch(header->rep_status) {"""
2132 template_helper_switch_rep_status_default_start = """\
2133 default:
2134 /* Unknown Reply Status */
2135 expert_add_info_format(pinfo, item, &ei_@dissector_name@_unknown_reply_status, "Unknown reply status %d", header->rep_status);"""
2137 template_helper_switch_rep_status_default_end = """\
2138 break;"""
2140 template_helper_switch_rep_status_end = """\
2141 } /* switch(header->rep_status) */
2143 break;"""
2146 # Templates for get_CDR_xxx accessors
2149 template_get_CDR_ulong = """\
2150 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-4, 4, get_CDR_ulong(tvb,offset,stream_is_big_endian, boundary));
2152 template_get_CDR_short = """\
2153 proto_tree_add_int(tree, hf_@hfname@, tvb, *offset-2, 2, get_CDR_short(tvb,offset,stream_is_big_endian, boundary));
2155 template_get_CDR_void = """\
2156 /* Function returns void */
2158 template_get_CDR_long = """\
2159 proto_tree_add_int(tree, hf_@hfname@, tvb, *offset-4, 4, get_CDR_long(tvb,offset,stream_is_big_endian, boundary));
2161 template_get_CDR_ushort = """\
2162 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-2, 2, get_CDR_ushort(tvb,offset,stream_is_big_endian, boundary));
2164 template_get_CDR_float = """\
2165 proto_tree_add_float(tree, hf_@hfname@, tvb, *offset-4, 4, get_CDR_float(tvb,offset,stream_is_big_endian, boundary));
2167 template_get_CDR_double = """\
2168 proto_tree_add_double(tree, hf_@hfname@, tvb, *offset-8, 8, get_CDR_double(tvb,offset,stream_is_big_endian, boundary));
2170 template_get_CDR_longlong = """\
2171 proto_tree_add_int64(tree, hf_@hfname@, tvb, *offset-8, 8, get_CDR_long_long(tvb,offset,stream_is_big_endian, boundary));
2173 template_get_CDR_ulonglong = """\
2174 proto_tree_add_uint64(tree, hf_@hfname@, tvb, *offset-8, 8, get_CDR_ulong_long(tvb,offset,stream_is_big_endian, boundary));
2176 template_get_CDR_boolean = """\
2177 proto_tree_add_boolean(tree, hf_@hfname@, tvb, *offset-1, 1, get_CDR_boolean(tvb,offset));
2179 template_get_CDR_char = """\
2180 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-1, 1, get_CDR_char(tvb,offset));
2182 template_get_CDR_octet = """\
2183 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-1, 1, get_CDR_octet(tvb,offset));
2185 template_get_CDR_any = """\
2186 get_CDR_any(tvb, pinfo, tree, item, offset, stream_is_big_endian, boundary, header);
2188 template_get_CDR_fixed = """\
2189 get_CDR_fixed(tvb, pinfo, item, &seq, offset, @digits@, @scale@);
2190 proto_tree_add_text(tree,tvb,*offset-@length@, @length@, "@varname@ < @digits@, @scale@> = %s",seq);
2192 template_get_CDR_enum_symbolic = """\
2193 u_octet4 = get_CDR_enum(tvb,offset,stream_is_big_endian, boundary);
2194 /* coverity[returned_pointer] */
2195 item = proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-4, 4, u_octet4);
2197 template_get_CDR_string = """\
2198 giop_add_CDR_string(tree, tvb, offset, stream_is_big_endian, boundary, hf_@hfname@);
2200 template_get_CDR_wstring = """\
2201 u_octet4 = get_CDR_wstring(tvb, &seq, offset, stream_is_big_endian, boundary, header);
2202 proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ (%u) = %s",
2203 u_octet4, (u_octet4 > 0) ? seq : \"\");
2205 template_get_CDR_wchar = """\
2206 s_octet1 = get_CDR_wchar(tvb, &seq, offset, header);
2207 if (tree) {
2208 if (s_octet1 > 0)
2209 proto_tree_add_text(tree,tvb,*offset-1-s_octet1,1,"length = %u",s_octet1);
2211 if (s_octet1 < 0)
2212 s_octet1 = -s_octet1;
2214 if (s_octet1 > 0)
2215 proto_tree_add_text(tree,tvb,*offset-s_octet1,s_octet1,"@varname@ = %s",seq);
2218 template_get_CDR_TypeCode = """\
2219 u_octet4 = get_CDR_typeCode(tvb, pinfo, tree, offset, stream_is_big_endian, boundary, header);
2222 template_get_CDR_object = """\
2223 get_CDR_object(tvb, pinfo, tree, offset, stream_is_big_endian, boundary);
2226 template_get_CDR_sequence_length = """\
2227 u_octet4_loop_@seqname@ = get_CDR_ulong(tvb, offset, stream_is_big_endian, boundary);
2228 /* coverity[returned_pointer] */
2229 item = proto_tree_add_uint(tree, hf_@seqname@, tvb,*offset-4, 4, u_octet4_loop_@seqname@);
2231 template_get_CDR_sequence_loop_start = """\
2232 for (i_@seqname@=0; i_@seqname@ < u_octet4_loop_@seqname@; i_@seqname@++) {
2234 template_get_CDR_sequence_loop_end = """\
2238 template_get_CDR_sequence_octet = """\
2239 if (u_octet4_loop_@seqname@ > 0 && tree) {
2240 get_CDR_octet_seq(tvb, &binary_seq_@seqname@, offset,
2241 u_octet4_loop_@seqname@);
2242 text_seq_@seqname@ = make_printable_string(binary_seq_@seqname@,
2243 u_octet4_loop_@seqname@);
2244 proto_tree_add_text(tree, tvb, *offset - u_octet4_loop_@seqname@,
2245 u_octet4_loop_@seqname@, \"@seqname@: %s\", text_seq_@seqname@);
2248 template_get_CDR_array_start = """\
2249 for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
2251 template_get_CDR_array_end = """\
2254 template_get_CDR_array_comment = """\
2255 /* Array: @aname@[ @asize@] */
2257 template_structure_start = """\
2258 /* Begin struct \"@name@\" */"""
2260 template_structure_end = """\
2261 /* End struct \"@name@\" */"""
2263 template_union_start = """\
2264 /* Begin union \"@name@\" */"""
2266 template_union_end = """\
2267 /* End union \"@name@\" */"""
2270 # Templates for get_CDR_xxx_hf accessors
2273 template_get_CDR_ulong_hf = """\
2274 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT32,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2276 template_get_CDR_short_hf = """\
2277 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_INT16,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2279 template_get_CDR_long_hf = """\
2280 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_INT32,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2282 template_get_CDR_ushort_hf = """\
2283 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT16,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2285 template_get_CDR_float_hf = """\
2286 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_FLOAT,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2288 template_get_CDR_double_hf = """\
2289 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_DOUBLE,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2291 template_get_CDR_longlong_hf = """\
2292 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_INT64,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2294 template_get_CDR_ulonglong_hf = """\
2295 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT64,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2297 template_get_CDR_boolean_hf = """\
2298 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_BOOLEAN,8,NULL,0x01,NULL,HFILL}},"""
2300 template_get_CDR_char_hf = """\
2301 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT8,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2303 template_get_CDR_octet_hf = """\
2304 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT8,BASE_HEX,NULL,0x0,NULL,HFILL}},"""
2306 template_get_CDR_enum_symbolic_hf = """\
2307 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT32,BASE_DEC,VALS(@valstringarray@),0x0,NULL,HFILL}},"""
2309 template_get_CDR_string_hf = """\
2310 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_STRING,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2312 template_get_CDR_wstring_hf = """\
2313 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_STRING,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2315 template_get_CDR_wchar_hf = """\
2316 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT16,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2318 template_get_CDR_TypeCode_hf = """\
2319 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT32,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2321 template_get_CDR_sequence_hf = """\
2322 {&hf_@hfname@, {"Seq length of @descname@","giop-@dissector_name@.@filtername@",FT_UINT32,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2324 template_get_CDR_sequence_octet_hf = """\
2325 {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT8,BASE_HEX,NULL,0x0,NULL,HFILL}},"""
2328 # Program Header Template
2331 template_Header = """\
2332 /* packet-@dissector_name@.c
2334 * $Id$
2336 * Routines for IDL dissection
2338 * Autogenerated from idl2wrs
2339 * Copyright 2001 Frank Singleton <frank.singleton@@ericsson.com>
2344 template_wireshark_copyright = """\
2346 * Wireshark - Network traffic analyzer
2347 * By Gerald Combs
2348 * Copyright 1999 - 2012 Gerald Combs
2355 # GPL Template
2359 template_GPL = """\
2361 * This program is free software; you can redistribute it and/or
2362 * modify it under the terms of the GNU General Public License
2363 * as published by the Free Software Foundation; either version 2
2364 * of the License, or (at your option) any later version.
2366 * This program is distributed in the hope that it will be useful,
2367 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2368 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2369 * GNU General Public License for more details.
2371 * You should have received a copy of the GNU General Public License
2372 * along with this program; if not, write to the Free Software
2373 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2378 # Includes template
2381 template_Includes = """\
2383 #include "config.h"
2385 #include <gmodule.h>
2387 #include <string.h>
2388 #include <glib.h>
2389 #include <epan/packet.h>
2390 #include <epan/proto.h>
2391 #include <epan/dissectors/packet-giop.h>
2392 #include <epan/expert.h>
2394 #ifdef _MSC_VER
2395 /* disable warning: "unreference local variable" */
2396 #pragma warning(disable:4101)
2397 #endif
2402 # Main dissector entry templates
2405 template_main_dissector_start = """\
2407 * Called once we accept the packet as being for us; it sets the
2408 * Protocol and Info columns and creates the top-level protocol
2409 * tree item.
2411 static proto_tree *
2412 start_dissecting(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset)
2415 proto_item *ti = NULL;
2416 proto_tree *tree = NULL; /* init later, inside if(tree) */
2418 col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
2421 * Do not clear COL_INFO, as nothing is being written there by
2422 * this dissector yet. So leave it as is from the GIOP dissector.
2423 * TODO: add something useful to COL_INFO
2424 * col_clear(pinfo->cinfo, COL_INFO);
2427 if (ptree) {
2428 ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, -1, ENC_NA);
2429 tree = proto_item_add_subtree(ti, ett_@dissname@);
2431 return tree;
2434 static proto_item*
2435 process_RequestOperation(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, MessageHeader *header, const gchar *operation)
2437 proto_item *pi;
2438 if(header->message_type == Reply) {
2439 /* fill-up info column */
2440 col_append_fstr(pinfo->cinfo, COL_INFO, " op = %s",operation);
2442 /* fill-up the field */
2443 pi=proto_tree_add_string(ptree, hf_operationrequest, tvb, 0, 0, operation);
2444 PROTO_ITEM_SET_GENERATED(pi);
2445 return pi;
2448 static gboolean
2449 dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, const gchar *operation, gchar *idlname)
2451 proto_item *item _U_;
2452 proto_tree *tree _U_;
2453 gboolean stream_is_big_endian = is_big_endian(header); /* get endianess */
2455 /* If we have a USER Exception, then decode it and return */
2456 if ((header->message_type == Reply) && (header->rep_status == USER_EXCEPTION)) {
2457 return decode_user_exception(tvb, pinfo, ptree, offset, header, operation, stream_is_big_endian);
2461 template_main_dissector_switch_msgtype_start = """\
2462 switch(header->message_type) {
2464 template_main_dissector_switch_msgtype_start_request_reply = """\
2465 case Request:
2466 case Reply:
2468 template_main_dissector_switch_msgtype_end_request_reply = """\
2469 break;
2471 template_main_dissector_switch_msgtype_all_other_msgtype = """\
2472 case CancelRequest:
2473 case LocateRequest:
2474 case LocateReply:
2475 case CloseConnection:
2476 case MessageError:
2477 case Fragment:
2478 return FALSE; /* not handled yet */
2480 default:
2481 return FALSE; /* not handled yet */
2483 } /* switch */
2485 template_main_dissector_end = """\
2487 return FALSE;
2489 } /* End of main dissector */
2498 #-------------------------------------------------------------#
2499 # Exception handling templates #
2500 #-------------------------------------------------------------#
2508 template_exception_helpers_start = """\
2509 /* Begin Exception Helper Functions */
2512 template_exception_helpers_end = """\
2514 /* End Exception Helper Functions */
2520 # template for Main delegator for exception handling
2523 template_main_exception_delegator_start = """\
2525 * Main delegator for exception handling
2528 static gboolean
2529 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_)
2531 proto_tree *tree _U_;
2533 if (!header->exception_id)
2534 return FALSE;
2539 # template for exception delegation code body
2541 template_ex_delegate_code = """\
2542 if (strcmp(header->exception_id, "@exname@") == 0) {
2543 tree = start_dissecting(tvb, pinfo, ptree, offset);
2544 decode_ex_@sname@(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian); /* @exname@ */
2545 return TRUE;
2551 # End of Main delegator for exception handling
2554 template_main_exception_delegator_end = """
2555 return FALSE; /* user exception not found */
2560 # template for exception helper code
2564 template_exception_helper_function_start = """\
2565 /* Exception = @exname@ */
2566 static void
2567 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_)
2569 proto_item *item _U_;
2572 template_exception_helper_function_end = """\
2578 # template for struct helper code
2582 template_struct_helper_function_start = """\
2583 /* Struct = @stname@ */
2584 static void
2585 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_)
2589 template_struct_helper_function_end = """\
2594 # template for union helper code
2598 template_union_helper_function_start = """\
2599 /* Union = @unname@ */
2600 static void
2601 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_)
2603 proto_item* item _U_;
2606 template_union_helper_function_end = """\
2612 #-------------------------------------------------------------#
2613 # Value string templates #
2614 #-------------------------------------------------------------#
2616 template_value_string_start = """\
2617 static const value_string @valstringname@[] = {
2619 template_value_string_entry = """\
2620 { @intval@, \"@description@\" },"""
2622 template_value_string_end = """\
2623 { 0, NULL },
2629 #-------------------------------------------------------------#
2630 # Enum handling templates #
2631 #-------------------------------------------------------------#
2633 template_comment_enums_start = """\
2635 * IDL Enums Start
2638 template_comment_enums_end = """\
2640 * IDL Enums End
2643 template_comment_enum_comment = """\
2645 * Enum = @ename@
2646 */"""
2650 #-------------------------------------------------------------#
2651 # Attribute handling templates #
2652 #-------------------------------------------------------------#
2655 template_comment_attributes_start = """\
2657 * IDL Attributes Start
2662 # get/set accessor method names are language mapping dependant.
2665 template_attributes_declare_Java_get = """static const char get_@sname@_at[] = \"_get_@atname@\" ;"""
2666 template_attributes_declare_Java_set = """static const char set_@sname@_at[] = \"_set_@atname@\" ;"""
2668 template_comment_attributes_end = """
2670 * IDL Attributes End
2676 # template for Attribute delegation code
2678 # Note: _get_xxx() should only be called for Reply with NO_EXCEPTION
2679 # Note: _set_xxx() should only be called for Request
2683 template_at_delegate_code_get = """\
2684 if (strcmp(operation, get_@sname@_at) == 0 && (header->message_type == Reply) && (header->rep_status == NO_EXCEPTION) ) {
2685 tree = start_dissecting(tvb, pinfo, ptree, offset);
2686 decode_get_@sname@_at(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);
2687 return TRUE;
2690 template_at_delegate_code_set = """\
2691 if (strcmp(operation, set_@sname@_at) == 0 && (header->message_type == Request) ) {
2692 tree = start_dissecting(tvb, pinfo, ptree, offset);
2693 decode_set_@sname@_at(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);
2694 return TRUE;
2697 template_attribute_helpers_start = """\
2698 /* Begin Attribute Helper Functions */
2700 template_attribute_helpers_end = """\
2702 /* End Attribute Helper Functions */
2706 # template for attribute helper code
2710 template_attribute_helper_function_start = """\
2712 /* Attribute = @atname@ */
2713 static void
2714 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_)
2716 proto_item* item _U_;
2719 template_attribute_helper_function_end = """\
2724 #-------------------------------------------------------------#
2725 # Debugging templates #
2726 #-------------------------------------------------------------#
2729 # Template for outputting TODO "C" comments
2730 # so user know I need ti improve something.
2733 template_debug_TODO = """\
2735 /* TODO - @message@ */
2738 # Template for outputting WARNING "C" comments
2739 # so user know if I have found a problem.
2742 template_debug_WARNING = """\
2743 /* WARNING - @message@ */
2748 #-------------------------------------------------------------#
2749 # IDL Union templates #
2750 #-------------------------------------------------------------#
2752 template_comment_union_code_start = """\
2754 * IDL Union Start - @uname@
2757 template_comment_union_code_end = """
2759 * IDL union End - @uname@
2762 template_comment_union_code_discriminant = """\
2764 * IDL Union - Discriminant - @uname@
2768 # Cast Unions types to something appropriate
2769 # Enum value cast to guint32, all others cast to gint32
2770 # as omniidl accessor returns integer or Enum.
2773 template_union_code_save_discriminant_enum = """\
2774 disc_s_@discname@ = (gint32) u_octet4; /* save Enum Value discriminant and cast to gint32 */
2776 template_union_code_save_discriminant_long = """\
2777 disc_s_@discname@ = (gint32) s_octet4; /* save gint32 discriminant and cast to gint32 */
2780 template_union_code_save_discriminant_ulong = """\
2781 disc_s_@discname@ = (gint32) u_octet4; /* save guint32 discriminant and cast to gint32 */
2783 template_union_code_save_discriminant_short = """\
2784 disc_s_@discname@ = (gint32) s_octet2; /* save gint16 discriminant and cast to gint32 */
2787 template_union_code_save_discriminant_ushort = """\
2788 disc_s_@discname@ = (gint32) u_octet2; /* save guint16 discriminant and cast to gint32 */
2790 template_union_code_save_discriminant_char = """\
2791 disc_s_@discname@ = (gint32) u_octet1; /* save guint1 discriminant and cast to gint32 */
2793 template_union_code_save_discriminant_boolean = """\
2794 disc_s_@discname@ = (gint32) u_octet1; /* save guint1 discriminant and cast to gint32 */
2796 template_comment_union_code_label_compare_start = """\
2797 if (disc_s_@discname@ == @labelval@) {
2799 template_comment_union_code_label_compare_end = """\
2800 return; /* End Compare for this discriminant type */
2805 template_comment_union_code_label_default_start = """
2806 /* Default Union Case Start */
2808 template_comment_union_code_label_default_end = """\
2809 /* Default Union Case End */
2813 # Templates for function prototypes.
2814 # This is used in genDeclares() for declaring function prototypes
2815 # for structs and union helper functions.
2818 template_hf_operations = """
2819 static int hf_operationrequest = -1;/* Request_Operation field */
2822 template_hf = """\
2823 static int hf_@name@ = -1;"""
2825 template_prototype_start_dissecting = """
2826 static proto_tree *start_dissecting(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset);
2829 template_prototype_struct_start = """\
2830 /* Struct prototype declaration Start */
2832 template_prototype_struct_end = """\
2833 /* Struct prototype declaration End */
2835 template_prototype_struct_body = """\
2836 /* Struct = @stname@ */
2837 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_);
2839 template_decode_struct = """\
2840 decode_@name@_st(tvb, pinfo, tree, item, offset, header, operation, stream_is_big_endian);"""
2842 template_prototype_union_start = """\
2843 /* Union prototype declaration Start */"""
2845 template_prototype_union_end = """\
2846 /* Union prototype declaration End */"""
2848 template_prototype_union_body = """
2849 /* Union = @unname@ */
2850 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_);
2852 template_decode_union = """
2853 decode_@name@_un(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);