krnl386: Explicitly fetch the service argument in VxDCall.
[wine.git] / dlls / rpcrt4 / ndr_stubless.c
blobf65eaf591788debfe15d6ca8fca6ebd4dfc67270
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"
43 #include "wine/rpcfc.h"
45 #include "cpsf.h"
46 #include "ndr_misc.h"
47 #include "ndr_stubless.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
51 #define NDR_TABLE_MASK 127
53 static inline BOOL is_oicf_stubdesc(const PMIDL_STUB_DESC pStubDesc)
55 return pStubDesc->Version >= 0x20000;
58 static inline void call_buffer_sizer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
59 const NDR_PARAM_OIF *param)
61 PFORMAT_STRING pFormat;
62 NDR_BUFFERSIZE m;
64 if (param->attr.IsBasetype)
66 pFormat = &param->u.type_format_char;
67 if (param->attr.IsSimpleRef) pMemory = *(unsigned char **)pMemory;
69 else
71 pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
72 if (!param->attr.IsByValue) pMemory = *(unsigned char **)pMemory;
75 m = NdrBufferSizer[pFormat[0] & NDR_TABLE_MASK];
76 if (m) m(pStubMsg, pMemory, pFormat);
77 else
79 FIXME("format type 0x%x not implemented\n", pFormat[0]);
80 RpcRaiseException(RPC_X_BAD_STUB_DATA);
84 static inline unsigned char *call_marshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
85 const NDR_PARAM_OIF *param)
87 PFORMAT_STRING pFormat;
88 NDR_MARSHALL m;
90 if (param->attr.IsBasetype)
92 pFormat = &param->u.type_format_char;
93 if (param->attr.IsSimpleRef) pMemory = *(unsigned char **)pMemory;
95 else
97 pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
98 if (!param->attr.IsByValue) pMemory = *(unsigned char **)pMemory;
101 m = NdrMarshaller[pFormat[0] & NDR_TABLE_MASK];
102 if (m) return m(pStubMsg, pMemory, pFormat);
103 else
105 FIXME("format type 0x%x not implemented\n", pFormat[0]);
106 RpcRaiseException(RPC_X_BAD_STUB_DATA);
107 return NULL;
111 static inline unsigned char *call_unmarshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char **ppMemory,
112 const NDR_PARAM_OIF *param, unsigned char fMustAlloc)
114 PFORMAT_STRING pFormat;
115 NDR_UNMARSHALL m;
117 if (param->attr.IsBasetype)
119 pFormat = &param->u.type_format_char;
120 if (param->attr.IsSimpleRef) ppMemory = (unsigned char **)*ppMemory;
122 else
124 pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
125 if (!param->attr.IsByValue) ppMemory = (unsigned char **)*ppMemory;
128 m = NdrUnmarshaller[pFormat[0] & NDR_TABLE_MASK];
129 if (m) return m(pStubMsg, ppMemory, pFormat, fMustAlloc);
130 else
132 FIXME("format type 0x%x not implemented\n", pFormat[0]);
133 RpcRaiseException(RPC_X_BAD_STUB_DATA);
134 return NULL;
138 static inline void call_freer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
139 const NDR_PARAM_OIF *param)
141 PFORMAT_STRING pFormat;
142 NDR_FREE m;
144 if (param->attr.IsBasetype) return; /* nothing to do */
145 pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
146 if (!param->attr.IsByValue) pMemory = *(unsigned char **)pMemory;
148 m = NdrFreer[pFormat[0] & NDR_TABLE_MASK];
149 if (m) m(pStubMsg, pMemory, pFormat);
152 static DWORD calc_arg_size(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat)
154 DWORD size;
155 switch(*pFormat)
157 case RPC_FC_RP:
158 if (pFormat[1] & RPC_FC_P_SIMPLEPOINTER)
160 FIXME("Simple reference pointer (type %#x).\n", pFormat[2]);
161 size = sizeof(void *);
162 break;
164 size = calc_arg_size(pStubMsg, &pFormat[2] + *(const SHORT*)&pFormat[2]);
165 break;
166 case RPC_FC_STRUCT:
167 case RPC_FC_PSTRUCT:
168 size = *(const WORD*)(pFormat + 2);
169 break;
170 case RPC_FC_BOGUS_STRUCT:
171 size = *(const WORD*)(pFormat + 2);
172 if(*(const WORD*)(pFormat + 4))
173 FIXME("Unhandled conformant description\n");
174 break;
175 case RPC_FC_CARRAY:
176 case RPC_FC_CVARRAY:
177 size = *(const WORD*)(pFormat + 2);
178 ComputeConformance(pStubMsg, NULL, pFormat + 4, 0);
179 size *= pStubMsg->MaxCount;
180 break;
181 case RPC_FC_SMFARRAY:
182 case RPC_FC_SMVARRAY:
183 size = *(const WORD*)(pFormat + 2);
184 break;
185 case RPC_FC_LGFARRAY:
186 case RPC_FC_LGVARRAY:
187 size = *(const DWORD*)(pFormat + 2);
188 break;
189 case RPC_FC_BOGUS_ARRAY:
190 pFormat = ComputeConformance(pStubMsg, NULL, pFormat + 4, *(const WORD*)&pFormat[2]);
191 TRACE("conformance = %ld\n", pStubMsg->MaxCount);
192 pFormat = ComputeVariance(pStubMsg, NULL, pFormat, pStubMsg->MaxCount);
193 size = ComplexStructSize(pStubMsg, pFormat);
194 size *= pStubMsg->MaxCount;
195 break;
196 case RPC_FC_USER_MARSHAL:
197 size = *(const WORD*)(pFormat + 4);
198 break;
199 case RPC_FC_CSTRING:
200 size = *(const WORD*)(pFormat + 2);
201 break;
202 case RPC_FC_WSTRING:
203 size = *(const WORD*)(pFormat + 2) * sizeof(WCHAR);
204 break;
205 case RPC_FC_C_CSTRING:
206 case RPC_FC_C_WSTRING:
207 if (*pFormat == RPC_FC_C_CSTRING)
208 size = sizeof(CHAR);
209 else
210 size = sizeof(WCHAR);
211 if (pFormat[1] == RPC_FC_STRING_SIZED)
212 ComputeConformance(pStubMsg, NULL, pFormat + 2, 0);
213 else
214 pStubMsg->MaxCount = 0;
215 size *= pStubMsg->MaxCount;
216 break;
217 default:
218 FIXME("Unhandled type %02x\n", *pFormat);
219 /* fallthrough */
220 case RPC_FC_IP:
221 size = sizeof(void *);
222 break;
224 return size;
227 void WINAPI NdrRpcSmSetClientToOsf(PMIDL_STUB_MESSAGE pMessage)
229 #if 0 /* these functions are not defined yet */
230 pMessage->pfnAllocate = NdrRpcSmClientAllocate;
231 pMessage->pfnFree = NdrRpcSmClientFree;
232 #endif
235 static const char *debugstr_PROC_PF(PARAM_ATTRIBUTES param_attributes)
237 char buffer[160];
239 buffer[0] = 0;
240 if (param_attributes.MustSize) strcat(buffer, " MustSize");
241 if (param_attributes.MustFree) strcat(buffer, " MustFree");
242 if (param_attributes.IsPipe) strcat(buffer, " IsPipe");
243 if (param_attributes.IsIn) strcat(buffer, " IsIn");
244 if (param_attributes.IsOut) strcat(buffer, " IsOut");
245 if (param_attributes.IsReturn) strcat(buffer, " IsReturn");
246 if (param_attributes.IsBasetype) strcat(buffer, " IsBasetype");
247 if (param_attributes.IsByValue) strcat(buffer, " IsByValue");
248 if (param_attributes.IsSimpleRef) strcat(buffer, " IsSimpleRef");
249 if (param_attributes.IsDontCallFreeInst) strcat(buffer, " IsDontCallFreeInst");
250 if (param_attributes.SaveForAsyncFinish) strcat(buffer, " SaveForAsyncFinish");
251 if (param_attributes.ServerAllocSize)
252 sprintf( buffer + strlen(buffer), " ServerAllocSize = %d", param_attributes.ServerAllocSize * 8);
253 return buffer[0] ? wine_dbg_sprintf( "%s", buffer + 1 ) : "";
256 static const char *debugstr_INTERPRETER_OPT_FLAGS(INTERPRETER_OPT_FLAGS Oi2Flags)
258 char buffer[160];
260 buffer[0] = 0;
261 if (Oi2Flags.ServerMustSize) strcat(buffer, " ServerMustSize");
262 if (Oi2Flags.ClientMustSize) strcat(buffer, " ClientMustSize");
263 if (Oi2Flags.HasReturn) strcat(buffer, " HasReturn");
264 if (Oi2Flags.HasPipes) strcat(buffer, " HasPipes");
265 if (Oi2Flags.Unused) strcat(buffer, " Unused");
266 if (Oi2Flags.HasAsyncUuid) strcat(buffer, " HasAsyncUuid");
267 if (Oi2Flags.HasExtensions) strcat(buffer, " HasExtensions");
268 if (Oi2Flags.HasAsyncHandle) strcat(buffer, " HasAsyncHandle");
269 return buffer[0] ? wine_dbg_sprintf( "%s", buffer + 1 ) : "";
272 #define ARG_FROM_OFFSET(args, offset) ((args) + (offset))
274 static PFORMAT_STRING client_get_handle(
275 PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
276 PFORMAT_STRING pFormat, handle_t *phBinding)
278 /* binding */
279 switch (pProcHeader->handle_type)
281 /* explicit binding: parse additional section */
282 case RPC_FC_BIND_EXPLICIT:
283 switch (*pFormat) /* handle_type */
285 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
287 const NDR_EHD_PRIMITIVE *pDesc = (const NDR_EHD_PRIMITIVE *)pFormat;
289 TRACE("Explicit primitive handle @ %d\n", pDesc->offset);
291 if (pDesc->flag) /* pointer to binding */
292 *phBinding = **(handle_t **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
293 else
294 *phBinding = *(handle_t *)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
295 return pFormat + sizeof(NDR_EHD_PRIMITIVE);
297 case RPC_FC_BIND_GENERIC: /* explicit generic */
299 const NDR_EHD_GENERIC *pDesc = (const NDR_EHD_GENERIC *)pFormat;
300 void *pObject = NULL;
301 void *pArg;
302 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
304 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
306 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
307 pArg = *(void **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
308 else
309 pArg = ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
310 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
311 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
312 *phBinding = pGenPair->pfnBind(pObject);
313 return pFormat + sizeof(NDR_EHD_GENERIC);
315 case RPC_FC_BIND_CONTEXT: /* explicit context */
317 const NDR_EHD_CONTEXT *pDesc = (const NDR_EHD_CONTEXT *)pFormat;
318 NDR_CCONTEXT context_handle;
319 TRACE("Explicit bind context\n");
320 if (pDesc->flags & HANDLE_PARAM_IS_VIA_PTR)
322 TRACE("\tHANDLE_PARAM_IS_VIA_PTR\n");
323 context_handle = **(NDR_CCONTEXT **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
325 else
326 context_handle = *(NDR_CCONTEXT *)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
328 if (context_handle) *phBinding = NDRCContextBinding(context_handle);
329 else if (pDesc->flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL)
331 ERR("null context handle isn't allowed\n");
332 RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);
333 return NULL;
335 /* FIXME: should we store this structure in stubMsg.pContext? */
336 return pFormat + sizeof(NDR_EHD_CONTEXT);
338 default:
339 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
340 RpcRaiseException(RPC_X_BAD_STUB_DATA);
342 break;
343 case RPC_FC_BIND_GENERIC: /* implicit generic */
344 FIXME("RPC_FC_BIND_GENERIC\n");
345 RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
346 break;
347 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
348 TRACE("Implicit primitive handle\n");
349 *phBinding = *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pPrimitiveHandle;
350 break;
351 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
352 TRACE("RPC_FC_CALLBACK_HANDLE\n");
353 /* server calls callback procedures only in response to remote call, and most recent
354 binding handle is used. Calling back to a client can potentially result in another
355 callback with different current handle. */
356 *phBinding = I_RpcGetCurrentCallHandle();
357 break;
358 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
359 /* strictly speaking, it isn't necessary to set hBinding here
360 * since it isn't actually used (hence the automatic in its name),
361 * but then why does MIDL generate a valid entry in the
362 * MIDL_STUB_DESC for it? */
363 TRACE("Implicit auto handle\n");
364 *phBinding = *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle;
365 break;
366 default:
367 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
368 RpcRaiseException(RPC_X_BAD_STUB_DATA);
370 return pFormat;
373 static void client_free_handle(
374 PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
375 PFORMAT_STRING pFormat, handle_t hBinding)
377 /* binding */
378 switch (pProcHeader->handle_type)
380 /* explicit binding: parse additional section */
381 case RPC_FC_BIND_EXPLICIT:
382 switch (*pFormat) /* handle_type */
384 case RPC_FC_BIND_GENERIC: /* explicit generic */
386 const NDR_EHD_GENERIC *pDesc = (const NDR_EHD_GENERIC *)pFormat;
387 void *pObject = NULL;
388 void *pArg;
389 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
391 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
393 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
394 pArg = *(void **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
395 else
396 pArg = ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
397 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
398 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
399 pGenPair->pfnUnbind(pObject, hBinding);
400 break;
402 case RPC_FC_BIND_CONTEXT: /* explicit context */
403 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
404 break;
405 default:
406 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
407 RpcRaiseException(RPC_X_BAD_STUB_DATA);
409 break;
410 case RPC_FC_BIND_GENERIC: /* implicit generic */
411 FIXME("RPC_FC_BIND_GENERIC\n");
412 RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
413 break;
414 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
415 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
416 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
417 break;
418 default:
419 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
420 RpcRaiseException(RPC_X_BAD_STUB_DATA);
424 void client_do_args( PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat, enum stubless_phase phase,
425 void **fpu_args, unsigned short number_of_params, unsigned char *pRetVal )
427 const NDR_PARAM_OIF *params = (const NDR_PARAM_OIF *)pFormat;
428 unsigned int i;
430 for (i = 0; i < number_of_params; i++)
432 unsigned char *pArg = pStubMsg->StackTop + params[i].stack_offset;
433 PFORMAT_STRING pTypeFormat = (PFORMAT_STRING)&pStubMsg->StubDesc->pFormatTypes[params[i].u.type_offset];
435 #ifdef __x86_64__ /* floats are passed as doubles through varargs functions */
436 float f;
438 if (params[i].attr.IsBasetype &&
439 params[i].u.type_format_char == RPC_FC_FLOAT &&
440 !params[i].attr.IsSimpleRef &&
441 !fpu_args)
443 f = *(double *)pArg;
444 pArg = (unsigned char *)&f;
446 #endif
448 TRACE("param[%d]: %p type %02x %s\n", i, pArg,
449 params[i].attr.IsBasetype ? params[i].u.type_format_char : *pTypeFormat,
450 debugstr_PROC_PF( params[i].attr ));
452 switch (phase)
454 case STUBLESS_INITOUT:
455 if (!params[i].attr.IsBasetype && params[i].attr.IsOut &&
456 !params[i].attr.IsIn && !params[i].attr.IsByValue)
458 memset( *(unsigned char **)pArg, 0, calc_arg_size( pStubMsg, pTypeFormat ));
460 break;
461 case STUBLESS_CALCSIZE:
462 if (params[i].attr.IsSimpleRef && !*(unsigned char **)pArg)
463 RpcRaiseException(RPC_X_NULL_REF_POINTER);
464 if (params[i].attr.IsIn) call_buffer_sizer(pStubMsg, pArg, &params[i]);
465 break;
466 case STUBLESS_MARSHAL:
467 if (params[i].attr.IsIn) call_marshaller(pStubMsg, pArg, &params[i]);
468 break;
469 case STUBLESS_UNMARSHAL:
470 if (params[i].attr.IsOut)
472 if (params[i].attr.IsReturn && pRetVal) pArg = pRetVal;
473 call_unmarshaller(pStubMsg, &pArg, &params[i], 0);
475 break;
476 case STUBLESS_FREE:
477 if (!params[i].attr.IsBasetype && params[i].attr.IsOut && !params[i].attr.IsByValue)
478 NdrClearOutParameters( pStubMsg, pTypeFormat, *(unsigned char **)pArg );
479 break;
480 default:
481 RpcRaiseException(RPC_S_INTERNAL_ERROR);
486 static unsigned int type_stack_size(unsigned char fc)
488 switch (fc)
490 case RPC_FC_BYTE:
491 case RPC_FC_CHAR:
492 case RPC_FC_SMALL:
493 case RPC_FC_USMALL:
494 case RPC_FC_WCHAR:
495 case RPC_FC_SHORT:
496 case RPC_FC_USHORT:
497 case RPC_FC_LONG:
498 case RPC_FC_ULONG:
499 case RPC_FC_INT3264:
500 case RPC_FC_UINT3264:
501 case RPC_FC_ENUM16:
502 case RPC_FC_ENUM32:
503 case RPC_FC_FLOAT:
504 case RPC_FC_ERROR_STATUS_T:
505 case RPC_FC_IGNORE:
506 return sizeof(void *);
507 case RPC_FC_DOUBLE:
508 return sizeof(double);
509 case RPC_FC_HYPER:
510 return sizeof(ULONGLONG);
511 default:
512 ERR("invalid base type 0x%x\n", fc);
513 RpcRaiseException(RPC_S_INTERNAL_ERROR);
517 static BOOL is_by_value( PFORMAT_STRING format )
519 switch (*format)
521 case RPC_FC_USER_MARSHAL:
522 case RPC_FC_STRUCT:
523 case RPC_FC_PSTRUCT:
524 case RPC_FC_CSTRUCT:
525 case RPC_FC_CPSTRUCT:
526 case RPC_FC_CVSTRUCT:
527 case RPC_FC_BOGUS_STRUCT:
528 return TRUE;
529 default:
530 return FALSE;
534 PFORMAT_STRING convert_old_args( PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat,
535 unsigned int stack_size, BOOL object_proc,
536 void *buffer, unsigned int size, unsigned int *count )
538 NDR_PARAM_OIF *args = buffer;
539 unsigned int i, stack_offset = object_proc ? sizeof(void *) : 0;
541 for (i = 0; stack_offset < stack_size; i++)
543 const NDR_PARAM_OI_BASETYPE *param = (const NDR_PARAM_OI_BASETYPE *)pFormat;
544 const NDR_PARAM_OI_OTHER *other = (const NDR_PARAM_OI_OTHER *)pFormat;
546 if (i + 1 > size / sizeof(*args))
548 FIXME( "%u args not supported\n", i );
549 RpcRaiseException( RPC_S_INTERNAL_ERROR );
552 args[i].stack_offset = stack_offset;
553 memset( &args[i].attr, 0, sizeof(args[i].attr) );
555 switch (param->param_direction)
557 case RPC_FC_IN_PARAM_BASETYPE:
558 args[i].attr.IsIn = 1;
559 args[i].attr.IsBasetype = 1;
560 break;
561 case RPC_FC_RETURN_PARAM_BASETYPE:
562 args[i].attr.IsOut = 1;
563 args[i].attr.IsReturn = 1;
564 args[i].attr.IsBasetype = 1;
565 break;
566 case RPC_FC_IN_PARAM:
567 args[i].attr.IsIn = 1;
568 args[i].attr.MustFree = 1;
569 break;
570 case RPC_FC_IN_PARAM_NO_FREE_INST:
571 args[i].attr.IsIn = 1;
572 args[i].attr.IsDontCallFreeInst = 1;
573 break;
574 case RPC_FC_IN_OUT_PARAM:
575 args[i].attr.IsIn = 1;
576 args[i].attr.IsOut = 1;
577 args[i].attr.MustFree = 1;
578 break;
579 case RPC_FC_OUT_PARAM:
580 args[i].attr.IsOut = 1;
581 break;
582 case RPC_FC_RETURN_PARAM:
583 args[i].attr.IsOut = 1;
584 args[i].attr.IsReturn = 1;
585 break;
587 if (args[i].attr.IsBasetype)
589 args[i].u.type_format_char = param->type_format_char;
590 stack_offset += type_stack_size( param->type_format_char );
591 pFormat += sizeof(NDR_PARAM_OI_BASETYPE);
593 else
595 args[i].u.type_offset = other->type_offset;
596 args[i].attr.IsByValue = is_by_value( &pStubMsg->StubDesc->pFormatTypes[other->type_offset] );
597 stack_offset += other->stack_size * sizeof(void *);
598 pFormat += sizeof(NDR_PARAM_OI_OTHER);
601 *count = i;
602 return (PFORMAT_STRING)args;
605 LONG_PTR CDECL DECLSPEC_HIDDEN ndr_client_call( PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat,
606 void **stack_top, void **fpu_stack )
608 /* pointer to start of stack where arguments start */
609 RPC_MESSAGE rpcMsg;
610 MIDL_STUB_MESSAGE stubMsg;
611 handle_t hBinding = NULL;
612 /* procedure number */
613 unsigned short procedure_number;
614 /* size of stack */
615 unsigned short stack_size;
616 /* number of parameters. optional for client to give it to us */
617 unsigned int number_of_params;
618 /* cache of Oif_flags from v2 procedure header */
619 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
620 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
621 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
622 /* header for procedure string */
623 const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
624 /* the value to return to the client from the remote procedure */
625 LONG_PTR RetVal = 0;
626 /* the pointer to the object when in OLE mode */
627 void * This = NULL;
628 PFORMAT_STRING pHandleFormat;
629 /* correlation cache */
630 ULONG_PTR NdrCorrCache[256];
632 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
634 TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
636 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
638 const NDR_PROC_HEADER_RPC *header_rpc = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
639 stack_size = header_rpc->stack_size;
640 procedure_number = header_rpc->proc_num;
641 pFormat += sizeof(NDR_PROC_HEADER_RPC);
643 else
645 stack_size = pProcHeader->stack_size;
646 procedure_number = pProcHeader->proc_num;
647 pFormat += sizeof(NDR_PROC_HEADER);
649 TRACE("stack size: 0x%x\n", stack_size);
650 TRACE("proc num: %d\n", procedure_number);
652 /* create the full pointer translation tables, if requested */
653 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
654 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
656 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
658 /* object is always the first argument */
659 This = stack_top[0];
660 NdrProxyInitialize(This, &rpcMsg, &stubMsg, pStubDesc, procedure_number);
662 else
663 NdrClientInitializeNew(&rpcMsg, &stubMsg, pStubDesc, procedure_number);
665 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
666 TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
668 stubMsg.StackTop = (unsigned char *)stack_top;
669 pHandleFormat = pFormat;
671 /* we only need a handle if this isn't an object method */
672 if (!(pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT))
674 pFormat = client_get_handle(&stubMsg, pProcHeader, pHandleFormat, &hBinding);
675 if (!pFormat) goto done;
678 if (is_oicf_stubdesc(pStubDesc)) /* -Oicf format */
680 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
681 (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
683 Oif_flags = pOIFHeader->Oi2Flags;
684 number_of_params = pOIFHeader->number_of_params;
686 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
688 TRACE("Oif_flags = %s\n", debugstr_INTERPRETER_OPT_FLAGS(Oif_flags) );
690 if (Oif_flags.HasExtensions)
692 const NDR_PROC_HEADER_EXTS *pExtensions = (const NDR_PROC_HEADER_EXTS *)pFormat;
693 ext_flags = pExtensions->Flags2;
694 pFormat += pExtensions->Size;
695 #ifdef __x86_64__
696 if (pExtensions->Size > sizeof(*pExtensions) && fpu_stack)
698 int i;
699 unsigned short fpu_mask = *(unsigned short *)(pExtensions + 1);
700 for (i = 0; i < 4; i++, fpu_mask >>= 2)
701 switch (fpu_mask & 3)
703 case 1: *(float *)&stack_top[i] = *(float *)&fpu_stack[i]; break;
704 case 2: *(double *)&stack_top[i] = *(double *)&fpu_stack[i]; break;
707 #endif
710 else
712 pFormat = convert_old_args( &stubMsg, pFormat, stack_size,
713 pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT,
714 /* reuse the correlation cache, it's not needed for v1 format */
715 NdrCorrCache, sizeof(NdrCorrCache), &number_of_params );
718 stubMsg.BufferLength = 0;
720 /* store the RPC flags away */
721 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
722 rpcMsg.RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
724 /* use alternate memory allocation routines */
725 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
726 NdrRpcSmSetClientToOsf(&stubMsg);
728 if (Oif_flags.HasPipes)
730 FIXME("pipes not supported yet\n");
731 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
732 /* init pipes package */
733 /* NdrPipesInitialize(...) */
735 if (ext_flags.HasNewCorrDesc)
737 /* initialize extra correlation package */
738 NdrCorrelationInitialize(&stubMsg, NdrCorrCache, sizeof(NdrCorrCache), 0);
739 if (ext_flags.Unused & 0x2) /* has range on conformance */
740 stubMsg.CorrDespIncrement = 12;
743 /* order of phases:
744 * 1. INITOUT - zero [out] parameters (proxies only)
745 * 2. CALCSIZE - calculate the buffer size
746 * 3. GETBUFFER - allocate the buffer
747 * 4. MARSHAL - marshal [in] params into the buffer
748 * 5. SENDRECEIVE - send/receive buffer
749 * 6. UNMARSHAL - unmarshal [out] params from buffer
750 * 7. FREE - clear [out] parameters (for proxies, and only on error)
752 if ((pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT) ||
753 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_HAS_COMM_OR_FAULT))
755 /* 1. INITOUT */
756 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
758 TRACE( "INITOUT\n" );
759 client_do_args(&stubMsg, pFormat, STUBLESS_INITOUT, fpu_stack,
760 number_of_params, (unsigned char *)&RetVal);
763 __TRY
765 /* 2. CALCSIZE */
766 TRACE( "CALCSIZE\n" );
767 client_do_args(&stubMsg, pFormat, STUBLESS_CALCSIZE, fpu_stack,
768 number_of_params, (unsigned char *)&RetVal);
770 /* 3. GETBUFFER */
771 TRACE( "GETBUFFER\n" );
772 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
774 /* allocate the buffer */
775 NdrProxyGetBuffer(This, &stubMsg);
777 else
779 /* allocate the buffer */
780 if (Oif_flags.HasPipes)
781 /* NdrGetPipeBuffer(...) */
782 FIXME("pipes not supported yet\n");
783 else
785 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
786 #if 0
787 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
788 #else
789 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
790 #endif
791 else
792 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
796 /* 4. MARSHAL */
797 TRACE( "MARSHAL\n" );
798 client_do_args(&stubMsg, pFormat, STUBLESS_MARSHAL, fpu_stack,
799 number_of_params, (unsigned char *)&RetVal);
801 /* 5. SENDRECEIVE */
802 TRACE( "SENDRECEIVE\n" );
803 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
805 /* send the [in] params and receive the [out] and [retval]
806 * params */
807 NdrProxySendReceive(This, &stubMsg);
809 else
811 /* send the [in] params and receive the [out] and [retval]
812 * params */
813 if (Oif_flags.HasPipes)
814 /* NdrPipesSendReceive(...) */
815 FIXME("pipes not supported yet\n");
816 else
818 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
819 #if 0
820 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
821 #else
822 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
823 #endif
824 else
825 NdrSendReceive(&stubMsg, stubMsg.Buffer);
829 /* convert strings, floating point values and endianness into our
830 * preferred format */
831 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
832 NdrConvert(&stubMsg, pFormat);
834 /* 6. UNMARSHAL */
835 TRACE( "UNMARSHAL\n" );
836 client_do_args(&stubMsg, pFormat, STUBLESS_UNMARSHAL, fpu_stack,
837 number_of_params, (unsigned char *)&RetVal);
839 __EXCEPT_ALL
841 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
843 /* 7. FREE */
844 TRACE( "FREE\n" );
845 client_do_args(&stubMsg, pFormat, STUBLESS_FREE, fpu_stack,
846 number_of_params, (unsigned char *)&RetVal);
847 RetVal = NdrProxyErrorHandler(GetExceptionCode());
849 else
851 const COMM_FAULT_OFFSETS *comm_fault_offsets = &pStubDesc->CommFaultOffsets[procedure_number];
852 ULONG *comm_status;
853 ULONG *fault_status;
855 TRACE("comm_fault_offsets = {0x%hx, 0x%hx}\n", comm_fault_offsets->CommOffset, comm_fault_offsets->FaultOffset);
857 if (comm_fault_offsets->CommOffset == -1)
858 comm_status = (ULONG *)&RetVal;
859 else if (comm_fault_offsets->CommOffset >= 0)
860 comm_status = *(ULONG **)ARG_FROM_OFFSET(stubMsg.StackTop, comm_fault_offsets->CommOffset);
861 else
862 comm_status = NULL;
864 if (comm_fault_offsets->FaultOffset == -1)
865 fault_status = (ULONG *)&RetVal;
866 else if (comm_fault_offsets->FaultOffset >= 0)
867 fault_status = *(ULONG **)ARG_FROM_OFFSET(stubMsg.StackTop, comm_fault_offsets->FaultOffset);
868 else
869 fault_status = NULL;
871 NdrMapCommAndFaultStatus(&stubMsg, comm_status, fault_status,
872 GetExceptionCode());
875 __ENDTRY
877 else
879 /* 2. CALCSIZE */
880 TRACE( "CALCSIZE\n" );
881 client_do_args(&stubMsg, pFormat, STUBLESS_CALCSIZE, fpu_stack,
882 number_of_params, (unsigned char *)&RetVal);
884 /* 3. GETBUFFER */
885 TRACE( "GETBUFFER\n" );
886 if (Oif_flags.HasPipes)
887 /* NdrGetPipeBuffer(...) */
888 FIXME("pipes not supported yet\n");
889 else
891 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
892 #if 0
893 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
894 #else
895 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
896 #endif
897 else
898 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
901 /* 4. MARSHAL */
902 TRACE( "MARSHAL\n" );
903 client_do_args(&stubMsg, pFormat, STUBLESS_MARSHAL, fpu_stack,
904 number_of_params, (unsigned char *)&RetVal);
906 /* 5. SENDRECEIVE */
907 TRACE( "SENDRECEIVE\n" );
908 if (Oif_flags.HasPipes)
909 /* NdrPipesSendReceive(...) */
910 FIXME("pipes not supported yet\n");
911 else
913 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
914 #if 0
915 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
916 #else
917 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
918 #endif
919 else
920 NdrSendReceive(&stubMsg, stubMsg.Buffer);
923 /* convert strings, floating point values and endianness into our
924 * preferred format */
925 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
926 NdrConvert(&stubMsg, pFormat);
928 /* 6. UNMARSHAL */
929 TRACE( "UNMARSHAL\n" );
930 client_do_args(&stubMsg, pFormat, STUBLESS_UNMARSHAL, fpu_stack,
931 number_of_params, (unsigned char *)&RetVal);
934 if (ext_flags.HasNewCorrDesc)
936 /* free extra correlation package */
937 NdrCorrelationFree(&stubMsg);
940 if (Oif_flags.HasPipes)
942 /* NdrPipesDone(...) */
945 /* free the full pointer translation tables */
946 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
947 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
949 /* free marshalling buffer */
950 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
951 NdrProxyFreeBuffer(This, &stubMsg);
952 else
954 NdrFreeBuffer(&stubMsg);
955 client_free_handle(&stubMsg, pProcHeader, pHandleFormat, hBinding);
958 done:
959 TRACE("RetVal = 0x%lx\n", RetVal);
960 return RetVal;
963 #ifdef __x86_64__
965 __ASM_GLOBAL_FUNC( NdrClientCall2,
966 "movq %r8,0x18(%rsp)\n\t"
967 "movq %r9,0x20(%rsp)\n\t"
968 "leaq 0x18(%rsp),%r8\n\t"
969 "xorq %r9,%r9\n\t"
970 "subq $0x28,%rsp\n\t"
971 __ASM_CFI(".cfi_adjust_cfa_offset 0x28\n\t")
972 "call " __ASM_NAME("ndr_client_call") "\n\t"
973 "addq $0x28,%rsp\n\t"
974 __ASM_CFI(".cfi_adjust_cfa_offset -0x28\n\t")
975 "ret" );
977 #else /* __x86_64__ */
979 /***********************************************************************
980 * NdrClientCall2 [RPCRT4.@]
982 CLIENT_CALL_RETURN WINAPIV NdrClientCall2( PMIDL_STUB_DESC desc, PFORMAT_STRING format, ... )
984 __ms_va_list args;
985 LONG_PTR ret;
987 __ms_va_start( args, format );
988 ret = ndr_client_call( desc, format, va_arg( args, void ** ), NULL );
989 __ms_va_end( args );
990 return *(CLIENT_CALL_RETURN *)&ret;
993 #endif /* __x86_64__ */
995 /* Calls a function with the specified arguments, restoring the stack
996 * properly afterwards as we don't know the calling convention of the
997 * function */
998 #if defined __i386__ && defined _MSC_VER
999 __declspec(naked) LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size)
1001 __asm
1003 push ebp
1004 mov ebp, esp
1005 push edi ; Save registers
1006 push esi
1007 mov eax, [ebp+16] ; Get stack size
1008 sub esp, eax ; Make room in stack for arguments
1009 and esp, 0xFFFFFFF0
1010 mov edi, esp
1011 mov ecx, eax
1012 mov esi, [ebp+12]
1013 shr ecx, 2
1015 rep movsd ; Copy dword blocks
1016 call [ebp+8] ; Call function
1017 lea esp, [ebp-8] ; Restore stack
1018 pop esi ; Restore registers
1019 pop edi
1020 pop ebp
1024 #elif defined __i386__ && defined __GNUC__
1025 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
1026 __ASM_GLOBAL_FUNC(call_server_func,
1027 "pushl %ebp\n\t"
1028 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1029 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1030 "movl %esp,%ebp\n\t"
1031 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1032 "pushl %edi\n\t" /* Save registers */
1033 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
1034 "pushl %esi\n\t"
1035 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
1036 "movl 16(%ebp), %eax\n\t" /* Get stack size */
1037 "subl %eax, %esp\n\t" /* Make room in stack for arguments */
1038 "andl $~15, %esp\n\t" /* Make sure stack has 16-byte alignment for Mac OS X */
1039 "movl %esp, %edi\n\t"
1040 "movl %eax, %ecx\n\t"
1041 "movl 12(%ebp), %esi\n\t"
1042 "shrl $2, %ecx\n\t" /* divide by 4 */
1043 "cld\n\t"
1044 "rep; movsl\n\t" /* Copy dword blocks */
1045 "call *8(%ebp)\n\t" /* Call function */
1046 "leal -8(%ebp), %esp\n\t" /* Restore stack */
1047 "popl %esi\n\t" /* Restore registers */
1048 __ASM_CFI(".cfi_same_value %esi\n\t")
1049 "popl %edi\n\t"
1050 __ASM_CFI(".cfi_same_value %edi\n\t")
1051 "popl %ebp\n\t"
1052 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1053 __ASM_CFI(".cfi_same_value %ebp\n\t")
1054 "ret" )
1055 #elif defined __x86_64__
1056 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
1057 __ASM_GLOBAL_FUNC( call_server_func,
1058 "pushq %rbp\n\t"
1059 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1060 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1061 "movq %rsp,%rbp\n\t"
1062 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
1063 "pushq %rsi\n\t"
1064 __ASM_CFI(".cfi_rel_offset %rsi,-8\n\t")
1065 "pushq %rdi\n\t"
1066 __ASM_CFI(".cfi_rel_offset %rdi,-16\n\t")
1067 "movq %rcx,%rax\n\t" /* function to call */
1068 "movq $32,%rcx\n\t" /* allocate max(32,stack_size) bytes of stack space */
1069 "cmpq %rcx,%r8\n\t"
1070 "cmovgq %r8,%rcx\n\t"
1071 "subq %rcx,%rsp\n\t"
1072 "andq $~15,%rsp\n\t"
1073 "movq %r8,%rcx\n\t"
1074 "shrq $3,%rcx\n\t"
1075 "movq %rsp,%rdi\n\t"
1076 "movq %rdx,%rsi\n\t"
1077 "rep; movsq\n\t" /* copy arguments */
1078 "movq 0(%rsp),%rcx\n\t"
1079 "movq 8(%rsp),%rdx\n\t"
1080 "movq 16(%rsp),%r8\n\t"
1081 "movq 24(%rsp),%r9\n\t"
1082 "movq 0(%rsp),%xmm0\n\t"
1083 "movq 8(%rsp),%xmm1\n\t"
1084 "movq 16(%rsp),%xmm2\n\t"
1085 "movq 24(%rsp),%xmm3\n\t"
1086 "callq *%rax\n\t"
1087 "leaq -16(%rbp),%rsp\n\t" /* restore stack */
1088 "popq %rdi\n\t"
1089 __ASM_CFI(".cfi_same_value %rdi\n\t")
1090 "popq %rsi\n\t"
1091 __ASM_CFI(".cfi_same_value %rsi\n\t")
1092 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
1093 "popq %rbp\n\t"
1094 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1095 __ASM_CFI(".cfi_same_value %rbp\n\t")
1096 "ret")
1097 #elif defined __arm__
1098 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char *args, unsigned int stack_size);
1099 __ASM_GLOBAL_FUNC( call_server_func,
1100 ".arm\n\t"
1101 "push {r4, r5, LR}\n\t"
1102 "mov r4, r0\n\t"
1103 "mov r5, SP\n\t"
1104 "lsr r3, r2, #2\n\t"
1105 "cmp r3, #0\n\t"
1106 "beq 5f\n\t"
1107 "sub SP, SP, r2\n\t"
1108 "tst r3, #1\n\t"
1109 "subeq SP, SP, #4\n\t"
1110 "1:\tsub r2, r2, #4\n\t"
1111 "ldr r0, [r1, r2]\n\t"
1112 "str r0, [SP, r2]\n\t"
1113 "cmp r2, #0\n\t"
1114 "bgt 1b\n\t"
1115 "cmp r3, #1\n\t"
1116 "bgt 2f\n\t"
1117 "pop {r0}\n\t"
1118 "b 5f\n\t"
1119 "2:\tcmp r3, #2\n\t"
1120 "bgt 3f\n\t"
1121 "pop {r0-r1}\n\t"
1122 "b 5f\n\t"
1123 "3:\tcmp r3, #3\n\t"
1124 "bgt 4f\n\t"
1125 "pop {r0-r2}\n\t"
1126 "b 5f\n\t"
1127 "4:\tpop {r0-r3}\n\t"
1128 "5:\tblx r4\n\t"
1129 "mov SP, r5\n\t"
1130 "pop {r4, r5, PC}" )
1131 #else
1132 #warning call_server_func not implemented for your architecture
1133 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned short stack_size)
1135 FIXME("Not implemented for your architecture\n");
1136 return 0;
1138 #endif
1140 static LONG_PTR *stub_do_args(MIDL_STUB_MESSAGE *pStubMsg,
1141 PFORMAT_STRING pFormat, enum stubless_phase phase,
1142 unsigned short number_of_params)
1144 const NDR_PARAM_OIF *params = (const NDR_PARAM_OIF *)pFormat;
1145 unsigned int i;
1146 LONG_PTR *retval_ptr = NULL;
1148 for (i = 0; i < number_of_params; i++)
1150 unsigned char *pArg = pStubMsg->StackTop + params[i].stack_offset;
1151 const unsigned char *pTypeFormat = &pStubMsg->StubDesc->pFormatTypes[params[i].u.type_offset];
1153 TRACE("param[%d]: %p -> %p type %02x %s\n", i,
1154 pArg, *(unsigned char **)pArg,
1155 params[i].attr.IsBasetype ? params[i].u.type_format_char : *pTypeFormat,
1156 debugstr_PROC_PF( params[i].attr ));
1158 switch (phase)
1160 case STUBLESS_MARSHAL:
1161 if (params[i].attr.IsOut || params[i].attr.IsReturn)
1162 call_marshaller(pStubMsg, pArg, &params[i]);
1163 break;
1164 case STUBLESS_MUSTFREE:
1165 if (params[i].attr.MustFree)
1167 call_freer(pStubMsg, pArg, &params[i]);
1169 break;
1170 case STUBLESS_FREE:
1171 if (params[i].attr.ServerAllocSize)
1173 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
1175 else if (params[i].attr.IsOut &&
1176 !params[i].attr.IsIn &&
1177 !params[i].attr.IsBasetype &&
1178 !params[i].attr.IsByValue)
1180 if (*pTypeFormat != RPC_FC_BIND_CONTEXT) pStubMsg->pfnFree(*(void **)pArg);
1182 break;
1183 case STUBLESS_INITOUT:
1184 if (!params[i].attr.IsIn &&
1185 params[i].attr.IsOut &&
1186 !params[i].attr.IsBasetype &&
1187 !params[i].attr.ServerAllocSize &&
1188 !params[i].attr.IsByValue)
1190 if (*pTypeFormat == RPC_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 & RPC_FC_PROC_OIF_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 RPC_FC_BIND_EXPLICIT:
1300 switch (*pFormat) /* handle_type */
1302 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
1303 pFormat += sizeof(NDR_EHD_PRIMITIVE);
1304 break;
1305 case RPC_FC_BIND_GENERIC: /* explicit generic */
1306 pFormat += sizeof(NDR_EHD_GENERIC);
1307 break;
1308 case RPC_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 RPC_FC_BIND_GENERIC: /* implicit generic */
1317 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
1318 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
1319 case RPC_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 & RPC_FC_PROC_OIF_OBJECT)
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 & RPC_FC_PROC_OIF_FULLPTR)
1333 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_SERVER);
1335 /* store the RPC flags away */
1336 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1337 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1339 /* use alternate memory allocation routines */
1340 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
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 & RPC_FC_PROC_OIF_OBJECT,
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 & RPC_FC_PROC_OIF_OBJECT)
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 & RPC_FC_PROC_OIF_OBJECT)
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 & RPC_FC_PROC_OIF_FULLPTR)
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 & RPC_FC_PROC_OIF_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 & RPC_FC_PROC_OIF_FULLPTR)
1587 pStubMsg->FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
1589 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
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 & RPC_FC_PROC_OIF_OBJECT,
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 & RPC_FC_PROC_OIF_RPCFLAGS)
1646 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1648 /* use alternate memory allocation routines */
1649 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
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 == RPC_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 == RPC_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 == RPC_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 & RPC_FC_PROC_OIF_FULLPTR)
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);