rpcrt4: Sign-compare warnings fix.
[wine/multimedia.git] / dlls / rpcrt4 / ndr_stubless.c
blob0daf22e936432fdc02d2024a4b708cbeac8db57d
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 WINAPI 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 WINAPI 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 = (void *)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 = (void *)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 /* the return type should be CLIENT_CALL_RETURN, but this is incompatible
552 * with the way gcc returns structures. "void *" should be the largest type
553 * that MIDL should allow you to return anyway */
554 LONG_PTR WINAPIV NdrClientCall2(PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat, ...)
556 /* pointer to start of stack where arguments start */
557 RPC_MESSAGE rpcMsg;
558 MIDL_STUB_MESSAGE stubMsg;
559 handle_t hBinding = NULL;
560 /* procedure number */
561 unsigned short procedure_number;
562 /* size of stack */
563 unsigned short stack_size;
564 /* number of parameters. optional for client to give it to us */
565 unsigned char number_of_params = ~0;
566 /* cache of Oif_flags from v2 procedure header */
567 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
568 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
569 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
570 /* the type of pass we are currently doing */
571 int phase;
572 /* header for procedure string */
573 const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
574 /* -Oif or -Oicf generated format */
575 BOOL bV2Format = FALSE;
576 /* the value to return to the client from the remote procedure */
577 LONG_PTR RetVal = 0;
578 /* the pointer to the object when in OLE mode */
579 void * This = NULL;
580 PFORMAT_STRING pHandleFormat;
581 /* correlation cache */
582 unsigned long NdrCorrCache[256];
584 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
586 /* Later NDR language versions probably won't be backwards compatible */
587 if (pStubDesc->Version > 0x50002)
589 FIXME("Incompatible stub description version: 0x%x\n", pStubDesc->Version);
590 RpcRaiseException(RPC_X_WRONG_STUB_VERSION);
593 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
595 const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
596 stack_size = pProcHeader->stack_size;
597 procedure_number = pProcHeader->proc_num;
598 pFormat += sizeof(NDR_PROC_HEADER_RPC);
600 else
602 stack_size = pProcHeader->stack_size;
603 procedure_number = pProcHeader->proc_num;
604 pFormat += sizeof(NDR_PROC_HEADER);
606 TRACE("stack size: 0x%x\n", stack_size);
607 TRACE("proc num: %d\n", procedure_number);
609 /* create the full pointer translation tables, if requested */
610 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
611 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
613 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
615 /* object is always the first argument */
616 This = **(void *const **)(&pFormat+1);
617 NdrProxyInitialize(This, &rpcMsg, &stubMsg, pStubDesc, procedure_number);
619 else
620 NdrClientInitializeNew(&rpcMsg, &stubMsg, pStubDesc, procedure_number);
622 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
623 TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
625 /* needed for conformance of top-level objects */
626 #ifdef __i386__
627 stubMsg.StackTop = *(unsigned char **)(&pFormat+1);
628 #else
629 # warning Stack not retrieved for your CPU architecture
630 #endif
632 pHandleFormat = pFormat;
634 /* we only need a handle if this isn't an object method */
635 if (!(pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT))
637 pFormat = client_get_handle(&stubMsg, pProcHeader, pHandleFormat, &hBinding);
638 if (!pFormat) return 0;
641 bV2Format = (pStubDesc->Version >= 0x20000);
643 if (bV2Format)
645 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
646 (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
648 Oif_flags = pOIFHeader->Oi2Flags;
649 number_of_params = pOIFHeader->number_of_params;
651 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
654 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
656 if (Oif_flags.HasExtensions)
658 const NDR_PROC_HEADER_EXTS *pExtensions =
659 (const NDR_PROC_HEADER_EXTS *)pFormat;
660 ext_flags = pExtensions->Flags2;
661 pFormat += pExtensions->Size;
664 stubMsg.BufferLength = 0;
666 /* store the RPC flags away */
667 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
668 rpcMsg.RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
670 /* use alternate memory allocation routines */
671 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
672 NdrRpcSmSetClientToOsf(&stubMsg);
674 if (Oif_flags.HasPipes)
676 FIXME("pipes not supported yet\n");
677 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
678 /* init pipes package */
679 /* NdrPipesInitialize(...) */
681 if (ext_flags.HasNewCorrDesc)
683 /* initialize extra correlation package */
684 NdrCorrelationInitialize(&stubMsg, NdrCorrCache, sizeof(NdrCorrCache), 0);
687 /* order of phases:
688 * 1. PROXY_CALCSIZE - calculate the buffer size
689 * 2. PROXY_GETBUFFER - allocate the buffer
690 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
691 * 4. PROXY_SENDRECEIVE - send/receive buffer
692 * 5. PROXY_UNMARHSAL - unmarshal [out] params from buffer
694 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
696 __TRY
698 for (phase = PROXY_CALCSIZE; phase <= PROXY_UNMARSHAL; phase++)
700 TRACE("phase = %d\n", phase);
701 switch (phase)
703 case PROXY_GETBUFFER:
704 /* allocate the buffer */
705 NdrProxyGetBuffer(This, &stubMsg);
706 break;
707 case PROXY_SENDRECEIVE:
708 /* send the [in] params and receive the [out] and [retval]
709 * params */
710 NdrProxySendReceive(This, &stubMsg);
712 /* convert strings, floating point values and endianess into our
713 * preferred format */
714 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
715 NdrConvert(&stubMsg, pFormat);
717 break;
718 case PROXY_CALCSIZE:
719 case PROXY_MARSHAL:
720 case PROXY_UNMARSHAL:
721 if (bV2Format)
722 client_do_args(&stubMsg, pFormat, phase, stubMsg.StackTop,
723 number_of_params, (unsigned char *)&RetVal);
724 else
725 client_do_args_old_format(&stubMsg, pFormat, phase,
726 stubMsg.StackTop, stack_size, (unsigned char *)&RetVal,
727 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT), FALSE);
728 break;
729 default:
730 ERR("shouldn't reach here. phase %d\n", phase);
731 break;
735 __EXCEPT_ALL
737 RetVal = NdrProxyErrorHandler(GetExceptionCode());
739 __ENDTRY
741 else
743 /* order of phases:
744 * 1. PROXY_CALCSIZE - calculate the buffer size
745 * 2. PROXY_GETBUFFER - allocate the buffer
746 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
747 * 4. PROXY_SENDRECEIVE - send/receive buffer
748 * 5. PROXY_UNMARHSAL - unmarshal [out] params from buffer
750 for (phase = PROXY_CALCSIZE; phase <= PROXY_UNMARSHAL; phase++)
752 TRACE("phase = %d\n", phase);
753 switch (phase)
755 case PROXY_GETBUFFER:
756 /* allocate the buffer */
757 if (Oif_flags.HasPipes)
758 /* NdrGetPipeBuffer(...) */
759 FIXME("pipes not supported yet\n");
760 else
762 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
763 #if 0
764 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
765 #else
766 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
767 #endif
768 else
769 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
771 break;
772 case PROXY_SENDRECEIVE:
773 /* send the [in] params and receive the [out] and [retval]
774 * params */
775 if (Oif_flags.HasPipes)
776 /* NdrPipesSendReceive(...) */
777 FIXME("pipes not supported yet\n");
778 else
780 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
781 #if 0
782 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
783 #else
784 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
785 #endif
786 else
787 NdrSendReceive(&stubMsg, stubMsg.Buffer);
790 /* convert strings, floating point values and endianess into our
791 * preferred format */
792 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
793 NdrConvert(&stubMsg, pFormat);
795 break;
796 case PROXY_CALCSIZE:
797 case PROXY_MARSHAL:
798 case PROXY_UNMARSHAL:
799 if (bV2Format)
800 client_do_args(&stubMsg, pFormat, phase, stubMsg.StackTop,
801 number_of_params, (unsigned char *)&RetVal);
802 else
803 client_do_args_old_format(&stubMsg, pFormat, phase,
804 stubMsg.StackTop, stack_size, (unsigned char *)&RetVal,
805 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT), FALSE);
806 break;
807 default:
808 ERR("shouldn't reach here. phase %d\n", phase);
809 break;
814 if (ext_flags.HasNewCorrDesc)
816 /* free extra correlation package */
817 NdrCorrelationFree(&stubMsg);
820 if (Oif_flags.HasPipes)
822 /* NdrPipesDone(...) */
825 /* free the full pointer translation tables */
826 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
827 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
829 /* free marshalling buffer */
830 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
831 NdrProxyFreeBuffer(This, &stubMsg);
832 else
834 NdrFreeBuffer(&stubMsg);
835 client_free_handle(&stubMsg, pProcHeader, pHandleFormat, hBinding);
838 TRACE("RetVal = 0x%lx\n", RetVal);
840 return RetVal;
843 /* Calls a function with the specified arguments, restoring the stack
844 * properly afterwards as we don't know the calling convention of the
845 * function */
846 #if defined __i386__ && defined _MSC_VER
847 __declspec(naked) LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size)
849 __asm
851 push ebp
852 push edi ; Save registers
853 push esi
854 mov ebp, esp
855 mov eax, [ebp+16] ; Get stack size
856 sub esp, eax ; Make room in stack for arguments
857 mov edi, esp
858 mov ecx, eax
859 mov esi, [ebp+12]
860 shr ecx, 2
862 rep movsd ; Copy dword blocks
863 call [ebp+8] ; Call function
864 lea esp, [ebp-8] ; Restore stack
865 pop esi ; Restore registers
866 pop edi
867 pop ebp
871 #elif defined __i386__ && defined __GNUC__
872 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
873 __ASM_GLOBAL_FUNC(call_server_func,
874 "pushl %ebp\n\t"
875 "movl %esp, %ebp\n\t"
876 "pushl %edi\n\t" /* Save registers */
877 "pushl %esi\n\t"
878 "movl 16(%ebp), %eax\n\t" /* Get stack size */
879 "subl %eax, %esp\n\t" /* Make room in stack for arguments */
880 "andl $~15, %esp\n\t" /* Make sure stack has 16-byte alignment for Mac OS X */
881 "movl %esp, %edi\n\t"
882 "movl %eax, %ecx\n\t"
883 "movl 12(%ebp), %esi\n\t"
884 "shrl $2, %ecx\n\t" /* divide by 4 */
885 "cld\n\t"
886 "rep; movsl\n\t" /* Copy dword blocks */
887 "call *8(%ebp)\n\t" /* Call function */
888 "leal -8(%ebp), %esp\n\t" /* Restore stack */
889 "popl %esi\n\t" /* Restore registers */
890 "popl %edi\n\t"
891 "popl %ebp\n\t"
892 "ret\n" )
893 #else
894 #warning call_server_func not implemented for your architecture
895 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned short stack_size)
897 FIXME("Not implemented for your architecture\n");
898 return 0;
900 #endif
902 static DWORD calc_arg_size(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat)
904 DWORD size;
905 switch(*pFormat)
907 case RPC_FC_STRUCT:
908 size = *(const WORD*)(pFormat + 2);
909 break;
910 case RPC_FC_CARRAY:
911 size = *(const WORD*)(pFormat + 2);
912 ComputeConformance(pStubMsg, NULL, pFormat + 4, 0);
913 size *= pStubMsg->MaxCount;
914 break;
915 case RPC_FC_SMFARRAY:
916 size = *(const WORD*)(pFormat + 2);
917 break;
918 case RPC_FC_LGFARRAY:
919 size = *(const DWORD*)(pFormat + 2);
920 break;
921 case RPC_FC_BOGUS_ARRAY:
922 pFormat = ComputeConformance(pStubMsg, NULL, pFormat + 4, *(const WORD*)&pFormat[2]);
923 TRACE("conformance = %ld\n", pStubMsg->MaxCount);
924 pFormat = ComputeVariance(pStubMsg, NULL, pFormat, pStubMsg->MaxCount);
925 size = ComplexStructSize(pStubMsg, pFormat);
926 size *= pStubMsg->MaxCount;
927 break;
928 default:
929 FIXME("Unhandled type %02x\n", *pFormat);
930 /* fallthrough */
931 case RPC_FC_RP:
932 size = sizeof(void *);
933 break;
935 return size;
938 static LONG_PTR *stub_do_args(MIDL_STUB_MESSAGE *pStubMsg,
939 PFORMAT_STRING pFormat, int phase,
940 unsigned char *args,
941 unsigned short number_of_params)
943 /* counter */
944 unsigned short i;
945 /* current format string offset */
946 int current_offset = 0;
947 /* current stack offset */
948 unsigned short current_stack_offset = 0;
949 /* location to put retval into */
950 LONG_PTR *retval_ptr = NULL;
952 for (i = 0; i < number_of_params; i++)
954 const NDR_PARAM_OIF_BASETYPE *pParam =
955 (const NDR_PARAM_OIF_BASETYPE *)&pFormat[current_offset];
956 unsigned char *pArg;
958 current_stack_offset = pParam->stack_offset;
959 pArg = args + current_stack_offset;
961 TRACE("param[%d]: new format\n", i);
962 TRACE("\tparam_attributes:"); dump_RPC_FC_PROC_PF(pParam->param_attributes); TRACE("\n");
963 TRACE("\tstack_offset: 0x%x\n", current_stack_offset);
964 TRACE("\tmemory addr (before): %p -> %p\n", pArg, *(unsigned char **)pArg);
966 if (pParam->param_attributes.IsBasetype)
968 const unsigned char *pTypeFormat =
969 &pParam->type_format_char;
971 TRACE("\tbase type: 0x%02x\n", *pTypeFormat);
973 /* make a note of the address of the return value parameter for later */
974 if (pParam->param_attributes.IsReturn)
975 retval_ptr = (LONG_PTR *)pArg;
977 switch (phase)
979 case STUBLESS_MARSHAL:
980 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
982 if (pParam->param_attributes.IsSimpleRef)
983 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
984 else
985 call_marshaller(pStubMsg, pArg, pTypeFormat);
987 break;
988 case STUBLESS_FREE:
989 if (pParam->param_attributes.ServerAllocSize)
990 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
991 break;
992 case STUBLESS_INITOUT:
993 break;
994 case STUBLESS_UNMARSHAL:
995 if (pParam->param_attributes.ServerAllocSize)
996 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
997 pParam->param_attributes.ServerAllocSize * 8);
999 if (pParam->param_attributes.IsIn)
1001 if (pParam->param_attributes.IsSimpleRef)
1002 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1003 else
1004 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1006 break;
1007 case STUBLESS_CALCSIZE:
1008 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1010 if (pParam->param_attributes.IsSimpleRef)
1011 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1012 else
1013 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1015 break;
1016 default:
1017 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1020 current_offset += sizeof(NDR_PARAM_OIF_BASETYPE);
1022 else
1024 const NDR_PARAM_OIF_OTHER *pParamOther =
1025 (const NDR_PARAM_OIF_OTHER *)&pFormat[current_offset];
1027 const unsigned char * pTypeFormat =
1028 &(pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset]);
1030 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1032 switch (phase)
1034 case STUBLESS_MARSHAL:
1035 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1037 if (pParam->param_attributes.IsByValue)
1038 call_marshaller(pStubMsg, pArg, pTypeFormat);
1039 else
1040 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1042 break;
1043 case STUBLESS_FREE:
1044 if (pParam->param_attributes.MustFree)
1046 if (pParam->param_attributes.IsByValue)
1047 call_freer(pStubMsg, pArg, pTypeFormat);
1048 else
1049 call_freer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1052 if (pParam->param_attributes.IsOut &&
1053 !pParam->param_attributes.IsIn &&
1054 !pParam->param_attributes.IsByValue &&
1055 !pParam->param_attributes.ServerAllocSize)
1057 pStubMsg->pfnFree(*(void **)pArg);
1060 if (pParam->param_attributes.ServerAllocSize)
1061 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
1062 break;
1063 case STUBLESS_INITOUT:
1064 if (!pParam->param_attributes.IsIn &&
1065 pParam->param_attributes.IsOut &&
1066 !pParam->param_attributes.ServerAllocSize &&
1067 !pParam->param_attributes.IsByValue)
1069 DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1071 if(size)
1073 *(void **)pArg = NdrAllocate(pStubMsg, size);
1074 memset(*(void **)pArg, 0, size);
1077 break;
1078 case STUBLESS_UNMARSHAL:
1079 if (pParam->param_attributes.ServerAllocSize)
1080 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1081 pParam->param_attributes.ServerAllocSize * 8);
1083 if (pParam->param_attributes.IsIn)
1085 if (pParam->param_attributes.IsByValue)
1086 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1087 else
1088 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1090 break;
1091 case STUBLESS_CALCSIZE:
1092 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1094 if (pParam->param_attributes.IsByValue)
1095 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1096 else
1097 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1099 break;
1100 default:
1101 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1104 current_offset += sizeof(NDR_PARAM_OIF_OTHER);
1106 TRACE("\tmemory addr (after): %p -> %p\n", pArg, *(unsigned char **)pArg);
1109 return retval_ptr;
1112 static LONG_PTR *stub_do_old_args(MIDL_STUB_MESSAGE *pStubMsg,
1113 PFORMAT_STRING pFormat, int phase,
1114 unsigned char *args,
1115 unsigned short stack_size, BOOL object)
1117 /* counter */
1118 unsigned short i;
1119 /* current format string offset */
1120 int current_offset = 0;
1121 /* current stack offset */
1122 unsigned short current_stack_offset = 0;
1123 /* location to put retval into */
1124 LONG_PTR *retval_ptr = NULL;
1126 for (i = 0; TRUE; i++)
1128 const NDR_PARAM_OI_BASETYPE *pParam =
1129 (const NDR_PARAM_OI_BASETYPE *)&pFormat[current_offset];
1130 /* note: current_stack_offset starts after the This pointer
1131 * if present, so adjust this */
1132 unsigned short current_stack_offset_adjusted = current_stack_offset +
1133 (object ? sizeof(void *) : 0);
1134 unsigned char *pArg = args + current_stack_offset_adjusted;
1136 /* no more parameters; exit loop */
1137 if (current_stack_offset_adjusted >= stack_size)
1138 break;
1140 TRACE("param[%d]: old format\n", i);
1141 TRACE("\tparam_direction: 0x%x\n", pParam->param_direction);
1142 TRACE("\tstack_offset: 0x%x\n", current_stack_offset_adjusted);
1144 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE ||
1145 pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1147 const unsigned char *pTypeFormat =
1148 &pParam->type_format_char;
1150 TRACE("\tbase type 0x%02x\n", *pTypeFormat);
1152 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1153 retval_ptr = (LONG_PTR *)pArg;
1155 switch (phase)
1157 case STUBLESS_MARSHAL:
1158 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1159 call_marshaller(pStubMsg, pArg, pTypeFormat);
1160 break;
1161 case STUBLESS_FREE:
1162 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1163 call_freer(pStubMsg, pArg, pTypeFormat);
1164 break;
1165 case STUBLESS_UNMARSHAL:
1166 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1167 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1168 break;
1169 case STUBLESS_CALCSIZE:
1170 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1171 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1172 break;
1173 default:
1174 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1177 current_stack_offset += type_stack_size(*pTypeFormat);
1178 current_offset += sizeof(NDR_PARAM_OI_BASETYPE);
1180 else
1182 const NDR_PARAM_OI_OTHER *pParamOther =
1183 (const NDR_PARAM_OI_OTHER *)&pFormat[current_offset];
1185 const unsigned char * pTypeFormat =
1186 &pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset];
1188 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1190 if (pParam->param_direction == RPC_FC_RETURN_PARAM)
1191 retval_ptr = (LONG_PTR *)pArg;
1193 switch (phase)
1195 case STUBLESS_MARSHAL:
1196 if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1197 pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1198 pParam->param_direction == RPC_FC_RETURN_PARAM)
1199 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1200 break;
1201 case STUBLESS_FREE:
1202 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1203 pParam->param_direction == RPC_FC_IN_PARAM)
1204 call_freer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1205 else if (pParam->param_direction == RPC_FC_OUT_PARAM)
1206 pStubMsg->pfnFree(*(void **)pArg);
1207 break;
1208 case STUBLESS_INITOUT:
1209 if (pParam->param_direction == RPC_FC_OUT_PARAM)
1211 DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1213 if(size)
1215 *(void **)pArg = NdrAllocate(pStubMsg, size);
1216 memset(*(void **)pArg, 0, size);
1219 break;
1220 case STUBLESS_UNMARSHAL:
1221 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1222 pParam->param_direction == RPC_FC_IN_PARAM)
1223 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1224 break;
1225 case STUBLESS_CALCSIZE:
1226 if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1227 pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1228 pParam->param_direction == RPC_FC_RETURN_PARAM)
1229 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1230 break;
1231 default:
1232 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1235 current_stack_offset += pParamOther->stack_size * sizeof(INT);
1236 current_offset += sizeof(NDR_PARAM_OI_OTHER);
1240 return retval_ptr;
1243 /***********************************************************************
1244 * NdrStubCall2 [RPCRT4.@]
1246 * Unmarshals [in] parameters, calls either a method in an object or a server
1247 * function, marshals any [out] parameters and frees any allocated data.
1249 * NOTES
1250 * Used by stubless MIDL-generated code.
1252 LONG WINAPI NdrStubCall2(
1253 struct IRpcStubBuffer * pThis,
1254 struct IRpcChannelBuffer * pChannel,
1255 PRPC_MESSAGE pRpcMsg,
1256 DWORD * pdwStubPhase)
1258 const MIDL_SERVER_INFO *pServerInfo;
1259 const MIDL_STUB_DESC *pStubDesc;
1260 PFORMAT_STRING pFormat;
1261 MIDL_STUB_MESSAGE stubMsg;
1262 /* pointer to start of stack to pass into stub implementation */
1263 unsigned char * args;
1264 /* size of stack */
1265 unsigned short stack_size;
1266 /* number of parameters. optional for client to give it to us */
1267 unsigned char number_of_params = ~0;
1268 /* cache of Oif_flags from v2 procedure header */
1269 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1270 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1271 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1272 /* the type of pass we are currently doing */
1273 int phase;
1274 /* header for procedure string */
1275 const NDR_PROC_HEADER *pProcHeader;
1276 /* offset in format string for start of params */
1277 int parameter_start_offset;
1278 /* current format string offset */
1279 int current_offset;
1280 /* -Oif or -Oicf generated format */
1281 BOOL bV2Format = FALSE;
1282 /* location to put retval into */
1283 LONG_PTR *retval_ptr = NULL;
1285 TRACE("pThis %p, pChannel %p, pRpcMsg %p, pdwStubPhase %p\n", pThis, pChannel, pRpcMsg, pdwStubPhase);
1287 if (pThis)
1288 pServerInfo = CStdStubBuffer_GetServerInfo(pThis);
1289 else
1290 pServerInfo = ((RPC_SERVER_INTERFACE *)pRpcMsg->RpcInterfaceInformation)->InterpreterInfo;
1292 pStubDesc = pServerInfo->pStubDesc;
1293 pFormat = pServerInfo->ProcString + pServerInfo->FmtStringOffset[pRpcMsg->ProcNum];
1294 pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1296 /* Later NDR language versions probably won't be backwards compatible */
1297 if (pStubDesc->Version > 0x50002)
1299 FIXME("Incompatible stub description version: 0x%x\n", pStubDesc->Version);
1300 RpcRaiseException(RPC_X_WRONG_STUB_VERSION);
1303 /* create the full pointer translation tables, if requested */
1304 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1305 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_SERVER);
1307 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1309 const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1310 stack_size = pProcHeader->stack_size;
1311 current_offset = sizeof(NDR_PROC_HEADER_RPC);
1314 else
1316 stack_size = pProcHeader->stack_size;
1317 current_offset = sizeof(NDR_PROC_HEADER);
1320 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1322 /* binding */
1323 switch (pProcHeader->handle_type)
1325 /* explicit binding: parse additional section */
1326 case RPC_FC_BIND_EXPLICIT:
1327 switch (pFormat[current_offset]) /* handle_type */
1329 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
1330 current_offset += sizeof(NDR_EHD_PRIMITIVE);
1331 break;
1332 case RPC_FC_BIND_GENERIC: /* explicit generic */
1333 current_offset += sizeof(NDR_EHD_GENERIC);
1334 break;
1335 case RPC_FC_BIND_CONTEXT: /* explicit context */
1336 current_offset += sizeof(NDR_EHD_CONTEXT);
1337 break;
1338 default:
1339 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1340 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1342 break;
1343 case RPC_FC_BIND_GENERIC: /* implicit generic */
1344 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
1345 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
1346 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
1347 break;
1348 default:
1349 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1350 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1353 bV2Format = (pStubDesc->Version >= 0x20000);
1355 if (bV2Format)
1357 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
1358 (const NDR_PROC_PARTIAL_OIF_HEADER *)&pFormat[current_offset];
1360 Oif_flags = pOIFHeader->Oi2Flags;
1361 number_of_params = pOIFHeader->number_of_params;
1363 current_offset += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1366 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
1368 if (Oif_flags.HasExtensions)
1370 const NDR_PROC_HEADER_EXTS *pExtensions =
1371 (const NDR_PROC_HEADER_EXTS *)&pFormat[current_offset];
1372 ext_flags = pExtensions->Flags2;
1373 current_offset += pExtensions->Size;
1376 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1377 NdrStubInitialize(pRpcMsg, &stubMsg, pStubDesc, pChannel);
1378 else
1379 NdrServerInitializeNew(pRpcMsg, &stubMsg, pStubDesc);
1381 /* store the RPC flags away */
1382 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1383 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1385 /* use alternate memory allocation routines */
1386 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
1387 #if 0
1388 NdrRpcSsEnableAllocate(&stubMsg);
1389 #else
1390 FIXME("Set RPCSS memory allocation routines\n");
1391 #endif
1393 if (Oif_flags.HasPipes)
1395 FIXME("pipes not supported yet\n");
1396 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1397 /* init pipes package */
1398 /* NdrPipesInitialize(...) */
1400 if (ext_flags.HasNewCorrDesc)
1402 /* initialize extra correlation package */
1403 FIXME("new correlation description not implemented\n");
1404 stubMsg.fHasNewCorrDesc = TRUE;
1407 /* convert strings, floating point values and endianess into our
1408 * preferred format */
1409 if ((pRpcMsg->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1410 NdrConvert(&stubMsg, pFormat);
1412 parameter_start_offset = current_offset;
1414 TRACE("allocating memory for stack of size %x\n", stack_size);
1416 args = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, stack_size);
1417 stubMsg.StackTop = args; /* used by conformance of top-level objects */
1419 /* add the implicit This pointer as the first arg to the function if we
1420 * are calling an object method */
1421 if (pThis)
1422 *(void **)args = ((CStdStubBuffer *)pThis)->pvServerObject;
1424 /* order of phases:
1425 * 1. STUBLESS_UNMARHSAL - unmarshal [in] params from buffer
1426 * 2. STUBLESS_CALLSERVER - send/receive buffer
1427 * 3. STUBLESS_CALCSIZE - get [out] buffer size
1428 * 4. STUBLESS_GETBUFFER - allocate [out] buffer
1429 * 5. STUBLESS_MARHSAL - marshal [out] params to buffer
1431 for (phase = STUBLESS_UNMARSHAL; phase <= STUBLESS_FREE; phase++)
1433 TRACE("phase = %d\n", phase);
1434 switch (phase)
1436 case STUBLESS_CALLSERVER:
1437 /* call the server function */
1438 if (pServerInfo->ThunkTable && pServerInfo->ThunkTable[pRpcMsg->ProcNum])
1439 pServerInfo->ThunkTable[pRpcMsg->ProcNum](&stubMsg);
1440 else
1442 SERVER_ROUTINE func;
1443 LONG_PTR retval;
1445 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1447 SERVER_ROUTINE *vtbl = *(SERVER_ROUTINE **)((CStdStubBuffer *)pThis)->pvServerObject;
1448 func = vtbl[pRpcMsg->ProcNum];
1450 else
1451 func = pServerInfo->DispatchTable[pRpcMsg->ProcNum];
1453 /* FIXME: what happens with return values that don't fit into a single register on x86? */
1454 retval = call_server_func(func, args, stack_size);
1456 if (retval_ptr)
1458 TRACE("stub implementation returned 0x%lx\n", retval);
1459 *retval_ptr = retval;
1461 else
1462 TRACE("void stub implementation\n");
1465 stubMsg.Buffer = NULL;
1466 stubMsg.BufferLength = 0;
1468 break;
1469 case STUBLESS_GETBUFFER:
1470 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1471 NdrStubGetBuffer(pThis, pChannel, &stubMsg);
1472 else
1474 RPC_STATUS Status;
1476 pRpcMsg->BufferLength = stubMsg.BufferLength;
1477 /* allocate buffer for [out] and [ret] params */
1478 Status = I_RpcGetBuffer(pRpcMsg);
1479 if (Status)
1480 RpcRaiseException(Status);
1481 stubMsg.Buffer = pRpcMsg->Buffer;
1483 break;
1484 case STUBLESS_UNMARSHAL:
1485 case STUBLESS_INITOUT:
1486 case STUBLESS_CALCSIZE:
1487 case STUBLESS_MARSHAL:
1488 case STUBLESS_FREE:
1489 if (bV2Format)
1490 retval_ptr = stub_do_args(&stubMsg, &pFormat[parameter_start_offset],
1491 phase, args, number_of_params);
1492 else
1493 retval_ptr = stub_do_old_args(&stubMsg, &pFormat[parameter_start_offset],
1494 phase, args, stack_size,
1495 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT));
1497 break;
1498 default:
1499 ERR("shouldn't reach here. phase %d\n", phase);
1500 break;
1504 pRpcMsg->BufferLength = (unsigned int)(stubMsg.Buffer - (unsigned char *)pRpcMsg->Buffer);
1506 if (ext_flags.HasNewCorrDesc)
1508 /* free extra correlation package */
1509 /* NdrCorrelationFree(&stubMsg); */
1512 if (Oif_flags.HasPipes)
1514 /* NdrPipesDone(...) */
1517 /* free the full pointer translation tables */
1518 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1519 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
1521 /* free server function stack */
1522 HeapFree(GetProcessHeap(), 0, args);
1524 return S_OK;
1527 /***********************************************************************
1528 * NdrServerCall2 [RPCRT4.@]
1530 void WINAPI NdrServerCall2(PRPC_MESSAGE pRpcMsg)
1532 DWORD dwPhase;
1533 NdrStubCall2(NULL, NULL, pRpcMsg, &dwPhase);