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