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