rpcrt4: Fix the stack offset for the old format stubless code when an object's method...
[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 #include "winreg.h"
37
38 #include "objbase.h"
39 #include "rpc.h"
40 #include "rpcproxy.h"
41 #include "ndrtypes.h"
42
43 #include "wine/debug.h"
44 #include "wine/rpcfc.h"
45
46 #include "ndr_misc.h"
47 #include "cpsf.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 static inline unsigned long call_memory_sizer(PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat)
95 {
96     NDR_MEMORYSIZE m = NdrMemorySizer[pFormat[0] & NDR_TABLE_MASK];
97     if (m) return m(pStubMsg, pFormat);
98     else
99     {
100         FIXME("format type 0x%x not implemented\n", pFormat[0]);
101         RpcRaiseException(RPC_X_BAD_STUB_DATA);
102         return 0;
103     }
104 }
105
106 /* there can't be any alignment with the structures in this file */
107 #include "pshpack1.h"
108
109 #define STUBLESS_UNMARSHAL  1
110 #define STUBLESS_CALLSERVER 2
111 #define STUBLESS_CALCSIZE   3
112 #define STUBLESS_GETBUFFER  4
113 #define STUBLESS_MARSHAL    5
114
115 /* From http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rpc/rpc/parameter_descriptors.asp */
116 typedef struct _NDR_PROC_HEADER
117 {
118     /* type of handle to use:
119      * RPC_FC_BIND_EXPLICIT = 0 - Explicit handle.
120      *   Handle is passed as a parameter to the function.
121      *   Indicates that explicit handle information follows the header,
122      *   which actually describes the handle.
123      * RPC_FC_BIND_GENERIC = 31 - Implicit handle with custom binding routines
124      *   (MIDL_STUB_DESC::IMPLICIT_HANDLE_INFO::pGenericBindingInfo)
125      * RPC_FC_BIND_PRIMITIVE = 32 - Implicit handle using handle_t created by
126      *   calling application
127      * RPC_FC_AUTO_HANDLE = 33 - Automatic handle
128      * RPC_FC_CALLBACK_HANDLE = 34 - undocmented
129      */
130     unsigned char handle_type;
131
132     /* procedure flags:
133      * Oi_FULL_PTR_USED = 0x01 - A full pointer can have the value NULL and can
134      *   change during the call from NULL to non-NULL and supports aliasing
135      *   and cycles. Indicates that the NdrFullPointerXlatInit function
136      *   should be called.
137      * Oi_RPCSS_ALLOC_USED = 0x02 - Use RpcSS allocate/free routines instead of
138      *   normal allocate/free routines
139      * Oi_OBJECT_PROC = 0x04 - Indicates a procedure that is part of an OLE
140      *   interface, rather than a DCE RPC interface.
141      * Oi_HAS_RPCFLAGS = 0x08 - Indicates that the rpc_flags element is 
142      *   present in the header.
143      * Oi_HAS_COMM_OR_FAULT = 0x20 - If Oi_OBJECT_PROC not present only then
144      *   indicates that the procedure has the comm_status or fault_status
145      *   MIDL attribute.
146      * Oi_OBJ_USE_V2_INTERPRETER = 0x20 - If Oi_OBJECT_PROC present only
147      *   then indicates that the format string is in -Oif or -Oicf format
148      * Oi_USE_NEW_INIT_ROUTINES = 0x40 - Use NdrXInitializeNew instead of
149      *   NdrXInitialize?
150      */
151     unsigned char Oi_flags;
152
153     /* the zero-based index of the procedure */
154     unsigned short proc_num;
155
156     /* total size of all parameters on the stack, including any "this"
157      * pointer and/or return value */
158     unsigned short stack_size;
159 } NDR_PROC_HEADER;
160
161 /* same as above struct except additional element rpc_flags */
162 typedef struct _NDR_PROC_HEADER_RPC
163 {
164     unsigned char handle_type;
165     unsigned char Oi_flags;
166
167     /*
168      * RPCF_Idempotent = 0x0001 - [idempotent] MIDL attribute
169      * RPCF_Broadcast = 0x0002 - [broadcast] MIDL attribute
170      * RPCF_Maybe = 0x0004 - [maybe] MIDL attribute
171      * Reserved = 0x0008 - 0x0080
172      * RPCF_Message = 0x0100 - [message] MIDL attribute
173      * Reserved = 0x0200 - 0x1000
174      * RPCF_InputSynchronous = 0x2000 - unknown
175      * RPCF_Asynchronous = 0x4000 - [async] MIDL attribute
176      * Reserved = 0x8000
177      */
178     unsigned long rpc_flags;
179     unsigned short proc_num;
180     unsigned short stack_size;
181
182 } NDR_PROC_HEADER_RPC;
183
184 typedef struct _NDR_PROC_PARTIAL_OIF_HEADER
185 {
186     /* the pre-computed client buffer size so that interpreter can skip all
187      * or some (if the flag RPC_FC_PROC_OI2F_CLTMUSTSIZE is specified) of the
188      * sizing pass */
189     unsigned short constant_client_buffer_size;
190
191     /* the pre-computed server buffer size so that interpreter can skip all
192      * or some (if the flag RPC_FC_PROC_OI2F_SRVMUSTSIZE is specified) of the
193      * sizing pass */
194     unsigned short constant_server_buffer_size;
195
196     INTERPRETER_OPT_FLAGS Oi2Flags;
197
198     /* number of params */
199     unsigned char number_of_params;
200 } NDR_PROC_PARTIAL_OIF_HEADER;
201
202 typedef struct _NDR_PARAM_OI_BASETYPE
203 {
204     /* parameter direction. One of:
205      * FC_IN_PARAM_BASETYPE = 0x4e - an in param
206      * FC_RETURN_PARAM_BASETYPE = 0x53 - a return param
207      */
208     unsigned char param_direction;
209
210     /* One of: FC_BYTE,FC_CHAR,FC_SMALL,FC_USMALL,FC_WCHAR,FC_SHORT,FC_USHORT,
211      * FC_LONG,FC_ULONG,FC_FLOAT,FC_HYPER,FC_DOUBLE,FC_ENUM16,FC_ENUM32,
212      * FC_ERROR_STATUS_T,FC_INT3264,FC_UINT3264 */
213     unsigned char type_format_char;
214 } NDR_PARAM_OI_BASETYPE;
215
216 typedef struct _NDR_PARAM_OI_OTHER
217 {
218     /* One of:
219      * FC_IN_PARAM = 0x4d - An in param
220      * FC_IN_OUT_PARAM = 0x50 - An in/out param
221      * FC_OUT_PARAM = 0x51 - An out param
222      * FC_RETURN_PARAM = 0x52 - A return value
223      * FC_IN_PARAM_NO_FREE_INST = 0x4f - A param for which no freeing is done
224      */
225     unsigned char param_direction;
226
227     /* Size of param on stack in NUMBERS OF INTS */
228     unsigned char stack_size;
229
230     /* offset in the type format string table */
231     unsigned short type_offset;
232 } NDR_PARAM_OI_OTHER;
233
234 typedef struct _NDR_PARAM_OIF_BASETYPE
235 {
236     PARAM_ATTRIBUTES param_attributes;
237
238     /* the offset on the calling stack where the parameter is located */
239     unsigned short stack_offset;
240
241     /* see NDR_PARAM_OI_BASETYPE::type_format_char */
242     unsigned char type_format_char;
243
244     /* always FC_PAD */
245     unsigned char unused;
246 } NDR_PARAM_OIF_BASETYPE;
247
248 typedef struct _NDR_PARAM_OIF_OTHER
249 {
250     PARAM_ATTRIBUTES param_attributes;
251
252     /* see NDR_PARAM_OIF_BASETYPE::stack_offset */
253     unsigned short stack_offset;
254
255     /* offset into the provided type format string where the type for this
256      * parameter starts */
257     unsigned short type_offset;
258 } NDR_PARAM_OIF_OTHER;
259
260 /* explicit handle description for FC_BIND_PRIMITIVE type */
261 typedef struct _NDR_EHD_PRIMITIVE
262 {
263     /* FC_BIND_PRIMITIVE */
264     unsigned char handle_type;
265
266     /* is the handle passed in via a pointer? */
267     unsigned char flag;
268
269     /* offset from the beginning of the stack to the handle in bytes */
270     unsigned short offset;
271 } NDR_EHD_PRIMITIVE;
272
273 /* explicit handle description for FC_BIND_GENERIC type */
274 typedef struct _NDR_EHD_GENERIC
275 {
276     /* FC_BIND_GENERIC */
277     unsigned char handle_type;
278
279     /* upper 4bits is a flag indicating whether the handle is passed in
280      * via a pointer. lower 4bits is the size of the user defined generic
281      * handle type. the size must be less than or equal to the machine
282      * register size */
283     unsigned char flag_and_size;
284
285     /* offset from the beginning of the stack to the handle in bytes */
286     unsigned short offset;
287
288     /* the index into the aGenericBindingRoutinesPairs field of MIDL_STUB_DESC
289      * giving the bind and unbind routines for the handle */
290     unsigned char binding_routine_pair_index;
291
292     /* FC_PAD */
293     unsigned char unused;
294 } NDR_EHD_GENERIC;
295
296 /* explicit handle description for FC_BIND_CONTEXT type */
297 typedef struct _NDR_EHD_CONTEXT
298 {
299     /* FC_BIND_CONTEXT */
300     unsigned char handle_type;
301
302     /* Any of the following flags:
303      * NDR_CONTEXT_HANDLE_CANNOT_BE_NULL = 0x01
304      * NDR_CONTEXT_HANDLE_SERIALIZE = 0x02
305      * NDR_CONTEXT_HANDLE_NO_SERIALIZE = 0x04
306      * NDR_STRICT_CONTEXT_HANDLE = 0x08
307      * HANDLE_PARAM_IS_OUT = 0x20
308      * HANDLE_PARAM_IS_RETURN = 0x21
309      * HANDLE_PARAM_IS_IN = 0x40
310      * HANDLE_PARAM_IS_VIA_PTR = 0x80
311      */
312     unsigned char flags;
313
314     /* offset from the beginning of the stack to the handle in bytes */
315     unsigned short offset;
316
317     /* zero-based index on rundown routine in apfnNdrRundownRoutines field
318      * of MIDL_STUB_DESC */
319     unsigned char context_rundown_routine_index;
320
321     /* varies depending on NDR version used.
322      * V1: zero-based index into parameters 
323      * V2: zero-based index into handles that are parameters */
324     unsigned char param_num;
325 } NDR_EHD_CONTEXT;
326
327 #include "poppack.h"
328
329 void WINAPI NdrRpcSmSetClientToOsf(PMIDL_STUB_MESSAGE pMessage)
330 {
331 #if 0 /* these functions are not defined yet */
332     pMessage->pfnAllocate = NdrRpcSmClientAllocate;
333     pMessage->pfnFree = NdrRpcSmClientFree;
334 #endif
335 }
336
337 static void WINAPI dump_RPC_FC_PROC_PF(PARAM_ATTRIBUTES param_attributes)
338 {
339     if (param_attributes.MustSize) TRACE(" MustSize");
340     if (param_attributes.MustFree) TRACE(" MustFree");
341     if (param_attributes.IsPipe) TRACE(" IsPipe");
342     if (param_attributes.IsIn) TRACE(" IsIn");
343     if (param_attributes.IsOut) TRACE(" IsOut");
344     if (param_attributes.IsReturn) TRACE(" IsReturn");
345     if (param_attributes.IsBasetype) TRACE(" IsBasetype");
346     if (param_attributes.IsByValue) TRACE(" IsByValue");
347     if (param_attributes.IsSimpleRef) TRACE(" IsSimpleRef");
348     if (param_attributes.IsDontCallFreeInst) TRACE(" IsDontCallFreeInst");
349     if (param_attributes.SaveForAsyncFinish) TRACE(" SaveForAsyncFinish");
350     if (param_attributes.ServerAllocSize) TRACE(" ServerAllocSize = %d", param_attributes.ServerAllocSize * 8);
351 }
352
353 static void WINAPI dump_INTERPRETER_OPT_FLAGS(INTERPRETER_OPT_FLAGS Oi2Flags)
354 {
355     if (Oi2Flags.ServerMustSize) TRACE(" ServerMustSize");
356     if (Oi2Flags.ClientMustSize) TRACE(" ClientMustSize");
357     if (Oi2Flags.HasReturn) TRACE(" HasReturn");
358     if (Oi2Flags.HasPipes) TRACE(" HasPipes");
359     if (Oi2Flags.Unused) TRACE(" Unused");
360     if (Oi2Flags.HasAsyncUuid) TRACE(" HasAsyncUuid");
361     if (Oi2Flags.HasExtensions) TRACE(" HasExtensions");
362     if (Oi2Flags.HasAsyncHandle) TRACE(" HasAsyncHandle");
363     TRACE("\n");
364 }
365
366 #define ARG_FROM_OFFSET(stubMsg, offset) ((stubMsg).StackTop + (offset))
367
368 static PFORMAT_STRING client_get_handle(
369     PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
370     PFORMAT_STRING pFormat, handle_t *phBinding)
371 {
372     /* binding */
373     switch (pProcHeader->handle_type)
374     {
375     /* explicit binding: parse additional section */
376     case RPC_FC_BIND_EXPLICIT:
377         switch (*pFormat) /* handle_type */
378         {
379         case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
380             {
381                 NDR_EHD_PRIMITIVE * pDesc = (NDR_EHD_PRIMITIVE *)pFormat;
382
383                 TRACE("Explicit primitive handle @ %d\n", pDesc->offset);
384
385                 if (pDesc->flag) /* pointer to binding */
386                     *phBinding = **(handle_t **)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
387                 else
388                     *phBinding = *(handle_t *)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
389                 return pFormat + sizeof(NDR_EHD_PRIMITIVE);
390             }
391         case RPC_FC_BIND_GENERIC: /* explicit generic */
392             {
393                 NDR_EHD_GENERIC * pDesc = (NDR_EHD_GENERIC *)pFormat;
394                 void *pObject = NULL;
395                 void *pArg;
396                 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
397
398                 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
399
400                 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
401                     pArg = *(void **)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
402                 else
403                     pArg = (void *)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
404                 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
405                 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
406                 *phBinding = pGenPair->pfnBind(pObject);
407                 return pFormat + sizeof(NDR_EHD_GENERIC);
408             }
409         case RPC_FC_BIND_CONTEXT: /* explicit context */
410             {
411                 NDR_EHD_CONTEXT * pDesc = (NDR_EHD_CONTEXT *)pFormat;
412                 NDR_CCONTEXT context_handle;
413                 TRACE("Explicit bind context\n");
414                 if (pDesc->flags & HANDLE_PARAM_IS_VIA_PTR)
415                 {
416                     TRACE("\tHANDLE_PARAM_IS_VIA_PTR\n");
417                     context_handle = **(NDR_CCONTEXT **)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
418                 }
419                 else
420                     context_handle = *(NDR_CCONTEXT *)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
421                 if ((pDesc->flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL) &&
422                     !context_handle)
423                 {
424                     ERR("null context handle isn't allowed\n");
425                     RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);
426                     return NULL;
427                 }
428                 *phBinding = NDRCContextBinding(context_handle);
429                 /* FIXME: should we store this structure in stubMsg.pContext? */
430                 return pFormat + sizeof(NDR_EHD_CONTEXT);
431             }
432         default:
433             ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
434             RpcRaiseException(RPC_X_BAD_STUB_DATA);
435         }
436         break;
437     case RPC_FC_BIND_GENERIC: /* implicit generic */
438         FIXME("RPC_FC_BIND_GENERIC\n");
439         RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
440         break;
441     case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
442         TRACE("Implicit primitive handle\n");
443         *phBinding = *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pPrimitiveHandle;
444         break;
445     case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
446         FIXME("RPC_FC_CALLBACK_HANDLE\n");
447         break;
448     case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
449         /* strictly speaking, it isn't necessary to set hBinding here
450          * since it isn't actually used (hence the automatic in its name),
451          * but then why does MIDL generate a valid entry in the
452          * MIDL_STUB_DESC for it? */
453         TRACE("Implicit auto handle\n");
454         *phBinding = *pStubMsg->StubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle;
455         break;
456     default:
457         ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
458         RpcRaiseException(RPC_X_BAD_STUB_DATA);
459     }
460     return pFormat;
461 }
462
463 static void client_free_handle(
464     PMIDL_STUB_MESSAGE pStubMsg, const NDR_PROC_HEADER *pProcHeader,
465     PFORMAT_STRING pFormat, handle_t hBinding)
466 {
467     /* binding */
468     switch (pProcHeader->handle_type)
469     {
470     /* explicit binding: parse additional section */
471     case RPC_FC_BIND_EXPLICIT:
472         switch (*pFormat) /* handle_type */
473         {
474         case RPC_FC_BIND_GENERIC: /* explicit generic */
475             {
476                 NDR_EHD_GENERIC * pDesc = (NDR_EHD_GENERIC *)pFormat;
477                 void *pObject = NULL;
478                 void *pArg;
479                 const GENERIC_BINDING_ROUTINE_PAIR *pGenPair;
480
481                 TRACE("Explicit generic binding handle #%d\n", pDesc->binding_routine_pair_index);
482
483                 if (pDesc->flag_and_size & HANDLE_PARAM_IS_VIA_PTR)
484                     pArg = *(void **)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
485                 else
486                     pArg = (void *)ARG_FROM_OFFSET(*pStubMsg, pDesc->offset);
487                 memcpy(&pObject, pArg, pDesc->flag_and_size & 0xf);
488                 pGenPair = &pStubMsg->StubDesc->aGenericBindingRoutinePairs[pDesc->binding_routine_pair_index];
489                 pGenPair->pfnUnbind(pObject, hBinding);
490                 break;
491             }
492         case RPC_FC_BIND_CONTEXT: /* explicit context */
493         case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
494             break;
495         default:
496             ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
497             RpcRaiseException(RPC_X_BAD_STUB_DATA);
498         }
499         break;
500     case RPC_FC_BIND_GENERIC: /* implicit generic */
501         FIXME("RPC_FC_BIND_GENERIC\n");
502         RpcRaiseException(RPC_X_BAD_STUB_DATA); /* FIXME: remove when implemented */
503         break;
504     case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
505     case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
506     case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
507         break;
508     default:
509         ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
510         RpcRaiseException(RPC_X_BAD_STUB_DATA);
511     }
512 }
513
514 /* the return type should be CLIENT_CALL_RETURN, but this is incompatible
515  * with the way gcc returns structures. "void *" should be the largest type
516  * that MIDL should allow you to return anyway */
517 LONG_PTR WINAPIV NdrClientCall2(PMIDL_STUB_DESC pStubDesc, PFORMAT_STRING pFormat, ...)
518 {
519     /* pointer to start of stack where arguments start */
520     RPC_MESSAGE rpcMsg;
521     MIDL_STUB_MESSAGE stubMsg;
522     handle_t hBinding = NULL;
523     /* procedure number */
524     unsigned short procedure_number;
525     /* size of stack */
526     unsigned short stack_size;
527     /* number of parameters. optional for client to give it to us */
528     unsigned char number_of_params = ~0;
529     /* cache of Oif_flags from v2 procedure header */
530     INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
531     /* cache of extension flags from NDR_PROC_HEADER_EXTS */
532     INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
533     /* the type of pass we are currently doing */
534     int phase;
535     /* header for procedure string */
536     const NDR_PROC_HEADER * pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
537     /* -Oif or -Oicf generated format */
538     BOOL bV2Format = FALSE;
539     /* the value to return to the client from the remote procedure */
540     LONG_PTR RetVal = 0;
541     /* the pointer to the object when in OLE mode */
542     void * This = NULL;
543     PFORMAT_STRING pHandleFormat;
544
545     TRACE("pStubDesc %p, pFormat %p, ...\n", pStubDesc, pFormat);
546
547     /* Later NDR language versions probably won't be backwards compatible */
548     if (pStubDesc->Version > 0x50002)
549     {
550         FIXME("Incompatible stub description version: 0x%lx\n", pStubDesc->Version);
551         RpcRaiseException(RPC_X_WRONG_STUB_VERSION);
552     }
553
554     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
555     {
556         NDR_PROC_HEADER_RPC * pProcHeader = (NDR_PROC_HEADER_RPC *)&pFormat[0];
557         stack_size = pProcHeader->stack_size;
558         procedure_number = pProcHeader->proc_num;
559         pFormat += sizeof(NDR_PROC_HEADER_RPC);
560     }
561     else
562     {
563         stack_size = pProcHeader->stack_size;
564         procedure_number = pProcHeader->proc_num;
565         TRACE("proc num: %d\n", procedure_number);
566         pFormat += sizeof(NDR_PROC_HEADER);
567     }
568
569     /* create the full pointer translation tables, if requested */
570     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
571         stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_CLIENT);
572
573     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
574     {
575         /* object is always the first argument */
576         This = **(void ***)(&pFormat+1);
577         NdrProxyInitialize(This, &rpcMsg, &stubMsg, pStubDesc, procedure_number);
578     }
579     else
580         NdrClientInitializeNew(&rpcMsg, &stubMsg, pStubDesc, procedure_number);
581
582     TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
583     TRACE("MIDL stub version = 0x%lx\n", pStubDesc->MIDLVersion);
584
585     /* needed for conformance of top-level objects */
586 #ifdef __i386__
587     stubMsg.StackTop = *(unsigned char **)(&pFormat+1);
588 #else
589 # warning Stack not retrieved for your CPU architecture
590 #endif
591
592     pHandleFormat = pFormat;
593
594     /* we only need a handle if this isn't an object method */
595     if (!(pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT))
596     {
597         pFormat = client_get_handle(&stubMsg, pProcHeader, pHandleFormat, &hBinding);
598         if (!pFormat) return 0;
599     }
600
601     bV2Format = (pStubDesc->Version >= 0x20000);
602
603     if (bV2Format)
604     {
605         NDR_PROC_PARTIAL_OIF_HEADER * pOIFHeader =
606             (NDR_PROC_PARTIAL_OIF_HEADER*)pFormat;
607
608         Oif_flags = pOIFHeader->Oi2Flags;
609         number_of_params = pOIFHeader->number_of_params;
610
611         pFormat += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
612     }
613
614     TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
615
616     if (Oif_flags.HasExtensions)
617     {
618         NDR_PROC_HEADER_EXTS * pExtensions =
619             (NDR_PROC_HEADER_EXTS *)pFormat;
620         ext_flags = pExtensions->Flags2;
621         pFormat += pExtensions->Size;
622     }
623
624     stubMsg.BufferLength = 0;
625
626     /* store the RPC flags away */
627     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
628         rpcMsg.RpcFlags = ((NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
629
630     /* use alternate memory allocation routines */
631     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
632         NdrRpcSmSetClientToOsf(&stubMsg);
633
634     if (Oif_flags.HasPipes)
635     {
636         FIXME("pipes not supported yet\n");
637         RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
638         /* init pipes package */
639         /* NdrPipesInitialize(...) */
640     }
641     if (ext_flags.HasNewCorrDesc)
642     {
643         /* initialize extra correlation package */
644         FIXME("new correlation description not implemented\n");
645         stubMsg.fHasNewCorrDesc = TRUE;
646     }
647
648     /* order of phases:
649      * 1. PROXY_CALCSIZE - calculate the buffer size
650      * 2. PROXY_GETBUFFER - allocate the buffer
651      * 3. PROXY_MARHSAL - marshal [in] params into the buffer
652      * 4. PROXY_SENDRECEIVE - send/receive buffer
653      * 5. PROXY_UNMARHSAL - unmarshal [out] params from buffer
654      */
655     for (phase = PROXY_CALCSIZE; phase <= PROXY_UNMARSHAL; phase++)
656     {
657         /* current format string offset */
658         int current_offset;
659         /* current stack offset */
660         unsigned short current_stack_offset;
661         /* counter */
662         unsigned short i;
663
664         TRACE("phase = %d\n", phase);
665         switch (phase)
666         {
667         case PROXY_GETBUFFER:
668             /* allocate the buffer */
669             if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
670                 NdrProxyGetBuffer(This, &stubMsg);
671             else if (Oif_flags.HasPipes)
672                 /* NdrGetPipeBuffer(...) */
673                 FIXME("pipes not supported yet\n");
674             else
675             {
676                 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
677 #if 0
678                     NdrNsGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
679 #else
680                     FIXME("using auto handle - call NdrNsGetBuffer when it gets implemented\n");
681 #endif
682                 else
683                     NdrGetBuffer(&stubMsg, stubMsg.BufferLength, hBinding);
684             }
685             break;
686         case PROXY_SENDRECEIVE:
687             /* send the [in] params and receive the [out] and [retval]
688              * params */
689             if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
690                 NdrProxySendReceive(This, &stubMsg);
691             else if (Oif_flags.HasPipes)
692                 /* NdrPipesSendReceive(...) */
693                 FIXME("pipes not supported yet\n");
694             else
695             {
696                 if (pProcHeader->handle_type == RPC_FC_AUTO_HANDLE)
697 #if 0
698                     NdrNsSendReceive(&stubMsg, stubMsg.Buffer, pStubDesc->IMPLICIT_HANDLE_INFO.pAutoHandle);
699 #else
700                     FIXME("using auto handle - call NdrNsSendReceive when it gets implemented\n");
701 #endif
702                 else
703                     NdrSendReceive(&stubMsg, stubMsg.Buffer);
704             }
705
706             /* convert strings, floating point values and endianess into our
707              * preferred format */
708             if ((rpcMsg.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
709                 NdrConvert(&stubMsg, pFormat);
710
711             break;
712         case PROXY_CALCSIZE:
713         case PROXY_MARSHAL:
714         case PROXY_UNMARSHAL:
715             current_offset = 0;
716             current_stack_offset = 0;
717
718             /* NOTE: V1 style format does't terminate on the number_of_params
719              * condition as it doesn't have this attribute. Instead it
720              * terminates when the stack size given in the header is exceeded.
721              */
722             for (i = 0; i < number_of_params; i++)
723             {
724                 if (bV2Format) /* new parameter format */
725                 {
726                     NDR_PARAM_OIF_BASETYPE * pParam =
727                         (NDR_PARAM_OIF_BASETYPE *)&pFormat[current_offset];
728                     unsigned char * pArg;
729
730                     current_stack_offset = pParam->stack_offset;
731                     pArg = ARG_FROM_OFFSET(stubMsg, current_stack_offset);
732
733                     TRACE("param[%d]: new format\n", i);
734                     TRACE("\tparam_attributes:"); dump_RPC_FC_PROC_PF(pParam->param_attributes); TRACE("\n");
735                     TRACE("\tstack_offset: 0x%x\n", current_stack_offset);
736                     TRACE("\tmemory addr (before): %p\n", pArg);
737
738                     if (pParam->param_attributes.IsBasetype)
739                     {
740                         const unsigned char * pTypeFormat =
741                             &pParam->type_format_char;
742
743                         if (pParam->param_attributes.IsSimpleRef)
744                             pArg = *(unsigned char **)pArg;
745
746                         TRACE("\tbase type: 0x%02x\n", *pTypeFormat);
747
748                         switch (phase)
749                         {
750                         case PROXY_CALCSIZE:
751                             if (pParam->param_attributes.IsIn)
752                                 call_buffer_sizer(&stubMsg, pArg, pTypeFormat);
753                             break;
754                         case PROXY_MARSHAL:
755                             if (pParam->param_attributes.IsIn)
756                                 call_marshaller(&stubMsg, pArg, pTypeFormat);
757                             break;
758                         case PROXY_UNMARSHAL:
759                             if (pParam->param_attributes.IsOut)
760                             {
761                                 unsigned char *pRetVal = (unsigned char *)&RetVal;
762                                 if (pParam->param_attributes.IsReturn)
763                                     call_unmarshaller(&stubMsg, &pRetVal, pTypeFormat, 0);
764                                 else
765                                     call_unmarshaller(&stubMsg, &pArg, pTypeFormat, 0);
766                                 TRACE("pRetVal = %p\n", pRetVal);
767                             }
768                             break;
769                         default:
770                             RpcRaiseException(RPC_S_INTERNAL_ERROR);
771                         }
772
773                         current_offset += sizeof(NDR_PARAM_OIF_BASETYPE);
774                     }
775                     else
776                     {
777                         NDR_PARAM_OIF_OTHER * pParamOther =
778                             (NDR_PARAM_OIF_OTHER *)&pFormat[current_offset];
779
780                         const unsigned char * pTypeFormat =
781                             &(pStubDesc->pFormatTypes[pParamOther->type_offset]);
782
783                         /* if a simple ref pointer then we have to do the
784                          * check for the pointer being non-NULL. */
785                         if (pParam->param_attributes.IsSimpleRef)
786                         {
787                             if (!*(unsigned char **)pArg)
788                                 RpcRaiseException(RPC_X_NULL_REF_POINTER);
789                         }
790
791                         TRACE("\tcomplex type: 0x%02x\n", *pTypeFormat);
792
793                         switch (phase)
794                         {
795                         case PROXY_CALCSIZE:
796                             if (pParam->param_attributes.IsIn)
797                             {
798                                 if (pParam->param_attributes.IsByValue)
799                                     call_buffer_sizer(&stubMsg, pArg, pTypeFormat);
800                                 else
801                                     call_buffer_sizer(&stubMsg, *(unsigned char **)pArg, pTypeFormat);
802                             }
803                             break;
804                         case PROXY_MARSHAL:
805                             if (pParam->param_attributes.IsIn)
806                             {
807                                 if (pParam->param_attributes.IsByValue)
808                                     call_marshaller(&stubMsg, pArg, pTypeFormat);
809                                 else
810                                     call_marshaller(&stubMsg, *(unsigned char **)pArg, pTypeFormat);
811                             }
812                             break;
813                         case PROXY_UNMARSHAL:
814                             if (pParam->param_attributes.IsOut)
815                             {
816                                 unsigned char *pRetVal = (unsigned char *)&RetVal;
817                                 if (pParam->param_attributes.IsReturn)
818                                     call_unmarshaller(&stubMsg, &pRetVal, pTypeFormat, 0);
819                                 else if (pParam->param_attributes.IsByValue)
820                                     call_unmarshaller(&stubMsg, &pArg, pTypeFormat, 0);
821                                 else
822                                     call_unmarshaller(&stubMsg, (unsigned char **)pArg, pTypeFormat, 0);
823                             }
824                             break;
825                         default:
826                             RpcRaiseException(RPC_S_INTERNAL_ERROR);
827                         }
828
829                         current_offset += sizeof(NDR_PARAM_OIF_OTHER);
830                     }
831                     TRACE("\tmemory addr (after): %p\n", pArg);
832                 }
833                 else /* old parameter format */
834                 {
835                     NDR_PARAM_OI_BASETYPE * pParam =
836                         (NDR_PARAM_OI_BASETYPE *)&pFormat[current_offset];
837                     /* note: current_stack_offset starts after the This pointer
838                      * if present, so adjust this */
839                     unsigned short current_stack_offset_adjusted = current_stack_offset +
840                         ((pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT) ? sizeof(void *) : 0);
841                     unsigned char * pArg = ARG_FROM_OFFSET(stubMsg, current_stack_offset_adjusted);
842
843                     /* no more parameters; exit loop */
844                     if (current_stack_offset_adjusted >= stack_size)
845                         break;
846
847                     TRACE("param[%d]: old format\n", i);
848                     TRACE("\tparam_direction: %x\n", pParam->param_direction);
849                     TRACE("\tstack_offset: 0x%x\n", current_stack_offset_adjusted);
850                     TRACE("\tmemory addr (before): %p\n", pArg);
851
852                     if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE ||
853                         pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
854                     {
855                         const unsigned char * pTypeFormat =
856                             &pParam->type_format_char;
857
858                         TRACE("\tbase type 0x%02x\n", *pTypeFormat);
859
860                         switch (phase)
861                         {
862                         case PROXY_CALCSIZE:
863                             if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
864                                 call_buffer_sizer(&stubMsg, pArg, pTypeFormat);
865                             break;
866                         case PROXY_MARSHAL:
867                             if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
868                                 call_marshaller(&stubMsg, pArg, pTypeFormat);
869                             break;
870                         case PROXY_UNMARSHAL:
871                             if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
872                             {
873                                 if (pParam->param_direction & RPC_FC_RETURN_PARAM)
874                                     call_unmarshaller(&stubMsg, (unsigned char **)&RetVal, pTypeFormat, 0);
875                                 else
876                                     call_unmarshaller(&stubMsg, &pArg, pTypeFormat, 0);
877                             }
878                             break;
879                         default:
880                             RpcRaiseException(RPC_S_INTERNAL_ERROR);
881                         }
882
883                         current_stack_offset += call_memory_sizer(&stubMsg, pTypeFormat);
884                         current_offset += sizeof(NDR_PARAM_OI_BASETYPE);
885                     }
886                     else
887                     {
888                         NDR_PARAM_OI_OTHER * pParamOther = 
889                             (NDR_PARAM_OI_OTHER *)&pFormat[current_offset];
890
891                         const unsigned char *pTypeFormat =
892                             &pStubDesc->pFormatTypes[pParamOther->type_offset];
893
894                         TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
895
896                         switch (phase)
897                         {
898                         case PROXY_CALCSIZE:
899                             if (pParam->param_direction == RPC_FC_IN_PARAM ||
900                                 pParam->param_direction & RPC_FC_IN_OUT_PARAM)
901                                 call_buffer_sizer(&stubMsg, pArg, pTypeFormat);
902                             break;
903                         case PROXY_MARSHAL:
904                             if (pParam->param_direction == RPC_FC_IN_PARAM ||
905                                 pParam->param_direction & RPC_FC_IN_OUT_PARAM)
906                                 call_marshaller(&stubMsg, pArg, pTypeFormat);
907                             break;
908                         case PROXY_UNMARSHAL:
909                             if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
910                                 pParam->param_direction == RPC_FC_OUT_PARAM)
911                                  call_unmarshaller(&stubMsg, &pArg, pTypeFormat, 0);
912                             else if (pParam->param_direction == RPC_FC_RETURN_PARAM)
913                                 call_unmarshaller(&stubMsg, (unsigned char **)&RetVal, pTypeFormat, 0);
914                             break;
915                         default:
916                             RpcRaiseException(RPC_S_INTERNAL_ERROR);
917                         }
918
919                         current_stack_offset += pParamOther->stack_size * sizeof(INT);
920                         current_offset += sizeof(NDR_PARAM_OI_OTHER);
921                     }
922                     TRACE("\tmemory addr (after): %p\n", pArg);
923                 }
924             }
925
926             break;
927         default:
928             ERR("shouldn't reach here. phase %d\n", phase);
929             break;
930         }
931     }
932
933     if (ext_flags.HasNewCorrDesc)
934     {
935         /* free extra correlation package */
936         /* NdrCorrelationFree(&stubMsg); */
937     }
938
939     if (Oif_flags.HasPipes)
940     {
941         /* NdrPipesDone(...) */
942     }
943
944     /* free the full pointer translation tables */
945     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
946         NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
947
948     /* free marshalling buffer */
949     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
950         NdrProxyFreeBuffer(This, &stubMsg);
951     else
952     {
953         NdrFreeBuffer(&stubMsg);
954         client_free_handle(&stubMsg, pProcHeader, pHandleFormat, hBinding);
955     }
956
957     TRACE("RetVal = 0x%lx\n", RetVal);
958
959     return RetVal;
960 }
961
962 /* calls a function with the specificed arguments, restoring the stack
963  * properly afterwards as we don't know the calling convention of the
964  * function */
965 #if defined __i386__ && defined _MSC_VER
966 __declspec(naked) LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size)
967 {
968     __asm
969     {
970         push ebp
971         push edi            ; Save registers
972         push esi
973         mov ebp, esp
974         mov eax, [ebp+16]   ; Get stack size
975         sub esp, eax        ; Make room in stack for arguments
976         mov edi, esp
977         mov ecx, eax
978         mov esi, [ebp+12]
979         shr ecx, 2
980         cld
981         rep movsd           ; Copy dword blocks
982         call [ebp+8]        ; Call function
983         lea esp, [ebp-8]    ; Restore stack
984         pop esi             ; Restore registers
985         pop edi
986         pop ebp
987         ret
988     }
989 }
990 #elif defined __i386__ && defined __GNUC__
991 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned int stack_size);
992 __ASM_GLOBAL_FUNC(call_server_func,
993     "pushl %ebp\n\t"
994     "movl %esp, %ebp\n\t"
995     "pushl %edi\n\t"            /* Save registers */
996     "pushl %esi\n\t"
997     "movl 16(%ebp), %eax\n\t"   /* Get stack size */
998     "subl %eax, %esp\n\t"       /* Make room in stack for arguments */
999     "andl $~15, %esp\n\t"       /* Make sure stack has 16-byte alignment for MacOS X */
1000     "movl %esp, %edi\n\t"
1001     "movl %eax, %ecx\n\t"
1002     "movl 12(%ebp), %esi\n\t"
1003     "shrl $2, %ecx\n\t"         /* divide by 4 */
1004     "cld\n\t"
1005     "rep; movsl\n\t"            /* Copy dword blocks */
1006     "call *8(%ebp)\n\t"         /* Call function */
1007     "leal -8(%ebp), %esp\n\t"   /* Restore stack */
1008     "popl %esi\n\t"             /* Restore registers */
1009     "popl %edi\n\t"
1010     "popl %ebp\n\t"
1011     "ret\n" );
1012 #else
1013 #warning call_server_func not implemented for your architecture
1014 LONG_PTR __cdecl call_server_func(SERVER_ROUTINE func, unsigned char * args, unsigned short stack_size)
1015 {
1016     FIXME("Not implemented for your architecture\n");
1017     return 0;
1018 }
1019 #endif
1020
1021 static DWORD calc_arg_size(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat)
1022 {
1023     DWORD size;
1024     switch(*pFormat)
1025     {
1026     case RPC_FC_STRUCT:
1027         size = *(const WORD*)(pFormat + 2);
1028         break;
1029     case RPC_FC_CARRAY:
1030         size = *(const WORD*)(pFormat + 2);
1031         ComputeConformance(pStubMsg, NULL, pFormat + 4, 0);
1032         size *= pStubMsg->MaxCount;
1033         break;
1034     default:
1035         FIXME("Unhandled type %02x\n", *pFormat);
1036         /* fallthrough */
1037     case RPC_FC_RP:
1038         size = sizeof(void *);
1039         break;
1040     }
1041     return size;
1042 }
1043
1044 /* FIXME: need to free some stuff in here too */
1045 long WINAPI NdrStubCall2(
1046     struct IRpcStubBuffer * pThis,
1047     struct IRpcChannelBuffer * pChannel,
1048     PRPC_MESSAGE pRpcMsg,
1049     unsigned long * pdwStubPhase)
1050 {
1051     const MIDL_SERVER_INFO *pServerInfo;
1052     const MIDL_STUB_DESC *pStubDesc;
1053     PFORMAT_STRING pFormat;
1054     MIDL_STUB_MESSAGE stubMsg;
1055     /* pointer to start of stack to pass into stub implementation */
1056     unsigned char * args;
1057     /* size of stack */
1058     unsigned short stack_size;
1059     /* current stack offset */
1060     unsigned short current_stack_offset;
1061     /* number of parameters. optional for client to give it to us */
1062     unsigned char number_of_params = ~0;
1063     /* counter */
1064     unsigned short i;
1065     /* cache of Oif_flags from v2 procedure header */
1066     INTERPRETER_OPT_FLAGS Oif_flags = { 0 };
1067     /* cache of extension flags from NDR_PROC_HEADER_EXTS */
1068     INTERPRETER_OPT_FLAGS2 ext_flags = { 0 };
1069     /* the type of pass we are currently doing */
1070     int phase;
1071     /* header for procedure string */
1072     const NDR_PROC_HEADER *pProcHeader;
1073     /* offset in format string for start of params */
1074     int parameter_start_offset;
1075     /* current format string offset */
1076     int current_offset;
1077     /* -Oif or -Oicf generated format */
1078     BOOL bV2Format = FALSE;
1079     /* location to put retval into */
1080     LONG_PTR *retval_ptr = NULL;
1081
1082     TRACE("pThis %p, pChannel %p, pRpcMsg %p, pdwStubPhase %p\n", pThis, pChannel, pRpcMsg, pdwStubPhase);
1083
1084     if (pThis)
1085         pServerInfo = CStdStubBuffer_GetServerInfo(pThis);
1086     else
1087         pServerInfo = ((RPC_SERVER_INTERFACE *)pRpcMsg->RpcInterfaceInformation)->InterpreterInfo;
1088
1089     pStubDesc = pServerInfo->pStubDesc;
1090     pFormat = pServerInfo->ProcString + pServerInfo->FmtStringOffset[pRpcMsg->ProcNum];
1091     pProcHeader = (const NDR_PROC_HEADER *)&pFormat[0];
1092
1093     /* Later NDR language versions probably won't be backwards compatible */
1094     if (pStubDesc->Version > 0x50002)
1095     {
1096         FIXME("Incompatible stub description version: 0x%lx\n", pStubDesc->Version);
1097         RpcRaiseException(RPC_X_WRONG_STUB_VERSION);
1098     }
1099
1100     /* create the full pointer translation tables, if requested */
1101     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1102         stubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,XLAT_SERVER);
1103
1104     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1105     {
1106         NDR_PROC_HEADER_RPC * pProcHeader = (NDR_PROC_HEADER_RPC *)&pFormat[0];
1107         stack_size = pProcHeader->stack_size;
1108         current_offset = sizeof(NDR_PROC_HEADER_RPC);
1109
1110     }
1111     else
1112     {
1113         stack_size = pProcHeader->stack_size;
1114         current_offset = sizeof(NDR_PROC_HEADER);
1115     }
1116
1117     TRACE("Oi_flags = 0x%02x\n", pProcHeader->Oi_flags);
1118
1119     /* binding */
1120     switch (pProcHeader->handle_type)
1121     {
1122     /* explicit binding: parse additional section */
1123     case RPC_FC_BIND_EXPLICIT:
1124         switch (pFormat[current_offset]) /* handle_type */
1125         {
1126         case RPC_FC_BIND_PRIMITIVE: /* explicit primitive */
1127             current_offset += sizeof(NDR_EHD_PRIMITIVE);
1128             break;
1129         case RPC_FC_BIND_GENERIC: /* explicit generic */
1130             current_offset += sizeof(NDR_EHD_GENERIC);
1131             break;
1132         case RPC_FC_BIND_CONTEXT: /* explicit context */
1133             current_offset += sizeof(NDR_EHD_CONTEXT);
1134             break;
1135         default:
1136             ERR("bad explicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1137             RpcRaiseException(RPC_X_BAD_STUB_DATA);
1138         }
1139         break;
1140     case RPC_FC_BIND_GENERIC: /* implicit generic */
1141     case RPC_FC_BIND_PRIMITIVE: /* implicit primitive */
1142     case RPC_FC_CALLBACK_HANDLE: /* implicit callback */
1143     case RPC_FC_AUTO_HANDLE: /* implicit auto handle */
1144         break;
1145     default:
1146         ERR("bad implicit binding handle type (0x%02x)\n", pProcHeader->handle_type);
1147         RpcRaiseException(RPC_X_BAD_STUB_DATA);
1148     }
1149
1150     bV2Format = (pStubDesc->Version >= 0x20000);
1151
1152     if (bV2Format)
1153     {
1154         NDR_PROC_PARTIAL_OIF_HEADER * pOIFHeader =
1155             (NDR_PROC_PARTIAL_OIF_HEADER*)&pFormat[current_offset];
1156
1157         Oif_flags = pOIFHeader->Oi2Flags;
1158         number_of_params = pOIFHeader->number_of_params;
1159
1160         current_offset += sizeof(NDR_PROC_PARTIAL_OIF_HEADER);
1161     }
1162
1163     TRACE("Oif_flags = "); dump_INTERPRETER_OPT_FLAGS(Oif_flags);
1164
1165     if (Oif_flags.HasExtensions)
1166     {
1167         NDR_PROC_HEADER_EXTS * pExtensions =
1168             (NDR_PROC_HEADER_EXTS *)&pFormat[current_offset];
1169         ext_flags = pExtensions->Flags2;
1170         current_offset += pExtensions->Size;
1171     }
1172
1173     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1174         NdrStubInitialize(pRpcMsg, &stubMsg, pStubDesc, pChannel);
1175     else
1176         NdrServerInitializeNew(pRpcMsg, &stubMsg, pStubDesc);
1177
1178     /* store the RPC flags away */
1179     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCFLAGS)
1180         pRpcMsg->RpcFlags = ((NDR_PROC_HEADER_RPC *)pProcHeader)->rpc_flags;
1181
1182     /* use alternate memory allocation routines */
1183     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_RPCSSALLOC)
1184 #if 0
1185           NdrRpcSsEnableAllocate(&stubMsg);
1186 #else
1187           FIXME("Set RPCSS memory allocation routines\n");
1188 #endif
1189
1190     if (Oif_flags.HasPipes)
1191     {
1192         FIXME("pipes not supported yet\n");
1193         RpcRaiseException(RPC_X_WRONG_STUB_VERSION); /* FIXME: remove when implemented */
1194         /* init pipes package */
1195         /* NdrPipesInitialize(...) */
1196     }
1197     if (ext_flags.HasNewCorrDesc)
1198     {
1199         /* initialize extra correlation package */
1200         FIXME("new correlation description not implemented\n");
1201         stubMsg.fHasNewCorrDesc = TRUE;
1202     }
1203
1204     /* convert strings, floating point values and endianess into our
1205      * preferred format */
1206     if ((pRpcMsg->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)
1207         NdrConvert(&stubMsg, pFormat);
1208
1209     parameter_start_offset = current_offset;
1210
1211     TRACE("allocating memory for stack of size %x\n", stack_size);
1212
1213     args = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, stack_size);
1214     stubMsg.StackTop = args; /* used by conformance of top-level objects */
1215
1216     /* add the implicit This pointer as the first arg to the function if we
1217      * are calling an object method */
1218     if (pThis)
1219         *(void **)args = ((CStdStubBuffer *)pThis)->pvServerObject;
1220
1221     /* order of phases:
1222      * 1. STUBLESS_UNMARHSAL - unmarshal [in] params from buffer
1223      * 2. STUBLESS_CALLSERVER - send/receive buffer
1224      * 3. STUBLESS_CALCSIZE - get [out] buffer size
1225      * 4. STUBLESS_GETBUFFER - allocate [out] buffer
1226      * 5. STUBLESS_MARHSAL - marshal [out] params to buffer
1227      */
1228     for (phase = STUBLESS_UNMARSHAL; phase <= STUBLESS_MARSHAL; phase++)
1229     {
1230         TRACE("phase = %d\n", phase);
1231         switch (phase)
1232         {
1233         case STUBLESS_CALLSERVER:
1234             /* call the server function */
1235             if (pServerInfo->ThunkTable && pServerInfo->ThunkTable[pRpcMsg->ProcNum])
1236                 pServerInfo->ThunkTable[pRpcMsg->ProcNum](&stubMsg);
1237             else
1238             {
1239                 SERVER_ROUTINE func;
1240                 LONG_PTR retval;
1241
1242                 if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1243                 {
1244                     SERVER_ROUTINE *vtbl = *(SERVER_ROUTINE **)((CStdStubBuffer *)pThis)->pvServerObject;
1245                     func = vtbl[pRpcMsg->ProcNum];
1246                 }
1247                 else
1248                     func = pServerInfo->DispatchTable[pRpcMsg->ProcNum];
1249
1250                 /* FIXME: what happens with return values that don't fit into a single register on x86? */
1251                 retval = call_server_func(func, args, stack_size);
1252
1253                 if (retval_ptr)
1254                 {
1255                     TRACE("stub implementation returned 0x%lx\n", retval);
1256                     *retval_ptr = retval;
1257                 }
1258                 else
1259                     TRACE("void stub implementation\n");
1260             }
1261
1262             stubMsg.Buffer = NULL;
1263             stubMsg.BufferLength = 0;
1264
1265             break;
1266         case STUBLESS_GETBUFFER:
1267             if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT)
1268                 NdrStubGetBuffer(pThis, pChannel, &stubMsg);
1269             else
1270             {
1271                 RPC_STATUS Status;
1272
1273                 pRpcMsg->BufferLength = stubMsg.BufferLength;
1274                 /* allocate buffer for [out] and [ret] params */
1275                 Status = I_RpcGetBuffer(pRpcMsg); 
1276                 if (Status)
1277                     RpcRaiseException(Status);
1278                 stubMsg.BufferStart = pRpcMsg->Buffer;
1279                 stubMsg.BufferEnd = stubMsg.BufferStart + stubMsg.BufferLength;
1280                 stubMsg.Buffer = stubMsg.BufferStart;
1281             }
1282             break;
1283         case STUBLESS_MARSHAL:
1284         case STUBLESS_UNMARSHAL:
1285         case STUBLESS_CALCSIZE:
1286             current_offset = parameter_start_offset;
1287             current_stack_offset = 0;
1288
1289             /* NOTE: V1 style format does't terminate on the number_of_params
1290              * condition as it doesn't have this attribute. Instead it
1291              * terminates when the stack size given in the header is exceeded.
1292              */
1293             for (i = 0; i < number_of_params; i++)
1294             {
1295                 if (bV2Format) /* new parameter format */
1296                 {
1297                     const NDR_PARAM_OIF_BASETYPE *pParam =
1298                         (NDR_PARAM_OIF_BASETYPE *)&pFormat[current_offset];
1299                     unsigned char *pArg;
1300
1301                     current_stack_offset = pParam->stack_offset;
1302                     pArg = (unsigned char *)(args+current_stack_offset);
1303
1304                     TRACE("param[%d]: new format\n", i);
1305                     TRACE("\tparam_attributes:"); dump_RPC_FC_PROC_PF(pParam->param_attributes); TRACE("\n");
1306                     TRACE("\tstack_offset: %x\n", current_stack_offset);
1307                     TRACE("\tmemory addr (before): %p -> %p\n", pArg, *(unsigned char **)pArg);
1308
1309                     if (pParam->param_attributes.ServerAllocSize)
1310                         FIXME("ServerAllocSize of %d ignored for parameter %d\n",
1311                             pParam->param_attributes.ServerAllocSize * 8, i);
1312
1313                     if (pParam->param_attributes.IsBasetype)
1314                     {
1315                         const unsigned char *pTypeFormat =
1316                             &pParam->type_format_char;
1317
1318                         TRACE("\tbase type: 0x%02x\n", *pTypeFormat);
1319
1320                         switch (phase)
1321                         {
1322                         case STUBLESS_MARSHAL:
1323                             if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1324                             {
1325                                 if (pParam->param_attributes.IsSimpleRef)
1326                                     call_marshaller(&stubMsg, *(unsigned char **)pArg, pTypeFormat);
1327                                 else
1328                                     call_marshaller(&stubMsg, pArg, pTypeFormat);
1329                             }
1330                             /* FIXME: call call_freer here */
1331                             break;
1332                         case STUBLESS_UNMARSHAL:
1333                             if (pParam->param_attributes.IsIn)
1334                             {
1335                                 if (pParam->param_attributes.IsSimpleRef)
1336                                     call_unmarshaller(&stubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1337                                 else
1338                                     call_unmarshaller(&stubMsg, &pArg, pTypeFormat, 0);
1339                             }
1340
1341                             /* make a note of the address of the return value parameter for later */
1342                             if (pParam->param_attributes.IsReturn)
1343                                 retval_ptr = (LONG_PTR *)pArg;
1344
1345                             break;
1346                         case STUBLESS_CALCSIZE:
1347                             if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1348                             {
1349                                 if (pParam->param_attributes.IsSimpleRef)
1350                                     call_buffer_sizer(&stubMsg, *(unsigned char **)pArg, pTypeFormat);
1351                                 else
1352                                     call_buffer_sizer(&stubMsg, pArg, pTypeFormat);
1353                             }
1354                             break;
1355                         default:
1356                             RpcRaiseException(RPC_S_INTERNAL_ERROR);
1357                         }
1358
1359                         current_offset += sizeof(NDR_PARAM_OIF_BASETYPE);
1360                     }
1361                     else
1362                     {
1363                         NDR_PARAM_OIF_OTHER * pParamOther =
1364                             (NDR_PARAM_OIF_OTHER *)&pFormat[current_offset];
1365
1366                         const unsigned char * pTypeFormat =
1367                             &(pStubDesc->pFormatTypes[pParamOther->type_offset]);
1368
1369                         TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1370
1371                         switch (phase)
1372                         {
1373                         case STUBLESS_MARSHAL:
1374                             if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1375                             {
1376                                 if (pParam->param_attributes.IsByValue)
1377                                     call_marshaller(&stubMsg, pArg, pTypeFormat);
1378                                 else
1379                                 {
1380                                     call_marshaller(&stubMsg, *(unsigned char **)pArg, pTypeFormat);
1381                                     stubMsg.pfnFree(*(void **)pArg);
1382                                 }
1383                             }
1384                             /* FIXME: call call_freer here for IN types */
1385                             break;
1386                         case STUBLESS_UNMARSHAL:
1387                             if (pParam->param_attributes.IsIn)
1388                             {
1389                                 if (pParam->param_attributes.IsByValue)
1390                                     call_unmarshaller(&stubMsg, &pArg, pTypeFormat, 0);
1391                                 else
1392                                     call_unmarshaller(&stubMsg, (unsigned char **)pArg, pTypeFormat, 0);
1393                             }
1394                             else if (pParam->param_attributes.IsOut &&
1395                                      !pParam->param_attributes.IsByValue)
1396                             {
1397                                 DWORD size = calc_arg_size(&stubMsg, pTypeFormat);
1398
1399                                 if(size)
1400                                 {
1401                                     *(void **)pArg = NdrAllocate(&stubMsg, size);
1402                                     memset(*(void **)pArg, 0, size);
1403                                 }
1404                             }
1405                             break;
1406                         case STUBLESS_CALCSIZE:
1407                             if (pParam->param_attributes.IsOut || pParam->param_attributes.IsReturn)
1408                             {
1409                                 if (pParam->param_attributes.IsByValue)
1410                                     call_buffer_sizer(&stubMsg, pArg, pTypeFormat);
1411                                 else
1412                                     call_buffer_sizer(&stubMsg, *(unsigned char **)pArg, pTypeFormat);
1413                             }
1414                             break;
1415                         default:
1416                             RpcRaiseException(RPC_S_INTERNAL_ERROR);
1417                         }
1418
1419                         current_offset += sizeof(NDR_PARAM_OIF_OTHER);
1420                     }
1421                     TRACE("\tmemory addr (after): %p -> %p\n", pArg, *(unsigned char **)pArg);
1422                 }
1423                 else /* old parameter format */
1424                 {
1425                     NDR_PARAM_OI_BASETYPE *pParam =
1426                         (NDR_PARAM_OI_BASETYPE *)&pFormat[current_offset];
1427                     /* note: current_stack_offset starts after the This pointer
1428                      * if present, so adjust this */
1429                     unsigned short current_stack_offset_adjusted = current_stack_offset +
1430                         ((pProcHeader->Oi_flags & RPC_FC_PROC_OIF_OBJECT) ? sizeof(void *) : 0);
1431                     unsigned char *pArg = (unsigned char *)(args+current_stack_offset_adjusted);
1432
1433                     /* no more parameters; exit loop */
1434                     if (current_stack_offset_adjusted >= stack_size)
1435                         break;
1436
1437                     TRACE("param[%d]: old format\n", i);
1438                     TRACE("\tparam_direction: 0x%x\n", pParam->param_direction);
1439                     TRACE("\tstack_offset: %x\n", current_stack_offset_adjusted);
1440
1441                     if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE ||
1442                         pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1443                     {
1444                         const unsigned char *pTypeFormat =
1445                             &pParam->type_format_char;
1446
1447                         TRACE("\tbase type 0x%02x\n", *pTypeFormat);
1448
1449                         switch (phase)
1450                         {
1451                         case STUBLESS_MARSHAL:
1452                             if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1453                                 call_marshaller(&stubMsg, pArg, pTypeFormat);
1454                             break;
1455                         case STUBLESS_UNMARSHAL:
1456                             if (pParam->param_direction == RPC_FC_IN_PARAM_BASETYPE)
1457                                 call_unmarshaller(&stubMsg, &pArg, pTypeFormat, 0);
1458                             break;
1459                         case STUBLESS_CALCSIZE:
1460                             if (pParam->param_direction == RPC_FC_RETURN_PARAM_BASETYPE)
1461                                 call_buffer_sizer(&stubMsg, pArg, pTypeFormat);
1462                             break;
1463                         default:
1464                             RpcRaiseException(RPC_S_INTERNAL_ERROR);
1465                         }
1466
1467                         current_stack_offset += call_memory_sizer(&stubMsg, pTypeFormat);
1468                         current_offset += sizeof(NDR_PARAM_OI_BASETYPE);
1469                     }
1470                     else
1471                     {
1472                         NDR_PARAM_OI_OTHER * pParamOther = 
1473                             (NDR_PARAM_OI_OTHER *)&pFormat[current_offset];
1474
1475                         const unsigned char * pTypeFormat =
1476                             &pStubDesc->pFormatTypes[pParamOther->type_offset];
1477
1478                         TRACE("\tcomplex type 0x%02x\n", *pTypeFormat);
1479
1480                         switch (phase)
1481                         {
1482                         case STUBLESS_MARSHAL:
1483                             if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1484                                 pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1485                                 pParam->param_direction == RPC_FC_RETURN_PARAM)
1486                                 call_marshaller(&stubMsg, pArg, pTypeFormat);
1487                             break;
1488                         case STUBLESS_UNMARSHAL:
1489                             if (pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1490                                 pParam->param_direction == RPC_FC_IN_PARAM)
1491                                 call_unmarshaller(&stubMsg, &pArg, pTypeFormat, 0);
1492                             break;
1493                         case STUBLESS_CALCSIZE:
1494                             if (pParam->param_direction == RPC_FC_OUT_PARAM ||
1495                                 pParam->param_direction == RPC_FC_IN_OUT_PARAM ||
1496                                 pParam->param_direction == RPC_FC_RETURN_PARAM)
1497                                 call_buffer_sizer(&stubMsg, pArg, pTypeFormat);
1498                             break;
1499                         default:
1500                             RpcRaiseException(RPC_S_INTERNAL_ERROR);
1501                         }
1502
1503                         current_stack_offset += pParamOther->stack_size * sizeof(INT);
1504                         current_offset += sizeof(NDR_PARAM_OI_OTHER);
1505                     }
1506                 }
1507             }
1508
1509             break;
1510         default:
1511             ERR("shouldn't reach here. phase %d\n", phase);
1512             break;
1513         }
1514     }
1515
1516     pRpcMsg->BufferLength = (unsigned int)(stubMsg.Buffer - (unsigned char *)pRpcMsg->Buffer);
1517
1518     if (ext_flags.HasNewCorrDesc)
1519     {
1520         /* free extra correlation package */
1521         /* NdrCorrelationFree(&stubMsg); */
1522     }
1523
1524     if (Oif_flags.HasPipes)
1525     {
1526         /* NdrPipesDone(...) */
1527     }
1528
1529     /* free the full pointer translation tables */
1530     if (pProcHeader->Oi_flags & RPC_FC_PROC_OIF_FULLPTR)
1531         NdrFullPointerXlatFree(stubMsg.FullPtrXlatTables);
1532
1533     /* free server function stack */
1534     HeapFree(GetProcessHeap(), 0, args);
1535
1536     return S_OK;
1537 }
1538
1539 void WINAPI NdrServerCall2(PRPC_MESSAGE pRpcMsg)
1540 {
1541     DWORD dwPhase;
1542     NdrStubCall2(NULL, NULL, pRpcMsg, &dwPhase);
1543 }