cabinet: Fixed free_decompression_temps/_mem , handle NULL (Coverity).
[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 /* the return type should be CLIENT_CALL_RETURN, but this is incompatible
552  * with the way gcc returns structures. "void *" should be the largest type
553  * that MIDL should allow you to return anyway */
554 LONG_PTR WINAPIV NdrClientCall2(PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat, ...)
555 {
556     /* pointer to start of stack where arguments start */
557     RPC_MESSAGE rpcMsg;
558     MIDL_STUB_MESSAGE stubMsg;
559     handle_t hBinding = NULL;
560     /* procedure number */
561     unsigned short procedure_number;
562     /* size of stack */
563     unsigned short stack_size;
564     /* number of parameters. optional for client to give it to us */
565     unsigned char number_of_params = ~0;
566     /* cache of Oif_flags from v2 procedure header */
567     INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
568     /* cache of extension flags from NDR_PROC_HEADER_EXTS */
569     INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
570     /* the type of pass we are currently doing */
571     int phase;
572     /* header for procedure string */
573     const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
574     /* -Oif or -Oicf generated format */
575     BOOL bV2Format = FALSE;
576     /* the value to return to the client from the remote procedure */
577     LONG_PTR RetVal = 0;
578     /* the pointer to the object when in OLE mode */
579     void * This = NULL;
580     PFORMAT_STRING pHandleFormat;
581     /* correlation cache */
582     unsigned long NdrCorrCache[256];
583
584     TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
585
586     TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
587
588     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
589     {
590         const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
591         stack_size = pProcHeader->stack_size;
592         procedure_number = pProcHeader->proc_num;
593         pFormat += sizeof(NDR_PROC_HEADER_RPC);
594     }
595     else
596     {
597         stack_size = pProcHeader->stack_size;
598         procedure_number = pProcHeader->proc_num;
599         pFormat += sizeof(NDR_PROC_HEADER);
600     }
601     TRACE("stack size: 0x%x\n", stack_size);
602     TRACE("proc num: %d\n", procedure_number);
603
604     /* create the full pointer translation tables, if requested */
605     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
606         stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
607
608     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
609     {
610         /* object is always the first argument */
611         This = **(void *const **)(&pFormat+1);
612         NdrProxyInitialize(This, &rpcMsg, &stubMsg, pStubDesc, procedure_number);
613     }
614     else
615         NdrClientInitializeNew(&rpcMsg, &stubMsg, pStubDesc, procedure_number);
616
617     TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
618     TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
619
620     /* needed for conformance of top-level objects */
621 #ifdef __i386__
622     stubMsg.StackTop = *(unsigned char **)(&pFormat+1);
623 #else
624 # warning Stack not retrieved for your CPU architecture
625 #endif
626
627     pHandleFormat = pFormat;
628
629     /* we only need a handle if this isn't an object method */
630     if (!(pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT))
631     {
632         pFormat = client_get_handle(&stubMsg, pProcHeader, pHandleFormat, &hBinding);
633         if (!pFormat) return 0;
634     }
635
636     bV2Format = (pStubDesc->Version >= 0x20000);
637
638     if (bV2Format)
639     {
640         const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
641             (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
642
643         Oif_flags = pOIFHeader->Oi2Flags;
644         number_of_params = pOIFHeader->number_of_params;
645
646         pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
647     }
648
649     TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
650
651     if (Oif_flags.HasExtensions)
652     {
653         const NDR_PROC_HEADER_EXTS *pExtensions =
654             (const NDR_PROC_HEADER_EXTS *)pFormat;
655         ext_flags = pExtensions->Flags2;
656         pFormat += pExtensions->Size;
657     }
658
659     stubMsg.BufferLength = 0;
660
661     /* store the RPC flags away */
662     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
663         rpcMsg.RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
664
665     /* use alternate memory allocation routines */
666     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
667         NdrRpcSmSetClientToOsf(&stubMsg);
668
669     if (Oif_flags.HasPipes)
670     {
671         FIXME("pipes not supported yet\n");
672         RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
673         /* init pipes package */
674         /* NdrPipesInitialize(...) */
675     }
676     if (ext_flags.HasNewCorrDesc)
677     {
678         /* initialize extra correlation package */
679         NdrCorrelationInitialize(&stubMsg, NdrCorrCache, sizeof(NdrCorrCache), 0);
680     }
681
682     /* order of phases:
683      * 1. PROXY_CALCSIZE - calculate the buffer size
684      * 2. PROXY_GETBUFFER - allocate the buffer
685      * 3. PROXY_MARHSAL - marshal [in] params into the buffer
686      * 4. PROXY_SENDRECEIVE - send/receive buffer
687      * 5. PROXY_UNMARHSAL - unmarshal [out] params from buffer
688      */
689     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
690     {
691         __TRY
692         {
693             for (phase = PROXY_CALCSIZE; phase <= PROXY_UNMARSHAL; phase++)
694             {
695                 TRACE("phase = %d\n", phase);
696                 switch (phase)
697                 {
698                 case PROXY_GETBUFFER:
699                     /* allocate the buffer */
700                     NdrProxyGetBuffer(This, &stubMsg);
701                     break;
702                 case PROXY_SENDRECEIVE:
703                     /* send the [in] params and receive the [out] and [retval]
704                      * params */
705                     NdrProxySendReceive(This, &stubMsg);
706
707                     /* convert strings, floating point values and endianess into our
708                      * preferred format */
709                     if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
710                         NdrConvert(&stubMsg, pFormat);
711
712                     break;
713                 case PROXY_CALCSIZE:
714                 case PROXY_MARSHAL:
715                 case PROXY_UNMARSHAL:
716                     if (bV2Format)
717                         client_do_args(&stubMsg, pFormat, phase, stubMsg.StackTop,
718                             number_of_params, (unsigned char *)&RetVal);
719                     else
720                         client_do_args_old_format(&stubMsg, pFormat, phase,
721                             stubMsg.StackTop, stack_size, (unsigned char *)&RetVal,
722                             (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT), FALSE);
723                     break;
724                 default:
725                     ERR("shouldn't reach here. phase %d\n", phase);
726                     break;
727                 }
728             }
729         }
730         __EXCEPT_ALL
731         {
732             RetVal = NdrProxyErrorHandler(GetExceptionCode());
733         }
734         __ENDTRY
735     }
736     else
737     {
738         /* order of phases:
739          * 1. PROXY_CALCSIZE - calculate the buffer size
740          * 2. PROXY_GETBUFFER - allocate the buffer
741          * 3. PROXY_MARHSAL - marshal [in] params into the buffer
742          * 4. PROXY_SENDRECEIVE - send/receive buffer
743          * 5. PROXY_UNMARHSAL - unmarshal [out] params from buffer
744          */
745         for (phase = PROXY_CALCSIZE; phase <= PROXY_UNMARSHAL; phase++)
746         {
747             TRACE("phase = %d\n", phase);
748             switch (phase)
749             {
750             case PROXY_GETBUFFER:
751                 /* allocate the buffer */
752                 if (Oif_flags.HasPipes)
753                     /* NdrGetPipeBuffer(...) */
754                     FIXME("pipes not supported yet\n");
755                 else
756                 {
757                     if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
758 #if 0
759                         NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
760 #else
761                         FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
762 #endif
763                     else
764                         NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
765                 }
766                 break;
767             case PROXY_SENDRECEIVE:
768                 /* send the [in] params and receive the [out] and [retval]
769                  * params */
770                 if (Oif_flags.HasPipes)
771                     /* NdrPipesSendReceive(...) */
772                     FIXME("pipes not supported yet\n");
773                 else
774                 {
775                     if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
776 #if 0
777                         NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
778 #else
779                         FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
780 #endif
781                     else
782                         NdrSendReceive(&stubMsg, stubMsg.Buffer);
783                 }
784
785                 /* convert strings, floating point values and endianess into our
786                  * preferred format */
787                 if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
788                     NdrConvert(&stubMsg, pFormat);
789
790                 break;
791             case PROXY_CALCSIZE:
792             case PROXY_MARSHAL:
793             case PROXY_UNMARSHAL:
794                 if (bV2Format)
795                     client_do_args(&stubMsg, pFormat, phase, stubMsg.StackTop,
796                         number_of_params, (unsigned char *)&RetVal);
797                 else
798                     client_do_args_old_format(&stubMsg, pFormat, phase,
799                         stubMsg.StackTop, stack_size, (unsigned char *)&RetVal,
800                         (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT), FALSE);
801                 break;
802             default:
803                 ERR("shouldn't reach here. phase %d\n", phase);
804                 break;
805             }
806         }
807     }
808
809     if (ext_flags.HasNewCorrDesc)
810     {
811         /* free extra correlation package */
812         NdrCorrelationFree(&stubMsg);
813     }
814
815     if (Oif_flags.HasPipes)
816     {
817         /* NdrPipesDone(...) */
818     }
819
820     /* free the full pointer translation tables */
821     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
822         NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
823
824     /* free marshalling buffer */
825     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
826         NdrProxyFreeBuffer(This, &stubMsg);
827     else
828     {
829         NdrFreeBuffer(&stubMsg);
830         client_free_handle(&stubMsg, pProcHeader, pHandleFormat, hBinding);
831     }
832
833     TRACE("RetVal = 0x%lx\n", RetVal);
834
835     return RetVal;
836 }
837
838 /* Calls a function with the specified arguments, restoring the stack
839  * properly afterwards as we don't know the calling convention of the
840  * function */
841 #if defined __i386__ && defined _MSC_VER
842 __declspec(naked) LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size)
843 {
844     __asm
845     {
846         push ebp
847         push edi            ; Save registers
848         push esi
849         mov ebp, esp
850         mov eax, [ebp+16]   ; Get stack size
851         sub esp, eax        ; Make room in stack for arguments
852         mov edi, esp
853         mov ecx, eax
854         mov esi, [ebp+12]
855         shr ecx, 2
856         cld
857         rep movsd           ; Copy dword blocks
858         call [ebp+8]        ; Call function
859         lea esp, [ebp-8]    ; Restore stack
860         pop esi             ; Restore registers
861         pop edi
862         pop ebp
863         ret
864     }
865 }
866 #elif defined __i386__ && defined __GNUC__
867 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
868 __ASM_GLOBAL_FUNC(call_server_func,
869     "pushl %ebp\n\t"
870     __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
871     __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
872     "movl %esp,%ebp\n\t"
873     __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
874     "pushl %edi\n\t"            /* Save registers */
875     __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
876     "pushl %esi\n\t"
877     __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
878     "movl 16(%ebp), %eax\n\t"   /* Get stack size */
879     "subl %eax, %esp\n\t"       /* Make room in stack for arguments */
880     "andl $~15, %esp\n\t"       /* Make sure stack has 16-byte alignment for Mac OS X */
881     "movl %esp, %edi\n\t"
882     "movl %eax, %ecx\n\t"
883     "movl 12(%ebp), %esi\n\t"
884     "shrl $2, %ecx\n\t"         /* divide by 4 */
885     "cld\n\t"
886     "rep; movsl\n\t"            /* Copy dword blocks */
887     "call *8(%ebp)\n\t"         /* Call function */
888     "leal -8(%ebp), %esp\n\t"   /* Restore stack */
889     "popl %esi\n\t"             /* Restore registers */
890     __ASM_CFI(".cfi_same_value %esi\n\t")
891     "popl %edi\n\t"
892     __ASM_CFI(".cfi_same_value %edi\n\t")
893     "popl %ebp\n\t"
894     __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
895     __ASM_CFI(".cfi_same_value %ebp\n\t")
896     "ret" )
897 #else
898 #warning call_server_func not implemented for your architecture
899 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned short stack_size)
900 {
901     FIXME("Not implemented for your architecture\n");
902     return 0;
903 }
904 #endif
905
906 static DWORD calc_arg_size(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat)
907 {
908     DWORD size;
909     switch(*pFormat)
910     {
911     case RPC_FC_STRUCT:
912         size = *(const WORD*)(pFormat + 2);
913         break;
914     case RPC_FC_CARRAY:
915         size = *(const WORD*)(pFormat + 2);
916         ComputeConformance(pStubMsg, NULL, pFormat + 4, 0);
917         size *= pStubMsg->MaxCount;
918         break;
919     case RPC_FC_SMFARRAY:
920         size = *(const WORD*)(pFormat + 2);
921         break;
922     case RPC_FC_LGFARRAY:
923         size = *(const DWORD*)(pFormat + 2);
924         break;
925     case RPC_FC_BOGUS_ARRAY:
926         pFormat = ComputeConformance(pStubMsg, NULL, pFormat + 4, *(const WORD*)&pFormat[2]);
927         TRACE("conformance = %ld\n", pStubMsg->MaxCount);
928         pFormat = ComputeVariance(pStubMsg, NULL, pFormat, pStubMsg->MaxCount);
929         size = ComplexStructSize(pStubMsg, pFormat);
930         size *= pStubMsg->MaxCount;
931         break;
932     default:
933         FIXME("Unhandled type %02x\n", *pFormat);
934         /* fallthrough */
935     case RPC_FC_RP:
936         size = sizeof(void *);
937         break;
938     }
939     return size;
940 }
941
942 static LONG_PTR *stub_do_args(MIDL_STUB_MESSAGE *pStubMsg,
943                               PFORMAT_STRING pFormat, int phase,
944                               unsigned char *args,
945                               unsigned short number_of_params)
946 {
947     /* counter */
948     unsigned short i;
949     /* current format string offset */
950     int current_offset = 0;
951     /* current stack offset */
952     unsigned short current_stack_offset = 0;
953     /* location to put retval into */
954     LONG_PTR *retval_ptr = NULL;
955
956     for (i = 0; i < number_of_params; i++)
957     {
958         const NDR_PARAM_OIF_BASETYPE *pParam =
959         (const NDR_PARAM_OIF_BASETYPE *)&pFormat[current_offset];
960         unsigned char *pArg;
961
962         current_stack_offset = pParam->stack_offset;
963         pArg = args + current_stack_offset;
964
965         TRACE("param[%d]: new format\n", i);
966         TRACE("\tparam_attributes:"); dump_RPC_FC_PROC_PF(pParam->param_attributes); TRACE("\n");
967         TRACE("\tstack_offset: 0x%x\n", current_stack_offset);
968         TRACE("\tmemory addr (before): %p -> %p\n", pArg, *(unsigned char **)pArg);
969
970         if (pParam->param_attributes.IsBasetype)
971         {
972             const unsigned char *pTypeFormat =
973             &pParam->type_format_char;
974
975             TRACE("\tbase type: 0x%02x\n", *pTypeFormat);
976
977             /* make a note of the address of the return value parameter for later */
978             if (pParam->param_attributes.IsReturn)
979                 retval_ptr = (LONG_PTR *)pArg;
980
981             switch (phase)
982             {
983                 case STUBLESS_MARSHAL:
984                     if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
985                     {
986                         if (pParam->param_attributes.IsSimpleRef)
987                             call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
988                         else
989                             call_marshaller(pStubMsg, pArg, pTypeFormat);
990                     }
991                     break;
992                 case STUBLESS_FREE:
993                     if (pParam->param_attributes.ServerAllocSize)
994                         HeapFree(GetProcessHeap(), 0, *(void **)pArg);
995                     break;
996                 case STUBLESS_INITOUT:
997                     break;
998                 case STUBLESS_UNMARSHAL:
999                     if (pParam->param_attributes.ServerAllocSize)
1000                         *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1001                                                    pParam->param_attributes.ServerAllocSize * 8);
1002
1003                     if (pParam->param_attributes.IsIn)
1004                     {
1005                         if (pParam->param_attributes.IsSimpleRef)
1006                             call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1007                         else
1008                             call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1009                     }
1010                     break;
1011                 case STUBLESS_CALCSIZE:
1012                     if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1013                     {
1014                         if (pParam->param_attributes.IsSimpleRef)
1015                             call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1016                         else
1017                             call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1018                     }
1019                     break;
1020                 default:
1021                     RpcRaiseException(RPC_S_INTERNAL_ERROR);
1022             }
1023
1024             current_offset += sizeof(NDR_PARAM_OIF_BASETYPE);
1025         }
1026         else
1027         {
1028             const NDR_PARAM_OIF_OTHER *pParamOther =
1029             (const NDR_PARAM_OIF_OTHER *)&pFormat[current_offset];
1030
1031             const unsigned char * pTypeFormat =
1032                 &(pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset]);
1033
1034             TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1035
1036             switch (phase)
1037             {
1038                 case STUBLESS_MARSHAL:
1039                     if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1040                     {
1041                         if (pParam->param_attributes.IsByValue)
1042                             call_marshaller(pStubMsg, pArg, pTypeFormat);
1043                         else
1044                             call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1045                     }
1046                     break;
1047                 case STUBLESS_FREE:
1048                     if (pParam->param_attributes.MustFree)
1049                     {
1050                         if (pParam->param_attributes.IsByValue)
1051                             call_freer(pStubMsg, pArg, pTypeFormat);
1052                         else
1053                             call_freer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1054                     }
1055
1056                     if (pParam->param_attributes.IsOut &&
1057                         !pParam->param_attributes.IsIn &&
1058                         !pParam->param_attributes.IsByValue &&
1059                         !pParam->param_attributes.ServerAllocSize)
1060                     {
1061                         pStubMsg->pfnFree(*(void **)pArg);
1062                     }
1063
1064                     if (pParam->param_attributes.ServerAllocSize)
1065                         HeapFree(GetProcessHeap(), 0, *(void **)pArg);
1066                     break;
1067                 case STUBLESS_INITOUT:
1068                     if (!pParam->param_attributes.IsIn &&
1069                              pParam->param_attributes.IsOut &&
1070                              !pParam->param_attributes.ServerAllocSize &&
1071                              !pParam->param_attributes.IsByValue)
1072                     {
1073                         DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1074
1075                         if(size)
1076                         {
1077                             *(void **)pArg = NdrAllocate(pStubMsg, size);
1078                             memset(*(void **)pArg, 0, size);
1079                         }
1080                     }
1081                     break;
1082                 case STUBLESS_UNMARSHAL:
1083                     if (pParam->param_attributes.ServerAllocSize)
1084                         *(void **)pArg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1085                                                    pParam->param_attributes.ServerAllocSize * 8);
1086
1087                     if (pParam->param_attributes.IsIn)
1088                     {
1089                         if (pParam->param_attributes.IsByValue)
1090                             call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1091                         else
1092                             call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1093                     }
1094                     break;
1095                 case STUBLESS_CALCSIZE:
1096                     if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1097                     {
1098                         if (pParam->param_attributes.IsByValue)
1099                             call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1100                         else
1101                             call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1102                     }
1103                     break;
1104                 default:
1105                     RpcRaiseException(RPC_S_INTERNAL_ERROR);
1106             }
1107
1108             current_offset += sizeof(NDR_PARAM_OIF_OTHER);
1109         }
1110         TRACE("\tmemory addr (after): %p -> %p\n", pArg, *(unsigned char **)pArg);
1111     }
1112
1113     return retval_ptr;
1114 }
1115
1116 static LONG_PTR *stub_do_old_args(MIDL_STUB_MESSAGE *pStubMsg,
1117                                   PFORMAT_STRING pFormat, int phase,
1118                                   unsigned char *args,
1119                                   unsigned short stack_size, BOOL object)
1120 {
1121     /* counter */
1122     unsigned short i;
1123     /* current format string offset */
1124     int current_offset = 0;
1125     /* current stack offset */
1126     unsigned short current_stack_offset = 0;
1127     /* location to put retval into */
1128     LONG_PTR *retval_ptr = NULL;
1129
1130     for (i = 0; TRUE; i++)
1131     {
1132         const NDR_PARAM_OI_BASETYPE *pParam =
1133         (const NDR_PARAM_OI_BASETYPE *)&pFormat[current_offset];
1134         /* note: current_stack_offset starts after the This pointer
1135          * if present, so adjust this */
1136         unsigned short current_stack_offset_adjusted = current_stack_offset +
1137             (object ? sizeof(void *) : 0);
1138         unsigned char *pArg = args + current_stack_offset_adjusted;
1139
1140         /* no more parameters; exit loop */
1141         if (current_stack_offset_adjusted >= stack_size)
1142             break;
1143
1144         TRACE("param[%d]: old format\n", i);
1145         TRACE("\tparam_direction: 0x%x\n", pParam->param_direction);
1146         TRACE("\tstack_offset: 0x%x\n", current_stack_offset_adjusted);
1147
1148         if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE ||
1149             pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1150         {
1151             const unsigned char *pTypeFormat =
1152             &pParam->type_format_char;
1153
1154             TRACE("\tbase type 0x%02x\n", *pTypeFormat);
1155
1156             if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1157                 retval_ptr = (LONG_PTR *)pArg;
1158
1159             switch (phase)
1160             {
1161                 case STUBLESS_MARSHAL:
1162                     if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1163                         call_marshaller(pStubMsg, pArg, pTypeFormat);
1164                     break;
1165                 case STUBLESS_FREE:
1166                     if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1167                         call_freer(pStubMsg, pArg, pTypeFormat);
1168                     break;
1169                 case STUBLESS_UNMARSHAL:
1170                     if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1171                         call_unmarshaller(pStubMsg, &pArg, pTypeFormat, 0);
1172                     break;
1173                 case STUBLESS_CALCSIZE:
1174                     if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1175                         call_buffer_sizer(pStubMsg, pArg, pTypeFormat);
1176                     break;
1177                 default:
1178                     RpcRaiseException(RPC_S_INTERNAL_ERROR);
1179             }
1180
1181             current_stack_offset += type_stack_size(*pTypeFormat);
1182             current_offset += sizeof(NDR_PARAM_OI_BASETYPE);
1183         }
1184         else
1185         {
1186             const NDR_PARAM_OI_OTHER *pParamOther =
1187             (const NDR_PARAM_OI_OTHER *)&pFormat[current_offset];
1188
1189             const unsigned char * pTypeFormat =
1190                 &pStubMsg->StubDesc->pFormatTypes[pParamOther->type_offset];
1191
1192             TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1193
1194             if (pParam->param_direction == RPC_FC_RETURN_PARAM)
1195                 retval_ptr = (LONG_PTR *)pArg;
1196
1197             switch (phase)
1198             {
1199                 case STUBLESS_MARSHAL:
1200                     if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1201                         pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1202                         pParam->param_direction == RPC_FC_RETURN_PARAM)
1203                         call_marshaller(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1204                     break;
1205                 case STUBLESS_FREE:
1206                     if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1207                         pParam->param_direction == RPC_FC_IN_PARAM)
1208                         call_freer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1209                     else if (pParam->param_direction == RPC_FC_OUT_PARAM)
1210                         pStubMsg->pfnFree(*(void **)pArg);
1211                     break;
1212                 case STUBLESS_INITOUT:
1213                     if (pParam->param_direction == RPC_FC_OUT_PARAM)
1214                     {
1215                         DWORD size = calc_arg_size(pStubMsg, pTypeFormat);
1216
1217                         if(size)
1218                         {
1219                             *(void **)pArg = NdrAllocate(pStubMsg, size);
1220                             memset(*(void **)pArg, 0, size);
1221                         }
1222                     }
1223                     break;
1224                 case STUBLESS_UNMARSHAL:
1225                     if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1226                         pParam->param_direction == RPC_FC_IN_PARAM)
1227                         call_unmarshaller(pStubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1228                     break;
1229                 case STUBLESS_CALCSIZE:
1230                     if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1231                         pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1232                         pParam->param_direction == RPC_FC_RETURN_PARAM)
1233                         call_buffer_sizer(pStubMsg, *(unsigned char **)pArg, pTypeFormat);
1234                     break;
1235                 default:
1236                     RpcRaiseException(RPC_S_INTERNAL_ERROR);
1237             }
1238
1239             current_stack_offset += pParamOther->stack_size * sizeof(INT);
1240             current_offset += sizeof(NDR_PARAM_OI_OTHER);
1241         }
1242     }
1243
1244     return retval_ptr;
1245 }
1246
1247 /***********************************************************************
1248  *            NdrStubCall2 [RPCRT4.@]
1249  *
1250  * Unmarshals [in] parameters, calls either a method in an object or a server
1251  * function, marshals any [out] parameters and frees any allocated data.
1252  *
1253  * NOTES
1254  *  Used by stubless MIDL-generated code.
1255  */
1256 LONG WINAPI NdrStubCall2(
1257     struct IRpcStubBuffer * pThis,
1258     struct IRpcChannelBuffer * pChannel,
1259     PRPC_MESSAGE pRpcMsg,
1260     DWORD * pdwStubPhase)
1261 {
1262     const MIDL_SERVER_INFO *pServerInfo;
1263     const MIDL_STUB_DESC *pStubDesc;
1264     PFORMAT_STRING pFormat;
1265     MIDL_STUB_MESSAGE stubMsg;
1266     /* pointer to start of stack to pass into stub implementation */
1267     unsigned char * args;
1268     /* size of stack */
1269     unsigned short stack_size;
1270     /* number of parameters. optional for client to give it to us */
1271     unsigned char number_of_params = ~0;
1272     /* cache of Oif_flags from v2 procedure header */
1273     INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1274     /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1275     INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1276     /* the type of pass we are currently doing */
1277     int phase;
1278     /* header for procedure string */
1279     const NDR_PROC_HEADER *pProcHeader;
1280     /* offset in format string for start of params */
1281     int parameter_start_offset;
1282     /* current format string offset */
1283     int current_offset;
1284     /* -Oif or -Oicf generated format */
1285     BOOL bV2Format = FALSE;
1286     /* location to put retval into */
1287     LONG_PTR *retval_ptr = NULL;
1288
1289     TRACE("pThis %p, pChannel %p, pRpcMsg %p, pdwStubPhase %p\n", pThis, pChannel, pRpcMsg, pdwStubPhase);
1290
1291     if (pThis)
1292         pServerInfo = CStdStubBuffer_GetServerInfo(pThis);
1293     else
1294         pServerInfo = ((RPC_SERVER_INTERFACE *)pRpcMsg->RpcInterfaceInformation)->InterpreterInfo;
1295
1296     pStubDesc = pServerInfo->pStubDesc;
1297     pFormat = pServerInfo->ProcString + pServerInfo->FmtStringOffset[pRpcMsg->ProcNum];
1298     pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1299
1300     TRACE("NDR Version: 0x%x\n", pStubDesc->Version);
1301
1302     /* create the full pointer translation tables, if requested */
1303     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1304         stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_SERVER);
1305
1306     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1307     {
1308         const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1309         stack_size = pProcHeader->stack_size;
1310         current_offset = sizeof(NDR_PROC_HEADER_RPC);
1311
1312     }
1313     else
1314     {
1315         stack_size = pProcHeader->stack_size;
1316         current_offset = sizeof(NDR_PROC_HEADER);
1317     }
1318
1319     TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1320
1321     /* binding */
1322     switch (pProcHeader->handle_type)
1323     {
1324     /* explicit binding: parse additional section */
1325     case RPC_FC_BIND_EXPLICIT:
1326         switch (pFormat[current_offset]) /* handle_type */
1327         {
1328         case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
1329             current_offset += sizeof(NDR_EHD_PRIMITIVE);
1330             break;
1331         case RPC_FC_BIND_GENERIC: /* explicit generic */
1332             current_offset += sizeof(NDR_EHD_GENERIC);
1333             break;
1334         case RPC_FC_BIND_CONTEXT: /* explicit context */
1335             current_offset += sizeof(NDR_EHD_CONTEXT);
1336             break;
1337         default:
1338             ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1339             RpcRaiseException(RPC_X_BAD_STUB_DATA);
1340         }
1341         break;
1342     case RPC_FC_BIND_GENERIC: /* implicit generic */
1343     case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
1344     case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
1345     case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
1346         break;
1347     default:
1348         ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1349         RpcRaiseException(RPC_X_BAD_STUB_DATA);
1350     }
1351
1352     bV2Format = (pStubDesc->Version >= 0x20000);
1353
1354     if (bV2Format)
1355     {
1356         const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
1357             (const NDR_PROC_PARTIAL_OIF_HEADER *)&pFormat[current_offset];
1358
1359         Oif_flags = pOIFHeader->Oi2Flags;
1360         number_of_params = pOIFHeader->number_of_params;
1361
1362         current_offset += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1363     }
1364
1365     TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
1366
1367     if (Oif_flags.HasExtensions)
1368     {
1369         const NDR_PROC_HEADER_EXTS *pExtensions =
1370             (const NDR_PROC_HEADER_EXTS *)&pFormat[current_offset];
1371         ext_flags = pExtensions->Flags2;
1372         current_offset += pExtensions->Size;
1373     }
1374
1375     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1376         NdrStubInitialize(pRpcMsg, &stubMsg, pStubDesc, pChannel);
1377     else
1378         NdrServerInitializeNew(pRpcMsg, &stubMsg, pStubDesc);
1379
1380     /* store the RPC flags away */
1381     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1382         pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1383
1384     /* use alternate memory allocation routines */
1385     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
1386 #if 0
1387           NdrRpcSsEnableAllocate(&stubMsg);
1388 #else
1389           FIXME("Set RPCSS memory allocation routines\n");
1390 #endif
1391
1392     if (Oif_flags.HasPipes)
1393     {
1394         FIXME("pipes not supported yet\n");
1395         RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1396         /* init pipes package */
1397         /* NdrPipesInitialize(...) */
1398     }
1399     if (ext_flags.HasNewCorrDesc)
1400     {
1401         /* initialize extra correlation package */
1402         FIXME("new correlation description not implemented\n");
1403         stubMsg.fHasNewCorrDesc = TRUE;
1404     }
1405
1406     /* convert strings, floating point values and endianess into our
1407      * preferred format */
1408     if ((pRpcMsg->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1409         NdrConvert(&stubMsg, pFormat);
1410
1411     parameter_start_offset = current_offset;
1412
1413     TRACE("allocating memory for stack of size %x\n", stack_size);
1414
1415     args = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, stack_size);
1416     stubMsg.StackTop = args; /* used by conformance of top-level objects */
1417
1418     /* add the implicit This pointer as the first arg to the function if we
1419      * are calling an object method */
1420     if (pThis)
1421         *(void **)args = ((CStdStubBuffer *)pThis)->pvServerObject;
1422
1423     /* order of phases:
1424      * 1. STUBLESS_UNMARHSAL - unmarshal [in] params from buffer
1425      * 2. STUBLESS_CALLSERVER - send/receive buffer
1426      * 3. STUBLESS_CALCSIZE - get [out] buffer size
1427      * 4. STUBLESS_GETBUFFER - allocate [out] buffer
1428      * 5. STUBLESS_MARHSAL - marshal [out] params to buffer
1429      */
1430     for (phase = STUBLESS_UNMARSHAL; phase <= STUBLESS_FREE; phase++)
1431     {
1432         TRACE("phase = %d\n", phase);
1433         switch (phase)
1434         {
1435         case STUBLESS_CALLSERVER:
1436             /* call the server function */
1437             if (pServerInfo->ThunkTable && pServerInfo->ThunkTable[pRpcMsg->ProcNum])
1438                 pServerInfo->ThunkTable[pRpcMsg->ProcNum](&stubMsg);
1439             else
1440             {
1441                 SERVER_ROUTINE func;
1442                 LONG_PTR retval;
1443
1444                 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1445                 {
1446                     SERVER_ROUTINE *vtbl = *(SERVER_ROUTINE **)((CStdStubBuffer *)pThis)->pvServerObject;
1447                     func = vtbl[pRpcMsg->ProcNum];
1448                 }
1449                 else
1450                     func = pServerInfo->DispatchTable[pRpcMsg->ProcNum];
1451
1452                 /* FIXME: what happens with return values that don't fit into a single register on x86? */
1453                 retval = call_server_func(func, args, stack_size);
1454
1455                 if (retval_ptr)
1456                 {
1457                     TRACE("stub implementation returned 0x%lx\n", retval);
1458                     *retval_ptr = retval;
1459                 }
1460                 else
1461                     TRACE("void stub implementation\n");
1462             }
1463
1464             stubMsg.Buffer = NULL;
1465             stubMsg.BufferLength = 0;
1466
1467             break;
1468         case STUBLESS_GETBUFFER:
1469             if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1470                 NdrStubGetBuffer(pThis, pChannel, &stubMsg);
1471             else
1472             {
1473                 RPC_STATUS Status;
1474
1475                 pRpcMsg->BufferLength = stubMsg.BufferLength;
1476                 /* allocate buffer for [out] and [ret] params */
1477                 Status = I_RpcGetBuffer(pRpcMsg); 
1478                 if (Status)
1479                     RpcRaiseException(Status);
1480                 stubMsg.Buffer = pRpcMsg->Buffer;
1481             }
1482             break;
1483         case STUBLESS_UNMARSHAL:
1484         case STUBLESS_INITOUT:
1485         case STUBLESS_CALCSIZE:
1486         case STUBLESS_MARSHAL:
1487         case STUBLESS_FREE:
1488             if (bV2Format)
1489                 retval_ptr = stub_do_args(&stubMsg, &pFormat[parameter_start_offset],
1490                                           phase, args, number_of_params);
1491             else
1492                 retval_ptr = stub_do_old_args(&stubMsg, &pFormat[parameter_start_offset],
1493                                               phase, args, stack_size,
1494                                               (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT));
1495
1496             break;
1497         default:
1498             ERR("shouldn't reach here. phase %d\n", phase);
1499             break;
1500         }
1501     }
1502
1503     pRpcMsg->BufferLength = (unsigned int)(stubMsg.Buffer - (unsigned char *)pRpcMsg->Buffer);
1504
1505     if (ext_flags.HasNewCorrDesc)
1506     {
1507         /* free extra correlation package */
1508         /* NdrCorrelationFree(&stubMsg); */
1509     }
1510
1511     if (Oif_flags.HasPipes)
1512     {
1513         /* NdrPipesDone(...) */
1514     }
1515
1516     /* free the full pointer translation tables */
1517     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1518         NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
1519
1520     /* free server function stack */
1521     HeapFree(GetProcessHeap(), 0, args);
1522
1523     return S_OK;
1524 }
1525
1526 /***********************************************************************
1527  *            NdrServerCall2 [RPCRT4.@]
1528  */
1529 void WINAPI NdrServerCall2(PRPC_MESSAGE pRpcMsg)
1530 {
1531     DWORD dwPhase;
1532     NdrStubCall2(NULL, NULL, pRpcMsg, &dwPhase);
1533 }
1534
1535 struct async_call_data
1536 {
1537     MIDL_STUB_MESSAGE *pStubMsg;
1538     const NDR_PROC_HEADER *pProcHeader;
1539     PFORMAT_STRING pHandleFormat;
1540     PFORMAT_STRING pParamFormat;
1541     RPC_BINDING_HANDLE hBinding;
1542     /* size of stack */
1543     unsigned short stack_size;
1544     /* number of parameters. optional for client to give it to us */
1545     unsigned char number_of_params;
1546     /* correlation cache */
1547     unsigned long NdrCorrCache[256];
1548 };
1549
1550 LONG_PTR WINAPIV NdrAsyncClientCall(PMIDL_STUB_DESC pStubDesc,
1551   PFORMAT_STRING pFormat, ...)
1552 {
1553     /* pointer to start of stack where arguments start */
1554     PRPC_MESSAGE pRpcMsg;
1555     PMIDL_STUB_MESSAGE pStubMsg;
1556     RPC_ASYNC_STATE *pAsync;
1557     struct async_call_data *async_call_data;
1558     /* procedure number */
1559     unsigned short procedure_number;
1560     /* cache of Oif_flags from v2 procedure header */
1561     INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1562     /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1563     INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1564     /* the type of pass we are currently doing */
1565     int phase;
1566     /* header for procedure string */
1567     const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1568     /* -Oif or -Oicf generated format */
1569     BOOL bV2Format = FALSE;
1570
1571     TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
1572
1573     /* Later NDR language versions probably won't be backwards compatible */
1574     if (pStubDesc->Version > 0x50002)
1575     {
1576         FIXME("Incompatible stub description version: 0x%x\n", pStubDesc->Version);
1577         RpcRaiseException(RPC_X_WRONG_STUB_VERSION);
1578     }
1579
1580     async_call_data = I_RpcAllocate(sizeof(*async_call_data) + sizeof(MIDL_STUB_MESSAGE) + sizeof(RPC_MESSAGE));
1581     if (!async_call_data) RpcRaiseException(ERROR_OUTOFMEMORY);
1582     async_call_data->number_of_params = ~0;
1583     async_call_data->pProcHeader = pProcHeader;
1584
1585     async_call_data->pStubMsg = pStubMsg = (PMIDL_STUB_MESSAGE)(async_call_data + 1);
1586     pRpcMsg = (PRPC_MESSAGE)(pStubMsg + 1);
1587
1588     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1589     {
1590         const NDR_PROC_HEADER_RPC *pProcHeader = (const NDR_PROC_HEADER_RPC *)&pFormat[0];
1591         async_call_data->stack_size = pProcHeader->stack_size;
1592         procedure_number = pProcHeader->proc_num;
1593         pFormat += sizeof(NDR_PROC_HEADER_RPC);
1594     }
1595     else
1596     {
1597         async_call_data->stack_size = pProcHeader->stack_size;
1598         procedure_number = pProcHeader->proc_num;
1599         pFormat += sizeof(NDR_PROC_HEADER);
1600     }
1601     TRACE("stack size: 0x%x\n", async_call_data->stack_size);
1602     TRACE("proc num: %d\n", procedure_number);
1603
1604     /* create the full pointer translation tables, if requested */
1605     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1606         pStubMsg->FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
1607
1608     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1609     {
1610         ERR("objects not supported\n");
1611         I_RpcFree(async_call_data);
1612         RpcRaiseException(RPC_X_BAD_STUB_DATA);
1613     }
1614
1615     NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDesc, procedure_number);
1616
1617     TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1618     TRACE("MIDL stub version = 0x%x\n", pStubDesc->MIDLVersion);
1619
1620     /* needed for conformance of top-level objects */
1621 #ifdef __i386__
1622     pStubMsg->StackTop = I_RpcAllocate(async_call_data->stack_size);
1623     /* FIXME: this may read one more DWORD than is necessary, but it shouldn't hurt */
1624     memcpy(pStubMsg->StackTop, *(unsigned char **)(&pFormat+1), async_call_data->stack_size);
1625 #else
1626 # warning Stack not retrieved for your CPU architecture
1627 #endif
1628
1629     pAsync = *(RPC_ASYNC_STATE **)pStubMsg->StackTop;
1630     pAsync->StubInfo = async_call_data;
1631     async_call_data->pHandleFormat = pFormat;
1632
1633     pFormat = client_get_handle(pStubMsg, pProcHeader, async_call_data->pHandleFormat, &async_call_data->hBinding);
1634     if (!pFormat) return 0;
1635
1636     bV2Format = (pStubDesc->Version >= 0x20000);
1637
1638     if (bV2Format)
1639     {
1640         const NDR_PROC_PARTIAL_OIF_HEADER *pOIFHeader =
1641             (const NDR_PROC_PARTIAL_OIF_HEADER *)pFormat;
1642
1643         Oif_flags = pOIFHeader->Oi2Flags;
1644         async_call_data->number_of_params = pOIFHeader->number_of_params;
1645
1646         pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1647     }
1648
1649     TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
1650
1651     if (Oif_flags.HasExtensions)
1652     {
1653         const NDR_PROC_HEADER_EXTS *pExtensions =
1654             (const NDR_PROC_HEADER_EXTS *)pFormat;
1655         ext_flags = pExtensions->Flags2;
1656         pFormat += pExtensions->Size;
1657     }
1658
1659     async_call_data->pParamFormat = pFormat;
1660
1661     pStubMsg->BufferLength = 0;
1662
1663     /* store the RPC flags away */
1664     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1665         pRpcMsg->RpcFlags = ((const NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1666
1667     /* use alternate memory allocation routines */
1668     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
1669         NdrRpcSmSetClientToOsf(pStubMsg);
1670
1671     if (Oif_flags.HasPipes)
1672     {
1673         FIXME("pipes not supported yet\n");
1674         RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1675         /* init pipes package */
1676         /* NdrPipesInitialize(...) */
1677     }
1678     if (ext_flags.HasNewCorrDesc)
1679     {
1680         /* initialize extra correlation package */
1681         NdrCorrelationInitialize(pStubMsg, async_call_data->NdrCorrCache, sizeof(async_call_data->NdrCorrCache), 0);
1682     }
1683
1684     /* order of phases:
1685      * 1. PROXY_CALCSIZE - calculate the buffer size
1686      * 2. PROXY_GETBUFFER - allocate the buffer
1687      * 3. PROXY_MARHSAL - marshal [in] params into the buffer
1688      * 4. PROXY_SENDRECEIVE - send buffer
1689      * Then in NdrpCompleteAsyncClientCall:
1690      * 1. PROXY_SENDRECEIVE - receive buffer
1691      * 2. PROXY_UNMARHSAL - unmarshal [out] params from buffer
1692      */
1693     for (phase = PROXY_CALCSIZE; phase <= PROXY_SENDRECEIVE; phase++)
1694     {
1695         RPC_STATUS status;
1696         TRACE("phase = %d\n", phase);
1697         switch (phase)
1698         {
1699         case PROXY_GETBUFFER:
1700             /* allocate the buffer */
1701             if (Oif_flags.HasPipes)
1702                 /* NdrGetPipeBuffer(...) */
1703                 FIXME("pipes not supported yet\n");
1704             else
1705             {
1706                 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
1707 #if 0
1708                     NdrNsGetBuffer(pStubMsg, pStubMsg->BufferLength, async_call_data->hBinding);
1709 #else
1710                     FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
1711 #endif
1712                 else
1713                     NdrGetBuffer(pStubMsg, pStubMsg->BufferLength, async_call_data->hBinding);
1714             }
1715             pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1716             status = I_RpcAsyncSetHandle(pRpcMsg, pAsync);
1717             if (status != RPC_S_OK)
1718                 RpcRaiseException(status);
1719             break;
1720         case PROXY_SENDRECEIVE:
1721             pRpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1722             /* send the [in] params only */
1723             if (Oif_flags.HasPipes)
1724                 /* NdrPipesSend(...) */
1725                 FIXME("pipes not supported yet\n");
1726             else
1727             {
1728                 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
1729 #if 0
1730                     NdrNsSend(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
1731 #else
1732                     FIXME("using auto handle - call NdrNsSend when it gets implemented\n");
1733 #endif
1734                 else
1735                 {
1736                     pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
1737                     status = I_RpcSend(pStubMsg->RpcMsg);
1738                     if (status != RPC_S_OK)
1739                         RpcRaiseException(status);
1740                 }
1741             }
1742
1743             break;
1744         case PROXY_CALCSIZE:
1745         case PROXY_MARSHAL:
1746             if (bV2Format)
1747                 client_do_args(pStubMsg, pFormat, phase, pStubMsg->StackTop,
1748                     async_call_data->number_of_params, NULL);
1749             else
1750                 client_do_args_old_format(pStubMsg, pFormat, phase,
1751                     pStubMsg->StackTop, async_call_data->stack_size, NULL,
1752                     (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT), FALSE);
1753             break;
1754         default:
1755             ERR("shouldn't reach here. phase %d\n", phase);
1756             break;
1757         }
1758     }
1759
1760     TRACE("returning 0\n");
1761     return 0;
1762 }
1763
1764 RPC_STATUS NdrpCompleteAsyncClientCall(RPC_ASYNC_STATE *pAsync, void *Reply)
1765 {
1766     /* pointer to start of stack where arguments start */
1767     PMIDL_STUB_MESSAGE pStubMsg;
1768     struct async_call_data *async_call_data;
1769     /* the type of pass we are currently doing */
1770     int phase;
1771     /* header for procedure string */
1772     const NDR_PROC_HEADER * pProcHeader;
1773     /* -Oif or -Oicf generated format */
1774     BOOL bV2Format;
1775     RPC_STATUS status = RPC_S_OK;
1776
1777     if (!pAsync->StubInfo)
1778         return RPC_S_INVALID_ASYNC_HANDLE;
1779
1780     async_call_data = pAsync->StubInfo;
1781     pStubMsg = async_call_data->pStubMsg;
1782     pProcHeader = async_call_data->pProcHeader;
1783
1784     bV2Format = (pStubMsg->StubDesc->Version >= 0x20000);
1785
1786     /* order of phases:
1787      * 1. PROXY_CALCSIZE - calculate the buffer size
1788      * 2. PROXY_GETBUFFER - allocate the buffer
1789      * 3. PROXY_MARHSAL - marshal [in] params into the buffer
1790      * 4. PROXY_SENDRECEIVE - send buffer
1791      * Then in NdrpCompleteAsyncClientCall:
1792      * 1. PROXY_SENDRECEIVE - receive buffer
1793      * 2. PROXY_UNMARHSAL - unmarshal [out] params from buffer
1794      */
1795     for (phase = PROXY_SENDRECEIVE; phase <= PROXY_UNMARSHAL; phase++)
1796     {
1797         switch (phase)
1798         {
1799         case PROXY_SENDRECEIVE:
1800             pStubMsg->RpcMsg->RpcFlags |= RPC_BUFFER_ASYNC;
1801             /* receive the [out] params */
1802             if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
1803 #if 0
1804                 NdrNsReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
1805 #else
1806                 FIXME("using auto handle - call NdrNsReceive when it gets implemented\n");
1807 #endif
1808             else
1809             {
1810                 status = I_RpcReceive(pStubMsg->RpcMsg);
1811                 if (status != RPC_S_OK)
1812                     goto cleanup;
1813                 pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
1814                 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
1815                 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
1816                 pStubMsg->Buffer = pStubMsg->BufferStart;
1817             }
1818
1819             /* convert strings, floating point values and endianess into our
1820              * preferred format */
1821 #if 0
1822             if ((pStubMsg->RpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1823                 NdrConvert(pStubMsg, pFormat);
1824 #endif
1825
1826             break;
1827         case PROXY_UNMARSHAL:
1828             if (bV2Format)
1829                 client_do_args(pStubMsg, async_call_data->pParamFormat, phase, pStubMsg->StackTop,
1830                     async_call_data->number_of_params, Reply);
1831             else
1832                 client_do_args_old_format(pStubMsg, async_call_data->pParamFormat, phase,
1833                     pStubMsg->StackTop, async_call_data->stack_size, Reply, FALSE, FALSE);
1834             break;
1835         default:
1836             ERR("shouldn't reach here. phase %d\n", phase);
1837             break;
1838         }
1839     }
1840
1841 cleanup:
1842     if (pStubMsg->fHasNewCorrDesc)
1843     {
1844         /* free extra correlation package */
1845         NdrCorrelationFree(pStubMsg);
1846     }
1847
1848     /* free the full pointer translation tables */
1849     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1850         NdrFullPointerXlatFree(pStubMsg->FullPtrXlatTables);
1851
1852     /* free marshalling buffer */
1853     NdrFreeBuffer(pStubMsg);
1854     client_free_handle(pStubMsg, pProcHeader, async_call_data->pHandleFormat, async_call_data->hBinding);
1855
1856     I_RpcFree(pStubMsg->StackTop);
1857     I_RpcFree(async_call_data);
1858
1859     TRACE("-- 0x%x\n", status);
1860     return status;
1861 }
1862
1863 RPCRTAPI LONG RPC_ENTRY NdrAsyncStubCall(struct IRpcStubBuffer* pThis,
1864     struct IRpcChannelBuffer* pChannel, PRPC_MESSAGE pRpcMsg,
1865     DWORD * pdwStubPhase)
1866 {
1867     FIXME("unimplemented, expect crash!\n");
1868     return 0;
1869 }