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