user32/tests: Do not check the last error on succeeding hotkey calls.
[wine.git] / dlls / rpcrt4 / ndr_stubless.c
blob171d74ddac33978f909906e3824ebff1c6a7b08e
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);
194 if ((pDesc->flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL) &&
195 !context_handle)
197 ERR("null context handle isn't allowed\n");
198 RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);
199 return NULL;
201 *phBinding = NDRCContextBinding(context_handle);
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)
318 pArg = *(unsigned char **)pArg;
320 TRACE("\tbase type: 0x%02x\n", *pTypeFormat);
322 switch (phase)
324 case PROXY_CALCSIZE:
325 if (pParam->param_attributes.IsIn)
326 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
327 break;
328 case PROXY_MARSHAL:
329 if (pParam->param_attributes.IsIn)
330 call_marshaller(pStubMsg, pArg, pTypeFormat);
331 break;
332 case PROXY_UNMARSHAL:
333 if (pParam->param_attributes.IsOut)
335 if (pParam->param_attributes.IsReturn)
336 call_unmarshaller(pStubMsg, &pRetVal, pTypeFormat, 0);
337 else
338 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
339 TRACE("pRetVal = %p\n", pRetVal);
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)
390 call_unmarshaller(pStubMsg, &pRetVal, pTypeFormat, 0);
391 else if (pParam->param_attributes.IsByValue)
392 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
393 else
394 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
396 break;
397 default:
398 RpcRaiseException(RPC_S_INTERNAL_ERROR);
401 current_offset += sizeof(NDR_PARAM_OIF_OTHER);
403 TRACE("\tmemory addr (after): %p\n", pArg);
407 static unsigned int type_stack_size(unsigned char fc)
409 switch (fc)
411 case RPC_FC_BYTE:
412 case RPC_FC_CHAR:
413 case RPC_FC_SMALL:
414 case RPC_FC_USMALL:
415 return sizeof(char);
416 case RPC_FC_WCHAR:
417 case RPC_FC_SHORT:
418 case RPC_FC_USHORT:
419 return sizeof(short);
420 case RPC_FC_LONG:
421 case RPC_FC_ULONG:
422 case RPC_FC_ENUM16:
423 case RPC_FC_ENUM32:
424 return sizeof(int);
425 case RPC_FC_FLOAT:
426 return sizeof(float);
427 case RPC_FC_DOUBLE:
428 return sizeof(double);
429 case RPC_FC_HYPER:
430 return sizeof(ULONGLONG);
431 case RPC_FC_ERROR_STATUS_T:
432 return sizeof(error_status_t);
433 case RPC_FC_IGNORE:
434 return sizeof(void *);
435 default:
436 ERR("invalid base type 0x%x\n", fc);
437 RpcRaiseException(RPC_S_INTERNAL_ERROR);
441 void client_do_args_old_format(PMIDL_STUB_MESSAGE pStubMsg,
442 PFORMAT_STRING pFormat, int phase, unsigned char *args,
443 unsigned short stack_size,
444 unsigned char *pRetVal, BOOL object_proc, BOOL ignore_retval)
446 /* current format string offset */
447 int current_offset = 0;
448 /* current stack offset */
449 unsigned short current_stack_offset = 0;
450 /* counter */
451 unsigned short i;
453 /* NOTE: V1 style format doesn't terminate on the number_of_params
454 * condition as it doesn't have this attribute. Instead it
455 * terminates when the stack size given in the header is exceeded.
457 for (i = 0; TRUE; i++)
459 const NDR_PARAM_OI_BASETYPE *pParam =
460 (const NDR_PARAM_OI_BASETYPE *)&pFormat[current_offset];
461 /* note: current_stack_offset starts after the This pointer
462 * if present, so adjust this */
463 unsigned short current_stack_offset_adjusted = current_stack_offset +
464 (object_proc ? sizeof(void *) : 0);
465 unsigned char * pArg = ARG_FROM_OFFSET(args, current_stack_offset_adjusted);
467 /* no more parameters; exit loop */
468 if (current_stack_offset_adjusted >= stack_size)
469 break;
471 TRACE("param[%d]: old format\n", i);
472 TRACE("\tparam_direction: 0x%x\n", pParam->param_direction);
473 TRACE("\tstack_offset: 0x%x\n", current_stack_offset_adjusted);
474 TRACE("\tmemory addr (before): %p\n", pArg);
476 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE ||
477 pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
479 const unsigned char * pTypeFormat =
480 &pParam->type_format_char;
482 TRACE("\tbase type 0x%02x\n", *pTypeFormat);
484 switch (phase)
486 case PROXY_CALCSIZE:
487 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
488 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
489 break;
490 case PROXY_MARSHAL:
491 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
492 call_marshaller(pStubMsg, pArg, pTypeFormat);
493 break;
494 case PROXY_UNMARSHAL:
495 if (!ignore_retval &&
496 pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
498 if (pParam->param_direction & RPC_FC_RETURN_PARAM)
499 call_unmarshaller(pStubMsg, &pRetVal, pTypeFormat, 0);
500 else
501 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
503 break;
504 default:
505 RpcRaiseException(RPC_S_INTERNAL_ERROR);
508 current_stack_offset += type_stack_size(*pTypeFormat);
509 current_offset += sizeof(NDR_PARAM_OI_BASETYPE);
511 else
513 const NDR_PARAM_OI_OTHER *pParamOther =
514 (const NDR_PARAM_OI_OTHER *)&pFormat[current_offset];
516 const unsigned char *pTypeFormat =
517 &pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset];
519 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
521 switch (phase)
523 case PROXY_CALCSIZE:
524 if (pParam->param_direction == RPC_FC_IN_PARAM ||
525 pParam->param_direction & RPC_FC_IN_OUT_PARAM)
526 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
527 break;
528 case PROXY_MARSHAL:
529 if (pParam->param_direction == RPC_FC_IN_PARAM ||
530 pParam->param_direction & RPC_FC_IN_OUT_PARAM)
531 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
532 break;
533 case PROXY_UNMARSHAL:
534 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
535 pParam->param_direction == RPC_FC_OUT_PARAM)
536 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
537 else if (pParam->param_direction == RPC_FC_RETURN_PARAM)
538 call_unmarshaller(pStubMsg, (unsigned char **)pRetVal, pTypeFormat, 0);
539 break;
540 default:
541 RpcRaiseException(RPC_S_INTERNAL_ERROR);
544 current_stack_offset += pParamOther->stack_size * sizeof(INT);
545 current_offset += sizeof(NDR_PARAM_OI_OTHER);
547 TRACE("\tmemory addr (after): %p\n", pArg);
551 CLIENT_CALL_RETURN WINAPIV NdrClientCall2(PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat, ...)
553 /* pointer to start of stack where arguments start */
554 RPC_MESSAGE rpcMsg;
555 MIDL_STUB_MESSAGE stubMsg;
556 handle_t hBinding = NULL;
557 /* procedure number */
558 unsigned short procedure_number;
559 /* size of stack */
560 unsigned short stack_size;
561 /* number of parameters. optional for client to give it to us */
562 unsigned char number_of_params = ~0;
563 /* cache of Oif_flags from v2 procedure header */
564 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
565 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
566 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
567 /* the type of pass we are currently doing */
568 int phase;
569 /* header for procedure string */
570 const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
571 /* -Oif or -Oicf generated format */
572 BOOL bV2Format = FALSE;
573 /* the value to return to the client from the remote procedure */
574 LONG_PTR RetVal = 0;
575 /* the pointer to the object when in OLE mode */
576 void * This = NULL;
577 PFORMAT_STRING pHandleFormat;
578 /* correlation cache */
579 ULONG_PTR NdrCorrCache[256];
580 __ms_va_list args;
582 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
584 TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
586 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
588 const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
589 stack_size = pProcHeader->stack_size;
590 procedure_number = pProcHeader->proc_num;
591 pFormat += sizeof(NDR_PROC_HEADER_RPC);
593 else
595 stack_size = pProcHeader->stack_size;
596 procedure_number = pProcHeader->proc_num;
597 pFormat += sizeof(NDR_PROC_HEADER);
599 TRACE("stack size: 0x%x\n", stack_size);
600 TRACE("proc num: %d\n", procedure_number);
602 /* create the full pointer translation tables, if requested */
603 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
604 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
606 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
608 /* object is always the first argument */
609 This = **(void *const **)(&pFormat+1);
610 NdrProxyInitialize(This, &rpcMsg, &stubMsg, pStubDesc, procedure_number);
612 else
613 NdrClientInitializeNew(&rpcMsg, &stubMsg, pStubDesc, procedure_number);
615 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
616 TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
618 /* needed for conformance of top-level objects */
619 __ms_va_start( args, pFormat );
620 stubMsg.StackTop = va_arg( args, unsigned char * );
621 __ms_va_end( args );
623 pHandleFormat = pFormat;
625 /* we only need a handle if this isn't an object method */
626 if (!(pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT))
628 pFormat = client_get_handle(&stubMsg, pProcHeader, pHandleFormat, &hBinding);
629 if (!pFormat) goto done;
632 bV2Format = (pStubDesc->Version >= 0x20000);
634 if (bV2Format)
636 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
637 (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
639 Oif_flags = pOIFHeader->Oi2Flags;
640 number_of_params = pOIFHeader->number_of_params;
642 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
645 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
647 if (Oif_flags.HasExtensions)
649 const NDR_PROC_HEADER_EXTS *pExtensions =
650 (const NDR_PROC_HEADER_EXTS *)pFormat;
651 ext_flags = pExtensions->Flags2;
652 pFormat += pExtensions->Size;
655 stubMsg.BufferLength = 0;
657 /* store the RPC flags away */
658 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
659 rpcMsg.RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
661 /* use alternate memory allocation routines */
662 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
663 NdrRpcSmSetClientToOsf(&stubMsg);
665 if (Oif_flags.HasPipes)
667 FIXME("pipes not supported yet\n");
668 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
669 /* init pipes package */
670 /* NdrPipesInitialize(...) */
672 if (ext_flags.HasNewCorrDesc)
674 /* initialize extra correlation package */
675 NdrCorrelationInitialize(&stubMsg, NdrCorrCache, sizeof(NdrCorrCache), 0);
678 /* order of phases:
679 * 1. PROXY_CALCSIZE - calculate the buffer size
680 * 2. PROXY_GETBUFFER - allocate the buffer
681 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
682 * 4. PROXY_SENDRECEIVE - send/receive buffer
683 * 5. PROXY_UNMARHSAL - unmarshal [out] params from buffer
685 if ((pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT) ||
686 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_HAS_COMM_OR_FAULT))
688 __TRY
690 for (phase = PROXY_CALCSIZE; phase <= PROXY_UNMARSHAL; phase++)
692 TRACE("phase = %d\n", phase);
693 switch (phase)
695 case PROXY_GETBUFFER:
696 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
698 /* allocate the buffer */
699 NdrProxyGetBuffer(This, &stubMsg);
701 else
703 /* allocate the buffer */
704 if (Oif_flags.HasPipes)
705 /* NdrGetPipeBuffer(...) */
706 FIXME("pipes not supported yet\n");
707 else
709 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
710 #if 0
711 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
712 #else
713 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
714 #endif
715 else
716 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
719 break;
720 case PROXY_SENDRECEIVE:
721 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
723 /* send the [in] params and receive the [out] and [retval]
724 * params */
725 NdrProxySendReceive(This, &stubMsg);
727 else
729 /* send the [in] params and receive the [out] and [retval]
730 * params */
731 if (Oif_flags.HasPipes)
732 /* NdrPipesSendReceive(...) */
733 FIXME("pipes not supported yet\n");
734 else
736 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
737 #if 0
738 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
739 #else
740 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
741 #endif
742 else
743 NdrSendReceive(&stubMsg, stubMsg.Buffer);
747 /* convert strings, floating point values and endianess into our
748 * preferred format */
749 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
750 NdrConvert(&stubMsg, pFormat);
752 break;
753 case PROXY_CALCSIZE:
754 case PROXY_MARSHAL:
755 case PROXY_UNMARSHAL:
756 if (bV2Format)
757 client_do_args(&stubMsg, pFormat, phase, stubMsg.StackTop,
758 number_of_params, (unsigned char *)&RetVal);
759 else
760 client_do_args_old_format(&stubMsg, pFormat, phase,
761 stubMsg.StackTop, stack_size, (unsigned char *)&RetVal,
762 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT), FALSE);
763 break;
764 default:
765 ERR("shouldn't reach here. phase %d\n", phase);
766 break;
770 __EXCEPT_ALL
772 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
773 RetVal = NdrProxyErrorHandler(GetExceptionCode());
774 else
776 const COMM_FAULT_OFFSETS *comm_fault_offsets = &pStubDesc->CommFaultOffsets[procedure_number];
777 ULONG *comm_status;
778 ULONG *fault_status;
780 TRACE("comm_fault_offsets = {0x%hx, 0x%hx}\n", comm_fault_offsets->CommOffset, comm_fault_offsets->FaultOffset);
782 if (comm_fault_offsets->CommOffset == -1)
783 comm_status = (ULONG *)&RetVal;
784 else if (comm_fault_offsets->CommOffset >= 0)
785 comm_status = *(ULONG **)ARG_FROM_OFFSET(stubMsg.StackTop, comm_fault_offsets->CommOffset);
786 else
787 comm_status = NULL;
789 if (comm_fault_offsets->FaultOffset == -1)
790 fault_status = (ULONG *)&RetVal;
791 else if (comm_fault_offsets->FaultOffset >= 0)
792 fault_status = *(ULONG **)ARG_FROM_OFFSET(stubMsg.StackTop, comm_fault_offsets->CommOffset);
793 else
794 fault_status = NULL;
796 NdrMapCommAndFaultStatus(&stubMsg, comm_status, fault_status,
797 GetExceptionCode());
800 __ENDTRY
802 else
804 /* order of phases:
805 * 1. PROXY_CALCSIZE - calculate the buffer size
806 * 2. PROXY_GETBUFFER - allocate the buffer
807 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
808 * 4. PROXY_SENDRECEIVE - send/receive buffer
809 * 5. PROXY_UNMARHSAL - unmarshal [out] params from buffer
811 for (phase = PROXY_CALCSIZE; phase <= PROXY_UNMARSHAL; phase++)
813 TRACE("phase = %d\n", phase);
814 switch (phase)
816 case PROXY_GETBUFFER:
817 /* allocate the buffer */
818 if (Oif_flags.HasPipes)
819 /* NdrGetPipeBuffer(...) */
820 FIXME("pipes not supported yet\n");
821 else
823 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
824 #if 0
825 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
826 #else
827 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
828 #endif
829 else
830 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
832 break;
833 case PROXY_SENDRECEIVE:
834 /* send the [in] params and receive the [out] and [retval]
835 * params */
836 if (Oif_flags.HasPipes)
837 /* NdrPipesSendReceive(...) */
838 FIXME("pipes not supported yet\n");
839 else
841 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
842 #if 0
843 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
844 #else
845 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
846 #endif
847 else
848 NdrSendReceive(&stubMsg, stubMsg.Buffer);
851 /* convert strings, floating point values and endianess into our
852 * preferred format */
853 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
854 NdrConvert(&stubMsg, pFormat);
856 break;
857 case PROXY_CALCSIZE:
858 case PROXY_MARSHAL:
859 case PROXY_UNMARSHAL:
860 if (bV2Format)
861 client_do_args(&stubMsg, pFormat, phase, stubMsg.StackTop,
862 number_of_params, (unsigned char *)&RetVal);
863 else
864 client_do_args_old_format(&stubMsg, pFormat, phase,
865 stubMsg.StackTop, stack_size, (unsigned char *)&RetVal,
866 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT), FALSE);
867 break;
868 default:
869 ERR("shouldn't reach here. phase %d\n", phase);
870 break;
875 if (ext_flags.HasNewCorrDesc)
877 /* free extra correlation package */
878 NdrCorrelationFree(&stubMsg);
881 if (Oif_flags.HasPipes)
883 /* NdrPipesDone(...) */
886 /* free the full pointer translation tables */
887 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
888 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
890 /* free marshalling buffer */
891 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
892 NdrProxyFreeBuffer(This, &stubMsg);
893 else
895 NdrFreeBuffer(&stubMsg);
896 client_free_handle(&stubMsg, pProcHeader, pHandleFormat, hBinding);
899 done:
900 TRACE("RetVal = 0x%lx\n", RetVal);
901 return *(CLIENT_CALL_RETURN *)&RetVal;
904 /* Calls a function with the specified arguments, restoring the stack
905 * properly afterwards as we don't know the calling convention of the
906 * function */
907 #if defined __i386__ && defined _MSC_VER
908 __declspec(naked) LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size)
910 __asm
912 push ebp
913 push edi ; Save registers
914 push esi
915 mov ebp, esp
916 mov eax, [ebp+16] ; Get stack size
917 sub esp, eax ; Make room in stack for arguments
918 mov edi, esp
919 mov ecx, eax
920 mov esi, [ebp+12]
921 shr ecx, 2
923 rep movsd ; Copy dword blocks
924 call [ebp+8] ; Call function
925 lea esp, [ebp-8] ; Restore stack
926 pop esi ; Restore registers
927 pop edi
928 pop ebp
932 #elif defined __i386__ && defined __GNUC__
933 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
934 __ASM_GLOBAL_FUNC(call_server_func,
935 "pushl %ebp\n\t"
936 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
937 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
938 "movl %esp,%ebp\n\t"
939 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
940 "pushl %edi\n\t" /* Save registers */
941 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
942 "pushl %esi\n\t"
943 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
944 "movl 16(%ebp), %eax\n\t" /* Get stack size */
945 "subl %eax, %esp\n\t" /* Make room in stack for arguments */
946 "andl $~15, %esp\n\t" /* Make sure stack has 16-byte alignment for Mac OS X */
947 "movl %esp, %edi\n\t"
948 "movl %eax, %ecx\n\t"
949 "movl 12(%ebp), %esi\n\t"
950 "shrl $2, %ecx\n\t" /* divide by 4 */
951 "cld\n\t"
952 "rep; movsl\n\t" /* Copy dword blocks */
953 "call *8(%ebp)\n\t" /* Call function */
954 "leal -8(%ebp), %esp\n\t" /* Restore stack */
955 "popl %esi\n\t" /* Restore registers */
956 __ASM_CFI(".cfi_same_value %esi\n\t")
957 "popl %edi\n\t"
958 __ASM_CFI(".cfi_same_value %edi\n\t")
959 "popl %ebp\n\t"
960 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
961 __ASM_CFI(".cfi_same_value %ebp\n\t")
962 "ret" )
963 #else
964 #warning call_server_func not implemented for your architecture
965 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned short stack_size)
967 FIXME("Not implemented for your architecture\n");
968 return 0;
970 #endif
972 static DWORD calc_arg_size(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat)
974 DWORD size;
975 switch(*pFormat)
977 case RPC_FC_STRUCT:
978 size = *(const WORD*)(pFormat + 2);
979 break;
980 case RPC_FC_BOGUS_STRUCT:
981 size = *(const WORD*)(pFormat + 2);
982 if(*(const WORD*)(pFormat + 4))
983 FIXME("Unhandled conformant description\n");
984 break;
985 case RPC_FC_CARRAY:
986 size = *(const WORD*)(pFormat + 2);
987 ComputeConformance(pStubMsg, NULL, pFormat + 4, 0);
988 size *= pStubMsg->MaxCount;
989 break;
990 case RPC_FC_SMFARRAY:
991 size = *(const WORD*)(pFormat + 2);
992 break;
993 case RPC_FC_LGFARRAY:
994 size = *(const DWORD*)(pFormat + 2);
995 break;
996 case RPC_FC_BOGUS_ARRAY:
997 pFormat = ComputeConformance(pStubMsg, NULL, pFormat + 4, *(const WORD*)&pFormat[2]);
998 TRACE("conformance = %ld\n", pStubMsg->MaxCount);
999 pFormat = ComputeVariance(pStubMsg, NULL, pFormat, pStubMsg->MaxCount);
1000 size = ComplexStructSize(pStubMsg, pFormat);
1001 size *= pStubMsg->MaxCount;
1002 break;
1003 case RPC_FC_C_CSTRING:
1004 case RPC_FC_C_WSTRING:
1005 if (*pFormat == RPC_FC_C_CSTRING)
1006 size = sizeof(CHAR);
1007 else
1008 size = sizeof(WCHAR);
1009 if (pFormat[1] == RPC_FC_STRING_SIZED)
1010 ComputeConformance(pStubMsg, NULL, pFormat + 2, 0);
1011 else
1012 pStubMsg->MaxCount = 0;
1013 size *= pStubMsg->MaxCount;
1014 break;
1015 default:
1016 FIXME("Unhandled type %02x\n", *pFormat);
1017 /* fallthrough */
1018 case RPC_FC_RP:
1019 size = sizeof(void *);
1020 break;
1022 return size;
1025 static LONG_PTR *stub_do_args(MIDL_STUB_MESSAGE *pStubMsg,
1026 PFORMAT_STRING pFormat, int phase,
1027 unsigned char *args,
1028 unsigned short number_of_params)
1030 /* counter */
1031 unsigned short i;
1032 /* current format string offset */
1033 int current_offset = 0;
1034 /* current stack offset */
1035 unsigned short current_stack_offset = 0;
1036 /* location to put retval into */
1037 LONG_PTR *retval_ptr = NULL;
1039 for (i = 0; i < number_of_params; i++)
1041 const NDR_PARAM_OIF_BASETYPE *pParam =
1042 (const NDR_PARAM_OIF_BASETYPE *)&pFormat[current_offset];
1043 unsigned char *pArg;
1045 current_stack_offset = pParam->stack_offset;
1046 pArg = args + current_stack_offset;
1048 TRACE("param[%d]: new format\n", i);
1049 TRACE("\tparam_attributes:"); dump_RPC_FC_PROC_PF(pParam->param_attributes); TRACE("\n");
1050 TRACE("\tstack_offset: 0x%x\n", current_stack_offset);
1051 TRACE("\tmemory addr (before): %p -> %p\n", pArg, *(unsigned char **)pArg);
1053 if (pParam->param_attributes.IsBasetype)
1055 const unsigned char *pTypeFormat =
1056 &pParam->type_format_char;
1058 TRACE("\tbase type: 0x%02x\n", *pTypeFormat);
1060 /* make a note of the address of the return value parameter for later */
1061 if (pParam->param_attributes.IsReturn)
1062 retval_ptr = (LONG_PTR *)pArg;
1064 switch (phase)
1066 case STUBLESS_MARSHAL:
1067 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1069 if (pParam->param_attributes.IsSimpleRef)
1070 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1071 else
1072 call_marshaller(pStubMsg, pArg, pTypeFormat);
1074 break;
1075 case STUBLESS_FREE:
1076 if (pParam->param_attributes.ServerAllocSize)
1077 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
1078 break;
1079 case STUBLESS_INITOUT:
1080 break;
1081 case STUBLESS_UNMARSHAL:
1082 if (pParam->param_attributes.ServerAllocSize)
1083 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1084 pParam->param_attributes.ServerAllocSize * 8);
1086 if (pParam->param_attributes.IsIn)
1088 if (pParam->param_attributes.IsSimpleRef)
1089 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1090 else
1091 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1093 break;
1094 case STUBLESS_CALCSIZE:
1095 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1097 if (pParam->param_attributes.IsSimpleRef)
1098 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1099 else
1100 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1102 break;
1103 default:
1104 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1107 current_offset += sizeof(NDR_PARAM_OIF_BASETYPE);
1109 else
1111 const NDR_PARAM_OIF_OTHER *pParamOther =
1112 (const NDR_PARAM_OIF_OTHER *)&pFormat[current_offset];
1114 const unsigned char * pTypeFormat =
1115 &(pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset]);
1117 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1119 switch (phase)
1121 case STUBLESS_MARSHAL:
1122 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1124 if (pParam->param_attributes.IsByValue)
1125 call_marshaller(pStubMsg, pArg, pTypeFormat);
1126 else
1127 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1129 break;
1130 case STUBLESS_FREE:
1131 if (pParam->param_attributes.MustFree)
1133 if (pParam->param_attributes.IsByValue)
1134 call_freer(pStubMsg, pArg, pTypeFormat);
1135 else
1136 call_freer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1139 if (pParam->param_attributes.IsOut &&
1140 !pParam->param_attributes.IsIn &&
1141 !pParam->param_attributes.IsByValue &&
1142 !pParam->param_attributes.ServerAllocSize)
1144 if (*pTypeFormat != RPC_FC_BIND_CONTEXT)
1145 pStubMsg->pfnFree(*(void **)pArg);
1148 if (pParam->param_attributes.ServerAllocSize)
1149 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
1150 break;
1151 case STUBLESS_INITOUT:
1152 if (!pParam->param_attributes.IsIn &&
1153 pParam->param_attributes.IsOut &&
1154 !pParam->param_attributes.ServerAllocSize &&
1155 !pParam->param_attributes.IsByValue)
1157 if (*pTypeFormat == RPC_FC_BIND_CONTEXT)
1159 NDR_SCONTEXT ctxt = NdrContextHandleInitialize(
1160 pStubMsg, pTypeFormat);
1161 *(void **)pArg = NDRSContextValue(ctxt);
1163 else
1165 DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1167 if(size)
1169 *(void **)pArg = NdrAllocate(pStubMsg, size);
1170 memset(*(void **)pArg, 0, size);
1174 break;
1175 case STUBLESS_UNMARSHAL:
1176 if (pParam->param_attributes.ServerAllocSize)
1177 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1178 pParam->param_attributes.ServerAllocSize * 8);
1180 if (pParam->param_attributes.IsIn)
1182 if (pParam->param_attributes.IsByValue)
1183 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1184 else
1185 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1187 break;
1188 case STUBLESS_CALCSIZE:
1189 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1191 if (pParam->param_attributes.IsByValue)
1192 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1193 else
1194 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1196 break;
1197 default:
1198 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1201 current_offset += sizeof(NDR_PARAM_OIF_OTHER);
1203 TRACE("\tmemory addr (after): %p -> %p\n", pArg, *(unsigned char **)pArg);
1206 return retval_ptr;
1209 static LONG_PTR *stub_do_old_args(MIDL_STUB_MESSAGE *pStubMsg,
1210 PFORMAT_STRING pFormat, int phase,
1211 unsigned char *args,
1212 unsigned short stack_size, BOOL object)
1214 /* counter */
1215 unsigned short i;
1216 /* current format string offset */
1217 int current_offset = 0;
1218 /* current stack offset */
1219 unsigned short current_stack_offset = 0;
1220 /* location to put retval into */
1221 LONG_PTR *retval_ptr = NULL;
1223 for (i = 0; TRUE; i++)
1225 const NDR_PARAM_OI_BASETYPE *pParam =
1226 (const NDR_PARAM_OI_BASETYPE *)&pFormat[current_offset];
1227 /* note: current_stack_offset starts after the This pointer
1228 * if present, so adjust this */
1229 unsigned short current_stack_offset_adjusted = current_stack_offset +
1230 (object ? sizeof(void *) : 0);
1231 unsigned char *pArg = args + current_stack_offset_adjusted;
1233 /* no more parameters; exit loop */
1234 if (current_stack_offset_adjusted >= stack_size)
1235 break;
1237 TRACE("param[%d]: old format\n", i);
1238 TRACE("\tparam_direction: 0x%x\n", pParam->param_direction);
1239 TRACE("\tstack_offset: 0x%x\n", current_stack_offset_adjusted);
1241 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE ||
1242 pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1244 const unsigned char *pTypeFormat =
1245 &pParam->type_format_char;
1247 TRACE("\tbase type 0x%02x\n", *pTypeFormat);
1249 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1250 retval_ptr = (LONG_PTR *)pArg;
1252 switch (phase)
1254 case STUBLESS_MARSHAL:
1255 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1256 call_marshaller(pStubMsg, pArg, pTypeFormat);
1257 break;
1258 case STUBLESS_FREE:
1259 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1260 call_freer(pStubMsg, pArg, pTypeFormat);
1261 break;
1262 case STUBLESS_INITOUT:
1263 break;
1264 case STUBLESS_UNMARSHAL:
1265 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1266 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1267 break;
1268 case STUBLESS_CALCSIZE:
1269 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1270 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1271 break;
1272 default:
1273 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1276 current_stack_offset += type_stack_size(*pTypeFormat);
1277 current_offset += sizeof(NDR_PARAM_OI_BASETYPE);
1279 else
1281 const NDR_PARAM_OI_OTHER *pParamOther =
1282 (const NDR_PARAM_OI_OTHER *)&pFormat[current_offset];
1284 const unsigned char * pTypeFormat =
1285 &pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset];
1287 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1289 if (pParam->param_direction == RPC_FC_RETURN_PARAM)
1290 retval_ptr = (LONG_PTR *)pArg;
1292 switch (phase)
1294 case STUBLESS_MARSHAL:
1295 if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1296 pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1297 pParam->param_direction == RPC_FC_RETURN_PARAM)
1298 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1299 break;
1300 case STUBLESS_FREE:
1301 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1302 pParam->param_direction == RPC_FC_IN_PARAM)
1303 call_freer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1304 else if (pParam->param_direction == RPC_FC_OUT_PARAM)
1305 pStubMsg->pfnFree(*(void **)pArg);
1306 break;
1307 case STUBLESS_INITOUT:
1308 if (pParam->param_direction == RPC_FC_OUT_PARAM)
1310 DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1312 if(size)
1314 *(void **)pArg = NdrAllocate(pStubMsg, size);
1315 memset(*(void **)pArg, 0, size);
1318 break;
1319 case STUBLESS_UNMARSHAL:
1320 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1321 pParam->param_direction == RPC_FC_IN_PARAM)
1322 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1323 break;
1324 case STUBLESS_CALCSIZE:
1325 if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1326 pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1327 pParam->param_direction == RPC_FC_RETURN_PARAM)
1328 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1329 break;
1330 default:
1331 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1334 current_stack_offset += pParamOther->stack_size * sizeof(INT);
1335 current_offset += sizeof(NDR_PARAM_OI_OTHER);
1339 return retval_ptr;
1342 /***********************************************************************
1343 * NdrStubCall2 [RPCRT4.@]
1345 * Unmarshals [in] parameters, calls either a method in an object or a server
1346 * function, marshals any [out] parameters and frees any allocated data.
1348 * NOTES
1349 * Used by stubless MIDL-generated code.
1351 LONG WINAPI NdrStubCall2(
1352 struct IRpcStubBuffer * pThis,
1353 struct IRpcChannelBuffer * pChannel,
1354 PRPC_MESSAGE pRpcMsg,
1355 DWORD * pdwStubPhase)
1357 const MIDL_SERVER_INFO *pServerInfo;
1358 const MIDL_STUB_DESC *pStubDesc;
1359 PFORMAT_STRING pFormat;
1360 MIDL_STUB_MESSAGE stubMsg;
1361 /* pointer to start of stack to pass into stub implementation */
1362 unsigned char * args;
1363 /* size of stack */
1364 unsigned short stack_size;
1365 /* number of parameters. optional for client to give it to us */
1366 unsigned char number_of_params = ~0;
1367 /* cache of Oif_flags from v2 procedure header */
1368 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1369 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1370 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1371 /* the type of pass we are currently doing */
1372 int phase;
1373 /* header for procedure string */
1374 const NDR_PROC_HEADER *pProcHeader;
1375 /* offset in format string for start of params */
1376 int parameter_start_offset;
1377 /* current format string offset */
1378 int current_offset;
1379 /* -Oif or -Oicf generated format */
1380 BOOL bV2Format = FALSE;
1381 /* location to put retval into */
1382 LONG_PTR *retval_ptr = NULL;
1384 TRACE("pThis %p, pChannel %p, pRpcMsg %p, pdwStubPhase %p\n", pThis, pChannel, pRpcMsg, pdwStubPhase);
1386 if (pThis)
1387 pServerInfo = CStdStubBuffer_GetServerInfo(pThis);
1388 else
1389 pServerInfo = ((RPC_SERVER_INTERFACE *)pRpcMsg->RpcInterfaceInformation)->InterpreterInfo;
1391 pStubDesc = pServerInfo->pStubDesc;
1392 pFormat = pServerInfo->ProcString + pServerInfo->FmtStringOffset[pRpcMsg->ProcNum];
1393 pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1395 TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
1397 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1399 const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1400 stack_size = pProcHeader->stack_size;
1401 current_offset = sizeof(NDR_PROC_HEADER_RPC);
1404 else
1406 stack_size = pProcHeader->stack_size;
1407 current_offset = sizeof(NDR_PROC_HEADER);
1410 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1412 /* binding */
1413 switch (pProcHeader->handle_type)
1415 /* explicit binding: parse additional section */
1416 case RPC_FC_BIND_EXPLICIT:
1417 switch (pFormat[current_offset]) /* handle_type */
1419 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
1420 current_offset += sizeof(NDR_EHD_PRIMITIVE);
1421 break;
1422 case RPC_FC_BIND_GENERIC: /* explicit generic */
1423 current_offset += sizeof(NDR_EHD_GENERIC);
1424 break;
1425 case RPC_FC_BIND_CONTEXT: /* explicit context */
1426 current_offset += sizeof(NDR_EHD_CONTEXT);
1427 break;
1428 default:
1429 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1430 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1432 break;
1433 case RPC_FC_BIND_GENERIC: /* implicit generic */
1434 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
1435 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
1436 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
1437 break;
1438 default:
1439 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1440 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1443 bV2Format = (pStubDesc->Version >= 0x20000);
1445 if (bV2Format)
1447 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
1448 (const NDR_PROC_PARTIAL_OIF_HEADER *)&pFormat[current_offset];
1450 Oif_flags = pOIFHeader->Oi2Flags;
1451 number_of_params = pOIFHeader->number_of_params;
1453 current_offset += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1456 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
1458 if (Oif_flags.HasExtensions)
1460 const NDR_PROC_HEADER_EXTS *pExtensions =
1461 (const NDR_PROC_HEADER_EXTS *)&pFormat[current_offset];
1462 ext_flags = pExtensions->Flags2;
1463 current_offset += pExtensions->Size;
1466 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1467 NdrStubInitialize(pRpcMsg, &stubMsg, pStubDesc, pChannel);
1468 else
1469 NdrServerInitializeNew(pRpcMsg, &stubMsg, pStubDesc);
1471 /* create the full pointer translation tables, if requested */
1472 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1473 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_SERVER);
1475 /* store the RPC flags away */
1476 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1477 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1479 /* use alternate memory allocation routines */
1480 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
1481 #if 0
1482 NdrRpcSsEnableAllocate(&stubMsg);
1483 #else
1484 FIXME("Set RPCSS memory allocation routines\n");
1485 #endif
1487 if (Oif_flags.HasPipes)
1489 FIXME("pipes not supported yet\n");
1490 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1491 /* init pipes package */
1492 /* NdrPipesInitialize(...) */
1494 if (ext_flags.HasNewCorrDesc)
1496 /* initialize extra correlation package */
1497 FIXME("new correlation description not implemented\n");
1498 stubMsg.fHasNewCorrDesc = TRUE;
1501 /* convert strings, floating point values and endianess into our
1502 * preferred format */
1503 if ((pRpcMsg->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1504 NdrConvert(&stubMsg, pFormat);
1506 parameter_start_offset = current_offset;
1508 TRACE("allocating memory for stack of size %x\n", stack_size);
1510 args = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, stack_size);
1511 stubMsg.StackTop = args; /* used by conformance of top-level objects */
1513 /* add the implicit This pointer as the first arg to the function if we
1514 * are calling an object method */
1515 if (pThis)
1516 *(void **)args = ((CStdStubBuffer *)pThis)->pvServerObject;
1518 for (phase = STUBLESS_UNMARSHAL; phase <= STUBLESS_FREE; phase++)
1520 TRACE("phase = %d\n", phase);
1521 switch (phase)
1523 case STUBLESS_CALLSERVER:
1524 /* call the server function */
1525 if (pServerInfo->ThunkTable && pServerInfo->ThunkTable[pRpcMsg->ProcNum])
1526 pServerInfo->ThunkTable[pRpcMsg->ProcNum](&stubMsg);
1527 else
1529 SERVER_ROUTINE func;
1530 LONG_PTR retval;
1532 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1534 SERVER_ROUTINE *vtbl = *(SERVER_ROUTINE **)((CStdStubBuffer *)pThis)->pvServerObject;
1535 func = vtbl[pRpcMsg->ProcNum];
1537 else
1538 func = pServerInfo->DispatchTable[pRpcMsg->ProcNum];
1540 /* FIXME: what happens with return values that don't fit into a single register on x86? */
1541 retval = call_server_func(func, args, stack_size);
1543 if (retval_ptr)
1545 TRACE("stub implementation returned 0x%lx\n", retval);
1546 *retval_ptr = retval;
1548 else
1549 TRACE("void stub implementation\n");
1552 stubMsg.Buffer = NULL;
1553 stubMsg.BufferLength = 0;
1555 break;
1556 case STUBLESS_GETBUFFER:
1557 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1558 NdrStubGetBuffer(pThis, pChannel, &stubMsg);
1559 else
1561 RPC_STATUS Status;
1563 pRpcMsg->BufferLength = stubMsg.BufferLength;
1564 /* allocate buffer for [out] and [ret] params */
1565 Status = I_RpcGetBuffer(pRpcMsg);
1566 if (Status)
1567 RpcRaiseException(Status);
1568 stubMsg.Buffer = pRpcMsg->Buffer;
1570 break;
1571 case STUBLESS_UNMARSHAL:
1572 case STUBLESS_INITOUT:
1573 case STUBLESS_CALCSIZE:
1574 case STUBLESS_MARSHAL:
1575 case STUBLESS_FREE:
1576 if (bV2Format)
1577 retval_ptr = stub_do_args(&stubMsg, &pFormat[parameter_start_offset],
1578 phase, args, number_of_params);
1579 else
1580 retval_ptr = stub_do_old_args(&stubMsg, &pFormat[parameter_start_offset],
1581 phase, args, stack_size,
1582 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT));
1584 break;
1585 default:
1586 ERR("shouldn't reach here. phase %d\n", phase);
1587 break;
1591 pRpcMsg->BufferLength = (unsigned int)(stubMsg.Buffer - (unsigned char *)pRpcMsg->Buffer);
1593 if (ext_flags.HasNewCorrDesc)
1595 /* free extra correlation package */
1596 /* NdrCorrelationFree(&stubMsg); */
1599 if (Oif_flags.HasPipes)
1601 /* NdrPipesDone(...) */
1604 /* free the full pointer translation tables */
1605 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1606 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
1608 /* free server function stack */
1609 HeapFree(GetProcessHeap(), 0, args);
1611 return S_OK;
1614 /***********************************************************************
1615 * NdrServerCall2 [RPCRT4.@]
1617 void WINAPI NdrServerCall2(PRPC_MESSAGE pRpcMsg)
1619 DWORD dwPhase;
1620 NdrStubCall2(NULL, NULL, pRpcMsg, &dwPhase);
1623 /***********************************************************************
1624 * NdrClientCall [RPCRT4.@]
1626 CLIENT_CALL_RETURN WINAPIV NdrClientCall( PMIDL_STUB_DESC desc, PFORMAT_STRING format, ... )
1628 __ms_va_list args;
1629 CLIENT_CALL_RETURN ret;
1631 __ms_va_start( args, format );
1632 ret = NdrClientCall2( desc, format, va_arg( args, unsigned char * ));
1633 __ms_va_end( args );
1634 return ret;
1637 /***********************************************************************
1638 * NdrStubCall [RPCRT4.@]
1640 LONG WINAPI NdrStubCall( struct IRpcStubBuffer *This, struct IRpcChannelBuffer *channel,
1641 PRPC_MESSAGE msg, DWORD *phase )
1643 return NdrStubCall2( This, channel, msg, phase );
1646 /***********************************************************************
1647 * NdrServerCall [RPCRT4.@]
1649 void WINAPI NdrServerCall( PRPC_MESSAGE msg )
1651 DWORD phase;
1652 NdrStubCall( NULL, NULL, msg, &phase );
1655 struct async_call_data
1657 MIDL_STUB_MESSAGE *pStubMsg;
1658 const NDR_PROC_HEADER *pProcHeader;
1659 PFORMAT_STRING pHandleFormat;
1660 PFORMAT_STRING pParamFormat;
1661 RPC_BINDING_HANDLE hBinding;
1662 /* size of stack */
1663 unsigned short stack_size;
1664 /* number of parameters. optional for client to give it to us */
1665 unsigned char number_of_params;
1666 /* correlation cache */
1667 ULONG_PTR NdrCorrCache[256];
1670 CLIENT_CALL_RETURN WINAPIV NdrAsyncClientCall(PMIDL_STUB_DESC pStubDesc,
1671 PFORMAT_STRING pFormat, ...)
1673 /* pointer to start of stack where arguments start */
1674 PRPC_MESSAGE pRpcMsg;
1675 PMIDL_STUB_MESSAGE pStubMsg;
1676 RPC_ASYNC_STATE *pAsync;
1677 struct async_call_data *async_call_data;
1678 /* procedure number */
1679 unsigned short procedure_number;
1680 /* cache of Oif_flags from v2 procedure header */
1681 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1682 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1683 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1684 /* the type of pass we are currently doing */
1685 int phase;
1686 /* header for procedure string */
1687 const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1688 /* -Oif or -Oicf generated format */
1689 BOOL bV2Format = FALSE;
1690 LONG_PTR RetVal;
1691 __ms_va_list args;
1693 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
1695 /* Later NDR language versions probably won't be backwards compatible */
1696 if (pStubDesc->Version > 0x50002)
1698 FIXME("Incompatible stub description version: 0x%x\n", pStubDesc->Version);
1699 RpcRaiseException(RPC_X_WRONG_STUB_VERSION);
1702 async_call_data = I_RpcAllocate(sizeof(*async_call_data) + sizeof(MIDL_STUB_MESSAGE) + sizeof(RPC_MESSAGE));
1703 if (!async_call_data) RpcRaiseException(ERROR_OUTOFMEMORY);
1704 async_call_data->number_of_params = ~0;
1705 async_call_data->pProcHeader = pProcHeader;
1707 async_call_data->pStubMsg = pStubMsg = (PMIDL_STUB_MESSAGE)(async_call_data + 1);
1708 pRpcMsg = (PRPC_MESSAGE)(pStubMsg + 1);
1710 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1712 const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1713 async_call_data->stack_size = pProcHeader->stack_size;
1714 procedure_number = pProcHeader->proc_num;
1715 pFormat += sizeof(NDR_PROC_HEADER_RPC);
1717 else
1719 async_call_data->stack_size = pProcHeader->stack_size;
1720 procedure_number = pProcHeader->proc_num;
1721 pFormat += sizeof(NDR_PROC_HEADER);
1723 TRACE("stack size: 0x%x\n", async_call_data->stack_size);
1724 TRACE("proc num: %d\n", procedure_number);
1726 /* create the full pointer translation tables, if requested */
1727 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1728 pStubMsg->FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
1730 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1732 ERR("objects not supported\n");
1733 I_RpcFree(async_call_data);
1734 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1737 NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDesc, procedure_number);
1739 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1740 TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
1742 /* needed for conformance of top-level objects */
1743 pStubMsg->StackTop = I_RpcAllocate(async_call_data->stack_size);
1744 __ms_va_start( args, pFormat );
1745 memcpy(pStubMsg->StackTop, va_arg( args, unsigned char * ), async_call_data->stack_size);
1746 __ms_va_end( args );
1748 pAsync = *(RPC_ASYNC_STATE **)pStubMsg->StackTop;
1749 pAsync->StubInfo = async_call_data;
1750 async_call_data->pHandleFormat = pFormat;
1752 pFormat = client_get_handle(pStubMsg, pProcHeader, async_call_data->pHandleFormat, &async_call_data->hBinding);
1753 if (!pFormat) goto done;
1755 bV2Format = (pStubDesc->Version >= 0x20000);
1757 if (bV2Format)
1759 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
1760 (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
1762 Oif_flags = pOIFHeader->Oi2Flags;
1763 async_call_data->number_of_params = pOIFHeader->number_of_params;
1765 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1768 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
1770 if (Oif_flags.HasExtensions)
1772 const NDR_PROC_HEADER_EXTS *pExtensions =
1773 (const NDR_PROC_HEADER_EXTS *)pFormat;
1774 ext_flags = pExtensions->Flags2;
1775 pFormat += pExtensions->Size;
1778 async_call_data->pParamFormat = pFormat;
1780 pStubMsg->BufferLength = 0;
1782 /* store the RPC flags away */
1783 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1784 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1786 /* use alternate memory allocation routines */
1787 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
1788 NdrRpcSmSetClientToOsf(pStubMsg);
1790 if (Oif_flags.HasPipes)
1792 FIXME("pipes not supported yet\n");
1793 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1794 /* init pipes package */
1795 /* NdrPipesInitialize(...) */
1797 if (ext_flags.HasNewCorrDesc)
1799 /* initialize extra correlation package */
1800 NdrCorrelationInitialize(pStubMsg, async_call_data->NdrCorrCache, sizeof(async_call_data->NdrCorrCache), 0);
1803 /* order of phases:
1804 * 1. PROXY_CALCSIZE - calculate the buffer size
1805 * 2. PROXY_GETBUFFER - allocate the buffer
1806 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
1807 * 4. PROXY_SENDRECEIVE - send buffer
1808 * Then in NdrpCompleteAsyncClientCall:
1809 * 1. PROXY_SENDRECEIVE - receive buffer
1810 * 2. PROXY_UNMARHSAL - unmarshal [out] params from buffer
1812 for (phase = PROXY_CALCSIZE; phase <= PROXY_SENDRECEIVE; phase++)
1814 RPC_STATUS status;
1815 TRACE("phase = %d\n", phase);
1816 switch (phase)
1818 case PROXY_GETBUFFER:
1819 /* allocate the buffer */
1820 if (Oif_flags.HasPipes)
1821 /* NdrGetPipeBuffer(...) */
1822 FIXME("pipes not supported yet\n");
1823 else
1825 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
1826 #if 0
1827 NdrNsGetBuffer(pStubMsg, pStubMsg->BufferLength, async_call_data->hBinding);
1828 #else
1829 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
1830 #endif
1831 else
1832 NdrGetBuffer(pStubMsg, pStubMsg->BufferLength, async_call_data->hBinding);
1834 pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1835 status = I_RpcAsyncSetHandle(pRpcMsg, pAsync);
1836 if (status != RPC_S_OK)
1837 RpcRaiseException(status);
1838 break;
1839 case PROXY_SENDRECEIVE:
1840 pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1841 /* send the [in] params only */
1842 if (Oif_flags.HasPipes)
1843 /* NdrPipesSend(...) */
1844 FIXME("pipes not supported yet\n");
1845 else
1847 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
1848 #if 0
1849 NdrNsSend(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
1850 #else
1851 FIXME("using auto handle - call NdrNsSend when it gets implemented\n");
1852 #endif
1853 else
1855 pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
1856 status = I_RpcSend(pStubMsg->RpcMsg);
1857 if (status != RPC_S_OK)
1858 RpcRaiseException(status);
1862 break;
1863 case PROXY_CALCSIZE:
1864 case PROXY_MARSHAL:
1865 if (bV2Format)
1866 client_do_args(pStubMsg, pFormat, phase, pStubMsg->StackTop,
1867 async_call_data->number_of_params, NULL);
1868 else
1869 client_do_args_old_format(pStubMsg, pFormat, phase,
1870 pStubMsg->StackTop, async_call_data->stack_size, NULL,
1871 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT), FALSE);
1872 break;
1873 default:
1874 ERR("shouldn't reach here. phase %d\n", phase);
1875 break;
1879 done:
1880 TRACE("returning 0\n");
1881 RetVal = 0;
1882 return *(CLIENT_CALL_RETURN *)&RetVal;
1885 RPC_STATUS NdrpCompleteAsyncClientCall(RPC_ASYNC_STATE *pAsync, void *Reply)
1887 /* pointer to start of stack where arguments start */
1888 PMIDL_STUB_MESSAGE pStubMsg;
1889 struct async_call_data *async_call_data;
1890 /* the type of pass we are currently doing */
1891 int phase;
1892 /* header for procedure string */
1893 const NDR_PROC_HEADER * pProcHeader;
1894 /* -Oif or -Oicf generated format */
1895 BOOL bV2Format;
1896 RPC_STATUS status = RPC_S_OK;
1898 if (!pAsync->StubInfo)
1899 return RPC_S_INVALID_ASYNC_HANDLE;
1901 async_call_data = pAsync->StubInfo;
1902 pStubMsg = async_call_data->pStubMsg;
1903 pProcHeader = async_call_data->pProcHeader;
1905 bV2Format = (pStubMsg->StubDesc->Version >= 0x20000);
1907 /* order of phases:
1908 * 1. PROXY_CALCSIZE - calculate the buffer size
1909 * 2. PROXY_GETBUFFER - allocate the buffer
1910 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
1911 * 4. PROXY_SENDRECEIVE - send buffer
1912 * Then in NdrpCompleteAsyncClientCall:
1913 * 1. PROXY_SENDRECEIVE - receive buffer
1914 * 2. PROXY_UNMARHSAL - unmarshal [out] params from buffer
1916 for (phase = PROXY_SENDRECEIVE; phase <= PROXY_UNMARSHAL; phase++)
1918 switch (phase)
1920 case PROXY_SENDRECEIVE:
1921 pStubMsg->RpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1922 /* receive the [out] params */
1923 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
1924 #if 0
1925 NdrNsReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
1926 #else
1927 FIXME("using auto handle - call NdrNsReceive when it gets implemented\n");
1928 #endif
1929 else
1931 status = I_RpcReceive(pStubMsg->RpcMsg);
1932 if (status != RPC_S_OK)
1933 goto cleanup;
1934 pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
1935 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
1936 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
1937 pStubMsg->Buffer = pStubMsg->BufferStart;
1940 /* convert strings, floating point values and endianess into our
1941 * preferred format */
1942 #if 0
1943 if ((pStubMsg->RpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1944 NdrConvert(pStubMsg, pFormat);
1945 #endif
1947 break;
1948 case PROXY_UNMARSHAL:
1949 if (bV2Format)
1950 client_do_args(pStubMsg, async_call_data->pParamFormat, phase, pStubMsg->StackTop,
1951 async_call_data->number_of_params, Reply);
1952 else
1953 client_do_args_old_format(pStubMsg, async_call_data->pParamFormat, phase,
1954 pStubMsg->StackTop, async_call_data->stack_size, Reply, FALSE, FALSE);
1955 break;
1956 default:
1957 ERR("shouldn't reach here. phase %d\n", phase);
1958 break;
1962 cleanup:
1963 if (pStubMsg->fHasNewCorrDesc)
1965 /* free extra correlation package */
1966 NdrCorrelationFree(pStubMsg);
1969 /* free the full pointer translation tables */
1970 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1971 NdrFullPointerXlatFree(pStubMsg->FullPtrXlatTables);
1973 /* free marshalling buffer */
1974 NdrFreeBuffer(pStubMsg);
1975 client_free_handle(pStubMsg, pProcHeader, async_call_data->pHandleFormat, async_call_data->hBinding);
1977 I_RpcFree(pStubMsg->StackTop);
1978 I_RpcFree(async_call_data);
1980 TRACE("-- 0x%x\n", status);
1981 return status;
1984 RPCRTAPI LONG RPC_ENTRY NdrAsyncStubCall(struct IRpcStubBuffer* pThis,
1985 struct IRpcChannelBuffer* pChannel, PRPC_MESSAGE pRpcMsg,
1986 DWORD * pdwStubPhase)
1988 FIXME("unimplemented, expect crash!\n");
1989 return 0;