r12205: Support 'TFS' command in conformance files
[samba.git] / source / pidl / lib / 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,2005
5 # Copyright jelmer@samba.org 2004-2005
6 # Portions based on idl2eth.c by Ronnie Sahlberg
7 # released under the GNU GPL
8
9 =pod
10
11 =head1 NAME
12
13 Parse::Pidl::Ethereal::NDR - Parser generator for Ethereal
14
15 =cut
16
17 package Parse::Pidl::Ethereal::NDR;
18
19 use strict;
20 use Parse::Pidl::Typelist qw(getType);
21 use Parse::Pidl::Util qw(has_property ParseExpr property_matches make_str);
22 use Parse::Pidl::NDR qw(ContainsString GetNextLevel);
23 use Parse::Pidl::Dump qw(DumpTypedef DumpFunction);
24 use Parse::Pidl::Ethereal::Conformance qw(ReadConformance);
25
26 use vars qw($VERSION);
27 $VERSION = '0.01';
28
29 sub error($$)
30 {
31         my ($e,$t) = @_;
32         print "$e->{FILE}:$e->{LINE}: $t\n";
33 }
34
35 my @ett;
36
37 my %hf_used = ();
38 my %dissector_used = ();
39
40 my $conformance = undef;
41
42 my %ptrtype_mappings = (
43         "unique" => "NDR_POINTER_UNIQUE",
44         "ref" => "NDR_POINTER_REF",
45         "ptr" => "NDR_POINTER_PTR"
46 );
47
48 sub StripPrefixes($)
49 {
50         my ($s) = @_;
51
52         foreach (@{$conformance->{strip_prefixes}}) {
53                 $s =~ s/^$_\_//g;
54         }
55
56         return $s;
57 }
58
59 # Convert a IDL structure field name (e.g access_mask) to a prettier
60 # string like 'Access Mask'.
61
62 sub field2name($)
63 {
64     my($field) = shift;
65
66     $field =~ s/_/ /g;          # Replace underscores with spaces
67     $field =~ s/(\w+)/\u\L$1/g; # Capitalise each word
68     
69     return $field;
70 }
71
72 my %res = ();
73 my $tabs = "";
74 sub pidl_code($)
75 {
76         my $d = shift;
77         if ($d) {
78                 $res{code} .= $tabs;
79                 $res{code} .= $d;
80         }
81         $res{code} .="\n";
82 }
83
84 sub pidl_hdr($) { my $x = shift; $res{hdr} .= "$x\n"; }
85 sub pidl_def($) { my $x = shift; $res{def} .= "$x\n"; }
86
87 sub indent()
88 {
89         $tabs .= "\t";
90 }
91
92 sub deindent()
93 {
94         $tabs = substr($tabs, 0, -1);
95 }
96
97 sub PrintIdl($)
98 {
99         my $idl = shift;
100
101         foreach (split /\n/, $idl) {
102                 pidl_code "/* IDL: $_ */";
103         }
104
105         pidl_code "";
106 }
107
108 #####################################################################
109 # parse the interface definitions
110 sub Interface($)
111 {
112         my($interface) = @_;
113         Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}});
114         Typedef($_,$interface->{NAME}) foreach (@{$interface->{TYPEDEFS}});
115         Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
116 }
117
118 sub Enum($$$)
119 {
120         my ($e,$name,$ifname) = @_;
121         my $valsstring = "$ifname\_$name\_vals";
122         my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name);
123
124         return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
125
126         foreach (@{$e->{ELEMENTS}}) {
127                 if (/([^=]*)=(.*)/) {
128                         pidl_hdr "#define $1 ($2)";
129                 }
130         }
131         
132         pidl_hdr "extern const value_string $valsstring\[];";
133         pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
134
135         pidl_def "const value_string ".$valsstring."[] = {";
136         foreach (@{$e->{ELEMENTS}}) {
137                 next unless (/([^=]*)=(.*)/);
138                 pidl_def "\t{ $1, \"$1\" },";
139         }
140
141         pidl_def "{ 0, NULL }";
142         pidl_def "};";
143
144         pidl_code "int";
145         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param _U_)";
146         pidl_code "{";
147         indent;
148         pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, NULL);";
149         pidl_code "return offset;";
150         deindent;
151         pidl_code "}\n";
152
153         my $enum_size = $e->{BASE_TYPE};
154         $enum_size =~ s/uint//g;
155         register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
156 }
157
158 sub Bitmap($$$)
159 {
160         my ($e,$name,$ifname) = @_;
161         my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name);
162
163         register_ett("ett_$ifname\_$name");
164
165         pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
166
167         pidl_code "int";
168         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
169         pidl_code "{";
170         indent;
171         pidl_code "proto_item *item = NULL;";
172         pidl_code "proto_tree *tree = NULL;";
173         pidl_code "";
174                 
175         pidl_code "g$e->{BASE_TYPE} flags;";
176         if ($e->{ALIGN} > 1) {
177                 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
178         }
179
180         pidl_code "";
181
182         pidl_code "if(parent_tree) {";
183         indent;
184         pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, TRUE);";
185         pidl_code "tree = proto_item_add_subtree(item,ett_$ifname\_$name);";
186         deindent;
187         pidl_code "}\n";
188
189         pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, NULL, drep, -1, &flags);";
190
191         pidl_code "proto_item_append_text(item, \": \");\n";
192         pidl_code "if (!flags)";
193         pidl_code "\tproto_item_append_text(item, \"(No values set)\");\n";
194
195         foreach (@{$e->{ELEMENTS}}) {
196                 next unless (/([^ ]*) (.*)/);
197                 my ($en,$ev) = ($1,$2);
198                 my $hf_bitname = "hf_$ifname\_$name\_$en";
199                 my $filtername = "$ifname\.$name\.$en";
200
201                 $hf_used{$hf_bitname} = 1;
202                 
203                 register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", $ev, "");
204
205                 pidl_def "static const true_false_string $name\_$en\_tfs = {";
206                 if (defined($conformance->{tfs}->{$hf_bitname})) {
207                         pidl_def "   $conformance->{tfs}->{$hf_bitname}->{TRUE_STRING},";
208                         pidl_def "   $conformance->{tfs}->{$hf_bitname}->{FALSE_STRING},";
209                 } else {
210                         pidl_def "   \"$en is SET\",";
211                         pidl_def "   \"$en is NOT SET\",";
212                 }
213                 pidl_def "};";
214                 
215                 pidl_code "proto_tree_add_boolean(tree, $hf_bitname, tvb, offset-$e->{ALIGN}, $e->{ALIGN}, flags);";
216                 pidl_code "if (flags&$ev){";
217                 pidl_code "\tproto_item_append_text(item, \"$en\");";
218                 pidl_code "\tif (flags & (~$ev))";
219                 pidl_code "\t\tproto_item_append_text(item, \", \");";
220                 pidl_code "}";
221                 pidl_code "flags&=(~$ev);";
222                 pidl_code "";
223         }
224
225         pidl_code "if(flags){";
226         pidl_code "\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);";
227         pidl_code "}\n";
228         pidl_code "return offset;";
229         deindent;
230         pidl_code "}\n";
231
232         my $size = $e->{BASE_TYPE};
233         $size =~ s/uint//g;
234         register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8);
235 }
236
237 sub ElementLevel($$$$$)
238 {
239         my ($e,$l,$hf,$myname,$pn) = @_;
240
241         my $param = 0;
242
243         if (defined($conformance->{dissectorparams}->{$myname})) {
244                 $conformance->{dissectorparams}->{$myname}->{PARAM} = 1;
245                 $param = $conformance->{dissectorparams}->{$myname}->{PARAM};
246         }
247
248         if ($l->{TYPE} eq "POINTER") {
249                 my $type;
250                 if ($l->{LEVEL} eq "TOP") {
251                         $type = "toplevel";
252                 } elsif ($l->{LEVEL} eq "EMBEDDED") {
253                         $type = "embedded";
254                 }
255                 pidl_code "offset = dissect_ndr_$type\_pointer(tvb, offset, pinfo, tree, drep, $myname\_, $ptrtype_mappings{$l->{POINTER_TYPE}}, \"Pointer to ".field2name(StripPrefixes($e->{NAME})) . " ($e->{TYPE})\",$hf);";
256         } elsif ($l->{TYPE} eq "ARRAY") {
257                 if ($l->{IS_INLINE}) {
258                         error($e->{ORIGINAL}, "Inline arrays not supported");
259                 } elsif ($l->{IS_FIXED}) {
260                         pidl_code "int i;";
261                         pidl_code "for (i = 0; i < $l->{SIZE_IS}; i++)";
262                         pidl_code "\toffset = $myname\_(tvb, offset, pinfo, tree, drep);";
263                 } else {
264                         my $type = "";
265                         $type .= "c" if ($l->{IS_CONFORMANT});
266                         $type .= "v" if ($l->{IS_VARYING});
267
268                         unless ($l->{IS_ZERO_TERMINATED}) {
269                                 pidl_code "offset = dissect_ndr_u" . $type . "array(tvb, offset, pinfo, tree, drep, $myname\_);";
270                         } else {
271                                 my $nl = GetNextLevel($e,$l);
272                                 pidl_code "char *data;";
273                                 pidl_code "";
274                                 pidl_code "offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, drep, sizeof(g$nl->{DATA_TYPE}), $hf, FALSE, &data);";
275                                 pidl_code "proto_item_append_text(tree, \": %s\", data);";
276                         }
277                 }
278         } elsif ($l->{TYPE} eq "DATA") {
279                 if ($l->{DATA_TYPE} eq "string") {
280                         my $bs = 2; # Byte size defaults to that of UCS2
281
282
283                         ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
284                         
285                         if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
286                                 pidl_code "char *data;\n";
287                                 pidl_code "offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, &data);";
288                                 pidl_code "proto_item_append_text(tree, \": %s\", data);";
289                         } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
290                                 pidl_code "offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, NULL);";
291                         } else {
292                                 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
293                         }
294                 } else {
295                         my $call;
296
297                         if ($conformance->{imports}->{$l->{DATA_TYPE}}) {
298                                 $call = $conformance->{imports}->{$l->{DATA_TYPE}}->{DATA};     
299                                 $conformance->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
300                         } elsif (defined($conformance->{imports}->{"$pn.$e->{NAME}"})) {
301                                 $call = $conformance->{imports}->{"$pn.$e->{NAME}"}->{DATA};
302                                 $conformance->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
303                             
304                         } elsif (defined($conformance->{types}->{$l->{DATA_TYPE}})) {
305                                 $call= $conformance->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
306                                 $conformance->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
307                         } else {
308                                 if ($l->{DATA_TYPE} =~ /^([a-z]+)\_(.*)$/)
309                                 {
310                                         pidl_code "offset = $1_dissect_struct_$2(tvb,offset,pinfo,tree,drep,$hf,$param);";
311                                 }
312
313                                 return;
314                         }
315
316                         $call =~ s/\@HF\@/$hf/g;
317                         $call =~ s/\@PARAM\@/$param/g;
318                         pidl_code "$call";
319                 }
320         } elsif ($_->{TYPE} eq "SUBCONTEXT") {
321                 my $num_bits = ($l->{HEADER_SIZE}*8);
322                 pidl_code "guint$num_bits size;";
323                 pidl_code "int start_offset = offset;";
324                 pidl_code "tvbuff_t *subtvb;";
325                 pidl_code "offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf, &size);";
326                 pidl_code "proto_tree_add_text(tree, tvb, start_offset, offset - start_offset + size, \"Subcontext size\");";
327
328                 pidl_code "subtvb = tvb_new_subset(tvb, offset, size, -1);";
329                 pidl_code "$myname\_(subtvb, 0, pinfo, tree, drep);";
330         } else {
331                 die("Unknown type `$_->{TYPE}'");
332         }
333 }
334
335 sub Element($$$)
336 {
337         my ($e,$pn,$ifname) = @_;
338
339         my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn)."\_".StripPrefixes($e->{NAME});
340
341         my $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
342
343         my $type = find_type($e->{TYPE});
344
345         if (not defined($type)) {
346                 # default settings
347                 $type = {
348                         MASK => 0,
349                         VALSSTRING => "NULL",
350                         FT_TYPE => "FT_NONE",
351                         BASE_TYPE => "BASE_HEX"
352                 };
353         }
354
355         if (ContainsString($e)) {
356                 $type = {
357                         MASK => 0,
358                         VALSSTRING => "NULL",
359                         FT_TYPE => "FT_STRING",
360                         BASE_TYPE => "BASE_DEC"
361                 };
362         }
363
364         my $hf = register_hf_field("hf_$ifname\_$pn\_$e->{NAME}", field2name($e->{NAME}), "$ifname.$pn.$e->{NAME}", $type->{FT_TYPE}, $type->{BASE_TYPE}, $type->{VALSSTRING}, $type->{MASK}, "");
365         $hf_used{$hf} = 1;
366
367         my $eltname = StripPrefixes($pn) . ".$e->{NAME}";
368         if (defined($conformance->{noemit}->{$eltname})) {
369                 return $call_code;
370         }
371
372         my $add = "";
373
374         foreach (@{$e->{LEVELS}}) {
375                 next if ($_->{TYPE} eq "SWITCH");
376                 pidl_def "static int $dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep);";
377                 pidl_code "static int";
378                 pidl_code "$dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)";
379                 pidl_code "{";
380                 indent;
381
382                 ElementLevel($e,$_,$hf,$dissectorname.$add,$pn);
383
384                 pidl_code "";
385                 pidl_code "return offset;";
386                 deindent;
387                 pidl_code "}\n";
388                 $add.="_";
389                 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
390         }
391
392         return $call_code;
393 }
394
395 sub Function($$$)
396 {
397         my ($fn,$ifname) = @_;
398
399         my %dissectornames;
400
401         foreach (@{$fn->{ELEMENTS}}) {
402             $dissectornames{$_->{NAME}} = Element($_, $fn->{NAME}, $ifname) if not defined($dissectornames{$_->{NAME}});
403         }
404         
405         my $fn_name = $_->{NAME};
406         $fn_name =~ s/^${ifname}_//;
407
408         PrintIdl DumpFunction($fn->{ORIGINAL});
409         pidl_code "static int";
410         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_)";
411         pidl_code "{";
412         indent;
413         pidl_code "guint32 status;\n";
414         foreach (@{$fn->{ELEMENTS}}) {
415                 if (grep(/out/,@{$_->{DIRECTION}})) {
416                         pidl_code "$dissectornames{$_->{NAME}}";
417                         pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
418                         pidl_code "";
419                 }
420         }
421
422         if (not defined($fn->{RETURN_TYPE})) {
423         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
424                 pidl_code "offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n";
425                 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
426                 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n";
427                 $hf_used{"hf\_$ifname\_status"} = 1;
428         } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
429                 pidl_code "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &status);\n";
430                 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
431                 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, DOS_errors, \"Unknown DOS error 0x%08x\"));\n";
432                 
433                 $hf_used{"hf\_$ifname\_werror"} = 1;
434         } else {
435                 print "$fn->{FILE}:$fn->{LINE}: error: return type `$fn->{RETURN_TYPE}' not yet supported\n";
436         }
437                 
438
439         pidl_code "return offset;";
440         deindent;
441         pidl_code "}\n";
442
443         pidl_code "static int";
444         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_)";
445         pidl_code "{";
446         indent;
447         foreach (@{$fn->{ELEMENTS}}) {
448                 if (grep(/in/,@{$_->{DIRECTION}})) {
449                         pidl_code "$dissectornames{$_->{NAME}}";
450                         pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
451                 }
452
453         }
454
455         pidl_code "return offset;";
456         deindent;
457         pidl_code "}\n";
458 }
459
460 sub Struct($$$)
461 {
462         my ($e,$name,$ifname) = @_;
463         my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name);
464
465         return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
466
467         register_ett("ett_$ifname\_$name");
468
469         my $res = "";
470         ($res.="\t".Element($_, $name, $ifname)."\n\n") foreach (@{$e->{ELEMENTS}});
471
472         pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_);";
473
474         pidl_code "int";
475         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
476         pidl_code "{";
477         indent;
478         pidl_code "proto_item *item = NULL;";
479         pidl_code "proto_tree *tree = NULL;";
480         pidl_code "int old_offset;";
481         pidl_code "";
482
483         if ($e->{ALIGN} > 1) {
484                 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
485         }
486         pidl_code "";
487
488         pidl_code "old_offset = offset;";
489         pidl_code "";
490         pidl_code "if(parent_tree){";
491         indent;
492         pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, TRUE);";
493         pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
494         deindent;
495         pidl_code "}";
496
497         pidl_code "\n$res";
498
499         pidl_code "proto_item_set_len(item, offset-old_offset);\n";
500         pidl_code "return offset;";
501         deindent;
502         pidl_code "}\n";
503
504         register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
505 }
506
507 sub Union($$$)
508 {
509         my ($e,$name,$ifname) = @_;
510
511         my $dissectorname = "$ifname\_dissect_".StripPrefixes($name);
512
513         return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
514         
515         register_ett("ett_$ifname\_$name");
516
517         my $res = "";
518         foreach (@{$e->{ELEMENTS}}) {
519                 $res.="\n\t\t$_->{CASE}:\n";
520                 if ($_->{TYPE} ne "EMPTY") {
521                         $res.="\t\t\t".Element($_, $name, $ifname)."\n";
522                 }
523                 $res.="\t\tbreak;\n";
524         }
525
526         my $switch_type;
527         my $switch_dissect;
528         my $switch_dt = getType($e->{SWITCH_TYPE});
529         if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
530                 $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt);
531                 $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt);
532         } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
533                 $switch_type = "g$e->{SWITCH_TYPE}";
534                 $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
535         }
536
537         pidl_code "static int";
538         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
539         pidl_code "{";
540         indent;
541         pidl_code "proto_item *item = NULL;";
542         pidl_code "proto_tree *tree = NULL;";
543         pidl_code "int old_offset;";
544         pidl_code "$switch_type level;";
545         pidl_code "";
546
547         if ($e->{ALIGN} > 1) {
548                 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
549         }
550
551         pidl_code "";
552
553         pidl_code "old_offset = offset;";
554         pidl_code "if(parent_tree){";
555         indent;
556         pidl_code "item = proto_tree_add_text(parent_tree, tvb, offset, -1, \"$name\");";
557         pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
558         deindent;
559         pidl_code "}";
560
561         pidl_code "";
562
563         pidl_code "offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);";
564
565         pidl_code "switch(level) {$res\t}";
566         pidl_code "proto_item_set_len(item, offset-old_offset);\n";
567         pidl_code "return offset;";
568         deindent;
569         pidl_code "}";
570
571         register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
572 }
573
574 sub Const($$)
575 {
576         my ($const,$ifname) = @_;
577         
578         if (!defined($const->{ARRAY_LEN}[0])) {
579                 pidl_hdr "#define $const->{NAME}\t( $const->{VALUE} )\n";
580         } else {
581                 pidl_hdr "#define $const->{NAME}\t $const->{VALUE}\n";
582         }
583 }
584
585 sub Typedef($$)
586 {
587         my ($e,$ifname) = @_;
588
589         PrintIdl DumpTypedef($e->{ORIGINAL});
590
591         {
592                 ENUM => \&Enum,
593                 STRUCT => \&Struct,
594                 UNION => \&Union,
595                 BITMAP => \&Bitmap
596         }->{$e->{DATA}->{TYPE}}->($e->{DATA}, $e->{NAME}, $ifname);
597 }
598
599 sub RegisterInterface($)
600 {
601         my ($x) = @_;
602
603         pidl_code "void proto_register_dcerpc_$x->{NAME}(void)";
604         pidl_code "{";
605         indent;
606
607         $res{code}.=DumpHfList()."\n";
608         $res{code}.="\n".DumpEttList()."\n";
609         
610         if (defined($x->{UUID})) {
611             # These can be changed to non-pidl_code names if the old dissectors
612             # in epan/dissctors are deleted.
613     
614             my $name = uc($x->{NAME}) . " (pidl)";
615             my $short_name = uc($x->{NAME});
616             my $filter_name = $x->{NAME};
617
618             if (has_property($x, "helpstring")) {
619                 $name = $x->{PROPERTIES}->{helpstring};
620             }
621
622             if (defined($conformance->{protocols}->{$x->{NAME}})) {
623                 $short_name = $conformance->{protocols}->{$x->{NAME}}->{SHORTNAME};
624                 $name = $conformance->{protocols}->{$x->{NAME}}->{LONGNAME};
625                 $filter_name = $conformance->{protocols}->{$x->{NAME}}->{FILTERNAME};
626             }
627
628             pidl_code "proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");";
629             
630             pidl_code "proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));";
631             pidl_code "proto_register_subtree_array(ett, array_length(ett));";
632         } else {
633             pidl_code "proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");";
634             pidl_code "proto_register_field_array(proto_dcerpc, hf, array_length(hf));";
635             pidl_code "proto_register_subtree_array(ett, array_length(ett));";
636         }
637             
638         deindent;
639         pidl_code "}\n";
640 }
641
642 sub RegisterInterfaceHandoff($)
643 {
644         my $x = shift;
645
646         if (defined($x->{UUID})) {
647             pidl_code "void proto_reg_handoff_dcerpc_$x->{NAME}(void)";
648             pidl_code "{";
649             indent;
650             pidl_code "dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},";
651             pidl_code "\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},";
652             pidl_code "\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);";
653             deindent;
654             pidl_code "}";
655
656                 $hf_used{"hf_$x->{NAME}_opnum"} = 1;
657         }
658 }
659
660 sub ProcessInterface($)
661 {
662         my ($x) = @_;
663
664         push(@{$conformance->{strip_prefixes}}, $x->{NAME});
665
666         my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
667         pidl_hdr "#ifndef $define";
668         pidl_hdr "#define $define";
669         pidl_hdr "";
670
671         if (defined $x->{PROPERTIES}->{depends}) {
672                 foreach (split / /, $x->{PROPERTIES}->{depends}) {
673                         next if($_ eq "security");
674                         pidl_hdr "#include \"packet-dcerpc-$_\.h\"\n";
675                 }
676         }
677
678         pidl_def "static gint proto_dcerpc_$x->{NAME} = -1;";
679         register_ett("ett_dcerpc_$x->{NAME}");
680         register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
681
682         if (defined($x->{UUID})) {
683                 my $if_uuid = $x->{UUID};
684
685             pidl_def "/* Version information */\n\n";
686             
687             pidl_def "static e_uuid_t uuid_dcerpc_$x->{NAME} = {";
688             pidl_def "\t0x" . substr($if_uuid, 1, 8) 
689                 . ", 0x" . substr($if_uuid, 10, 4)
690             . ", 0x" . substr($if_uuid, 15, 4) . ",";
691             pidl_def "\t{ 0x" . substr($if_uuid, 20, 2) 
692                 . ", 0x" . substr($if_uuid, 22, 2)
693             . ", 0x" . substr($if_uuid, 25, 2)
694             . ", 0x" . substr($if_uuid, 27, 2)
695             . ", 0x" . substr($if_uuid, 29, 2)
696             . ", 0x" . substr($if_uuid, 31, 2)
697             . ", 0x" . substr($if_uuid, 33, 2)
698             . ", 0x" . substr($if_uuid, 35, 2) . " }";
699             pidl_def "};";
700         
701             my $maj = $x->{VERSION};
702             $maj =~ s/\.(.*)$//g;
703             pidl_def "static guint16 ver_dcerpc_$x->{NAME} = $maj;";
704             pidl_def "";
705         }
706
707         Interface($x);
708
709         pidl_code "\n".DumpFunctionTable($x);
710
711         # Only register these two return types if they were actually used
712         if (defined($hf_used{"hf_$x->{NAME}_status"})) {
713                 register_hf_field("hf_$x->{NAME}_status", "Status", "$x->{NAME}.status", "FT_UINT32", "BASE_HEX", "VALS(NT_errors)", 0, "");
714         }
715
716         if (defined($hf_used{"hf_$x->{NAME}_werror"})) {
717                 register_hf_field("hf_$x->{NAME}_werror", "Windows Error", "$x->{NAME}.werror", "FT_UINT32", "BASE_HEX", "VALS(DOS_errors)", 0, "");
718         }
719
720         RegisterInterface($x);
721         RegisterInterfaceHandoff($x);
722
723         pidl_hdr "#endif /* $define */";
724 }
725
726 sub find_type($)
727 {
728         my $n = shift;
729
730         return $conformance->{types}->{$n};
731 }
732
733 sub register_type($$$$$$$)
734 {
735         my ($type,$call,$ft,$base,$mask,$vals,$length) = @_;
736
737         $conformance->{types}->{$type} = {
738                 NAME => $type,
739                 DISSECTOR_NAME => $call,
740                 FT_TYPE => $ft,
741                 BASE_TYPE => $base,
742                 MASK => $mask,
743                 VALSSTRING => $vals,
744                 ALIGNMENT => $length
745         };
746 }
747
748 # Loads the default types
749 sub Initialize($)
750 {
751         my $cnf_file = shift;
752
753         $conformance = {
754                 imports => {},
755                 header_fields=> {} 
756         };
757
758         ReadConformance($cnf_file, $conformance) or print "Warning: No conformance file `$cnf_file'\n";
759         
760         foreach my $bytes (qw(1 2 4 8)) {
761                 my $bits = $bytes * 8;
762                 register_type("uint$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@,NULL);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
763                 register_type("int$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
764         }
765                 
766         register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
767         register_type("bool8", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
768         register_type("char", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
769         register_type("long", "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
770         register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
771         register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
772         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);
773         register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
774         register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
775         register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "BASE_DEC", 0, "NULL", 4);
776         register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
777         register_type("SID", "
778                 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
779
780                 di->hf_index = \@HF\@;
781
782                 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
783         ","FT_STRING", "BASE_DEC", 0, "NULL", 4);
784         register_type("WERROR", 
785                 "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
786
787 }
788
789 #####################################################################
790 # Generate ethereal parser and header code
791 sub Parse($$$$)
792 {
793         my($ndr,$idl_file,$h_filename,$cnf_file) = @_;
794         Initialize($cnf_file);
795
796         return (undef, undef) if defined($conformance->{noemit_dissector});
797
798         $tabs = "";
799
800         %res = (code=>"",def=>"",hdr=>"");
801         @ett = ();
802
803         my $notice = 
804 "/* DO NOT EDIT
805         This filter was automatically generated
806         from $idl_file and $cnf_file.
807         
808         Pidl is a perl based IDL compiler for DCE/RPC idl files. 
809         It is maintained by the Samba team, not the Ethereal team.
810         Instructions on how to download and install Pidl can be 
811         found at http://wiki.ethereal.com/Pidl
812 */
813
814 ";
815
816         pidl_hdr $notice;
817
818         $res{headers} = "\n";
819         $res{headers} .= "#ifdef HAVE_CONFIG_H\n";
820         $res{headers} .= "#include \"config.h\"\n";
821         $res{headers} .= "#endif\n\n";
822         $res{headers} .= "#include <glib.h>\n";
823         $res{headers} .= "#include <string.h>\n";
824         $res{headers} .= "#include <epan/packet.h>\n\n";
825
826         $res{headers} .= "#include \"packet-dcerpc.h\"\n";
827         $res{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
828         $res{headers} .= "#include \"packet-windows-common.h\"\n";
829
830         use File::Basename;     
831         my $h_basename = basename($h_filename);
832
833         $res{headers} .= "#include \"$h_basename\"\n";
834         pidl_code "";
835
836         # Ethereal protocol registration
837
838         ProcessInterface($_) foreach (@$ndr);
839
840         $res{ett} = DumpEttDeclaration();
841         $res{hf} = DumpHfDeclaration();
842
843         my $parser = $notice;
844         $parser.= $res{headers};
845         $parser.=$res{ett};
846         $parser.=$res{hf};
847         $parser.=$res{def};
848         $parser.=$conformance->{override};
849         $parser.=$res{code};
850
851         my $header = "/* autogenerated by pidl */\n\n";
852         $header.=$res{hdr};
853
854         CheckUsed($conformance);
855     
856         return ($parser,$header);
857 }
858
859 ###############################################################################
860 # ETT
861 ###############################################################################
862
863 sub register_ett($)
864 {
865         my $name = shift;
866
867         push (@ett, $name);     
868 }
869
870 sub DumpEttList()
871 {
872         my $res = "\tstatic gint *ett[] = {\n";
873         foreach (@ett) {
874                 $res .= "\t\t&$_,\n";
875         }
876
877         return "$res\t};\n";
878 }
879
880 sub DumpEttDeclaration()
881 {
882         my $res = "\n/* Ett declarations */\n";
883         foreach (@ett) {
884                 $res .= "static gint $_ = -1;\n";
885         }
886
887         return "$res\n";
888 }
889
890 ###############################################################################
891 # HF
892 ###############################################################################
893
894 sub register_hf_field($$$$$$$$) 
895 {
896         my ($index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
897
898         if (defined ($conformance->{hf_renames}->{$index})) {
899                 $conformance->{hf_renames}->{$index}->{USED} = 1;
900                 return $conformance->{hf_renames}->{$index}->{NEWNAME};
901         }
902
903         $conformance->{header_fields}->{$index} = {
904                 INDEX => $index,
905                 NAME => $name,
906                 FILTER => $filter_name,
907                 FT_TYPE => $ft_type,
908                 BASE_TYPE => $base_type,
909                 VALSSTRING => $valsstring,
910                 MASK => $mask,
911                 BLURB => $blurb
912         };
913
914         if ((not defined($blurb) or $blurb eq "") and 
915                         defined($conformance->{fielddescription}->{$index})) {
916                 $conformance->{header_fields}->{$index}->{BLURB} = 
917                         $conformance->{fielddescription}->{$index}->{DESCRIPTION};
918                 $conformance->{fielddescription}->{$index}->{USED} = 1;
919         }
920
921         return $index;
922 }
923
924 sub DumpHfDeclaration()
925 {
926         my $res = "";
927
928         $res = "\n/* Header field declarations */\n";
929
930         foreach (keys %{$conformance->{header_fields}}) 
931         {
932                 $res .= "static gint $_ = -1;\n";
933         }
934
935         return "$res\n";
936 }
937
938 sub DumpHfList()
939 {
940         my $res = "\tstatic hf_register_info hf[] = {\n";
941
942         foreach (values %{$conformance->{header_fields}}) 
943         {
944                 $res .= "\t{ &$_->{INDEX}, 
945           { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str($_->{BLURB}).", HFILL }},
946 ";
947         }
948
949         return $res."\t};\n";
950 }
951
952
953 ###############################################################################
954 # Function table
955 ###############################################################################
956
957 sub DumpFunctionTable($)
958 {
959         my $if = shift;
960
961         my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
962         foreach (@{$if->{FUNCTIONS}}) {
963                 my $fn_name = $_->{NAME};
964                 $fn_name =~ s/^$if->{NAME}_//;
965                 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
966                 $res.= "\t   $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
967         }
968
969         $res .= "\t{ 0, NULL, NULL, NULL }\n";
970
971         return "$res};\n";
972 }
973
974 sub CheckUsed($)
975 {
976         my $conformance = shift;
977         foreach (values %{$conformance->{header_fields}}) {
978                 if (not defined($hf_used{$_->{INDEX}})) {
979                         print "$_->{POS}: warning: hf field `$_->{INDEX}' not used\n";
980                 }
981         }
982
983         foreach (values %{$conformance->{hf_renames}}) {
984                 if (not $_->{USED}) {
985                         print "$_->{POS}: warning: hf field `$_->{OLDNAME}' not used\n";
986                 }
987         }
988
989         foreach (values %{$conformance->{dissectorparams}}) {
990                 if (not $_->{USED}) {
991                         print "$_->{POS}: warning: dissector param never used\n";
992                 }
993         }
994
995         foreach (values %{$conformance->{imports}}) {
996                 if (not $_->{USED}) {
997                         print "$_->{POS}: warning: import never used\n";
998                 }
999         }
1000
1001         foreach (values %{$conformance->{types}}) {
1002                 if (not $_->{USED} and defined($_->{POS})) {
1003                         print "$_->{POS}: warning: type never used\n";
1004                 }
1005         }
1006
1007         foreach (values %{$conformance->{fielddescription}}) {
1008                 if (not $_->{USED}) {
1009                         print "$_->{POS}: warning: description never used\n";
1010                 }
1011         }
1012 }
1013
1014 1;