Update 3GPP AVP:s
[obnox/wireshark/wip.git] / tools / pidl / lib / Parse / Pidl / Wireshark / NDR.pm
1 ##################################################
2 # Samba4 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-2005
6 # Portions based on idl2wrs.c by Ronnie Sahlberg
7 # released under the GNU GPL
8
9 =pod
10
11 =head1 NAME
12
13 Parse::Pidl::Ethereal::NDR - Parser generator for Wireshark
14
15 =cut
16
17 package Parse::Pidl::Ethereal::NDR;
18
19 use strict;
20 use Parse::Pidl::Typelist qw(getType);
21 use Parse::Pidl::Util qw(has_property ParseExpr property_matches make_str);
22 use Parse::Pidl::NDR qw(ContainsString GetNextLevel);
23 use Parse::Pidl::Dump qw(DumpTypedef DumpFunction);
24 use Parse::Pidl::Ethereal::Conformance qw(ReadConformance);
25 use File::Basename;     
26
27 use vars qw($VERSION);
28 $VERSION = '0.01';
29
30 sub error($$)
31 {
32         my ($e,$t) = @_;
33         print "$e->{FILE}:$e->{LINE}: $t\n";
34 }
35
36 my @ett;
37
38 my %hf_used = ();
39 my %dissector_used = ();
40
41 my $conformance = undef;
42
43 my %ptrtype_mappings = (
44         "unique" => "NDR_POINTER_UNIQUE",
45         "ref" => "NDR_POINTER_REF",
46         "ptr" => "NDR_POINTER_PTR"
47 );
48
49 sub StripPrefixes($)
50 {
51         my ($s) = @_;
52
53         foreach (@{$conformance->{strip_prefixes}}) {
54                 $s =~ s/^$_\_//g;
55         }
56
57         return $s;
58 }
59
60 # Convert a IDL structure field name (e.g access_mask) to a prettier
61 # string like 'Access Mask'.
62
63 sub field2name($)
64 {
65     my($field) = shift;
66
67     $field =~ s/_/ /g;          # Replace underscores with spaces
68     $field =~ s/(\w+)/\u\L$1/g; # Capitalise each word
69     
70     return $field;
71 }
72
73 my %res = ();
74 my $tabs = "";
75 sub pidl_code($)
76 {
77         my $d = shift;
78         if ($d) {
79                 $res{code} .= $tabs;
80                 $res{code} .= $d;
81         }
82         $res{code} .="\n";
83 }
84
85 sub pidl_hdr($) { my $x = shift; $res{hdr} .= "$x\n"; }
86 sub pidl_def($) { my $x = shift; $res{def} .= "$x\n"; }
87
88 sub indent()
89 {
90         $tabs .= "\t";
91 }
92
93 sub deindent()
94 {
95         $tabs = substr($tabs, 0, -1);
96 }
97
98 sub PrintIdl($)
99 {
100         my $idl = shift;
101
102         foreach (split /\n/, $idl) {
103                 pidl_code "/* IDL: $_ */";
104         }
105
106         pidl_code "";
107 }
108
109 #####################################################################
110 # parse the interface definitions
111 sub Interface($)
112 {
113         my($interface) = @_;
114         Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}});
115         Typedef($_,$interface->{NAME}) foreach (@{$interface->{TYPES}});
116         Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
117 }
118
119 sub Enum($$$)
120 {
121         my ($e,$name,$ifname) = @_;
122         my $valsstring = "$ifname\_$name\_vals";
123         my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name);
124
125         return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
126
127         foreach (@{$e->{ELEMENTS}}) {
128                 if (/([^=]*)=(.*)/) {
129                         pidl_hdr "#define $1 ($2)";
130                 }
131         }
132         
133         pidl_hdr "extern const value_string $valsstring\[];";
134         pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
135
136         pidl_def "const value_string ".$valsstring."[] = {";
137         foreach (@{$e->{ELEMENTS}}) {
138                 next unless (/([^=]*)=(.*)/);
139                 pidl_def "\t{ $1, \"$1\" },";
140         }
141
142         pidl_def "{ 0, NULL }";
143         pidl_def "};";
144
145         pidl_code "int";
146         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param _U_)";
147         pidl_code "{";
148         indent;
149         pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, NULL);";
150         pidl_code "return offset;";
151         deindent;
152         pidl_code "}\n";
153
154         my $enum_size = $e->{BASE_TYPE};
155         $enum_size =~ s/uint//g;
156         register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
157 }
158
159 sub Bitmap($$$)
160 {
161         my ($e,$name,$ifname) = @_;
162         my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name);
163
164         register_ett("ett_$ifname\_$name");
165
166         pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
167
168         pidl_code "int";
169         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
170         pidl_code "{";
171         indent;
172         pidl_code "proto_item *item = NULL;";
173         pidl_code "proto_tree *tree = NULL;";
174         pidl_code "";
175                 
176         pidl_code "g$e->{BASE_TYPE} flags;";
177         if ($e->{ALIGN} > 1) {
178                 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
179         }
180
181         pidl_code "";
182
183         pidl_code "if (parent_tree) {";
184         indent;
185         pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, TRUE);";
186         pidl_code "tree = proto_item_add_subtree(item,ett_$ifname\_$name);";
187         deindent;
188         pidl_code "}\n";
189
190         pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, NULL, drep, -1, &flags);";
191
192         pidl_code "proto_item_append_text(item, \": \");\n";
193         pidl_code "if (!flags)";
194         pidl_code "\tproto_item_append_text(item, \"(No values set)\");\n";
195
196         foreach (@{$e->{ELEMENTS}}) {
197                 next unless (/([^ ]*) (.*)/);
198                 my ($en,$ev) = ($1,$2);
199                 my $hf_bitname = "hf_$ifname\_$name\_$en";
200                 my $filtername = "$ifname\.$name\.$en";
201
202                 $hf_used{$hf_bitname} = 1;
203                 
204                 register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", $ev, "");
205
206                 pidl_def "static const true_false_string $name\_$en\_tfs = {";
207                 if (defined($conformance->{tfs}->{$hf_bitname})) {
208                         pidl_def "   $conformance->{tfs}->{$hf_bitname}->{TRUE_STRING},";
209                         pidl_def "   $conformance->{tfs}->{$hf_bitname}->{FALSE_STRING},";
210                         $conformance->{tfs}->{$hf_bitname}->{USED} = 1;
211                 } else {
212                         pidl_def "   \"$en is SET\",";
213                         pidl_def "   \"$en is NOT SET\",";
214                 }
215                 pidl_def "};";
216                 
217                 pidl_code "proto_tree_add_boolean(tree, $hf_bitname, tvb, offset-$e->{ALIGN}, $e->{ALIGN}, flags);";
218                 pidl_code "if (flags&$ev){";
219                 pidl_code "\tproto_item_append_text(item, \"$en\");";
220                 pidl_code "\tif (flags & (~$ev))";
221                 pidl_code "\t\tproto_item_append_text(item, \", \");";
222                 pidl_code "}";
223                 pidl_code "flags&=(~$ev);";
224                 pidl_code "";
225         }
226
227         pidl_code "if (flags) {";
228         pidl_code "\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);";
229         pidl_code "}\n";
230         pidl_code "return offset;";
231         deindent;
232         pidl_code "}\n";
233
234         my $size = $e->{BASE_TYPE};
235         $size =~ s/uint//g;
236         register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8);
237 }
238
239 sub ElementLevel($$$$$)
240 {
241         my ($e,$l,$hf,$myname,$pn) = @_;
242
243         my $param = 0;
244
245         if (defined($conformance->{dissectorparams}->{$myname})) {
246                 $conformance->{dissectorparams}->{$myname}->{PARAM} = 1;
247                 $param = $conformance->{dissectorparams}->{$myname}->{PARAM};
248         }
249
250         if ($l->{TYPE} eq "POINTER") {
251                 my $type;
252                 if ($l->{LEVEL} eq "TOP") {
253                         $type = "toplevel";
254                 } elsif ($l->{LEVEL} eq "EMBEDDED") {
255                         $type = "embedded";
256                 }
257                 pidl_code "offset = dissect_ndr_$type\_pointer(tvb, offset, pinfo, tree, drep, $myname\_, $ptrtype_mappings{$l->{POINTER_TYPE}}, \"Pointer to ".field2name(StripPrefixes($e->{NAME})) . " ($e->{TYPE})\",$hf);";
258         } elsif ($l->{TYPE} eq "ARRAY") {
259                 if ($l->{IS_INLINE}) {
260                         error($e->{ORIGINAL}, "Inline arrays not supported");
261                 } elsif ($l->{IS_FIXED}) {
262                         pidl_code "int i;";
263                         pidl_code "for (i = 0; i < $l->{SIZE_IS}; i++)";
264                         pidl_code "\toffset = $myname\_(tvb, offset, pinfo, tree, drep);";
265                 } else {
266                         my $type = "";
267                         $type .= "c" if ($l->{IS_CONFORMANT});
268                         $type .= "v" if ($l->{IS_VARYING});
269
270                         unless ($l->{IS_ZERO_TERMINATED}) {
271                                 pidl_code "offset = dissect_ndr_u" . $type . "array(tvb, offset, pinfo, tree, drep, $myname\_);";
272                         } else {
273                                 my $nl = GetNextLevel($e,$l);
274                                 pidl_code "char *data;";
275                                 pidl_code "";
276                                 pidl_code "offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, drep, sizeof(g$nl->{DATA_TYPE}), $hf, FALSE, &data);";
277                                 pidl_code "proto_item_append_text(tree, \": %s\", data);";
278                         }
279                 }
280         } elsif ($l->{TYPE} eq "DATA") {
281                 if ($l->{DATA_TYPE} eq "string") {
282                         my $bs = 2; # Byte size defaults to that of UCS2
283
284
285                         ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
286                         
287                         if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
288                                 pidl_code "char *data;\n";
289                                 pidl_code "offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, &data);";
290                                 pidl_code "proto_item_append_text(tree, \": %s\", data);";
291                         } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
292                                 pidl_code "offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, NULL);";
293                         } else {
294                                 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
295                         }
296                 } else {
297                         my $call;
298
299                         if ($conformance->{imports}->{$l->{DATA_TYPE}}) {
300                                 $call = $conformance->{imports}->{$l->{DATA_TYPE}}->{DATA};     
301                                 $conformance->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
302                         } elsif (defined($conformance->{imports}->{"$pn.$e->{NAME}"})) {
303                                 $call = $conformance->{imports}->{"$pn.$e->{NAME}"}->{DATA};
304                                 $conformance->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
305                             
306                         } elsif (defined($conformance->{types}->{$l->{DATA_TYPE}})) {
307                                 $call= $conformance->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
308                                 $conformance->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
309                         } else {
310                                 if ($l->{DATA_TYPE} =~ /^([a-z]+)\_(.*)$/)
311                                 {
312                                         pidl_code "offset = $1_dissect_struct_$2(tvb,offset,pinfo,tree,drep,$hf,$param);";
313                                 }
314
315                                 return;
316                         }
317
318                         $call =~ s/\@HF\@/$hf/g;
319                         $call =~ s/\@PARAM\@/$param/g;
320                         pidl_code "$call";
321                 }
322         } elsif ($_->{TYPE} eq "SUBCONTEXT") {
323                 my $num_bits = ($l->{HEADER_SIZE}*8);
324                 pidl_code "guint$num_bits size;";
325                 pidl_code "int start_offset = offset;";
326                 pidl_code "tvbuff_t *subtvb;";
327                 pidl_code "offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf, &size);";
328                 pidl_code "proto_tree_add_text(tree, tvb, start_offset, offset - start_offset + size, \"Subcontext size\");";
329
330                 pidl_code "subtvb = tvb_new_subset(tvb, offset, size, -1);";
331                 pidl_code "$myname\_(subtvb, 0, pinfo, tree, drep);";
332         } else {
333                 die("Unknown type `$_->{TYPE}'");
334         }
335 }
336
337 sub Element($$$)
338 {
339         my ($e,$pn,$ifname) = @_;
340
341         my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn)."\_".StripPrefixes($e->{NAME});
342
343         my $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
344
345         my $type = find_type($e->{TYPE});
346
347         if (not defined($type)) {
348                 # default settings
349                 $type = {
350                         MASK => 0,
351                         VALSSTRING => "NULL",
352                         FT_TYPE => "FT_NONE",
353                         BASE_TYPE => "BASE_HEX"
354                 };
355         }
356
357         if (ContainsString($e)) {
358                 $type = {
359                         MASK => 0,
360                         VALSSTRING => "NULL",
361                         FT_TYPE => "FT_STRING",
362                         BASE_TYPE => "BASE_DEC"
363                 };
364         }
365
366         my $hf = 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}, "");
367         $hf_used{$hf} = 1;
368
369         my $eltname = StripPrefixes($pn) . ".$e->{NAME}";
370         if (defined($conformance->{noemit}->{$eltname})) {
371                 return $call_code;
372         }
373
374         my $add = "";
375
376         foreach (@{$e->{LEVELS}}) {
377                 next if ($_->{TYPE} eq "SWITCH");
378                 pidl_def "static int $dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep);";
379                 pidl_code "static int";
380                 pidl_code "$dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)";
381                 pidl_code "{";
382                 indent;
383
384                 ElementLevel($e,$_,$hf,$dissectorname.$add,$pn);
385
386                 pidl_code "";
387                 pidl_code "return offset;";
388                 deindent;
389                 pidl_code "}\n";
390                 $add.="_";
391                 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
392         }
393
394         return $call_code;
395 }
396
397 sub Function($$$)
398 {
399         my ($fn,$ifname) = @_;
400
401         my %dissectornames;
402
403         foreach (@{$fn->{ELEMENTS}}) {
404             $dissectornames{$_->{NAME}} = Element($_, $fn->{NAME}, $ifname) if not defined($dissectornames{$_->{NAME}});
405         }
406         
407         my $fn_name = $_->{NAME};
408         $fn_name =~ s/^${ifname}_//;
409
410         PrintIdl DumpFunction($fn->{ORIGINAL});
411         pidl_code "static int";
412         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_)";
413         pidl_code "{";
414         indent;
415         pidl_code "guint32 status;\n";
416         foreach (@{$fn->{ELEMENTS}}) {
417                 if (grep(/out/,@{$_->{DIRECTION}})) {
418                         pidl_code "$dissectornames{$_->{NAME}}";
419                         pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
420                         pidl_code "";
421                 }
422         }
423
424         if (not defined($fn->{RETURN_TYPE})) {
425         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
426                 pidl_code "offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n";
427                 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
428                 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n";
429                 $hf_used{"hf\_$ifname\_status"} = 1;
430         } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
431                 pidl_code "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &status);\n";
432                 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
433                 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, DOS_errors, \"Unknown DOS error 0x%08x\"));\n";
434                 
435                 $hf_used{"hf\_$ifname\_werror"} = 1;
436         } else {
437                 print "$fn->{FILE}:$fn->{LINE}: error: return type `$fn->{RETURN_TYPE}' not yet supported\n";
438         }
439                 
440
441         pidl_code "return offset;";
442         deindent;
443         pidl_code "}\n";
444
445         pidl_code "static int";
446         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_)";
447         pidl_code "{";
448         indent;
449         foreach (@{$fn->{ELEMENTS}}) {
450                 if (grep(/in/,@{$_->{DIRECTION}})) {
451                         pidl_code "$dissectornames{$_->{NAME}}";
452                         pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
453                 }
454
455         }
456
457         pidl_code "return offset;";
458         deindent;
459         pidl_code "}\n";
460 }
461
462 sub Struct($$$)
463 {
464         my ($e,$name,$ifname) = @_;
465         my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name);
466
467         return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
468
469         register_ett("ett_$ifname\_$name");
470
471         my $res = "";
472         ($res.="\t".Element($_, $name, $ifname)."\n\n") foreach (@{$e->{ELEMENTS}});
473
474         pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_);";
475
476         pidl_code "int";
477         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
478         pidl_code "{";
479         indent;
480         pidl_code "proto_item *item = NULL;";
481         pidl_code "proto_tree *tree = NULL;";
482         pidl_code "int old_offset;";
483         pidl_code "";
484
485         if ($e->{ALIGN} > 1) {
486                 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
487         }
488         pidl_code "";
489
490         pidl_code "old_offset = offset;";
491         pidl_code "";
492         pidl_code "if (parent_tree) {";
493         indent;
494         pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, TRUE);";
495         pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
496         deindent;
497         pidl_code "}";
498
499         pidl_code "\n$res";
500
501         pidl_code "proto_item_set_len(item, offset-old_offset);\n";
502         pidl_code "return offset;";
503         deindent;
504         pidl_code "}\n";
505
506         register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
507 }
508
509 sub Union($$$)
510 {
511         my ($e,$name,$ifname) = @_;
512
513         my $dissectorname = "$ifname\_dissect_".StripPrefixes($name);
514
515         return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
516         
517         register_ett("ett_$ifname\_$name");
518
519         my $res = "";
520         foreach (@{$e->{ELEMENTS}}) {
521                 $res.="\n\t\t$_->{CASE}:\n";
522                 if ($_->{TYPE} ne "EMPTY") {
523                         $res.="\t\t\t".Element($_, $name, $ifname)."\n";
524                 }
525                 $res.="\t\tbreak;\n";
526         }
527
528         my $switch_type;
529         my $switch_dissect;
530         my $switch_dt = getType($e->{SWITCH_TYPE});
531         if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
532                 $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt);
533                 $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt);
534         } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
535                 $switch_type = "g$e->{SWITCH_TYPE}";
536                 $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
537         }
538
539         pidl_code "static int";
540         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
541         pidl_code "{";
542         indent;
543         pidl_code "proto_item *item = NULL;";
544         pidl_code "proto_tree *tree = NULL;";
545         pidl_code "int old_offset;";
546         pidl_code "$switch_type level;";
547         pidl_code "";
548
549         if ($e->{ALIGN} > 1) {
550                 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
551         }
552
553         pidl_code "";
554
555         pidl_code "old_offset = offset;";
556         pidl_code "if (parent_tree) {";
557         indent;
558         pidl_code "item = proto_tree_add_text(parent_tree, tvb, offset, -1, \"$name\");";
559         pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
560         deindent;
561         pidl_code "}";
562
563         pidl_code "";
564
565         pidl_code "offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);";
566
567         pidl_code "switch(level) {$res\t}";
568         pidl_code "proto_item_set_len(item, offset-old_offset);\n";
569         pidl_code "return offset;";
570         deindent;
571         pidl_code "}";
572
573         register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
574 }
575
576 sub Const($$)
577 {
578         my ($const,$ifname) = @_;
579         
580         if (!defined($const->{ARRAY_LEN}[0])) {
581                 pidl_hdr "#define $const->{NAME}\t( $const->{VALUE} )\n";
582         } else {
583                 pidl_hdr "#define $const->{NAME}\t $const->{VALUE}\n";
584         }
585 }
586
587 sub Typedef($$)
588 {
589         my ($e,$ifname) = @_;
590
591         PrintIdl DumpTypedef($e->{ORIGINAL});
592
593         {
594                 ENUM => \&Enum,
595                 STRUCT => \&Struct,
596                 UNION => \&Union,
597                 BITMAP => \&Bitmap
598         }->{$e->{DATA}->{TYPE}}->($e->{DATA}, $e->{NAME}, $ifname);
599 }
600
601 sub RegisterInterface($)
602 {
603         my ($x) = @_;
604
605         pidl_code "void proto_register_dcerpc_$x->{NAME}(void)";
606         pidl_code "{";
607         indent;
608
609         $res{code}.=DumpHfList()."\n";
610         $res{code}.="\n".DumpEttList()."\n";
611         
612         if (defined($x->{UUID})) {
613             # These can be changed to non-pidl_code names if the old dissectors
614             # in epan/dissctors are deleted.
615     
616             my $name = uc($x->{NAME}) . " (pidl)";
617             my $short_name = uc($x->{NAME});
618             my $filter_name = $x->{NAME};
619
620             if (has_property($x, "helpstring")) {
621                 $name = $x->{PROPERTIES}->{helpstring};
622             }
623
624             if (defined($conformance->{protocols}->{$x->{NAME}})) {
625                 $short_name = $conformance->{protocols}->{$x->{NAME}}->{SHORTNAME};
626                 $name = $conformance->{protocols}->{$x->{NAME}}->{LONGNAME};
627                 $filter_name = $conformance->{protocols}->{$x->{NAME}}->{FILTERNAME};
628             }
629
630             pidl_code "proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");";
631             
632             pidl_code "proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));";
633             pidl_code "proto_register_subtree_array(ett, array_length(ett));";
634         } else {
635             pidl_code "proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");";
636             pidl_code "proto_register_field_array(proto_dcerpc, hf, array_length(hf));";
637             pidl_code "proto_register_subtree_array(ett, array_length(ett));";
638         }
639             
640         deindent;
641         pidl_code "}\n";
642 }
643
644 sub RegisterInterfaceHandoff($)
645 {
646         my $x = shift;
647
648         if (defined($x->{UUID})) {
649             pidl_code "void proto_reg_handoff_dcerpc_$x->{NAME}(void)";
650             pidl_code "{";
651             indent;
652             pidl_code "dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},";
653             pidl_code "\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},";
654             pidl_code "\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);";
655             deindent;
656             pidl_code "}";
657
658                 $hf_used{"hf_$x->{NAME}_opnum"} = 1;
659         }
660 }
661
662 sub ProcessInterface($)
663 {
664         my ($x) = @_;
665
666         push(@{$conformance->{strip_prefixes}}, $x->{NAME});
667
668         my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
669         pidl_hdr "#ifndef $define";
670         pidl_hdr "#define $define";
671         pidl_hdr "";
672
673         if (defined $x->{PROPERTIES}->{depends}) {
674                 foreach (split / /, $x->{PROPERTIES}->{depends}) {
675                         next if($_ eq "security");
676                         pidl_hdr "#include \"packet-dcerpc-$_\.h\"\n";
677                 }
678         }
679
680         pidl_def "static gint proto_dcerpc_$x->{NAME} = -1;";
681         register_ett("ett_dcerpc_$x->{NAME}");
682         register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
683
684         if (defined($x->{UUID})) {
685                 my $if_uuid = $x->{UUID};
686
687             pidl_def "/* Version information */\n\n";
688             
689             pidl_def "static e_uuid_t uuid_dcerpc_$x->{NAME} = {";
690             pidl_def "\t0x" . substr($if_uuid, 1, 8) 
691                 . ", 0x" . substr($if_uuid, 10, 4)
692             . ", 0x" . substr($if_uuid, 15, 4) . ",";
693             pidl_def "\t{ 0x" . substr($if_uuid, 20, 2) 
694                 . ", 0x" . substr($if_uuid, 22, 2)
695             . ", 0x" . substr($if_uuid, 25, 2)
696             . ", 0x" . substr($if_uuid, 27, 2)
697             . ", 0x" . substr($if_uuid, 29, 2)
698             . ", 0x" . substr($if_uuid, 31, 2)
699             . ", 0x" . substr($if_uuid, 33, 2)
700             . ", 0x" . substr($if_uuid, 35, 2) . " }";
701             pidl_def "};";
702         
703             my $maj = $x->{VERSION};
704             $maj =~ s/\.(.*)$//g;
705             pidl_def "static guint16 ver_dcerpc_$x->{NAME} = $maj;";
706             pidl_def "";
707         }
708
709         Interface($x);
710
711         pidl_code "\n".DumpFunctionTable($x);
712
713         # Only register these two return types if they were actually used
714         if (defined($hf_used{"hf_$x->{NAME}_status"})) {
715                 register_hf_field("hf_$x->{NAME}_status", "Status", "$x->{NAME}.status", "FT_UINT32", "BASE_HEX", "VALS(NT_errors)", 0, "");
716         }
717
718         if (defined($hf_used{"hf_$x->{NAME}_werror"})) {
719                 register_hf_field("hf_$x->{NAME}_werror", "Windows Error", "$x->{NAME}.werror", "FT_UINT32", "BASE_HEX", "VALS(DOS_errors)", 0, "");
720         }
721
722         RegisterInterface($x);
723         RegisterInterfaceHandoff($x);
724
725         pidl_hdr "#endif /* $define */";
726 }
727
728 sub find_type($)
729 {
730         my $n = shift;
731
732         return $conformance->{types}->{$n};
733 }
734
735 sub register_type($$$$$$$)
736 {
737         my ($type,$call,$ft,$base,$mask,$vals,$length) = @_;
738
739         $conformance->{types}->{$type} = {
740                 NAME => $type,
741                 DISSECTOR_NAME => $call,
742                 FT_TYPE => $ft,
743                 BASE_TYPE => $base,
744                 MASK => $mask,
745                 VALSSTRING => $vals,
746                 ALIGNMENT => $length
747         };
748 }
749
750 # Loads the default types
751 sub Initialize($)
752 {
753         my $cnf_file = shift;
754
755         $conformance = {
756                 imports => {},
757                 header_fields=> {} 
758         };
759
760         ReadConformance($cnf_file, $conformance) or print "Warning: No conformance file `$cnf_file'\n";
761         
762         foreach my $bytes (qw(1 2 4 8)) {
763                 my $bits = $bytes * 8;
764                 register_type("uint$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@,NULL);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
765                 register_type("int$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
766         }
767                 
768         register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
769         register_type("bool8", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
770         register_type("char", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
771         register_type("long", "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
772         register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
773         register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
774         register_type("policy_handle", "offset = dissect_nt_policy_hnd(tvb, offset, pinfo, tree, drep, \@HF\@, NULL, NULL, \@PARAM\@&0x01, \@PARAM\@&0x02);","FT_BYTES", "BASE_NONE", 0, "NULL", 4);
775         register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
776         register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
777         register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "BASE_DEC", 0, "NULL", 4);
778         register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
779         register_type("SID", "
780                 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
781
782                 di->hf_index = \@HF\@;
783
784                 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
785         ","FT_STRING", "BASE_DEC", 0, "NULL", 4);
786         register_type("WERROR", 
787                 "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
788
789 }
790
791 #####################################################################
792 # Generate ethereal parser and header code
793 sub Parse($$$$)
794 {
795         my($ndr,$idl_file,$h_filename,$cnf_file) = @_;
796         Initialize($cnf_file);
797
798         return (undef, undef) if defined($conformance->{noemit_dissector});
799
800         $tabs = "";
801
802         %res = (code=>"",def=>"",hdr=>"");
803         @ett = ();
804
805         my $notice = 
806 "/* DO NOT EDIT
807         This filter was automatically generated
808         from $idl_file and $cnf_file.
809         
810         Pidl is a perl based IDL compiler for DCE/RPC idl files. 
811         It is maintained by the Samba team, not the Wireshark team.
812         Instructions on how to download and install Pidl can be 
813         found at http://wiki.wireshark.org/Pidl
814 */
815
816 ";
817
818         pidl_hdr $notice;
819
820         $res{headers} = "\n";
821         $res{headers} .= "#ifdef HAVE_CONFIG_H\n";
822         $res{headers} .= "#include \"config.h\"\n";
823         $res{headers} .= "#endif\n\n";
824         $res{headers} .= "#include <glib.h>\n";
825         $res{headers} .= "#include <string.h>\n";
826         $res{headers} .= "#include <epan/packet.h>\n\n";
827
828         $res{headers} .= "#include \"packet-dcerpc.h\"\n";
829         $res{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
830         $res{headers} .= "#include \"packet-windows-common.h\"\n";
831
832         my $h_basename = basename($h_filename);
833
834         $res{headers} .= "#include \"$h_basename\"\n";
835         pidl_code "";
836
837         # Ethereal protocol registration
838
839         ProcessInterface($_) foreach (@$ndr);
840
841         $res{ett} = DumpEttDeclaration();
842         $res{hf} = DumpHfDeclaration();
843
844         my $parser = $notice;
845         $parser.= $res{headers};
846         $parser.=$res{ett};
847         $parser.=$res{hf};
848         $parser.=$res{def};
849         $parser.=$conformance->{override};
850         $parser.=$res{code};
851
852         my $header = "/* autogenerated by pidl */\n\n";
853         $header.=$res{hdr};
854
855         CheckUsed($conformance);
856     
857         return ($parser,$header);
858 }
859
860 ###############################################################################
861 # ETT
862 ###############################################################################
863
864 sub register_ett($)
865 {
866         my $name = shift;
867
868         push (@ett, $name);     
869 }
870
871 sub DumpEttList()
872 {
873         my $res = "\tstatic gint *ett[] = {\n";
874         foreach (@ett) {
875                 $res .= "\t\t&$_,\n";
876         }
877
878         return "$res\t};\n";
879 }
880
881 sub DumpEttDeclaration()
882 {
883         my $res = "\n/* Ett declarations */\n";
884         foreach (@ett) {
885                 $res .= "static gint $_ = -1;\n";
886         }
887
888         return "$res\n";
889 }
890
891 ###############################################################################
892 # HF
893 ###############################################################################
894
895 sub register_hf_field($$$$$$$$) 
896 {
897         my ($index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
898
899         if (defined ($conformance->{hf_renames}->{$index})) {
900                 $conformance->{hf_renames}->{$index}->{USED} = 1;
901                 return $conformance->{hf_renames}->{$index}->{NEWNAME};
902         }
903
904         $conformance->{header_fields}->{$index} = {
905                 INDEX => $index,
906                 NAME => $name,
907                 FILTER => $filter_name,
908                 FT_TYPE => $ft_type,
909                 BASE_TYPE => $base_type,
910                 VALSSTRING => $valsstring,
911                 MASK => $mask,
912                 BLURB => $blurb
913         };
914
915         if ((not defined($blurb) or $blurb eq "") and 
916                         defined($conformance->{fielddescription}->{$index})) {
917                 $conformance->{header_fields}->{$index}->{BLURB} = 
918                         $conformance->{fielddescription}->{$index}->{DESCRIPTION};
919                 $conformance->{fielddescription}->{$index}->{USED} = 1;
920         }
921
922         return $index;
923 }
924
925 sub DumpHfDeclaration()
926 {
927         my $res = "";
928
929         $res = "\n/* Header field declarations */\n";
930
931         foreach (keys %{$conformance->{header_fields}}) 
932         {
933                 $res .= "static gint $_ = -1;\n";
934         }
935
936         return "$res\n";
937 }
938
939 sub DumpHfList()
940 {
941         my $res = "\tstatic hf_register_info hf[] = {\n";
942
943         foreach (values %{$conformance->{header_fields}}) 
944         {
945                 $res .= "\t{ &$_->{INDEX}, 
946           { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str($_->{BLURB}).", HFILL }},
947 ";
948         }
949
950         return $res."\t};\n";
951 }
952
953
954 ###############################################################################
955 # Function table
956 ###############################################################################
957
958 sub DumpFunctionTable($)
959 {
960         my $if = shift;
961
962         my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
963         foreach (@{$if->{FUNCTIONS}}) {
964                 my $fn_name = $_->{NAME};
965                 $fn_name =~ s/^$if->{NAME}_//;
966                 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
967                 $res.= "\t   $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
968         }
969
970         $res .= "\t{ 0, NULL, NULL, NULL }\n";
971
972         return "$res};\n";
973 }
974
975 sub CheckUsed($)
976 {
977         my $conformance = shift;
978         foreach (values %{$conformance->{header_fields}}) {
979                 if (not defined($hf_used{$_->{INDEX}})) {
980                         print "$_->{POS}: warning: hf field `$_->{INDEX}' not used\n";
981                 }
982         }
983
984         foreach (values %{$conformance->{hf_renames}}) {
985                 if (not $_->{USED}) {
986                         print "$_->{POS}: warning: hf field `$_->{OLDNAME}' not used\n";
987                 }
988         }
989
990         foreach (values %{$conformance->{dissectorparams}}) {
991                 if (not $_->{USED}) {
992                         print "$_->{POS}: warning: dissector param never used\n";
993                 }
994         }
995
996         foreach (values %{$conformance->{imports}}) {
997                 if (not $_->{USED}) {
998                         print "$_->{POS}: warning: import never used\n";
999                 }
1000         }
1001
1002         foreach (values %{$conformance->{types}}) {
1003                 if (not $_->{USED} and defined($_->{POS})) {
1004                         print "$_->{POS}: warning: type never used\n";
1005                 }
1006         }
1007
1008         foreach (values %{$conformance->{fielddescription}}) {
1009                 if (not $_->{USED}) {
1010                         print "$_->{POS}: warning: description never used\n";
1011                 }
1012         }
1013
1014         foreach (values %{$conformance->{tfs}}) {
1015                 if (not $_->{USED}) {
1016                         print "$_->{POS}: warning: True/False description never used\n";
1017                 }
1018         }
1019 }
1020
1021 1;