Glib docs recommend using the slice API if you know you won't need to realloc.
[metze/wireshark/wip.git] / tools / wireshark_gen.py
1 # -*- python -*-
2 #
3 # $Id$
4 #
5 # wireshark_gen.py (part of idl2wrs)
6 #
7 # Author : Frank Singleton (frank.singleton@ericsson.com)
8 #
9 #    Copyright (C) 2001 Frank Singleton, Ericsson Inc.
10 #
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.
14 #
15 #  Please see packet-giop.h in Wireshark distro for API description.
16 #  Wireshark is available at http://www.wireshark.org/
17 #
18 #  Omniidl is part of the OmniOrb distribution, and is available at
19 #  http://omniorb.sourceforge.net
20 #
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.
25 #
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.
30 #
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.
35 #
36 # Description:
37 #
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.
41 #
42 #
43 # Strategy (sneaky but ...)
44 #
45 # problem: I dont know what variables to declare until AFTER the helper functions
46 # have been built, so ...
47 #
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.
52 #
53
54
55 """Wireshark IDL compiler back-end."""
56
57 from omniidl import idlast, idltype, idlutil, output
58 import sys, string
59 import tempfile
60
61 #
62 # Output class, generates "C" src code for the sub-dissector
63 #
64 # in:
65 #
66 #
67 # self - me
68 # st   - output stream
69 # node - a reference to an Operations object.
70 # name - scoped name (Module::Module::Interface:: .. ::Operation
71 #
72
73
74
75 #
76 # TODO -- FS
77 #
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
93 #
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]
101 #
102 # Also test, Test, TEST
103 #
104
105
106
107 #
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
115 #
116 #
117
118 class wireshark_gen_C:
119
120
121     #
122     # Turn DEBUG stuff on/off
123     #
124
125     DEBUG = 0
126
127     #
128     # Some string constants for our templates
129     #
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;"
138
139     c_float       = "gfloat    my_float;"
140     c_double      = "gdouble   my_double;"
141
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)
147
148     #
149     # Constructor
150     #
151
152     def __init__(self, st, protocol_name, dissector_name ,description):
153         self.st = output.Stream(tempfile.TemporaryFile(),4) # for first pass only
154
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
165
166
167     #
168     # genCode()
169     #
170     # Main entry point, controls sequence of
171     # generated code.
172     #
173     #
174
175     def genCode(self,oplist, atlist, enlist, stlist, unlist):   # operation,attribute,enums,struct and union lists
176
177
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.
181
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.
185
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.
189
190
191         self.fn_hash_built = 1          # DONE, so now I know , see genOperation()
192
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
204
205
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"
210
211         self.genHelpers(oplist,stlist,unlist)  # operation, struct and union decode helper functions
212
213         self.genMainEntryStart(oplist)
214         self.genOpDelegator(oplist)
215         self.genAtDelegator(atlist)
216         self.genMainEntryEnd()
217
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()
222
223         #self.dumpvars()                 # debug
224
225
226
227     #
228     # genHeader
229     #
230     # Generate Standard Wireshark Header Comments
231     #
232     #
233
234     def genHeader(self):
235         self.st.out(self.template_Header,dissector_name=self.dissname)
236         if self.DEBUG:
237             print "XXX genHeader"
238
239
240
241
242     #
243     # genEthCopyright
244     #
245     # Wireshark Copyright Info
246     #
247     #
248
249     def genEthCopyright(self):
250         if self.DEBUG:
251             print "XXX genEthCopyright"
252         self.st.out(self.template_wireshark_copyright)
253
254
255     #
256     # genGPL
257     #
258     # GPL licencse
259     #
260     #
261
262     def genGPL(self):
263         if self.DEBUG:
264             print "XXX genGPL"
265
266         self.st.out(self.template_GPL)
267
268     #
269     # genIncludes
270     #
271     # GPL licencse
272     #
273     #
274
275     def genIncludes(self):
276         if self.DEBUG:
277             print "XXX genIncludes"
278
279         self.st.out(self.template_Includes)
280
281     #
282     # genOpDeclares()
283     #
284     # Generate hf variables for operation filters
285     #
286     # in: opnode ( an operation node)
287     #
288
289     def genOpDeclares(self, op):
290         if self.DEBUG:
291             print "XXX genOpDeclares"
292             print "XXX return type  = " , op.returnType().kind()
293
294         sname = self.namespace(op, "_")
295         rt = op.returnType()
296
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")
303
304         for p in op.parameters():
305              self.st.out(self.template_hf, name=sname + "_" + p.identifier())
306
307     #
308     # genAtDeclares()
309     #
310     # Generate hf variables for attributes
311     #
312     # in: at ( an attribute)
313     #
314
315     def genAtDeclares(self, at):
316         if self.DEBUG:
317             print "XXX genAtDeclares"
318
319         for decl in at.declarators():
320             sname = self.namespace(decl, "_")
321
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())
325
326     #
327     # genStDeclares()
328     #
329     # Generate hf variables for structs
330     #
331     # in: st ( a struct)
332     #
333
334     def genStDeclares(self, st):
335         if self.DEBUG:
336             print "XXX genStDeclares"
337
338         sname = self.namespace(st, "_")
339
340         for m in st.members():
341             for decl in m.declarators():
342                 self.st.out(self.template_hf, name=sname + "_" + decl.identifier())
343
344     #
345     # genExDeclares()
346     #
347     # Generate hf variables for user exception filters
348     #
349     # in: exnode ( an exception node)
350     #
351
352     def genExDeclares(self,ex):
353         if self.DEBUG:
354             print "XXX genExDeclares"
355
356         sname = self.namespace(ex, "_")
357
358         for m in ex.members():
359             for decl in m.declarators():
360                 self.st.out(self.template_hf, name=sname + "_" + decl.identifier())
361
362     #
363     # genUnionDeclares()
364     #
365     # Generate hf variables for union filters
366     #
367     # in: un ( an union)
368     #
369
370     def genUnionDeclares(self,un):
371         if self.DEBUG:
372             print "XXX genUnionDeclares"
373
374         sname = self.namespace(un, "_")
375         self.st.out(self.template_hf, name=sname + "_" + un.identifier())
376
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())
380
381     #
382     # genDeclares
383     #
384     # generate function prototypes if required
385     #
386     # Currently this is used for struct and union helper function declarations.
387     #
388
389
390     def genDeclares(self,oplist,atlist,enlist,stlist,unlist):
391         if self.DEBUG:
392             print "XXX genDeclares"
393
394         # prototype for operation filters 
395         self.st.out(self.template_hf_operations)
396
397         #operation specific filters
398         if (len(oplist) > 0):
399             self.st.out(self.template_proto_register_op_filter_comment)
400         for op in oplist:
401             self.genOpDeclares(op)
402
403         #attribute filters
404         if (len(atlist) > 0):
405             self.st.out(self.template_proto_register_at_filter_comment)
406         for at in atlist:
407             self.genAtDeclares(at)
408
409         #struct filters
410         if (len(stlist) > 0):
411             self.st.out(self.template_proto_register_st_filter_comment)
412         for st in stlist:
413             self.genStDeclares(st)
414
415         # exception List filters
416         exlist = self.get_exceptionList(oplist) # grab list of exception nodes
417         if (len(exlist) > 0):
418             self.st.out(self.template_proto_register_ex_filter_comment)
419         for ex in exlist:
420             if (ex.members()):          # only if has members
421                 self.genExDeclares(ex)
422
423         #union filters
424         if (len(unlist) > 0):
425             self.st.out(self.template_proto_register_un_filter_comment)
426         for un in unlist:
427             self.genUnionDeclares(un)
428
429         # prototype for start_dissecting()
430
431         self.st.out(self.template_prototype_start_dissecting)
432
433         # struct prototypes
434
435         if len(stlist):
436             self.st.out(self.template_prototype_struct_start)
437             for st in stlist:
438                 #print st.repoId()
439                 sname = self.namespace(st, "_")
440                 self.st.out(self.template_prototype_struct_body, stname=st.repoId(),name=sname)
441
442             self.st.out(self.template_prototype_struct_end)
443
444         # union prototypes
445         if len(unlist):
446             self.st.out(self.template_prototype_union_start)
447             for un in unlist:
448                 sname = self.namespace(un, "_")
449                 self.st.out(self.template_prototype_union_body, unname=un.repoId(),name=sname)
450             self.st.out(self.template_prototype_union_end)
451
452
453     #
454     # genProtocol
455     #
456     #
457
458     def genProtocol(self):
459         self.st.out(self.template_protocol, dissector_name=self.dissname)
460         self.st.out(self.template_init_boundary)
461
462     #
463     # genMainEntryStart
464     #
465
466     def genMainEntryStart(self,oplist):
467         self.st.out(self.template_main_dissector_start, dissname=self.dissname, disprot=self.protoname)
468         self.st.inc_indent()
469         self.st.out(self.template_main_dissector_switch_msgtype_start)
470         self.st.out(self.template_main_dissector_switch_msgtype_start_request_reply)
471         self.st.inc_indent()
472
473
474     #
475     # genMainEntryEnd
476     #
477
478     def genMainEntryEnd(self):
479
480         self.st.out(self.template_main_dissector_switch_msgtype_end_request_reply)
481         self.st.dec_indent()
482         self.st.out(self.template_main_dissector_switch_msgtype_all_other_msgtype)
483         self.st.dec_indent()
484         self.st.out(self.template_main_dissector_end)
485
486
487     #
488     # genAtList
489     #
490     # in: atlist
491     #
492     # out: C code for IDL attribute decalarations.
493     #
494     # NOTE: Mapping of attributes to  operation(function) names is tricky.
495     #
496     # The actual accessor function names are language-mapping specific. The attribute name
497     # is subject to OMG IDL's name scoping rules; the accessor function names are
498     # guaranteed not to collide with any legal operation names specifiable in OMG IDL.
499     #
500     # eg:
501     #
502     # static const char get_Penguin_Echo_get_width_at[] = "get_width" ;
503     # static const char set_Penguin_Echo_set_width_at[] = "set_width" ;
504     #
505     # or:
506     #
507     # static const char get_Penguin_Echo_get_width_at[] = "_get_width" ;
508     # static const char set_Penguin_Echo_set_width_at[] = "_set_width" ;
509     #
510     # TODO: Implement some language dependant templates to handle naming conventions
511     #       language <=> attribute. for C, C++. Java etc
512     #
513     # OR, just add a runtime GUI option to select language binding for attributes -- FS
514     #
515     #
516     #
517     # ie: def genAtlist(self,atlist,language)
518     #
519
520
521
522     def genAtList(self,atlist):
523         self.st.out(self.template_comment_attributes_start)
524
525         for n in atlist:
526             for i in n.declarators():   #
527                 sname = self.namespace(i, "_")
528                 atname = i.identifier()
529                 self.st.out(self.template_attributes_declare_Java_get, sname=sname, atname=atname)
530                 if not n.readonly():
531                     self.st.out(self.template_attributes_declare_Java_set, sname=sname, atname=atname)
532
533         self.st.out(self.template_comment_attributes_end)
534
535
536     #
537     # genEnList
538     #
539     # in: enlist
540     #
541     # out: C code for IDL Enum decalarations using "static const value_string" template
542     #
543
544
545
546     def genEnList(self,enlist):
547
548         self.st.out(self.template_comment_enums_start)
549
550         for enum in enlist:
551             sname = self.namespace(enum, "_")
552
553             self.st.out(self.template_comment_enum_comment, ename=enum.repoId())
554             self.st.out(self.template_value_string_start, valstringname=sname)
555             for enumerator in enum.enumerators():
556                 self.st.out(self.template_value_string_entry, intval=str(self.valFromEnum(enum,enumerator)), description=enumerator.identifier())
557
558
559             #atname = n.identifier()
560             self.st.out(self.template_value_string_end, valstringname=sname)
561
562         self.st.out(self.template_comment_enums_end)
563
564
565
566
567
568
569
570
571
572
573     #
574     # genExceptionDelegator
575     #
576     # in: oplist
577     #
578     # out: C code for User exception delegator
579     #
580     # eg:
581     #
582     #
583
584     def genExceptionDelegator(self,oplist):
585
586         self.st.out(self.template_main_exception_delegator_start)
587         self.st.inc_indent()
588
589         exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
590
591         for ex in exlist:
592             if self.DEBUG:
593                 print "XXX Exception " , ex.repoId()
594                 print "XXX Exception Identifier" , ex.identifier()
595                 print "XXX Exception Scoped Name" , ex.scopedName()
596
597             if (ex.members()):          # only if has members
598                 sname = self.namespace(ex, "_")
599                 exname = ex.repoId()
600                 self.st.out(self.template_ex_delegate_code,  sname=sname, exname=ex.repoId())
601
602         self.st.dec_indent()
603         self.st.out(self.template_main_exception_delegator_end)
604
605
606     #
607     # genAttribueHelpers()
608     #
609     # Generate private helper functions to decode Attributes.
610     #
611     # in: atlist
612     #
613     # For readonly attribute - generate get_xxx()
614     # If NOT readonly attribute - also generate set_xxx()
615     #
616
617     def genAttributeHelpers(self,atlist):
618         if self.DEBUG:
619             print "XXX genAttributeHelpers: atlist = ", atlist
620
621         self.st.out(self.template_attribute_helpers_start)
622
623         for attrib in atlist:
624             for decl in attrib.declarators():
625                 self.genAtHelper(attrib,decl,"get") # get accessor
626                 if not attrib.readonly():
627                     self.genAtHelper(attrib,decl,"set") # set accessor
628
629         self.st.out(self.template_attribute_helpers_end)
630
631     #
632     # genAtHelper()
633     #
634     # Generate private helper functions to decode an attribute
635     #
636     # in: at - attribute node
637     # in: decl - declarator belonging to this attribute
638     # in: order - to generate a "get" or "set" helper
639
640     def genAtHelper(self,attrib,decl,order):
641         if self.DEBUG:
642             print "XXX genAtHelper"
643
644         sname = order + "_" + self.namespace(decl, "_")  # must use set or get prefix to avoid collision
645         self.curr_sname = sname                    # update current opnode/exnode scoped name
646
647         if not self.fn_hash_built:
648             self.fn_hash[sname] = []        # init empty list as val for this sname key
649                                             # but only if the fn_hash is not already built
650
651         self.st.out(self.template_attribute_helper_function_start, sname=sname, atname=decl.repoId())
652         self.st.inc_indent()
653
654         if (len(self.fn_hash[sname]) > 0):
655             self.st.out(self.template_helper_function_vars_start)
656             self.dumpCvars(sname)
657             self.st.out(self.template_helper_function_vars_end )
658
659         self.getCDR(attrib.attrType(), sname + "_" + decl.identifier() )
660
661         self.st.dec_indent()
662         self.st.out(self.template_attribute_helper_function_end)
663
664
665
666     #
667     # genExceptionHelpers()
668     #
669     # Generate private helper functions to decode Exceptions used
670     # within operations
671     #
672     # in: oplist
673     #
674
675
676     def genExceptionHelpers(self,oplist):
677         exlist = self.get_exceptionList(oplist) # grab list of exception nodes
678         if self.DEBUG:
679             print "XXX genExceptionHelpers: exlist = ", exlist
680
681         self.st.out(self.template_exception_helpers_start)
682         for ex in exlist:
683             if (ex.members()):          # only if has members
684                 #print "XXX Exception = " + ex.identifier()
685                 self.genExHelper(ex)
686
687         self.st.out(self.template_exception_helpers_end)
688
689
690     #
691     # genExhelper()
692     #
693     # Generate private helper functions to decode User Exceptions
694     #
695     # in: exnode ( an exception node)
696     #
697
698     def genExHelper(self,ex):
699         if self.DEBUG:
700             print "XXX genExHelper"
701
702         sname = self.namespace(ex, "_")
703         self.curr_sname = sname         # update current opnode/exnode scoped name
704         if not self.fn_hash_built:
705             self.fn_hash[sname] = []        # init empty list as val for this sname key
706                                             # but only if the fn_hash is not already built
707
708         self.st.out(self.template_exception_helper_function_start, sname=sname, exname=ex.repoId())
709         self.st.inc_indent()
710
711         if (len(self.fn_hash[sname]) > 0):
712             self.st.out(self.template_helper_function_vars_start)
713             self.dumpCvars(sname)
714             self.st.out(self.template_helper_function_vars_end )
715
716         for m in ex.members():
717             if self.DEBUG:
718                 print "XXX genExhelper, member = ", m, "member type = ", m.memberType()
719
720             for decl in m.declarators():
721                 if self.DEBUG:
722                     print "XXX genExhelper, d = ", decl
723
724                 if decl.sizes():        # an array
725                     indices = self.get_indices_from_sizes(decl.sizes())
726                     string_indices = '%i ' % indices # convert int to string
727                     self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
728                     self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
729                     self.addvar(self.c_i + decl.identifier() + ";")
730
731                     self.st.inc_indent()
732                     self.getCDR(m.memberType(), sname + "_" + decl.identifier() )
733
734                     self.st.dec_indent()
735                     self.st.out(self.template_get_CDR_array_end)
736
737
738                 else:
739                     self.getCDR(m.memberType(), sname + "_" + decl.identifier() )
740
741         self.st.dec_indent()
742         self.st.out(self.template_exception_helper_function_end)
743
744
745     #
746     # genHelpers()
747     #
748     # Generate private helper functions for each IDL operation.
749     # Generate private helper functions for each IDL struct.
750     # Generate private helper functions for each IDL union.
751     #
752     #
753     # in: oplist, stlist, unlist
754     #
755
756
757     def genHelpers(self,oplist,stlist,unlist):
758         for op in oplist:
759             self.genOperation(op)
760         for st in stlist:
761             self.genStructHelper(st)
762         for un in unlist:
763             self.genUnionHelper(un)
764
765     #
766     # genOperation()
767     #
768     # Generate private helper functions for a specificIDL operation.
769     #
770     # in: opnode
771     #
772
773     def genOperation(self,opnode):
774         if self.DEBUG:
775             print "XXX genOperation called"
776
777         sname = self.namespace(opnode, "_")
778         if not self.fn_hash_built:
779             self.fn_hash[sname] = []        # init empty list as val for this sname key
780                                             # but only if the fn_hash is not already built
781
782         self.curr_sname = sname         # update current opnode's scoped name
783         opname = opnode.identifier()
784
785         self.st.out(self.template_helper_function_comment, repoid=opnode.repoId() )
786
787         self.st.out(self.template_helper_function_start, sname=sname)
788         self.st.inc_indent()
789
790         if (len(self.fn_hash[sname]) > 0):
791             self.st.out(self.template_helper_function_vars_start)
792             self.dumpCvars(sname)
793             self.st.out(self.template_helper_function_vars_end )
794
795         self.st.out(self.template_helper_switch_msgtype_start)
796
797         self.st.out(self.template_helper_switch_msgtype_request_start)
798         self.st.inc_indent()
799         self.genOperationRequest(opnode)
800         self.st.out(self.template_helper_switch_msgtype_request_end)
801         self.st.dec_indent()
802
803         self.st.out(self.template_helper_switch_msgtype_reply_start)
804         self.st.inc_indent()
805
806         self.st.out(self.template_helper_switch_rep_status_start)
807
808
809         self.st.out(self.template_helper_switch_msgtype_reply_no_exception_start)
810         self.st.inc_indent()
811         self.genOperationReply(opnode)
812         self.st.out(self.template_helper_switch_msgtype_reply_no_exception_end)
813         self.st.dec_indent()
814
815         self.st.out(self.template_helper_switch_msgtype_reply_user_exception_start)
816         self.st.inc_indent()
817         self.genOpExceptions(opnode)
818         self.st.out(self.template_helper_switch_msgtype_reply_user_exception_end)
819         self.st.dec_indent()
820
821         self.st.out(self.template_helper_switch_msgtype_reply_default_start)
822         self.st.out(self.template_helper_switch_msgtype_reply_default_end)
823
824         self.st.out(self.template_helper_switch_rep_status_end)
825
826         self.st.dec_indent()
827
828         self.st.out(self.template_helper_switch_msgtype_default_start)
829         self.st.out(self.template_helper_switch_msgtype_default_end)
830
831         self.st.out(self.template_helper_switch_msgtype_end)
832         self.st.dec_indent()
833
834
835         self.st.out(self.template_helper_function_end, sname=sname)
836
837
838
839
840     #
841     # Decode function parameters for a GIOP request message
842     #
843     #
844
845     def genOperationRequest(self,opnode):
846         for p in opnode.parameters():
847             if p.is_in():
848                 if self.DEBUG:
849                     print "XXX parameter = " ,p
850                     print "XXX parameter type = " ,p.paramType()
851                     print "XXX parameter type kind = " ,p.paramType().kind()
852
853                 self.getCDR(p.paramType(), self.curr_sname + "_" + p.identifier())
854
855
856     #
857     # Decode function parameters for a GIOP reply message
858     #
859
860
861     def genOperationReply(self,opnode):
862
863         rt = opnode.returnType()        # get return type
864         if self.DEBUG:
865             print "XXX genOperationReply"
866             print "XXX opnode  = " , opnode
867             print "XXX return type  = " , rt
868             print "XXX return type.unalias  = " , rt.unalias()
869             print "XXX return type.kind()  = " , rt.kind();
870
871         sname = self.namespace(opnode, "_")
872
873         if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
874             #self.getCDR(rt.decl().alias().aliasType(),"dummy")    # return value maybe a typedef
875             self.get_CDR_alias(rt, sname + "_return" )
876             #self.get_CDR_alias(rt, rt.name() )
877
878         else:
879             self.getCDR(rt, sname + "_return")    # return value is NOT an alias
880
881         for p in opnode.parameters():
882             if p.is_out():              # out or inout
883                 self.getCDR(p.paramType(), self.curr_sname + "_" + p.identifier())
884
885         #self.st.dec_indent()
886
887     def genOpExceptions(self,opnode):
888         for ex in opnode.raises():
889             if ex.members():
890                 #print ex.members()
891                 for m in ex.members():
892                     t=0
893                     #print m.memberType(), m.memberType().kind()
894     #
895     # Delegator for Operations
896     #
897
898     def genOpDelegator(self,oplist):
899         for op in oplist:
900             iname = "/".join(op.scopedName()[:-1])
901             opname = op.identifier()
902             sname = self.namespace(op, "_")
903             self.st.out(self.template_op_delegate_code, interface=iname, sname=sname, opname=opname)
904
905     #
906     # Delegator for Attributes
907     #
908
909     def genAtDelegator(self,atlist):
910         for a in atlist:
911             for i in a.declarators():
912                 atname = i.identifier()
913                 sname = self.namespace(i, "_")
914                 self.st.out(self.template_at_delegate_code_get, sname=sname)
915                 if not a.readonly():
916                     self.st.out(self.template_at_delegate_code_set, sname=sname)
917
918
919     #
920     # Add a variable declaration to the hash of list
921     #
922
923     def addvar(self, var):
924         if not ( var in self.fn_hash[self.curr_sname] ):
925             self.fn_hash[self.curr_sname].append(var)
926
927     #
928     # Print the variable declaration from  the hash of list
929     #
930
931
932     def dumpvars(self):
933         for fn in self.fn_hash.keys():
934             print "FN = " + fn
935             for v in self.fn_hash[fn]:
936                 print "-> " + v
937     #
938     # Print the "C" variable declaration from  the hash of list
939     # for a given scoped operation name (eg: tux_penguin_eat)
940     #
941
942
943     def dumpCvars(self, sname):
944             for v in self.fn_hash[sname]:
945                 self.st.out(v)
946
947
948     #
949     # Given an enum node, and a enumerator node, return
950     # the enumerator's numerical value.
951     #
952     # eg: enum Color {red,green,blue} should return
953     # val = 1 for green
954     #
955
956     def valFromEnum(self,enumNode, enumeratorNode):
957         if self.DEBUG:
958             print "XXX valFromEnum, enumNode = ", enumNode, " from ", enumNode.repoId()
959             print "XXX valFromEnum, enumeratorNode = ", enumeratorNode, " from ", enumeratorNode.repoId()
960
961         if isinstance(enumeratorNode,idlast.Enumerator):
962             value = enumNode.enumerators().index(enumeratorNode)
963             return value
964
965
966 ## tk_null               = 0
967 ## tk_void               = 1
968 ## tk_short              = 2
969 ## tk_long               = 3
970 ## tk_ushort             = 4
971 ## tk_ulong              = 5
972 ## tk_float              = 6
973 ## tk_double             = 7
974 ## tk_boolean            = 8
975 ## tk_char               = 9
976 ## tk_octet              = 10
977 ## tk_any                = 11
978 ## tk_TypeCode           = 12
979 ## tk_Principal          = 13
980 ## tk_objref             = 14
981 ## tk_struct             = 15
982 ## tk_union              = 16
983 ## tk_enum               = 17
984 ## tk_string             = 18
985 ## tk_sequence           = 19
986 ## tk_array              = 20
987 ## tk_alias              = 21
988 ## tk_except             = 22
989 ## tk_longlong           = 23
990 ## tk_ulonglong          = 24
991 ## tk_longdouble         = 25
992 ## tk_wchar              = 26
993 ## tk_wstring            = 27
994 ## tk_fixed              = 28
995 ## tk_value              = 29
996 ## tk_value_box          = 30
997 ## tk_native             = 31
998 ## tk_abstract_interface = 32
999
1000
1001     #
1002     # getCDR()
1003     #
1004     # This is the main "iterator" function. It takes a node, and tries to output
1005     # a get_CDR_XXX accessor method(s). It can call itself multiple times
1006     # if I find nested structures etc.
1007     #
1008
1009     def getCDR(self,type,name="fred"):
1010
1011         pt = type.unalias().kind()      # param CDR type
1012         pn = name                       # param name
1013
1014         if self.DEBUG:
1015             print "XXX getCDR: kind = " , pt
1016             print "XXX getCDR: name = " , pn
1017
1018         if pt == idltype.tk_ulong:
1019             self.get_CDR_ulong(pn)
1020         elif pt == idltype.tk_longlong:
1021             self.get_CDR_longlong(pn)
1022         elif pt == idltype.tk_ulonglong:
1023             self.get_CDR_ulonglong(pn)
1024         elif pt ==  idltype.tk_void:
1025             self.get_CDR_void(pn)
1026         elif pt ==  idltype.tk_short:
1027             self.get_CDR_short(pn)
1028         elif pt ==  idltype.tk_long:
1029             self.get_CDR_long(pn)
1030         elif pt ==  idltype.tk_ushort:
1031             self.get_CDR_ushort(pn)
1032         elif pt ==  idltype.tk_float:
1033             self.get_CDR_float(pn)
1034         elif pt ==  idltype.tk_double:
1035             self.get_CDR_double(pn)
1036         elif pt == idltype.tk_fixed:
1037             self.get_CDR_fixed(type.unalias(),pn)
1038         elif pt ==  idltype.tk_boolean:
1039             self.get_CDR_boolean(pn)
1040         elif pt ==  idltype.tk_char:
1041             self.get_CDR_char(pn)
1042         elif pt ==  idltype.tk_octet:
1043             self.get_CDR_octet(pn)
1044         elif pt ==  idltype.tk_any:
1045             self.get_CDR_any(pn)
1046         elif pt ==  idltype.tk_string:
1047             self.get_CDR_string(pn)
1048         elif pt ==  idltype.tk_wstring:
1049             self.get_CDR_wstring(pn)
1050         elif pt ==  idltype.tk_wchar:
1051             self.get_CDR_wchar(pn)
1052         elif pt ==  idltype.tk_enum:
1053             #print type.decl()
1054             self.get_CDR_enum(pn,type)
1055             #self.get_CDR_enum(pn)
1056
1057         elif pt ==  idltype.tk_struct:
1058             self.get_CDR_struct(type,pn)
1059         elif pt ==  idltype.tk_TypeCode: # will I ever get here ?
1060             self.get_CDR_TypeCode(pn)
1061         elif pt == idltype.tk_sequence:
1062             if type.unalias().seqType().kind() == idltype.tk_octet:
1063                 self.get_CDR_sequence_octet(type,pn)
1064             else:
1065                 self.get_CDR_sequence(type,pn)
1066         elif pt == idltype.tk_objref:
1067             self.get_CDR_objref(type,pn)
1068         elif pt == idltype.tk_array:
1069             pn = pn # Supported elsewhere
1070         elif pt == idltype.tk_union:
1071             self.get_CDR_union(type,pn)
1072         elif pt == idltype.tk_alias:
1073             if self.DEBUG:
1074                 print "XXXXX Alias type XXXXX " , type
1075             self.get_CDR_alias(type,pn)
1076         else:
1077             self.genWARNING("Unknown typecode = " + '%i ' % pt) # put comment in source code
1078
1079
1080     #
1081     # get_CDR_XXX methods are here ..
1082     #
1083     #
1084
1085
1086     def get_CDR_ulong(self,pn):
1087         self.st.out(self.template_get_CDR_ulong, hfname=pn)
1088
1089     def get_CDR_short(self,pn):
1090         self.st.out(self.template_get_CDR_short, hfname=pn)
1091
1092     def get_CDR_void(self,pn):
1093         self.st.out(self.template_get_CDR_void, hfname=pn)
1094
1095     def get_CDR_long(self,pn):
1096         self.st.out(self.template_get_CDR_long, hfname=pn)
1097
1098     def get_CDR_ushort(self,pn):
1099         self.st.out(self.template_get_CDR_ushort, hfname=pn)
1100
1101     def get_CDR_float(self,pn):
1102         self.st.out(self.template_get_CDR_float, hfname=pn)
1103
1104     def get_CDR_double(self,pn):
1105         self.st.out(self.template_get_CDR_double, hfname=pn)
1106
1107     def get_CDR_longlong(self,pn):
1108         self.st.out(self.template_get_CDR_longlong, hfname=pn)
1109
1110     def get_CDR_ulonglong(self,pn):
1111         self.st.out(self.template_get_CDR_ulonglong, hfname=pn)
1112
1113     def get_CDR_boolean(self,pn):
1114         self.st.out(self.template_get_CDR_boolean, hfname=pn)
1115
1116     def get_CDR_fixed(self,type,pn):
1117         if self.DEBUG:
1118             print "XXXX calling get_CDR_fixed, type = ", type
1119             print "XXXX calling get_CDR_fixed, type.digits() = ", type.digits()
1120             print "XXXX calling get_CDR_fixed, type.scale() = ", type.scale()
1121
1122         string_digits = '%i ' % type.digits() # convert int to string
1123         string_scale  = '%i ' % type.scale()  # convert int to string
1124         string_length  = '%i ' % self.dig_to_len(type.digits())  # how many octets to hilight for a number of digits
1125
1126         self.st.out(self.template_get_CDR_fixed, varname=pn, digits=string_digits, scale=string_scale, length=string_length )
1127         self.addvar(self.c_seq)
1128
1129
1130     def get_CDR_char(self,pn):
1131         self.st.out(self.template_get_CDR_char, hfname=pn)
1132
1133     def get_CDR_octet(self,pn):
1134         self.st.out(self.template_get_CDR_octet, hfname=pn)
1135
1136     def get_CDR_any(self,pn):
1137         self.st.out(self.template_get_CDR_any, varname=pn)
1138
1139     def get_CDR_enum(self,pn,type):
1140         #self.st.out(self.template_get_CDR_enum, hfname=pn)
1141         sname = self.namespace(type.unalias(), "_")
1142         self.st.out(self.template_get_CDR_enum_symbolic, valstringarray=sname,hfname=pn)
1143         self.addvar(self.c_u_octet4)
1144
1145     def get_CDR_string(self,pn):
1146         self.st.out(self.template_get_CDR_string, hfname=pn)
1147
1148     def get_CDR_wstring(self,pn):
1149         self.st.out(self.template_get_CDR_wstring, varname=pn)
1150         self.addvar(self.c_u_octet4)
1151         self.addvar(self.c_seq)
1152
1153     def get_CDR_wchar(self,pn):
1154         self.st.out(self.template_get_CDR_wchar, varname=pn)
1155         self.addvar(self.c_s_octet1)
1156         self.addvar(self.c_seq)
1157
1158     def get_CDR_TypeCode(self,pn):
1159         self.st.out(self.template_get_CDR_TypeCode, varname=pn)
1160         self.addvar(self.c_u_octet4)
1161
1162     def get_CDR_objref(self,type,pn):
1163         self.st.out(self.template_get_CDR_object)
1164
1165     def get_CDR_sequence_len(self,pn):
1166         self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1167
1168
1169     def get_CDR_union(self,type,pn):
1170         if self.DEBUG:
1171             print "XXX Union type =" , type, " pn = ",pn
1172             print "XXX Union type.decl()" , type.decl()
1173             print "XXX Union Scoped Name" , type.scopedName()
1174
1175        #  If I am a typedef union {..}; node then find the union node
1176
1177         if isinstance(type.decl(), idlast.Declarator):
1178             ntype = type.decl().alias().aliasType().decl()
1179         else:
1180             ntype = type.decl()         # I am a union node
1181
1182         if self.DEBUG:
1183             print "XXX Union ntype =" , ntype
1184
1185         sname = self.namespace(ntype, "_")
1186         self.st.out(self.template_union_start, name=sname )
1187
1188         # Output a call to the union helper function so I can handle recursive union also.
1189
1190         self.st.out(self.template_decode_union,name=sname)
1191
1192         self.st.out(self.template_union_end, name=sname )
1193
1194     #
1195     # getCDR_hf()
1196     #
1197     # This takes a node, and tries to output the appropriate item for the
1198     # hf array. 
1199     #
1200
1201     def getCDR_hf(self,type,desc,filter,hf_name="fred"):
1202
1203         pt = type.unalias().kind()      # param CDR type
1204         pn = hf_name                       # param name
1205
1206         if self.DEBUG:
1207             print "XXX getCDR_hf: kind = " , pt
1208             print "XXX getCDR_hf: name = " , pn
1209
1210         if pt == idltype.tk_ulong:
1211             self.get_CDR_ulong_hf(pn, desc, filter, self.dissname)
1212         elif pt == idltype.tk_longlong:
1213             self.get_CDR_longlong_hf(pn, desc, filter, self.dissname)
1214         elif pt == idltype.tk_ulonglong:
1215             self.get_CDR_ulonglong_hf(pn, desc, filter, self.dissname)
1216         elif pt == idltype.tk_void:
1217             pt = pt   # no hf_ variables needed
1218         elif pt ==  idltype.tk_short:
1219             self.get_CDR_short_hf(pn, desc, filter, self.dissname)
1220         elif pt ==  idltype.tk_long:
1221             self.get_CDR_long_hf(pn, desc, filter, self.dissname)
1222         elif pt ==  idltype.tk_ushort:
1223             self.get_CDR_ushort_hf(pn, desc, filter, self.dissname)
1224         elif pt ==  idltype.tk_float:
1225             self.get_CDR_float_hf(pn, desc, filter, self.dissname)
1226         elif pt ==  idltype.tk_double:
1227             self.get_CDR_double_hf(pn, desc, filter, self.dissname)
1228         elif pt == idltype.tk_fixed:
1229             pt = pt   # no hf_ variables needed
1230         elif pt ==  idltype.tk_boolean:
1231             self.get_CDR_boolean_hf(pn, desc, filter, self.dissname)
1232         elif pt ==  idltype.tk_char:
1233             self.get_CDR_char_hf(pn, desc, filter, self.dissname)
1234         elif pt ==  idltype.tk_octet:
1235             self.get_CDR_octet_hf(pn, desc, filter, self.dissname)
1236         elif pt ==  idltype.tk_any:
1237             pt = pt   # no hf_ variables needed
1238         elif pt ==  idltype.tk_string:
1239             self.get_CDR_string_hf(pn, desc, filter, self.dissname)
1240         elif pt ==  idltype.tk_wstring:
1241             self.get_CDR_wstring_hf(pn, desc, filter, self.dissname)
1242         elif pt ==  idltype.tk_wchar:
1243             self.get_CDR_wchar_hf(pn, desc, filter, self.dissname)
1244         elif pt ==  idltype.tk_enum:
1245             self.get_CDR_enum_hf(pn, type, desc, filter, self.dissname)
1246         elif pt ==  idltype.tk_struct:
1247             pt = pt   # no hf_ variables needed (should be already contained in struct members)
1248         elif pt ==  idltype.tk_TypeCode: # will I ever get here ?
1249             self.get_CDR_TypeCode_hf(pn, desc, filter, self.dissname)
1250         elif pt == idltype.tk_sequence:
1251             if type.unalias().seqType().kind() == idltype.tk_octet:
1252                 self.get_CDR_sequence_octet_hf(type, pn, desc, filter, self.dissname)
1253             else:
1254                 self.get_CDR_sequence_hf(type, pn, desc, filter, self.dissname)
1255         elif pt == idltype.tk_objref:
1256             pt = pt   # no object specific hf_ variables used, use generic ones from giop dissector
1257         elif pt == idltype.tk_array:
1258             pt = pt   # Supported elsewhere
1259         elif pt == idltype.tk_union:
1260             pt = pt   # no hf_ variables needed (should be already contained in union members)
1261         elif pt == idltype.tk_alias:
1262             if self.DEBUG:
1263                 print "XXXXX Alias type hf XXXXX " , type
1264             self.get_CDR_alias_hf(type,pn)
1265         else:
1266             self.genWARNING("Unknown typecode = " + '%i ' % pt) # put comment in source code
1267
1268     #
1269     # get_CDR_XXX_hf methods are here ..
1270     #
1271     #
1272
1273
1274     def get_CDR_ulong_hf(self,pn,desc,filter,diss):
1275         self.st.out(self.template_get_CDR_ulong_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1276
1277     def get_CDR_short_hf(self,pn,desc,filter,diss):
1278         self.st.out(self.template_get_CDR_short_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1279
1280     def get_CDR_long_hf(self,pn,desc,filter,diss):
1281         self.st.out(self.template_get_CDR_long_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1282
1283     def get_CDR_ushort_hf(self,pn,desc,filter,diss):
1284         self.st.out(self.template_get_CDR_ushort_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1285
1286     def get_CDR_float_hf(self,pn,desc,filter,diss):
1287         self.st.out(self.template_get_CDR_float_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1288
1289     def get_CDR_double_hf(self,pn,desc,filter,diss):
1290         self.st.out(self.template_get_CDR_double_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1291
1292     def get_CDR_longlong_hf(self,pn,desc,filter,diss):
1293         self.st.out(self.template_get_CDR_longlong_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1294
1295     def get_CDR_ulonglong_hf(self,pn,desc,filter,diss):
1296         self.st.out(self.template_get_CDR_ulonglong_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1297
1298     def get_CDR_boolean_hf(self,pn,desc,filter,diss):
1299         self.st.out(self.template_get_CDR_boolean_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1300
1301     def get_CDR_char_hf(self,pn,desc,filter,diss):
1302         self.st.out(self.template_get_CDR_char_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1303
1304     def get_CDR_octet_hf(self,pn,desc,filter,diss):
1305         self.st.out(self.template_get_CDR_octet_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1306
1307     def get_CDR_enum_hf(self,pn,type,desc,filter,diss):
1308         sname = self.namespace(type.unalias(), "_")
1309         self.st.out(self.template_get_CDR_enum_symbolic_hf, valstringarray=sname,hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1310
1311     def get_CDR_string_hf(self,pn,desc,filter,diss):
1312         self.st.out(self.template_get_CDR_string_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1313
1314     def get_CDR_wstring_hf(self,pn,desc,filter,diss):
1315         self.st.out(self.template_get_CDR_wstring_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1316 #        self.addvar(self.c_u_octet4)
1317 #        self.addvar(self.c_seq)
1318
1319     def get_CDR_wchar_hf(self,pn,desc,filter,diss):
1320         self.st.out(self.template_get_CDR_wchar_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1321 #        self.addvar(self.c_s_octet1)
1322 #        self.addvar(self.c_seq)
1323
1324     def get_CDR_TypeCode_hf(self,pn,desc,filter,diss):
1325         self.st.out(self.template_get_CDR_TypeCode_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1326
1327     def get_CDR_sequence_octet_hf(self,type,pn,desc,filter,diss):
1328         self.st.out(self.template_get_CDR_sequence_octet_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1329
1330     def get_CDR_sequence_hf(self,type,pn,desc,filter,diss):
1331         self.st.out(self.template_get_CDR_sequence_hf, hfname=pn, dissector_name=diss, descname=desc, filtername=filter)
1332
1333     def get_CDR_alias_hf(self,type,pn):
1334         if self.DEBUG:
1335             print "XXX get_CDR_alias_hf, type = " ,type , " pn = " , pn
1336             print "XXX get_CDR_alias_hf, type.decl() = " ,type.decl()
1337             print "XXX get_CDR_alias_hf, type.decl().alias() = " ,type.decl().alias()
1338
1339         decl = type.decl()              # get declarator object
1340
1341         if (decl.sizes()):        # a typedef array
1342             #indices = self.get_indices_from_sizes(decl.sizes())
1343             #string_indices = '%i ' % indices # convert int to string
1344             #self.st.out(self.template_get_CDR_array_comment, aname=pn, asize=string_indices)
1345
1346             #self.st.out(self.template_get_CDR_array_start, aname=pn, aval=string_indices)
1347             #self.addvar(self.c_i + pn + ";")
1348             #self.st.inc_indent()
1349             self.getCDR_hf(type.decl().alias().aliasType(),  pn )
1350
1351             #self.st.dec_indent()
1352             #self.st.out(self.template_get_CDR_array_end)
1353
1354
1355         else:                           # a simple typdef
1356             if self.DEBUG:
1357                 print "XXX get_CDR_alias_hf, type = " ,type , " pn = " , pn
1358                 print "XXX get_CDR_alias_hf, type.decl() = " ,type.decl()
1359
1360             self.getCDR_hf(type, decl.identifier() )
1361
1362
1363     #
1364     # Code to generate Union Helper functions
1365     #
1366     # in: un - a union node
1367     #
1368     #
1369
1370
1371     def genUnionHelper(self,un):
1372         if self.DEBUG:
1373             print "XXX genUnionHelper called"
1374             print "XXX Union type =" , un
1375             print "XXX Union type.switchType()" , un.switchType()
1376             print "XXX Union Scoped Name" , un.scopedName()
1377
1378         sname = self.namespace(un, "_")
1379         self.curr_sname = sname         # update current opnode/exnode/stnode/unnode scoped name
1380         if not self.fn_hash_built:
1381             self.fn_hash[sname] = []        # init empty list as val for this sname key
1382                                             # but only if the fn_hash is not already built
1383
1384         self.st.out(self.template_union_helper_function_start, sname=sname, unname=un.repoId())
1385         self.st.inc_indent()
1386
1387         if (len(self.fn_hash[sname]) > 0):
1388             self.st.out(self.template_helper_function_vars_start)
1389             self.dumpCvars(sname)
1390             self.st.out(self.template_helper_function_vars_end )
1391
1392         st = un.switchType().unalias() # may be typedef switch type, so find real type
1393
1394         self.st.out(self.template_comment_union_code_start, uname=un.repoId() )
1395
1396         self.getCDR(st, sname + "_" + un.identifier());
1397
1398         # Depending on what kind of discriminant I come accross (enum,integer,char,
1399         # short, boolean), make sure I cast the return value of the get_XXX accessor
1400         # to an appropriate value. Omniidl idlast.CaseLabel.value() accessor will
1401         # return an integer, or an Enumerator object that is then converted to its
1402         # integer equivalent.
1403         #
1404         #
1405         # NOTE - May be able to skip some of this stuff, but leave it in for now -- FS
1406         #
1407
1408         if (st.kind() == idltype.tk_enum):
1409             std = st.decl()
1410             self.st.out(self.template_comment_union_code_discriminant, uname=std.repoId() )
1411             self.st.out(self.template_union_code_save_discriminant_enum, discname=un.identifier() )
1412             self.addvar(self.c_s_disc + un.identifier() + ";")
1413
1414         elif (st.kind() == idltype.tk_long):
1415             self.st.out(self.template_union_code_save_discriminant_long, discname=un.identifier() )
1416             self.addvar(self.c_s_disc + un.identifier() + ";")
1417
1418         elif (st.kind() == idltype.tk_ulong):
1419             self.st.out(self.template_union_code_save_discriminant_ulong, discname=un.identifier() )
1420             self.addvar(self.c_s_disc + un.identifier() + ";")
1421
1422         elif (st.kind() == idltype.tk_short):
1423             self.st.out(self.template_union_code_save_discriminant_short, discname=un.identifier() )
1424             self.addvar(self.c_s_disc + un.identifier() + ";")
1425
1426         elif (st.kind() == idltype.tk_ushort):
1427             self.st.out(self.template_union_code_save_discriminant_ushort, discname=un.identifier() )
1428             self.addvar(self.c_s_disc + un.identifier() + ";")
1429
1430         elif (st.kind() == idltype.tk_boolean):
1431             self.st.out(self.template_union_code_save_discriminant_boolean, discname=un.identifier()  )
1432             self.addvar(self.c_s_disc + un.identifier() + ";")
1433
1434         elif (st.kind() == idltype.tk_char):
1435             self.st.out(self.template_union_code_save_discriminant_char, discname=un.identifier() )
1436             self.addvar(self.c_s_disc + un.identifier() + ";")
1437
1438         else:
1439             print "XXX Unknown st.kind() = ", st.kind()
1440
1441         #
1442         # Loop over all cases in this union
1443         #
1444
1445         for uc in un.cases():           # for all UnionCase objects in this union
1446             for cl in uc.labels():      # for all Caselabel objects in this UnionCase
1447
1448                 # get integer value, even if discriminant is
1449                 # an Enumerator node
1450
1451                 if isinstance(cl.value(),idlast.Enumerator):
1452                     if self.DEBUG:
1453                         print "XXX clv.identifier()", cl.value().identifier()
1454                         print "XXX clv.repoId()", cl.value().repoId()
1455                         print "XXX clv.scopedName()", cl.value().scopedName()
1456
1457                     # find index of enumerator in enum declaration
1458                     # eg: RED is index 0 in enum Colors { RED, BLUE, GREEN }
1459
1460                     clv = self.valFromEnum(std,cl.value())
1461
1462                 else:
1463                     clv = cl.value()
1464
1465                 #print "XXX clv = ",clv
1466
1467                 #
1468                 # if char, dont convert to int, but put inside single quotes so that it is understood by C.
1469                 # eg: if (disc == 'b')..
1470                 #
1471                 # TODO : handle \xxx chars generically from a function or table lookup rather than
1472                 #        a whole bunch of "if" statements. -- FS
1473
1474
1475                 if (st.kind() == idltype.tk_char):
1476                     if (clv == '\n'):          # newline
1477                         string_clv = "'\\n'"
1478                     elif (clv == '\t'):        # tab
1479                         string_clv = "'\\t'"
1480                     else:
1481                         string_clv = "'" + clv + "'"
1482                 else:
1483                     string_clv = '%i ' % clv
1484
1485                 #
1486                 # If default case, then skp comparison with discriminator
1487                 #
1488
1489                 if not cl.default():
1490                     self.st.out(self.template_comment_union_code_label_compare_start, discname=un.identifier(),labelval=string_clv )
1491                     self.st.inc_indent()
1492                 else:
1493                     self.st.out(self.template_comment_union_code_label_default_start  )
1494
1495
1496                 self.getCDR(uc.caseType(),sname + "_" + uc.declarator().identifier())
1497
1498                 if not cl.default():
1499                     self.st.dec_indent()
1500                     self.st.out(self.template_comment_union_code_label_compare_end )
1501                 else:
1502                     self.st.out(self.template_comment_union_code_label_default_end  )
1503
1504         self.st.dec_indent()
1505         self.st.out(self.template_union_helper_function_end)
1506
1507
1508
1509     #
1510     # Currently, get_CDR_alias is geared to finding typdef
1511     #
1512
1513     def get_CDR_alias(self,type,pn):
1514         if self.DEBUG:
1515             print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1516             print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1517             print "XXX get_CDR_alias, type.decl().alias() = " ,type.decl().alias()
1518
1519         decl = type.decl()              # get declarator object
1520
1521         if (decl.sizes()):        # a typedef array
1522             indices = self.get_indices_from_sizes(decl.sizes())
1523             string_indices = '%i ' % indices # convert int to string
1524             self.st.out(self.template_get_CDR_array_comment, aname=pn, asize=string_indices)
1525
1526             self.st.out(self.template_get_CDR_array_start, aname=pn, aval=string_indices)
1527             self.addvar(self.c_i + pn + ";")
1528             self.st.inc_indent()
1529             self.getCDR(type.decl().alias().aliasType(),  pn )
1530
1531             self.st.dec_indent()
1532             self.st.out(self.template_get_CDR_array_end)
1533
1534
1535         else:                           # a simple typdef
1536             if self.DEBUG:
1537                 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1538                 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1539
1540             self.getCDR(type, pn )
1541
1542
1543
1544
1545
1546
1547     #
1548     # Handle structs, including recursive
1549     #
1550
1551     def get_CDR_struct(self,type,pn):
1552
1553         #  If I am a typedef struct {..}; node then find the struct node
1554
1555         if isinstance(type.decl(), idlast.Declarator):
1556             ntype = type.decl().alias().aliasType().decl()
1557         else:
1558             ntype = type.decl()         # I am a struct node
1559
1560         sname = self.namespace(ntype, "_")
1561         self.st.out(self.template_structure_start, name=sname )
1562
1563         # Output a call to the struct helper function so I can handle recursive structs also.
1564
1565         self.st.out(self.template_decode_struct,name=sname)
1566
1567         self.st.out(self.template_structure_end, name=sname )
1568
1569     #
1570     # genStructhelper()
1571     #
1572     # Generate private helper functions to decode a struct
1573     #
1574     # in: stnode ( a struct node)
1575     #
1576
1577     def genStructHelper(self,st):
1578         if self.DEBUG:
1579             print "XXX genStructHelper"
1580
1581         sname = self.namespace(st, "_")
1582         self.curr_sname = sname         # update current opnode/exnode/stnode scoped name
1583         if not self.fn_hash_built:
1584             self.fn_hash[sname] = []        # init empty list as val for this sname key
1585                                             # but only if the fn_hash is not already built
1586
1587         self.st.out(self.template_struct_helper_function_start, sname=sname, stname=st.repoId())
1588         self.st.inc_indent()
1589
1590         if (len(self.fn_hash[sname]) > 0):
1591             self.st.out(self.template_helper_function_vars_start)
1592             self.dumpCvars(sname)
1593             self.st.out(self.template_helper_function_vars_end )
1594
1595         for m in st.members():
1596             for decl in m.declarators():
1597                 if decl.sizes():        # an array
1598                     indices = self.get_indices_from_sizes(decl.sizes())
1599                     string_indices = '%i ' % indices # convert int to string
1600                     self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
1601                     self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
1602                     self.addvar(self.c_i + decl.identifier() + ";")
1603
1604                     self.st.inc_indent()
1605                     self.getCDR(m.memberType(), sname + "_" + decl.identifier() )
1606                     self.st.dec_indent()
1607                     self.st.out(self.template_get_CDR_array_end)
1608
1609
1610                 else:
1611                     self.getCDR(m.memberType(), sname + "_" + decl.identifier() )
1612
1613         self.st.dec_indent()
1614         self.st.out(self.template_struct_helper_function_end)
1615
1616
1617
1618
1619
1620     #
1621     # Generate code to access a sequence of a type
1622     #
1623
1624
1625     def get_CDR_sequence(self,type, pn):
1626         self.st.out(self.template_get_CDR_sequence_length, seqname=pn )
1627         self.st.out(self.template_get_CDR_sequence_loop_start, seqname=pn )
1628         self.addvar(self.c_i_lim + pn + ";" )
1629         self.addvar(self.c_i + pn + ";")
1630
1631         self.st.inc_indent()
1632         self.getCDR(type.unalias().seqType(), pn ) # and start all over with the type
1633         self.st.dec_indent()
1634
1635         self.st.out(self.template_get_CDR_sequence_loop_end)
1636
1637
1638     #
1639     # Generate code to access a sequence of octet
1640     #
1641
1642     def get_CDR_sequence_octet(self,type, pn):
1643         self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1644         self.st.out(self.template_get_CDR_sequence_octet, seqname=pn)
1645         self.addvar(self.c_i_lim + pn + ";")
1646         self.addvar("gchar * binary_seq_" + pn + ";")
1647         self.addvar("gchar * text_seq_" + pn + ";")
1648
1649
1650    #
1651    # namespace()
1652    #
1653    # in - op node
1654    #
1655    # out - scoped operation name, using sep character instead of "::"
1656    #
1657    # eg: Penguin::Echo::echoWString => Penguin_Echo_echoWString if sep = "_"
1658    #
1659    #
1660
1661     def namespace(self,node,sep):
1662         sname = string.replace(idlutil.ccolonName(node.scopedName()), '::', sep)
1663         #print "XXX namespace: sname = " + sname
1664         return sname
1665
1666
1667     #
1668     # generate code for plugin initialisation
1669     #
1670
1671     def gen_plugin_register(self):
1672         self.st.out(self.template_plugin_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1673
1674     #
1675     # generate  register_giop_user_module code, and register only
1676     # unique interfaces that contain operations. Also output
1677     # a heuristic register in case we want to use that.
1678     #
1679     # TODO - make this a command line option
1680     #
1681     # -e explicit
1682     # -h heuristic
1683     #
1684
1685
1686
1687     def gen_proto_reg_handoff(self, oplist):
1688
1689         self.st.out(self.template_proto_reg_handoff_start, dissector_name=self.dissname)
1690         self.st.inc_indent()
1691
1692         for iname in self.get_intlist(oplist):
1693             self.st.out(self.template_proto_reg_handoff_body, dissector_name=self.dissname, protocol_name=self.protoname, interface=iname )
1694
1695         self.st.out(self.template_proto_reg_handoff_heuristic, dissector_name=self.dissname,  protocol_name=self.protoname)
1696         self.st.dec_indent()
1697
1698         self.st.out(self.template_proto_reg_handoff_end)
1699
1700     #
1701     # generate hf_ array element for operation, attribute, enums, struct and union lists
1702     #
1703
1704     def genOp_hf(self,op):
1705         sname = self.namespace(op, "_")
1706         opname = sname[string.find(sname, "_")+1:]
1707         opname = opname[:string.find(opname, "_")]
1708         rt = op.returnType()
1709
1710         if (rt.kind() != idltype.tk_void):
1711             if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
1712                 self.getCDR_hf(rt, rt.name(),\
1713                     opname + "." + op.identifier() + ".return", sname + "_return")
1714             else:
1715                 self.getCDR_hf(rt, "Return value",\
1716                     opname + "." + op.identifier() + ".return", sname + "_return")
1717
1718         for p in op.parameters():
1719             self.getCDR_hf(p.paramType(), p.identifier(),\
1720                 opname + "." + op.identifier() + "." + p.identifier(), sname + "_" + p.identifier())
1721
1722     def genAt_hf(self,at):
1723         for decl in at.declarators():
1724             sname = self.namespace(decl, "_")
1725             atname = sname[string.find(sname, "_")+1:]
1726             atname = atname[:string.find(atname, "_")]
1727
1728             self.getCDR_hf(at.attrType(), decl.identifier(),\
1729                      atname + "." + decl.identifier() + ".get", "get" + "_" + sname + "_" + decl.identifier())
1730             if not at.readonly():
1731                 self.getCDR_hf(at.attrType(), decl.identifier(),\
1732                     atname + "." + decl.identifier() + ".set", "set" + "_" + sname + "_" + decl.identifier())
1733
1734     def genSt_hf(self,st):
1735         sname = self.namespace(st, "_")
1736         stname = sname[string.find(sname, "_")+1:]
1737         stname = stname[:string.find(stname, "_")]
1738         for m in st.members():
1739             for decl in m.declarators():
1740                 self.getCDR_hf(m.memberType(), st.identifier() + "_" + decl.identifier(),\
1741                         st.identifier() + "." + decl.identifier(), sname + "_" + decl.identifier())
1742
1743     def genEx_hf(self,ex):
1744         sname = self.namespace(ex, "_")
1745         exname = sname[string.find(sname, "_")+1:]
1746         exname = exname[:string.find(exname, "_")]
1747         for m in ex.members():
1748             for decl in m.declarators():
1749                 self.getCDR_hf(m.memberType(), ex.identifier() + "_" + decl.identifier(),\
1750                         exname + "." + ex.identifier() + "_" + decl.identifier(), sname + "_" + decl.identifier())
1751
1752     def genUnion_hf(self,un):
1753         sname = self.namespace(un, "_")
1754         unname = sname[:string.rfind(sname, "_")]
1755         unname = string.replace(unname, "_", ".")
1756
1757         self.getCDR_hf(un.switchType().unalias(), un.identifier(),\
1758                 unname + "." + un.identifier(), sname + "_" + un.identifier())
1759
1760         for uc in un.cases():           # for all UnionCase objects in this union
1761             for cl in uc.labels():      # for all Caselabel objects in this UnionCase
1762                 self.getCDR_hf(uc.caseType(), un.identifier() + "_" + uc.declarator().identifier(),\
1763                       unname + "." + un.identifier() + "." + uc.declarator().identifier(),\
1764                       sname + "_" + uc.declarator().identifier())
1765
1766     #
1767     # generate  proto_register_<protoname> code,
1768     #
1769     # in - oplist[], atlist[], stline[], unlist[]
1770     #
1771
1772
1773     def gen_proto_register(self, oplist, atlist, stlist, unlist):
1774         self.st.out(self.template_proto_register_start, dissector_name=self.dissname)
1775         
1776         #operation specific filters
1777         self.st.out(self.template_proto_register_op_filter_comment)
1778         for op in oplist:
1779             self.genOp_hf(op)
1780
1781         #attribute filters
1782         self.st.out(self.template_proto_register_at_filter_comment)
1783         for at in atlist:
1784             self.genAt_hf(at)
1785
1786         #struct filters
1787         self.st.out(self.template_proto_register_st_filter_comment)
1788         for st in stlist:
1789             if (st.members()):          # only if has members
1790                 self.genSt_hf(st)
1791
1792         # exception List filters
1793         exlist = self.get_exceptionList(oplist) # grab list of exception nodes
1794         self.st.out(self.template_proto_register_ex_filter_comment)
1795         for ex in exlist:
1796             if (ex.members()):          # only if has members
1797                 self.genEx_hf(ex)
1798
1799         # Union filters
1800         self.st.out(self.template_proto_register_un_filter_comment)
1801         for un in unlist:
1802             self.genUnion_hf(un)
1803
1804         self.st.out(self.template_proto_register_end, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1805
1806
1807     #
1808     # in - oplist[]
1809     #
1810     # out - a list of unique interface names. This will be used in
1811     # register_giop_user_module(dissect_giop_auto, "TEST IDL", "Penguin/Echo" );   so the operation
1812     # name must be removed from the scope. And we also only want unique interfaces.
1813     #
1814
1815     def get_intlist(self,oplist):
1816         int_hash = {}                   # holds a hash of unique interfaces
1817         for op in oplist:
1818             sc = op.scopedName()        # eg: penguin,tux,bite
1819             sc1 = sc[:-1]               # drop last entry
1820             sn = idlutil.slashName(sc1)         # penguin/tux
1821             if not int_hash.has_key(sn):
1822                 int_hash[sn] = 0;       # dummy val, but at least key is unique
1823         ret = int_hash.keys()
1824         ret.sort()
1825         return ret
1826
1827
1828
1829     #
1830     # in - oplist[]
1831     #
1832     # out - a list of exception nodes (unique). This will be used in
1833     # to generate dissect_exception_XXX functions.
1834     #
1835
1836
1837
1838     def get_exceptionList(self,oplist):
1839         ex_hash = {}                   # holds a hash of unique exceptions.
1840         for op in oplist:
1841             for ex in op.raises():
1842                 if not ex_hash.has_key(ex):
1843                     ex_hash[ex] = 0; # dummy val, but at least key is unique
1844                     if self.DEBUG:
1845                         print "XXX Exception = " + ex.identifier()
1846         ret = ex_hash.keys()
1847         ret.sort()
1848         return ret
1849
1850
1851
1852     #
1853     # Simple function to take a list of array sizes and find the
1854     # total number of elements
1855     #
1856     #
1857     # eg: temp[4][3] = 12 elements
1858     #
1859
1860     def get_indices_from_sizes(self,sizelist):
1861         val = 1;
1862         for i in sizelist:
1863             val = val * i
1864
1865         return val
1866
1867     #
1868     # Determine how many octets contain requested number
1869     # of digits for an "fixed" IDL type  "on the wire"
1870     #
1871
1872     def dig_to_len(self,dignum):
1873         return (dignum/2) + 1
1874
1875
1876
1877     #
1878     # Output some TODO comment
1879     #
1880
1881
1882     def genTODO(self,message):
1883         self.st.out(self.template_debug_TODO, message=message)
1884
1885     #
1886     # Output some WARNING comment
1887     #
1888
1889
1890     def genWARNING(self,message):
1891         self.st.out(self.template_debug_WARNING, message=message)
1892
1893     #
1894     # Templates for C code
1895     #
1896
1897     template_helper_function_comment = """\
1898 /*
1899  * @repoid@
1900  */"""
1901     template_helper_function_vars_start = """\
1902 /* Operation specific Variable declarations Begin */"""
1903
1904     template_helper_function_vars_end = """\
1905 /* Operation specific Variable declarations End */
1906
1907 (void)item; /* Avoid coverity param_set_but_unused parse warning */
1908 """
1909
1910     template_helper_function_start = """\
1911 static void
1912 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_)
1913 {"""
1914
1915     template_helper_function_end = """\
1916 }
1917 """
1918     #
1919     # proto_reg_handoff() templates
1920     #
1921
1922     template_proto_reg_handoff_start = """\
1923 /* register me as handler for these interfaces */
1924 void proto_reg_handoff_giop_@dissector_name@(void)
1925 {"""
1926
1927     template_proto_reg_handoff_body = """
1928 /* Register for Explicit Dissection */
1929 register_giop_user_module(dissect_@dissector_name@, \"@protocol_name@\", \"@interface@\", proto_@dissector_name@ );     /* explicit dissector */"""
1930
1931     template_proto_reg_handoff_heuristic = """
1932 /* Register for Heuristic Dissection */
1933 register_giop_user(dissect_@dissector_name@, \"@protocol_name@\" ,proto_@dissector_name@);     /* heuristic dissector */"""
1934
1935     template_proto_reg_handoff_end = """\
1936 }
1937 """
1938
1939     #
1940     # Initialize the protocol
1941     #
1942
1943     template_protocol = """
1944 /* Initialise the protocol and subtree pointers */
1945 static int proto_@dissector_name@ = -1;
1946 static gint ett_@dissector_name@ = -1;
1947 """
1948     #
1949     # Initialize the boundary Alignment
1950     #
1951
1952     template_init_boundary = """
1953 /* Initialise the initial Alignment */
1954 static guint32  boundary = GIOP_HEADER_SIZE;  /* initial value */"""
1955
1956     #
1957     # plugin_register and plugin_reg_handoff templates
1958     #
1959
1960     template_plugin_register = """
1961 #if 0
1962
1963 WS_DLL_PUBLIC_NOEXTERN void
1964 plugin_register(void)
1965 {
1966    if (proto_@dissector_name@ == -1) {
1967      proto_register_giop_@dissector_name@();
1968    }
1969 }
1970
1971 WS_DLL_PUBLIC_NOEXTERN void
1972 plugin_reg_handoff(void){
1973    proto_register_handoff_giop_@dissector_name@();
1974 }
1975 #endif
1976 """
1977     #
1978     # proto_register_<dissector name>(void) templates
1979     #
1980
1981     template_proto_register_start = """
1982 /* Register the protocol with Wireshark */
1983 void proto_register_giop_@dissector_name@(void)
1984 {
1985    /* setup list of header fields */
1986    static hf_register_info hf[] = {
1987         /* field that indicates the currently ongoing request/reply exchange */
1988                 {&hf_operationrequest, {"Request_Operation","giop-@dissector_name@.Request_Operation",FT_STRING,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
1989
1990     template_proto_register_end = """
1991    };
1992
1993    /* setup protocol subtree array */
1994
1995    static gint *ett[] = {
1996       &ett_@dissector_name@,
1997    };
1998
1999    /* Register the protocol name and description */
2000    proto_@dissector_name@ = proto_register_protocol(\"@description@\" , \"@protocol_name@\", \"giop-@dissector_name@\" );
2001    proto_register_field_array(proto_@dissector_name@, hf, array_length(hf));
2002    proto_register_subtree_array(ett,array_length(ett));
2003 }
2004 """
2005
2006     template_proto_register_op_filter_comment = """\
2007         /* Operation filters */"""
2008
2009     template_proto_register_at_filter_comment = """\
2010         /* Attribute filters */"""
2011
2012     template_proto_register_st_filter_comment = """\
2013         /* Struct filters */"""
2014
2015     template_proto_register_ex_filter_comment = """\
2016         /* User exception filters */"""
2017
2018     template_proto_register_un_filter_comment = """\
2019         /* Union filters */"""
2020
2021
2022     #
2023     # template for delegation code
2024     #
2025
2026     template_op_delegate_code = """\
2027 if (strcmp(operation, "@opname@") == 0
2028     && (!idlname || strcmp(idlname, \"@interface@\") == 0)) {
2029    item = process_RequestOperation(tvb, pinfo, ptree, header, operation);  /* fill-up Request_Operation field & info column */
2030    tree = start_dissecting(tvb, pinfo, ptree, offset);
2031    decode_@sname@(tvb, pinfo, tree, item, offset, header, operation, stream_is_big_endian);
2032    return TRUE;
2033 }
2034 """
2035     #
2036     # Templates for the helper functions
2037     #
2038     #
2039     #
2040
2041     template_helper_switch_msgtype_start = """\
2042 switch(header->message_type) {"""
2043
2044     template_helper_switch_msgtype_default_start = """\
2045 default:
2046     /* Unknown GIOP Message */
2047     expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR, "Unknown GIOP message %d", header->message_type);"""
2048     
2049     template_helper_switch_msgtype_default_end = """\
2050 break;"""
2051
2052     template_helper_switch_msgtype_end = """\
2053 } /* switch(header->message_type) */"""
2054
2055     template_helper_switch_msgtype_request_start = """\
2056 case Request:"""
2057
2058     template_helper_switch_msgtype_request_end = """\
2059 break;"""
2060
2061     template_helper_switch_msgtype_reply_start = """\
2062 case Reply:"""
2063
2064     template_helper_switch_msgtype_reply_no_exception_start = """\
2065 case NO_EXCEPTION:"""
2066
2067     template_helper_switch_msgtype_reply_no_exception_end = """\
2068 break;"""
2069
2070     template_helper_switch_msgtype_reply_user_exception_start = """\
2071 case USER_EXCEPTION:"""
2072
2073     template_helper_switch_msgtype_reply_user_exception_end = """\
2074 break;"""
2075
2076     template_helper_switch_msgtype_reply_default_start = """\
2077 default:
2078     /* Unknown Exception */
2079     expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR, "Unknown exception %d", header->rep_status);"""
2080     
2081     template_helper_switch_msgtype_reply_default_end = """\
2082     break;"""
2083     
2084     template_helper_switch_msgtype_reply_end = """\
2085 break;"""
2086
2087     template_helper_switch_msgtype_default_start = """\
2088 default:
2089     /* Unknown GIOP Message */
2090     expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR, "Unknown GIOP message %d", header->message_type);"""
2091     
2092     template_helper_switch_msgtype_default_end = """\
2093     break;"""
2094     
2095     template_helper_switch_rep_status_start = """\
2096 switch(header->rep_status) {"""
2097
2098     template_helper_switch_rep_status_default_start = """\
2099 default:
2100     /* Unknown Reply Status */
2101     expert_add_info_format(pinfo, item, PI_MALFORMED, PI_ERROR, "Unknown reply status %d", header->rep_status);"""
2102     
2103     template_helper_switch_rep_status_default_end = """\
2104     break;"""
2105     
2106     template_helper_switch_rep_status_end = """\
2107 }   /* switch(header->rep_status) */
2108
2109 break;"""
2110
2111     #
2112     # Templates for get_CDR_xxx accessors
2113     #
2114
2115     template_get_CDR_ulong = """\
2116 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-4, 4, get_CDR_ulong(tvb,offset,stream_is_big_endian, boundary));
2117 """
2118     template_get_CDR_short = """\
2119 proto_tree_add_int(tree, hf_@hfname@, tvb, *offset-2, 2, get_CDR_short(tvb,offset,stream_is_big_endian, boundary));
2120 """
2121     template_get_CDR_void = """\
2122 /* Function returns void */
2123 """
2124     template_get_CDR_long = """\
2125 proto_tree_add_int(tree, hf_@hfname@, tvb, *offset-4, 4, get_CDR_long(tvb,offset,stream_is_big_endian, boundary));
2126 """
2127     template_get_CDR_ushort = """\
2128 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-2, 2, get_CDR_ushort(tvb,offset,stream_is_big_endian, boundary));
2129 """
2130     template_get_CDR_float = """\
2131 proto_tree_add_float(tree, hf_@hfname@, tvb, *offset-4, 4, get_CDR_float(tvb,offset,stream_is_big_endian, boundary));
2132 """
2133     template_get_CDR_double = """\
2134 proto_tree_add_double(tree, hf_@hfname@, tvb, *offset-8, 8, get_CDR_double(tvb,offset,stream_is_big_endian, boundary));
2135 """
2136     template_get_CDR_longlong = """\
2137 proto_tree_add_int64(tree, hf_@hfname@, tvb, *offset-8, 8, get_CDR_long_long(tvb,offset,stream_is_big_endian, boundary));
2138 """
2139     template_get_CDR_ulonglong = """\
2140 proto_tree_add_uint64(tree, hf_@hfname@, tvb, *offset-8, 8, get_CDR_ulong_long(tvb,offset,stream_is_big_endian, boundary));
2141 """
2142     template_get_CDR_boolean = """\
2143 proto_tree_add_boolean(tree, hf_@hfname@, tvb, *offset-1, 1, get_CDR_boolean(tvb,offset));
2144 """
2145     template_get_CDR_char = """\
2146 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-1, 1, get_CDR_char(tvb,offset));
2147 """
2148     template_get_CDR_octet = """\
2149 proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-1, 1, get_CDR_octet(tvb,offset));
2150 """
2151     template_get_CDR_any = """\
2152 get_CDR_any(tvb, pinfo, tree, item, offset, stream_is_big_endian, boundary, header);
2153 """
2154     template_get_CDR_fixed = """\
2155 get_CDR_fixed(tvb, pinfo, item, &seq, offset, @digits@, @scale@);
2156 proto_tree_add_text(tree,tvb,*offset-@length@, @length@, "@varname@ < @digits@, @scale@> = %s",seq);
2157 """
2158     template_get_CDR_enum_symbolic = """\
2159 u_octet4 = get_CDR_enum(tvb,offset,stream_is_big_endian, boundary);
2160 /* coverity[returned_pointer] */
2161 item = proto_tree_add_uint(tree, hf_@hfname@, tvb, *offset-4, 4, u_octet4);
2162 """
2163     template_get_CDR_string = """\
2164 giop_add_CDR_string(tree, tvb, offset, stream_is_big_endian, boundary, hf_@hfname@);
2165 """
2166     template_get_CDR_wstring = """\
2167 u_octet4 = get_CDR_wstring(tvb, &seq, offset, stream_is_big_endian, boundary, header);
2168 proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ (%u) = %s",
2169       u_octet4, (u_octet4 > 0) ? seq : \"\");
2170 """
2171     template_get_CDR_wchar = """\
2172 s_octet1 = get_CDR_wchar(tvb, &seq, offset, header);
2173 if (tree) {
2174     if (s_octet1 > 0)
2175         proto_tree_add_text(tree,tvb,*offset-1-s_octet1,1,"length = %u",s_octet1);
2176
2177     if (s_octet1 < 0)
2178         s_octet1 = -s_octet1;
2179
2180     if (s_octet1 > 0)
2181         proto_tree_add_text(tree,tvb,*offset-s_octet1,s_octet1,"@varname@ = %s",seq);
2182 }
2183 """
2184     template_get_CDR_TypeCode = """\
2185 u_octet4 = get_CDR_typeCode(tvb, pinfo, tree, offset, stream_is_big_endian, boundary, header);
2186 """
2187
2188     template_get_CDR_object = """\
2189 get_CDR_object(tvb, pinfo, tree, offset, stream_is_big_endian, boundary);
2190 """
2191
2192     template_get_CDR_sequence_length = """\
2193 u_octet4_loop_@seqname@ = get_CDR_ulong(tvb, offset, stream_is_big_endian, boundary);
2194 /* coverity[returned_pointer] */
2195 item = proto_tree_add_uint(tree, hf_@seqname@, tvb,*offset-4, 4, u_octet4_loop_@seqname@);
2196 """
2197     template_get_CDR_sequence_loop_start = """\
2198 for (i_@seqname@=0; i_@seqname@ < u_octet4_loop_@seqname@; i_@seqname@++) {
2199 """
2200     template_get_CDR_sequence_loop_end = """\
2201 }
2202 """
2203
2204     template_get_CDR_sequence_octet = """\
2205 if (u_octet4_loop_@seqname@ > 0 && tree) {
2206     get_CDR_octet_seq(tvb, &binary_seq_@seqname@, offset,
2207         u_octet4_loop_@seqname@);
2208     text_seq_@seqname@ = make_printable_string(binary_seq_@seqname@,
2209         u_octet4_loop_@seqname@);
2210     proto_tree_add_text(tree, tvb, *offset - u_octet4_loop_@seqname@,
2211         u_octet4_loop_@seqname@, \"@seqname@: %s\", text_seq_@seqname@);
2212 }
2213 """
2214     template_get_CDR_array_start = """\
2215 for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
2216 """
2217     template_get_CDR_array_end = """\
2218 }
2219 """
2220     template_get_CDR_array_comment = """\
2221 /* Array: @aname@[ @asize@]  */
2222 """
2223     template_structure_start = """\
2224 /*  Begin struct \"@name@\"  */"""
2225
2226     template_structure_end = """\
2227 /*  End struct \"@name@\"  */"""
2228
2229     template_union_start = """\
2230 /*  Begin union \"@name@\"  */"""
2231
2232     template_union_end = """\
2233 /*  End union \"@name@\"  */"""
2234
2235     #
2236     # Templates for get_CDR_xxx_hf accessors
2237     #
2238
2239     template_get_CDR_ulong_hf = """\
2240         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT32,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2241
2242     template_get_CDR_short_hf = """\
2243         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_INT16,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2244
2245     template_get_CDR_long_hf = """\
2246         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_INT32,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2247
2248     template_get_CDR_ushort_hf = """\
2249         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT16,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2250
2251     template_get_CDR_float_hf = """\
2252         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_FLOAT,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2253
2254     template_get_CDR_double_hf = """\
2255         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_DOUBLE,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2256
2257     template_get_CDR_longlong_hf = """\
2258         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_INT64,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2259
2260     template_get_CDR_ulonglong_hf = """\
2261         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT64,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2262
2263     template_get_CDR_boolean_hf = """\
2264         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_BOOLEAN,8,NULL,0x01,NULL,HFILL}},"""
2265
2266     template_get_CDR_char_hf = """\
2267         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT8,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2268
2269     template_get_CDR_octet_hf = """\
2270         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT8,BASE_HEX,NULL,0x0,NULL,HFILL}},"""
2271
2272     template_get_CDR_enum_symbolic_hf = """\
2273         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT32,BASE_DEC,VALS(@valstringarray@),0x0,NULL,HFILL}},"""
2274
2275     template_get_CDR_string_hf = """\
2276         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_STRING,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2277
2278     template_get_CDR_wstring_hf = """\
2279         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_STRING,BASE_NONE,NULL,0x0,NULL,HFILL}},"""
2280
2281     template_get_CDR_wchar_hf = """\
2282         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT16,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2283
2284     template_get_CDR_TypeCode_hf = """\
2285         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT32,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2286
2287     template_get_CDR_sequence_hf = """\
2288         {&hf_@hfname@, {"Seq length of @descname@","giop-@dissector_name@.@filtername@",FT_UINT32,BASE_DEC,NULL,0x0,NULL,HFILL}},"""
2289
2290     template_get_CDR_sequence_octet_hf = """\
2291         {&hf_@hfname@, {"@descname@","giop-@dissector_name@.@filtername@",FT_UINT8,BASE_HEX,NULL,0x0,NULL,HFILL}},"""
2292
2293 #
2294 # Program Header Template
2295 #
2296
2297     template_Header = """\
2298 /* packet-@dissector_name@.c
2299  *
2300  * $Id$
2301  *
2302  * Routines for IDL dissection
2303  *
2304  * Autogenerated from idl2wrs
2305  * Copyright 2001 Frank Singleton <frank.singleton@@ericsson.com>
2306  */
2307
2308 """
2309
2310     template_wireshark_copyright = """\
2311 /*
2312  * Wireshark - Network traffic analyzer
2313  * By Gerald Combs
2314  * Copyright 1999 - 2012 Gerald Combs
2315  */
2316 """
2317
2318
2319
2320 #
2321 # GPL Template
2322 #
2323
2324
2325     template_GPL = """\
2326 /*
2327  * This program is free software; you can redistribute it and/or
2328  * modify it under the terms of the GNU General Public License
2329  * as published by the Free Software Foundation; either version 2
2330  * of the License, or (at your option) any later version.
2331  *
2332  * This program is distributed in the hope that it will be useful,
2333  * but WITHOUT ANY WARRANTY; without even the implied warranty of
2334  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2335  * GNU General Public License for more details.
2336  *
2337  * You should have received a copy of the GNU General Public License
2338  * along with this program; if not, write to the Free Software
2339  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2340  */
2341 """
2342
2343 #
2344 # Includes template
2345 #
2346
2347     template_Includes = """\
2348
2349 #include "config.h"
2350
2351 #include <gmodule.h>
2352
2353 #include <string.h>
2354 #include <glib.h>
2355 #include <epan/packet.h>
2356 #include <epan/proto.h>
2357 #include <epan/dissectors/packet-giop.h>
2358 #include <epan/expert.h>
2359
2360 /* plugins are DLLs */
2361 #define WS_BUILD_DLL
2362 #include "ws_symbol_export.h"
2363
2364 #ifdef _MSC_VER
2365 /* disable warning: "unreference local variable" */
2366 #pragma warning(disable:4101)
2367 #endif
2368 """
2369
2370
2371 #
2372 # Main dissector entry templates
2373 #
2374
2375     template_main_dissector_start = """\
2376 /*
2377  * Called once we accept the packet as being for us; it sets the
2378  * Protocol and Info columns and creates the top-level protocol
2379  * tree item.
2380  */
2381 static proto_tree *
2382 start_dissecting(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset)
2383 {
2384
2385     proto_item *ti = NULL;
2386     proto_tree *tree = NULL;            /* init later, inside if(tree) */
2387
2388     col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
2389
2390     /*
2391      * Do not clear COL_INFO, as nothing is being written there by
2392      * this dissector yet. So leave it as is from the GIOP dissector.
2393      * TODO: add something useful to COL_INFO
2394      *     col_clear(pinfo->cinfo, COL_INFO);
2395      */
2396
2397     if (ptree) {
2398         ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, -1, ENC_NA);
2399         tree = proto_item_add_subtree(ti, ett_@dissname@);
2400     }
2401     return tree;
2402 }
2403
2404 static proto_item*
2405 process_RequestOperation(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, MessageHeader *header, const gchar *operation)
2406 {
2407     proto_item *pi;
2408     if(header->message_type == Reply) {
2409         /* fill-up info column */
2410         col_append_fstr(pinfo->cinfo, COL_INFO, " op = %s",operation);
2411     }
2412     /* fill-up the field */
2413     pi=proto_tree_add_string(ptree, hf_operationrequest, tvb, 0, 0, operation);
2414     PROTO_ITEM_SET_GENERATED(pi);
2415     return pi;
2416 }
2417
2418 static gboolean
2419 dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, const gchar *operation, gchar *idlname)
2420 {
2421     proto_item *item _U_;
2422     proto_tree *tree _U_;
2423     gboolean stream_is_big_endian = is_big_endian(header); /* get endianess */
2424
2425     /* If we have a USER Exception, then decode it and return */
2426     if ((header->message_type == Reply) && (header->rep_status == USER_EXCEPTION)) {
2427        return decode_user_exception(tvb, pinfo, ptree, offset, header, operation, stream_is_big_endian);
2428     }
2429 """
2430
2431     template_main_dissector_switch_msgtype_start = """\
2432 switch(header->message_type) {
2433 """
2434     template_main_dissector_switch_msgtype_start_request_reply = """\
2435 case Request:
2436 case Reply:
2437 """
2438     template_main_dissector_switch_msgtype_end_request_reply = """\
2439 break;
2440 """
2441     template_main_dissector_switch_msgtype_all_other_msgtype = """\
2442 case CancelRequest:
2443 case LocateRequest:
2444 case LocateReply:
2445 case CloseConnection:
2446 case MessageError:
2447 case Fragment:
2448    return FALSE;      /* not handled yet */
2449
2450 default:
2451    return FALSE;      /* not handled yet */
2452
2453 }   /* switch */
2454 """
2455     template_main_dissector_end = """\
2456
2457     return FALSE;
2458
2459 }  /* End of main dissector  */
2460 """
2461
2462
2463
2464
2465
2466
2467
2468 #-------------------------------------------------------------#
2469 #             Exception handling templates                    #
2470 #-------------------------------------------------------------#
2471
2472
2473
2474
2475
2476
2477
2478     template_exception_helpers_start = """\
2479 /*  Begin Exception Helper Functions  */
2480
2481 """
2482     template_exception_helpers_end = """\
2483
2484 /*  End Exception Helper Functions  */
2485 """
2486
2487
2488
2489 #
2490 # template for Main delegator for exception handling
2491 #
2492
2493     template_main_exception_delegator_start = """\
2494 /*
2495  * Main delegator for exception handling
2496  *
2497  */
2498 static gboolean
2499 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_)
2500 {
2501     proto_tree *tree _U_;
2502
2503     if (!header->exception_id)
2504         return FALSE;
2505 """
2506
2507
2508 #
2509 # template for exception delegation code body
2510 #
2511     template_ex_delegate_code = """\
2512 if (strcmp(header->exception_id, "@exname@") == 0) {
2513    tree = start_dissecting(tvb, pinfo, ptree, offset);
2514    decode_ex_@sname@(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);   /*  @exname@  */
2515    return TRUE;
2516 }
2517 """
2518
2519
2520 #
2521 # End of Main delegator for exception handling
2522 #
2523
2524     template_main_exception_delegator_end = """
2525     return FALSE;    /* user exception not found */
2526 }
2527 """
2528
2529 #
2530 # template for exception helper code
2531 #
2532
2533
2534     template_exception_helper_function_start = """\
2535 /* Exception = @exname@ */
2536 static void
2537 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_)
2538 {
2539     proto_item *item _U_;
2540 """
2541
2542     template_exception_helper_function_end = """\
2543 }
2544 """
2545
2546
2547 #
2548 # template for struct helper code
2549 #
2550
2551
2552     template_struct_helper_function_start = """\
2553 /* Struct = @stname@ */
2554 static void
2555 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_)
2556 {
2557 """
2558
2559     template_struct_helper_function_end = """\
2560 }
2561 """
2562
2563 #
2564 # template for union helper code
2565 #
2566
2567
2568     template_union_helper_function_start = """\
2569 /* Union = @unname@ */
2570 static void
2571 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_)
2572 {
2573     proto_item* item _U_;
2574 """
2575
2576     template_union_helper_function_end = """\
2577 }
2578 """
2579
2580
2581
2582 #-------------------------------------------------------------#
2583 #             Value string  templates                         #
2584 #-------------------------------------------------------------#
2585
2586     template_value_string_start = """\
2587 static const value_string @valstringname@[] = {
2588 """
2589     template_value_string_entry = """\
2590    { @intval@, \"@description@\" },"""
2591
2592     template_value_string_end = """\
2593    { 0,       NULL },
2594 };
2595 """
2596
2597
2598
2599 #-------------------------------------------------------------#
2600 #             Enum   handling templates                       #
2601 #-------------------------------------------------------------#
2602
2603     template_comment_enums_start = """\
2604 /*
2605  * IDL Enums Start
2606  */
2607 """
2608     template_comment_enums_end = """\
2609 /*
2610  * IDL Enums End
2611  */
2612 """
2613     template_comment_enum_comment = """\
2614 /*
2615  * Enum = @ename@
2616  */"""
2617
2618
2619
2620 #-------------------------------------------------------------#
2621 #             Attribute handling templates                    #
2622 #-------------------------------------------------------------#
2623
2624
2625     template_comment_attributes_start = """\
2626 /*
2627  * IDL Attributes Start
2628  */
2629 """
2630
2631     #
2632     # get/set accessor method names are language mapping dependant.
2633     #
2634
2635     template_attributes_declare_Java_get = """static const char get_@sname@_at[] = \"_get_@atname@\" ;"""
2636     template_attributes_declare_Java_set = """static const char set_@sname@_at[] = \"_set_@atname@\" ;"""
2637
2638     template_comment_attributes_end = """
2639 /*
2640  * IDL Attributes End
2641  */
2642 """
2643
2644
2645     #
2646     # template for Attribute delegation code
2647     #
2648     # Note: _get_xxx() should only be called for Reply with NO_EXCEPTION
2649     # Note: _set_xxx() should only be called for Request
2650     #
2651     #
2652
2653     template_at_delegate_code_get = """\
2654 if (strcmp(operation, get_@sname@_at) == 0 && (header->message_type == Reply) && (header->rep_status == NO_EXCEPTION) ) {
2655    tree = start_dissecting(tvb, pinfo, ptree, offset);
2656    decode_get_@sname@_at(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);
2657    return TRUE;
2658 }
2659 """
2660     template_at_delegate_code_set = """\
2661 if (strcmp(operation, set_@sname@_at) == 0 && (header->message_type == Request) ) {
2662    tree = start_dissecting(tvb, pinfo, ptree, offset);
2663    decode_set_@sname@_at(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);
2664    return TRUE;
2665 }
2666 """
2667     template_attribute_helpers_start = """\
2668 /*  Begin Attribute Helper Functions  */
2669 """
2670     template_attribute_helpers_end = """\
2671
2672 /*  End Attribute Helper Functions  */
2673 """
2674
2675 #
2676 # template for attribute helper code
2677 #
2678
2679
2680     template_attribute_helper_function_start = """\
2681
2682 /* Attribute = @atname@ */
2683 static void
2684 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_)
2685 {
2686     proto_item* item _U_;
2687 """
2688
2689     template_attribute_helper_function_end = """\
2690 }
2691 """
2692
2693
2694 #-------------------------------------------------------------#
2695 #                     Debugging  templates                    #
2696 #-------------------------------------------------------------#
2697
2698     #
2699     # Template for outputting TODO "C" comments
2700     # so user know I need ti improve something.
2701     #
2702
2703     template_debug_TODO = """\
2704
2705 /* TODO - @message@ */
2706 """
2707     #
2708     # Template for outputting WARNING "C" comments
2709     # so user know if I have found a problem.
2710     #
2711
2712     template_debug_WARNING = """\
2713 /* WARNING - @message@ */
2714 """
2715
2716
2717
2718 #-------------------------------------------------------------#
2719 #                     IDL Union  templates                    #
2720 #-------------------------------------------------------------#
2721
2722     template_comment_union_code_start = """\
2723 /*
2724  * IDL Union Start - @uname@
2725  */
2726 """
2727     template_comment_union_code_end = """
2728 /*
2729  * IDL union End - @uname@
2730  */
2731 """
2732     template_comment_union_code_discriminant = """\
2733 /*
2734  * IDL Union - Discriminant - @uname@
2735  */
2736 """
2737     #
2738     # Cast Unions types to something appropriate
2739     # Enum value cast to guint32, all others cast to gint32
2740     # as omniidl accessor returns integer or Enum.
2741     #
2742
2743     template_union_code_save_discriminant_enum = """\
2744 disc_s_@discname@ = (gint32) u_octet4;     /* save Enum Value  discriminant and cast to gint32 */
2745 """
2746     template_union_code_save_discriminant_long = """\
2747 disc_s_@discname@ = (gint32) s_octet4;     /* save gint32 discriminant and cast to gint32 */
2748 """
2749
2750     template_union_code_save_discriminant_ulong = """\
2751 disc_s_@discname@ = (gint32) u_octet4;     /* save guint32 discriminant and cast to gint32 */
2752 """
2753     template_union_code_save_discriminant_short = """\
2754 disc_s_@discname@ = (gint32) s_octet2;     /* save gint16 discriminant and cast to gint32 */
2755 """
2756
2757     template_union_code_save_discriminant_ushort = """\
2758 disc_s_@discname@ = (gint32) u_octet2;     /* save guint16 discriminant and cast to gint32 */
2759 """
2760     template_union_code_save_discriminant_char = """\
2761 disc_s_@discname@ = (gint32) u_octet1;     /* save guint1 discriminant and cast to gint32 */
2762 """
2763     template_union_code_save_discriminant_boolean = """\
2764 disc_s_@discname@ = (gint32) u_octet1;     /* save guint1 discriminant and cast to gint32 */
2765 """
2766     template_comment_union_code_label_compare_start = """\
2767 if (disc_s_@discname@ == @labelval@) {
2768 """
2769     template_comment_union_code_label_compare_end = """\
2770     return;     /* End Compare for this discriminant type */
2771 }
2772 """
2773
2774
2775     template_comment_union_code_label_default_start = """
2776 /* Default Union Case Start */
2777 """
2778     template_comment_union_code_label_default_end = """\
2779 /* Default Union Case End */
2780 """
2781
2782     #
2783     # Templates for function prototypes.
2784     # This is used in genDeclares() for declaring function prototypes
2785     # for structs and union helper functions.
2786     #
2787
2788     template_hf_operations = """
2789 static int hf_operationrequest = -1;/* Request_Operation field */
2790 """
2791
2792     template_hf = """\
2793 static int hf_@name@ = -1;"""
2794
2795     template_prototype_start_dissecting = """
2796 static proto_tree *start_dissecting(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset);
2797
2798 """
2799     template_prototype_struct_start = """\
2800 /* Struct prototype declaration Start */
2801 """
2802     template_prototype_struct_end = """\
2803 /* Struct prototype declaration End */
2804 """
2805     template_prototype_struct_body = """\
2806 /* Struct = @stname@ */
2807 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_);
2808 """
2809     template_decode_struct = """\
2810 decode_@name@_st(tvb, pinfo, tree, item, offset, header, operation, stream_is_big_endian);"""
2811
2812     template_prototype_union_start = """\
2813 /* Union prototype declaration Start */"""
2814
2815     template_prototype_union_end = """\
2816 /* Union prototype declaration End */"""
2817
2818     template_prototype_union_body = """
2819 /* Union = @unname@ */
2820 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_);
2821 """
2822     template_decode_union = """
2823 decode_@name@_un(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);
2824 """
2825