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