89cbf84cd90dfdb0c261b5555bb6e93f47c2fec4
[amitay/samba.git] / pidl / lib / Parse / Pidl / Wireshark / NDR.pm
1 ##################################################
2 # Wireshark 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-2007
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::Wireshark::NDR - Parser generator for Wireshark
14
15 =cut
16
17 package Parse::Pidl::Wireshark::NDR;
18
19 use Exporter;
20 @ISA = qw(Exporter);
21 @EXPORT_OK = qw(field2name %res PrintIdl StripPrefixes RegisterInterfaceHandoff register_hf_field CheckUsed ProcessImport ProcessInclude find_type DumpEttList DumpEttDeclaration DumpHfList DumpHfDeclaration DumpFunctionTable register_type register_ett);
22
23 use strict;
24 use Parse::Pidl qw(error warning);
25 use Parse::Pidl::Typelist qw(getType);
26 use Parse::Pidl::Util qw(has_property property_matches make_str);
27 use Parse::Pidl::NDR qw(ContainsString GetNextLevel);
28 use Parse::Pidl::Dump qw(DumpType DumpFunction);
29 use Parse::Pidl::Wireshark::Conformance qw(ReadConformance);
30 use File::Basename;     
31
32 use vars qw($VERSION);
33 $VERSION = '0.01';
34
35 my %return_types = ();
36 my %dissector_used = ();
37
38 my %ptrtype_mappings = (
39         "unique" => "NDR_POINTER_UNIQUE",
40         "ref" => "NDR_POINTER_REF",
41         "ptr" => "NDR_POINTER_PTR"
42 );
43
44 sub StripPrefixes($$)
45 {
46         my ($s, $prefixes) = @_;
47
48         foreach (@$prefixes) {
49                 $s =~ s/^$_\_//g;
50         }
51
52         return $s;
53 }
54
55 # Convert a IDL structure field name (e.g access_mask) to a prettier
56 # string like 'Access Mask'.
57
58 sub field2name($)
59 {
60     my($field) = shift;
61
62     $field =~ s/_/ /g;          # Replace underscores with spaces
63     $field =~ s/(\w+)/\u\L$1/g; # Capitalise each word
64     
65     return $field;
66 }
67
68 sub new($)
69 {
70         my ($class) = @_;
71         my $self = {res => {hdr => "", def => "", code => ""}, tabs => "", cur_fn => undef,
72                 hf_used => {}, ett => [], conformance => undef
73
74         };
75         bless($self, $class);
76 }
77
78 sub pidl_fn_start($$)
79 {
80         my ($self, $fn) = @_;
81         $self->{cur_fn} = $fn;
82 }
83 sub pidl_fn_end($$)
84 {
85         my ($self, $fn) = @_;
86         die("Inconsistent state: $fn != $self->{cur_fn}") if ($fn ne $self->{cur_fn});
87         $self->{cur_fn} = undef;
88 }
89
90 sub pidl_code($$)
91 {
92         my ($self, $d) = @_;
93         return if (defined($self->{cur_fn}) and defined($self->{conformance}->{manual}->{$self->{cur_fn}}));
94  
95         if ($d) {
96                 $self->{res}->{code} .= $self->{tabs};
97                 $self->{res}->{code} .= $d;
98         }
99         $self->{res}->{code} .="\n";
100 }
101
102 sub pidl_hdr($$) { my ($self,$x) = @_; $self->{res}->{hdr} .= "$x\n"; }
103 sub pidl_def($$) { my ($self,$x) = @_; $self->{res}->{def} .= "$x\n"; }
104
105 sub indent($)
106 {
107         my ($self) = @_;
108         $self->{tabs} .= "\t";
109 }
110
111 sub deindent($)
112 {
113         my ($self) = @_;
114         $self->{tabs} = substr($self->{tabs}, 0, -1);
115 }
116
117 sub PrintIdl($$)
118 {
119         my ($self, $idl) = @_;
120
121         foreach (split /\n/, $idl) {
122                 $self->pidl_code("/* IDL: $_ */");
123         }
124
125         $self->pidl_code("");
126 }
127
128 #####################################################################
129 # parse the interface definitions
130 sub Interface($$)
131 {
132         my($self, $interface) = @_;
133         $self->Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}});
134         $self->Type($_, $_->{NAME}, $interface->{NAME}) foreach (@{$interface->{TYPES}});
135         $self->Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
136 }
137
138 sub Enum($$$$)
139 {
140         my ($self, $e,$name,$ifname) = @_;
141         my $valsstring = "$ifname\_$name\_vals";
142         my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
143
144         return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
145
146         foreach (@{$e->{ELEMENTS}}) {
147                 if (/([^=]*)=(.*)/) {
148                         $self->pidl_hdr("#define $1 ($2)");
149                 }
150         }
151         
152         $self->pidl_hdr("extern const value_string $valsstring\[];");
153         $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_, int hf_index _U_, g$e->{BASE_TYPE} *param _U_);");
154
155         $self->pidl_def("const value_string ".$valsstring."[] = {");
156         foreach (@{$e->{ELEMENTS}}) {
157                 next unless (/([^=]*)=(.*)/);
158                 $self->pidl_def("\t{ $1, \"$1\" },");
159         }
160
161         $self->pidl_def("{ 0, NULL }");
162         $self->pidl_def("};");
163
164         $self->pidl_fn_start($dissectorname);
165         $self->pidl_code("int");
166         $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_, int hf_index _U_, g$e->{BASE_TYPE} *param _U_)");
167         $self->pidl_code("{");
168         $self->indent;
169         $self->pidl_code("g$e->{BASE_TYPE} parameter=0;");
170         $self->pidl_code("if (param) {");
171         $self->indent;
172         $self->pidl_code("parameter = *param;");
173         $self->deindent;
174         $self->pidl_code("}");
175         $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, &parameter);");
176         $self->pidl_code("if (param) {");
177         $self->indent;
178         $self->pidl_code("*param = parameter;");
179         $self->deindent;
180         $self->pidl_code("}");
181         $self->pidl_code("return offset;");
182         $self->deindent;
183         $self->pidl_code("}\n");
184         $self->pidl_fn_end($dissectorname);
185
186         my $enum_size = $e->{BASE_TYPE};
187         $enum_size =~ s/uint//g;
188         $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
189 }
190
191 sub Pipe($$$$)
192 {
193         my ($self,$e,$name,$ifname) = @_;
194         error($e->{ORIGINAL}, "Pipe not yet supported");
195         return;
196 }
197
198 sub Bitmap($$$$)
199 {
200         my ($self,$e,$name,$ifname) = @_;
201         my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
202
203         $self->register_ett("ett_$ifname\_$name");
204
205         $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_);");
206
207         $self->pidl_fn_start($dissectorname);
208         $self->pidl_code("int");
209         $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_)");
210         $self->pidl_code("{");
211         $self->indent;
212         $self->pidl_code("proto_item *item = NULL;");
213         $self->pidl_code("proto_tree *tree = NULL;");
214         $self->pidl_code("");
215                 
216         $self->pidl_code("g$e->{BASE_TYPE} flags;");
217         if ($e->{ALIGN} > 1) {
218                 $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
219         }
220
221         $self->pidl_code("");
222
223         $self->pidl_code("if (parent_tree) {");
224         $self->indent;
225         $self->pidl_code("item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, DREP_ENC_INTEGER(drep));");
226         $self->pidl_code("tree = proto_item_add_subtree(item,ett_$ifname\_$name);");
227         $self->deindent;
228         $self->pidl_code("}\n");
229
230         $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, NULL, drep, -1, &flags);");
231
232         $self->pidl_code("proto_item_append_text(item, \": \");\n");
233         $self->pidl_code("if (!flags)");
234         $self->pidl_code("\tproto_item_append_text(item, \"(No values set)\");\n");
235
236         foreach (@{$e->{ELEMENTS}}) {
237                 next unless (/([^ ]*) (.*)/);
238                 my ($en,$ev) = ($1,$2);
239                 my $hf_bitname = "hf_$ifname\_$name\_$en";
240                 my $filtername = "$ifname\.$name\.$en";
241
242                 $self->{hf_used}->{$hf_bitname} = 1;
243                 
244                 $self->register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", $ev, "");
245
246                 $self->pidl_def("static const true_false_string $name\_$en\_tfs = {");
247                 if (defined($self->{conformance}->{tfs}->{$hf_bitname})) {
248                         $self->pidl_def("   $self->{conformance}->{tfs}->{$hf_bitname}->{TRUE_STRING},");
249                         $self->pidl_def("   $self->{conformance}->{tfs}->{$hf_bitname}->{FALSE_STRING},");
250                         $self->{conformance}->{tfs}->{$hf_bitname}->{USED} = 1;
251                 } else {
252                         $self->pidl_def("   \"$en is SET\",");
253                         $self->pidl_def("   \"$en is NOT SET\",");
254                 }
255                 $self->pidl_def("};");
256                 
257                 $self->pidl_code("proto_tree_add_boolean(tree, $hf_bitname, tvb, offset-$e->{ALIGN}, $e->{ALIGN}, flags);");
258                 $self->pidl_code("if (flags&$ev){");
259                 $self->pidl_code("\tproto_item_append_text(item, \"$en\");");
260                 $self->pidl_code("\tif (flags & (~$ev))");
261                 $self->pidl_code("\t\tproto_item_append_text(item, \", \");");
262                 $self->pidl_code("}");
263                 $self->pidl_code("flags&=(~$ev);");
264                 $self->pidl_code("");
265         }
266
267         $self->pidl_code("if (flags) {");
268         $self->pidl_code("\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);");
269         $self->pidl_code("}\n");
270         $self->pidl_code("return offset;");
271         $self->deindent;
272         $self->pidl_code("}\n");
273         $self->pidl_fn_end($dissectorname);
274
275         my $size = $e->{BASE_TYPE};
276         $size =~ s/uint//g;
277         $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8);
278 }
279
280 sub ElementLevel($$$$$$$$)
281 {
282         my ($self,$e,$l,$hf,$myname,$pn,$ifname,$param) = @_;
283
284         if (defined($self->{conformance}->{dissectorparams}->{$myname})) {
285                 $param = $self->{conformance}->{dissectorparams}->{$myname}->{PARAM};
286         }
287
288         if ($l->{TYPE} eq "POINTER") {
289                 my $type;
290                 if ($l->{LEVEL} eq "TOP") {
291                         $type = "toplevel";
292                 } elsif ($l->{LEVEL} eq "EMBEDDED") {
293                         $type = "embedded";
294                 }
295                 $self->pidl_code("offset = dissect_ndr_$type\_pointer(tvb, offset, pinfo, tree, drep, $myname\_, $ptrtype_mappings{$l->{POINTER_TYPE}}, \"Pointer to ".field2name(StripPrefixes($e->{NAME}, $self->{conformance}->{strip_prefixes})) . " ($e->{TYPE})\",$hf);");
296         } elsif ($l->{TYPE} eq "ARRAY") {
297                 if ($l->{IS_INLINE}) {
298                         error($e->{ORIGINAL}, "Inline arrays not supported");
299                 } elsif ($l->{IS_FIXED}) {
300                         $self->pidl_code("int i;");
301                         $self->pidl_code("for (i = 0; i < $l->{SIZE_IS}; i++)");
302                         $self->pidl_code("\toffset = $myname\_(tvb, offset, pinfo, tree, drep);");
303                 } else {
304                         my $type = "";
305                         $type .= "c" if ($l->{IS_CONFORMANT});
306                         $type .= "v" if ($l->{IS_VARYING});
307
308                         unless ($l->{IS_ZERO_TERMINATED}) {
309                                 $self->pidl_code("offset = dissect_ndr_u" . $type . "array(tvb, offset, pinfo, tree, drep, $myname\_);");
310                         } else {
311                                 my $nl = GetNextLevel($e,$l);
312                                 $self->pidl_code("char *data;");
313                                 $self->pidl_code("");
314                                 $self->pidl_code("offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, drep, sizeof(g$nl->{DATA_TYPE}), $hf, FALSE, &data);");
315                                 $self->pidl_code("proto_item_append_text(tree, \": %s\", data);");
316                         }
317                 }
318         } elsif ($l->{TYPE} eq "DATA") {
319                 if ($l->{DATA_TYPE} eq "string") {
320                         my $bs = 2; # Byte size defaults to that of UCS2
321
322
323                         ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
324                         
325                         if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
326                                 $self->pidl_code("char *data;\n");
327                                 $self->pidl_code("offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, &data);");
328                                 $self->pidl_code("proto_item_append_text(tree, \": %s\", data);");
329                         } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
330                                 $self->pidl_code("offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, NULL);");
331                         } elsif (property_matches($e, "flag", ".*STR_NULLTERM.*")) {
332                                 if ($bs == 2) {
333                                         $self->pidl_code("offset = dissect_null_term_wstring(tvb, offset, pinfo, tree, drep, $hf , 0);")
334                                 } else {
335                                         $self->pidl_code("offset = dissect_null_term_string(tvb, offset, pinfo, tree, drep, $hf , 0);")
336                                 }
337                         } else {
338                                 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
339                         }
340                 } elsif ($l->{DATA_TYPE} eq "DATA_BLOB") {
341                         my $remain = 0;
342                         $remain = 1 if (property_matches($e->{ORIGINAL}, "flag", ".*LIBNDR_FLAG_REMAINING.*"));
343                         $self->pidl_code("offset = dissect_ndr_datablob(tvb, offset, pinfo, tree, drep, $hf, $remain);");
344                 } else {
345                         my $call;
346
347                         if ($self->{conformance}->{imports}->{$l->{DATA_TYPE}}) {
348                                 $call = $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{DATA};
349                                 $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
350
351                         } elsif (defined($self->{conformance}->{imports}->{"$pn.$e->{NAME}"})) {
352                                 $call = $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{DATA};
353                                 $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
354
355                         } elsif (defined($self->{conformance}->{types}->{$l->{DATA_TYPE}})) {
356                                 $call= $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
357                                 $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
358                         } else {
359                                 my $t;
360                                 if (ref($l->{DATA_TYPE}) eq "HASH" ) {
361                                         $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
362                                 } else {
363                                         $t = $l->{DATA_TYPE};
364                                 }
365
366                                 $self->pidl_code("offset = $ifname\_dissect_struct_" . $t . "(tvb,offset,pinfo,tree,drep,$hf,$param);");
367
368                                 return;
369                         }
370
371                         $call =~ s/\@HF\@/$hf/g;
372                         $call =~ s/\@PARAM\@/$param/g;
373                         $self->pidl_code($call);
374                 }
375         } elsif ($_->{TYPE} eq "SUBCONTEXT") {
376                 my $varswitch;
377                 if (has_property($e, "switch_is")) {
378                         $varswitch = $e->{PROPERTIES}->{switch_is};
379                 }
380                 my $num_bits = ($l->{HEADER_SIZE}*8);
381                 my $hf2 = $self->register_hf_field($hf."_", "Subcontext length", "$ifname.$pn.$_->{NAME}subcontext", "FT_UINT$num_bits", "BASE_HEX", "NULL", 0, "");
382                 $num_bits = 3264 if ($num_bits == 32);
383                 $self->{hf_used}->{$hf2} = 1;
384                 $self->pidl_code("dcerpc_info *di = (dcerpc_info*)pinfo->private_data;");
385                 $self->pidl_code("guint$num_bits size;");
386                 $self->pidl_code("int conformant = di->conformant_run;");
387                 $self->pidl_code("tvbuff_t *subtvb;");
388                 $self->pidl_code("");
389                 # We need to be able to dissect the length of the context in every case
390                 # and conformant run skips the dissections of scalars ...
391                 $self->pidl_code("if (!conformant) {");
392                 $self->indent;
393                 $self->pidl_code("guint32 saved_flags = di->call_data->flags;");
394                 $self->pidl_code("offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf2, &size);");
395                 # This is a subcontext, there is normally no such thing as
396                 # 64 bit NDR is subcontext so we clear the flag so that we can
397                 # continue to dissect handmarshalled stuff with pidl
398                 $self->pidl_code("di->call_data->flags &= ~DCERPC_IS_NDR64;");
399
400                 $self->pidl_code("subtvb = tvb_new_subset(tvb, offset, size, -1);");
401                 if ($param ne 0) {
402                         $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, drep, $param);");
403                 } else {
404                         $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, drep);");
405                 }
406                 $self->pidl_code("offset += size;");
407                 $self->pidl_code("di->call_data->flags = saved_flags;");
408                 $self->deindent;
409                 $self->pidl_code("}");
410         } elsif ($_->{TYPE} eq "PIPE") {
411                 error($e->{ORIGINAL}, "Type PIPE not yet supported");
412         } else {
413                 die("Unknown type `$_->{TYPE}'");
414         }
415 }
416
417 sub SwitchType($$;$)
418 {
419         my ($e, $type, $nodiscriminant) = @_;
420
421         my $switch_dt =  getType($type);
422         my $switch_type = undef;
423         if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
424                 $switch_type = Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
425         } elsif ($switch_dt->{DATA}->{TYPE} eq "BITMAP") {
426                 $switch_type = Parse::Pidl::Typelist::bitmap_type_fn($switch_dt->{DATA});
427         } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
428                 if (defined $e->{SWITCH_TYPE}) {
429                         $switch_type = "$e->{SWITCH_TYPE}";
430                 } else {
431                         $switch_type = "$switch_dt->{DATA}->{NAME}";
432                 }
433         } elsif (not defined $e->{SWITCH_TYPE}) {
434                 $switch_type = $nodiscriminant;
435         }
436
437         return $switch_type
438 }
439
440 sub Element($$$$$)
441 {
442         my ($self,$e,$pn,$ifname,$isoruseswitch) = @_;
443
444         my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn, $self->{conformance}->{strip_prefixes})."\_".StripPrefixes($e->{NAME}, $self->{conformance}->{strip_prefixes});
445
446         my ($call_code, $moreparam);
447         my $param = 0;
448         if (defined $isoruseswitch) {
449                 my $type = $isoruseswitch->[0];
450                 my $name = $isoruseswitch->[1];
451
452                 my $switch_raw_type = SwitchType($e, $type, "uint32");
453                 if (not defined($switch_raw_type)) {
454                         die("Unknown type[$type]\n");
455                 }
456                 my $switch_type = "g${switch_raw_type}";
457
458                 $moreparam = ", $switch_type *".$name;
459                 $param = $name;
460                 $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep, &$name);";
461         } else {
462                 $moreparam = "";
463                 $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
464         }
465
466
467         my $type = $self->find_type($e->{TYPE});
468
469         if (not defined($type)) {
470                 # default settings
471                 $type = {
472                         MASK => 0,
473                         VALSSTRING => "NULL",
474                         FT_TYPE => "FT_NONE",
475                         BASE_TYPE => "BASE_NONE"
476                 };
477         }
478
479         if (ContainsString($e)) {
480                 $type = {
481                         MASK => 0,
482                         VALSSTRING => "NULL",
483                         FT_TYPE => "FT_STRING",
484                         BASE_TYPE => "BASE_NONE"
485                 };
486         }
487         if (property_matches($e, "flag", ".*LIBNDR_FLAG_ALIGN.*")) {
488                 my $align_flag = $e->{PROPERTIES}->{flag};
489                 if ($align_flag =~ m/LIBNDR_FLAG_ALIGN(\d+)/) {
490                         $call_code = "ALIGN_TO_$1_BYTES; ".$call_code;
491                 }
492         }
493
494         my $hf = $self->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}, "");
495         $self->{hf_used}->{$hf} = 1;
496
497         my $eltname = StripPrefixes($pn, $self->{conformance}->{strip_prefixes}) . ".$e->{NAME}";
498         if (defined($self->{conformance}->{noemit}->{$eltname})) {
499                 return $call_code;
500         }
501
502         my $add = "";
503
504         my $oldparam = undef;
505         foreach (@{$e->{LEVELS}}) {
506                 if (defined $_->{SWITCH_IS}) {
507                         $oldparam = $param;
508                         $param = "*$param";
509                 }
510                 next if ($_->{TYPE} eq "SWITCH");
511                 $self->pidl_def("static int $dissectorname$add(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_$moreparam);");
512                 $self->pidl_fn_start("$dissectorname$add");
513                 $self->pidl_code("static int");
514                 $self->pidl_code("$dissectorname$add(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_$moreparam)");
515                 $self->pidl_code("{");
516                 $self->indent;
517
518                 $self->ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname,$param);
519                 if (defined $oldparam) {
520                         $param = $oldparam;
521                 }
522
523                 $self->pidl_code("");
524                 $self->pidl_code("return offset;");
525                 $self->deindent;
526                 $self->pidl_code("}\n");
527                 $self->pidl_fn_end("$dissectorname$add");
528                 $add.="_";
529                 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
530         }
531
532         return $call_code;
533 }
534
535 sub Function($$$)
536 {
537         my ($self, $fn,$ifname) = @_;
538
539         my %dissectornames;
540
541         foreach (@{$fn->{ELEMENTS}}) {
542             $dissectornames{$_->{NAME}} = $self->Element($_, $fn->{NAME}, $ifname, undef) if not defined($dissectornames{$_->{NAME}});
543         }
544         
545         my $fn_name = $_->{NAME};
546         $fn_name =~ s/^${ifname}_//;
547
548         $self->PrintIdl(DumpFunction($fn->{ORIGINAL}));
549         $self->pidl_fn_start("$ifname\_dissect\_$fn_name\_response");
550         $self->pidl_code("static int");
551         $self->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_)");
552         $self->pidl_code("{");
553         $self->indent;
554         if ( not defined($fn->{RETURN_TYPE})) {
555         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR") 
556         {
557                 $self->pidl_code("guint32 status;\n");
558         } elsif (my $type = getType($fn->{RETURN_TYPE})) {
559                 if ($type->{DATA}->{TYPE} eq "ENUM") {
560                         $self->pidl_code("g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA}) . " status;\n");
561                 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
562                         $self->pidl_code("g$fn->{RETURN_TYPE} status;\n");
563                 } else {
564                 error($fn, "return type `$fn->{RETURN_TYPE}' not yet supported");
565                 }
566         } else {
567                 error($fn, "unknown return type `$fn->{RETURN_TYPE}'");
568         }
569
570         $self->pidl_code("pinfo->dcerpc_procedure_name=\"${fn_name}\";");
571         foreach (@{$fn->{ELEMENTS}}) {
572                 if (grep(/out/,@{$_->{DIRECTION}})) {
573                         $self->pidl_code("$dissectornames{$_->{NAME}}");
574                         $self->pidl_code("offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);");
575                         $self->pidl_code("");
576                 }
577         }
578
579         if (not defined($fn->{RETURN_TYPE})) {
580         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
581                 $self->pidl_code("offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n");
582                 $self->pidl_code("if (status != 0)");
583                 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n");
584                 $return_types{$ifname}->{"status"} = ["NTSTATUS", "NT Error"];
585         } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
586                 $self->pidl_code("offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &status);\n");
587                 $self->pidl_code("if (status != 0)");
588                 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, WERR_errors, \"Unknown DOS error 0x%08x\"));\n");
589                 
590                 $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"];
591         } elsif (my $type = getType($fn->{RETURN_TYPE})) {
592                 if ($type->{DATA}->{TYPE} eq "ENUM") {
593                         my $return_type = "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
594                         my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
595
596                         $self->pidl_code("offset = $return_dissect(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);");
597                         $self->pidl_code("if (status != 0)");
598                         $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %s\", val_to_str(status, $ifname\_$fn->{RETURN_TYPE}\_vals, \"Unknown " . $fn->{RETURN_TYPE} . " error 0x%08x\"));\n");
599                         $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
600                 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
601                         $self->pidl_code("offset = dissect_ndr_$fn->{RETURN_TYPE}(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);");
602                         $self->pidl_code("if (status != 0)");
603                         $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %d\", status);\n");
604                         $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
605                 }
606         }
607
608         $self->pidl_code("return offset;");
609         $self->deindent;
610         $self->pidl_code("}\n");
611         $self->pidl_fn_end("$ifname\_dissect\_$fn_name\_response");
612
613         $self->pidl_fn_start("$ifname\_dissect\_$fn_name\_request");
614         $self->pidl_code("static int");
615         $self->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_)");
616         $self->pidl_code("{");
617         $self->indent;
618         $self->pidl_code("pinfo->dcerpc_procedure_name=\"${fn_name}\";");
619         foreach (@{$fn->{ELEMENTS}}) {
620                 if (grep(/in/,@{$_->{DIRECTION}})) {
621                         $self->pidl_code("$dissectornames{$_->{NAME}}");
622                         $self->pidl_code("offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);");
623                 }
624
625         }
626
627         $self->pidl_code("return offset;");
628         $self->deindent;
629         $self->pidl_code("}\n");
630         $self->pidl_fn_end("$ifname\_dissect\_$fn_name\_request");
631 }
632
633 sub Struct($$$$)
634 {
635         my ($self,$e,$name,$ifname) = @_;
636         my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
637
638         return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
639
640         $self->register_ett("ett_$ifname\_$name");
641
642         my $res = "";
643         my $varswitchs = {};
644         # will contain the switch var declaration;
645         my $vars = [];
646         foreach (@{$e->{ELEMENTS}}) {
647                 if (has_property($_, "switch_is")) {
648                         $varswitchs->{$_->{PROPERTIES}->{switch_is}} = [];
649                 }
650         }
651         foreach (@{$e->{ELEMENTS}}) {
652                 my $switch_info = undef;
653
654                 my $v = $_->{NAME};
655                 if (scalar(grep {/^$v$/} keys(%$varswitchs)) == 1) {
656                         # This element is one of the switch attribute
657                         my $switch_raw_type = SwitchType($e, $_->{TYPE}, "uint32");
658                         if (not defined($switch_raw_type)) {
659                                 die("Unknown type[$_->{TYPE}]\n");
660                         }
661                         my $switch_type = "g${switch_raw_type}";
662
663                         push @$vars, "$switch_type $v;";
664                         $switch_info = [ $_->{TYPE}, $v ];
665                         $varswitchs->{$v} = $switch_info;
666                 }
667
668                 if (has_property($_, "switch_is")) {
669                         my $varswitch = $_->{PROPERTIES}->{switch_is};
670                         $switch_info = $varswitchs->{$varswitch};
671                 }
672
673                 $res.="\t".$self->Element($_, $name, $ifname, $switch_info)."\n\n";
674         }
675
676         my $doalign = undef;
677         if ($e->{ALIGN} > 1 and not property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) {
678                 $doalign = 1;
679         } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) {
680                 $doalign = 0;
681         }
682
683         $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_);");
684
685         $self->pidl_fn_start($dissectorname);
686         $self->pidl_code("int");
687         $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_)");
688         $self->pidl_code("{");
689         $self->indent;
690         $self->pidl_code($_) foreach (@$vars);
691         $self->pidl_code("proto_item *item = NULL;");
692         $self->pidl_code("proto_tree *tree = NULL;");
693         if (defined($doalign)) {
694                 $self->pidl_code("dcerpc_info *di = (dcerpc_info *)pinfo->private_data;");
695                 if ($doalign == 0) {
696                         $self->pidl_code("gboolean oldalign = di->no_align;");
697                 }
698         }
699         $self->pidl_code("int old_offset;");
700         $self->pidl_code("");
701
702         if (defined($doalign)) {
703                 if ($doalign == 1) {
704                         $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
705                 }
706                 if ($doalign == 0) {
707                         $self->pidl_code("di->no_align = TRUE;");
708                 }
709                 $self->pidl_code("");
710         }
711
712         $self->pidl_code("old_offset = offset;");
713         $self->pidl_code("");
714         $self->pidl_code("if (parent_tree) {");
715         $self->indent;
716         $self->pidl_code("item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, ENC_NA);");
717         $self->pidl_code("tree = proto_item_add_subtree(item, ett_$ifname\_$name);");
718         $self->deindent;
719         $self->pidl_code("}");
720
721         $self->pidl_code("\n$res");
722
723         $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n");
724         if (defined($doalign) and $doalign == 1) {
725                 $self->pidl_code("");
726                 $self->pidl_code("if (di->call_data->flags & DCERPC_IS_NDR64) {");
727                 $self->indent;
728                 $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
729                 $self->deindent;
730                 $self->pidl_code("}");
731         }
732         if (defined($doalign) and $doalign == 0) {
733                 $self->pidl_code("");
734                 $self->pidl_code("di->no_align = oldalign;");
735         }
736         $self->pidl_code("");
737         $self->pidl_code("return offset;");
738         $self->deindent;
739         $self->pidl_code("}\n");
740         $self->pidl_fn_end($dissectorname);
741
742         $self->register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
743 }
744
745 sub Union($$$$)
746 {
747         my ($self,$e,$name,$ifname) = @_;
748
749         my $dissectorname = "$ifname\_dissect_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
750
751         return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
752         
753         $self->register_ett("ett_$ifname\_$name");
754
755         my $res = "";
756         foreach (@{$e->{ELEMENTS}}) {
757                 $res.="\n\t\t$_->{CASE}:\n";
758                 if ($_->{TYPE} ne "EMPTY") {
759                         $res.="\t\t\t".$self->Element($_, $name, $ifname, undef)."\n";
760                 }
761                 $res.="\t\tbreak;\n";
762         }
763
764         my $switch_type = undef;
765         my $switch_dissect = undef;
766         my $switch_raw_type = SwitchType($e, $e->{SWITCH_TYPE});
767         if (defined($switch_raw_type)) {
768                 $switch_type = "g${switch_raw_type}";
769                 $switch_dissect = "dissect_ndr_${switch_raw_type}";
770         }
771
772         $self->pidl_fn_start($dissectorname);
773         $self->pidl_code("static int");
774         $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_)");
775         $self->pidl_code("{");
776         $self->indent;
777         $self->pidl_code("proto_item *item = NULL;");
778         $self->pidl_code("proto_tree *tree = NULL;");
779         $self->pidl_code("int old_offset;");
780         if (!defined $switch_type) {
781                 $self->pidl_code("guint32 level = param;");
782         } else {
783                 $self->pidl_code("$switch_type level;");
784         }
785         $self->pidl_code("");
786
787         $self->pidl_code("old_offset = offset;");
788         $self->pidl_code("if (parent_tree) {");
789         $self->indent;
790         $self->pidl_code("item = proto_tree_add_text(parent_tree, tvb, offset, -1, \"$name\");");
791         $self->pidl_code("tree = proto_item_add_subtree(item, ett_$ifname\_$name);");
792         $self->deindent;
793         $self->pidl_code("}");
794
795         $self->pidl_code("");
796
797         if (defined $switch_type) {
798                 $self->pidl_code("offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);");
799
800                 if ($e->{ALIGN} > 1) {
801                         $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
802                         $self->pidl_code("");
803                 }
804         }
805
806
807         $self->pidl_code("switch(level) {$res\t}");
808         $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n");
809         $self->pidl_code("");
810
811         $self->pidl_code("return offset;");
812         $self->deindent;
813         $self->pidl_code("}");
814         $self->pidl_fn_end($dissectorname);
815
816         $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
817 }
818
819 sub Const($$$)
820 {
821         my ($self,$const,$ifname) = @_;
822         
823         if (!defined($const->{ARRAY_LEN}[0])) {
824                 $self->pidl_hdr("#define $const->{NAME}\t( $const->{VALUE} )\n");
825         } else {
826                 $self->pidl_hdr("#define $const->{NAME}\t $const->{VALUE}\n");
827         }
828 }
829
830 sub Typedef($$$$)
831 {
832         my ($self,$e,$name,$ifname) = @_;
833
834         $self->Type($e->{DATA}, $name, $ifname);
835 }
836
837 sub Type($$$$)
838 {
839         my ($self, $e, $name, $ifname) = @_;
840
841         $self->PrintIdl(DumpType($e->{ORIGINAL}));
842         {
843                 ENUM => \&Enum,
844                 STRUCT => \&Struct,
845                 UNION => \&Union,
846                 BITMAP => \&Bitmap,
847                 TYPEDEF => \&Typedef,
848                 PIPE    => \&Pipe
849         }->{$e->{TYPE}}->($self, $e, $name, $ifname);
850 }
851
852 sub RegisterInterface($$)
853 {
854         my ($self, $x) = @_;
855
856         $self->pidl_fn_start("proto_register_dcerpc_$x->{NAME}");
857         $self->pidl_code("void proto_register_dcerpc_$x->{NAME}(void)");
858         $self->pidl_code("{");
859         $self->indent;
860
861         $self->{res}->{code}.=$self->DumpHfList()."\n";
862         $self->{res}->{code}.="\n".DumpEttList($self->{ett})."\n";
863         
864         if (defined($x->{UUID})) {
865             # These can be changed to non-pidl_code names if the old dissectors
866             # in epan/dissctors are deleted.
867     
868             my $name = uc($x->{NAME}) . " (pidl)";
869             my $short_name = uc($x->{NAME});
870             my $filter_name = $x->{NAME};
871
872             if (has_property($x, "helpstring")) {
873                 $name = $x->{PROPERTIES}->{helpstring};
874             }
875
876             if (defined($self->{conformance}->{protocols}->{$x->{NAME}})) {
877                 $short_name = $self->{conformance}->{protocols}->{$x->{NAME}}->{SHORTNAME};
878                 $name = $self->{conformance}->{protocols}->{$x->{NAME}}->{LONGNAME};
879                 $filter_name = $self->{conformance}->{protocols}->{$x->{NAME}}->{FILTERNAME};
880             }
881
882             $self->pidl_code("proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");");
883             
884             $self->pidl_code("proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));");
885             $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));");
886         } else {
887             $self->pidl_code("proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");");
888             $self->pidl_code("proto_register_field_array(proto_dcerpc, hf, array_length(hf));");
889             $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));");
890         }
891             
892         $self->deindent;
893         $self->pidl_code("}\n");
894         $self->pidl_fn_end("proto_register_dcerpc_$x->{NAME}");
895 }
896
897 sub RegisterInterfaceHandoff($$)
898 {
899         my ($self,$x) = @_;
900
901         if (defined($x->{UUID})) {
902                 $self->pidl_fn_start("proto_reg_handoff_dcerpc_$x->{NAME}");
903             $self->pidl_code("void proto_reg_handoff_dcerpc_$x->{NAME}(void)");
904             $self->pidl_code("{");
905             $self->indent;
906             $self->pidl_code("dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},");
907             $self->pidl_code("\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},");
908             $self->pidl_code("\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);");
909             $self->deindent;
910             $self->pidl_code("}");
911                 $self->pidl_fn_end("proto_reg_handoff_dcerpc_$x->{NAME}");
912
913                 $self->{hf_used}->{"hf_$x->{NAME}_opnum"} = 1;
914         }
915 }
916
917 sub ProcessInclude
918 {
919         my $self = shift;
920         my @includes = @_;
921         foreach (@includes) {
922                 $self->pidl_hdr("#include \"$_\"");
923         }
924         $self->pidl_hdr("");
925 }
926
927 sub ProcessImport
928 {
929         my $self = shift;
930         my @imports = @_;
931         foreach (@imports) {
932                 next if($_ eq "security");
933                 s/^\"//;
934                 s/\.idl"?$//;
935                 $self->pidl_hdr("#include \"packet-dcerpc-$_\.h\"");
936         }
937         $self->pidl_hdr("");
938 }
939
940 sub ProcessInterface($$)
941 {
942         my ($self, $x) = @_;
943
944         push(@{$self->{conformance}->{strip_prefixes}}, $x->{NAME});
945
946         my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
947         $self->pidl_hdr("#ifndef $define");
948         $self->pidl_hdr("#define $define");
949         $self->pidl_hdr("");
950
951         $self->pidl_def("static gint proto_dcerpc_$x->{NAME} = -1;");
952         $self->register_ett("ett_dcerpc_$x->{NAME}");
953         $self->register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
954
955         if (defined($x->{UUID})) {
956                 my $if_uuid = $x->{UUID};
957
958             $self->pidl_def("/* Version information */\n\n");
959             
960             $self->pidl_def("static e_uuid_t uuid_dcerpc_$x->{NAME} = {");
961             $self->pidl_def("\t0x" . substr($if_uuid, 1, 8) 
962                 . ", 0x" . substr($if_uuid, 10, 4)
963             . ", 0x" . substr($if_uuid, 15, 4) . ",");
964             $self->pidl_def("\t{ 0x" . substr($if_uuid, 20, 2) 
965                 . ", 0x" . substr($if_uuid, 22, 2)
966             . ", 0x" . substr($if_uuid, 25, 2)
967             . ", 0x" . substr($if_uuid, 27, 2)
968             . ", 0x" . substr($if_uuid, 29, 2)
969             . ", 0x" . substr($if_uuid, 31, 2)
970             . ", 0x" . substr($if_uuid, 33, 2)
971             . ", 0x" . substr($if_uuid, 35, 2) . " }");
972             $self->pidl_def("};");
973         
974             my $maj = 0x0000FFFF & $x->{VERSION};
975             $maj =~ s/\.(.*)$//g;
976             $self->pidl_def("static guint16 ver_dcerpc_$x->{NAME} = $maj;");
977             $self->pidl_def("");
978         }
979
980         $return_types{$x->{NAME}} = {};
981
982         $self->Interface($x);
983         $self->pidl_code("\n".DumpFunctionTable($x));
984
985         foreach (sort(keys %{$return_types{$x->{NAME}}})) {
986                 my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
987                 my $dt = $self->find_type($type);
988                 $dt or die("Unable to find information about return type `$type'");
989                 $self->register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, "BASE_HEX", $dt->{VALSSTRING}, 0, "");
990                 $self->{hf_used}->{"hf_$x->{NAME}_$_"} = 1;
991         }
992
993         $self->RegisterInterface($x);
994         $self->RegisterInterfaceHandoff($x);
995
996         $self->pidl_hdr("#endif /* $define */");
997 }
998
999 sub find_type($$)
1000 {
1001         my ($self, $n) = @_;
1002
1003         return $self->{conformance}->{types}->{$n};
1004 }
1005
1006 sub register_type($$$$$$$$)
1007 {
1008         my ($self, $type,$call,$ft,$base,$mask,$vals,$length) = @_;
1009
1010         return if (defined($self->{conformance}->{types}->{$type}));
1011
1012         $self->{conformance}->{types}->{$type} = {
1013                 NAME => $type,
1014                 DISSECTOR_NAME => $call,
1015                 FT_TYPE => $ft,
1016                 BASE_TYPE => $base,
1017                 MASK => $mask,
1018                 VALSSTRING => $vals,
1019                 ALIGNMENT => $length
1020         };
1021 }
1022
1023 # Loads the default types
1024 sub Initialize($$)
1025 {
1026         my ($self, $cnf_file) = @_;
1027
1028         $self->{conformance} = {
1029                 imports => {},
1030                 header_fields=> {} 
1031         };
1032
1033         ReadConformance($cnf_file, $self->{conformance}) or print STDERR "warning: No conformance file `$cnf_file'\n";
1034         
1035         foreach my $bytes (qw(1 2 4 8)) {
1036                 my $bits = $bytes * 8;
1037                 $self->register_type("uint$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
1038                 $self->register_type("int$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
1039         }
1040                 
1041         $self->register_type("uint3264", "offset = dissect_ndr_uint3264(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT32", "BASE_DEC", 0, "NULL", 8);
1042         $self->register_type("hyper", "offset = dissect_ndr_uint64(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 8);
1043         $self->register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
1044         $self->register_type("bool8", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
1045         $self->register_type("char", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
1046         $self->register_type("long", "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
1047         $self->register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
1048         $self->register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
1049         $self->register_type("policy_handle", "offset = PIDL_dissect_policy_hnd(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_BYTES", "BASE_NONE", 0, "NULL", 4);
1050         $self->register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1051         $self->register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1052         $self->register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1053         $self->register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1054         $self->register_type("dom_sid28", "{
1055                 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
1056                 di->hf_index = \@HF\@;
1057
1058                 offset = dissect_ndr_nt_SID28(tvb, offset, pinfo, tree, drep);
1059         }", "FT_STRING", "BASE_NONE", 0, "NULL", 4);
1060         $self->register_type("SID", "{
1061                 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
1062
1063                 di->hf_index = \@HF\@;
1064
1065                 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
1066         }", "FT_STRING", "BASE_NONE", 0, "NULL", 4);
1067         $self->register_type("WERROR", 
1068                 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4);
1069         $self->register_type("NTSTATUS", 
1070                 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
1071         $self->register_type("ipv6address", "proto_tree_add_item(tree, \@HF\@, tvb, offset, 16, ENC_NA); offset += 16;", "FT_IPv6", "BASE_NONE", 0, "NULL", 16);
1072         $self->register_type("ipv4address", "proto_tree_add_item(tree, \@HF\@, tvb, offset, 4, ENC_BIG_ENDIAN); offset += 4;", "FT_IPv4", "BASE_NONE", 0, "NULL", 4);
1073
1074 }
1075
1076 #####################################################################
1077 # Generate Wireshark parser and header code
1078 sub Parse($$$$$)
1079 {
1080         my($self,$ndr,$idl_file,$h_filename,$cnf_file) = @_;
1081
1082         $self->Initialize($cnf_file);
1083
1084         return (undef, undef) if defined($self->{conformance}->{noemit_dissector});
1085
1086         my $notice = 
1087 "/* DO NOT EDIT
1088         This filter was automatically generated
1089         from $idl_file and $cnf_file.
1090
1091         Pidl is a perl based IDL compiler for DCE/RPC idl files.
1092         It is maintained by the Samba team, not the Wireshark team.
1093         Instructions on how to download and install Pidl can be
1094         found at http://wiki.wireshark.org/Pidl
1095 */
1096
1097 ";
1098
1099         $self->pidl_hdr($notice);
1100
1101         $self->{res}->{headers} = "\n";
1102         $self->{res}->{headers} .= "#include \"config.h\"\n";
1103
1104         $self->{res}->{headers} .= "#ifdef _MSC_VER\n";
1105         $self->{res}->{headers} .= "#pragma warning(disable:4005)\n";
1106         $self->{res}->{headers} .= "#pragma warning(disable:4013)\n";
1107         $self->{res}->{headers} .= "#pragma warning(disable:4018)\n";
1108         $self->{res}->{headers} .= "#pragma warning(disable:4101)\n";
1109         $self->{res}->{headers} .= "#endif\n\n";
1110
1111         $self->{res}->{headers} .= "#include <glib.h>\n";
1112         $self->{res}->{headers} .= "#include <string.h>\n";
1113         $self->{res}->{headers} .= "#include <epan/packet.h>\n\n";
1114
1115         $self->{res}->{headers} .= "#include \"packet-dcerpc.h\"\n";
1116         $self->{res}->{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
1117         $self->{res}->{headers} .= "#include \"packet-windows-common.h\"\n";
1118
1119         my $h_basename = basename($h_filename);
1120
1121         $self->{res}->{headers} .= "#include \"$h_basename\"\n";
1122         $self->pidl_code("");
1123
1124         if (defined($self->{conformance}->{ett})) {
1125                 register_ett($self,$_) foreach(@{$self->{conformance}->{ett}})
1126         }
1127
1128         # Wireshark protocol registration
1129
1130         foreach (@$ndr) {
1131                 $self->ProcessInterface($_) if ($_->{TYPE} eq "INTERFACE");
1132                 $self->ProcessImport(@{$_->{PATHS}}) if ($_->{TYPE} eq "IMPORT");
1133                 $self->ProcessInclude(@{$_->{PATHS}}) if ($_->{TYPE} eq "INCLUDE");
1134         }
1135
1136         $self->{res}->{ett} = DumpEttDeclaration($self->{ett});
1137         $self->{res}->{hf} = $self->DumpHfDeclaration();
1138
1139         my $parser = $notice;
1140         $parser.= $self->{res}->{headers};
1141         $parser.=$self->{res}->{ett};
1142         $parser.=$self->{res}->{hf};
1143         $parser.=$self->{res}->{def};
1144         if (exists ($self->{conformance}->{override})) {
1145                 $parser.=$self->{conformance}->{override};
1146         }
1147         $parser.=$self->{res}->{code};
1148
1149         my $header = "/* autogenerated by pidl */\n\n";
1150         $header.=$self->{res}->{hdr};
1151
1152         $self->CheckUsed($self->{conformance});
1153     
1154         return ($parser,$header);
1155 }
1156
1157 ###############################################################################
1158 # ETT
1159 ###############################################################################
1160
1161 sub register_ett($$)
1162 {
1163         my ($self, $name) = @_;
1164
1165         push (@{$self->{ett}}, $name);  
1166 }
1167
1168 sub DumpEttList
1169 {
1170         my ($ett) = @_;
1171         my $res = "\tstatic gint *ett[] = {\n";
1172         foreach (@$ett) {
1173                 $res .= "\t\t&$_,\n";
1174         }
1175
1176         return "$res\t};\n";
1177 }
1178
1179 sub DumpEttDeclaration
1180 {
1181         my ($ett) = @_;
1182         my $res = "\n/* Ett declarations */\n";
1183         foreach (@$ett) {
1184                 $res .= "static gint $_ = -1;\n";
1185         }
1186
1187         return "$res\n";
1188 }
1189
1190 ###############################################################################
1191 # HF
1192 ###############################################################################
1193
1194 sub register_hf_field($$$$$$$$$) 
1195 {
1196         my ($self,$index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
1197
1198         if (defined ($self->{conformance}->{hf_renames}->{$index})) {
1199                 $self->{conformance}->{hf_renames}->{$index}->{USED} = 1;
1200                 return $self->{conformance}->{hf_renames}->{$index}->{NEWNAME};
1201         }
1202
1203         $self->{conformance}->{header_fields}->{$index} = {
1204                 INDEX => $index,
1205                 NAME => $name,
1206                 FILTER => $filter_name,
1207                 FT_TYPE => $ft_type,
1208                 BASE_TYPE => $base_type,
1209                 VALSSTRING => $valsstring,
1210                 MASK => $mask,
1211                 BLURB => $blurb
1212         };
1213
1214         if ((not defined($blurb) or $blurb eq "") and 
1215                         defined($self->{conformance}->{fielddescription}->{$index})) {
1216                 $self->{conformance}->{header_fields}->{$index}->{BLURB} = 
1217                         $self->{conformance}->{fielddescription}->{$index}->{DESCRIPTION};
1218                 $self->{conformance}->{fielddescription}->{$index}->{USED} = 1;
1219         }
1220
1221         return $index;
1222 }
1223
1224 sub change_hf_field_type($$$$)
1225 {
1226         my ($self,$index,$ft_type,$base_type) = @_;
1227         if (defined ($self->{conformance}->{hf_renames}->{$index})) {
1228                 print "Field $index has been renamed to ".$self->{conformance}->{hf_renames}->{$index}->{NEWNAME}." you can't change it's type";
1229                 return 0;
1230         }
1231
1232         if (!defined ($self->{conformance}->{header_fields}->{$index})) {
1233                 print "Field $index doesn't exists";
1234                 return 0;
1235         }
1236         $self->{conformance}->{header_fields}->{$index}->{FT_TYPE} = $ft_type;
1237         $self->{conformance}->{header_fields}->{$index}->{BASE_TYPE} = $base_type;
1238         return 1;
1239 }
1240
1241 sub DumpHfDeclaration($)
1242 {
1243         my ($self) = @_;
1244         my $res = "";
1245
1246         $res = "\n/* Header field declarations */\n";
1247
1248         foreach (sort(keys %{$self->{conformance}->{header_fields}}))
1249         {
1250                 $res .= "static gint $_ = -1;\n";
1251         }
1252
1253         return "$res\n";
1254 }
1255
1256 sub make_str_or_null($)
1257 {
1258       my $str = shift;
1259       if (substr($str, 0, 1) eq "\"") {
1260               $str = substr($str, 1, length($str)-2);
1261       }
1262       $str =~ s/^\s*//;
1263       $str =~ s/\s*$//;
1264       if ($str eq "") {
1265               return "NULL";
1266       }
1267       return make_str($str);
1268 }
1269
1270 sub DumpHfList($)
1271 {
1272         my ($self) = @_;
1273         my $res = "\tstatic hf_register_info hf[] = {\n";
1274
1275         foreach (sort {$a->{INDEX} cmp $b->{INDEX}} values %{$self->{conformance}->{header_fields}})
1276         {
1277                 $res .= "\t{ &$_->{INDEX},
1278           { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str_or_null($_->{BLURB}).", HFILL }},
1279 ";
1280         }
1281
1282         return $res."\t};\n";
1283 }
1284
1285
1286 ###############################################################################
1287 # Function table
1288 ###############################################################################
1289
1290 sub DumpFunctionTable($)
1291 {
1292         my $if = shift;
1293
1294         my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
1295         foreach (@{$if->{FUNCTIONS}}) {
1296                 my $fn_name = $_->{NAME};
1297                 $fn_name =~ s/^$if->{NAME}_//;
1298                 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1299                 $res.= "\t   $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1300         }
1301
1302         $res .= "\t{ 0, NULL, NULL, NULL }\n";
1303
1304         return "$res};\n";
1305 }
1306
1307 sub CheckUsed($$)
1308 {
1309         my ($self, $conformance) = @_;
1310         foreach (values %{$conformance->{header_fields}}) {
1311                 if (not defined($self->{hf_used}->{$_->{INDEX}})) {
1312                         warning($_->{POS}, "hf field `$_->{INDEX}' not used");
1313                 }
1314         }
1315
1316         foreach (values %{$conformance->{hf_renames}}) {
1317                 if (not $_->{USED}) {
1318                         warning($_->{POS}, "hf field `$_->{OLDNAME}' not used");
1319                 }
1320         }
1321
1322         foreach (values %{$conformance->{dissectorparams}}) {
1323                 if (not $_->{USED}) {
1324                         warning($_->{POS}, "dissector param never used");
1325                 }
1326         }
1327
1328         foreach (values %{$conformance->{imports}}) {
1329                 if (not $_->{USED}) {
1330                         warning($_->{POS}, "import never used");
1331                 }
1332         }
1333
1334         foreach (values %{$conformance->{types}}) {
1335                 if (not $_->{USED} and defined($_->{POS})) {
1336                         warning($_->{POS}, "type never used");
1337                 }
1338         }
1339
1340         foreach (values %{$conformance->{fielddescription}}) {
1341                 if (not $_->{USED}) {
1342                         warning($_->{POS}, "description never used");
1343                 }
1344         }
1345
1346         foreach (values %{$conformance->{tfs}}) {
1347                 if (not $_->{USED}) {
1348                         warning($_->{POS}, "True/False description never used");
1349                 }
1350         }
1351 }
1352
1353 1;