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