diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2008-09-17 17:12:27 +0200 |
---|---|---|
committer | Jelmer Vernooij <jelmer@samba.org> | 2008-09-17 17:12:27 +0200 |
commit | 79190992b3820cd028c961c48bdea9b35baf13c9 (patch) | |
tree | 0de851669d98f04e947d20349d96723462cd1eb0 /pidl/lib/Parse/Pidl/Samba4 | |
parent | 3b5330e9094ecf0be94d3dbea744de140ec55e19 (diff) | |
download | samba-79190992b3820cd028c961c48bdea9b35baf13c9.tar.gz samba-79190992b3820cd028c961c48bdea9b35baf13c9.tar.bz2 samba-79190992b3820cd028c961c48bdea9b35baf13c9.zip |
Move pidl to top-level directory.
Diffstat (limited to 'pidl/lib/Parse/Pidl/Samba4')
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/COM/Header.pm | 155 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/COM/Proxy.pm | 221 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/COM/Stub.pm | 327 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/EJS.pm | 874 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/Header.pm | 475 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/NDR/Client.pm | 156 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm | 2695 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/NDR/Server.pm | 328 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/Python.pm | 1216 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/SWIG.pm | 177 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/TDR.pm | 281 | ||||
-rw-r--r-- | pidl/lib/Parse/Pidl/Samba4/Template.pm | 98 |
12 files changed, 7003 insertions, 0 deletions
diff --git a/pidl/lib/Parse/Pidl/Samba4/COM/Header.pm b/pidl/lib/Parse/Pidl/Samba4/COM/Header.pm new file mode 100644 index 0000000000..996689b4b6 --- /dev/null +++ b/pidl/lib/Parse/Pidl/Samba4/COM/Header.pm @@ -0,0 +1,155 @@ +# COM Header generation +# (C) 2005 Jelmer Vernooij <jelmer@samba.org> + +package Parse::Pidl::Samba4::COM::Header; + +use Parse::Pidl::Typelist qw(mapTypeName); +use Parse::Pidl::Util qw(has_property is_constant); + +use vars qw($VERSION); +$VERSION = '0.01'; + +use strict; + +sub GetArgumentProtoList($) +{ + my $f = shift; + my $res = ""; + + foreach my $a (@{$f->{ELEMENTS}}) { + + $res .= ", " . mapTypeName($a->{TYPE}) . " "; + + my $l = $a->{POINTERS}; + $l-- if (Parse::Pidl::Typelist::scalar_is_reference($a->{TYPE})); + foreach my $i (1..$l) { + $res .= "*"; + } + + if (defined $a->{ARRAY_LEN}[0] && !is_constant($a->{ARRAY_LEN}[0]) && + !$a->{POINTERS}) { + $res .= "*"; + } + $res .= $a->{NAME}; + if (defined $a->{ARRAY_LEN}[0] && is_constant($a->{ARRAY_LEN}[0])) { + $res .= "[$a->{ARRAY_LEN}[0]]"; + } + } + + return $res; +} + +sub GetArgumentList($) +{ + my $f = shift; + my $res = ""; + + foreach (@{$f->{ELEMENTS}}) { $res .= ", $_->{NAME}"; } + + return $res; +} + +##################################################################### +# generate vtable structure for COM interface +sub HeaderVTable($) +{ + my $interface = shift; + my $res; + $res .= "#define " . uc($interface->{NAME}) . "_METHODS \\\n"; + if (defined($interface->{BASE})) { + $res .= "\t" . uc($interface->{BASE} . "_METHODS") . "\\\n"; + } + + my $data = $interface->{DATA}; + foreach my $d (@{$data}) { + $res .= "\t" . mapTypeName($d->{RETURN_TYPE}) . " (*$d->{NAME}) (struct $interface->{NAME} *d, TALLOC_CTX *mem_ctx" . GetArgumentProtoList($d) . ");\\\n" if ($d->{TYPE} eq "FUNCTION"); + } + $res .= "\n"; + $res .= "struct $interface->{NAME}_vtable {\n"; + $res .= "\tstruct GUID iid;\n"; + $res .= "\t" . uc($interface->{NAME}) . "_METHODS\n"; + $res .= "};\n\n"; + + return $res; +} + +sub ParseInterface($) +{ + my $if = shift; + my $res; + + $res .= "\n#ifndef _$if->{NAME}_\n"; + $res .= "#define _$if->{NAME}_\n"; + + $res .="\n\n/* $if->{NAME} */\n"; + + $res .="#define COM_" . uc($if->{NAME}) . "_UUID $if->{PROPERTIES}->{uuid}\n\n"; + + $res .="struct $if->{NAME}_vtable;\n\n"; + + $res .="struct $if->{NAME} { + struct OBJREF obj; + struct com_context *ctx; + struct $if->{NAME}_vtable *vtable; + void *object_data; +};\n\n"; + + $res.=HeaderVTable($if); + + foreach my $d (@{$if->{DATA}}) { + next if ($d->{TYPE} ne "FUNCTION"); + + $res .= "#define $if->{NAME}_$d->{NAME}(interface, mem_ctx" . GetArgumentList($d) . ") "; + + $res .= "((interface)->vtable->$d->{NAME}(interface, mem_ctx" . GetArgumentList($d) . "))"; + + $res .="\n"; + } + + $res .= "#endif\n"; + + return $res; +} + +sub ParseCoClass($) +{ + my ($c) = @_; + my $res = ""; + $res .= "#define CLSID_" . uc($c->{NAME}) . " $c->{PROPERTIES}->{uuid}\n"; + if (has_property($c, "progid")) { + $res .= "#define PROGID_" . uc($c->{NAME}) . " $c->{PROPERTIES}->{progid}\n"; + } + $res .= "\n"; + return $res; +} + +sub Parse($$) +{ + my ($idl,$ndr_header) = @_; + my $res = ""; + + $res .= "#include \"librpc/gen_ndr/orpc.h\"\n" . + "#include \"$ndr_header\"\n\n"; + + foreach (@{$idl}) + { + if ($_->{TYPE} eq "INTERFACE" && has_property($_, "object")) { + $res .="struct $_->{NAME};\n"; + } + } + + foreach (@{$idl}) + { + if ($_->{TYPE} eq "INTERFACE" && has_property($_, "object")) { + $res.=ParseInterface($_); + } + + if ($_->{TYPE} eq "COCLASS") { + $res.=ParseCoClass($_); + } + } + + return $res; +} + +1; diff --git a/pidl/lib/Parse/Pidl/Samba4/COM/Proxy.pm b/pidl/lib/Parse/Pidl/Samba4/COM/Proxy.pm new file mode 100644 index 0000000000..ca9f37a053 --- /dev/null +++ b/pidl/lib/Parse/Pidl/Samba4/COM/Proxy.pm @@ -0,0 +1,221 @@ +################################################### +# DCOM parser for Samba +# Basically the glue between COM and DCE/RPC with NDR +# Copyright jelmer@samba.org 2003-2005 +# released under the GNU GPL + +package Parse::Pidl::Samba4::COM::Proxy; + +use Parse::Pidl::Samba4::COM::Header; +use Parse::Pidl::Typelist qw(mapTypeName); +use Parse::Pidl::Util qw(has_property); + +use vars qw($VERSION); +$VERSION = '0.01'; + +use strict; + +my($res); + +sub ParseVTable($$) +{ + my ($interface, $name) = @_; + + # Generate the vtable + $res .="\tstruct $interface->{NAME}_vtable $name = {"; + + if (defined($interface->{BASE})) { + $res .= "\n\t\t{},"; + } + + my $data = $interface->{DATA}; + + foreach my $d (@{$data}) { + if ($d->{TYPE} eq "FUNCTION") { + $res .= "\n\t\tdcom_proxy_$interface->{NAME}_$d->{NAME}"; + $res .= ","; + } + } + + $res .= "\n\t};\n\n"; +} + +sub ParseRegFunc($) +{ + my $interface = shift; + + $res .= "static NTSTATUS dcom_proxy_$interface->{NAME}_init(void) +{ + struct $interface->{NAME}_vtable *proxy_vtable = talloc(talloc_autofree_context(), struct $interface->{NAME}_vtable); +"; + + if (defined($interface->{BASE})) { + $res.= " + struct GUID base_iid; + const void *base_vtable; + + base_iid = ndr_table_$interface->{BASE}.syntax_id.uuid; + + base_vtable = dcom_proxy_vtable_by_iid(&base_iid); + if (base_vtable == NULL) { + DEBUG(0, (\"No proxy registered for base interface '$interface->{BASE}'\\n\")); + return NT_STATUS_FOOBAR; + } + + memcpy(&proxy_vtable, base_vtable, sizeof(struct $interface->{BASE}_vtable)); + +"; + } + foreach my $x (@{$interface->{DATA}}) { + next unless ($x->{TYPE} eq "FUNCTION"); + + $res .= "\tproxy_vtable->$x->{NAME} = dcom_proxy_$interface->{NAME}_$x->{NAME};\n"; + } + + $res.= " + proxy_vtable->iid = ndr_table_$interface->{NAME}.syntax_id.uuid; + + return dcom_register_proxy((struct IUnknown_vtable *)proxy_vtable); +}\n\n"; +} + +##################################################################### +# parse a function +sub ParseFunction($$) +{ + my ($interface, $fn) = @_; + my $name = $fn->{NAME}; + my $uname = uc $name; + + my $tn = mapTypeName($fn->{RETURN_TYPE}); + + $res.=" +static $tn dcom_proxy_$interface->{NAME}_$name(struct $interface->{NAME} *d, TALLOC_CTX *mem_ctx" . Parse::Pidl::Samba4::COM::Header::GetArgumentProtoList($fn) . ") +{ + struct dcerpc_pipe *p; + NTSTATUS status = dcom_get_pipe(d, &p); + struct $name r; + struct rpc_request *req; + + if (NT_STATUS_IS_ERR(status)) { + return status; + } + + ZERO_STRUCT(r.in.ORPCthis); + r.in.ORPCthis.version.MajorVersion = COM_MAJOR_VERSION; + r.in.ORPCthis.version.MinorVersion = COM_MINOR_VERSION; +"; + + # Put arguments into r + foreach my $a (@{$fn->{ELEMENTS}}) { + next unless (has_property($a, "in")); + if (Parse::Pidl::Typelist::typeIs($a->{TYPE}, "INTERFACE")) { + $res .="\tNDR_CHECK(dcom_OBJREF_from_IUnknown(mem_ctx, &r.in.$a->{NAME}.obj, $a->{NAME}));\n"; + } else { + $res .= "\tr.in.$a->{NAME} = $a->{NAME};\n"; + } + } + + $res .=" + if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) { + NDR_PRINT_IN_DEBUG($name, &r); + } + + status = dcerpc_ndr_request(p, &d->ipid, &ndr_table_$interface->{NAME}, NDR_$uname, mem_ctx, &r); + + if (NT_STATUS_IS_OK(status) && (p->conn->flags & DCERPC_DEBUG_PRINT_OUT)) { + NDR_PRINT_OUT_DEBUG($name, r); + } + +"; + + # Put r info back into arguments + foreach my $a (@{$fn->{ELEMENTS}}) { + next unless (has_property($a, "out")); + + if (Parse::Pidl::Typelist::typeIs($a->{TYPE}, "INTERFACE")) { + $res .="\tNDR_CHECK(dcom_IUnknown_from_OBJREF(d->ctx, &$a->{NAME}, r.out.$a->{NAME}.obj));\n"; + } else { + $res .= "\t*$a->{NAME} = r.out.$a->{NAME};\n"; + } + + } + + if ($fn->{RETURN_TYPE} eq "NTSTATUS") { + $res .= "\tif (NT_STATUS_IS_OK(status)) status = r.out.result;\n"; + } + + $res .= + " + return r.out.result; +}\n\n"; +} + +##################################################################### +# parse the interface definitions +sub ParseInterface($) +{ + my($interface) = shift; + my($data) = $interface->{DATA}; + $res = "/* DCOM proxy for $interface->{NAME} generated by pidl */\n\n"; + foreach my $d (@{$data}) { + ($d->{TYPE} eq "FUNCTION") && + ParseFunction($interface, $d); + } + + ParseRegFunc($interface); +} + +sub RegistrationFunction($$) +{ + my $idl = shift; + my $basename = shift; + + my $res = "\n\nNTSTATUS dcom_$basename\_init(void)\n"; + $res .= "{\n"; + $res .="\tNTSTATUS status = NT_STATUS_OK;\n"; + foreach my $interface (@{$idl}) { + next if $interface->{TYPE} ne "INTERFACE"; + next if not has_property($interface, "object"); + + my $data = $interface->{DATA}; + my $count = 0; + foreach my $d (@{$data}) { + if ($d->{TYPE} eq "FUNCTION") { $count++; } + } + + next if ($count == 0); + + $res .= "\tstatus = dcom_$interface->{NAME}_init();\n"; + $res .= "\tif (NT_STATUS_IS_ERR(status)) {\n"; + $res .= "\t\treturn status;\n"; + $res .= "\t}\n\n"; + } + $res .= "\treturn status;\n"; + $res .= "}\n\n"; + + return $res; +} + +sub Parse($$) +{ + my ($pidl,$comh_filename) = @_; + my $res = ""; + + $res .= "#include \"includes.h\"\n" . + "#include \"lib/com/dcom/dcom.h\"\n" . + "#include \"$comh_filename\"\n" . + "#include \"librpc/rpc/dcerpc.h\"\n"; + + foreach (@{$pidl}) { + next if ($_->{TYPE} ne "INTERFACE"); + next if has_property($_, "local"); + next unless has_property($_, "object"); + + $res .= ParseInterface($_); + } + + return $res; +} + +1; diff --git a/pidl/lib/Parse/Pidl/Samba4/COM/Stub.pm b/pidl/lib/Parse/Pidl/Samba4/COM/Stub.pm new file mode 100644 index 0000000000..150acbfde9 --- /dev/null +++ b/pidl/lib/Parse/Pidl/Samba4/COM/Stub.pm @@ -0,0 +1,327 @@ +################################################### +# DCOM stub boilerplate generator +# Copyright jelmer@samba.org 2004-2005 +# Copyright tridge@samba.org 2003 +# Copyright metze@samba.org 2004 +# released under the GNU GPL + +package Parse::Pidl::Samba4::COM::Stub; + +use Parse::Pidl::Util qw(has_property); +use strict; + +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 $data = shift; + + my $count = 0; + foreach my $d (@{$data}) { + next if ($d->{TYPE} ne "FUNCTION"); + + pidl "\tcase $count: {\n"; + if ($d->{RETURN_TYPE} && $d->{RETURN_TYPE} ne "void") { + pidl "\t\tNTSTATUS result;\n"; + } + pidl "\t\tstruct $d->{NAME} *r2 = r;\n"; + pidl "\t\tif (DEBUGLEVEL > 10) {\n"; + pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($d->{NAME}, NDR_IN, r2);\n"; + pidl "\t\t}\n"; + if ($d->{RETURN_TYPE} && $d->{RETURN_TYPE} ne "void") { + pidl "\t\tresult = vtable->$d->{NAME}(iface, mem_ctx, r2);\n"; + } else { + pidl "\t\tvtable->$d->{NAME}(iface, mem_ctx, r2);\n"; + } + pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n"; + pidl "\t\t\tDEBUG(5,(\"function $d->{NAME} will reply async\\n\"));\n"; + pidl "\t\t}\n"; + pidl "\t\tbreak;\n\t}\n"; + $count++; + } +} + +##################################################### +# generate the switch statement for function reply +sub gen_reply_switch($) +{ + my $data = shift; + + my $count = 0; + foreach my $d (@{$data}) { + next if ($d->{TYPE} ne "FUNCTION"); + + pidl "\tcase $count: {\n"; + pidl "\t\tstruct $d->{NAME} *r2 = r;\n"; + pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n"; + pidl "\t\t\tDEBUG(5,(\"function $d->{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($d->{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 $d->{NAME}\\n\", dcerpc_errstr(mem_ctx, dce_call->fault_code)));\n"; + pidl "\t\t}\n"; + pidl "\t\tbreak;\n\t}\n"; + $count++; + } +} + +##################################################################### +# produce boilerplate code for a interface +sub Boilerplate_Iface($) +{ + my($interface) = shift; + my($data) = $interface->{DATA}; + my $name = $interface->{NAME}; + my $uname = uc $name; + my $uuid = Parse::Pidl::Util::make_str($interface->{PROPERTIES}->{uuid}); + my $if_version = $interface->{PROPERTIES}->{version}; + + pidl " +static NTSTATUS $name\__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface) +{ +#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) +{ + NTSTATUS status; + uint16_t opnum = dce_call->pkt.u.request.opnum; + + dce_call->fault_code = 0; + + if (opnum >= dcerpc_table_$name.num_calls) { + dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR; + return NT_STATUS_NET_WRITE_FAULT; + } + + *r = talloc_size(mem_ctx, dcerpc_table_$name.calls[opnum].struct_size); + NT_STATUS_HAVE_NO_MEMORY(*r); + + /* unravel the NDR for the packet */ + status = dcerpc_table_$name.calls[opnum].ndr_pull(pull, NDR_IN, *r); + if (!NT_STATUS_IS_OK(status)) { + dcerpc_log_packet(&dcerpc_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; + struct GUID ipid = dce_call->pkt.u.request.object.object; + struct dcom_interface_p *iface = dcom_get_local_iface_p(&ipid); + const struct dcom_$name\_vtable *vtable = iface->vtable; + + switch (opnum) { +"; + gen_dispatch_switch($data); + +pidl " + default: + dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR; + break; + } + + if (dce_call->fault_code != 0) { + dcerpc_log_packet(&dcerpc_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($data); + +pidl " + default: + dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR; + break; + } + + if (dce_call->fault_code != 0) { + dcerpc_log_packet(&dcerpc_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) +{ + NTSTATUS status; + uint16_t opnum = dce_call->pkt.u.request.opnum; + + status = dcerpc_table_$name.calls[opnum].ndr_push(push, NDR_OUT, r); + if (!NT_STATUS_IS_OK(status)) { + dce_call->fault_code = DCERPC_FAULT_NDR; + return NT_STATUS_NET_WRITE_FAULT; + } + + return NT_STATUS_OK; +} + +static const struct dcesrv_interface $name\_interface = { + .name = \"$name\", + .uuid = $uuid, + .if_version = $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 +}; + +"; +} + +##################################################################### +# 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<dcerpc_table_$name.endpoints->count;i++) { + NTSTATUS ret; + const char *name = dcerpc_table_$name.endpoints->names[i]; + + ret = dcesrv_interface_register(dce_ctx, name, &$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 char *uuid, uint32_t if_version) +{ + if (dcerpc_table_$name.if_version == if_version && + strcmp(dcerpc_table_$name.uuid, uuid)==0) { + memcpy(iface,&dcerpc_table_$name, sizeof(*iface)); + return True; + } + + return False; +} + +static BOOL $name\__op_interface_by_name(struct dcesrv_interface *iface, const char *name) +{ + if (strcmp(dcerpc_table_$name.name, name)==0) { + memcpy(iface,&dcerpc_table_$name, sizeof(*iface)); + return True; + } + + return False; +} + +NTSTATUS dcerpc_server_$name\_init(void) +{ + NTSTATUS ret; + struct dcesrv_endpoint_server ep_server; + + /* fill in our name */ + ep_server.name = \"$name\"; + + /* fill in all the operations */ + ep_server.init_server = $name\__op_init_server; + + ep_server.interface_by_uuid = $name\__op_interface_by_uuid; + ep_server.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; +} + +"; +} + +##################################################################### +# dcom interface stub from a parsed IDL structure +sub ParseInterface($) +{ + my($interface) = shift; + + return "" if has_property($interface, "local"); + + my($data) = $interface->{DATA}; + my $count = 0; + + $res = ""; + + if (!defined $interface->{PROPERTIES}->{uuid}) { + return $res; + } + + if (!defined $interface->{PROPERTIES}->{version}) { + $interface->{PROPERTIES}->{version} = "0.0"; + } + + foreach my $d (@{$data}) { + if ($d->{TYPE} eq "FUNCTION") { $count++; } + } + + if ($count == 0) { + return $res; + } + + $res = "/* dcom interface stub generated by pidl */\n\n"; + Boilerplate_Iface($interface); + Boilerplate_Ep_Server($interface); + + return $res; +} + +1; diff --git a/pidl/lib/Parse/Pidl/Samba4/EJS.pm b/pidl/lib/Parse/Pidl/Samba4/EJS.pm new file mode 100644 index 0000000000..efb3f2858d --- /dev/null +++ b/pidl/lib/Parse/Pidl/Samba4/EJS.pm @@ -0,0 +1,874 @@ +################################################### +# EJS function wrapper generator +# Copyright jelmer@samba.org 2005 +# Copyright Andrew Tridgell 2005 +# released under the GNU GPL + +package Parse::Pidl::Samba4::EJS; + +use Exporter; +@ISA = qw(Exporter); +@EXPORT_OK = qw(check_null_pointer fn_declare TypeFunctionName); + +use strict; +use Parse::Pidl::Typelist qw(typeHasBody); +use Parse::Pidl::CUtil qw(get_pointer_to get_value_of); +use Parse::Pidl::Util qw(has_property ParseExpr); +use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel); +use Parse::Pidl::Samba4::Header qw(GenerateStructEnv GenerateFunctionInEnv + GenerateFunctionOutEnv); + +use vars qw($VERSION); +$VERSION = '0.01'; + +sub new($) { + my ($class) = @_; + my $self = { res => "", res_hdr => "", tabs => "", constants => {}}; + bless($self, $class); +} + +sub pidl_hdr ($$) +{ + my $self = shift; + $self->{res_hdr} .= shift; +} + +sub pidl($$) +{ + my ($self, $d) = @_; + if ($d) { + $self->{res} .= $self->{tabs}; + $self->{res} .= $d; + } + $self->{res} .= "\n"; +} + +sub indent($) +{ + my ($self) = @_; + $self->{tabs} .= "\t"; +} + +sub deindent($) +{ + my ($self) = @_; + $self->{tabs} = substr($self->{tabs}, 0, -1); +} + +##################################################################### +# check that a variable we get from ParseExpr isn't a null pointer +sub check_null_pointer($$) +{ + my ($self, $size) = @_; + if ($size =~ /^\*/) { + my $size2 = substr($size, 1); + $self->pidl("if ($size2 == NULL) return NT_STATUS_INVALID_PARAMETER_MIX;"); + } +} + +##################################################################### +# work out is a parse function should be declared static or not +sub fn_declare($$$) +{ + my ($self,$fn,$decl) = @_; + + if (has_property($fn, "public")) { + $self->pidl_hdr("$decl;\n"); + $self->pidl("_PUBLIC_ $decl"); + } else { + $self->pidl("static $decl"); + } +} + +########################### +# pull a scalar element +sub EjsPullScalar($$$$$$$) +{ + my ($self, $e, $l, $var, $name, $env) = @_; + + return if (has_property($e, "value")); + + if (ref($e->{TYPE}) eq "HASH" and not defined($e->{TYPE}->{NAME})) { + $self->EjsTypePull($e->{TYPE}, $var); + } else { + my $pl = Parse::Pidl::NDR::GetPrevLevel($e, $l); + $var = get_pointer_to($var); + # have to handle strings specially :( + if (Parse::Pidl::Typelist::scalar_is_reference($e->{TYPE}) + and (defined($pl) and $pl->{TYPE} eq "POINTER")) { + $var = get_pointer_to($var); + } + + my $t; + if (ref($e->{TYPE}) eq "HASH") { + $t = "$e->{TYPE}->{TYPE}_$e->{TYPE}->{NAME}"; + } else { + $t = $e->{TYPE}; + } + $self->pidl("EJS_CHECK(ejs_pull_$t(ejs, v, $name, $var));"); + } +} + +########################### +# pull a pointer element +sub EjsPullPointer($$$$$$) +{ + my ($self, $e, $l, $var, $name, $env) = @_; + $self->pidl("if (ejs_pull_null(ejs, v, $name)) {"); + $self->indent; + if ($l->{POINTER_TYPE} eq "ref") { + $self->pidl("return NT_STATUS_INVALID_PARAMETER_MIX;"); + } else { + $self->pidl("$var = NULL;"); + } + $self->deindent; + $self->pidl("} else {"); + $self->indent; + $self->pidl("EJS_ALLOC(ejs, $var);"); + $var = get_value_of($var); + $self->EjsPullElement($e, GetNextLevel($e, $l), $var, $name, $env); + $self->deindent; + $self->pidl("}"); +} + +########################### +# pull a string element +sub EjsPullString($$$$$$) +{ + my ($self, $e, $l, $var, $name, $env) = @_; + my $pl = GetPrevLevel($e, $l); + $var = get_pointer_to($var); + if (defined($pl) and $pl->{TYPE} eq "POINTER") { + $var = get_pointer_to($var); + } + $self->pidl("EJS_CHECK(ejs_pull_string(ejs, v, $name, $var));"); +} + +########################### +# pull an array element +sub EjsPullArray($$$$$$) +{ + my ($self, $e, $l, $var, $name, $env) = @_; + my $nl = GetNextLevel($e, $l); + my $length = ParseExpr($l->{LENGTH_IS}, $env, $e); + my $size = ParseExpr($l->{SIZE_IS}, $env, $e); + my $pl = GetPrevLevel($e, $l); + if ($pl && $pl->{TYPE} eq "POINTER") { + $var = get_pointer_to($var); + } + # uint8 arrays are treated as data blobs + if ($nl->{TYPE} eq 'DATA' && $e->{TYPE} eq 'uint8') { + if (!$l->{IS_FIXED}) { + $self->check_null_pointer($size); + $self->pidl("EJS_ALLOC_N(ejs, $var, $size);"); + } + $self->check_null_pointer($length); + $self->pidl("ejs_pull_array_uint8(ejs, v, $name, $var, $length);"); + return; + } + my $avar = $var . "[i]"; + $self->pidl("{"); + $self->indent; + $self->pidl("uint32_t i;"); + if (!$l->{IS_FIXED}) { + $self->pidl("EJS_ALLOC_N(ejs, $var, $size);"); + } + $self->pidl("for (i=0;i<$length;i++) {"); + $self->indent; + $self->pidl("char *id = talloc_asprintf(ejs, \"%s.%u\", $name, i);"); + $self->EjsPullElement($e, $nl, $avar, "id", $env); + $self->pidl("talloc_free(id);"); + $self->deindent; + $self->pidl("}"); + $self->pidl("ejs_push_uint32(ejs, v, $name \".length\", &i);"); + $self->deindent; + $self->pidl("}"); +} + +########################### +# pull a switch element +sub EjsPullSwitch($$$$$$) +{ + my ($self, $e, $l, $var, $name, $env) = @_; + my $switch_var = ParseExpr($l->{SWITCH_IS}, $env, $e); + $self->pidl("ejs_set_switch(ejs, $switch_var);"); + $self->EjsPullElement($e, GetNextLevel($e, $l), $var, $name, $env); +} + +########################### +# pull a structure element +sub EjsPullElement($$$$$$) +{ + my ($self, $e, $l, $var, $name, $env) = @_; + if (($l->{TYPE} eq "POINTER")) { + $self->EjsPullPointer($e, $l, $var, $name, $env); + } elsif (has_property($e, "charset")) { + $self->EjsPullString($e, $l, $var, $name, $env); + } elsif ($l->{TYPE} eq "ARRAY") { + $self->EjsPullArray($e, $l, $var, $name, $env); + } elsif ($l->{TYPE} eq "DATA") { + $self->EjsPullScalar($e, $l, $var, $name, $env); + } elsif (($l->{TYPE} eq "SWITCH")) { + $self->EjsPullSwitch($e, $l, $var, $name, $env); + } else { + $self->pidl("return ejs_panic(ejs, \"unhandled pull type $l->{TYPE}\");"); + } +} + +############################################# +# pull a structure/union element at top level +sub EjsPullElementTop($$$) +{ + my ($self, $e, $env) = @_; + my $l = $e->{LEVELS}[0]; + my $var = ParseExpr($e->{NAME}, $env, $e); + my $name = "\"$e->{NAME}\""; + $self->EjsPullElement($e, $l, $var, $name, $env); +} + +########################### +# pull a struct +sub EjsStructPull($$$) +{ + my ($self, $d, $varname) = @_; + my $env = GenerateStructEnv($d, $varname); + $self->pidl("EJS_CHECK(ejs_pull_struct_start(ejs, &v, name));"); + foreach my $e (@{$d->{ELEMENTS}}) { + $self->EjsPullElementTop($e, $env); + } +} + +########################### +# pull a union +sub EjsUnionPull($$$) +{ + my ($self, $d, $varname) = @_; + my $have_default = 0; + $self->pidl("EJS_CHECK(ejs_pull_struct_start(ejs, &v, name));"); + $self->pidl("switch (ejs->switch_var) {"); + $self->indent; + foreach my $e (@{$d->{ELEMENTS}}) { + if ($e->{CASE} eq "default") { + $have_default = 1; + } + $self->pidl("$e->{CASE}:"); + $self->indent; + if ($e->{TYPE} ne "EMPTY") { + $self->EjsPullElementTop($e, { $e->{NAME} => "$varname->$e->{NAME}"}); + } + $self->pidl("break;"); + $self->deindent; + } + if (! $have_default) { + $self->pidl("default:"); + $self->indent; + $self->pidl("return ejs_panic(ejs, \"Bad switch value\");"); + $self->deindent; + } + $self->deindent; + $self->pidl("}"); +} + +############################################## +# put the enum elements in the constants array +sub EjsEnumConstant($$) +{ + my ($self, $d) = @_; + return unless (defined($d->{ELEMENTS})); + my $v = 0; + foreach my $e (@{$d->{ELEMENTS}}) { + my $el = $e; + chomp $el; + if ($el =~ /^(.*)=\s*(.*)\s*$/) { + $el = $1; + $v = $2; + } + $self->{constants}->{$el} = $v; + $v++; + } +} + +########################### +# pull a enum +sub EjsEnumPull($$$) +{ + my ($self, $d, $varname) = @_; + $self->EjsEnumConstant($d); + $self->pidl("unsigned e;"); + $self->pidl("EJS_CHECK(ejs_pull_enum(ejs, v, name, &e));"); + $self->pidl("*$varname = e;"); +} + +########################### +# pull a bitmap +sub EjsBitmapPull($$$) +{ + my ($self, $d, $varname) = @_; + my $type_fn = $d->{BASE_TYPE}; + $self->pidl("EJS_CHECK(ejs_pull_$type_fn(ejs, v, name, $varname));"); +} + +sub EjsTypePullFunction($$$) +{ + sub EjsTypePullFunction($$$); + my ($self, $d, $name) = @_; + return if (has_property($d, "noejs")); + + if ($d->{TYPE} eq "TYPEDEF") { + $self->EjsTypePullFunction($d->{DATA}, $name); + return; + } + + if ($d->{TYPE} eq "STRUCT") { + $self->fn_declare($d, "NTSTATUS ejs_pull_$name(struct ejs_rpc *ejs, struct MprVar *v, const char *name, struct $name *r)"); + } elsif ($d->{TYPE} eq "UNION") { + $self->fn_declare($d, "NTSTATUS ejs_pull_$name(struct ejs_rpc *ejs, struct MprVar *v, const char *name, union $name *r)"); + } elsif ($d->{TYPE} eq "ENUM") { + $self->fn_declare($d, "NTSTATUS ejs_pull_$name(struct ejs_rpc *ejs, struct MprVar *v, const char *name, enum $name *r)"); + } elsif ($d->{TYPE} eq "BITMAP") { + my($type_decl) = Parse::Pidl::Typelist::mapTypeName($d->{BASE_TYPE}); + $self->fn_declare($d, "NTSTATUS ejs_pull_$name(struct ejs_rpc *ejs, struct MprVar *v, const char *name, $type_decl *r)"); + } + $self->pidl("{"); + $self->indent; + + $self->EjsTypePull($d, "r"); + + $self->pidl("return NT_STATUS_OK;"); + $self->deindent; + $self->pidl("}\n"); +} + +sub EjsTypePull($$$) +{ + my ($self, $d, $varname) = @_; + if ($d->{TYPE} eq 'STRUCT') { + $self->EjsStructPull($d, $varname); + } elsif ($d->{TYPE} eq 'UNION') { + $self->EjsUnionPull($d, $varname); + } elsif ($d->{TYPE} eq 'ENUM') { + $self->EjsEnumPull($d, $varname); + } elsif ($d->{TYPE} eq 'BITMAP') { + $self->EjsBitmapPull($d, $varname); + } else { + warn "Unhandled pull $varname of type $d->{TYPE}"; + } +} + +##################### +# generate a function +sub EjsPullFunction($$) +{ + my ($self, $d) = @_; + my $env = GenerateFunctionInEnv($d); + my $name = $d->{NAME}; + + $self->pidl("\nstatic NTSTATUS ejs_pull_$name(struct ejs_rpc *ejs, struct MprVar *v, struct $name *r)"); + $self->pidl("{"); + $self->indent; + $self->pidl("EJS_CHECK(ejs_pull_struct_start(ejs, &v, \"input\"));"); + + # we pull non-array elements before array elements as arrays + # may have length_is() or size_is() properties that depend + # on the non-array elements + foreach my $e (@{$d->{ELEMENTS}}) { + next unless (grep(/in/, @{$e->{DIRECTION}})); + next if (has_property($e, "length_is") || has_property($e, "size_is")); + $self->EjsPullElementTop($e, $env); + } + + foreach my $e (@{$d->{ELEMENTS}}) { + next unless (grep(/in/, @{$e->{DIRECTION}})); + next unless (has_property($e, "length_is") || has_property($e, "size_is")); + $self->EjsPullElementTop($e, $env); + } + + $self->pidl("return NT_STATUS_OK;"); + $self->deindent; + $self->pidl("}\n"); +} + +########################### +# push a scalar element +sub EjsPushScalar($$$$$$) +{ + my ($self, $e, $l, $var, $name, $env) = @_; + + if (ref($e->{TYPE}) eq "HASH" and not defined($e->{TYPE}->{NAME})) { + $self->EjsTypePush($e->{TYPE}, get_pointer_to($var)); + } else { + # have to handle strings specially :( + my $pl = GetPrevLevel($e, $l); + + if ((not Parse::Pidl::Typelist::scalar_is_reference($e->{TYPE})) + or (defined($pl) and $pl->{TYPE} eq "POINTER")) { + $var = get_pointer_to($var); + } + + $self->pidl("EJS_CHECK(".TypeFunctionName("ejs_push", $e->{TYPE})."(ejs, v, $name, $var));"); + } +} + +########################### +# push a string element +sub EjsPushString($$$$$$) +{ + my ($self, $e, $l, $var, $name, $env) = @_; + my $pl = GetPrevLevel($e, $l); + if (defined($pl) and $pl->{TYPE} eq "POINTER") { + $var = get_pointer_to($var); + } + $self->pidl("EJS_CHECK(ejs_push_string(ejs, v, $name, $var));"); +} + +########################### +# push a pointer element +sub EjsPushPointer($$$$$$) +{ + my ($self, $e, $l, $var, $name, $env) = @_; + $self->pidl("if (NULL == $var) {"); + $self->indent; + if ($l->{POINTER_TYPE} eq "ref") { + $self->pidl("return NT_STATUS_INVALID_PARAMETER_MIX;"); + } else { + $self->pidl("EJS_CHECK(ejs_push_null(ejs, v, $name));"); + } + $self->deindent; + $self->pidl("} else {"); + $self->indent; + $var = get_value_of($var); + $self->EjsPushElement($e, GetNextLevel($e, $l), $var, $name, $env); + $self->deindent; + $self->pidl("}"); +} + +########################### +# push a switch element +sub EjsPushSwitch($$$$$$) +{ + my ($self, $e, $l, $var, $name, $env) = @_; + my $switch_var = ParseExpr($l->{SWITCH_IS}, $env, $e); + $self->pidl("ejs_set_switch(ejs, $switch_var);"); + $self->EjsPushElement($e, GetNextLevel($e, $l), $var, $name, $env); +} + +########################### +# push an array element +sub EjsPushArray($$$$$$) +{ + my ($self, $e, $l, $var, $name, $env) = @_; + my $nl = GetNextLevel($e, $l); + my $length = ParseExpr($l->{LENGTH_IS}, $env, $e); + my $pl = GetPrevLevel($e, $l); + if ($pl && $pl->{TYPE} eq "POINTER") { + $var = get_pointer_to($var); + } + # uint8 arrays are treated as data blobs + if ($nl->{TYPE} eq 'DATA' && $e->{TYPE} eq 'uint8') { + $self->check_null_pointer($length); + $self->pidl("ejs_push_array_uint8(ejs, v, $name, $var, $length);"); + return; + } + my $avar = $var . "[i]"; + $self->pidl("{"); + $self->indent; + $self->pidl("uint32_t i;"); + $self->pidl("for (i=0;i<$length;i++) {"); + $self->indent; + $self->pidl("const char *id = talloc_asprintf(ejs, \"%s.%u\", $name, i);"); + $self->EjsPushElement($e, $nl, $avar, "id", $env); + $self->deindent; + $self->pidl("}"); + $self->pidl("ejs_push_uint32(ejs, v, $name \".length\", &i);"); + $self->deindent; + $self->pidl("}"); +} + +################################ +# push a structure/union element +sub EjsPushElement($$$$$$) +{ + my ($self, $e, $l, $var, $name, $env) = @_; + if (($l->{TYPE} eq "POINTER")) { + $self->EjsPushPointer($e, $l, $var, $name, $env); + } elsif (has_property($e, "charset")) { + $self->EjsPushString($e, $l, $var, $name, $env); + } elsif ($l->{TYPE} eq "ARRAY") { + $self->EjsPushArray($e, $l, $var, $name, $env); + } elsif ($l->{TYPE} eq "DATA") { + $self->EjsPushScalar($e, $l, $var, $name, $env); + } elsif (($l->{TYPE} eq "SWITCH")) { + $self->EjsPushSwitch($e, $l, $var, $name, $env); + } else { + $self->pidl("return ejs_panic(ejs, \"unhandled push type $l->{TYPE}\");"); + } +} + +############################################# +# push a structure/union element at top level +sub EjsPushElementTop($$$) +{ + my ($self, $e, $env) = @_; + my $l = $e->{LEVELS}[0]; + my $var = ParseExpr($e->{NAME}, $env, $e); + my $name = "\"$e->{NAME}\""; + $self->EjsPushElement($e, $l, $var, $name, $env); +} + +########################### +# push a struct +sub EjsStructPush($$$) +{ + my ($self, $d, $varname) = @_; + my $env = GenerateStructEnv($d, $varname); + $self->pidl("EJS_CHECK(ejs_push_struct_start(ejs, &v, name));"); + foreach my $e (@{$d->{ELEMENTS}}) { + $self->EjsPushElementTop($e, $env); + } +} + +########################### +# push a union +sub EjsUnionPush($$$) +{ + my ($self, $d, $varname) = @_; + my $have_default = 0; + $self->pidl("EJS_CHECK(ejs_push_struct_start(ejs, &v, name));"); + $self->pidl("switch (ejs->switch_var) {"); + $self->indent; + foreach my $e (@{$d->{ELEMENTS}}) { + if ($e->{CASE} eq "default") { + $have_default = 1; + } + $self->pidl("$e->{CASE}:"); + $self->indent; + if ($e->{TYPE} ne "EMPTY") { + $self->EjsPushElementTop($e, { $e->{NAME} => "$varname->$e->{NAME}"} ); + } + $self->pidl("break;"); + $self->deindent; + } + if (! $have_default) { + $self->pidl("default:"); + $self->indent; + $self->pidl("return ejs_panic(ejs, \"Bad switch value\");"); + $self->deindent; + } + $self->deindent; + $self->pidl("}"); +} + +########################### +# push a enum +sub EjsEnumPush($$$) +{ + my ($self, $d, $varname) = @_; + $self->EjsEnumConstant($d); + $self->pidl("unsigned e = ".get_value_of($varname).";"); + $self->pidl("EJS_CHECK(ejs_push_enum(ejs, v, name, &e));"); +} + +########################### +# push a bitmap +sub EjsBitmapPush($$$) +{ + my ($self, $d, $varname) = @_; + return unless (defined($d->{ELEMENTS})); + my $type_fn = $d->{BASE_TYPE}; + # put the bitmap elements in the constants array + foreach my $e (@{$d->{ELEMENTS}}) { + if ($e =~ /^(\w*)\s*(.*)\s*$/) { + my $bname = $1; + my $v = $2; + $self->{constants}->{$bname} = $v; + } + } + $self->pidl("EJS_CHECK(ejs_push_$type_fn(ejs, v, name, $varname));"); +} + +sub EjsTypePushFunction($$$) +{ + sub EjsTypePushFunction($$$); + my ($self, $d, $name) = @_; + return if (has_property($d, "noejs")); + + my $var = undef; + my $dt = $d; + if ($dt->{TYPE} eq "TYPEDEF") { + $dt = $dt->{DATA}; + } + if ($dt->{TYPE} eq "STRUCT") { + $var = "const struct $name *r"; + } elsif ($dt->{TYPE} eq "UNION") { + $var = "const union $name *r"; + } elsif ($dt->{TYPE} eq "ENUM") { + $var = "const enum $name *r"; + } elsif ($dt->{TYPE} eq "BITMAP") { + my($type_decl) = Parse::Pidl::Typelist::mapTypeName($dt->{BASE_TYPE}); + $var = "const $type_decl *r"; + } + $self->fn_declare($d, "NTSTATUS ".TypeFunctionName("ejs_push", $d) . "(struct ejs_rpc *ejs, struct MprVar *v, const char *name, $var)"); + $self->pidl("{"); + $self->indent; + $self->EjsTypePush($d, "r"); + $self->pidl("return NT_STATUS_OK;"); + $self->deindent; + $self->pidl("}\n"); +} + +sub EjsTypePush($$$) +{ + sub EjsTypePush($$$); + my ($self, $d, $varname) = @_; + + if ($d->{TYPE} eq 'STRUCT') { + $self->EjsStructPush($d, $varname); + } elsif ($d->{TYPE} eq 'UNION') { + $self->EjsUnionPush($d, $varname); + } elsif ($d->{TYPE} eq 'ENUM') { + $self->EjsEnumPush($d, $varname); + } elsif ($d->{TYPE} eq 'BITMAP') { + $self->EjsBitmapPush($d, $varname); + } elsif ($d->{TYPE} eq 'TYPEDEF') { + $self->EjsTypePush($d->{DATA}, $varname); + } else { + warn "Unhandled push $varname of type $d->{TYPE}"; + } +} + +##################### +# generate a function +sub EjsPushFunction($$) +{ + my ($self, $d) = @_; + my $env = GenerateFunctionOutEnv($d); + + $self->pidl("\nstatic NTSTATUS ejs_push_$d->{NAME}(struct ejs_rpc *ejs, struct MprVar *v, const struct $d->{NAME} *r)"); + $self->pidl("{"); + $self->indent; + $self->pidl("EJS_CHECK(ejs_push_struct_start(ejs, &v, \"output\"));"); + + foreach my $e (@{$d->{ELEMENTS}}) { + next unless (grep(/out/, @{$e->{DIRECTION}})); + $self->EjsPushElementTop($e, $env); + } + + if ($d->{RETURN_TYPE}) { + $self->pidl("EJS_CHECK(".TypeFunctionName("ejs_push", $d->{RETURN_TYPE})."(ejs, v, \"result\", &r->out.result));"); + } + + $self->pidl("return NT_STATUS_OK;"); + $self->deindent; + $self->pidl("}\n"); +} + +################################# +# generate a ejs mapping function +sub EjsFunction($$$) +{ + my ($self, $d, $iface) = @_; + my $name = $d->{NAME}; + my $callnum = uc("NDR_$name"); + my $table = "&ndr_table_$iface"; + + $self->pidl("static int ejs_$name(int eid, int argc, struct MprVar **argv)"); + $self->pidl("{"); + $self->indent; + $self->pidl("return ejs_rpc_call(eid, argc, argv, $table, $callnum, (ejs_pull_function_t)ejs_pull_$name, (ejs_push_function_t)ejs_push_$name);"); + $self->deindent; + $self->pidl("}\n"); +} + +################### +# handle a constant +sub EjsConst($$) +{ + my ($self, $const) = @_; + $self->{constants}->{$const->{NAME}} = $const->{VALUE}; +} + +sub EjsImport +{ + my $self = shift; + my @imports = @_; + foreach (@imports) { + s/\.idl\"$//; + s/^\"//; + $self->pidl_hdr("#include \"librpc/gen_ndr/ndr_$_\_ejs\.h\"\n"); + } +} + +##################################################################### +# parse the interface definitions +sub EjsInterface($$$) +{ + my($self,$interface,$needed) = @_; + my @fns = (); + my $name = $interface->{NAME}; + + $self->pidl_hdr("#ifndef _HEADER_EJS_$interface->{NAME}\n"); + $self->pidl_hdr("#define _HEADER_EJS_$interface->{NAME}\n\n"); + + $self->pidl_hdr("\n"); + + foreach my $d (@{$interface->{TYPES}}) { + next unless (typeHasBody($d)); + ($needed->{TypeFunctionName("ejs_push", $d)}) && $self->EjsTypePushFunction($d, $d->{NAME}); + ($needed->{TypeFunctionName("ejs_pull", $d)}) && $self->EjsTypePullFunction($d, $d->{NAME}); + } + + foreach my $d (@{$interface->{FUNCTIONS}}) { + next if not defined($d->{OPNUM}); + next if has_property($d, "noejs"); + + $self->EjsPullFunction($d); + $self->EjsPushFunction($d); + $self->EjsFunction($d, $name); + + push (@fns, $d->{NAME}); + } + + foreach my $d (@{$interface->{CONSTS}}) { + $self->EjsConst($d); + } + + $self->pidl("static int ejs_$name\_init(int eid, int argc, struct MprVar **argv)"); + $self->pidl("{"); + $self->indent; + $self->pidl("struct MprVar *obj = mprInitObject(eid, \"$name\", argc, argv);"); + foreach (@fns) { + $self->pidl("mprSetCFunction(obj, \"$_\", ejs_$_);"); + } + foreach my $v (keys %{$self->{constants}}) { + my $value = $self->{constants}->{$v}; + if (substr($value, 0, 1) eq "\"") { + $self->pidl("mprSetVar(obj, \"$v\", mprString($value));"); + } else { + $self->pidl("mprSetVar(obj, \"$v\", mprCreateNumberVar($value));"); + } + } + $self->pidl("return ejs_rpc_init(obj, \"$name\");"); + $self->deindent; + $self->pidl("}\n"); + + $self->pidl("NTSTATUS ejs_init_$name(void)"); + $self->pidl("{"); + $self->indent; + $self->pidl("ejsDefineCFunction(-1, \"$name\_init\", ejs_$name\_init, NULL, MPR_VAR_SCRIPT_HANDLE);"); + $self->pidl("return NT_STATUS_OK;"); + $self->deindent; + $self->pidl("}"); + + $self->pidl_hdr("\n"); + $self->pidl_hdr("#endif /* _HEADER_EJS_$interface->{NAME} */\n"); +} + +##################################################################### +# parse a parsed IDL into a C header +sub Parse($$$) +{ + my($self,$ndr,$hdr) = @_; + + my $ejs_hdr = $hdr; + $ejs_hdr =~ s/.h$/_ejs.h/; + + $self->pidl_hdr("/* header auto-generated by pidl */\n\n"); + + $self->pidl(" +/* EJS wrapper functions auto-generated by pidl */ +#include \"includes.h\" +#include \"librpc/rpc/dcerpc.h\" +#include \"lib/appweb/ejs/ejs.h\" +#include \"scripting/ejs/ejsrpc.h\" +#include \"scripting/ejs/smbcalls.h\" +#include \"librpc/gen_ndr/ndr_misc_ejs.h\" +#include \"$hdr\" +#include \"$ejs_hdr\" + +"); + + my %needed = (); + + foreach my $x (@{$ndr}) { + ($x->{TYPE} eq "INTERFACE") && NeededInterface($x, \%needed); + } + + foreach my $x (@$ndr) { + ($x->{TYPE} eq "INTERFACE") && $self->EjsInterface($x, \%needed); + ($x->{TYPE} eq "IMPORT") && $self->EjsImport(@{$x->{PATHS}}); + } + + return ($self->{res_hdr}, $self->{res}); +} + +sub NeededFunction($$) +{ + my ($fn,$needed) = @_; + + $needed->{"ejs_pull_$fn->{NAME}"} = 1; + $needed->{"ejs_push_$fn->{NAME}"} = 1; + + foreach (@{$fn->{ELEMENTS}}) { + next if (has_property($_, "subcontext")); #FIXME: Support subcontexts + if (grep(/in/, @{$_->{DIRECTION}})) { + $needed->{TypeFunctionName("ejs_pull", $_->{TYPE})} = 1; + } + if (grep(/out/, @{$_->{DIRECTION}})) { + $needed->{TypeFunctionName("ejs_push", $_->{TYPE})} = 1; + } + } +} + +sub NeededType($$$) +{ + sub NeededType($$$); + my ($t,$needed,$req) = @_; + + NeededType($t->{DATA}, $needed, $req) if ($t->{TYPE} eq "TYPEDEF"); + + return unless (($t->{TYPE} eq "STRUCT") or ($t->{TYPE} eq "UNION")); + + return unless(typeHasBody($t)); + + foreach (@{$t->{ELEMENTS}}) { + next if (has_property($_, "subcontext")); #FIXME: Support subcontexts + my $n; + if (ref($_->{TYPE}) ne "HASH" or defined($_->{TYPE}->{NAME})) { + $needed->{TypeFunctionName("ejs_$req", $_->{TYPE})} = 1; + } + NeededType($_->{TYPE}, $needed, $req) if (ref($_->{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("ejs_pull", $_)} = not has_property($_, "noejs"); + $needed->{TypeFunctionName("ejs_push", $_)} = not has_property($_, "noejs"); + } + + NeededType($_, $needed, "pull") if ($needed->{TypeFunctionName("ejs_pull", $_)}); + NeededType($_, $needed, "push") if ($needed->{TypeFunctionName("ejs_push", $_)}); + } +} + +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/pidl/lib/Parse/Pidl/Samba4/Header.pm b/pidl/lib/Parse/Pidl/Samba4/Header.pm new file mode 100644 index 0000000000..611f0adb92 --- /dev/null +++ b/pidl/lib/Parse/Pidl/Samba4/Header.pm @@ -0,0 +1,475 @@ +################################################### +# create C header files for an IDL structure +# Copyright tridge@samba.org 2000 +# Copyright jelmer@samba.org 2005 +# released under the GNU GPL + +package Parse::Pidl::Samba4::Header; + +require Exporter; + +@ISA = qw(Exporter); +@EXPORT_OK = qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv); + +use strict; +use Parse::Pidl qw(fatal); +use Parse::Pidl::Typelist qw(mapTypeName scalar_is_reference); +use Parse::Pidl::Util qw(has_property is_constant unmake_str ParseExpr); +use Parse::Pidl::Samba4 qw(is_intree ElementStars ArrayBrackets choose_header); + +use vars qw($VERSION); +$VERSION = '0.01'; + +my($res); +my($tab_depth); + +sub pidl($) { $res .= shift; } + +sub tabs() +{ + my $res = ""; + $res .="\t" foreach (1..$tab_depth); + return $res; +} + +##################################################################### +# parse a properties list +sub HeaderProperties($$) +{ + my($props,$ignores) = @_; + my $ret = ""; + + foreach my $d (keys %{$props}) { + next if (grep(/^$d$/, @$ignores)); + if($props->{$d} ne "1") { + $ret.= "$d($props->{$d}),"; + } else { + $ret.="$d,"; + } + } + + if ($ret) { + pidl "/* [" . substr($ret, 0, -1) . "] */"; + } +} + +##################################################################### +# parse a structure element +sub HeaderElement($) +{ + my($element) = shift; + + pidl tabs(); + if (has_property($element, "represent_as")) { + pidl mapTypeName($element->{PROPERTIES}->{represent_as})." "; + } else { + if (ref($element->{TYPE}) eq "HASH") { + HeaderType($element, $element->{TYPE}, $element->{TYPE}->{NAME}); + } else { + HeaderType($element, $element->{TYPE}, ""); + } + pidl " ".ElementStars($element); + } + pidl $element->{NAME}; + pidl ArrayBrackets($element); + + pidl ";"; + if (defined $element->{PROPERTIES}) { + HeaderProperties($element->{PROPERTIES}, ["in", "out"]); + } + pidl "\n"; +} + +##################################################################### +# parse a struct +sub HeaderStruct($$;$) +{ + my($struct,$name,$tail) = @_; + pidl "struct $name"; + pidl $tail if defined($tail) and not defined($struct->{ELEMENTS}); + return if (not defined($struct->{ELEMENTS})); + pidl " {\n"; + $tab_depth++; + my $el_count=0; + foreach (@{$struct->{ELEMENTS}}) { + HeaderElement($_); + $el_count++; + } + if ($el_count == 0) { + # some compilers can't handle empty structures + pidl tabs()."char _empty_;\n"; + } + $tab_depth--; + pidl tabs()."}"; + if (defined $struct->{PROPERTIES}) { + HeaderProperties($struct->{PROPERTIES}, []); + } + pidl $tail if defined($tail); +} + +##################################################################### +# parse a enum +sub HeaderEnum($$;$) +{ + my($enum,$name,$tail) = @_; + my $first = 1; + + pidl "enum $name"; + if (defined($enum->{ELEMENTS})) { + pidl "\n#ifndef USE_UINT_ENUMS\n"; + pidl " {\n"; + $tab_depth++; + foreach my $e (@{$enum->{ELEMENTS}}) { + unless ($first) { pidl ",\n"; } + $first = 0; + pidl tabs(); + pidl $e; + } + pidl "\n"; + $tab_depth--; + pidl "}"; + pidl "\n"; + pidl "#else\n"; + my $count = 0; + my $with_val = 0; + my $without_val = 0; + pidl " { __donnot_use_enum_$name=0x7FFFFFFF}\n"; + foreach my $e (@{$enum->{ELEMENTS}}) { + my $t = "$e"; + my $name; + my $value; + if ($t =~ /(.*)=(.*)/) { + $name = $1; + $value = $2; + $with_val = 1; + fatal($e->{ORIGINAL}, "you can't mix enum member with values and without values!") + unless ($without_val == 0); + } else { + $name = $t; + $value = $count++; + $without_val = 1; + fatal($e->{ORIGINAL}, "you can't mix enum member with values and without values!") + unless ($with_val == 0); + } + pidl "#define $name ( $value )\n"; + } + pidl "#endif\n"; + } + pidl $tail if defined($tail); +} + +##################################################################### +# parse a bitmap +sub HeaderBitmap($$) +{ + my($bitmap,$name) = @_; + + return unless defined($bitmap->{ELEMENTS}); + + pidl "/* bitmap $name */\n"; + pidl "#define $_\n" foreach (@{$bitmap->{ELEMENTS}}); + pidl "\n"; +} + +##################################################################### +# parse a union +sub HeaderUnion($$;$) +{ + my($union,$name,$tail) = @_; + my %done = (); + + pidl "union $name"; + pidl $tail if defined($tail) and not defined($union->{ELEMENTS}); + return if (not defined($union->{ELEMENTS})); + pidl " {\n"; + $tab_depth++; + foreach my $e (@{$union->{ELEMENTS}}) { + if ($e->{TYPE} ne "EMPTY") { + if (! defined $done{$e->{NAME}}) { + HeaderElement($e); + } + $done{$e->{NAME}} = 1; + } + } + $tab_depth--; + pidl "}"; + + if (defined $union->{PROPERTIES}) { + HeaderProperties($union->{PROPERTIES}, []); + } + pidl $tail if defined($tail); +} + +##################################################################### +# parse a type +sub HeaderType($$$;$) +{ + my($e,$data,$name,$tail) = @_; + if (ref($data) eq "HASH") { + ($data->{TYPE} eq "ENUM") && HeaderEnum($data, $name, $tail); + ($data->{TYPE} eq "BITMAP") && HeaderBitmap($data, $name); + ($data->{TYPE} eq "STRUCT") && HeaderStruct($data, $name, $tail); + ($data->{TYPE} eq "UNION") && HeaderUnion($data, $name, $tail); + return; + } + + if (has_property($e, "charset")) { + pidl "const char"; + } else { + pidl mapTypeName($e->{TYPE}); + } + pidl $tail if defined($tail); +} + +##################################################################### +# parse a typedef +sub HeaderTypedef($;$) +{ + my($typedef,$tail) = @_; + HeaderType($typedef, $typedef->{DATA}, $typedef->{NAME}, $tail) if defined ($typedef->{DATA}); +} + +##################################################################### +# parse a const +sub HeaderConst($) +{ + my($const) = shift; + if (!defined($const->{ARRAY_LEN}[0])) { + pidl "#define $const->{NAME}\t( $const->{VALUE} )\n"; + } else { + pidl "#define $const->{NAME}\t $const->{VALUE}\n"; + } +} + +sub ElementDirection($) +{ + my ($e) = @_; + + return "inout" 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 "inout"; +} + +##################################################################### +# parse a function +sub HeaderFunctionInOut($$) +{ + my($fn,$prop) = @_; + + return unless defined($fn->{ELEMENTS}); + + foreach my $e (@{$fn->{ELEMENTS}}) { + HeaderElement($e) if (ElementDirection($e) eq $prop); + } +} + +##################################################################### +# determine if we need an "in" or "out" section +sub HeaderFunctionInOut_needed($$) +{ + my($fn,$prop) = @_; + + return 1 if ($prop eq "out" && defined($fn->{RETURN_TYPE})); + + return undef unless defined($fn->{ELEMENTS}); + + foreach my $e (@{$fn->{ELEMENTS}}) { + return 1 if (ElementDirection($e) eq $prop); + } + + return undef; +} + +my %headerstructs; + +##################################################################### +# parse a function +sub HeaderFunction($) +{ + my($fn) = shift; + + return if ($headerstructs{$fn->{NAME}}); + + $headerstructs{$fn->{NAME}} = 1; + + pidl "\nstruct $fn->{NAME} {\n"; + $tab_depth++; + my $needed = 0; + + if (HeaderFunctionInOut_needed($fn, "in") or + HeaderFunctionInOut_needed($fn, "inout")) { + pidl tabs()."struct {\n"; + $tab_depth++; + HeaderFunctionInOut($fn, "in"); + HeaderFunctionInOut($fn, "inout"); + $tab_depth--; + pidl tabs()."} in;\n\n"; + $needed++; + } + + if (HeaderFunctionInOut_needed($fn, "out") or + HeaderFunctionInOut_needed($fn, "inout")) { + pidl tabs()."struct {\n"; + $tab_depth++; + HeaderFunctionInOut($fn, "out"); + HeaderFunctionInOut($fn, "inout"); + if (defined($fn->{RETURN_TYPE})) { + pidl tabs().mapTypeName($fn->{RETURN_TYPE}) . " result;\n"; + } + $tab_depth--; + pidl tabs()."} out;\n\n"; + $needed++; + } + + if (!$needed) { + # sigh - some compilers don't like empty structures + pidl tabs()."int _dummy_element;\n"; + } + + $tab_depth--; + pidl "};\n\n"; +} + +sub HeaderImport +{ + my @imports = @_; + foreach my $import (@imports) { + $import = unmake_str($import); + $import =~ s/\.idl$//; + pidl choose_header("librpc/gen_ndr/$import\.h", "gen_ndr/$import.h") . "\n"; + } +} + +sub HeaderInclude +{ + my @includes = @_; + foreach (@includes) { + pidl "#include $_\n"; + } +} + +##################################################################### +# parse the interface definitions +sub HeaderInterface($) +{ + my($interface) = shift; + + pidl "#ifndef _HEADER_$interface->{NAME}\n"; + pidl "#define _HEADER_$interface->{NAME}\n\n"; + + foreach my $c (@{$interface->{CONSTS}}) { + HeaderConst($c); + } + + foreach my $t (@{$interface->{TYPES}}) { + HeaderTypedef($t, ";\n\n") if ($t->{TYPE} eq "TYPEDEF"); + HeaderStruct($t, $t->{NAME}, ";\n\n") if ($t->{TYPE} eq "STRUCT"); + HeaderUnion($t, $t->{NAME}, ";\n\n") if ($t->{TYPE} eq "UNION"); + HeaderEnum($t, $t->{NAME}, ";\n\n") if ($t->{TYPE} eq "ENUM"); + HeaderBitmap($t, $t->{NAME}) if ($t->{TYPE} eq "BITMAP"); + } + + foreach my $fn (@{$interface->{FUNCTIONS}}) { + HeaderFunction($fn); + } + + pidl "#endif /* _HEADER_$interface->{NAME} */\n"; +} + +sub HeaderQuote($) +{ + my($quote) = shift; + + pidl unmake_str($quote->{DATA}) . "\n"; +} + +##################################################################### +# parse a parsed IDL into a C header +sub Parse($) +{ + my($ndr) = shift; + $tab_depth = 0; + + $res = ""; + %headerstructs = (); + pidl "/* header auto-generated by pidl */\n\n"; + if (!is_intree()) { + pidl "#include <util/data_blob.h>\n"; + } + pidl "#include <stdint.h>\n"; + pidl "\n"; + + foreach (@{$ndr}) { + ($_->{TYPE} eq "CPP_QUOTE") && HeaderQuote($_); + ($_->{TYPE} eq "INTERFACE") && HeaderInterface($_); + ($_->{TYPE} eq "IMPORT") && HeaderImport(@{$_->{PATHS}}); + ($_->{TYPE} eq "INCLUDE") && HeaderInclude(@{$_->{PATHS}}); + } + + return $res; +} + +sub GenerateStructEnv($$) +{ + my ($x, $v) = @_; + my %env; + + foreach my $e (@{$x->{ELEMENTS}}) { + $env{$e->{NAME}} = "$v->$e->{NAME}"; + } + + $env{"this"} = $v; + + return \%env; +} + +sub EnvSubstituteValue($$) +{ + my ($env,$s) = @_; + + # Substitute the value() values in the env + foreach my $e (@{$s->{ELEMENTS}}) { + next unless (defined(my $v = has_property($e, "value"))); + + $env->{$e->{NAME}} = ParseExpr($v, $env, $e); + } + + return $env; +} + +sub GenerateFunctionInEnv($;$) +{ + my ($fn, $base) = @_; + my %env; + + $base = "r->" unless defined($base); + + foreach my $e (@{$fn->{ELEMENTS}}) { + if (grep (/in/, @{$e->{DIRECTION}})) { + $env{$e->{NAME}} = $base."in.$e->{NAME}"; + } + } + + return \%env; +} + +sub GenerateFunctionOutEnv($;$) +{ + my ($fn, $base) = @_; + my %env; + + $base = "r->" unless defined($base); + + foreach my $e (@{$fn->{ELEMENTS}}) { + if (grep (/out/, @{$e->{DIRECTION}})) { + $env{$e->{NAME}} = $base."out.$e->{NAME}"; + } elsif (grep (/in/, @{$e->{DIRECTION}})) { + $env{$e->{NAME}} = $base."in.$e->{NAME}"; + } + } + + return \%env; +} + +1; diff --git a/pidl/lib/Parse/Pidl/Samba4/NDR/Client.pm b/pidl/lib/Parse/Pidl/Samba4/NDR/Client.pm new file mode 100644 index 0000000000..f8209be654 --- /dev/null +++ b/pidl/lib/Parse/Pidl/Samba4/NDR/Client.pm @@ -0,0 +1,156 @@ +################################################### +# 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 Parse::Pidl::Samba4 qw(choose_header is_intree); +use Parse::Pidl::Util qw(has_property); + +use vars qw($VERSION); +$VERSION = '0.01'; + +use strict; + +my($res,$res_hdr); + +sub ParseFunctionSend($$$) +{ + my ($interface, $fn, $name) = @_; + my $uname = uc $name; + + my $proto = "struct rpc_request *dcerpc_$name\_send(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r)"; + + $res_hdr .= "\n$proto;\n"; + + $res .= "$proto\n{\n"; + + if (has_property($fn, "todo")) { + $res .= "\treturn NULL;\n"; + } else { + $res .= " + if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) { + NDR_PRINT_IN_DEBUG($name, r); + } + + return dcerpc_ndr_request_send(p, NULL, &ndr_table_$interface->{NAME}, NDR_$uname, mem_ctx, r); +"; + } + + $res .= "}\n\n"; +} + +sub ParseFunctionSync($$$) +{ + my ($interface, $fn, $name) = @_; + + my $proto = "NTSTATUS dcerpc_$name(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r)"; + + $res_hdr .= "\n$proto;\n"; + $res .= "$proto\n{\n"; + + if (has_property($fn, "todo")) { + $res .= "\treturn NT_STATUS_NOT_IMPLEMENTED;\n"; + } else { + $res .= " + struct rpc_request *req; + NTSTATUS status; + + req = dcerpc_$name\_send(p, mem_ctx, r); + if (req == NULL) return NT_STATUS_NO_MEMORY; + + status = dcerpc_ndr_request_recv(req); + + if (NT_STATUS_IS_OK(status) && (p->conn->flags & DCERPC_DEBUG_PRINT_OUT)) { + NDR_PRINT_OUT_DEBUG($name, r); + } +"; + + if (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "NTSTATUS") { + $res .= "\tif (NT_STATUS_IS_OK(status)) status = r->out.result;\n"; + } + $res .= +" + return status; +"; + } + + $res .= "}\n\n"; +} + +##################################################################### +# parse a function +sub ParseFunction($$) +{ + my ($interface, $fn) = @_; + + ParseFunctionSend($interface, $fn, $fn->{NAME}); + ParseFunctionSync($interface, $fn, $fn->{NAME}); +} + +my %done; + +##################################################################### +# parse the interface definitions +sub ParseInterface($) +{ + my($interface) = shift; + + $res_hdr .= "#ifndef _HEADER_RPC_$interface->{NAME}\n"; + $res_hdr .= "#define _HEADER_RPC_$interface->{NAME}\n\n"; + + if (defined $interface->{PROPERTIES}->{uuid}) { + $res_hdr .= "extern const struct ndr_interface_table ndr_table_$interface->{NAME};\n"; + } + + $res .= "/* $interface->{NAME} - client functions generated by pidl */\n\n"; + + foreach my $fn (@{$interface->{FUNCTIONS}}) { + next if not defined($fn->{OPNUM}); + next if defined($done{$fn->{NAME}}); + ParseFunction($interface, $fn); + $done{$fn->{NAME}} = 1; + } + + $res_hdr .= "#endif /* _HEADER_RPC_$interface->{NAME} */\n"; + + return $res; +} + +sub Parse($$$$) +{ + my($ndr,$header,$ndr_header,$client_header) = @_; + + $res = ""; + $res_hdr = ""; + + $res .= "/* client functions auto-generated by pidl */\n"; + $res .= "\n"; + if (is_intree()) { + $res .= "#include \"includes.h\"\n"; + } else { + $res .= "#define _GNU_SOURCE\n"; + $res .= "#include <stdio.h>\n"; + $res .= "#include <stdbool.h>\n"; + $res .= "#include <stdlib.h>\n"; + $res .= "#include <stdint.h>\n"; + $res .= "#include <stdarg.h>\n"; + $res .= "#include <core/ntstatus.h>\n"; + } + $res .= "#include \"$ndr_header\"\n"; + $res .= "#include \"$client_header\"\n"; + $res .= "\n"; + + $res_hdr .= choose_header("librpc/rpc/dcerpc.h", "dcerpc.h")."\n"; + $res_hdr .= "#include \"$header\"\n"; + + foreach my $x (@{$ndr}) { + ($x->{TYPE} eq "INTERFACE") && ParseInterface($x); + } + + return ($res,$res_hdr); +} + +1; diff --git a/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm b/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm new file mode 100644 index 0000000000..fb73075f1a --- /dev/null +++ b/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm @@ -0,0 +1,2695 @@ +################################################### +# 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 is_charset_array); +use Parse::Pidl::Samba4 qw(is_intree choose_header); +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))"; + } + } 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_uint32($ndr, NDR_SCALARS, $size));"); + } + + if ($l->{IS_VARYING}) { + $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, 0));"); # array offset + $self->pidl("NDR_CHECK(ndr_push_uint32($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 derefenced 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; + } +} + +##################################################################### +# parse an array - pull side +sub ParseArrayPullHeader($$$$$$) +{ + my ($self,$e,$l,$ndr,$var_name,$env) = @_; + + my $length; + my $size; + + if ($l->{IS_CONFORMANT}) { + $length = $size = "ndr_get_array_size($ndr, " . get_pointer_to($var_name) . ")"; + } elsif ($l->{IS_ZERO_TERMINATED}) { # Noheader arrays + $length = $size = "ndr_get_string_size($ndr, sizeof(*$var_name))"; + } else { + $length = $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)); + } + + 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) . "));"); + $length = "ndr_get_array_length($ndr, " . get_pointer_to($var_name) .")"; + } + + if ($length ne $size) { + $self->pidl("if ($length > $size) {"); + $self->indent; + $self->pidl("return ndr_pull_error($ndr, NDR_ERR_ARRAY_SIZE, \"Bad array size %u should exceed array length %u\", $size, $length);"); + $self->deindent; + $self->pidl("}"); + } + + if ($l->{IS_CONFORMANT} and 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 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 (not $l->{IS_FIXED} and not is_charset_array($e, $l)) { + $self->AllocateArrayLevel($e,$l,$ndr,$var_name,$size); + } + + return $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); + + $self->pidl("{"); + $self->indent; + $self->pidl("struct ndr_pull *$comndr;"); + $self->pidl("NDR_CHECK(ndr_pull_compression_start($ndr, &$comndr, $alg, $dlen));"); + + 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)) { + $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 $deferred) { + 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($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") { + $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}"; + + $var_name = get_array_element($var_name, $counter); + + if (($primitives and not $l->{IS_DEFERRED}) or ($deferred and $l->{IS_DEFERRED})) { + $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)) { + $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}}; + + 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") { + $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));"); + } + } elsif ($l->{POINTER_TYPE} eq "relative") { + $self->pidl("NDR_CHECK(ndr_push_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));"); + } 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")); + + if ($e->{REPRESENTATION_TYPE} ne $e->{TYPE}) { + $self->pidl("ndr_print_$e->{REPRESENTATION_TYPE}($ndr, \"$e->{NAME}\", $var_name);"); + 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}}) { + if ($l->{TYPE} eq "POINTER") { + $self->pidl("ndr_print_ptr($ndr, \"$e->{NAME}\", $var_name);"); + $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}) { + $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; + $self->pidl("char *idx_$l->{LEVEL_INDEX}=NULL;"); + $self->pidl("if (asprintf(&idx_$l->{LEVEL_INDEX}, \"[\%d]\", $counter) != -1) {"); + $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}}) { + if ($l->{TYPE} eq "POINTER") { + 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->pidl("free(idx_$l->{LEVEL_INDEX});"); + $self->deindent; + $self->pidl("}"); + $self->deindent; + $self->pidl("}"); + $self->pidl("$ndr->depth--;"); + } + } +} + +##################################################################### +# 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));"); + + if (my $range = has_property($e, "range")) { + $var_name = get_value_of($var_name); + my ($low, $high) = split(/,/, $range, 2); + $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 "ARRAY" and $l->{IS_FIXED}); + 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); + my $next_is_array = ($nl->{TYPE} eq "ARRAY"); + my $next_is_string = (($nl->{TYPE} eq "DATA") and + ($nl->{DATA_TYPE} eq "string")); + if ($next_is_array or $next_is_string) { + return undef; + } elsif ($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); + + 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); + + my $nl = GetNextLevel($e, $l); + + if (is_charset_array($e,$l)) { + if ($l->{IS_ZERO_TERMINATED}) { + $self->CheckStringTerminator($ndr, $e, $l, $length); + } + $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 $deferred) { + if ($l->{POINTER_TYPE} ne "ref") { + $self->pidl("if ($var_name) {"); + $self->indent; + + if ($l->{POINTER_TYPE} eq "relative") { + $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") { + $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 = ParseExpr($l->{LENGTH_IS}, $env, $e->{ORIGINAL}); + my $counter = "cntr_$e->{NAME}_$l->{LEVEL_INDEX}"; + my $array_name = $var_name; + + $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 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}));"); + } else { + die("Unhandled pointer type $l->{POINTER_TYPE}"); + } + + $self->pidl("if (_ptr_$e->{NAME}) {"); + $self->indent; + + # 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") { + $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 ParseStructPushPrimitives($$$$$) +{ + my ($self, $struct, $ndr, $varname, $env) = @_; + + # 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}))"; + } + } else { + $size = ParseExpr($e->{LEVELS}[0]->{SIZE_IS}, $env, $e->{ORIGINAL}); + } + + $self->pidl("NDR_CHECK(ndr_push_uint32($ndr, NDR_SCALARS, $size));"); + } else { + $self->pidl("NDR_CHECK(ndr_push_uint32($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}}); +} + +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->DeclareArrayVariables($_) foreach (@{$struct->{ELEMENTS}}); + + $self->start_flags($struct, $ndr); + + $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_$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_$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->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) = @_; + foreach my $l (@{$e->{LEVELS}}) { + if ($l->{TYPE} eq "POINTER" and + not ($l->{POINTER_TYPE} eq "ref" and $l->{LEVEL} eq "TOP")) { + $self->pidl("uint32_t _ptr_$e->{NAME};"); + last; + } + } +} + +sub DeclareArrayVariables($$) +{ + my ($self,$e) = @_; + + foreach my $l (@{$e->{LEVELS}}) { + next if has_fast_array($e,$l); + next if is_charset_array($e,$l); + if ($l->{TYPE} eq "ARRAY") { + $self->pidl("uint32_t cntr_$e->{NAME}_$l->{LEVEL_INDEX};"); + } + } +} + +sub DeclareMemCtxVariables($$) +{ + my ($self,$e) = @_; + foreach my $l (@{$e->{LEVELS}}) { + my $mem_flags = $self->ParseMemCtxPullFlags($e, $l); + if (defined($mem_flags)) { + $self->pidl("TALLOC_CTX *_mem_save_$e->{NAME}_$l->{LEVEL_INDEX};"); + } + } +} + +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(); +} + +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); + $self->DeclareMemCtxVariables($e); + } + + $self->start_flags($struct, $ndr); + + my $env = GenerateStructEnv($struct, $varname); + + $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("int level = ndr_push_get_switch_value($ndr, $varname);"); + + if (defined($e->{SWITCH_TYPE})) { + $self->pidl("NDR_CHECK(ndr_push_$e->{SWITCH_TYPE}($ndr, NDR_SCALARS, level));"); + } + + $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); + $self->ParseElementPush($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_push_error($ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);"); + } + $self->deindent; + $self->pidl("}"); +} + +sub ParseUnionPushDeferred($$$$) +{ + my ($self,$e,$ndr,$varname) = @_; + + my $have_default = 0; + + $self->pidl("int 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\", level);"); + } + $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("if (ndr_flags & NDR_SCALARS) {"); + $self->indent; + $self->ParseUnionPushPrimitives($e, $ndr, $varname); + $self->deindent; + $self->pidl("}"); + $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("int 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)) { + $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\", _level);"); + $self->pidl("}"); + } + + $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->DeclarePtrVariables($el); + $self->DeclareArrayVariables($el); + 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\", level);"); + } + $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\", level);"); + } + $self->deindent; + $self->pidl("}"); + + +} + +##################################################################### +# parse a union - pull side +sub ParseUnionPull($$$$) +{ + my ($self,$e,$ndr,$varname) = @_; + my $switch_type = $e->{SWITCH_TYPE}; + + $self->pidl("int 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); + $double_cases{"$el->{NAME}"} = 1; + } + + $self->start_flags($e, $ndr); + + $self->pidl("level = ndr_pull_get_switch_value($ndr, $varname);"); + + $self->pidl("if (ndr_flags & NDR_SCALARS) {"); + $self->indent; + $self->ParseUnionPullPrimitives($e,$ndr,$varname,$switch_type); + $self->deindent; + $self->pidl("}"); + + $self->pidl("if (ndr_flags & NDR_BUFFERS) {"); + $self->indent; + $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, +}; + +##################################################################### +# 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("$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("if (flags & NDR_IN) {"); + $self->indent; + + my $env = GenerateFunctionInEnv($fn); + + EnvSubstituteValue($env, $fn); + + 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); + 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); + } + + 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("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 "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}));"); + } + } 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 FunctionCallEntry($$) +{ + my ($self, $d) = @_; + return if not defined($d->{OPNUM}); + $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".($d->{ASYNC}?"true":"false").","); + $self->pidl("\t},"); +} + +##################################################################### +# 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}); + + $self->pidl("static const struct ndr_interface_call $interface->{NAME}\_calls[] = {"); + + foreach my $d (@{$interface->{INHERITED_FUNCTIONS}},@{$interface->{FUNCTIONS}}) { + $self->FunctionCallEntry($d); + $count++; + } + $self->pidl("\t{ NULL, 0, NULL, NULL, NULL, false }"); + $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->{PROPERTIES}->{uuid}))); + + if(!defined $interface->{PROPERTIES}->{version}) { $interface->{PROPERTIES}->{version} = "0.0"; } + $self->pidl_hdr("#define NDR_$name\_VERSION $interface->{PROPERTIES}->{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}}) { + 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); + + # Make sure we don't generate a function twice... + $needed->{"ndr_push_$d->{NAME}"} = $needed->{"ndr_pull_$d->{NAME}"} = + $needed->{"ndr_print_$d->{NAME}"} = 0; + } + + $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("#define _GNU_SOURCE"); + $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"); + + 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/pidl/lib/Parse/Pidl/Samba4/NDR/Server.pm b/pidl/lib/Parse/Pidl/Samba4/NDR/Server.pm new file mode 100644 index 0000000000..e30102b4e1 --- /dev/null +++ b/pidl/lib/Parse/Pidl/Samba4/NDR/Server.pm @@ -0,0 +1,328 @@ +################################################### +# 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->{PROPERTIES}->{uuid}); + my $if_version = $interface->{PROPERTIES}->{version}; + + pidl " +static NTSTATUS $name\__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface) +{ +#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(&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(&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(&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; +} + +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 +}; + +"; +} + +##################################################################### +# 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(void) +{ + NTSTATUS ret; + struct dcesrv_endpoint_server ep_server; + + /* fill in our name */ + ep_server.name = \"$name\"; + + /* fill in all the operations */ + ep_server.init_server = $name\__op_init_server; + + ep_server.interface_by_uuid = $name\__op_interface_by_uuid; + ep_server.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; + + 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 .= "\n"; + + foreach my $x (@{$ndr}) { + ParseInterface($x) if ($x->{TYPE} eq "INTERFACE" and not defined($x->{PROPERTIES}{object})); + } + + return $res; +} + +1; diff --git a/pidl/lib/Parse/Pidl/Samba4/Python.pm b/pidl/lib/Parse/Pidl/Samba4/Python.pm new file mode 100644 index 0000000000..74cec5a827 --- /dev/null +++ b/pidl/lib/Parse/Pidl/Samba4/Python.pm @@ -0,0 +1,1216 @@ +################################################### +# Python function wrapper generator +# Copyright jelmer@samba.org 2007-2008 +# released under the GNU GPL + +package Parse::Pidl::Samba4::Python; + +use Exporter; +@ISA = qw(Exporter); + +use strict; +use Parse::Pidl qw(warning fatal); +use Parse::Pidl::Typelist qw(hasType resolveType getType mapTypeName expandAlias); +use Parse::Pidl::Util qw(has_property ParseExpr unmake_str); +use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred is_charset_array); +use Parse::Pidl::CUtil qw(get_value_of get_pointer_to); +use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv); + +use vars qw($VERSION); +$VERSION = '0.01'; + +sub new($) { + my ($class) = @_; + my $self = { res => "", res_hdr => "", tabs => "", constants => {}, + module_methods => [], module_objects => [], ready_types => [], + readycode => [] }; + bless($self, $class); +} + +sub pidl_hdr ($$) +{ + my $self = shift; + $self->{res_hdr} .= shift; +} + +sub pidl($$) +{ + my ($self, $d) = @_; + if ($d) { + $self->{res} .= $self->{tabs}; + $self->{res} .= $d; + } + $self->{res} .= "\n"; +} + +sub indent($) +{ + my ($self) = @_; + $self->{tabs} .= "\t"; +} + +sub deindent($) +{ + my ($self) = @_; + $self->{tabs} = substr($self->{tabs}, 0, -1); +} + +sub Import +{ + my $self = shift; + my @imports = @_; + foreach (@imports) { + $_ = unmake_str($_); + s/\.idl$//; + $self->pidl_hdr("#include \"librpc/gen_ndr/py_$_\.h\"\n"); + } +} + +sub Const($$) +{ + my ($self, $const) = @_; + $self->register_constant($const->{NAME}, $const->{DTYPE}, $const->{VALUE}); +} + +sub register_constant($$$$) +{ + my ($self, $name, $type, $value) = @_; + + $self->{constants}->{$name} = [$type, $value]; +} + +sub EnumAndBitmapConsts($$$) +{ + my ($self, $name, $d) = @_; + + return unless (defined($d->{ELEMENTS})); + + foreach my $e (@{$d->{ELEMENTS}}) { + $e =~ /^([A-Za-z0-9_]+)/; + my $cname = $1; + + $self->register_constant($cname, $d, $cname); + } +} + +sub FromUnionToPythonFunction($$$$) +{ + my ($self, $mem_ctx, $type, $switch, $name) = @_; + + $self->pidl("PyObject *ret;"); + $self->pidl(""); + + $self->pidl("switch ($switch) {"); + $self->indent; + + foreach my $e (@{$type->{ELEMENTS}}) { + $self->pidl("$e->{CASE}:"); + + $self->indent; + + if ($e->{NAME}) { + $self->ConvertObjectToPython($mem_ctx, {}, $e, "$name->$e->{NAME}", "ret", "return NULL;"); + } else { + $self->pidl("ret = Py_None;"); + } + + $self->pidl("return ret;"); + $self->pidl(""); + + $self->deindent; + } + + $self->deindent; + $self->pidl("}"); + + $self->pidl("PyErr_SetString(PyExc_TypeError, \"unknown union level\");"); + $self->pidl("return NULL;"); +} + +sub FromPythonToUnionFunction($$$$$) +{ + my ($self, $type, $typename, $switch, $mem_ctx, $name) = @_; + + my $has_default = 0; + + $self->pidl("$typename *ret = talloc_zero($mem_ctx, $typename);"); + + $self->pidl("switch ($switch) {"); + $self->indent; + + foreach my $e (@{$type->{ELEMENTS}}) { + $self->pidl("$e->{CASE}:"); + if ($e->{CASE} eq "default") { $has_default = 1; } + $self->indent; + if ($e->{NAME}) { + $self->ConvertObjectFromPython({}, $mem_ctx, $e, $name, "ret->$e->{NAME}", "talloc_free(ret); return NULL;"); + } + $self->pidl("break;"); + $self->deindent; + $self->pidl(""); + } + + if (!$has_default) { + $self->pidl("default:"); + $self->indent; + $self->pidl("PyErr_SetString(PyExc_TypeError, \"invalid union level value\");"); + $self->pidl("talloc_free(ret);"); + $self->pidl("ret = NULL;"); + $self->deindent; + } + + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("return ret;"); +} + +sub PythonStruct($$$$$$) +{ + my ($self, $modulename, $prettyname, $name, $cname, $d) = @_; + + my $env = GenerateStructEnv($d, "object"); + + $self->pidl(""); + + my $getsetters = "NULL"; + + if ($#{$d->{ELEMENTS}} > -1) { + foreach my $e (@{$d->{ELEMENTS}}) { + my $varname = "object->$e->{NAME}"; + $self->pidl("static PyObject *py_$name\_get_$e->{NAME}(PyObject *obj, void *closure)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$cname *object = py_talloc_get_ptr(obj);"); + $self->pidl("PyObject *py_$e->{NAME};"); + $self->ConvertObjectToPython("py_talloc_get_mem_ctx(obj)", $env, $e, $varname, "py_$e->{NAME}", "return NULL;"); + $self->pidl("return py_$e->{NAME};"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static int py_$name\_set_$e->{NAME}(PyObject *py_obj, PyObject *value, void *closure)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$cname *object = py_talloc_get_ptr(py_obj);"); + my $mem_ctx = "py_talloc_get_mem_ctx(py_obj)"; + my $l = $e->{LEVELS}[0]; + my $nl = GetNextLevel($e, $l); + if ($l->{TYPE} eq "POINTER" and + not ($nl->{TYPE} eq "ARRAY" and ($nl->{IS_FIXED} or is_charset_array($e, $nl))) and + not ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE}))) { + $self->pidl("talloc_free($varname);"); + } + $self->ConvertObjectFromPython($env, $mem_ctx, $e, "value", $varname, "return -1;"); + $self->pidl("return 0;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + } + + $getsetters = "py_$name\_getsetters"; + $self->pidl("static PyGetSetDef ".$getsetters."[] = {"); + $self->indent; + foreach my $e (@{$d->{ELEMENTS}}) { + $self->pidl("{ discard_const_p(char, \"$e->{NAME}\"), py_$name\_get_$e->{NAME}, py_$name\_set_$e->{NAME} },"); + } + $self->pidl("{ NULL }"); + $self->deindent; + $self->pidl("};"); + $self->pidl(""); + } + + $self->pidl("static PyObject *py_$name\_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$cname *ret = talloc_zero(NULL, $cname);"); + $self->pidl("return py_talloc_import(&$name\_Type, ret);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + my $py_methods = "NULL"; + + # If the struct is not public there ndr_pull/ndr_push functions will + # be static so not callable from here + if (has_property($d, "public")) { + $self->pidl("static PyObject *py_$name\_ndr_pack(PyObject *py_obj)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$cname *object = py_talloc_get_ptr(py_obj);"); + $self->pidl("DATA_BLOB blob;"); + $self->pidl("enum ndr_err_code err;"); + $self->pidl("err = ndr_push_struct_blob(&blob, py_talloc_get_mem_ctx(py_obj), NULL, object, (ndr_push_flags_fn_t)ndr_push_$name);"); + $self->pidl("if (err != NDR_ERR_SUCCESS) {"); + $self->indent; + $self->pidl("PyErr_SetNdrError(err);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("return PyString_FromStringAndSize((char *)blob.data, blob.length);"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("static PyObject *py_$name\_ndr_unpack(PyObject *py_obj, PyObject *args)"); + $self->pidl("{"); + $self->indent; + $self->pidl("$cname *object = py_talloc_get_ptr(py_obj);"); + $self->pidl("DATA_BLOB blob;"); + $self->pidl("enum ndr_err_code err;"); + $self->pidl("if (!PyArg_ParseTuple(args, \"s#:__ndr_unpack__\", &blob.data, &blob.length))"); + $self->pidl("\treturn NULL;"); + $self->pidl(""); + $self->pidl("err = ndr_pull_struct_blob_all(&blob, py_talloc_get_mem_ctx(py_obj), NULL, object, (ndr_pull_flags_fn_t)ndr_pull_$name);"); + $self->pidl("if (err != NDR_ERR_SUCCESS) {"); + $self->indent; + $self->pidl("PyErr_SetNdrError(err);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("return Py_None;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $py_methods = "py_$name\_methods"; + $self->pidl("static PyMethodDef $py_methods\[] = {"); + $self->indent; + $self->pidl("{ \"__ndr_pack__\", (PyCFunction)py_$name\_ndr_pack, METH_NOARGS, \"S.pack() -> blob\\nNDR pack\" },"); + $self->pidl("{ \"__ndr_unpack__\", (PyCFunction)py_$name\_ndr_unpack, METH_VARARGS, \"S.unpack(blob) -> None\\nNDR unpack\" },"); + $self->pidl("{ NULL, NULL, 0, NULL }"); + $self->deindent; + $self->pidl("};"); + $self->pidl(""); + } + + $self->pidl_hdr("PyAPI_DATA(PyTypeObject) $name\_Type;\n"); + $self->pidl_hdr("#define $name\_Check(op) PyObject_TypeCheck(op, &$name\_Type)\n"); + $self->pidl_hdr("#define $name\_CheckExact(op) ((op)->ob_type == &$name\_Type)\n"); + $self->pidl_hdr("\n"); + my $docstring = ($self->DocString($d, $name) or "NULL"); + my $typeobject = "$name\_Type"; + $self->pidl("PyTypeObject $typeobject = {"); + $self->indent; + $self->pidl("PyObject_HEAD_INIT(NULL) 0,"); + $self->pidl(".tp_name = \"$modulename.$prettyname\","); + $self->pidl(".tp_basicsize = sizeof(py_talloc_Object),"); + $self->pidl(".tp_dealloc = py_talloc_dealloc,"); + $self->pidl(".tp_getset = $getsetters,"); + $self->pidl(".tp_repr = py_talloc_default_repr,"); + $self->pidl(".tp_doc = $docstring,"); + $self->pidl(".tp_methods = $py_methods,"); + $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); + $self->pidl(".tp_new = py_$name\_new,"); + $self->deindent; + $self->pidl("};"); + + $self->pidl(""); + + return "&$typeobject"; +} + +sub get_metadata_var($) +{ + my ($e) = @_; + sub get_var($) { my $x = shift; $x =~ s/\*//g; return $x; } + + if (has_property($e, "length_is")) { + return get_var($e->{PROPERTIES}->{length_is}); + } elsif (has_property($e, "size_is")) { + return get_var($e->{PROPERTIES}->{size_is}); + } + + return undef; +} + +sub find_metadata_args($) +{ + my ($fn) = @_; + my $metadata_args = { in => {}, out => {} }; + + # Determine arguments that are metadata for other arguments (size_is/length_is) + foreach my $e (@{$fn->{ELEMENTS}}) { + foreach my $dir (@{$e->{DIRECTION}}) { + my $main = get_metadata_var($e); + if ($main) { + $metadata_args->{$dir}->{$main} = $e->{NAME}; + } + } + } + + return $metadata_args; +} + +sub PythonFunctionUnpackOut($$$) +{ + my ($self, $fn, $fnname) = @_; + + my $outfnname = "unpack_$fnname\_args_out"; + my $signature = ""; + + my $metadata_args = find_metadata_args($fn); + + my $env = GenerateFunctionOutEnv($fn, "r->"); + my $result_size = 0; + + $self->pidl("static PyObject *$outfnname(struct $fn->{NAME} *r)"); + $self->pidl("{"); + $self->indent; + $self->pidl("PyObject *result = Py_None;"); + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/out/,@{$e->{DIRECTION}})); + next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or + ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}})); + $self->pidl("PyObject *py_$e->{NAME};"); + $result_size++; + } + + if ($fn->{RETURN_TYPE}) { + $result_size++ unless ($fn->{RETURN_TYPE} eq "WERROR" or $fn->{RETURN_TYPE} eq "NTSTATUS"); + } + + my $i = 0; + + if ($result_size > 1) { + $self->pidl("result = PyTuple_New($result_size);"); + $signature .= "("; + } elsif ($result_size == 0) { + $signature .= "None"; + } + + foreach my $e (@{$fn->{ELEMENTS}}) { + next if ($metadata_args->{out}->{$e->{NAME}}); + my $py_name = "py_$e->{NAME}"; + if (grep(/out/,@{$e->{DIRECTION}})) { + $self->ConvertObjectToPython("r", $env, $e, "r->out.$e->{NAME}", $py_name, "return NULL;"); + if ($result_size > 1) { + $self->pidl("PyTuple_SetItem(result, $i, $py_name);"); + $i++; + $signature .= "$e->{NAME}, "; + } else { + $self->pidl("result = $py_name;"); + $signature .= $e->{NAME}; + } + } + } + + if (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "NTSTATUS") { + $self->handle_ntstatus("r->out.result", "NULL", undef); + } elsif (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "WERROR") { + $self->handle_werror("r->out.result", "NULL", undef); + } elsif (defined($fn->{RETURN_TYPE})) { + my $conv = $self->ConvertObjectToPythonData("r", $fn->{RETURN_TYPE}, "r->out.result"); + if ($result_size > 1) { + $self->pidl("PyTuple_SetItem(result, $i, $conv);"); + } else { + $self->pidl("result = $conv;"); + } + $signature .= "result"; + } + + if (substr($signature, -2) eq ", ") { + $signature = substr($signature, 0, -2); + } + if ($result_size > 1) { + $signature .= ")"; + } + + $self->pidl("return result;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + return ($outfnname, $signature); +} + +sub PythonFunctionPackIn($$$) +{ + my ($self, $fn, $fnname) = @_; + my $metadata_args = find_metadata_args($fn); + + my $infnname = "pack_$fnname\_args_in"; + + $self->pidl("static bool $infnname(PyObject *args, PyObject *kwargs, struct $fn->{NAME} *r)"); + $self->pidl("{"); + $self->indent; + my $args_format = ""; + my $args_string = ""; + my $args_names = ""; + my $signature = ""; + + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/in/,@{$e->{DIRECTION}})); + next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or + ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}})); + $self->pidl("PyObject *py_$e->{NAME};"); + $args_format .= "O"; + $args_string .= ", &py_$e->{NAME}"; + $args_names .= "\"$e->{NAME}\", "; + $signature .= "$e->{NAME}, "; + } + if (substr($signature, -2) eq ", ") { + $signature = substr($signature, 0, -2); + } + $self->pidl("const char *kwnames[] = {"); + $self->indent; + $self->pidl($args_names . "NULL"); + $self->deindent; + $self->pidl("};"); + + $self->pidl(""); + $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"$args_format:$fn->{NAME}\", discard_const_p(char *, kwnames)$args_string)) {"); + $self->indent; + $self->pidl("return false;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + my $env = GenerateFunctionInEnv($fn, "r->"); + + my $fail = "return false;"; + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/in/,@{$e->{DIRECTION}})); + if ($metadata_args->{in}->{$e->{NAME}}) { + my $py_var = "py_".$metadata_args->{in}->{$e->{NAME}}; + $self->pidl("PY_CHECK_TYPE(PyList, $py_var, $fail);"); + my $val = "PyList_Size($py_var)"; + if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") { + $self->pidl("r->in.$e->{NAME} = talloc_ptrtype(r, r->in.$e->{NAME});"); + $self->pidl("*r->in.$e->{NAME} = $val;"); + } else { + $self->pidl("r->in.$e->{NAME} = $val;"); + } + } else { + $self->ConvertObjectFromPython($env, "r", $e, "py_$e->{NAME}", "r->in.$e->{NAME}", $fail); + } + } + $self->pidl("return true;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + return ($infnname, $signature); +} + +sub PythonFunction($$$) +{ + my ($self, $fn, $iface, $prettyname) = @_; + + my $fnname = "py_$fn->{NAME}"; + my $docstring = $self->DocString($fn, $fn->{NAME}); + + my ($insignature, $outsignature); + my ($infn, $outfn); + + if (has_property($fn, "todo")) { + unless ($docstring) { $docstring = "NULL"; } + $infn = "NULL"; + $outfn = "NULL"; + } else { + ($infn, $insignature) = $self->PythonFunctionPackIn($fn, $fnname); + ($outfn, $outsignature) = $self->PythonFunctionUnpackOut($fn, $fnname); + my $signature = "S.$prettyname($insignature) -> $outsignature"; + if ($docstring) { + $docstring = "\"$signature\\n\\n\"$docstring"; + } else { + $docstring = "\"$signature\""; + } + } + + return ($infn, $outfn, $docstring); +} + +sub handle_werror($$$$) +{ + my ($self, $var, $retval, $mem_ctx) = @_; + + $self->pidl("if (!W_ERROR_IS_OK($var)) {"); + $self->indent; + $self->pidl("PyErr_SetWERROR($var);"); + $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx); + $self->pidl("return $retval;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub handle_ntstatus($$$$) +{ + my ($self, $var, $retval, $mem_ctx) = @_; + + $self->pidl("if (NT_STATUS_IS_ERR($var)) {"); + $self->indent; + $self->pidl("PyErr_SetNTSTATUS($var);"); + $self->pidl("talloc_free($mem_ctx);") if ($mem_ctx); + $self->pidl("return $retval;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); +} + +sub PythonType($$$$) +{ + my ($self, $modulename, $d, $interface, $basename) = @_; + + my $actual_ctype = $d; + if ($actual_ctype->{TYPE} eq "TYPEDEF") { + $actual_ctype = $actual_ctype->{DATA}; + } + + if ($actual_ctype->{TYPE} eq "STRUCT") { + my $typeobject; + my $fn_name = $d->{NAME}; + + $fn_name =~ s/^$interface->{NAME}_//; + $fn_name =~ s/^$basename\_//; + + + if ($d->{TYPE} eq "STRUCT") { + $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d); + } else { + $typeobject = $self->PythonStruct($modulename, $fn_name, $d->{NAME}, mapTypeName($d), $d->{DATA}); + } + + $self->register_module_typeobject($fn_name, $typeobject); + } + + if ($d->{TYPE} eq "ENUM" or $d->{TYPE} eq "BITMAP") { + $self->EnumAndBitmapConsts($d->{NAME}, $d); + } + + if ($d->{TYPE} eq "TYPEDEF" and ($d->{DATA}->{TYPE} eq "ENUM" or $d->{DATA}->{TYPE} eq "BITMAP")) { + $self->EnumAndBitmapConsts($d->{NAME}, $d->{DATA}); + } + + if ($actual_ctype->{TYPE} eq "UNION" and defined($actual_ctype->{ELEMENTS})) { + $self->pidl("PyObject *py_import_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, " .mapTypeName($d) . " *in)"); + $self->pidl("{"); + $self->indent; + $self->FromUnionToPythonFunction("mem_ctx", $actual_ctype, "level", "in") if ($actual_ctype->{TYPE} eq "UNION"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl(mapTypeName($d) . " *py_export_$d->{NAME}(TALLOC_CTX *mem_ctx, int level, PyObject *in)"); + $self->pidl("{"); + $self->indent; + $self->FromPythonToUnionFunction($actual_ctype, mapTypeName($d), "level", "mem_ctx", "in") if ($actual_ctype->{TYPE} eq "UNION"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + } +} + +sub DocString($$$) +{ + my ($self, $d, $name) = @_; + if (has_property($d, "helpstring")) { + my $docstring = uc("py_doc_$name"); + $self->pidl("#define $docstring ".has_property($d, "helpstring")); + return $docstring; + } + + return undef; +} + +sub Interface($$$) +{ + my($self,$interface,$basename) = @_; + + $self->pidl_hdr("#ifndef _HEADER_PYTHON_$interface->{NAME}\n"); + $self->pidl_hdr("#define _HEADER_PYTHON_$interface->{NAME}\n\n"); + + $self->pidl_hdr("\n"); + + $self->Const($_) foreach (@{$interface->{CONSTS}}); + + foreach my $d (@{$interface->{TYPES}}) { + next if has_property($d, "nopython"); + + $self->PythonType($basename, $d, $interface, $basename); + } + + if (defined $interface->{PROPERTIES}->{uuid}) { + $self->pidl_hdr("PyAPI_DATA(PyTypeObject) $interface->{NAME}_InterfaceType;\n"); + $self->pidl(""); + + my @fns = (); + + foreach my $d (@{$interface->{FUNCTIONS}}) { + next if not defined($d->{OPNUM}); + next if has_property($d, "nopython"); + + my $prettyname = $d->{NAME}; + + $prettyname =~ s/^$interface->{NAME}_//; + $prettyname =~ s/^$basename\_//; + + my ($infn, $outfn, $fndocstring) = $self->PythonFunction($d, $interface->{NAME}, $prettyname); + + push (@fns, [$infn, $outfn, "dcerpc_$d->{NAME}", $prettyname, $fndocstring, $d->{OPNUM}]); + } + + $self->pidl("const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[] = {"); + $self->pidl_hdr("extern const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[];"); + $self->indent; + foreach my $d (@fns) { + my ($infn, $outfn, $callfn, $prettyname, $docstring, $opnum) = @$d; + $self->pidl("{ \"$prettyname\", $docstring, (dcerpc_call_fn)$callfn, (py_data_pack_fn)$infn, (py_data_unpack_fn)$outfn, $opnum, &ndr_table_$interface->{NAME} },"); + } + $self->pidl("{ NULL }"); + $self->deindent; + $self->pidl("};"); + $self->pidl(""); + + $self->pidl("static PyObject *interface_$interface->{NAME}_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)"); + $self->pidl("{"); + $self->indent; + $self->pidl("dcerpc_InterfaceObject *ret;"); + $self->pidl("const char *binding_string;"); + $self->pidl("struct cli_credentials *credentials;"); + $self->pidl("struct loadparm_context *lp_ctx = NULL;"); + $self->pidl("PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None, *py_basis = Py_None;"); + $self->pidl("TALLOC_CTX *mem_ctx = NULL;"); + $self->pidl("struct event_context *event_ctx;"); + $self->pidl("NTSTATUS status;"); + $self->pidl(""); + $self->pidl("const char *kwnames[] = {"); + $self->indent; + $self->pidl("\"binding\", \"lp_ctx\", \"credentials\", \"basis_connection\", NULL"); + $self->deindent; + $self->pidl("};"); + $self->pidl("extern struct loadparm_context *lp_from_py_object(PyObject *py_obj);"); + $self->pidl("extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);"); + $self->pidl(""); + $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"s|OOO:$interface->{NAME}\", discard_const_p(char *, kwnames), &binding_string, &py_lp_ctx, &py_credentials, &py_basis)) {"); + $self->indent; + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("lp_ctx = lp_from_py_object(py_lp_ctx);"); + $self->pidl("if (lp_ctx == NULL) {"); + $self->indent; + $self->pidl("PyErr_SetString(PyExc_TypeError, \"Expected loadparm context\");"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + $self->pidl("credentials = cli_credentials_from_py_object(py_credentials);"); + $self->pidl("if (credentials == NULL) {"); + $self->indent; + $self->pidl("PyErr_SetString(PyExc_TypeError, \"Expected credentials\");"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + + $self->pidl("ret = PyObject_New(dcerpc_InterfaceObject, &$interface->{NAME}_InterfaceType);"); + $self->pidl(""); + $self->pidl("event_ctx = event_context_init(mem_ctx);"); + $self->pidl(""); + + $self->pidl("if (py_basis != Py_None) {"); + $self->indent; + $self->pidl("struct dcerpc_pipe *base_pipe;"); + $self->pidl(""); + $self->pidl("if (!PyObject_TypeCheck(py_basis, &dcerpc_InterfaceType)) {"); + $self->indent; + $self->pidl("PyErr_SetString(PyExc_ValueError, \"basis_connection must be a DCE/RPC connection\");"); + $self->pidl("talloc_free(mem_ctx);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("base_pipe = ((dcerpc_InterfaceObject *)py_basis)->pipe;"); + $self->pidl(""); + $self->pidl("status = dcerpc_secondary_context(base_pipe, &ret->pipe, &ndr_table_$interface->{NAME});"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + $self->pidl("status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, "); + $self->pidl(" &ndr_table_$interface->{NAME}, credentials, event_ctx, lp_ctx);"); + $self->deindent; + $self->pidl("}"); + $self->handle_ntstatus("status", "NULL", "mem_ctx"); + + $self->pidl("ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;"); + + $self->pidl("return (PyObject *)ret;"); + $self->deindent; + $self->pidl("}"); + + $self->pidl(""); + + my $signature = +"\"$interface->{NAME}(binding, lp_ctx=None, credentials=None) -> connection\\n\" +\"\\n\" +\"binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\\n\" +\"lp_ctx should be a path to a smb.conf file or a param.LoadParm object\\n\" +\"credentials should be a credentials.Credentials object.\\n\\n\""; + + my $docstring = $self->DocString($interface, $interface->{NAME}); + + if ($docstring) { + $docstring = "$signature$docstring"; + } else { + $docstring = $signature; + } + + $self->pidl("PyTypeObject $interface->{NAME}_InterfaceType = {"); + $self->indent; + $self->pidl("PyObject_HEAD_INIT(NULL) 0,"); + $self->pidl(".tp_name = \"$basename.$interface->{NAME}\","); + $self->pidl(".tp_basicsize = sizeof(dcerpc_InterfaceObject),"); + $self->pidl(".tp_base = &dcerpc_InterfaceType,"); + $self->pidl(".tp_doc = $docstring,"); + $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); + $self->pidl(".tp_new = interface_$interface->{NAME}_new,"); + $self->deindent; + $self->pidl("};"); + + $self->pidl(""); + + $self->register_module_typeobject($interface->{NAME}, "&$interface->{NAME}_InterfaceType"); + $self->register_module_readycode(["if (!PyInterface_AddNdrRpcMethods(&$interface->{NAME}_InterfaceType, py_ndr_$interface->{NAME}\_methods))", "\treturn;", ""]); + } + + $self->pidl_hdr("\n"); + $self->pidl_hdr("#endif /* _HEADER_NDR_$interface->{NAME} */\n"); +} + +sub register_module_method($$$$$) +{ + my ($self, $fn_name, $pyfn_name, $flags, $doc) = @_; + + push (@{$self->{module_methods}}, [$fn_name, $pyfn_name, $flags, $doc]) +} + +sub register_module_typeobject($$$) +{ + my ($self, $name, $py_name) = @_; + + $self->register_module_object($name, "(PyObject *)$py_name"); + + $self->check_ready_type($py_name); +} + +sub check_ready_type($$) +{ + my ($self, $py_name) = @_; + push (@{$self->{ready_types}}, $py_name) unless (grep(/^$py_name$/,@{$self->{ready_types}})); +} + +sub register_module_readycode($$) +{ + my ($self, $code) = @_; + + push (@{$self->{readycode}}, @$code); +} + +sub register_module_object($$$) +{ + my ($self, $name, $py_name) = @_; + + push (@{$self->{module_objects}}, [$name, $py_name]) +} + +sub assign($$$) +{ + my ($self, $dest, $src) = @_; + if ($dest =~ /^\&/) { + $self->pidl("memcpy($dest, $src, sizeof(" . get_value_of($dest) . "));"); + } else { + $self->pidl("$dest = $src;"); + } +} + +sub ConvertObjectFromPythonData($$$$$$) +{ + my ($self, $mem_ctx, $cvar, $ctype, $target, $fail) = @_; + + die("undef type for $cvar") unless(defined($ctype)); + + $ctype = resolveType($ctype); + + my $actual_ctype = $ctype; + if ($ctype->{TYPE} eq "TYPEDEF") { + $actual_ctype = $ctype->{DATA}; + } + + if ($actual_ctype->{TYPE} eq "ENUM" or $actual_ctype->{TYPE} eq "BITMAP" or + $actual_ctype->{TYPE} eq "SCALAR" and ( + expandAlias($actual_ctype->{NAME}) =~ /^(u?int[0-9]*|hyper|NTTIME|time_t|NTTIME_hyper|NTTIME_1sec|dlong|udlong|udlongr)$/)) { + $self->pidl("PY_CHECK_TYPE(PyInt, $cvar, $fail);"); + $self->pidl("$target = PyInt_AsLong($cvar);"); + return; + } + + if ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") { + $self->pidl("PY_CHECK_TYPE($ctype->{NAME}, $cvar, $fail);"); + $self->assign($target, "py_talloc_get_ptr($cvar)"); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "DATA_BLOB") { + $self->pidl("$target = data_blob_talloc($mem_ctx, PyString_AsString($cvar), PyString_Size($cvar));"); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and + ($actual_ctype->{NAME} eq "string" or $actual_ctype->{NAME} eq "nbt_string" or $actual_ctype->{NAME} eq "nbt_name" or $actual_ctype->{NAME} eq "wrepl_nbt_name")) { + $self->pidl("$target = talloc_strdup($mem_ctx, PyString_AsString($cvar));"); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "ipv4address") { + $self->pidl("$target = PyString_AsString($cvar);"); + return; + } + + + if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "NTSTATUS") { + $self->pidl("$target = NT_STATUS(PyInt_AsLong($cvar));"); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "WERROR") { + $self->pidl("$target = W_ERROR(PyInt_AsLong($cvar));"); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "string_array") { + $self->pidl("$target = PyCObject_AsVoidPtr($cvar);"); + return; + } + + if ($actual_ctype->{TYPE} eq "SCALAR" and $actual_ctype->{NAME} eq "pointer") { + $self->assign($target, "PyCObject_AsVoidPtr($cvar)"); + return; + } + + fatal($ctype, "unknown type $actual_ctype->{TYPE} for ".mapTypeName($ctype) . ": $cvar"); + +} + +sub ConvertObjectFromPythonLevel($$$$$$$$) +{ + my ($self, $env, $mem_ctx, $py_var, $e, $l, $var_name, $fail) = @_; + my $nl = GetNextLevel($e, $l); + + if ($l->{TYPE} eq "POINTER") { + if ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) { + $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, $var_name, $fail); + return; + } + if ($l->{POINTER_TYPE} ne "ref") { + $self->pidl("if ($py_var == Py_None) {"); + $self->indent; + $self->pidl("$var_name = NULL;"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + } + $self->pidl("$var_name = talloc_ptrtype($mem_ctx, $var_name);"); + $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, $nl, get_value_of($var_name), $fail); + if ($l->{POINTER_TYPE} ne "ref") { + $self->deindent; + $self->pidl("}"); + } + } elsif ($l->{TYPE} eq "ARRAY") { + my $pl = GetPrevLevel($e, $l); + if ($pl && $pl->{TYPE} eq "POINTER") { + $var_name = get_pointer_to($var_name); + } + + if (is_charset_array($e, $l)) { + $self->pidl("PY_CHECK_TYPE(PyUnicode, $py_var, $fail);"); + # FIXME: Use Unix charset setting rather than utf-8 + $self->pidl($var_name . " = PyString_AsString(PyUnicode_AsEncodedString($py_var, \"utf-8\", \"ignore\"));"); + } else { + my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}"; + $self->pidl("PY_CHECK_TYPE(PyList, $py_var, $fail);"); + $self->pidl("{"); + $self->indent; + $self->pidl("int $counter;"); + if (!$l->{IS_FIXED}) { + $self->pidl("$var_name = talloc_array_ptrtype($mem_ctx, $var_name, PyList_Size($py_var));"); + } + $self->pidl("for ($counter = 0; $counter < PyList_Size($py_var); $counter++) {"); + $self->indent; + $self->ConvertObjectFromPythonLevel($env, $var_name, "PyList_GetItem($py_var, $counter)", $e, GetNextLevel($e, $l), $var_name."[$counter]", $fail); + $self->deindent; + $self->pidl("}"); + $self->deindent; + $self->pidl("}"); + } + } elsif ($l->{TYPE} eq "DATA") { + + if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) { + $var_name = get_pointer_to($var_name); + } + $self->ConvertObjectFromPythonData($mem_ctx, $py_var, $l->{DATA_TYPE}, $var_name, $fail); + } elsif ($l->{TYPE} eq "SWITCH") { + $var_name = get_pointer_to($var_name); + my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e); + $self->assign($var_name, "py_export_" . GetNextLevel($e, $l)->{DATA_TYPE} . "($mem_ctx, $switch, $py_var)"); + } elsif ($l->{TYPE} eq "SUBCONTEXT") { + $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $py_var, $e, GetNextLevel($e, $l), $var_name, $fail); + } else { + die("unknown level type $l->{TYPE}"); + } +} + +sub ConvertObjectFromPython($$$$$$$) +{ + my ($self, $env, $mem_ctx, $ctype, $cvar, $target, $fail) = @_; + + $self->ConvertObjectFromPythonLevel($env, $mem_ctx, $cvar, $ctype, $ctype->{LEVELS}[0], $target, $fail); +} + +sub ConvertScalarToPython($$$) +{ + my ($self, $ctypename, $cvar) = @_; + + die("expected string for $cvar, not $ctypename") if (ref($ctypename) eq "HASH"); + + $ctypename = expandAlias($ctypename); + + if ($ctypename =~ /^(char|u?int[0-9]*|hyper|dlong|udlong|udlongr|time_t|NTTIME_hyper|NTTIME|NTTIME_1sec)$/) { + return "PyInt_FromLong($cvar)"; + } + + if ($ctypename eq "DATA_BLOB") { + return "PyString_FromStringAndSize((char *)($cvar).data, ($cvar).length)"; + } + + if ($ctypename eq "NTSTATUS") { + return "PyErr_FromNTSTATUS($cvar)"; + } + + if ($ctypename eq "WERROR") { + return "PyErr_FromWERROR($cvar)"; + } + + if (($ctypename eq "string" or $ctypename eq "nbt_string" or $ctypename eq "nbt_name" or $ctypename eq "wrepl_nbt_name")) { + return "PyString_FromString($cvar)"; + } + + # Not yet supported + if ($ctypename eq "string_array") { return "PyCObject_FromVoidPtr($cvar)"; } + if ($ctypename eq "ipv4address") { return "PyString_FromString($cvar)"; } + if ($ctypename eq "pointer") { + return "PyCObject_FromVoidPtr($cvar, talloc_free)"; + } + + die("Unknown scalar type $ctypename"); +} + +sub ConvertObjectToPythonData($$$$$) +{ + my ($self, $mem_ctx, $ctype, $cvar) = @_; + + die("undef type for $cvar") unless(defined($ctype)); + + $ctype = resolveType($ctype); + + my $actual_ctype = $ctype; + if ($ctype->{TYPE} eq "TYPEDEF") { + $actual_ctype = $ctype->{DATA}; + } + + if ($actual_ctype->{TYPE} eq "ENUM") { + return $self->ConvertScalarToPython(Parse::Pidl::Typelist::enum_type_fn($actual_ctype), $cvar); + } elsif ($actual_ctype->{TYPE} eq "BITMAP") { + return $self->ConvertScalarToPython(Parse::Pidl::Typelist::bitmap_type_fn($actual_ctype), $cvar); + } elsif ($actual_ctype->{TYPE} eq "SCALAR") { + return $self->ConvertScalarToPython($actual_ctype->{NAME}, $cvar); + } elsif ($actual_ctype->{TYPE} eq "UNION") { + fatal($ctype, "union without discriminant: " . mapTypeName($ctype) . ": $cvar"); + } elsif ($actual_ctype->{TYPE} eq "STRUCT" or $actual_ctype->{TYPE} eq "INTERFACE") { + return "py_talloc_import_ex(&$ctype->{NAME}_Type, $mem_ctx, $cvar)"; + } + + fatal($ctype, "unknown type $actual_ctype->{TYPE} for ".mapTypeName($ctype) . ": $cvar"); +} + +sub fail_on_null($$$) +{ + my ($self, $var, $fail) = @_; + $self->pidl("if ($var == NULL) {"); + $self->indent; + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); +} + +sub ConvertObjectToPythonLevel($$$$$$) +{ + my ($self, $mem_ctx, $env, $e, $l, $var_name, $py_var, $fail) = @_; + my $nl = GetNextLevel($e, $l); + + if ($l->{TYPE} eq "POINTER") { + if ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) { + $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, $var_name, $py_var, $fail); + return; + } + if ($l->{POINTER_TYPE} ne "ref") { + $self->pidl("if ($var_name == NULL) {"); + $self->indent; + $self->pidl("$py_var = Py_None;"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; + } + $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, get_value_of($var_name), $py_var, $fail); + if ($l->{POINTER_TYPE} ne "ref") { + $self->deindent; + $self->pidl("}"); + } + } elsif ($l->{TYPE} eq "ARRAY") { + my $pl = GetPrevLevel($e, $l); + if ($pl && $pl->{TYPE} eq "POINTER") { + $var_name = get_pointer_to($var_name); + } + + if (is_charset_array($e, $l)) { + # FIXME: Use Unix charset setting rather than utf-8 + $self->pidl("$py_var = PyUnicode_Decode($var_name, strlen($var_name), \"utf-8\", \"ignore\");"); + } else { + die("No SIZE_IS for array $var_name") unless (defined($l->{SIZE_IS})); + my $length = $l->{SIZE_IS}; + if (defined($l->{LENGTH_IS})) { + $length = $l->{LENGTH_IS}; + } + + $length = ParseExpr($length, $env, $e); + $self->pidl("$py_var = PyList_New($length);"); + $self->fail_on_null($py_var, $fail); + $self->pidl("{"); + $self->indent; + my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}"; + $self->pidl("int $counter;"); + $self->pidl("for ($counter = 0; $counter < $length; $counter++) {"); + $self->indent; + my $member_var = "py_$e->{NAME}_$l->{LEVEL_INDEX}"; + $self->pidl("PyObject *$member_var;"); + $self->ConvertObjectToPythonLevel($var_name, $env, $e, GetNextLevel($e, $l), $var_name."[$counter]", $member_var, $fail); + $self->pidl("PyList_SetItem($py_var, $counter, $member_var);"); + $self->deindent; + $self->pidl("}"); + $self->deindent; + $self->pidl("}"); + } + } elsif ($l->{TYPE} eq "SWITCH") { + $var_name = get_pointer_to($var_name); + my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e); + $self->pidl("$py_var = py_import_" . GetNextLevel($e, $l)->{DATA_TYPE} . "($mem_ctx, $switch, $var_name);"); + $self->fail_on_null($py_var, $fail); + + } elsif ($l->{TYPE} eq "DATA") { + if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) { + $var_name = get_pointer_to($var_name); + } + my $conv = $self->ConvertObjectToPythonData($mem_ctx, $l->{DATA_TYPE}, $var_name); + $self->pidl("$py_var = $conv;"); + } elsif ($l->{TYPE} eq "SUBCONTEXT") { + $self->ConvertObjectToPythonLevel($mem_ctx, $env, $e, GetNextLevel($e, $l), $var_name, $py_var, $fail); + } else { + die("Unknown level type $l->{TYPE} $var_name"); + } +} + +sub ConvertObjectToPython($$$$$$) +{ + my ($self, $mem_ctx, $env, $ctype, $cvar, $py_var, $fail) = @_; + + $self->ConvertObjectToPythonLevel($mem_ctx, $env, $ctype, $ctype->{LEVELS}[0], $cvar, $py_var, $fail); +} + +sub Parse($$$$$) +{ + my($self,$basename,$ndr,$ndr_hdr,$hdr) = @_; + + my $py_hdr = $hdr; + $py_hdr =~ s/ndr_([^\/]+)$/py_$1/g; + + $self->pidl_hdr("/* header auto-generated by pidl */\n\n"); + + $self->pidl(" +/* Python wrapper functions auto-generated by pidl */ +#include \"includes.h\" +#include <Python.h> +#include \"librpc/rpc/dcerpc.h\" +#include \"scripting/python/pytalloc.h\" +#include \"librpc/rpc/pyrpc.h\" +#include \"lib/events/events.h\" +#include \"$hdr\" +#include \"$ndr_hdr\" +#include \"$py_hdr\" + +"); + + foreach my $x (@$ndr) { + ($x->{TYPE} eq "IMPORT") && $self->Import(@{$x->{PATHS}}); + ($x->{TYPE} eq "INTERFACE") && $self->Interface($x, $basename); + } + + $self->pidl("static PyMethodDef $basename\_methods[] = {"); + $self->indent; + foreach (@{$self->{module_methods}}) { + my ($fn_name, $pyfn_name, $flags, $doc) = @$_; + $self->pidl("{ \"$fn_name\", (PyCFunction)$pyfn_name, $flags, $doc },"); + } + + $self->pidl("{ NULL, NULL, 0, NULL }"); + $self->deindent; + $self->pidl("};"); + + $self->pidl(""); + + $self->pidl("void init$basename(void)"); + $self->pidl("{"); + $self->indent; + $self->pidl("PyObject *m;"); + $self->pidl(""); + + foreach (@{$self->{ready_types}}) { + $self->pidl("if (PyType_Ready($_) < 0)"); + $self->pidl("\treturn;"); + } + + $self->pidl($_) foreach (@{$self->{readycode}}); + + $self->pidl(""); + + $self->pidl("m = Py_InitModule3(\"$basename\", $basename\_methods, \"$basename DCE/RPC\");"); + $self->pidl("if (m == NULL)"); + $self->pidl("\treturn;"); + $self->pidl(""); + foreach my $name (keys %{$self->{constants}}) { + my $py_obj; + my ($ctype, $cvar) = @{$self->{constants}->{$name}}; + if ($cvar =~ /^[0-9]+$/ or $cvar =~ /^0x[0-9a-fA-F]+$/) { + $py_obj = "PyInt_FromLong($cvar)"; + } elsif ($cvar =~ /^".*"$/) { + $py_obj = "PyString_FromString($cvar)"; + } else { + $py_obj = $self->ConvertObjectToPythonData("NULL", expandAlias($ctype), $cvar); + } + + $self->pidl("PyModule_AddObject(m, \"$name\", $py_obj);"); + } + + foreach (@{$self->{module_objects}}) { + my ($object_name, $c_name) = @$_; + $self->pidl("Py_INCREF($c_name);"); + $self->pidl("PyModule_AddObject(m, \"$object_name\", $c_name);"); + } + + $self->deindent; + $self->pidl("}"); + return ($self->{res_hdr}, $self->{res}); +} + +1; diff --git a/pidl/lib/Parse/Pidl/Samba4/SWIG.pm b/pidl/lib/Parse/Pidl/Samba4/SWIG.pm new file mode 100644 index 0000000000..14424cf260 --- /dev/null +++ b/pidl/lib/Parse/Pidl/Samba4/SWIG.pm @@ -0,0 +1,177 @@ +################################################### +# Samba4 parser generator for swig wrappers +# Copyright tpot@samba.org 2004,2005 +# Copyright jelmer@samba.org 2006 +# released under the GNU GPL + +package Parse::Pidl::Samba4::SWIG; + +use vars qw($VERSION); +use Parse::Pidl::Samba4 qw(DeclLong); +use Parse::Pidl::Typelist qw(mapTypeName); +use Parse::Pidl::Util qw(has_property); +$VERSION = '0.01'; + +use strict; + +my $ret = ""; +my $tabs = ""; + +sub pidl($) +{ + my $p = shift; + $ret .= $tabs. $p . "\n"; +} + +sub indent() { $tabs.=" "; } +sub deindent() { $tabs = substr($tabs,0,-2); } + +sub IgnoreInterface($$) +{ + my ($basename,$if) = @_; + + foreach (@{$if->{TYPES}}) { + next unless (has_property($_, "public")); + pidl "\%types($_->{NAME});"; + } +} + +sub GenerateResultTypemap($) +{ + my $name = shift; + pidl "%typemap(in,numinputs=0) $name*result ($name tmp) {"; + indent; + pidl "\$1 = &tmp;"; + deindent; + pidl "}"; + pidl ""; + pidl "%typemap(argout) $name*result {"; + indent; + pidl "\$result = SWIG_NewPointerObj(*\$1, \$1_descriptor, 0);"; + deindent; + pidl "}"; +} + +sub ParseInterface($$) +{ + my ($basename,$if) = @_; + + pidl "\%inline {"; + pidl "typedef struct $if->{NAME} { struct dcerpc_pipe *pipe; } $if->{NAME};"; + pidl "}"; + pidl ""; + pidl "%talloctype($if->{NAME});"; + pidl ""; + pidl "\%extend $if->{NAME} {"; + indent(); + pidl "$if->{NAME} () {"; + indent; + pidl "return talloc(NULL, struct $if->{NAME});"; + deindent; + pidl "}"; + pidl ""; + pidl "NTSTATUS connect (const char *binding, struct cli_credentials *cred, struct event_context *event)"; + pidl "{"; + indent; + pidl "return dcerpc_pipe_connect(\$self, &\$self->pipe, binding, &ndr_table_$if->{NAME}, cred, event);"; + deindent; + pidl "}"; + pidl ""; + + foreach my $fn (@{$if->{FUNCTIONS}}) { + pidl "/* $fn->{NAME} */"; + my $args = ""; + foreach (@{$fn->{ELEMENTS}}) { + $args .= DeclLong($_) . ", "; + } + my $name = $fn->{NAME}; + $name =~ s/^$if->{NAME}_//g; + $name =~ s/^$basename\_//g; + $args .= "TALLOC_CTX *mem_ctx, " . mapTypeName($fn->{RETURN_TYPE}) . " *result"; + pidl "NTSTATUS $name($args)"; + pidl "{"; + indent; + pidl "struct $fn->{NAME} r;"; + pidl "NTSTATUS status;"; + pidl ""; + pidl "/* Fill r structure */"; + + foreach (@{$fn->{ELEMENTS}}) { + if (grep(/in/, @{$_->{DIRECTION}})) { + pidl "r.in.$_->{NAME} = $_->{NAME};"; + } + } + + pidl ""; + pidl "status = dcerpc_$fn->{NAME}(\$self->pipe, mem_ctx, &r);"; + pidl "if (NT_STATUS_IS_ERR(status)) {"; + indent; pidl "return status;"; deindent; + pidl "}"; + pidl ""; + pidl "/* Set out arguments */"; + foreach (@{$fn->{ELEMENTS}}) { + next unless (grep(/out/, @{$_->{DIRECTION}})); + + pidl ("/* FIXME: $_->{NAME} [out] argument is not a pointer */") if ($_->{LEVELS}[0]->{TYPE} ne "POINTER"); + + pidl "*$_->{NAME} = *r.out.$_->{NAME};"; + } + + if (defined($fn->{RETURN_TYPE})) { + pidl "*result = r.out.result;"; + } + pidl "return NT_STATUS_OK;"; + deindent; + pidl "}"; + pidl ""; + } + + deindent(); + pidl "};"; + pidl ""; + + foreach (@{$if->{TYPES}}) { + pidl "/* $_->{NAME} */"; + } + + pidl ""; +} + +sub Parse($$$$) +{ + my($ndr,$basename,$header,$gen_header) = @_; + + $ret = ""; + + pidl "/* This file is autogenerated by pidl. DO NOT EDIT */"; + + pidl "\%module $basename"; + + pidl ""; + + pidl "\%{"; + pidl "#include \"includes.h\""; + pidl "#include \"$header\""; + pidl "#include \"$gen_header\""; + pidl "%}"; + pidl "\%import \"../rpc/dcerpc.i\""; + pidl "\%import \"../../libcli/util/errors.i\""; + pidl "\%import \"../../lib/talloc/talloc.i\""; + pidl ""; + foreach (@$ndr) { + IgnoreInterface($basename, $_) if ($_->{TYPE} eq "INTERFACE"); + } + pidl ""; + + pidl ""; + + foreach (@$ndr) { + ParseInterface($basename, $_) if ($_->{TYPE} eq "INTERFACE"); + } + #FIXME: Foreach ref pointer, set NONNULL + #FIXME: Foreach unique/full pointer, set MAYBENULL + #FIXME: Foreach [out] parameter, set OUTPARAM + return $ret; +} + +1; diff --git a/pidl/lib/Parse/Pidl/Samba4/TDR.pm b/pidl/lib/Parse/Pidl/Samba4/TDR.pm new file mode 100644 index 0000000000..568dff5adf --- /dev/null +++ b/pidl/lib/Parse/Pidl/Samba4/TDR.pm @@ -0,0 +1,281 @@ +################################################### +# Trivial Parser Generator +# Copyright jelmer@samba.org 2005-2007 +# released under the GNU GPL + +package Parse::Pidl::Samba4::TDR; +use Parse::Pidl qw(fatal); +use Parse::Pidl::Util qw(has_property ParseExpr is_constant); +use Parse::Pidl::Samba4 qw(is_intree choose_header); + +use Exporter; +@ISA = qw(Exporter); +@EXPORT_OK = qw(ParserType $ret $ret_hdr); + +use vars qw($VERSION); +$VERSION = '0.01'; + +use strict; + +sub new($) { + my ($class) = shift; + my $self = { ret => "", ret_hdr => "", tabs => "" }; + bless($self, $class); +} + +sub indent($) { my $self = shift; $self->{tabs}.="\t"; } +sub deindent($) { my $self = shift; $self->{tabs} = substr($self->{tabs}, 1); } +sub pidl($$) { my $self = shift; $self->{ret} .= $self->{tabs}.(shift)."\n"; } +sub pidl_hdr($$) { my $self = shift; $self->{ret_hdr} .= (shift)."\n"; } +sub typearg($) { + my $t = shift; + return(", const char *name") if ($t eq "print"); + return(", TALLOC_CTX *mem_ctx") if ($t eq "pull"); + return(""); +} + +sub fn_declare($$$) +{ + my ($self, $p, $d) = @_; + if ($p) { + $self->pidl($d); $self->pidl_hdr("$d;"); + } else { + $self->pidl("static $d"); + } +} + +sub ContainsArray($) +{ + my $e = shift; + foreach (@{$e->{ELEMENTS}}) { + next if (has_property($_, "charset") and + scalar(@{$_->{ARRAY_LEN}}) == 1); + return 1 if (defined($_->{ARRAY_LEN}) and + scalar(@{$_->{ARRAY_LEN}}) > 0); + } + return 0; +} + +sub ParserElement($$$$) +{ + my ($self, $e,$t,$env) = @_; + my $switch = ""; + my $array = ""; + my $name = ""; + my $mem_ctx = "mem_ctx"; + + fatal($e,"Pointers not supported in TDR") if ($e->{POINTERS} > 0); + fatal($e,"size_is() not supported in TDR") if (has_property($e, "size_is")); + fatal($e,"length_is() not supported in TDR") if (has_property($e, "length_is")); + + if ($t eq "print") { + $name = ", \"$e->{NAME}\"$array"; + } + + if (has_property($e, "flag")) { + $self->pidl("{"); + $self->indent; + $self->pidl("uint32_t saved_flags = tdr->flags;"); + $self->pidl("tdr->flags |= $e->{PROPERTIES}->{flag};"); + } + + if (has_property($e, "charset")) { + fatal($e,"charset() on non-array element") unless (defined($e->{ARRAY_LEN}) and scalar(@{$e->{ARRAY_LEN}}) > 0); + + my $len = ParseExpr(@{$e->{ARRAY_LEN}}[0], $env, $e); + if ($len eq "*") { $len = "-1"; } + $name = ", mem_ctx" if ($t eq "pull"); + $self->pidl("TDR_CHECK(tdr_$t\_charset(tdr$name, &v->$e->{NAME}, $len, sizeof($e->{TYPE}_t), CH_$e->{PROPERTIES}->{charset}));"); + return; + } + + if (has_property($e, "switch_is")) { + $switch = ", " . ParseExpr($e->{PROPERTIES}->{switch_is}, $env, $e); + } + + if (defined($e->{ARRAY_LEN}) and scalar(@{$e->{ARRAY_LEN}}) > 0) { + my $len = ParseExpr($e->{ARRAY_LEN}[0], $env, $e); + + if ($t eq "pull" and not is_constant($len)) { + $self->pidl("TDR_ALLOC(mem_ctx, v->$e->{NAME}, $len);"); + $mem_ctx = "v->$e->{NAME}"; + } + + $self->pidl("for (i = 0; i < $len; i++) {"); + $self->indent; + $array = "[i]"; + } + + if ($t eq "pull") { + $name = ", $mem_ctx"; + } + + if (has_property($e, "value") && $t eq "push") { + $self->pidl("v->$e->{NAME} = ".ParseExpr($e->{PROPERTIES}->{value}, $env, $e).";"); + } + + $self->pidl("TDR_CHECK(tdr_$t\_$e->{TYPE}(tdr$name$switch, &v->$e->{NAME}$array));"); + + if ($array) { $self->deindent; $self->pidl("}"); } + + if (has_property($e, "flag")) { + $self->pidl("tdr->flags = saved_flags;"); + $self->deindent; + $self->pidl("}"); + } +} + +sub ParserStruct($$$$$) +{ + my ($self, $e,$t,$p) = @_; + + $self->fn_declare($p,"NTSTATUS tdr_$t\_$e->{NAME} (struct tdr_$t *tdr".typearg($t).", struct $e->{NAME} *v)"); + $self->pidl("{"); $self->indent; + $self->pidl("int i;") if (ContainsArray($e)); + + if ($t eq "print") { + $self->pidl("tdr->print(tdr, \"\%-25s: struct $e->{NAME}\", name);"); + $self->pidl("tdr->level++;"); + } + + my %env = map { $_->{NAME} => "v->$_->{NAME}" } @{$e->{ELEMENTS}}; + $env{"this"} = "v"; + $self->ParserElement($_, $t, \%env) foreach (@{$e->{ELEMENTS}}); + + if ($t eq "print") { + $self->pidl("tdr->level--;"); + } + + $self->pidl("return NT_STATUS_OK;"); + + $self->deindent; $self->pidl("}"); +} + +sub ParserUnion($$$$) +{ + my ($self, $e,$t,$p) = @_; + + $self->fn_declare($p,"NTSTATUS tdr_$t\_$e->{NAME}(struct tdr_$t *tdr".typearg($t).", int level, union $e->{NAME} *v)"); + $self->pidl("{"); $self->indent; + $self->pidl("int i;") if (ContainsArray($e)); + + if ($t eq "print") { + $self->pidl("tdr->print(tdr, \"\%-25s: union $e->{NAME}\", name);"); + $self->pidl("tdr->level++;"); + } + + $self->pidl("switch (level) {"); $self->indent; + foreach (@{$e->{ELEMENTS}}) { + if (has_property($_, "case")) { + $self->pidl("case " . $_->{PROPERTIES}->{case} . ":"); + } elsif (has_property($_, "default")) { + $self->pidl("default:"); + } + $self->indent; $self->ParserElement($_, $t, {}); $self->deindent; + $self->pidl("break;"); + } + $self->deindent; $self->pidl("}"); + + if ($t eq "print") { + $self->pidl("tdr->level--;"); + } + + $self->pidl("return NT_STATUS_OK;\n"); + $self->deindent; $self->pidl("}"); +} + +sub ParserBitmap($$$$) +{ + my ($self,$e,$t,$p) = @_; + return if ($p); + $self->pidl("#define tdr_$t\_$e->{NAME} tdr_$t\_" . Parse::Pidl::Typelist::bitmap_type_fn($e)); +} + +sub ParserEnum($$$$) +{ + my ($self,$e,$t,$p) = @_; + my $bt = Parse::Pidl::Typelist::enum_type_fn($e); + + $self->fn_declare($p, "NTSTATUS tdr_$t\_$e->{NAME} (struct tdr_$t *tdr".typearg($t).", enum $e->{NAME} *v)"); + $self->pidl("{"); + if ($t eq "pull") { + $self->pidl("\t$bt\_t r;"); + $self->pidl("\tTDR_CHECK(tdr_$t\_$bt(tdr, mem_ctx, \&r));"); + $self->pidl("\t*v = r;"); + } elsif ($t eq "push") { + $self->pidl("\tTDR_CHECK(tdr_$t\_$bt(tdr, ($bt\_t *)v));"); + } elsif ($t eq "print") { + $self->pidl("\t/* FIXME */"); + } + $self->pidl("\treturn NT_STATUS_OK;"); + $self->pidl("}"); +} + +sub ParserTypedef($$$$) +{ + my ($self, $e,$t,$p) = @_; + + $self->ParserType($e->{DATA},$t); +} + +sub ParserType($$$) +{ + my ($self, $e,$t) = @_; + + return if (has_property($e, "no$t")); + + my $handlers = { + STRUCT => \&ParserStruct, UNION => \&ParserUnion, + ENUM => \&ParserEnum, BITMAP => \&ParserBitmap, + TYPEDEF => \&ParserTypedef + }; + + $handlers->{$e->{TYPE}}->($self, $e, $t, has_property($e, "public")) + if (defined($handlers->{$e->{TYPE}})); + + $self->pidl(""); +} + +sub ParserInterface($$) +{ + my ($self,$x) = @_; + + $self->pidl_hdr("#ifndef __TDR_$x->{NAME}_HEADER__"); + $self->pidl_hdr("#define __TDR_$x->{NAME}_HEADER__"); + + foreach (@{$x->{DATA}}) { + $self->ParserType($_, "pull"); + $self->ParserType($_, "push"); + $self->ParserType($_, "print"); + } + + $self->pidl_hdr("#endif /* __TDR_$x->{NAME}_HEADER__ */"); +} + +sub Parser($$$$) +{ + my ($self,$idl,$hdrname,$baseheader) = @_; + $self->pidl("/* autogenerated by pidl */"); + if (is_intree()) { + $self->pidl("#include \"includes.h\""); + } else { + $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 \"$hdrname\""); + $self->pidl(""); + $self->pidl_hdr("/* autogenerated by pidl */"); + $self->pidl_hdr("#include \"$baseheader\""); + $self->pidl_hdr(choose_header("tdr/tdr.h", "tdr.h")); + $self->pidl_hdr(""); + + foreach (@$idl) { $self->ParserInterface($_) if ($_->{TYPE} eq "INTERFACE"); } + return ($self->{ret_hdr}, $self->{ret}); +} + +1; diff --git a/pidl/lib/Parse/Pidl/Samba4/Template.pm b/pidl/lib/Parse/Pidl/Samba4/Template.pm new file mode 100644 index 0000000000..a35fc7d2eb --- /dev/null +++ b/pidl/lib/Parse/Pidl/Samba4/Template.pm @@ -0,0 +1,98 @@ +################################################### +# server template function generator +# Copyright tridge@samba.org 2003 +# released under the GNU GPL + +package Parse::Pidl::Samba4::Template; + +use vars qw($VERSION); +$VERSION = '0.01'; + +use strict; + +my($res); + +##################################################################### +# produce boilerplate code for a interface +sub Template($) +{ + my($interface) = shift; + my($data) = $interface->{DATA}; + my $name = $interface->{NAME}; + + $res .= +"/* + Unix SMB/CIFS implementation. + + endpoint server for the $name pipe + + Copyright (C) YOUR NAME HERE YEAR + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include \"includes.h\" +#include \"rpc_server/dcerpc_server.h\" +#include \"librpc/gen_ndr/ndr_$name.h\" +#include \"rpc_server/common/common.h\" + +"; + + foreach my $d (@{$data}) { + if ($d->{TYPE} eq "FUNCTION") { + my $fname = $d->{NAME}; + $res .= +" +/* + $fname +*/ +static $d->{RETURN_TYPE} dcesrv_$fname(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, + struct $fname *r) +{ +"; + + if ($d->{RETURN_TYPE} eq "void") { + $res .= "\tDCESRV_FAULT_VOID(DCERPC_FAULT_OP_RNG_ERROR);\n"; + } else { + $res .= "\tDCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);\n"; + } + + $res .= "} + +"; + } + } + + $res .= +" +/* include the generated boilerplate */ +#include \"librpc/gen_ndr/ndr_$name\_s.c\" +" +} + + +##################################################################### +# parse a parsed IDL structure back into an IDL file +sub Parse($) +{ + my($idl) = shift; + $res = ""; + foreach my $x (@{$idl}) { + ($x->{TYPE} eq "INTERFACE") && + Template($x); + } + return $res; +} + +1; |