xref: /reactos/dll/win32/rpcrt4/ndr_stubless.c (revision e54b32b4)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * NDR -Oi,-Oif,-Oicf Interpreter
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 2001 Ove Kåven, TransGaming Technologies
5c2c66affSColin Finck  * Copyright 2003-5 Robert Shearman (for CodeWeavers)
6c2c66affSColin Finck  *
7c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
8c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
9c2c66affSColin Finck  * License as published by the Free Software Foundation; either
10c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
11c2c66affSColin Finck  *
12c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
13c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15c2c66affSColin Finck  * Lesser General Public License for more details.
16c2c66affSColin Finck  *
17c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
18c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
19c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20c2c66affSColin Finck  *
21c2c66affSColin Finck  * TODO:
22c2c66affSColin Finck  *  - Pipes
23c2c66affSColin Finck  *  - Some types of binding handles
24c2c66affSColin Finck  */
25c2c66affSColin Finck 
2687a48c83SAmine Khaldi #include <stdarg.h>
2787a48c83SAmine Khaldi #include <stdio.h>
2887a48c83SAmine Khaldi #include <string.h>
2987a48c83SAmine Khaldi 
3087a48c83SAmine Khaldi #include "windef.h"
3187a48c83SAmine Khaldi #include "winbase.h"
3287a48c83SAmine Khaldi #include "winerror.h"
3387a48c83SAmine Khaldi 
3487a48c83SAmine Khaldi #include "objbase.h"
3587a48c83SAmine Khaldi #include "rpc.h"
3687a48c83SAmine Khaldi #include "rpcproxy.h"
3787a48c83SAmine Khaldi 
3887a48c83SAmine Khaldi #include "wine/exception.h"
396feb8748SAmine Khaldi #include "wine/asm.h"
4087a48c83SAmine Khaldi #include "wine/debug.h"
4187a48c83SAmine Khaldi 
4287a48c83SAmine Khaldi #include "cpsf.h"
4387a48c83SAmine Khaldi #include "ndr_misc.h"
4487a48c83SAmine Khaldi #include "ndr_stubless.h"
45c2c66affSColin Finck 
46c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(rpc);
47c2c66affSColin Finck 
48c2c66affSColin Finck #define NDR_TABLE_MASK 127
49c2c66affSColin Finck 
is_oicf_stubdesc(const PMIDL_STUB_DESC pStubDesc)50c2c66affSColin Finck static inline BOOL is_oicf_stubdesc(const PMIDL_STUB_DESC pStubDesc)
51c2c66affSColin Finck {
52c2c66affSColin Finck     return pStubDesc->Version >= 0x20000;
53c2c66affSColin Finck }
54c2c66affSColin Finck 
call_buffer_sizer(PMIDL_STUB_MESSAGE pStubMsg,unsigned char * pMemory,const NDR_PARAM_OIF * param)55c2c66affSColin Finck static inline void call_buffer_sizer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
56c2c66affSColin Finck                                      const NDR_PARAM_OIF *param)
57c2c66affSColin Finck {
58c2c66affSColin Finck     PFORMAT_STRING pFormat;
59c2c66affSColin Finck     NDR_BUFFERSIZE m;
60c2c66affSColin Finck 
61c2c66affSColin Finck     if (param->attr.IsBasetype)
62c2c66affSColin Finck     {
63c2c66affSColin Finck         pFormat = &param->u.type_format_char;
64c2c66affSColin Finck         if (param->attr.IsSimpleRef) pMemory = *(unsigned char **)pMemory;
65c2c66affSColin Finck     }
66c2c66affSColin Finck     else
67c2c66affSColin Finck     {
68c2c66affSColin Finck         pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
69c2c66affSColin Finck         if (!param->attr.IsByValue) pMemory = *(unsigned char **)pMemory;
70c2c66affSColin Finck     }
71c2c66affSColin Finck 
72c2c66affSColin Finck     m = NdrBufferSizer[pFormat[0] & NDR_TABLE_MASK];
73c2c66affSColin Finck     if (m) m(pStubMsg, pMemory, pFormat);
74c2c66affSColin Finck     else
75c2c66affSColin Finck     {
76c2c66affSColin Finck         FIXME("format type 0x%x not implemented\n", pFormat[0]);
77c2c66affSColin Finck         RpcRaiseException(RPC_X_BAD_STUB_DATA);
78c2c66affSColin Finck     }
79c2c66affSColin Finck }
80c2c66affSColin Finck 
call_marshaller(PMIDL_STUB_MESSAGE pStubMsg,unsigned char * pMemory,const NDR_PARAM_OIF * param)81c2c66affSColin Finck static inline unsigned char *call_marshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
82c2c66affSColin Finck                                              const NDR_PARAM_OIF *param)
83c2c66affSColin Finck {
84c2c66affSColin Finck     PFORMAT_STRING pFormat;
85c2c66affSColin Finck     NDR_MARSHALL m;
86c2c66affSColin Finck 
87c2c66affSColin Finck     if (param->attr.IsBasetype)
88c2c66affSColin Finck     {
89c2c66affSColin Finck         pFormat = &param->u.type_format_char;
90c2c66affSColin Finck         if (param->attr.IsSimpleRef) pMemory = *(unsigned char **)pMemory;
91c2c66affSColin Finck     }
92c2c66affSColin Finck     else
93c2c66affSColin Finck     {
94c2c66affSColin Finck         pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
95c2c66affSColin Finck         if (!param->attr.IsByValue) pMemory = *(unsigned char **)pMemory;
96c2c66affSColin Finck     }
97c2c66affSColin Finck 
98c2c66affSColin Finck     m = NdrMarshaller[pFormat[0] & NDR_TABLE_MASK];
99c2c66affSColin Finck     if (m) return m(pStubMsg, pMemory, pFormat);
100c2c66affSColin Finck     else
101c2c66affSColin Finck     {
102c2c66affSColin Finck         FIXME("format type 0x%x not implemented\n", pFormat[0]);
103c2c66affSColin Finck         RpcRaiseException(RPC_X_BAD_STUB_DATA);
104c2c66affSColin Finck         return NULL;
105c2c66affSColin Finck     }
106c2c66affSColin Finck }
107c2c66affSColin Finck 
call_unmarshaller(PMIDL_STUB_MESSAGE pStubMsg,unsigned char ** ppMemory,const NDR_PARAM_OIF * param,unsigned char fMustAlloc)108c2c66affSColin Finck static inline unsigned char *call_unmarshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char **ppMemory,
109c2c66affSColin Finck                                                const NDR_PARAM_OIF *param, unsigned char fMustAlloc)
110c2c66affSColin Finck {
111c2c66affSColin Finck     PFORMAT_STRING pFormat;
112c2c66affSColin Finck     NDR_UNMARSHALL m;
113c2c66affSColin Finck 
114c2c66affSColin Finck     if (param->attr.IsBasetype)
115c2c66affSColin Finck     {
116c2c66affSColin Finck         pFormat = &param->u.type_format_char;
117c2c66affSColin Finck         if (param->attr.IsSimpleRef) ppMemory = (unsigned char **)*ppMemory;
118c2c66affSColin Finck     }
119c2c66affSColin Finck     else
120c2c66affSColin Finck     {
121c2c66affSColin Finck         pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
122c2c66affSColin Finck         if (!param->attr.IsByValue) ppMemory = (unsigned char **)*ppMemory;
123c2c66affSColin Finck     }
124c2c66affSColin Finck 
125c2c66affSColin Finck     m = NdrUnmarshaller[pFormat[0] & NDR_TABLE_MASK];
126c2c66affSColin Finck     if (m) return m(pStubMsg, ppMemory, pFormat, fMustAlloc);
127c2c66affSColin Finck     else
128c2c66affSColin Finck     {
129c2c66affSColin Finck         FIXME("format type 0x%x not implemented\n", pFormat[0]);
130c2c66affSColin Finck         RpcRaiseException(RPC_X_BAD_STUB_DATA);
131c2c66affSColin Finck         return NULL;
132c2c66affSColin Finck     }
133c2c66affSColin Finck }
134c2c66affSColin Finck 
call_freer(PMIDL_STUB_MESSAGE pStubMsg,unsigned char * pMemory,const NDR_PARAM_OIF * param)135c2c66affSColin Finck static inline void call_freer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
136c2c66affSColin Finck                               const NDR_PARAM_OIF *param)
137c2c66affSColin Finck {
138c2c66affSColin Finck     PFORMAT_STRING pFormat;
139c2c66affSColin Finck     NDR_FREE m;
140c2c66affSColin Finck 
141c2c66affSColin Finck     if (param->attr.IsBasetype) return;  /* nothing to do */
142c2c66affSColin Finck     pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
143c2c66affSColin Finck     if (!param->attr.IsByValue) pMemory = *(unsigned char **)pMemory;
144c2c66affSColin Finck 
145c2c66affSColin Finck     m = NdrFreer[pFormat[0] & NDR_TABLE_MASK];
146c2c66affSColin Finck     if (m) m(pStubMsg, pMemory, pFormat);
147c2c66affSColin Finck }
148c2c66affSColin Finck 
calc_arg_size(MIDL_STUB_MESSAGE * pStubMsg,PFORMAT_STRING pFormat)149c2c66affSColin Finck static DWORD calc_arg_size(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat)
150c2c66affSColin Finck {
151c2c66affSColin Finck     DWORD size;
152c2c66affSColin Finck     switch(*pFormat)
153c2c66affSColin Finck     {
154b6fdb56bSAmine Khaldi     case FC_RP:
155b6fdb56bSAmine Khaldi         if (pFormat[1] & FC_SIMPLE_POINTER)
156c2c66affSColin Finck         {
157b6fdb56bSAmine Khaldi             size = 0;
158c2c66affSColin Finck             break;
159c2c66affSColin Finck         }
160c2c66affSColin Finck         size = calc_arg_size(pStubMsg, &pFormat[2] + *(const SHORT*)&pFormat[2]);
161c2c66affSColin Finck         break;
162b6fdb56bSAmine Khaldi     case FC_STRUCT:
163b6fdb56bSAmine Khaldi     case FC_PSTRUCT:
164c2c66affSColin Finck         size = *(const WORD*)(pFormat + 2);
165c2c66affSColin Finck         break;
166b6fdb56bSAmine Khaldi     case FC_BOGUS_STRUCT:
167c2c66affSColin Finck         size = *(const WORD*)(pFormat + 2);
168c2c66affSColin Finck         if(*(const WORD*)(pFormat + 4))
169c2c66affSColin Finck             FIXME("Unhandled conformant description\n");
170c2c66affSColin Finck         break;
171b6fdb56bSAmine Khaldi     case FC_CARRAY:
172b6fdb56bSAmine Khaldi     case FC_CVARRAY:
173c2c66affSColin Finck         size = *(const WORD*)(pFormat + 2);
174c2c66affSColin Finck         ComputeConformance(pStubMsg, NULL, pFormat + 4, 0);
175c2c66affSColin Finck         size *= pStubMsg->MaxCount;
176c2c66affSColin Finck         break;
177b6fdb56bSAmine Khaldi     case FC_SMFARRAY:
178b6fdb56bSAmine Khaldi     case FC_SMVARRAY:
179c2c66affSColin Finck         size = *(const WORD*)(pFormat + 2);
180c2c66affSColin Finck         break;
181b6fdb56bSAmine Khaldi     case FC_LGFARRAY:
182b6fdb56bSAmine Khaldi     case FC_LGVARRAY:
183c2c66affSColin Finck         size = *(const DWORD*)(pFormat + 2);
184c2c66affSColin Finck         break;
185b6fdb56bSAmine Khaldi     case FC_BOGUS_ARRAY:
186c2c66affSColin Finck         pFormat = ComputeConformance(pStubMsg, NULL, pFormat + 4, *(const WORD*)&pFormat[2]);
187c2c66affSColin Finck         TRACE("conformance = %ld\n", pStubMsg->MaxCount);
188c2c66affSColin Finck         pFormat = ComputeVariance(pStubMsg, NULL, pFormat, pStubMsg->MaxCount);
189c2c66affSColin Finck         size = ComplexStructSize(pStubMsg, pFormat);
190c2c66affSColin Finck         size *= pStubMsg->MaxCount;
191c2c66affSColin Finck         break;
192b6fdb56bSAmine Khaldi     case FC_USER_MARSHAL:
193c2c66affSColin Finck         size = *(const WORD*)(pFormat + 4);
194c2c66affSColin Finck         break;
195b6fdb56bSAmine Khaldi     case FC_CSTRING:
196c2c66affSColin Finck         size = *(const WORD*)(pFormat + 2);
197c2c66affSColin Finck         break;
198b6fdb56bSAmine Khaldi     case FC_WSTRING:
199c2c66affSColin Finck         size = *(const WORD*)(pFormat + 2) * sizeof(WCHAR);
200c2c66affSColin Finck         break;
201b6fdb56bSAmine Khaldi     case FC_C_CSTRING:
202b6fdb56bSAmine Khaldi     case FC_C_WSTRING:
203b6fdb56bSAmine Khaldi         if (*pFormat == FC_C_CSTRING)
204c2c66affSColin Finck             size = sizeof(CHAR);
205c2c66affSColin Finck         else
206c2c66affSColin Finck             size = sizeof(WCHAR);
207b6fdb56bSAmine Khaldi         if (pFormat[1] == FC_STRING_SIZED)
208c2c66affSColin Finck             ComputeConformance(pStubMsg, NULL, pFormat + 2, 0);
209c2c66affSColin Finck         else
210c2c66affSColin Finck             pStubMsg->MaxCount = 0;
211c2c66affSColin Finck         size *= pStubMsg->MaxCount;
212c2c66affSColin Finck         break;
213c2c66affSColin Finck     default:
214c2c66affSColin Finck         FIXME("Unhandled type %02x\n", *pFormat);
215c2c66affSColin Finck         /* fallthrough */
216b6fdb56bSAmine Khaldi     case FC_UP:
217b6fdb56bSAmine Khaldi     case FC_OP:
218b6fdb56bSAmine Khaldi     case FC_FP:
219b6fdb56bSAmine Khaldi     case FC_IP:
220c2c66affSColin Finck         size = sizeof(void *);
221c2c66affSColin Finck         break;
222c2c66affSColin Finck     }
223c2c66affSColin Finck     return size;
224c2c66affSColin Finck }
225c2c66affSColin Finck 
NdrRpcSmSetClientToOsf(PMIDL_STUB_MESSAGE pMessage)226c2c66affSColin Finck void WINAPI NdrRpcSmSetClientToOsf(PMIDL_STUB_MESSAGE pMessage)
227c2c66affSColin Finck {
228c2c66affSColin Finck #if 0 /* these functions are not defined yet */
229c2c66affSColin Finck     pMessage->pfnAllocate = NdrRpcSmClientAllocate;
230c2c66affSColin Finck     pMessage->pfnFree = NdrRpcSmClientFree;
231c2c66affSColin Finck #endif
232c2c66affSColin Finck }
233c2c66affSColin Finck 
debugstr_PROC_PF(PARAM_ATTRIBUTES param_attributes)234c2c66affSColin Finck static const char *debugstr_PROC_PF(PARAM_ATTRIBUTES param_attributes)
235c2c66affSColin Finck {
236c2c66affSColin Finck     char buffer[160];
237c2c66affSColin Finck 
238c2c66affSColin Finck     buffer[0] = 0;
239c2c66affSColin Finck     if (param_attributes.MustSize) strcat(buffer, " MustSize");
240c2c66affSColin Finck     if (param_attributes.MustFree) strcat(buffer, " MustFree");
241c2c66affSColin Finck     if (param_attributes.IsPipe) strcat(buffer, " IsPipe");
242c2c66affSColin Finck     if (param_attributes.IsIn) strcat(buffer, " IsIn");
243c2c66affSColin Finck     if (param_attributes.IsOut) strcat(buffer, " IsOut");
244c2c66affSColin Finck     if (param_attributes.IsReturn) strcat(buffer, " IsReturn");
245c2c66affSColin Finck     if (param_attributes.IsBasetype) strcat(buffer, " IsBasetype");
246c2c66affSColin Finck     if (param_attributes.IsByValue) strcat(buffer, " IsByValue");
247c2c66affSColin Finck     if (param_attributes.IsSimpleRef) strcat(buffer, " IsSimpleRef");
248c2c66affSColin Finck     if (param_attributes.IsDontCallFreeInst) strcat(buffer, " IsDontCallFreeInst");
249c2c66affSColin Finck     if (param_attributes.SaveForAsyncFinish) strcat(buffer, " SaveForAsyncFinish");
250c2c66affSColin Finck     if (param_attributes.ServerAllocSize)
251c2c66affSColin Finck         sprintf( buffer + strlen(buffer), " ServerAllocSize = %d", param_attributes.ServerAllocSize * 8);
252c2c66affSColin Finck     return buffer[0] ? wine_dbg_sprintf( "%s", buffer + 1 ) : "";
253c2c66affSColin Finck }
254c2c66affSColin Finck 
debugstr_INTERPRETER_OPT_FLAGS(INTERPRETER_OPT_FLAGS Oi2Flags)255c2c66affSColin Finck static const char *debugstr_INTERPRETER_OPT_FLAGS(INTERPRETER_OPT_FLAGS Oi2Flags)
256c2c66affSColin Finck {
257c2c66affSColin Finck     char buffer[160];
258c2c66affSColin Finck 
259c2c66affSColin Finck     buffer[0] = 0;
260c2c66affSColin Finck     if (Oi2Flags.ServerMustSize) strcat(buffer, " ServerMustSize");
261c2c66affSColin Finck     if (Oi2Flags.ClientMustSize) strcat(buffer, " ClientMustSize");
262c2c66affSColin Finck     if (Oi2Flags.HasReturn) strcat(buffer, " HasReturn");
263c2c66affSColin Finck     if (Oi2Flags.HasPipes) strcat(buffer, " HasPipes");
264c2c66affSColin Finck     if (Oi2Flags.Unused) strcat(buffer, " Unused");
265c2c66affSColin Finck     if (Oi2Flags.HasAsyncUuid) strcat(buffer, " HasAsyncUuid");
266c2c66affSColin Finck     if (Oi2Flags.HasExtensions) strcat(buffer, " HasExtensions");
267c2c66affSColin Finck     if (Oi2Flags.HasAsyncHandle) strcat(buffer, " HasAsyncHandle");
268c2c66affSColin Finck     return buffer[0] ? wine_dbg_sprintf( "%s", buffer + 1 ) : "";
269c2c66affSColin Finck }
270c2c66affSColin Finck 
271c2c66affSColin Finck #define ARG_FROM_OFFSET(args, offset) ((args) + (offset))
272c2c66affSColin Finck 
get_handle_desc_size(const NDR_PROC_HEADER * proc_header,PFORMAT_STRING format)2736feb8748SAmine Khaldi static size_t get_handle_desc_size(const NDR_PROC_HEADER *proc_header, PFORMAT_STRING format)
2746feb8748SAmine Khaldi {
2756feb8748SAmine Khaldi     if (!proc_header->handle_type)
2766feb8748SAmine Khaldi     {
2776feb8748SAmine Khaldi         if (*format == FC_BIND_PRIMITIVE)
2786feb8748SAmine Khaldi             return sizeof(NDR_EHD_PRIMITIVE);
2796feb8748SAmine Khaldi         else if (*format == FC_BIND_GENERIC)
2806feb8748SAmine Khaldi             return sizeof(NDR_EHD_GENERIC);
2816feb8748SAmine Khaldi         else if (*format == FC_BIND_CONTEXT)
2826feb8748SAmine Khaldi             return sizeof(NDR_EHD_CONTEXT);
2836feb8748SAmine Khaldi     }
2846feb8748SAmine Khaldi     return 0;
2856feb8748SAmine Khaldi }
2866feb8748SAmine Khaldi 
client_get_handle(const MIDL_STUB_MESSAGE * pStubMsg,const NDR_PROC_HEADER * pProcHeader,const PFORMAT_STRING pFormat)2876feb8748SAmine Khaldi static handle_t client_get_handle(const MIDL_STUB_MESSAGE *pStubMsg,
2886feb8748SAmine Khaldi         const NDR_PROC_HEADER *pProcHeader, const PFORMAT_STRING pFormat)
289c2c66affSColin Finck {
290c2c66affSColin Finck     /* binding */
291c2c66affSColin Finck     switch (pProcHeader->handle_type)
292c2c66affSColin Finck     {
293c2c66affSColin Finck     /* explicit binding: parse additional section */
294b6fdb56bSAmine Khaldi     case 0:
295c2c66affSColin Finck         switch (*pFormat) /* handle_type */
296c2c66affSColin Finck         {
297b6fdb56bSAmine Khaldi         case FC_BIND_PRIMITIVE: /* explicit primitive */
298c2c66affSColin Finck             {
299c2c66affSColin Finck                 const NDR_EHD_PRIMITIVE *pDesc = (const NDR_EHD_PRIMITIVE *)pFormat;
300c2c66affSColin Finck 
301c2c66affSColin Finck                 TRACE("Explicit primitive handle @ %d\n", pDesc->offset);
302c2c66affSColin Finck 
303c2c66affSColin Finck                 if (pDesc->flag) /* pointer to binding */
3046feb8748SAmine Khaldi                     return **(handle_t **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
305c2c66affSColin Finck                 else
3066feb8748SAmine Khaldi                     return *(handle_t *)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
307c2c66affSColin Finck             }
308b6fdb56bSAmine Khaldi         case FC_BIND_GENERIC: /* explicit generic */
309c2c66affSColin Finck             {
310c2c66affSColin Finck                 const NDR_EHD_GENERIC *pDesc = (const NDR_EHD_GENERIC *)pFormat;
311c2c66affSColin Finck                 void *pObject = NULL;
312c2c66affSColin Finck                 void *pArg;
313c2c66affSColin Finck                 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
314c2c66affSColin Finck 
315c2c66affSColin Finck                 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
316c2c66affSColin Finck 
317c2c66affSColin Finck                 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
318c2c66affSColin Finck                     pArg = *(void **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
319c2c66affSColin Finck                 else
320c2c66affSColin Finck                     pArg = ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
321c2c66affSColin Finck                 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
322c2c66affSColin Finck                 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
3236feb8748SAmine Khaldi                 return pGenPair->pfnBind(pObject);
324c2c66affSColin Finck             }
325b6fdb56bSAmine Khaldi         case FC_BIND_CONTEXT: /* explicit context */
326c2c66affSColin Finck             {
327c2c66affSColin Finck                 const NDR_EHD_CONTEXT *pDesc = (const NDR_EHD_CONTEXT *)pFormat;
328c2c66affSColin Finck                 NDR_CCONTEXT context_handle;
329c2c66affSColin Finck                 TRACE("Explicit bind context\n");
330c2c66affSColin Finck                 if (pDesc->flags & HANDLE_PARAM_IS_VIA_PTR)
331c2c66affSColin Finck                 {
332c2c66affSColin Finck                     TRACE("\tHANDLE_PARAM_IS_VIA_PTR\n");
333c2c66affSColin Finck                     context_handle = **(NDR_CCONTEXT **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
334c2c66affSColin Finck                 }
335c2c66affSColin Finck                 else
336c2c66affSColin Finck                     context_handle = *(NDR_CCONTEXT *)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
337c2c66affSColin Finck 
3386feb8748SAmine Khaldi                 if (context_handle) return NDRCContextBinding(context_handle);
339c2c66affSColin Finck                 else if (pDesc->flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL)
340c2c66affSColin Finck                 {
341c2c66affSColin Finck                     ERR("null context handle isn't allowed\n");
342c2c66affSColin Finck                     RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);
343c2c66affSColin Finck                     return NULL;
344c2c66affSColin Finck                 }
345c2c66affSColin Finck                 /* FIXME: should we store this structure in stubMsg.pContext? */
346c2c66affSColin Finck             }
347c2c66affSColin Finck         default:
348c2c66affSColin Finck             ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
349c2c66affSColin Finck             RpcRaiseException(RPC_X_BAD_STUB_DATA);
350c2c66affSColin Finck         }
351c2c66affSColin Finck         break;
352b6fdb56bSAmine Khaldi     case FC_BIND_GENERIC: /* implicit generic */
353b6fdb56bSAmine Khaldi         FIXME("FC_BIND_GENERIC\n");
354c2c66affSColin Finck         RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
355c2c66affSColin Finck         break;
356b6fdb56bSAmine Khaldi     case FC_BIND_PRIMITIVE: /* implicit primitive */
357c2c66affSColin Finck         TRACE("Implicit primitive handle\n");
3586feb8748SAmine Khaldi         return *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pPrimitiveHandle;
359b6fdb56bSAmine Khaldi     case FC_CALLBACK_HANDLE: /* implicit callback */
360b6fdb56bSAmine Khaldi         TRACE("FC_CALLBACK_HANDLE\n");
361c2c66affSColin Finck         /* server calls callback procedures only in response to remote call, and most recent
362c2c66affSColin Finck            binding handle is used. Calling back to a client can potentially result in another
363c2c66affSColin Finck            callback with different current handle. */
3646feb8748SAmine Khaldi         return I_RpcGetCurrentCallHandle();
365b6fdb56bSAmine Khaldi     case FC_AUTO_HANDLE: /* implicit auto handle */
366c2c66affSColin Finck         /* strictly speaking, it isn't necessary to set hBinding here
367c2c66affSColin Finck          * since it isn't actually used (hence the automatic in its name),
368c2c66affSColin Finck          * but then why does MIDL generate a valid entry in the
369c2c66affSColin Finck          * MIDL_STUB_DESC for it? */
370c2c66affSColin Finck         TRACE("Implicit auto handle\n");
3716feb8748SAmine Khaldi         return *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle;
372c2c66affSColin Finck     default:
373c2c66affSColin Finck         ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
374c2c66affSColin Finck         RpcRaiseException(RPC_X_BAD_STUB_DATA);
375c2c66affSColin Finck     }
3766feb8748SAmine Khaldi     return NULL;
377c2c66affSColin Finck }
378c2c66affSColin Finck 
client_free_handle(PMIDL_STUB_MESSAGE pStubMsg,const NDR_PROC_HEADER * pProcHeader,PFORMAT_STRING pFormat,handle_t hBinding)379c2c66affSColin Finck static void client_free_handle(
380c2c66affSColin Finck     PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
381c2c66affSColin Finck     PFORMAT_STRING pFormat, handle_t hBinding)
382c2c66affSColin Finck {
383c2c66affSColin Finck     /* binding */
384c2c66affSColin Finck     switch (pProcHeader->handle_type)
385c2c66affSColin Finck     {
386c2c66affSColin Finck     /* explicit binding: parse additional section */
387b6fdb56bSAmine Khaldi     case 0:
388c2c66affSColin Finck         switch (*pFormat) /* handle_type */
389c2c66affSColin Finck         {
390b6fdb56bSAmine Khaldi         case FC_BIND_GENERIC: /* explicit generic */
391c2c66affSColin Finck             {
392c2c66affSColin Finck                 const NDR_EHD_GENERIC *pDesc = (const NDR_EHD_GENERIC *)pFormat;
393c2c66affSColin Finck                 void *pObject = NULL;
394c2c66affSColin Finck                 void *pArg;
395c2c66affSColin Finck                 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
396c2c66affSColin Finck 
397c2c66affSColin Finck                 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
398c2c66affSColin Finck 
399c2c66affSColin Finck                 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
400c2c66affSColin Finck                     pArg = *(void **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
401c2c66affSColin Finck                 else
402c2c66affSColin Finck                     pArg = ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
403c2c66affSColin Finck                 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
404c2c66affSColin Finck                 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
4051cc84c6dSJoachim Henze #ifdef __REACTOS__
4061cc84c6dSJoachim Henze                 if (hBinding) pGenPair->pfnUnbind(pObject, hBinding);
4071cc84c6dSJoachim Henze #else
408c2c66affSColin Finck                 pGenPair->pfnUnbind(pObject, hBinding);
4091cc84c6dSJoachim Henze #endif
410c2c66affSColin Finck                 break;
411c2c66affSColin Finck             }
412b6fdb56bSAmine Khaldi         case FC_BIND_CONTEXT: /* explicit context */
413b6fdb56bSAmine Khaldi         case FC_BIND_PRIMITIVE: /* explicit primitive */
414c2c66affSColin Finck             break;
415c2c66affSColin Finck         default:
416c2c66affSColin Finck             ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
417c2c66affSColin Finck             RpcRaiseException(RPC_X_BAD_STUB_DATA);
418c2c66affSColin Finck         }
419c2c66affSColin Finck         break;
420b6fdb56bSAmine Khaldi     case FC_BIND_GENERIC: /* implicit generic */
421b6fdb56bSAmine Khaldi         FIXME("FC_BIND_GENERIC\n");
422c2c66affSColin Finck         RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
423c2c66affSColin Finck         break;
424b6fdb56bSAmine Khaldi     case FC_CALLBACK_HANDLE: /* implicit callback */
425b6fdb56bSAmine Khaldi     case FC_BIND_PRIMITIVE: /* implicit primitive */
426b6fdb56bSAmine Khaldi     case FC_AUTO_HANDLE: /* implicit auto handle */
427c2c66affSColin Finck         break;
428c2c66affSColin Finck     default:
429c2c66affSColin Finck         ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
430c2c66affSColin Finck         RpcRaiseException(RPC_X_BAD_STUB_DATA);
431c2c66affSColin Finck     }
432c2c66affSColin Finck }
433c2c66affSColin Finck 
param_needs_alloc(PARAM_ATTRIBUTES attr)434b6fdb56bSAmine Khaldi static inline BOOL param_needs_alloc( PARAM_ATTRIBUTES attr )
435b6fdb56bSAmine Khaldi {
436b6fdb56bSAmine Khaldi     return attr.IsOut && !attr.IsIn && !attr.IsBasetype && !attr.IsByValue;
437b6fdb56bSAmine Khaldi }
438b6fdb56bSAmine Khaldi 
param_is_out_basetype(PARAM_ATTRIBUTES attr)439b6fdb56bSAmine Khaldi static inline BOOL param_is_out_basetype( PARAM_ATTRIBUTES attr )
440b6fdb56bSAmine Khaldi {
441b6fdb56bSAmine Khaldi     return attr.IsOut && !attr.IsIn && attr.IsBasetype && attr.IsSimpleRef;
442b6fdb56bSAmine Khaldi }
443b6fdb56bSAmine Khaldi 
basetype_arg_size(unsigned char fc)444b6fdb56bSAmine Khaldi static size_t basetype_arg_size( unsigned char fc )
445b6fdb56bSAmine Khaldi {
446b6fdb56bSAmine Khaldi     switch (fc)
447b6fdb56bSAmine Khaldi     {
448b6fdb56bSAmine Khaldi     case FC_BYTE:
449b6fdb56bSAmine Khaldi     case FC_CHAR:
450b6fdb56bSAmine Khaldi     case FC_SMALL:
451b6fdb56bSAmine Khaldi     case FC_USMALL:
452b6fdb56bSAmine Khaldi         return sizeof(char);
453b6fdb56bSAmine Khaldi     case FC_WCHAR:
454b6fdb56bSAmine Khaldi     case FC_SHORT:
455b6fdb56bSAmine Khaldi     case FC_USHORT:
456b6fdb56bSAmine Khaldi         return sizeof(short);
457b6fdb56bSAmine Khaldi     case FC_LONG:
458b6fdb56bSAmine Khaldi     case FC_ULONG:
459b6fdb56bSAmine Khaldi     case FC_ENUM16:
460b6fdb56bSAmine Khaldi     case FC_ENUM32:
461b6fdb56bSAmine Khaldi     case FC_ERROR_STATUS_T:
462b6fdb56bSAmine Khaldi         return sizeof(int);
463b6fdb56bSAmine Khaldi     case FC_FLOAT:
464b6fdb56bSAmine Khaldi         return sizeof(float);
465b6fdb56bSAmine Khaldi     case FC_HYPER:
466b6fdb56bSAmine Khaldi         return sizeof(LONGLONG);
467b6fdb56bSAmine Khaldi     case FC_DOUBLE:
468b6fdb56bSAmine Khaldi         return sizeof(double);
469b6fdb56bSAmine Khaldi     case FC_INT3264:
470b6fdb56bSAmine Khaldi     case FC_UINT3264:
471b6fdb56bSAmine Khaldi         return sizeof(INT_PTR);
472b6fdb56bSAmine Khaldi     default:
473b6fdb56bSAmine Khaldi         FIXME("Unhandled basetype %#x.\n", fc);
474b6fdb56bSAmine Khaldi         return 0;
475b6fdb56bSAmine Khaldi     }
476b6fdb56bSAmine Khaldi }
477b6fdb56bSAmine Khaldi 
client_do_args(PMIDL_STUB_MESSAGE pStubMsg,PFORMAT_STRING pFormat,enum stubless_phase phase,void ** fpu_args,unsigned short number_of_params,unsigned char * pRetVal)478c2c66affSColin Finck void client_do_args( PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat, enum stubless_phase phase,
479c2c66affSColin Finck                      void **fpu_args, unsigned short number_of_params, unsigned char *pRetVal )
480c2c66affSColin Finck {
481c2c66affSColin Finck     const NDR_PARAM_OIF *params = (const NDR_PARAM_OIF *)pFormat;
482c2c66affSColin Finck     unsigned int i;
483c2c66affSColin Finck 
484c2c66affSColin Finck     for (i = 0; i < number_of_params; i++)
485c2c66affSColin Finck     {
486c2c66affSColin Finck         unsigned char *pArg = pStubMsg->StackTop + params[i].stack_offset;
487c2c66affSColin Finck         PFORMAT_STRING pTypeFormat = (PFORMAT_STRING)&pStubMsg->StubDesc->pFormatTypes[params[i].u.type_offset];
488c2c66affSColin Finck 
489c2c66affSColin Finck #ifdef __x86_64__  /* floats are passed as doubles through varargs functions */
490c2c66affSColin Finck         float f;
491c2c66affSColin Finck 
492c2c66affSColin Finck         if (params[i].attr.IsBasetype &&
493b6fdb56bSAmine Khaldi             params[i].u.type_format_char == FC_FLOAT &&
494c2c66affSColin Finck             !params[i].attr.IsSimpleRef &&
495c2c66affSColin Finck             !fpu_args)
496c2c66affSColin Finck         {
497c2c66affSColin Finck             f = *(double *)pArg;
498c2c66affSColin Finck             pArg = (unsigned char *)&f;
499c2c66affSColin Finck         }
500c2c66affSColin Finck #endif
501c2c66affSColin Finck 
502c2c66affSColin Finck         TRACE("param[%d]: %p type %02x %s\n", i, pArg,
503c2c66affSColin Finck               params[i].attr.IsBasetype ? params[i].u.type_format_char : *pTypeFormat,
504c2c66affSColin Finck               debugstr_PROC_PF( params[i].attr ));
505c2c66affSColin Finck 
506c2c66affSColin Finck         switch (phase)
507c2c66affSColin Finck         {
508c2c66affSColin Finck         case STUBLESS_INITOUT:
509b6fdb56bSAmine Khaldi             if (*(unsigned char **)pArg)
510c2c66affSColin Finck             {
511b6fdb56bSAmine Khaldi                 if (param_needs_alloc(params[i].attr))
512c2c66affSColin Finck                     memset( *(unsigned char **)pArg, 0, calc_arg_size( pStubMsg, pTypeFormat ));
513b6fdb56bSAmine Khaldi                 else if (param_is_out_basetype(params[i].attr))
514b6fdb56bSAmine Khaldi                     memset( *(unsigned char **)pArg, 0, basetype_arg_size( params[i].u.type_format_char ));
515c2c66affSColin Finck             }
516c2c66affSColin Finck             break;
517c2c66affSColin Finck         case STUBLESS_CALCSIZE:
518c2c66affSColin Finck             if (params[i].attr.IsSimpleRef && !*(unsigned char **)pArg)
519c2c66affSColin Finck                 RpcRaiseException(RPC_X_NULL_REF_POINTER);
520c2c66affSColin Finck             if (params[i].attr.IsIn) call_buffer_sizer(pStubMsg, pArg, &params[i]);
521c2c66affSColin Finck             break;
522c2c66affSColin Finck         case STUBLESS_MARSHAL:
523c2c66affSColin Finck             if (params[i].attr.IsIn) call_marshaller(pStubMsg, pArg, &params[i]);
524c2c66affSColin Finck             break;
525c2c66affSColin Finck         case STUBLESS_UNMARSHAL:
526c2c66affSColin Finck             if (params[i].attr.IsOut)
527c2c66affSColin Finck             {
528c2c66affSColin Finck                 if (params[i].attr.IsReturn && pRetVal) pArg = pRetVal;
529c2c66affSColin Finck                 call_unmarshaller(pStubMsg, &pArg, &params[i], 0);
530c2c66affSColin Finck             }
531c2c66affSColin Finck             break;
532c2c66affSColin Finck         case STUBLESS_FREE:
533c2c66affSColin Finck             if (!params[i].attr.IsBasetype && params[i].attr.IsOut && !params[i].attr.IsByValue)
534c2c66affSColin Finck                 NdrClearOutParameters( pStubMsg, pTypeFormat, *(unsigned char **)pArg );
535c2c66affSColin Finck             break;
536c2c66affSColin Finck         default:
537c2c66affSColin Finck             RpcRaiseException(RPC_S_INTERNAL_ERROR);
538c2c66affSColin Finck         }
539c2c66affSColin Finck     }
540c2c66affSColin Finck }
541c2c66affSColin Finck 
type_stack_size(unsigned char fc)542c2c66affSColin Finck static unsigned int type_stack_size(unsigned char fc)
543c2c66affSColin Finck {
544c2c66affSColin Finck     switch (fc)
545c2c66affSColin Finck     {
546b6fdb56bSAmine Khaldi     case FC_BYTE:
547b6fdb56bSAmine Khaldi     case FC_CHAR:
548b6fdb56bSAmine Khaldi     case FC_SMALL:
549b6fdb56bSAmine Khaldi     case FC_USMALL:
550b6fdb56bSAmine Khaldi     case FC_WCHAR:
551b6fdb56bSAmine Khaldi     case FC_SHORT:
552b6fdb56bSAmine Khaldi     case FC_USHORT:
553b6fdb56bSAmine Khaldi     case FC_LONG:
554b6fdb56bSAmine Khaldi     case FC_ULONG:
555b6fdb56bSAmine Khaldi     case FC_INT3264:
556b6fdb56bSAmine Khaldi     case FC_UINT3264:
557b6fdb56bSAmine Khaldi     case FC_ENUM16:
558b6fdb56bSAmine Khaldi     case FC_ENUM32:
559b6fdb56bSAmine Khaldi     case FC_FLOAT:
560b6fdb56bSAmine Khaldi     case FC_ERROR_STATUS_T:
561b6fdb56bSAmine Khaldi     case FC_IGNORE:
562c2c66affSColin Finck         return sizeof(void *);
563b6fdb56bSAmine Khaldi     case FC_DOUBLE:
564c2c66affSColin Finck         return sizeof(double);
565b6fdb56bSAmine Khaldi     case FC_HYPER:
566c2c66affSColin Finck         return sizeof(ULONGLONG);
567c2c66affSColin Finck     default:
568c2c66affSColin Finck         ERR("invalid base type 0x%x\n", fc);
569c2c66affSColin Finck         RpcRaiseException(RPC_S_INTERNAL_ERROR);
570c2c66affSColin Finck     }
571c2c66affSColin Finck }
572c2c66affSColin Finck 
is_by_value(PFORMAT_STRING format)573c2c66affSColin Finck static BOOL is_by_value( PFORMAT_STRING format )
574c2c66affSColin Finck {
575c2c66affSColin Finck     switch (*format)
576c2c66affSColin Finck     {
577b6fdb56bSAmine Khaldi     case FC_USER_MARSHAL:
578b6fdb56bSAmine Khaldi     case FC_STRUCT:
579b6fdb56bSAmine Khaldi     case FC_PSTRUCT:
580b6fdb56bSAmine Khaldi     case FC_CSTRUCT:
581b6fdb56bSAmine Khaldi     case FC_CPSTRUCT:
582b6fdb56bSAmine Khaldi     case FC_CVSTRUCT:
583b6fdb56bSAmine Khaldi     case FC_BOGUS_STRUCT:
584c2c66affSColin Finck         return TRUE;
585c2c66affSColin Finck     default:
586c2c66affSColin Finck         return FALSE;
587c2c66affSColin Finck     }
588c2c66affSColin Finck }
589c2c66affSColin Finck 
convert_old_args(PMIDL_STUB_MESSAGE pStubMsg,PFORMAT_STRING pFormat,unsigned int stack_size,BOOL object_proc,void * buffer,unsigned int size,unsigned int * count)590c2c66affSColin Finck PFORMAT_STRING convert_old_args( PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat,
591c2c66affSColin Finck                                  unsigned int stack_size, BOOL object_proc,
592c2c66affSColin Finck                                  void *buffer, unsigned int size, unsigned int *count )
593c2c66affSColin Finck {
594c2c66affSColin Finck     NDR_PARAM_OIF *args = buffer;
595c2c66affSColin Finck     unsigned int i, stack_offset = object_proc ? sizeof(void *) : 0;
596c2c66affSColin Finck 
597c2c66affSColin Finck     for (i = 0; stack_offset < stack_size; i++)
598c2c66affSColin Finck     {
599c2c66affSColin Finck         const NDR_PARAM_OI_BASETYPE *param = (const NDR_PARAM_OI_BASETYPE *)pFormat;
600c2c66affSColin Finck         const NDR_PARAM_OI_OTHER *other = (const NDR_PARAM_OI_OTHER *)pFormat;
601c2c66affSColin Finck 
602c2c66affSColin Finck         if (i + 1 > size / sizeof(*args))
603c2c66affSColin Finck         {
604c2c66affSColin Finck             FIXME( "%u args not supported\n", i );
605c2c66affSColin Finck             RpcRaiseException( RPC_S_INTERNAL_ERROR );
606c2c66affSColin Finck         }
607c2c66affSColin Finck 
608c2c66affSColin Finck         args[i].stack_offset = stack_offset;
609c2c66affSColin Finck         memset( &args[i].attr, 0, sizeof(args[i].attr) );
610c2c66affSColin Finck 
611c2c66affSColin Finck         switch (param->param_direction)
612c2c66affSColin Finck         {
613b6fdb56bSAmine Khaldi         case FC_IN_PARAM_BASETYPE:
614c2c66affSColin Finck             args[i].attr.IsIn = 1;
615c2c66affSColin Finck             args[i].attr.IsBasetype = 1;
616c2c66affSColin Finck             break;
617b6fdb56bSAmine Khaldi         case FC_RETURN_PARAM_BASETYPE:
618c2c66affSColin Finck             args[i].attr.IsOut = 1;
619c2c66affSColin Finck             args[i].attr.IsReturn = 1;
620c2c66affSColin Finck             args[i].attr.IsBasetype = 1;
621c2c66affSColin Finck             break;
622b6fdb56bSAmine Khaldi         case FC_IN_PARAM:
623c2c66affSColin Finck             args[i].attr.IsIn = 1;
624c2c66affSColin Finck             args[i].attr.MustFree = 1;
625c2c66affSColin Finck             break;
626b6fdb56bSAmine Khaldi         case FC_IN_PARAM_NO_FREE_INST:
627c2c66affSColin Finck             args[i].attr.IsIn = 1;
628c2c66affSColin Finck             args[i].attr.IsDontCallFreeInst = 1;
629c2c66affSColin Finck             break;
630b6fdb56bSAmine Khaldi         case FC_IN_OUT_PARAM:
631c2c66affSColin Finck             args[i].attr.IsIn = 1;
632c2c66affSColin Finck             args[i].attr.IsOut = 1;
633c2c66affSColin Finck             args[i].attr.MustFree = 1;
634c2c66affSColin Finck             break;
635b6fdb56bSAmine Khaldi         case FC_OUT_PARAM:
636c2c66affSColin Finck             args[i].attr.IsOut = 1;
637c2c66affSColin Finck             break;
638b6fdb56bSAmine Khaldi         case FC_RETURN_PARAM:
639c2c66affSColin Finck             args[i].attr.IsOut = 1;
640c2c66affSColin Finck             args[i].attr.IsReturn = 1;
641c2c66affSColin Finck             break;
642c2c66affSColin Finck         }
643c2c66affSColin Finck         if (args[i].attr.IsBasetype)
644c2c66affSColin Finck         {
645c2c66affSColin Finck             args[i].u.type_format_char = param->type_format_char;
646c2c66affSColin Finck             stack_offset += type_stack_size( param->type_format_char );
647c2c66affSColin Finck             pFormat += sizeof(NDR_PARAM_OI_BASETYPE);
648c2c66affSColin Finck         }
649c2c66affSColin Finck         else
650c2c66affSColin Finck         {
651c2c66affSColin Finck             args[i].u.type_offset = other->type_offset;
652c2c66affSColin Finck             args[i].attr.IsByValue = is_by_value( &pStubMsg->StubDesc->pFormatTypes[other->type_offset] );
653c2c66affSColin Finck             stack_offset += other->stack_size * sizeof(void *);
654c2c66affSColin Finck             pFormat += sizeof(NDR_PARAM_OI_OTHER);
655c2c66affSColin Finck         }
656c2c66affSColin Finck     }
657c2c66affSColin Finck     *count = i;
658c2c66affSColin Finck     return (PFORMAT_STRING)args;
659c2c66affSColin Finck }
660c2c66affSColin Finck 
6616feb8748SAmine Khaldi struct ndr_client_call_ctx
6626feb8748SAmine Khaldi {
6636feb8748SAmine Khaldi     MIDL_STUB_MESSAGE *stub_msg;
6646feb8748SAmine Khaldi     INTERPRETER_OPT_FLAGS Oif_flags;
6656feb8748SAmine Khaldi     INTERPRETER_OPT_FLAGS2 ext_flags;
6666feb8748SAmine Khaldi     const NDR_PROC_HEADER *proc_header;
6676feb8748SAmine Khaldi     void *This;
6686feb8748SAmine Khaldi     PFORMAT_STRING handle_format;
6696feb8748SAmine Khaldi     handle_t hbinding;
6706feb8748SAmine Khaldi };
6716feb8748SAmine Khaldi 
ndr_client_call_finally(BOOL normal,void * arg)6726feb8748SAmine Khaldi static void CALLBACK ndr_client_call_finally(BOOL normal, void *arg)
6736feb8748SAmine Khaldi {
6746feb8748SAmine Khaldi     struct ndr_client_call_ctx *ctx = arg;
6756feb8748SAmine Khaldi 
6766feb8748SAmine Khaldi     if (ctx->ext_flags.HasNewCorrDesc)
6776feb8748SAmine Khaldi     {
6786feb8748SAmine Khaldi         /* free extra correlation package */
6796feb8748SAmine Khaldi         NdrCorrelationFree(ctx->stub_msg);
6806feb8748SAmine Khaldi     }
6816feb8748SAmine Khaldi 
6826feb8748SAmine Khaldi     if (ctx->Oif_flags.HasPipes)
6836feb8748SAmine Khaldi     {
6846feb8748SAmine Khaldi         /* NdrPipesDone(...) */
6856feb8748SAmine Khaldi     }
6866feb8748SAmine Khaldi 
6876feb8748SAmine Khaldi     /* free the full pointer translation tables */
6886feb8748SAmine Khaldi     if (ctx->proc_header->Oi_flags & Oi_FULL_PTR_USED)
6896feb8748SAmine Khaldi         NdrFullPointerXlatFree(ctx->stub_msg->FullPtrXlatTables);
6906feb8748SAmine Khaldi 
6916feb8748SAmine Khaldi     /* free marshalling buffer */
6926feb8748SAmine Khaldi     if (ctx->proc_header->Oi_flags & Oi_OBJECT_PROC)
6936feb8748SAmine Khaldi         NdrProxyFreeBuffer(ctx->This, ctx->stub_msg);
6946feb8748SAmine Khaldi     else
6956feb8748SAmine Khaldi     {
6966feb8748SAmine Khaldi         NdrFreeBuffer(ctx->stub_msg);
6976feb8748SAmine Khaldi         client_free_handle(ctx->stub_msg, ctx->proc_header, ctx->handle_format, ctx->hbinding);
6986feb8748SAmine Khaldi     }
6996feb8748SAmine Khaldi }
7006feb8748SAmine Khaldi 
7016feb8748SAmine Khaldi /* Helper for ndr_client_call, to factor out the part that may or may not be
7026feb8748SAmine Khaldi  * guarded by a try/except block. */
do_ndr_client_call(const MIDL_STUB_DESC * stub_desc,const PFORMAT_STRING format,const PFORMAT_STRING handle_format,void ** stack_top,void ** fpu_stack,MIDL_STUB_MESSAGE * stub_msg,unsigned short procedure_number,unsigned short stack_size,unsigned int number_of_params,INTERPRETER_OPT_FLAGS Oif_flags,INTERPRETER_OPT_FLAGS2 ext_flags,const NDR_PROC_HEADER * proc_header)7036feb8748SAmine Khaldi static LONG_PTR do_ndr_client_call( const MIDL_STUB_DESC *stub_desc, const PFORMAT_STRING format,
7046feb8748SAmine Khaldi         const PFORMAT_STRING handle_format, void **stack_top, void **fpu_stack, MIDL_STUB_MESSAGE *stub_msg,
7056feb8748SAmine Khaldi         unsigned short procedure_number, unsigned short stack_size, unsigned int number_of_params,
7066feb8748SAmine Khaldi         INTERPRETER_OPT_FLAGS Oif_flags, INTERPRETER_OPT_FLAGS2 ext_flags, const NDR_PROC_HEADER *proc_header )
7076feb8748SAmine Khaldi {
7086feb8748SAmine Khaldi     struct ndr_client_call_ctx finally_ctx;
7096feb8748SAmine Khaldi     RPC_MESSAGE rpc_msg;
7106feb8748SAmine Khaldi     handle_t hbinding = NULL;
7116feb8748SAmine Khaldi     /* the value to return to the client from the remote procedure */
7126feb8748SAmine Khaldi     LONG_PTR retval = 0;
7136feb8748SAmine Khaldi     /* the pointer to the object when in OLE mode */
7146feb8748SAmine Khaldi     void *This = NULL;
7156feb8748SAmine Khaldi     /* correlation cache */
7166feb8748SAmine Khaldi     ULONG_PTR NdrCorrCache[256];
7176feb8748SAmine Khaldi 
7186feb8748SAmine Khaldi     /* create the full pointer translation tables, if requested */
7196feb8748SAmine Khaldi     if (proc_header->Oi_flags & Oi_FULL_PTR_USED)
7206feb8748SAmine Khaldi         stub_msg->FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
7216feb8748SAmine Khaldi 
7226feb8748SAmine Khaldi     if (proc_header->Oi_flags & Oi_OBJECT_PROC)
7236feb8748SAmine Khaldi     {
7246feb8748SAmine Khaldi         /* object is always the first argument */
7256feb8748SAmine Khaldi         This = stack_top[0];
7266feb8748SAmine Khaldi         NdrProxyInitialize(This, &rpc_msg, stub_msg, stub_desc, procedure_number);
7276feb8748SAmine Khaldi     }
7286feb8748SAmine Khaldi 
7296feb8748SAmine Khaldi     finally_ctx.stub_msg = stub_msg;
7306feb8748SAmine Khaldi     finally_ctx.Oif_flags = Oif_flags;
7316feb8748SAmine Khaldi     finally_ctx.ext_flags = ext_flags;
7326feb8748SAmine Khaldi     finally_ctx.proc_header = proc_header;
7336feb8748SAmine Khaldi     finally_ctx.This = This;
7346feb8748SAmine Khaldi     finally_ctx.handle_format = handle_format;
7356feb8748SAmine Khaldi     finally_ctx.hbinding = hbinding;
7366feb8748SAmine Khaldi 
7376feb8748SAmine Khaldi     __TRY
7386feb8748SAmine Khaldi     {
7396feb8748SAmine Khaldi         if (!(proc_header->Oi_flags & Oi_OBJECT_PROC))
7406feb8748SAmine Khaldi             NdrClientInitializeNew(&rpc_msg, stub_msg, stub_desc, procedure_number);
7416feb8748SAmine Khaldi 
7426feb8748SAmine Khaldi         stub_msg->StackTop = (unsigned char *)stack_top;
7436feb8748SAmine Khaldi 
7446feb8748SAmine Khaldi         /* we only need a handle if this isn't an object method */
7456feb8748SAmine Khaldi         if (!(proc_header->Oi_flags & Oi_OBJECT_PROC))
7466feb8748SAmine Khaldi         {
7476feb8748SAmine Khaldi             hbinding = client_get_handle(stub_msg, proc_header, handle_format);
7486feb8748SAmine Khaldi             if (!hbinding) return 0;
7496feb8748SAmine Khaldi         }
7506feb8748SAmine Khaldi 
7516feb8748SAmine Khaldi         stub_msg->BufferLength = 0;
7526feb8748SAmine Khaldi 
7536feb8748SAmine Khaldi         /* store the RPC flags away */
7546feb8748SAmine Khaldi         if (proc_header->Oi_flags & Oi_HAS_RPCFLAGS)
7556feb8748SAmine Khaldi             rpc_msg.RpcFlags = ((const NDR_PROC_HEADER_RPC *)proc_header)->rpc_flags;
7566feb8748SAmine Khaldi 
7576feb8748SAmine Khaldi         /* use alternate memory allocation routines */
7586feb8748SAmine Khaldi         if (proc_header->Oi_flags & Oi_RPCSS_ALLOC_USED)
7596feb8748SAmine Khaldi             NdrRpcSmSetClientToOsf(stub_msg);
7606feb8748SAmine Khaldi 
7616feb8748SAmine Khaldi         if (Oif_flags.HasPipes)
7626feb8748SAmine Khaldi         {
7636feb8748SAmine Khaldi             FIXME("pipes not supported yet\n");
7646feb8748SAmine Khaldi             RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
7656feb8748SAmine Khaldi             /* init pipes package */
7666feb8748SAmine Khaldi             /* NdrPipesInitialize(...) */
7676feb8748SAmine Khaldi         }
7686feb8748SAmine Khaldi         if (ext_flags.HasNewCorrDesc)
7696feb8748SAmine Khaldi         {
7706feb8748SAmine Khaldi             /* initialize extra correlation package */
7716feb8748SAmine Khaldi             NdrCorrelationInitialize(stub_msg, NdrCorrCache, sizeof(NdrCorrCache), 0);
7726feb8748SAmine Khaldi             if (ext_flags.Unused & 0x2) /* has range on conformance */
7736feb8748SAmine Khaldi                 stub_msg->CorrDespIncrement = 12;
7746feb8748SAmine Khaldi         }
7756feb8748SAmine Khaldi 
7766feb8748SAmine Khaldi         /* order of phases:
7776feb8748SAmine Khaldi          * 1. INITOUT - zero [out] parameters (proxies only)
7786feb8748SAmine Khaldi          * 2. CALCSIZE - calculate the buffer size
7796feb8748SAmine Khaldi          * 3. GETBUFFER - allocate the buffer
7806feb8748SAmine Khaldi          * 4. MARSHAL - marshal [in] params into the buffer
7816feb8748SAmine Khaldi          * 5. SENDRECEIVE - send/receive buffer
7826feb8748SAmine Khaldi          * 6. UNMARSHAL - unmarshal [out] params from buffer
7836feb8748SAmine Khaldi          * 7. FREE - clear [out] parameters (for proxies, and only on error)
7846feb8748SAmine Khaldi          */
7856feb8748SAmine Khaldi 
7866feb8748SAmine Khaldi         /* 1. INITOUT */
7876feb8748SAmine Khaldi         if (proc_header->Oi_flags & Oi_OBJECT_PROC)
7886feb8748SAmine Khaldi         {
7896feb8748SAmine Khaldi             TRACE( "INITOUT\n" );
7906feb8748SAmine Khaldi             client_do_args(stub_msg, format, STUBLESS_INITOUT, fpu_stack,
7916feb8748SAmine Khaldi                            number_of_params, (unsigned char *)&retval);
7926feb8748SAmine Khaldi         }
7936feb8748SAmine Khaldi 
7946feb8748SAmine Khaldi         /* 2. CALCSIZE */
7956feb8748SAmine Khaldi         TRACE( "CALCSIZE\n" );
7966feb8748SAmine Khaldi         client_do_args(stub_msg, format, STUBLESS_CALCSIZE, fpu_stack,
7976feb8748SAmine Khaldi                        number_of_params, (unsigned char *)&retval);
7986feb8748SAmine Khaldi 
7996feb8748SAmine Khaldi         /* 3. GETBUFFER */
8006feb8748SAmine Khaldi         TRACE( "GETBUFFER\n" );
8016feb8748SAmine Khaldi         if (proc_header->Oi_flags & Oi_OBJECT_PROC)
8026feb8748SAmine Khaldi             NdrProxyGetBuffer(This, stub_msg);
8036feb8748SAmine Khaldi         else if (Oif_flags.HasPipes)
8046feb8748SAmine Khaldi             FIXME("pipes not supported yet\n");
8056feb8748SAmine Khaldi         else if (proc_header->handle_type == FC_AUTO_HANDLE)
8066feb8748SAmine Khaldi #if 0
8076feb8748SAmine Khaldi             NdrNsGetBuffer(stub_msg, stub_msg->BufferLength, hBinding);
8086feb8748SAmine Khaldi #else
8096feb8748SAmine Khaldi             FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
8106feb8748SAmine Khaldi #endif
8116feb8748SAmine Khaldi         else
8126feb8748SAmine Khaldi             NdrGetBuffer(stub_msg, stub_msg->BufferLength, hbinding);
8136feb8748SAmine Khaldi 
8146feb8748SAmine Khaldi         /* 4. MARSHAL */
8156feb8748SAmine Khaldi         TRACE( "MARSHAL\n" );
8166feb8748SAmine Khaldi         client_do_args(stub_msg, format, STUBLESS_MARSHAL, fpu_stack,
8176feb8748SAmine Khaldi                        number_of_params, (unsigned char *)&retval);
8186feb8748SAmine Khaldi 
8196feb8748SAmine Khaldi         /* 5. SENDRECEIVE */
8206feb8748SAmine Khaldi         TRACE( "SENDRECEIVE\n" );
8216feb8748SAmine Khaldi         if (proc_header->Oi_flags & Oi_OBJECT_PROC)
8226feb8748SAmine Khaldi             NdrProxySendReceive(This, stub_msg);
8236feb8748SAmine Khaldi         else if (Oif_flags.HasPipes)
8246feb8748SAmine Khaldi             /* NdrPipesSendReceive(...) */
8256feb8748SAmine Khaldi             FIXME("pipes not supported yet\n");
8266feb8748SAmine Khaldi         else if (proc_header->handle_type == FC_AUTO_HANDLE)
8276feb8748SAmine Khaldi #if 0
8286feb8748SAmine Khaldi             NdrNsSendReceive(stub_msg, stub_msg->Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
8296feb8748SAmine Khaldi #else
8306feb8748SAmine Khaldi             FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
8316feb8748SAmine Khaldi #endif
8326feb8748SAmine Khaldi         else
8336feb8748SAmine Khaldi             NdrSendReceive(stub_msg, stub_msg->Buffer);
8346feb8748SAmine Khaldi 
8356feb8748SAmine Khaldi         /* convert strings, floating point values and endianness into our
8366feb8748SAmine Khaldi          * preferred format */
8376feb8748SAmine Khaldi         if ((rpc_msg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
8386feb8748SAmine Khaldi             NdrConvert(stub_msg, format);
8396feb8748SAmine Khaldi 
8406feb8748SAmine Khaldi         /* 6. UNMARSHAL */
8416feb8748SAmine Khaldi         TRACE( "UNMARSHAL\n" );
8426feb8748SAmine Khaldi         client_do_args(stub_msg, format, STUBLESS_UNMARSHAL, fpu_stack,
8436feb8748SAmine Khaldi                        number_of_params, (unsigned char *)&retval);
8446feb8748SAmine Khaldi     }
8456feb8748SAmine Khaldi     __FINALLY_CTX(ndr_client_call_finally, &finally_ctx)
8466feb8748SAmine Khaldi 
8476feb8748SAmine Khaldi     return retval;
8486feb8748SAmine Khaldi }
8496feb8748SAmine Khaldi 
ndr_client_call(PMIDL_STUB_DESC pStubDesc,PFORMAT_STRING pFormat,void ** stack_top,void ** fpu_stack)850c2c66affSColin Finck LONG_PTR CDECL DECLSPEC_HIDDEN ndr_client_call( PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat,
851c2c66affSColin Finck                                                 void **stack_top, void **fpu_stack )
852c2c66affSColin Finck {
853c2c66affSColin Finck     /* pointer to start of stack where arguments start */
854c2c66affSColin Finck     MIDL_STUB_MESSAGE stubMsg;
855c2c66affSColin Finck     /* procedure number */
856c2c66affSColin Finck     unsigned short procedure_number;
857c2c66affSColin Finck     /* size of stack */
858c2c66affSColin Finck     unsigned short stack_size;
859c2c66affSColin Finck     /* number of parameters. optional for client to give it to us */
860c2c66affSColin Finck     unsigned int number_of_params;
861c2c66affSColin Finck     /* cache of Oif_flags from v2 procedure header */
862c2c66affSColin Finck     INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
863c2c66affSColin Finck     /* cache of extension flags from NDR_PROC_HEADER_EXTS */
864c2c66affSColin Finck     INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
865c2c66affSColin Finck     /* header for procedure string */
866c2c66affSColin Finck     const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
867c2c66affSColin Finck     /* the value to return to the client from the remote procedure */
868c2c66affSColin Finck     LONG_PTR RetVal = 0;
869c2c66affSColin Finck     PFORMAT_STRING pHandleFormat;
8706feb8748SAmine Khaldi     NDR_PARAM_OIF old_args[256];
871c2c66affSColin Finck 
872c2c66affSColin Finck     TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
873c2c66affSColin Finck 
874c2c66affSColin Finck     TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
875c2c66affSColin Finck 
876b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_HAS_RPCFLAGS)
877c2c66affSColin Finck     {
878c2c66affSColin Finck         const NDR_PROC_HEADER_RPC *header_rpc = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
879c2c66affSColin Finck         stack_size = header_rpc->stack_size;
880c2c66affSColin Finck         procedure_number = header_rpc->proc_num;
881c2c66affSColin Finck         pFormat += sizeof(NDR_PROC_HEADER_RPC);
882c2c66affSColin Finck     }
883c2c66affSColin Finck     else
884c2c66affSColin Finck     {
885c2c66affSColin Finck         stack_size = pProcHeader->stack_size;
886c2c66affSColin Finck         procedure_number = pProcHeader->proc_num;
887c2c66affSColin Finck         pFormat += sizeof(NDR_PROC_HEADER);
888c2c66affSColin Finck     }
889c2c66affSColin Finck     TRACE("stack size: 0x%x\n", stack_size);
890c2c66affSColin Finck     TRACE("proc num: %d\n", procedure_number);
891c2c66affSColin Finck     TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
892c2c66affSColin Finck     TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
893c2c66affSColin Finck 
894c2c66affSColin Finck     pHandleFormat = pFormat;
895c2c66affSColin Finck 
896c2c66affSColin Finck     /* we only need a handle if this isn't an object method */
897b6fdb56bSAmine Khaldi     if (!(pProcHeader->Oi_flags & Oi_OBJECT_PROC))
8986feb8748SAmine Khaldi         pFormat += get_handle_desc_size(pProcHeader, pFormat);
899c2c66affSColin Finck 
900c2c66affSColin Finck     if (is_oicf_stubdesc(pStubDesc))  /* -Oicf format */
901c2c66affSColin Finck     {
902c2c66affSColin Finck         const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
903c2c66affSColin Finck             (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
904c2c66affSColin Finck 
905c2c66affSColin Finck         Oif_flags = pOIFHeader->Oi2Flags;
906c2c66affSColin Finck         number_of_params = pOIFHeader->number_of_params;
907c2c66affSColin Finck 
908c2c66affSColin Finck         pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
909c2c66affSColin Finck 
910c2c66affSColin Finck         TRACE("Oif_flags = %s\n", debugstr_INTERPRETER_OPT_FLAGS(Oif_flags) );
911c2c66affSColin Finck 
912c2c66affSColin Finck         if (Oif_flags.HasExtensions)
913c2c66affSColin Finck         {
914c2c66affSColin Finck             const NDR_PROC_HEADER_EXTS *pExtensions = (const NDR_PROC_HEADER_EXTS *)pFormat;
915c2c66affSColin Finck             ext_flags = pExtensions->Flags2;
916c2c66affSColin Finck             pFormat += pExtensions->Size;
917c2c66affSColin Finck #ifdef __x86_64__
918c2c66affSColin Finck             if (pExtensions->Size > sizeof(*pExtensions) && fpu_stack)
919c2c66affSColin Finck             {
920c2c66affSColin Finck                 int i;
921c2c66affSColin Finck                 unsigned short fpu_mask = *(unsigned short *)(pExtensions + 1);
922c2c66affSColin Finck                 for (i = 0; i < 4; i++, fpu_mask >>= 2)
923c2c66affSColin Finck                     switch (fpu_mask & 3)
924c2c66affSColin Finck                     {
925c2c66affSColin Finck                     case 1: *(float *)&stack_top[i] = *(float *)&fpu_stack[i]; break;
926c2c66affSColin Finck                     case 2: *(double *)&stack_top[i] = *(double *)&fpu_stack[i]; break;
927c2c66affSColin Finck                     }
928c2c66affSColin Finck             }
929c2c66affSColin Finck #endif
930c2c66affSColin Finck         }
931c2c66affSColin Finck     }
932c2c66affSColin Finck     else
933c2c66affSColin Finck     {
934c2c66affSColin Finck         pFormat = convert_old_args( &stubMsg, pFormat, stack_size,
935b6fdb56bSAmine Khaldi                                     pProcHeader->Oi_flags & Oi_OBJECT_PROC,
9366feb8748SAmine Khaldi                                     old_args, sizeof(old_args), &number_of_params );
937c2c66affSColin Finck     }
938c2c66affSColin Finck 
939b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
940c2c66affSColin Finck     {
941c2c66affSColin Finck         __TRY
942c2c66affSColin Finck         {
9436feb8748SAmine Khaldi             RetVal = do_ndr_client_call(pStubDesc, pFormat, pHandleFormat,
9446feb8748SAmine Khaldi                     stack_top, fpu_stack, &stubMsg, procedure_number, stack_size,
9456feb8748SAmine Khaldi                     number_of_params, Oif_flags, ext_flags, pProcHeader);
946c2c66affSColin Finck         }
947c2c66affSColin Finck         __EXCEPT_ALL
948c2c66affSColin Finck         {
949c2c66affSColin Finck             /* 7. FREE */
950c2c66affSColin Finck             TRACE( "FREE\n" );
951c2c66affSColin Finck             client_do_args(&stubMsg, pFormat, STUBLESS_FREE, fpu_stack,
952c2c66affSColin Finck                            number_of_params, (unsigned char *)&RetVal);
953c2c66affSColin Finck             RetVal = NdrProxyErrorHandler(GetExceptionCode());
954c2c66affSColin Finck         }
9556feb8748SAmine Khaldi         __ENDTRY
9566feb8748SAmine Khaldi     }
9576feb8748SAmine Khaldi     else if (pProcHeader->Oi_flags & Oi_HAS_COMM_OR_FAULT)
9586feb8748SAmine Khaldi     {
9596feb8748SAmine Khaldi         __TRY
9606feb8748SAmine Khaldi         {
9616feb8748SAmine Khaldi             RetVal = do_ndr_client_call(pStubDesc, pFormat, pHandleFormat,
9626feb8748SAmine Khaldi                     stack_top, fpu_stack, &stubMsg, procedure_number, stack_size,
9636feb8748SAmine Khaldi                     number_of_params, Oif_flags, ext_flags, pProcHeader);
9646feb8748SAmine Khaldi         }
9656feb8748SAmine Khaldi         __EXCEPT_ALL
966c2c66affSColin Finck         {
967c2c66affSColin Finck             const COMM_FAULT_OFFSETS *comm_fault_offsets = &pStubDesc->CommFaultOffsets[procedure_number];
968c2c66affSColin Finck             ULONG *comm_status;
969c2c66affSColin Finck             ULONG *fault_status;
970c2c66affSColin Finck 
971c2c66affSColin Finck             TRACE("comm_fault_offsets = {0x%hx, 0x%hx}\n", comm_fault_offsets->CommOffset, comm_fault_offsets->FaultOffset);
972c2c66affSColin Finck 
973c2c66affSColin Finck             if (comm_fault_offsets->CommOffset == -1)
974c2c66affSColin Finck                 comm_status = (ULONG *)&RetVal;
975c2c66affSColin Finck             else if (comm_fault_offsets->CommOffset >= 0)
976c2c66affSColin Finck                 comm_status = *(ULONG **)ARG_FROM_OFFSET(stubMsg.StackTop, comm_fault_offsets->CommOffset);
977c2c66affSColin Finck             else
978c2c66affSColin Finck                 comm_status = NULL;
979c2c66affSColin Finck 
980c2c66affSColin Finck             if (comm_fault_offsets->FaultOffset == -1)
981c2c66affSColin Finck                 fault_status = (ULONG *)&RetVal;
982c2c66affSColin Finck             else if (comm_fault_offsets->FaultOffset >= 0)
983c2c66affSColin Finck                 fault_status = *(ULONG **)ARG_FROM_OFFSET(stubMsg.StackTop, comm_fault_offsets->FaultOffset);
984c2c66affSColin Finck             else
985c2c66affSColin Finck                 fault_status = NULL;
986c2c66affSColin Finck 
987c2c66affSColin Finck             NdrMapCommAndFaultStatus(&stubMsg, comm_status, fault_status,
988c2c66affSColin Finck                                      GetExceptionCode());
989c2c66affSColin Finck         }
990c2c66affSColin Finck         __ENDTRY
991c2c66affSColin Finck     }
992c2c66affSColin Finck     else
993c2c66affSColin Finck     {
9946feb8748SAmine Khaldi         RetVal = do_ndr_client_call(pStubDesc, pFormat, pHandleFormat,
9956feb8748SAmine Khaldi                 stack_top, fpu_stack, &stubMsg, procedure_number, stack_size,
9966feb8748SAmine Khaldi                 number_of_params, Oif_flags, ext_flags, pProcHeader);
997c2c66affSColin Finck     }
998c2c66affSColin Finck 
999c2c66affSColin Finck     TRACE("RetVal = 0x%lx\n", RetVal);
1000c2c66affSColin Finck     return RetVal;
1001c2c66affSColin Finck }
1002c2c66affSColin Finck 
1003c2c66affSColin Finck #ifdef __x86_64__
1004c2c66affSColin Finck 
1005c2c66affSColin Finck __ASM_GLOBAL_FUNC( NdrClientCall2,
1006c2c66affSColin Finck                    "movq %r8,0x18(%rsp)\n\t"
1007c2c66affSColin Finck                    "movq %r9,0x20(%rsp)\n\t"
1008c2c66affSColin Finck                    "leaq 0x18(%rsp),%r8\n\t"
1009c2c66affSColin Finck                    "xorq %r9,%r9\n\t"
1010c2c66affSColin Finck                    "subq $0x28,%rsp\n\t"
1011c2c66affSColin Finck                    __ASM_CFI(".cfi_adjust_cfa_offset 0x28\n\t")
1012c2c66affSColin Finck                    "call " __ASM_NAME("ndr_client_call") "\n\t"
1013c2c66affSColin Finck                    "addq $0x28,%rsp\n\t"
1014c2c66affSColin Finck                    __ASM_CFI(".cfi_adjust_cfa_offset -0x28\n\t")
1015c2c66affSColin Finck                    "ret" );
1016c2c66affSColin Finck 
1017c2c66affSColin Finck #else  /* __x86_64__ */
1018c2c66affSColin Finck 
1019c2c66affSColin Finck /***********************************************************************
1020c2c66affSColin Finck  *            NdrClientCall2 [RPCRT4.@]
1021c2c66affSColin Finck  */
NdrClientCall2(PMIDL_STUB_DESC desc,PFORMAT_STRING format,...)1022c2c66affSColin Finck CLIENT_CALL_RETURN WINAPIV NdrClientCall2( PMIDL_STUB_DESC desc, PFORMAT_STRING format, ... )
1023c2c66affSColin Finck {
1024c2c66affSColin Finck     __ms_va_list args;
1025c2c66affSColin Finck     LONG_PTR ret;
1026c2c66affSColin Finck 
1027c2c66affSColin Finck     __ms_va_start( args, format );
1028c2c66affSColin Finck     ret = ndr_client_call( desc, format, va_arg( args, void ** ), NULL );
1029c2c66affSColin Finck     __ms_va_end( args );
1030c2c66affSColin Finck     return *(CLIENT_CALL_RETURN *)&ret;
1031c2c66affSColin Finck }
1032c2c66affSColin Finck 
1033c2c66affSColin Finck #endif  /* __x86_64__ */
1034c2c66affSColin Finck 
1035c2c66affSColin Finck /* Calls a function with the specified arguments, restoring the stack
1036c2c66affSColin Finck  * properly afterwards as we don't know the calling convention of the
1037c2c66affSColin Finck  * function */
1038c2c66affSColin Finck #if defined __i386__ && defined _MSC_VER
call_server_func(SERVER_ROUTINE func,unsigned char * args,unsigned int stack_size)1039c2c66affSColin Finck __declspec(naked) LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size)
1040c2c66affSColin Finck {
1041c2c66affSColin Finck     __asm
1042c2c66affSColin Finck     {
1043c2c66affSColin Finck         push ebp
1044c2c66affSColin Finck         mov ebp, esp
1045c2c66affSColin Finck         push edi            ; Save registers
1046c2c66affSColin Finck         push esi
1047c2c66affSColin Finck         mov eax, [ebp+16]   ; Get stack size
1048c2c66affSColin Finck         sub esp, eax        ; Make room in stack for arguments
1049c2c66affSColin Finck         and esp, 0xFFFFFFF0
1050c2c66affSColin Finck         mov edi, esp
1051c2c66affSColin Finck         mov ecx, eax
1052c2c66affSColin Finck         mov esi, [ebp+12]
1053c2c66affSColin Finck         shr ecx, 2
1054c2c66affSColin Finck         cld
1055c2c66affSColin Finck         rep movsd           ; Copy dword blocks
1056c2c66affSColin Finck         call [ebp+8]        ; Call function
1057c2c66affSColin Finck         lea esp, [ebp-8]    ; Restore stack
1058c2c66affSColin Finck         pop esi             ; Restore registers
1059c2c66affSColin Finck         pop edi
1060c2c66affSColin Finck         pop ebp
1061c2c66affSColin Finck         ret
1062c2c66affSColin Finck     }
1063c2c66affSColin Finck }
1064c2c66affSColin Finck #elif defined __i386__ && defined __GNUC__
1065c2c66affSColin Finck LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
1066c2c66affSColin Finck __ASM_GLOBAL_FUNC(call_server_func,
1067c2c66affSColin Finck     "pushl %ebp\n\t"
1068c2c66affSColin Finck     __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1069c2c66affSColin Finck     __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1070c2c66affSColin Finck     "movl %esp,%ebp\n\t"
1071c2c66affSColin Finck     __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1072c2c66affSColin Finck     "pushl %edi\n\t"            /* Save registers */
1073c2c66affSColin Finck     __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
1074c2c66affSColin Finck     "pushl %esi\n\t"
1075c2c66affSColin Finck     __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
1076c2c66affSColin Finck     "movl 16(%ebp), %eax\n\t"   /* Get stack size */
1077c2c66affSColin Finck     "subl %eax, %esp\n\t"       /* Make room in stack for arguments */
1078c2c66affSColin Finck     "andl $~15, %esp\n\t"	/* Make sure stack has 16-byte alignment for Mac OS X */
1079c2c66affSColin Finck     "movl %esp, %edi\n\t"
1080c2c66affSColin Finck     "movl %eax, %ecx\n\t"
1081c2c66affSColin Finck     "movl 12(%ebp), %esi\n\t"
1082c2c66affSColin Finck     "shrl $2, %ecx\n\t"         /* divide by 4 */
1083c2c66affSColin Finck     "cld\n\t"
1084c2c66affSColin Finck     "rep; movsl\n\t"            /* Copy dword blocks */
1085c2c66affSColin Finck     "call *8(%ebp)\n\t"         /* Call function */
1086c2c66affSColin Finck     "leal -8(%ebp), %esp\n\t"   /* Restore stack */
1087c2c66affSColin Finck     "popl %esi\n\t"             /* Restore registers */
1088c2c66affSColin Finck     __ASM_CFI(".cfi_same_value %esi\n\t")
1089c2c66affSColin Finck     "popl %edi\n\t"
1090c2c66affSColin Finck     __ASM_CFI(".cfi_same_value %edi\n\t")
1091c2c66affSColin Finck     "popl %ebp\n\t"
1092c2c66affSColin Finck     __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1093c2c66affSColin Finck     __ASM_CFI(".cfi_same_value %ebp\n\t")
1094c2c66affSColin Finck     "ret" )
1095c2c66affSColin Finck #elif defined __x86_64__
1096c2c66affSColin Finck LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
1097c2c66affSColin Finck __ASM_GLOBAL_FUNC( call_server_func,
1098c2c66affSColin Finck                    "pushq %rbp\n\t"
10996feb8748SAmine Khaldi                    __ASM_SEH(".seh_pushreg %rbp\n\t")
1100c2c66affSColin Finck                    __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1101c2c66affSColin Finck                    __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1102c2c66affSColin Finck                    "movq %rsp,%rbp\n\t"
11036feb8748SAmine Khaldi                    __ASM_SEH(".seh_setframe %rbp,0\n\t")
1104c2c66affSColin Finck                    __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
1105c2c66affSColin Finck                    "pushq %rsi\n\t"
11066feb8748SAmine Khaldi                    __ASM_SEH(".seh_pushreg %rsi\n\t")
1107c2c66affSColin Finck                    __ASM_CFI(".cfi_rel_offset %rsi,-8\n\t")
1108c2c66affSColin Finck                    "pushq %rdi\n\t"
11096feb8748SAmine Khaldi                    __ASM_SEH(".seh_pushreg %rdi\n\t")
11106feb8748SAmine Khaldi                    __ASM_SEH(".seh_endprologue\n\t")
1111c2c66affSColin Finck                    __ASM_CFI(".cfi_rel_offset %rdi,-16\n\t")
1112c2c66affSColin Finck                    "movq %rcx,%rax\n\t"   /* function to call */
1113c2c66affSColin Finck                    "movq $32,%rcx\n\t"    /* allocate max(32,stack_size) bytes of stack space */
1114c2c66affSColin Finck                    "cmpq %rcx,%r8\n\t"
1115c2c66affSColin Finck                    "cmovgq %r8,%rcx\n\t"
1116c2c66affSColin Finck                    "subq %rcx,%rsp\n\t"
1117c2c66affSColin Finck                    "andq $~15,%rsp\n\t"
1118c2c66affSColin Finck                    "movq %r8,%rcx\n\t"
1119c2c66affSColin Finck                    "shrq $3,%rcx\n\t"
1120c2c66affSColin Finck                    "movq %rsp,%rdi\n\t"
1121c2c66affSColin Finck                    "movq %rdx,%rsi\n\t"
1122c2c66affSColin Finck                    "rep; movsq\n\t"       /* copy arguments */
1123c2c66affSColin Finck                    "movq 0(%rsp),%rcx\n\t"
1124c2c66affSColin Finck                    "movq 8(%rsp),%rdx\n\t"
1125c2c66affSColin Finck                    "movq 16(%rsp),%r8\n\t"
1126c2c66affSColin Finck                    "movq 24(%rsp),%r9\n\t"
1127c2c66affSColin Finck                    "movq 0(%rsp),%xmm0\n\t"
1128c2c66affSColin Finck                    "movq 8(%rsp),%xmm1\n\t"
1129c2c66affSColin Finck                    "movq 16(%rsp),%xmm2\n\t"
1130c2c66affSColin Finck                    "movq 24(%rsp),%xmm3\n\t"
1131c2c66affSColin Finck                    "callq *%rax\n\t"
1132c2c66affSColin Finck                    "leaq -16(%rbp),%rsp\n\t"  /* restore stack */
1133c2c66affSColin Finck                    "popq %rdi\n\t"
1134c2c66affSColin Finck                    __ASM_CFI(".cfi_same_value %rdi\n\t")
1135c2c66affSColin Finck                    "popq %rsi\n\t"
1136c2c66affSColin Finck                    __ASM_CFI(".cfi_same_value %rsi\n\t")
1137c2c66affSColin Finck                    __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
1138c2c66affSColin Finck                    "popq %rbp\n\t"
1139c2c66affSColin Finck                    __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1140c2c66affSColin Finck                    __ASM_CFI(".cfi_same_value %rbp\n\t")
1141c2c66affSColin Finck                    "ret")
1142c2c66affSColin Finck #elif defined __arm__
1143c2c66affSColin Finck LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char *args, unsigned int stack_size);
1144c2c66affSColin Finck __ASM_GLOBAL_FUNC( call_server_func,
1145c2c66affSColin Finck                    ".arm\n\t"
1146c2c66affSColin Finck                    "push {r4, r5, LR}\n\t"
1147c2c66affSColin Finck                    "mov r4, r0\n\t"
1148c2c66affSColin Finck                    "mov r5, SP\n\t"
1149c2c66affSColin Finck                    "lsr r3, r2, #2\n\t"
1150c2c66affSColin Finck                    "cmp r3, #0\n\t"
1151c2c66affSColin Finck                    "beq 5f\n\t"
1152c2c66affSColin Finck                    "sub SP, SP, r2\n\t"
1153c2c66affSColin Finck                    "tst r3, #1\n\t"
1154c2c66affSColin Finck                    "subeq SP, SP, #4\n\t"
1155c2c66affSColin Finck                    "1:\tsub r2, r2, #4\n\t"
1156c2c66affSColin Finck                    "ldr r0, [r1, r2]\n\t"
1157c2c66affSColin Finck                    "str r0, [SP, r2]\n\t"
1158c2c66affSColin Finck                    "cmp r2, #0\n\t"
1159c2c66affSColin Finck                    "bgt 1b\n\t"
1160c2c66affSColin Finck                    "cmp r3, #1\n\t"
1161c2c66affSColin Finck                    "bgt 2f\n\t"
1162c2c66affSColin Finck                    "pop {r0}\n\t"
1163c2c66affSColin Finck                    "b 5f\n\t"
1164c2c66affSColin Finck                    "2:\tcmp r3, #2\n\t"
1165c2c66affSColin Finck                    "bgt 3f\n\t"
1166c2c66affSColin Finck                    "pop {r0-r1}\n\t"
1167c2c66affSColin Finck                    "b 5f\n\t"
1168c2c66affSColin Finck                    "3:\tcmp r3, #3\n\t"
1169c2c66affSColin Finck                    "bgt 4f\n\t"
1170c2c66affSColin Finck                    "pop {r0-r2}\n\t"
1171c2c66affSColin Finck                    "b 5f\n\t"
1172c2c66affSColin Finck                    "4:\tpop {r0-r3}\n\t"
1173c2c66affSColin Finck                    "5:\tblx r4\n\t"
1174c2c66affSColin Finck                    "mov SP, r5\n\t"
1175c2c66affSColin Finck                    "pop {r4, r5, PC}" )
11766feb8748SAmine Khaldi #elif defined __aarch64__
11776feb8748SAmine Khaldi LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char *args, unsigned int stack_size);
11786feb8748SAmine Khaldi __ASM_GLOBAL_FUNC( call_server_func,
11796feb8748SAmine Khaldi                    "stp x29, x30, [sp, #-16]!\n\t"
11806feb8748SAmine Khaldi                    "mov x29, sp\n\t"
11816feb8748SAmine Khaldi                    "add x3, x2, #15\n\t"
11826feb8748SAmine Khaldi                    "lsr x3, x3, #4\n\t"
11836feb8748SAmine Khaldi                    "sub sp, sp, x3, lsl #4\n\t"
11846feb8748SAmine Khaldi                    "cbz x2, 2f\n"
11856feb8748SAmine Khaldi                    "1:\tsub x2, x2, #8\n\t"
11866feb8748SAmine Khaldi                    "ldr x4, [x1, x2]\n\t"
11876feb8748SAmine Khaldi                    "str x4, [sp, x2]\n\t"
11886feb8748SAmine Khaldi                    "cbnz x2, 1b\n"
11896feb8748SAmine Khaldi                    "2:\tmov x8, x0\n\t"
11906feb8748SAmine Khaldi                    "cbz x3, 3f\n\t"
11916feb8748SAmine Khaldi                    "ldp x0, x1, [sp], #16\n\t"
11926feb8748SAmine Khaldi                    "cmp x3, #1\n\t"
11936feb8748SAmine Khaldi                    "b.le 3f\n\t"
11946feb8748SAmine Khaldi                    "ldp x2, x3, [sp], #16\n\t"
11956feb8748SAmine Khaldi                    "cmp x3, #2\n\t"
11966feb8748SAmine Khaldi                    "b.le 3f\n\t"
11976feb8748SAmine Khaldi                    "ldp x4, x5, [sp], #16\n\t"
11986feb8748SAmine Khaldi                    "cmp x3, #3\n\t"
11996feb8748SAmine Khaldi                    "b.le 3f\n\t"
12006feb8748SAmine Khaldi                    "ldp x6, x7, [sp], #16\n"
12016feb8748SAmine Khaldi                    "3:\tblr x8\n\t"
12026feb8748SAmine Khaldi                    "mov sp, x29\n\t"
12036feb8748SAmine Khaldi                    "ldp x29, x30, [sp], #16\n\t"
12046feb8748SAmine Khaldi                    "ret" )
1205c2c66affSColin Finck #else
1206c2c66affSColin Finck #warning call_server_func not implemented for your architecture
1207c2c66affSColin Finck LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned short stack_size)
1208c2c66affSColin Finck {
1209c2c66affSColin Finck     FIXME("Not implemented for your architecture\n");
1210c2c66affSColin Finck     return 0;
1211c2c66affSColin Finck }
1212c2c66affSColin Finck #endif
1213c2c66affSColin Finck 
stub_do_args(MIDL_STUB_MESSAGE * pStubMsg,PFORMAT_STRING pFormat,enum stubless_phase phase,unsigned short number_of_params)1214c2c66affSColin Finck static LONG_PTR *stub_do_args(MIDL_STUB_MESSAGE *pStubMsg,
1215c2c66affSColin Finck                               PFORMAT_STRING pFormat, enum stubless_phase phase,
1216c2c66affSColin Finck                               unsigned short number_of_params)
1217c2c66affSColin Finck {
1218c2c66affSColin Finck     const NDR_PARAM_OIF *params = (const NDR_PARAM_OIF *)pFormat;
1219c2c66affSColin Finck     unsigned int i;
1220c2c66affSColin Finck     LONG_PTR *retval_ptr = NULL;
1221c2c66affSColin Finck 
1222c2c66affSColin Finck     for (i = 0; i < number_of_params; i++)
1223c2c66affSColin Finck     {
1224c2c66affSColin Finck         unsigned char *pArg = pStubMsg->StackTop + params[i].stack_offset;
1225c2c66affSColin Finck         const unsigned char *pTypeFormat = &pStubMsg->StubDesc->pFormatTypes[params[i].u.type_offset];
1226c2c66affSColin Finck 
1227c2c66affSColin Finck         TRACE("param[%d]: %p -> %p type %02x %s\n", i,
1228c2c66affSColin Finck               pArg, *(unsigned char **)pArg,
1229c2c66affSColin Finck               params[i].attr.IsBasetype ? params[i].u.type_format_char : *pTypeFormat,
1230c2c66affSColin Finck               debugstr_PROC_PF( params[i].attr ));
1231c2c66affSColin Finck 
1232c2c66affSColin Finck         switch (phase)
1233c2c66affSColin Finck         {
1234c2c66affSColin Finck         case STUBLESS_MARSHAL:
1235c2c66affSColin Finck             if (params[i].attr.IsOut || params[i].attr.IsReturn)
1236c2c66affSColin Finck                 call_marshaller(pStubMsg, pArg, &params[i]);
1237c2c66affSColin Finck             break;
1238c2c66affSColin Finck         case STUBLESS_MUSTFREE:
1239c2c66affSColin Finck             if (params[i].attr.MustFree)
1240c2c66affSColin Finck             {
1241c2c66affSColin Finck                 call_freer(pStubMsg, pArg, &params[i]);
1242c2c66affSColin Finck             }
1243c2c66affSColin Finck             break;
1244c2c66affSColin Finck         case STUBLESS_FREE:
1245c2c66affSColin Finck             if (params[i].attr.ServerAllocSize)
1246c2c66affSColin Finck             {
1247c2c66affSColin Finck                 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
1248c2c66affSColin Finck             }
124990155f74SAmine Khaldi             else if (param_needs_alloc(params[i].attr) &&
125090155f74SAmine Khaldi                      (!params[i].attr.MustFree || params[i].attr.IsSimpleRef))
1251c2c66affSColin Finck             {
1252b6fdb56bSAmine Khaldi                 if (*pTypeFormat != FC_BIND_CONTEXT) pStubMsg->pfnFree(*(void **)pArg);
1253c2c66affSColin Finck             }
1254c2c66affSColin Finck             break;
1255c2c66affSColin Finck         case STUBLESS_INITOUT:
125690155f74SAmine Khaldi             if (param_needs_alloc(params[i].attr) && !params[i].attr.ServerAllocSize)
1257c2c66affSColin Finck             {
1258b6fdb56bSAmine Khaldi                 if (*pTypeFormat == FC_BIND_CONTEXT)
1259c2c66affSColin Finck                 {
1260c2c66affSColin Finck                     NDR_SCONTEXT ctxt = NdrContextHandleInitialize(pStubMsg, pTypeFormat);
1261c2c66affSColin Finck                     *(void **)pArg = NDRSContextValue(ctxt);
12626feb8748SAmine Khaldi                     if (params[i].attr.IsReturn) retval_ptr = (LONG_PTR *)NDRSContextValue(ctxt);
1263c2c66affSColin Finck                 }
1264c2c66affSColin Finck                 else
1265c2c66affSColin Finck                 {
1266c2c66affSColin Finck                     DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1267c2c66affSColin Finck                     if (size)
1268c2c66affSColin Finck                     {
1269c2c66affSColin Finck                         *(void **)pArg = NdrAllocate(pStubMsg, size);
1270c2c66affSColin Finck                         memset(*(void **)pArg, 0, size);
1271c2c66affSColin Finck                     }
1272c2c66affSColin Finck                 }
1273c2c66affSColin Finck             }
12746feb8748SAmine Khaldi             if (!retval_ptr && params[i].attr.IsReturn) retval_ptr = (LONG_PTR *)pArg;
1275c2c66affSColin Finck             break;
1276c2c66affSColin Finck         case STUBLESS_UNMARSHAL:
1277c2c66affSColin Finck             if (params[i].attr.ServerAllocSize)
1278c2c66affSColin Finck                 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1279c2c66affSColin Finck                                            params[i].attr.ServerAllocSize * 8);
1280c2c66affSColin Finck 
1281c2c66affSColin Finck             if (params[i].attr.IsIn)
1282c2c66affSColin Finck                 call_unmarshaller(pStubMsg, &pArg, &params[i], 0);
1283c2c66affSColin Finck             break;
1284c2c66affSColin Finck         case STUBLESS_CALCSIZE:
1285c2c66affSColin Finck             if (params[i].attr.IsOut || params[i].attr.IsReturn)
1286c2c66affSColin Finck                 call_buffer_sizer(pStubMsg, pArg, &params[i]);
1287c2c66affSColin Finck             break;
1288c2c66affSColin Finck         default:
1289c2c66affSColin Finck             RpcRaiseException(RPC_S_INTERNAL_ERROR);
1290c2c66affSColin Finck         }
1291c2c66affSColin Finck         TRACE("\tmemory addr (after): %p -> %p\n", pArg, *(unsigned char **)pArg);
1292c2c66affSColin Finck     }
1293c2c66affSColin Finck     return retval_ptr;
1294c2c66affSColin Finck }
1295c2c66affSColin Finck 
1296c2c66affSColin Finck /***********************************************************************
1297c2c66affSColin Finck  *            NdrStubCall2 [RPCRT4.@]
1298c2c66affSColin Finck  *
1299c2c66affSColin Finck  * Unmarshals [in] parameters, calls either a method in an object or a server
1300c2c66affSColin Finck  * function, marshals any [out] parameters and frees any allocated data.
1301c2c66affSColin Finck  *
1302c2c66affSColin Finck  * NOTES
1303c2c66affSColin Finck  *  Used by stubless MIDL-generated code.
1304c2c66affSColin Finck  */
NdrStubCall2(struct IRpcStubBuffer * pThis,struct IRpcChannelBuffer * pChannel,PRPC_MESSAGE pRpcMsg,DWORD * pdwStubPhase)1305c2c66affSColin Finck LONG WINAPI NdrStubCall2(
1306c2c66affSColin Finck     struct IRpcStubBuffer * pThis,
1307c2c66affSColin Finck     struct IRpcChannelBuffer * pChannel,
1308c2c66affSColin Finck     PRPC_MESSAGE pRpcMsg,
1309c2c66affSColin Finck     DWORD * pdwStubPhase)
1310c2c66affSColin Finck {
1311c2c66affSColin Finck     const MIDL_SERVER_INFO *pServerInfo;
1312c2c66affSColin Finck     const MIDL_STUB_DESC *pStubDesc;
1313c2c66affSColin Finck     PFORMAT_STRING pFormat;
1314c2c66affSColin Finck     MIDL_STUB_MESSAGE stubMsg;
1315c2c66affSColin Finck     /* pointer to start of stack to pass into stub implementation */
1316c2c66affSColin Finck     unsigned char * args;
1317c2c66affSColin Finck     /* size of stack */
1318c2c66affSColin Finck     unsigned short stack_size;
1319c2c66affSColin Finck     /* number of parameters. optional for client to give it to us */
1320c2c66affSColin Finck     unsigned int number_of_params;
1321c2c66affSColin Finck     /* cache of Oif_flags from v2 procedure header */
1322c2c66affSColin Finck     INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1323c2c66affSColin Finck     /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1324c2c66affSColin Finck     INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1325c2c66affSColin Finck     /* the type of pass we are currently doing */
1326c2c66affSColin Finck     enum stubless_phase phase;
1327c2c66affSColin Finck     /* header for procedure string */
1328c2c66affSColin Finck     const NDR_PROC_HEADER *pProcHeader;
1329c2c66affSColin Finck     /* location to put retval into */
1330c2c66affSColin Finck     LONG_PTR *retval_ptr = NULL;
1331c2c66affSColin Finck     /* correlation cache */
1332c2c66affSColin Finck     ULONG_PTR NdrCorrCache[256];
1333*e54b32b4SEric Kohl     unsigned short BindingHandleOffset = (USHORT)-1;
1334c2c66affSColin Finck 
1335c2c66affSColin Finck     TRACE("pThis %p, pChannel %p, pRpcMsg %p, pdwStubPhase %p\n", pThis, pChannel, pRpcMsg, pdwStubPhase);
1336c2c66affSColin Finck 
1337c2c66affSColin Finck     if (pThis)
1338c2c66affSColin Finck         pServerInfo = CStdStubBuffer_GetServerInfo(pThis);
1339c2c66affSColin Finck     else
1340c2c66affSColin Finck         pServerInfo = ((RPC_SERVER_INTERFACE *)pRpcMsg->RpcInterfaceInformation)->InterpreterInfo;
1341c2c66affSColin Finck 
1342c2c66affSColin Finck     pStubDesc = pServerInfo->pStubDesc;
1343c2c66affSColin Finck     pFormat = pServerInfo->ProcString + pServerInfo->FmtStringOffset[pRpcMsg->ProcNum];
1344c2c66affSColin Finck     pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1345c2c66affSColin Finck 
1346c2c66affSColin Finck     TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
1347c2c66affSColin Finck 
1348b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_HAS_RPCFLAGS)
1349c2c66affSColin Finck     {
1350c2c66affSColin Finck         const NDR_PROC_HEADER_RPC *header_rpc = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1351c2c66affSColin Finck         stack_size = header_rpc->stack_size;
1352c2c66affSColin Finck         pFormat += sizeof(NDR_PROC_HEADER_RPC);
1353c2c66affSColin Finck 
1354c2c66affSColin Finck     }
1355c2c66affSColin Finck     else
1356c2c66affSColin Finck     {
1357c2c66affSColin Finck         stack_size = pProcHeader->stack_size;
1358c2c66affSColin Finck         pFormat += sizeof(NDR_PROC_HEADER);
1359c2c66affSColin Finck     }
1360c2c66affSColin Finck 
1361c2c66affSColin Finck     TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1362c2c66affSColin Finck 
1363c2c66affSColin Finck     /* binding */
1364c2c66affSColin Finck     switch (pProcHeader->handle_type)
1365c2c66affSColin Finck     {
1366c2c66affSColin Finck     /* explicit binding: parse additional section */
1367b6fdb56bSAmine Khaldi     case 0:
1368c2c66affSColin Finck         switch (*pFormat) /* handle_type */
1369c2c66affSColin Finck         {
1370b6fdb56bSAmine Khaldi         case FC_BIND_PRIMITIVE: /* explicit primitive */
1371*e54b32b4SEric Kohl             BindingHandleOffset = ((NDR_EHD_PRIMITIVE*)pFormat)->offset;
1372c2c66affSColin Finck             pFormat += sizeof(NDR_EHD_PRIMITIVE);
1373c2c66affSColin Finck             break;
1374b6fdb56bSAmine Khaldi         case FC_BIND_GENERIC: /* explicit generic */
1375*e54b32b4SEric Kohl             BindingHandleOffset = ((NDR_EHD_GENERIC*)pFormat)->offset;
1376c2c66affSColin Finck             pFormat += sizeof(NDR_EHD_GENERIC);
1377c2c66affSColin Finck             break;
1378b6fdb56bSAmine Khaldi         case FC_BIND_CONTEXT: /* explicit context */
1379*e54b32b4SEric Kohl             BindingHandleOffset = ((NDR_EHD_CONTEXT*)pFormat)->offset;
1380c2c66affSColin Finck             pFormat += sizeof(NDR_EHD_CONTEXT);
1381c2c66affSColin Finck             break;
1382c2c66affSColin Finck         default:
1383c2c66affSColin Finck             ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1384c2c66affSColin Finck             RpcRaiseException(RPC_X_BAD_STUB_DATA);
1385c2c66affSColin Finck         }
1386c2c66affSColin Finck         break;
1387b6fdb56bSAmine Khaldi     case FC_BIND_GENERIC: /* implicit generic */
1388b6fdb56bSAmine Khaldi     case FC_BIND_PRIMITIVE: /* implicit primitive */
1389b6fdb56bSAmine Khaldi     case FC_CALLBACK_HANDLE: /* implicit callback */
1390b6fdb56bSAmine Khaldi     case FC_AUTO_HANDLE: /* implicit auto handle */
1391c2c66affSColin Finck         break;
1392c2c66affSColin Finck     default:
1393c2c66affSColin Finck         ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1394c2c66affSColin Finck         RpcRaiseException(RPC_X_BAD_STUB_DATA);
1395c2c66affSColin Finck     }
1396c2c66affSColin Finck 
1397b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
1398c2c66affSColin Finck         NdrStubInitialize(pRpcMsg, &stubMsg, pStubDesc, pChannel);
1399c2c66affSColin Finck     else
1400c2c66affSColin Finck         NdrServerInitializeNew(pRpcMsg, &stubMsg, pStubDesc);
1401c2c66affSColin Finck 
1402c2c66affSColin Finck     /* create the full pointer translation tables, if requested */
1403b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_FULL_PTR_USED)
1404c2c66affSColin Finck         stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_SERVER);
1405c2c66affSColin Finck 
1406c2c66affSColin Finck     /* store the RPC flags away */
1407b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_HAS_RPCFLAGS)
1408c2c66affSColin Finck         pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1409c2c66affSColin Finck 
1410c2c66affSColin Finck     /* use alternate memory allocation routines */
1411b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_RPCSS_ALLOC_USED)
1412c2c66affSColin Finck #if 0
1413c2c66affSColin Finck           NdrRpcSsEnableAllocate(&stubMsg);
1414c2c66affSColin Finck #else
1415c2c66affSColin Finck           FIXME("Set RPCSS memory allocation routines\n");
1416c2c66affSColin Finck #endif
1417c2c66affSColin Finck 
1418c2c66affSColin Finck     TRACE("allocating memory for stack of size %x\n", stack_size);
1419c2c66affSColin Finck 
1420c2c66affSColin Finck     args = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, stack_size);
1421c2c66affSColin Finck     stubMsg.StackTop = args; /* used by conformance of top-level objects */
1422c2c66affSColin Finck 
1423c2c66affSColin Finck     /* add the implicit This pointer as the first arg to the function if we
1424c2c66affSColin Finck      * are calling an object method */
1425c2c66affSColin Finck     if (pThis)
1426c2c66affSColin Finck         *(void **)args = ((CStdStubBuffer *)pThis)->pvServerObject;
1427c2c66affSColin Finck 
1428*e54b32b4SEric Kohl     /* add the binding handle to the stack if we are using explicit binding handles */
1429*e54b32b4SEric Kohl     if (BindingHandleOffset != (USHORT)-1)
1430*e54b32b4SEric Kohl         *(RPC_BINDING_HANDLE*)&(args[BindingHandleOffset]) = pRpcMsg->Handle;
1431*e54b32b4SEric Kohl 
1432c2c66affSColin Finck     if (is_oicf_stubdesc(pStubDesc))
1433c2c66affSColin Finck     {
1434c2c66affSColin Finck         const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader = (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
1435c2c66affSColin Finck 
1436c2c66affSColin Finck         Oif_flags = pOIFHeader->Oi2Flags;
1437c2c66affSColin Finck         number_of_params = pOIFHeader->number_of_params;
1438c2c66affSColin Finck 
1439c2c66affSColin Finck         pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1440c2c66affSColin Finck 
1441c2c66affSColin Finck         TRACE("Oif_flags = %s\n", debugstr_INTERPRETER_OPT_FLAGS(Oif_flags) );
1442c2c66affSColin Finck 
1443c2c66affSColin Finck         if (Oif_flags.HasExtensions)
1444c2c66affSColin Finck         {
1445c2c66affSColin Finck             const NDR_PROC_HEADER_EXTS *pExtensions = (const NDR_PROC_HEADER_EXTS *)pFormat;
1446c2c66affSColin Finck             ext_flags = pExtensions->Flags2;
1447c2c66affSColin Finck             pFormat += pExtensions->Size;
1448c2c66affSColin Finck         }
1449c2c66affSColin Finck 
1450c2c66affSColin Finck         if (Oif_flags.HasPipes)
1451c2c66affSColin Finck         {
1452c2c66affSColin Finck             FIXME("pipes not supported yet\n");
1453c2c66affSColin Finck             RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1454c2c66affSColin Finck             /* init pipes package */
1455c2c66affSColin Finck             /* NdrPipesInitialize(...) */
1456c2c66affSColin Finck         }
1457c2c66affSColin Finck         if (ext_flags.HasNewCorrDesc)
1458c2c66affSColin Finck         {
1459c2c66affSColin Finck             /* initialize extra correlation package */
1460c2c66affSColin Finck             NdrCorrelationInitialize(&stubMsg, NdrCorrCache, sizeof(NdrCorrCache), 0);
1461c2c66affSColin Finck             if (ext_flags.Unused & 0x2) /* has range on conformance */
1462c2c66affSColin Finck                 stubMsg.CorrDespIncrement = 12;
1463c2c66affSColin Finck         }
1464c2c66affSColin Finck     }
1465c2c66affSColin Finck     else
1466c2c66affSColin Finck     {
1467c2c66affSColin Finck         pFormat = convert_old_args( &stubMsg, pFormat, stack_size,
1468b6fdb56bSAmine Khaldi                                     pProcHeader->Oi_flags & Oi_OBJECT_PROC,
1469c2c66affSColin Finck                                     /* reuse the correlation cache, it's not needed for v1 format */
1470c2c66affSColin Finck                                     NdrCorrCache, sizeof(NdrCorrCache), &number_of_params );
1471c2c66affSColin Finck     }
1472c2c66affSColin Finck 
1473c2c66affSColin Finck     /* convert strings, floating point values and endianness into our
1474c2c66affSColin Finck      * preferred format */
1475c2c66affSColin Finck     if ((pRpcMsg->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1476c2c66affSColin Finck         NdrConvert(&stubMsg, pFormat);
1477c2c66affSColin Finck 
1478c2c66affSColin Finck     for (phase = STUBLESS_UNMARSHAL; phase <= STUBLESS_FREE; phase++)
1479c2c66affSColin Finck     {
1480c2c66affSColin Finck         TRACE("phase = %d\n", phase);
1481c2c66affSColin Finck         switch (phase)
1482c2c66affSColin Finck         {
1483c2c66affSColin Finck         case STUBLESS_CALLSERVER:
1484c2c66affSColin Finck             /* call the server function */
1485c2c66affSColin Finck             if (pServerInfo->ThunkTable && pServerInfo->ThunkTable[pRpcMsg->ProcNum])
1486c2c66affSColin Finck                 pServerInfo->ThunkTable[pRpcMsg->ProcNum](&stubMsg);
1487c2c66affSColin Finck             else
1488c2c66affSColin Finck             {
1489c2c66affSColin Finck                 SERVER_ROUTINE func;
1490c2c66affSColin Finck                 LONG_PTR retval;
1491c2c66affSColin Finck 
1492b6fdb56bSAmine Khaldi                 if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
1493c2c66affSColin Finck                 {
1494c2c66affSColin Finck                     SERVER_ROUTINE *vtbl = *(SERVER_ROUTINE **)((CStdStubBuffer *)pThis)->pvServerObject;
1495c2c66affSColin Finck                     func = vtbl[pRpcMsg->ProcNum];
1496c2c66affSColin Finck                 }
1497c2c66affSColin Finck                 else
1498c2c66affSColin Finck                     func = pServerInfo->DispatchTable[pRpcMsg->ProcNum];
1499c2c66affSColin Finck 
1500c2c66affSColin Finck                 /* FIXME: what happens with return values that don't fit into a single register on x86? */
1501c2c66affSColin Finck                 retval = call_server_func(func, args, stack_size);
1502c2c66affSColin Finck 
1503c2c66affSColin Finck                 if (retval_ptr)
1504c2c66affSColin Finck                 {
1505c2c66affSColin Finck                     TRACE("stub implementation returned 0x%lx\n", retval);
1506c2c66affSColin Finck                     *retval_ptr = retval;
1507c2c66affSColin Finck                 }
1508c2c66affSColin Finck                 else
1509c2c66affSColin Finck                     TRACE("void stub implementation\n");
1510c2c66affSColin Finck             }
1511c2c66affSColin Finck 
1512c2c66affSColin Finck             stubMsg.Buffer = NULL;
1513c2c66affSColin Finck             stubMsg.BufferLength = 0;
1514c2c66affSColin Finck 
1515c2c66affSColin Finck             break;
1516c2c66affSColin Finck         case STUBLESS_GETBUFFER:
1517b6fdb56bSAmine Khaldi             if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
1518c2c66affSColin Finck                 NdrStubGetBuffer(pThis, pChannel, &stubMsg);
1519c2c66affSColin Finck             else
1520c2c66affSColin Finck             {
1521c2c66affSColin Finck                 RPC_STATUS Status;
1522c2c66affSColin Finck 
1523c2c66affSColin Finck                 pRpcMsg->BufferLength = stubMsg.BufferLength;
1524c2c66affSColin Finck                 /* allocate buffer for [out] and [ret] params */
1525c2c66affSColin Finck                 Status = I_RpcGetBuffer(pRpcMsg);
1526c2c66affSColin Finck                 if (Status)
1527c2c66affSColin Finck                     RpcRaiseException(Status);
1528c2c66affSColin Finck                 stubMsg.Buffer = pRpcMsg->Buffer;
1529c2c66affSColin Finck             }
1530c2c66affSColin Finck             break;
1531c2c66affSColin Finck         case STUBLESS_UNMARSHAL:
1532c2c66affSColin Finck         case STUBLESS_INITOUT:
1533c2c66affSColin Finck         case STUBLESS_CALCSIZE:
1534c2c66affSColin Finck         case STUBLESS_MARSHAL:
1535c2c66affSColin Finck         case STUBLESS_MUSTFREE:
1536c2c66affSColin Finck         case STUBLESS_FREE:
1537c2c66affSColin Finck             retval_ptr = stub_do_args(&stubMsg, pFormat, phase, number_of_params);
1538c2c66affSColin Finck             break;
1539c2c66affSColin Finck         default:
1540c2c66affSColin Finck             ERR("shouldn't reach here. phase %d\n", phase);
1541c2c66affSColin Finck             break;
1542c2c66affSColin Finck         }
1543c2c66affSColin Finck     }
1544c2c66affSColin Finck 
1545c2c66affSColin Finck     pRpcMsg->BufferLength = (unsigned int)(stubMsg.Buffer - (unsigned char *)pRpcMsg->Buffer);
1546c2c66affSColin Finck 
1547c2c66affSColin Finck     if (ext_flags.HasNewCorrDesc)
1548c2c66affSColin Finck     {
1549c2c66affSColin Finck         /* free extra correlation package */
1550c2c66affSColin Finck         NdrCorrelationFree(&stubMsg);
1551c2c66affSColin Finck     }
1552c2c66affSColin Finck 
1553c2c66affSColin Finck     if (Oif_flags.HasPipes)
1554c2c66affSColin Finck     {
1555c2c66affSColin Finck         /* NdrPipesDone(...) */
1556c2c66affSColin Finck     }
1557c2c66affSColin Finck 
1558c2c66affSColin Finck     /* free the full pointer translation tables */
1559b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_FULL_PTR_USED)
1560c2c66affSColin Finck         NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
1561c2c66affSColin Finck 
1562c2c66affSColin Finck     /* free server function stack */
1563c2c66affSColin Finck     HeapFree(GetProcessHeap(), 0, args);
1564c2c66affSColin Finck 
1565c2c66affSColin Finck     return S_OK;
1566c2c66affSColin Finck }
1567c2c66affSColin Finck 
1568c2c66affSColin Finck /***********************************************************************
1569c2c66affSColin Finck  *            NdrServerCall2 [RPCRT4.@]
1570c2c66affSColin Finck  */
NdrServerCall2(PRPC_MESSAGE pRpcMsg)1571c2c66affSColin Finck void WINAPI NdrServerCall2(PRPC_MESSAGE pRpcMsg)
1572c2c66affSColin Finck {
1573c2c66affSColin Finck     DWORD dwPhase;
1574c2c66affSColin Finck     NdrStubCall2(NULL, NULL, pRpcMsg, &dwPhase);
1575c2c66affSColin Finck }
1576c2c66affSColin Finck 
1577c2c66affSColin Finck /***********************************************************************
1578c2c66affSColin Finck  *            NdrStubCall [RPCRT4.@]
1579c2c66affSColin Finck  */
NdrStubCall(struct IRpcStubBuffer * This,struct IRpcChannelBuffer * channel,PRPC_MESSAGE msg,DWORD * phase)1580c2c66affSColin Finck LONG WINAPI NdrStubCall( struct IRpcStubBuffer *This, struct IRpcChannelBuffer *channel,
1581c2c66affSColin Finck                          PRPC_MESSAGE msg, DWORD *phase )
1582c2c66affSColin Finck {
1583c2c66affSColin Finck     return NdrStubCall2( This, channel, msg, phase );
1584c2c66affSColin Finck }
1585c2c66affSColin Finck 
1586c2c66affSColin Finck /***********************************************************************
1587c2c66affSColin Finck  *            NdrServerCall [RPCRT4.@]
1588c2c66affSColin Finck  */
NdrServerCall(PRPC_MESSAGE msg)1589c2c66affSColin Finck void WINAPI NdrServerCall( PRPC_MESSAGE msg )
1590c2c66affSColin Finck {
1591c2c66affSColin Finck     DWORD phase;
1592c2c66affSColin Finck     NdrStubCall( NULL, NULL, msg, &phase );
1593c2c66affSColin Finck }
1594c2c66affSColin Finck 
15956feb8748SAmine Khaldi /***********************************************************************
15966feb8748SAmine Khaldi  *            NdrServerCallAll [RPCRT4.@]
15976feb8748SAmine Khaldi  */
NdrServerCallAll(PRPC_MESSAGE msg)15986feb8748SAmine Khaldi void WINAPI NdrServerCallAll( PRPC_MESSAGE msg )
1599c2c66affSColin Finck {
16006feb8748SAmine Khaldi     FIXME("%p stub\n", msg);
16016feb8748SAmine Khaldi }
1602c2c66affSColin Finck 
16036feb8748SAmine Khaldi /* Helper for ndr_async_client_call, to factor out the part that may or may not be
16046feb8748SAmine Khaldi  * guarded by a try/except block. */
do_ndr_async_client_call(const MIDL_STUB_DESC * pStubDesc,PFORMAT_STRING pFormat,void ** stack_top)16056feb8748SAmine Khaldi static void do_ndr_async_client_call( const MIDL_STUB_DESC *pStubDesc, PFORMAT_STRING pFormat, void **stack_top )
1606c2c66affSColin Finck {
1607c2c66affSColin Finck     /* pointer to start of stack where arguments start */
1608c2c66affSColin Finck     PRPC_MESSAGE pRpcMsg;
1609c2c66affSColin Finck     PMIDL_STUB_MESSAGE pStubMsg;
1610c2c66affSColin Finck     RPC_ASYNC_STATE *pAsync;
1611c2c66affSColin Finck     struct async_call_data *async_call_data;
1612c2c66affSColin Finck     /* procedure number */
1613c2c66affSColin Finck     unsigned short procedure_number;
1614c2c66affSColin Finck     /* cache of Oif_flags from v2 procedure header */
1615c2c66affSColin Finck     INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1616c2c66affSColin Finck     /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1617c2c66affSColin Finck     INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1618c2c66affSColin Finck     /* header for procedure string */
1619c2c66affSColin Finck     const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1620c2c66affSColin Finck     RPC_STATUS status;
1621c2c66affSColin Finck 
1622c2c66affSColin Finck     /* Later NDR language versions probably won't be backwards compatible */
1623c2c66affSColin Finck     if (pStubDesc->Version > 0x50002)
1624c2c66affSColin Finck     {
1625c2c66affSColin Finck         FIXME("Incompatible stub description version: 0x%x\n", pStubDesc->Version);
1626c2c66affSColin Finck         RpcRaiseException(RPC_X_WRONG_STUB_VERSION);
1627c2c66affSColin Finck     }
1628c2c66affSColin Finck 
1629c2c66affSColin Finck     async_call_data = I_RpcAllocate(sizeof(*async_call_data) + sizeof(MIDL_STUB_MESSAGE) + sizeof(RPC_MESSAGE));
1630c2c66affSColin Finck     if (!async_call_data) RpcRaiseException(RPC_X_NO_MEMORY);
1631c2c66affSColin Finck     async_call_data->pProcHeader = pProcHeader;
1632c2c66affSColin Finck 
1633c2c66affSColin Finck     async_call_data->pStubMsg = pStubMsg = (PMIDL_STUB_MESSAGE)(async_call_data + 1);
1634c2c66affSColin Finck     pRpcMsg = (PRPC_MESSAGE)(pStubMsg + 1);
1635c2c66affSColin Finck 
1636b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_HAS_RPCFLAGS)
1637c2c66affSColin Finck     {
1638c2c66affSColin Finck         const NDR_PROC_HEADER_RPC *header_rpc = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1639c2c66affSColin Finck         async_call_data->stack_size = header_rpc->stack_size;
1640c2c66affSColin Finck         procedure_number = header_rpc->proc_num;
1641c2c66affSColin Finck         pFormat += sizeof(NDR_PROC_HEADER_RPC);
1642c2c66affSColin Finck     }
1643c2c66affSColin Finck     else
1644c2c66affSColin Finck     {
1645c2c66affSColin Finck         async_call_data->stack_size = pProcHeader->stack_size;
1646c2c66affSColin Finck         procedure_number = pProcHeader->proc_num;
1647c2c66affSColin Finck         pFormat += sizeof(NDR_PROC_HEADER);
1648c2c66affSColin Finck     }
1649c2c66affSColin Finck     TRACE("stack size: 0x%x\n", async_call_data->stack_size);
1650c2c66affSColin Finck     TRACE("proc num: %d\n", procedure_number);
1651c2c66affSColin Finck 
1652c2c66affSColin Finck     /* create the full pointer translation tables, if requested */
1653b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_FULL_PTR_USED)
1654c2c66affSColin Finck         pStubMsg->FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
1655c2c66affSColin Finck 
1656b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
1657c2c66affSColin Finck     {
1658c2c66affSColin Finck         ERR("objects not supported\n");
1659c2c66affSColin Finck         I_RpcFree(async_call_data);
1660c2c66affSColin Finck         RpcRaiseException(RPC_X_BAD_STUB_DATA);
1661c2c66affSColin Finck     }
1662c2c66affSColin Finck 
1663c2c66affSColin Finck     NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDesc, procedure_number);
1664c2c66affSColin Finck 
1665c2c66affSColin Finck     TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1666c2c66affSColin Finck     TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
1667c2c66affSColin Finck 
1668c2c66affSColin Finck     /* needed for conformance of top-level objects */
1669c2c66affSColin Finck     pStubMsg->StackTop = I_RpcAllocate(async_call_data->stack_size);
1670c2c66affSColin Finck     memcpy(pStubMsg->StackTop, stack_top, async_call_data->stack_size);
1671c2c66affSColin Finck 
1672c2c66affSColin Finck     pAsync = *(RPC_ASYNC_STATE **)pStubMsg->StackTop;
1673c2c66affSColin Finck     pAsync->StubInfo = async_call_data;
1674c2c66affSColin Finck     async_call_data->pHandleFormat = pFormat;
1675c2c66affSColin Finck 
16766feb8748SAmine Khaldi     TRACE("pAsync %p, pAsync->StubInfo %p, NotificationType %d\n", pAsync, pAsync->StubInfo, pAsync->NotificationType);
16776feb8748SAmine Khaldi 
16786feb8748SAmine Khaldi     pFormat += get_handle_desc_size(pProcHeader, pFormat);
16796feb8748SAmine Khaldi     async_call_data->hBinding = client_get_handle(pStubMsg, pProcHeader, async_call_data->pHandleFormat);
16806feb8748SAmine Khaldi     if (!async_call_data->hBinding) return;
1681c2c66affSColin Finck 
1682c2c66affSColin Finck     if (is_oicf_stubdesc(pStubDesc))
1683c2c66affSColin Finck     {
1684c2c66affSColin Finck         const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
1685c2c66affSColin Finck             (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
1686c2c66affSColin Finck 
1687c2c66affSColin Finck         Oif_flags = pOIFHeader->Oi2Flags;
1688c2c66affSColin Finck         async_call_data->number_of_params = pOIFHeader->number_of_params;
1689c2c66affSColin Finck 
1690c2c66affSColin Finck         pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1691c2c66affSColin Finck 
1692c2c66affSColin Finck         TRACE("Oif_flags = %s\n", debugstr_INTERPRETER_OPT_FLAGS(Oif_flags) );
1693c2c66affSColin Finck 
1694c2c66affSColin Finck         if (Oif_flags.HasExtensions)
1695c2c66affSColin Finck         {
1696c2c66affSColin Finck             const NDR_PROC_HEADER_EXTS *pExtensions =
1697c2c66affSColin Finck                 (const NDR_PROC_HEADER_EXTS *)pFormat;
1698c2c66affSColin Finck             ext_flags = pExtensions->Flags2;
1699c2c66affSColin Finck             pFormat += pExtensions->Size;
1700c2c66affSColin Finck         }
1701c2c66affSColin Finck     }
1702c2c66affSColin Finck     else
1703c2c66affSColin Finck     {
1704c2c66affSColin Finck         pFormat = convert_old_args( pStubMsg, pFormat, async_call_data->stack_size,
1705b6fdb56bSAmine Khaldi                                     pProcHeader->Oi_flags & Oi_OBJECT_PROC,
1706c2c66affSColin Finck                                     async_call_data->NdrCorrCache, sizeof(async_call_data->NdrCorrCache),
1707c2c66affSColin Finck                                     &async_call_data->number_of_params );
1708c2c66affSColin Finck     }
1709c2c66affSColin Finck 
1710c2c66affSColin Finck     async_call_data->pParamFormat = pFormat;
1711c2c66affSColin Finck 
1712c2c66affSColin Finck     pStubMsg->BufferLength = 0;
1713c2c66affSColin Finck 
1714c2c66affSColin Finck     /* store the RPC flags away */
1715b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_HAS_RPCFLAGS)
1716c2c66affSColin Finck         pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1717c2c66affSColin Finck 
1718c2c66affSColin Finck     /* use alternate memory allocation routines */
1719b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_RPCSS_ALLOC_USED)
1720c2c66affSColin Finck         NdrRpcSmSetClientToOsf(pStubMsg);
1721c2c66affSColin Finck 
1722c2c66affSColin Finck     if (Oif_flags.HasPipes)
1723c2c66affSColin Finck     {
1724c2c66affSColin Finck         FIXME("pipes not supported yet\n");
1725c2c66affSColin Finck         RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1726c2c66affSColin Finck         /* init pipes package */
1727c2c66affSColin Finck         /* NdrPipesInitialize(...) */
1728c2c66affSColin Finck     }
1729c2c66affSColin Finck     if (ext_flags.HasNewCorrDesc)
1730c2c66affSColin Finck     {
1731c2c66affSColin Finck         /* initialize extra correlation package */
1732c2c66affSColin Finck         NdrCorrelationInitialize(pStubMsg, async_call_data->NdrCorrCache, sizeof(async_call_data->NdrCorrCache), 0);
1733c2c66affSColin Finck         if (ext_flags.Unused & 0x2) /* has range on conformance */
1734c2c66affSColin Finck             pStubMsg->CorrDespIncrement = 12;
1735c2c66affSColin Finck     }
1736c2c66affSColin Finck 
1737c2c66affSColin Finck     /* order of phases:
1738c2c66affSColin Finck      * 1. CALCSIZE - calculate the buffer size
1739c2c66affSColin Finck      * 2. GETBUFFER - allocate the buffer
1740c2c66affSColin Finck      * 3. MARSHAL - marshal [in] params into the buffer
1741c2c66affSColin Finck      * 4. SENDRECEIVE - send buffer
1742c2c66affSColin Finck      * Then in NdrpCompleteAsyncClientCall:
1743c2c66affSColin Finck      * 1. SENDRECEIVE - receive buffer
1744c2c66affSColin Finck      * 2. UNMARSHAL - unmarshal [out] params from buffer
1745c2c66affSColin Finck      */
1746c2c66affSColin Finck 
1747c2c66affSColin Finck     /* 1. CALCSIZE */
1748c2c66affSColin Finck     TRACE( "CALCSIZE\n" );
1749c2c66affSColin Finck     client_do_args(pStubMsg, pFormat, STUBLESS_CALCSIZE, NULL, async_call_data->number_of_params, NULL);
1750c2c66affSColin Finck 
1751c2c66affSColin Finck     /* 2. GETBUFFER */
1752c2c66affSColin Finck     TRACE( "GETBUFFER\n" );
1753c2c66affSColin Finck     if (Oif_flags.HasPipes)
1754c2c66affSColin Finck         /* NdrGetPipeBuffer(...) */
1755c2c66affSColin Finck         FIXME("pipes not supported yet\n");
1756c2c66affSColin Finck     else
1757c2c66affSColin Finck     {
1758b6fdb56bSAmine Khaldi         if (pProcHeader->handle_type == FC_AUTO_HANDLE)
1759c2c66affSColin Finck #if 0
1760c2c66affSColin Finck             NdrNsGetBuffer(pStubMsg, pStubMsg->BufferLength, async_call_data->hBinding);
1761c2c66affSColin Finck #else
1762c2c66affSColin Finck         FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
1763c2c66affSColin Finck #endif
1764c2c66affSColin Finck         else
1765c2c66affSColin Finck             NdrGetBuffer(pStubMsg, pStubMsg->BufferLength, async_call_data->hBinding);
1766c2c66affSColin Finck     }
1767c2c66affSColin Finck     pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1768c2c66affSColin Finck     status = I_RpcAsyncSetHandle(pRpcMsg, pAsync);
1769c2c66affSColin Finck     if (status != RPC_S_OK)
1770c2c66affSColin Finck         RpcRaiseException(status);
1771c2c66affSColin Finck 
1772c2c66affSColin Finck     /* 3. MARSHAL */
1773c2c66affSColin Finck     TRACE( "MARSHAL\n" );
1774c2c66affSColin Finck     client_do_args(pStubMsg, pFormat, STUBLESS_MARSHAL, NULL, async_call_data->number_of_params, NULL);
1775c2c66affSColin Finck 
1776c2c66affSColin Finck     /* 4. SENDRECEIVE */
1777c2c66affSColin Finck     TRACE( "SEND\n" );
1778c2c66affSColin Finck     pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1779c2c66affSColin Finck     /* send the [in] params only */
1780c2c66affSColin Finck     if (Oif_flags.HasPipes)
1781c2c66affSColin Finck         /* NdrPipesSend(...) */
1782c2c66affSColin Finck         FIXME("pipes not supported yet\n");
1783c2c66affSColin Finck     else
1784c2c66affSColin Finck     {
1785b6fdb56bSAmine Khaldi         if (pProcHeader->handle_type == FC_AUTO_HANDLE)
1786c2c66affSColin Finck #if 0
1787c2c66affSColin Finck             NdrNsSend(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
1788c2c66affSColin Finck #else
1789c2c66affSColin Finck         FIXME("using auto handle - call NdrNsSend when it gets implemented\n");
1790c2c66affSColin Finck #endif
1791c2c66affSColin Finck         else
1792c2c66affSColin Finck         {
1793c2c66affSColin Finck             pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
1794c2c66affSColin Finck             status = I_RpcSend(pStubMsg->RpcMsg);
1795c2c66affSColin Finck             if (status != RPC_S_OK)
1796c2c66affSColin Finck                 RpcRaiseException(status);
1797c2c66affSColin Finck         }
1798c2c66affSColin Finck     }
17996feb8748SAmine Khaldi }
1800c2c66affSColin Finck 
ndr_async_client_call(PMIDL_STUB_DESC pStubDesc,PFORMAT_STRING pFormat,void ** stack_top)18016feb8748SAmine Khaldi LONG_PTR CDECL DECLSPEC_HIDDEN ndr_async_client_call( PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat,
18026feb8748SAmine Khaldi                                                       void **stack_top )
18036feb8748SAmine Khaldi {
18046feb8748SAmine Khaldi     LONG_PTR ret = 0;
18056feb8748SAmine Khaldi     const NDR_PROC_HEADER *pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
18066feb8748SAmine Khaldi 
18076feb8748SAmine Khaldi     TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
18086feb8748SAmine Khaldi 
18096feb8748SAmine Khaldi     if (pProcHeader->Oi_flags & Oi_HAS_COMM_OR_FAULT)
18106feb8748SAmine Khaldi     {
18116feb8748SAmine Khaldi         __TRY
18126feb8748SAmine Khaldi         {
18136feb8748SAmine Khaldi             do_ndr_async_client_call( pStubDesc, pFormat, stack_top );
18146feb8748SAmine Khaldi         }
18156feb8748SAmine Khaldi         __EXCEPT_ALL
18166feb8748SAmine Khaldi         {
18176feb8748SAmine Khaldi             FIXME("exception %x during ndr_async_client_call()\n", GetExceptionCode());
18186feb8748SAmine Khaldi             ret = GetExceptionCode();
18196feb8748SAmine Khaldi         }
18206feb8748SAmine Khaldi         __ENDTRY
18216feb8748SAmine Khaldi     }
18226feb8748SAmine Khaldi     else
18236feb8748SAmine Khaldi         do_ndr_async_client_call( pStubDesc, pFormat, stack_top);
18246feb8748SAmine Khaldi 
18256feb8748SAmine Khaldi     TRACE("returning %ld\n", ret);
18266feb8748SAmine Khaldi     return ret;
1827c2c66affSColin Finck }
1828c2c66affSColin Finck 
NdrpCompleteAsyncClientCall(RPC_ASYNC_STATE * pAsync,void * Reply)1829c2c66affSColin Finck RPC_STATUS NdrpCompleteAsyncClientCall(RPC_ASYNC_STATE *pAsync, void *Reply)
1830c2c66affSColin Finck {
1831c2c66affSColin Finck     /* pointer to start of stack where arguments start */
1832c2c66affSColin Finck     PMIDL_STUB_MESSAGE pStubMsg;
1833c2c66affSColin Finck     struct async_call_data *async_call_data;
1834c2c66affSColin Finck     /* header for procedure string */
1835c2c66affSColin Finck     const NDR_PROC_HEADER * pProcHeader;
1836c2c66affSColin Finck     RPC_STATUS status = RPC_S_OK;
1837c2c66affSColin Finck 
1838c2c66affSColin Finck     if (!pAsync->StubInfo)
1839c2c66affSColin Finck         return RPC_S_INVALID_ASYNC_HANDLE;
1840c2c66affSColin Finck 
1841c2c66affSColin Finck     async_call_data = pAsync->StubInfo;
1842c2c66affSColin Finck     pStubMsg = async_call_data->pStubMsg;
1843c2c66affSColin Finck     pProcHeader = async_call_data->pProcHeader;
1844c2c66affSColin Finck 
1845c2c66affSColin Finck     /* order of phases:
1846c2c66affSColin Finck      * 1. CALCSIZE - calculate the buffer size
1847c2c66affSColin Finck      * 2. GETBUFFER - allocate the buffer
1848c2c66affSColin Finck      * 3. MARSHAL - marshal [in] params into the buffer
1849c2c66affSColin Finck      * 4. SENDRECEIVE - send buffer
1850c2c66affSColin Finck      * Then in NdrpCompleteAsyncClientCall:
1851c2c66affSColin Finck      * 1. SENDRECEIVE - receive buffer
1852c2c66affSColin Finck      * 2. UNMARSHAL - unmarshal [out] params from buffer
1853c2c66affSColin Finck      */
1854c2c66affSColin Finck 
1855c2c66affSColin Finck     /* 1. SENDRECEIVE */
1856c2c66affSColin Finck     TRACE( "RECEIVE\n" );
1857c2c66affSColin Finck     pStubMsg->RpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1858c2c66affSColin Finck     /* receive the [out] params */
1859b6fdb56bSAmine Khaldi     if (pProcHeader->handle_type == FC_AUTO_HANDLE)
1860c2c66affSColin Finck #if 0
1861c2c66affSColin Finck         NdrNsReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
1862c2c66affSColin Finck #else
1863c2c66affSColin Finck     FIXME("using auto handle - call NdrNsReceive when it gets implemented\n");
1864c2c66affSColin Finck #endif
1865c2c66affSColin Finck     else
1866c2c66affSColin Finck     {
1867c2c66affSColin Finck         status = I_RpcReceive(pStubMsg->RpcMsg);
1868c2c66affSColin Finck         if (status != RPC_S_OK)
1869c2c66affSColin Finck             goto cleanup;
1870c2c66affSColin Finck         pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
1871c2c66affSColin Finck         pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
1872c2c66affSColin Finck         pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
1873c2c66affSColin Finck         pStubMsg->Buffer = pStubMsg->BufferStart;
1874c2c66affSColin Finck     }
1875c2c66affSColin Finck 
1876c2c66affSColin Finck     /* convert strings, floating point values and endianness into our
1877c2c66affSColin Finck      * preferred format */
1878c2c66affSColin Finck #if 0
1879c2c66affSColin Finck     if ((pStubMsg->RpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1880c2c66affSColin Finck         NdrConvert(pStubMsg, pFormat);
1881c2c66affSColin Finck #endif
1882c2c66affSColin Finck 
1883c2c66affSColin Finck     /* 2. UNMARSHAL */
1884c2c66affSColin Finck     TRACE( "UNMARSHAL\n" );
1885c2c66affSColin Finck     client_do_args(pStubMsg, async_call_data->pParamFormat, STUBLESS_UNMARSHAL,
1886c2c66affSColin Finck                    NULL, async_call_data->number_of_params, Reply);
1887c2c66affSColin Finck 
1888c2c66affSColin Finck cleanup:
1889c2c66affSColin Finck     if (pStubMsg->fHasNewCorrDesc)
1890c2c66affSColin Finck     {
1891c2c66affSColin Finck         /* free extra correlation package */
1892c2c66affSColin Finck         NdrCorrelationFree(pStubMsg);
1893c2c66affSColin Finck     }
1894c2c66affSColin Finck 
1895c2c66affSColin Finck     /* free the full pointer translation tables */
1896b6fdb56bSAmine Khaldi     if (pProcHeader->Oi_flags & Oi_FULL_PTR_USED)
1897c2c66affSColin Finck         NdrFullPointerXlatFree(pStubMsg->FullPtrXlatTables);
1898c2c66affSColin Finck 
1899c2c66affSColin Finck     /* free marshalling buffer */
1900c2c66affSColin Finck     NdrFreeBuffer(pStubMsg);
1901c2c66affSColin Finck     client_free_handle(pStubMsg, pProcHeader, async_call_data->pHandleFormat, async_call_data->hBinding);
1902c2c66affSColin Finck 
1903c2c66affSColin Finck     I_RpcFree(pStubMsg->StackTop);
1904c2c66affSColin Finck     I_RpcFree(async_call_data);
1905c2c66affSColin Finck 
1906c2c66affSColin Finck     TRACE("-- 0x%x\n", status);
1907c2c66affSColin Finck     return status;
1908c2c66affSColin Finck }
1909c2c66affSColin Finck 
1910c2c66affSColin Finck #ifdef __x86_64__
1911c2c66affSColin Finck 
1912c2c66affSColin Finck __ASM_GLOBAL_FUNC( NdrAsyncClientCall,
1913c2c66affSColin Finck                    "subq $0x28,%rsp\n\t"
19146feb8748SAmine Khaldi                    __ASM_SEH(".seh_stackalloc 0x28\n\t")
19156feb8748SAmine Khaldi                    __ASM_SEH(".seh_endprologue\n\t")
1916c2c66affSColin Finck                    __ASM_CFI(".cfi_adjust_cfa_offset 0x28\n\t")
19176feb8748SAmine Khaldi                    "movq %r8,0x40(%rsp)\n\t"
19186feb8748SAmine Khaldi                    "movq %r9,0x48(%rsp)\n\t"
19196feb8748SAmine Khaldi                    "leaq 0x40(%rsp),%r8\n\t"
1920c2c66affSColin Finck                    "call " __ASM_NAME("ndr_async_client_call") "\n\t"
1921c2c66affSColin Finck                    "addq $0x28,%rsp\n\t"
1922c2c66affSColin Finck                    __ASM_CFI(".cfi_adjust_cfa_offset -0x28\n\t")
1923c2c66affSColin Finck                    "ret" );
1924c2c66affSColin Finck 
1925c2c66affSColin Finck #else  /* __x86_64__ */
1926c2c66affSColin Finck 
1927c2c66affSColin Finck /***********************************************************************
1928c2c66affSColin Finck  *            NdrAsyncClientCall [RPCRT4.@]
1929c2c66affSColin Finck  */
NdrAsyncClientCall(PMIDL_STUB_DESC desc,PFORMAT_STRING format,...)1930c2c66affSColin Finck CLIENT_CALL_RETURN WINAPIV NdrAsyncClientCall( PMIDL_STUB_DESC desc, PFORMAT_STRING format, ... )
1931c2c66affSColin Finck {
1932c2c66affSColin Finck     __ms_va_list args;
1933c2c66affSColin Finck     LONG_PTR ret;
1934c2c66affSColin Finck 
1935c2c66affSColin Finck     __ms_va_start( args, format );
1936c2c66affSColin Finck     ret = ndr_async_client_call( desc, format, va_arg( args, void ** ));
1937c2c66affSColin Finck     __ms_va_end( args );
1938c2c66affSColin Finck     return *(CLIENT_CALL_RETURN *)&ret;
1939c2c66affSColin Finck }
1940c2c66affSColin Finck 
1941c2c66affSColin Finck #endif  /* __x86_64__ */
1942c2c66affSColin Finck 
NdrAsyncStubCall(struct IRpcStubBuffer * pThis,struct IRpcChannelBuffer * pChannel,PRPC_MESSAGE pRpcMsg,DWORD * pdwStubPhase)1943c2c66affSColin Finck RPCRTAPI LONG RPC_ENTRY NdrAsyncStubCall(struct IRpcStubBuffer* pThis,
1944c2c66affSColin Finck     struct IRpcChannelBuffer* pChannel, PRPC_MESSAGE pRpcMsg,
1945c2c66affSColin Finck     DWORD * pdwStubPhase)
1946c2c66affSColin Finck {
1947c2c66affSColin Finck     FIXME("unimplemented, expect crash!\n");
1948c2c66affSColin Finck     return 0;
1949c2c66affSColin Finck }
1950c2c66affSColin Finck 
NdrAsyncServerCall(PRPC_MESSAGE pRpcMsg)1951c2c66affSColin Finck void RPC_ENTRY NdrAsyncServerCall(PRPC_MESSAGE pRpcMsg)
1952c2c66affSColin Finck {
19536feb8748SAmine Khaldi     const MIDL_SERVER_INFO *pServerInfo;
19546feb8748SAmine Khaldi     const MIDL_STUB_DESC *pStubDesc;
19556feb8748SAmine Khaldi     PFORMAT_STRING pFormat;
19566feb8748SAmine Khaldi     /* pointer to start of stack to pass into stub implementation */
19576feb8748SAmine Khaldi     unsigned char *args;
19586feb8748SAmine Khaldi     /* header for procedure string */
19596feb8748SAmine Khaldi     const NDR_PROC_HEADER *pProcHeader;
19606feb8748SAmine Khaldi     struct async_call_data *async_call_data;
19616feb8748SAmine Khaldi     PRPC_ASYNC_STATE pAsync;
19626feb8748SAmine Khaldi     RPC_STATUS status;
19636feb8748SAmine Khaldi 
19646feb8748SAmine Khaldi     TRACE("%p\n", pRpcMsg);
19656feb8748SAmine Khaldi 
19666feb8748SAmine Khaldi     pServerInfo = ((RPC_SERVER_INTERFACE *)pRpcMsg->RpcInterfaceInformation)->InterpreterInfo;
19676feb8748SAmine Khaldi 
19686feb8748SAmine Khaldi     pStubDesc = pServerInfo->pStubDesc;
19696feb8748SAmine Khaldi     pFormat = pServerInfo->ProcString + pServerInfo->FmtStringOffset[pRpcMsg->ProcNum];
19706feb8748SAmine Khaldi     pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
19716feb8748SAmine Khaldi 
19726feb8748SAmine Khaldi     TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
19736feb8748SAmine Khaldi 
19746feb8748SAmine Khaldi     async_call_data = I_RpcAllocate(sizeof(*async_call_data) + sizeof(MIDL_STUB_MESSAGE) + sizeof(RPC_MESSAGE));
19756feb8748SAmine Khaldi     if (!async_call_data) RpcRaiseException(RPC_X_NO_MEMORY);
19766feb8748SAmine Khaldi     async_call_data->pProcHeader = pProcHeader;
19776feb8748SAmine Khaldi 
19786feb8748SAmine Khaldi     async_call_data->pStubMsg = (PMIDL_STUB_MESSAGE)(async_call_data + 1);
19796feb8748SAmine Khaldi     *(PRPC_MESSAGE)(async_call_data->pStubMsg + 1) = *pRpcMsg;
19806feb8748SAmine Khaldi 
19816feb8748SAmine Khaldi     if (pProcHeader->Oi_flags & Oi_HAS_RPCFLAGS)
19826feb8748SAmine Khaldi     {
19836feb8748SAmine Khaldi         const NDR_PROC_HEADER_RPC *header_rpc = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
19846feb8748SAmine Khaldi         async_call_data->stack_size = header_rpc->stack_size;
19856feb8748SAmine Khaldi         pFormat += sizeof(NDR_PROC_HEADER_RPC);
19866feb8748SAmine Khaldi     }
19876feb8748SAmine Khaldi     else
19886feb8748SAmine Khaldi     {
19896feb8748SAmine Khaldi         async_call_data->stack_size = pProcHeader->stack_size;
19906feb8748SAmine Khaldi         pFormat += sizeof(NDR_PROC_HEADER);
19916feb8748SAmine Khaldi     }
19926feb8748SAmine Khaldi 
19936feb8748SAmine Khaldi     TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
19946feb8748SAmine Khaldi 
19956feb8748SAmine Khaldi     /* binding */
19966feb8748SAmine Khaldi     switch (pProcHeader->handle_type)
19976feb8748SAmine Khaldi     {
19986feb8748SAmine Khaldi     /* explicit binding: parse additional section */
19996feb8748SAmine Khaldi     case 0:
20006feb8748SAmine Khaldi         switch (*pFormat) /* handle_type */
20016feb8748SAmine Khaldi         {
20026feb8748SAmine Khaldi         case FC_BIND_PRIMITIVE: /* explicit primitive */
20036feb8748SAmine Khaldi             pFormat += sizeof(NDR_EHD_PRIMITIVE);
20046feb8748SAmine Khaldi             break;
20056feb8748SAmine Khaldi         case FC_BIND_GENERIC: /* explicit generic */
20066feb8748SAmine Khaldi             pFormat += sizeof(NDR_EHD_GENERIC);
20076feb8748SAmine Khaldi             break;
20086feb8748SAmine Khaldi         case FC_BIND_CONTEXT: /* explicit context */
20096feb8748SAmine Khaldi             pFormat += sizeof(NDR_EHD_CONTEXT);
20106feb8748SAmine Khaldi             break;
20116feb8748SAmine Khaldi         default:
20126feb8748SAmine Khaldi             ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
20136feb8748SAmine Khaldi             RpcRaiseException(RPC_X_BAD_STUB_DATA);
20146feb8748SAmine Khaldi         }
20156feb8748SAmine Khaldi         break;
20166feb8748SAmine Khaldi     case FC_BIND_GENERIC: /* implicit generic */
20176feb8748SAmine Khaldi     case FC_BIND_PRIMITIVE: /* implicit primitive */
20186feb8748SAmine Khaldi     case FC_CALLBACK_HANDLE: /* implicit callback */
20196feb8748SAmine Khaldi     case FC_AUTO_HANDLE: /* implicit auto handle */
20206feb8748SAmine Khaldi         break;
20216feb8748SAmine Khaldi     default:
20226feb8748SAmine Khaldi         ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
20236feb8748SAmine Khaldi         RpcRaiseException(RPC_X_BAD_STUB_DATA);
20246feb8748SAmine Khaldi     }
20256feb8748SAmine Khaldi 
20266feb8748SAmine Khaldi     if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
20276feb8748SAmine Khaldi     {
20286feb8748SAmine Khaldi         ERR("objects not supported\n");
20296feb8748SAmine Khaldi         I_RpcFree(async_call_data);
20306feb8748SAmine Khaldi         RpcRaiseException(RPC_X_BAD_STUB_DATA);
20316feb8748SAmine Khaldi     }
20326feb8748SAmine Khaldi 
20336feb8748SAmine Khaldi     NdrServerInitializeNew(pRpcMsg, async_call_data->pStubMsg, pStubDesc);
20346feb8748SAmine Khaldi 
20356feb8748SAmine Khaldi     /* create the full pointer translation tables, if requested */
20366feb8748SAmine Khaldi     if (pProcHeader->Oi_flags & Oi_FULL_PTR_USED)
20376feb8748SAmine Khaldi         async_call_data->pStubMsg->FullPtrXlatTables = NdrFullPointerXlatInit(0, XLAT_SERVER);
20386feb8748SAmine Khaldi 
20396feb8748SAmine Khaldi     /* use alternate memory allocation routines */
20406feb8748SAmine Khaldi     if (pProcHeader->Oi_flags & Oi_RPCSS_ALLOC_USED)
20416feb8748SAmine Khaldi #if 0
20426feb8748SAmine Khaldi           NdrRpcSsEnableAllocate(&stubMsg);
20436feb8748SAmine Khaldi #else
20446feb8748SAmine Khaldi           FIXME("Set RPCSS memory allocation routines\n");
20456feb8748SAmine Khaldi #endif
20466feb8748SAmine Khaldi 
20476feb8748SAmine Khaldi     TRACE("allocating memory for stack of size %x\n", async_call_data->stack_size);
20486feb8748SAmine Khaldi 
20496feb8748SAmine Khaldi     args = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, async_call_data->stack_size);
20506feb8748SAmine Khaldi     async_call_data->pStubMsg->StackTop = args; /* used by conformance of top-level objects */
20516feb8748SAmine Khaldi 
20526feb8748SAmine Khaldi     pAsync = I_RpcAllocate(sizeof(*pAsync));
20536feb8748SAmine Khaldi     if (!pAsync) RpcRaiseException(RPC_X_NO_MEMORY);
20546feb8748SAmine Khaldi 
20556feb8748SAmine Khaldi     status = RpcAsyncInitializeHandle(pAsync, sizeof(*pAsync));
20566feb8748SAmine Khaldi     if (status != RPC_S_OK)
20576feb8748SAmine Khaldi         RpcRaiseException(status);
20586feb8748SAmine Khaldi 
20596feb8748SAmine Khaldi     pAsync->StubInfo = async_call_data;
20606feb8748SAmine Khaldi     TRACE("pAsync %p, pAsync->StubInfo %p, pFormat %p\n", pAsync, pAsync->StubInfo, async_call_data->pHandleFormat);
20616feb8748SAmine Khaldi 
20626feb8748SAmine Khaldi     /* add the implicit pAsync pointer as the first arg to the function */
20636feb8748SAmine Khaldi     *(void **)args = pAsync;
20646feb8748SAmine Khaldi 
20656feb8748SAmine Khaldi     if (is_oicf_stubdesc(pStubDesc))
20666feb8748SAmine Khaldi     {
20676feb8748SAmine Khaldi         const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader = (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
20686feb8748SAmine Khaldi         /* cache of Oif_flags from v2 procedure header */
20696feb8748SAmine Khaldi         INTERPRETER_OPT_FLAGS Oif_flags;
20706feb8748SAmine Khaldi         /* cache of extension flags from NDR_PROC_HEADER_EXTS */
20716feb8748SAmine Khaldi         INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
20726feb8748SAmine Khaldi 
20736feb8748SAmine Khaldi         Oif_flags = pOIFHeader->Oi2Flags;
20746feb8748SAmine Khaldi         async_call_data->number_of_params = pOIFHeader->number_of_params;
20756feb8748SAmine Khaldi 
20766feb8748SAmine Khaldi         pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
20776feb8748SAmine Khaldi 
20786feb8748SAmine Khaldi         TRACE("Oif_flags = %s\n", debugstr_INTERPRETER_OPT_FLAGS(Oif_flags) );
20796feb8748SAmine Khaldi 
20806feb8748SAmine Khaldi         if (Oif_flags.HasExtensions)
20816feb8748SAmine Khaldi         {
20826feb8748SAmine Khaldi             const NDR_PROC_HEADER_EXTS *pExtensions = (const NDR_PROC_HEADER_EXTS *)pFormat;
20836feb8748SAmine Khaldi             ext_flags = pExtensions->Flags2;
20846feb8748SAmine Khaldi             pFormat += pExtensions->Size;
20856feb8748SAmine Khaldi         }
20866feb8748SAmine Khaldi 
20876feb8748SAmine Khaldi         if (Oif_flags.HasPipes)
20886feb8748SAmine Khaldi         {
20896feb8748SAmine Khaldi             FIXME("pipes not supported yet\n");
20906feb8748SAmine Khaldi             RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
20916feb8748SAmine Khaldi             /* init pipes package */
20926feb8748SAmine Khaldi             /* NdrPipesInitialize(...) */
20936feb8748SAmine Khaldi         }
20946feb8748SAmine Khaldi         if (ext_flags.HasNewCorrDesc)
20956feb8748SAmine Khaldi         {
20966feb8748SAmine Khaldi             /* initialize extra correlation package */
20976feb8748SAmine Khaldi             NdrCorrelationInitialize(async_call_data->pStubMsg, async_call_data->NdrCorrCache, sizeof(async_call_data->NdrCorrCache), 0);
20986feb8748SAmine Khaldi             if (ext_flags.Unused & 0x2) /* has range on conformance */
20996feb8748SAmine Khaldi                 async_call_data->pStubMsg->CorrDespIncrement = 12;
21006feb8748SAmine Khaldi         }
21016feb8748SAmine Khaldi     }
21026feb8748SAmine Khaldi     else
21036feb8748SAmine Khaldi     {
21046feb8748SAmine Khaldi         pFormat = convert_old_args( async_call_data->pStubMsg, pFormat, async_call_data->stack_size,
21056feb8748SAmine Khaldi                                     pProcHeader->Oi_flags & Oi_OBJECT_PROC,
21066feb8748SAmine Khaldi                                     /* reuse the correlation cache, it's not needed for v1 format */
21076feb8748SAmine Khaldi                                     async_call_data->NdrCorrCache, sizeof(async_call_data->NdrCorrCache), &async_call_data->number_of_params );
21086feb8748SAmine Khaldi     }
21096feb8748SAmine Khaldi 
21106feb8748SAmine Khaldi     /* convert strings, floating point values and endianness into our
21116feb8748SAmine Khaldi      * preferred format */
21126feb8748SAmine Khaldi     if ((pRpcMsg->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
21136feb8748SAmine Khaldi         NdrConvert(async_call_data->pStubMsg, pFormat);
21146feb8748SAmine Khaldi 
21156feb8748SAmine Khaldi     async_call_data->pHandleFormat = pFormat;
21166feb8748SAmine Khaldi 
21176feb8748SAmine Khaldi     /* 1. UNMARSHAL */
21186feb8748SAmine Khaldi     TRACE("UNMARSHAL\n");
21196feb8748SAmine Khaldi     stub_do_args(async_call_data->pStubMsg, pFormat, STUBLESS_UNMARSHAL, async_call_data->number_of_params);
21206feb8748SAmine Khaldi 
21216feb8748SAmine Khaldi     /* 2. INITOUT */
21226feb8748SAmine Khaldi     TRACE("INITOUT\n");
21236feb8748SAmine Khaldi     async_call_data->retval_ptr = stub_do_args(async_call_data->pStubMsg, pFormat, STUBLESS_INITOUT, async_call_data->number_of_params);
21246feb8748SAmine Khaldi 
21256feb8748SAmine Khaldi     /* 3. CALLSERVER */
21266feb8748SAmine Khaldi     TRACE("CALLSERVER\n");
21276feb8748SAmine Khaldi     if (pServerInfo->ThunkTable && pServerInfo->ThunkTable[pRpcMsg->ProcNum])
21286feb8748SAmine Khaldi         pServerInfo->ThunkTable[pRpcMsg->ProcNum](async_call_data->pStubMsg);
21296feb8748SAmine Khaldi     else
21306feb8748SAmine Khaldi         call_server_func(pServerInfo->DispatchTable[pRpcMsg->ProcNum], args, async_call_data->stack_size);
21316feb8748SAmine Khaldi }
21326feb8748SAmine Khaldi 
NdrpCompleteAsyncServerCall(RPC_ASYNC_STATE * pAsync,void * Reply)21336feb8748SAmine Khaldi RPC_STATUS NdrpCompleteAsyncServerCall(RPC_ASYNC_STATE *pAsync, void *Reply)
21346feb8748SAmine Khaldi {
21356feb8748SAmine Khaldi     /* pointer to start of stack where arguments start */
21366feb8748SAmine Khaldi     PMIDL_STUB_MESSAGE pStubMsg;
21376feb8748SAmine Khaldi     struct async_call_data *async_call_data;
21386feb8748SAmine Khaldi     /* the type of pass we are currently doing */
21396feb8748SAmine Khaldi     enum stubless_phase phase;
21406feb8748SAmine Khaldi     RPC_STATUS status = RPC_S_OK;
21416feb8748SAmine Khaldi 
21426feb8748SAmine Khaldi     if (!pAsync->StubInfo)
21436feb8748SAmine Khaldi         return RPC_S_INVALID_ASYNC_HANDLE;
21446feb8748SAmine Khaldi 
21456feb8748SAmine Khaldi     async_call_data = pAsync->StubInfo;
21466feb8748SAmine Khaldi     pStubMsg = async_call_data->pStubMsg;
21476feb8748SAmine Khaldi 
21486feb8748SAmine Khaldi     TRACE("pAsync %p, pAsync->StubInfo %p, pFormat %p\n", pAsync, pAsync->StubInfo, async_call_data->pHandleFormat);
21496feb8748SAmine Khaldi 
21506feb8748SAmine Khaldi     if (async_call_data->retval_ptr)
21516feb8748SAmine Khaldi     {
21526feb8748SAmine Khaldi         TRACE("stub implementation returned 0x%lx\n", *(LONG_PTR *)Reply);
21536feb8748SAmine Khaldi         *async_call_data->retval_ptr = *(LONG_PTR *)Reply;
21546feb8748SAmine Khaldi     }
21556feb8748SAmine Khaldi     else
21566feb8748SAmine Khaldi         TRACE("void stub implementation\n");
21576feb8748SAmine Khaldi 
21586feb8748SAmine Khaldi     for (phase = STUBLESS_CALCSIZE; phase <= STUBLESS_FREE; phase++)
21596feb8748SAmine Khaldi     {
21606feb8748SAmine Khaldi         TRACE("phase = %d\n", phase);
21616feb8748SAmine Khaldi         switch (phase)
21626feb8748SAmine Khaldi         {
21636feb8748SAmine Khaldi         case STUBLESS_GETBUFFER:
21646feb8748SAmine Khaldi             if (async_call_data->pProcHeader->Oi_flags & Oi_OBJECT_PROC)
21656feb8748SAmine Khaldi             {
21666feb8748SAmine Khaldi                 ERR("objects not supported\n");
21676feb8748SAmine Khaldi                 HeapFree(GetProcessHeap(), 0, async_call_data->pStubMsg->StackTop);
21686feb8748SAmine Khaldi                 I_RpcFree(async_call_data);
21696feb8748SAmine Khaldi                 I_RpcFree(pAsync);
21706feb8748SAmine Khaldi                 RpcRaiseException(RPC_X_BAD_STUB_DATA);
21716feb8748SAmine Khaldi             }
21726feb8748SAmine Khaldi             else
21736feb8748SAmine Khaldi             {
21746feb8748SAmine Khaldi                 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
21756feb8748SAmine Khaldi                 /* allocate buffer for [out] and [ret] params */
21766feb8748SAmine Khaldi                 status = I_RpcGetBuffer(pStubMsg->RpcMsg);
21776feb8748SAmine Khaldi                 if (status)
21786feb8748SAmine Khaldi                     RpcRaiseException(status);
21796feb8748SAmine Khaldi                 pStubMsg->Buffer = pStubMsg->RpcMsg->Buffer;
21806feb8748SAmine Khaldi             }
21816feb8748SAmine Khaldi             break;
21826feb8748SAmine Khaldi 
21836feb8748SAmine Khaldi         case STUBLESS_CALCSIZE:
21846feb8748SAmine Khaldi         case STUBLESS_MARSHAL:
21856feb8748SAmine Khaldi         case STUBLESS_MUSTFREE:
21866feb8748SAmine Khaldi         case STUBLESS_FREE:
21876feb8748SAmine Khaldi             stub_do_args(pStubMsg, async_call_data->pHandleFormat, phase, async_call_data->number_of_params);
21886feb8748SAmine Khaldi             break;
21896feb8748SAmine Khaldi         default:
21906feb8748SAmine Khaldi             ERR("shouldn't reach here. phase %d\n", phase);
21916feb8748SAmine Khaldi             break;
21926feb8748SAmine Khaldi         }
21936feb8748SAmine Khaldi     }
21946feb8748SAmine Khaldi 
21956feb8748SAmine Khaldi #if 0 /* FIXME */
21966feb8748SAmine Khaldi     if (ext_flags.HasNewCorrDesc)
21976feb8748SAmine Khaldi     {
21986feb8748SAmine Khaldi         /* free extra correlation package */
21996feb8748SAmine Khaldi         NdrCorrelationFree(pStubMsg);
22006feb8748SAmine Khaldi     }
22016feb8748SAmine Khaldi 
22026feb8748SAmine Khaldi     if (Oif_flags.HasPipes)
22036feb8748SAmine Khaldi     {
22046feb8748SAmine Khaldi         /* NdrPipesDone(...) */
22056feb8748SAmine Khaldi     }
22066feb8748SAmine Khaldi 
22076feb8748SAmine Khaldi     /* free the full pointer translation tables */
22086feb8748SAmine Khaldi     if (async_call_data->pProcHeader->Oi_flags & Oi_FULL_PTR_USED)
22096feb8748SAmine Khaldi         NdrFullPointerXlatFree(pStubMsg->FullPtrXlatTables);
22106feb8748SAmine Khaldi #endif
22116feb8748SAmine Khaldi 
22126feb8748SAmine Khaldi     /* free server function stack */
22136feb8748SAmine Khaldi     HeapFree(GetProcessHeap(), 0, async_call_data->pStubMsg->StackTop);
22146feb8748SAmine Khaldi     I_RpcFree(async_call_data);
22156feb8748SAmine Khaldi     I_RpcFree(pAsync);
22166feb8748SAmine Khaldi 
22176feb8748SAmine Khaldi     return S_OK;
2218c2c66affSColin Finck }
2219