summaryrefslogtreecommitdiffstats
path: root/tools/pidl/lib/Parse/Pidl/Samba4/NDR
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 20:34:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 20:34:10 +0000
commite4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc (patch)
tree68cb5ef9081156392f1dd62a00c6ccc1451b93df /tools/pidl/lib/Parse/Pidl/Samba4/NDR
parentInitial commit. (diff)
downloadwireshark-e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc.tar.xz
wireshark-e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc.zip
Adding upstream version 4.2.2.upstream/4.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/pidl/lib/Parse/Pidl/Samba4/NDR')
-rw-r--r--tools/pidl/lib/Parse/Pidl/Samba4/NDR/Client.pm884
-rw-r--r--tools/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm3224
-rw-r--r--tools/pidl/lib/Parse/Pidl/Samba4/NDR/Server.pm342
3 files changed, 4450 insertions, 0 deletions
diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Client.pm b/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Client.pm
new file mode 100644
index 00000000..040cd5a4
--- /dev/null
+++ b/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Client.pm
@@ -0,0 +1,884 @@
+###################################################
+# client calls generator
+# Copyright tridge@samba.org 2003
+# Copyright jelmer@samba.org 2005-2006
+# released under the GNU GPL
+
+package Parse::Pidl::Samba4::NDR::Client;
+
+use Exporter;
+@ISA = qw(Exporter);
+@EXPORT_OK = qw(Parse);
+
+use Parse::Pidl qw(fatal warning error);
+use Parse::Pidl::Util qw(has_property ParseExpr genpad);
+use Parse::Pidl::NDR qw(ContainsPipe);
+use Parse::Pidl::Typelist qw(mapTypeName);
+use Parse::Pidl::Samba4 qw(choose_header is_intree DeclLong);
+use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv);
+
+use vars qw($VERSION);
+$VERSION = '0.01';
+
+use strict;
+
+sub indent($) { my ($self) = @_; $self->{tabs}.="\t"; }
+sub deindent($) { my ($self) = @_; $self->{tabs} = substr($self->{tabs}, 1); }
+sub pidl($$) { my ($self,$txt) = @_; $self->{res} .= $txt ? "$self->{tabs}$txt\n" : "\n"; }
+sub pidl_hdr($$) { my ($self, $txt) = @_; $self->{res_hdr} .= "$txt\n"; }
+sub pidl_both($$) { my ($self, $txt) = @_; $self->{hdr} .= "$txt\n"; $self->{res_hdr} .= "$txt\n"; }
+sub fn_declare($$) { my ($self,$n) = @_; $self->pidl($n); $self->pidl_hdr("$n;"); }
+
+sub new($)
+{
+ my ($class) = shift;
+ my $self = { res => "", res_hdr => "", tabs => "" };
+ bless($self, $class);
+}
+
+sub ParseFunctionHasPipes($$)
+{
+ my ($self, $fn) = @_;
+
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ return 1 if ContainsPipe($e, $e->{LEVELS}[0]);
+ }
+
+ return 0;
+}
+
+sub ParseFunction_r_State($$$$)
+{
+ my ($self, $if, $fn, $name) = @_;
+ my $uname = uc $name;
+
+ $self->pidl("struct dcerpc_$name\_r_state {");
+ $self->indent;
+ $self->pidl("TALLOC_CTX *out_mem_ctx;");
+ $self->deindent;
+ $self->pidl("};");
+ $self->pidl("");
+ $self->pidl("static void dcerpc_$name\_r_done(struct tevent_req *subreq);");
+ $self->pidl("");
+}
+
+sub ParseFunction_r_Send($$$$)
+{
+ my ($self, $if, $fn, $name) = @_;
+ my $uname = uc $name;
+
+ my $proto = "struct tevent_req *dcerpc_$name\_r_send(TALLOC_CTX *mem_ctx,\n";
+ $proto .= "\tstruct tevent_context *ev,\n",
+ $proto .= "\tstruct dcerpc_binding_handle *h,\n",
+ $proto .= "\tstruct $name *r)";
+
+ $self->fn_declare($proto);
+
+ $self->pidl("{");
+ $self->indent;
+
+ $self->pidl("struct tevent_req *req;");
+ $self->pidl("struct dcerpc_$name\_r_state *state;");
+ $self->pidl("struct tevent_req *subreq;");
+ $self->pidl("");
+
+ $self->pidl("req = tevent_req_create(mem_ctx, &state,");
+ $self->pidl("\t\t\tstruct dcerpc_$name\_r_state);");
+ $self->pidl("if (req == NULL) {");
+ $self->indent;
+ $self->pidl("return NULL;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+
+ my $out_params = 0;
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next unless grep(/out/, @{$e->{DIRECTION}});
+ next if ContainsPipe($e, $e->{LEVELS}[0]);
+ $out_params++;
+
+ }
+
+ my $submem;
+ if ($out_params > 0) {
+ $self->pidl("state->out_mem_ctx = talloc_new(state);");
+ $self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {");
+ $self->indent;
+ $self->pidl("return tevent_req_post(req, ev);");
+ $self->deindent;
+ $self->pidl("}");
+ $submem = "state->out_mem_ctx";
+ } else {
+ $self->pidl("state->out_mem_ctx = NULL;");
+ $submem = "state";
+ }
+ $self->pidl("");
+
+ $self->pidl("subreq = dcerpc_binding_handle_call_send(state, ev, h,");
+ $self->pidl("\t\tNULL, &ndr_table_$if->{NAME},");
+ $self->pidl("\t\tNDR_$uname, $submem, r);");
+ $self->pidl("if (tevent_req_nomem(subreq, req)) {");
+ $self->indent;
+ $self->pidl("return tevent_req_post(req, ev);");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("tevent_req_set_callback(subreq, dcerpc_$name\_r_done, req);");
+ $self->pidl("");
+
+ $self->pidl("return req;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub ParseFunction_r_Done($$$$)
+{
+ my ($self, $if, $fn, $name) = @_;
+ my $uname = uc $name;
+
+ my $proto = "static void dcerpc_$name\_r_done(struct tevent_req *subreq)";
+
+ $self->pidl("$proto");
+ $self->pidl("{");
+ $self->indent;
+
+ $self->pidl("struct tevent_req *req =");
+ $self->pidl("\ttevent_req_callback_data(subreq,");
+ $self->pidl("\tstruct tevent_req);");
+ $self->pidl("NTSTATUS status;");
+ $self->pidl("");
+
+ $self->pidl("status = dcerpc_binding_handle_call_recv(subreq);");
+ $self->pidl("TALLOC_FREE(subreq);");
+ $self->pidl("if (tevent_req_nterror(req, status)) {");
+ $self->indent;
+ $self->pidl("return;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+
+ $self->pidl("tevent_req_done(req);");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub ParseFunction_r_Recv($$$$)
+{
+ my ($self, $if, $fn, $name) = @_;
+ my $uname = uc $name;
+
+ my $proto = "NTSTATUS dcerpc_$name\_r_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)";
+
+ $self->fn_declare($proto);
+
+ $self->pidl("{");
+ $self->indent;
+
+ $self->pidl("struct dcerpc_$name\_r_state *state =");
+ $self->pidl("\ttevent_req_data(req,");
+ $self->pidl("\tstruct dcerpc_$name\_r_state);");
+ $self->pidl("NTSTATUS status;");
+ $self->pidl("");
+
+ $self->pidl("if (tevent_req_is_nterror(req, &status)) {");
+ $self->indent;
+ $self->pidl("tevent_req_received(req);");
+ $self->pidl("return status;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+
+ $self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);");
+ $self->pidl("");
+
+ $self->pidl("tevent_req_received(req);");
+ $self->pidl("return NT_STATUS_OK;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub ParseFunction_r_Sync($$$$)
+{
+ my ($self, $if, $fn, $name) = @_;
+ my $uname = uc $name;
+
+ if ($self->ParseFunctionHasPipes($fn)) {
+ $self->pidl_both("/*");
+ $self->pidl_both(" * The following function is skipped because");
+ $self->pidl_both(" * it uses pipes:");
+ $self->pidl_both(" *");
+ $self->pidl_both(" * dcerpc_$name\_r()");
+ $self->pidl_both(" */");
+ $self->pidl_both("");
+ return;
+ }
+
+ my $proto = "NTSTATUS dcerpc_$name\_r(struct dcerpc_binding_handle *h, TALLOC_CTX *mem_ctx, struct $name *r)";
+
+ $self->fn_declare($proto);
+
+ $self->pidl("{");
+ $self->indent;
+ $self->pidl("NTSTATUS status;");
+ $self->pidl("");
+
+ $self->pidl("status = dcerpc_binding_handle_call(h,");
+ $self->pidl("\t\tNULL, &ndr_table_$if->{NAME},");
+ $self->pidl("\t\tNDR_$uname, mem_ctx, r);");
+ $self->pidl("");
+ $self->pidl("return status;");
+
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub ElementDirection($)
+{
+ my ($e) = @_;
+
+ return "[in,out]" if (has_property($e, "in") and has_property($e, "out"));
+ return "[in]" if (has_property($e, "in"));
+ return "[out]" if (has_property($e, "out"));
+ return "[in,out]";
+}
+
+sub HeaderProperties($$)
+{
+ my($props,$ignores) = @_;
+ my $ret = "";
+
+ foreach my $d (sort(keys %{$props})) {
+ next if (grep(/^$d$/, @$ignores));
+ if($props->{$d} ne "1") {
+ $ret.= "$d($props->{$d}),";
+ } else {
+ $ret.="$d,";
+ }
+ }
+
+ if ($ret) {
+ return "[" . substr($ret, 0, -1) . "]";
+ }
+}
+
+sub ParseCopyArgument($$$$$)
+{
+ my ($self, $fn, $e, $r, $i) = @_;
+ my $l = $e->{LEVELS}[0];
+
+ if ($l->{TYPE} eq "ARRAY" and $l->{IS_FIXED} == 1) {
+ $self->pidl("memcpy(${r}$e->{NAME}, ${i}$e->{NAME}, sizeof(${r}$e->{NAME}));");
+ } else {
+ $self->pidl("${r}$e->{NAME} = ${i}$e->{NAME};");
+ }
+}
+
+sub ParseInvalidResponse($$)
+{
+ my ($self, $type) = @_;
+
+ if ($type eq "sync") {
+ $self->pidl("return NT_STATUS_INVALID_NETWORK_RESPONSE;");
+ } elsif ($type eq "async") {
+ $self->pidl("tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);");
+ $self->pidl("return;");
+ } else {
+ die("ParseInvalidResponse($type)");
+ }
+}
+
+sub ParseOutputArgument($$$$$$)
+{
+ my ($self, $fn, $e, $r, $o, $invalid_response_type) = @_;
+ my $level = 0;
+
+ if ($e->{LEVELS}[0]->{TYPE} ne "POINTER" and $e->{LEVELS}[0]->{TYPE} ne "ARRAY") {
+ fatal($e->{ORIGINAL}, "[out] argument is not a pointer or array");
+ return;
+ }
+
+ if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
+ $level = 1;
+ if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
+ $self->pidl("if ($o$e->{NAME} && ${r}out.$e->{NAME}) {");
+ $self->indent;
+ }
+ }
+
+ if ($e->{LEVELS}[$level]->{TYPE} eq "ARRAY") {
+ # This is a call to GenerateFunctionInEnv intentionally.
+ # Since the data is being copied into a user-provided data
+ # structure, the user should be able to know the size beforehand
+ # to allocate a structure of the right size.
+ my $in_env = GenerateFunctionInEnv($fn, $r);
+ my $out_env = GenerateFunctionOutEnv($fn, $r);
+ my $l = $e->{LEVELS}[$level];
+
+ my $in_var = undef;
+ if (grep(/in/, @{$e->{DIRECTION}})) {
+ $in_var = ParseExpr($e->{NAME}, $in_env, $e->{ORIGINAL});
+ }
+ my $out_var = ParseExpr($e->{NAME}, $out_env, $e->{ORIGINAL});
+
+ my $in_size_is = undef;
+ my $out_size_is = undef;
+ my $out_length_is = undef;
+
+ my $avail_len = undef;
+ my $needed_len = undef;
+
+ $self->pidl("{");
+ $self->indent;
+ my $copy_len_var = "_copy_len_$e->{NAME}";
+ $self->pidl("size_t $copy_len_var;");
+
+ if (not defined($l->{SIZE_IS})) {
+ if (not $l->{IS_ZERO_TERMINATED}) {
+ fatal($e->{ORIGINAL}, "no size known for [out] array `$e->{NAME}'");
+ }
+ if (has_property($e, "charset")) {
+ $avail_len = "ndr_charset_length($in_var, CH_UNIX)";
+ $needed_len = "ndr_charset_length($out_var, CH_UNIX)";
+ } else {
+ $avail_len = "ndr_string_length($in_var, sizeof(*$in_var))";
+ $needed_len = "ndr_string_length($out_var, sizeof(*$out_var))";
+ }
+ $in_size_is = "";
+ $out_size_is = "";
+ $out_length_is = "";
+ } else {
+ $in_size_is = ParseExpr($l->{SIZE_IS}, $in_env, $e->{ORIGINAL});
+ $out_size_is = ParseExpr($l->{SIZE_IS}, $out_env, $e->{ORIGINAL});
+ $out_length_is = $out_size_is;
+ if (defined($l->{LENGTH_IS})) {
+ $out_length_is = ParseExpr($l->{LENGTH_IS}, $out_env, $e->{ORIGINAL});
+ }
+ if (has_property($e, "charset")) {
+ if (defined($in_var)) {
+ $avail_len = "ndr_charset_length($in_var, CH_UNIX)";
+ } else {
+ $avail_len = $out_length_is;
+ }
+ $needed_len = "ndr_charset_length($out_var, CH_UNIX)";
+ }
+ }
+
+ if ($out_size_is ne $in_size_is) {
+ $self->pidl("if (($out_size_is) > ($in_size_is)) {");
+ $self->indent;
+ $self->ParseInvalidResponse($invalid_response_type);
+ $self->deindent;
+ $self->pidl("}");
+ }
+ if ($out_length_is ne $out_size_is) {
+ $self->pidl("if (($out_length_is) > ($out_size_is)) {");
+ $self->indent;
+ $self->ParseInvalidResponse($invalid_response_type);
+ $self->deindent;
+ $self->pidl("}");
+ }
+ if (defined($needed_len)) {
+ $self->pidl("$copy_len_var = $needed_len;");
+ $self->pidl("if ($copy_len_var > $avail_len) {");
+ $self->indent;
+ $self->ParseInvalidResponse($invalid_response_type);
+ $self->deindent;
+ $self->pidl("}");
+ } else {
+ $self->pidl("$copy_len_var = $out_length_is;");
+ }
+
+ my $dest_ptr = "$o$e->{NAME}";
+ my $elem_size = "sizeof(*$dest_ptr)";
+ $self->pidl("if ($dest_ptr != $out_var) {");
+ $self->indent;
+ if (has_property($e, "charset")) {
+ $dest_ptr = "discard_const_p(uint8_t *, $dest_ptr)";
+ }
+ $self->pidl("memcpy($dest_ptr, $out_var, $copy_len_var * $elem_size);");
+ $self->deindent;
+ $self->pidl("}");
+
+ $self->deindent;
+ $self->pidl("}");
+ } else {
+ $self->pidl("*$o$e->{NAME} = *${r}out.$e->{NAME};");
+ }
+
+ if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
+ if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
+ $self->deindent;
+ $self->pidl("}");
+ }
+ }
+}
+
+sub ParseFunction_State($$$$)
+{
+ my ($self, $if, $fn, $name) = @_;
+
+ my $state_str = "struct dcerpc_$name\_state";
+ my $done_fn = "dcerpc_$name\_done";
+
+ $self->pidl("$state_str {");
+ $self->indent;
+ $self->pidl("struct $name orig;");
+ $self->pidl("struct $name tmp;");
+ $self->pidl("TALLOC_CTX *out_mem_ctx;");
+ $self->deindent;
+ $self->pidl("};");
+ $self->pidl("");
+ $self->pidl("static void $done_fn(struct tevent_req *subreq);");
+ $self->pidl("");
+}
+
+sub ParseFunction_Send($$$$)
+{
+ my ($self, $if, $fn, $name) = @_;
+
+ my $fn_args = "";
+ my $state_str = "struct dcerpc_$name\_state";
+ my $done_fn = "dcerpc_$name\_done";
+ my $out_mem_ctx = "dcerpc_$name\_out_memory";
+ my $fn_str = "struct tevent_req *dcerpc_$name\_send";
+ my $pad = genpad($fn_str);
+
+ $fn_args .= "TALLOC_CTX *mem_ctx";
+ $fn_args .= ",\n" . $pad . "struct tevent_context *ev";
+ $fn_args .= ",\n" . $pad . "struct dcerpc_binding_handle *h";
+
+ foreach (@{$fn->{ELEMENTS}}) {
+ my $dir = ElementDirection($_);
+ my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]);
+ $fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */";
+ }
+
+ $self->fn_declare("$fn_str($fn_args)");
+ $self->pidl("{");
+ $self->indent;
+ $self->pidl("struct tevent_req *req;");
+ $self->pidl("$state_str *state;");
+ $self->pidl("struct tevent_req *subreq;");
+ $self->pidl("");
+ $self->pidl("req = tevent_req_create(mem_ctx, &state,");
+ $self->pidl("\t\t\t$state_str);");
+ $self->pidl("if (req == NULL) {");
+ $self->indent;
+ $self->pidl("return NULL;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("state->out_mem_ctx = NULL;");
+ $self->pidl("");
+
+ $self->pidl("/* In parameters */");
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next unless (grep(/in/, @{$e->{DIRECTION}}));
+
+ $self->ParseCopyArgument($fn, $e, "state->orig.in.", "_");
+ }
+ $self->pidl("");
+
+ my $out_params = 0;
+ $self->pidl("/* Out parameters */");
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next unless grep(/out/, @{$e->{DIRECTION}});
+
+ $self->ParseCopyArgument($fn, $e, "state->orig.out.", "_");
+
+ next if ContainsPipe($e, $e->{LEVELS}[0]);
+
+ $out_params++;
+ }
+ $self->pidl("");
+
+ if (defined($fn->{RETURN_TYPE})) {
+ $self->pidl("/* Result */");
+ $self->pidl("ZERO_STRUCT(state->orig.out.result);");
+ $self->pidl("");
+ }
+
+ if ($out_params > 0) {
+ $self->pidl("state->out_mem_ctx = talloc_named_const(state, 0,");
+ $self->pidl("\t\t \"$out_mem_ctx\");");
+ $self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {");
+ $self->indent;
+ $self->pidl("return tevent_req_post(req, ev);");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+ }
+
+ $self->pidl("/* make a temporary copy, that we pass to the dispatch function */");
+ $self->pidl("state->tmp = state->orig;");
+ $self->pidl("");
+
+ $self->pidl("subreq = dcerpc_$name\_r_send(state, ev, h, &state->tmp);");
+ $self->pidl("if (tevent_req_nomem(subreq, req)) {");
+ $self->indent;
+ $self->pidl("return tevent_req_post(req, ev);");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("tevent_req_set_callback(subreq, $done_fn, req);");
+ $self->pidl("return req;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub ParseFunction_Done($$$$)
+{
+ my ($self, $if, $fn, $name) = @_;
+
+ my $state_str = "struct dcerpc_$name\_state";
+ my $done_fn = "dcerpc_$name\_done";
+
+ $self->pidl("static void $done_fn(struct tevent_req *subreq)");
+ $self->pidl("{");
+ $self->indent;
+ $self->pidl("struct tevent_req *req = tevent_req_callback_data(");
+ $self->pidl("\tsubreq, struct tevent_req);");
+ $self->pidl("$state_str *state = tevent_req_data(");
+ $self->pidl("\treq, $state_str);");
+ $self->pidl("NTSTATUS status;");
+ $self->pidl("TALLOC_CTX *mem_ctx;");
+ $self->pidl("");
+
+ $self->pidl("if (state->out_mem_ctx) {");
+ $self->indent;
+ $self->pidl("mem_ctx = state->out_mem_ctx;");
+ $self->deindent;
+ $self->pidl("} else {");
+ $self->indent;
+ $self->pidl("mem_ctx = state;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+
+ $self->pidl("status = dcerpc_$name\_r_recv(subreq, mem_ctx);");
+ $self->pidl("TALLOC_FREE(subreq);");
+ $self->pidl("if (tevent_req_nterror(req, status)) {");
+ $self->indent;
+ $self->pidl("return;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+
+ $self->pidl("/* Copy out parameters */");
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next if ContainsPipe($e, $e->{LEVELS}[0]);
+ next unless (grep(/out/, @{$e->{DIRECTION}}));
+
+ $self->ParseOutputArgument($fn, $e,
+ "state->tmp.",
+ "state->orig.out.",
+ "async");
+ }
+ $self->pidl("");
+
+ if (defined($fn->{RETURN_TYPE})) {
+ $self->pidl("/* Copy result */");
+ $self->pidl("state->orig.out.result = state->tmp.out.result;");
+ $self->pidl("");
+ }
+
+ $self->pidl("/* Reset temporary structure */");
+ $self->pidl("ZERO_STRUCT(state->tmp);");
+ $self->pidl("");
+
+ $self->pidl("tevent_req_done(req);");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub ParseFunction_Recv($$$$)
+{
+ my ($self, $if, $fn, $name) = @_;
+
+ my $fn_args = "";
+ my $state_str = "struct dcerpc_$name\_state";
+ my $fn_str = "NTSTATUS dcerpc_$name\_recv";
+ my $pad = genpad($fn_str);
+
+ $fn_args .= "struct tevent_req *req,\n" . $pad . "TALLOC_CTX *mem_ctx";
+
+ if (defined($fn->{RETURN_TYPE})) {
+ $fn_args .= ",\n" . $pad . mapTypeName($fn->{RETURN_TYPE}). " *result";
+ }
+
+ $self->fn_declare("$fn_str($fn_args)");
+ $self->pidl("{");
+ $self->indent;
+ $self->pidl("$state_str *state = tevent_req_data(");
+ $self->pidl("\treq, $state_str);");
+ $self->pidl("NTSTATUS status;");
+ $self->pidl("");
+ $self->pidl("if (tevent_req_is_nterror(req, &status)) {");
+ $self->indent;
+ $self->pidl("tevent_req_received(req);");
+ $self->pidl("return status;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+
+ $self->pidl("/* Steal possible out parameters to the callers context */");
+ $self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);");
+ $self->pidl("");
+
+ if (defined($fn->{RETURN_TYPE})) {
+ $self->pidl("/* Return result */");
+ $self->pidl("*result = state->orig.out.result;");
+ $self->pidl("");
+ }
+
+ $self->pidl("tevent_req_received(req);");
+ $self->pidl("return NT_STATUS_OK;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub ParseFunction_Sync($$$$)
+{
+ my ($self, $if, $fn, $name) = @_;
+
+ if ($self->ParseFunctionHasPipes($fn)) {
+ $self->pidl_both("/*");
+ $self->pidl_both(" * The following function is skipped because");
+ $self->pidl_both(" * it uses pipes:");
+ $self->pidl_both(" *");
+ $self->pidl_both(" * dcerpc_$name()");
+ $self->pidl_both(" */");
+ $self->pidl_both("");
+ return;
+ }
+
+ my $uname = uc $name;
+ my $fn_args = "";
+ my $fn_str = "NTSTATUS dcerpc_$name";
+ my $pad = genpad($fn_str);
+
+ $fn_args .= "struct dcerpc_binding_handle *h,\n" . $pad . "TALLOC_CTX *mem_ctx";
+
+ foreach (@{$fn->{ELEMENTS}}) {
+ my $dir = ElementDirection($_);
+ my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]);
+ $fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */";
+ }
+
+ if (defined($fn->{RETURN_TYPE})) {
+ $fn_args .= ",\n" . $pad . mapTypeName($fn->{RETURN_TYPE}). " *result";
+ }
+
+ $self->fn_declare("$fn_str($fn_args)");
+ $self->pidl("{");
+ $self->indent;
+ $self->pidl("struct $name r;");
+ $self->pidl("NTSTATUS status;");
+ $self->pidl("");
+
+ $self->pidl("/* In parameters */");
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next unless (grep(/in/, @{$e->{DIRECTION}}));
+
+ $self->ParseCopyArgument($fn, $e, "r.in.", "_");
+ }
+ $self->pidl("");
+
+ $self->pidl("/* Out parameters */");
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next unless grep(/out/, @{$e->{DIRECTION}});
+
+ $self->ParseCopyArgument($fn, $e, "r.out.", "_");
+ }
+ $self->pidl("");
+
+ if (defined($fn->{RETURN_TYPE})) {
+ $self->pidl("/* Result */");
+ $self->pidl("ZERO_STRUCT(r.out.result);");
+ $self->pidl("");
+ }
+
+ $self->pidl("status = dcerpc_$name\_r(h, mem_ctx, &r);");
+ $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
+ $self->indent;
+ $self->pidl("return status;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+
+ $self->pidl("/* Return variables */");
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next if ContainsPipe($e, $e->{LEVELS}[0]);
+ next unless (grep(/out/, @{$e->{DIRECTION}}));
+
+ $self->ParseOutputArgument($fn, $e, "r.", "_", "sync");
+ }
+ $self->pidl("");
+
+ $self->pidl("/* Return result */");
+ if ($fn->{RETURN_TYPE}) {
+ $self->pidl("*result = r.out.result;");
+ }
+ $self->pidl("");
+
+ $self->pidl("return NT_STATUS_OK;");
+
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+#####################################################################
+# parse a function
+sub ParseFunction($$$)
+{
+ my ($self, $if, $fn) = @_;
+
+ if ($self->ParseFunctionHasPipes($fn)) {
+ $self->pidl_both("/*");
+ $self->pidl_both(" * The following function is skipped because");
+ $self->pidl_both(" * it uses pipes:");
+ $self->pidl_both(" *");
+ $self->pidl_both(" * dcerpc_$fn->{NAME}_r_send()");
+ $self->pidl_both(" * dcerpc_$fn->{NAME}_r_recv()");
+ $self->pidl_both(" * dcerpc_$fn->{NAME}_r()");
+ $self->pidl_both(" *");
+ $self->pidl_both(" * dcerpc_$fn->{NAME}_send()");
+ $self->pidl_both(" * dcerpc_$fn->{NAME}_recv()");
+ $self->pidl_both(" * dcerpc_$fn->{NAME}()");
+ $self->pidl_both(" */");
+ $self->pidl_both("");
+ warning($fn->{ORIGINAL}, "$fn->{NAME}: dcerpc client does not support pipe yet");
+ return;
+ }
+
+ $self->ParseFunction_r_State($if, $fn, $fn->{NAME});
+ $self->ParseFunction_r_Send($if, $fn, $fn->{NAME});
+ $self->ParseFunction_r_Done($if, $fn, $fn->{NAME});
+ $self->ParseFunction_r_Recv($if, $fn, $fn->{NAME});
+ $self->ParseFunction_r_Sync($if, $fn, $fn->{NAME});
+
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next unless (grep(/out/, @{$e->{DIRECTION}}));
+
+ my $reason = "is not a pointer or array";
+
+ # TODO: make this fatal at NDR level
+ if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
+ if ($e->{LEVELS}[1]->{TYPE} eq "DATA" and
+ $e->{LEVELS}[1]->{DATA_TYPE} eq "string") {
+ $reason = "is a pointer to type 'string'";
+ } elsif ($e->{LEVELS}[1]->{TYPE} eq "ARRAY" and
+ $e->{LEVELS}[1]->{IS_ZERO_TERMINATED}) {
+ next;
+ } elsif ($e->{LEVELS}[1]->{TYPE} eq "ARRAY" and
+ not defined($e->{LEVELS}[1]->{SIZE_IS})) {
+ $reason = "is a pointer to an unsized array";
+ } else {
+ next;
+ }
+ }
+ if ($e->{LEVELS}[0]->{TYPE} eq "ARRAY") {
+ if (not defined($e->{LEVELS}[0]->{SIZE_IS})) {
+ $reason = "is an unsized array";
+ } else {
+ next;
+ }
+ }
+
+ $self->pidl_both("/*");
+ $self->pidl_both(" * The following functions are skipped because");
+ $self->pidl_both(" * an [out] argument $e->{NAME} $reason:");
+ $self->pidl_both(" *");
+ $self->pidl_both(" * dcerpc_$fn->{NAME}_send()");
+ $self->pidl_both(" * dcerpc_$fn->{NAME}_recv()");
+ $self->pidl_both(" * dcerpc_$fn->{NAME}()");
+ $self->pidl_both(" */");
+ $self->pidl_both("");
+
+ error($e->{ORIGINAL}, "$fn->{NAME}: [out] argument '$e->{NAME}' $reason, skip client functions");
+ return;
+ }
+
+ $self->ParseFunction_State($if, $fn, $fn->{NAME});
+ $self->ParseFunction_Send($if, $fn, $fn->{NAME});
+ $self->ParseFunction_Done($if, $fn, $fn->{NAME});
+ $self->ParseFunction_Recv($if, $fn, $fn->{NAME});
+ $self->ParseFunction_Sync($if, $fn, $fn->{NAME});
+
+ $self->pidl_hdr("");
+}
+
+my %done;
+
+#####################################################################
+# parse the interface definitions
+sub ParseInterface($$)
+{
+ my ($self, $if) = @_;
+ my $ifu = uc($if->{NAME});
+
+ $self->pidl_hdr("#ifndef _HEADER_RPC_$if->{NAME}");
+ $self->pidl_hdr("#define _HEADER_RPC_$if->{NAME}");
+ $self->pidl_hdr("");
+
+ if (defined $if->{PROPERTIES}->{uuid}) {
+ $self->pidl_hdr("extern const struct ndr_interface_table ndr_table_$if->{NAME};");
+ $self->pidl_hdr("");
+ }
+
+ $self->pidl("/* $if->{NAME} - client functions generated by pidl */");
+ $self->pidl("");
+
+ foreach my $fn (@{$if->{FUNCTIONS}}) {
+ next if defined($done{$fn->{NAME}});
+ next if has_property($fn, "noopnum");
+ next if has_property($fn, "todo");
+ $self->ParseFunction($if, $fn);
+ $done{$fn->{NAME}} = 1;
+ }
+
+ $self->pidl_hdr("#endif /* _HEADER_RPC_$if->{NAME} */");
+}
+
+sub Parse($$$$$$)
+{
+ my($self,$ndr,$header,$ndr_header,$client_header) = @_;
+
+ $self->pidl("/* client functions auto-generated by pidl */");
+ $self->pidl("");
+ if (is_intree()) {
+ $self->pidl("#include \"includes.h\"");
+ } else {
+ $self->pidl("#ifndef _GNU_SOURCE");
+ $self->pidl("#define _GNU_SOURCE");
+ $self->pidl("#endif");
+ $self->pidl("#include <stdio.h>");
+ $self->pidl("#include <stdbool.h>");
+ $self->pidl("#include <stdlib.h>");
+ $self->pidl("#include <stdint.h>");
+ $self->pidl("#include <stdarg.h>");
+ $self->pidl("#include <string.h>");
+ $self->pidl("#include <core/ntstatus.h>");
+ }
+ $self->pidl("#include <tevent.h>");
+ $self->pidl(choose_header("lib/util/tevent_ntstatus.h", "util/tevent_ntstatus.h")."");
+ $self->pidl("#include \"$ndr_header\"");
+ $self->pidl("#include \"$client_header\"");
+ $self->pidl("");
+
+ $self->pidl_hdr(choose_header("librpc/rpc/dcerpc.h", "dcerpc.h")."");
+ $self->pidl_hdr("#include \"$header\"");
+
+ foreach my $x (@{$ndr}) {
+ ($x->{TYPE} eq "INTERFACE") && $self->ParseInterface($x);
+ }
+
+ return ($self->{res},$self->{res_hdr});
+}
+
+1;
diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm b/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm
new file mode 100644
index 00000000..cfcd29e2
--- /dev/null
+++ b/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm
@@ -0,0 +1,3224 @@
+###################################################
+# Samba4 NDR parser generator for IDL structures
+# Copyright tridge@samba.org 2000-2003
+# Copyright tpot@samba.org 2001
+# Copyright jelmer@samba.org 2004-2006
+# released under the GNU GPL
+
+package Parse::Pidl::Samba4::NDR::Parser;
+
+require Exporter;
+@ISA = qw(Exporter);
+@EXPORT_OK = qw(check_null_pointer NeededFunction NeededElement NeededType $res NeededInterface TypeFunctionName ParseElementPrint);
+
+use strict;
+use Parse::Pidl::Typelist qw(hasType getType mapTypeName typeHasBody);
+use Parse::Pidl::Util qw(has_property ParseExpr ParseExprExt print_uuid unmake_str);
+use Parse::Pidl::CUtil qw(get_pointer_to get_value_of get_array_element);
+use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred ContainsPipe is_charset_array);
+use Parse::Pidl::Samba4 qw(is_intree choose_header ArrayDynamicallyAllocated);
+use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv);
+use Parse::Pidl qw(warning);
+
+use vars qw($VERSION);
+$VERSION = '0.01';
+
+# list of known types
+my %typefamily;
+
+sub new($$) {
+ my ($class) = @_;
+ my $self = { res => "", res_hdr => "", deferred => [], tabs => "", defer_tabs => "" };
+ bless($self, $class);
+}
+
+sub get_typefamily($)
+{
+ my $n = shift;
+ return $typefamily{$n};
+}
+
+sub append_prefix($$)
+{
+ my ($e, $var_name) = @_;
+ my $pointers = 0;
+ my $arrays = 0;
+
+ foreach my $l (@{$e->{LEVELS}}) {
+ if ($l->{TYPE} eq "POINTER") {
+ $pointers++;
+ } elsif ($l->{TYPE} eq "ARRAY") {
+ $arrays++;
+ if (($pointers == 0) and
+ (not $l->{IS_FIXED}) and
+ (not $l->{IS_INLINE})) {
+ return get_value_of($var_name);
+ }
+ } elsif ($l->{TYPE} eq "DATA") {
+ if (Parse::Pidl::Typelist::scalar_is_reference($l->{DATA_TYPE})) {
+ return get_value_of($var_name) unless ($pointers or $arrays);
+ }
+ }
+ }
+
+ return $var_name;
+}
+
+sub has_fast_array($$)
+{
+ my ($e,$l) = @_;
+
+ return 0 if ($l->{TYPE} ne "ARRAY");
+
+ my $nl = GetNextLevel($e,$l);
+ return 0 unless ($nl->{TYPE} eq "DATA");
+ return 0 unless (hasType($nl->{DATA_TYPE}));
+
+ my $t = getType($nl->{DATA_TYPE});
+
+ # Only uint8 and string have fast array functions at the moment
+ return ($t->{NAME} eq "uint8") or ($t->{NAME} eq "string");
+}
+
+
+####################################
+# pidl() is our basic output routine
+sub pidl($$)
+{
+ my ($self, $d) = @_;
+ if ($d) {
+ $self->{res} .= $self->{tabs};
+ $self->{res} .= $d;
+ }
+ $self->{res} .="\n";
+}
+
+sub pidl_hdr($$) { my ($self, $d) = @_; $self->{res_hdr} .= "$d\n"; }
+
+####################################
+# defer() is like pidl(), but adds to
+# a deferred buffer which is then added to the
+# output buffer at the end of the structure/union/function
+# This is needed to cope with code that must be pushed back
+# to the end of a block of elements
+sub defer_indent($) { my ($self) = @_; $self->{defer_tabs}.="\t"; }
+sub defer_deindent($) { my ($self) = @_; $self->{defer_tabs}=substr($self->{defer_tabs}, 0, -1); }
+
+sub defer($$)
+{
+ my ($self, $d) = @_;
+ if ($d) {
+ push(@{$self->{deferred}}, $self->{defer_tabs}.$d);
+ }
+}
+
+########################################
+# add the deferred content to the current
+# output
+sub add_deferred($)
+{
+ my ($self) = @_;
+ $self->pidl($_) foreach (@{$self->{deferred}});
+ $self->{deferred} = [];
+ $self->{defer_tabs} = "";
+}
+
+sub indent($)
+{
+ my ($self) = @_;
+ $self->{tabs} .= "\t";
+}
+
+sub deindent($)
+{
+ my ($self) = @_;
+ $self->{tabs} = substr($self->{tabs}, 0, -1);
+}
+
+#####################################################################
+# declare a function public or static, depending on its attributes
+sub fn_declare($$$$)
+{
+ my ($self,$type,$fn,$decl) = @_;
+
+ if (has_property($fn, "no$type")) {
+ $self->pidl_hdr("$decl;");
+ return 0;
+ }
+
+ if (has_property($fn, "public")) {
+ $self->pidl_hdr("$decl;");
+ $self->pidl("_PUBLIC_ $decl");
+ } else {
+ $self->pidl("static $decl");
+ }
+
+ return 1;
+}
+
+###################################################################
+# setup any special flags for an element or structure
+sub start_flags($$$)
+{
+ my ($self, $e, $ndr) = @_;
+ my $flags = has_property($e, "flag");
+ if (defined $flags) {
+ $self->pidl("{");
+ $self->indent;
+ $self->pidl("uint32_t _flags_save_$e->{TYPE} = $ndr->flags;");
+ $self->pidl("ndr_set_flags(&$ndr->flags, $flags);");
+ }
+}
+
+###################################################################
+# end any special flags for an element or structure
+sub end_flags($$$)
+{
+ my ($self, $e, $ndr) = @_;
+ my $flags = has_property($e, "flag");
+ if (defined $flags) {
+ $self->pidl("$ndr->flags = _flags_save_$e->{TYPE};");
+ $self->deindent;
+ $self->pidl("}");
+ }
+}
+
+#####################################################################
+# parse the data of an array - push side
+sub ParseArrayPushHeader($$$$$$)
+{
+ my ($self,$e,$l,$ndr,$var_name,$env) = @_;
+
+ my $size;
+ my $length;
+
+ if ($l->{IS_ZERO_TERMINATED}) {
+ if (has_property($e, "charset")) {
+ $size = $length = "ndr_charset_length($var_name, CH_$e->{PROPERTIES}->{charset})";
+ } else {
+ $size = $length = "ndr_string_length($var_name, sizeof(*$var_name))";
+ }
+ if (defined($l->{SIZE_IS})) {
+ $size = ParseExpr($l->{SIZE_IS}, $env, $e);
+ }
+ if (defined($l->{LENGTH_IS})) {
+ $length = ParseExpr($l->{LENGTH_IS}, $env, $e);
+ }
+ } else {
+ $size = ParseExpr($l->{SIZE_IS}, $env, $e);
+ $length = ParseExpr($l->{LENGTH_IS}, $env, $e);
+ }
+
+ if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
+ $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
+ }
+
+ if ($l->{IS_VARYING}) {
+ $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, 0));"); # array offset
+ $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $length));");
+ }
+
+ return $length;
+}
+
+sub check_fully_dereferenced($$)
+{
+ my ($element, $env) = @_;
+
+ return sub ($) {
+ my $origvar = shift;
+ my $check = 0;
+
+ # Figure out the number of pointers in $ptr
+ my $expandedvar = $origvar;
+ $expandedvar =~ s/^(\**)//;
+ my $ptr = $1;
+
+ my $var = undef;
+ foreach (keys %$env) {
+ if ($env->{$_} eq $expandedvar) {
+ $var = $_;
+ last;
+ }
+ }
+
+ return($origvar) unless (defined($var));
+ my $e;
+ foreach (@{$element->{PARENT}->{ELEMENTS}}) {
+ if ($_->{NAME} eq $var) {
+ $e = $_;
+ last;
+ }
+ }
+
+ $e or die("Environment doesn't match siblings");
+
+ # See if pointer at pointer level $level
+ # needs to be checked.
+ my $nump = 0;
+ foreach (@{$e->{LEVELS}}) {
+ if ($_->{TYPE} eq "POINTER") {
+ $nump = $_->{POINTER_INDEX}+1;
+ }
+ }
+ warning($element->{ORIGINAL}, "Got pointer for `$e->{NAME}', expected fully dereferenced variable") if ($nump > length($ptr));
+ return ($origvar);
+ }
+}
+
+sub check_null_pointer($$$$)
+{
+ my ($element, $env, $print_fn, $return) = @_;
+
+ return sub ($) {
+ my $expandedvar = shift;
+ my $check = 0;
+
+ # Figure out the number of pointers in $ptr
+ $expandedvar =~ s/^(\**)//;
+ my $ptr = $1;
+
+ my $var = undef;
+ foreach (keys %$env) {
+ if ($env->{$_} eq $expandedvar) {
+ $var = $_;
+ last;
+ }
+ }
+
+ if (defined($var)) {
+ my $e;
+ # lookup ptr in $e
+ foreach (@{$element->{PARENT}->{ELEMENTS}}) {
+ if ($_->{NAME} eq $var) {
+ $e = $_;
+ last;
+ }
+ }
+
+ $e or die("Environment doesn't match siblings");
+
+ # See if pointer at pointer level $level
+ # needs to be checked.
+ foreach my $l (@{$e->{LEVELS}}) {
+ if ($l->{TYPE} eq "POINTER" and
+ $l->{POINTER_INDEX} == length($ptr)) {
+ # No need to check ref pointers
+ $check = ($l->{POINTER_TYPE} ne "ref");
+ last;
+ }
+
+ if ($l->{TYPE} eq "DATA") {
+ warning($element, "too much dereferences for `$var'");
+ }
+ }
+ } else {
+ warning($element, "unknown dereferenced expression `$expandedvar'");
+ $check = 1;
+ }
+
+ $print_fn->("if ($ptr$expandedvar == NULL) $return") if $check;
+ }
+}
+
+sub is_deferred_switch_non_empty($)
+{
+ # 1 if there needs to be a deferred branch in an ndr_pull/push,
+ # 0 otherwise.
+ my ($e) = @_;
+ my $have_default = 0;
+ foreach my $el (@{$e->{ELEMENTS}}) {
+ if ($el->{CASE} eq "default") {
+ $have_default = 1;
+ }
+ if ($el->{TYPE} ne "EMPTY") {
+ if (ContainsDeferred($el, $el->{LEVELS}[0])) {
+ return 1;
+ }
+ }
+ }
+ return ! $have_default;
+}
+
+sub ParseArrayPullGetSize($$$$$$)
+{
+ my ($self,$e,$l,$ndr,$var_name,$env) = @_;
+
+ my $size;
+
+ if ($l->{IS_CONFORMANT}) {
+ $size = "ndr_get_array_size($ndr, " . get_pointer_to($var_name) . ")";
+ } elsif ($l->{IS_ZERO_TERMINATED} and $l->{SIZE_IS} == 0 and $l->{LENGTH_IS} == 0) { # Noheader arrays
+ $size = "ndr_get_string_size($ndr, sizeof(*$var_name))";
+ } else {
+ $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
+ check_null_pointer($e, $env, sub { $self->pidl(shift); },
+ "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
+ check_fully_dereferenced($e, $env));
+ }
+
+ $self->pidl("size_$e->{NAME}_$l->{LEVEL_INDEX} = $size;");
+ my $array_size = "size_$e->{NAME}_$l->{LEVEL_INDEX}";
+
+ if (my $range = has_property($e, "range")) {
+ my ($low, $high) = split(/,/, $range, 2);
+ if ($low < 0) {
+ warning(0, "$low is invalid for the range of an array size");
+ }
+ if ($low == 0) {
+ $self->pidl("if ($array_size > $high) {");
+ } else {
+ $self->pidl("if ($array_size < $low || $array_size > $high) {");
+ }
+ $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
+ $self->pidl("}");
+ }
+
+ return $array_size;
+}
+
+#####################################################################
+# parse an array - pull side
+sub ParseArrayPullGetLength($$$$$$;$)
+{
+ my ($self,$e,$l,$ndr,$var_name,$env,$array_size) = @_;
+
+ if (not defined($array_size)) {
+ $array_size = $self->ParseArrayPullGetSize($e, $l, $ndr, $var_name, $env);
+ }
+
+ if (not $l->{IS_VARYING}) {
+ return $array_size;
+ }
+
+ my $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")";
+ $self->pidl("length_$e->{NAME}_$l->{LEVEL_INDEX} = $length;");
+ my $array_length = "length_$e->{NAME}_$l->{LEVEL_INDEX}";
+
+ if (my $range = has_property($e, "range")) {
+ my ($low, $high) = split(/,/, $range, 2);
+ if ($low < 0) {
+ warning(0, "$low is invalid for the range of an array size");
+ }
+ if ($low == 0) {
+ $self->pidl("if ($array_length > $high) {");
+ } else {
+ $self->pidl("if ($array_length < $low || $array_length > $high) {");
+ }
+ $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
+ $self->pidl("}");
+ }
+
+ return $array_length;
+}
+
+#####################################################################
+# parse an array - pull side
+sub ParseArrayPullHeader($$$$$$)
+{
+ my ($self,$e,$l,$ndr,$var_name,$env) = @_;
+
+ if ((!$l->{IS_SURROUNDING}) and $l->{IS_CONFORMANT}) {
+ $self->pidl("NDR_CHECK(ndr_pull_array_size($ndr, " . get_pointer_to($var_name) . "));");
+ }
+
+ if ($l->{IS_VARYING}) {
+ $self->pidl("NDR_CHECK(ndr_pull_array_length($ndr, " . get_pointer_to($var_name) . "));");
+ }
+
+ my $array_size = $self->ParseArrayPullGetSize($e, $l, $ndr, $var_name, $env);
+ my $array_length = $self->ParseArrayPullGetLength($e, $l, $ndr, $var_name, $env, $array_size);
+
+ if ($array_length ne $array_size) {
+ $self->pidl("if ($array_length > $array_size) {");
+ $self->indent;
+ $self->pidl("return ndr_pull_error($ndr, NDR_ERR_ARRAY_SIZE, \"Bad array size %u should exceed array length %u\", $array_size, $array_length);");
+ $self->deindent;
+ $self->pidl("}");
+ }
+
+ if ($l->{IS_CONFORMANT} and (defined($l->{SIZE_IS}) or not $l->{IS_ZERO_TERMINATED})) {
+ $self->defer("if ($var_name) {");
+ $self->defer_indent;
+ my $size = ParseExprExt($l->{SIZE_IS}, $env, $e->{ORIGINAL},
+ check_null_pointer($e, $env, sub { $self->defer(shift); },
+ "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
+ check_fully_dereferenced($e, $env));
+ $self->defer("NDR_CHECK(ndr_check_array_size($ndr, (void*)" . get_pointer_to($var_name) . ", $size));");
+ $self->defer_deindent;
+ $self->defer("}");
+ }
+
+ if ($l->{IS_VARYING} and (defined($l->{LENGTH_IS}) or not $l->{IS_ZERO_TERMINATED})) {
+ $self->defer("if ($var_name) {");
+ $self->defer_indent;
+ my $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL},
+ check_null_pointer($e, $env, sub { $self->defer(shift); },
+ "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for length_is()\");"),
+ check_fully_dereferenced($e, $env));
+ $self->defer("NDR_CHECK(ndr_check_array_length($ndr, (void*)" . get_pointer_to($var_name) . ", $length));");
+ $self->defer_deindent;
+ $self->defer("}");
+ }
+
+ if (ArrayDynamicallyAllocated($e,$l) and not is_charset_array($e,$l)) {
+ $self->AllocateArrayLevel($e,$l,$ndr,$var_name,$array_size);
+ }
+
+ return $array_length;
+}
+
+sub compression_alg($$)
+{
+ my ($e, $l) = @_;
+ my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
+
+ return $alg;
+}
+
+sub compression_clen($$$)
+{
+ my ($e, $l, $env) = @_;
+ my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
+
+ return ParseExpr($clen, $env, $e->{ORIGINAL});
+}
+
+sub compression_dlen($$$)
+{
+ my ($e,$l,$env) = @_;
+ my ($alg, $clen, $dlen) = split(/,/, $l->{COMPRESSION});
+
+ return ParseExpr($dlen, $env, $e->{ORIGINAL});
+}
+
+sub ParseCompressionPushStart($$$$$)
+{
+ my ($self,$e,$l,$ndr,$env) = @_;
+ my $comndr = "$ndr\_compressed";
+ my $alg = compression_alg($e, $l);
+ my $dlen = compression_dlen($e, $l, $env);
+
+ $self->pidl("{");
+ $self->indent;
+ $self->pidl("struct ndr_push *$comndr;");
+ $self->pidl("NDR_CHECK(ndr_push_compression_start($ndr, &$comndr, $alg, $dlen));");
+
+ return $comndr;
+}
+
+sub ParseCompressionPushEnd($$$$$)
+{
+ my ($self,$e,$l,$ndr,$env) = @_;
+ my $comndr = "$ndr\_compressed";
+ my $alg = compression_alg($e, $l);
+ my $dlen = compression_dlen($e, $l, $env);
+
+ $self->pidl("NDR_CHECK(ndr_push_compression_end($ndr, $comndr, $alg, $dlen));");
+ $self->deindent;
+ $self->pidl("}");
+}
+
+sub ParseCompressionPullStart($$$$$)
+{
+ my ($self,$e,$l,$ndr,$env) = @_;
+ my $comndr = "$ndr\_compressed";
+ my $alg = compression_alg($e, $l);
+ my $dlen = compression_dlen($e, $l, $env);
+ my $clen = compression_clen($e, $l, $env);
+
+ $self->pidl("{");
+ $self->indent;
+ $self->pidl("struct ndr_pull *$comndr;");
+ $self->pidl("NDR_CHECK(ndr_pull_compression_start($ndr, &$comndr, $alg, $dlen, $clen));");
+
+ return $comndr;
+}
+
+sub ParseCompressionPullEnd($$$$$)
+{
+ my ($self,$e,$l,$ndr,$env) = @_;
+ my $comndr = "$ndr\_compressed";
+ my $alg = compression_alg($e, $l);
+ my $dlen = compression_dlen($e, $l, $env);
+
+ $self->pidl("NDR_CHECK(ndr_pull_compression_end($ndr, $comndr, $alg, $dlen));");
+ $self->deindent;
+ $self->pidl("}");
+}
+
+sub ParseSubcontextPushStart($$$$$)
+{
+ my ($self,$e,$l,$ndr,$env) = @_;
+ my $subndr = "_ndr_$e->{NAME}";
+ my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
+
+ $self->pidl("{");
+ $self->indent;
+ $self->pidl("struct ndr_push *$subndr;");
+ $self->pidl("NDR_CHECK(ndr_push_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));");
+
+ if (defined $l->{COMPRESSION}) {
+ $subndr = $self->ParseCompressionPushStart($e, $l, $subndr, $env);
+ }
+
+ return $subndr;
+}
+
+sub ParseSubcontextPushEnd($$$$$)
+{
+ my ($self,$e,$l,$ndr,$env) = @_;
+ my $subndr = "_ndr_$e->{NAME}";
+ my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
+
+ if (defined $l->{COMPRESSION}) {
+ $self->ParseCompressionPushEnd($e, $l, $subndr, $env);
+ }
+
+ $self->pidl("NDR_CHECK(ndr_push_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));");
+ $self->deindent;
+ $self->pidl("}");
+}
+
+sub ParseSubcontextPullStart($$$$$)
+{
+ my ($self,$e,$l,$ndr,$env) = @_;
+ my $subndr = "_ndr_$e->{NAME}";
+ my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
+
+ $self->pidl("{");
+ $self->indent;
+ $self->pidl("struct ndr_pull *$subndr;");
+ $self->pidl("NDR_CHECK(ndr_pull_subcontext_start($ndr, &$subndr, $l->{HEADER_SIZE}, $subcontext_size));");
+
+ if (defined $l->{COMPRESSION}) {
+ $subndr = $self->ParseCompressionPullStart($e, $l, $subndr, $env);
+ }
+
+ return $subndr;
+}
+
+sub ParseSubcontextPullEnd($$$$$)
+{
+ my ($self,$e,$l,$ndr,$env) = @_;
+ my $subndr = "_ndr_$e->{NAME}";
+ my $subcontext_size = ParseExpr($l->{SUBCONTEXT_SIZE}, $env, $e->{ORIGINAL});
+
+ if (defined $l->{COMPRESSION}) {
+ $self->ParseCompressionPullEnd($e, $l, $subndr, $env);
+ }
+
+ $self->pidl("NDR_CHECK(ndr_pull_subcontext_end($ndr, $subndr, $l->{HEADER_SIZE}, $subcontext_size));");
+ $self->deindent;
+ $self->pidl("}");
+}
+
+sub ParseElementPushLevel
+{
+ my ($self,$e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_;
+
+ my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
+
+ if ($l->{TYPE} eq "ARRAY" and ($l->{IS_CONFORMANT} or $l->{IS_VARYING})) {
+ $var_name = get_pointer_to($var_name);
+ }
+
+ if (defined($ndr_flags)) {
+ if ($l->{TYPE} eq "SUBCONTEXT") {
+ my $subndr = $self->ParseSubcontextPushStart($e, $l, $ndr, $env);
+ $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $subndr, $var_name, $env, 1, 1);
+ $self->ParseSubcontextPushEnd($e, $l, $ndr, $env);
+ } elsif ($l->{TYPE} eq "POINTER") {
+ $self->ParsePtrPush($e, $l, $ndr, $var_name);
+ } elsif ($l->{TYPE} eq "ARRAY") {
+ my $length = $self->ParseArrayPushHeader($e, $l, $ndr, $var_name, $env);
+
+ my $nl = GetNextLevel($e, $l);
+
+ # Allow speedups for arrays of scalar types
+ if (is_charset_array($e,$l)) {
+ if ($l->{IS_TO_NULL}) {
+ $self->pidl("NDR_CHECK(ndr_push_charset_to_null($ndr, $ndr_flags, $var_name, $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));");
+ } else {
+ $self->pidl("NDR_CHECK(ndr_push_charset($ndr, $ndr_flags, $var_name, $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));");
+ }
+ return;
+ } elsif (has_fast_array($e,$l)) {
+ $self->pidl("NDR_CHECK(ndr_push_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));");
+ return;
+ }
+ } elsif ($l->{TYPE} eq "SWITCH") {
+ $self->ParseSwitchPush($e, $l, $ndr, $var_name, $env);
+ } elsif ($l->{TYPE} eq "DATA") {
+ $self->ParseDataPush($e, $l, $ndr, $var_name, $primitives, $deferred);
+ } elsif ($l->{TYPE} eq "TYPEDEF") {
+ $typefamily{$e->{DATA}->{TYPE}}->{PUSH_FN_BODY}->($self, $e->{DATA}, $ndr, $var_name);
+ }
+ }
+
+ if ($l->{TYPE} eq "POINTER" and $l->{POINTER_TYPE} eq "ignore") {
+ $self->pidl("/* [ignore] '$e->{NAME}' */");
+ } elsif ($l->{TYPE} eq "POINTER" and $deferred) {
+ my $rel_var_name = $var_name;
+ if ($l->{POINTER_TYPE} ne "ref") {
+ $self->pidl("if ($var_name) {");
+ $self->indent;
+ if ($l->{POINTER_TYPE} eq "relative") {
+ $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_start($ndr, $rel_var_name));");
+ }
+ if ($l->{POINTER_TYPE} eq "relative_short") {
+ $self->pidl("NDR_CHECK(ndr_push_short_relative_ptr2($ndr, $var_name));");
+ }
+ }
+ $var_name = get_value_of($var_name);
+ $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 1);
+
+ if ($l->{POINTER_TYPE} ne "ref") {
+ if ($l->{POINTER_TYPE} eq "relative") {
+ $self->pidl("NDR_CHECK(ndr_push_relative_ptr2_end($ndr, $rel_var_name));");
+ }
+ $self->deindent;
+ $self->pidl("}");
+ }
+ } elsif ($l->{TYPE} eq "ARRAY" and not has_fast_array($e,$l) and
+ not is_charset_array($e, $l)) {
+ my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
+ my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
+
+ my $array_pointless = ($length eq "0");
+
+ if ($array_pointless) {
+ warning($e->{ORIGINAL}, "pointless array `$e->{NAME}' will always have size 0");
+ }
+
+ $var_name = get_array_element($var_name, $counter);
+
+ if ((($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) and not $array_pointless) {
+ $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {");
+ $self->indent;
+ $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 1, 0);
+ $self->deindent;
+ $self->pidl("}");
+ }
+
+ if ($deferred and ContainsDeferred($e, $l) and not $array_pointless) {
+ $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {");
+ $self->indent;
+ $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, 0, 1);
+ $self->deindent;
+ $self->pidl("}");
+ }
+ } elsif ($l->{TYPE} eq "SWITCH") {
+ $self->ParseElementPushLevel($e, GetNextLevel($e, $l), $ndr, $var_name, $env, $primitives, $deferred);
+ }
+}
+
+#####################################################################
+# parse scalars in a structure element
+sub ParseElementPush($$$$$$)
+{
+ my ($self,$e,$ndr,$env,$primitives,$deferred) = @_;
+ my $subndr = undef;
+
+ my $var_name = $env->{$e->{NAME}};
+
+ if (has_property($e, "skip") or has_property($e, "skip_noinit")) {
+ $self->pidl("/* [skip] '$var_name' */");
+ return;
+ }
+
+ return if ContainsPipe($e, $e->{LEVELS}[0]);
+
+ return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0]));
+
+ # Representation type is different from transmit_as
+ if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
+ $self->pidl("{");
+ $self->indent;
+ my $transmit_name = "_transmit_$e->{NAME}";
+ $self->pidl(mapTypeName($e->{TYPE}) ." $transmit_name;");
+ $self->pidl("NDR_CHECK(ndr_$e->{REPRESENTATION_TYPE}_to_$e->{TYPE}($var_name, " . get_pointer_to($transmit_name) . "));");
+ $var_name = $transmit_name;
+ }
+
+ $var_name = append_prefix($e, $var_name);
+
+ $self->start_flags($e, $ndr);
+
+ if (defined(my $value = has_property($e, "value"))) {
+ $var_name = ParseExpr($value, $env, $e->{ORIGINAL});
+ }
+
+ $self->ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $var_name, $env, $primitives, $deferred);
+
+ $self->end_flags($e, $ndr);
+
+ if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
+ $self->deindent;
+ $self->pidl("}");
+ }
+}
+
+#####################################################################
+# parse a pointer in a struct element or function
+sub ParsePtrPush($$$$$)
+{
+ my ($self,$e,$l,$ndr,$var_name) = @_;
+
+ if ($l->{POINTER_TYPE} eq "ref") {
+ if ($l->{LEVEL_INDEX} > 0) {
+ $self->pidl("if ($var_name == NULL) {");
+ $self->indent;
+ $self->pidl("return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL [ref] pointer\");");
+ $self->deindent;
+ $self->pidl("}");
+ }
+ if ($l->{LEVEL} eq "EMBEDDED") {
+ $self->pidl("NDR_CHECK(ndr_push_ref_ptr(ndr)); /* $var_name */");
+ }
+ } elsif ($l->{POINTER_TYPE} eq "relative") {
+ $self->pidl("NDR_CHECK(ndr_push_relative_ptr1($ndr, $var_name));");
+ } elsif ($l->{POINTER_TYPE} eq "relative_short") {
+ $self->pidl("NDR_CHECK(ndr_push_short_relative_ptr1($ndr, $var_name));");
+ } elsif ($l->{POINTER_TYPE} eq "unique") {
+ $self->pidl("NDR_CHECK(ndr_push_unique_ptr($ndr, $var_name));");
+ } elsif ($l->{POINTER_TYPE} eq "full") {
+ $self->pidl("NDR_CHECK(ndr_push_full_ptr($ndr, $var_name));");
+ } elsif ($l->{POINTER_TYPE} eq "ignore") {
+ # We don't want this pointer to appear on the wire at all
+ $self->pidl("NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, 0));");
+ } else {
+ die("Unhandled pointer type $l->{POINTER_TYPE}");
+ }
+}
+
+sub need_pointer_to($$$)
+{
+ my ($e, $l, $scalar_only) = @_;
+
+ my $t;
+ if (ref($l->{DATA_TYPE})) {
+ $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
+ } else {
+ $t = $l->{DATA_TYPE};
+ }
+
+ if (not Parse::Pidl::Typelist::is_scalar($t)) {
+ return 1 if $scalar_only;
+ }
+
+ my $arrays = 0;
+
+ foreach my $tl (@{$e->{LEVELS}}) {
+ last if $l == $tl;
+ if ($tl->{TYPE} eq "ARRAY") {
+ $arrays++;
+ }
+ }
+
+ if (Parse::Pidl::Typelist::scalar_is_reference($t)) {
+ return 1 unless $arrays;
+ }
+
+ return 0;
+}
+
+sub ParseDataPrint($$$$$)
+{
+ my ($self, $e, $l, $ndr, $var_name) = @_;
+
+ if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
+
+ if (need_pointer_to($e, $l, 1)) {
+ $var_name = get_pointer_to($var_name);
+ }
+
+ $self->pidl(TypeFunctionName("ndr_print", $l->{DATA_TYPE})."($ndr, \"$e->{NAME}\", $var_name);");
+ } else {
+ $self->ParseTypePrint($l->{DATA_TYPE}, $ndr, $var_name);
+ }
+}
+
+#####################################################################
+# print scalars in a structure element
+sub ParseElementPrint($$$$$)
+{
+ my($self, $e, $ndr, $var_name, $env) = @_;
+
+ return if (has_property($e, "noprint"));
+ my $cur_depth = 0;
+ my $ignore_depth = 0xFFFF;
+
+ $self->start_flags($e, $ndr);
+ if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
+ $self->pidl("ndr_print_$e->{REPRESENTATION_TYPE}($ndr, \"$e->{NAME}\", $var_name);");
+ $self->end_flags($e, $ndr);
+ return;
+ }
+
+ $var_name = append_prefix($e, $var_name);
+
+ if (defined(my $value = has_property($e, "value"))) {
+ $var_name = "($ndr->flags & LIBNDR_PRINT_SET_VALUES)?" . ParseExpr($value,$env, $e->{ORIGINAL}) . ":$var_name";
+ }
+
+ foreach my $l (@{$e->{LEVELS}}) {
+ $cur_depth += 1;
+
+ if ($cur_depth > $ignore_depth) {
+ next;
+ }
+
+ if ($l->{TYPE} eq "POINTER") {
+ $self->pidl("ndr_print_ptr($ndr, \"$e->{NAME}\", $var_name);");
+ if ($l->{POINTER_TYPE} eq "ignore") {
+ $self->pidl("/* [ignore] '$e->{NAME}' */");
+ $ignore_depth = $cur_depth;
+ last;
+ }
+ $self->pidl("$ndr->depth++;");
+ if ($l->{POINTER_TYPE} ne "ref") {
+ $self->pidl("if ($var_name) {");
+ $self->indent;
+ }
+ $var_name = get_value_of($var_name);
+ } elsif ($l->{TYPE} eq "ARRAY") {
+ my $length;
+
+ if ($l->{IS_CONFORMANT} or $l->{IS_VARYING}) {
+ $var_name = get_pointer_to($var_name);
+ }
+
+ if ($l->{IS_ZERO_TERMINATED} and not defined($l->{LENGTH_IS})) {
+ $length = "ndr_string_length($var_name, sizeof(*$var_name))";
+ } else {
+ $length = ParseExprExt($l->{LENGTH_IS}, $env, $e->{ORIGINAL},
+ check_null_pointer($e, $env, sub { $self->pidl(shift); }, "return;"), check_fully_dereferenced($e, $env));
+ }
+
+ if (is_charset_array($e,$l)) {
+ $self->pidl("ndr_print_string($ndr, \"$e->{NAME}\", $var_name);");
+ last;
+ } elsif (has_fast_array($e, $l)) {
+ my $nl = GetNextLevel($e, $l);
+ $self->pidl("ndr_print_array_$nl->{DATA_TYPE}($ndr, \"$e->{NAME}\", $var_name, $length);");
+ last;
+ } else {
+ my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
+
+ $self->pidl("$ndr->print($ndr, \"\%s: ARRAY(\%d)\", \"$e->{NAME}\", (int)$length);");
+ $self->pidl("$ndr->depth++;");
+ $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {");
+ $self->indent;
+
+ $var_name = get_array_element($var_name, $counter);
+ }
+ } elsif ($l->{TYPE} eq "DATA") {
+ $self->ParseDataPrint($e, $l, $ndr, $var_name);
+ } elsif ($l->{TYPE} eq "SWITCH") {
+ my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
+ check_null_pointer($e, $env, sub { $self->pidl(shift); }, "return;"), check_fully_dereferenced($e, $env));
+ $self->pidl("ndr_print_set_switch_value($ndr, " . get_pointer_to($var_name) . ", $switch_var);");
+ }
+ }
+
+ foreach my $l (reverse @{$e->{LEVELS}}) {
+ $cur_depth -= 1;
+
+ if ($cur_depth > $ignore_depth) {
+ next;
+ }
+
+ if ($l->{TYPE} eq "POINTER") {
+ if ($l->{POINTER_TYPE} eq "ignore") {
+ next;
+ }
+
+ if ($l->{POINTER_TYPE} ne "ref") {
+ $self->deindent;
+ $self->pidl("}");
+ }
+ $self->pidl("$ndr->depth--;");
+ } elsif (($l->{TYPE} eq "ARRAY")
+ and not is_charset_array($e,$l)
+ and not has_fast_array($e,$l)) {
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("$ndr->depth--;");
+ }
+ }
+
+ $self->end_flags($e, $ndr);
+}
+
+#####################################################################
+# parse scalars in a structure element - pull size
+sub ParseSwitchPull($$$$$$)
+{
+ my($self,$e,$l,$ndr,$var_name,$env) = @_;
+ my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
+ check_null_pointer($e, $env, sub { $self->pidl(shift); },
+ "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for switch_is()\");"),
+ check_fully_dereferenced($e, $env));
+
+ $var_name = get_pointer_to($var_name);
+ $self->pidl("NDR_CHECK(ndr_pull_set_switch_value($ndr, $var_name, $switch_var));");
+}
+
+#####################################################################
+# push switch element
+sub ParseSwitchPush($$$$$$)
+{
+ my($self,$e,$l,$ndr,$var_name,$env) = @_;
+ my $switch_var = ParseExprExt($l->{SWITCH_IS}, $env, $e->{ORIGINAL},
+ check_null_pointer($e, $env, sub { $self->pidl(shift); },
+ "return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for switch_is()\");"),
+ check_fully_dereferenced($e, $env));
+
+ $var_name = get_pointer_to($var_name);
+ $self->pidl("NDR_CHECK(ndr_push_set_switch_value($ndr, $var_name, $switch_var));");
+}
+
+sub ParseDataPull($$$$$$$)
+{
+ my ($self,$e,$l,$ndr,$var_name,$primitives,$deferred) = @_;
+
+ if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
+
+ my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
+
+ if (need_pointer_to($e, $l, 0)) {
+ $var_name = get_pointer_to($var_name);
+ }
+
+ $var_name = get_pointer_to($var_name);
+
+ $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_pull", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));");
+
+ my $pl = GetPrevLevel($e, $l);
+
+ my $range = has_property($e, "range");
+ if ($range and $pl->{TYPE} ne "ARRAY") {
+ $var_name = get_value_of($var_name);
+ my $signed = Parse::Pidl::Typelist::is_signed($l->{DATA_TYPE});
+ my ($low, $high) = split(/,/, $range, 2);
+ if ($low < 0 and not $signed) {
+ warning(0, "$low is invalid for the range of an unsigned type");
+ }
+ if ($low == 0 and not $signed) {
+ $self->pidl("if ($var_name > $high) {");
+ } else {
+ $self->pidl("if ($var_name < $low || $var_name > $high) {");
+ }
+ $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_RANGE, \"value out of range\");");
+ $self->pidl("}");
+ }
+ } else {
+ $self->ParseTypePull($l->{DATA_TYPE}, $ndr, $var_name, $primitives, $deferred);
+ }
+}
+
+sub ParseDataPush($$$$$$$)
+{
+ my ($self,$e,$l,$ndr,$var_name,$primitives,$deferred) = @_;
+
+ if (not ref($l->{DATA_TYPE}) or defined($l->{DATA_TYPE}->{NAME})) {
+
+ my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
+
+ # strings are passed by value rather than reference
+ if (need_pointer_to($e, $l, 1)) {
+ $var_name = get_pointer_to($var_name);
+ }
+
+ $self->pidl("NDR_CHECK(".TypeFunctionName("ndr_push", $l->{DATA_TYPE})."($ndr, $ndr_flags, $var_name));");
+ } else {
+ $self->ParseTypePush($l->{DATA_TYPE}, $ndr, $var_name, $primitives, $deferred);
+ }
+}
+
+sub CalcNdrFlags($$$)
+{
+ my ($l,$primitives,$deferred) = @_;
+
+ my $scalars = 0;
+ my $buffers = 0;
+
+ # Add NDR_SCALARS if this one is deferred
+ # and deferreds may be pushed
+ $scalars = 1 if ($l->{IS_DEFERRED} and $deferred);
+
+ # Add NDR_SCALARS if this one is not deferred and
+ # primitives may be pushed
+ $scalars = 1 if (!$l->{IS_DEFERRED} and $primitives);
+
+ # Add NDR_BUFFERS if this one contains deferred stuff
+ # and deferreds may be pushed
+ $buffers = 1 if ($l->{CONTAINS_DEFERRED} and $deferred);
+
+ return "NDR_SCALARS|NDR_BUFFERS" if ($scalars and $buffers);
+ return "NDR_SCALARS" if ($scalars);
+ return "NDR_BUFFERS" if ($buffers);
+ return undef;
+}
+
+sub ParseMemCtxPullFlags($$$$)
+{
+ my ($self, $e, $l) = @_;
+
+ return undef unless ($l->{TYPE} eq "POINTER" or $l->{TYPE} eq "ARRAY");
+ return undef if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ignore"));
+
+ return undef unless ($l->{TYPE} ne "ARRAY" or ArrayDynamicallyAllocated($e,$l));
+ return undef if has_fast_array($e, $l);
+ return undef if is_charset_array($e, $l);
+
+ my $mem_flags = "0";
+
+ if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ref")) {
+ my $nl = GetNextLevel($e, $l);
+ return undef if ($nl->{TYPE} eq "PIPE");
+ return undef if ($nl->{TYPE} eq "ARRAY");
+ return undef if (($nl->{TYPE} eq "DATA") and ($nl->{DATA_TYPE} eq "string"));
+
+ if ($l->{LEVEL} eq "TOP") {
+ $mem_flags = "LIBNDR_FLAG_REF_ALLOC";
+ }
+ }
+
+ return $mem_flags;
+}
+
+sub ParseMemCtxPullStart($$$$$)
+{
+ my ($self, $e, $l, $ndr, $ptr_name) = @_;
+
+ my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}";
+ my $mem_c_ctx = $ptr_name;
+ my $mem_c_flags = $self->ParseMemCtxPullFlags($e, $l);
+
+ return unless defined($mem_c_flags);
+
+ $self->pidl("$mem_r_ctx = NDR_PULL_GET_MEM_CTX($ndr);");
+ $self->pidl("NDR_PULL_SET_MEM_CTX($ndr, $mem_c_ctx, $mem_c_flags);");
+}
+
+sub ParseMemCtxPullEnd($$$$)
+{
+ my ($self, $e, $l, $ndr) = @_;
+
+ my $mem_r_ctx = "_mem_save_$e->{NAME}_$l->{LEVEL_INDEX}";
+ my $mem_r_flags = $self->ParseMemCtxPullFlags($e, $l);
+
+ return unless defined($mem_r_flags);
+
+ $self->pidl("NDR_PULL_SET_MEM_CTX($ndr, $mem_r_ctx, $mem_r_flags);");
+}
+
+sub CheckStringTerminator($$$$$)
+{
+ my ($self,$ndr,$e,$l,$length) = @_;
+ my $nl = GetNextLevel($e, $l);
+
+ # Make sure last element is zero!
+ $self->pidl("NDR_CHECK(ndr_check_string_terminator($ndr, $length, sizeof($nl->{DATA_TYPE}_t)));");
+}
+
+sub ParseElementPullLevel
+{
+ my($self,$e,$l,$ndr,$var_name,$env,$primitives,$deferred) = @_;
+
+ my $ndr_flags = CalcNdrFlags($l, $primitives, $deferred);
+ my $array_length = undef;
+
+ if (has_property($e, "skip") or has_property($e, "skip_noinit")) {
+ $self->pidl("/* [skip] '$var_name' */");
+ if (not has_property($e, "skip_noinit")) {
+ $self->pidl("ZERO_STRUCT($var_name);");
+ }
+ return;
+ }
+
+ if ($l->{TYPE} eq "ARRAY" and ($l->{IS_VARYING} or $l->{IS_CONFORMANT})) {
+ $var_name = get_pointer_to($var_name);
+ }
+
+ # Only pull something if there's actually something to be pulled
+ if (defined($ndr_flags)) {
+ if ($l->{TYPE} eq "SUBCONTEXT") {
+ my $subndr = $self->ParseSubcontextPullStart($e, $l, $ndr, $env);
+ $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $subndr, $var_name, $env, 1, 1);
+ $self->ParseSubcontextPullEnd($e, $l, $ndr, $env);
+ } elsif ($l->{TYPE} eq "ARRAY") {
+ my $length = $self->ParseArrayPullHeader($e, $l, $ndr, $var_name, $env);
+ $array_length = $length;
+
+ my $nl = GetNextLevel($e, $l);
+
+ if (is_charset_array($e,$l)) {
+ if ($l->{IS_ZERO_TERMINATED}) {
+ $self->CheckStringTerminator($ndr, $e, $l, $length);
+ }
+ if ($l->{IS_TO_NULL}) {
+ $self->pidl("NDR_CHECK(ndr_pull_charset_to_null($ndr, $ndr_flags, ".get_pointer_to($var_name).", $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));");
+ } else {
+ $self->pidl("NDR_CHECK(ndr_pull_charset($ndr, $ndr_flags, ".get_pointer_to($var_name).", $length, sizeof(" . mapTypeName($nl->{DATA_TYPE}) . "), CH_$e->{PROPERTIES}->{charset}));");
+ }
+ return;
+ } elsif (has_fast_array($e, $l)) {
+ if ($l->{IS_ZERO_TERMINATED}) {
+ $self->CheckStringTerminator($ndr,$e,$l,$length);
+ }
+ $self->pidl("NDR_CHECK(ndr_pull_array_$nl->{DATA_TYPE}($ndr, $ndr_flags, $var_name, $length));");
+ return;
+ }
+ } elsif ($l->{TYPE} eq "POINTER") {
+ $self->ParsePtrPull($e, $l, $ndr, $var_name);
+ } elsif ($l->{TYPE} eq "SWITCH") {
+ $self->ParseSwitchPull($e, $l, $ndr, $var_name, $env);
+ } elsif ($l->{TYPE} eq "DATA") {
+ $self->ParseDataPull($e, $l, $ndr, $var_name, $primitives, $deferred);
+ } elsif ($l->{TYPE} eq "TYPEDEF") {
+ $typefamily{$e->{DATA}->{TYPE}}->{PULL_FN_BODY}->($self, $e->{DATA}, $ndr, $var_name);
+ }
+ }
+
+ # add additional constructions
+ if ($l->{TYPE} eq "POINTER" and $l->{POINTER_TYPE} eq "ignore") {
+ $self->pidl("/* [ignore] '$e->{NAME}' */");
+ } elsif ($l->{TYPE} eq "POINTER" and $deferred) {
+ if ($l->{POINTER_TYPE} ne "ref") {
+ $self->pidl("if ($var_name) {");
+ $self->indent;
+
+ if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") {
+ $self->pidl("uint32_t _relative_save_offset;");
+ $self->pidl("_relative_save_offset = $ndr->offset;");
+ $self->pidl("NDR_CHECK(ndr_pull_relative_ptr2($ndr, $var_name));");
+ }
+ }
+
+ $self->ParseMemCtxPullStart($e, $l, $ndr, $var_name);
+
+ $var_name = get_value_of($var_name);
+ $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, 1, 1);
+
+ $self->ParseMemCtxPullEnd($e, $l, $ndr);
+
+ if ($l->{POINTER_TYPE} ne "ref") {
+ if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") {
+ $self->pidl("if ($ndr->offset > $ndr->relative_highest_offset) {");
+ $self->indent;
+ $self->pidl("$ndr->relative_highest_offset = $ndr->offset;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("$ndr->offset = _relative_save_offset;");
+ }
+ $self->deindent;
+ $self->pidl("}");
+ }
+ } elsif ($l->{TYPE} eq "ARRAY" and
+ not has_fast_array($e,$l) and not is_charset_array($e, $l)) {
+ my $length = $array_length;
+ my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}";
+ my $array_name = $var_name;
+
+ if (not defined($length)) {
+ $length = $self->ParseArrayPullGetLength($e, $l, $ndr, $var_name, $env);
+ }
+
+ $var_name = get_array_element($var_name, $counter);
+
+ $self->ParseMemCtxPullStart($e, $l, $ndr, $array_name);
+
+ if (($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) {
+ my $nl = GetNextLevel($e,$l);
+
+ if ($l->{IS_ZERO_TERMINATED}) {
+ $self->CheckStringTerminator($ndr,$e,$l,$length);
+ }
+
+ $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {");
+ $self->indent;
+ $self->ParseElementPullLevel($e, $nl, $ndr, $var_name, $env, 1, 0);
+ $self->deindent;
+ $self->pidl("}");
+ }
+
+ if ($deferred and ContainsDeferred($e, $l)) {
+ $self->pidl("for ($counter = 0; $counter < ($length); $counter++) {");
+ $self->indent;
+ $self->ParseElementPullLevel($e,GetNextLevel($e,$l), $ndr, $var_name, $env, 0, 1);
+ $self->deindent;
+ $self->pidl("}");
+ }
+
+ $self->ParseMemCtxPullEnd($e, $l, $ndr);
+
+ } elsif ($l->{TYPE} eq "SWITCH") {
+ $self->ParseElementPullLevel($e, GetNextLevel($e,$l), $ndr, $var_name, $env, $primitives, $deferred);
+ }
+}
+
+#####################################################################
+# parse scalars in a structure element - pull size
+sub ParseElementPull($$$$$$)
+{
+ my($self,$e,$ndr,$env,$primitives,$deferred) = @_;
+
+ my $var_name = $env->{$e->{NAME}};
+ my $represent_name;
+ my $transmit_name;
+
+ return if ContainsPipe($e, $e->{LEVELS}[0]);
+
+ return unless $primitives or ($deferred and ContainsDeferred($e, $e->{LEVELS}[0]));
+
+ if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
+ $self->pidl("{");
+ $self->indent;
+ $represent_name = $var_name;
+ $transmit_name = "_transmit_$e->{NAME}";
+ $var_name = $transmit_name;
+ $self->pidl(mapTypeName($e->{TYPE})." $var_name;");
+ }
+
+ $var_name = append_prefix($e, $var_name);
+
+ $self->start_flags($e, $ndr);
+
+ $self->ParseElementPullLevel($e,$e->{LEVELS}[0],$ndr,$var_name,$env,$primitives,$deferred);
+
+ $self->end_flags($e, $ndr);
+
+ # Representation type is different from transmit_as
+ if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) {
+ $self->pidl("NDR_CHECK(ndr_$e->{TYPE}_to_$e->{REPRESENTATION_TYPE}($transmit_name, ".get_pointer_to($represent_name)."));");
+ $self->deindent;
+ $self->pidl("}");
+ }
+}
+
+#####################################################################
+# parse a pointer in a struct element or function
+sub ParsePtrPull($$$$$)
+{
+ my($self, $e,$l,$ndr,$var_name) = @_;
+
+ my $nl = GetNextLevel($e, $l);
+ my $next_is_array = ($nl->{TYPE} eq "ARRAY");
+ my $next_is_string = (($nl->{TYPE} eq "DATA") and
+ ($nl->{DATA_TYPE} eq "string"));
+
+ if ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP") {
+
+ if (!$next_is_array and !$next_is_string) {
+ $self->pidl("if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {");
+ $self->pidl("\tNDR_PULL_ALLOC($ndr, $var_name);");
+ $self->pidl("}");
+ }
+
+ return;
+ } elsif ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "EMBEDDED") {
+ $self->pidl("NDR_CHECK(ndr_pull_ref_ptr($ndr, &_ptr_$e->{NAME}));");
+ } elsif (($l->{POINTER_TYPE} eq "unique") or
+ ($l->{POINTER_TYPE} eq "relative") or
+ ($l->{POINTER_TYPE} eq "full")) {
+ $self->pidl("NDR_CHECK(ndr_pull_generic_ptr($ndr, &_ptr_$e->{NAME}));");
+ } elsif ($l->{POINTER_TYPE} eq "relative_short") {
+ $self->pidl("NDR_CHECK(ndr_pull_relative_ptr_short($ndr, &_ptr_$e->{NAME}));");
+ } elsif ($l->{POINTER_TYPE} eq "ignore") {
+ #We want to consume the pointer bytes, but ignore the pointer value
+ $self->pidl("NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &_ptr_$e->{NAME}));");
+ $self->pidl("_ptr_$e->{NAME} = 0;");
+ } else {
+ die("Unhandled pointer type $l->{POINTER_TYPE}");
+ }
+
+ $self->pidl("if (_ptr_$e->{NAME}) {");
+ $self->indent;
+
+ if ($l->{POINTER_TYPE} eq "ignore") {
+ # Don't do anything, we don't want to do the
+ # allocation, as we forced it to NULL just above, and
+ # we may not know the declared type anyway.
+ } else {
+ # Don't do this for arrays, they're allocated at the actual level
+ # of the array
+ unless ($next_is_array or $next_is_string) {
+ $self->pidl("NDR_PULL_ALLOC($ndr, $var_name);");
+ } else {
+ # FIXME: Yes, this is nasty.
+ # We allocate an array twice
+ # - once just to indicate that it's there,
+ # - then the real allocation...
+ $self->pidl("NDR_PULL_ALLOC($ndr, $var_name);");
+ }
+ }
+
+ #$self->pidl("memset($var_name, 0, sizeof($var_name));");
+ if ($l->{POINTER_TYPE} eq "relative" or $l->{POINTER_TYPE} eq "relative_short") {
+ $self->pidl("NDR_CHECK(ndr_pull_relative_ptr1($ndr, $var_name, _ptr_$e->{NAME}));");
+ }
+ $self->deindent;
+ $self->pidl("} else {");
+ $self->pidl("\t$var_name = NULL;");
+ $self->pidl("}");
+}
+
+sub CheckRefPtrs($$$$)
+{
+ my ($self,$e,$ndr,$env) = @_;
+
+ return if ContainsPipe($e, $e->{LEVELS}[0]);
+ return if ($e->{LEVELS}[0]->{TYPE} ne "POINTER");
+ return if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref");
+
+ my $var_name = $env->{$e->{NAME}};
+ $var_name = append_prefix($e, $var_name);
+
+ $self->pidl("if ($var_name == NULL) {");
+ $self->indent;
+ $self->pidl("return ndr_push_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL [ref] pointer\");");
+ $self->deindent;
+ $self->pidl("}");
+}
+
+sub ParseStructPushPrimitives($$$$$)
+{
+ my ($self, $struct, $ndr, $varname, $env) = @_;
+
+ $self->CheckRefPtrs($_, $ndr, $env) foreach (@{$struct->{ELEMENTS}});
+
+ # see if the structure contains a conformant array. If it
+ # does, then it must be the last element of the structure, and
+ # we need to push the conformant length early, as it fits on
+ # the wire before the structure (and even before the structure
+ # alignment)
+ if (defined($struct->{SURROUNDING_ELEMENT})) {
+ my $e = $struct->{SURROUNDING_ELEMENT};
+
+ if (defined($e->{LEVELS}[0]) and
+ $e->{LEVELS}[0]->{TYPE} eq "ARRAY") {
+ my $size;
+
+ if ($e->{LEVELS}[0]->{IS_ZERO_TERMINATED}) {
+ if (has_property($e, "charset")) {
+ $size = "ndr_charset_length($varname->$e->{NAME}, CH_$e->{PROPERTIES}->{charset})";
+ } else {
+ $size = "ndr_string_length($varname->$e->{NAME}, sizeof(*$varname->$e->{NAME}))";
+ }
+ if (defined($e->{LEVELS}[0]->{SIZE_IS})) {
+ $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL});
+ }
+ } else {
+ $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL});
+ }
+
+ $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, $size));");
+ } else {
+ $self->pidl("NDR_CHECK(ndr_push_uint3264($ndr, NDR_SCALARS, ndr_string_array_size($ndr, $varname->$e->{NAME})));");
+ }
+ }
+
+ $self->pidl("NDR_CHECK(ndr_push_align($ndr, $struct->{ALIGN}));");
+
+ if (defined($struct->{PROPERTIES}{relative_base})) {
+ # set the current offset as base for relative pointers
+ # and store it based on the toplevel struct/union
+ $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
+ }
+
+ $self->ParseElementPush($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
+
+ $self->pidl("NDR_CHECK(ndr_push_trailer_align($ndr, $struct->{ALIGN}));");
+}
+
+sub ParseStructPushDeferred($$$$)
+{
+ my ($self, $struct, $ndr, $varname, $env) = @_;
+ if (defined($struct->{PROPERTIES}{relative_base})) {
+ # retrieve the current offset as base for relative pointers
+ # based on the toplevel struct/union
+ $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset2($ndr, $varname));");
+ }
+ $self->ParseElementPush($_, $ndr, $env, 0, 1) foreach (@{$struct->{ELEMENTS}});
+}
+
+#####################################################################
+# parse a struct
+sub ParseStructPush($$$$)
+{
+ my ($self, $struct, $ndr, $varname) = @_;
+
+ return unless defined($struct->{ELEMENTS});
+
+ my $env = GenerateStructEnv($struct, $varname);
+
+ EnvSubstituteValue($env, $struct);
+
+ $self->DeclareArrayVariablesNoZero($_, $env) foreach (@{$struct->{ELEMENTS}});
+
+ $self->start_flags($struct, $ndr);
+
+ $self->pidl("NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);");
+ $self->pidl("if (ndr_flags & NDR_SCALARS) {");
+ $self->indent;
+ $self->ParseStructPushPrimitives($struct, $ndr, $varname, $env);
+ $self->deindent;
+ $self->pidl("}");
+
+ $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
+ $self->indent;
+ $self->ParseStructPushDeferred($struct, $ndr, $varname, $env);
+ $self->deindent;
+ $self->pidl("}");
+
+ $self->end_flags($struct, $ndr);
+}
+
+#####################################################################
+# generate a push function for an enum
+sub ParseEnumPush($$$$)
+{
+ my($self,$enum,$ndr,$varname) = @_;
+ my($type_fn) = $enum->{BASE_TYPE};
+
+ $self->start_flags($enum, $ndr);
+ $self->pidl("NDR_CHECK(ndr_push_enum_$type_fn($ndr, NDR_SCALARS, $varname));");
+ $self->end_flags($enum, $ndr);
+}
+
+#####################################################################
+# generate a pull function for an enum
+sub ParseEnumPull($$$$)
+{
+ my($self,$enum,$ndr,$varname) = @_;
+ my($type_fn) = $enum->{BASE_TYPE};
+ my($type_v_decl) = mapTypeName($type_fn);
+
+ $self->pidl("$type_v_decl v;");
+ $self->start_flags($enum, $ndr);
+ $self->pidl("NDR_CHECK(ndr_pull_enum_$type_fn($ndr, NDR_SCALARS, &v));");
+ $self->pidl("*$varname = v;");
+
+ $self->end_flags($enum, $ndr);
+}
+
+#####################################################################
+# generate a print function for an enum
+sub ParseEnumPrint($$$$$)
+{
+ my($self,$enum,$ndr,$name,$varname) = @_;
+
+ $self->pidl("const char *val = NULL;");
+ $self->pidl("");
+
+ $self->start_flags($enum, $ndr);
+
+ $self->pidl("switch ($varname) {");
+ $self->indent;
+ my $els = \@{$enum->{ELEMENTS}};
+ foreach my $i (0 .. $#{$els}) {
+ my $e = ${$els}[$i];
+ chomp $e;
+ if ($e =~ /^(.*)=/) {
+ $e = $1;
+ }
+ $self->pidl("case $e: val = \"$e\"; break;");
+ }
+
+ $self->deindent;
+ $self->pidl("}");
+
+ $self->pidl("ndr_print_enum($ndr, name, \"$enum->{TYPE}\", val, $varname);");
+
+ $self->end_flags($enum, $ndr);
+}
+
+sub DeclEnum($$$$)
+{
+ my ($e,$t,$name,$varname) = @_;
+ return "enum $name " .
+ ($t eq "pull"?"*":"") . $varname;
+}
+
+$typefamily{ENUM} = {
+ DECL => \&DeclEnum,
+ PUSH_FN_BODY => \&ParseEnumPush,
+ PULL_FN_BODY => \&ParseEnumPull,
+ PRINT_FN_BODY => \&ParseEnumPrint,
+};
+
+#####################################################################
+# generate a push function for a bitmap
+sub ParseBitmapPush($$$$)
+{
+ my($self,$bitmap,$ndr,$varname) = @_;
+ my($type_fn) = $bitmap->{BASE_TYPE};
+
+ $self->start_flags($bitmap, $ndr);
+
+ $self->pidl("NDR_CHECK(ndr_push_$type_fn($ndr, NDR_SCALARS, $varname));");
+
+ $self->end_flags($bitmap, $ndr);
+}
+
+#####################################################################
+# generate a pull function for an bitmap
+sub ParseBitmapPull($$$$)
+{
+ my($self,$bitmap,$ndr,$varname) = @_;
+ my $type_fn = $bitmap->{BASE_TYPE};
+ my($type_decl) = mapTypeName($bitmap->{BASE_TYPE});
+
+ $self->pidl("$type_decl v;");
+ $self->start_flags($bitmap, $ndr);
+ $self->pidl("NDR_CHECK(ndr_pull_$type_fn($ndr, NDR_SCALARS, &v));");
+ $self->pidl("*$varname = v;");
+
+ $self->end_flags($bitmap, $ndr);
+}
+
+#####################################################################
+# generate a print function for an bitmap
+sub ParseBitmapPrintElement($$$$$$)
+{
+ my($self,$e,$bitmap,$ndr,$name,$varname) = @_;
+ my($type_decl) = mapTypeName($bitmap->{BASE_TYPE});
+ my($type_fn) = $bitmap->{BASE_TYPE};
+ my($flag);
+
+ if ($e =~ /^(\w+) .*$/) {
+ $flag = "$1";
+ } else {
+ die "Bitmap: \"$name\" invalid Flag: \"$e\"";
+ }
+
+ $self->pidl("ndr_print_bitmap_flag($ndr, sizeof($type_decl), \"$flag\", $flag, $varname);");
+}
+
+#####################################################################
+# generate a print function for an bitmap
+sub ParseBitmapPrint($$$$$)
+{
+ my($self,$bitmap,$ndr,$name,$varname) = @_;
+ my($type_decl) = mapTypeName($bitmap->{TYPE});
+ my($type_fn) = $bitmap->{BASE_TYPE};
+
+ $self->start_flags($bitmap, $ndr);
+
+ $self->pidl("ndr_print_$type_fn($ndr, name, $varname);");
+
+ $self->pidl("$ndr->depth++;");
+ foreach my $e (@{$bitmap->{ELEMENTS}}) {
+ $self->ParseBitmapPrintElement($e, $bitmap, $ndr, $name, $varname);
+ }
+ $self->pidl("$ndr->depth--;");
+
+ $self->end_flags($bitmap, $ndr);
+}
+
+sub DeclBitmap($$$$)
+{
+ my ($e,$t,$name,$varname) = @_;
+ return mapTypeName(Parse::Pidl::Typelist::bitmap_type_fn($e)) .
+ ($t eq "pull"?" *":" ") . $varname;
+}
+
+$typefamily{BITMAP} = {
+ DECL => \&DeclBitmap,
+ PUSH_FN_BODY => \&ParseBitmapPush,
+ PULL_FN_BODY => \&ParseBitmapPull,
+ PRINT_FN_BODY => \&ParseBitmapPrint,
+};
+
+#####################################################################
+# generate a struct print function
+sub ParseStructPrint($$$$$)
+{
+ my($self,$struct,$ndr,$name,$varname) = @_;
+
+ return unless defined $struct->{ELEMENTS};
+
+ my $env = GenerateStructEnv($struct, $varname);
+
+ $self->DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}});
+
+ $self->pidl("ndr_print_struct($ndr, name, \"$name\");");
+ $self->pidl("if (r == NULL) { ndr_print_null($ndr); return; }");
+
+ $self->start_flags($struct, $ndr);
+
+ $self->pidl("$ndr->depth++;");
+
+ $self->ParseElementPrint($_, $ndr, $env->{$_->{NAME}}, $env)
+ foreach (@{$struct->{ELEMENTS}});
+ $self->pidl("$ndr->depth--;");
+
+ $self->end_flags($struct, $ndr);
+}
+
+sub DeclarePtrVariables($$)
+{
+ my ($self,$e) = @_;
+
+ if (has_property($e, "skip") or has_property($e, "skip_noinit")) {
+ return;
+ }
+
+ foreach my $l (@{$e->{LEVELS}}) {
+ my $size = 32;
+ if ($l->{TYPE} eq "POINTER" and
+ not ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP")) {
+ if ($l->{POINTER_TYPE} eq "relative_short") {
+ $size = 16;
+ }
+ $self->pidl("uint${size}_t _ptr_$e->{NAME};");
+ last;
+ }
+ }
+}
+
+sub DeclareArrayVariables($$;$)
+{
+ my ($self,$e,$pull) = @_;
+
+ if (has_property($e, "skip") or has_property($e, "skip_noinit")) {
+ return;
+ }
+
+ foreach my $l (@{$e->{LEVELS}}) {
+ next if ($l->{TYPE} ne "ARRAY");
+ if (defined($pull)) {
+ $self->pidl("uint32_t size_$e->{NAME}_$l->{LEVEL_INDEX} = 0;");
+ if ($l->{IS_VARYING}) {
+ $self->pidl("uint32_t length_$e->{NAME}_$l->{LEVEL_INDEX} = 0;");
+ }
+ }
+ next if has_fast_array($e,$l);
+ next if is_charset_array($e,$l);
+ $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};");
+ }
+}
+
+sub DeclareArrayVariablesNoZero($$$)
+{
+ my ($self,$e,$env) = @_;
+
+ if (has_property($e, "skip") or has_property($e, "skip_noinit")) {
+ return;
+ }
+
+ foreach my $l (@{$e->{LEVELS}}) {
+ next if ($l->{TYPE} ne "ARRAY");
+ next if has_fast_array($e,$l);
+ next if is_charset_array($e,$l);
+ my $length = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL});
+ if ($length eq "0") {
+ warning($e->{ORIGINAL}, "pointless array cntr: 'cntr_$e->{NAME}_$l->{LEVEL_INDEX}': length=$length");
+ } else {
+ $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};");
+ }
+ }
+}
+
+sub DeclareMemCtxVariables($$)
+{
+ my ($self,$e) = @_;
+
+ if (has_property($e, "skip") or has_property($e, "skip_noinit")) {
+ return;
+ }
+
+ foreach my $l (@{$e->{LEVELS}}) {
+ my $mem_flags = $self->ParseMemCtxPullFlags($e, $l);
+
+ if (($l->{TYPE} eq "POINTER") and ($l->{POINTER_TYPE} eq "ignore")) {
+ last;
+ }
+
+ if (defined($mem_flags)) {
+ $self->pidl("TALLOC_CTX *_mem_save_$e->{NAME}_$l->{LEVEL_INDEX} = NULL;");
+ }
+ }
+}
+
+sub ParseStructPullPrimitives($$$$$)
+{
+ my($self,$struct,$ndr,$varname,$env) = @_;
+
+ if (defined $struct->{SURROUNDING_ELEMENT}) {
+ $self->pidl("NDR_CHECK(ndr_pull_array_size($ndr, &$varname->$struct->{SURROUNDING_ELEMENT}->{NAME}));");
+ }
+
+ $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $struct->{ALIGN}));");
+
+ if (defined($struct->{PROPERTIES}{relative_base})) {
+ # set the current offset as base for relative pointers
+ # and store it based on the toplevel struct/union
+ $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
+ }
+
+ $self->ParseElementPull($_, $ndr, $env, 1, 0) foreach (@{$struct->{ELEMENTS}});
+
+ $self->add_deferred();
+
+ $self->pidl("NDR_CHECK(ndr_pull_trailer_align($ndr, $struct->{ALIGN}));");
+}
+
+sub ParseStructPullDeferred($$$$$)
+{
+ my ($self,$struct,$ndr,$varname,$env) = @_;
+
+ if (defined($struct->{PROPERTIES}{relative_base})) {
+ # retrieve the current offset as base for relative pointers
+ # based on the toplevel struct/union
+ $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset2($ndr, $varname));");
+ }
+ foreach my $e (@{$struct->{ELEMENTS}}) {
+ $self->ParseElementPull($e, $ndr, $env, 0, 1);
+ }
+
+ $self->add_deferred();
+}
+
+#####################################################################
+# parse a struct - pull side
+sub ParseStructPull($$$$)
+{
+ my($self,$struct,$ndr,$varname) = @_;
+
+ return unless defined $struct->{ELEMENTS};
+
+ # declare any internal pointers we need
+ foreach my $e (@{$struct->{ELEMENTS}}) {
+ $self->DeclarePtrVariables($e);
+ $self->DeclareArrayVariables($e, "pull");
+ $self->DeclareMemCtxVariables($e);
+ }
+
+ $self->start_flags($struct, $ndr);
+
+ my $env = GenerateStructEnv($struct, $varname);
+
+ $self->pidl("NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);");
+ $self->pidl("if (ndr_flags & NDR_SCALARS) {");
+ $self->indent;
+ $self->ParseStructPullPrimitives($struct,$ndr,$varname,$env);
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
+ $self->indent;
+ $self->ParseStructPullDeferred($struct,$ndr,$varname,$env);
+ $self->deindent;
+ $self->pidl("}");
+
+ $self->end_flags($struct, $ndr);
+}
+
+#####################################################################
+# calculate size of ndr struct
+sub ParseStructNdrSize($$$$)
+{
+ my ($self,$t, $name, $varname) = @_;
+ my $sizevar;
+
+ if (my $flags = has_property($t, "flag")) {
+ $self->pidl("flags |= $flags;");
+ }
+ $self->pidl("return ndr_size_struct($varname, flags, (ndr_push_flags_fn_t)ndr_push_$name);");
+}
+
+sub DeclStruct($$$$)
+{
+ my ($e,$t,$name,$varname) = @_;
+ return ($t ne "pull"?"const ":"") . "struct $name *$varname";
+}
+
+sub ArgsStructNdrSize($$$)
+{
+ my ($d, $name, $varname) = @_;
+ return "const struct $name *$varname, int flags";
+}
+
+$typefamily{STRUCT} = {
+ PUSH_FN_BODY => \&ParseStructPush,
+ DECL => \&DeclStruct,
+ PULL_FN_BODY => \&ParseStructPull,
+ PRINT_FN_BODY => \&ParseStructPrint,
+ SIZE_FN_BODY => \&ParseStructNdrSize,
+ SIZE_FN_ARGS => \&ArgsStructNdrSize,
+};
+
+#####################################################################
+# calculate size of ndr struct
+sub ParseUnionNdrSize($$$)
+{
+ my ($self, $t, $name, $varname) = @_;
+ my $sizevar;
+
+ if (my $flags = has_property($t, "flag")) {
+ $self->pidl("flags |= $flags;");
+ }
+
+ $self->pidl("return ndr_size_union($varname, flags, level, (ndr_push_flags_fn_t)ndr_push_$name);");
+}
+
+sub ParseUnionPushPrimitives($$$$)
+{
+ my ($self, $e, $ndr ,$varname) = @_;
+
+ my $have_default = 0;
+
+ $self->pidl("uint32_t level = ndr_push_get_switch_value($ndr, $varname);");
+
+ if (defined($e->{SWITCH_TYPE})) {
+ if (defined($e->{ALIGN})) {
+ $self->pidl("NDR_CHECK(ndr_push_union_align($ndr, $e->{ALIGN}));");
+ }
+
+ $self->pidl("NDR_CHECK(ndr_push_$e->{SWITCH_TYPE}($ndr, NDR_SCALARS, level));");
+ }
+
+ if (defined($e->{ALIGN})) {
+ if ($e->{IS_MS_UNION}) {
+ $self->pidl("/* ms_union is always aligned to the largest union arm*/");
+ $self->pidl("NDR_CHECK(ndr_push_align($ndr, $e->{ALIGN}));");
+ } else {
+ $self->pidl("NDR_CHECK(ndr_push_union_align($ndr, $e->{ALIGN}));");
+ }
+ }
+
+ $self->pidl("switch (level) {");
+ $self->indent;
+ foreach my $el (@{$e->{ELEMENTS}}) {
+ if ($el->{CASE} eq "default") {
+ $have_default = 1;
+ }
+ $self->pidl("$el->{CASE}: {");
+
+ if ($el->{TYPE} ne "EMPTY") {
+ $self->indent;
+ if (defined($e->{PROPERTIES}{relative_base})) {
+ $self->pidl("NDR_CHECK(ndr_push_align($ndr, $el->{ALIGN}));");
+ # set the current offset as base for relative pointers
+ # and store it based on the toplevel struct/union
+ $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
+ }
+ $self->DeclareArrayVariables($el);
+ my $el_env = {$el->{NAME} => "$varname->$el->{NAME}"};
+ $self->CheckRefPtrs($el, $ndr, $el_env);
+ $self->ParseElementPush($el, $ndr, $el_env, 1, 0);
+ $self->deindent;
+ }
+ $self->pidl("break; }");
+ $self->pidl("");
+ }
+ if (! $have_default) {
+ $self->pidl("default:");
+ $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
+ }
+ $self->deindent;
+ $self->pidl("}");
+}
+
+sub ParseUnionPushDeferred($$$$)
+{
+ my ($self,$e,$ndr,$varname) = @_;
+
+ my $have_default = 0;
+
+ $self->pidl("uint32_t level = ndr_push_get_switch_value($ndr, $varname);");
+ if (defined($e->{PROPERTIES}{relative_base})) {
+ # retrieve the current offset as base for relative pointers
+ # based on the toplevel struct/union
+ $self->pidl("NDR_CHECK(ndr_push_setup_relative_base_offset2($ndr, $varname));");
+ }
+ $self->pidl("switch (level) {");
+ $self->indent;
+ foreach my $el (@{$e->{ELEMENTS}}) {
+ if ($el->{CASE} eq "default") {
+ $have_default = 1;
+ }
+
+ $self->pidl("$el->{CASE}:");
+ if ($el->{TYPE} ne "EMPTY") {
+ $self->indent;
+ $self->ParseElementPush($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1);
+ $self->deindent;
+ }
+ $self->pidl("break;");
+ $self->pidl("");
+ }
+ if (! $have_default) {
+ $self->pidl("default:");
+ $self->pidl("\treturn ndr_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
+ }
+ $self->deindent;
+ $self->pidl("}");
+}
+
+#####################################################################
+# parse a union - push side
+sub ParseUnionPush($$$$)
+{
+ my ($self,$e,$ndr,$varname) = @_;
+ my $have_default = 0;
+
+ $self->start_flags($e, $ndr);
+
+ $self->pidl("NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);");
+ $self->pidl("if (ndr_flags & NDR_SCALARS) {");
+ $self->indent;
+ $self->ParseUnionPushPrimitives($e, $ndr, $varname);
+ $self->deindent;
+ $self->pidl("}");
+ if (is_deferred_switch_non_empty($e)) {
+ $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
+ $self->indent;
+ $self->ParseUnionPushDeferred($e, $ndr, $varname);
+ $self->deindent;
+ $self->pidl("}");
+ }
+ $self->end_flags($e, $ndr);
+}
+
+#####################################################################
+# print a union
+sub ParseUnionPrint($$$$$)
+{
+ my ($self,$e,$ndr,$name,$varname) = @_;
+ my $have_default = 0;
+
+ $self->pidl("uint32_t level;");
+ foreach my $el (@{$e->{ELEMENTS}}) {
+ $self->DeclareArrayVariables($el);
+ }
+
+ $self->start_flags($e, $ndr);
+
+ $self->pidl("level = ndr_print_get_switch_value($ndr, $varname);");
+
+ $self->pidl("ndr_print_union($ndr, name, level, \"$name\");");
+
+ $self->pidl("switch (level) {");
+ $self->indent;
+ foreach my $el (@{$e->{ELEMENTS}}) {
+ if ($el->{CASE} eq "default") {
+ $have_default = 1;
+ }
+ $self->pidl("$el->{CASE}:");
+ if ($el->{TYPE} ne "EMPTY") {
+ $self->indent;
+ $self->ParseElementPrint($el, $ndr, "$varname->$el->{NAME}", {});
+ $self->deindent;
+ }
+ $self->pidl("break;");
+ $self->pidl("");
+ }
+ if (! $have_default) {
+ $self->pidl("default:");
+ $self->pidl("\tndr_print_bad_level($ndr, name, level);");
+ }
+ $self->deindent;
+ $self->pidl("}");
+
+ $self->end_flags($e, $ndr);
+}
+
+sub ParseUnionPullPrimitives($$$$$)
+{
+ my ($self,$e,$ndr,$varname,$switch_type) = @_;
+ my $have_default = 0;
+
+
+ if (defined($switch_type)) {
+ if (defined($e->{ALIGN})) {
+ $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));");
+ }
+
+ $self->pidl("NDR_CHECK(ndr_pull_$switch_type($ndr, NDR_SCALARS, &_level));");
+ $self->pidl("if (_level != level) {");
+ $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u for $varname at \%s\", _level, __location__);");
+ $self->pidl("}");
+ }
+
+ if (defined($e->{ALIGN})) {
+ if ($e->{IS_MS_UNION}) {
+ $self->pidl("/* ms_union is always aligned to the largest union arm*/");
+ $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $e->{ALIGN}));");
+ } else {
+ $self->pidl("NDR_CHECK(ndr_pull_union_align($ndr, $e->{ALIGN}));");
+ }
+ }
+
+ $self->pidl("switch (level) {");
+ $self->indent;
+ foreach my $el (@{$e->{ELEMENTS}}) {
+ if ($el->{CASE} eq "default") {
+ $have_default = 1;
+ }
+ $self->pidl("$el->{CASE}: {");
+
+ if ($el->{TYPE} ne "EMPTY") {
+ $self->indent;
+ if (defined($e->{PROPERTIES}{relative_base})) {
+ $self->pidl("NDR_CHECK(ndr_pull_align($ndr, $el->{ALIGN}));");
+ # set the current offset as base for relative pointers
+ # and store it based on the toplevel struct/union
+ $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset1($ndr, $varname, $ndr->offset));");
+ }
+ $self->ParseElementPull($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 1, 0);
+ $self->deindent;
+ }
+ $self->pidl("break; }");
+ $self->pidl("");
+ }
+ if (! $have_default) {
+ $self->pidl("default:");
+ $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
+ }
+ $self->deindent;
+ $self->pidl("}");
+}
+
+sub ParseUnionPullDeferred($$$$)
+{
+ my ($self,$e,$ndr,$varname) = @_;
+ my $have_default = 0;
+
+ if (defined($e->{PROPERTIES}{relative_base})) {
+ # retrieve the current offset as base for relative pointers
+ # based on the toplevel struct/union
+ $self->pidl("NDR_CHECK(ndr_pull_setup_relative_base_offset2($ndr, $varname));");
+ }
+ $self->pidl("switch (level) {");
+ $self->indent;
+ foreach my $el (@{$e->{ELEMENTS}}) {
+ if ($el->{CASE} eq "default") {
+ $have_default = 1;
+ }
+
+ $self->pidl("$el->{CASE}:");
+ if ($el->{TYPE} ne "EMPTY") {
+ $self->indent;
+ $self->ParseElementPull($el, $ndr, {$el->{NAME} => "$varname->$el->{NAME}"}, 0, 1);
+ $self->deindent;
+ }
+ $self->pidl("break;");
+ $self->pidl("");
+ }
+ if (! $have_default) {
+ $self->pidl("default:");
+ $self->pidl("\treturn ndr_pull_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u at \%s\", level, __location__);");
+ }
+ $self->deindent;
+ $self->pidl("}");
+
+
+}
+
+#####################################################################
+# parse a union - pull side
+sub ParseUnionPull($$$$)
+{
+ my ($self,$e,$ndr,$varname) = @_;
+ my $switch_type = $e->{SWITCH_TYPE};
+ my $needs_deferred_switch = is_deferred_switch_non_empty($e);
+ $self->pidl("uint32_t level;");
+ if (defined($switch_type)) {
+ if (Parse::Pidl::Typelist::typeIs($switch_type, "ENUM")) {
+ $switch_type = Parse::Pidl::Typelist::enum_type_fn(getType($switch_type)->{DATA});
+ }
+ $self->pidl(mapTypeName($switch_type) . " _level;");
+ }
+
+ my %double_cases = ();
+ foreach my $el (@{$e->{ELEMENTS}}) {
+ next if ($el->{TYPE} eq "EMPTY");
+ next if ($double_cases{"$el->{NAME}"});
+ $self->DeclareMemCtxVariables($el);
+ $self->DeclarePtrVariables($el);
+ $self->DeclareArrayVariables($el, "pull");
+ $double_cases{"$el->{NAME}"} = 1;
+ }
+
+ $self->start_flags($e, $ndr);
+
+ $self->pidl("NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);");
+ $self->pidl("if (ndr_flags & NDR_SCALARS) {");
+ $self->indent;
+ if (! $needs_deferred_switch) {
+ $self->pidl("/* This token is not used again */");
+ $self->pidl("level = ndr_pull_steal_switch_value($ndr, $varname);");
+ } else {
+ $self->pidl("level = ndr_pull_get_switch_value($ndr, $varname);");
+ }
+ $self->ParseUnionPullPrimitives($e,$ndr,$varname,$switch_type);
+ $self->deindent;
+ $self->pidl("}");
+ if ($needs_deferred_switch) {
+ $self->pidl("if (ndr_flags & NDR_BUFFERS) {");
+ $self->indent;
+ $self->pidl("/* The token is not needed after this. */");
+ $self->pidl("level = ndr_pull_steal_switch_value($ndr, $varname);");
+ $self->ParseUnionPullDeferred($e,$ndr,$varname);
+ $self->deindent;
+ $self->pidl("}");
+ }
+ $self->add_deferred();
+
+ $self->end_flags($e, $ndr);
+}
+
+sub DeclUnion($$$$)
+{
+ my ($e,$t,$name,$varname) = @_;
+ return ($t ne "pull"?"const ":"") . "union $name *$varname";
+}
+
+sub ArgsUnionNdrSize($$)
+{
+ my ($d,$name) = @_;
+ return "const union $name *r, uint32_t level, int flags";
+}
+
+$typefamily{UNION} = {
+ PUSH_FN_BODY => \&ParseUnionPush,
+ DECL => \&DeclUnion,
+ PULL_FN_BODY => \&ParseUnionPull,
+ PRINT_FN_BODY => \&ParseUnionPrint,
+ SIZE_FN_ARGS => \&ArgsUnionNdrSize,
+ SIZE_FN_BODY => \&ParseUnionNdrSize,
+};
+
+#####################################################################
+# parse a typedef - push side
+sub ParseTypedefPush($$$$)
+{
+ my($self,$e,$ndr,$varname) = @_;
+
+ my $env;
+
+ $env->{$e->{NAME}} = $varname;
+
+ $self->ParseElementPushLevel($e, $e->{LEVELS}[0], $ndr, $varname, $env, 1, 1);
+}
+
+#####################################################################
+# parse a typedef - pull side
+sub ParseTypedefPull($$$$)
+{
+ my($self,$e,$ndr,$varname) = @_;
+
+ my $env;
+
+ $env->{$e->{NAME}} = $varname;
+
+ $self->ParseElementPullLevel($e, $e->{LEVELS}[0], $ndr, $varname, $env, 1, 1);
+}
+
+#####################################################################
+# parse a typedef - print side
+sub ParseTypedefPrint($$$$$)
+{
+ my($self,$e,$ndr,$name,$varname) = @_;
+
+ $typefamily{$e->{DATA}->{TYPE}}->{PRINT_FN_BODY}->($self, $e->{DATA}, $ndr, $name, $varname);
+}
+
+#####################################################################
+## calculate the size of a structure
+sub ParseTypedefNdrSize($$$$)
+{
+ my($self,$t,$name,$varname) = @_;
+
+ $typefamily{$t->{DATA}->{TYPE}}->{SIZE_FN_BODY}->($self, $t->{DATA}, $name, $varname);
+}
+
+sub DeclTypedef($$$$)
+{
+ my ($e, $t, $name, $varname) = @_;
+
+ return $typefamily{$e->{DATA}->{TYPE}}->{DECL}->($e->{DATA}, $t, $name, $varname);
+}
+
+sub ArgsTypedefNdrSize($$$)
+{
+ my ($d, $name, $varname) = @_;
+ return $typefamily{$d->{DATA}->{TYPE}}->{SIZE_FN_ARGS}->($d->{DATA}, $name, $varname);
+}
+
+$typefamily{TYPEDEF} = {
+ PUSH_FN_BODY => \&ParseTypedefPush,
+ DECL => \&DeclTypedef,
+ PULL_FN_BODY => \&ParseTypedefPull,
+ PRINT_FN_BODY => \&ParseTypedefPrint,
+ SIZE_FN_ARGS => \&ArgsTypedefNdrSize,
+ SIZE_FN_BODY => \&ParseTypedefNdrSize,
+};
+
+sub ParsePipePushChunk($$)
+{
+ my ($self, $t) = @_;
+
+ my $pipe = $t;
+ $pipe = $t->{DATA} if ($t->{TYPE} eq "TYPEDEF");
+ my $struct = $pipe->{DATA};
+
+ my $name = "$struct->{NAME}";
+ my $ndr = "ndr";
+ my $varname = "r";
+
+ my $args = $typefamily{$struct->{TYPE}}->{DECL}->($struct, "push", $name, $varname);
+
+ $self->fn_declare("push", $struct, "enum ndr_err_code ndr_push_$name(struct ndr_push *$ndr, int ndr_flags, $args)") or return;
+
+ return if has_property($t, "nopush");
+
+ $self->pidl("{");
+ $self->indent;
+
+ $self->ParseStructPush($struct, $ndr, $varname);
+ $self->pidl("");
+
+ $self->pidl("NDR_CHECK(ndr_push_pipe_chunk_trailer(ndr, ndr_flags, $varname->count));");
+ $self->pidl("");
+
+ $self->pidl("return NDR_ERR_SUCCESS;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub ParsePipePullChunk($$)
+{
+ my ($self, $t) = @_;
+
+ my $pipe = $t;
+ $pipe = $t->{DATA} if ($t->{TYPE} eq "TYPEDEF");
+ my $struct = $pipe->{DATA};
+
+ my $name = "$struct->{NAME}";
+ my $ndr = "ndr";
+ my $varname = "r";
+
+ my $args = $typefamily{$struct->{TYPE}}->{DECL}->($struct, "pull", $name, $varname);
+
+ $self->fn_declare("pull", $struct, "enum ndr_err_code ndr_pull_$name(struct ndr_pull *$ndr, int ndr_flags, $args)") or return;
+
+ return if has_property($struct, "nopull");
+
+ $self->pidl("{");
+ $self->indent;
+
+ $self->ParseStructPull($struct, $ndr, $varname);
+ $self->pidl("");
+
+ $self->pidl("NDR_CHECK(ndr_check_pipe_chunk_trailer($ndr, ndr_flags, $varname->count));");
+ $self->pidl("");
+
+ $self->pidl("return NDR_ERR_SUCCESS;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub ParsePipePrintChunk($$)
+{
+ my ($self, $t) = @_;
+
+ my $pipe = $t;
+ $pipe = $t->{DATA} if ($t->{TYPE} eq "TYPEDEF");
+ my $struct = $pipe->{DATA};
+
+ my $name = "$struct->{NAME}";
+ my $ndr = "ndr";
+ my $varname = "r";
+
+ my $args = $typefamily{$struct->{TYPE}}->{DECL}->($struct, "print", $name, $varname);
+
+ $self->pidl_hdr("void ndr_print_$name(struct ndr_print *ndr, const char *name, $args);");
+
+ return if (has_property($t, "noprint"));
+
+ $self->pidl("_PUBLIC_ void ndr_print_$name(struct ndr_print *$ndr, const char *name, $args)");
+ $self->pidl("{");
+ $self->indent;
+ $self->ParseTypePrint($struct, $ndr, $varname);
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+#####################################################################
+# parse a function - print side
+sub ParseFunctionPrint($$)
+{
+ my($self, $fn) = @_;
+ my $ndr = "ndr";
+
+ $self->pidl_hdr("void ndr_print_$fn->{NAME}(struct ndr_print *$ndr, const char *name, int flags, const struct $fn->{NAME} *r);");
+
+ return if has_property($fn, "noprint");
+
+ $self->pidl("_PUBLIC_ void ndr_print_$fn->{NAME}(struct ndr_print *$ndr, const char *name, int flags, const struct $fn->{NAME} *r)");
+ $self->pidl("{");
+ $self->indent;
+
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ $self->DeclareArrayVariables($e);
+ }
+
+ $self->pidl("ndr_print_struct($ndr, name, \"$fn->{NAME}\");");
+ $self->pidl("if (r == NULL) { ndr_print_null($ndr); return; }");
+ $self->pidl("$ndr->depth++;");
+
+ $self->pidl("if (flags & NDR_SET_VALUES) {");
+ $self->pidl("\t$ndr->flags |= LIBNDR_PRINT_SET_VALUES;");
+ $self->pidl("}");
+
+ $self->pidl("if (flags & NDR_IN) {");
+ $self->indent;
+ $self->pidl("ndr_print_struct($ndr, \"in\", \"$fn->{NAME}\");");
+ $self->pidl("$ndr->depth++;");
+
+ my $env = GenerateFunctionInEnv($fn);
+
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ if (grep(/in/,@{$e->{DIRECTION}})) {
+ $self->ParseElementPrint($e, $ndr, $env->{$e->{NAME}}, $env);
+ }
+ }
+ $self->pidl("$ndr->depth--;");
+ $self->deindent;
+ $self->pidl("}");
+
+ $self->pidl("if (flags & NDR_OUT) {");
+ $self->indent;
+ $self->pidl("ndr_print_struct($ndr, \"out\", \"$fn->{NAME}\");");
+ $self->pidl("$ndr->depth++;");
+
+ $env = GenerateFunctionOutEnv($fn);
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ if (grep(/out/,@{$e->{DIRECTION}})) {
+ $self->ParseElementPrint($e, $ndr, $env->{$e->{NAME}}, $env);
+ }
+ }
+ if ($fn->{RETURN_TYPE}) {
+ $self->pidl("ndr_print_$fn->{RETURN_TYPE}($ndr, \"result\", r->out.result);");
+ }
+ $self->pidl("$ndr->depth--;");
+ $self->deindent;
+ $self->pidl("}");
+
+ $self->pidl("$ndr->depth--;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+#####################################################################
+# parse a function
+sub ParseFunctionPush($$)
+{
+ my($self, $fn) = @_;
+ my $ndr = "ndr";
+
+ $self->fn_declare("push", $fn, "enum ndr_err_code ndr_push_$fn->{NAME}(struct ndr_push *$ndr, int flags, const struct $fn->{NAME} *r)") or return;
+
+ return if has_property($fn, "nopush");
+
+ $self->pidl("{");
+ $self->indent;
+
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ $self->DeclareArrayVariables($e);
+ }
+
+ $self->pidl("NDR_PUSH_CHECK_FN_FLAGS(ndr, flags);");
+
+ $self->pidl("if (flags & NDR_IN) {");
+ $self->indent;
+
+ my $env = GenerateFunctionInEnv($fn);
+
+ EnvSubstituteValue($env, $fn);
+
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ if (grep(/in/,@{$e->{DIRECTION}})) {
+ $self->CheckRefPtrs($e, $ndr, $env);
+ }
+ }
+
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ if (grep(/in/,@{$e->{DIRECTION}})) {
+ $self->ParseElementPush($e, $ndr, $env, 1, 1);
+ }
+ }
+
+ $self->deindent;
+ $self->pidl("}");
+
+ $self->pidl("if (flags & NDR_OUT) {");
+ $self->indent;
+
+ $env = GenerateFunctionOutEnv($fn);
+ EnvSubstituteValue($env, $fn);
+
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ if (grep(/out/,@{$e->{DIRECTION}})) {
+ $self->CheckRefPtrs($e, $ndr, $env);
+ }
+ }
+
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ if (grep(/out/,@{$e->{DIRECTION}})) {
+ $self->ParseElementPush($e, $ndr, $env, 1, 1);
+ }
+ }
+
+ if ($fn->{RETURN_TYPE}) {
+ $self->pidl("NDR_CHECK(ndr_push_$fn->{RETURN_TYPE}($ndr, NDR_SCALARS, r->out.result));");
+ }
+
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("return NDR_ERR_SUCCESS;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub AllocateArrayLevel($$$$$$)
+{
+ my ($self,$e,$l,$ndr,$var,$size) = @_;
+
+ my $pl = GetPrevLevel($e, $l);
+ if (defined($pl) and
+ $pl->{TYPE} eq "POINTER" and
+ $pl->{POINTER_TYPE} eq "ref"
+ and not $l->{IS_ZERO_TERMINATED}) {
+ $self->pidl("if ($ndr->flags & LIBNDR_FLAG_REF_ALLOC) {");
+ $self->pidl("\tNDR_PULL_ALLOC_N($ndr, $var, $size);");
+ $self->pidl("}");
+ if (grep(/in/,@{$e->{DIRECTION}}) and
+ grep(/out/,@{$e->{DIRECTION}})) {
+ $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));");
+ }
+ return;
+ }
+
+ $self->pidl("NDR_PULL_ALLOC_N($ndr, $var, $size);");
+}
+
+#####################################################################
+# parse a function
+sub ParseFunctionPull($$)
+{
+ my($self,$fn) = @_;
+ my $ndr = "ndr";
+
+ # pull function args
+ $self->fn_declare("pull", $fn, "enum ndr_err_code ndr_pull_$fn->{NAME}(struct ndr_pull *$ndr, int flags, struct $fn->{NAME} *r)") or return;
+
+ $self->pidl("{");
+ $self->indent;
+
+ # declare any internal pointers we need
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ $self->DeclarePtrVariables($e);
+ $self->DeclareArrayVariables($e, "pull");
+ }
+
+ my %double_cases = ();
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next if ($e->{TYPE} eq "EMPTY");
+ next if ($double_cases{"$e->{NAME}"});
+ $self->DeclareMemCtxVariables($e);
+ $double_cases{"$e->{NAME}"} = 1;
+ }
+
+ $self->pidl("NDR_PULL_CHECK_FN_FLAGS(ndr, flags);");
+
+ $self->pidl("if (flags & NDR_IN) {");
+ $self->indent;
+
+ # auto-init the out section of a structure. I originally argued that
+ # this was a bad idea as it hides bugs, but coping correctly
+ # with initialisation and not wiping ref vars is turning
+ # out to be too tricky (tridge)
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next unless grep(/out/, @{$e->{DIRECTION}});
+ $self->pidl("ZERO_STRUCT(r->out);");
+ $self->pidl("");
+ last;
+ }
+
+ my $env = GenerateFunctionInEnv($fn);
+
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next unless (grep(/in/, @{$e->{DIRECTION}}));
+ $self->ParseElementPull($e, $ndr, $env, 1, 1);
+ }
+
+ # allocate the "simple" out ref variables. FIXME: Shouldn't this have it's
+ # own flag rather than be in NDR_IN ?
+
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next unless (grep(/out/, @{$e->{DIRECTION}}));
+ next unless ($e->{LEVELS}[0]->{TYPE} eq "POINTER" and
+ $e->{LEVELS}[0]->{POINTER_TYPE} eq "ref");
+ next if (($e->{LEVELS}[1]->{TYPE} eq "DATA") and
+ ($e->{LEVELS}[1]->{DATA_TYPE} eq "string"));
+ next if ($e->{LEVELS}[1]->{TYPE} eq "PIPE");
+ next if (($e->{LEVELS}[1]->{TYPE} eq "ARRAY")
+ and $e->{LEVELS}[1]->{IS_ZERO_TERMINATED});
+
+ if ($e->{LEVELS}[1]->{TYPE} eq "ARRAY") {
+ my $size = ParseExprExt($e->{LEVELS}[1]->{SIZE_IS}, $env, $e->{ORIGINAL},
+ check_null_pointer($e, $env, sub { $self->pidl(shift); },
+ "return ndr_pull_error($ndr, NDR_ERR_INVALID_POINTER, \"NULL Pointer for size_is()\");"),
+ check_fully_dereferenced($e, $env));
+ $self->pidl("NDR_PULL_ALLOC_N($ndr, r->out.$e->{NAME}, $size);");
+
+ if (grep(/in/, @{$e->{DIRECTION}})) {
+ $self->pidl("memcpy(r->out.$e->{NAME}, r->in.$e->{NAME}, ($size) * sizeof(*r->in.$e->{NAME}));");
+ } else {
+ $self->pidl("memset(r->out.$e->{NAME}, 0, ($size) * sizeof(*r->out.$e->{NAME}));");
+ }
+ } elsif ($e->{LEVELS}[1]->{TYPE} eq "ARRAY") {
+ if (grep(/in/, @{$e->{DIRECTION}})) {
+ $self->pidl("r->out.$e->{NAME} = r->in.$e->{NAME};");
+ } else {
+ $self->pidl("r->out.$e->{NAME} = NULL;");
+ }
+ } else {
+ $self->pidl("NDR_PULL_ALLOC($ndr, r->out.$e->{NAME});");
+
+ if (grep(/in/, @{$e->{DIRECTION}})) {
+ $self->pidl("*r->out.$e->{NAME} = *r->in.$e->{NAME};");
+ } else {
+ $self->pidl("ZERO_STRUCTP(r->out.$e->{NAME});");
+ }
+ }
+ }
+
+ $self->add_deferred();
+ $self->deindent;
+ $self->pidl("}");
+
+ $self->pidl("if (flags & NDR_OUT) {");
+ $self->indent;
+
+ $env = GenerateFunctionOutEnv($fn);
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next unless grep(/out/, @{$e->{DIRECTION}});
+ $self->ParseElementPull($e, $ndr, $env, 1, 1);
+ }
+
+ if ($fn->{RETURN_TYPE}) {
+ $self->pidl("NDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}($ndr, NDR_SCALARS, &r->out.result));");
+ }
+
+ $self->add_deferred();
+ $self->deindent;
+ $self->pidl("}");
+
+ $self->pidl("return NDR_ERR_SUCCESS;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub AuthServiceStruct($$$)
+{
+ my ($self, $ifacename, $authservice) = @_;
+ my @a = split /,/, $authservice;
+ my $authservice_count = $#a + 1;
+
+ $self->pidl("static const char * const $ifacename\_authservice_strings[] = {");
+ foreach my $ap (@a) {
+ $self->pidl("\t$ap, ");
+ }
+ $self->pidl("};");
+ $self->pidl("");
+
+ $self->pidl("static const struct ndr_interface_string_array $ifacename\_authservices = {");
+ $self->pidl("\t.count\t= $authservice_count,");
+ $self->pidl("\t.names\t= $ifacename\_authservice_strings");
+ $self->pidl("};");
+ $self->pidl("");
+}
+
+sub ParseGeneratePipeArray($$$)
+{
+ my ($self, $fn, $direction) = @_;
+
+ $self->pidl("static const struct ndr_interface_call_pipe $fn->{NAME}\_$direction\_pipes[] = {");
+ $self->indent;
+
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ next unless ContainsPipe($e, $e->{LEVELS}[0]);
+ next unless (grep(/$direction/, @{$e->{DIRECTION}}));
+
+ my $cname = "$e->{TYPE}_chunk";
+
+ $self->pidl("{");
+ $self->indent;
+ $self->pidl("\"$direction.$e->{NAME}\",");
+ $self->pidl("\"$cname\",");
+ $self->pidl("sizeof(struct $cname),");
+ $self->pidl("(ndr_push_flags_fn_t) ndr_push_$cname,");
+ $self->pidl("(ndr_pull_flags_fn_t) ndr_pull_$cname,");
+ $self->pidl("(ndr_print_fn_t) ndr_print_$cname,");
+ $self->deindent;
+ $self->pidl("},");
+ }
+ $self->pidl("{ NULL, NULL, 0, NULL, NULL, NULL }");
+ $self->deindent;
+ $self->pidl("};");
+ $self->pidl("");
+}
+
+sub FunctionCallPipes($$)
+{
+ my ($self, $d) = @_;
+ return if not defined($d->{OPNUM});
+
+ my $in_pipes = 0;
+ my $out_pipes = 0;
+
+ foreach my $e (@{$d->{ELEMENTS}}) {
+ next unless ContainsPipe($e, $e->{LEVELS}[0]);
+
+ if (grep(/in/, @{$e->{DIRECTION}})) {
+ $in_pipes++;
+ }
+ if (grep(/out/, @{$e->{DIRECTION}})) {
+ $out_pipes++;
+ }
+ }
+
+ if ($in_pipes) {
+ $self->ParseGeneratePipeArray($d, "in");
+ }
+
+ if ($out_pipes) {
+ $self->ParseGeneratePipeArray($d, "out");
+ }
+}
+
+sub FunctionCallEntry($$)
+{
+ my ($self, $d) = @_;
+ return 0 if not defined($d->{OPNUM});
+
+ my $in_pipes = 0;
+ my $out_pipes = 0;
+
+ foreach my $e (@{$d->{ELEMENTS}}) {
+ next unless ContainsPipe($e, $e->{LEVELS}[0]);
+
+ if (grep(/in/, @{$e->{DIRECTION}})) {
+ $in_pipes++;
+ }
+ if (grep(/out/, @{$e->{DIRECTION}})) {
+ $out_pipes++;
+ }
+ }
+
+ my $in_pipes_ptr = "NULL";
+ my $out_pipes_ptr = "NULL";
+
+ if ($in_pipes) {
+ $in_pipes_ptr = "$d->{NAME}_in_pipes";
+ }
+
+ if ($out_pipes) {
+ $out_pipes_ptr = "$d->{NAME}_out_pipes";
+ }
+
+ $self->pidl("\t{");
+ $self->pidl("\t\t\"$d->{NAME}\",");
+ $self->pidl("\t\tsizeof(struct $d->{NAME}),");
+ $self->pidl("\t\t(ndr_push_flags_fn_t) ndr_push_$d->{NAME},");
+ $self->pidl("\t\t(ndr_pull_flags_fn_t) ndr_pull_$d->{NAME},");
+ $self->pidl("\t\t(ndr_print_function_t) ndr_print_$d->{NAME},");
+ $self->pidl("\t\t{ $in_pipes, $in_pipes_ptr },");
+ $self->pidl("\t\t{ $out_pipes, $out_pipes_ptr },");
+ $self->pidl("\t},");
+ return 1;
+}
+
+#####################################################################
+# produce a function call table
+sub FunctionTable($$)
+{
+ my($self,$interface) = @_;
+ my $count = 0;
+ my $uname = uc $interface->{NAME};
+
+ return if ($#{$interface->{FUNCTIONS}}+1 == 0);
+ return unless defined ($interface->{PROPERTIES}->{uuid});
+
+ foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) {
+ $self->FunctionCallPipes($d);
+ }
+
+ $self->pidl("static const struct ndr_interface_call $interface->{NAME}\_calls[] = {");
+
+ foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) {
+ $count += $self->FunctionCallEntry($d);
+ }
+ $self->pidl("\t{ NULL, 0, NULL, NULL, NULL }");
+ $self->pidl("};");
+ $self->pidl("");
+
+ $self->pidl("static const char * const $interface->{NAME}\_endpoint_strings[] = {");
+ foreach my $ep (@{$interface->{ENDPOINTS}}) {
+ $self->pidl("\t$ep, ");
+ }
+ my $endpoint_count = $#{$interface->{ENDPOINTS}}+1;
+
+ $self->pidl("};");
+ $self->pidl("");
+
+ $self->pidl("static const struct ndr_interface_string_array $interface->{NAME}\_endpoints = {");
+ $self->pidl("\t.count\t= $endpoint_count,");
+ $self->pidl("\t.names\t= $interface->{NAME}\_endpoint_strings");
+ $self->pidl("};");
+ $self->pidl("");
+
+ if (! defined $interface->{PROPERTIES}->{authservice}) {
+ $interface->{PROPERTIES}->{authservice} = "\"host\"";
+ }
+
+ $self->AuthServiceStruct($interface->{NAME},
+ $interface->{PROPERTIES}->{authservice});
+
+ $self->pidl("\nconst struct ndr_interface_table ndr_table_$interface->{NAME} = {");
+ $self->pidl("\t.name\t\t= \"$interface->{NAME}\",");
+ $self->pidl("\t.syntax_id\t= {");
+ $self->pidl("\t\t" . print_uuid($interface->{UUID}) .",");
+ $self->pidl("\t\tNDR_$uname\_VERSION");
+ $self->pidl("\t},");
+ $self->pidl("\t.helpstring\t= NDR_$uname\_HELPSTRING,");
+ $self->pidl("\t.num_calls\t= $count,");
+ $self->pidl("\t.calls\t\t= $interface->{NAME}\_calls,");
+ $self->pidl("\t.endpoints\t= &$interface->{NAME}\_endpoints,");
+ $self->pidl("\t.authservices\t= &$interface->{NAME}\_authservices");
+ $self->pidl("};");
+ $self->pidl("");
+
+}
+
+#####################################################################
+# generate include statements for imported idl files
+sub HeaderImport
+{
+ my $self = shift;
+ my @imports = @_;
+ foreach (@imports) {
+ $_ = unmake_str($_);
+ s/\.idl$//;
+ $self->pidl(choose_header("librpc/gen_ndr/ndr_$_\.h", "gen_ndr/ndr_$_.h"));
+ }
+}
+
+#####################################################################
+# generate include statements for included header files
+sub HeaderInclude
+{
+ my $self = shift;
+ my @includes = @_;
+ foreach (@includes) {
+ $self->pidl_hdr("#include $_");
+ }
+}
+
+#####################################################################
+# generate prototypes and defines for the interface definitions
+# FIXME: these prototypes are for the DCE/RPC client functions, not the
+# NDR parser and so do not belong here, technically speaking
+sub HeaderInterface($$$)
+{
+ my($self,$interface,$needed) = @_;
+
+ my $count = 0;
+
+ if ($needed->{"compression"}) {
+ $self->pidl(choose_header("librpc/ndr/ndr_compression.h", "ndr/compression.h"));
+ }
+
+ if (has_property($interface, "object")) {
+ $self->pidl(choose_header("librpc/gen_ndr/ndr_orpc.h", "ndr/orpc.h"));
+ }
+
+ if (defined $interface->{PROPERTIES}->{helper}) {
+ $self->HeaderInclude(split /,/, $interface->{PROPERTIES}->{helper});
+ }
+
+ if (defined $interface->{PROPERTIES}->{uuid}) {
+ my $name = uc $interface->{NAME};
+ $self->pidl_hdr("#define NDR_$name\_UUID " .
+ Parse::Pidl::Util::make_str(lc($interface->{UUID})));
+
+ $self->pidl_hdr("#define NDR_$name\_VERSION $interface->{VERSION}");
+
+ $self->pidl_hdr("#define NDR_$name\_NAME \"$interface->{NAME}\"");
+
+ if(!defined $interface->{PROPERTIES}->{helpstring}) { $interface->{PROPERTIES}->{helpstring} = "NULL"; }
+ $self->pidl_hdr("#define NDR_$name\_HELPSTRING $interface->{PROPERTIES}->{helpstring}");
+
+ $self->pidl_hdr("extern const struct ndr_interface_table ndr_table_$interface->{NAME};");
+ }
+
+ foreach (@{$interface->{FUNCTIONS}}) {
+ next if has_property($_, "noopnum");
+ next if grep(/^$_->{NAME}$/,@{$interface->{INHERITED_FUNCTIONS}});
+ my $u_name = uc $_->{NAME};
+
+ my $val = sprintf("0x%02x", $count);
+ if (defined($interface->{BASE})) {
+ $val .= " + NDR_" . uc $interface->{BASE} . "_CALL_COUNT";
+ }
+
+ $self->pidl_hdr("#define NDR_$u_name ($val)");
+
+ $self->pidl_hdr("");
+ $count++;
+ }
+
+ my $val = $count;
+
+ if (defined($interface->{BASE})) {
+ $val .= " + NDR_" . uc $interface->{BASE} . "_CALL_COUNT";
+ }
+
+ $self->pidl_hdr("#define NDR_" . uc $interface->{NAME} . "_CALL_COUNT ($val)");
+
+}
+
+sub ParseTypePush($$$$$$)
+{
+ my ($self,$e, $ndr, $varname, $primitives, $deferred) = @_;
+
+ # save the old relative_base_offset
+ $self->pidl("uint32_t _save_relative_base_offset = ndr_push_get_relative_base_offset($ndr);") if defined(has_property($e, "relative_base"));
+ $typefamily{$e->{TYPE}}->{PUSH_FN_BODY}->($self, $e, $ndr, $varname);
+ # restore the old relative_base_offset
+ $self->pidl("ndr_push_restore_relative_base_offset($ndr, _save_relative_base_offset);") if defined(has_property($e, "relative_base"));
+}
+
+sub ParseTypePushFunction($$$)
+{
+ my ($self, $e, $varname) = @_;
+ my $ndr = "ndr";
+
+ my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "push", $e->{NAME}, $varname);
+
+ $self->fn_declare("push", $e, "enum ndr_err_code ".TypeFunctionName("ndr_push", $e)."(struct ndr_push *$ndr, int ndr_flags, $args)") or return;
+
+ $self->pidl("{");
+ $self->indent;
+ $self->ParseTypePush($e, $ndr, $varname, 1, 1);
+ $self->pidl("return NDR_ERR_SUCCESS;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");;
+}
+
+sub ParseTypePull($$$$$$)
+{
+ my ($self, $e, $ndr, $varname, $primitives, $deferred) = @_;
+
+ # save the old relative_base_offset
+ $self->pidl("uint32_t _save_relative_base_offset = ndr_pull_get_relative_base_offset($ndr);") if defined(has_property($e, "relative_base"));
+ $typefamily{$e->{TYPE}}->{PULL_FN_BODY}->($self, $e, $ndr, $varname);
+ # restore the old relative_base_offset
+ $self->pidl("ndr_pull_restore_relative_base_offset($ndr, _save_relative_base_offset);") if defined(has_property($e, "relative_base"));
+}
+
+sub ParseTypePullFunction($$)
+{
+ my ($self, $e, $varname) = @_;
+ my $ndr = "ndr";
+
+ my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "pull", $e->{NAME}, $varname);
+
+ $self->fn_declare("pull", $e, "enum ndr_err_code ".TypeFunctionName("ndr_pull", $e)."(struct ndr_pull *$ndr, int ndr_flags, $args)") or return;
+
+ $self->pidl("{");
+ $self->indent;
+ $self->ParseTypePull($e, $ndr, $varname, 1, 1);
+ $self->pidl("return NDR_ERR_SUCCESS;");
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub ParseTypePrint($$$$)
+{
+ my ($self, $e, $ndr, $varname) = @_;
+
+ $typefamily{$e->{TYPE}}->{PRINT_FN_BODY}->($self, $e, $ndr, $e->{NAME}, $varname);
+}
+
+sub ParseTypePrintFunction($$$)
+{
+ my ($self, $e, $varname) = @_;
+ my $ndr = "ndr";
+
+ my $args = $typefamily{$e->{TYPE}}->{DECL}->($e, "print", $e->{NAME}, $varname);
+
+ $self->pidl_hdr("void ".TypeFunctionName("ndr_print", $e)."(struct ndr_print *ndr, const char *name, $args);");
+
+ return if (has_property($e, "noprint"));
+
+ $self->pidl("_PUBLIC_ void ".TypeFunctionName("ndr_print", $e)."(struct ndr_print *$ndr, const char *name, $args)");
+ $self->pidl("{");
+ $self->indent;
+ $self->ParseTypePrint($e, $ndr, $varname);
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+sub ParseTypeNdrSize($$)
+{
+ my ($self,$t) = @_;
+
+ my $varname = "r";
+ my $tf = $typefamily{$t->{TYPE}};
+ my $args = $tf->{SIZE_FN_ARGS}->($t, $t->{NAME}, $varname);
+
+ $self->fn_declare("size", $t, "size_t ndr_size_$t->{NAME}($args)") or return;
+
+ $self->pidl("{");
+ $self->indent;
+ $typefamily{$t->{TYPE}}->{SIZE_FN_BODY}->($self,$t, $t->{NAME}, $varname);
+ $self->deindent;
+ $self->pidl("}");
+ $self->pidl("");
+}
+
+#####################################################################
+# parse the interface definitions
+sub ParseInterface($$$)
+{
+ my($self,$interface,$needed) = @_;
+
+ $self->pidl_hdr("#ifndef _HEADER_NDR_$interface->{NAME}");
+ $self->pidl_hdr("#define _HEADER_NDR_$interface->{NAME}");
+
+ $self->pidl_hdr("");
+
+ $self->HeaderInterface($interface, $needed);
+
+ # Typedefs
+ foreach my $d (@{$interface->{TYPES}}) {
+ if (Parse::Pidl::Typelist::typeIs($d, "PIPE")) {
+ ($needed->{TypeFunctionName("ndr_push", $d)}) &&
+ $self->ParsePipePushChunk($d);
+ ($needed->{TypeFunctionName("ndr_pull", $d)}) &&
+ $self->ParsePipePullChunk($d);
+ ($needed->{TypeFunctionName("ndr_print", $d)}) &&
+ $self->ParsePipePrintChunk($d);
+
+ $needed->{TypeFunctionName("ndr_pull", $d)} = 0;
+ $needed->{TypeFunctionName("ndr_push", $d)} = 0;
+ $needed->{TypeFunctionName("ndr_print", $d)} = 0;
+ next;
+ }
+
+ next unless(typeHasBody($d));
+
+ ($needed->{TypeFunctionName("ndr_push", $d)}) && $self->ParseTypePushFunction($d, "r");
+ ($needed->{TypeFunctionName("ndr_pull", $d)}) && $self->ParseTypePullFunction($d, "r");
+ ($needed->{TypeFunctionName("ndr_print", $d)}) && $self->ParseTypePrintFunction($d, "r");
+
+ # Make sure we don't generate a function twice...
+ $needed->{TypeFunctionName("ndr_push", $d)} =
+ $needed->{TypeFunctionName("ndr_pull", $d)} =
+ $needed->{TypeFunctionName("ndr_print", $d)} = 0;
+
+ ($needed->{"ndr_size_$d->{NAME}"}) && $self->ParseTypeNdrSize($d);
+ }
+
+ # Functions
+ foreach my $d (@{$interface->{FUNCTIONS}}) {
+ ($needed->{"ndr_push_$d->{NAME}"}) && $self->ParseFunctionPush($d);
+ ($needed->{"ndr_pull_$d->{NAME}"}) && $self->ParseFunctionPull($d);
+ ($needed->{"ndr_print_$d->{NAME}"}) && $self->ParseFunctionPrint($d);
+ }
+
+ $self->FunctionTable($interface);
+
+ $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */");
+}
+
+sub GenerateIncludes($)
+{
+ my ($self) = @_;
+ if (is_intree()) {
+ $self->pidl("#include \"includes.h\"");
+ } else {
+ $self->pidl("#ifndef _GNU_SOURCE");
+ $self->pidl("#define _GNU_SOURCE");
+ $self->pidl("#endif");
+ $self->pidl("#include <stdint.h>");
+ $self->pidl("#include <stdlib.h>");
+ $self->pidl("#include <stdio.h>");
+ $self->pidl("#include <stdbool.h>");
+ $self->pidl("#include <stdarg.h>");
+ $self->pidl("#include <string.h>");
+ }
+}
+
+#####################################################################
+# parse a parsed IDL structure back into an IDL file
+sub Parse($$$$)
+{
+ my($self, $ndr,$gen_header,$ndr_header) = @_;
+
+ $self->pidl_hdr("/* header auto-generated by pidl */");
+ $self->pidl_hdr("");
+ $self->pidl_hdr(choose_header("librpc/ndr/libndr.h", "ndr.h"));
+ $self->pidl_hdr("#include \"$gen_header\"") if ($gen_header);
+ $self->pidl_hdr("");
+
+ $self->pidl("/* parser auto-generated by pidl */");
+ $self->pidl("");
+ $self->GenerateIncludes();
+ $self->pidl("#include \"$ndr_header\"") if ($ndr_header);
+ $self->pidl("");
+
+ my %needed = ();
+
+ foreach (@{$ndr}) {
+ ($_->{TYPE} eq "INTERFACE") && NeededInterface($_, \%needed);
+ }
+
+ foreach (@{$ndr}) {
+ ($_->{TYPE} eq "INTERFACE") && $self->ParseInterface($_, \%needed);
+ ($_->{TYPE} eq "IMPORT") && $self->HeaderImport(@{$_->{PATHS}});
+ ($_->{TYPE} eq "INCLUDE") && $self->HeaderInclude(@{$_->{PATHS}});
+ }
+
+ return ($self->{res_hdr}, $self->{res});
+}
+
+sub NeededElement($$$)
+{
+ my ($e, $dir, $needed) = @_;
+
+ return if ($e->{TYPE} eq "EMPTY");
+
+ return if (ref($e->{TYPE}) eq "HASH" and
+ not defined($e->{TYPE}->{NAME}));
+
+ my ($t, $rt);
+ if (ref($e->{TYPE}) eq "HASH") {
+ $t = $e->{TYPE}->{TYPE}."_".$e->{TYPE}->{NAME};
+ } else {
+ $t = $e->{TYPE};
+ }
+
+ if (ref($e->{REPRESENTATION_TYPE}) eq "HASH") {
+ $rt = $e->{REPRESENTATION_TYPE}->{TYPE}."_".$e->{REPRESENTATION_TYPE}->{NAME};
+ } else {
+ $rt = $e->{REPRESENTATION_TYPE};
+ }
+
+ die ("$e->{NAME} $t, $rt FOO") unless ($rt ne "");
+
+ my @fn = ();
+ if ($dir eq "print") {
+ push(@fn, TypeFunctionName("ndr_print", $e->{REPRESENTATION_TYPE}));
+ } elsif ($dir eq "pull") {
+ push (@fn, TypeFunctionName("ndr_pull", $e->{TYPE}));
+ push (@fn, "ndr_$t\_to_$rt")
+ if ($rt ne $t);
+ } elsif ($dir eq "push") {
+ push (@fn, TypeFunctionName("ndr_push", $e->{TYPE}));
+ push (@fn, "ndr_$rt\_to_$t")
+ if ($rt ne $t);
+ } else {
+ die("invalid direction `$dir'");
+ }
+
+ foreach (@fn) {
+ unless (defined($needed->{$_})) {
+ $needed->{$_} = 1;
+ }
+ }
+}
+
+sub NeededFunction($$)
+{
+ my ($fn,$needed) = @_;
+ $needed->{"ndr_pull_$fn->{NAME}"} = 1;
+ $needed->{"ndr_push_$fn->{NAME}"} = 1;
+ $needed->{"ndr_print_$fn->{NAME}"} = 1;
+ foreach my $e (@{$fn->{ELEMENTS}}) {
+ $e->{PARENT} = $fn;
+ NeededElement($e, $_, $needed) foreach ("pull", "push", "print");
+ }
+}
+
+sub NeededType($$$)
+{
+ sub NeededType($$$);
+ my ($t,$needed,$req) = @_;
+
+ NeededType($t->{DATA}, $needed, $req) if ($t->{TYPE} eq "TYPEDEF");
+ NeededType($t->{DATA}, $needed, $req) if ($t->{TYPE} eq "PIPE");
+
+ if ($t->{TYPE} eq "STRUCT" or $t->{TYPE} eq "UNION") {
+ return unless defined($t->{ELEMENTS});
+ for my $e (@{$t->{ELEMENTS}}) {
+ $e->{PARENT} = $t;
+ if (has_property($e, "compression")) {
+ $needed->{"compression"} = 1;
+ }
+ NeededElement($e, $req, $needed);
+ NeededType($e->{TYPE}, $needed, $req) if (ref($e->{TYPE}) eq "HASH");
+ }
+ }
+}
+
+#####################################################################
+# work out what parse functions are needed
+sub NeededInterface($$)
+{
+ my ($interface,$needed) = @_;
+ NeededFunction($_, $needed) foreach (@{$interface->{FUNCTIONS}});
+ foreach (reverse @{$interface->{TYPES}}) {
+
+ if (has_property($_, "public")) {
+ $needed->{TypeFunctionName("ndr_pull", $_)} = $needed->{TypeFunctionName("ndr_push", $_)} =
+ $needed->{TypeFunctionName("ndr_print", $_)} = 1;
+ }
+
+ NeededType($_, $needed, "pull") if ($needed->{TypeFunctionName("ndr_pull", $_)});
+ NeededType($_, $needed, "push") if ($needed->{TypeFunctionName("ndr_push", $_)});
+ NeededType($_, $needed, "print") if ($needed->{TypeFunctionName("ndr_print", $_)});
+ if (has_property($_, "gensize")) {
+ $needed->{"ndr_size_$_->{NAME}"} = 1;
+ }
+ }
+}
+
+sub TypeFunctionName($$)
+{
+ my ($prefix, $t) = @_;
+
+ return "$prefix\_$t->{NAME}" if (ref($t) eq "HASH" and
+ $t->{TYPE} eq "TYPEDEF");
+ return "$prefix\_$t->{TYPE}_$t->{NAME}" if (ref($t) eq "HASH");
+ return "$prefix\_$t";
+}
+
+1;
diff --git a/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Server.pm b/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Server.pm
new file mode 100644
index 00000000..ad36f000
--- /dev/null
+++ b/tools/pidl/lib/Parse/Pidl/Samba4/NDR/Server.pm
@@ -0,0 +1,342 @@
+###################################################
+# server boilerplate generator
+# Copyright tridge@samba.org 2003
+# Copyright metze@samba.org 2004
+# released under the GNU GPL
+
+package Parse::Pidl::Samba4::NDR::Server;
+
+use strict;
+use Parse::Pidl::Util;
+
+use vars qw($VERSION);
+$VERSION = '0.01';
+
+my($res);
+
+sub pidl($)
+{
+ $res .= shift;
+}
+
+
+#####################################################
+# generate the switch statement for function dispatch
+sub gen_dispatch_switch($)
+{
+ my $interface = shift;
+
+ foreach my $fn (@{$interface->{FUNCTIONS}}) {
+ next if not defined($fn->{OPNUM});
+
+ pidl "\tcase $fn->{OPNUM}: {\n";
+ pidl "\t\tstruct $fn->{NAME} *r2 = (struct $fn->{NAME} *)r;\n";
+ pidl "\t\tif (DEBUGLEVEL >= 10) {\n";
+ pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_IN, r2);\n";
+ pidl "\t\t}\n";
+ if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
+ pidl "\t\tr2->out.result = dcesrv_$fn->{NAME}(dce_call, mem_ctx, r2);\n";
+ } else {
+ pidl "\t\tdcesrv_$fn->{NAME}(dce_call, mem_ctx, r2);\n";
+ }
+ pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n";
+ pidl "\t\t\tDEBUG(5,(\"function $fn->{NAME} will reply async\\n\"));\n";
+ pidl "\t\t}\n";
+ pidl "\t\tbreak;\n\t}\n";
+ }
+}
+
+#####################################################
+# generate the switch statement for function reply
+sub gen_reply_switch($)
+{
+ my $interface = shift;
+
+ foreach my $fn (@{$interface->{FUNCTIONS}}) {
+ next if not defined($fn->{OPNUM});
+
+ pidl "\tcase $fn->{OPNUM}: {\n";
+ pidl "\t\tstruct $fn->{NAME} *r2 = (struct $fn->{NAME} *)r;\n";
+ pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n";
+ pidl "\t\t\tDEBUG(5,(\"function $fn->{NAME} replied async\\n\"));\n";
+ pidl "\t\t}\n";
+ pidl "\t\tif (DEBUGLEVEL >= 10 && dce_call->fault_code == 0) {\n";
+ pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_OUT | NDR_SET_VALUES, r2);\n";
+ pidl "\t\t}\n";
+ pidl "\t\tif (dce_call->fault_code != 0) {\n";
+ pidl "\t\t\tDEBUG(2,(\"dcerpc_fault %s in $fn->{NAME}\\n\", dcerpc_errstr(mem_ctx, dce_call->fault_code)));\n";
+ pidl "\t\t}\n";
+ pidl "\t\tbreak;\n\t}\n";
+ }
+}
+
+#####################################################################
+# produce boilerplate code for a interface
+sub Boilerplate_Iface($)
+{
+ my($interface) = shift;
+ my $name = $interface->{NAME};
+ my $uname = uc $name;
+ my $uuid = lc($interface->{UUID});
+ my $if_version = $interface->{VERSION};
+
+ pidl "
+static NTSTATUS $name\__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface, uint32_t if_version)
+{
+#ifdef DCESRV_INTERFACE_$uname\_BIND
+ return DCESRV_INTERFACE_$uname\_BIND(dce_call,iface);
+#else
+ return NT_STATUS_OK;
+#endif
+}
+
+static void $name\__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
+{
+#ifdef DCESRV_INTERFACE_$uname\_UNBIND
+ DCESRV_INTERFACE_$uname\_UNBIND(context, iface);
+#else
+ return;
+#endif
+}
+
+static NTSTATUS $name\__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
+{
+ enum ndr_err_code ndr_err;
+ uint16_t opnum = dce_call->pkt.u.request.opnum;
+
+ dce_call->fault_code = 0;
+
+ if (opnum >= ndr_table_$name.num_calls) {
+ dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
+ return NT_STATUS_NET_WRITE_FAULT;
+ }
+
+ *r = talloc_named(mem_ctx,
+ ndr_table_$name.calls[opnum].struct_size,
+ \"struct %s\",
+ ndr_table_$name.calls[opnum].name);
+ NT_STATUS_HAVE_NO_MEMORY(*r);
+
+ /* unravel the NDR for the packet */
+ ndr_err = ndr_table_$name.calls[opnum].ndr_pull(pull, NDR_IN, *r);
+ if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+ dcerpc_log_packet(dce_call->conn->packet_log_dir,
+ &ndr_table_$name, opnum, NDR_IN,
+ &dce_call->pkt.u.request.stub_and_verifier);
+ dce_call->fault_code = DCERPC_FAULT_NDR;
+ return NT_STATUS_NET_WRITE_FAULT;
+ }
+
+ return NT_STATUS_OK;
+}
+
+static NTSTATUS $name\__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
+{
+ uint16_t opnum = dce_call->pkt.u.request.opnum;
+
+ switch (opnum) {
+";
+ gen_dispatch_switch($interface);
+
+pidl "
+ default:
+ dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
+ break;
+ }
+
+ if (dce_call->fault_code != 0) {
+ dcerpc_log_packet(dce_call->conn->packet_log_dir,
+ &ndr_table_$name, opnum, NDR_IN,
+ &dce_call->pkt.u.request.stub_and_verifier);
+ return NT_STATUS_NET_WRITE_FAULT;
+ }
+
+ return NT_STATUS_OK;
+}
+
+static NTSTATUS $name\__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
+{
+ uint16_t opnum = dce_call->pkt.u.request.opnum;
+
+ switch (opnum) {
+";
+ gen_reply_switch($interface);
+
+pidl "
+ default:
+ dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
+ break;
+ }
+
+ if (dce_call->fault_code != 0) {
+ dcerpc_log_packet(dce_call->conn->packet_log_dir,
+ &ndr_table_$name, opnum, NDR_IN,
+ &dce_call->pkt.u.request.stub_and_verifier);
+ return NT_STATUS_NET_WRITE_FAULT;
+ }
+
+ return NT_STATUS_OK;
+}
+
+static NTSTATUS $name\__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
+{
+ enum ndr_err_code ndr_err;
+ uint16_t opnum = dce_call->pkt.u.request.opnum;
+
+ ndr_err = ndr_table_$name.calls[opnum].ndr_push(push, NDR_OUT, r);
+ if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+ dce_call->fault_code = DCERPC_FAULT_NDR;
+ return NT_STATUS_NET_WRITE_FAULT;
+ }
+
+ return NT_STATUS_OK;
+}
+
+static const struct dcesrv_interface dcesrv\_$name\_interface = {
+ .name = \"$name\",
+ .syntax_id = {".print_uuid($uuid).",$if_version},
+ .bind = $name\__op_bind,
+ .unbind = $name\__op_unbind,
+ .ndr_pull = $name\__op_ndr_pull,
+ .dispatch = $name\__op_dispatch,
+ .reply = $name\__op_reply,
+ .ndr_push = $name\__op_ndr_push,
+#ifdef DCESRV_INTERFACE_$uname\_FLAGS
+ .flags = DCESRV_INTERFACE_$uname\_FLAGS
+#else
+ .flags = 0
+#endif
+};
+
+";
+}
+
+#####################################################################
+# produce boilerplate code for an endpoint server
+sub Boilerplate_Ep_Server($)
+{
+ my($interface) = shift;
+ my $name = $interface->{NAME};
+ my $uname = uc $name;
+
+ pidl "
+static NTSTATUS $name\__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
+{
+ int i;
+
+ for (i=0;i<ndr_table_$name.endpoints->count;i++) {
+ NTSTATUS ret;
+ const char *name = ndr_table_$name.endpoints->names[i];
+
+ ret = dcesrv_interface_register(dce_ctx, name, &dcesrv_$name\_interface, NULL);
+ if (!NT_STATUS_IS_OK(ret)) {
+ DEBUG(1,(\"$name\_op_init_server: failed to register endpoint \'%s\'\\n\",name));
+ return ret;
+ }
+ }
+
+ return NT_STATUS_OK;
+}
+
+static bool $name\__op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
+{
+ if (dcesrv_$name\_interface.syntax_id.if_version == if_version &&
+ GUID_equal(\&dcesrv\_$name\_interface.syntax_id.uuid, uuid)) {
+ memcpy(iface,&dcesrv\_$name\_interface, sizeof(*iface));
+ return true;
+ }
+
+ return false;
+}
+
+static bool $name\__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
+{
+ if (strcmp(dcesrv_$name\_interface.name, name)==0) {
+ memcpy(iface, &dcesrv_$name\_interface, sizeof(*iface));
+ return true;
+ }
+
+ return false;
+}
+
+NTSTATUS dcerpc_server_$name\_init(TALLOC_CTX *ctx)
+{
+ NTSTATUS ret;
+ static const struct dcesrv_endpoint_server ep_server = {
+ /* fill in our name */
+ .name = \"$name\",
+
+ /* fill in all the operations */
+#ifdef DCESRV_INTERFACE_$uname\_INIT_SERVER
+ .init_server = DCESRV_INTERFACE_$uname\_INIT_SERVER,
+#else
+ .init_server = $name\__op_init_server,
+#endif
+ .interface_by_uuid = $name\__op_interface_by_uuid,
+ .interface_by_name = $name\__op_interface_by_name
+ };
+ /* register ourselves with the DCERPC subsystem. */
+ ret = dcerpc_register_ep_server(&ep_server);
+
+ if (!NT_STATUS_IS_OK(ret)) {
+ DEBUG(0,(\"Failed to register \'$name\' endpoint server!\\n\"));
+ return ret;
+ }
+
+ return ret;
+}
+
+";
+}
+
+#####################################################################
+# dcerpc server boilerplate from a parsed IDL structure
+sub ParseInterface($)
+{
+ my($interface) = shift;
+ my $count = 0;
+
+ $res .= "NTSTATUS dcerpc_server_$interface->{NAME}\_init(TALLOC_CTX *);\n";
+ $res .= "\n";
+
+ if (!defined $interface->{PROPERTIES}->{uuid}) {
+ return $res;
+ }
+
+ if (!defined $interface->{PROPERTIES}->{version}) {
+ $interface->{PROPERTIES}->{version} = "0.0";
+ }
+
+ foreach my $fn (@{$interface->{FUNCTIONS}}) {
+ if (defined($fn->{OPNUM})) { $count++; }
+ }
+
+ if ($count == 0) {
+ return $res;
+ }
+
+ $res .= "/* $interface->{NAME} - dcerpc server boilerplate generated by pidl */\n\n";
+ Boilerplate_Iface($interface);
+ Boilerplate_Ep_Server($interface);
+
+ return $res;
+}
+
+sub Parse($$)
+{
+ my($ndr,$header) = @_;
+
+ $res = "";
+ $res .= "/* server functions auto-generated by pidl */\n";
+ $res .= "#include \"$header\"\n";
+ $res .= "#include <util/debug.h>\n";
+ $res .= "\n";
+
+ foreach my $x (@{$ndr}) {
+ ParseInterface($x) if ($x->{TYPE} eq "INTERFACE" and not defined($x->{PROPERTIES}{object}));
+ }
+
+ return $res;
+}
+
+1;