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