rpcrt4: Also check base types for null ref pointers.
[wine/multimedia.git] / dlls / rpcrt4 / ndr_stubless.c
blobf01977646ef37d57aa1f68639d7163fed9f015a3
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 void call_buffer_sizer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
55 NDR_BUFFERSIZE m = NdrBufferSizer[pFormat[0] & NDR_TABLE_MASK];
56 if (m) m(pStubMsg, pMemory, pFormat);
57 else
59 FIXME("format type 0x%x not implemented\n", pFormat[0]);
60 RpcRaiseException(RPC_X_BAD_STUB_DATA);
64 static inline unsigned char *call_marshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
66 NDR_MARSHALL m = NdrMarshaller[pFormat[0] & NDR_TABLE_MASK];
67 if (m) return m(pStubMsg, pMemory, pFormat);
68 else
70 FIXME("format type 0x%x not implemented\n", pFormat[0]);
71 RpcRaiseException(RPC_X_BAD_STUB_DATA);
72 return NULL;
76 static inline unsigned char *call_unmarshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char **ppMemory, PFORMAT_STRING pFormat, unsigned char fMustAlloc)
78 NDR_UNMARSHALL m = NdrUnmarshaller[pFormat[0] & NDR_TABLE_MASK];
79 if (m) return m(pStubMsg, ppMemory, pFormat, fMustAlloc);
80 else
82 FIXME("format type 0x%x not implemented\n", pFormat[0]);
83 RpcRaiseException(RPC_X_BAD_STUB_DATA);
84 return NULL;
88 static inline void call_freer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
90 NDR_FREE m = NdrFreer[pFormat[0] & NDR_TABLE_MASK];
91 if (m) m(pStubMsg, pMemory, pFormat);
94 #define STUBLESS_UNMARSHAL 1
95 #define STUBLESS_INITOUT 2
96 #define STUBLESS_CALLSERVER 3
97 #define STUBLESS_CALCSIZE 4
98 #define STUBLESS_GETBUFFER 5
99 #define STUBLESS_MARSHAL 6
100 #define STUBLESS_FREE 7
102 void WINAPI NdrRpcSmSetClientToOsf(PMIDL_STUB_MESSAGE pMessage)
104 #if 0 /* these functions are not defined yet */
105 pMessage->pfnAllocate = NdrRpcSmClientAllocate;
106 pMessage->pfnFree = NdrRpcSmClientFree;
107 #endif
110 static void dump_RPC_FC_PROC_PF(PARAM_ATTRIBUTES param_attributes)
112 if (param_attributes.MustSize) TRACE(" MustSize");
113 if (param_attributes.MustFree) TRACE(" MustFree");
114 if (param_attributes.IsPipe) TRACE(" IsPipe");
115 if (param_attributes.IsIn) TRACE(" IsIn");
116 if (param_attributes.IsOut) TRACE(" IsOut");
117 if (param_attributes.IsReturn) TRACE(" IsReturn");
118 if (param_attributes.IsBasetype) TRACE(" IsBasetype");
119 if (param_attributes.IsByValue) TRACE(" IsByValue");
120 if (param_attributes.IsSimpleRef) TRACE(" IsSimpleRef");
121 if (param_attributes.IsDontCallFreeInst) TRACE(" IsDontCallFreeInst");
122 if (param_attributes.SaveForAsyncFinish) TRACE(" SaveForAsyncFinish");
123 if (param_attributes.ServerAllocSize) TRACE(" ServerAllocSize = %d", param_attributes.ServerAllocSize * 8);
126 static void dump_INTERPRETER_OPT_FLAGS(INTERPRETER_OPT_FLAGS Oi2Flags)
128 if (Oi2Flags.ServerMustSize) TRACE(" ServerMustSize");
129 if (Oi2Flags.ClientMustSize) TRACE(" ClientMustSize");
130 if (Oi2Flags.HasReturn) TRACE(" HasReturn");
131 if (Oi2Flags.HasPipes) TRACE(" HasPipes");
132 if (Oi2Flags.Unused) TRACE(" Unused");
133 if (Oi2Flags.HasAsyncUuid) TRACE(" HasAsyncUuid");
134 if (Oi2Flags.HasExtensions) TRACE(" HasExtensions");
135 if (Oi2Flags.HasAsyncHandle) TRACE(" HasAsyncHandle");
136 TRACE("\n");
139 #define ARG_FROM_OFFSET(args, offset) ((args) + (offset))
141 static PFORMAT_STRING client_get_handle(
142 PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
143 PFORMAT_STRING pFormat, handle_t *phBinding)
145 /* binding */
146 switch (pProcHeader->handle_type)
148 /* explicit binding: parse additional section */
149 case RPC_FC_BIND_EXPLICIT:
150 switch (*pFormat) /* handle_type */
152 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
154 const NDR_EHD_PRIMITIVE *pDesc = (const NDR_EHD_PRIMITIVE *)pFormat;
156 TRACE("Explicit primitive handle @ %d\n", pDesc->offset);
158 if (pDesc->flag) /* pointer to binding */
159 *phBinding = **(handle_t **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
160 else
161 *phBinding = *(handle_t *)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
162 return pFormat + sizeof(NDR_EHD_PRIMITIVE);
164 case RPC_FC_BIND_GENERIC: /* explicit generic */
166 const NDR_EHD_GENERIC *pDesc = (const NDR_EHD_GENERIC *)pFormat;
167 void *pObject = NULL;
168 void *pArg;
169 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
171 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
173 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
174 pArg = *(void **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
175 else
176 pArg = ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
177 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
178 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
179 *phBinding = pGenPair->pfnBind(pObject);
180 return pFormat + sizeof(NDR_EHD_GENERIC);
182 case RPC_FC_BIND_CONTEXT: /* explicit context */
184 const NDR_EHD_CONTEXT *pDesc = (const NDR_EHD_CONTEXT *)pFormat;
185 NDR_CCONTEXT context_handle;
186 TRACE("Explicit bind context\n");
187 if (pDesc->flags & HANDLE_PARAM_IS_VIA_PTR)
189 TRACE("\tHANDLE_PARAM_IS_VIA_PTR\n");
190 context_handle = **(NDR_CCONTEXT **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
192 else
193 context_handle = *(NDR_CCONTEXT *)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
195 if (context_handle) *phBinding = NDRCContextBinding(context_handle);
196 else if (pDesc->flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL)
198 ERR("null context handle isn't allowed\n");
199 RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);
200 return NULL;
202 /* FIXME: should we store this structure in stubMsg.pContext? */
203 return pFormat + sizeof(NDR_EHD_CONTEXT);
205 default:
206 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
207 RpcRaiseException(RPC_X_BAD_STUB_DATA);
209 break;
210 case RPC_FC_BIND_GENERIC: /* implicit generic */
211 FIXME("RPC_FC_BIND_GENERIC\n");
212 RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
213 break;
214 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
215 TRACE("Implicit primitive handle\n");
216 *phBinding = *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pPrimitiveHandle;
217 break;
218 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
219 FIXME("RPC_FC_CALLBACK_HANDLE\n");
220 break;
221 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
222 /* strictly speaking, it isn't necessary to set hBinding here
223 * since it isn't actually used (hence the automatic in its name),
224 * but then why does MIDL generate a valid entry in the
225 * MIDL_STUB_DESC for it? */
226 TRACE("Implicit auto handle\n");
227 *phBinding = *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle;
228 break;
229 default:
230 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
231 RpcRaiseException(RPC_X_BAD_STUB_DATA);
233 return pFormat;
236 static void client_free_handle(
237 PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
238 PFORMAT_STRING pFormat, handle_t hBinding)
240 /* binding */
241 switch (pProcHeader->handle_type)
243 /* explicit binding: parse additional section */
244 case RPC_FC_BIND_EXPLICIT:
245 switch (*pFormat) /* handle_type */
247 case RPC_FC_BIND_GENERIC: /* explicit generic */
249 const NDR_EHD_GENERIC *pDesc = (const NDR_EHD_GENERIC *)pFormat;
250 void *pObject = NULL;
251 void *pArg;
252 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
254 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
256 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
257 pArg = *(void **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
258 else
259 pArg = ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
260 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
261 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
262 pGenPair->pfnUnbind(pObject, hBinding);
263 break;
265 case RPC_FC_BIND_CONTEXT: /* explicit context */
266 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
267 break;
268 default:
269 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
270 RpcRaiseException(RPC_X_BAD_STUB_DATA);
272 break;
273 case RPC_FC_BIND_GENERIC: /* implicit generic */
274 FIXME("RPC_FC_BIND_GENERIC\n");
275 RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
276 break;
277 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
278 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
279 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
280 break;
281 default:
282 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
283 RpcRaiseException(RPC_X_BAD_STUB_DATA);
287 static void client_do_args(PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat,
288 int phase, unsigned char *args, unsigned short number_of_params,
289 unsigned char *pRetVal)
291 /* current format string offset */
292 int current_offset = 0;
293 /* current stack offset */
294 unsigned short current_stack_offset = 0;
295 /* counter */
296 unsigned short i;
298 for (i = 0; i < number_of_params; i++)
300 const NDR_PARAM_OIF_BASETYPE *pParam =
301 (const NDR_PARAM_OIF_BASETYPE *)&pFormat[current_offset];
302 unsigned char * pArg;
304 current_stack_offset = pParam->stack_offset;
305 pArg = ARG_FROM_OFFSET(args, current_stack_offset);
307 TRACE("param[%d]: new format\n", i);
308 TRACE("\tparam_attributes:"); dump_RPC_FC_PROC_PF(pParam->param_attributes); TRACE("\n");
309 TRACE("\tstack_offset: 0x%x\n", current_stack_offset);
310 TRACE("\tmemory addr (before): %p\n", pArg);
312 if (pParam->param_attributes.IsBasetype)
314 const unsigned char * pTypeFormat =
315 &pParam->type_format_char;
317 if (pParam->param_attributes.IsSimpleRef)
319 pArg = *(unsigned char **)pArg;
320 if (!pArg) RpcRaiseException(RPC_X_NULL_REF_POINTER);
323 TRACE("\tbase type: 0x%02x\n", *pTypeFormat);
325 switch (phase)
327 case PROXY_CALCSIZE:
328 if (pParam->param_attributes.IsIn)
329 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
330 break;
331 case PROXY_MARSHAL:
332 if (pParam->param_attributes.IsIn)
333 call_marshaller(pStubMsg, pArg, pTypeFormat);
334 break;
335 case PROXY_UNMARSHAL:
336 if (pParam->param_attributes.IsOut)
338 if (pParam->param_attributes.IsReturn) pArg = pRetVal;
339 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
341 break;
342 default:
343 RpcRaiseException(RPC_S_INTERNAL_ERROR);
346 current_offset += sizeof(NDR_PARAM_OIF_BASETYPE);
348 else
350 const NDR_PARAM_OIF_OTHER *pParamOther =
351 (const NDR_PARAM_OIF_OTHER *)&pFormat[current_offset];
353 const unsigned char * pTypeFormat =
354 &(pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset]);
356 /* if a simple ref pointer then we have to do the
357 * check for the pointer being non-NULL. */
358 if (pParam->param_attributes.IsSimpleRef)
360 if (!*(unsigned char **)pArg)
361 RpcRaiseException(RPC_X_NULL_REF_POINTER);
364 TRACE("\tcomplex type: 0x%02x\n", *pTypeFormat);
366 switch (phase)
368 case PROXY_CALCSIZE:
369 if (pParam->param_attributes.IsIn)
371 if (pParam->param_attributes.IsByValue)
372 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
373 else
374 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
376 break;
377 case PROXY_MARSHAL:
378 if (pParam->param_attributes.IsIn)
380 if (pParam->param_attributes.IsByValue)
381 call_marshaller(pStubMsg, pArg, pTypeFormat);
382 else
383 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
385 break;
386 case PROXY_UNMARSHAL:
387 if (pParam->param_attributes.IsOut)
389 if (pParam->param_attributes.IsReturn) pArg = pRetVal;
390 if (pParam->param_attributes.IsByValue)
391 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
392 else
393 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
395 break;
396 default:
397 RpcRaiseException(RPC_S_INTERNAL_ERROR);
400 current_offset += sizeof(NDR_PARAM_OIF_OTHER);
402 TRACE("\tmemory addr (after): %p\n", pArg);
406 static unsigned int type_stack_size(unsigned char fc)
408 switch (fc)
410 case RPC_FC_BYTE:
411 case RPC_FC_CHAR:
412 case RPC_FC_SMALL:
413 case RPC_FC_USMALL:
414 case RPC_FC_WCHAR:
415 case RPC_FC_SHORT:
416 case RPC_FC_USHORT:
417 case RPC_FC_LONG:
418 case RPC_FC_ULONG:
419 case RPC_FC_INT3264:
420 case RPC_FC_UINT3264:
421 case RPC_FC_ENUM16:
422 case RPC_FC_ENUM32:
423 case RPC_FC_FLOAT:
424 case RPC_FC_ERROR_STATUS_T:
425 case RPC_FC_IGNORE:
426 return sizeof(void *);
427 case RPC_FC_DOUBLE:
428 return sizeof(double);
429 case RPC_FC_HYPER:
430 return sizeof(ULONGLONG);
431 default:
432 ERR("invalid base type 0x%x\n", fc);
433 RpcRaiseException(RPC_S_INTERNAL_ERROR);
437 static BOOL is_by_value( PFORMAT_STRING format )
439 switch (*format)
441 case RPC_FC_USER_MARSHAL:
442 case RPC_FC_STRUCT:
443 case RPC_FC_PSTRUCT:
444 case RPC_FC_CSTRUCT:
445 case RPC_FC_CPSTRUCT:
446 case RPC_FC_CVSTRUCT:
447 case RPC_FC_BOGUS_STRUCT:
448 return TRUE;
449 default:
450 return FALSE;
454 void client_do_args_old_format(PMIDL_STUB_MESSAGE pStubMsg,
455 PFORMAT_STRING pFormat, int phase, unsigned char *args,
456 unsigned short stack_size,
457 unsigned char *pRetVal, BOOL object_proc, BOOL ignore_retval)
459 /* current format string offset */
460 int current_offset = 0;
461 /* current stack offset */
462 unsigned short current_stack_offset = object_proc ? sizeof(void *) : 0;
463 /* counter */
464 unsigned short i;
466 /* NOTE: V1 style format doesn't terminate on the number_of_params
467 * condition as it doesn't have this attribute. Instead it
468 * terminates when the stack size given in the header is exceeded.
470 for (i = 0; TRUE; i++)
472 const NDR_PARAM_OI_BASETYPE *pParam =
473 (const NDR_PARAM_OI_BASETYPE *)&pFormat[current_offset];
474 unsigned char * pArg = ARG_FROM_OFFSET(args, current_stack_offset);
476 /* no more parameters; exit loop */
477 if (current_stack_offset >= stack_size)
478 break;
480 TRACE("param[%d]: old format\n", i);
481 TRACE("\tparam_direction: 0x%x\n", pParam->param_direction);
482 TRACE("\tstack_offset: 0x%x\n", current_stack_offset);
483 TRACE("\tmemory addr (before): %p\n", pArg);
485 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE ||
486 pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
488 const unsigned char * pTypeFormat =
489 &pParam->type_format_char;
491 TRACE("\tbase type 0x%02x\n", *pTypeFormat);
493 switch (phase)
495 case PROXY_CALCSIZE:
496 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
497 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
498 break;
499 case PROXY_MARSHAL:
500 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
501 call_marshaller(pStubMsg, pArg, pTypeFormat);
502 break;
503 case PROXY_UNMARSHAL:
504 if (!ignore_retval &&
505 pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
507 if (pParam->param_direction & RPC_FC_RETURN_PARAM)
508 call_unmarshaller(pStubMsg, &pRetVal, pTypeFormat, 0);
509 else
510 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
512 break;
513 default:
514 RpcRaiseException(RPC_S_INTERNAL_ERROR);
517 current_stack_offset += type_stack_size(*pTypeFormat);
518 current_offset += sizeof(NDR_PARAM_OI_BASETYPE);
520 else
522 const NDR_PARAM_OI_OTHER *pParamOther = (const NDR_PARAM_OI_OTHER *)&pFormat[current_offset];
523 const unsigned char *pTypeFormat = &pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset];
524 const BOOL by_value = is_by_value( pTypeFormat );
526 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
528 switch (phase)
530 case PROXY_CALCSIZE:
531 if (pParam->param_direction == RPC_FC_IN_PARAM ||
532 pParam->param_direction == RPC_FC_IN_OUT_PARAM)
534 if (!by_value) pArg = *(unsigned char **)pArg;
535 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
537 break;
538 case PROXY_MARSHAL:
539 if (pParam->param_direction == RPC_FC_IN_PARAM ||
540 pParam->param_direction == RPC_FC_IN_OUT_PARAM)
542 if (!by_value) pArg = *(unsigned char **)pArg;
543 call_marshaller(pStubMsg, pArg, pTypeFormat);
545 break;
546 case PROXY_UNMARSHAL:
547 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
548 pParam->param_direction == RPC_FC_OUT_PARAM)
550 if (by_value)
551 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
552 else
553 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
555 else if (pParam->param_direction == RPC_FC_RETURN_PARAM)
556 call_unmarshaller(pStubMsg, (unsigned char **)pRetVal, pTypeFormat, 0);
557 break;
558 default:
559 RpcRaiseException(RPC_S_INTERNAL_ERROR);
562 current_stack_offset += pParamOther->stack_size * sizeof(void *);
563 current_offset += sizeof(NDR_PARAM_OI_OTHER);
568 CLIENT_CALL_RETURN WINAPIV NdrClientCall2(PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat, ...)
570 /* pointer to start of stack where arguments start */
571 RPC_MESSAGE rpcMsg;
572 MIDL_STUB_MESSAGE stubMsg;
573 handle_t hBinding = NULL;
574 /* procedure number */
575 unsigned short procedure_number;
576 /* size of stack */
577 unsigned short stack_size;
578 /* number of parameters. optional for client to give it to us */
579 unsigned char number_of_params = ~0;
580 /* cache of Oif_flags from v2 procedure header */
581 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
582 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
583 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
584 /* the type of pass we are currently doing */
585 int phase;
586 /* header for procedure string */
587 const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
588 /* -Oif or -Oicf generated format */
589 BOOL bV2Format = FALSE;
590 /* the value to return to the client from the remote procedure */
591 LONG_PTR RetVal = 0;
592 /* the pointer to the object when in OLE mode */
593 void * This = NULL;
594 PFORMAT_STRING pHandleFormat;
595 /* correlation cache */
596 ULONG_PTR NdrCorrCache[256];
597 __ms_va_list args;
599 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
601 TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
603 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
605 const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
606 stack_size = pProcHeader->stack_size;
607 procedure_number = pProcHeader->proc_num;
608 pFormat += sizeof(NDR_PROC_HEADER_RPC);
610 else
612 stack_size = pProcHeader->stack_size;
613 procedure_number = pProcHeader->proc_num;
614 pFormat += sizeof(NDR_PROC_HEADER);
616 TRACE("stack size: 0x%x\n", stack_size);
617 TRACE("proc num: %d\n", procedure_number);
619 /* create the full pointer translation tables, if requested */
620 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
621 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
623 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
625 /* object is always the first argument */
626 This = **(void *const **)(&pFormat+1);
627 NdrProxyInitialize(This, &rpcMsg, &stubMsg, pStubDesc, procedure_number);
629 else
630 NdrClientInitializeNew(&rpcMsg, &stubMsg, pStubDesc, procedure_number);
632 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
633 TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
635 /* needed for conformance of top-level objects */
636 __ms_va_start( args, pFormat );
637 stubMsg.StackTop = va_arg( args, unsigned char * );
638 __ms_va_end( args );
640 pHandleFormat = pFormat;
642 /* we only need a handle if this isn't an object method */
643 if (!(pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT))
645 pFormat = client_get_handle(&stubMsg, pProcHeader, pHandleFormat, &hBinding);
646 if (!pFormat) goto done;
649 bV2Format = (pStubDesc->Version >= 0x20000);
651 if (bV2Format)
653 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
654 (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
656 Oif_flags = pOIFHeader->Oi2Flags;
657 number_of_params = pOIFHeader->number_of_params;
659 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
662 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
664 if (Oif_flags.HasExtensions)
666 const NDR_PROC_HEADER_EXTS *pExtensions =
667 (const NDR_PROC_HEADER_EXTS *)pFormat;
668 ext_flags = pExtensions->Flags2;
669 pFormat += pExtensions->Size;
672 stubMsg.BufferLength = 0;
674 /* store the RPC flags away */
675 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
676 rpcMsg.RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
678 /* use alternate memory allocation routines */
679 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
680 NdrRpcSmSetClientToOsf(&stubMsg);
682 if (Oif_flags.HasPipes)
684 FIXME("pipes not supported yet\n");
685 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
686 /* init pipes package */
687 /* NdrPipesInitialize(...) */
689 if (ext_flags.HasNewCorrDesc)
691 /* initialize extra correlation package */
692 NdrCorrelationInitialize(&stubMsg, NdrCorrCache, sizeof(NdrCorrCache), 0);
695 /* order of phases:
696 * 1. PROXY_CALCSIZE - calculate the buffer size
697 * 2. PROXY_GETBUFFER - allocate the buffer
698 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
699 * 4. PROXY_SENDRECEIVE - send/receive buffer
700 * 5. PROXY_UNMARHSAL - unmarshal [out] params from buffer
702 if ((pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT) ||
703 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_HAS_COMM_OR_FAULT))
705 __TRY
707 for (phase = PROXY_CALCSIZE; phase <= PROXY_UNMARSHAL; phase++)
709 TRACE("phase = %d\n", phase);
710 switch (phase)
712 case PROXY_GETBUFFER:
713 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
715 /* allocate the buffer */
716 NdrProxyGetBuffer(This, &stubMsg);
718 else
720 /* allocate the buffer */
721 if (Oif_flags.HasPipes)
722 /* NdrGetPipeBuffer(...) */
723 FIXME("pipes not supported yet\n");
724 else
726 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
727 #if 0
728 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
729 #else
730 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
731 #endif
732 else
733 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
736 break;
737 case PROXY_SENDRECEIVE:
738 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
740 /* send the [in] params and receive the [out] and [retval]
741 * params */
742 NdrProxySendReceive(This, &stubMsg);
744 else
746 /* send the [in] params and receive the [out] and [retval]
747 * params */
748 if (Oif_flags.HasPipes)
749 /* NdrPipesSendReceive(...) */
750 FIXME("pipes not supported yet\n");
751 else
753 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
754 #if 0
755 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
756 #else
757 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
758 #endif
759 else
760 NdrSendReceive(&stubMsg, stubMsg.Buffer);
764 /* convert strings, floating point values and endianess into our
765 * preferred format */
766 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
767 NdrConvert(&stubMsg, pFormat);
769 break;
770 case PROXY_CALCSIZE:
771 case PROXY_MARSHAL:
772 case PROXY_UNMARSHAL:
773 if (bV2Format)
774 client_do_args(&stubMsg, pFormat, phase, stubMsg.StackTop,
775 number_of_params, (unsigned char *)&RetVal);
776 else
777 client_do_args_old_format(&stubMsg, pFormat, phase,
778 stubMsg.StackTop, stack_size, (unsigned char *)&RetVal,
779 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT), FALSE);
780 break;
781 default:
782 ERR("shouldn't reach here. phase %d\n", phase);
783 break;
787 __EXCEPT_ALL
789 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
790 RetVal = NdrProxyErrorHandler(GetExceptionCode());
791 else
793 const COMM_FAULT_OFFSETS *comm_fault_offsets = &pStubDesc->CommFaultOffsets[procedure_number];
794 ULONG *comm_status;
795 ULONG *fault_status;
797 TRACE("comm_fault_offsets = {0x%hx, 0x%hx}\n", comm_fault_offsets->CommOffset, comm_fault_offsets->FaultOffset);
799 if (comm_fault_offsets->CommOffset == -1)
800 comm_status = (ULONG *)&RetVal;
801 else if (comm_fault_offsets->CommOffset >= 0)
802 comm_status = *(ULONG **)ARG_FROM_OFFSET(stubMsg.StackTop, comm_fault_offsets->CommOffset);
803 else
804 comm_status = NULL;
806 if (comm_fault_offsets->FaultOffset == -1)
807 fault_status = (ULONG *)&RetVal;
808 else if (comm_fault_offsets->FaultOffset >= 0)
809 fault_status = *(ULONG **)ARG_FROM_OFFSET(stubMsg.StackTop, comm_fault_offsets->CommOffset);
810 else
811 fault_status = NULL;
813 NdrMapCommAndFaultStatus(&stubMsg, comm_status, fault_status,
814 GetExceptionCode());
817 __ENDTRY
819 else
821 /* order of phases:
822 * 1. PROXY_CALCSIZE - calculate the buffer size
823 * 2. PROXY_GETBUFFER - allocate the buffer
824 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
825 * 4. PROXY_SENDRECEIVE - send/receive buffer
826 * 5. PROXY_UNMARHSAL - unmarshal [out] params from buffer
828 for (phase = PROXY_CALCSIZE; phase <= PROXY_UNMARSHAL; phase++)
830 TRACE("phase = %d\n", phase);
831 switch (phase)
833 case PROXY_GETBUFFER:
834 /* allocate the buffer */
835 if (Oif_flags.HasPipes)
836 /* NdrGetPipeBuffer(...) */
837 FIXME("pipes not supported yet\n");
838 else
840 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
841 #if 0
842 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
843 #else
844 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
845 #endif
846 else
847 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
849 break;
850 case PROXY_SENDRECEIVE:
851 /* send the [in] params and receive the [out] and [retval]
852 * params */
853 if (Oif_flags.HasPipes)
854 /* NdrPipesSendReceive(...) */
855 FIXME("pipes not supported yet\n");
856 else
858 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
859 #if 0
860 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
861 #else
862 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
863 #endif
864 else
865 NdrSendReceive(&stubMsg, stubMsg.Buffer);
868 /* convert strings, floating point values and endianess into our
869 * preferred format */
870 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
871 NdrConvert(&stubMsg, pFormat);
873 break;
874 case PROXY_CALCSIZE:
875 case PROXY_MARSHAL:
876 case PROXY_UNMARSHAL:
877 if (bV2Format)
878 client_do_args(&stubMsg, pFormat, phase, stubMsg.StackTop,
879 number_of_params, (unsigned char *)&RetVal);
880 else
881 client_do_args_old_format(&stubMsg, pFormat, phase,
882 stubMsg.StackTop, stack_size, (unsigned char *)&RetVal,
883 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT), FALSE);
884 break;
885 default:
886 ERR("shouldn't reach here. phase %d\n", phase);
887 break;
892 if (ext_flags.HasNewCorrDesc)
894 /* free extra correlation package */
895 NdrCorrelationFree(&stubMsg);
898 if (Oif_flags.HasPipes)
900 /* NdrPipesDone(...) */
903 /* free the full pointer translation tables */
904 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
905 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
907 /* free marshalling buffer */
908 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
909 NdrProxyFreeBuffer(This, &stubMsg);
910 else
912 NdrFreeBuffer(&stubMsg);
913 client_free_handle(&stubMsg, pProcHeader, pHandleFormat, hBinding);
916 done:
917 TRACE("RetVal = 0x%lx\n", RetVal);
918 return *(CLIENT_CALL_RETURN *)&RetVal;
921 /* Calls a function with the specified arguments, restoring the stack
922 * properly afterwards as we don't know the calling convention of the
923 * function */
924 #if defined __i386__ && defined _MSC_VER
925 __declspec(naked) LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size)
927 __asm
929 push ebp
930 push edi ; Save registers
931 push esi
932 mov ebp, esp
933 mov eax, [ebp+16] ; Get stack size
934 sub esp, eax ; Make room in stack for arguments
935 mov edi, esp
936 mov ecx, eax
937 mov esi, [ebp+12]
938 shr ecx, 2
940 rep movsd ; Copy dword blocks
941 call [ebp+8] ; Call function
942 lea esp, [ebp-8] ; Restore stack
943 pop esi ; Restore registers
944 pop edi
945 pop ebp
949 #elif defined __i386__ && defined __GNUC__
950 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
951 __ASM_GLOBAL_FUNC(call_server_func,
952 "pushl %ebp\n\t"
953 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
954 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
955 "movl %esp,%ebp\n\t"
956 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
957 "pushl %edi\n\t" /* Save registers */
958 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
959 "pushl %esi\n\t"
960 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
961 "movl 16(%ebp), %eax\n\t" /* Get stack size */
962 "subl %eax, %esp\n\t" /* Make room in stack for arguments */
963 "andl $~15, %esp\n\t" /* Make sure stack has 16-byte alignment for Mac OS X */
964 "movl %esp, %edi\n\t"
965 "movl %eax, %ecx\n\t"
966 "movl 12(%ebp), %esi\n\t"
967 "shrl $2, %ecx\n\t" /* divide by 4 */
968 "cld\n\t"
969 "rep; movsl\n\t" /* Copy dword blocks */
970 "call *8(%ebp)\n\t" /* Call function */
971 "leal -8(%ebp), %esp\n\t" /* Restore stack */
972 "popl %esi\n\t" /* Restore registers */
973 __ASM_CFI(".cfi_same_value %esi\n\t")
974 "popl %edi\n\t"
975 __ASM_CFI(".cfi_same_value %edi\n\t")
976 "popl %ebp\n\t"
977 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
978 __ASM_CFI(".cfi_same_value %ebp\n\t")
979 "ret" )
980 #else
981 #warning call_server_func not implemented for your architecture
982 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned short stack_size)
984 FIXME("Not implemented for your architecture\n");
985 return 0;
987 #endif
989 static DWORD calc_arg_size(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat)
991 DWORD size;
992 switch(*pFormat)
994 case RPC_FC_STRUCT:
995 size = *(const WORD*)(pFormat + 2);
996 break;
997 case RPC_FC_BOGUS_STRUCT:
998 size = *(const WORD*)(pFormat + 2);
999 if(*(const WORD*)(pFormat + 4))
1000 FIXME("Unhandled conformant description\n");
1001 break;
1002 case RPC_FC_CARRAY:
1003 case RPC_FC_CVARRAY:
1004 size = *(const WORD*)(pFormat + 2);
1005 ComputeConformance(pStubMsg, NULL, pFormat + 4, 0);
1006 size *= pStubMsg->MaxCount;
1007 break;
1008 case RPC_FC_SMFARRAY:
1009 case RPC_FC_SMVARRAY:
1010 size = *(const WORD*)(pFormat + 2);
1011 break;
1012 case RPC_FC_LGFARRAY:
1013 case RPC_FC_LGVARRAY:
1014 size = *(const DWORD*)(pFormat + 2);
1015 break;
1016 case RPC_FC_BOGUS_ARRAY:
1017 pFormat = ComputeConformance(pStubMsg, NULL, pFormat + 4, *(const WORD*)&pFormat[2]);
1018 TRACE("conformance = %ld\n", pStubMsg->MaxCount);
1019 pFormat = ComputeVariance(pStubMsg, NULL, pFormat, pStubMsg->MaxCount);
1020 size = ComplexStructSize(pStubMsg, pFormat);
1021 size *= pStubMsg->MaxCount;
1022 break;
1023 case RPC_FC_C_CSTRING:
1024 case RPC_FC_C_WSTRING:
1025 if (*pFormat == RPC_FC_C_CSTRING)
1026 size = sizeof(CHAR);
1027 else
1028 size = sizeof(WCHAR);
1029 if (pFormat[1] == RPC_FC_STRING_SIZED)
1030 ComputeConformance(pStubMsg, NULL, pFormat + 2, 0);
1031 else
1032 pStubMsg->MaxCount = 0;
1033 size *= pStubMsg->MaxCount;
1034 break;
1035 default:
1036 FIXME("Unhandled type %02x\n", *pFormat);
1037 /* fallthrough */
1038 case RPC_FC_RP:
1039 size = sizeof(void *);
1040 break;
1042 return size;
1045 static LONG_PTR *stub_do_args(MIDL_STUB_MESSAGE *pStubMsg,
1046 PFORMAT_STRING pFormat, int phase,
1047 unsigned char *args,
1048 unsigned short number_of_params)
1050 /* counter */
1051 unsigned short i;
1052 /* current format string offset */
1053 int current_offset = 0;
1054 /* current stack offset */
1055 unsigned short current_stack_offset = 0;
1056 /* location to put retval into */
1057 LONG_PTR *retval_ptr = NULL;
1059 for (i = 0; i < number_of_params; i++)
1061 const NDR_PARAM_OIF_BASETYPE *pParam =
1062 (const NDR_PARAM_OIF_BASETYPE *)&pFormat[current_offset];
1063 unsigned char *pArg;
1065 current_stack_offset = pParam->stack_offset;
1066 pArg = args + current_stack_offset;
1068 TRACE("param[%d]: new format\n", i);
1069 TRACE("\tparam_attributes:"); dump_RPC_FC_PROC_PF(pParam->param_attributes); TRACE("\n");
1070 TRACE("\tstack_offset: 0x%x\n", current_stack_offset);
1071 TRACE("\tmemory addr (before): %p -> %p\n", pArg, *(unsigned char **)pArg);
1073 if (pParam->param_attributes.IsBasetype)
1075 const unsigned char *pTypeFormat =
1076 &pParam->type_format_char;
1078 TRACE("\tbase type: 0x%02x\n", *pTypeFormat);
1080 switch (phase)
1082 case STUBLESS_MARSHAL:
1083 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1085 if (pParam->param_attributes.IsSimpleRef)
1086 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1087 else
1088 call_marshaller(pStubMsg, pArg, pTypeFormat);
1090 break;
1091 case STUBLESS_FREE:
1092 if (pParam->param_attributes.ServerAllocSize)
1093 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
1094 break;
1095 case STUBLESS_INITOUT:
1096 break;
1097 case STUBLESS_UNMARSHAL:
1098 if (pParam->param_attributes.ServerAllocSize)
1099 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1100 pParam->param_attributes.ServerAllocSize * 8);
1102 if (pParam->param_attributes.IsIn)
1104 if (pParam->param_attributes.IsSimpleRef)
1105 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1106 else
1107 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1109 break;
1110 case STUBLESS_CALCSIZE:
1111 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1113 if (pParam->param_attributes.IsSimpleRef)
1114 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1115 else
1116 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1118 break;
1119 default:
1120 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1123 current_offset += sizeof(NDR_PARAM_OIF_BASETYPE);
1125 else
1127 const NDR_PARAM_OIF_OTHER *pParamOther =
1128 (const NDR_PARAM_OIF_OTHER *)&pFormat[current_offset];
1130 const unsigned char * pTypeFormat =
1131 &(pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset]);
1133 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1135 switch (phase)
1137 case STUBLESS_MARSHAL:
1138 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1140 if (pParam->param_attributes.IsByValue)
1141 call_marshaller(pStubMsg, pArg, pTypeFormat);
1142 else
1143 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1145 break;
1146 case STUBLESS_FREE:
1147 if (pParam->param_attributes.MustFree)
1149 if (pParam->param_attributes.IsByValue)
1150 call_freer(pStubMsg, pArg, pTypeFormat);
1151 else
1152 call_freer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1155 if (pParam->param_attributes.IsOut &&
1156 !pParam->param_attributes.IsIn &&
1157 !pParam->param_attributes.IsByValue &&
1158 !pParam->param_attributes.ServerAllocSize)
1160 if (*pTypeFormat != RPC_FC_BIND_CONTEXT)
1161 pStubMsg->pfnFree(*(void **)pArg);
1164 if (pParam->param_attributes.ServerAllocSize)
1165 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
1166 break;
1167 case STUBLESS_INITOUT:
1168 if (!pParam->param_attributes.IsIn &&
1169 pParam->param_attributes.IsOut &&
1170 !pParam->param_attributes.ServerAllocSize &&
1171 !pParam->param_attributes.IsByValue)
1173 if (*pTypeFormat == RPC_FC_BIND_CONTEXT)
1175 NDR_SCONTEXT ctxt = NdrContextHandleInitialize(
1176 pStubMsg, pTypeFormat);
1177 *(void **)pArg = NDRSContextValue(ctxt);
1179 else
1181 DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1183 if(size)
1185 *(void **)pArg = NdrAllocate(pStubMsg, size);
1186 memset(*(void **)pArg, 0, size);
1190 break;
1191 case STUBLESS_UNMARSHAL:
1192 if (pParam->param_attributes.ServerAllocSize)
1193 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1194 pParam->param_attributes.ServerAllocSize * 8);
1196 if (pParam->param_attributes.IsIn)
1198 if (pParam->param_attributes.IsByValue)
1199 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1200 else
1201 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1203 break;
1204 case STUBLESS_CALCSIZE:
1205 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1207 if (pParam->param_attributes.IsByValue)
1208 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1209 else
1210 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1212 break;
1213 default:
1214 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1217 current_offset += sizeof(NDR_PARAM_OIF_OTHER);
1219 TRACE("\tmemory addr (after): %p -> %p\n", pArg, *(unsigned char **)pArg);
1221 /* make a note of the address of the return value parameter for later */
1222 if (pParam->param_attributes.IsReturn) retval_ptr = (LONG_PTR *)pArg;
1225 return retval_ptr;
1228 static LONG_PTR *stub_do_old_args(MIDL_STUB_MESSAGE *pStubMsg,
1229 PFORMAT_STRING pFormat, int phase,
1230 unsigned char *args,
1231 unsigned short stack_size, BOOL object)
1233 /* counter */
1234 unsigned short i;
1235 /* current format string offset */
1236 int current_offset = 0;
1237 /* current stack offset */
1238 unsigned short current_stack_offset = object ? sizeof(void *) : 0;
1239 /* location to put retval into */
1240 LONG_PTR *retval_ptr = NULL;
1242 for (i = 0; TRUE; i++)
1244 const NDR_PARAM_OI_BASETYPE *pParam =
1245 (const NDR_PARAM_OI_BASETYPE *)&pFormat[current_offset];
1246 unsigned char *pArg = args + current_stack_offset;
1248 /* no more parameters; exit loop */
1249 if (current_stack_offset >= stack_size)
1250 break;
1252 TRACE("param[%d]: old format\n", i);
1253 TRACE("\tparam_direction: 0x%x\n", pParam->param_direction);
1254 TRACE("\tstack_offset: 0x%x\n", current_stack_offset);
1256 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE ||
1257 pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1259 const unsigned char *pTypeFormat =
1260 &pParam->type_format_char;
1262 TRACE("\tbase type 0x%02x\n", *pTypeFormat);
1264 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1265 retval_ptr = (LONG_PTR *)pArg;
1267 switch (phase)
1269 case STUBLESS_MARSHAL:
1270 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1271 call_marshaller(pStubMsg, pArg, pTypeFormat);
1272 break;
1273 case STUBLESS_FREE:
1274 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1275 call_freer(pStubMsg, pArg, pTypeFormat);
1276 break;
1277 case STUBLESS_INITOUT:
1278 break;
1279 case STUBLESS_UNMARSHAL:
1280 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1281 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1282 break;
1283 case STUBLESS_CALCSIZE:
1284 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1285 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1286 break;
1287 default:
1288 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1291 current_stack_offset += type_stack_size(*pTypeFormat);
1292 current_offset += sizeof(NDR_PARAM_OI_BASETYPE);
1294 else
1296 const NDR_PARAM_OI_OTHER *pParamOther =
1297 (const NDR_PARAM_OI_OTHER *)&pFormat[current_offset];
1299 const unsigned char * pTypeFormat =
1300 &pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset];
1301 const BOOL by_value = is_by_value( pTypeFormat );
1303 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1305 if (pParam->param_direction == RPC_FC_RETURN_PARAM)
1306 retval_ptr = (LONG_PTR *)pArg;
1308 switch (phase)
1310 case STUBLESS_MARSHAL:
1311 if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1312 pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1313 pParam->param_direction == RPC_FC_RETURN_PARAM)
1315 if (!by_value) pArg = *(unsigned char **)pArg;
1316 call_marshaller(pStubMsg, pArg, pTypeFormat);
1318 break;
1319 case STUBLESS_FREE:
1320 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1321 pParam->param_direction == RPC_FC_IN_PARAM)
1323 if (!by_value) pArg = *(unsigned char **)pArg;
1324 call_freer(pStubMsg, pArg, pTypeFormat);
1326 else if (pParam->param_direction == RPC_FC_OUT_PARAM && !by_value)
1327 pStubMsg->pfnFree(*(void **)pArg);
1328 break;
1329 case STUBLESS_INITOUT:
1330 if (pParam->param_direction == RPC_FC_OUT_PARAM && !by_value)
1332 DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1334 if(size)
1336 *(void **)pArg = NdrAllocate(pStubMsg, size);
1337 memset(*(void **)pArg, 0, size);
1340 break;
1341 case STUBLESS_UNMARSHAL:
1342 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1343 pParam->param_direction == RPC_FC_IN_PARAM)
1345 if (by_value)
1346 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1347 else
1348 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1350 break;
1351 case STUBLESS_CALCSIZE:
1352 if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1353 pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1354 pParam->param_direction == RPC_FC_RETURN_PARAM)
1356 if (!by_value) pArg = *(unsigned char **)pArg;
1357 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1359 break;
1360 default:
1361 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1364 current_stack_offset += pParamOther->stack_size * sizeof(void *);
1365 current_offset += sizeof(NDR_PARAM_OI_OTHER);
1369 return retval_ptr;
1372 /***********************************************************************
1373 * NdrStubCall2 [RPCRT4.@]
1375 * Unmarshals [in] parameters, calls either a method in an object or a server
1376 * function, marshals any [out] parameters and frees any allocated data.
1378 * NOTES
1379 * Used by stubless MIDL-generated code.
1381 LONG WINAPI NdrStubCall2(
1382 struct IRpcStubBuffer * pThis,
1383 struct IRpcChannelBuffer * pChannel,
1384 PRPC_MESSAGE pRpcMsg,
1385 DWORD * pdwStubPhase)
1387 const MIDL_SERVER_INFO *pServerInfo;
1388 const MIDL_STUB_DESC *pStubDesc;
1389 PFORMAT_STRING pFormat;
1390 MIDL_STUB_MESSAGE stubMsg;
1391 /* pointer to start of stack to pass into stub implementation */
1392 unsigned char * args;
1393 /* size of stack */
1394 unsigned short stack_size;
1395 /* number of parameters. optional for client to give it to us */
1396 unsigned char number_of_params = ~0;
1397 /* cache of Oif_flags from v2 procedure header */
1398 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1399 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1400 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1401 /* the type of pass we are currently doing */
1402 int phase;
1403 /* header for procedure string */
1404 const NDR_PROC_HEADER *pProcHeader;
1405 /* offset in format string for start of params */
1406 int parameter_start_offset;
1407 /* current format string offset */
1408 int current_offset;
1409 /* -Oif or -Oicf generated format */
1410 BOOL bV2Format = FALSE;
1411 /* location to put retval into */
1412 LONG_PTR *retval_ptr = NULL;
1414 TRACE("pThis %p, pChannel %p, pRpcMsg %p, pdwStubPhase %p\n", pThis, pChannel, pRpcMsg, pdwStubPhase);
1416 if (pThis)
1417 pServerInfo = CStdStubBuffer_GetServerInfo(pThis);
1418 else
1419 pServerInfo = ((RPC_SERVER_INTERFACE *)pRpcMsg->RpcInterfaceInformation)->InterpreterInfo;
1421 pStubDesc = pServerInfo->pStubDesc;
1422 pFormat = pServerInfo->ProcString + pServerInfo->FmtStringOffset[pRpcMsg->ProcNum];
1423 pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1425 TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
1427 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1429 const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1430 stack_size = pProcHeader->stack_size;
1431 current_offset = sizeof(NDR_PROC_HEADER_RPC);
1434 else
1436 stack_size = pProcHeader->stack_size;
1437 current_offset = sizeof(NDR_PROC_HEADER);
1440 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1442 /* binding */
1443 switch (pProcHeader->handle_type)
1445 /* explicit binding: parse additional section */
1446 case RPC_FC_BIND_EXPLICIT:
1447 switch (pFormat[current_offset]) /* handle_type */
1449 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
1450 current_offset += sizeof(NDR_EHD_PRIMITIVE);
1451 break;
1452 case RPC_FC_BIND_GENERIC: /* explicit generic */
1453 current_offset += sizeof(NDR_EHD_GENERIC);
1454 break;
1455 case RPC_FC_BIND_CONTEXT: /* explicit context */
1456 current_offset += sizeof(NDR_EHD_CONTEXT);
1457 break;
1458 default:
1459 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1460 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1462 break;
1463 case RPC_FC_BIND_GENERIC: /* implicit generic */
1464 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
1465 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
1466 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
1467 break;
1468 default:
1469 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1470 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1473 bV2Format = (pStubDesc->Version >= 0x20000);
1475 if (bV2Format)
1477 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
1478 (const NDR_PROC_PARTIAL_OIF_HEADER *)&pFormat[current_offset];
1480 Oif_flags = pOIFHeader->Oi2Flags;
1481 number_of_params = pOIFHeader->number_of_params;
1483 current_offset += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1486 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
1488 if (Oif_flags.HasExtensions)
1490 const NDR_PROC_HEADER_EXTS *pExtensions =
1491 (const NDR_PROC_HEADER_EXTS *)&pFormat[current_offset];
1492 ext_flags = pExtensions->Flags2;
1493 current_offset += pExtensions->Size;
1496 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1497 NdrStubInitialize(pRpcMsg, &stubMsg, pStubDesc, pChannel);
1498 else
1499 NdrServerInitializeNew(pRpcMsg, &stubMsg, pStubDesc);
1501 /* create the full pointer translation tables, if requested */
1502 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1503 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_SERVER);
1505 /* store the RPC flags away */
1506 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1507 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1509 /* use alternate memory allocation routines */
1510 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
1511 #if 0
1512 NdrRpcSsEnableAllocate(&stubMsg);
1513 #else
1514 FIXME("Set RPCSS memory allocation routines\n");
1515 #endif
1517 if (Oif_flags.HasPipes)
1519 FIXME("pipes not supported yet\n");
1520 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1521 /* init pipes package */
1522 /* NdrPipesInitialize(...) */
1524 if (ext_flags.HasNewCorrDesc)
1526 /* initialize extra correlation package */
1527 FIXME("new correlation description not implemented\n");
1528 stubMsg.fHasNewCorrDesc = TRUE;
1531 /* convert strings, floating point values and endianess into our
1532 * preferred format */
1533 if ((pRpcMsg->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1534 NdrConvert(&stubMsg, pFormat);
1536 parameter_start_offset = current_offset;
1538 TRACE("allocating memory for stack of size %x\n", stack_size);
1540 args = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, stack_size);
1541 stubMsg.StackTop = args; /* used by conformance of top-level objects */
1543 /* add the implicit This pointer as the first arg to the function if we
1544 * are calling an object method */
1545 if (pThis)
1546 *(void **)args = ((CStdStubBuffer *)pThis)->pvServerObject;
1548 for (phase = STUBLESS_UNMARSHAL; phase <= STUBLESS_FREE; phase++)
1550 TRACE("phase = %d\n", phase);
1551 switch (phase)
1553 case STUBLESS_CALLSERVER:
1554 /* call the server function */
1555 if (pServerInfo->ThunkTable && pServerInfo->ThunkTable[pRpcMsg->ProcNum])
1556 pServerInfo->ThunkTable[pRpcMsg->ProcNum](&stubMsg);
1557 else
1559 SERVER_ROUTINE func;
1560 LONG_PTR retval;
1562 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1564 SERVER_ROUTINE *vtbl = *(SERVER_ROUTINE **)((CStdStubBuffer *)pThis)->pvServerObject;
1565 func = vtbl[pRpcMsg->ProcNum];
1567 else
1568 func = pServerInfo->DispatchTable[pRpcMsg->ProcNum];
1570 /* FIXME: what happens with return values that don't fit into a single register on x86? */
1571 retval = call_server_func(func, args, stack_size);
1573 if (retval_ptr)
1575 TRACE("stub implementation returned 0x%lx\n", retval);
1576 *retval_ptr = retval;
1578 else
1579 TRACE("void stub implementation\n");
1582 stubMsg.Buffer = NULL;
1583 stubMsg.BufferLength = 0;
1585 break;
1586 case STUBLESS_GETBUFFER:
1587 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1588 NdrStubGetBuffer(pThis, pChannel, &stubMsg);
1589 else
1591 RPC_STATUS Status;
1593 pRpcMsg->BufferLength = stubMsg.BufferLength;
1594 /* allocate buffer for [out] and [ret] params */
1595 Status = I_RpcGetBuffer(pRpcMsg);
1596 if (Status)
1597 RpcRaiseException(Status);
1598 stubMsg.Buffer = pRpcMsg->Buffer;
1600 break;
1601 case STUBLESS_UNMARSHAL:
1602 case STUBLESS_INITOUT:
1603 case STUBLESS_CALCSIZE:
1604 case STUBLESS_MARSHAL:
1605 case STUBLESS_FREE:
1606 if (bV2Format)
1607 retval_ptr = stub_do_args(&stubMsg, &pFormat[parameter_start_offset],
1608 phase, args, number_of_params);
1609 else
1610 retval_ptr = stub_do_old_args(&stubMsg, &pFormat[parameter_start_offset],
1611 phase, args, stack_size,
1612 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT));
1614 break;
1615 default:
1616 ERR("shouldn't reach here. phase %d\n", phase);
1617 break;
1621 pRpcMsg->BufferLength = (unsigned int)(stubMsg.Buffer - (unsigned char *)pRpcMsg->Buffer);
1623 if (ext_flags.HasNewCorrDesc)
1625 /* free extra correlation package */
1626 /* NdrCorrelationFree(&stubMsg); */
1629 if (Oif_flags.HasPipes)
1631 /* NdrPipesDone(...) */
1634 /* free the full pointer translation tables */
1635 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1636 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
1638 /* free server function stack */
1639 HeapFree(GetProcessHeap(), 0, args);
1641 return S_OK;
1644 /***********************************************************************
1645 * NdrServerCall2 [RPCRT4.@]
1647 void WINAPI NdrServerCall2(PRPC_MESSAGE pRpcMsg)
1649 DWORD dwPhase;
1650 NdrStubCall2(NULL, NULL, pRpcMsg, &dwPhase);
1653 /***********************************************************************
1654 * NdrClientCall [RPCRT4.@]
1656 CLIENT_CALL_RETURN WINAPIV NdrClientCall( PMIDL_STUB_DESC desc, PFORMAT_STRING format, ... )
1658 __ms_va_list args;
1659 CLIENT_CALL_RETURN ret;
1661 __ms_va_start( args, format );
1662 ret = NdrClientCall2( desc, format, va_arg( args, unsigned char * ));
1663 __ms_va_end( args );
1664 return ret;
1667 /***********************************************************************
1668 * NdrStubCall [RPCRT4.@]
1670 LONG WINAPI NdrStubCall( struct IRpcStubBuffer *This, struct IRpcChannelBuffer *channel,
1671 PRPC_MESSAGE msg, DWORD *phase )
1673 return NdrStubCall2( This, channel, msg, phase );
1676 /***********************************************************************
1677 * NdrServerCall [RPCRT4.@]
1679 void WINAPI NdrServerCall( PRPC_MESSAGE msg )
1681 DWORD phase;
1682 NdrStubCall( NULL, NULL, msg, &phase );
1685 struct async_call_data
1687 MIDL_STUB_MESSAGE *pStubMsg;
1688 const NDR_PROC_HEADER *pProcHeader;
1689 PFORMAT_STRING pHandleFormat;
1690 PFORMAT_STRING pParamFormat;
1691 RPC_BINDING_HANDLE hBinding;
1692 /* size of stack */
1693 unsigned short stack_size;
1694 /* number of parameters. optional for client to give it to us */
1695 unsigned char number_of_params;
1696 /* correlation cache */
1697 ULONG_PTR NdrCorrCache[256];
1700 CLIENT_CALL_RETURN WINAPIV NdrAsyncClientCall(PMIDL_STUB_DESC pStubDesc,
1701 PFORMAT_STRING pFormat, ...)
1703 /* pointer to start of stack where arguments start */
1704 PRPC_MESSAGE pRpcMsg;
1705 PMIDL_STUB_MESSAGE pStubMsg;
1706 RPC_ASYNC_STATE *pAsync;
1707 struct async_call_data *async_call_data;
1708 /* procedure number */
1709 unsigned short procedure_number;
1710 /* cache of Oif_flags from v2 procedure header */
1711 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1712 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1713 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1714 /* the type of pass we are currently doing */
1715 int phase;
1716 /* header for procedure string */
1717 const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1718 /* -Oif or -Oicf generated format */
1719 BOOL bV2Format = FALSE;
1720 LONG_PTR RetVal;
1721 __ms_va_list args;
1723 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
1725 /* Later NDR language versions probably won't be backwards compatible */
1726 if (pStubDesc->Version > 0x50002)
1728 FIXME("Incompatible stub description version: 0x%x\n", pStubDesc->Version);
1729 RpcRaiseException(RPC_X_WRONG_STUB_VERSION);
1732 async_call_data = I_RpcAllocate(sizeof(*async_call_data) + sizeof(MIDL_STUB_MESSAGE) + sizeof(RPC_MESSAGE));
1733 if (!async_call_data) RpcRaiseException(ERROR_OUTOFMEMORY);
1734 async_call_data->number_of_params = ~0;
1735 async_call_data->pProcHeader = pProcHeader;
1737 async_call_data->pStubMsg = pStubMsg = (PMIDL_STUB_MESSAGE)(async_call_data + 1);
1738 pRpcMsg = (PRPC_MESSAGE)(pStubMsg + 1);
1740 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1742 const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1743 async_call_data->stack_size = pProcHeader->stack_size;
1744 procedure_number = pProcHeader->proc_num;
1745 pFormat += sizeof(NDR_PROC_HEADER_RPC);
1747 else
1749 async_call_data->stack_size = pProcHeader->stack_size;
1750 procedure_number = pProcHeader->proc_num;
1751 pFormat += sizeof(NDR_PROC_HEADER);
1753 TRACE("stack size: 0x%x\n", async_call_data->stack_size);
1754 TRACE("proc num: %d\n", procedure_number);
1756 /* create the full pointer translation tables, if requested */
1757 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1758 pStubMsg->FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
1760 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1762 ERR("objects not supported\n");
1763 I_RpcFree(async_call_data);
1764 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1767 NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDesc, procedure_number);
1769 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1770 TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
1772 /* needed for conformance of top-level objects */
1773 pStubMsg->StackTop = I_RpcAllocate(async_call_data->stack_size);
1774 __ms_va_start( args, pFormat );
1775 memcpy(pStubMsg->StackTop, va_arg( args, unsigned char * ), async_call_data->stack_size);
1776 __ms_va_end( args );
1778 pAsync = *(RPC_ASYNC_STATE **)pStubMsg->StackTop;
1779 pAsync->StubInfo = async_call_data;
1780 async_call_data->pHandleFormat = pFormat;
1782 pFormat = client_get_handle(pStubMsg, pProcHeader, async_call_data->pHandleFormat, &async_call_data->hBinding);
1783 if (!pFormat) goto done;
1785 bV2Format = (pStubDesc->Version >= 0x20000);
1787 if (bV2Format)
1789 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
1790 (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
1792 Oif_flags = pOIFHeader->Oi2Flags;
1793 async_call_data->number_of_params = pOIFHeader->number_of_params;
1795 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1798 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
1800 if (Oif_flags.HasExtensions)
1802 const NDR_PROC_HEADER_EXTS *pExtensions =
1803 (const NDR_PROC_HEADER_EXTS *)pFormat;
1804 ext_flags = pExtensions->Flags2;
1805 pFormat += pExtensions->Size;
1808 async_call_data->pParamFormat = pFormat;
1810 pStubMsg->BufferLength = 0;
1812 /* store the RPC flags away */
1813 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1814 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1816 /* use alternate memory allocation routines */
1817 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
1818 NdrRpcSmSetClientToOsf(pStubMsg);
1820 if (Oif_flags.HasPipes)
1822 FIXME("pipes not supported yet\n");
1823 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1824 /* init pipes package */
1825 /* NdrPipesInitialize(...) */
1827 if (ext_flags.HasNewCorrDesc)
1829 /* initialize extra correlation package */
1830 NdrCorrelationInitialize(pStubMsg, async_call_data->NdrCorrCache, sizeof(async_call_data->NdrCorrCache), 0);
1833 /* order of phases:
1834 * 1. PROXY_CALCSIZE - calculate the buffer size
1835 * 2. PROXY_GETBUFFER - allocate the buffer
1836 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
1837 * 4. PROXY_SENDRECEIVE - send buffer
1838 * Then in NdrpCompleteAsyncClientCall:
1839 * 1. PROXY_SENDRECEIVE - receive buffer
1840 * 2. PROXY_UNMARHSAL - unmarshal [out] params from buffer
1842 for (phase = PROXY_CALCSIZE; phase <= PROXY_SENDRECEIVE; phase++)
1844 RPC_STATUS status;
1845 TRACE("phase = %d\n", phase);
1846 switch (phase)
1848 case PROXY_GETBUFFER:
1849 /* allocate the buffer */
1850 if (Oif_flags.HasPipes)
1851 /* NdrGetPipeBuffer(...) */
1852 FIXME("pipes not supported yet\n");
1853 else
1855 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
1856 #if 0
1857 NdrNsGetBuffer(pStubMsg, pStubMsg->BufferLength, async_call_data->hBinding);
1858 #else
1859 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
1860 #endif
1861 else
1862 NdrGetBuffer(pStubMsg, pStubMsg->BufferLength, async_call_data->hBinding);
1864 pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1865 status = I_RpcAsyncSetHandle(pRpcMsg, pAsync);
1866 if (status != RPC_S_OK)
1867 RpcRaiseException(status);
1868 break;
1869 case PROXY_SENDRECEIVE:
1870 pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1871 /* send the [in] params only */
1872 if (Oif_flags.HasPipes)
1873 /* NdrPipesSend(...) */
1874 FIXME("pipes not supported yet\n");
1875 else
1877 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
1878 #if 0
1879 NdrNsSend(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
1880 #else
1881 FIXME("using auto handle - call NdrNsSend when it gets implemented\n");
1882 #endif
1883 else
1885 pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
1886 status = I_RpcSend(pStubMsg->RpcMsg);
1887 if (status != RPC_S_OK)
1888 RpcRaiseException(status);
1892 break;
1893 case PROXY_CALCSIZE:
1894 case PROXY_MARSHAL:
1895 if (bV2Format)
1896 client_do_args(pStubMsg, pFormat, phase, pStubMsg->StackTop,
1897 async_call_data->number_of_params, NULL);
1898 else
1899 client_do_args_old_format(pStubMsg, pFormat, phase,
1900 pStubMsg->StackTop, async_call_data->stack_size, NULL,
1901 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT), FALSE);
1902 break;
1903 default:
1904 ERR("shouldn't reach here. phase %d\n", phase);
1905 break;
1909 done:
1910 TRACE("returning 0\n");
1911 RetVal = 0;
1912 return *(CLIENT_CALL_RETURN *)&RetVal;
1915 RPC_STATUS NdrpCompleteAsyncClientCall(RPC_ASYNC_STATE *pAsync, void *Reply)
1917 /* pointer to start of stack where arguments start */
1918 PMIDL_STUB_MESSAGE pStubMsg;
1919 struct async_call_data *async_call_data;
1920 /* the type of pass we are currently doing */
1921 int phase;
1922 /* header for procedure string */
1923 const NDR_PROC_HEADER * pProcHeader;
1924 /* -Oif or -Oicf generated format */
1925 BOOL bV2Format;
1926 RPC_STATUS status = RPC_S_OK;
1928 if (!pAsync->StubInfo)
1929 return RPC_S_INVALID_ASYNC_HANDLE;
1931 async_call_data = pAsync->StubInfo;
1932 pStubMsg = async_call_data->pStubMsg;
1933 pProcHeader = async_call_data->pProcHeader;
1935 bV2Format = (pStubMsg->StubDesc->Version >= 0x20000);
1937 /* order of phases:
1938 * 1. PROXY_CALCSIZE - calculate the buffer size
1939 * 2. PROXY_GETBUFFER - allocate the buffer
1940 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
1941 * 4. PROXY_SENDRECEIVE - send buffer
1942 * Then in NdrpCompleteAsyncClientCall:
1943 * 1. PROXY_SENDRECEIVE - receive buffer
1944 * 2. PROXY_UNMARHSAL - unmarshal [out] params from buffer
1946 for (phase = PROXY_SENDRECEIVE; phase <= PROXY_UNMARSHAL; phase++)
1948 switch (phase)
1950 case PROXY_SENDRECEIVE:
1951 pStubMsg->RpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1952 /* receive the [out] params */
1953 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
1954 #if 0
1955 NdrNsReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
1956 #else
1957 FIXME("using auto handle - call NdrNsReceive when it gets implemented\n");
1958 #endif
1959 else
1961 status = I_RpcReceive(pStubMsg->RpcMsg);
1962 if (status != RPC_S_OK)
1963 goto cleanup;
1964 pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
1965 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
1966 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
1967 pStubMsg->Buffer = pStubMsg->BufferStart;
1970 /* convert strings, floating point values and endianess into our
1971 * preferred format */
1972 #if 0
1973 if ((pStubMsg->RpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1974 NdrConvert(pStubMsg, pFormat);
1975 #endif
1977 break;
1978 case PROXY_UNMARSHAL:
1979 if (bV2Format)
1980 client_do_args(pStubMsg, async_call_data->pParamFormat, phase, pStubMsg->StackTop,
1981 async_call_data->number_of_params, Reply);
1982 else
1983 client_do_args_old_format(pStubMsg, async_call_data->pParamFormat, phase,
1984 pStubMsg->StackTop, async_call_data->stack_size, Reply, FALSE, FALSE);
1985 break;
1986 default:
1987 ERR("shouldn't reach here. phase %d\n", phase);
1988 break;
1992 cleanup:
1993 if (pStubMsg->fHasNewCorrDesc)
1995 /* free extra correlation package */
1996 NdrCorrelationFree(pStubMsg);
1999 /* free the full pointer translation tables */
2000 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
2001 NdrFullPointerXlatFree(pStubMsg->FullPtrXlatTables);
2003 /* free marshalling buffer */
2004 NdrFreeBuffer(pStubMsg);
2005 client_free_handle(pStubMsg, pProcHeader, async_call_data->pHandleFormat, async_call_data->hBinding);
2007 I_RpcFree(pStubMsg->StackTop);
2008 I_RpcFree(async_call_data);
2010 TRACE("-- 0x%x\n", status);
2011 return status;
2014 RPCRTAPI LONG RPC_ENTRY NdrAsyncStubCall(struct IRpcStubBuffer* pThis,
2015 struct IRpcChannelBuffer* pChannel, PRPC_MESSAGE pRpcMsg,
2016 DWORD * pdwStubPhase)
2018 FIXME("unimplemented, expect crash!\n");
2019 return 0;