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
23 * - Some types of binding handles
27 #include "wine/port.h"
41 #include "wine/exception.h"
42 #include "wine/debug.h"
43 #include "wine/rpcfc.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
);
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
);
70 FIXME("format type 0x%x not implemented\n", pFormat
[0]);
71 RpcRaiseException(RPC_X_BAD_STUB_DATA
);
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
);
82 FIXME("format type 0x%x not implemented\n", pFormat
[0]);
83 RpcRaiseException(RPC_X_BAD_STUB_DATA
);
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
;
110 static void dump_RPC_FC_PROC_PF(PARAM_ATTRIBUTES param_attributes
)
112 if (param_attributes
.MustSize
) TRACE(" MustSize");
113 if (param_attributes
.MustFree
) TRACE(" MustFree");
114 if (param_attributes
.IsPipe
) TRACE(" IsPipe");
115 if (param_attributes
.IsIn
) TRACE(" IsIn");
116 if (param_attributes
.IsOut
) TRACE(" IsOut");
117 if (param_attributes
.IsReturn
) TRACE(" IsReturn");
118 if (param_attributes
.IsBasetype
) TRACE(" IsBasetype");
119 if (param_attributes
.IsByValue
) TRACE(" IsByValue");
120 if (param_attributes
.IsSimpleRef
) TRACE(" IsSimpleRef");
121 if (param_attributes
.IsDontCallFreeInst
) TRACE(" IsDontCallFreeInst");
122 if (param_attributes
.SaveForAsyncFinish
) TRACE(" SaveForAsyncFinish");
123 if (param_attributes
.ServerAllocSize
) TRACE(" ServerAllocSize = %d", param_attributes
.ServerAllocSize
* 8);
126 static void dump_INTERPRETER_OPT_FLAGS(INTERPRETER_OPT_FLAGS Oi2Flags
)
128 if (Oi2Flags
.ServerMustSize
) TRACE(" ServerMustSize");
129 if (Oi2Flags
.ClientMustSize
) TRACE(" ClientMustSize");
130 if (Oi2Flags
.HasReturn
) TRACE(" HasReturn");
131 if (Oi2Flags
.HasPipes
) TRACE(" HasPipes");
132 if (Oi2Flags
.Unused
) TRACE(" Unused");
133 if (Oi2Flags
.HasAsyncUuid
) TRACE(" HasAsyncUuid");
134 if (Oi2Flags
.HasExtensions
) TRACE(" HasExtensions");
135 if (Oi2Flags
.HasAsyncHandle
) TRACE(" HasAsyncHandle");
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
)
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
);
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
;
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
);
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
);
193 context_handle
= *(NDR_CCONTEXT
*)ARG_FROM_OFFSET(pStubMsg
->StackTop
, pDesc
->offset
);
194 if ((pDesc
->flags
& NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
) &&
197 ERR("null context handle isn't allowed\n");
198 RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT
);
201 *phBinding
= NDRCContextBinding(context_handle
);
202 /* FIXME: should we store this structure in stubMsg.pContext? */
203 return pFormat
+ sizeof(NDR_EHD_CONTEXT
);
206 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader
->handle_type
);
207 RpcRaiseException(RPC_X_BAD_STUB_DATA
);
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 */
214 case RPC_FC_BIND_PRIMITIVE
: /* implicit primitive */
215 TRACE("Implicit primitive handle\n");
216 *phBinding
= *pStubMsg
->StubDesc
->IMPLICIT_HANDLE_INFO
.pPrimitiveHandle
;
218 case RPC_FC_CALLBACK_HANDLE
: /* implicit callback */
219 FIXME("RPC_FC_CALLBACK_HANDLE\n");
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
;
230 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader
->handle_type
);
231 RpcRaiseException(RPC_X_BAD_STUB_DATA
);
236 static void client_free_handle(
237 PMIDL_STUB_MESSAGE pStubMsg
, const NDR_PROC_HEADER
*pProcHeader
,
238 PFORMAT_STRING pFormat
, handle_t hBinding
)
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
;
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
);
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
);
265 case RPC_FC_BIND_CONTEXT
: /* explicit context */
266 case RPC_FC_BIND_PRIMITIVE
: /* explicit primitive */
269 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader
->handle_type
);
270 RpcRaiseException(RPC_X_BAD_STUB_DATA
);
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 */
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 */
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;
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
);
325 if (pParam
->param_attributes
.IsIn
)
326 call_buffer_sizer(pStubMsg
, pArg
, pTypeFormat
);
329 if (pParam
->param_attributes
.IsIn
)
330 call_marshaller(pStubMsg
, pArg
, pTypeFormat
);
332 case PROXY_UNMARSHAL
:
333 if (pParam
->param_attributes
.IsOut
)
335 if (pParam
->param_attributes
.IsReturn
)
336 call_unmarshaller(pStubMsg
, &pRetVal
, pTypeFormat
, 0);
338 call_unmarshaller(pStubMsg
, &pArg
, pTypeFormat
, 0);
339 TRACE("pRetVal = %p\n", pRetVal
);
343 RpcRaiseException(RPC_S_INTERNAL_ERROR
);
346 current_offset
+= sizeof(NDR_PARAM_OIF_BASETYPE
);
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
);
369 if (pParam
->param_attributes
.IsIn
)
371 if (pParam
->param_attributes
.IsByValue
)
372 call_buffer_sizer(pStubMsg
, pArg
, pTypeFormat
);
374 call_buffer_sizer(pStubMsg
, *(unsigned char **)pArg
, pTypeFormat
);
378 if (pParam
->param_attributes
.IsIn
)
380 if (pParam
->param_attributes
.IsByValue
)
381 call_marshaller(pStubMsg
, pArg
, pTypeFormat
);
383 call_marshaller(pStubMsg
, *(unsigned char **)pArg
, pTypeFormat
);
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);
394 call_unmarshaller(pStubMsg
, (unsigned char **)pArg
, pTypeFormat
, 0);
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
)
419 return sizeof(short);
426 return sizeof(float);
428 return sizeof(double);
430 return sizeof(ULONGLONG
);
431 case RPC_FC_ERROR_STATUS_T
:
432 return sizeof(error_status_t
);
434 return sizeof(void *);
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;
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
)
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
);
487 if (pParam
->param_direction
== RPC_FC_IN_PARAM_BASETYPE
)
488 call_buffer_sizer(pStubMsg
, pArg
, pTypeFormat
);
491 if (pParam
->param_direction
== RPC_FC_IN_PARAM_BASETYPE
)
492 call_marshaller(pStubMsg
, pArg
, pTypeFormat
);
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);
501 call_unmarshaller(pStubMsg
, &pArg
, pTypeFormat
, 0);
505 RpcRaiseException(RPC_S_INTERNAL_ERROR
);
508 current_stack_offset
+= type_stack_size(*pTypeFormat
);
509 current_offset
+= sizeof(NDR_PARAM_OI_BASETYPE
);
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
);
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
);
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
);
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);
541 RpcRaiseException(RPC_S_INTERNAL_ERROR
);
544 current_stack_offset
+= pParamOther
->stack_size
* sizeof(INT
);
545 current_offset
+= sizeof(NDR_PARAM_OI_OTHER
);
547 TRACE("\tmemory addr (after): %p\n", pArg
);
551 CLIENT_CALL_RETURN WINAPIV
NdrClientCall2(PMIDL_STUB_DESC pStubDesc
, PFORMAT_STRING pFormat
, ...)
553 /* pointer to start of stack where arguments start */
555 MIDL_STUB_MESSAGE stubMsg
;
556 handle_t hBinding
= NULL
;
557 /* procedure number */
558 unsigned short procedure_number
;
560 unsigned short stack_size
;
561 /* number of parameters. optional for client to give it to us */
562 unsigned char number_of_params
= ~0;
563 /* cache of Oif_flags from v2 procedure header */
564 INTERPRETER_OPT_FLAGS Oif_flags
= { 0 };
565 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
566 INTERPRETER_OPT_FLAGS2 ext_flags
= { 0 };
567 /* the type of pass we are currently doing */
569 /* header for procedure string */
570 const NDR_PROC_HEADER
* pProcHeader
= (const NDR_PROC_HEADER
*)&pFormat
[0];
571 /* -Oif or -Oicf generated format */
572 BOOL bV2Format
= FALSE
;
573 /* the value to return to the client from the remote procedure */
574 CLIENT_CALL_RETURN RetVal
= {0};
575 /* the pointer to the object when in OLE mode */
577 PFORMAT_STRING pHandleFormat
;
578 /* correlation cache */
579 unsigned long NdrCorrCache
[256];
581 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc
, pFormat
);
583 TRACE("NDR Version: 0x%x\n", pStubDesc
->Version
);
585 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_RPCFLAGS
)
587 const NDR_PROC_HEADER_RPC
*pProcHeader
= (const NDR_PROC_HEADER_RPC
*)&pFormat
[0];
588 stack_size
= pProcHeader
->stack_size
;
589 procedure_number
= pProcHeader
->proc_num
;
590 pFormat
+= sizeof(NDR_PROC_HEADER_RPC
);
594 stack_size
= pProcHeader
->stack_size
;
595 procedure_number
= pProcHeader
->proc_num
;
596 pFormat
+= sizeof(NDR_PROC_HEADER
);
598 TRACE("stack size: 0x%x\n", stack_size
);
599 TRACE("proc num: %d\n", procedure_number
);
601 /* create the full pointer translation tables, if requested */
602 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_FULLPTR
)
603 stubMsg
.FullPtrXlatTables
= NdrFullPointerXlatInit(0,XLAT_CLIENT
);
605 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_OBJECT
)
607 /* object is always the first argument */
608 This
= **(void *const **)(&pFormat
+1);
609 NdrProxyInitialize(This
, &rpcMsg
, &stubMsg
, pStubDesc
, procedure_number
);
612 NdrClientInitializeNew(&rpcMsg
, &stubMsg
, pStubDesc
, procedure_number
);
614 TRACE("Oi_flags = 0x%02x\n", pProcHeader
->Oi_flags
);
615 TRACE("MIDL stub version = 0x%x\n", pStubDesc
->MIDLVersion
);
617 /* needed for conformance of top-level objects */
619 stubMsg
.StackTop
= *(unsigned char **)(&pFormat
+1);
621 # warning Stack not retrieved for your CPU architecture
624 pHandleFormat
= pFormat
;
626 /* we only need a handle if this isn't an object method */
627 if (!(pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_OBJECT
))
629 pFormat
= client_get_handle(&stubMsg
, pProcHeader
, pHandleFormat
, &hBinding
);
630 if (!pFormat
) return RetVal
;
633 bV2Format
= (pStubDesc
->Version
>= 0x20000);
637 const NDR_PROC_PARTIAL_OIF_HEADER
*pOIFHeader
=
638 (const NDR_PROC_PARTIAL_OIF_HEADER
*)pFormat
;
640 Oif_flags
= pOIFHeader
->Oi2Flags
;
641 number_of_params
= pOIFHeader
->number_of_params
;
643 pFormat
+= sizeof(NDR_PROC_PARTIAL_OIF_HEADER
);
646 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags
);
648 if (Oif_flags
.HasExtensions
)
650 const NDR_PROC_HEADER_EXTS
*pExtensions
=
651 (const NDR_PROC_HEADER_EXTS
*)pFormat
;
652 ext_flags
= pExtensions
->Flags2
;
653 pFormat
+= pExtensions
->Size
;
656 stubMsg
.BufferLength
= 0;
658 /* store the RPC flags away */
659 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_RPCFLAGS
)
660 rpcMsg
.RpcFlags
= ((const NDR_PROC_HEADER_RPC
*)pProcHeader
)->rpc_flags
;
662 /* use alternate memory allocation routines */
663 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_RPCSSALLOC
)
664 NdrRpcSmSetClientToOsf(&stubMsg
);
666 if (Oif_flags
.HasPipes
)
668 FIXME("pipes not supported yet\n");
669 RpcRaiseException(RPC_X_WRONG_STUB_VERSION
); /* FIXME: remove when implemented */
670 /* init pipes package */
671 /* NdrPipesInitialize(...) */
673 if (ext_flags
.HasNewCorrDesc
)
675 /* initialize extra correlation package */
676 NdrCorrelationInitialize(&stubMsg
, NdrCorrCache
, sizeof(NdrCorrCache
), 0);
680 * 1. PROXY_CALCSIZE - calculate the buffer size
681 * 2. PROXY_GETBUFFER - allocate the buffer
682 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
683 * 4. PROXY_SENDRECEIVE - send/receive buffer
684 * 5. PROXY_UNMARHSAL - unmarshal [out] params from buffer
686 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_OBJECT
)
690 for (phase
= PROXY_CALCSIZE
; phase
<= PROXY_UNMARSHAL
; phase
++)
692 TRACE("phase = %d\n", phase
);
695 case PROXY_GETBUFFER
:
696 /* allocate the buffer */
697 NdrProxyGetBuffer(This
, &stubMsg
);
699 case PROXY_SENDRECEIVE
:
700 /* send the [in] params and receive the [out] and [retval]
702 NdrProxySendReceive(This
, &stubMsg
);
704 /* convert strings, floating point values and endianess into our
705 * preferred format */
706 if ((rpcMsg
.DataRepresentation
& 0x0000FFFFUL
) != NDR_LOCAL_DATA_REPRESENTATION
)
707 NdrConvert(&stubMsg
, pFormat
);
712 case PROXY_UNMARSHAL
:
714 client_do_args(&stubMsg
, pFormat
, phase
, stubMsg
.StackTop
,
715 number_of_params
, (unsigned char *)&RetVal
);
717 client_do_args_old_format(&stubMsg
, pFormat
, phase
,
718 stubMsg
.StackTop
, stack_size
, (unsigned char *)&RetVal
,
719 (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_OBJECT
), FALSE
);
722 ERR("shouldn't reach here. phase %d\n", phase
);
729 RetVal
.Simple
= NdrProxyErrorHandler(GetExceptionCode());
736 * 1. PROXY_CALCSIZE - calculate the buffer size
737 * 2. PROXY_GETBUFFER - allocate the buffer
738 * 3. PROXY_MARHSAL - marshal [in] params into the buffer
739 * 4. PROXY_SENDRECEIVE - send/receive buffer
740 * 5. PROXY_UNMARHSAL - unmarshal [out] params from buffer
742 for (phase
= PROXY_CALCSIZE
; phase
<= PROXY_UNMARSHAL
; phase
++)
744 TRACE("phase = %d\n", phase
);
747 case PROXY_GETBUFFER
:
748 /* allocate the buffer */
749 if (Oif_flags
.HasPipes
)
750 /* NdrGetPipeBuffer(...) */
751 FIXME("pipes not supported yet\n");
754 if (pProcHeader
->handle_type
== RPC_FC_AUTO_HANDLE
)
756 NdrNsGetBuffer(&stubMsg
, stubMsg
.BufferLength
, hBinding
);
758 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
761 NdrGetBuffer(&stubMsg
, stubMsg
.BufferLength
, hBinding
);
764 case PROXY_SENDRECEIVE
:
765 /* send the [in] params and receive the [out] and [retval]
767 if (Oif_flags
.HasPipes
)
768 /* NdrPipesSendReceive(...) */
769 FIXME("pipes not supported yet\n");
772 if (pProcHeader
->handle_type
== RPC_FC_AUTO_HANDLE
)
774 NdrNsSendReceive(&stubMsg
, stubMsg
.Buffer
, pStubDesc
->IMPLICIT_HANDLE_INFO
.pAutoHandle
);
776 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
779 NdrSendReceive(&stubMsg
, stubMsg
.Buffer
);
782 /* convert strings, floating point values and endianess into our
783 * preferred format */
784 if ((rpcMsg
.DataRepresentation
& 0x0000FFFFUL
) != NDR_LOCAL_DATA_REPRESENTATION
)
785 NdrConvert(&stubMsg
, pFormat
);
790 case PROXY_UNMARSHAL
:
792 client_do_args(&stubMsg
, pFormat
, phase
, stubMsg
.StackTop
,
793 number_of_params
, (unsigned char *)&RetVal
);
795 client_do_args_old_format(&stubMsg
, pFormat
, phase
,
796 stubMsg
.StackTop
, stack_size
, (unsigned char *)&RetVal
,
797 (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_OBJECT
), FALSE
);
800 ERR("shouldn't reach here. phase %d\n", phase
);
806 if (ext_flags
.HasNewCorrDesc
)
808 /* free extra correlation package */
809 NdrCorrelationFree(&stubMsg
);
812 if (Oif_flags
.HasPipes
)
814 /* NdrPipesDone(...) */
817 /* free the full pointer translation tables */
818 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_FULLPTR
)
819 NdrFullPointerXlatFree(stubMsg
.FullPtrXlatTables
);
821 /* free marshalling buffer */
822 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_OBJECT
)
823 NdrProxyFreeBuffer(This
, &stubMsg
);
826 NdrFreeBuffer(&stubMsg
);
827 client_free_handle(&stubMsg
, pProcHeader
, pHandleFormat
, hBinding
);
830 TRACE("RetVal = 0x%lx\n", RetVal
);
835 /* Calls a function with the specified arguments, restoring the stack
836 * properly afterwards as we don't know the calling convention of the
838 #if defined __i386__ && defined _MSC_VER
839 __declspec(naked
) LONG_PTR __cdecl
call_server_func(SERVER_ROUTINE func
, unsigned char * args
, unsigned int stack_size
)
844 push edi
; Save registers
847 mov eax
, [ebp
+16] ; Get stack size
848 sub esp
, eax
; Make room in stack
for arguments
854 rep movsd
; Copy dword blocks
855 call
[ebp
+8] ; Call function
856 lea esp
, [ebp
-8] ; Restore stack
857 pop esi
; Restore registers
863 #elif defined __i386__ && defined __GNUC__
864 LONG_PTR __cdecl
call_server_func(SERVER_ROUTINE func
, unsigned char * args
, unsigned int stack_size
);
865 __ASM_GLOBAL_FUNC(call_server_func
,
867 "movl %esp, %ebp\n\t"
868 "pushl %edi\n\t" /* Save registers */
870 "movl 16(%ebp), %eax\n\t" /* Get stack size */
871 "subl %eax, %esp\n\t" /* Make room in stack for arguments */
872 "andl $~15, %esp\n\t" /* Make sure stack has 16-byte alignment for Mac OS X */
873 "movl %esp, %edi\n\t"
874 "movl %eax, %ecx\n\t"
875 "movl 12(%ebp), %esi\n\t"
876 "shrl $2, %ecx\n\t" /* divide by 4 */
878 "rep; movsl\n\t" /* Copy dword blocks */
879 "call *8(%ebp)\n\t" /* Call function */
880 "leal -8(%ebp), %esp\n\t" /* Restore stack */
881 "popl %esi\n\t" /* Restore registers */
886 #warning call_server_func not implemented for your architecture
887 LONG_PTR __cdecl
call_server_func(SERVER_ROUTINE func
, unsigned char * args
, unsigned short stack_size
)
889 FIXME("Not implemented for your architecture\n");
894 static DWORD
calc_arg_size(MIDL_STUB_MESSAGE
*pStubMsg
, PFORMAT_STRING pFormat
)
900 size
= *(const WORD
*)(pFormat
+ 2);
903 size
= *(const WORD
*)(pFormat
+ 2);
904 ComputeConformance(pStubMsg
, NULL
, pFormat
+ 4, 0);
905 size
*= pStubMsg
->MaxCount
;
907 case RPC_FC_SMFARRAY
:
908 size
= *(const WORD
*)(pFormat
+ 2);
910 case RPC_FC_LGFARRAY
:
911 size
= *(const DWORD
*)(pFormat
+ 2);
913 case RPC_FC_BOGUS_ARRAY
:
914 pFormat
= ComputeConformance(pStubMsg
, NULL
, pFormat
+ 4, *(const WORD
*)&pFormat
[2]);
915 TRACE("conformance = %ld\n", pStubMsg
->MaxCount
);
916 pFormat
= ComputeVariance(pStubMsg
, NULL
, pFormat
, pStubMsg
->MaxCount
);
917 size
= ComplexStructSize(pStubMsg
, pFormat
);
918 size
*= pStubMsg
->MaxCount
;
921 FIXME("Unhandled type %02x\n", *pFormat
);
924 size
= sizeof(void *);
930 static LONG_PTR
*stub_do_args(MIDL_STUB_MESSAGE
*pStubMsg
,
931 PFORMAT_STRING pFormat
, int phase
,
933 unsigned short number_of_params
)
937 /* current format string offset */
938 int current_offset
= 0;
939 /* current stack offset */
940 unsigned short current_stack_offset
= 0;
941 /* location to put retval into */
942 LONG_PTR
*retval_ptr
= NULL
;
944 for (i
= 0; i
< number_of_params
; i
++)
946 const NDR_PARAM_OIF_BASETYPE
*pParam
=
947 (const NDR_PARAM_OIF_BASETYPE
*)&pFormat
[current_offset
];
950 current_stack_offset
= pParam
->stack_offset
;
951 pArg
= args
+ current_stack_offset
;
953 TRACE("param[%d]: new format\n", i
);
954 TRACE("\tparam_attributes:"); dump_RPC_FC_PROC_PF(pParam
->param_attributes
); TRACE("\n");
955 TRACE("\tstack_offset: 0x%x\n", current_stack_offset
);
956 TRACE("\tmemory addr (before): %p -> %p\n", pArg
, *(unsigned char **)pArg
);
958 if (pParam
->param_attributes
.IsBasetype
)
960 const unsigned char *pTypeFormat
=
961 &pParam
->type_format_char
;
963 TRACE("\tbase type: 0x%02x\n", *pTypeFormat
);
965 /* make a note of the address of the return value parameter for later */
966 if (pParam
->param_attributes
.IsReturn
)
967 retval_ptr
= (LONG_PTR
*)pArg
;
971 case STUBLESS_MARSHAL
:
972 if (pParam
->param_attributes
.IsOut
|| pParam
->param_attributes
.IsReturn
)
974 if (pParam
->param_attributes
.IsSimpleRef
)
975 call_marshaller(pStubMsg
, *(unsigned char **)pArg
, pTypeFormat
);
977 call_marshaller(pStubMsg
, pArg
, pTypeFormat
);
981 if (pParam
->param_attributes
.ServerAllocSize
)
982 HeapFree(GetProcessHeap(), 0, *(void **)pArg
);
984 case STUBLESS_INITOUT
:
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
.IsSimpleRef
)
994 call_unmarshaller(pStubMsg
, (unsigned char **)pArg
, pTypeFormat
, 0);
996 call_unmarshaller(pStubMsg
, &pArg
, pTypeFormat
, 0);
999 case STUBLESS_CALCSIZE
:
1000 if (pParam
->param_attributes
.IsOut
|| pParam
->param_attributes
.IsReturn
)
1002 if (pParam
->param_attributes
.IsSimpleRef
)
1003 call_buffer_sizer(pStubMsg
, *(unsigned char **)pArg
, pTypeFormat
);
1005 call_buffer_sizer(pStubMsg
, pArg
, pTypeFormat
);
1009 RpcRaiseException(RPC_S_INTERNAL_ERROR
);
1012 current_offset
+= sizeof(NDR_PARAM_OIF_BASETYPE
);
1016 const NDR_PARAM_OIF_OTHER
*pParamOther
=
1017 (const NDR_PARAM_OIF_OTHER
*)&pFormat
[current_offset
];
1019 const unsigned char * pTypeFormat
=
1020 &(pStubMsg
->StubDesc
->pFormatTypes
[pParamOther
->type_offset
]);
1022 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat
);
1026 case STUBLESS_MARSHAL
:
1027 if (pParam
->param_attributes
.IsOut
|| pParam
->param_attributes
.IsReturn
)
1029 if (pParam
->param_attributes
.IsByValue
)
1030 call_marshaller(pStubMsg
, pArg
, pTypeFormat
);
1032 call_marshaller(pStubMsg
, *(unsigned char **)pArg
, pTypeFormat
);
1036 if (pParam
->param_attributes
.MustFree
)
1038 if (pParam
->param_attributes
.IsByValue
)
1039 call_freer(pStubMsg
, pArg
, pTypeFormat
);
1041 call_freer(pStubMsg
, *(unsigned char **)pArg
, pTypeFormat
);
1044 if (pParam
->param_attributes
.IsOut
&&
1045 !pParam
->param_attributes
.IsIn
&&
1046 !pParam
->param_attributes
.IsByValue
&&
1047 !pParam
->param_attributes
.ServerAllocSize
)
1049 pStubMsg
->pfnFree(*(void **)pArg
);
1052 if (pParam
->param_attributes
.ServerAllocSize
)
1053 HeapFree(GetProcessHeap(), 0, *(void **)pArg
);
1055 case STUBLESS_INITOUT
:
1056 if (!pParam
->param_attributes
.IsIn
&&
1057 pParam
->param_attributes
.IsOut
&&
1058 !pParam
->param_attributes
.ServerAllocSize
&&
1059 !pParam
->param_attributes
.IsByValue
)
1061 DWORD size
= calc_arg_size(pStubMsg
, pTypeFormat
);
1065 *(void **)pArg
= NdrAllocate(pStubMsg
, size
);
1066 memset(*(void **)pArg
, 0, size
);
1070 case STUBLESS_UNMARSHAL
:
1071 if (pParam
->param_attributes
.ServerAllocSize
)
1072 *(void **)pArg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1073 pParam
->param_attributes
.ServerAllocSize
* 8);
1075 if (pParam
->param_attributes
.IsIn
)
1077 if (pParam
->param_attributes
.IsByValue
)
1078 call_unmarshaller(pStubMsg
, &pArg
, pTypeFormat
, 0);
1080 call_unmarshaller(pStubMsg
, (unsigned char **)pArg
, pTypeFormat
, 0);
1083 case STUBLESS_CALCSIZE
:
1084 if (pParam
->param_attributes
.IsOut
|| pParam
->param_attributes
.IsReturn
)
1086 if (pParam
->param_attributes
.IsByValue
)
1087 call_buffer_sizer(pStubMsg
, pArg
, pTypeFormat
);
1089 call_buffer_sizer(pStubMsg
, *(unsigned char **)pArg
, pTypeFormat
);
1093 RpcRaiseException(RPC_S_INTERNAL_ERROR
);
1096 current_offset
+= sizeof(NDR_PARAM_OIF_OTHER
);
1098 TRACE("\tmemory addr (after): %p -> %p\n", pArg
, *(unsigned char **)pArg
);
1104 static LONG_PTR
*stub_do_old_args(MIDL_STUB_MESSAGE
*pStubMsg
,
1105 PFORMAT_STRING pFormat
, int phase
,
1106 unsigned char *args
,
1107 unsigned short stack_size
, BOOL object
)
1111 /* current format string offset */
1112 int current_offset
= 0;
1113 /* current stack offset */
1114 unsigned short current_stack_offset
= 0;
1115 /* location to put retval into */
1116 LONG_PTR
*retval_ptr
= NULL
;
1118 for (i
= 0; TRUE
; i
++)
1120 const NDR_PARAM_OI_BASETYPE
*pParam
=
1121 (const NDR_PARAM_OI_BASETYPE
*)&pFormat
[current_offset
];
1122 /* note: current_stack_offset starts after the This pointer
1123 * if present, so adjust this */
1124 unsigned short current_stack_offset_adjusted
= current_stack_offset
+
1125 (object
? sizeof(void *) : 0);
1126 unsigned char *pArg
= args
+ current_stack_offset_adjusted
;
1128 /* no more parameters; exit loop */
1129 if (current_stack_offset_adjusted
>= stack_size
)
1132 TRACE("param[%d]: old format\n", i
);
1133 TRACE("\tparam_direction: 0x%x\n", pParam
->param_direction
);
1134 TRACE("\tstack_offset: 0x%x\n", current_stack_offset_adjusted
);
1136 if (pParam
->param_direction
== RPC_FC_IN_PARAM_BASETYPE
||
1137 pParam
->param_direction
== RPC_FC_RETURN_PARAM_BASETYPE
)
1139 const unsigned char *pTypeFormat
=
1140 &pParam
->type_format_char
;
1142 TRACE("\tbase type 0x%02x\n", *pTypeFormat
);
1144 if (pParam
->param_direction
== RPC_FC_RETURN_PARAM_BASETYPE
)
1145 retval_ptr
= (LONG_PTR
*)pArg
;
1149 case STUBLESS_MARSHAL
:
1150 if (pParam
->param_direction
== RPC_FC_RETURN_PARAM_BASETYPE
)
1151 call_marshaller(pStubMsg
, pArg
, pTypeFormat
);
1154 if (pParam
->param_direction
== RPC_FC_IN_PARAM_BASETYPE
)
1155 call_freer(pStubMsg
, pArg
, pTypeFormat
);
1157 case STUBLESS_UNMARSHAL
:
1158 if (pParam
->param_direction
== RPC_FC_IN_PARAM_BASETYPE
)
1159 call_unmarshaller(pStubMsg
, &pArg
, pTypeFormat
, 0);
1161 case STUBLESS_CALCSIZE
:
1162 if (pParam
->param_direction
== RPC_FC_RETURN_PARAM_BASETYPE
)
1163 call_buffer_sizer(pStubMsg
, pArg
, pTypeFormat
);
1166 RpcRaiseException(RPC_S_INTERNAL_ERROR
);
1169 current_stack_offset
+= type_stack_size(*pTypeFormat
);
1170 current_offset
+= sizeof(NDR_PARAM_OI_BASETYPE
);
1174 const NDR_PARAM_OI_OTHER
*pParamOther
=
1175 (const NDR_PARAM_OI_OTHER
*)&pFormat
[current_offset
];
1177 const unsigned char * pTypeFormat
=
1178 &pStubMsg
->StubDesc
->pFormatTypes
[pParamOther
->type_offset
];
1180 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat
);
1182 if (pParam
->param_direction
== RPC_FC_RETURN_PARAM
)
1183 retval_ptr
= (LONG_PTR
*)pArg
;
1187 case STUBLESS_MARSHAL
:
1188 if (pParam
->param_direction
== RPC_FC_OUT_PARAM
||
1189 pParam
->param_direction
== RPC_FC_IN_OUT_PARAM
||
1190 pParam
->param_direction
== RPC_FC_RETURN_PARAM
)
1191 call_marshaller(pStubMsg
, *(unsigned char **)pArg
, pTypeFormat
);
1194 if (pParam
->param_direction
== RPC_FC_IN_OUT_PARAM
||
1195 pParam
->param_direction
== RPC_FC_IN_PARAM
)
1196 call_freer(pStubMsg
, *(unsigned char **)pArg
, pTypeFormat
);
1197 else if (pParam
->param_direction
== RPC_FC_OUT_PARAM
)
1198 pStubMsg
->pfnFree(*(void **)pArg
);
1200 case STUBLESS_INITOUT
:
1201 if (pParam
->param_direction
== RPC_FC_OUT_PARAM
)
1203 DWORD size
= calc_arg_size(pStubMsg
, pTypeFormat
);
1207 *(void **)pArg
= NdrAllocate(pStubMsg
, size
);
1208 memset(*(void **)pArg
, 0, size
);
1212 case STUBLESS_UNMARSHAL
:
1213 if (pParam
->param_direction
== RPC_FC_IN_OUT_PARAM
||
1214 pParam
->param_direction
== RPC_FC_IN_PARAM
)
1215 call_unmarshaller(pStubMsg
, (unsigned char **)pArg
, pTypeFormat
, 0);
1217 case STUBLESS_CALCSIZE
:
1218 if (pParam
->param_direction
== RPC_FC_OUT_PARAM
||
1219 pParam
->param_direction
== RPC_FC_IN_OUT_PARAM
||
1220 pParam
->param_direction
== RPC_FC_RETURN_PARAM
)
1221 call_buffer_sizer(pStubMsg
, *(unsigned char **)pArg
, pTypeFormat
);
1224 RpcRaiseException(RPC_S_INTERNAL_ERROR
);
1227 current_stack_offset
+= pParamOther
->stack_size
* sizeof(INT
);
1228 current_offset
+= sizeof(NDR_PARAM_OI_OTHER
);
1235 /***********************************************************************
1236 * NdrStubCall2 [RPCRT4.@]
1238 * Unmarshals [in] parameters, calls either a method in an object or a server
1239 * function, marshals any [out] parameters and frees any allocated data.
1242 * Used by stubless MIDL-generated code.
1244 LONG WINAPI
NdrStubCall2(
1245 struct IRpcStubBuffer
* pThis
,
1246 struct IRpcChannelBuffer
* pChannel
,
1247 PRPC_MESSAGE pRpcMsg
,
1248 DWORD
* pdwStubPhase
)
1250 const MIDL_SERVER_INFO
*pServerInfo
;
1251 const MIDL_STUB_DESC
*pStubDesc
;
1252 PFORMAT_STRING pFormat
;
1253 MIDL_STUB_MESSAGE stubMsg
;
1254 /* pointer to start of stack to pass into stub implementation */
1255 unsigned char * args
;
1257 unsigned short stack_size
;
1258 /* number of parameters. optional for client to give it to us */
1259 unsigned char number_of_params
= ~0;
1260 /* cache of Oif_flags from v2 procedure header */
1261 INTERPRETER_OPT_FLAGS Oif_flags
= { 0 };
1262 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1263 INTERPRETER_OPT_FLAGS2 ext_flags
= { 0 };
1264 /* the type of pass we are currently doing */
1266 /* header for procedure string */
1267 const NDR_PROC_HEADER
*pProcHeader
;
1268 /* offset in format string for start of params */
1269 int parameter_start_offset
;
1270 /* current format string offset */
1272 /* -Oif or -Oicf generated format */
1273 BOOL bV2Format
= FALSE
;
1274 /* location to put retval into */
1275 LONG_PTR
*retval_ptr
= NULL
;
1277 TRACE("pThis %p, pChannel %p, pRpcMsg %p, pdwStubPhase %p\n", pThis
, pChannel
, pRpcMsg
, pdwStubPhase
);
1280 pServerInfo
= CStdStubBuffer_GetServerInfo(pThis
);
1282 pServerInfo
= ((RPC_SERVER_INTERFACE
*)pRpcMsg
->RpcInterfaceInformation
)->InterpreterInfo
;
1284 pStubDesc
= pServerInfo
->pStubDesc
;
1285 pFormat
= pServerInfo
->ProcString
+ pServerInfo
->FmtStringOffset
[pRpcMsg
->ProcNum
];
1286 pProcHeader
= (const NDR_PROC_HEADER
*)&pFormat
[0];
1288 TRACE("NDR Version: 0x%x\n", pStubDesc
->Version
);
1290 /* create the full pointer translation tables, if requested */
1291 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_FULLPTR
)
1292 stubMsg
.FullPtrXlatTables
= NdrFullPointerXlatInit(0,XLAT_SERVER
);
1294 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_RPCFLAGS
)
1296 const NDR_PROC_HEADER_RPC
*pProcHeader
= (const NDR_PROC_HEADER_RPC
*)&pFormat
[0];
1297 stack_size
= pProcHeader
->stack_size
;
1298 current_offset
= sizeof(NDR_PROC_HEADER_RPC
);
1303 stack_size
= pProcHeader
->stack_size
;
1304 current_offset
= sizeof(NDR_PROC_HEADER
);
1307 TRACE("Oi_flags = 0x%02x\n", pProcHeader
->Oi_flags
);
1310 switch (pProcHeader
->handle_type
)
1312 /* explicit binding: parse additional section */
1313 case RPC_FC_BIND_EXPLICIT
:
1314 switch (pFormat
[current_offset
]) /* handle_type */
1316 case RPC_FC_BIND_PRIMITIVE
: /* explicit primitive */
1317 current_offset
+= sizeof(NDR_EHD_PRIMITIVE
);
1319 case RPC_FC_BIND_GENERIC
: /* explicit generic */
1320 current_offset
+= sizeof(NDR_EHD_GENERIC
);
1322 case RPC_FC_BIND_CONTEXT
: /* explicit context */
1323 current_offset
+= sizeof(NDR_EHD_CONTEXT
);
1326 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader
->handle_type
);
1327 RpcRaiseException(RPC_X_BAD_STUB_DATA
);
1330 case RPC_FC_BIND_GENERIC
: /* implicit generic */
1331 case RPC_FC_BIND_PRIMITIVE
: /* implicit primitive */
1332 case RPC_FC_CALLBACK_HANDLE
: /* implicit callback */
1333 case RPC_FC_AUTO_HANDLE
: /* implicit auto handle */
1336 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader
->handle_type
);
1337 RpcRaiseException(RPC_X_BAD_STUB_DATA
);
1340 bV2Format
= (pStubDesc
->Version
>= 0x20000);
1344 const NDR_PROC_PARTIAL_OIF_HEADER
*pOIFHeader
=
1345 (const NDR_PROC_PARTIAL_OIF_HEADER
*)&pFormat
[current_offset
];
1347 Oif_flags
= pOIFHeader
->Oi2Flags
;
1348 number_of_params
= pOIFHeader
->number_of_params
;
1350 current_offset
+= sizeof(NDR_PROC_PARTIAL_OIF_HEADER
);
1353 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags
);
1355 if (Oif_flags
.HasExtensions
)
1357 const NDR_PROC_HEADER_EXTS
*pExtensions
=
1358 (const NDR_PROC_HEADER_EXTS
*)&pFormat
[current_offset
];
1359 ext_flags
= pExtensions
->Flags2
;
1360 current_offset
+= pExtensions
->Size
;
1363 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_OBJECT
)
1364 NdrStubInitialize(pRpcMsg
, &stubMsg
, pStubDesc
, pChannel
);
1366 NdrServerInitializeNew(pRpcMsg
, &stubMsg
, pStubDesc
);
1368 /* store the RPC flags away */
1369 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_RPCFLAGS
)
1370 pRpcMsg
->RpcFlags
= ((const NDR_PROC_HEADER_RPC
*)pProcHeader
)->rpc_flags
;
1372 /* use alternate memory allocation routines */
1373 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_RPCSSALLOC
)
1375 NdrRpcSsEnableAllocate(&stubMsg
);
1377 FIXME("Set RPCSS memory allocation routines\n");
1380 if (Oif_flags
.HasPipes
)
1382 FIXME("pipes not supported yet\n");
1383 RpcRaiseException(RPC_X_WRONG_STUB_VERSION
); /* FIXME: remove when implemented */
1384 /* init pipes package */
1385 /* NdrPipesInitialize(...) */
1387 if (ext_flags
.HasNewCorrDesc
)
1389 /* initialize extra correlation package */
1390 FIXME("new correlation description not implemented\n");
1391 stubMsg
.fHasNewCorrDesc
= TRUE
;
1394 /* convert strings, floating point values and endianess into our
1395 * preferred format */
1396 if ((pRpcMsg
->DataRepresentation
& 0x0000FFFFUL
) != NDR_LOCAL_DATA_REPRESENTATION
)
1397 NdrConvert(&stubMsg
, pFormat
);
1399 parameter_start_offset
= current_offset
;
1401 TRACE("allocating memory for stack of size %x\n", stack_size
);
1403 args
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, stack_size
);
1404 stubMsg
.StackTop
= args
; /* used by conformance of top-level objects */
1406 /* add the implicit This pointer as the first arg to the function if we
1407 * are calling an object method */
1409 *(void **)args
= ((CStdStubBuffer
*)pThis
)->pvServerObject
;
1412 * 1. STUBLESS_UNMARHSAL - unmarshal [in] params from buffer
1413 * 2. STUBLESS_CALLSERVER - send/receive buffer
1414 * 3. STUBLESS_CALCSIZE - get [out] buffer size
1415 * 4. STUBLESS_GETBUFFER - allocate [out] buffer
1416 * 5. STUBLESS_MARHSAL - marshal [out] params to buffer
1418 for (phase
= STUBLESS_UNMARSHAL
; phase
<= STUBLESS_FREE
; phase
++)
1420 TRACE("phase = %d\n", phase
);
1423 case STUBLESS_CALLSERVER
:
1424 /* call the server function */
1425 if (pServerInfo
->ThunkTable
&& pServerInfo
->ThunkTable
[pRpcMsg
->ProcNum
])
1426 pServerInfo
->ThunkTable
[pRpcMsg
->ProcNum
](&stubMsg
);
1429 SERVER_ROUTINE func
;
1432 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_OBJECT
)
1434 SERVER_ROUTINE
*vtbl
= *(SERVER_ROUTINE
**)((CStdStubBuffer
*)pThis
)->pvServerObject
;
1435 func
= vtbl
[pRpcMsg
->ProcNum
];
1438 func
= pServerInfo
->DispatchTable
[pRpcMsg
->ProcNum
];
1440 /* FIXME: what happens with return values that don't fit into a single register on x86? */
1441 retval
= call_server_func(func
, args
, stack_size
);
1445 TRACE("stub implementation returned 0x%lx\n", retval
);
1446 *retval_ptr
= retval
;
1449 TRACE("void stub implementation\n");
1452 stubMsg
.Buffer
= NULL
;
1453 stubMsg
.BufferLength
= 0;
1456 case STUBLESS_GETBUFFER
:
1457 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_OBJECT
)
1458 NdrStubGetBuffer(pThis
, pChannel
, &stubMsg
);
1463 pRpcMsg
->BufferLength
= stubMsg
.BufferLength
;
1464 /* allocate buffer for [out] and [ret] params */
1465 Status
= I_RpcGetBuffer(pRpcMsg
);
1467 RpcRaiseException(Status
);
1468 stubMsg
.Buffer
= pRpcMsg
->Buffer
;
1471 case STUBLESS_UNMARSHAL
:
1472 case STUBLESS_INITOUT
:
1473 case STUBLESS_CALCSIZE
:
1474 case STUBLESS_MARSHAL
:
1477 retval_ptr
= stub_do_args(&stubMsg
, &pFormat
[parameter_start_offset
],
1478 phase
, args
, number_of_params
);
1480 retval_ptr
= stub_do_old_args(&stubMsg
, &pFormat
[parameter_start_offset
],
1481 phase
, args
, stack_size
,
1482 (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_OBJECT
));
1486 ERR("shouldn't reach here. phase %d\n", phase
);
1491 pRpcMsg
->BufferLength
= (unsigned int)(stubMsg
.Buffer
- (unsigned char *)pRpcMsg
->Buffer
);
1493 if (ext_flags
.HasNewCorrDesc
)
1495 /* free extra correlation package */
1496 /* NdrCorrelationFree(&stubMsg); */
1499 if (Oif_flags
.HasPipes
)
1501 /* NdrPipesDone(...) */
1504 /* free the full pointer translation tables */
1505 if (pProcHeader
->Oi_flags
& RPC_FC_PROC_OIF_FULLPTR
)
1506 NdrFullPointerXlatFree(stubMsg
.FullPtrXlatTables
);
1508 /* free server function stack */
1509 HeapFree(GetProcessHeap(), 0, args
);
1514 /***********************************************************************
1515 * NdrServerCall2 [RPCRT4.@]
1517 void WINAPI
NdrServerCall2(PRPC_MESSAGE pRpcMsg
)
1520 NdrStubCall2(NULL
, NULL
, pRpcMsg
, &dwPhase
);