* Sync with recent trunk (r52637).
[reactos.git] / dll / win32 / rpcrt4 / ndr_stubless.c
1 /*
2 * NDR -Oi,-Oif,-Oicf Interpreter
3 *
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2003-5 Robert Shearman (for CodeWeavers)
6 *
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.
11 *
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.
16 *
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
20 *
21 * TODO:
22 * - Pipes
23 * - Some types of binding handles
24 */
25
26 #include "config.h"
27 #include "wine/port.h"
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36
37 #include "objbase.h"
38 #include "rpc.h"
39 #include "rpcproxy.h"
40
41 #include "wine/exception.h"
42 #include "wine/debug.h"
43 #include "wine/rpcfc.h"
44
45 #include "cpsf.h"
46 #include "ndr_misc.h"
47 #include "ndr_stubless.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
50
51 #define NDR_TABLE_MASK 127
52
53 static inline void call_buffer_sizer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
54 {
55 NDR_BUFFERSIZE m = NdrBufferSizer[pFormat[0] & NDR_TABLE_MASK];
56 if (m) m(pStubMsg, pMemory, pFormat);
57 else
58 {
59 FIXME("format type 0x%x not implemented\n", pFormat[0]);
60 RpcRaiseException(RPC_X_BAD_STUB_DATA);
61 }
62 }
63
64 static inline unsigned char *call_marshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
65 {
66 NDR_MARSHALL m = NdrMarshaller[pFormat[0] & NDR_TABLE_MASK];
67 if (m) return m(pStubMsg, pMemory, pFormat);
68 else
69 {
70 FIXME("format type 0x%x not implemented\n", pFormat[0]);
71 RpcRaiseException(RPC_X_BAD_STUB_DATA);
72 return NULL;
73 }
74 }
75
76 static inline unsigned char *call_unmarshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char **ppMemory, PFORMAT_STRING pFormat, unsigned char fMustAlloc)
77 {
78 NDR_UNMARSHALL m = NdrUnmarshaller[pFormat[0] & NDR_TABLE_MASK];
79 if (m) return m(pStubMsg, ppMemory, pFormat, fMustAlloc);
80 else
81 {
82 FIXME("format type 0x%x not implemented\n", pFormat[0]);
83 RpcRaiseException(RPC_X_BAD_STUB_DATA);
84 return NULL;
85 }
86 }
87
88 static inline void call_freer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
89 {
90 NDR_FREE m = NdrFreer[pFormat[0] & NDR_TABLE_MASK];
91 if (m) m(pStubMsg, pMemory, pFormat);
92 }
93
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
101
102 void WINAPI NdrRpcSmSetClientToOsf(PMIDL_STUB_MESSAGE pMessage)
103 {
104 #if 0 /* these functions are not defined yet */
105 pMessage->pfnAllocate = NdrRpcSmClientAllocate;
106 pMessage->pfnFree = NdrRpcSmClientFree;
107 #endif
108 }
109
110 static void dump_RPC_FC_PROC_PF(PARAM_ATTRIBUTES param_attributes)
111 {
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);
124 }
125
126 static void dump_INTERPRETER_OPT_FLAGS(INTERPRETER_OPT_FLAGS Oi2Flags)
127 {
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");
137 }
138
139 #define ARG_FROM_OFFSET(args, offset) ((args) + (offset))
140
141 static PFORMAT_STRING client_get_handle(
142 PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
143 PFORMAT_STRING pFormat, handle_t *phBinding)
144 {
145 /* binding */
146 switch (pProcHeader->handle_type)
147 {
148 /* explicit binding: parse additional section */
149 case RPC_FC_BIND_EXPLICIT:
150 switch (*pFormat) /* handle_type */
151 {
152 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
153 {
154 const NDR_EHD_PRIMITIVE *pDesc = (const NDR_EHD_PRIMITIVE *)pFormat;
155
156 TRACE("Explicit primitive handle @ %d\n", pDesc->offset);
157
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);
163 }
164 case RPC_FC_BIND_GENERIC: /* explicit generic */
165 {
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;
170
171 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
172
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);
181 }
182 case RPC_FC_BIND_CONTEXT: /* explicit context */
183 {
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)
188 {
189 TRACE("\tHANDLE_PARAM_IS_VIA_PTR\n");
190 context_handle = **(NDR_CCONTEXT **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
191 }
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)
196 {
197 ERR("null context handle isn't allowed\n");
198 RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);
199 return NULL;
200 }
201 *phBinding = NDRCContextBinding(context_handle);
202 /* FIXME: should we store this structure in stubMsg.pContext? */
203 return pFormat + sizeof(NDR_EHD_CONTEXT);
204 }
205 default:
206 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
207 RpcRaiseException(RPC_X_BAD_STUB_DATA);
208 }
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);
232 }
233 return pFormat;
234 }
235
236 static void client_free_handle(
237 PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
238 PFORMAT_STRING pFormat, handle_t hBinding)
239 {
240 /* binding */
241 switch (pProcHeader->handle_type)
242 {
243 /* explicit binding: parse additional section */
244 case RPC_FC_BIND_EXPLICIT:
245 switch (*pFormat) /* handle_type */
246 {
247 case RPC_FC_BIND_GENERIC: /* explicit generic */
248 {
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;
253
254 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
255
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;
264 }
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);
271 }
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);
284 }
285 }
286
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)
290 {
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;
297
298 for (i = 0; i < number_of_params; i++)
299 {
300 const NDR_PARAM_OIF_BASETYPE *pParam =
301 (const NDR_PARAM_OIF_BASETYPE *)&pFormat[current_offset];
302 unsigned char * pArg;
303
304 current_stack_offset = pParam->stack_offset;
305 pArg = ARG_FROM_OFFSET(args, current_stack_offset);
306
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);
311
312 if (pParam->param_attributes.IsBasetype)
313 {
314 const unsigned char * pTypeFormat =
315 &pParam->type_format_char;
316
317 if (pParam->param_attributes.IsSimpleRef)
318 pArg = *(unsigned char **)pArg;
319
320 TRACE("\tbase type: 0x%02x\n", *pTypeFormat);
321
322 switch (phase)
323 {
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)
334 {
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);
340 }
341 break;
342 default:
343 RpcRaiseException(RPC_S_INTERNAL_ERROR);
344 }
345
346 current_offset += sizeof(NDR_PARAM_OIF_BASETYPE);
347 }
348 else
349 {
350 const NDR_PARAM_OIF_OTHER *pParamOther =
351 (const NDR_PARAM_OIF_OTHER *)&pFormat[current_offset];
352
353 const unsigned char * pTypeFormat =
354 &(pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset]);
355
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)
359 {
360 if (!*(unsigned char **)pArg)
361 RpcRaiseException(RPC_X_NULL_REF_POINTER);
362 }
363
364 TRACE("\tcomplex type: 0x%02x\n", *pTypeFormat);
365
366 switch (phase)
367 {
368 case PROXY_CALCSIZE:
369 if (pParam->param_attributes.IsIn)
370 {
371 if (pParam->param_attributes.IsByValue)
372 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
373 else
374 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
375 }
376 break;
377 case PROXY_MARSHAL:
378 if (pParam->param_attributes.IsIn)
379 {
380 if (pParam->param_attributes.IsByValue)
381 call_marshaller(pStubMsg, pArg, pTypeFormat);
382 else
383 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
384 }
385 break;
386 case PROXY_UNMARSHAL:
387 if (pParam->param_attributes.IsOut)
388 {
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);
395 }
396 break;
397 default:
398 RpcRaiseException(RPC_S_INTERNAL_ERROR);
399 }
400
401 current_offset += sizeof(NDR_PARAM_OIF_OTHER);
402 }
403 TRACE("\tmemory addr (after): %p\n", pArg);
404 }
405 }
406
407 static unsigned int type_stack_size(unsigned char fc)
408 {
409 switch (fc)
410 {
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);
438 }
439 }
440
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)
445 {
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;
452
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.
456 */
457 for (i = 0; TRUE; i++)
458 {
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);
466
467 /* no more parameters; exit loop */
468 if (current_stack_offset_adjusted >= stack_size)
469 break;
470
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);
475
476 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE ||
477 pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
478 {
479 const unsigned char * pTypeFormat =
480 &pParam->type_format_char;
481
482 TRACE("\tbase type 0x%02x\n", *pTypeFormat);
483
484 switch (phase)
485 {
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)
497 {
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);
502 }
503 break;
504 default:
505 RpcRaiseException(RPC_S_INTERNAL_ERROR);
506 }
507
508 current_stack_offset += type_stack_size(*pTypeFormat);
509 current_offset += sizeof(NDR_PARAM_OI_BASETYPE);
510 }
511 else
512 {
513 const NDR_PARAM_OI_OTHER *pParamOther =
514 (const NDR_PARAM_OI_OTHER *)&pFormat[current_offset];
515
516 const unsigned char *pTypeFormat =
517 &pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset];
518
519 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
520
521 switch (phase)
522 {
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);
542 }
543
544 current_stack_offset += pParamOther->stack_size * sizeof(INT);
545 current_offset += sizeof(NDR_PARAM_OI_OTHER);
546 }
547 TRACE("\tmemory addr (after): %p\n", pArg);
548 }
549 }
550
551 CLIENT_CALL_RETURN WINAPIV NdrClientCall2(PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat, ...)
552 {
553 /* pointer to start of stack where arguments start */
554 RPC_MESSAGE rpcMsg;
555 MIDL_STUB_MESSAGE stubMsg;
556 handle_t hBinding = NULL;
557 /* procedure number */
558 unsigned short procedure_number;
559 /* size of stack */
560 unsigned short stack_size;
561 /* number of parameters. optional for client to give it to us */
562 unsigned char number_of_params = ~0;
563 /* cache of Oif_flags from v2 procedure header */
564 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
565 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
566 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
567 /* the type of pass we are currently doing */
568 int phase;
569 /* header for procedure string */
570 const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
571 /* -Oif or -Oicf generated format */
572 BOOL bV2Format = FALSE;
573 /* the value to return to the client from the remote procedure */
574 CLIENT_CALL_RETURN RetVal = {0};
575 /* the pointer to the object when in OLE mode */
576 void * This = NULL;
577 PFORMAT_STRING pHandleFormat;
578 /* correlation cache */
579 unsigned long NdrCorrCache[256];
580
581 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
582
583 TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
584
585 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
586 {
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);
591 }
592 else
593 {
594 stack_size = pProcHeader->stack_size;
595 procedure_number = pProcHeader->proc_num;
596 pFormat += sizeof(NDR_PROC_HEADER);
597 }
598 TRACE("stack size: 0x%x\n", stack_size);
599 TRACE("proc num: %d\n", procedure_number);
600
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);
604
605 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
606 {
607 /* object is always the first argument */
608 This = **(void *const **)(&pFormat+1);
609 NdrProxyInitialize(This, &rpcMsg, &stubMsg, pStubDesc, procedure_number);
610 }
611 else
612 NdrClientInitializeNew(&rpcMsg, &stubMsg, pStubDesc, procedure_number);
613
614 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
615 TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
616
617 /* needed for conformance of top-level objects */
618 #ifdef __i386__
619 stubMsg.StackTop = *(unsigned char **)(&pFormat+1);
620 #else
621 # warning Stack not retrieved for your CPU architecture
622 #endif
623
624 pHandleFormat = pFormat;
625
626 /* we only need a handle if this isn't an object method */
627 if (!(pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT))
628 {
629 pFormat = client_get_handle(&stubMsg, pProcHeader, pHandleFormat, &hBinding);
630 if (!pFormat) return RetVal;
631 }
632
633 bV2Format = (pStubDesc->Version >= 0x20000);
634
635 if (bV2Format)
636 {
637 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
638 (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
639
640 Oif_flags = pOIFHeader->Oi2Flags;
641 number_of_params = pOIFHeader->number_of_params;
642
643 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
644 }
645
646 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
647
648 if (Oif_flags.HasExtensions)
649 {
650 const NDR_PROC_HEADER_EXTS *pExtensions =
651 (const NDR_PROC_HEADER_EXTS *)pFormat;
652 ext_flags = pExtensions->Flags2;
653 pFormat += pExtensions->Size;
654 }
655
656 stubMsg.BufferLength = 0;
657
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;
661
662 /* use alternate memory allocation routines */
663 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
664 NdrRpcSmSetClientToOsf(&stubMsg);
665
666 if (Oif_flags.HasPipes)
667 {
668 FIXME("pipes not supported yet\n");
669 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
670 /* init pipes package */
671 /* NdrPipesInitialize(...) */
672 }
673 if (ext_flags.HasNewCorrDesc)
674 {
675 /* initialize extra correlation package */
676 NdrCorrelationInitialize(&stubMsg, NdrCorrCache, sizeof(NdrCorrCache), 0);
677 }
678
679 /* order of phases:
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
685 */
686 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
687 {
688 __TRY
689 {
690 for (phase = PROXY_CALCSIZE; phase <= PROXY_UNMARSHAL; phase++)
691 {
692 TRACE("phase = %d\n", phase);
693 switch (phase)
694 {
695 case PROXY_GETBUFFER:
696 /* allocate the buffer */
697 NdrProxyGetBuffer(This, &stubMsg);
698 break;
699 case PROXY_SENDRECEIVE:
700 /* send the [in] params and receive the [out] and [retval]
701 * params */
702 NdrProxySendReceive(This, &stubMsg);
703
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);
708
709 break;
710 case PROXY_CALCSIZE:
711 case PROXY_MARSHAL:
712 case PROXY_UNMARSHAL:
713 if (bV2Format)
714 client_do_args(&stubMsg, pFormat, phase, stubMsg.StackTop,
715 number_of_params, (unsigned char *)&RetVal);
716 else
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);
720 break;
721 default:
722 ERR("shouldn't reach here. phase %d\n", phase);
723 break;
724 }
725 }
726 }
727 __EXCEPT_ALL
728 {
729 RetVal.Simple = NdrProxyErrorHandler(GetExceptionCode());
730 }
731 __ENDTRY
732 }
733 else
734 {
735 /* order of phases:
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
741 */
742 for (phase = PROXY_CALCSIZE; phase <= PROXY_UNMARSHAL; phase++)
743 {
744 TRACE("phase = %d\n", phase);
745 switch (phase)
746 {
747 case PROXY_GETBUFFER:
748 /* allocate the buffer */
749 if (Oif_flags.HasPipes)
750 /* NdrGetPipeBuffer(...) */
751 FIXME("pipes not supported yet\n");
752 else
753 {
754 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
755 #if 0
756 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
757 #else
758 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
759 #endif
760 else
761 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
762 }
763 break;
764 case PROXY_SENDRECEIVE:
765 /* send the [in] params and receive the [out] and [retval]
766 * params */
767 if (Oif_flags.HasPipes)
768 /* NdrPipesSendReceive(...) */
769 FIXME("pipes not supported yet\n");
770 else
771 {
772 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
773 #if 0
774 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
775 #else
776 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
777 #endif
778 else
779 NdrSendReceive(&stubMsg, stubMsg.Buffer);
780 }
781
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);
786
787 break;
788 case PROXY_CALCSIZE:
789 case PROXY_MARSHAL:
790 case PROXY_UNMARSHAL:
791 if (bV2Format)
792 client_do_args(&stubMsg, pFormat, phase, stubMsg.StackTop,
793 number_of_params, (unsigned char *)&RetVal);
794 else
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);
798 break;
799 default:
800 ERR("shouldn't reach here. phase %d\n", phase);
801 break;
802 }
803 }
804 }
805
806 if (ext_flags.HasNewCorrDesc)
807 {
808 /* free extra correlation package */
809 NdrCorrelationFree(&stubMsg);
810 }
811
812 if (Oif_flags.HasPipes)
813 {
814 /* NdrPipesDone(...) */
815 }
816
817 /* free the full pointer translation tables */
818 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
819 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
820
821 /* free marshalling buffer */
822 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
823 NdrProxyFreeBuffer(This, &stubMsg);
824 else
825 {
826 NdrFreeBuffer(&stubMsg);
827 client_free_handle(&stubMsg, pProcHeader, pHandleFormat, hBinding);
828 }
829
830 TRACE("RetVal = 0x%p\n", RetVal.Pointer);
831
832 return RetVal;
833 }
834
835 /* Calls a function with the specified arguments, restoring the stack
836 * properly afterwards as we don't know the calling convention of the
837 * function */
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)
840 {
841 __asm
842 {
843 push ebp
844 mov ebp, esp
845 push edi ; Save registers
846 push esi
847 mov eax, [ebp+16] ; Get stack size
848 sub esp, eax ; Make room in stack for arguments
849 and esp, 0xFFFFFFF0
850 mov edi, esp
851 mov ecx, eax
852 mov esi, [ebp+12]
853 shr ecx, 2
854 cld
855 rep movsd ; Copy dword blocks
856 call [ebp+8] ; Call function
857 lea esp, [ebp-8] ; Restore stack
858 pop esi ; Restore registers
859 pop edi
860 pop ebp
861 ret
862 }
863 }
864 #elif defined __i386__ && defined __GNUC__
865 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
866 __ASM_GLOBAL_FUNC(call_server_func,
867 "pushl %ebp\n\t"
868 "movl %esp, %ebp\n\t"
869 "pushl %edi\n\t" /* Save registers */
870 "pushl %esi\n\t"
871 "movl 16(%ebp), %eax\n\t" /* Get stack size */
872 "subl %eax, %esp\n\t" /* Make room in stack for arguments */
873 "andl $~15, %esp\n\t" /* Make sure stack has 16-byte alignment for Mac OS X */
874 "movl %esp, %edi\n\t"
875 "movl %eax, %ecx\n\t"
876 "movl 12(%ebp), %esi\n\t"
877 "shrl $2, %ecx\n\t" /* divide by 4 */
878 "cld\n\t"
879 "rep; movsl\n\t" /* Copy dword blocks */
880 "call *8(%ebp)\n\t" /* Call function */
881 "leal -8(%ebp), %esp\n\t" /* Restore stack */
882 "popl %esi\n\t" /* Restore registers */
883 "popl %edi\n\t"
884 "popl %ebp\n\t"
885 "ret\n" )
886 #else
887 #warning call_server_func not implemented for your architecture
888 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned short stack_size)
889 {
890 FIXME("Not implemented for your architecture\n");
891 return 0;
892 }
893 #endif
894
895 static DWORD calc_arg_size(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat)
896 {
897 DWORD size;
898 switch(*pFormat)
899 {
900 case RPC_FC_STRUCT:
901 size = *(const WORD*)(pFormat + 2);
902 break;
903 case RPC_FC_CARRAY:
904 size = *(const WORD*)(pFormat + 2);
905 ComputeConformance(pStubMsg, NULL, pFormat + 4, 0);
906 size *= pStubMsg->MaxCount;
907 break;
908 case RPC_FC_SMFARRAY:
909 size = *(const WORD*)(pFormat + 2);
910 break;
911 case RPC_FC_LGFARRAY:
912 size = *(const DWORD*)(pFormat + 2);
913 break;
914 case RPC_FC_BOGUS_ARRAY:
915 pFormat = ComputeConformance(pStubMsg, NULL, pFormat + 4, *(const WORD*)&pFormat[2]);
916 TRACE("conformance = %ld\n", pStubMsg->MaxCount);
917 pFormat = ComputeVariance(pStubMsg, NULL, pFormat, pStubMsg->MaxCount);
918 size = ComplexStructSize(pStubMsg, pFormat);
919 size *= pStubMsg->MaxCount;
920 break;
921 default:
922 FIXME("Unhandled type %02x\n", *pFormat);
923 /* fallthrough */
924 case RPC_FC_RP:
925 size = sizeof(void *);
926 break;
927 }
928 return size;
929 }
930
931 static LONG_PTR *stub_do_args(MIDL_STUB_MESSAGE *pStubMsg,
932 PFORMAT_STRING pFormat, int phase,
933 unsigned char *args,
934 unsigned short number_of_params)
935 {
936 /* counter */
937 unsigned short i;
938 /* current format string offset */
939 int current_offset = 0;
940 /* current stack offset */
941 unsigned short current_stack_offset = 0;
942 /* location to put retval into */
943 LONG_PTR *retval_ptr = NULL;
944
945 for (i = 0; i < number_of_params; i++)
946 {
947 const NDR_PARAM_OIF_BASETYPE *pParam =
948 (const NDR_PARAM_OIF_BASETYPE *)&pFormat[current_offset];
949 unsigned char *pArg;
950
951 current_stack_offset = pParam->stack_offset;
952 pArg = args + current_stack_offset;
953
954 TRACE("param[%d]: new format\n", i);
955 TRACE("\tparam_attributes:"); dump_RPC_FC_PROC_PF(pParam->param_attributes); TRACE("\n");
956 TRACE("\tstack_offset: 0x%x\n", current_stack_offset);
957 TRACE("\tmemory addr (before): %p -> %p\n", pArg, *(unsigned char **)pArg);
958
959 if (pParam->param_attributes.IsBasetype)
960 {
961 const unsigned char *pTypeFormat =
962 &pParam->type_format_char;
963
964 TRACE("\tbase type: 0x%02x\n", *pTypeFormat);
965
966 /* make a note of the address of the return value parameter for later */
967 if (pParam->param_attributes.IsReturn)
968 retval_ptr = (LONG_PTR *)pArg;
969
970 switch (phase)
971 {
972 case STUBLESS_MARSHAL:
973 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
974 {
975 if (pParam->param_attributes.IsSimpleRef)
976 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
977 else
978 call_marshaller(pStubMsg, pArg, pTypeFormat);
979 }
980 break;
981 case STUBLESS_FREE:
982 if (pParam->param_attributes.ServerAllocSize)
983 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
984 break;
985 case STUBLESS_INITOUT:
986 break;
987 case STUBLESS_UNMARSHAL:
988 if (pParam->param_attributes.ServerAllocSize)
989 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
990 pParam->param_attributes.ServerAllocSize * 8);
991
992 if (pParam->param_attributes.IsIn)
993 {
994 if (pParam->param_attributes.IsSimpleRef)
995 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
996 else
997 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
998 }
999 break;
1000 case STUBLESS_CALCSIZE:
1001 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1002 {
1003 if (pParam->param_attributes.IsSimpleRef)
1004 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1005 else
1006 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1007 }
1008 break;
1009 default:
1010 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1011 }
1012
1013 current_offset += sizeof(NDR_PARAM_OIF_BASETYPE);
1014 }
1015 else
1016 {
1017 const NDR_PARAM_OIF_OTHER *pParamOther =
1018 (const NDR_PARAM_OIF_OTHER *)&pFormat[current_offset];
1019
1020 const unsigned char * pTypeFormat =
1021 &(pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset]);
1022
1023 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1024
1025 switch (phase)
1026 {
1027 case STUBLESS_MARSHAL:
1028 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1029 {
1030 if (pParam->param_attributes.IsByValue)
1031 call_marshaller(pStubMsg, pArg, pTypeFormat);
1032 else
1033 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1034 }
1035 break;
1036 case STUBLESS_FREE:
1037 if (pParam->param_attributes.MustFree)
1038 {
1039 if (pParam->param_attributes.IsByValue)
1040 call_freer(pStubMsg, pArg, pTypeFormat);
1041 else
1042 call_freer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1043 }
1044
1045 if (pParam->param_attributes.IsOut &&
1046 !pParam->param_attributes.IsIn &&
1047 !pParam->param_attributes.IsByValue &&
1048 !pParam->param_attributes.ServerAllocSize)
1049 {
1050 pStubMsg->pfnFree(*(void **)pArg);
1051 }
1052
1053 if (pParam->param_attributes.ServerAllocSize)
1054 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
1055 break;
1056 case STUBLESS_INITOUT:
1057 if (!pParam->param_attributes.IsIn &&
1058 pParam->param_attributes.IsOut &&
1059 !pParam->param_attributes.ServerAllocSize &&
1060 !pParam->param_attributes.IsByValue)
1061 {
1062 DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1063
1064 if(size)
1065 {
1066 *(void **)pArg = NdrAllocate(pStubMsg, size);
1067 memset(*(void **)pArg, 0, size);
1068 }
1069 }
1070 break;
1071 case STUBLESS_UNMARSHAL:
1072 if (pParam->param_attributes.ServerAllocSize)
1073 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1074 pParam->param_attributes.ServerAllocSize * 8);
1075
1076 if (pParam->param_attributes.IsIn)
1077 {
1078 if (pParam->param_attributes.IsByValue)
1079 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1080 else
1081 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1082 }
1083 break;
1084 case STUBLESS_CALCSIZE:
1085 if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1086 {
1087 if (pParam->param_attributes.IsByValue)
1088 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1089 else
1090 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1091 }
1092 break;
1093 default:
1094 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1095 }
1096
1097 current_offset += sizeof(NDR_PARAM_OIF_OTHER);
1098 }
1099 TRACE("\tmemory addr (after): %p -> %p\n", pArg, *(unsigned char **)pArg);
1100 }
1101
1102 return retval_ptr;
1103 }
1104
1105 static LONG_PTR *stub_do_old_args(MIDL_STUB_MESSAGE *pStubMsg,
1106 PFORMAT_STRING pFormat, int phase,
1107 unsigned char *args,
1108 unsigned short stack_size, BOOL object)
1109 {
1110 /* counter */
1111 unsigned short i;
1112 /* current format string offset */
1113 int current_offset = 0;
1114 /* current stack offset */
1115 unsigned short current_stack_offset = 0;
1116 /* location to put retval into */
1117 LONG_PTR *retval_ptr = NULL;
1118
1119 for (i = 0; TRUE; i++)
1120 {
1121 const NDR_PARAM_OI_BASETYPE *pParam =
1122 (const NDR_PARAM_OI_BASETYPE *)&pFormat[current_offset];
1123 /* note: current_stack_offset starts after the This pointer
1124 * if present, so adjust this */
1125 unsigned short current_stack_offset_adjusted = current_stack_offset +
1126 (object ? sizeof(void *) : 0);
1127 unsigned char *pArg = args + current_stack_offset_adjusted;
1128
1129 /* no more parameters; exit loop */
1130 if (current_stack_offset_adjusted >= stack_size)
1131 break;
1132
1133 TRACE("param[%d]: old format\n", i);
1134 TRACE("\tparam_direction: 0x%x\n", pParam->param_direction);
1135 TRACE("\tstack_offset: 0x%x\n", current_stack_offset_adjusted);
1136
1137 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE ||
1138 pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1139 {
1140 const unsigned char *pTypeFormat =
1141 &pParam->type_format_char;
1142
1143 TRACE("\tbase type 0x%02x\n", *pTypeFormat);
1144
1145 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1146 retval_ptr = (LONG_PTR *)pArg;
1147
1148 switch (phase)
1149 {
1150 case STUBLESS_MARSHAL:
1151 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1152 call_marshaller(pStubMsg, pArg, pTypeFormat);
1153 break;
1154 case STUBLESS_FREE:
1155 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1156 call_freer(pStubMsg, pArg, pTypeFormat);
1157 break;
1158 case STUBLESS_UNMARSHAL:
1159 if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1160 call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1161 break;
1162 case STUBLESS_CALCSIZE:
1163 if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1164 call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1165 break;
1166 default:
1167 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1168 }
1169
1170 current_stack_offset += type_stack_size(*pTypeFormat);
1171 current_offset += sizeof(NDR_PARAM_OI_BASETYPE);
1172 }
1173 else
1174 {
1175 const NDR_PARAM_OI_OTHER *pParamOther =
1176 (const NDR_PARAM_OI_OTHER *)&pFormat[current_offset];
1177
1178 const unsigned char * pTypeFormat =
1179 &pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset];
1180
1181 TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1182
1183 if (pParam->param_direction == RPC_FC_RETURN_PARAM)
1184 retval_ptr = (LONG_PTR *)pArg;
1185
1186 switch (phase)
1187 {
1188 case STUBLESS_MARSHAL:
1189 if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1190 pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1191 pParam->param_direction == RPC_FC_RETURN_PARAM)
1192 call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1193 break;
1194 case STUBLESS_FREE:
1195 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1196 pParam->param_direction == RPC_FC_IN_PARAM)
1197 call_freer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1198 else if (pParam->param_direction == RPC_FC_OUT_PARAM)
1199 pStubMsg->pfnFree(*(void **)pArg);
1200 break;
1201 case STUBLESS_INITOUT:
1202 if (pParam->param_direction == RPC_FC_OUT_PARAM)
1203 {
1204 DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1205
1206 if(size)
1207 {
1208 *(void **)pArg = NdrAllocate(pStubMsg, size);
1209 memset(*(void **)pArg, 0, size);
1210 }
1211 }
1212 break;
1213 case STUBLESS_UNMARSHAL:
1214 if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1215 pParam->param_direction == RPC_FC_IN_PARAM)
1216 call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1217 break;
1218 case STUBLESS_CALCSIZE:
1219 if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1220 pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1221 pParam->param_direction == RPC_FC_RETURN_PARAM)
1222 call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1223 break;
1224 default:
1225 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1226 }
1227
1228 current_stack_offset += pParamOther->stack_size * sizeof(INT);
1229 current_offset += sizeof(NDR_PARAM_OI_OTHER);
1230 }
1231 }
1232
1233 return retval_ptr;
1234 }
1235
1236 /***********************************************************************
1237 * NdrStubCall2 [RPCRT4.@]
1238 *
1239 * Unmarshals [in] parameters, calls either a method in an object or a server
1240 * function, marshals any [out] parameters and frees any allocated data.
1241 *
1242 * NOTES
1243 * Used by stubless MIDL-generated code.
1244 */
1245 LONG WINAPI NdrStubCall2(
1246 struct IRpcStubBuffer * pThis,
1247 struct IRpcChannelBuffer * pChannel,
1248 PRPC_MESSAGE pRpcMsg,
1249 DWORD * pdwStubPhase)
1250 {
1251 const MIDL_SERVER_INFO *pServerInfo;
1252 const MIDL_STUB_DESC *pStubDesc;
1253 PFORMAT_STRING pFormat;
1254 MIDL_STUB_MESSAGE stubMsg;
1255 /* pointer to start of stack to pass into stub implementation */
1256 unsigned char * args;
1257 /* size of stack */
1258 unsigned short stack_size;
1259 /* number of parameters. optional for client to give it to us */
1260 unsigned char number_of_params = ~0;
1261 /* cache of Oif_flags from v2 procedure header */
1262 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1263 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1264 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1265 /* the type of pass we are currently doing */
1266 int phase;
1267 /* header for procedure string */
1268 const NDR_PROC_HEADER *pProcHeader;
1269 /* offset in format string for start of params */
1270 int parameter_start_offset;
1271 /* current format string offset */
1272 int current_offset;
1273 /* -Oif or -Oicf generated format */
1274 BOOL bV2Format = FALSE;
1275 /* location to put retval into */
1276 LONG_PTR *retval_ptr = NULL;
1277
1278 TRACE("pThis %p, pChannel %p, pRpcMsg %p, pdwStubPhase %p\n", pThis, pChannel, pRpcMsg, pdwStubPhase);
1279
1280 if (pThis)
1281 pServerInfo = CStdStubBuffer_GetServerInfo(pThis);
1282 else
1283 pServerInfo = ((RPC_SERVER_INTERFACE *)pRpcMsg->RpcInterfaceInformation)->InterpreterInfo;
1284
1285 pStubDesc = pServerInfo->pStubDesc;
1286 pFormat = pServerInfo->ProcString + pServerInfo->FmtStringOffset[pRpcMsg->ProcNum];
1287 pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1288
1289 TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
1290
1291 /* create the full pointer translation tables, if requested */
1292 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1293 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_SERVER);
1294
1295 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1296 {
1297 const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1298 stack_size = pProcHeader->stack_size;
1299 current_offset = sizeof(NDR_PROC_HEADER_RPC);
1300
1301 }
1302 else
1303 {
1304 stack_size = pProcHeader->stack_size;
1305 current_offset = sizeof(NDR_PROC_HEADER);
1306 }
1307
1308 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1309
1310 /* binding */
1311 switch (pProcHeader->handle_type)
1312 {
1313 /* explicit binding: parse additional section */
1314 case RPC_FC_BIND_EXPLICIT:
1315 switch (pFormat[current_offset]) /* handle_type */
1316 {
1317 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
1318 current_offset += sizeof(NDR_EHD_PRIMITIVE);
1319 break;
1320 case RPC_FC_BIND_GENERIC: /* explicit generic */
1321 current_offset += sizeof(NDR_EHD_GENERIC);
1322 break;
1323 case RPC_FC_BIND_CONTEXT: /* explicit context */
1324 current_offset += sizeof(NDR_EHD_CONTEXT);
1325 break;
1326 default:
1327 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1328 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1329 }
1330 break;
1331 case RPC_FC_BIND_GENERIC: /* implicit generic */
1332 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
1333 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
1334 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
1335 break;
1336 default:
1337 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1338 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1339 }
1340
1341 bV2Format = (pStubDesc->Version >= 0x20000);
1342
1343 if (bV2Format)
1344 {
1345 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
1346 (const NDR_PROC_PARTIAL_OIF_HEADER *)&pFormat[current_offset];
1347
1348 Oif_flags = pOIFHeader->Oi2Flags;
1349 number_of_params = pOIFHeader->number_of_params;
1350
1351 current_offset += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1352 }
1353
1354 TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
1355
1356 if (Oif_flags.HasExtensions)
1357 {
1358 const NDR_PROC_HEADER_EXTS *pExtensions =
1359 (const NDR_PROC_HEADER_EXTS *)&pFormat[current_offset];
1360 ext_flags = pExtensions->Flags2;
1361 current_offset += pExtensions->Size;
1362 }
1363
1364 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1365 NdrStubInitialize(pRpcMsg, &stubMsg, pStubDesc, pChannel);
1366 else
1367 NdrServerInitializeNew(pRpcMsg, &stubMsg, pStubDesc);
1368
1369 /* store the RPC flags away */
1370 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1371 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1372
1373 /* use alternate memory allocation routines */
1374 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
1375 #if 0
1376 NdrRpcSsEnableAllocate(&stubMsg);
1377 #else
1378 FIXME("Set RPCSS memory allocation routines\n");
1379 #endif
1380
1381 if (Oif_flags.HasPipes)
1382 {
1383 FIXME("pipes not supported yet\n");
1384 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1385 /* init pipes package */
1386 /* NdrPipesInitialize(...) */
1387 }
1388 if (ext_flags.HasNewCorrDesc)
1389 {
1390 /* initialize extra correlation package */
1391 FIXME("new correlation description not implemented\n");
1392 stubMsg.fHasNewCorrDesc = TRUE;
1393 }
1394
1395 /* convert strings, floating point values and endianess into our
1396 * preferred format */
1397 if ((pRpcMsg->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1398 NdrConvert(&stubMsg, pFormat);
1399
1400 parameter_start_offset = current_offset;
1401
1402 TRACE("allocating memory for stack of size %x\n", stack_size);
1403
1404 args = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, stack_size);
1405 stubMsg.StackTop = args; /* used by conformance of top-level objects */
1406
1407 /* add the implicit This pointer as the first arg to the function if we
1408 * are calling an object method */
1409 if (pThis)
1410 *(void **)args = ((CStdStubBuffer *)pThis)->pvServerObject;
1411
1412 /* order of phases:
1413 * 1. STUBLESS_UNMARHSAL - unmarshal [in] params from buffer
1414 * 2. STUBLESS_CALLSERVER - send/receive buffer
1415 * 3. STUBLESS_CALCSIZE - get [out] buffer size
1416 * 4. STUBLESS_GETBUFFER - allocate [out] buffer
1417 * 5. STUBLESS_MARHSAL - marshal [out] params to buffer
1418 */
1419 for (phase = STUBLESS_UNMARSHAL; phase <= STUBLESS_FREE; phase++)
1420 {
1421 TRACE("phase = %d\n", phase);
1422 switch (phase)
1423 {
1424 case STUBLESS_CALLSERVER:
1425 /* call the server function */
1426 if (pServerInfo->ThunkTable && pServerInfo->ThunkTable[pRpcMsg->ProcNum])
1427 pServerInfo->ThunkTable[pRpcMsg->ProcNum](&stubMsg);
1428 else
1429 {
1430 SERVER_ROUTINE func;
1431 LONG_PTR retval;
1432
1433 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1434 {
1435 SERVER_ROUTINE *vtbl = *(SERVER_ROUTINE **)((CStdStubBuffer *)pThis)->pvServerObject;
1436 func = vtbl[pRpcMsg->ProcNum];
1437 }
1438 else
1439 func = pServerInfo->DispatchTable[pRpcMsg->ProcNum];
1440
1441 /* FIXME: what happens with return values that don't fit into a single register on x86? */
1442 retval = call_server_func(func, args, stack_size);
1443
1444 if (retval_ptr)
1445 {
1446 TRACE("stub implementation returned 0x%lx\n", retval);
1447 *retval_ptr = retval;
1448 }
1449 else
1450 TRACE("void stub implementation\n");
1451 }
1452
1453 stubMsg.Buffer = NULL;
1454 stubMsg.BufferLength = 0;
1455
1456 break;
1457 case STUBLESS_GETBUFFER:
1458 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1459 NdrStubGetBuffer(pThis, pChannel, &stubMsg);
1460 else
1461 {
1462 RPC_STATUS Status;
1463
1464 pRpcMsg->BufferLength = stubMsg.BufferLength;
1465 /* allocate buffer for [out] and [ret] params */
1466 Status = I_RpcGetBuffer(pRpcMsg);
1467 if (Status)
1468 RpcRaiseException(Status);
1469 stubMsg.Buffer = pRpcMsg->Buffer;
1470 }
1471 break;
1472 case STUBLESS_UNMARSHAL:
1473 case STUBLESS_INITOUT:
1474 case STUBLESS_CALCSIZE:
1475 case STUBLESS_MARSHAL:
1476 case STUBLESS_FREE:
1477 if (bV2Format)
1478 retval_ptr = stub_do_args(&stubMsg, &pFormat[parameter_start_offset],
1479 phase, args, number_of_params);
1480 else
1481 retval_ptr = stub_do_old_args(&stubMsg, &pFormat[parameter_start_offset],
1482 phase, args, stack_size,
1483 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT));
1484
1485 break;
1486 default:
1487 ERR("shouldn't reach here. phase %d\n", phase);
1488 break;
1489 }
1490 }
1491
1492 pRpcMsg->BufferLength = (unsigned int)(stubMsg.Buffer - (unsigned char *)pRpcMsg->Buffer);
1493
1494 if (ext_flags.HasNewCorrDesc)
1495 {
1496 /* free extra correlation package */
1497 /* NdrCorrelationFree(&stubMsg); */
1498 }
1499
1500 if (Oif_flags.HasPipes)
1501 {
1502 /* NdrPipesDone(...) */
1503 }
1504
1505 /* free the full pointer translation tables */
1506 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1507 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
1508
1509 /* free server function stack */
1510 HeapFree(GetProcessHeap(), 0, args);
1511
1512 return S_OK;
1513 }
1514
1515 /***********************************************************************
1516 * NdrServerCall2 [RPCRT4.@]
1517 */
1518 void WINAPI NdrServerCall2(PRPC_MESSAGE pRpcMsg)
1519 {
1520 DWORD dwPhase;
1521 NdrStubCall2(NULL, NULL, pRpcMsg, &dwPhase);
1522 }