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