6c6d8c08ef138070c96ac0808413845568d01e02
[samba.git] / source4 / build / pidl / Parse / Pidl / Ethereal / NDR.pm
1 ##################################################
2 # Samba4 NDR parser generator for IDL structures
3 # Copyright tridge@samba.org 2000-2003
4 # Copyright tpot@samba.org 2001
5 # Copyright jelmer@samba.org 2004-2005
6 # Portions based on idl2eth.c by Ronnie Sahlberg
7 # released under the GNU GPL
8
9 # TODO:
10 #  - subcontexts using tvb_new_subset()
11 #  - fixed arrays
12 #  - allow overrides in conformance file
13 #  - more built-in types:
14 #    sec_desc_buf -> lsa_dissect_sec_desc_buf
15
16 package Parse::Pidl::Ethereal::NDR;
17
18 use strict;
19 use Parse::Pidl::Typelist;
20 use Parse::Pidl::Util qw(has_property ParseExpr property_matches);
21 use Parse::Pidl::NDR;
22 use Parse::Pidl::Dump qw(DumpTypedef DumpFunction);
23 use Parse::Pidl::Ethereal::Conformance qw(ReadConformance);
24
25 my %types;
26
27 my $conformance = {};
28
29 my %ptrtype_mappings = (
30         "unique" => "NDR_POINTER_UNIQUE",
31         "ref" => "NDR_POINTER_REF",
32         "ptr" => "NDR_POINTER_PTR"
33 );
34
35 sub type2ft($)
36 {
37     my($t) = shift;
38  
39     return "FT_UINT$1" if $t =~ /uint(8|16|32|64)/;
40     return "FT_INT$1" if $t =~ /int(8|16|32|64)/;
41     return "FT_UINT64", if $t eq "HYPER_T" or $t eq "NTTIME"
42         or $t eq "NTTIME_1sec" or $t eq "NTTIME_hyper" or $t eq "hyper";
43
44     return "FT_STRING" if ($t eq "string");
45    
46     return "FT_NONE";
47 }
48
49 sub StripPrefixes($)
50 {
51         my ($s) = @_;
52
53         foreach (@{$conformance->{strip_prefixes}}) {
54                 $s =~ s/^$_\_//g;
55         }
56
57         return $s;
58 }
59
60 # Convert a IDL structure field name (e.g access_mask) to a prettier
61 # string like 'Access Mask'.
62
63 sub field2name($)
64 {
65     my($field) = shift;
66
67     $field =~ s/_/ /g;          # Replace underscores with spaces
68     $field =~ s/(\w+)/\u\L$1/g; # Capitalise each word
69     
70     return $field;
71 }
72
73 my %res = ();
74 my $tabs = "";
75 sub pidl_code($)
76 {
77         my $d = shift;
78         if ($d) {
79                 $res{code} .= $tabs;
80                 $res{code} .= $d;
81         }
82         $res{code} .="\n";
83 }
84
85 sub pidl_hdr($) { my $x = shift; $res{hdr} .= "$x\n"; }
86 sub pidl_def($) { my $x = shift; $res{def} .= "$x\n"; }
87
88 sub indent()
89 {
90         $tabs .= "\t";
91 }
92
93 sub deindent()
94 {
95         $tabs = substr($tabs, 0, -1);
96 }
97
98 sub PrintIdl($)
99 {
100         my $idl = shift;
101
102         foreach (split /\n/, $idl) {
103                 pidl_code "/* IDL: $_ */";
104         }
105 }
106
107 #####################################################################
108 # parse the interface definitions
109 sub Interface($)
110 {
111         my($interface) = @_;
112         Typedef($_,$interface->{NAME}) foreach (@{$interface->{TYPEDEFS}});
113         Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
114 }
115
116 sub Enum($$$)
117 {
118         my ($e,$name,$ifname) = @_;
119         my $valsstring = "$ifname\_$name\_vals";
120         my $dissectorname = "$ifname\_dissect\_".StripPrefixes($name);
121         return if (defined($conformance->{noemit}->{$dissectorname}));
122
123         foreach (@{$e->{ELEMENTS}}) {
124                 if (/([^=]*)=(.*)/) {
125                         pidl_hdr "#define $1 $2";
126                 }
127         }
128         
129         pidl_hdr "extern const value_string $valsstring;";
130         pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
131
132         pidl_def "const value_string ".$valsstring."[] = {";
133         indent;
134         foreach (@{$e->{ELEMENTS}}) {
135                 next unless (/([^=]*)=(.*)/);
136                 pidl_code "{ $1, \"$2\" },";
137         }
138
139         pidl_def "{ 0, NULL }";
140         deindent;
141         pidl_def "};";
142
143         pidl_code "int";
144         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param _U_)";
145         pidl_code "{";
146         indent;
147         pidl_code "offset=dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, NULL);";
148         pidl_code "return offset;";
149         pidl_code "}\n";
150
151         my $enum_size = $e->{BASE_TYPE};
152         $enum_size =~ s/uint//g;
153         register_type($name, "offset=$dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", type2ft($e->{BASE_TYPE}), "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
154 }
155
156 sub Bitmap($$$)
157 {
158         my ($e,$name,$ifname) = @_;
159         my $dissectorname = "$ifname\_dissect\_".StripPrefixes($name);
160
161         register_ett("ett_$ifname\_$name");
162
163
164         pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
165
166         pidl_code "int";
167         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
168         pidl_code "{";
169         indent;
170         pidl_code "proto_item *item=NULL;";
171         pidl_code "proto_tree *tree=NULL;";
172         pidl_code "";
173                 
174         pidl_code "g$e->{BASE_TYPE} flags;";
175         if ($e->{ALIGN} > 1) {
176                 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
177         }
178
179         pidl_code "";
180
181         pidl_code "if(parent_tree) {";
182         indent;
183         pidl_code "item=proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, TRUE);";
184         pidl_code "tree=proto_item_add_subtree(item,ett_$ifname\_$name);";
185         deindent;
186         pidl_code "}\n";
187
188         pidl_code "offset=dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, NULL, drep, -1, &flags);";
189
190         foreach (@{$e->{ELEMENTS}}) {
191                 next unless (/([^ ]*) (.*)/);
192                 my ($en,$ev) = ($1,$2);
193                 my $hf_bitname = "hf_$ifname\_$name\_$en";
194                 my $filtername = "$ifname\.$name\.$en";
195                 
196                 register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", $ev, "");
197
198                 pidl_def "static const true_false_string $name\_$en\_tfs = {";
199                 pidl_def "   \"$en is SET\",";
200                 pidl_def "   \"$en is NOT SET\",";
201                 pidl_def "};";
202                 
203                 pidl_code "proto_tree_add_boolean(tree, $hf_bitname, tvb, offset-$e->{ALIGN}, $e->{ALIGN}, flags);";
204                 pidl_code "if (flags&$ev){";
205                 pidl_code "\tproto_item_append_text(item,\"$en \");";
206                 pidl_code "}";
207                 pidl_code "flags&=(~$ev);";
208                 pidl_code "";
209         }
210
211         pidl_code "if(flags){";
212         pidl_code "\tproto_item_append_text(item, \"UNKNOWN-FLAGS\");";
213         pidl_code "}\n";
214         pidl_code "return offset;";
215         deindent;
216         pidl_code "}\n";
217
218         my $size = $e->{BASE_TYPE};
219         $size =~ s/uint//g;
220         register_type($name, "offset=$dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", type2ft($e->{BASE_TYPE}), "BASE_DEC", "0", "NULL", $size/8);
221 }
222
223 sub ElementLevel($$$$$)
224 {
225         my ($e,$l,$hf,$myname,$pn) = @_;
226
227         if ($l->{TYPE} eq "POINTER") {
228                 my $type;
229                 if ($l->{LEVEL} eq "TOP") {
230                         $type = "toplevel";
231                 } elsif ($l->{LEVEL} eq "EMBEDDED") {
232                         $type = "embedded";
233                 }
234                 pidl_code "offset=dissect_ndr_$type\_pointer(tvb,offset,pinfo,tree,drep,$myname\_,$ptrtype_mappings{$l->{POINTER_TYPE}},\"".field2name(StripPrefixes($e->{NAME})) . " (".StripPrefixes($e->{TYPE}).")\",$hf);";
235         } elsif ($l->{TYPE} eq "ARRAY") {
236                 my $af = "";
237
238                 ($af = "ucarray") if ($l->{IS_CONFORMANT});
239                 ($af = "uvarray") if ($l->{IS_VARYING});
240                 ($af = "ucvarray") if ($l->{IS_CONFORMANT} and $l->{IS_VARYING});
241
242                 pidl_code "offset=dissect_ndr_$af(tvb,offset,pinfo,tree,drep,$myname\_);";
243         } elsif ($l->{TYPE} eq "DATA") {
244                 if ($l->{DATA_TYPE} eq "string") {
245                         my $bs = 2;
246
247                         if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*")) {
248                                 $bs = 1;
249                         }
250                         
251                         if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
252                                 pidl_code "offset=dissect_ndr_cvstring(tvb,offset,pinfo,tree,drep,$bs,$hf,FALSE,NULL);";
253                         } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_LEN4.*")) {
254                                 pidl_code "offset=dissect_ndr_vstring(tvb,offset,pinfo,tree,drep,$bs,$hf,FALSE,NULL);";
255                         }
256                 } elsif (defined($types{$l->{DATA_TYPE}})) {
257                         my $param = 0;
258                         if (defined($conformance->{dissectorparams}->{$myname})) {
259                                 $param = $conformance->{dissectorparams}->{$myname};
260                         }
261                         my $x = $types{$l->{DATA_TYPE}}->{CALL};
262                         $x =~ s/\@HF\@/$hf/g;
263                         $x =~ s/\@PARAM\@/$param/g;
264                         pidl_code "$x";
265                 } else {
266                         warn("Unknown data type `$l->{DATA_TYPE}'");
267                 }
268         } elsif ($_->{TYPE} eq "SUBCONTEXT") {
269                 my $num_bits = ($l->{HEADER_SIZE}*8);
270                 pidl_code "guint$num_bits size;";
271                 pidl_code "int start_offset=offset;";
272                 pidl_code "tvbuff_t *subtvb;";
273                 pidl_code "offset=dissect_ndr_uint$num_bits(tvb,offset,pinfo,drep,&size);";
274                 pidl_code "proto_tree_add_text(tree,tvb,start_offset,offset-start_offset+size,\"Subcontext size\");";
275
276                 pidl_code "subtvb = tvb_new_subset(tvb,offset,size,-1);";
277                 pidl_code "$myname\_(subtvb,0,pinfo,tree,drep);";
278         }
279 }
280
281 sub Element($$$)
282 {
283         my ($e,$pn,$ifname) = @_;
284
285         my $dissectorname = "$ifname\_dissect\_".StripPrefixes($pn)."\_".StripPrefixes($e->{NAME});
286
287         my $call_code = "offset=$dissectorname(tvb,offset,pinfo,tree,drep);";
288
289         my $hf = register_hf_field("hf_$ifname\_$pn\_$e->{NAME}", field2name($e->{NAME}), "$ifname.$pn.$e->{NAME}", type2ft($e->{TYPE}), "BASE_HEX", "NULL", 0, "");
290
291         if (defined($conformance->{noemit}->{$dissectorname})) {
292                 return $call_code;
293         }
294
295         my $add = "";
296
297         foreach (@{$e->{LEVELS}}) {
298                 next if ($_->{TYPE} eq "SWITCH");
299                 pidl_def "static int $dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep);";
300                 pidl_code "static int";
301                 pidl_code "$dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)";
302                 pidl_code "{";
303                 indent;
304
305                 ElementLevel($e,$_,$hf,$dissectorname.$add,$pn);
306
307                 pidl_code "return offset;";
308                 deindent;
309                 pidl_code "}\n";
310                 $add.="_";
311         }
312
313         return $call_code;
314 }
315
316 sub Function($$$)
317 {
318         my ($fn,$ifname) = @_;
319
320         my %dissectornames;
321
322         foreach (@{$fn->{ELEMENTS}}) {
323                 $dissectornames{$_->{NAME}} = Element($_, $fn->{NAME}, $ifname) 
324         }
325         
326         PrintIdl DumpFunction($fn->{ORIGINAL});
327         pidl_code "static int";
328         pidl_code "$ifname\_dissect\_$fn->{NAME}_response(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_)";
329         pidl_code "{";
330         indent;
331         foreach (@{$fn->{ELEMENTS}}) {
332                 if (grep(/out/,@{$_->{DIRECTION}})) {
333                         pidl_code "$dissectornames{$_->{NAME}}";
334                         pidl_code "offset=dissect_deferred_pointers(pinfo,tvb,offset,drep);";
335                         pidl_code "";
336                 }
337         }
338
339         if (not defined($fn->{RETURN_TYPE})) {
340         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
341                 pidl_code "offset=dissect_ntstatus(tvb,offset,pinfo,tree,drep,hf\_$ifname\_status, NULL);";
342         } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
343                 pidl_code "offset=dissect_ndr_uint32(tvb,offset,pinfo,tree,drep,hf\_$ifname\_werror, NULL);";
344         }
345
346         pidl_code "return offset;";
347         deindent;
348         pidl_code "}\n";
349
350         pidl_code "static int";
351         pidl_code "$ifname\_dissect\_$fn->{NAME}_request(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_)";
352         pidl_code "{";
353         indent;
354         foreach (@{$fn->{ELEMENTS}}) {
355                 if (grep(/in/,@{$_->{DIRECTION}})) {
356                         pidl_code "$dissectornames{$_->{NAME}}";
357                         pidl_code "offset=dissect_deferred_pointers(pinfo,tvb,offset,drep);";
358                 }
359
360         }
361
362         pidl_code "return offset;";
363         deindent;
364         pidl_code "}\n";
365 }
366
367 sub Struct($$$)
368 {
369         my ($e,$name,$ifname) = @_;
370         my $dissectorname = "$ifname\_dissect\_".StripPrefixes($name);
371
372         return if (defined($conformance->{noemit}->{$dissectorname}));
373
374         register_ett("ett_$ifname\_$name");
375
376         my $res = "";
377         ($res.="\t".Element($_, $name, $ifname)."\n\n") foreach (@{$e->{ELEMENTS}});
378
379         pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_);";
380
381         pidl_code "int";
382         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
383         pidl_code "{";
384         indent;
385         pidl_code "proto_item *item = NULL;";
386         pidl_code "proto_tree *tree = NULL;";
387         pidl_code "int old_offset;";
388         pidl_code "";
389
390         if ($e->{ALIGN} > 1) {
391                 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
392         }
393         pidl_code "";
394
395         pidl_code "old_offset=offset;";
396         pidl_code "";
397         pidl_code "if(parent_tree){";
398         indent;
399         pidl_code "item=proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, TRUE);";
400         pidl_code "tree=proto_item_add_subtree(item, ett_$ifname\_$name);";
401         deindent;
402         pidl_code "}";
403
404         pidl_code "\n$res";
405
406         pidl_code "proto_item_set_len(item, offset-old_offset);";
407         pidl_code "return offset;";
408         deindent;
409         pidl_code "}\n";
410
411         register_type($name, "offset=$dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
412 }
413
414 sub Union($$$)
415 {
416         my ($e,$name,$ifname) = @_;
417
418         my $dissectorname = "$ifname\_dissect_".StripPrefixes($name);
419         return if (defined($conformance->{noemit}->{$dissectorname}));
420         
421         register_ett("ett_$ifname\_$name");
422
423         my $res = "";
424         foreach (@{$e->{ELEMENTS}}) {
425                 $res.="\t\t\t$_->{CASE}:\n";
426                 if ($_->{TYPE} ne "EMPTY") {
427                         $res.="\t\t\t\t".Element($_, $name, $ifname)."\n";
428                 }
429                 $res.="\t\t\tbreak;\n\n";
430         }
431
432         pidl_code "static int";
433         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
434         pidl_code "{";
435         indent;
436         pidl_code "proto_item *item=NULL;";
437         pidl_code "proto_tree *tree=NULL;";
438         pidl_code "int old_offset;";
439         pidl_code "g$e->{SWITCH_TYPE} level;";
440         pidl_code "";
441
442         if ($e->{ALIGN} > 1) {
443                 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
444         }
445
446         pidl_code "";
447
448         pidl_code "old_offset=offset;";
449         pidl_code "if(parent_tree){";
450         indent;
451         pidl_code "item=proto_tree_add_text(parent_tree,tvb,offset,-1,\"$name\");";
452         pidl_code "tree=proto_item_add_subtree(item,ett_$ifname\_$name);";
453         deindent;
454         pidl_code "}";
455
456         pidl_code "";
457
458         pidl_code "offset = dissect_ndr_$e->{SWITCH_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, &level);";
459
460         pidl_code "switch(level) {";
461         pidl_code $res;
462         pidl_code "proto_item_set_len(item, offset-old_offset);";
463         pidl_code "return offset;";
464         deindent;
465         pidl_code "}";
466
467         register_type($name, "offset=$dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
468 }
469
470 sub Typedef($$)
471 {
472         my ($e,$ifname) = @_;
473
474         PrintIdl DumpTypedef($e->{ORIGINAL});
475
476         {
477                 ENUM => \&Enum,
478                 STRUCT => \&Struct,
479                 UNION => \&Union,
480                 BITMAP => \&Bitmap
481         }->{$e->{DATA}->{TYPE}}->($e->{DATA}, $e->{NAME}, $ifname);
482 }
483
484 sub RegisterInterface($)
485 {
486         my ($x) = @_;
487
488         pidl_code "void proto_register_dcerpc_$x->{NAME}(void)";
489         pidl_code "{";
490         indent;
491
492         $res{code}.=DumpHfList()."\n";
493         $res{code}.="\n".DumpEttList()."\n";
494         
495         if (defined($x->{UUID})) {
496             # These can be changed to non-pidl_code names if the old dissectors
497             # in epan/dissctors are deleted.
498     
499             my $name = "\"" . uc($x->{NAME}) . " (pidl)\"";
500             my $short_name = $x->{NAME};
501             my $filter_name = $x->{NAME};
502
503             if (has_property($x, "helpstring")) {
504                 $name = $x->{PROPERTIES}->{helpstring};
505             }
506
507             if (defined($conformance->{protocols}->{$x->{NAME}})) {
508                 $short_name = $conformance->{protocols}->{$x->{NAME}}->{SHORTNAME};
509                 $name = $conformance->{protocols}->{$x->{NAME}}->{LONGNAME};
510                 $filter_name = $conformance->{protocols}->{$x->{NAME}}->{FILTERNAME};
511             }
512
513             pidl_code "proto_dcerpc_$x->{NAME} = proto_register_protocol($name, \"$short_name\", \"$filter_name\");";
514             
515             pidl_code "proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));";
516             pidl_code "proto_register_subtree_array(ett, array_length(ett));";
517         } else {
518             pidl_code "proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");";
519             pidl_code "proto_register_field_array(proto_dcerpc, hf, array_length(hf));";
520             pidl_code "proto_register_subtree_array(ett, array_length(ett));";
521         }
522             
523         deindent;
524         pidl_code "}\n";
525 }
526
527 sub RegisterInterfaceHandoff($)
528 {
529         my $x = shift;
530         pidl_code "void proto_reg_handoff_dcerpc_$x->{NAME}(void)";
531         pidl_code "{";
532         indent;
533         pidl_code "dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},";
534         pidl_code "\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},";
535         pidl_code "\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);";
536         deindent;
537         pidl_code "}";
538 }
539
540 sub ProcessInterface($)
541 {
542         my ($x) = @_;
543
544         push(@{$conformance->{strip_prefixes}}, $x->{NAME});
545
546         my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
547         pidl_hdr "#ifndef $define";
548         pidl_hdr "#define $define";
549         pidl_hdr "";
550
551         if (defined $x->{PROPERTIES}->{depends}) {
552                 foreach (split / /, $x->{PROPERTIES}->{depends}) {
553                         pidl_hdr "#include \"packet-dcerpc-$_\.h\"\n";
554                 }
555         }
556
557         pidl_def "static gint proto_dcerpc_$x->{NAME} = -1;";
558         register_ett("ett_dcerpc_$x->{NAME}");
559         register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
560         register_hf_field("hf_$x->{NAME}_status", "Status", "$x->{NAME}.status", "FT_UINT32", "BASE_HEX", "VALS(NT_errors)", 0, "");
561         register_hf_field("hf_$x->{NAME}_werror", "Windows Error", "$x->{NAME}.werror", "FT_UINT32", "BASE_HEX", "NULL", 0, "");
562
563         if (defined($x->{UUID})) {
564                 my $if_uuid = $x->{UUID};
565
566             pidl_def "/* Version information */\n\n";
567             
568             pidl_def "static e_uuid_t uuid_dcerpc_$x->{NAME} = {";
569             pidl_def "\t0x" . substr($if_uuid, 1, 8) 
570                 . ", 0x" . substr($if_uuid, 10, 4)
571             . ", 0x" . substr($if_uuid, 15, 4) . ",";
572             pidl_def "\t{ 0x" . substr($if_uuid, 20, 2) 
573                 . ", 0x" . substr($if_uuid, 22, 2)
574             . ", 0x" . substr($if_uuid, 25, 2)
575             . ", 0x" . substr($if_uuid, 27, 2)
576             . ", 0x" . substr($if_uuid, 29, 2)
577             . ", 0x" . substr($if_uuid, 31, 2)
578             . ", 0x" . substr($if_uuid, 33, 2)
579             . ", 0x" . substr($if_uuid, 35, 2) . " }";
580             pidl_def "};";
581         
582             my $maj = $x->{VERSION};
583             $maj =~ s/\.(.*)$//g;
584             pidl_def "static guint16 ver_dcerpc_$x->{NAME} = $maj;";
585             pidl_def "";
586         }
587
588         Interface($x);
589
590         pidl_code "\n".DumpFunctionTable($x);
591
592         RegisterInterface($x);
593         RegisterInterfaceHandoff($x);
594
595         pidl_hdr "#endif /* $define */";
596 }
597
598
599 sub register_type($$$$$$$)
600 {
601         my ($type,$call,$ft,$base,$mask,$vals,$length) = @_;
602
603         $types{$type} = {
604                 TYPE => $type,
605                 CALL => $call,
606                 FT_TYPE => $ft,
607                 BASE => $base,
608                 MASK => $mask,
609                 VALSSTRING => $vals,
610                 LENGTH => $length
611         };
612 }
613
614 # Loads the default types
615 sub Initialize($)
616 {
617         my $cnf_file = shift;
618
619         $conformance = {};
620
621         ReadConformance($cnf_file, $conformance) or print "Warning: Not using conformance file `$cnf_file'\n";
622         
623         foreach my $bytes (qw(1 2 4 8)) {
624                 my $bits = $bytes * 8;
625                 register_type("uint$bits", "offset=dissect_ndr_uint$bits(tvb,offset,pinfo,tree,drep,\@HF\@,NULL);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
626                 register_type("int$bits", "offset=dissect_ndr_int$bits(tvb,offset,pinfo,tree,drep,\@HF\@,NULL);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
627         }
628                 
629         register_type("udlong", "offset=dissect_ndr_duint32(tvb,offset,pinfo,tree,drep,\@HF\@,NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
630         register_type("bool8", "offset=dissect_ndr_uint8(tvb,offset,pinfo,tree,drep,\@HF\@,NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
631         register_type("char", "offset=dissect_ndr_int8(tvb,offset,pinfo,tree,drep,\@HF\@,NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
632         register_type("long", "offset=dissect_ndr_int32(tvb,offset,pinfo,tree,drep,\@HF\@,NULL);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
633         register_type("dlong", "offset=dissect_ndr_duint32(tvb,offset,pinfo,tree,drep,\@HF\@,NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
634         register_type("GUID", "offset=dissect_ndr_uuid_t(tvb,offset,pinfo,tree,drep,\@HF\@,NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
635         register_type("policy_handle", "offset=dissect_nt_policy_hnd(tvb,offset,pinfo,tree,drep,\@HF\@,NULL,NULL,\@PARAM\@&0x01,\@PARAM\@&0x02);","FT_BYTES", "BASE_NONE", 0, "NULL", 4);
636         register_type("NTTIME", "offset=dissect_ndr_nt_NTTIME(tvb,offset,pinfo,tree,drep,\@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
637         register_type("NTTIME_hyper", "offset=dissect_ndr_nt_NTTIME(tvb,offset,pinfo,tree,drep,\@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
638         register_type("time_t", "offset=dissect_ndr_time_t(tvb,offset,pinfo,tree,drep,\@HF\@,NULL);","FT_ABSOLUTE_TIME", "BASE_DEC", 0, "NULL", 4);
639         register_type("NTTIME_1sec", "offset=dissect_ndr_nt_NTTIME(tvb,offset,pinfo,tree,drep,\@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
640         register_type("SID", "
641                 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
642
643                 di->hf_index = \@HF\@;
644
645                 offset=dissect_ndr_nt_SID_with_options(tvb,offset,pinfo,tree,drep,param);
646         ","FT_STRING", "BASE_DEC", 0, "NULL", 4);
647         register_type("WERROR", 
648                 "offset=dissect_ndr_uint32(tvb,offset,pinfo,tree,drep,\@HF\@,NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
649
650 }
651
652 #####################################################################
653 # Generate ethereal parser and header code
654 sub Parse($$)
655 {
656         my($ndr,$h_filename,$cnf_file) = @_;
657         Initialize($cnf_file);
658
659         $tabs = "";
660
661         %res = (code=>"",def=>"",hdr=>"");
662
663         pidl_hdr "/* header auto-generated by pidl */";
664
665         $res{headers} = "\n";
666         $res{headers} .= "#ifdef HAVE_CONFIG_H\n";
667         $res{headers} .= "#include \"config.h\"\n";
668         $res{headers} .= "#endif\n\n";
669         $res{headers} .= "#include <glib.h>\n";
670         $res{headers} .= "#include <string.h>\n";
671         $res{headers} .= "#include <epan/packet.h>\n\n";
672
673         $res{headers} .= "#include \"packet-dcerpc.h\"\n";
674         $res{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
675         $res{headers} .= "#include \"packet-windows-common.h\"\n";
676         $res{headers} .= "#include \"$h_filename\"\n";
677         pidl_code "";
678
679         # Ethereal protocol registration
680
681         ProcessInterface($_) foreach (@$ndr);
682
683         $res{ett} = DumpEttDeclaration();
684         $res{hf} = DumpHfDeclaration();
685
686         my $parser = "/* parser auto-generated by pidl */";
687         $parser.= $res{headers};
688         $parser.=$res{ett};
689         $parser.=$res{hf};
690         $parser.=$res{def};
691         $parser.=$conformance->{override};
692         $parser.=$res{code};
693
694         my $header = "/* autogenerated by pidl */\n\n";
695         $header.=$res{hdr};
696     
697         return ($parser,$header);
698 }
699
700 ###############################################################################
701 # ETT
702 ###############################################################################
703
704 my @ett = ();
705
706 sub register_ett($)
707 {
708         my $name = shift;
709
710         push (@ett, $name);     
711 }
712
713 sub DumpEttList()
714 {
715         my $res = "\tstatic gint *ett[] = {\n";
716         foreach (@ett) {
717                 $res .= "\t\t&$_,\n";
718         }
719
720         return "$res\t};\n";
721 }
722
723 sub DumpEttDeclaration()
724 {
725         my $res = "\n/* Ett declarations */\n";
726         foreach (@ett) {
727                 $res .= "static gint $_ = -1;\n";
728         }
729
730         return "$res\n";
731 }
732
733 ###############################################################################
734 # HF
735 ###############################################################################
736
737 my %hf = ();
738
739 sub register_hf_field($$$$$$$$) 
740 {
741         my ($index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
742
743         return $conformance->{hf_renames}->{$index} if defined ($conformance->{hf_renames}->{$index});
744
745         $hf{$index} = {
746                 INDEX => $index,
747                 NAME => $name,
748                 FILTER => $filter_name,
749                 FT_TYPE => $ft_type,
750                 BASE_TYPE => $base_type,
751                 VALS => $valsstring,
752                 MASK => $mask,
753                 BLURB => $blurb
754         };
755
756         return $index;
757 }
758
759 sub DumpHfDeclaration()
760 {
761         my $res = "";
762
763         $res = "\n/* Header field declarations */\n";
764
765         foreach (keys %hf) 
766         {
767                 $res .= "static gint $_ = -1;\n";
768         }
769
770         return "$res\n";
771 }
772
773 sub DumpHfList()
774 {
775         my $res = "\tstatic hf_register_info hf[] = {\n";
776
777         foreach (values %hf) 
778         {
779                 $res .= "\t{ &$_->{INDEX}, 
780           { \"$_->{NAME}\", \"$_->{FILTER}\", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALS}, $_->{MASK}, \"$_->{BLURB}\", HFILL }},
781 ";
782         }
783
784         return $res."\t};\n";
785 }
786
787
788 ###############################################################################
789 # Function table
790 ###############################################################################
791
792 sub DumpFunctionTable($)
793 {
794         my $if = shift;
795
796         my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
797         
798         foreach (@{$if->{FUNCTIONS}}) {
799                 $res.= "\t{ $_->{OPNUM}, \"$_->{NAME}\",\n";
800                 $res.= "\t   $if->{NAME}_dissect_$_->{NAME}_request, $if->{NAME}_dissect_$_->{NAME}_response},\n";
801         }
802
803         $res .= "\t{ 0, NULL, NULL, NULL }\n";
804
805         return "$res};\n";
806 }
807
808 1;