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