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