pidl-wireshark: add the type dom_sid28 and call ad-hoc dissector
[metze/samba/wip.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, block_display => [],
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_, guint32 *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_, guint32 *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=(g$e->{BASE_TYPE})*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=(guint32)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("dcerpc_info *di = (dcerpc_info*)pinfo->private_data;");
301                         $self->pidl_code("int conformant = di->conformant_run;");
302
303                         my $additional = "";
304                         if ($e->{TYPE} eq "uint8") {
305                                 push @{$self->{block_display}} , int($e);
306                                 $additional = " $l->{SIZE_IS},";
307                         }
308                         $self->pidl_code("if (!conformant) {");
309                         $self->indent;
310                         $self->pidl_code("offset = $myname\_(tvb, offset,$additional pinfo, tree, drep);");
311                         $self->deindent;
312                         $self->pidl_code("}");
313                 } else {
314                         my $type = "";
315                         $type .= "c" if ($l->{IS_CONFORMANT});
316                         $type .= "v" if ($l->{IS_VARYING});
317
318                         unless ($l->{IS_ZERO_TERMINATED}) {
319                                 my $suffix = "";
320                                 if ($e->{TYPE} eq "uint8" && !$l->{IS_SURROUNDING}) {
321                                         $suffix = "_block";
322                                         push @{$self->{block_display}} , int($e);
323                                 }
324                                 $self->pidl_code("offset = dissect_ndr_u" . $type . "array$suffix(tvb, offset, pinfo, tree, drep, $myname\_);");
325                         } else {
326                                 my $nl = GetNextLevel($e,$l);
327                                 $self->pidl_code("char *data;");
328                                 $self->pidl_code("");
329                                 $self->pidl_code("offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, drep, sizeof(g$nl->{DATA_TYPE}), $hf, FALSE, &data);");
330                                 $self->pidl_code("proto_item_append_text(tree, \": %s\", data);");
331                         }
332                 }
333         } elsif ($l->{TYPE} eq "DATA") {
334                 if ($l->{DATA_TYPE} eq "string") {
335                         my $bs = 2; # Byte size defaults to that of UCS2
336
337
338                         ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
339                         
340                         if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
341                                 $self->pidl_code("char *data;\n");
342                                 $self->pidl_code("offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, &data);");
343                                 $self->pidl_code("proto_item_append_text(tree, \": %s\", data);");
344                         } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
345                                 $self->pidl_code("offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, NULL);");
346                         } elsif (property_matches($e, "flag", ".*STR_NULLTERM.*")) {
347                                 if ($bs == 2) {
348                                         $self->pidl_code("offset = dissect_null_term_wstring(tvb, offset, pinfo, tree, drep, $hf , 0);")
349                                 } else {
350                                         $self->pidl_code("offset = dissect_null_term_string(tvb, offset, pinfo, tree, drep, $hf , 0);")
351                                 }
352                         } else {
353                                 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
354                         }
355                 } elsif ($l->{DATA_TYPE} eq "DATA_BLOB") {
356                         my $remain = 0;
357                         $remain = 1 if (property_matches($e->{ORIGINAL}, "flag", ".*LIBNDR_FLAG_REMAINING.*"));
358                         $self->pidl_code("offset = dissect_ndr_datablob(tvb, offset, pinfo, tree, drep, $hf, $remain);");
359                 } else {
360                         my $call;
361
362                         if ($self->{conformance}->{imports}->{$l->{DATA_TYPE}}) {
363                                 $call = $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{DATA};
364                                 $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
365
366                         } elsif (defined($self->{conformance}->{imports}->{"$pn.$e->{NAME}"})) {
367                                 $call = $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{DATA};
368                                 $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
369
370                         } elsif (defined($self->{conformance}->{types}->{$l->{DATA_TYPE}})) {
371                                 my $ref_e = int($e);
372                                 if (grep /$ref_e/,@{$self->{block_display}}) {
373                                         $call = "proto_tree_add_bytes(tree, \@HF\@, tvb, offset, length, tvb_get_ptr(tvb, offset,length)); offset += length;";
374                                 } else {
375                                         $call= $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
376                                         $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
377                                 }
378                         } else {
379                                 $self->pidl_code("offset = $ifname\_dissect_struct_" . $l->{DATA_TYPE} . "(tvb,offset,pinfo,tree,drep,$hf,$param);");
380                                 return;
381                         }
382
383                         $call =~ s/\@HF\@/$hf/g;
384                         $call =~ s/\@PARAM\@/$param/g;
385                         $self->pidl_code($call);
386                 }
387         } elsif ($_->{TYPE} eq "SUBCONTEXT") {
388                 my $varswitch;
389                 if (has_property($e, "switch_is")) {
390                         $varswitch = $e->{PROPERTIES}->{switch_is};
391                 }
392                 my $num_bits = ($l->{HEADER_SIZE}*8);
393                 my $hf2 = $self->register_hf_field($hf."_", "Subcontext length", "$ifname.$pn.$_->{NAME}subcontext", "FT_UINT$num_bits", "BASE_HEX", "NULL", 0, "");
394                 $num_bits = 3264 if ($num_bits == 32);
395                 $self->{hf_used}->{$hf2} = 1;
396                 $self->pidl_code("dcerpc_info *di = (dcerpc_info*)pinfo->private_data;");
397                 $self->pidl_code("guint$num_bits size;");
398                 $self->pidl_code("int conformant = di->conformant_run;");
399                 $self->pidl_code("tvbuff_t *subtvb;");
400                 $self->pidl_code("");
401                 # We need to be able to dissect the length of the context in every case
402                 # and conformant run skips the dissections of scalars ...
403                 $self->pidl_code("if (!conformant) {");
404                 $self->indent;
405                 $self->pidl_code("guint32 saved_flags = di->call_data->flags;");
406                 $self->pidl_code("offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf2, &size);");
407                 # This is a subcontext, there is normally no such thing as
408                 # 64 bit NDR is subcontext so we clear the flag so that we can
409                 # continue to dissect handmarshalled stuff with pidl
410                 $self->pidl_code("di->call_data->flags &= ~DCERPC_IS_NDR64;");
411
412                 $self->pidl_code("subtvb = tvb_new_subset(tvb, offset, size, -1);");
413                 if ($param ne 0) {
414                         $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, drep, $param);");
415                 } else {
416                         $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, drep);");
417                 }
418                 $self->pidl_code("offset += size;");
419                 $self->pidl_code("di->call_data->flags = saved_flags;");
420                 $self->deindent;
421                 $self->pidl_code("}");
422         } elsif ($_->{TYPE} eq "PIPE") {
423                 error($e->{ORIGINAL}, "Type PIPE not yet supported");
424         } else {
425                 die("Unknown type `$_->{TYPE}'");
426         }
427 }
428
429 sub Element($$$$$)
430 {
431         my ($self,$e,$pn,$ifname,$isoruseswitch) = @_;
432
433         my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn, $self->{conformance}->{strip_prefixes})."\_".StripPrefixes($e->{NAME}, $self->{conformance}->{strip_prefixes});
434
435         my ($call_code, $moreparam);
436         my $param = 0;
437         if (defined $isoruseswitch) {
438                 my $type = $isoruseswitch->[0];
439                 my $name = $isoruseswitch->[1];
440
441                 my $switch_dt =  getType($type);
442                 my $switch_type;
443                 if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
444                         $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
445                 } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
446                         $switch_type = "g$e->{SWITCH_TYPE}";
447                 }
448                 $moreparam = ", $switch_type *".$name;
449                 $param = $name;
450                 $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep, &$name);";
451         } else {
452                 $moreparam = "";
453                 $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
454         }
455
456
457         my $type = $self->find_type($e->{TYPE});
458
459         if (not defined($type)) {
460                 # default settings
461                 $type = {
462                         MASK => 0,
463                         VALSSTRING => "NULL",
464                         FT_TYPE => "FT_NONE",
465                         BASE_TYPE => "BASE_NONE"
466                 };
467         }
468
469         if (ContainsString($e)) {
470                 $type = {
471                         MASK => 0,
472                         VALSSTRING => "NULL",
473                         FT_TYPE => "FT_STRING",
474                         BASE_TYPE => "BASE_NONE"
475                 };
476         }
477
478         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}, "");
479         $self->{hf_used}->{$hf} = 1;
480
481         my $eltname = StripPrefixes($pn, $self->{conformance}->{strip_prefixes}) . ".$e->{NAME}";
482         if (defined($self->{conformance}->{noemit}->{$eltname})) {
483                 return $call_code;
484         }
485
486         my $add = "";
487
488         my $oldparam = undef;
489         foreach (@{$e->{LEVELS}}) {
490                 if (defined $_->{SWITCH_IS}) {
491                         $oldparam = $param;
492                         $param = "*$param";
493                 }
494                 next if ($_->{TYPE} eq "SWITCH");
495                 my $tmp = "${dissectorname}${add}";
496                 my $additionalparam = "";
497
498                 my $ref_e = int($e);
499                 if (grep /$ref_e/, @{$self->{block_display}}) {
500                         $additionalparam = "int length _U_,";
501                         $self->change_hf_field_type("hf_$ifname\_$pn\_$e->{NAME}", "FT_BYTES", "BASE_NONE");
502                 }
503                 if (not defined ($self->{conformance}->{manual}->{$tmp})) {
504                         $self->pidl_def("static int $dissectorname$add(tvbuff_t *tvb _U_, int offset _U_,$additionalparam packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_$moreparam);");
505                 }
506                 $self->pidl_fn_start("$dissectorname$add");
507                 $self->pidl_code("static int");
508                 $self->pidl_code("$dissectorname$add(tvbuff_t *tvb _U_, int offset _U_,$additionalparam packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_$moreparam)");
509                 $self->pidl_code("{");
510                 $self->indent;
511
512                 $self->ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname,$param);
513                 if (defined $oldparam) {
514                         $param = $oldparam;
515                 }
516
517                 $self->pidl_code("");
518                 $self->pidl_code("return offset;");
519                 $self->deindent;
520                 $self->pidl_code("}\n");
521                 $self->pidl_fn_end("$dissectorname$add");
522                 $add.="_";
523                 # Reset at the end of the loop
524                 $additionalparam = "";
525                 if ($_->{TYPE} eq "ARRAY" or $_->{TYPE} eq "SWITCH") {
526                         $moreparam = "";
527                         $param = "0";
528                 }
529                 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
530         }
531         pop @{$self->{block_display}} if (scalar(@{$self->{block_display}}));
532
533         return $call_code;
534 }
535
536 sub Function($$$)
537 {
538         my ($self, $fn,$ifname) = @_;
539
540         my %dissectornames;
541
542         foreach (@{$fn->{ELEMENTS}}) {
543             $dissectornames{$_->{NAME}} = $self->Element($_, $fn->{NAME}, $ifname, undef) if not defined($dissectornames{$_->{NAME}});
544         }
545         
546         my $fn_name = $_->{NAME};
547         $fn_name =~ s/^${ifname}_//;
548
549         $self->PrintIdl(DumpFunction($fn->{ORIGINAL}));
550         $self->pidl_fn_start("$ifname\_dissect\_$fn_name\_response");
551         $self->pidl_code("static int");
552         $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_)");
553         $self->pidl_code("{");
554         $self->indent;
555         if ( not defined($fn->{RETURN_TYPE})) {
556         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR") 
557         {
558                 $self->pidl_code("guint32 status;\n");
559         } elsif (my $type = getType($fn->{RETURN_TYPE})) {
560                 if ($type->{DATA}->{TYPE} eq "ENUM") {
561                         $self->pidl_code("g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA}) . " status;\n");
562                 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
563                         $self->pidl_code("g$fn->{RETURN_TYPE} status;\n");
564                 } else {
565                 error($fn, "return type `$fn->{RETURN_TYPE}' not yet supported");
566                 }
567         } else {
568                 error($fn, "unknown return type `$fn->{RETURN_TYPE}'");
569         }
570
571         $self->pidl_code("pinfo->dcerpc_procedure_name=\"${fn_name}\";");
572         foreach (@{$fn->{ELEMENTS}}) {
573                 if (grep(/out/,@{$_->{DIRECTION}})) {
574                         $self->pidl_code("$dissectornames{$_->{NAME}}");
575                         $self->pidl_code("offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);");
576                         $self->pidl_code("");
577                 }
578         }
579
580         if (not defined($fn->{RETURN_TYPE})) {
581         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
582                 $self->pidl_code("offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n");
583                 $self->pidl_code("if (status != 0)");
584                 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n");
585                 $return_types{$ifname}->{"status"} = ["NTSTATUS", "NT Error"];
586         } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
587                 $self->pidl_code("offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &status);\n");
588                 $self->pidl_code("if (status != 0)");
589                 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, WERR_errors, \"Unknown DOS error 0x%08x\"));\n");
590                 
591                 $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"];
592         } elsif (my $type = getType($fn->{RETURN_TYPE})) {
593                 if ($type->{DATA}->{TYPE} eq "ENUM") {
594                         my $return_type = "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
595                         my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
596
597                         $self->pidl_code("offset = $return_dissect(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);");
598                         $self->pidl_code("if (status != 0)");
599                         $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");
600                         $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
601                 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
602                         $self->pidl_code("offset = dissect_ndr_$fn->{RETURN_TYPE}(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);");
603                         $self->pidl_code("if (status != 0)");
604                         $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %d\", status);\n");
605                         $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
606                 }
607         }
608
609         $self->pidl_code("return offset;");
610         $self->deindent;
611         $self->pidl_code("}\n");
612         $self->pidl_fn_end("$ifname\_dissect\_$fn_name\_response");
613
614         $self->pidl_fn_start("$ifname\_dissect\_$fn_name\_request");
615         $self->pidl_code("static int");
616         $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_)");
617         $self->pidl_code("{");
618         $self->indent;
619         $self->pidl_code("pinfo->dcerpc_procedure_name=\"${fn_name}\";");
620         foreach (@{$fn->{ELEMENTS}}) {
621                 if (grep(/in/,@{$_->{DIRECTION}})) {
622                         $self->pidl_code("$dissectornames{$_->{NAME}}");
623                         $self->pidl_code("offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);");
624                 }
625
626         }
627
628         $self->pidl_code("return offset;");
629         $self->deindent;
630         $self->pidl_code("}\n");
631         $self->pidl_fn_end("$ifname\_dissect\_$fn_name\_request");
632 }
633
634 sub Struct($$$$)
635 {
636         my ($self,$e,$name,$ifname) = @_;
637         my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
638
639         return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
640
641         $self->register_ett("ett_$ifname\_$name");
642
643         my $res = "";
644         my $varswitchs = {};
645         # will contain the switch var declaration;
646         my $vars = [];
647         foreach (@{$e->{ELEMENTS}}) {
648                 if (has_property($_, "switch_is")) {
649                         $varswitchs->{$_->{PROPERTIES}->{switch_is}} = [];
650                 }
651         }
652         foreach (@{$e->{ELEMENTS}}) {
653                 my $switch_info = undef;
654
655                 my $v = $_->{NAME};
656                 if (scalar(grep {/$v/} keys(%$varswitchs)) == 1) {
657                         # This element is one of the switch attribute
658                         my $switch_dt =  getType($_->{TYPE});
659                         my $switch_type;
660                         if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
661                                 $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
662                         } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
663                                 $switch_type = "g$e->{SWITCH_TYPE}";
664                         }
665
666                         push @$vars, "$switch_type $v;";
667                         $switch_info = [ $_->{TYPE}, $v ];
668                         $varswitchs->{$v} = $switch_info;
669                 }
670
671                 if (has_property($_, "switch_is")) {
672                         my $varswitch = $_->{PROPERTIES}->{switch_is};
673                         $switch_info = $varswitchs->{$varswitch};
674                 }
675
676                 $res.="\t".$self->Element($_, $name, $ifname, $switch_info)."\n\n";
677         }
678
679         $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_);");
680
681         $self->pidl_fn_start($dissectorname);
682         $self->pidl_code("int");
683         $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_)");
684         $self->pidl_code("{");
685         $self->indent;
686         $self->pidl_code($_) foreach (@$vars);
687         $self->pidl_code("proto_item *item = NULL;");
688         $self->pidl_code("proto_tree *tree = NULL;");
689         if ($e->{ALIGN} > 1) {
690                 $self->pidl_code("dcerpc_info *di = (dcerpc_info *)pinfo->private_data;");
691         }
692         $self->pidl_code("int old_offset;");
693         $self->pidl_code("");
694
695         if ($e->{ALIGN} > 1 and not property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) {
696                 $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
697         }
698         $self->pidl_code("");
699
700         $self->pidl_code("old_offset = offset;");
701         $self->pidl_code("");
702         $self->pidl_code("if (parent_tree) {");
703         $self->indent;
704         $self->pidl_code("item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, ENC_NA);");
705         $self->pidl_code("tree = proto_item_add_subtree(item, ett_$ifname\_$name);");
706         $self->deindent;
707         $self->pidl_code("}");
708
709         $self->pidl_code("\n$res");
710
711         $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n");
712         if ($e->{ALIGN} > 1) {
713                 $self->pidl_code("");
714                 $self->pidl_code("if (di->call_data->flags & DCERPC_IS_NDR64) {");
715                 $self->indent;
716                 $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
717                 $self->deindent;
718                 $self->pidl_code("}");
719         }
720         $self->pidl_code("");
721         $self->pidl_code("return offset;");
722         $self->deindent;
723         $self->pidl_code("}\n");
724         $self->pidl_fn_end($dissectorname);
725
726         $self->register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
727 }
728
729 sub Union($$$$)
730 {
731         my ($self,$e,$name,$ifname) = @_;
732
733         my $dissectorname = "$ifname\_dissect_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
734
735         return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
736         
737         $self->register_ett("ett_$ifname\_$name");
738
739         my $res = "";
740         foreach (@{$e->{ELEMENTS}}) {
741                 $res.="\n\t\t$_->{CASE}:\n";
742                 if ($_->{TYPE} ne "EMPTY") {
743                         $res.="\t\t\t".$self->Element($_, $name, $ifname, undef)."\n";
744                 }
745                 $res.="\t\tbreak;\n";
746         }
747
748         my $switch_type;
749         my $switch_dissect;
750         my $switch_dt = getType($e->{SWITCH_TYPE});
751         if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
752                 $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
753                 $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
754         } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
755                 $switch_type = "g$e->{SWITCH_TYPE}";
756                 $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
757         }
758
759         $self->pidl_fn_start($dissectorname);
760         $self->pidl_code("static int");
761         $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_)");
762         $self->pidl_code("{");
763         $self->indent;
764         $self->pidl_code("proto_item *item = NULL;");
765         $self->pidl_code("proto_tree *tree = NULL;");
766         $self->pidl_code("int old_offset;");
767         if (!defined $switch_type) {
768                 $self->pidl_code("guint32 level = param;");
769         } else {
770                 $self->pidl_code("$switch_type level;");
771         }
772         $self->pidl_code("");
773
774         $self->pidl_code("old_offset = offset;");
775         $self->pidl_code("if (parent_tree) {");
776         $self->indent;
777         $self->pidl_code("item = proto_tree_add_text(parent_tree, tvb, offset, -1, \"$name\");");
778         $self->pidl_code("tree = proto_item_add_subtree(item, ett_$ifname\_$name);");
779         $self->deindent;
780         $self->pidl_code("}");
781
782         $self->pidl_code("");
783
784         if (defined $switch_type) {
785                 $self->pidl_code("offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);");
786
787                 if ($e->{ALIGN} > 1) {
788                         $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
789                         $self->pidl_code("");
790                 }
791         }
792
793
794         $self->pidl_code("switch(level) {$res\t}");
795         $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n");
796         $self->pidl_code("");
797
798         $self->pidl_code("return offset;");
799         $self->deindent;
800         $self->pidl_code("}");
801         $self->pidl_fn_end($dissectorname);
802
803         $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
804 }
805
806 sub Const($$$)
807 {
808         my ($self,$const,$ifname) = @_;
809         
810         if (!defined($const->{ARRAY_LEN}[0])) {
811                 $self->pidl_hdr("#define $const->{NAME}\t( $const->{VALUE} )\n");
812         } else {
813                 $self->pidl_hdr("#define $const->{NAME}\t $const->{VALUE}\n");
814         }
815 }
816
817 sub Typedef($$$$)
818 {
819         my ($self,$e,$name,$ifname) = @_;
820
821         $self->Type($e->{DATA}, $name, $ifname);
822 }
823
824 sub Type($$$$)
825 {
826         my ($self, $e, $name, $ifname) = @_;
827
828         $self->PrintIdl(DumpType($e->{ORIGINAL}));
829         {
830                 ENUM => \&Enum,
831                 STRUCT => \&Struct,
832                 UNION => \&Union,
833                 BITMAP => \&Bitmap,
834                 TYPEDEF => \&Typedef,
835                 PIPE    => \&Pipe
836         }->{$e->{TYPE}}->($self, $e, $name, $ifname);
837 }
838
839 sub RegisterInterface($$)
840 {
841         my ($self, $x) = @_;
842
843         $self->pidl_fn_start("proto_register_dcerpc_$x->{NAME}");
844         $self->pidl_code("void proto_register_dcerpc_$x->{NAME}(void)");
845         $self->pidl_code("{");
846         $self->indent;
847
848         $self->{res}->{code}.=$self->DumpHfList()."\n";
849         $self->{res}->{code}.="\n".DumpEttList($self->{ett})."\n";
850         
851         if (defined($x->{UUID})) {
852             # These can be changed to non-pidl_code names if the old dissectors
853             # in epan/dissctors are deleted.
854     
855             my $name = uc($x->{NAME}) . " (pidl)";
856             my $short_name = uc($x->{NAME});
857             my $filter_name = $x->{NAME};
858
859             if (has_property($x, "helpstring")) {
860                 $name = $x->{PROPERTIES}->{helpstring};
861             }
862
863             if (defined($self->{conformance}->{protocols}->{$x->{NAME}})) {
864                 $short_name = $self->{conformance}->{protocols}->{$x->{NAME}}->{SHORTNAME};
865                 $name = $self->{conformance}->{protocols}->{$x->{NAME}}->{LONGNAME};
866                 $filter_name = $self->{conformance}->{protocols}->{$x->{NAME}}->{FILTERNAME};
867             }
868
869             $self->pidl_code("proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");");
870             
871             $self->pidl_code("proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));");
872             $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));");
873         } else {
874             $self->pidl_code("proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");");
875             $self->pidl_code("proto_register_field_array(proto_dcerpc, hf, array_length(hf));");
876             $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));");
877         }
878             
879         $self->deindent;
880         $self->pidl_code("}\n");
881         $self->pidl_fn_end("proto_register_dcerpc_$x->{NAME}");
882 }
883
884 sub RegisterInterfaceHandoff($$)
885 {
886         my ($self,$x) = @_;
887
888         if (defined($x->{UUID})) {
889                 $self->pidl_fn_start("proto_reg_handoff_dcerpc_$x->{NAME}");
890             $self->pidl_code("void proto_reg_handoff_dcerpc_$x->{NAME}(void)");
891             $self->pidl_code("{");
892             $self->indent;
893             $self->pidl_code("dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},");
894             $self->pidl_code("\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},");
895             $self->pidl_code("\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);");
896             $self->deindent;
897             $self->pidl_code("}");
898                 $self->pidl_fn_end("proto_reg_handoff_dcerpc_$x->{NAME}");
899
900                 $self->{hf_used}->{"hf_$x->{NAME}_opnum"} = 1;
901         }
902 }
903
904 sub ProcessInclude
905 {
906         my $self = shift;
907         my @includes = @_;
908         foreach (@includes) {
909                 $self->pidl_hdr("#include \"$_\"");
910         }
911         $self->pidl_hdr("");
912 }
913
914 sub ProcessImport
915 {
916         my $self = shift;
917         my @imports = @_;
918         foreach (@imports) {
919                 next if($_ eq "security");
920                 s/^\"//;
921                 s/\.idl"?$//;
922                 $self->pidl_hdr("#include \"packet-dcerpc-$_\.h\"");
923         }
924         $self->pidl_hdr("");
925 }
926
927 sub ProcessInterface($$)
928 {
929         my ($self, $x) = @_;
930
931         push(@{$self->{conformance}->{strip_prefixes}}, $x->{NAME});
932
933         my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
934         $self->pidl_hdr("#ifndef $define");
935         $self->pidl_hdr("#define $define");
936         $self->pidl_hdr("");
937
938         $self->pidl_def("static gint proto_dcerpc_$x->{NAME} = -1;");
939         $self->register_ett("ett_dcerpc_$x->{NAME}");
940         $self->register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
941
942         if (defined($x->{UUID})) {
943                 my $if_uuid = $x->{UUID};
944
945             $self->pidl_def("/* Version information */\n\n");
946             
947             $self->pidl_def("static e_uuid_t uuid_dcerpc_$x->{NAME} = {");
948             $self->pidl_def("\t0x" . substr($if_uuid, 1, 8) 
949                 . ", 0x" . substr($if_uuid, 10, 4)
950             . ", 0x" . substr($if_uuid, 15, 4) . ",");
951             $self->pidl_def("\t{ 0x" . substr($if_uuid, 20, 2) 
952                 . ", 0x" . substr($if_uuid, 22, 2)
953             . ", 0x" . substr($if_uuid, 25, 2)
954             . ", 0x" . substr($if_uuid, 27, 2)
955             . ", 0x" . substr($if_uuid, 29, 2)
956             . ", 0x" . substr($if_uuid, 31, 2)
957             . ", 0x" . substr($if_uuid, 33, 2)
958             . ", 0x" . substr($if_uuid, 35, 2) . " }");
959             $self->pidl_def("};");
960         
961             my $maj = 0x0000FFFF & $x->{VERSION};
962             $maj =~ s/\.(.*)$//g;
963             $self->pidl_def("static guint16 ver_dcerpc_$x->{NAME} = $maj;");
964             $self->pidl_def("");
965         }
966
967         $return_types{$x->{NAME}} = {};
968
969         $self->Interface($x);
970         $self->pidl_code("\n".DumpFunctionTable($x));
971
972         foreach (keys %{$return_types{$x->{NAME}}}) {
973                 my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
974                 my $dt = $self->find_type($type);
975                 $dt or die("Unable to find information about return type `$type'");
976                 $self->register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, "BASE_HEX", $dt->{VALSSTRING}, 0, "");
977                 $self->{hf_used}->{"hf_$x->{NAME}_$_"} = 1;
978         }
979
980         $self->RegisterInterface($x);
981         $self->RegisterInterfaceHandoff($x);
982
983         $self->pidl_hdr("#endif /* $define */");
984 }
985
986 sub find_type($$)
987 {
988         my ($self, $n) = @_;
989
990         return $self->{conformance}->{types}->{$n};
991 }
992
993 sub register_type($$$$$$$$)
994 {
995         my ($self, $type,$call,$ft,$base,$mask,$vals,$length) = @_;
996
997         return if (defined($self->{conformance}->{types}->{$type}));
998
999         $self->{conformance}->{types}->{$type} = {
1000                 NAME => $type,
1001                 DISSECTOR_NAME => $call,
1002                 FT_TYPE => $ft,
1003                 BASE_TYPE => $base,
1004                 MASK => $mask,
1005                 VALSSTRING => $vals,
1006                 ALIGNMENT => $length
1007         };
1008 }
1009
1010 # Loads the default types
1011 sub Initialize($$)
1012 {
1013         my ($self, $cnf_file) = @_;
1014
1015         $self->{conformance} = {
1016                 imports => {},
1017                 header_fields=> {} 
1018         };
1019
1020         ReadConformance($cnf_file, $self->{conformance}) or print STDERR "warning: No conformance file `$cnf_file'\n";
1021         
1022         foreach my $bytes (qw(1 2 4 8)) {
1023                 my $bits = $bytes * 8;
1024                 $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);
1025                 $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);
1026         }
1027                 
1028         $self->register_type("uint3264", "offset = dissect_ndr_uint3264(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT32", "BASE_DEC", 0, "NULL", 8);
1029         $self->register_type("hyper", "offset = dissect_ndr_uint64(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 8);
1030         $self->register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
1031         $self->register_type("bool8", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
1032         $self->register_type("char", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
1033         $self->register_type("long", "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
1034         $self->register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
1035         $self->register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
1036         $self->register_type("policy_handle", "offset = PIDL_dissect_policy_hnd(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_BYTES", "BASE_NONE", 0, "NULL", 4);
1037         $self->register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1038         $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);
1039         $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);
1040         $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);
1041         $self->register_type("dom_sid28", "{
1042                 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
1043                 di->hf_index = \@HF\@;
1044
1045                 offset = dissect_ndr_nt_SID28(tvb, offset, pinfo, tree, drep);
1046         }", "FT_STRING", "BASE_NONE", 0, "NULL", 4);
1047         $self->register_type("SID", "{
1048                 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
1049
1050                 di->hf_index = \@HF\@;
1051
1052                 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
1053         }", "FT_STRING", "BASE_NONE", 0, "NULL", 4);
1054         $self->register_type("WERROR", 
1055                 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4);
1056         $self->register_type("NTSTATUS", 
1057                 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
1058
1059 }
1060
1061 #####################################################################
1062 # Generate Wireshark parser and header code
1063 sub Parse($$$$$)
1064 {
1065         my($self,$ndr,$idl_file,$h_filename,$cnf_file) = @_;
1066
1067         $self->Initialize($cnf_file);
1068
1069         return (undef, undef) if defined($self->{conformance}->{noemit_dissector});
1070
1071         my $notice = 
1072 "/* DO NOT EDIT
1073         This filter was automatically generated
1074         from $idl_file and $cnf_file.
1075         
1076         Pidl is a perl based IDL compiler for DCE/RPC idl files.
1077         It is maintained by the Samba team, not the Wireshark team.
1078         Instructions on how to download and install Pidl can be
1079         found at http://wiki.wireshark.org/Pidl
1080
1081         \$Id\$
1082 */
1083
1084 ";
1085
1086         $self->pidl_hdr($notice);
1087
1088         $self->{res}->{headers} = "\n";
1089         $self->{res}->{headers} .= "#include \"config.h\"\n";
1090
1091         $self->{res}->{headers} .= "#ifdef _MSC_VER\n";
1092         $self->{res}->{headers} .= "#pragma warning(disable:4005)\n";
1093         $self->{res}->{headers} .= "#pragma warning(disable:4013)\n";
1094         $self->{res}->{headers} .= "#pragma warning(disable:4018)\n";
1095         $self->{res}->{headers} .= "#pragma warning(disable:4101)\n";
1096         $self->{res}->{headers} .= "#endif\n\n";
1097
1098         $self->{res}->{headers} .= "#include <glib.h>\n";
1099         $self->{res}->{headers} .= "#include <string.h>\n";
1100         $self->{res}->{headers} .= "#include <epan/packet.h>\n\n";
1101
1102         $self->{res}->{headers} .= "#include \"packet-dcerpc.h\"\n";
1103         $self->{res}->{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
1104         $self->{res}->{headers} .= "#include \"packet-windows-common.h\"\n";
1105
1106         my $h_basename = basename($h_filename);
1107
1108         $self->{res}->{headers} .= "#include \"$h_basename\"\n";
1109         $self->pidl_code("");
1110
1111         if (defined($self->{conformance}->{ett})) {
1112                 register_ett($self,$_) foreach(@{$self->{conformance}->{ett}})
1113         }
1114
1115         # Wireshark protocol registration
1116
1117         foreach (@$ndr) {
1118                 $self->ProcessInterface($_) if ($_->{TYPE} eq "INTERFACE");
1119                 $self->ProcessImport(@{$_->{PATHS}}) if ($_->{TYPE} eq "IMPORT");
1120                 $self->ProcessInclude(@{$_->{PATHS}}) if ($_->{TYPE} eq "INCLUDE");
1121         }
1122
1123         $self->{res}->{ett} = DumpEttDeclaration($self->{ett});
1124         $self->{res}->{hf} = $self->DumpHfDeclaration();
1125
1126         my $parser = $notice;
1127         $parser.= $self->{res}->{headers};
1128         $parser.=$self->{res}->{ett};
1129         $parser.=$self->{res}->{hf};
1130         $parser.=$self->{res}->{def};
1131         if (exists ($self->{conformance}->{override})) {
1132                 $parser.=$self->{conformance}->{override};
1133         }
1134         $parser.=$self->{res}->{code};
1135
1136         my $header = "/* autogenerated by pidl */\n\n";
1137         $header.=$self->{res}->{hdr};
1138
1139         $self->CheckUsed($self->{conformance});
1140     
1141         return ($parser,$header);
1142 }
1143
1144 ###############################################################################
1145 # ETT
1146 ###############################################################################
1147
1148 sub register_ett($$)
1149 {
1150         my ($self, $name) = @_;
1151
1152         push (@{$self->{ett}}, $name);  
1153 }
1154
1155 sub DumpEttList
1156 {
1157         my ($ett) = @_;
1158         my $res = "\tstatic gint *ett[] = {\n";
1159         foreach (@$ett) {
1160                 $res .= "\t\t&$_,\n";
1161         }
1162
1163         return "$res\t};\n";
1164 }
1165
1166 sub DumpEttDeclaration
1167 {
1168         my ($ett) = @_;
1169         my $res = "\n/* Ett declarations */\n";
1170         foreach (@$ett) {
1171                 $res .= "static gint $_ = -1;\n";
1172         }
1173
1174         return "$res\n";
1175 }
1176
1177 ###############################################################################
1178 # HF
1179 ###############################################################################
1180
1181 sub register_hf_field($$$$$$$$$) 
1182 {
1183         my ($self,$index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
1184
1185         if (defined ($self->{conformance}->{hf_renames}->{$index})) {
1186                 $self->{conformance}->{hf_renames}->{$index}->{USED} = 1;
1187                 return $self->{conformance}->{hf_renames}->{$index}->{NEWNAME};
1188         }
1189
1190         $self->{conformance}->{header_fields}->{$index} = {
1191                 INDEX => $index,
1192                 NAME => $name,
1193                 FILTER => $filter_name,
1194                 FT_TYPE => $ft_type,
1195                 BASE_TYPE => $base_type,
1196                 VALSSTRING => $valsstring,
1197                 MASK => $mask,
1198                 BLURB => $blurb
1199         };
1200
1201         if ((not defined($blurb) or $blurb eq "") and 
1202                         defined($self->{conformance}->{fielddescription}->{$index})) {
1203                 $self->{conformance}->{header_fields}->{$index}->{BLURB} = 
1204                         $self->{conformance}->{fielddescription}->{$index}->{DESCRIPTION};
1205                 $self->{conformance}->{fielddescription}->{$index}->{USED} = 1;
1206         }
1207
1208         return $index;
1209 }
1210
1211 sub change_hf_field_type($$$$)
1212 {
1213         my ($self,$index,$ft_type,$base_type) = @_;
1214         if (defined ($self->{conformance}->{hf_renames}->{$index})) {
1215                 print "Field $index has been renamed to ".$self->{conformance}->{hf_renames}->{$index}->{NEWNAME}." you can't change it's type";
1216                 return 0;
1217         }
1218
1219         if (!defined ($self->{conformance}->{header_fields}->{$index})) {
1220                 print "Field $index doesn't exists";
1221                 return 0;
1222         }
1223         $self->{conformance}->{header_fields}->{$index}->{FT_TYPE} = $ft_type;
1224         $self->{conformance}->{header_fields}->{$index}->{BASE_TYPE} = $base_type;
1225         return 1;
1226 }
1227
1228 sub DumpHfDeclaration($)
1229 {
1230         my ($self) = @_;
1231         my $res = "";
1232
1233         $res = "\n/* Header field declarations */\n";
1234
1235         foreach (keys %{$self->{conformance}->{header_fields}}) 
1236         {
1237                 $res .= "static gint $_ = -1;\n";
1238         }
1239
1240         return "$res\n";
1241 }
1242
1243 sub make_str_or_null($)
1244 {
1245       my $str = shift;
1246       if (substr($str, 0, 1) eq "\"") {
1247               $str = substr($str, 1, length($str)-2);
1248       }
1249       $str =~ s/^\s*//;
1250       $str =~ s/\s*$//;
1251       if ($str eq "") {
1252               return "NULL";
1253       }
1254       return make_str($str);
1255 }
1256
1257 sub DumpHfList($)
1258 {
1259         my ($self) = @_;
1260         my $res = "\tstatic hf_register_info hf[] = {\n";
1261
1262         foreach (values %{$self->{conformance}->{header_fields}}) 
1263         {
1264                 $res .= "\t{ &$_->{INDEX},
1265           { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str_or_null($_->{BLURB}).", HFILL }},
1266 ";
1267         }
1268
1269         return $res."\t};\n";
1270 }
1271
1272
1273 ###############################################################################
1274 # Function table
1275 ###############################################################################
1276
1277 sub DumpFunctionTable($)
1278 {
1279         my $if = shift;
1280
1281         my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
1282         foreach (@{$if->{FUNCTIONS}}) {
1283                 my $fn_name = $_->{NAME};
1284                 $fn_name =~ s/^$if->{NAME}_//;
1285                 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1286                 $res.= "\t   $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1287         }
1288
1289         $res .= "\t{ 0, NULL, NULL, NULL }\n";
1290
1291         return "$res};\n";
1292 }
1293
1294 sub CheckUsed($$)
1295 {
1296         my ($self, $conformance) = @_;
1297         foreach (values %{$conformance->{header_fields}}) {
1298                 if (not defined($self->{hf_used}->{$_->{INDEX}})) {
1299                         warning($_->{POS}, "hf field `$_->{INDEX}' not used");
1300                 }
1301         }
1302
1303         foreach (values %{$conformance->{hf_renames}}) {
1304                 if (not $_->{USED}) {
1305                         warning($_->{POS}, "hf field `$_->{OLDNAME}' not used");
1306                 }
1307         }
1308
1309         foreach (values %{$conformance->{dissectorparams}}) {
1310                 if (not $_->{USED}) {
1311                         warning($_->{POS}, "dissector param never used");
1312                 }
1313         }
1314
1315         foreach (values %{$conformance->{imports}}) {
1316                 if (not $_->{USED}) {
1317                         warning($_->{POS}, "import never used");
1318                 }
1319         }
1320
1321         foreach (values %{$conformance->{types}}) {
1322                 if (not $_->{USED} and defined($_->{POS})) {
1323                         warning($_->{POS}, "type never used");
1324                 }
1325         }
1326
1327         foreach (values %{$conformance->{fielddescription}}) {
1328                 if (not $_->{USED}) {
1329                         warning($_->{POS}, "description never used");
1330                 }
1331         }
1332
1333         foreach (values %{$conformance->{tfs}}) {
1334                 if (not $_->{USED}) {
1335                         warning($_->{POS}, "True/False description never used");
1336                 }
1337         }
1338 }
1339
1340 1;