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