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