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