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