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