[RPCRT4]
[reactos.git] / reactos / 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 "precomp.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
29
30 #define NDR_TABLE_MASK 127
31
32 static inline void call_buffer_sizer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
33 const NDR_PARAM_OIF *param)
34 {
35 PFORMAT_STRING pFormat;
36 NDR_BUFFERSIZE m;
37
38 if (param->attr.IsBasetype)
39 {
40 pFormat = &param->u.type_format_char;
41 if (param->attr.IsSimpleRef) pMemory = *(unsigned char **)pMemory;
42 }
43 else
44 {
45 pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
46 if (!param->attr.IsByValue) pMemory = *(unsigned char **)pMemory;
47 }
48
49 m = NdrBufferSizer[pFormat[0] & NDR_TABLE_MASK];
50 if (m) m(pStubMsg, pMemory, pFormat);
51 else
52 {
53 FIXME("format type 0x%x not implemented\n", pFormat[0]);
54 RpcRaiseException(RPC_X_BAD_STUB_DATA);
55 }
56 }
57
58 static inline unsigned char *call_marshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
59 const NDR_PARAM_OIF *param)
60 {
61 PFORMAT_STRING pFormat;
62 NDR_MARSHALL m;
63
64 if (param->attr.IsBasetype)
65 {
66 pFormat = &param->u.type_format_char;
67 if (param->attr.IsSimpleRef) pMemory = *(unsigned char **)pMemory;
68 }
69 else
70 {
71 pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
72 if (!param->attr.IsByValue) pMemory = *(unsigned char **)pMemory;
73 }
74
75 m = NdrMarshaller[pFormat[0] & NDR_TABLE_MASK];
76 if (m) return m(pStubMsg, pMemory, pFormat);
77 else
78 {
79 FIXME("format type 0x%x not implemented\n", pFormat[0]);
80 RpcRaiseException(RPC_X_BAD_STUB_DATA);
81 return NULL;
82 }
83 }
84
85 static inline unsigned char *call_unmarshaller(PMIDL_STUB_MESSAGE pStubMsg, unsigned char **ppMemory,
86 const NDR_PARAM_OIF *param, unsigned char fMustAlloc)
87 {
88 PFORMAT_STRING pFormat;
89 NDR_UNMARSHALL m;
90
91 if (param->attr.IsBasetype)
92 {
93 pFormat = &param->u.type_format_char;
94 if (param->attr.IsSimpleRef) ppMemory = (unsigned char **)*ppMemory;
95 }
96 else
97 {
98 pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
99 if (!param->attr.IsByValue) ppMemory = (unsigned char **)*ppMemory;
100 }
101
102 m = NdrUnmarshaller[pFormat[0] & NDR_TABLE_MASK];
103 if (m) return m(pStubMsg, ppMemory, pFormat, fMustAlloc);
104 else
105 {
106 FIXME("format type 0x%x not implemented\n", pFormat[0]);
107 RpcRaiseException(RPC_X_BAD_STUB_DATA);
108 return NULL;
109 }
110 }
111
112 static inline void call_freer(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
113 const NDR_PARAM_OIF *param)
114 {
115 PFORMAT_STRING pFormat;
116 NDR_FREE m;
117
118 if (param->attr.IsBasetype) return; /* nothing to do */
119 pFormat = &pStubMsg->StubDesc->pFormatTypes[param->u.type_offset];
120 if (!param->attr.IsByValue) pMemory = *(unsigned char **)pMemory;
121
122 m = NdrFreer[pFormat[0] & NDR_TABLE_MASK];
123 if (m) m(pStubMsg, pMemory, pFormat);
124 }
125
126 static DWORD calc_arg_size(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat)
127 {
128 DWORD size;
129 switch(*pFormat)
130 {
131 case RPC_FC_RP:
132 if (pFormat[1] & RPC_FC_P_SIMPLEPOINTER)
133 {
134 FIXME("Simple reference pointer (type %#x).\n", pFormat[2]);
135 size = sizeof(void *);
136 break;
137 }
138 size = calc_arg_size(pStubMsg, &pFormat[2] + *(const SHORT*)&pFormat[2]);
139 break;
140 case RPC_FC_STRUCT:
141 case RPC_FC_PSTRUCT:
142 size = *(const WORD*)(pFormat + 2);
143 break;
144 case RPC_FC_BOGUS_STRUCT:
145 size = *(const WORD*)(pFormat + 2);
146 if(*(const WORD*)(pFormat + 4))
147 FIXME("Unhandled conformant description\n");
148 break;
149 case RPC_FC_CARRAY:
150 case RPC_FC_CVARRAY:
151 size = *(const WORD*)(pFormat + 2);
152 ComputeConformance(pStubMsg, NULL, pFormat + 4, 0);
153 size *= pStubMsg->MaxCount;
154 break;
155 case RPC_FC_SMFARRAY:
156 case RPC_FC_SMVARRAY:
157 size = *(const WORD*)(pFormat + 2);
158 break;
159 case RPC_FC_LGFARRAY:
160 case RPC_FC_LGVARRAY:
161 size = *(const DWORD*)(pFormat + 2);
162 break;
163 case RPC_FC_BOGUS_ARRAY:
164 pFormat = ComputeConformance(pStubMsg, NULL, pFormat + 4, *(const WORD*)&pFormat[2]);
165 TRACE("conformance = %ld\n", pStubMsg->MaxCount);
166 pFormat = ComputeVariance(pStubMsg, NULL, pFormat, pStubMsg->MaxCount);
167 size = ComplexStructSize(pStubMsg, pFormat);
168 size *= pStubMsg->MaxCount;
169 break;
170 case RPC_FC_USER_MARSHAL:
171 size = *(const WORD*)(pFormat + 4);
172 break;
173 case RPC_FC_CSTRING:
174 size = *(const WORD*)(pFormat + 2);
175 break;
176 case RPC_FC_WSTRING:
177 size = *(const WORD*)(pFormat + 2) * sizeof(WCHAR);
178 break;
179 case RPC_FC_C_CSTRING:
180 case RPC_FC_C_WSTRING:
181 if (*pFormat == RPC_FC_C_CSTRING)
182 size = sizeof(CHAR);
183 else
184 size = sizeof(WCHAR);
185 if (pFormat[1] == RPC_FC_STRING_SIZED)
186 ComputeConformance(pStubMsg, NULL, pFormat + 2, 0);
187 else
188 pStubMsg->MaxCount = 0;
189 size *= pStubMsg->MaxCount;
190 break;
191 default:
192 FIXME("Unhandled type %02x\n", *pFormat);
193 /* fallthrough */
194 case RPC_FC_IP:
195 size = sizeof(void *);
196 break;
197 }
198 return size;
199 }
200
201 void WINAPI NdrRpcSmSetClientToOsf(PMIDL_STUB_MESSAGE pMessage)
202 {
203 #if 0 /* these functions are not defined yet */
204 pMessage->pfnAllocate = NdrRpcSmClientAllocate;
205 pMessage->pfnFree = NdrRpcSmClientFree;
206 #endif
207 }
208
209 static const char *debugstr_PROC_PF(PARAM_ATTRIBUTES param_attributes)
210 {
211 char buffer[160];
212
213 buffer[0] = 0;
214 if (param_attributes.MustSize) strcat(buffer, " MustSize");
215 if (param_attributes.MustFree) strcat(buffer, " MustFree");
216 if (param_attributes.IsPipe) strcat(buffer, " IsPipe");
217 if (param_attributes.IsIn) strcat(buffer, " IsIn");
218 if (param_attributes.IsOut) strcat(buffer, " IsOut");
219 if (param_attributes.IsReturn) strcat(buffer, " IsReturn");
220 if (param_attributes.IsBasetype) strcat(buffer, " IsBasetype");
221 if (param_attributes.IsByValue) strcat(buffer, " IsByValue");
222 if (param_attributes.IsSimpleRef) strcat(buffer, " IsSimpleRef");
223 if (param_attributes.IsDontCallFreeInst) strcat(buffer, " IsDontCallFreeInst");
224 if (param_attributes.SaveForAsyncFinish) strcat(buffer, " SaveForAsyncFinish");
225 if (param_attributes.ServerAllocSize)
226 sprintf( buffer + strlen(buffer), " ServerAllocSize = %d", param_attributes.ServerAllocSize * 8);
227 return buffer[0] ? wine_dbg_sprintf( "%s", buffer + 1 ) : "";
228 }
229
230 static const char *debugstr_INTERPRETER_OPT_FLAGS(INTERPRETER_OPT_FLAGS Oi2Flags)
231 {
232 char buffer[160];
233
234 buffer[0] = 0;
235 if (Oi2Flags.ServerMustSize) strcat(buffer, " ServerMustSize");
236 if (Oi2Flags.ClientMustSize) strcat(buffer, " ClientMustSize");
237 if (Oi2Flags.HasReturn) strcat(buffer, " HasReturn");
238 if (Oi2Flags.HasPipes) strcat(buffer, " HasPipes");
239 if (Oi2Flags.Unused) strcat(buffer, " Unused");
240 if (Oi2Flags.HasAsyncUuid) strcat(buffer, " HasAsyncUuid");
241 if (Oi2Flags.HasExtensions) strcat(buffer, " HasExtensions");
242 if (Oi2Flags.HasAsyncHandle) strcat(buffer, " HasAsyncHandle");
243 return buffer[0] ? wine_dbg_sprintf( "%s", buffer + 1 ) : "";
244 }
245
246 #define ARG_FROM_OFFSET(args, offset) ((args) + (offset))
247
248 static PFORMAT_STRING client_get_handle(
249 PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
250 PFORMAT_STRING pFormat, handle_t *phBinding)
251 {
252 /* binding */
253 switch (pProcHeader->handle_type)
254 {
255 /* explicit binding: parse additional section */
256 case RPC_FC_BIND_EXPLICIT:
257 switch (*pFormat) /* handle_type */
258 {
259 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
260 {
261 const NDR_EHD_PRIMITIVE *pDesc = (const NDR_EHD_PRIMITIVE *)pFormat;
262
263 TRACE("Explicit primitive handle @ %d\n", pDesc->offset);
264
265 if (pDesc->flag) /* pointer to binding */
266 *phBinding = **(handle_t **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
267 else
268 *phBinding = *(handle_t *)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
269 return pFormat + sizeof(NDR_EHD_PRIMITIVE);
270 }
271 case RPC_FC_BIND_GENERIC: /* explicit generic */
272 {
273 const NDR_EHD_GENERIC *pDesc = (const NDR_EHD_GENERIC *)pFormat;
274 void *pObject = NULL;
275 void *pArg;
276 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
277
278 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
279
280 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
281 pArg = *(void **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
282 else
283 pArg = ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
284 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
285 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
286 *phBinding = pGenPair->pfnBind(pObject);
287 return pFormat + sizeof(NDR_EHD_GENERIC);
288 }
289 case RPC_FC_BIND_CONTEXT: /* explicit context */
290 {
291 const NDR_EHD_CONTEXT *pDesc = (const NDR_EHD_CONTEXT *)pFormat;
292 NDR_CCONTEXT context_handle;
293 TRACE("Explicit bind context\n");
294 if (pDesc->flags & HANDLE_PARAM_IS_VIA_PTR)
295 {
296 TRACE("\tHANDLE_PARAM_IS_VIA_PTR\n");
297 context_handle = **(NDR_CCONTEXT **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
298 }
299 else
300 context_handle = *(NDR_CCONTEXT *)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
301
302 if (context_handle) *phBinding = NDRCContextBinding(context_handle);
303 else if (pDesc->flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL)
304 {
305 ERR("null context handle isn't allowed\n");
306 RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);
307 return NULL;
308 }
309 /* FIXME: should we store this structure in stubMsg.pContext? */
310 return pFormat + sizeof(NDR_EHD_CONTEXT);
311 }
312 default:
313 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
314 RpcRaiseException(RPC_X_BAD_STUB_DATA);
315 }
316 break;
317 case RPC_FC_BIND_GENERIC: /* implicit generic */
318 FIXME("RPC_FC_BIND_GENERIC\n");
319 RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
320 break;
321 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
322 TRACE("Implicit primitive handle\n");
323 *phBinding = *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pPrimitiveHandle;
324 break;
325 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
326 FIXME("RPC_FC_CALLBACK_HANDLE\n");
327 break;
328 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
329 /* strictly speaking, it isn't necessary to set hBinding here
330 * since it isn't actually used (hence the automatic in its name),
331 * but then why does MIDL generate a valid entry in the
332 * MIDL_STUB_DESC for it? */
333 TRACE("Implicit auto handle\n");
334 *phBinding = *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle;
335 break;
336 default:
337 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
338 RpcRaiseException(RPC_X_BAD_STUB_DATA);
339 }
340 return pFormat;
341 }
342
343 static void client_free_handle(
344 PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
345 PFORMAT_STRING pFormat, handle_t hBinding)
346 {
347 /* binding */
348 switch (pProcHeader->handle_type)
349 {
350 /* explicit binding: parse additional section */
351 case RPC_FC_BIND_EXPLICIT:
352 switch (*pFormat) /* handle_type */
353 {
354 case RPC_FC_BIND_GENERIC: /* explicit generic */
355 {
356 const NDR_EHD_GENERIC *pDesc = (const NDR_EHD_GENERIC *)pFormat;
357 void *pObject = NULL;
358 void *pArg;
359 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
360
361 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
362
363 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
364 pArg = *(void **)ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
365 else
366 pArg = ARG_FROM_OFFSET(pStubMsg->StackTop, pDesc->offset);
367 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
368 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
369 pGenPair->pfnUnbind(pObject, hBinding);
370 break;
371 }
372 case RPC_FC_BIND_CONTEXT: /* explicit context */
373 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
374 break;
375 default:
376 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
377 RpcRaiseException(RPC_X_BAD_STUB_DATA);
378 }
379 break;
380 case RPC_FC_BIND_GENERIC: /* implicit generic */
381 FIXME("RPC_FC_BIND_GENERIC\n");
382 RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
383 break;
384 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
385 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
386 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
387 break;
388 default:
389 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
390 RpcRaiseException(RPC_X_BAD_STUB_DATA);
391 }
392 }
393
394 void client_do_args( PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat, enum stubless_phase phase,
395 void **fpu_args, unsigned short number_of_params, unsigned char *pRetVal )
396 {
397 const NDR_PARAM_OIF *params = (const NDR_PARAM_OIF *)pFormat;
398 unsigned int i;
399
400 for (i = 0; i < number_of_params; i++)
401 {
402 unsigned char *pArg = pStubMsg->StackTop + params[i].stack_offset;
403 PFORMAT_STRING pTypeFormat = (PFORMAT_STRING)&pStubMsg->StubDesc->pFormatTypes[params[i].u.type_offset];
404
405 #ifdef __x86_64__ /* floats are passed as doubles through varargs functions */
406 float f;
407
408 if (params[i].attr.IsBasetype &&
409 params[i].u.type_format_char == RPC_FC_FLOAT &&
410 !params[i].attr.IsSimpleRef &&
411 !fpu_args)
412 {
413 f = *(double *)pArg;
414 pArg = (unsigned char *)&f;
415 }
416 #endif
417
418 TRACE("param[%d]: %p type %02x %s\n", i, pArg,
419 params[i].attr.IsBasetype ? params[i].u.type_format_char : *pTypeFormat,
420 debugstr_PROC_PF( params[i].attr ));
421
422 switch (phase)
423 {
424 case STUBLESS_INITOUT:
425 if (!params[i].attr.IsBasetype && params[i].attr.IsOut &&
426 !params[i].attr.IsIn && !params[i].attr.IsByValue)
427 {
428 memset( *(unsigned char **)pArg, 0, calc_arg_size( pStubMsg, pTypeFormat ));
429 }
430 break;
431 case STUBLESS_CALCSIZE:
432 if (params[i].attr.IsSimpleRef && !*(unsigned char **)pArg)
433 RpcRaiseException(RPC_X_NULL_REF_POINTER);
434 if (params[i].attr.IsIn) call_buffer_sizer(pStubMsg, pArg, &params[i]);
435 break;
436 case STUBLESS_MARSHAL:
437 if (params[i].attr.IsIn) call_marshaller(pStubMsg, pArg, &params[i]);
438 break;
439 case STUBLESS_UNMARSHAL:
440 if (params[i].attr.IsOut)
441 {
442 if (params[i].attr.IsReturn && pRetVal) pArg = pRetVal;
443 call_unmarshaller(pStubMsg, &pArg, &params[i], 0);
444 }
445 break;
446 case STUBLESS_FREE:
447 if (!params[i].attr.IsBasetype && params[i].attr.IsOut && !params[i].attr.IsByValue)
448 NdrClearOutParameters( pStubMsg, pTypeFormat, *(unsigned char **)pArg );
449 break;
450 default:
451 RpcRaiseException(RPC_S_INTERNAL_ERROR);
452 }
453 }
454 }
455
456 static unsigned int type_stack_size(unsigned char fc)
457 {
458 switch (fc)
459 {
460 case RPC_FC_BYTE:
461 case RPC_FC_CHAR:
462 case RPC_FC_SMALL:
463 case RPC_FC_USMALL:
464 case RPC_FC_WCHAR:
465 case RPC_FC_SHORT:
466 case RPC_FC_USHORT:
467 case RPC_FC_LONG:
468 case RPC_FC_ULONG:
469 case RPC_FC_INT3264:
470 case RPC_FC_UINT3264:
471 case RPC_FC_ENUM16:
472 case RPC_FC_ENUM32:
473 case RPC_FC_FLOAT:
474 case RPC_FC_ERROR_STATUS_T:
475 case RPC_FC_IGNORE:
476 return sizeof(void *);
477 case RPC_FC_DOUBLE:
478 return sizeof(double);
479 case RPC_FC_HYPER:
480 return sizeof(ULONGLONG);
481 default:
482 ERR("invalid base type 0x%x\n", fc);
483 RpcRaiseException(RPC_S_INTERNAL_ERROR);
484 }
485 }
486
487 static BOOL is_by_value( PFORMAT_STRING format )
488 {
489 switch (*format)
490 {
491 case RPC_FC_USER_MARSHAL:
492 case RPC_FC_STRUCT:
493 case RPC_FC_PSTRUCT:
494 case RPC_FC_CSTRUCT:
495 case RPC_FC_CPSTRUCT:
496 case RPC_FC_CVSTRUCT:
497 case RPC_FC_BOGUS_STRUCT:
498 return TRUE;
499 default:
500 return FALSE;
501 }
502 }
503
504 PFORMAT_STRING convert_old_args( PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat,
505 unsigned int stack_size, BOOL object_proc,
506 void *buffer, unsigned int size, unsigned int *count )
507 {
508 NDR_PARAM_OIF *args = buffer;
509 unsigned int i, stack_offset = object_proc ? sizeof(void *) : 0;
510
511 for (i = 0; stack_offset < stack_size; i++)
512 {
513 const NDR_PARAM_OI_BASETYPE *param = (const NDR_PARAM_OI_BASETYPE *)pFormat;
514 const NDR_PARAM_OI_OTHER *other = (const NDR_PARAM_OI_OTHER *)pFormat;
515
516 if (i + 1 > size / sizeof(*args))
517 {
518 FIXME( "%u args not supported\n", i );
519 RpcRaiseException( RPC_S_INTERNAL_ERROR );
520 }
521
522 args[i].stack_offset = stack_offset;
523 memset( &args[i].attr, 0, sizeof(args[i].attr) );
524
525 switch (param->param_direction)
526 {
527 case RPC_FC_IN_PARAM_BASETYPE:
528 args[i].attr.IsIn = 1;
529 args[i].attr.IsBasetype = 1;
530 break;
531 case RPC_FC_RETURN_PARAM_BASETYPE:
532 args[i].attr.IsOut = 1;
533 args[i].attr.IsReturn = 1;
534 args[i].attr.IsBasetype = 1;
535 break;
536 case RPC_FC_IN_PARAM:
537 args[i].attr.IsIn = 1;
538 args[i].attr.MustFree = 1;
539 break;
540 case RPC_FC_IN_PARAM_NO_FREE_INST:
541 args[i].attr.IsIn = 1;
542 args[i].attr.IsDontCallFreeInst = 1;
543 break;
544 case RPC_FC_IN_OUT_PARAM:
545 args[i].attr.IsIn = 1;
546 args[i].attr.IsOut = 1;
547 args[i].attr.MustFree = 1;
548 break;
549 case RPC_FC_OUT_PARAM:
550 args[i].attr.IsOut = 1;
551 break;
552 case RPC_FC_RETURN_PARAM:
553 args[i].attr.IsOut = 1;
554 args[i].attr.IsReturn = 1;
555 break;
556 }
557 if (args[i].attr.IsBasetype)
558 {
559 args[i].u.type_format_char = param->type_format_char;
560 stack_offset += type_stack_size( param->type_format_char );
561 pFormat += sizeof(NDR_PARAM_OI_BASETYPE);
562 }
563 else
564 {
565 args[i].u.type_offset = other->type_offset;
566 args[i].attr.IsByValue = is_by_value( &pStubMsg->StubDesc->pFormatTypes[other->type_offset] );
567 stack_offset += other->stack_size * sizeof(void *);
568 pFormat += sizeof(NDR_PARAM_OI_OTHER);
569 }
570 }
571 *count = i;
572 return (PFORMAT_STRING)args;
573 }
574
575 LONG_PTR CDECL ndr_client_call( PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat,
576 void **stack_top, void **fpu_stack )
577 {
578 /* pointer to start of stack where arguments start */
579 RPC_MESSAGE rpcMsg;
580 MIDL_STUB_MESSAGE stubMsg;
581 handle_t hBinding = NULL;
582 /* procedure number */
583 unsigned short procedure_number;
584 /* size of stack */
585 unsigned short stack_size;
586 /* number of parameters. optional for client to give it to us */
587 unsigned int number_of_params;
588 /* cache of Oif_flags from v2 procedure header */
589 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
590 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
591 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
592 /* header for procedure string */
593 const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
594 /* the value to return to the client from the remote procedure */
595 LONG_PTR RetVal = 0;
596 /* the pointer to the object when in OLE mode */
597 void * This = NULL;
598 PFORMAT_STRING pHandleFormat;
599 /* correlation cache */
600 ULONG_PTR NdrCorrCache[256];
601
602 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
603
604 TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
605
606 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
607 {
608 const NDR_PROC_HEADER_RPC *header_rpc = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
609 stack_size = header_rpc->stack_size;
610 procedure_number = header_rpc->proc_num;
611 pFormat += sizeof(NDR_PROC_HEADER_RPC);
612 }
613 else
614 {
615 stack_size = pProcHeader->stack_size;
616 procedure_number = pProcHeader->proc_num;
617 pFormat += sizeof(NDR_PROC_HEADER);
618 }
619 TRACE("stack size: 0x%x\n", stack_size);
620 TRACE("proc num: %d\n", procedure_number);
621
622 /* create the full pointer translation tables, if requested */
623 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
624 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
625
626 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
627 {
628 /* object is always the first argument */
629 This = stack_top[0];
630 NdrProxyInitialize(This, &rpcMsg, &stubMsg, pStubDesc, procedure_number);
631 }
632 else
633 NdrClientInitializeNew(&rpcMsg, &stubMsg, pStubDesc, procedure_number);
634
635 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
636 TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
637
638 stubMsg.StackTop = (unsigned char *)stack_top;
639 pHandleFormat = pFormat;
640
641 /* we only need a handle if this isn't an object method */
642 if (!(pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT))
643 {
644 pFormat = client_get_handle(&stubMsg, pProcHeader, pHandleFormat, &hBinding);
645 if (!pFormat) goto done;
646 }
647
648 if (pStubDesc->Version >= 0x20000) /* -Oicf format */
649 {
650 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
651 (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
652
653 Oif_flags = pOIFHeader->Oi2Flags;
654 number_of_params = pOIFHeader->number_of_params;
655
656 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
657
658 TRACE("Oif_flags = %s\n", debugstr_INTERPRETER_OPT_FLAGS(Oif_flags) );
659
660 if (Oif_flags.HasExtensions)
661 {
662 const NDR_PROC_HEADER_EXTS *pExtensions = (const NDR_PROC_HEADER_EXTS *)pFormat;
663 ext_flags = pExtensions->Flags2;
664 pFormat += pExtensions->Size;
665 #ifdef __x86_64__
666 if (pExtensions->Size > sizeof(*pExtensions) && fpu_stack)
667 {
668 int i;
669 unsigned short fpu_mask = *(unsigned short *)(pExtensions + 1);
670 for (i = 0; i < 4; i++, fpu_mask >>= 2)
671 switch (fpu_mask & 3)
672 {
673 case 1: *(float *)&stack_top[i] = *(float *)&fpu_stack[i]; break;
674 case 2: *(double *)&stack_top[i] = *(double *)&fpu_stack[i]; break;
675 }
676 }
677 #endif
678 }
679 }
680 else
681 {
682 pFormat = convert_old_args( &stubMsg, pFormat, stack_size,
683 pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT,
684 /* reuse the correlation cache, it's not needed for v1 format */
685 NdrCorrCache, sizeof(NdrCorrCache), &number_of_params );
686 }
687
688 stubMsg.BufferLength = 0;
689
690 /* store the RPC flags away */
691 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
692 rpcMsg.RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
693
694 /* use alternate memory allocation routines */
695 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
696 NdrRpcSmSetClientToOsf(&stubMsg);
697
698 if (Oif_flags.HasPipes)
699 {
700 FIXME("pipes not supported yet\n");
701 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
702 /* init pipes package */
703 /* NdrPipesInitialize(...) */
704 }
705 if (ext_flags.HasNewCorrDesc)
706 {
707 /* initialize extra correlation package */
708 NdrCorrelationInitialize(&stubMsg, NdrCorrCache, sizeof(NdrCorrCache), 0);
709 }
710
711 /* order of phases:
712 * 1. INITOUT - zero [out] parameters (proxies only)
713 * 2. CALCSIZE - calculate the buffer size
714 * 3. GETBUFFER - allocate the buffer
715 * 4. MARSHAL - marshal [in] params into the buffer
716 * 5. SENDRECEIVE - send/receive buffer
717 * 6. UNMARSHAL - unmarshal [out] params from buffer
718 * 7. FREE - clear [out] parameters (for proxies, and only on error)
719 */
720 if ((pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT) ||
721 (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_HAS_COMM_OR_FAULT))
722 {
723 /* 1. INITOUT */
724 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
725 {
726 TRACE( "INITOUT\n" );
727 client_do_args(&stubMsg, pFormat, STUBLESS_INITOUT, fpu_stack,
728 number_of_params, (unsigned char *)&RetVal);
729 }
730
731 __TRY
732 {
733 /* 2. CALCSIZE */
734 TRACE( "CALCSIZE\n" );
735 client_do_args(&stubMsg, pFormat, STUBLESS_CALCSIZE, fpu_stack,
736 number_of_params, (unsigned char *)&RetVal);
737
738 /* 3. GETBUFFER */
739 TRACE( "GETBUFFER\n" );
740 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
741 {
742 /* allocate the buffer */
743 NdrProxyGetBuffer(This, &stubMsg);
744 }
745 else
746 {
747 /* allocate the buffer */
748 if (Oif_flags.HasPipes)
749 /* NdrGetPipeBuffer(...) */
750 FIXME("pipes not supported yet\n");
751 else
752 {
753 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
754 #if 0
755 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
756 #else
757 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
758 #endif
759 else
760 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
761 }
762 }
763
764 /* 4. MARSHAL */
765 TRACE( "MARSHAL\n" );
766 client_do_args(&stubMsg, pFormat, STUBLESS_MARSHAL, fpu_stack,
767 number_of_params, (unsigned char *)&RetVal);
768
769 /* 5. SENDRECEIVE */
770 TRACE( "SENDRECEIVE\n" );
771 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
772 {
773 /* send the [in] params and receive the [out] and [retval]
774 * params */
775 NdrProxySendReceive(This, &stubMsg);
776 }
777 else
778 {
779 /* send the [in] params and receive the [out] and [retval]
780 * params */
781 if (Oif_flags.HasPipes)
782 /* NdrPipesSendReceive(...) */
783 FIXME("pipes not supported yet\n");
784 else
785 {
786 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
787 #if 0
788 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
789 #else
790 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
791 #endif
792 else
793 NdrSendReceive(&stubMsg, stubMsg.Buffer);
794 }
795 }
796
797 /* convert strings, floating point values and endianness into our
798 * preferred format */
799 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
800 NdrConvert(&stubMsg, pFormat);
801
802 /* 6. UNMARSHAL */
803 TRACE( "UNMARSHAL\n" );
804 client_do_args(&stubMsg, pFormat, STUBLESS_UNMARSHAL, fpu_stack,
805 number_of_params, (unsigned char *)&RetVal);
806 }
807 __EXCEPT_ALL
808 {
809 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
810 {
811 /* 7. FREE */
812 TRACE( "FREE\n" );
813 client_do_args(&stubMsg, pFormat, STUBLESS_FREE, fpu_stack,
814 number_of_params, (unsigned char *)&RetVal);
815 RetVal = NdrProxyErrorHandler(GetExceptionCode());
816 }
817 else
818 {
819 const COMM_FAULT_OFFSETS *comm_fault_offsets = &pStubDesc->CommFaultOffsets[procedure_number];
820 ULONG *comm_status;
821 ULONG *fault_status;
822
823 TRACE("comm_fault_offsets = {0x%hx, 0x%hx}\n", comm_fault_offsets->CommOffset, comm_fault_offsets->FaultOffset);
824
825 if (comm_fault_offsets->CommOffset == -1)
826 comm_status = (ULONG *)&RetVal;
827 else if (comm_fault_offsets->CommOffset >= 0)
828 comm_status = *(ULONG **)ARG_FROM_OFFSET(stubMsg.StackTop, comm_fault_offsets->CommOffset);
829 else
830 comm_status = NULL;
831
832 if (comm_fault_offsets->FaultOffset == -1)
833 fault_status = (ULONG *)&RetVal;
834 else if (comm_fault_offsets->FaultOffset >= 0)
835 fault_status = *(ULONG **)ARG_FROM_OFFSET(stubMsg.StackTop, comm_fault_offsets->FaultOffset);
836 else
837 fault_status = NULL;
838
839 NdrMapCommAndFaultStatus(&stubMsg, comm_status, fault_status,
840 GetExceptionCode());
841 }
842 }
843 __ENDTRY
844 }
845 else
846 {
847 /* 2. CALCSIZE */
848 TRACE( "CALCSIZE\n" );
849 client_do_args(&stubMsg, pFormat, STUBLESS_CALCSIZE, fpu_stack,
850 number_of_params, (unsigned char *)&RetVal);
851
852 /* 3. GETBUFFER */
853 TRACE( "GETBUFFER\n" );
854 if (Oif_flags.HasPipes)
855 /* NdrGetPipeBuffer(...) */
856 FIXME("pipes not supported yet\n");
857 else
858 {
859 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
860 #if 0
861 NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
862 #else
863 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
864 #endif
865 else
866 NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
867 }
868
869 /* 4. MARSHAL */
870 TRACE( "MARSHAL\n" );
871 client_do_args(&stubMsg, pFormat, STUBLESS_MARSHAL, fpu_stack,
872 number_of_params, (unsigned char *)&RetVal);
873
874 /* 5. SENDRECEIVE */
875 TRACE( "SENDRECEIVE\n" );
876 if (Oif_flags.HasPipes)
877 /* NdrPipesSendReceive(...) */
878 FIXME("pipes not supported yet\n");
879 else
880 {
881 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
882 #if 0
883 NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
884 #else
885 FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
886 #endif
887 else
888 NdrSendReceive(&stubMsg, stubMsg.Buffer);
889 }
890
891 /* convert strings, floating point values and endianness into our
892 * preferred format */
893 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
894 NdrConvert(&stubMsg, pFormat);
895
896 /* 6. UNMARSHAL */
897 TRACE( "UNMARSHAL\n" );
898 client_do_args(&stubMsg, pFormat, STUBLESS_UNMARSHAL, fpu_stack,
899 number_of_params, (unsigned char *)&RetVal);
900 }
901
902 if (ext_flags.HasNewCorrDesc)
903 {
904 /* free extra correlation package */
905 NdrCorrelationFree(&stubMsg);
906 }
907
908 if (Oif_flags.HasPipes)
909 {
910 /* NdrPipesDone(...) */
911 }
912
913 /* free the full pointer translation tables */
914 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
915 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
916
917 /* free marshalling buffer */
918 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
919 NdrProxyFreeBuffer(This, &stubMsg);
920 else
921 {
922 NdrFreeBuffer(&stubMsg);
923 client_free_handle(&stubMsg, pProcHeader, pHandleFormat, hBinding);
924 }
925
926 done:
927 TRACE("RetVal = 0x%lx\n", RetVal);
928 return RetVal;
929 }
930
931 #ifdef __x86_64__
932
933 __ASM_GLOBAL_FUNC( NdrClientCall2,
934 "movq %r8,0x18(%rsp)\n\t"
935 "movq %r9,0x20(%rsp)\n\t"
936 "leaq 0x18(%rsp),%r8\n\t"
937 "xorq %r9,%r9\n\t"
938 "subq $0x28,%rsp\n\t"
939 __ASM_CFI(".cfi_adjust_cfa_offset 0x28\n\t")
940 "call " __ASM_NAME("ndr_client_call") "\n\t"
941 "addq $0x28,%rsp\n\t"
942 __ASM_CFI(".cfi_adjust_cfa_offset -0x28\n\t")
943 "ret" );
944
945 #else /* __x86_64__ */
946
947 /***********************************************************************
948 * NdrClientCall2 [RPCRT4.@]
949 */
950 CLIENT_CALL_RETURN WINAPIV NdrClientCall2( PMIDL_STUB_DESC desc, PFORMAT_STRING format, ... )
951 {
952 __ms_va_list args;
953 LONG_PTR ret;
954
955 __ms_va_start( args, format );
956 ret = ndr_client_call( desc, format, va_arg( args, void ** ), NULL );
957 __ms_va_end( args );
958 return *(CLIENT_CALL_RETURN *)&ret;
959 }
960
961 #endif /* __x86_64__ */
962
963 /* Calls a function with the specified arguments, restoring the stack
964 * properly afterwards as we don't know the calling convention of the
965 * function */
966 #if defined __i386__ && defined _MSC_VER
967 __declspec(naked) LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size)
968 {
969 __asm
970 {
971 push ebp
972 mov ebp, esp
973 push edi ; Save registers
974 push esi
975 mov eax, [ebp+16] ; Get stack size
976 sub esp, eax ; Make room in stack for arguments
977 and esp, 0xFFFFFFF0
978 mov edi, esp
979 mov ecx, eax
980 mov esi, [ebp+12]
981 shr ecx, 2
982 cld
983 rep movsd ; Copy dword blocks
984 call [ebp+8] ; Call function
985 lea esp, [ebp-8] ; Restore stack
986 pop esi ; Restore registers
987 pop edi
988 pop ebp
989 ret
990 }
991 }
992 #elif defined __i386__ && defined __GNUC__
993 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
994 __ASM_GLOBAL_FUNC(call_server_func,
995 "pushl %ebp\n\t"
996 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
997 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
998 "movl %esp,%ebp\n\t"
999 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1000 "pushl %edi\n\t" /* Save registers */
1001 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
1002 "pushl %esi\n\t"
1003 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
1004 "movl 16(%ebp), %eax\n\t" /* Get stack size */
1005 "subl %eax, %esp\n\t" /* Make room in stack for arguments */
1006 "andl $~15, %esp\n\t" /* Make sure stack has 16-byte alignment for Mac OS X */
1007 "movl %esp, %edi\n\t"
1008 "movl %eax, %ecx\n\t"
1009 "movl 12(%ebp), %esi\n\t"
1010 "shrl $2, %ecx\n\t" /* divide by 4 */
1011 "cld\n\t"
1012 "rep; movsl\n\t" /* Copy dword blocks */
1013 "call *8(%ebp)\n\t" /* Call function */
1014 "leal -8(%ebp), %esp\n\t" /* Restore stack */
1015 "popl %esi\n\t" /* Restore registers */
1016 __ASM_CFI(".cfi_same_value %esi\n\t")
1017 "popl %edi\n\t"
1018 __ASM_CFI(".cfi_same_value %edi\n\t")
1019 "popl %ebp\n\t"
1020 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1021 __ASM_CFI(".cfi_same_value %ebp\n\t")
1022 "ret" )
1023 #elif defined __x86_64__
1024 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
1025 __ASM_GLOBAL_FUNC( call_server_func,
1026 "pushq %rbp\n\t"
1027 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1028 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1029 "movq %rsp,%rbp\n\t"
1030 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
1031 "pushq %rsi\n\t"
1032 __ASM_CFI(".cfi_rel_offset %rsi,-8\n\t")
1033 "pushq %rdi\n\t"
1034 __ASM_CFI(".cfi_rel_offset %rdi,-16\n\t")
1035 "movq %rcx,%rax\n\t" /* function to call */
1036 "movq $32,%rcx\n\t" /* allocate max(32,stack_size) bytes of stack space */
1037 "cmpq %rcx,%r8\n\t"
1038 "cmovgq %r8,%rcx\n\t"
1039 "subq %rcx,%rsp\n\t"
1040 "andq $~15,%rsp\n\t"
1041 "movq %r8,%rcx\n\t"
1042 "shrq $3,%rcx\n\t"
1043 "movq %rsp,%rdi\n\t"
1044 "movq %rdx,%rsi\n\t"
1045 "rep; movsq\n\t" /* copy arguments */
1046 "movq 0(%rsp),%rcx\n\t"
1047 "movq 8(%rsp),%rdx\n\t"
1048 "movq 16(%rsp),%r8\n\t"
1049 "movq 24(%rsp),%r9\n\t"
1050 "movq %rcx,%xmm0\n\t"
1051 "movq %rdx,%xmm1\n\t"
1052 "movq %r8,%xmm2\n\t"
1053 "movq %r9,%xmm3\n\t"
1054 "callq *%rax\n\t"
1055 "leaq -16(%rbp),%rsp\n\t" /* restore stack */
1056 "popq %rdi\n\t"
1057 __ASM_CFI(".cfi_same_value %rdi\n\t")
1058 "popq %rsi\n\t"
1059 __ASM_CFI(".cfi_same_value %rsi\n\t")
1060 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
1061 "popq %rbp\n\t"
1062 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1063 __ASM_CFI(".cfi_same_value %rbp\n\t")
1064 "ret")
1065 #else
1066 #warning call_server_func not implemented for your architecture
1067 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned short stack_size)
1068 {
1069 FIXME("Not implemented for your architecture\n");
1070 return 0;
1071 }
1072 #endif
1073
1074 static LONG_PTR *stub_do_args(MIDL_STUB_MESSAGE *pStubMsg,
1075 PFORMAT_STRING pFormat, enum stubless_phase phase,
1076 unsigned short number_of_params)
1077 {
1078 const NDR_PARAM_OIF *params = (const NDR_PARAM_OIF *)pFormat;
1079 unsigned int i;
1080 LONG_PTR *retval_ptr = NULL;
1081
1082 if (phase == STUBLESS_FREE)
1083 {
1084 /* Process the params allocated by the application first */
1085 for (i = 0; i < number_of_params; i++)
1086 {
1087 unsigned char *pArg = pStubMsg->StackTop + params[i].stack_offset;
1088 if (params[i].attr.MustFree)
1089 {
1090 call_freer(pStubMsg, pArg, &params[i]);
1091 }
1092 }
1093 }
1094
1095 for (i = 0; i < number_of_params; i++)
1096 {
1097 unsigned char *pArg = pStubMsg->StackTop + params[i].stack_offset;
1098 const unsigned char *pTypeFormat = &pStubMsg->StubDesc->pFormatTypes[params[i].u.type_offset];
1099
1100 TRACE("param[%d]: %p -> %p type %02x %s\n", i,
1101 pArg, *(unsigned char **)pArg,
1102 params[i].attr.IsBasetype ? params[i].u.type_format_char : *pTypeFormat,
1103 debugstr_PROC_PF( params[i].attr ));
1104
1105 switch (phase)
1106 {
1107 case STUBLESS_MARSHAL:
1108 if (params[i].attr.IsOut || params[i].attr.IsReturn)
1109 call_marshaller(pStubMsg, pArg, &params[i]);
1110 break;
1111 case STUBLESS_FREE:
1112 if (params[i].attr.ServerAllocSize)
1113 {
1114 HeapFree(GetProcessHeap(), 0, *(void **)pArg);
1115 }
1116 else if (params[i].attr.IsOut &&
1117 !params[i].attr.IsIn &&
1118 !params[i].attr.IsBasetype &&
1119 !params[i].attr.IsByValue)
1120 {
1121 if (*pTypeFormat != RPC_FC_BIND_CONTEXT) pStubMsg->pfnFree(*(void **)pArg);
1122 }
1123 break;
1124 case STUBLESS_INITOUT:
1125 if (!params[i].attr.IsIn &&
1126 params[i].attr.IsOut &&
1127 !params[i].attr.IsBasetype &&
1128 !params[i].attr.ServerAllocSize &&
1129 !params[i].attr.IsByValue)
1130 {
1131 if (*pTypeFormat == RPC_FC_BIND_CONTEXT)
1132 {
1133 NDR_SCONTEXT ctxt = NdrContextHandleInitialize(pStubMsg, pTypeFormat);
1134 *(void **)pArg = NDRSContextValue(ctxt);
1135 }
1136 else
1137 {
1138 DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1139 if (size)
1140 {
1141 *(void **)pArg = NdrAllocate(pStubMsg, size);
1142 memset(*(void **)pArg, 0, size);
1143 }
1144 }
1145 }
1146 break;
1147 case STUBLESS_UNMARSHAL:
1148 if (params[i].attr.ServerAllocSize)
1149 *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1150 params[i].attr.ServerAllocSize * 8);
1151
1152 if (params[i].attr.IsIn)
1153 call_unmarshaller(pStubMsg, &pArg, &params[i], 0);
1154 break;
1155 case STUBLESS_CALCSIZE:
1156 if (params[i].attr.IsOut || params[i].attr.IsReturn)
1157 call_buffer_sizer(pStubMsg, pArg, &params[i]);
1158 break;
1159 default:
1160 RpcRaiseException(RPC_S_INTERNAL_ERROR);
1161 }
1162 TRACE("\tmemory addr (after): %p -> %p\n", pArg, *(unsigned char **)pArg);
1163
1164 /* make a note of the address of the return value parameter for later */
1165 if (params[i].attr.IsReturn) retval_ptr = (LONG_PTR *)pArg;
1166 }
1167 return retval_ptr;
1168 }
1169
1170 /***********************************************************************
1171 * NdrStubCall2 [RPCRT4.@]
1172 *
1173 * Unmarshals [in] parameters, calls either a method in an object or a server
1174 * function, marshals any [out] parameters and frees any allocated data.
1175 *
1176 * NOTES
1177 * Used by stubless MIDL-generated code.
1178 */
1179 LONG WINAPI NdrStubCall2(
1180 struct IRpcStubBuffer * pThis,
1181 struct IRpcChannelBuffer * pChannel,
1182 PRPC_MESSAGE pRpcMsg,
1183 DWORD * pdwStubPhase)
1184 {
1185 const MIDL_SERVER_INFO *pServerInfo;
1186 const MIDL_STUB_DESC *pStubDesc;
1187 PFORMAT_STRING pFormat;
1188 MIDL_STUB_MESSAGE stubMsg;
1189 /* pointer to start of stack to pass into stub implementation */
1190 unsigned char * args;
1191 /* size of stack */
1192 unsigned short stack_size;
1193 /* number of parameters. optional for client to give it to us */
1194 unsigned int number_of_params;
1195 /* cache of Oif_flags from v2 procedure header */
1196 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1197 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1198 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1199 /* the type of pass we are currently doing */
1200 enum stubless_phase phase;
1201 /* header for procedure string */
1202 const NDR_PROC_HEADER *pProcHeader;
1203 /* location to put retval into */
1204 LONG_PTR *retval_ptr = NULL;
1205 /* correlation cache */
1206 ULONG_PTR NdrCorrCache[256];
1207
1208 TRACE("pThis %p, pChannel %p, pRpcMsg %p, pdwStubPhase %p\n", pThis, pChannel, pRpcMsg, pdwStubPhase);
1209
1210 if (pThis)
1211 pServerInfo = CStdStubBuffer_GetServerInfo(pThis);
1212 else
1213 pServerInfo = ((RPC_SERVER_INTERFACE *)pRpcMsg->RpcInterfaceInformation)->InterpreterInfo;
1214
1215 pStubDesc = pServerInfo->pStubDesc;
1216 pFormat = pServerInfo->ProcString + pServerInfo->FmtStringOffset[pRpcMsg->ProcNum];
1217 pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1218
1219 TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
1220
1221 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1222 {
1223 const NDR_PROC_HEADER_RPC *header_rpc = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1224 stack_size = header_rpc->stack_size;
1225 pFormat += sizeof(NDR_PROC_HEADER_RPC);
1226
1227 }
1228 else
1229 {
1230 stack_size = pProcHeader->stack_size;
1231 pFormat += sizeof(NDR_PROC_HEADER);
1232 }
1233
1234 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1235
1236 /* binding */
1237 switch (pProcHeader->handle_type)
1238 {
1239 /* explicit binding: parse additional section */
1240 case RPC_FC_BIND_EXPLICIT:
1241 switch (*pFormat) /* handle_type */
1242 {
1243 case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
1244 pFormat += sizeof(NDR_EHD_PRIMITIVE);
1245 break;
1246 case RPC_FC_BIND_GENERIC: /* explicit generic */
1247 pFormat += sizeof(NDR_EHD_GENERIC);
1248 break;
1249 case RPC_FC_BIND_CONTEXT: /* explicit context */
1250 pFormat += sizeof(NDR_EHD_CONTEXT);
1251 break;
1252 default:
1253 ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1254 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1255 }
1256 break;
1257 case RPC_FC_BIND_GENERIC: /* implicit generic */
1258 case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
1259 case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
1260 case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
1261 break;
1262 default:
1263 ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1264 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1265 }
1266
1267 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1268 NdrStubInitialize(pRpcMsg, &stubMsg, pStubDesc, pChannel);
1269 else
1270 NdrServerInitializeNew(pRpcMsg, &stubMsg, pStubDesc);
1271
1272 /* create the full pointer translation tables, if requested */
1273 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1274 stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_SERVER);
1275
1276 /* store the RPC flags away */
1277 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1278 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1279
1280 /* use alternate memory allocation routines */
1281 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
1282 #if 0
1283 NdrRpcSsEnableAllocate(&stubMsg);
1284 #else
1285 FIXME("Set RPCSS memory allocation routines\n");
1286 #endif
1287
1288 TRACE("allocating memory for stack of size %x\n", stack_size);
1289
1290 args = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, stack_size);
1291 stubMsg.StackTop = args; /* used by conformance of top-level objects */
1292
1293 /* add the implicit This pointer as the first arg to the function if we
1294 * are calling an object method */
1295 if (pThis)
1296 *(void **)args = ((CStdStubBuffer *)pThis)->pvServerObject;
1297
1298 if (pStubDesc->Version >= 0x20000) /* -Oicf format */
1299 {
1300 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader = (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
1301
1302 Oif_flags = pOIFHeader->Oi2Flags;
1303 number_of_params = pOIFHeader->number_of_params;
1304
1305 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1306
1307 TRACE("Oif_flags = %s\n", debugstr_INTERPRETER_OPT_FLAGS(Oif_flags) );
1308
1309 if (Oif_flags.HasExtensions)
1310 {
1311 const NDR_PROC_HEADER_EXTS *pExtensions = (const NDR_PROC_HEADER_EXTS *)pFormat;
1312 ext_flags = pExtensions->Flags2;
1313 pFormat += pExtensions->Size;
1314 }
1315
1316 if (Oif_flags.HasPipes)
1317 {
1318 FIXME("pipes not supported yet\n");
1319 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1320 /* init pipes package */
1321 /* NdrPipesInitialize(...) */
1322 }
1323 if (ext_flags.HasNewCorrDesc)
1324 {
1325 /* initialize extra correlation package */
1326 FIXME("new correlation description not implemented\n");
1327 stubMsg.fHasNewCorrDesc = TRUE;
1328 }
1329 }
1330 else
1331 {
1332 pFormat = convert_old_args( &stubMsg, pFormat, stack_size,
1333 pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT,
1334 /* reuse the correlation cache, it's not needed for v1 format */
1335 NdrCorrCache, sizeof(NdrCorrCache), &number_of_params );
1336 }
1337
1338 /* convert strings, floating point values and endianness into our
1339 * preferred format */
1340 if ((pRpcMsg->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1341 NdrConvert(&stubMsg, pFormat);
1342
1343 for (phase = STUBLESS_UNMARSHAL; phase <= STUBLESS_FREE; phase++)
1344 {
1345 TRACE("phase = %d\n", phase);
1346 switch (phase)
1347 {
1348 case STUBLESS_CALLSERVER:
1349 /* call the server function */
1350 if (pServerInfo->ThunkTable && pServerInfo->ThunkTable[pRpcMsg->ProcNum])
1351 pServerInfo->ThunkTable[pRpcMsg->ProcNum](&stubMsg);
1352 else
1353 {
1354 SERVER_ROUTINE func;
1355 LONG_PTR retval;
1356
1357 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1358 {
1359 SERVER_ROUTINE *vtbl = *(SERVER_ROUTINE **)((CStdStubBuffer *)pThis)->pvServerObject;
1360 func = vtbl[pRpcMsg->ProcNum];
1361 }
1362 else
1363 func = pServerInfo->DispatchTable[pRpcMsg->ProcNum];
1364
1365 /* FIXME: what happens with return values that don't fit into a single register on x86? */
1366 retval = call_server_func(func, args, stack_size);
1367
1368 if (retval_ptr)
1369 {
1370 TRACE("stub implementation returned 0x%lx\n", retval);
1371 *retval_ptr = retval;
1372 }
1373 else
1374 TRACE("void stub implementation\n");
1375 }
1376
1377 stubMsg.Buffer = NULL;
1378 stubMsg.BufferLength = 0;
1379
1380 break;
1381 case STUBLESS_GETBUFFER:
1382 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1383 NdrStubGetBuffer(pThis, pChannel, &stubMsg);
1384 else
1385 {
1386 RPC_STATUS Status;
1387
1388 pRpcMsg->BufferLength = stubMsg.BufferLength;
1389 /* allocate buffer for [out] and [ret] params */
1390 Status = I_RpcGetBuffer(pRpcMsg);
1391 if (Status)
1392 RpcRaiseException(Status);
1393 stubMsg.Buffer = pRpcMsg->Buffer;
1394 }
1395 break;
1396 case STUBLESS_UNMARSHAL:
1397 case STUBLESS_INITOUT:
1398 case STUBLESS_CALCSIZE:
1399 case STUBLESS_MARSHAL:
1400 case STUBLESS_FREE:
1401 retval_ptr = stub_do_args(&stubMsg, pFormat, phase, number_of_params);
1402 break;
1403 default:
1404 ERR("shouldn't reach here. phase %d\n", phase);
1405 break;
1406 }
1407 }
1408
1409 pRpcMsg->BufferLength = (unsigned int)(stubMsg.Buffer - (unsigned char *)pRpcMsg->Buffer);
1410
1411 if (ext_flags.HasNewCorrDesc)
1412 {
1413 /* free extra correlation package */
1414 /* NdrCorrelationFree(&stubMsg); */
1415 }
1416
1417 if (Oif_flags.HasPipes)
1418 {
1419 /* NdrPipesDone(...) */
1420 }
1421
1422 /* free the full pointer translation tables */
1423 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1424 NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
1425
1426 /* free server function stack */
1427 HeapFree(GetProcessHeap(), 0, args);
1428
1429 return S_OK;
1430 }
1431
1432 /***********************************************************************
1433 * NdrServerCall2 [RPCRT4.@]
1434 */
1435 void WINAPI NdrServerCall2(PRPC_MESSAGE pRpcMsg)
1436 {
1437 DWORD dwPhase;
1438 NdrStubCall2(NULL, NULL, pRpcMsg, &dwPhase);
1439 }
1440
1441 /***********************************************************************
1442 * NdrStubCall [RPCRT4.@]
1443 */
1444 LONG WINAPI NdrStubCall( struct IRpcStubBuffer *This, struct IRpcChannelBuffer *channel,
1445 PRPC_MESSAGE msg, DWORD *phase )
1446 {
1447 return NdrStubCall2( This, channel, msg, phase );
1448 }
1449
1450 /***********************************************************************
1451 * NdrServerCall [RPCRT4.@]
1452 */
1453 void WINAPI NdrServerCall( PRPC_MESSAGE msg )
1454 {
1455 DWORD phase;
1456 NdrStubCall( NULL, NULL, msg, &phase );
1457 }
1458
1459 struct async_call_data
1460 {
1461 MIDL_STUB_MESSAGE *pStubMsg;
1462 const NDR_PROC_HEADER *pProcHeader;
1463 PFORMAT_STRING pHandleFormat;
1464 PFORMAT_STRING pParamFormat;
1465 RPC_BINDING_HANDLE hBinding;
1466 /* size of stack */
1467 unsigned short stack_size;
1468 /* number of parameters. optional for client to give it to us */
1469 unsigned int number_of_params;
1470 /* correlation cache */
1471 ULONG_PTR NdrCorrCache[256];
1472 };
1473
1474 LONG_PTR CDECL ndr_async_client_call( PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat, void **stack_top )
1475 {
1476 /* pointer to start of stack where arguments start */
1477 PRPC_MESSAGE pRpcMsg;
1478 PMIDL_STUB_MESSAGE pStubMsg;
1479 RPC_ASYNC_STATE *pAsync;
1480 struct async_call_data *async_call_data;
1481 /* procedure number */
1482 unsigned short procedure_number;
1483 /* cache of Oif_flags from v2 procedure header */
1484 INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1485 /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1486 INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1487 /* header for procedure string */
1488 const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1489 /* -Oif or -Oicf generated format */
1490 BOOL bV2Format = FALSE;
1491 RPC_STATUS status;
1492
1493 TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
1494
1495 /* Later NDR language versions probably won't be backwards compatible */
1496 if (pStubDesc->Version > 0x50002)
1497 {
1498 FIXME("Incompatible stub description version: 0x%x\n", pStubDesc->Version);
1499 RpcRaiseException(RPC_X_WRONG_STUB_VERSION);
1500 }
1501
1502 async_call_data = I_RpcAllocate(sizeof(*async_call_data) + sizeof(MIDL_STUB_MESSAGE) + sizeof(RPC_MESSAGE));
1503 if (!async_call_data) RpcRaiseException(ERROR_OUTOFMEMORY);
1504 async_call_data->pProcHeader = pProcHeader;
1505
1506 async_call_data->pStubMsg = pStubMsg = (PMIDL_STUB_MESSAGE)(async_call_data + 1);
1507 pRpcMsg = (PRPC_MESSAGE)(pStubMsg + 1);
1508
1509 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1510 {
1511 const NDR_PROC_HEADER_RPC *header_rpc = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1512 async_call_data->stack_size = header_rpc->stack_size;
1513 procedure_number = header_rpc->proc_num;
1514 pFormat += sizeof(NDR_PROC_HEADER_RPC);
1515 }
1516 else
1517 {
1518 async_call_data->stack_size = pProcHeader->stack_size;
1519 procedure_number = pProcHeader->proc_num;
1520 pFormat += sizeof(NDR_PROC_HEADER);
1521 }
1522 TRACE("stack size: 0x%x\n", async_call_data->stack_size);
1523 TRACE("proc num: %d\n", procedure_number);
1524
1525 /* create the full pointer translation tables, if requested */
1526 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1527 pStubMsg->FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
1528
1529 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1530 {
1531 ERR("objects not supported\n");
1532 I_RpcFree(async_call_data);
1533 RpcRaiseException(RPC_X_BAD_STUB_DATA);
1534 }
1535
1536 NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDesc, procedure_number);
1537
1538 TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1539 TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
1540
1541 /* needed for conformance of top-level objects */
1542 pStubMsg->StackTop = I_RpcAllocate(async_call_data->stack_size);
1543 memcpy(pStubMsg->StackTop, stack_top, async_call_data->stack_size);
1544
1545 pAsync = *(RPC_ASYNC_STATE **)pStubMsg->StackTop;
1546 pAsync->StubInfo = async_call_data;
1547 async_call_data->pHandleFormat = pFormat;
1548
1549 pFormat = client_get_handle(pStubMsg, pProcHeader, async_call_data->pHandleFormat, &async_call_data->hBinding);
1550 if (!pFormat) goto done;
1551
1552 bV2Format = (pStubDesc->Version >= 0x20000);
1553
1554 if (bV2Format)
1555 {
1556 const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
1557 (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
1558
1559 Oif_flags = pOIFHeader->Oi2Flags;
1560 async_call_data->number_of_params = pOIFHeader->number_of_params;
1561
1562 pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1563
1564 TRACE("Oif_flags = %s\n", debugstr_INTERPRETER_OPT_FLAGS(Oif_flags) );
1565
1566 if (Oif_flags.HasExtensions)
1567 {
1568 const NDR_PROC_HEADER_EXTS *pExtensions =
1569 (const NDR_PROC_HEADER_EXTS *)pFormat;
1570 ext_flags = pExtensions->Flags2;
1571 pFormat += pExtensions->Size;
1572 }
1573 }
1574 else
1575 {
1576 pFormat = convert_old_args( pStubMsg, pFormat, async_call_data->stack_size,
1577 pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT,
1578 async_call_data->NdrCorrCache, sizeof(async_call_data->NdrCorrCache),
1579 &async_call_data->number_of_params );
1580 }
1581
1582 async_call_data->pParamFormat = pFormat;
1583
1584 pStubMsg->BufferLength = 0;
1585
1586 /* store the RPC flags away */
1587 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1588 pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1589
1590 /* use alternate memory allocation routines */
1591 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
1592 NdrRpcSmSetClientToOsf(pStubMsg);
1593
1594 if (Oif_flags.HasPipes)
1595 {
1596 FIXME("pipes not supported yet\n");
1597 RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1598 /* init pipes package */
1599 /* NdrPipesInitialize(...) */
1600 }
1601 if (ext_flags.HasNewCorrDesc)
1602 {
1603 /* initialize extra correlation package */
1604 NdrCorrelationInitialize(pStubMsg, async_call_data->NdrCorrCache, sizeof(async_call_data->NdrCorrCache), 0);
1605 }
1606
1607 /* order of phases:
1608 * 1. CALCSIZE - calculate the buffer size
1609 * 2. GETBUFFER - allocate the buffer
1610 * 3. MARSHAL - marshal [in] params into the buffer
1611 * 4. SENDRECEIVE - send buffer
1612 * Then in NdrpCompleteAsyncClientCall:
1613 * 1. SENDRECEIVE - receive buffer
1614 * 2. UNMARSHAL - unmarshal [out] params from buffer
1615 */
1616
1617 /* 1. CALCSIZE */
1618 TRACE( "CALCSIZE\n" );
1619 client_do_args(pStubMsg, pFormat, STUBLESS_CALCSIZE, NULL, async_call_data->number_of_params, NULL);
1620
1621 /* 2. GETBUFFER */
1622 TRACE( "GETBUFFER\n" );
1623 if (Oif_flags.HasPipes)
1624 /* NdrGetPipeBuffer(...) */
1625 FIXME("pipes not supported yet\n");
1626 else
1627 {
1628 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
1629 #if 0
1630 NdrNsGetBuffer(pStubMsg, pStubMsg->BufferLength, async_call_data->hBinding);
1631 #else
1632 FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
1633 #endif
1634 else
1635 NdrGetBuffer(pStubMsg, pStubMsg->BufferLength, async_call_data->hBinding);
1636 }
1637 pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1638 status = I_RpcAsyncSetHandle(pRpcMsg, pAsync);
1639 if (status != RPC_S_OK)
1640 RpcRaiseException(status);
1641
1642 /* 3. MARSHAL */
1643 TRACE( "MARSHAL\n" );
1644 client_do_args(pStubMsg, pFormat, STUBLESS_MARSHAL, NULL, async_call_data->number_of_params, NULL);
1645
1646 /* 4. SENDRECEIVE */
1647 TRACE( "SEND\n" );
1648 pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1649 /* send the [in] params only */
1650 if (Oif_flags.HasPipes)
1651 /* NdrPipesSend(...) */
1652 FIXME("pipes not supported yet\n");
1653 else
1654 {
1655 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
1656 #if 0
1657 NdrNsSend(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
1658 #else
1659 FIXME("using auto handle - call NdrNsSend when it gets implemented\n");
1660 #endif
1661 else
1662 {
1663 pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
1664 status = I_RpcSend(pStubMsg->RpcMsg);
1665 if (status != RPC_S_OK)
1666 RpcRaiseException(status);
1667 }
1668 }
1669
1670 done:
1671 TRACE("returning 0\n");
1672 return 0;
1673 }
1674
1675 RPC_STATUS NdrpCompleteAsyncClientCall(RPC_ASYNC_STATE *pAsync, void *Reply)
1676 {
1677 /* pointer to start of stack where arguments start */
1678 PMIDL_STUB_MESSAGE pStubMsg;
1679 struct async_call_data *async_call_data;
1680 /* header for procedure string */
1681 const NDR_PROC_HEADER * pProcHeader;
1682 RPC_STATUS status = RPC_S_OK;
1683
1684 if (!pAsync->StubInfo)
1685 return RPC_S_INVALID_ASYNC_HANDLE;
1686
1687 async_call_data = pAsync->StubInfo;
1688 pStubMsg = async_call_data->pStubMsg;
1689 pProcHeader = async_call_data->pProcHeader;
1690
1691 /* order of phases:
1692 * 1. CALCSIZE - calculate the buffer size
1693 * 2. GETBUFFER - allocate the buffer
1694 * 3. MARSHAL - marshal [in] params into the buffer
1695 * 4. SENDRECEIVE - send buffer
1696 * Then in NdrpCompleteAsyncClientCall:
1697 * 1. SENDRECEIVE - receive buffer
1698 * 2. UNMARSHAL - unmarshal [out] params from buffer
1699 */
1700
1701 /* 1. SENDRECEIVE */
1702 TRACE( "RECEIVE\n" );
1703 pStubMsg->RpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1704 /* receive the [out] params */
1705 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
1706 #if 0
1707 NdrNsReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
1708 #else
1709 FIXME("using auto handle - call NdrNsReceive when it gets implemented\n");
1710 #endif
1711 else
1712 {
1713 status = I_RpcReceive(pStubMsg->RpcMsg);
1714 if (status != RPC_S_OK)
1715 goto cleanup;
1716 pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
1717 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
1718 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
1719 pStubMsg->Buffer = pStubMsg->BufferStart;
1720 }
1721
1722 /* convert strings, floating point values and endianness into our
1723 * preferred format */
1724 #if 0
1725 if ((pStubMsg->RpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1726 NdrConvert(pStubMsg, pFormat);
1727 #endif
1728
1729 /* 2. UNMARSHAL */
1730 TRACE( "UNMARSHAL\n" );
1731 client_do_args(pStubMsg, async_call_data->pParamFormat, STUBLESS_UNMARSHAL,
1732 NULL, async_call_data->number_of_params, Reply);
1733
1734 cleanup:
1735 if (pStubMsg->fHasNewCorrDesc)
1736 {
1737 /* free extra correlation package */
1738 NdrCorrelationFree(pStubMsg);
1739 }
1740
1741 /* free the full pointer translation tables */
1742 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1743 NdrFullPointerXlatFree(pStubMsg->FullPtrXlatTables);
1744
1745 /* free marshalling buffer */
1746 NdrFreeBuffer(pStubMsg);
1747 client_free_handle(pStubMsg, pProcHeader, async_call_data->pHandleFormat, async_call_data->hBinding);
1748
1749 I_RpcFree(pStubMsg->StackTop);
1750 I_RpcFree(async_call_data);
1751
1752 TRACE("-- 0x%x\n", status);
1753 return status;
1754 }
1755
1756 #ifdef __x86_64__
1757
1758 __ASM_GLOBAL_FUNC( NdrAsyncClientCall,
1759 "movq %r8,0x18(%rsp)\n\t"
1760 "movq %r9,0x20(%rsp)\n\t"
1761 "leaq 0x18(%rsp),%r8\n\t"
1762 "subq $0x28,%rsp\n\t"
1763 __ASM_CFI(".cfi_adjust_cfa_offset 0x28\n\t")
1764 "call " __ASM_NAME("ndr_async_client_call") "\n\t"
1765 "addq $0x28,%rsp\n\t"
1766 __ASM_CFI(".cfi_adjust_cfa_offset -0x28\n\t")
1767 "ret" );
1768
1769 #else /* __x86_64__ */
1770
1771 /***********************************************************************
1772 * NdrAsyncClientCall [RPCRT4.@]
1773 */
1774 CLIENT_CALL_RETURN WINAPIV NdrAsyncClientCall( PMIDL_STUB_DESC desc, PFORMAT_STRING format, ... )
1775 {
1776 __ms_va_list args;
1777 LONG_PTR ret;
1778
1779 __ms_va_start( args, format );
1780 ret = ndr_async_client_call( desc, format, va_arg( args, void ** ));
1781 __ms_va_end( args );
1782 return *(CLIENT_CALL_RETURN *)&ret;
1783 }
1784
1785 #endif /* __x86_64__ */
1786
1787 RPCRTAPI LONG RPC_ENTRY NdrAsyncStubCall(struct IRpcStubBuffer* pThis,
1788 struct IRpcChannelBuffer* pChannel, PRPC_MESSAGE pRpcMsg,
1789 DWORD * pdwStubPhase)
1790 {
1791 FIXME("unimplemented, expect crash!\n");
1792 return 0;
1793 }