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