rpcrt4: Correctly handle simple ref pointers in calc_arg_size().
[wine.git] / dlls / rpcrt4 / ndr_stubless.c
blob18ce74dcca8331915da1829fc7e64ed74fa03c79
1 /*
2 * NDR -Oi,-Oif,-Oicf Interpreter
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2003-5 Robert Shearman (for CodeWeavers)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * TODO:
22 * - Pipes
23 * - Some types of binding handles
26 #include "config.h"
27 #include "wine/port.h"
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
37 #include "objbase.h"
38 #include "rpc.h"
39 #include "rpcproxy.h"
41 #include "wine/exception.h"
42 #include "wine/debug.h"
44 #include "cpsf.h"
45 #include "ndr_misc.h"
46 #include "ndr_stubless.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
50 #define NDR_TABLE_MASK 127
52 static inline BOOL is_oicf_stubdesc(const PMIDL_STUB_DESC pStubDesc)
54 return pStubDesc->Version >= 0x20000;
57 static inline void call_buffer_sizer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
58 const NDR_PARAM_OIF *param)
60 PFORMAT_STRING pFormat;
61 NDR_BUFFERSIZE m;
63 if (param->attr.IsBasetype)
65 pFormat = &param->u.type_format_char;
66 if (param->attr.IsSimpleRef) pMemory = *(unsigned char **)pMemory;
68 else
70 pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
71 if (!param->attr.IsByValue) pMemory = *(unsigned char **)pMemory;
74 m = NdrBufferSizer[pFormat[0] & NDR_TABLE_MASK];
75 if (m) m(pStubMsg, pMemory, pFormat);
76 else
78 FIXME("format type 0x%x not implemented\n", pFormat[0]);
79 RpcRaiseException(RPC_X_BAD_STUB_DATA);
83 static inline unsigned char *call_marshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
84 const NDR_PARAM_OIF *param)
86 PFORMAT_STRING pFormat;
87 NDR_MARSHALL m;
89 if (param->attr.IsBasetype)
91 pFormat = &param->u.type_format_char;
92 if (param->attr.IsSimpleRef) pMemory = *(unsigned char **)pMemory;
94 else
96 pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
97 if (!param->attr.IsByValue) pMemory = *(unsigned char **)pMemory;
100 m = NdrMarshaller[pFormat[0] & NDR_TABLE_MASK];
101 if (m) return m(pStubMsg, pMemory, pFormat);
102 else
104 FIXME("format type 0x%x not implemented\n", pFormat[0]);
105 RpcRaiseException(RPC_X_BAD_STUB_DATA);
106 return NULL;
110 static inline unsigned char *call_unmarshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char **ppMemory,
111 const NDR_PARAM_OIF *param, unsigned char fMustAlloc)
113 PFORMAT_STRING pFormat;
114 NDR_UNMARSHALL m;
116 if (param->attr.IsBasetype)
118 pFormat = &param->u.type_format_char;
119 if (param->attr.IsSimpleRef) ppMemory = (unsigned char **)*ppMemory;
121 else
123 pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
124 if (!param->attr.IsByValue) ppMemory = (unsigned char **)*ppMemory;
127 m = NdrUnmarshaller[pFormat[0] & NDR_TABLE_MASK];
128 if (m) return m(pStubMsg, ppMemory, pFormat, fMustAlloc);
129 else
131 FIXME("format type 0x%x not implemented\n", pFormat[0]);
132 RpcRaiseException(RPC_X_BAD_STUB_DATA);
133 return NULL;
137 static inline void call_freer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
138 const NDR_PARAM_OIF *param)
140 PFORMAT_STRING pFormat;
141 NDR_FREE m;
143 if (param->attr.IsBasetype) return; /* nothing to do */
144 pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
145 if (!param->attr.IsByValue) pMemory = *(unsigned char **)pMemory;
147 m = NdrFreer[pFormat[0] & NDR_TABLE_MASK];
148 if (m) m(pStubMsg, pMemory, pFormat);
151 static DWORD calc_arg_size(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat)
153 DWORD size;
154 switch(*pFormat)
156 case FC_RP:
157 if (pFormat[1] & FC_SIMPLE_POINTER)
159 size = 0;
160 break;
162 size = calc_arg_size(pStubMsg, &pFormat[2] + *(const SHORT*)&pFormat[2]);
163 break;
164 case FC_STRUCT:
165 case FC_PSTRUCT:
166 size = *(const WORD*)(pFormat + 2);
167 break;
168 case FC_BOGUS_STRUCT:
169 size = *(const WORD*)(pFormat + 2);
170 if(*(const WORD*)(pFormat + 4))
171 FIXME("Unhandled conformant description\n");
172 break;
173 case FC_CARRAY:
174 case FC_CVARRAY:
175 size = *(const WORD*)(pFormat + 2);
176 ComputeConformance(pStubMsg, NULL, pFormat + 4, 0);
177 size *= pStubMsg->MaxCount;
178 break;
179 case FC_SMFARRAY:
180 case FC_SMVARRAY:
181 size = *(const WORD*)(pFormat + 2);
182 break;
183 case FC_LGFARRAY:
184 case FC_LGVARRAY:
185 size = *(const DWORD*)(pFormat + 2);
186 break;
187 case FC_BOGUS_ARRAY:
188 pFormat = ComputeConformance(pStubMsg, NULL, pFormat + 4, *(const WORD*)&pFormat[2]);
189 TRACE("conformance = %ld\n", pStubMsg->MaxCount);
190 pFormat = ComputeVariance(pStubMsg, NULL, pFormat, pStubMsg->MaxCount);
191 size = ComplexStructSize(pStubMsg, pFormat);
192 size *= pStubMsg->MaxCount;
193 break;
194 case FC_USER_MARSHAL:
195 size = *(const WORD*)(pFormat + 4);
196 break;
197 case FC_CSTRING:
198 size = *(const WORD*)(pFormat + 2);
199 break;
200 case FC_WSTRING:
201 size = *(const WORD*)(pFormat + 2) * sizeof(WCHAR);
202 break;
203 case FC_C_CSTRING:
204 case FC_C_WSTRING:
205 if (*pFormat == FC_C_CSTRING)
206 size = sizeof(CHAR);
207 else
208 size = sizeof(WCHAR);
209 if (pFormat[1] == FC_STRING_SIZED)
210 ComputeConformance(pStubMsg, NULL, pFormat + 2, 0);
211 else
212 pStubMsg->MaxCount = 0;
213 size *= pStubMsg->MaxCount;
214 break;
215 default:
216 FIXME("Unhandled type %02x\n", *pFormat);
217 /* fallthrough */
218 case FC_UP:
219 case FC_OP:
220 case FC_FP:
221 case FC_IP:
222 size = sizeof(void *);
223 break;
225 return size;
228 void WINAPI NdrRpcSmSetClientToOsf(PMIDL_STUB_MESSAGE pMessage)
230 #if 0 /* these functions are not defined yet */
231 pMessage->pfnAllocate = NdrRpcSmClientAllocate;
232 pMessage->pfnFree = NdrRpcSmClientFree;
233 #endif
236 static const char *debugstr_PROC_PF(PARAM_ATTRIBUTES param_attributes)
238 char buffer[160];
240 buffer[0] = 0;
241 if (param_attributes.MustSize) strcat(buffer, " MustSize");
242 if (param_attributes.MustFree) strcat(buffer, " MustFree");
243 if (param_attributes.IsPipe) strcat(buffer, " IsPipe");
244 if (param_attributes.IsIn) strcat(buffer, " IsIn");
245 if (param_attributes.IsOut) strcat(buffer, " IsOut");
246 if (param_attributes.IsReturn) strcat(buffer, " IsReturn");
247 if (param_attributes.IsBasetype) strcat(buffer, " IsBasetype");
248 if (param_attributes.IsByValue) strcat(buffer, " IsByValue");
249 if (param_attributes.IsSimpleRef) strcat(buffer, " IsSimpleRef");
250 if (param_attributes.IsDontCallFreeInst) strcat(buffer, " IsDontCallFreeInst");
251 if (param_attributes.SaveForAsyncFinish) strcat(buffer, " SaveForAsyncFinish");
252 if (param_attributes.ServerAllocSize)
253 sprintf( buffer + strlen(buffer), " ServerAllocSize = %d", param_attributes.ServerAllocSize * 8);
254 return buffer[0] ? wine_dbg_sprintf( "%s", buffer + 1 ) : "";
257 static const char *debugstr_INTERPRETER_OPT_FLAGS(INTERPRETER_OPT_FLAGS Oi2Flags)
259 char buffer[160];
261 buffer[0] = 0;
262 if (Oi2Flags.ServerMustSize) strcat(buffer, " ServerMustSize");
263 if (Oi2Flags.ClientMustSize) strcat(buffer, " ClientMustSize");
264 if (Oi2Flags.HasReturn) strcat(buffer, " HasReturn");
265 if (Oi2Flags.HasPipes) strcat(buffer, " HasPipes");
266 if (Oi2Flags.Unused) strcat(buffer, " Unused");
267 if (Oi2Flags.HasAsyncUuid) strcat(buffer, " HasAsyncUuid");
268 if (Oi2Flags.HasExtensions) strcat(buffer, " HasExtensions");
269 if (Oi2Flags.HasAsyncHandle) strcat(buffer, " HasAsyncHandle");
270 return buffer[0] ? wine_dbg_sprintf( "%s", buffer + 1 ) : "";
273 #define ARG_FROM_OFFSET(args, offset) ((args) + (offset))
275 static PFORMAT_STRING client_get_handle(
276 PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
277 PFORMAT_STRING pFormat, handle_t *phBinding)
279 /* binding */
280 switch (pProcHeader->handle_type)
282 /* explicit binding: parse additional section */
283 case 0:
284 switch (*pFormat) /* handle_type */
286 case FC_BIND_PRIMITIVE: /* explicit primitive */
288 const NDR_EHD_PRIMITIVE *pDesc = (const NDR_EHD_PRIMITIVE *)pFormat;
290 TRACE("Explicit primitive handle @ %d\n", pDesc->offset);
292 if (pDesc->flag) /* pointer to binding */
293 *phBinding = **(handle_t **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
294 else
295 *phBinding = *(handle_t *)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
296 return pFormat + sizeof(NDR_EHD_PRIMITIVE);
298 case FC_BIND_GENERIC: /* explicit generic */
300 const NDR_EHD_GENERIC *pDesc = (const NDR_EHD_GENERIC *)pFormat;
301 void *pObject = NULL;
302 void *pArg;
303 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
305 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
307 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
308 pArg = *(void **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
309 else
310 pArg = ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
311 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
312 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
313 *phBinding = pGenPair->pfnBind(pObject);
314 return pFormat + sizeof(NDR_EHD_GENERIC);
316 case FC_BIND_CONTEXT: /* explicit context */
318 const NDR_EHD_CONTEXT *pDesc = (const NDR_EHD_CONTEXT *)pFormat;
319 NDR_CCONTEXT context_handle;
320 TRACE("Explicit bind context\n");
321 if (pDesc->flags & HANDLE_PARAM_IS_VIA_PTR)
323 TRACE("\tHANDLE_PARAM_IS_VIA_PTR\n");
324 context_handle = **(NDR_CCONTEXT **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
326 else
327 context_handle = *(NDR_CCONTEXT *)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
329 if (context_handle) *phBinding = NDRCContextBinding(context_handle);
330 else if (pDesc->flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL)
332 ERR("null context handle isn't allowed\n");
333 RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);
334 return NULL;
336 /* FIXME: should we store this structure in stubMsg.pContext? */
337 return pFormat + sizeof(NDR_EHD_CONTEXT);
339 default:
340 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
341 RpcRaiseException(RPC_X_BAD_STUB_DATA);
343 break;
344 case FC_BIND_GENERIC: /* implicit generic */
345 FIXME("FC_BIND_GENERIC\n");
346 RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
347 break;
348 case FC_BIND_PRIMITIVE: /* implicit primitive */
349 TRACE("Implicit primitive handle\n");
350 *phBinding = *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pPrimitiveHandle;
351 break;
352 case FC_CALLBACK_HANDLE: /* implicit callback */
353 TRACE("FC_CALLBACK_HANDLE\n");
354 /* server calls callback procedures only in response to remote call, and most recent
355 binding handle is used. Calling back to a client can potentially result in another
356 callback with different current handle. */
357 *phBinding = I_RpcGetCurrentCallHandle();
358 break;
359 case FC_AUTO_HANDLE: /* implicit auto handle */
360 /* strictly speaking, it isn't necessary to set hBinding here
361 * since it isn't actually used (hence the automatic in its name),
362 * but then why does MIDL generate a valid entry in the
363 * MIDL_STUB_DESC for it? */
364 TRACE("Implicit auto handle\n");
365 *phBinding = *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle;
366 break;
367 default:
368 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
369 RpcRaiseException(RPC_X_BAD_STUB_DATA);
371 return pFormat;
374 static void client_free_handle(
375 PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
376 PFORMAT_STRING pFormat, handle_t hBinding)
378 /* binding */
379 switch (pProcHeader->handle_type)
381 /* explicit binding: parse additional section */
382 case 0:
383 switch (*pFormat) /* handle_type */
385 case FC_BIND_GENERIC: /* explicit generic */
387 const NDR_EHD_GENERIC *pDesc = (const NDR_EHD_GENERIC *)pFormat;
388 void *pObject = NULL;
389 void *pArg;
390 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
392 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
394 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
395 pArg = *(void **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
396 else
397 pArg = ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
398 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
399 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
400 pGenPair->pfnUnbind(pObject, hBinding);
401 break;
403 case FC_BIND_CONTEXT: /* explicit context */
404 case FC_BIND_PRIMITIVE: /* explicit primitive */
405 break;
406 default:
407 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
408 RpcRaiseException(RPC_X_BAD_STUB_DATA);
410 break;
411 case FC_BIND_GENERIC: /* implicit generic */
412 FIXME("FC_BIND_GENERIC\n");
413 RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
414 break;
415 case FC_CALLBACK_HANDLE: /* implicit callback */
416 case FC_BIND_PRIMITIVE: /* implicit primitive */
417 case FC_AUTO_HANDLE: /* implicit auto handle */
418 break;
419 default:
420 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
421 RpcRaiseException(RPC_X_BAD_STUB_DATA);
425 void client_do_args( PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat, enum stubless_phase phase,
426 void **fpu_args, unsigned short number_of_params, unsigned char *pRetVal )
428 const NDR_PARAM_OIF *params = (const NDR_PARAM_OIF *)pFormat;
429 unsigned int i;
431 for (i = 0; i < number_of_params; i++)
433 unsigned char *pArg = pStubMsg->StackTop + params[i].stack_offset;
434 PFORMAT_STRING pTypeFormat = (PFORMAT_STRING)&pStubMsg->StubDesc->pFormatTypes[params[i].u.type_offset];
436 #ifdef __x86_64__ /* floats are passed as doubles through varargs functions */
437 float f;
439 if (params[i].attr.IsBasetype &&
440 params[i].u.type_format_char == FC_FLOAT &&
441 !params[i].attr.IsSimpleRef &&
442 !fpu_args)
444 f = *(double *)pArg;
445 pArg = (unsigned char *)&f;
447 #endif
449 TRACE("param[%d]: %p type %02x %s\n", i, pArg,
450 params[i].attr.IsBasetype ? params[i].u.type_format_char : *pTypeFormat,
451 debugstr_PROC_PF( params[i].attr ));
453 switch (phase)
455 case STUBLESS_INITOUT:
456 if (!params[i].attr.IsBasetype && params[i].attr.IsOut &&
457 !params[i].attr.IsIn && !params[i].attr.IsByValue)
459 memset( *(unsigned char **)pArg, 0, calc_arg_size( pStubMsg, pTypeFormat ));
461 break;
462 case STUBLESS_CALCSIZE:
463 if (params[i].attr.IsSimpleRef && !*(unsigned char **)pArg)
464 RpcRaiseException(RPC_X_NULL_REF_POINTER);
465 if (params[i].attr.IsIn) call_buffer_sizer(pStubMsg, pArg, &params[i]);
466 break;
467 case STUBLESS_MARSHAL:
468 if (params[i].attr.IsIn) call_marshaller(pStubMsg, pArg, &params[i]);
469 break;
470 case STUBLESS_UNMARSHAL:
471 if (params[i].attr.IsOut)
473 if (params[i].attr.IsReturn && pRetVal) pArg = pRetVal;
474 call_unmarshaller(pStubMsg, &pArg, &params[i], 0);
476 break;
477 case STUBLESS_FREE:
478 if (!params[i].attr.IsBasetype && params[i].attr.IsOut && !params[i].attr.IsByValue)
479 NdrClearOutParameters( pStubMsg, pTypeFormat, *(unsigned char **)pArg );
480 break;
481 default:
482 RpcRaiseException(RPC_S_INTERNAL_ERROR);
487 static unsigned int type_stack_size(unsigned char fc)
489 switch (fc)
491 case FC_BYTE:
492 case FC_CHAR:
493 case FC_SMALL:
494 case FC_USMALL:
495 case FC_WCHAR:
496 case FC_SHORT:
497 case FC_USHORT:
498 case FC_LONG:
499 case FC_ULONG:
500 case FC_INT3264:
501 case FC_UINT3264:
502 case FC_ENUM16:
503 case FC_ENUM32:
504 case FC_FLOAT:
505 case FC_ERROR_STATUS_T:
506 case FC_IGNORE:
507 return sizeof(void *);
508 case FC_DOUBLE:
509 return sizeof(double);
510 case FC_HYPER:
511 return sizeof(ULONGLONG);
512 default:
513 ERR("invalid base type 0x%x\n", fc);
514 RpcRaiseException(RPC_S_INTERNAL_ERROR);
518 static BOOL is_by_value( PFORMAT_STRING format )
520 switch (*format)
522 case FC_USER_MARSHAL:
523 case FC_STRUCT:
524 case FC_PSTRUCT:
525 case FC_CSTRUCT:
526 case FC_CPSTRUCT:
527 case FC_CVSTRUCT:
528 case FC_BOGUS_STRUCT:
529 return TRUE;
530 default:
531 return FALSE;
535 PFORMAT_STRING convert_old_args( PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat,
536 unsigned int stack_size, BOOL object_proc,
537 void *buffer, unsigned int size, unsigned int *count )
539 NDR_PARAM_OIF *args = buffer;
540 unsigned int i, stack_offset = object_proc ? sizeof(void *) : 0;
542 for (i = 0; stack_offset < stack_size; i++)
544 const NDR_PARAM_OI_BASETYPE *param = (const NDR_PARAM_OI_BASETYPE *)pFormat;
545 const NDR_PARAM_OI_OTHER *other = (const NDR_PARAM_OI_OTHER *)pFormat;
547 if (i + 1 > size / sizeof(*args))
549 FIXME( "%u args not supported\n", i );
550 RpcRaiseException( RPC_S_INTERNAL_ERROR );
553 args[i].stack_offset = stack_offset;
554 memset( &args[i].attr, 0, sizeof(args[i].attr) );
556 switch (param->param_direction)
558 case FC_IN_PARAM_BASETYPE:
559 args[i].attr.IsIn = 1;
560 args[i].attr.IsBasetype = 1;
561 break;
562 case FC_RETURN_PARAM_BASETYPE:
563 args[i].attr.IsOut = 1;
564 args[i].attr.IsReturn = 1;
565 args[i].attr.IsBasetype = 1;
566 break;
567 case FC_IN_PARAM:
568 args[i].attr.IsIn = 1;
569 args[i].attr.MustFree = 1;
570 break;
571 case FC_IN_PARAM_NO_FREE_INST:
572 args[i].attr.IsIn = 1;
573 args[i].attr.IsDontCallFreeInst = 1;
574 break;
575 case FC_IN_OUT_PARAM:
576 args[i].attr.IsIn = 1;
577 args[i].attr.IsOut = 1;
578 args[i].attr.MustFree = 1;
579 break;
580 case FC_OUT_PARAM:
581 args[i].attr.IsOut = 1;
582 break;
583 case FC_RETURN_PARAM:
584 args[i].attr.IsOut = 1;
585 args[i].attr.IsReturn = 1;
586 break;
588 if (args[i].attr.IsBasetype)
590 args[i].u.type_format_char = param->type_format_char;
591 stack_offset += type_stack_size( param->type_format_char );
592 pFormat += sizeof(NDR_PARAM_OI_BASETYPE);
594 else
596 args[i].u.type_offset = other->type_offset;
597 args[i].attr.IsByValue = is_by_value( &pStubMsg->StubDesc->pFormatTypes[other->type_offset] );
598 stack_offset += other->stack_size * sizeof(void *);
599 pFormat += sizeof(NDR_PARAM_OI_OTHER);
602 *count = i;
603 return (PFORMAT_STRING)args;
606 LONG_PTR CDECL DECLSPEC_HIDDEN ndr_client_call( PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat,
607 void **stack_top, void **fpu_stack )
609 /* pointer to start of stack where arguments start */
610 RPC_MESSAGE rpcMsg;
611 MIDL_STUB_MESSAGE stubMsg;
612 handle_t hBinding = NULL;
613 /* procedure number */
614 unsigned short procedure_number;
615 /* size of stack */
616 unsigned short stack_size;
617 /* number of parameters. optional for client to give it to us */
618 unsigned int number_of_params;
619 /* cache of Oif_flags from v2 procedure header */
620 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
621 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
622 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
623 /* header for procedure string */
624 const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
625 /* the value to return to the client from the remote procedure */
626 LONG_PTR RetVal = 0;
627 /* the pointer to the object when in OLE mode */
628 void * This = NULL;
629 PFORMAT_STRING pHandleFormat;
630 /* correlation cache */
631 ULONG_PTR NdrCorrCache[256];
633 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
635 TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
637 if (pProcHeader->Oi_flags & Oi_HAS_RPCFLAGS)
639 const NDR_PROC_HEADER_RPC *header_rpc = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
640 stack_size = header_rpc->stack_size;
641 procedure_number = header_rpc->proc_num;
642 pFormat += sizeof(NDR_PROC_HEADER_RPC);
644 else
646 stack_size = pProcHeader->stack_size;
647 procedure_number = pProcHeader->proc_num;
648 pFormat += sizeof(NDR_PROC_HEADER);
650 TRACE("stack size: 0x%x\n", stack_size);
651 TRACE("proc num: %d\n", procedure_number);
653 /* create the full pointer translation tables, if requested */
654 if (pProcHeader->Oi_flags & Oi_FULL_PTR_USED)
655 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
657 if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
659 /* object is always the first argument */
660 This = stack_top[0];
661 NdrProxyInitialize(This, &rpcMsg, &stubMsg, pStubDesc, procedure_number);
663 else
664 NdrClientInitializeNew(&rpcMsg, &stubMsg, pStubDesc, procedure_number);
666 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
667 TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
669 stubMsg.StackTop = (unsigned char *)stack_top;
670 pHandleFormat = pFormat;
672 /* we only need a handle if this isn't an object method */
673 if (!(pProcHeader->Oi_flags & Oi_OBJECT_PROC))
675 pFormat = client_get_handle(&stubMsg, pProcHeader, pHandleFormat, &hBinding);
676 if (!pFormat) goto done;
679 if (is_oicf_stubdesc(pStubDesc)) /* -Oicf format */
681 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
682 (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
684 Oif_flags = pOIFHeader->Oi2Flags;
685 number_of_params = pOIFHeader->number_of_params;
687 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
689 TRACE("Oif_flags = %s\n", debugstr_INTERPRETER_OPT_FLAGS(Oif_flags) );
691 if (Oif_flags.HasExtensions)
693 const NDR_PROC_HEADER_EXTS *pExtensions = (const NDR_PROC_HEADER_EXTS *)pFormat;
694 ext_flags = pExtensions->Flags2;
695 pFormat += pExtensions->Size;
696 #ifdef __x86_64__
697 if (pExtensions->Size > sizeof(*pExtensions) && fpu_stack)
699 int i;
700 unsigned short fpu_mask = *(unsigned short *)(pExtensions + 1);
701 for (i = 0; i < 4; i++, fpu_mask >>= 2)
702 switch (fpu_mask & 3)
704 case 1: *(float *)&stack_top[i] = *(float *)&fpu_stack[i]; break;
705 case 2: *(double *)&stack_top[i] = *(double *)&fpu_stack[i]; break;
708 #endif
711 else
713 pFormat = convert_old_args( &stubMsg, pFormat, stack_size,
714 pProcHeader->Oi_flags & Oi_OBJECT_PROC,
715 /* reuse the correlation cache, it's not needed for v1 format */
716 NdrCorrCache, sizeof(NdrCorrCache), &number_of_params );
719 stubMsg.BufferLength = 0;
721 /* store the RPC flags away */
722 if (pProcHeader->Oi_flags & Oi_HAS_RPCFLAGS)
723 rpcMsg.RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
725 /* use alternate memory allocation routines */
726 if (pProcHeader->Oi_flags & Oi_RPCSS_ALLOC_USED)
727 NdrRpcSmSetClientToOsf(&stubMsg);
729 if (Oif_flags.HasPipes)
731 FIXME("pipes not supported yet\n");
732 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
733 /* init pipes package */
734 /* NdrPipesInitialize(...) */
736 if (ext_flags.HasNewCorrDesc)
738 /* initialize extra correlation package */
739 NdrCorrelationInitialize(&stubMsg, NdrCorrCache, sizeof(NdrCorrCache), 0);
740 if (ext_flags.Unused & 0x2) /* has range on conformance */
741 stubMsg.CorrDespIncrement = 12;
744 /* order of phases:
745 * 1. INITOUT - zero [out] parameters (proxies only)
746 * 2. CALCSIZE - calculate the buffer size
747 * 3. GETBUFFER - allocate the buffer
748 * 4. MARSHAL - marshal [in] params into the buffer
749 * 5. SENDRECEIVE - send/receive buffer
750 * 6. UNMARSHAL - unmarshal [out] params from buffer
751 * 7. FREE - clear [out] parameters (for proxies, and only on error)
753 if ((pProcHeader->Oi_flags & Oi_OBJECT_PROC) ||
754 (pProcHeader->Oi_flags & Oi_HAS_COMM_OR_FAULT))
756 /* 1. INITOUT */
757 if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
759 TRACE( "INITOUT\n" );
760 client_do_args(&stubMsg, pFormat, STUBLESS_INITOUT, fpu_stack,
761 number_of_params, (unsigned char *)&RetVal);
764 __TRY
766 /* 2. CALCSIZE */
767 TRACE( "CALCSIZE\n" );
768 client_do_args(&stubMsg, pFormat, STUBLESS_CALCSIZE, fpu_stack,
769 number_of_params, (unsigned char *)&RetVal);
771 /* 3. GETBUFFER */
772 TRACE( "GETBUFFER\n" );
773 if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
775 /* allocate the buffer */
776 NdrProxyGetBuffer(This, &stubMsg);
778 else
780 /* allocate the buffer */
781 if (Oif_flags.HasPipes)
782 /* NdrGetPipeBuffer(...) */
783 FIXME("pipes not supported yet\n");
784 else
786 if (pProcHeader->handle_type == FC_AUTO_HANDLE)
787 #if 0
788 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
789 #else
790 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
791 #endif
792 else
793 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
797 /* 4. MARSHAL */
798 TRACE( "MARSHAL\n" );
799 client_do_args(&stubMsg, pFormat, STUBLESS_MARSHAL, fpu_stack,
800 number_of_params, (unsigned char *)&RetVal);
802 /* 5. SENDRECEIVE */
803 TRACE( "SENDRECEIVE\n" );
804 if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
806 /* send the [in] params and receive the [out] and [retval]
807 * params */
808 NdrProxySendReceive(This, &stubMsg);
810 else
812 /* send the [in] params and receive the [out] and [retval]
813 * params */
814 if (Oif_flags.HasPipes)
815 /* NdrPipesSendReceive(...) */
816 FIXME("pipes not supported yet\n");
817 else
819 if (pProcHeader->handle_type == FC_AUTO_HANDLE)
820 #if 0
821 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
822 #else
823 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
824 #endif
825 else
826 NdrSendReceive(&stubMsg, stubMsg.Buffer);
830 /* convert strings, floating point values and endianness into our
831 * preferred format */
832 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
833 NdrConvert(&stubMsg, pFormat);
835 /* 6. UNMARSHAL */
836 TRACE( "UNMARSHAL\n" );
837 client_do_args(&stubMsg, pFormat, STUBLESS_UNMARSHAL, fpu_stack,
838 number_of_params, (unsigned char *)&RetVal);
840 __EXCEPT_ALL
842 if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
844 /* 7. FREE */
845 TRACE( "FREE\n" );
846 client_do_args(&stubMsg, pFormat, STUBLESS_FREE, fpu_stack,
847 number_of_params, (unsigned char *)&RetVal);
848 RetVal = NdrProxyErrorHandler(GetExceptionCode());
850 else
852 const COMM_FAULT_OFFSETS *comm_fault_offsets = &pStubDesc->CommFaultOffsets[procedure_number];
853 ULONG *comm_status;
854 ULONG *fault_status;
856 TRACE("comm_fault_offsets = {0x%hx, 0x%hx}\n", comm_fault_offsets->CommOffset, comm_fault_offsets->FaultOffset);
858 if (comm_fault_offsets->CommOffset == -1)
859 comm_status = (ULONG *)&RetVal;
860 else if (comm_fault_offsets->CommOffset >= 0)
861 comm_status = *(ULONG **)ARG_FROM_OFFSET(stubMsg.StackTop, comm_fault_offsets->CommOffset);
862 else
863 comm_status = NULL;
865 if (comm_fault_offsets->FaultOffset == -1)
866 fault_status = (ULONG *)&RetVal;
867 else if (comm_fault_offsets->FaultOffset >= 0)
868 fault_status = *(ULONG **)ARG_FROM_OFFSET(stubMsg.StackTop, comm_fault_offsets->FaultOffset);
869 else
870 fault_status = NULL;
872 NdrMapCommAndFaultStatus(&stubMsg, comm_status, fault_status,
873 GetExceptionCode());
876 __ENDTRY
878 else
880 /* 2. CALCSIZE */
881 TRACE( "CALCSIZE\n" );
882 client_do_args(&stubMsg, pFormat, STUBLESS_CALCSIZE, fpu_stack,
883 number_of_params, (unsigned char *)&RetVal);
885 /* 3. GETBUFFER */
886 TRACE( "GETBUFFER\n" );
887 if (Oif_flags.HasPipes)
888 /* NdrGetPipeBuffer(...) */
889 FIXME("pipes not supported yet\n");
890 else
892 if (pProcHeader->handle_type == FC_AUTO_HANDLE)
893 #if 0
894 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
895 #else
896 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
897 #endif
898 else
899 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
902 /* 4. MARSHAL */
903 TRACE( "MARSHAL\n" );
904 client_do_args(&stubMsg, pFormat, STUBLESS_MARSHAL, fpu_stack,
905 number_of_params, (unsigned char *)&RetVal);
907 /* 5. SENDRECEIVE */
908 TRACE( "SENDRECEIVE\n" );
909 if (Oif_flags.HasPipes)
910 /* NdrPipesSendReceive(...) */
911 FIXME("pipes not supported yet\n");
912 else
914 if (pProcHeader->handle_type == FC_AUTO_HANDLE)
915 #if 0
916 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
917 #else
918 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
919 #endif
920 else
921 NdrSendReceive(&stubMsg, stubMsg.Buffer);
924 /* convert strings, floating point values and endianness into our
925 * preferred format */
926 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
927 NdrConvert(&stubMsg, pFormat);
929 /* 6. UNMARSHAL */
930 TRACE( "UNMARSHAL\n" );
931 client_do_args(&stubMsg, pFormat, STUBLESS_UNMARSHAL, fpu_stack,
932 number_of_params, (unsigned char *)&RetVal);
935 if (ext_flags.HasNewCorrDesc)
937 /* free extra correlation package */
938 NdrCorrelationFree(&stubMsg);
941 if (Oif_flags.HasPipes)
943 /* NdrPipesDone(...) */
946 /* free the full pointer translation tables */
947 if (pProcHeader->Oi_flags & Oi_FULL_PTR_USED)
948 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
950 /* free marshalling buffer */
951 if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
952 NdrProxyFreeBuffer(This, &stubMsg);
953 else
955 NdrFreeBuffer(&stubMsg);
956 client_free_handle(&stubMsg, pProcHeader, pHandleFormat, hBinding);
959 done:
960 TRACE("RetVal = 0x%lx\n", RetVal);
961 return RetVal;
964 #ifdef __x86_64__
966 __ASM_GLOBAL_FUNC( NdrClientCall2,
967 "movq %r8,0x18(%rsp)\n\t"
968 "movq %r9,0x20(%rsp)\n\t"
969 "leaq 0x18(%rsp),%r8\n\t"
970 "xorq %r9,%r9\n\t"
971 "subq $0x28,%rsp\n\t"
972 __ASM_CFI(".cfi_adjust_cfa_offset 0x28\n\t")
973 "call " __ASM_NAME("ndr_client_call") "\n\t"
974 "addq $0x28,%rsp\n\t"
975 __ASM_CFI(".cfi_adjust_cfa_offset -0x28\n\t")
976 "ret" );
978 #else /* __x86_64__ */
980 /***********************************************************************
981 * NdrClientCall2 [RPCRT4.@]
983 CLIENT_CALL_RETURN WINAPIV NdrClientCall2( PMIDL_STUB_DESC desc, PFORMAT_STRING format, ... )
985 __ms_va_list args;
986 LONG_PTR ret;
988 __ms_va_start( args, format );
989 ret = ndr_client_call( desc, format, va_arg( args, void ** ), NULL );
990 __ms_va_end( args );
991 return *(CLIENT_CALL_RETURN *)&ret;
994 #endif /* __x86_64__ */
996 /* Calls a function with the specified arguments, restoring the stack
997 * properly afterwards as we don't know the calling convention of the
998 * function */
999 #if defined __i386__ && defined _MSC_VER
1000 __declspec(naked) LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size)
1002 __asm
1004 push ebp
1005 mov ebp, esp
1006 push edi ; Save registers
1007 push esi
1008 mov eax, [ebp+16] ; Get stack size
1009 sub esp, eax ; Make room in stack for arguments
1010 and esp, 0xFFFFFFF0
1011 mov edi, esp
1012 mov ecx, eax
1013 mov esi, [ebp+12]
1014 shr ecx, 2
1016 rep movsd ; Copy dword blocks
1017 call [ebp+8] ; Call function
1018 lea esp, [ebp-8] ; Restore stack
1019 pop esi ; Restore registers
1020 pop edi
1021 pop ebp
1025 #elif defined __i386__ && defined __GNUC__
1026 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
1027 __ASM_GLOBAL_FUNC(call_server_func,
1028 "pushl %ebp\n\t"
1029 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1030 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1031 "movl %esp,%ebp\n\t"
1032 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1033 "pushl %edi\n\t" /* Save registers */
1034 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
1035 "pushl %esi\n\t"
1036 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
1037 "movl 16(%ebp), %eax\n\t" /* Get stack size */
1038 "subl %eax, %esp\n\t" /* Make room in stack for arguments */
1039 "andl $~15, %esp\n\t" /* Make sure stack has 16-byte alignment for Mac OS X */
1040 "movl %esp, %edi\n\t"
1041 "movl %eax, %ecx\n\t"
1042 "movl 12(%ebp), %esi\n\t"
1043 "shrl $2, %ecx\n\t" /* divide by 4 */
1044 "cld\n\t"
1045 "rep; movsl\n\t" /* Copy dword blocks */
1046 "call *8(%ebp)\n\t" /* Call function */
1047 "leal -8(%ebp), %esp\n\t" /* Restore stack */
1048 "popl %esi\n\t" /* Restore registers */
1049 __ASM_CFI(".cfi_same_value %esi\n\t")
1050 "popl %edi\n\t"
1051 __ASM_CFI(".cfi_same_value %edi\n\t")
1052 "popl %ebp\n\t"
1053 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1054 __ASM_CFI(".cfi_same_value %ebp\n\t")
1055 "ret" )
1056 #elif defined __x86_64__
1057 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
1058 __ASM_GLOBAL_FUNC( call_server_func,
1059 "pushq %rbp\n\t"
1060 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1061 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1062 "movq %rsp,%rbp\n\t"
1063 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
1064 "pushq %rsi\n\t"
1065 __ASM_CFI(".cfi_rel_offset %rsi,-8\n\t")
1066 "pushq %rdi\n\t"
1067 __ASM_CFI(".cfi_rel_offset %rdi,-16\n\t")
1068 "movq %rcx,%rax\n\t" /* function to call */
1069 "movq $32,%rcx\n\t" /* allocate max(32,stack_size) bytes of stack space */
1070 "cmpq %rcx,%r8\n\t"
1071 "cmovgq %r8,%rcx\n\t"
1072 "subq %rcx,%rsp\n\t"
1073 "andq $~15,%rsp\n\t"
1074 "movq %r8,%rcx\n\t"
1075 "shrq $3,%rcx\n\t"
1076 "movq %rsp,%rdi\n\t"
1077 "movq %rdx,%rsi\n\t"
1078 "rep; movsq\n\t" /* copy arguments */
1079 "movq 0(%rsp),%rcx\n\t"
1080 "movq 8(%rsp),%rdx\n\t"
1081 "movq 16(%rsp),%r8\n\t"
1082 "movq 24(%rsp),%r9\n\t"
1083 "movq 0(%rsp),%xmm0\n\t"
1084 "movq 8(%rsp),%xmm1\n\t"
1085 "movq 16(%rsp),%xmm2\n\t"
1086 "movq 24(%rsp),%xmm3\n\t"
1087 "callq *%rax\n\t"
1088 "leaq -16(%rbp),%rsp\n\t" /* restore stack */
1089 "popq %rdi\n\t"
1090 __ASM_CFI(".cfi_same_value %rdi\n\t")
1091 "popq %rsi\n\t"
1092 __ASM_CFI(".cfi_same_value %rsi\n\t")
1093 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
1094 "popq %rbp\n\t"
1095 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1096 __ASM_CFI(".cfi_same_value %rbp\n\t")
1097 "ret")
1098 #elif defined __arm__
1099 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char *args, unsigned int stack_size);
1100 __ASM_GLOBAL_FUNC( call_server_func,
1101 ".arm\n\t"
1102 "push {r4, r5, LR}\n\t"
1103 "mov r4, r0\n\t"
1104 "mov r5, SP\n\t"
1105 "lsr r3, r2, #2\n\t"
1106 "cmp r3, #0\n\t"
1107 "beq 5f\n\t"
1108 "sub SP, SP, r2\n\t"
1109 "tst r3, #1\n\t"
1110 "subeq SP, SP, #4\n\t"
1111 "1:\tsub r2, r2, #4\n\t"
1112 "ldr r0, [r1, r2]\n\t"
1113 "str r0, [SP, r2]\n\t"
1114 "cmp r2, #0\n\t"
1115 "bgt 1b\n\t"
1116 "cmp r3, #1\n\t"
1117 "bgt 2f\n\t"
1118 "pop {r0}\n\t"
1119 "b 5f\n\t"
1120 "2:\tcmp r3, #2\n\t"
1121 "bgt 3f\n\t"
1122 "pop {r0-r1}\n\t"
1123 "b 5f\n\t"
1124 "3:\tcmp r3, #3\n\t"
1125 "bgt 4f\n\t"
1126 "pop {r0-r2}\n\t"
1127 "b 5f\n\t"
1128 "4:\tpop {r0-r3}\n\t"
1129 "5:\tblx r4\n\t"
1130 "mov SP, r5\n\t"
1131 "pop {r4, r5, PC}" )
1132 #else
1133 #warning call_server_func not implemented for your architecture
1134 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned short stack_size)
1136 FIXME("Not implemented for your architecture\n");
1137 return 0;
1139 #endif
1141 static inline BOOL param_needs_alloc( PARAM_ATTRIBUTES attr )
1143 return attr.IsOut && !attr.IsIn && !attr.IsBasetype && !attr.IsByValue;
1146 static LONG_PTR *stub_do_args(MIDL_STUB_MESSAGE *pStubMsg,
1147 PFORMAT_STRING pFormat, enum stubless_phase phase,
1148 unsigned short number_of_params)
1150 const NDR_PARAM_OIF *params = (const NDR_PARAM_OIF *)pFormat;
1151 unsigned int i;
1152 LONG_PTR *retval_ptr = NULL;
1154 for (i = 0; i < number_of_params; i++)
1156 unsigned char *pArg = pStubMsg->StackTop + params[i].stack_offset;
1157 const unsigned char *pTypeFormat = &pStubMsg->StubDesc->pFormatTypes[params[i].u.type_offset];
1159 TRACE("param[%d]: %p -> %p type %02x %s\n", i,
1160 pArg, *(unsigned char **)pArg,
1161 params[i].attr.IsBasetype ? params[i].u.type_format_char : *pTypeFormat,
1162 debugstr_PROC_PF( params[i].attr ));
1164 switch (phase)
1166 case STUBLESS_MARSHAL:
1167 if (params[i].attr.IsOut || params[i].attr.IsReturn)
1168 call_marshaller(pStubMsg, pArg, &params[i]);
1169 break;
1170 case STUBLESS_MUSTFREE:
1171 if (params[i].attr.MustFree)
1173 call_freer(pStubMsg, pArg, &params[i]);
1175 break;
1176 case STUBLESS_FREE:
1177 if (params[i].attr.ServerAllocSize)
1179 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
1181 else if (param_needs_alloc(params[i].attr) &&
1182 (!params[i].attr.MustFree || params[i].attr.IsSimpleRef))
1184 if (*pTypeFormat != FC_BIND_CONTEXT) pStubMsg->pfnFree(*(void **)pArg);
1186 break;
1187 case STUBLESS_INITOUT:
1188 if (param_needs_alloc(params[i].attr) && !params[i].attr.ServerAllocSize)
1190 if (*pTypeFormat == FC_BIND_CONTEXT)
1192 NDR_SCONTEXT ctxt = NdrContextHandleInitialize(pStubMsg, pTypeFormat);
1193 *(void **)pArg = NDRSContextValue(ctxt);
1195 else
1197 DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1198 if (size)
1200 *(void **)pArg = NdrAllocate(pStubMsg, size);
1201 memset(*(void **)pArg, 0, size);
1205 break;
1206 case STUBLESS_UNMARSHAL:
1207 if (params[i].attr.ServerAllocSize)
1208 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1209 params[i].attr.ServerAllocSize * 8);
1211 if (params[i].attr.IsIn)
1212 call_unmarshaller(pStubMsg, &pArg, &params[i], 0);
1213 break;
1214 case STUBLESS_CALCSIZE:
1215 if (params[i].attr.IsOut || params[i].attr.IsReturn)
1216 call_buffer_sizer(pStubMsg, pArg, &params[i]);
1217 break;
1218 default:
1219 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1221 TRACE("\tmemory addr (after): %p -> %p\n", pArg, *(unsigned char **)pArg);
1223 /* make a note of the address of the return value parameter for later */
1224 if (params[i].attr.IsReturn) retval_ptr = (LONG_PTR *)pArg;
1226 return retval_ptr;
1229 /***********************************************************************
1230 * NdrStubCall2 [RPCRT4.@]
1232 * Unmarshals [in] parameters, calls either a method in an object or a server
1233 * function, marshals any [out] parameters and frees any allocated data.
1235 * NOTES
1236 * Used by stubless MIDL-generated code.
1238 LONG WINAPI NdrStubCall2(
1239 struct IRpcStubBuffer * pThis,
1240 struct IRpcChannelBuffer * pChannel,
1241 PRPC_MESSAGE pRpcMsg,
1242 DWORD * pdwStubPhase)
1244 const MIDL_SERVER_INFO *pServerInfo;
1245 const MIDL_STUB_DESC *pStubDesc;
1246 PFORMAT_STRING pFormat;
1247 MIDL_STUB_MESSAGE stubMsg;
1248 /* pointer to start of stack to pass into stub implementation */
1249 unsigned char * args;
1250 /* size of stack */
1251 unsigned short stack_size;
1252 /* number of parameters. optional for client to give it to us */
1253 unsigned int number_of_params;
1254 /* cache of Oif_flags from v2 procedure header */
1255 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1256 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1257 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1258 /* the type of pass we are currently doing */
1259 enum stubless_phase phase;
1260 /* header for procedure string */
1261 const NDR_PROC_HEADER *pProcHeader;
1262 /* location to put retval into */
1263 LONG_PTR *retval_ptr = NULL;
1264 /* correlation cache */
1265 ULONG_PTR NdrCorrCache[256];
1267 TRACE("pThis %p, pChannel %p, pRpcMsg %p, pdwStubPhase %p\n", pThis, pChannel, pRpcMsg, pdwStubPhase);
1269 if (pThis)
1270 pServerInfo = CStdStubBuffer_GetServerInfo(pThis);
1271 else
1272 pServerInfo = ((RPC_SERVER_INTERFACE *)pRpcMsg->RpcInterfaceInformation)->InterpreterInfo;
1274 pStubDesc = pServerInfo->pStubDesc;
1275 pFormat = pServerInfo->ProcString + pServerInfo->FmtStringOffset[pRpcMsg->ProcNum];
1276 pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1278 TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
1280 if (pProcHeader->Oi_flags & Oi_HAS_RPCFLAGS)
1282 const NDR_PROC_HEADER_RPC *header_rpc = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1283 stack_size = header_rpc->stack_size;
1284 pFormat += sizeof(NDR_PROC_HEADER_RPC);
1287 else
1289 stack_size = pProcHeader->stack_size;
1290 pFormat += sizeof(NDR_PROC_HEADER);
1293 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1295 /* binding */
1296 switch (pProcHeader->handle_type)
1298 /* explicit binding: parse additional section */
1299 case 0:
1300 switch (*pFormat) /* handle_type */
1302 case FC_BIND_PRIMITIVE: /* explicit primitive */
1303 pFormat += sizeof(NDR_EHD_PRIMITIVE);
1304 break;
1305 case FC_BIND_GENERIC: /* explicit generic */
1306 pFormat += sizeof(NDR_EHD_GENERIC);
1307 break;
1308 case FC_BIND_CONTEXT: /* explicit context */
1309 pFormat += sizeof(NDR_EHD_CONTEXT);
1310 break;
1311 default:
1312 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1313 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1315 break;
1316 case FC_BIND_GENERIC: /* implicit generic */
1317 case FC_BIND_PRIMITIVE: /* implicit primitive */
1318 case FC_CALLBACK_HANDLE: /* implicit callback */
1319 case FC_AUTO_HANDLE: /* implicit auto handle */
1320 break;
1321 default:
1322 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1323 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1326 if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
1327 NdrStubInitialize(pRpcMsg, &stubMsg, pStubDesc, pChannel);
1328 else
1329 NdrServerInitializeNew(pRpcMsg, &stubMsg, pStubDesc);
1331 /* create the full pointer translation tables, if requested */
1332 if (pProcHeader->Oi_flags & Oi_FULL_PTR_USED)
1333 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_SERVER);
1335 /* store the RPC flags away */
1336 if (pProcHeader->Oi_flags & Oi_HAS_RPCFLAGS)
1337 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1339 /* use alternate memory allocation routines */
1340 if (pProcHeader->Oi_flags & Oi_RPCSS_ALLOC_USED)
1341 #if 0
1342 NdrRpcSsEnableAllocate(&stubMsg);
1343 #else
1344 FIXME("Set RPCSS memory allocation routines\n");
1345 #endif
1347 TRACE("allocating memory for stack of size %x\n", stack_size);
1349 args = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, stack_size);
1350 stubMsg.StackTop = args; /* used by conformance of top-level objects */
1352 /* add the implicit This pointer as the first arg to the function if we
1353 * are calling an object method */
1354 if (pThis)
1355 *(void **)args = ((CStdStubBuffer *)pThis)->pvServerObject;
1357 if (is_oicf_stubdesc(pStubDesc))
1359 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader = (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
1361 Oif_flags = pOIFHeader->Oi2Flags;
1362 number_of_params = pOIFHeader->number_of_params;
1364 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1366 TRACE("Oif_flags = %s\n", debugstr_INTERPRETER_OPT_FLAGS(Oif_flags) );
1368 if (Oif_flags.HasExtensions)
1370 const NDR_PROC_HEADER_EXTS *pExtensions = (const NDR_PROC_HEADER_EXTS *)pFormat;
1371 ext_flags = pExtensions->Flags2;
1372 pFormat += pExtensions->Size;
1375 if (Oif_flags.HasPipes)
1377 FIXME("pipes not supported yet\n");
1378 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1379 /* init pipes package */
1380 /* NdrPipesInitialize(...) */
1382 if (ext_flags.HasNewCorrDesc)
1384 /* initialize extra correlation package */
1385 NdrCorrelationInitialize(&stubMsg, NdrCorrCache, sizeof(NdrCorrCache), 0);
1386 if (ext_flags.Unused & 0x2) /* has range on conformance */
1387 stubMsg.CorrDespIncrement = 12;
1390 else
1392 pFormat = convert_old_args( &stubMsg, pFormat, stack_size,
1393 pProcHeader->Oi_flags & Oi_OBJECT_PROC,
1394 /* reuse the correlation cache, it's not needed for v1 format */
1395 NdrCorrCache, sizeof(NdrCorrCache), &number_of_params );
1398 /* convert strings, floating point values and endianness into our
1399 * preferred format */
1400 if ((pRpcMsg->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1401 NdrConvert(&stubMsg, pFormat);
1403 for (phase = STUBLESS_UNMARSHAL; phase <= STUBLESS_FREE; phase++)
1405 TRACE("phase = %d\n", phase);
1406 switch (phase)
1408 case STUBLESS_CALLSERVER:
1409 /* call the server function */
1410 if (pServerInfo->ThunkTable && pServerInfo->ThunkTable[pRpcMsg->ProcNum])
1411 pServerInfo->ThunkTable[pRpcMsg->ProcNum](&stubMsg);
1412 else
1414 SERVER_ROUTINE func;
1415 LONG_PTR retval;
1417 if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
1419 SERVER_ROUTINE *vtbl = *(SERVER_ROUTINE **)((CStdStubBuffer *)pThis)->pvServerObject;
1420 func = vtbl[pRpcMsg->ProcNum];
1422 else
1423 func = pServerInfo->DispatchTable[pRpcMsg->ProcNum];
1425 /* FIXME: what happens with return values that don't fit into a single register on x86? */
1426 retval = call_server_func(func, args, stack_size);
1428 if (retval_ptr)
1430 TRACE("stub implementation returned 0x%lx\n", retval);
1431 *retval_ptr = retval;
1433 else
1434 TRACE("void stub implementation\n");
1437 stubMsg.Buffer = NULL;
1438 stubMsg.BufferLength = 0;
1440 break;
1441 case STUBLESS_GETBUFFER:
1442 if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
1443 NdrStubGetBuffer(pThis, pChannel, &stubMsg);
1444 else
1446 RPC_STATUS Status;
1448 pRpcMsg->BufferLength = stubMsg.BufferLength;
1449 /* allocate buffer for [out] and [ret] params */
1450 Status = I_RpcGetBuffer(pRpcMsg);
1451 if (Status)
1452 RpcRaiseException(Status);
1453 stubMsg.Buffer = pRpcMsg->Buffer;
1455 break;
1456 case STUBLESS_UNMARSHAL:
1457 case STUBLESS_INITOUT:
1458 case STUBLESS_CALCSIZE:
1459 case STUBLESS_MARSHAL:
1460 case STUBLESS_MUSTFREE:
1461 case STUBLESS_FREE:
1462 retval_ptr = stub_do_args(&stubMsg, pFormat, phase, number_of_params);
1463 break;
1464 default:
1465 ERR("shouldn't reach here. phase %d\n", phase);
1466 break;
1470 pRpcMsg->BufferLength = (unsigned int)(stubMsg.Buffer - (unsigned char *)pRpcMsg->Buffer);
1472 if (ext_flags.HasNewCorrDesc)
1474 /* free extra correlation package */
1475 NdrCorrelationFree(&stubMsg);
1478 if (Oif_flags.HasPipes)
1480 /* NdrPipesDone(...) */
1483 /* free the full pointer translation tables */
1484 if (pProcHeader->Oi_flags & Oi_FULL_PTR_USED)
1485 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
1487 /* free server function stack */
1488 HeapFree(GetProcessHeap(), 0, args);
1490 return S_OK;
1493 /***********************************************************************
1494 * NdrServerCall2 [RPCRT4.@]
1496 void WINAPI NdrServerCall2(PRPC_MESSAGE pRpcMsg)
1498 DWORD dwPhase;
1499 NdrStubCall2(NULL, NULL, pRpcMsg, &dwPhase);
1502 /***********************************************************************
1503 * NdrStubCall [RPCRT4.@]
1505 LONG WINAPI NdrStubCall( struct IRpcStubBuffer *This, struct IRpcChannelBuffer *channel,
1506 PRPC_MESSAGE msg, DWORD *phase )
1508 return NdrStubCall2( This, channel, msg, phase );
1511 /***********************************************************************
1512 * NdrServerCall [RPCRT4.@]
1514 void WINAPI NdrServerCall( PRPC_MESSAGE msg )
1516 DWORD phase;
1517 NdrStubCall( NULL, NULL, msg, &phase );
1520 struct async_call_data
1522 MIDL_STUB_MESSAGE *pStubMsg;
1523 const NDR_PROC_HEADER *pProcHeader;
1524 PFORMAT_STRING pHandleFormat;
1525 PFORMAT_STRING pParamFormat;
1526 RPC_BINDING_HANDLE hBinding;
1527 /* size of stack */
1528 unsigned short stack_size;
1529 /* number of parameters. optional for client to give it to us */
1530 unsigned int number_of_params;
1531 /* correlation cache */
1532 ULONG_PTR NdrCorrCache[256];
1535 LONG_PTR CDECL DECLSPEC_HIDDEN ndr_async_client_call( PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat,
1536 void **stack_top )
1538 /* pointer to start of stack where arguments start */
1539 PRPC_MESSAGE pRpcMsg;
1540 PMIDL_STUB_MESSAGE pStubMsg;
1541 RPC_ASYNC_STATE *pAsync;
1542 struct async_call_data *async_call_data;
1543 /* procedure number */
1544 unsigned short procedure_number;
1545 /* cache of Oif_flags from v2 procedure header */
1546 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1547 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1548 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1549 /* header for procedure string */
1550 const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1551 RPC_STATUS status;
1553 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
1555 /* Later NDR language versions probably won't be backwards compatible */
1556 if (pStubDesc->Version > 0x50002)
1558 FIXME("Incompatible stub description version: 0x%x\n", pStubDesc->Version);
1559 RpcRaiseException(RPC_X_WRONG_STUB_VERSION);
1562 async_call_data = I_RpcAllocate(sizeof(*async_call_data) + sizeof(MIDL_STUB_MESSAGE) + sizeof(RPC_MESSAGE));
1563 if (!async_call_data) RpcRaiseException(RPC_X_NO_MEMORY);
1564 async_call_data->pProcHeader = pProcHeader;
1566 async_call_data->pStubMsg = pStubMsg = (PMIDL_STUB_MESSAGE)(async_call_data + 1);
1567 pRpcMsg = (PRPC_MESSAGE)(pStubMsg + 1);
1569 if (pProcHeader->Oi_flags & Oi_HAS_RPCFLAGS)
1571 const NDR_PROC_HEADER_RPC *header_rpc = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1572 async_call_data->stack_size = header_rpc->stack_size;
1573 procedure_number = header_rpc->proc_num;
1574 pFormat += sizeof(NDR_PROC_HEADER_RPC);
1576 else
1578 async_call_data->stack_size = pProcHeader->stack_size;
1579 procedure_number = pProcHeader->proc_num;
1580 pFormat += sizeof(NDR_PROC_HEADER);
1582 TRACE("stack size: 0x%x\n", async_call_data->stack_size);
1583 TRACE("proc num: %d\n", procedure_number);
1585 /* create the full pointer translation tables, if requested */
1586 if (pProcHeader->Oi_flags & Oi_FULL_PTR_USED)
1587 pStubMsg->FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
1589 if (pProcHeader->Oi_flags & Oi_OBJECT_PROC)
1591 ERR("objects not supported\n");
1592 I_RpcFree(async_call_data);
1593 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1596 NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDesc, procedure_number);
1598 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1599 TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
1601 /* needed for conformance of top-level objects */
1602 pStubMsg->StackTop = I_RpcAllocate(async_call_data->stack_size);
1603 memcpy(pStubMsg->StackTop, stack_top, async_call_data->stack_size);
1605 pAsync = *(RPC_ASYNC_STATE **)pStubMsg->StackTop;
1606 pAsync->StubInfo = async_call_data;
1607 async_call_data->pHandleFormat = pFormat;
1609 pFormat = client_get_handle(pStubMsg, pProcHeader, async_call_data->pHandleFormat, &async_call_data->hBinding);
1610 if (!pFormat) goto done;
1612 if (is_oicf_stubdesc(pStubDesc))
1614 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
1615 (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
1617 Oif_flags = pOIFHeader->Oi2Flags;
1618 async_call_data->number_of_params = pOIFHeader->number_of_params;
1620 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1622 TRACE("Oif_flags = %s\n", debugstr_INTERPRETER_OPT_FLAGS(Oif_flags) );
1624 if (Oif_flags.HasExtensions)
1626 const NDR_PROC_HEADER_EXTS *pExtensions =
1627 (const NDR_PROC_HEADER_EXTS *)pFormat;
1628 ext_flags = pExtensions->Flags2;
1629 pFormat += pExtensions->Size;
1632 else
1634 pFormat = convert_old_args( pStubMsg, pFormat, async_call_data->stack_size,
1635 pProcHeader->Oi_flags & Oi_OBJECT_PROC,
1636 async_call_data->NdrCorrCache, sizeof(async_call_data->NdrCorrCache),
1637 &async_call_data->number_of_params );
1640 async_call_data->pParamFormat = pFormat;
1642 pStubMsg->BufferLength = 0;
1644 /* store the RPC flags away */
1645 if (pProcHeader->Oi_flags & Oi_HAS_RPCFLAGS)
1646 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1648 /* use alternate memory allocation routines */
1649 if (pProcHeader->Oi_flags & Oi_RPCSS_ALLOC_USED)
1650 NdrRpcSmSetClientToOsf(pStubMsg);
1652 if (Oif_flags.HasPipes)
1654 FIXME("pipes not supported yet\n");
1655 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1656 /* init pipes package */
1657 /* NdrPipesInitialize(...) */
1659 if (ext_flags.HasNewCorrDesc)
1661 /* initialize extra correlation package */
1662 NdrCorrelationInitialize(pStubMsg, async_call_data->NdrCorrCache, sizeof(async_call_data->NdrCorrCache), 0);
1663 if (ext_flags.Unused & 0x2) /* has range on conformance */
1664 pStubMsg->CorrDespIncrement = 12;
1667 /* order of phases:
1668 * 1. CALCSIZE - calculate the buffer size
1669 * 2. GETBUFFER - allocate the buffer
1670 * 3. MARSHAL - marshal [in] params into the buffer
1671 * 4. SENDRECEIVE - send buffer
1672 * Then in NdrpCompleteAsyncClientCall:
1673 * 1. SENDRECEIVE - receive buffer
1674 * 2. UNMARSHAL - unmarshal [out] params from buffer
1677 /* 1. CALCSIZE */
1678 TRACE( "CALCSIZE\n" );
1679 client_do_args(pStubMsg, pFormat, STUBLESS_CALCSIZE, NULL, async_call_data->number_of_params, NULL);
1681 /* 2. GETBUFFER */
1682 TRACE( "GETBUFFER\n" );
1683 if (Oif_flags.HasPipes)
1684 /* NdrGetPipeBuffer(...) */
1685 FIXME("pipes not supported yet\n");
1686 else
1688 if (pProcHeader->handle_type == FC_AUTO_HANDLE)
1689 #if 0
1690 NdrNsGetBuffer(pStubMsg, pStubMsg->BufferLength, async_call_data->hBinding);
1691 #else
1692 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
1693 #endif
1694 else
1695 NdrGetBuffer(pStubMsg, pStubMsg->BufferLength, async_call_data->hBinding);
1697 pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1698 status = I_RpcAsyncSetHandle(pRpcMsg, pAsync);
1699 if (status != RPC_S_OK)
1700 RpcRaiseException(status);
1702 /* 3. MARSHAL */
1703 TRACE( "MARSHAL\n" );
1704 client_do_args(pStubMsg, pFormat, STUBLESS_MARSHAL, NULL, async_call_data->number_of_params, NULL);
1706 /* 4. SENDRECEIVE */
1707 TRACE( "SEND\n" );
1708 pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1709 /* send the [in] params only */
1710 if (Oif_flags.HasPipes)
1711 /* NdrPipesSend(...) */
1712 FIXME("pipes not supported yet\n");
1713 else
1715 if (pProcHeader->handle_type == FC_AUTO_HANDLE)
1716 #if 0
1717 NdrNsSend(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
1718 #else
1719 FIXME("using auto handle - call NdrNsSend when it gets implemented\n");
1720 #endif
1721 else
1723 pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
1724 status = I_RpcSend(pStubMsg->RpcMsg);
1725 if (status != RPC_S_OK)
1726 RpcRaiseException(status);
1730 done:
1731 TRACE("returning 0\n");
1732 return 0;
1735 RPC_STATUS NdrpCompleteAsyncClientCall(RPC_ASYNC_STATE *pAsync, void *Reply)
1737 /* pointer to start of stack where arguments start */
1738 PMIDL_STUB_MESSAGE pStubMsg;
1739 struct async_call_data *async_call_data;
1740 /* header for procedure string */
1741 const NDR_PROC_HEADER * pProcHeader;
1742 RPC_STATUS status = RPC_S_OK;
1744 if (!pAsync->StubInfo)
1745 return RPC_S_INVALID_ASYNC_HANDLE;
1747 async_call_data = pAsync->StubInfo;
1748 pStubMsg = async_call_data->pStubMsg;
1749 pProcHeader = async_call_data->pProcHeader;
1751 /* order of phases:
1752 * 1. CALCSIZE - calculate the buffer size
1753 * 2. GETBUFFER - allocate the buffer
1754 * 3. MARSHAL - marshal [in] params into the buffer
1755 * 4. SENDRECEIVE - send buffer
1756 * Then in NdrpCompleteAsyncClientCall:
1757 * 1. SENDRECEIVE - receive buffer
1758 * 2. UNMARSHAL - unmarshal [out] params from buffer
1761 /* 1. SENDRECEIVE */
1762 TRACE( "RECEIVE\n" );
1763 pStubMsg->RpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1764 /* receive the [out] params */
1765 if (pProcHeader->handle_type == FC_AUTO_HANDLE)
1766 #if 0
1767 NdrNsReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
1768 #else
1769 FIXME("using auto handle - call NdrNsReceive when it gets implemented\n");
1770 #endif
1771 else
1773 status = I_RpcReceive(pStubMsg->RpcMsg);
1774 if (status != RPC_S_OK)
1775 goto cleanup;
1776 pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
1777 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
1778 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
1779 pStubMsg->Buffer = pStubMsg->BufferStart;
1782 /* convert strings, floating point values and endianness into our
1783 * preferred format */
1784 #if 0
1785 if ((pStubMsg->RpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1786 NdrConvert(pStubMsg, pFormat);
1787 #endif
1789 /* 2. UNMARSHAL */
1790 TRACE( "UNMARSHAL\n" );
1791 client_do_args(pStubMsg, async_call_data->pParamFormat, STUBLESS_UNMARSHAL,
1792 NULL, async_call_data->number_of_params, Reply);
1794 cleanup:
1795 if (pStubMsg->fHasNewCorrDesc)
1797 /* free extra correlation package */
1798 NdrCorrelationFree(pStubMsg);
1801 /* free the full pointer translation tables */
1802 if (pProcHeader->Oi_flags & Oi_FULL_PTR_USED)
1803 NdrFullPointerXlatFree(pStubMsg->FullPtrXlatTables);
1805 /* free marshalling buffer */
1806 NdrFreeBuffer(pStubMsg);
1807 client_free_handle(pStubMsg, pProcHeader, async_call_data->pHandleFormat, async_call_data->hBinding);
1809 I_RpcFree(pStubMsg->StackTop);
1810 I_RpcFree(async_call_data);
1812 TRACE("-- 0x%x\n", status);
1813 return status;
1816 #ifdef __x86_64__
1818 __ASM_GLOBAL_FUNC( NdrAsyncClientCall,
1819 "movq %r8,0x18(%rsp)\n\t"
1820 "movq %r9,0x20(%rsp)\n\t"
1821 "leaq 0x18(%rsp),%r8\n\t"
1822 "subq $0x28,%rsp\n\t"
1823 __ASM_CFI(".cfi_adjust_cfa_offset 0x28\n\t")
1824 "call " __ASM_NAME("ndr_async_client_call") "\n\t"
1825 "addq $0x28,%rsp\n\t"
1826 __ASM_CFI(".cfi_adjust_cfa_offset -0x28\n\t")
1827 "ret" );
1829 #else /* __x86_64__ */
1831 /***********************************************************************
1832 * NdrAsyncClientCall [RPCRT4.@]
1834 CLIENT_CALL_RETURN WINAPIV NdrAsyncClientCall( PMIDL_STUB_DESC desc, PFORMAT_STRING format, ... )
1836 __ms_va_list args;
1837 LONG_PTR ret;
1839 __ms_va_start( args, format );
1840 ret = ndr_async_client_call( desc, format, va_arg( args, void ** ));
1841 __ms_va_end( args );
1842 return *(CLIENT_CALL_RETURN *)&ret;
1845 #endif /* __x86_64__ */
1847 RPCRTAPI LONG RPC_ENTRY NdrAsyncStubCall(struct IRpcStubBuffer* pThis,
1848 struct IRpcChannelBuffer* pChannel, PRPC_MESSAGE pRpcMsg,
1849 DWORD * pdwStubPhase)
1851 FIXME("unimplemented, expect crash!\n");
1852 return 0;
1855 void RPC_ENTRY NdrAsyncServerCall(PRPC_MESSAGE pRpcMsg)
1857 FIXME("unimplemented, %p\n", pRpcMsg);