rpcrt4: ndr_stubless.h should include ndrtypes.h as it depends on types it defines.
[wine/gsoc_dplay.git] / dlls / rpcrt4 / ndr_stubless.c
blob5c90aa0e01214522d6968924c01712d5b2972c5b
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/debug.h"
42 #include "wine/rpcfc.h"
44 #include "cpsf.h"
45 #include "ndr_misc.h"
46 #include "ndr_stubless.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
50 #define NDR_TABLE_MASK 127
52 static inline void call_buffer_sizer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
54 NDR_BUFFERSIZE m = NdrBufferSizer[pFormat[0] & NDR_TABLE_MASK];
55 if (m) m(pStubMsg, pMemory, pFormat);
56 else
58 FIXME("format type 0x%x not implemented\n", pFormat[0]);
59 RpcRaiseException(RPC_X_BAD_STUB_DATA);
63 static inline unsigned char *call_marshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
65 NDR_MARSHALL m = NdrMarshaller[pFormat[0] & NDR_TABLE_MASK];
66 if (m) return m(pStubMsg, pMemory, pFormat);
67 else
69 FIXME("format type 0x%x not implemented\n", pFormat[0]);
70 RpcRaiseException(RPC_X_BAD_STUB_DATA);
71 return NULL;
75 static inline unsigned char *call_unmarshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char **ppMemory, PFORMAT_STRING pFormat, unsigned char fMustAlloc)
77 NDR_UNMARSHALL m = NdrUnmarshaller[pFormat[0] & NDR_TABLE_MASK];
78 if (m) return m(pStubMsg, ppMemory, pFormat, fMustAlloc);
79 else
81 FIXME("format type 0x%x not implemented\n", pFormat[0]);
82 RpcRaiseException(RPC_X_BAD_STUB_DATA);
83 return NULL;
87 static inline void call_freer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
89 NDR_FREE m = NdrFreer[pFormat[0] & NDR_TABLE_MASK];
90 if (m) m(pStubMsg, pMemory, pFormat);
93 static inline unsigned long call_memory_sizer(PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat)
95 NDR_MEMORYSIZE m = NdrMemorySizer[pFormat[0] & NDR_TABLE_MASK];
96 if (m)
98 unsigned char *saved_buffer = pStubMsg->Buffer;
99 unsigned long ret;
100 int saved_ignore_embedded_pointers = pStubMsg->IgnoreEmbeddedPointers;
101 pStubMsg->MemorySize = 0;
102 pStubMsg->IgnoreEmbeddedPointers = 1;
103 ret = m(pStubMsg, pFormat);
104 pStubMsg->IgnoreEmbeddedPointers = saved_ignore_embedded_pointers;
105 pStubMsg->Buffer = saved_buffer;
106 return ret;
108 else
110 FIXME("format type 0x%x not implemented\n", pFormat[0]);
111 RpcRaiseException(RPC_X_BAD_STUB_DATA);
112 return 0;
116 #define STUBLESS_UNMARSHAL 1
117 #define STUBLESS_CALLSERVER 2
118 #define STUBLESS_CALCSIZE 3
119 #define STUBLESS_GETBUFFER 4
120 #define STUBLESS_MARSHAL 5
121 #define STUBLESS_FREE 6
123 void WINAPI NdrRpcSmSetClientToOsf(PMIDL_STUB_MESSAGE pMessage)
125 #if 0 /* these functions are not defined yet */
126 pMessage->pfnAllocate = NdrRpcSmClientAllocate;
127 pMessage->pfnFree = NdrRpcSmClientFree;
128 #endif
131 static void WINAPI dump_RPC_FC_PROC_PF(PARAM_ATTRIBUTES param_attributes)
133 if (param_attributes.MustSize) TRACE(" MustSize");
134 if (param_attributes.MustFree) TRACE(" MustFree");
135 if (param_attributes.IsPipe) TRACE(" IsPipe");
136 if (param_attributes.IsIn) TRACE(" IsIn");
137 if (param_attributes.IsOut) TRACE(" IsOut");
138 if (param_attributes.IsReturn) TRACE(" IsReturn");
139 if (param_attributes.IsBasetype) TRACE(" IsBasetype");
140 if (param_attributes.IsByValue) TRACE(" IsByValue");
141 if (param_attributes.IsSimpleRef) TRACE(" IsSimpleRef");
142 if (param_attributes.IsDontCallFreeInst) TRACE(" IsDontCallFreeInst");
143 if (param_attributes.SaveForAsyncFinish) TRACE(" SaveForAsyncFinish");
144 if (param_attributes.ServerAllocSize) TRACE(" ServerAllocSize = %d", param_attributes.ServerAllocSize * 8);
147 static void WINAPI dump_INTERPRETER_OPT_FLAGS(INTERPRETER_OPT_FLAGS Oi2Flags)
149 if (Oi2Flags.ServerMustSize) TRACE(" ServerMustSize");
150 if (Oi2Flags.ClientMustSize) TRACE(" ClientMustSize");
151 if (Oi2Flags.HasReturn) TRACE(" HasReturn");
152 if (Oi2Flags.HasPipes) TRACE(" HasPipes");
153 if (Oi2Flags.Unused) TRACE(" Unused");
154 if (Oi2Flags.HasAsyncUuid) TRACE(" HasAsyncUuid");
155 if (Oi2Flags.HasExtensions) TRACE(" HasExtensions");
156 if (Oi2Flags.HasAsyncHandle) TRACE(" HasAsyncHandle");
157 TRACE("\n");
160 #define ARG_FROM_OFFSET(stubMsg, offset) ((stubMsg).StackTop + (offset))
162 static PFORMAT_STRING client_get_handle(
163 PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
164 PFORMAT_STRING pFormat, handle_t *phBinding)
166 /* binding */
167 switch (pProcHeader->handle_type)
169 /* explicit binding: parse additional section */
170 case RPC_FC_BIND_EXPLICIT:
171 switch (*pFormat) /* handle_type */
173 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
175 const NDR_EHD_PRIMITIVE *pDesc = (const NDR_EHD_PRIMITIVE *)pFormat;
177 TRACE("Explicit primitive handle @ %d\n", pDesc->offset);
179 if (pDesc->flag) /* pointer to binding */
180 *phBinding = **(handle_t **)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
181 else
182 *phBinding = *(handle_t *)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
183 return pFormat + sizeof(NDR_EHD_PRIMITIVE);
185 case RPC_FC_BIND_GENERIC: /* explicit generic */
187 const NDR_EHD_GENERIC *pDesc = (const NDR_EHD_GENERIC *)pFormat;
188 void *pObject = NULL;
189 void *pArg;
190 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
192 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
194 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
195 pArg = *(void **)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
196 else
197 pArg = (void *)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
198 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
199 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
200 *phBinding = pGenPair->pfnBind(pObject);
201 return pFormat + sizeof(NDR_EHD_GENERIC);
203 case RPC_FC_BIND_CONTEXT: /* explicit context */
205 const NDR_EHD_CONTEXT *pDesc = (const NDR_EHD_CONTEXT *)pFormat;
206 NDR_CCONTEXT context_handle;
207 TRACE("Explicit bind context\n");
208 if (pDesc->flags & HANDLE_PARAM_IS_VIA_PTR)
210 TRACE("\tHANDLE_PARAM_IS_VIA_PTR\n");
211 context_handle = **(NDR_CCONTEXT **)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
213 else
214 context_handle = *(NDR_CCONTEXT *)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
215 if ((pDesc->flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL) &&
216 !context_handle)
218 ERR("null context handle isn't allowed\n");
219 RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);
220 return NULL;
222 *phBinding = NDRCContextBinding(context_handle);
223 /* FIXME: should we store this structure in stubMsg.pContext? */
224 return pFormat + sizeof(NDR_EHD_CONTEXT);
226 default:
227 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
228 RpcRaiseException(RPC_X_BAD_STUB_DATA);
230 break;
231 case RPC_FC_BIND_GENERIC: /* implicit generic */
232 FIXME("RPC_FC_BIND_GENERIC\n");
233 RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
234 break;
235 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
236 TRACE("Implicit primitive handle\n");
237 *phBinding = *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pPrimitiveHandle;
238 break;
239 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
240 FIXME("RPC_FC_CALLBACK_HANDLE\n");
241 break;
242 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
243 /* strictly speaking, it isn't necessary to set hBinding here
244 * since it isn't actually used (hence the automatic in its name),
245 * but then why does MIDL generate a valid entry in the
246 * MIDL_STUB_DESC for it? */
247 TRACE("Implicit auto handle\n");
248 *phBinding = *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle;
249 break;
250 default:
251 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
252 RpcRaiseException(RPC_X_BAD_STUB_DATA);
254 return pFormat;
257 static void client_free_handle(
258 PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
259 PFORMAT_STRING pFormat, handle_t hBinding)
261 /* binding */
262 switch (pProcHeader->handle_type)
264 /* explicit binding: parse additional section */
265 case RPC_FC_BIND_EXPLICIT:
266 switch (*pFormat) /* handle_type */
268 case RPC_FC_BIND_GENERIC: /* explicit generic */
270 const NDR_EHD_GENERIC *pDesc = (const NDR_EHD_GENERIC *)pFormat;
271 void *pObject = NULL;
272 void *pArg;
273 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
275 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
277 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
278 pArg = *(void **)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
279 else
280 pArg = (void *)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
281 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
282 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
283 pGenPair->pfnUnbind(pObject, hBinding);
284 break;
286 case RPC_FC_BIND_CONTEXT: /* explicit context */
287 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
288 break;
289 default:
290 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
291 RpcRaiseException(RPC_X_BAD_STUB_DATA);
293 break;
294 case RPC_FC_BIND_GENERIC: /* implicit generic */
295 FIXME("RPC_FC_BIND_GENERIC\n");
296 RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
297 break;
298 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
299 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
300 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
301 break;
302 default:
303 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
304 RpcRaiseException(RPC_X_BAD_STUB_DATA);
308 static void client_do_args(PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat,
309 int phase, unsigned short number_of_params, unsigned char *pRetVal)
311 /* current format string offset */
312 int current_offset = 0;
313 /* current stack offset */
314 unsigned short current_stack_offset = 0;
315 /* counter */
316 unsigned short i;
318 for (i = 0; i < number_of_params; i++)
320 const NDR_PARAM_OIF_BASETYPE *pParam =
321 (const NDR_PARAM_OIF_BASETYPE *)&pFormat[current_offset];
322 unsigned char * pArg;
324 current_stack_offset = pParam->stack_offset;
325 pArg = ARG_FROM_OFFSET(*pStubMsg, current_stack_offset);
327 TRACE("param[%d]: new format\n", i);
328 TRACE("\tparam_attributes:"); dump_RPC_FC_PROC_PF(pParam->param_attributes); TRACE("\n");
329 TRACE("\tstack_offset: 0x%x\n", current_stack_offset);
330 TRACE("\tmemory addr (before): %p\n", pArg);
332 if (pParam->param_attributes.IsBasetype)
334 const unsigned char * pTypeFormat =
335 &pParam->type_format_char;
337 if (pParam->param_attributes.IsSimpleRef)
338 pArg = *(unsigned char **)pArg;
340 TRACE("\tbase type: 0x%02x\n", *pTypeFormat);
342 switch (phase)
344 case PROXY_CALCSIZE:
345 if (pParam->param_attributes.IsIn)
346 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
347 break;
348 case PROXY_MARSHAL:
349 if (pParam->param_attributes.IsIn)
350 call_marshaller(pStubMsg, pArg, pTypeFormat);
351 break;
352 case PROXY_UNMARSHAL:
353 if (pParam->param_attributes.IsOut)
355 if (pParam->param_attributes.IsReturn)
356 call_unmarshaller(pStubMsg, &pRetVal, pTypeFormat, 0);
357 else
358 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
359 TRACE("pRetVal = %p\n", pRetVal);
361 break;
362 default:
363 RpcRaiseException(RPC_S_INTERNAL_ERROR);
366 current_offset += sizeof(NDR_PARAM_OIF_BASETYPE);
368 else
370 const NDR_PARAM_OIF_OTHER *pParamOther =
371 (const NDR_PARAM_OIF_OTHER *)&pFormat[current_offset];
373 const unsigned char * pTypeFormat =
374 &(pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset]);
376 /* if a simple ref pointer then we have to do the
377 * check for the pointer being non-NULL. */
378 if (pParam->param_attributes.IsSimpleRef)
380 if (!*(unsigned char **)pArg)
381 RpcRaiseException(RPC_X_NULL_REF_POINTER);
384 TRACE("\tcomplex type: 0x%02x\n", *pTypeFormat);
386 switch (phase)
388 case PROXY_CALCSIZE:
389 if (pParam->param_attributes.IsIn)
391 if (pParam->param_attributes.IsByValue)
392 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
393 else
394 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
396 break;
397 case PROXY_MARSHAL:
398 if (pParam->param_attributes.IsIn)
400 if (pParam->param_attributes.IsByValue)
401 call_marshaller(pStubMsg, pArg, pTypeFormat);
402 else
403 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
405 break;
406 case PROXY_UNMARSHAL:
407 if (pParam->param_attributes.IsOut)
409 if (pParam->param_attributes.IsReturn)
410 call_unmarshaller(pStubMsg, &pRetVal, pTypeFormat, 0);
411 else if (pParam->param_attributes.IsByValue)
412 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
413 else
414 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
416 break;
417 default:
418 RpcRaiseException(RPC_S_INTERNAL_ERROR);
421 current_offset += sizeof(NDR_PARAM_OIF_OTHER);
423 TRACE("\tmemory addr (after): %p\n", pArg);
427 static void client_do_args_old_format(PMIDL_STUB_MESSAGE pStubMsg,
428 PFORMAT_STRING pFormat, int phase, unsigned short stack_size,
429 unsigned char *pRetVal, BOOL object_proc)
431 /* current format string offset */
432 int current_offset = 0;
433 /* current stack offset */
434 unsigned short current_stack_offset = 0;
435 /* counter */
436 unsigned short i;
438 /* NOTE: V1 style format does't terminate on the number_of_params
439 * condition as it doesn't have this attribute. Instead it
440 * terminates when the stack size given in the header is exceeded.
442 for (i = 0; TRUE; i++)
444 const NDR_PARAM_OI_BASETYPE *pParam =
445 (const NDR_PARAM_OI_BASETYPE *)&pFormat[current_offset];
446 /* note: current_stack_offset starts after the This pointer
447 * if present, so adjust this */
448 unsigned short current_stack_offset_adjusted = current_stack_offset +
449 (object_proc ? sizeof(void *) : 0);
450 unsigned char * pArg = ARG_FROM_OFFSET(*pStubMsg, current_stack_offset_adjusted);
452 /* no more parameters; exit loop */
453 if (current_stack_offset_adjusted >= stack_size)
454 break;
456 TRACE("param[%d]: old format\n", i);
457 TRACE("\tparam_direction: 0x%x\n", pParam->param_direction);
458 TRACE("\tstack_offset: 0x%x\n", current_stack_offset_adjusted);
459 TRACE("\tmemory addr (before): %p\n", pArg);
461 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE ||
462 pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
464 const unsigned char * pTypeFormat =
465 &pParam->type_format_char;
467 TRACE("\tbase type 0x%02x\n", *pTypeFormat);
469 switch (phase)
471 case PROXY_CALCSIZE:
472 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
473 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
474 break;
475 case PROXY_MARSHAL:
476 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
477 call_marshaller(pStubMsg, pArg, pTypeFormat);
478 break;
479 case PROXY_UNMARSHAL:
480 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
482 if (pParam->param_direction & RPC_FC_RETURN_PARAM)
483 call_unmarshaller(pStubMsg, (unsigned char **)pRetVal, pTypeFormat, 0);
484 else
485 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
487 break;
488 default:
489 RpcRaiseException(RPC_S_INTERNAL_ERROR);
492 current_stack_offset += call_memory_sizer(pStubMsg, pTypeFormat);
493 current_offset += sizeof(NDR_PARAM_OI_BASETYPE);
495 else
497 const NDR_PARAM_OI_OTHER *pParamOther =
498 (const NDR_PARAM_OI_OTHER *)&pFormat[current_offset];
500 const unsigned char *pTypeFormat =
501 &pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset];
503 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
505 switch (phase)
507 case PROXY_CALCSIZE:
508 if (pParam->param_direction == RPC_FC_IN_PARAM ||
509 pParam->param_direction & RPC_FC_IN_OUT_PARAM)
510 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
511 break;
512 case PROXY_MARSHAL:
513 if (pParam->param_direction == RPC_FC_IN_PARAM ||
514 pParam->param_direction & RPC_FC_IN_OUT_PARAM)
515 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
516 break;
517 case PROXY_UNMARSHAL:
518 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
519 pParam->param_direction == RPC_FC_OUT_PARAM)
520 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
521 else if (pParam->param_direction == RPC_FC_RETURN_PARAM)
522 call_unmarshaller(pStubMsg, (unsigned char **)pRetVal, pTypeFormat, 0);
523 break;
524 default:
525 RpcRaiseException(RPC_S_INTERNAL_ERROR);
528 current_stack_offset += pParamOther->stack_size * sizeof(INT);
529 current_offset += sizeof(NDR_PARAM_OI_OTHER);
531 TRACE("\tmemory addr (after): %p\n", pArg);
535 /* the return type should be CLIENT_CALL_RETURN, but this is incompatible
536 * with the way gcc returns structures. "void *" should be the largest type
537 * that MIDL should allow you to return anyway */
538 LONG_PTR WINAPIV NdrClientCall2(PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat, ...)
540 /* pointer to start of stack where arguments start */
541 RPC_MESSAGE rpcMsg;
542 MIDL_STUB_MESSAGE stubMsg;
543 handle_t hBinding = NULL;
544 /* procedure number */
545 unsigned short procedure_number;
546 /* size of stack */
547 unsigned short stack_size;
548 /* number of parameters. optional for client to give it to us */
549 unsigned char number_of_params = ~0;
550 /* cache of Oif_flags from v2 procedure header */
551 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
552 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
553 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
554 /* the type of pass we are currently doing */
555 int phase;
556 /* header for procedure string */
557 const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
558 /* -Oif or -Oicf generated format */
559 BOOL bV2Format = FALSE;
560 /* the value to return to the client from the remote procedure */
561 LONG_PTR RetVal = 0;
562 /* the pointer to the object when in OLE mode */
563 void * This = NULL;
564 PFORMAT_STRING pHandleFormat;
566 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
568 /* Later NDR language versions probably won't be backwards compatible */
569 if (pStubDesc->Version > 0x50002)
571 FIXME("Incompatible stub description version: 0x%x\n", pStubDesc->Version);
572 RpcRaiseException(RPC_X_WRONG_STUB_VERSION);
575 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
577 const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
578 stack_size = pProcHeader->stack_size;
579 procedure_number = pProcHeader->proc_num;
580 pFormat += sizeof(NDR_PROC_HEADER_RPC);
582 else
584 stack_size = pProcHeader->stack_size;
585 procedure_number = pProcHeader->proc_num;
586 pFormat += sizeof(NDR_PROC_HEADER);
588 TRACE("stack size: 0x%x\n", stack_size);
589 TRACE("proc num: %d\n", procedure_number);
591 /* create the full pointer translation tables, if requested */
592 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
593 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
595 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
597 /* object is always the first argument */
598 This = **(void *const **)(&pFormat+1);
599 NdrProxyInitialize(This, &rpcMsg, &stubMsg, pStubDesc, procedure_number);
601 else
602 NdrClientInitializeNew(&rpcMsg, &stubMsg, pStubDesc, procedure_number);
604 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
605 TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
607 /* needed for conformance of top-level objects */
608 #ifdef __i386__
609 stubMsg.StackTop = *(unsigned char **)(&pFormat+1);
610 #else
611 # warning Stack not retrieved for your CPU architecture
612 #endif
614 pHandleFormat = pFormat;
616 /* we only need a handle if this isn't an object method */
617 if (!(pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT))
619 pFormat = client_get_handle(&stubMsg, pProcHeader, pHandleFormat, &hBinding);
620 if (!pFormat) return 0;
623 bV2Format = (pStubDesc->Version >= 0x20000);
625 if (bV2Format)
627 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
628 (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
630 Oif_flags = pOIFHeader->Oi2Flags;
631 number_of_params = pOIFHeader->number_of_params;
633 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
636 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
638 if (Oif_flags.HasExtensions)
640 const NDR_PROC_HEADER_EXTS *pExtensions =
641 (const NDR_PROC_HEADER_EXTS *)pFormat;
642 ext_flags = pExtensions->Flags2;
643 pFormat += pExtensions->Size;
646 stubMsg.BufferLength = 0;
648 /* store the RPC flags away */
649 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
650 rpcMsg.RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
652 /* use alternate memory allocation routines */
653 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
654 NdrRpcSmSetClientToOsf(&stubMsg);
656 if (Oif_flags.HasPipes)
658 FIXME("pipes not supported yet\n");
659 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
660 /* init pipes package */
661 /* NdrPipesInitialize(...) */
663 if (ext_flags.HasNewCorrDesc)
665 /* initialize extra correlation package */
666 FIXME("new correlation description not implemented\n");
667 stubMsg.fHasNewCorrDesc = TRUE;
670 /* order of phases:
671 * 1. PROXY_CALCSIZE - calculate the buffer size
672 * 2. PROXY_GETBUFFER - allocate the buffer
673 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
674 * 4. PROXY_SENDRECEIVE - send/receive buffer
675 * 5. PROXY_UNMARHSAL - unmarshal [out] params from buffer
677 for (phase = PROXY_CALCSIZE; phase <= PROXY_UNMARSHAL; phase++)
679 TRACE("phase = %d\n", phase);
680 switch (phase)
682 case PROXY_GETBUFFER:
683 /* allocate the buffer */
684 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
685 NdrProxyGetBuffer(This, &stubMsg);
686 else if (Oif_flags.HasPipes)
687 /* NdrGetPipeBuffer(...) */
688 FIXME("pipes not supported yet\n");
689 else
691 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
692 #if 0
693 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
694 #else
695 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
696 #endif
697 else
698 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
700 break;
701 case PROXY_SENDRECEIVE:
702 /* send the [in] params and receive the [out] and [retval]
703 * params */
704 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
705 NdrProxySendReceive(This, &stubMsg);
706 else if (Oif_flags.HasPipes)
707 /* NdrPipesSendReceive(...) */
708 FIXME("pipes not supported yet\n");
709 else
711 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
712 #if 0
713 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
714 #else
715 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
716 #endif
717 else
718 NdrSendReceive(&stubMsg, stubMsg.Buffer);
721 /* convert strings, floating point values and endianess into our
722 * preferred format */
723 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
724 NdrConvert(&stubMsg, pFormat);
726 break;
727 case PROXY_CALCSIZE:
728 case PROXY_MARSHAL:
729 case PROXY_UNMARSHAL:
730 if (bV2Format)
731 client_do_args(&stubMsg, pFormat, phase, number_of_params,
732 (unsigned char *)&RetVal);
733 else
734 client_do_args_old_format(&stubMsg, pFormat, phase, stack_size,
735 (unsigned char *)&RetVal,
736 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT));
737 break;
738 default:
739 ERR("shouldn't reach here. phase %d\n", phase);
740 break;
744 if (ext_flags.HasNewCorrDesc)
746 /* free extra correlation package */
747 /* NdrCorrelationFree(&stubMsg); */
750 if (Oif_flags.HasPipes)
752 /* NdrPipesDone(...) */
755 /* free the full pointer translation tables */
756 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
757 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
759 /* free marshalling buffer */
760 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
761 NdrProxyFreeBuffer(This, &stubMsg);
762 else
764 NdrFreeBuffer(&stubMsg);
765 client_free_handle(&stubMsg, pProcHeader, pHandleFormat, hBinding);
768 TRACE("RetVal = 0x%lx\n", RetVal);
770 return RetVal;
773 /* calls a function with the specificed arguments, restoring the stack
774 * properly afterwards as we don't know the calling convention of the
775 * function */
776 #if defined __i386__ && defined _MSC_VER
777 __declspec(naked) LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size)
779 __asm
781 push ebp
782 push edi ; Save registers
783 push esi
784 mov ebp, esp
785 mov eax, [ebp+16] ; Get stack size
786 sub esp, eax ; Make room in stack for arguments
787 mov edi, esp
788 mov ecx, eax
789 mov esi, [ebp+12]
790 shr ecx, 2
792 rep movsd ; Copy dword blocks
793 call [ebp+8] ; Call function
794 lea esp, [ebp-8] ; Restore stack
795 pop esi ; Restore registers
796 pop edi
797 pop ebp
801 #elif defined __i386__ && defined __GNUC__
802 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
803 __ASM_GLOBAL_FUNC(call_server_func,
804 "pushl %ebp\n\t"
805 "movl %esp, %ebp\n\t"
806 "pushl %edi\n\t" /* Save registers */
807 "pushl %esi\n\t"
808 "movl 16(%ebp), %eax\n\t" /* Get stack size */
809 "subl %eax, %esp\n\t" /* Make room in stack for arguments */
810 "andl $~15, %esp\n\t" /* Make sure stack has 16-byte alignment for Mac OS X */
811 "movl %esp, %edi\n\t"
812 "movl %eax, %ecx\n\t"
813 "movl 12(%ebp), %esi\n\t"
814 "shrl $2, %ecx\n\t" /* divide by 4 */
815 "cld\n\t"
816 "rep; movsl\n\t" /* Copy dword blocks */
817 "call *8(%ebp)\n\t" /* Call function */
818 "leal -8(%ebp), %esp\n\t" /* Restore stack */
819 "popl %esi\n\t" /* Restore registers */
820 "popl %edi\n\t"
821 "popl %ebp\n\t"
822 "ret\n" )
823 #else
824 #warning call_server_func not implemented for your architecture
825 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned short stack_size)
827 FIXME("Not implemented for your architecture\n");
828 return 0;
830 #endif
832 static DWORD calc_arg_size(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat)
834 DWORD size;
835 switch(*pFormat)
837 case RPC_FC_STRUCT:
838 size = *(const WORD*)(pFormat + 2);
839 break;
840 case RPC_FC_CARRAY:
841 size = *(const WORD*)(pFormat + 2);
842 ComputeConformance(pStubMsg, NULL, pFormat + 4, 0);
843 size *= pStubMsg->MaxCount;
844 break;
845 case RPC_FC_SMFARRAY:
846 size = *(const WORD*)(pFormat + 2);
847 break;
848 case RPC_FC_LGFARRAY:
849 size = *(const DWORD*)(pFormat + 2);
850 break;
851 default:
852 FIXME("Unhandled type %02x\n", *pFormat);
853 /* fallthrough */
854 case RPC_FC_RP:
855 size = sizeof(void *);
856 break;
858 return size;
861 static LONG_PTR *stub_do_args(MIDL_STUB_MESSAGE *pStubMsg,
862 PFORMAT_STRING pFormat, int phase,
863 unsigned char *args,
864 unsigned short number_of_params)
866 /* counter */
867 unsigned short i;
868 /* current format string offset */
869 int current_offset = 0;
870 /* current stack offset */
871 unsigned short current_stack_offset = 0;
872 /* location to put retval into */
873 LONG_PTR *retval_ptr = NULL;
875 for (i = 0; i < number_of_params; i++)
877 const NDR_PARAM_OIF_BASETYPE *pParam =
878 (const NDR_PARAM_OIF_BASETYPE *)&pFormat[current_offset];
879 unsigned char *pArg;
881 current_stack_offset = pParam->stack_offset;
882 pArg = (unsigned char *)(args+current_stack_offset);
884 TRACE("param[%d]: new format\n", i);
885 TRACE("\tparam_attributes:"); dump_RPC_FC_PROC_PF(pParam->param_attributes); TRACE("\n");
886 TRACE("\tstack_offset: 0x%x\n", current_stack_offset);
887 TRACE("\tmemory addr (before): %p -> %p\n", pArg, *(unsigned char **)pArg);
889 if (pParam->param_attributes.IsBasetype)
891 const unsigned char *pTypeFormat =
892 &pParam->type_format_char;
894 TRACE("\tbase type: 0x%02x\n", *pTypeFormat);
896 switch (phase)
898 case STUBLESS_MARSHAL:
899 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
901 if (pParam->param_attributes.IsSimpleRef)
902 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
903 else
904 call_marshaller(pStubMsg, pArg, pTypeFormat);
906 break;
907 case STUBLESS_FREE:
908 if (pParam->param_attributes.ServerAllocSize)
909 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
910 break;
911 case STUBLESS_UNMARSHAL:
912 if (pParam->param_attributes.ServerAllocSize)
913 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
914 pParam->param_attributes.ServerAllocSize * 8);
916 if (pParam->param_attributes.IsIn)
918 if (pParam->param_attributes.IsSimpleRef)
919 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
920 else
921 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
924 /* make a note of the address of the return value parameter for later */
925 if (pParam->param_attributes.IsReturn)
926 retval_ptr = (LONG_PTR *)pArg;
928 break;
929 case STUBLESS_CALCSIZE:
930 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
932 if (pParam->param_attributes.IsSimpleRef)
933 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
934 else
935 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
937 break;
938 default:
939 RpcRaiseException(RPC_S_INTERNAL_ERROR);
942 current_offset += sizeof(NDR_PARAM_OIF_BASETYPE);
944 else
946 const NDR_PARAM_OIF_OTHER *pParamOther =
947 (const NDR_PARAM_OIF_OTHER *)&pFormat[current_offset];
949 const unsigned char * pTypeFormat =
950 &(pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset]);
952 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
954 switch (phase)
956 case STUBLESS_MARSHAL:
957 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
959 if (pParam->param_attributes.IsByValue)
960 call_marshaller(pStubMsg, pArg, pTypeFormat);
961 else
962 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
964 break;
965 case STUBLESS_FREE:
966 if (pParam->param_attributes.MustFree)
968 if (pParam->param_attributes.IsByValue)
969 call_freer(pStubMsg, pArg, pTypeFormat);
970 else
971 call_freer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
974 if (pParam->param_attributes.IsOut &&
975 !pParam->param_attributes.IsIn &&
976 !pParam->param_attributes.IsByValue &&
977 !pParam->param_attributes.ServerAllocSize)
979 pStubMsg->pfnFree(*(void **)pArg);
982 if (pParam->param_attributes.ServerAllocSize)
983 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
984 /* FIXME: call call_freer here for IN types with MustFree set */
985 break;
986 case STUBLESS_UNMARSHAL:
987 if (pParam->param_attributes.ServerAllocSize)
988 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
989 pParam->param_attributes.ServerAllocSize * 8);
991 if (pParam->param_attributes.IsIn)
993 if (pParam->param_attributes.IsByValue)
994 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
995 else
996 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
998 else if (pParam->param_attributes.IsOut &&
999 !pParam->param_attributes.ServerAllocSize &&
1000 !pParam->param_attributes.IsByValue)
1002 DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1004 if(size)
1006 *(void **)pArg = NdrAllocate(pStubMsg, size);
1007 memset(*(void **)pArg, 0, size);
1010 break;
1011 case STUBLESS_CALCSIZE:
1012 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1014 if (pParam->param_attributes.IsByValue)
1015 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1016 else
1017 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1019 break;
1020 default:
1021 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1024 current_offset += sizeof(NDR_PARAM_OIF_OTHER);
1026 TRACE("\tmemory addr (after): %p -> %p\n", pArg, *(unsigned char **)pArg);
1029 return retval_ptr;
1032 static LONG_PTR *stub_do_old_args(MIDL_STUB_MESSAGE *pStubMsg,
1033 PFORMAT_STRING pFormat, int phase,
1034 unsigned char *args,
1035 unsigned short stack_size, BOOL object)
1037 /* counter */
1038 unsigned short i;
1039 /* current format string offset */
1040 int current_offset = 0;
1041 /* current stack offset */
1042 unsigned short current_stack_offset = 0;
1043 /* location to put retval into */
1044 LONG_PTR *retval_ptr = NULL;
1046 for (i = 0; TRUE; i++)
1048 const NDR_PARAM_OI_BASETYPE *pParam =
1049 (const NDR_PARAM_OI_BASETYPE *)&pFormat[current_offset];
1050 /* note: current_stack_offset starts after the This pointer
1051 * if present, so adjust this */
1052 unsigned short current_stack_offset_adjusted = current_stack_offset +
1053 (object ? sizeof(void *) : 0);
1054 unsigned char *pArg = (unsigned char *)(args+current_stack_offset_adjusted);
1056 /* no more parameters; exit loop */
1057 if (current_stack_offset_adjusted >= stack_size)
1058 break;
1060 TRACE("param[%d]: old format\n", i);
1061 TRACE("\tparam_direction: 0x%x\n", pParam->param_direction);
1062 TRACE("\tstack_offset: 0x%x\n", current_stack_offset_adjusted);
1064 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE ||
1065 pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1067 const unsigned char *pTypeFormat =
1068 &pParam->type_format_char;
1070 TRACE("\tbase type 0x%02x\n", *pTypeFormat);
1072 switch (phase)
1074 case STUBLESS_MARSHAL:
1075 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1076 call_marshaller(pStubMsg, pArg, pTypeFormat);
1077 break;
1078 case STUBLESS_FREE:
1079 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1080 call_freer(pStubMsg, pArg, pTypeFormat);
1081 break;
1082 case STUBLESS_UNMARSHAL:
1083 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1084 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1085 else if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1086 retval_ptr = (LONG_PTR *)pArg;
1087 break;
1088 case STUBLESS_CALCSIZE:
1089 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1090 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1091 break;
1092 default:
1093 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1096 current_stack_offset += call_memory_sizer(pStubMsg, pTypeFormat);
1097 current_offset += sizeof(NDR_PARAM_OI_BASETYPE);
1099 else
1101 const NDR_PARAM_OI_OTHER *pParamOther =
1102 (const NDR_PARAM_OI_OTHER *)&pFormat[current_offset];
1104 const unsigned char * pTypeFormat =
1105 &pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset];
1107 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1109 switch (phase)
1111 case STUBLESS_MARSHAL:
1112 if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1113 pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1114 pParam->param_direction == RPC_FC_RETURN_PARAM)
1115 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1116 break;
1117 case STUBLESS_FREE:
1118 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1119 pParam->param_direction == RPC_FC_IN_PARAM)
1120 call_freer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1121 else if (pParam->param_direction == RPC_FC_OUT_PARAM)
1122 pStubMsg->pfnFree(*(void **)pArg);
1123 break;
1124 case STUBLESS_UNMARSHAL:
1125 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1126 pParam->param_direction == RPC_FC_IN_PARAM)
1127 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1128 else if (pParam->param_direction == RPC_FC_RETURN_PARAM)
1129 retval_ptr = (LONG_PTR *)pArg;
1130 else if (pParam->param_direction == RPC_FC_OUT_PARAM)
1132 DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1134 if(size)
1136 *(void **)pArg = NdrAllocate(pStubMsg, size);
1137 memset(*(void **)pArg, 0, size);
1140 break;
1141 case STUBLESS_CALCSIZE:
1142 if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1143 pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1144 pParam->param_direction == RPC_FC_RETURN_PARAM)
1145 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1146 break;
1147 default:
1148 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1151 current_stack_offset += pParamOther->stack_size * sizeof(INT);
1152 current_offset += sizeof(NDR_PARAM_OI_OTHER);
1156 return retval_ptr;
1159 /***********************************************************************
1160 * NdrStubCall2 [RPCRT4.@]
1162 * Unmarshals [in] parameters, calls either a method in an object or a server
1163 * function, marshals any [out] parameters and frees any allocated data.
1165 * NOTES
1166 * Used by stubless MIDL-generated code.
1168 LONG WINAPI NdrStubCall2(
1169 struct IRpcStubBuffer * pThis,
1170 struct IRpcChannelBuffer * pChannel,
1171 PRPC_MESSAGE pRpcMsg,
1172 DWORD * pdwStubPhase)
1174 const MIDL_SERVER_INFO *pServerInfo;
1175 const MIDL_STUB_DESC *pStubDesc;
1176 PFORMAT_STRING pFormat;
1177 MIDL_STUB_MESSAGE stubMsg;
1178 /* pointer to start of stack to pass into stub implementation */
1179 unsigned char * args;
1180 /* size of stack */
1181 unsigned short stack_size;
1182 /* number of parameters. optional for client to give it to us */
1183 unsigned char number_of_params = ~0;
1184 /* cache of Oif_flags from v2 procedure header */
1185 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1186 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1187 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1188 /* the type of pass we are currently doing */
1189 int phase;
1190 /* header for procedure string */
1191 const NDR_PROC_HEADER *pProcHeader;
1192 /* offset in format string for start of params */
1193 int parameter_start_offset;
1194 /* current format string offset */
1195 int current_offset;
1196 /* -Oif or -Oicf generated format */
1197 BOOL bV2Format = FALSE;
1198 /* location to put retval into */
1199 LONG_PTR *retval_ptr = NULL;
1201 TRACE("pThis %p, pChannel %p, pRpcMsg %p, pdwStubPhase %p\n", pThis, pChannel, pRpcMsg, pdwStubPhase);
1203 if (pThis)
1204 pServerInfo = CStdStubBuffer_GetServerInfo(pThis);
1205 else
1206 pServerInfo = ((RPC_SERVER_INTERFACE *)pRpcMsg->RpcInterfaceInformation)->InterpreterInfo;
1208 pStubDesc = pServerInfo->pStubDesc;
1209 pFormat = pServerInfo->ProcString + pServerInfo->FmtStringOffset[pRpcMsg->ProcNum];
1210 pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1212 /* Later NDR language versions probably won't be backwards compatible */
1213 if (pStubDesc->Version > 0x50002)
1215 FIXME("Incompatible stub description version: 0x%x\n", pStubDesc->Version);
1216 RpcRaiseException(RPC_X_WRONG_STUB_VERSION);
1219 /* create the full pointer translation tables, if requested */
1220 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1221 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_SERVER);
1223 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1225 const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1226 stack_size = pProcHeader->stack_size;
1227 current_offset = sizeof(NDR_PROC_HEADER_RPC);
1230 else
1232 stack_size = pProcHeader->stack_size;
1233 current_offset = sizeof(NDR_PROC_HEADER);
1236 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1238 /* binding */
1239 switch (pProcHeader->handle_type)
1241 /* explicit binding: parse additional section */
1242 case RPC_FC_BIND_EXPLICIT:
1243 switch (pFormat[current_offset]) /* handle_type */
1245 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
1246 current_offset += sizeof(NDR_EHD_PRIMITIVE);
1247 break;
1248 case RPC_FC_BIND_GENERIC: /* explicit generic */
1249 current_offset += sizeof(NDR_EHD_GENERIC);
1250 break;
1251 case RPC_FC_BIND_CONTEXT: /* explicit context */
1252 current_offset += sizeof(NDR_EHD_CONTEXT);
1253 break;
1254 default:
1255 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1256 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1258 break;
1259 case RPC_FC_BIND_GENERIC: /* implicit generic */
1260 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
1261 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
1262 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
1263 break;
1264 default:
1265 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1266 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1269 bV2Format = (pStubDesc->Version >= 0x20000);
1271 if (bV2Format)
1273 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
1274 (const NDR_PROC_PARTIAL_OIF_HEADER *)&pFormat[current_offset];
1276 Oif_flags = pOIFHeader->Oi2Flags;
1277 number_of_params = pOIFHeader->number_of_params;
1279 current_offset += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1282 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
1284 if (Oif_flags.HasExtensions)
1286 const NDR_PROC_HEADER_EXTS *pExtensions =
1287 (const NDR_PROC_HEADER_EXTS *)&pFormat[current_offset];
1288 ext_flags = pExtensions->Flags2;
1289 current_offset += pExtensions->Size;
1292 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1293 NdrStubInitialize(pRpcMsg, &stubMsg, pStubDesc, pChannel);
1294 else
1295 NdrServerInitializeNew(pRpcMsg, &stubMsg, pStubDesc);
1297 /* store the RPC flags away */
1298 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1299 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1301 /* use alternate memory allocation routines */
1302 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
1303 #if 0
1304 NdrRpcSsEnableAllocate(&stubMsg);
1305 #else
1306 FIXME("Set RPCSS memory allocation routines\n");
1307 #endif
1309 if (Oif_flags.HasPipes)
1311 FIXME("pipes not supported yet\n");
1312 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1313 /* init pipes package */
1314 /* NdrPipesInitialize(...) */
1316 if (ext_flags.HasNewCorrDesc)
1318 /* initialize extra correlation package */
1319 FIXME("new correlation description not implemented\n");
1320 stubMsg.fHasNewCorrDesc = TRUE;
1323 /* convert strings, floating point values and endianess into our
1324 * preferred format */
1325 if ((pRpcMsg->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1326 NdrConvert(&stubMsg, pFormat);
1328 parameter_start_offset = current_offset;
1330 TRACE("allocating memory for stack of size %x\n", stack_size);
1332 args = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, stack_size);
1333 stubMsg.StackTop = args; /* used by conformance of top-level objects */
1335 /* add the implicit This pointer as the first arg to the function if we
1336 * are calling an object method */
1337 if (pThis)
1338 *(void **)args = ((CStdStubBuffer *)pThis)->pvServerObject;
1340 /* order of phases:
1341 * 1. STUBLESS_UNMARHSAL - unmarshal [in] params from buffer
1342 * 2. STUBLESS_CALLSERVER - send/receive buffer
1343 * 3. STUBLESS_CALCSIZE - get [out] buffer size
1344 * 4. STUBLESS_GETBUFFER - allocate [out] buffer
1345 * 5. STUBLESS_MARHSAL - marshal [out] params to buffer
1347 for (phase = STUBLESS_UNMARSHAL; phase <= STUBLESS_FREE; phase++)
1349 TRACE("phase = %d\n", phase);
1350 switch (phase)
1352 case STUBLESS_CALLSERVER:
1353 /* call the server function */
1354 if (pServerInfo->ThunkTable && pServerInfo->ThunkTable[pRpcMsg->ProcNum])
1355 pServerInfo->ThunkTable[pRpcMsg->ProcNum](&stubMsg);
1356 else
1358 SERVER_ROUTINE func;
1359 LONG_PTR retval;
1361 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1363 SERVER_ROUTINE *vtbl = *(SERVER_ROUTINE **)((CStdStubBuffer *)pThis)->pvServerObject;
1364 func = vtbl[pRpcMsg->ProcNum];
1366 else
1367 func = pServerInfo->DispatchTable[pRpcMsg->ProcNum];
1369 /* FIXME: what happens with return values that don't fit into a single register on x86? */
1370 retval = call_server_func(func, args, stack_size);
1372 if (retval_ptr)
1374 TRACE("stub implementation returned 0x%lx\n", retval);
1375 *retval_ptr = retval;
1377 else
1378 TRACE("void stub implementation\n");
1381 stubMsg.Buffer = NULL;
1382 stubMsg.BufferLength = 0;
1384 break;
1385 case STUBLESS_GETBUFFER:
1386 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1387 NdrStubGetBuffer(pThis, pChannel, &stubMsg);
1388 else
1390 RPC_STATUS Status;
1392 pRpcMsg->BufferLength = stubMsg.BufferLength;
1393 /* allocate buffer for [out] and [ret] params */
1394 Status = I_RpcGetBuffer(pRpcMsg);
1395 if (Status)
1396 RpcRaiseException(Status);
1397 stubMsg.BufferStart = pRpcMsg->Buffer;
1398 stubMsg.BufferEnd = stubMsg.BufferStart + stubMsg.BufferLength;
1399 stubMsg.Buffer = stubMsg.BufferStart;
1401 break;
1402 case STUBLESS_MARSHAL:
1403 case STUBLESS_UNMARSHAL:
1404 case STUBLESS_CALCSIZE:
1405 case STUBLESS_FREE:
1406 if (bV2Format)
1407 retval_ptr = stub_do_args(&stubMsg, &pFormat[parameter_start_offset],
1408 phase, args, number_of_params);
1409 else
1410 retval_ptr = stub_do_old_args(&stubMsg, &pFormat[parameter_start_offset],
1411 phase, args, stack_size,
1412 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT));
1414 break;
1415 default:
1416 ERR("shouldn't reach here. phase %d\n", phase);
1417 break;
1421 pRpcMsg->BufferLength = (unsigned int)(stubMsg.Buffer - (unsigned char *)pRpcMsg->Buffer);
1423 if (ext_flags.HasNewCorrDesc)
1425 /* free extra correlation package */
1426 /* NdrCorrelationFree(&stubMsg); */
1429 if (Oif_flags.HasPipes)
1431 /* NdrPipesDone(...) */
1434 /* free the full pointer translation tables */
1435 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1436 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
1438 /* free server function stack */
1439 HeapFree(GetProcessHeap(), 0, args);
1441 return S_OK;
1444 /***********************************************************************
1445 * NdrServerCall2 [RPCRT4.@]
1447 void WINAPI NdrServerCall2(PRPC_MESSAGE pRpcMsg)
1449 DWORD dwPhase;
1450 NdrStubCall2(NULL, NULL, pRpcMsg, &dwPhase);