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