1 ###################################################
2 # Samba4 parser generator for IDL structures
3 # Copyright tridge@samba.org 2000-2003
4 # Copyright tpot@samba.org 2001,2004-2005
5 # released under the GNU GPL
11 # the list of needed functions
24 #####################################################################
25 # a list of annotations
27 my $nopull_typedefs = {
28 # "policy_handle" => "1",
31 #####################################################################
32 # work out is a parse function should be declared static or not
36 if ($fn->{TYPE
} eq "TYPEDEF") {
37 if (util
::has_property
($fn, "public")) {
42 if ($fn->{TYPE
} eq "FUNCTION") {
43 if (util
::has_property
($fn, "public")) {
51 #####################################################################
53 sub ParseFunctionPull
($)
56 my $static = fn_prefix
($fn);
59 pidl
"int $fn->{NAME}_rqst(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)\n{\n";
61 pidl
"\tstruct pidl_pull *ndr = pidl_pull_init(tvb, offset, pinfo, drep);\n";
62 pidl
"\tstruct $fn->{NAME} *r = talloc_p(NULL, struct $fn->{NAME});\n";
63 pidl
"\tpidl_tree ptree;\n\n";
65 pidl
"\tptree.proto_tree = tree;\n";
66 pidl
"\tptree.subtree_list = NULL;\n\n";
68 pidl
"\tndr_pull_$fn->{NAME}(ndr, NDR_IN, &ptree, r);\n";
70 pidl
"\n\treturn ndr->offset;\n";
74 pidl
"int $fn->{NAME}_resp(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)\n{\n";
76 pidl
"\tstruct pidl_pull *ndr = pidl_pull_init(tvb, offset, pinfo, drep);\n";
77 pidl
"\tstruct $fn->{NAME} *r = talloc_p(NULL, struct $fn->{NAME});\n";
78 pidl
"\tpidl_tree ptree;\n\n";
80 pidl
"\tptree.proto_tree = tree;\n";
81 pidl
"\tptree.subtree_list = NULL;\n\n";
83 pidl
"\tndr_pull_$fn->{NAME}(ndr, NDR_OUT, &ptree, r);\n";
85 pidl
"\n\treturn ndr->offset;\n";
89 #####################################################################
90 # produce a function call table
93 my($interface) = shift;
94 my($data) = $interface->{DATA
};
96 pidl
"static dcerpc_sub_dissector dcerpc_dissectors[] = {\n";
98 foreach my $d (@
{$data}) {
99 if ($d->{TYPE
} eq "FUNCTION") {
100 # Strip module name from function name, if present
102 $n = substr($d->{NAME
}, length($module) + 1),
103 if $module eq substr($d->{NAME
}, 0, length($module));
104 pidl
"\t{ $num, \"$n\",\n";
105 pidl
"\t\t$d->{NAME}_rqst,\n";
106 pidl
"\t\t$d->{NAME}_resp },\n";
117 return "FT_UINT$1" if $t =~ /uint(8|16|32|64)/;
118 return "FT_INT$1" if $t =~ /int(8|16|32|64)/;
119 return "FT_UINT64", if ($t eq "HYPER_T" or $t eq "NTTIME");
126 # Determine the display base for an element
132 if (my $base = util
::has_property
($e, "display")) {
133 return "BASE_" . uc($base);
136 return "BASE_DEC", if $e->{TYPE
} eq "ENUM";
137 return "BASE_DEC", if $e->{TYPE
} =~ /u?int(8|16|32|64)/;
138 return "BASE_DEC", if $e->{TYPE
} eq "NTTIME" or $e->{TYPE
} eq "HYPER_T";
145 # Convert a IDL structure field name (e.g access_mask) to a prettier
146 # string like 'Access Mask'.
152 $field =~ s/_/ /g; # Replace underscores with spaces
153 $field =~ s/(\w+)/\u\L$1/g; # Capitalise each word
158 sub NeededFunction
($)
162 $needed{"pull_$fn->{NAME}"} = 1;
164 # Add entries for function arguments
166 foreach my $e (@
{$fn->{DATA
}}) {
169 $needed{"pull_$e->{TYPE}"} = 1;
171 if (util
::is_scalar_type
($e->{TYPE
})) {
173 if (defined($e->{ARRAY_LEN
}) or
174 util
::has_property
($e, "size_is")) {
176 # Array of scalar types
178 $needed{"hf_$fn->{NAME}_$e->{NAME}_array"} = {
179 'name' => field2name
($e->{NAME
}),
180 'type' => $e->{TYPE
},
182 'base' => elementbase
($e)
187 $needed{"hf_$fn->{NAME}_$e->{NAME}"} = {
188 'name' => field2name
($e->{NAME
}),
189 'type' => $e->{TYPE
},
190 'ft' => type2ft
($e->{TYPE
}),
191 'base' => elementbase
($e)
199 $needed{"ett_$e->{TYPE}"} = 1;
203 # Add entry for return value
205 $needed{"hf_$fn->{NAME}_result"} = {
206 'name' => field2name
('result'),
207 'type' => $fn->{RETURN_TYPE
},
208 'ft' => type2ft
($fn->{RETURN_TYPE
}),
209 'base' => elementbase
($fn)
217 if (util
::has_property
($t, "public")) {
218 $needed{"pull_$t->{NAME}"} = 1;
221 if ($t->{DATA
}->{TYPE
} eq "STRUCT") {
223 for my $e (@
{$t->{DATA
}->{ELEMENTS
}}) {
225 $e->{PARENT
} = $t->{DATA
};
227 if ($needed{"pull_$t->{NAME}"}) {
228 $needed{"pull_$e->{TYPE}"} = 1;
231 if (util
::is_scalar_type
($e->{TYPE
})) {
233 if (defined($e->{ARRAY_LEN
}) or
234 util
::has_property
($e, "size_is")) {
236 # Arrays of scalar types are FT_BYTES
238 $needed{"hf_$t->{NAME}_$e->{NAME}_array"} = {
239 'name' => field2name
($e->{NAME
}),
240 'type' => $e->{TYPE
},
242 'base' => elementbase
($e)
247 $needed{"hf_$t->{NAME}_$e->{NAME}"} = {
248 'name' => field2name
($e->{NAME
}),
249 'type' => $e->{TYPE
},
250 'ft' => type2ft
($e->{TYPE
}),
251 'base' => elementbase
($e)
255 $e->{PARENT
} = $t->{DATA
};
257 if ($needed{"pull_$t->{NAME}"}) {
258 $needed{"pull_$e->{TYPE}"} = 1;
263 $needed{"ett_$e->{TYPE}"} = 1;
269 if ($t->{DATA
}->{TYPE
} eq "UNION") {
271 for my $e (@
{$t->{DATA
}->{DATA
}}) {
273 $e->{PARENT
} = $t->{DATA
};
275 if ($e->{TYPE
} eq "UNION_ELEMENT") {
277 if ($needed{"pull_$t->{NAME}"}) {
278 $needed{"pull_$e->{DATA}->{TYPE}"} = 1;
281 $needed{"ett_$e->{DATA}{TYPE}"} = 1;
285 $needed{"ett_$t->{NAME}"} = 1;
288 if ($t->{DATA
}->{TYPE
} eq "ENUM") {
290 $needed{"hf_$t->{NAME}"} = {
291 'name' => $t->{NAME
},
298 #####################################################################
299 # work out what parse functions are needed
302 my($interface) = shift;
304 my($data) = $interface->{DATA
};
306 foreach my $d (@
{$data}) {
307 ($d->{TYPE
} eq "FUNCTION") &&
311 foreach my $d (reverse @
{$data}) {
312 ($d->{TYPE
} eq "TYPEDEF") &&
317 #####################################################################
318 # parse the interface definitions
323 $if_uuid = $h->{PROPERTIES
}->{uuid
};
324 $if_version = $h->{PROPERTIES
}->{version
};
325 $if_endpoints = $h->{PROPERTIES
}->{endpoints
};
328 #####################################################################
329 # Generate a header file that contains function prototypes for
330 # structs and typedefs.
334 my($filename) = shift;
336 open(OUT
, ">$filename") || die "can't open $filename";
338 pidl
"/* parser auto-generated by pidl */\n\n";
340 foreach my $x (@
{$idl}) {
341 if ($x->{TYPE
} eq "INTERFACE") {
342 foreach my $d (@
{$x->{DATA
}}) {
344 # Make prototypes for [public] structures and
347 if ($d->{TYPE
} eq "TYPEDEF" and
348 util
::has_property
($d, "public")) {
350 if ($d->{DATA
}{TYPE
} eq "STRUCT") {
351 pidl
"void ndr_pull_$d->{NAME}(struct ndr_pull *ndr, int ndr_flags, proto_tree *tree, struct $d->{NAME} *r);\n\n";
354 if ($d->{DATA
}{TYPE
} eq "UNION") {
355 pidl
"void ndr_pull_$d->{NAME}(struct ndr_pull *ndr, int ndr_flags, proto_tree *tree, union $d->{NAME} *r, uint16 level);\n\n";
365 #####################################################################
366 # rewrite autogenerated header file
367 sub RewriteHeader
($$$)
377 open(IN
, "<$input") || die "can't open $input for reading";
378 open(OUT
, ">$output") || die "can't open $output for writing";
384 # Not interested in ndr_push or ndr_print routines as they
385 # define structures we aren't interested in.
387 s/^NTSTATUS ndr_push.*?;\n//smg;
388 s/^void ndr_print.*?;\n//smg;
390 # Get rid of async send and receive function.
392 s/^NTSTATUS dcerpc_.*?;//smg;
393 s/^struct rpc_request.*?;//smg;
395 # Rewrite librpc includes
397 s/^\#include\ \"librpc\/gen_ndr\/ndr_
(.*?
).h
\"$
398 /\#include \"packet-dcerpc-$1.h\"/smgx;
400 # Convert samba fixed width types to stdint types
402 s/((u)?int)([0-9]+)/$1$3_t/smg;
404 # Rename struct ndr_pull to struct pidl_pull
406 s/struct ndr_pull \*ndr/struct pidl_pull \*ndr/smg;
408 # Change prototypes for public functions
410 s/(struct pidl_pull \*ndr, int ndr_flags)/$1, pidl_tree *tree/smg;
414 s/(uint32_t \*r\);)/pidl_tree *tree, int hf, $1/smg;
422 #####################################################################
423 # rewrite autogenerated C file
432 open(IN
, "<$input") || die "can't open $input for reading";
433 open(OUT
, ">$output") || die "can't open $output for writing";
437 foreach my $x (@
{$idl}) {
438 if ($x->{TYPE
} eq "INTERFACE") {
440 $module = $x->{NAME
};
445 pidl
"#include \"eparser.h\"\n\n";
447 pidl
"extern const value_string NT_errors[];\n\n";
449 # Declarations for hf variables
451 pidl
"static int hf_opnum = -1;\n";
452 pidl
"static int hf_ptr = -1;\n";
453 pidl
"static int hf_array_size = -1;\n";
454 pidl
"static int hf_result_NTSTATUS = -1;\n";
458 foreach my $y (keys(%needed)) {
459 pidl
"static int $y = -1;\n", if $y =~ /^hf_/;
464 foreach my $y (keys(%needed)) {
465 pidl
"static gint $y = -1;\n", if $y =~ /^ett_/;
477 # Regexps to do a first pass at removing stuff we aren't
478 # interested in for ehtereal parsers.
481 next, if /^\#include \"includes.h\"/;
483 # Remove the NDR_CHECK() macro calls. Ethereal take care of
484 # this for us as part of the tvbuff_t structure.
486 s/NDR_CHECK\((.*)\)/$1/g;
488 # We're not interested in ndr_{print,push,size} functions so
491 next, if /^(static )?NTSTATUS ndr_push/ .. /^}/;
492 next, if /^void ndr_print/ .. /^}/;
493 next, if /^size_t ndr_size/ .. /^}/;
495 # Get rid of dcerpc interface structures and functions since
496 # they are also not very interesting.
498 next, if /^static const struct dcerpc_interface_call/ .. /^};/;
499 next, if /^static const char \* const [a-z]+_endpoint_strings/ ../^};/;
500 next, if /^static const struct dcerpc_endpoint_list/ .. /^};/;
501 next, if /^const struct dcerpc_interface_table/ .. /^};/;
502 next, if /^static NTSTATUS dcerpc_ndr_[a-z]+_init/ .. /^}/;
503 next, if /^NTSTATUS dcerpc_[a-z]+_init/ .. /^}/;
505 # Rewrite includes to packet-dcerpc-foo.h instead of ndr_foo.h
507 s/^\#include \".*?ndr_(.*?).h\"$/\#include \"packet-dcerpc-$1.h\"/smg;
510 # Remember which structure or function we are processing.
513 $cur_fn = $1, if /NTSTATUS ndr_pull_(.*?)\(struct/;
515 # Skip functions we have marked as nopull
519 foreach my $f (keys(%{$nopull_typedefs})) {
520 $skip_fn = 1, if $cur_fn eq $f;
523 $cur_fn = "", if /^}/;
528 # OK start wrapping the ndr_pull functions that actually
529 # implement the NDR decoding routines. This mainly consists
530 # of adding a couple of parameters to each function call.
533 # Add proto tree and name argument to ndr_pull_ptr() calls.
535 s
/(ndr_pull_ptr\
(ndr
,\
(&_ptr_
([^\
)]*?
))\
);)
536 /ndr_pull_ptr(ndr, tree, "$3", $2);/smgx;
538 # Wrap ndr_pull_array_size() and ndr_pull_array_length()
539 # functions. Add leading space in front of first parameter so
540 # we won't get caught by later regexps.
542 s
/(ndr_pull_array_
(size
|length)\
(ndr
,\
([^\
)]*?
)\
);)
543 /ndr_pull_array_$2( ndr, tree, $3);/smgx;
545 # Add tree argument to ndr_pull_array() and
546 # ndr_pull_array_foo() calls.
550 ([^,]*?
),\
# NDR_SCALARS etc
551 (\
(void\ \
*\
*\
)r
->(in|out
|)\
.?
([^,]*?
)),\
# Pointer to array entries
552 ([^\
)].*?
)\
);) # All other arguments
553 /ndr_pull_array( ndr, $2, tree, $3, $6);/smgx;
555 s
/(ndr_pull_array_
([^\
(]*?
)\
(
557 ([^,]*?
),\
# NDR_SCALARS etc
558 (r
->((in|out
).)?
([^,]*?
)),\
# Pointer to array elements
559 (.*?
)\
);) # Number of elements
560 /ndr_pull_array_$2( ndr, $3, tree, hf_${cur_fn}_$7_array, $4, $8);/smgx;
562 # Save ndr_pull_relative{1,2}() calls from being wrapped by the
563 # proceeding regexp by adding a leading space.
565 s
/ndr_pull_(relative1|relative2)\((.*?)\);/
566 ndr_pull_
$1( $2);/smgx
;
570 s
/(^static\ NTSTATUS\ ndr_pull_
(.+?
),\
(enum\
.+?
)\
))
571 /static NTSTATUS ndr_pull_$2, pidl_tree *tree, int hf, $3)/smgx;
572 s/uint(8|16|32) v;/uint$1_t v;/smg;
573 s
/(ndr_pull_
([^\
)]*?
)\
(ndr
,\
&v\
);)
574 /ndr_pull_$2(ndr, tree, hf, &v);/smgx;
576 s
/(ndr_pull_
([^\
(]+?
)\
(ndr
,\
&_level\
);)
577 /ndr_pull_$2(ndr, tree, hf_${cur_fn}_level, &_level);/smgx;
581 s
/(^(static\
)?NTSTATUS\ ndr_pull_
(.+?
),\ uint
(8|16|32)\ \
*r\
))
582 /NTSTATUS ndr_pull_$3, pidl_tree *tree, int hf, uint$4_t *r)/smgx;
584 # Call ethereal wrappers for pull of scalar values in
585 # structures and functions, e.g
587 # ndr_pull_uint32(ndr, &r->in.access_mask);
588 # ndr_pull_uint32(ndr, &r->idx);
590 s
/(ndr_pull_
([^\
)]*?
)
592 (&?r
->((in|out
)\
.)?
# Function args contain leading junk
593 ([^\
)]*?
)) # Element name
595 /ndr_pull_$2(ndr, tree, hf_${cur_fn}_$6, $3);/smgx;
597 # Add tree and hf argument to pulls of "internal" scalars like
598 # array sizes, levels, etc.
600 s
/(ndr_pull_
(uint32
|uint16
)\
(
602 (&_
([^\
)]*?
)) # Internal arg names have leading underscore
604 /ndr_pull_$2(ndr, tree, hf_$4, $3);/smgx;
606 # Add subtree argument to calls dissecting structures/unions, e.g
608 # ndr_pull_string(ndr, NDR_SCALARS|NDR_BUFFERS, &r->command);
609 # ndr_pull_atsvc_enum_ctr(ndr, NDR_SCALARS|NDR_BUFFERS, r->in.ctr);
611 # Three argument version is for structures
613 if (/ndr_pull([^\)]*?)\(ndr, (NDR_[^,]*?), ([^,]*?)\);/) {
615 s
/(ndr_pull_
([^\
)]*?
)\
(
618 (&?r
->(in|out
|)\
.?
([^\
(].*?
))\
);)
619 /ndr_pull_
$2(ndr
, $3, get_subtree
(tree
, \"$6\", ndr
, ett_
$2), $4);
623 # Four argument version if for unions
625 if (/ndr_pull([^\)]*?)\(ndr, (NDR_[SB][^,]*?), ([^,]*?), ([^,]*?)\);/) {
626 s
/(ndr_pull_
([^\
)]*?
)\
(
629 (&?r
->(in|out
|)\
.?
([^\
(].*?
))\
);)
630 /ndr_pull_
$2(ndr
, $3, get_subtree
(tree
, \"$2\", ndr
, ett_
$2), $4);
634 # Add proto_tree parameter to pull function prototypes, e.g
636 # static NTSTATUS ndr_pull_atsvc_JobInfo(struct ndr_pull *ndr,
637 # int ndr_flags, struct atsvc_JobInfo *r)
639 s
/^((static\
)?NTSTATUS\ ndr_pull_
([^\
(]*?
)\
(
640 struct\ ndr_pull\ \
*ndr
,\
642 /$1, proto_tree \*tree/smgx;
644 # Add proto_tree parameter to ndr_pull_subcontext_flags_fn()
646 s/(ndr_pull_subcontext_flags_fn\(ndr)(.*?);/$1, tree$2;/smg;
648 # Get rid of ndr_pull_error() calls for the moment. Ethereal
649 # should take care of buffer overruns and inconsistent array
650 # sizes for us but it would be nice to have some error text in
653 s/(return ndr_pull_error([^;]*?);)/return NT_STATUS_OK; \/\
/ $1/smg;
655 # Rename proto_tree args to pidl_tree
657 s/(int (ndr_)?flags), proto_tree \*tree/$1, pidl_tree \*tree/smg;
659 # Rename struct ndr_pull to struct pidl_pull
661 s/struct ndr_pull \*ndr/struct pidl_pull \*ndr/smg;
663 # Fix some internal variable declarations
665 s/(u?)int(8|16|32) _level;/$1int$2_t _level;/smg;
666 s
/ndr_pull_
([^\
(]*)\
(ndr
,\ tree
,\ hf_level
,\
&_level\
);
667 /ndr_pull_$1(ndr, tree, hf_level_$1, &_level);/smgx;
669 # Set the end of a structure
671 s/(ndr_pull_struct_end.*)/$1\tproto_item_set_end(tree->proto_tree, ndr->tvb, ndr->offset);\n/smg;
676 # Function call table
678 foreach my $x (@
{$idl}) {
679 if ($x->{TYPE
} eq "INTERFACE") {
680 foreach my $y (@
{$x->{"INHERITED_DATA"}}) {
681 ($y->{TYPE
} eq "FUNCTION") && ParseFunctionPull
($y);
688 # Ethereal protocol registration
690 pidl
"int proto_dcerpc_pidl_$module = -1;\n\n";
692 pidl
"static gint ett_dcerpc_$module = -1;\n\n";
694 if (defined($if_uuid)) {
696 pidl
"static e_uuid_t uuid_dcerpc_$module = {\n";
697 pidl
"\t0x" . substr($if_uuid, 1, 8);
698 pidl
", 0x" . substr($if_uuid, 10, 4);
699 pidl
", 0x" . substr($if_uuid, 15, 4) . ",\n";
700 pidl
"\t{ 0x" . substr($if_uuid, 20, 2);
701 pidl
", 0x" . substr($if_uuid, 22, 2);
702 pidl
", 0x" . substr($if_uuid, 25, 2);
703 pidl
", 0x" . substr($if_uuid, 27, 2);
704 pidl
", 0x" . substr($if_uuid, 29, 2);
705 pidl
", 0x" . substr($if_uuid, 31, 2);
706 pidl
", 0x" . substr($if_uuid, 33, 2);
707 pidl
", 0x" . substr($if_uuid, 35, 2) . " }\n";
711 if (defined($if_version)) {
712 pidl
"static guint16 ver_dcerpc_$module = " . $if_version . ";\n\n";
715 pidl
"void proto_register_dcerpc_pidl_$module(void)\n";
718 pidl
"\tstatic hf_register_info hf[] = {\n";
719 pidl
"\t{ &hf_opnum, { \"Operation\", \"$module.opnum\", FT_UINT16, BASE_DEC, NULL, 0x0, \"Operation\", HFILL }},\n";
720 pidl
"\t{ &hf_result_NTSTATUS, { \"Return code\", \"$module.rc\", FT_UINT32, BASE_HEX, VALS(NT_errors), 0x0, \"Return status code\", HFILL }},\n";
721 pidl
"\t{ &hf_ptr, { \"Pointer\", \"$module.ptr\", FT_UINT32, BASE_HEX, NULL, 0x0, \"Pointer\", HFILL }},\n";
723 foreach my $x (keys(%needed)) {
724 next, if !($x =~ /^hf_/);
726 pidl
"\t { \"$needed{$x}{name}\", \"$x\", $needed{$x}{ft}, $needed{$x}{base}, NULL, 0, \"$x\", HFILL }},\n";
731 pidl
"\tstatic gint *ett[] = {\n";
732 pidl
"\t\t&ett_dcerpc_$module,\n";
733 foreach my $x (keys(%needed)) {
734 pidl
"\t\t&$x,\n", if $x =~ /^ett_/;
738 if (defined($if_uuid)) {
740 # These can be changed to non-pidl names if the old dissectors
741 # in epan/dissctors are deleted.
743 my $name = uc($module) . " (pidl)";
744 my $short_name = "pidl_$module";
745 my $filter_name = "pidl_$module";
747 pidl
"\tproto_dcerpc_pidl_$module = proto_register_protocol(\"$name\", \"$short_name\", \"$filter_name\");\n\n";
749 pidl
"\tproto_register_field_array(proto_dcerpc_pidl_$module, hf, array_length (hf));\n";
750 pidl
"\tproto_register_subtree_array(ett, array_length(ett));\n";
754 pidl
"void proto_reg_handoff_dcerpc_pidl_$module(void)\n";
756 pidl
"\tdcerpc_init_uuid(proto_dcerpc_pidl_$module, ett_dcerpc_$module, \n";
757 pidl
"\t\t&uuid_dcerpc_$module, ver_dcerpc_$module, \n";
758 pidl
"\t\tdcerpc_dissectors, hf_opnum);\n";
763 pidl
"\tint proto_dcerpc;\n\n";
764 pidl
"\tproto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");\n";
765 pidl
"\tproto_register_field_array(proto_dcerpc, hf, array_length(hf));\n";
766 pidl
"\tproto_register_subtree_array(ett, array_length(ett));\n";