~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/rpcrt4/ndr_marshall.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  1 /*
  2  * NDR data marshalling
  3  *
  4  * Copyright 2002 Greg Turner
  5  * Copyright 2003-2006 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  *  - String structs
 23  *  - Byte count pointers
 24  *  - transmit_as/represent as
 25  *  - Multi-dimensional arrays
 26  *  - Conversion functions (NdrConvert)
 27  *  - Checks for integer addition overflow in user marshall functions
 28  */
 29 
 30 #include <stdarg.h>
 31 #include <stdio.h>
 32 #include <string.h>
 33 #include <limits.h>
 34 
 35 #include "windef.h"
 36 #include "winbase.h"
 37 #include "winerror.h"
 38 
 39 #include "ndr_misc.h"
 40 #include "rpcndr.h"
 41 
 42 #include "wine/unicode.h"
 43 #include "wine/rpcfc.h"
 44 
 45 #include "wine/debug.h"
 46 
 47 WINE_DEFAULT_DEBUG_CHANNEL(ole);
 48 
 49 #if defined(__i386__)
 50 # define LITTLE_ENDIAN_UINT32_WRITE(pchar, uint32) \
 51     (*((UINT32 *)(pchar)) = (uint32))
 52 
 53 # define LITTLE_ENDIAN_UINT32_READ(pchar) \
 54     (*((UINT32 *)(pchar)))
 55 #else
 56   /* these would work for i386 too, but less efficient */
 57 # define LITTLE_ENDIAN_UINT32_WRITE(pchar, uint32) \
 58     (*(pchar)     = LOBYTE(LOWORD(uint32)), \
 59      *((pchar)+1) = HIBYTE(LOWORD(uint32)), \
 60      *((pchar)+2) = LOBYTE(HIWORD(uint32)), \
 61      *((pchar)+3) = HIBYTE(HIWORD(uint32)))
 62 
 63 # define LITTLE_ENDIAN_UINT32_READ(pchar) \
 64     (MAKELONG( \
 65       MAKEWORD(*(pchar), *((pchar)+1)), \
 66       MAKEWORD(*((pchar)+2), *((pchar)+3))))
 67 #endif
 68 
 69 #define BIG_ENDIAN_UINT32_WRITE(pchar, uint32) \
 70   (*((pchar)+3) = LOBYTE(LOWORD(uint32)), \
 71    *((pchar)+2) = HIBYTE(LOWORD(uint32)), \
 72    *((pchar)+1) = LOBYTE(HIWORD(uint32)), \
 73    *(pchar)     = HIBYTE(HIWORD(uint32)))
 74 
 75 #define BIG_ENDIAN_UINT32_READ(pchar) \
 76   (MAKELONG( \
 77     MAKEWORD(*((pchar)+3), *((pchar)+2)), \
 78     MAKEWORD(*((pchar)+1), *(pchar))))
 79 
 80 #ifdef NDR_LOCAL_IS_BIG_ENDIAN
 81 # define NDR_LOCAL_UINT32_WRITE(pchar, uint32) \
 82     BIG_ENDIAN_UINT32_WRITE(pchar, uint32)
 83 # define NDR_LOCAL_UINT32_READ(pchar) \
 84     BIG_ENDIAN_UINT32_READ(pchar)
 85 #else
 86 # define NDR_LOCAL_UINT32_WRITE(pchar, uint32) \
 87     LITTLE_ENDIAN_UINT32_WRITE(pchar, uint32)
 88 # define NDR_LOCAL_UINT32_READ(pchar) \
 89     LITTLE_ENDIAN_UINT32_READ(pchar)
 90 #endif
 91 
 92 /* _Align must be the desired alignment,
 93  * e.g. ALIGN_LENGTH(len, 4) to align on a dword boundary. */
 94 #define ALIGNED_LENGTH(_Len, _Align) (((_Len)+(_Align)-1)&~((_Align)-1))
 95 #define ALIGNED_POINTER(_Ptr, _Align) ((LPVOID)ALIGNED_LENGTH((ULONG_PTR)(_Ptr), _Align))
 96 #define ALIGN_LENGTH(_Len, _Align) _Len = ALIGNED_LENGTH(_Len, _Align)
 97 #define ALIGN_POINTER(_Ptr, _Align) _Ptr = ALIGNED_POINTER(_Ptr, _Align)
 98 #define ALIGN_POINTER_CLEAR(_Ptr, _Align) \
 99     do { \
100         memset((_Ptr), 0, ((_Align) - (ULONG_PTR)(_Ptr)) & ((_Align) - 1)); \
101         ALIGN_POINTER(_Ptr, _Align); \
102     } while(0)
103 
104 #define STD_OVERFLOW_CHECK(_Msg) do { \
105     TRACE("buffer=%d/%d\n", _Msg->Buffer - (unsigned char *)_Msg->RpcMsg->Buffer, _Msg->BufferLength); \
106     if (_Msg->Buffer > (unsigned char *)_Msg->RpcMsg->Buffer + _Msg->BufferLength) \
107         ERR("buffer overflow %d bytes\n", _Msg->Buffer - ((unsigned char *)_Msg->RpcMsg->Buffer + _Msg->BufferLength)); \
108   } while (0)
109 
110 #define NDR_POINTER_ID_BASE 0x20000
111 #define NDR_POINTER_ID(pStubMsg) (NDR_POINTER_ID_BASE + ((pStubMsg)->UniquePtrCount++) * 4)
112 #define NDR_TABLE_SIZE 128
113 #define NDR_TABLE_MASK 127
114 
115 static unsigned char *WINAPI NdrBaseTypeMarshall(PMIDL_STUB_MESSAGE, unsigned char *, PFORMAT_STRING);
116 static unsigned char *WINAPI NdrBaseTypeUnmarshall(PMIDL_STUB_MESSAGE, unsigned char **, PFORMAT_STRING, unsigned char);
117 static void WINAPI NdrBaseTypeBufferSize(PMIDL_STUB_MESSAGE, unsigned char *, PFORMAT_STRING);
118 static void WINAPI NdrBaseTypeFree(PMIDL_STUB_MESSAGE, unsigned char *, PFORMAT_STRING);
119 static ULONG WINAPI NdrBaseTypeMemorySize(PMIDL_STUB_MESSAGE, PFORMAT_STRING);
120 
121 static unsigned char *WINAPI NdrContextHandleMarshall(PMIDL_STUB_MESSAGE, unsigned char *, PFORMAT_STRING);
122 static void WINAPI NdrContextHandleBufferSize(PMIDL_STUB_MESSAGE, unsigned char *, PFORMAT_STRING);
123 static unsigned char *WINAPI NdrContextHandleUnmarshall(PMIDL_STUB_MESSAGE, unsigned char **, PFORMAT_STRING, unsigned char);
124 
125 static unsigned char *WINAPI NdrRangeMarshall(PMIDL_STUB_MESSAGE,unsigned char *, PFORMAT_STRING);
126 static void WINAPI NdrRangeBufferSize(PMIDL_STUB_MESSAGE, unsigned char *, PFORMAT_STRING);
127 static ULONG WINAPI NdrRangeMemorySize(PMIDL_STUB_MESSAGE, PFORMAT_STRING);
128 static void WINAPI NdrRangeFree(PMIDL_STUB_MESSAGE, unsigned char *, PFORMAT_STRING);
129 
130 static ULONG WINAPI NdrByteCountPointerMemorySize(PMIDL_STUB_MESSAGE, PFORMAT_STRING);
131 
132 const NDR_MARSHALL NdrMarshaller[NDR_TABLE_SIZE] = {
133   0,
134   NdrBaseTypeMarshall, NdrBaseTypeMarshall, NdrBaseTypeMarshall,
135   NdrBaseTypeMarshall, NdrBaseTypeMarshall, NdrBaseTypeMarshall, NdrBaseTypeMarshall,
136   NdrBaseTypeMarshall, NdrBaseTypeMarshall, NdrBaseTypeMarshall, NdrBaseTypeMarshall,
137   NdrBaseTypeMarshall, NdrBaseTypeMarshall, NdrBaseTypeMarshall, NdrBaseTypeMarshall,
138   /* 0x10 */
139   NdrBaseTypeMarshall,
140   /* 0x11 */
141   NdrPointerMarshall, NdrPointerMarshall,
142   NdrPointerMarshall, NdrPointerMarshall,
143   /* 0x15 */
144   NdrSimpleStructMarshall, NdrSimpleStructMarshall,
145   NdrConformantStructMarshall, NdrConformantStructMarshall,
146   NdrConformantVaryingStructMarshall,
147   NdrComplexStructMarshall,
148   /* 0x1b */
149   NdrConformantArrayMarshall, 
150   NdrConformantVaryingArrayMarshall,
151   NdrFixedArrayMarshall, NdrFixedArrayMarshall,
152   NdrVaryingArrayMarshall, NdrVaryingArrayMarshall,
153   NdrComplexArrayMarshall,
154   /* 0x22 */
155   NdrConformantStringMarshall, 0, 0,
156   NdrConformantStringMarshall,
157   NdrNonConformantStringMarshall, 0, 0, 0,
158   /* 0x2a */
159   NdrEncapsulatedUnionMarshall,
160   NdrNonEncapsulatedUnionMarshall,
161   NdrByteCountPointerMarshall,
162   NdrXmitOrRepAsMarshall, NdrXmitOrRepAsMarshall,
163   /* 0x2f */
164   NdrInterfacePointerMarshall,
165   /* 0x30 */
166   NdrContextHandleMarshall,
167   /* 0xb1 */
168   0, 0, 0,
169   NdrUserMarshalMarshall,
170   0, 0,
171   /* 0xb7 */
172   NdrRangeMarshall
173 };
174 const NDR_UNMARSHALL NdrUnmarshaller[NDR_TABLE_SIZE] = {
175   0,
176   NdrBaseTypeUnmarshall, NdrBaseTypeUnmarshall, NdrBaseTypeUnmarshall,
177   NdrBaseTypeUnmarshall, NdrBaseTypeUnmarshall, NdrBaseTypeUnmarshall, NdrBaseTypeUnmarshall,
178   NdrBaseTypeUnmarshall, NdrBaseTypeUnmarshall, NdrBaseTypeUnmarshall, NdrBaseTypeUnmarshall,
179   NdrBaseTypeUnmarshall, NdrBaseTypeUnmarshall, NdrBaseTypeUnmarshall, NdrBaseTypeUnmarshall,
180   /* 0x10 */
181   NdrBaseTypeUnmarshall,
182   /* 0x11 */
183   NdrPointerUnmarshall, NdrPointerUnmarshall,
184   NdrPointerUnmarshall, NdrPointerUnmarshall,
185   /* 0x15 */
186   NdrSimpleStructUnmarshall, NdrSimpleStructUnmarshall,
187   NdrConformantStructUnmarshall, NdrConformantStructUnmarshall,
188   NdrConformantVaryingStructUnmarshall,
189   NdrComplexStructUnmarshall,
190   /* 0x1b */
191   NdrConformantArrayUnmarshall, 
192   NdrConformantVaryingArrayUnmarshall,
193   NdrFixedArrayUnmarshall, NdrFixedArrayUnmarshall,
194   NdrVaryingArrayUnmarshall, NdrVaryingArrayUnmarshall,
195   NdrComplexArrayUnmarshall,
196   /* 0x22 */
197   NdrConformantStringUnmarshall, 0, 0,
198   NdrConformantStringUnmarshall,
199   NdrNonConformantStringUnmarshall, 0, 0, 0,
200   /* 0x2a */
201   NdrEncapsulatedUnionUnmarshall,
202   NdrNonEncapsulatedUnionUnmarshall,
203   NdrByteCountPointerUnmarshall,
204   NdrXmitOrRepAsUnmarshall, NdrXmitOrRepAsUnmarshall,
205   /* 0x2f */
206   NdrInterfacePointerUnmarshall,
207   /* 0x30 */
208   NdrContextHandleUnmarshall,
209   /* 0xb1 */
210   0, 0, 0,
211   NdrUserMarshalUnmarshall,
212   0, 0,
213   /* 0xb7 */
214   NdrRangeUnmarshall
215 };
216 const NDR_BUFFERSIZE NdrBufferSizer[NDR_TABLE_SIZE] = {
217   0,
218   NdrBaseTypeBufferSize, NdrBaseTypeBufferSize, NdrBaseTypeBufferSize,
219   NdrBaseTypeBufferSize, NdrBaseTypeBufferSize, NdrBaseTypeBufferSize, NdrBaseTypeBufferSize,
220   NdrBaseTypeBufferSize, NdrBaseTypeBufferSize, NdrBaseTypeBufferSize, NdrBaseTypeBufferSize,
221   NdrBaseTypeBufferSize, NdrBaseTypeBufferSize, NdrBaseTypeBufferSize, NdrBaseTypeBufferSize,
222   /* 0x10 */
223   NdrBaseTypeBufferSize,
224   /* 0x11 */
225   NdrPointerBufferSize, NdrPointerBufferSize,
226   NdrPointerBufferSize, NdrPointerBufferSize,
227   /* 0x15 */
228   NdrSimpleStructBufferSize, NdrSimpleStructBufferSize,
229   NdrConformantStructBufferSize, NdrConformantStructBufferSize,
230   NdrConformantVaryingStructBufferSize,
231   NdrComplexStructBufferSize,
232   /* 0x1b */
233   NdrConformantArrayBufferSize, 
234   NdrConformantVaryingArrayBufferSize,
235   NdrFixedArrayBufferSize, NdrFixedArrayBufferSize,
236   NdrVaryingArrayBufferSize, NdrVaryingArrayBufferSize,
237   NdrComplexArrayBufferSize,
238   /* 0x22 */
239   NdrConformantStringBufferSize, 0, 0,
240   NdrConformantStringBufferSize,
241   NdrNonConformantStringBufferSize, 0, 0, 0,
242   /* 0x2a */
243   NdrEncapsulatedUnionBufferSize,
244   NdrNonEncapsulatedUnionBufferSize,
245   NdrByteCountPointerBufferSize,
246   NdrXmitOrRepAsBufferSize, NdrXmitOrRepAsBufferSize,
247   /* 0x2f */
248   NdrInterfacePointerBufferSize,
249   /* 0x30 */
250   NdrContextHandleBufferSize,
251   /* 0xb1 */
252   0, 0, 0,
253   NdrUserMarshalBufferSize,
254   0, 0,
255   /* 0xb7 */
256   NdrRangeBufferSize
257 };
258 const NDR_MEMORYSIZE NdrMemorySizer[NDR_TABLE_SIZE] = {
259   0,
260   NdrBaseTypeMemorySize, NdrBaseTypeMemorySize, NdrBaseTypeMemorySize,
261   NdrBaseTypeMemorySize, NdrBaseTypeMemorySize, NdrBaseTypeMemorySize, NdrBaseTypeMemorySize,
262   NdrBaseTypeMemorySize, NdrBaseTypeMemorySize, NdrBaseTypeMemorySize, NdrBaseTypeMemorySize,
263   NdrBaseTypeMemorySize, NdrBaseTypeMemorySize, NdrBaseTypeMemorySize, NdrBaseTypeMemorySize,
264   /* 0x10 */
265   NdrBaseTypeMemorySize,
266   /* 0x11 */
267   NdrPointerMemorySize, NdrPointerMemorySize,
268   NdrPointerMemorySize, NdrPointerMemorySize,
269   /* 0x15 */
270   NdrSimpleStructMemorySize, NdrSimpleStructMemorySize,
271   NdrConformantStructMemorySize, NdrConformantStructMemorySize,
272   NdrConformantVaryingStructMemorySize,
273   NdrComplexStructMemorySize,
274   /* 0x1b */
275   NdrConformantArrayMemorySize,
276   NdrConformantVaryingArrayMemorySize,
277   NdrFixedArrayMemorySize, NdrFixedArrayMemorySize,
278   NdrVaryingArrayMemorySize, NdrVaryingArrayMemorySize,
279   NdrComplexArrayMemorySize,
280   /* 0x22 */
281   NdrConformantStringMemorySize, 0, 0,
282   NdrConformantStringMemorySize,
283   NdrNonConformantStringMemorySize, 0, 0, 0,
284   /* 0x2a */
285   NdrEncapsulatedUnionMemorySize,
286   NdrNonEncapsulatedUnionMemorySize,
287   NdrByteCountPointerMemorySize,
288   NdrXmitOrRepAsMemorySize, NdrXmitOrRepAsMemorySize,
289   /* 0x2f */
290   NdrInterfacePointerMemorySize,
291   /* 0x30 */
292   0,
293   /* 0xb1 */
294   0, 0, 0,
295   NdrUserMarshalMemorySize,
296   0, 0,
297   /* 0xb7 */
298   NdrRangeMemorySize
299 };
300 const NDR_FREE NdrFreer[NDR_TABLE_SIZE] = {
301   0,
302   NdrBaseTypeFree, NdrBaseTypeFree, NdrBaseTypeFree,
303   NdrBaseTypeFree, NdrBaseTypeFree, NdrBaseTypeFree, NdrBaseTypeFree,
304   NdrBaseTypeFree, NdrBaseTypeFree, NdrBaseTypeFree, NdrBaseTypeFree,
305   NdrBaseTypeFree, NdrBaseTypeFree, NdrBaseTypeFree, NdrBaseTypeFree,
306   /* 0x10 */
307   NdrBaseTypeFree,
308   /* 0x11 */
309   NdrPointerFree, NdrPointerFree,
310   NdrPointerFree, NdrPointerFree,
311   /* 0x15 */
312   NdrSimpleStructFree, NdrSimpleStructFree,
313   NdrConformantStructFree, NdrConformantStructFree,
314   NdrConformantVaryingStructFree,
315   NdrComplexStructFree,
316   /* 0x1b */
317   NdrConformantArrayFree, 
318   NdrConformantVaryingArrayFree,
319   NdrFixedArrayFree, NdrFixedArrayFree,
320   NdrVaryingArrayFree, NdrVaryingArrayFree,
321   NdrComplexArrayFree,
322   /* 0x22 */
323   0, 0, 0,
324   0, 0, 0, 0, 0,
325   /* 0x2a */
326   NdrEncapsulatedUnionFree,
327   NdrNonEncapsulatedUnionFree,
328   0,
329   NdrXmitOrRepAsFree, NdrXmitOrRepAsFree,
330   /* 0x2f */
331   NdrInterfacePointerFree,
332   /* 0x30 */
333   0,
334   /* 0xb1 */
335   0, 0, 0,
336   NdrUserMarshalFree,
337   0, 0,
338   /* 0xb7 */
339   NdrRangeFree
340 };
341 
342 typedef struct _NDR_MEMORY_LIST
343 {
344     ULONG magic;
345     ULONG size;
346     ULONG reserved;
347     struct _NDR_MEMORY_LIST *next;
348 } NDR_MEMORY_LIST;
349 
350 #define MEML_MAGIC  ('M' << 24 | 'E' << 16 | 'M' << 8 | 'L')
351 
352 /***********************************************************************
353  *            NdrAllocate [RPCRT4.@]
354  *
355  * Allocates a block of memory using pStubMsg->pfnAllocate.
356  *
357  * PARAMS
358  *  pStubMsg [I/O] MIDL_STUB_MESSAGE structure.
359  *  len      [I]   Size of memory block to allocate.
360  *
361  * RETURNS
362  *  The memory block of size len that was allocated.
363  *
364  * NOTES
365  *  The memory block is always 8-byte aligned.
366  *  If the function is unable to allocate memory an ERROR_OUTOFMEMORY
367  *  exception is raised.
368  */
369 void * WINAPI NdrAllocate(MIDL_STUB_MESSAGE *pStubMsg, SIZE_T len)
370 {
371     SIZE_T aligned_len;
372     SIZE_T adjusted_len;
373     void *p;
374     NDR_MEMORY_LIST *mem_list;
375 
376     aligned_len = ALIGNED_LENGTH(len, 8);
377     adjusted_len = aligned_len + sizeof(NDR_MEMORY_LIST);
378     /* check for overflow */
379     if (adjusted_len < len)
380     {
381         ERR("overflow of adjusted_len %ld, len %ld\n", adjusted_len, len);
382         RpcRaiseException(RPC_X_BAD_STUB_DATA);
383     }
384 
385     p = pStubMsg->pfnAllocate(adjusted_len);
386     if (!p) RpcRaiseException(ERROR_OUTOFMEMORY);
387 
388     mem_list = (NDR_MEMORY_LIST *)((char *)p + aligned_len);
389     mem_list->magic = MEML_MAGIC;
390     mem_list->size = aligned_len;
391     mem_list->reserved = 0;
392     mem_list->next = pStubMsg->pMemoryList;
393     pStubMsg->pMemoryList = mem_list;
394 
395     TRACE("-- %p\n", p);
396     return p;
397 }
398 
399 static void NdrFree(MIDL_STUB_MESSAGE *pStubMsg, unsigned char *Pointer)
400 {
401     TRACE("(%p, %p)\n", pStubMsg, Pointer);
402 
403     pStubMsg->pfnFree(Pointer);
404 }
405 
406 static inline BOOL IsConformanceOrVariancePresent(PFORMAT_STRING pFormat)
407 {
408     return (*(const ULONG *)pFormat != -1);
409 }
410 
411 static PFORMAT_STRING ReadConformance(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat)
412 {
413   ALIGN_POINTER(pStubMsg->Buffer, 4);
414   if (pStubMsg->Buffer + 4 > pStubMsg->BufferEnd)
415       RpcRaiseException(RPC_X_BAD_STUB_DATA);
416   pStubMsg->MaxCount = NDR_LOCAL_UINT32_READ(pStubMsg->Buffer);
417   pStubMsg->Buffer += 4;
418   TRACE("unmarshalled conformance is %ld\n", pStubMsg->MaxCount);
419   if (pStubMsg->fHasNewCorrDesc)
420     return pFormat+6;
421   else
422     return pFormat+4;
423 }
424 
425 static inline PFORMAT_STRING ReadVariance(MIDL_STUB_MESSAGE *pStubMsg, PFORMAT_STRING pFormat, ULONG MaxValue)
426 {
427   if (pFormat && !IsConformanceOrVariancePresent(pFormat))
428   {
429     pStubMsg->Offset = 0;
430     pStubMsg->ActualCount = pStubMsg->MaxCount;
431     goto done;
432   }
433 
434   ALIGN_POINTER(pStubMsg->Buffer, 4);
435   if (pStubMsg->Buffer + 8 > pStubMsg->BufferEnd)
436     RpcRaiseException(RPC_X_BAD_STUB_DATA);
437   pStubMsg->Offset      = NDR_LOCAL_UINT32_READ(pStubMsg->Buffer);
438   pStubMsg->Buffer += 4;
439   TRACE("offset is %d\n", pStubMsg->Offset);
440   pStubMsg->ActualCount = NDR_LOCAL_UINT32_READ(pStubMsg->Buffer);
441   pStubMsg->Buffer += 4;
442   TRACE("variance is %d\n", pStubMsg->ActualCount);
443 
444   if ((pStubMsg->ActualCount > MaxValue) ||
445       (pStubMsg->ActualCount + pStubMsg->Offset > MaxValue))
446   {
447     ERR("invalid array bound(s): ActualCount = %d, Offset = %d, MaxValue = %d\n",
448         pStubMsg->ActualCount, pStubMsg->Offset, MaxValue);
449     RpcRaiseException(RPC_S_INVALID_BOUND);
450     return NULL;
451   }
452 
453 done:
454   if (pStubMsg->fHasNewCorrDesc)
455     return pFormat+6;
456   else
457     return pFormat+4;
458 }
459 
460 /* writes the conformance value to the buffer */
461 static inline void WriteConformance(MIDL_STUB_MESSAGE *pStubMsg)
462 {
463     ALIGN_POINTER_CLEAR(pStubMsg->Buffer, 4);
464     if (pStubMsg->Buffer + 4 > (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength)
465         RpcRaiseException(RPC_X_BAD_STUB_DATA);
466     NDR_LOCAL_UINT32_WRITE(pStubMsg->Buffer, pStubMsg->MaxCount);
467     pStubMsg->Buffer += 4;
468 }
469 
470 /* writes the variance values to the buffer */
471 static inline void WriteVariance(MIDL_STUB_MESSAGE *pStubMsg)
472 {
473     ALIGN_POINTER_CLEAR(pStubMsg->Buffer, 4);
474     if (pStubMsg->Buffer + 8 > (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength)
475         RpcRaiseException(RPC_X_BAD_STUB_DATA);
476     NDR_LOCAL_UINT32_WRITE(pStubMsg->Buffer, pStubMsg->Offset);
477     pStubMsg->Buffer += 4;
478     NDR_LOCAL_UINT32_WRITE(pStubMsg->Buffer, pStubMsg->ActualCount);
479     pStubMsg->Buffer += 4;
480 }
481 
482 /* requests buffer space for the conformance value */
483 static inline void SizeConformance(MIDL_STUB_MESSAGE *pStubMsg)
484 {
485     ALIGN_LENGTH(pStubMsg->BufferLength, 4);
486     if (pStubMsg->BufferLength + 4 < pStubMsg->BufferLength)
487         RpcRaiseException(RPC_X_BAD_STUB_DATA);
488     pStubMsg->BufferLength += 4;
489 }
490 
491 /* requests buffer space for the variance values */
492 static inline void SizeVariance(MIDL_STUB_MESSAGE *pStubMsg)
493 {
494     ALIGN_LENGTH(pStubMsg->BufferLength, 4);
495     if (pStubMsg->BufferLength + 8 < pStubMsg->BufferLength)
496         RpcRaiseException(RPC_X_BAD_STUB_DATA);
497     pStubMsg->BufferLength += 8;
498 }
499 
500 PFORMAT_STRING ComputeConformanceOrVariance(
501     MIDL_STUB_MESSAGE *pStubMsg, unsigned char *pMemory,
502     PFORMAT_STRING pFormat, ULONG_PTR def, ULONG_PTR *pCount)
503 {
504   BYTE dtype = pFormat[0] & 0xf;
505   short ofs = *(const short *)&pFormat[2];
506   LPVOID ptr = NULL;
507   DWORD data = 0;
508 
509   if (!IsConformanceOrVariancePresent(pFormat)) {
510     /* null descriptor */
511     *pCount = def;
512     goto finish_conf;
513   }
514 
515   switch (pFormat[0] & 0xf0) {
516   case RPC_FC_NORMAL_CONFORMANCE:
517     TRACE("normal conformance, ofs=%d\n", ofs);
518     ptr = pMemory;
519     break;
520   case RPC_FC_POINTER_CONFORMANCE:
521     TRACE("pointer conformance, ofs=%d\n", ofs);
522     ptr = pStubMsg->Memory;
523     break;
524   case RPC_FC_TOP_LEVEL_CONFORMANCE:
525     TRACE("toplevel conformance, ofs=%d\n", ofs);
526     if (pStubMsg->StackTop) {
527       ptr = pStubMsg->StackTop;
528     }
529     else {
530       /* -Os mode, *pCount is already set */
531       goto finish_conf;
532     }
533     break;
534   case RPC_FC_CONSTANT_CONFORMANCE:
535     data = ofs | ((DWORD)pFormat[1] << 16);
536     TRACE("constant conformance, val=%d\n", data);
537     *pCount = data;
538     goto finish_conf;
539   case RPC_FC_TOP_LEVEL_MULTID_CONFORMANCE:
540     FIXME("toplevel multidimensional conformance, ofs=%d\n", ofs);
541     if (pStubMsg->StackTop) {
542       ptr = pStubMsg->StackTop;
543     }
544     else {
545       /* ? */
546       goto done_conf_grab;
547     }
548     break;
549   default:
550     FIXME("unknown conformance type %x\n", pFormat[0] & 0xf0);
551   }
552 
553   switch (pFormat[1]) {
554   case RPC_FC_DEREFERENCE:
555     ptr = *(LPVOID*)((char *)ptr + ofs);
556     break;
557   case RPC_FC_CALLBACK:
558   {
559     unsigned char *old_stack_top = pStubMsg->StackTop;
560     pStubMsg->StackTop = ptr;
561 
562     /* ofs is index into StubDesc->apfnExprEval */
563     TRACE("callback conformance into apfnExprEval[%d]\n", ofs);
564     pStubMsg->StubDesc->apfnExprEval[ofs](pStubMsg);
565 
566     pStubMsg->StackTop = old_stack_top;
567 
568     /* the callback function always stores the computed value in MaxCount */
569     *pCount = pStubMsg->MaxCount;
570     goto finish_conf;
571   }
572   default:
573     ptr = (char *)ptr + ofs;
574     break;
575   }
576 
577   switch (dtype) {
578   case RPC_FC_LONG:
579   case RPC_FC_ULONG:
580     data = *(DWORD*)ptr;
581     break;
582   case RPC_FC_SHORT:
583     data = *(SHORT*)ptr;
584     break;
585   case RPC_FC_USHORT:
586     data = *(USHORT*)ptr;
587     break;
588   case RPC_FC_CHAR:
589   case RPC_FC_SMALL:
590     data = *(CHAR*)ptr;
591     break;
592   case RPC_FC_BYTE:
593   case RPC_FC_USMALL:
594     data = *(UCHAR*)ptr;
595     break;
596   default:
597     FIXME("unknown conformance data type %x\n", dtype);
598     goto done_conf_grab;
599   }
600   TRACE("dereferenced data type %x at %p, got %d\n", dtype, ptr, data);
601 
602 done_conf_grab:
603   switch (pFormat[1]) {
604   case RPC_FC_DEREFERENCE: /* already handled */
605   case 0: /* no op */
606     *pCount = data;
607     break;
608   case RPC_FC_ADD_1:
609     *pCount = data + 1;
610     break;
611   case RPC_FC_SUB_1:
612     *pCount = data - 1;
613     break;
614   case RPC_FC_MULT_2:
615     *pCount = data * 2;
616     break;
617   case RPC_FC_DIV_2:
618     *pCount = data / 2;
619     break;
620   default:
621     FIXME("unknown conformance op %d\n", pFormat[1]);
622     goto finish_conf;
623   }
624 
625 finish_conf:
626   TRACE("resulting conformance is %ld\n", *pCount);
627   if (pStubMsg->fHasNewCorrDesc)
628     return pFormat+6;
629   else
630     return pFormat+4;
631 }
632 
633 static inline PFORMAT_STRING SkipConformance(PMIDL_STUB_MESSAGE pStubMsg,
634                                              PFORMAT_STRING pFormat)
635 {
636   if (IsConformanceOrVariancePresent(pFormat))
637   {
638     if (pStubMsg->fHasNewCorrDesc)
639       pFormat += 6;
640     else
641       pFormat += 4;
642   }
643   return pFormat;
644 }
645 
646 /* multiply two numbers together, raising an RPC_S_INVALID_BOUND exception if
647  * the result overflows 32-bits */
648 static inline ULONG safe_multiply(ULONG a, ULONG b)
649 {
650     ULONGLONG ret = (ULONGLONG)a * b;
651     if (ret > 0xffffffff)
652     {
653         RpcRaiseException(RPC_S_INVALID_BOUND);
654         return 0;
655     }
656     return ret;
657 }
658 
659 static inline void safe_buffer_increment(MIDL_STUB_MESSAGE *pStubMsg, ULONG size)
660 {
661     if ((pStubMsg->Buffer + size < pStubMsg->Buffer) || /* integer overflow of pStubMsg->Buffer */
662         (pStubMsg->Buffer + size > (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength))
663         RpcRaiseException(RPC_X_BAD_STUB_DATA);
664     pStubMsg->Buffer += size;
665 }
666 
667 static inline void safe_buffer_length_increment(MIDL_STUB_MESSAGE *pStubMsg, ULONG size)
668 {
669     if (pStubMsg->BufferLength + size < pStubMsg->BufferLength) /* integer overflow of pStubMsg->BufferSize */
670     {
671         ERR("buffer length overflow - BufferLength = %u, size = %u\n",
672             pStubMsg->BufferLength, size);
673         RpcRaiseException(RPC_X_BAD_STUB_DATA);
674     }
675     pStubMsg->BufferLength += size;
676 }
677 
678 /* copies data from the buffer, checking that there is enough data in the buffer
679  * to do so */
680 static inline void safe_copy_from_buffer(MIDL_STUB_MESSAGE *pStubMsg, void *p, ULONG size)
681 {
682     if ((pStubMsg->Buffer + size < pStubMsg->Buffer) || /* integer overflow of pStubMsg->Buffer */
683         (pStubMsg->Buffer + size > pStubMsg->BufferEnd))
684     {
685         ERR("buffer overflow - Buffer = %p, BufferEnd = %p, size = %u\n",
686             pStubMsg->Buffer, pStubMsg->BufferEnd, size);
687         RpcRaiseException(RPC_X_BAD_STUB_DATA);
688     }
689     if (p == pStubMsg->Buffer)
690         ERR("pointer is the same as the buffer\n");
691     memcpy(p, pStubMsg->Buffer, size);
692     pStubMsg->Buffer += size;
693 }
694 
695 /* copies data to the buffer, checking that there is enough space to do so */
696 static inline void safe_copy_to_buffer(MIDL_STUB_MESSAGE *pStubMsg, const void *p, ULONG size)
697 {
698     if ((pStubMsg->Buffer + size < pStubMsg->Buffer) || /* integer overflow of pStubMsg->Buffer */
699         (pStubMsg->Buffer + size > (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength))
700     {
701         ERR("buffer overflow - Buffer = %p, BufferEnd = %p, size = %u\n",
702             pStubMsg->Buffer, (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength,
703             size);
704         RpcRaiseException(RPC_X_BAD_STUB_DATA);
705     }
706     memcpy(pStubMsg->Buffer, p, size);
707     pStubMsg->Buffer += size;
708 }
709 
710 /* verify that string data sitting in the buffer is valid and safe to
711  * unmarshall */
712 static void validate_string_data(MIDL_STUB_MESSAGE *pStubMsg, ULONG bufsize, ULONG esize)
713 {
714     ULONG i;
715 
716     /* verify the buffer is safe to access */
717     if ((pStubMsg->Buffer + bufsize < pStubMsg->Buffer) ||
718         (pStubMsg->Buffer + bufsize > pStubMsg->BufferEnd))
719     {
720         ERR("bufsize 0x%x exceeded buffer end %p of buffer %p\n", bufsize,
721             pStubMsg->BufferEnd, pStubMsg->Buffer);
722         RpcRaiseException(RPC_X_BAD_STUB_DATA);
723     }
724 
725     /* strings must always have null terminating bytes */
726     if (bufsize < esize)
727     {
728         ERR("invalid string length of %d\n", bufsize / esize);
729         RpcRaiseException(RPC_S_INVALID_BOUND);
730     }
731 
732     for (i = bufsize - esize; i < bufsize; i++)
733         if (pStubMsg->Buffer[i] != 0)
734         {
735             ERR("string not null-terminated at byte position %d, data is 0x%x\n",
736                 i, pStubMsg->Buffer[i]);
737             RpcRaiseException(RPC_S_INVALID_BOUND);
738         }
739 }
740 
741 static inline void dump_pointer_attr(unsigned char attr)
742 {
743     if (attr & RPC_FC_P_ALLOCALLNODES)
744         TRACE(" RPC_FC_P_ALLOCALLNODES");
745     if (attr & RPC_FC_P_DONTFREE)
746         TRACE(" RPC_FC_P_DONTFREE");
747     if (attr & RPC_FC_P_ONSTACK)
748         TRACE(" RPC_FC_P_ONSTACK");
749     if (attr & RPC_FC_P_SIMPLEPOINTER)
750         TRACE(" RPC_FC_P_SIMPLEPOINTER");
751     if (attr & RPC_FC_P_DEREF)
752         TRACE(" RPC_FC_P_DEREF");
753     TRACE("\n");
754 }
755 
756 /***********************************************************************
757  *           PointerMarshall [internal]
758  */
759 static void PointerMarshall(PMIDL_STUB_MESSAGE pStubMsg,
760                             unsigned char *Buffer,
761                             unsigned char *Pointer,
762                             PFORMAT_STRING pFormat)
763 {
764   unsigned type = pFormat[0], attr = pFormat[1];
765   PFORMAT_STRING desc;
766   NDR_MARSHALL m;
767   ULONG pointer_id;
768   int pointer_needs_marshaling;
769 
770   TRACE("(%p,%p,%p,%p)\n", pStubMsg, Buffer, Pointer, pFormat);
771   TRACE("type=0x%x, attr=", type); dump_pointer_attr(attr);
772   pFormat += 2;
773   if (attr & RPC_FC_P_SIMPLEPOINTER) desc = pFormat;
774   else desc = pFormat + *(const SHORT*)pFormat;
775 
776   switch (type) {
777   case RPC_FC_RP: /* ref pointer (always non-null) */
778     if (!Pointer)
779     {
780       ERR("NULL ref pointer is not allowed\n");
781       RpcRaiseException(RPC_X_NULL_REF_POINTER);
782     }
783     pointer_needs_marshaling = 1;
784     break;
785   case RPC_FC_UP: /* unique pointer */
786   case RPC_FC_OP: /* object pointer - same as unique here */
787     if (Pointer)
788       pointer_needs_marshaling = 1;
789     else
790       pointer_needs_marshaling = 0;
791     pointer_id = Pointer ? NDR_POINTER_ID(pStubMsg) : 0;
792     TRACE("writing 0x%08x to buffer\n", pointer_id);
793     NDR_LOCAL_UINT32_WRITE(Buffer, pointer_id);
794     break;
795   case RPC_FC_FP:
796     pointer_needs_marshaling = !NdrFullPointerQueryPointer(
797       pStubMsg->FullPtrXlatTables, Pointer, 1, &pointer_id);
798     TRACE("writing 0x%08x to buffer\n", pointer_id);
799     NDR_LOCAL_UINT32_WRITE(Buffer, pointer_id);
800     break;
801   default:
802     FIXME("unhandled ptr type=%02x\n", type);
803     RpcRaiseException(RPC_X_BAD_STUB_DATA);
804     return;
805   }
806 
807   TRACE("calling marshaller for type 0x%x\n", (int)*desc);
808 
809   if (pointer_needs_marshaling) {
810     if (attr & RPC_FC_P_DEREF) {
811       Pointer = *(unsigned char**)Pointer;
812       TRACE("deref => %p\n", Pointer);
813     }
814     m = NdrMarshaller[*desc & NDR_TABLE_MASK];
815     if (m) m(pStubMsg, Pointer, desc);
816     else FIXME("no marshaller for data type=%02x\n", *desc);
817   }
818 
819   STD_OVERFLOW_CHECK(pStubMsg);
820 }
821 
822 /***********************************************************************
823  *           PointerUnmarshall [internal]
824  */
825 static void PointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
826                               unsigned char *Buffer,
827                               unsigned char **pPointer,
828                               unsigned char *pSrcPointer,
829                               PFORMAT_STRING pFormat,
830                               unsigned char fMustAlloc)
831 {
832   unsigned type = pFormat[0], attr = pFormat[1];
833   PFORMAT_STRING desc;
834   NDR_UNMARSHALL m;
835   DWORD pointer_id = 0;
836   int pointer_needs_unmarshaling;
837 
838   TRACE("(%p,%p,%p,%p,%p,%d)\n", pStubMsg, Buffer, pPointer, pSrcPointer, pFormat, fMustAlloc);
839   TRACE("type=0x%x, attr=", type); dump_pointer_attr(attr);
840   pFormat += 2;
841   if (attr & RPC_FC_P_SIMPLEPOINTER) desc = pFormat;
842   else desc = pFormat + *(const SHORT*)pFormat;
843 
844   switch (type) {
845   case RPC_FC_RP: /* ref pointer (always non-null) */
846     pointer_needs_unmarshaling = 1;
847     break;
848   case RPC_FC_UP: /* unique pointer */
849     pointer_id = NDR_LOCAL_UINT32_READ(Buffer);
850     TRACE("pointer_id is 0x%08x\n", pointer_id);
851     if (pointer_id)
852       pointer_needs_unmarshaling = 1;
853     else {
854       *pPointer = NULL;
855       pointer_needs_unmarshaling = 0;
856     }
857     break;
858   case RPC_FC_OP: /* object pointer - we must free data before overwriting it */
859     pointer_id = NDR_LOCAL_UINT32_READ(Buffer);
860     TRACE("pointer_id is 0x%08x\n", pointer_id);
861     if (!fMustAlloc && pSrcPointer)
862     {
863         FIXME("free object pointer %p\n", pSrcPointer);
864         fMustAlloc = TRUE;
865     }
866     if (pointer_id)
867       pointer_needs_unmarshaling = 1;
868     else
869     {
870       *pPointer = NULL;    
871       pointer_needs_unmarshaling = 0;
872     }
873     break;
874   case RPC_FC_FP:
875     pointer_id = NDR_LOCAL_UINT32_READ(Buffer);
876     TRACE("pointer_id is 0x%08x\n", pointer_id);
877     pointer_needs_unmarshaling = !NdrFullPointerQueryRefId(
878       pStubMsg->FullPtrXlatTables, pointer_id, 1, (void **)pPointer);
879     break;
880   default:
881     FIXME("unhandled ptr type=%02x\n", type);
882     RpcRaiseException(RPC_X_BAD_STUB_DATA);
883     return;
884   }
885 
886   if (pointer_needs_unmarshaling) {
887     unsigned char *base_ptr_val = *pPointer;
888     unsigned char **current_ptr = pPointer;
889     if (pStubMsg->IsClient) {
890       TRACE("client\n");
891       /* if we aren't forcing allocation of memory then try to use the existing
892        * (source) pointer to unmarshall the data into so that [in,out]
893        * parameters behave correctly. it doesn't matter if the parameter is
894        * [out] only since in that case the pointer will be NULL. we force
895        * allocation when the source pointer is NULL here instead of in the type
896        * unmarshalling routine for the benefit of the deref code below */
897       if (!fMustAlloc) {
898         if (pSrcPointer) {
899           TRACE("setting *pPointer to %p\n", pSrcPointer);
900           *pPointer = base_ptr_val = pSrcPointer;
901         } else
902           fMustAlloc = TRUE;
903       }
904     } else {
905       TRACE("server\n");
906       /* the memory in a stub is never initialised, so we have to work out here
907        * whether we have to initialise it so we can use the optimisation of
908        * setting the pointer to the buffer, if possible, or set fMustAlloc to
909        * TRUE. */
910       if (attr & RPC_FC_P_DEREF) {
911         fMustAlloc = TRUE;
912       } else {
913         base_ptr_val = NULL;
914         *current_ptr = NULL;
915       }
916     }
917 
918     if (attr & RPC_FC_P_ALLOCALLNODES)
919         FIXME("RPC_FC_P_ALLOCALLNODES not implemented\n");
920 
921     if (attr & RPC_FC_P_DEREF) {
922       if (fMustAlloc) {
923         base_ptr_val = NdrAllocate(pStubMsg, sizeof(void *));
924         *pPointer = base_ptr_val;
925         current_ptr = (unsigned char **)base_ptr_val;
926       } else
927         current_ptr = *(unsigned char***)current_ptr;
928       TRACE("deref => %p\n", current_ptr);
929       if (!fMustAlloc && !*current_ptr) fMustAlloc = TRUE;
930     }
931     m = NdrUnmarshaller[*desc & NDR_TABLE_MASK];
932     if (m) m(pStubMsg, current_ptr, desc, fMustAlloc);
933     else FIXME("no unmarshaller for data type=%02x\n", *desc);
934 
935     if (type == RPC_FC_FP)
936       NdrFullPointerInsertRefId(pStubMsg->FullPtrXlatTables, pointer_id,
937                                 base_ptr_val);
938   }
939 
940   TRACE("pointer=%p\n", *pPointer);
941 }
942 
943 /***********************************************************************
944  *           PointerBufferSize [internal]
945  */
946 static void PointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
947                               unsigned char *Pointer,
948                               PFORMAT_STRING pFormat)
949 {
950   unsigned type = pFormat[0], attr = pFormat[1];
951   PFORMAT_STRING desc;
952   NDR_BUFFERSIZE m;
953   int pointer_needs_sizing;
954   ULONG pointer_id;
955 
956   TRACE("(%p,%p,%p)\n", pStubMsg, Pointer, pFormat);
957   TRACE("type=0x%x, attr=", type); dump_pointer_attr(attr);
958   pFormat += 2;
959   if (attr & RPC_FC_P_SIMPLEPOINTER) desc = pFormat;
960   else desc = pFormat + *(const SHORT*)pFormat;
961 
962   switch (type) {
963   case RPC_FC_RP: /* ref pointer (always non-null) */
964     if (!Pointer)
965     {
966       ERR("NULL ref pointer is not allowed\n");
967       RpcRaiseException(RPC_X_NULL_REF_POINTER);
968     }
969     break;
970   case RPC_FC_OP:
971   case RPC_FC_UP:
972     /* NULL pointer has no further representation */
973     if (!Pointer)
974         return;
975     break;
976   case RPC_FC_FP:
977     pointer_needs_sizing = !NdrFullPointerQueryPointer(
978       pStubMsg->FullPtrXlatTables, Pointer, 0, &pointer_id);
979     if (!pointer_needs_sizing)
980       return;
981     break;
982   default:
983     FIXME("unhandled ptr type=%02x\n", type);
984     RpcRaiseException(RPC_X_BAD_STUB_DATA);
985     return;
986   }
987 
988   if (attr & RPC_FC_P_DEREF) {
989     Pointer = *(unsigned char**)Pointer;
990     TRACE("deref => %p\n", Pointer);
991   }
992 
993   m = NdrBufferSizer[*desc & NDR_TABLE_MASK];
994   if (m) m(pStubMsg, Pointer, desc);
995   else FIXME("no buffersizer for data type=%02x\n", *desc);
996 }
997 
998 /***********************************************************************
999  *           PointerMemorySize [internal]
1000  */
1001 static unsigned long PointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
1002                                        unsigned char *Buffer,
1003                                        PFORMAT_STRING pFormat)
1004 {
1005   unsigned type = pFormat[0], attr = pFormat[1];
1006   PFORMAT_STRING desc;
1007   NDR_MEMORYSIZE m;
1008   DWORD pointer_id = 0;
1009   int pointer_needs_sizing;
1010 
1011   TRACE("(%p,%p,%p)\n", pStubMsg, Buffer, pFormat);
1012   TRACE("type=0x%x, attr=", type); dump_pointer_attr(attr);
1013   pFormat += 2;
1014   if (attr & RPC_FC_P_SIMPLEPOINTER) desc = pFormat;
1015   else desc = pFormat + *(const SHORT*)pFormat;
1016 
1017   switch (type) {
1018   case RPC_FC_RP: /* ref pointer (always non-null) */
1019     pointer_needs_sizing = 1;
1020     break;
1021   case RPC_FC_UP: /* unique pointer */
1022   case RPC_FC_OP: /* object pointer - we must free data before overwriting it */
1023     pointer_id = NDR_LOCAL_UINT32_READ(Buffer);
1024     TRACE("pointer_id is 0x%08x\n", pointer_id);
1025     if (pointer_id)
1026       pointer_needs_sizing = 1;
1027     else
1028       pointer_needs_sizing = 0;
1029     break;
1030   case RPC_FC_FP:
1031   {
1032     void *pointer;
1033     pointer_id = NDR_LOCAL_UINT32_READ(Buffer);
1034     TRACE("pointer_id is 0x%08x\n", pointer_id);
1035     pointer_needs_sizing = !NdrFullPointerQueryRefId(
1036       pStubMsg->FullPtrXlatTables, pointer_id, 1, &pointer);
1037     break;
1038   }
1039   default:
1040     FIXME("unhandled ptr type=%02x\n", type);
1041     RpcRaiseException(RPC_X_BAD_STUB_DATA);
1042     return 0;
1043   }
1044 
1045   if (attr & RPC_FC_P_DEREF) {
1046     TRACE("deref\n");
1047   }
1048 
1049   if (pointer_needs_sizing) {
1050     m = NdrMemorySizer[*desc & NDR_TABLE_MASK];
1051     if (m) m(pStubMsg, desc);
1052     else FIXME("no memorysizer for data type=%02x\n", *desc);
1053   }
1054 
1055   return pStubMsg->MemorySize;
1056 }
1057 
1058 /***********************************************************************
1059  *           PointerFree [internal]
1060  */
1061 static void PointerFree(PMIDL_STUB_MESSAGE pStubMsg,
1062                         unsigned char *Pointer,
1063                         PFORMAT_STRING pFormat)
1064 {
1065   unsigned type = pFormat[0], attr = pFormat[1];
1066   PFORMAT_STRING desc;
1067   NDR_FREE m;
1068   unsigned char *current_pointer = Pointer;
1069 
1070   TRACE("(%p,%p,%p)\n", pStubMsg, Pointer, pFormat);
1071   TRACE("type=0x%x, attr=", type); dump_pointer_attr(attr);
1072   if (attr & RPC_FC_P_DONTFREE) return;
1073   pFormat += 2;
1074   if (attr & RPC_FC_P_SIMPLEPOINTER) desc = pFormat;
1075   else desc = pFormat + *(const SHORT*)pFormat;
1076 
1077   if (!Pointer) return;
1078 
1079   if (type == RPC_FC_FP) {
1080     int pointer_needs_freeing = NdrFullPointerFree(
1081       pStubMsg->FullPtrXlatTables, Pointer);
1082     if (!pointer_needs_freeing)
1083       return;
1084   }
1085 
1086   if (attr & RPC_FC_P_DEREF) {
1087     current_pointer = *(unsigned char**)Pointer;
1088     TRACE("deref => %p\n", current_pointer);
1089   }
1090 
1091   m = NdrFreer[*desc & NDR_TABLE_MASK];
1092   if (m) m(pStubMsg, current_pointer, desc);
1093 
1094   /* this check stops us from trying to free buffer memory. we don't have to
1095    * worry about clients, since they won't call this function.
1096    * we don't have to check for the buffer being reallocated because
1097    * BufferStart and BufferEnd won't be reset when allocating memory for
1098    * sending the response. we don't have to check for the new buffer here as
1099    * it won't be used a type memory, only for buffer memory */
1100   if (Pointer >= pStubMsg->BufferStart && Pointer < pStubMsg->BufferEnd)
1101       goto notfree;
1102 
1103   if (attr & RPC_FC_P_ONSTACK) {
1104     TRACE("not freeing stack ptr %p\n", Pointer);
1105     return;
1106   }
1107   TRACE("freeing %p\n", Pointer);
1108   NdrFree(pStubMsg, Pointer);
1109   return;
1110 notfree:
1111   TRACE("not freeing %p\n", Pointer);
1112 }
1113 
1114 /***********************************************************************
1115  *           EmbeddedPointerMarshall
1116  */
1117 static unsigned char * EmbeddedPointerMarshall(PMIDL_STUB_MESSAGE pStubMsg,
1118                                                unsigned char *pMemory,
1119                                                PFORMAT_STRING pFormat)
1120 {
1121   unsigned char *Mark = pStubMsg->BufferMark;
1122   unsigned rep, count, stride;
1123   unsigned i;
1124   unsigned char *saved_buffer = NULL;
1125 
1126   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
1127 
1128   if (*pFormat != RPC_FC_PP) return NULL;
1129   pFormat += 2;
1130 
1131   if (pStubMsg->PointerBufferMark)
1132   {
1133     saved_buffer = pStubMsg->Buffer;
1134     pStubMsg->Buffer = pStubMsg->PointerBufferMark;
1135     pStubMsg->PointerBufferMark = NULL;
1136   }
1137 
1138   while (pFormat[0] != RPC_FC_END) {
1139     switch (pFormat[0]) {
1140     default:
1141       FIXME("unknown repeat type %d\n", pFormat[0]);
1142     case RPC_FC_NO_REPEAT:
1143       rep = 1;
1144       stride = 0;
1145       count = 1;
1146       pFormat += 2;
1147       break;
1148     case RPC_FC_FIXED_REPEAT:
1149       rep = *(const WORD*)&pFormat[2];
1150       stride = *(const WORD*)&pFormat[4];
1151       count = *(const WORD*)&pFormat[8];
1152       pFormat += 10;
1153       break;
1154     case RPC_FC_VARIABLE_REPEAT:
1155       rep = (pFormat[1] == RPC_FC_VARIABLE_OFFSET) ? pStubMsg->ActualCount : pStubMsg->MaxCount;
1156       stride = *(const WORD*)&pFormat[2];
1157       count = *(const WORD*)&pFormat[6];
1158       pFormat += 8;
1159       break;
1160     }
1161     for (i = 0; i < rep; i++) {
1162       PFORMAT_STRING info = pFormat;
1163       unsigned char *membase = pMemory + (i * stride);
1164       unsigned char *bufbase = Mark + (i * stride);
1165       unsigned u;
1166 
1167       for (u=0; u<count; u++,info+=8) {
1168         unsigned char *memptr = membase + *(const SHORT*)&info[0];
1169         unsigned char *bufptr = bufbase + *(const SHORT*)&info[2];
1170         unsigned char *saved_memory = pStubMsg->Memory;
1171 
1172         pStubMsg->Memory = pMemory;
1173         PointerMarshall(pStubMsg, bufptr, *(unsigned char**)memptr, info+4);
1174         pStubMsg->Memory = saved_memory;
1175       }
1176     }
1177     pFormat += 8 * count;
1178   }
1179 
1180   if (saved_buffer)
1181   {
1182     pStubMsg->PointerBufferMark = pStubMsg->Buffer;
1183     pStubMsg->Buffer = saved_buffer;
1184   }
1185 
1186   STD_OVERFLOW_CHECK(pStubMsg);
1187 
1188   return NULL;
1189 }
1190 
1191 /***********************************************************************
1192  *           EmbeddedPointerUnmarshall
1193  */
1194 static unsigned char * EmbeddedPointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
1195                                                  unsigned char *pDstBuffer,
1196                                                  unsigned char *pSrcMemoryPtrs,
1197                                                  PFORMAT_STRING pFormat,
1198                                                  unsigned char fMustAlloc)
1199 {
1200   unsigned char *Mark = pStubMsg->BufferMark;
1201   unsigned rep, count, stride;
1202   unsigned i;
1203   unsigned char *saved_buffer = NULL;
1204 
1205   TRACE("(%p,%p,%p,%p,%d)\n", pStubMsg, pDstBuffer, pSrcMemoryPtrs, pFormat, fMustAlloc);
1206 
1207   if (*pFormat != RPC_FC_PP) return NULL;
1208   pFormat += 2;
1209 
1210   if (pStubMsg->PointerBufferMark)
1211   {
1212     saved_buffer = pStubMsg->Buffer;
1213     pStubMsg->Buffer = pStubMsg->PointerBufferMark;
1214     pStubMsg->PointerBufferMark = NULL;
1215   }
1216 
1217   while (pFormat[0] != RPC_FC_END) {
1218     TRACE("pFormat[0] = 0x%x\n", pFormat[0]);
1219     switch (pFormat[0]) {
1220     default:
1221       FIXME("unknown repeat type %d\n", pFormat[0]);
1222     case RPC_FC_NO_REPEAT:
1223       rep = 1;
1224       stride = 0;
1225       count = 1;
1226       pFormat += 2;
1227       break;
1228     case RPC_FC_FIXED_REPEAT:
1229       rep = *(const WORD*)&pFormat[2];
1230       stride = *(const WORD*)&pFormat[4];
1231       count = *(const WORD*)&pFormat[8];
1232       pFormat += 10;
1233       break;
1234     case RPC_FC_VARIABLE_REPEAT:
1235       rep = (pFormat[1] == RPC_FC_VARIABLE_OFFSET) ? pStubMsg->ActualCount : pStubMsg->MaxCount;
1236       stride = *(const WORD*)&pFormat[2];
1237       count = *(const WORD*)&pFormat[6];
1238       pFormat += 8;
1239       break;
1240     }
1241     for (i = 0; i < rep; i++) {
1242       PFORMAT_STRING info = pFormat;
1243       unsigned char *bufdstbase = pDstBuffer + (i * stride);
1244       unsigned char *memsrcbase = pSrcMemoryPtrs + (i * stride);
1245       unsigned char *bufbase = Mark + (i * stride);
1246       unsigned u;
1247 
1248       for (u=0; u<count; u++,info+=8) {
1249         unsigned char **bufdstptr = (unsigned char **)(bufdstbase + *(const SHORT*)&info[2]);
1250         unsigned char **memsrcptr = (unsigned char **)(memsrcbase + *(const SHORT*)&info[0]);
1251         unsigned char *bufptr = bufbase + *(const SHORT*)&info[2];
1252         PointerUnmarshall(pStubMsg, bufptr, bufdstptr, *memsrcptr, info+4, fMustAlloc);
1253       }
1254     }
1255     pFormat += 8 * count;
1256   }
1257 
1258   if (saved_buffer)
1259   {
1260     pStubMsg->PointerBufferMark = pStubMsg->Buffer;
1261     pStubMsg->Buffer = saved_buffer;
1262   }
1263 
1264   return NULL;
1265 }
1266 
1267 /***********************************************************************
1268  *           EmbeddedPointerBufferSize
1269  */
1270 static void EmbeddedPointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
1271                                       unsigned char *pMemory,
1272                                       PFORMAT_STRING pFormat)
1273 {
1274   unsigned rep, count, stride;
1275   unsigned i;
1276   ULONG saved_buffer_length = 0;
1277 
1278   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
1279 
1280   if (pStubMsg->IgnoreEmbeddedPointers) return;
1281 
1282   if (*pFormat != RPC_FC_PP) return;
1283   pFormat += 2;
1284 
1285   if (pStubMsg->PointerLength)
1286   {
1287     saved_buffer_length = pStubMsg->BufferLength;
1288     pStubMsg->BufferLength = pStubMsg->PointerLength;
1289     pStubMsg->PointerLength = 0;
1290   }
1291 
1292   while (pFormat[0] != RPC_FC_END) {
1293     switch (pFormat[0]) {
1294     default:
1295       FIXME("unknown repeat type %d\n", pFormat[0]);
1296     case RPC_FC_NO_REPEAT:
1297       rep = 1;
1298       stride = 0;
1299       count = 1;
1300       pFormat += 2;
1301       break;
1302     case RPC_FC_FIXED_REPEAT:
1303       rep = *(const WORD*)&pFormat[2];
1304       stride = *(const WORD*)&pFormat[4];
1305       count = *(const WORD*)&pFormat[8];
1306       pFormat += 10;
1307       break;
1308     case RPC_FC_VARIABLE_REPEAT:
1309       rep = (pFormat[1] == RPC_FC_VARIABLE_OFFSET) ? pStubMsg->ActualCount : pStubMsg->MaxCount;
1310       stride = *(const WORD*)&pFormat[2];
1311       count = *(const WORD*)&pFormat[6];
1312       pFormat += 8;
1313       break;
1314     }
1315     for (i = 0; i < rep; i++) {
1316       PFORMAT_STRING info = pFormat;
1317       unsigned char *membase = pMemory + (i * stride);
1318       unsigned u;
1319 
1320       for (u=0; u<count; u++,info+=8) {
1321         unsigned char *memptr = membase + *(const SHORT*)&info[0];
1322         unsigned char *saved_memory = pStubMsg->Memory;
1323 
1324         pStubMsg->Memory = pMemory;
1325         PointerBufferSize(pStubMsg, *(unsigned char**)memptr, info+4);
1326         pStubMsg->Memory = saved_memory;
1327       }
1328     }
1329     pFormat += 8 * count;
1330   }
1331 
1332   if (saved_buffer_length)
1333   {
1334     pStubMsg->PointerLength = pStubMsg->BufferLength;
1335     pStubMsg->BufferLength = saved_buffer_length;
1336   }
1337 }
1338 
1339 /***********************************************************************
1340  *           EmbeddedPointerMemorySize [internal]
1341  */
1342 static unsigned long EmbeddedPointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
1343                                                PFORMAT_STRING pFormat)
1344 {
1345   unsigned char *Mark = pStubMsg->BufferMark;
1346   unsigned rep, count, stride;
1347   unsigned i;
1348   unsigned char *saved_buffer = NULL;
1349 
1350   TRACE("(%p,%p)\n", pStubMsg, pFormat);
1351 
1352   if (pStubMsg->IgnoreEmbeddedPointers) return 0;
1353 
1354   if (pStubMsg->PointerBufferMark)
1355   {
1356     saved_buffer = pStubMsg->Buffer;
1357     pStubMsg->Buffer = pStubMsg->PointerBufferMark;
1358     pStubMsg->PointerBufferMark = NULL;
1359   }
1360 
1361   if (*pFormat != RPC_FC_PP) return 0;
1362   pFormat += 2;
1363 
1364   while (pFormat[0] != RPC_FC_END) {
1365     switch (pFormat[0]) {
1366     default:
1367       FIXME("unknown repeat type %d\n", pFormat[0]);
1368     case RPC_FC_NO_REPEAT:
1369       rep = 1;
1370       stride = 0;
1371       count = 1;
1372       pFormat += 2;
1373       break;
1374     case RPC_FC_FIXED_REPEAT:
1375       rep = *(const WORD*)&pFormat[2];
1376       stride = *(const WORD*)&pFormat[4];
1377       count = *(const WORD*)&pFormat[8];
1378       pFormat += 10;
1379       break;
1380     case RPC_FC_VARIABLE_REPEAT:
1381       rep = (pFormat[1] == RPC_FC_VARIABLE_OFFSET) ? pStubMsg->ActualCount : pStubMsg->MaxCount;
1382       stride = *(const WORD*)&pFormat[2];
1383       count = *(const WORD*)&pFormat[6];
1384       pFormat += 8;
1385       break;
1386     }
1387     for (i = 0; i < rep; i++) {
1388       PFORMAT_STRING info = pFormat;
1389       unsigned char *bufbase = Mark + (i * stride);
1390       unsigned u;
1391       for (u=0; u<count; u++,info+=8) {
1392         unsigned char *bufptr = bufbase + *(const SHORT*)&info[2];
1393         PointerMemorySize(pStubMsg, bufptr, info+4);
1394       }
1395     }
1396     pFormat += 8 * count;
1397   }
1398 
1399   if (saved_buffer)
1400   {
1401     pStubMsg->PointerBufferMark = pStubMsg->Buffer;
1402     pStubMsg->Buffer = saved_buffer;
1403   }
1404 
1405   return 0;
1406 }
1407 
1408 /***********************************************************************
1409  *           EmbeddedPointerFree [internal]
1410  */
1411 static void EmbeddedPointerFree(PMIDL_STUB_MESSAGE pStubMsg,
1412                                 unsigned char *pMemory,
1413                                 PFORMAT_STRING pFormat)
1414 {
1415   unsigned rep, count, stride;
1416   unsigned i;
1417 
1418   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
1419   if (*pFormat != RPC_FC_PP) return;
1420   pFormat += 2;
1421 
1422   while (pFormat[0] != RPC_FC_END) {
1423     switch (pFormat[0]) {
1424     default:
1425       FIXME("unknown repeat type %d\n", pFormat[0]);
1426     case RPC_FC_NO_REPEAT:
1427       rep = 1;
1428       stride = 0;
1429       count = 1;
1430       pFormat += 2;
1431       break;
1432     case RPC_FC_FIXED_REPEAT:
1433       rep = *(const WORD*)&pFormat[2];
1434       stride = *(const WORD*)&pFormat[4];
1435       count = *(const WORD*)&pFormat[8];
1436       pFormat += 10;
1437       break;
1438     case RPC_FC_VARIABLE_REPEAT:
1439       rep = (pFormat[1] == RPC_FC_VARIABLE_OFFSET) ? pStubMsg->ActualCount : pStubMsg->MaxCount;
1440       stride = *(const WORD*)&pFormat[2];
1441       count = *(const WORD*)&pFormat[6];
1442       pFormat += 8;
1443       break;
1444     }
1445     for (i = 0; i < rep; i++) {
1446       PFORMAT_STRING info = pFormat;
1447       unsigned char *membase = pMemory + (i * stride);
1448       unsigned u;
1449 
1450       for (u=0; u<count; u++,info+=8) {
1451         unsigned char *memptr = membase + *(const SHORT*)&info[0];
1452         unsigned char *saved_memory = pStubMsg->Memory;
1453 
1454         pStubMsg->Memory = pMemory;
1455         PointerFree(pStubMsg, *(unsigned char**)memptr, info+4);
1456         pStubMsg->Memory = saved_memory;
1457       }
1458     }
1459     pFormat += 8 * count;
1460   }
1461 }
1462 
1463 /***********************************************************************
1464  *           NdrPointerMarshall [RPCRT4.@]
1465  */
1466 unsigned char * WINAPI NdrPointerMarshall(PMIDL_STUB_MESSAGE pStubMsg,
1467                                           unsigned char *pMemory,
1468                                           PFORMAT_STRING pFormat)
1469 {
1470   unsigned char *Buffer;
1471 
1472   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
1473 
1474   /* Increment the buffer here instead of in PointerMarshall,
1475    * as that is used by embedded pointers which already handle the incrementing
1476    * the buffer, and shouldn't write any additional pointer data to the wire */
1477   if (*pFormat != RPC_FC_RP)
1478   {
1479     ALIGN_POINTER_CLEAR(pStubMsg->Buffer, 4);
1480     Buffer = pStubMsg->Buffer;
1481     safe_buffer_increment(pStubMsg, 4);
1482   }
1483   else
1484     Buffer = pStubMsg->Buffer;
1485 
1486   PointerMarshall(pStubMsg, Buffer, pMemory, pFormat);
1487 
1488   return NULL;
1489 }
1490 
1491 /***********************************************************************
1492  *           NdrPointerUnmarshall [RPCRT4.@]
1493  */
1494 unsigned char * WINAPI NdrPointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
1495                                             unsigned char **ppMemory,
1496                                             PFORMAT_STRING pFormat,
1497                                             unsigned char fMustAlloc)
1498 {
1499   unsigned char *Buffer;
1500 
1501   TRACE("(%p,%p,%p,%d)\n", pStubMsg, ppMemory, pFormat, fMustAlloc);
1502 
1503   /* Increment the buffer here instead of in PointerUnmarshall,
1504    * as that is used by embedded pointers which already handle the incrementing
1505    * the buffer, and shouldn't read any additional pointer data from the
1506    * buffer */
1507   if (*pFormat != RPC_FC_RP)
1508   {
1509     ALIGN_POINTER(pStubMsg->Buffer, 4);
1510     Buffer = pStubMsg->Buffer;
1511     safe_buffer_increment(pStubMsg, 4);
1512   }
1513   else
1514     Buffer = pStubMsg->Buffer;
1515 
1516   PointerUnmarshall(pStubMsg, Buffer, ppMemory, *ppMemory, pFormat, fMustAlloc);
1517 
1518   return NULL;
1519 }
1520 
1521 /***********************************************************************
1522  *           NdrPointerBufferSize [RPCRT4.@]
1523  */
1524 void WINAPI NdrPointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
1525                                       unsigned char *pMemory,
1526                                       PFORMAT_STRING pFormat)
1527 {
1528   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
1529 
1530   /* Increment the buffer length here instead of in PointerBufferSize,
1531    * as that is used by embedded pointers which already handle the buffer
1532    * length, and shouldn't write anything more to the wire */
1533   if (*pFormat != RPC_FC_RP)
1534   {
1535     ALIGN_LENGTH(pStubMsg->BufferLength, 4);
1536     safe_buffer_length_increment(pStubMsg, 4);
1537   }
1538 
1539   PointerBufferSize(pStubMsg, pMemory, pFormat);
1540 }
1541 
1542 /***********************************************************************
1543  *           NdrPointerMemorySize [RPCRT4.@]
1544  */
1545 ULONG WINAPI NdrPointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
1546                                   PFORMAT_STRING pFormat)
1547 {
1548   /* unsigned size = *(LPWORD)(pFormat+2); */
1549   FIXME("(%p,%p): stub\n", pStubMsg, pFormat);
1550   PointerMemorySize(pStubMsg, pStubMsg->Buffer, pFormat);
1551   return 0;
1552 }
1553 
1554 /***********************************************************************
1555  *           NdrPointerFree [RPCRT4.@]
1556  */
1557 void WINAPI NdrPointerFree(PMIDL_STUB_MESSAGE pStubMsg,
1558                            unsigned char *pMemory,
1559                            PFORMAT_STRING pFormat)
1560 {
1561   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
1562   PointerFree(pStubMsg, pMemory, pFormat);
1563 }
1564 
1565 /***********************************************************************
1566  *           NdrSimpleTypeMarshall [RPCRT4.@]
1567  */
1568 void WINAPI NdrSimpleTypeMarshall( PMIDL_STUB_MESSAGE pStubMsg, unsigned char* pMemory,
1569                                    unsigned char FormatChar )
1570 {
1571     NdrBaseTypeMarshall(pStubMsg, pMemory, &FormatChar);
1572 }
1573 
1574 /***********************************************************************
1575  *           NdrSimpleTypeUnmarshall [RPCRT4.@]
1576  *
1577  * Unmarshall a base type.
1578  *
1579  * NOTES
1580  *  Doesn't check that the buffer is long enough before copying, so the caller
1581  * should do this.
1582  */
1583 void WINAPI NdrSimpleTypeUnmarshall( PMIDL_STUB_MESSAGE pStubMsg, unsigned char* pMemory,
1584                                      unsigned char FormatChar )
1585 {
1586 #define BASE_TYPE_UNMARSHALL(type) \
1587         ALIGN_POINTER(pStubMsg->Buffer, sizeof(type)); \
1588         TRACE("pMemory: %p\n", pMemory); \
1589         *(type *)pMemory = *(type *)pStubMsg->Buffer; \
1590         pStubMsg->Buffer += sizeof(type);
1591 
1592     switch(FormatChar)
1593     {
1594     case RPC_FC_BYTE:
1595     case RPC_FC_CHAR:
1596     case RPC_FC_SMALL:
1597     case RPC_FC_USMALL:
1598         BASE_TYPE_UNMARSHALL(UCHAR);
1599         TRACE("value: 0x%02x\n", *pMemory);
1600         break;
1601     case RPC_FC_WCHAR:
1602     case RPC_FC_SHORT:
1603     case RPC_FC_USHORT:
1604         BASE_TYPE_UNMARSHALL(USHORT);
1605         TRACE("value: 0x%04x\n", *(USHORT *)pMemory);
1606         break;
1607     case RPC_FC_LONG:
1608     case RPC_FC_ULONG:
1609     case RPC_FC_ERROR_STATUS_T:
1610     case RPC_FC_ENUM32:
1611         BASE_TYPE_UNMARSHALL(ULONG);
1612         TRACE("value: 0x%08x\n", *(ULONG *)pMemory);
1613         break;
1614    case RPC_FC_FLOAT:
1615         BASE_TYPE_UNMARSHALL(float);
1616         TRACE("value: %f\n", *(float *)pMemory);
1617         break;
1618     case RPC_FC_DOUBLE:
1619         BASE_TYPE_UNMARSHALL(double);
1620         TRACE("value: %f\n", *(double *)pMemory);
1621         break;
1622     case RPC_FC_HYPER:
1623         BASE_TYPE_UNMARSHALL(ULONGLONG);
1624         TRACE("value: %s\n", wine_dbgstr_longlong(*(ULONGLONG *)pMemory));
1625         break;
1626     case RPC_FC_ENUM16:
1627         ALIGN_POINTER(pStubMsg->Buffer, sizeof(USHORT));
1628         TRACE("pMemory: %p\n", pMemory);
1629         /* 16-bits on the wire, but int in memory */
1630         *(UINT *)pMemory = *(USHORT *)pStubMsg->Buffer;
1631         pStubMsg->Buffer += sizeof(USHORT);
1632         TRACE("value: 0x%08x\n", *(UINT *)pMemory);
1633         break;
1634     case RPC_FC_IGNORE:
1635         break;
1636     default:
1637         FIXME("Unhandled base type: 0x%02x\n", FormatChar);
1638     }
1639 #undef BASE_TYPE_UNMARSHALL
1640 }
1641 
1642 /***********************************************************************
1643  *           NdrSimpleStructMarshall [RPCRT4.@]
1644  */
1645 unsigned char * WINAPI NdrSimpleStructMarshall(PMIDL_STUB_MESSAGE pStubMsg,
1646                                                unsigned char *pMemory,
1647                                                PFORMAT_STRING pFormat)
1648 {
1649   unsigned size = *(const WORD*)(pFormat+2);
1650   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
1651 
1652   ALIGN_POINTER_CLEAR(pStubMsg->Buffer, pFormat[1] + 1);
1653 
1654   pStubMsg->BufferMark = pStubMsg->Buffer;
1655   safe_copy_to_buffer(pStubMsg, pMemory, size);
1656 
1657   if (pFormat[0] != RPC_FC_STRUCT)
1658     EmbeddedPointerMarshall(pStubMsg, pMemory, pFormat+4);
1659 
1660   return NULL;
1661 }
1662 
1663 /***********************************************************************
1664  *           NdrSimpleStructUnmarshall [RPCRT4.@]
1665  */
1666 unsigned char * WINAPI NdrSimpleStructUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
1667                                                  unsigned char **ppMemory,
1668                                                  PFORMAT_STRING pFormat,
1669                                                  unsigned char fMustAlloc)
1670 {
1671   unsigned size = *(const WORD*)(pFormat+2);
1672   unsigned char *saved_buffer;
1673   TRACE("(%p,%p,%p,%d)\n", pStubMsg, ppMemory, pFormat, fMustAlloc);
1674 
1675   ALIGN_POINTER(pStubMsg->Buffer, pFormat[1] + 1);
1676 
1677   if (fMustAlloc)
1678     *ppMemory = NdrAllocate(pStubMsg, size);
1679   else
1680   {
1681     if (!pStubMsg->IsClient && !*ppMemory)
1682       /* for servers, we just point straight into the RPC buffer */
1683       *ppMemory = pStubMsg->Buffer;
1684   }
1685 
1686   saved_buffer = pStubMsg->BufferMark = pStubMsg->Buffer;
1687   safe_buffer_increment(pStubMsg, size);
1688   if (pFormat[0] == RPC_FC_PSTRUCT)
1689       EmbeddedPointerUnmarshall(pStubMsg, saved_buffer, *ppMemory, pFormat+4, fMustAlloc);
1690 
1691   TRACE("copying %p to %p\n", saved_buffer, *ppMemory);
1692   if (*ppMemory != saved_buffer)
1693       memcpy(*ppMemory, saved_buffer, size);
1694 
1695   return NULL;
1696 }
1697 
1698 /***********************************************************************
1699  *           NdrSimpleStructBufferSize [RPCRT4.@]
1700  */
1701 void WINAPI NdrSimpleStructBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
1702                                       unsigned char *pMemory,
1703                                       PFORMAT_STRING pFormat)
1704 {
1705   unsigned size = *(const WORD*)(pFormat+2);
1706   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
1707 
1708   ALIGN_LENGTH(pStubMsg->BufferLength, pFormat[1] + 1);
1709 
1710   safe_buffer_length_increment(pStubMsg, size);
1711   if (pFormat[0] != RPC_FC_STRUCT)
1712     EmbeddedPointerBufferSize(pStubMsg, pMemory, pFormat+4);
1713 }
1714 
1715 /***********************************************************************
1716  *           NdrSimpleStructMemorySize [RPCRT4.@]
1717  */
1718 ULONG WINAPI NdrSimpleStructMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
1719                                        PFORMAT_STRING pFormat)
1720 {
1721   unsigned short size = *(const WORD *)(pFormat+2);
1722 
1723   TRACE("(%p,%p)\n", pStubMsg, pFormat);
1724 
1725   ALIGN_POINTER(pStubMsg->Buffer, pFormat[1] + 1);
1726   pStubMsg->MemorySize += size;
1727   safe_buffer_increment(pStubMsg, size);
1728 
1729   if (pFormat[0] != RPC_FC_STRUCT)
1730     EmbeddedPointerMemorySize(pStubMsg, pFormat+4);
1731   return pStubMsg->MemorySize;
1732 }
1733 
1734 /***********************************************************************
1735  *           NdrSimpleStructFree [RPCRT4.@]
1736  */
1737 void WINAPI NdrSimpleStructFree(PMIDL_STUB_MESSAGE pStubMsg,
1738                                 unsigned char *pMemory,
1739                                 PFORMAT_STRING pFormat)
1740 {
1741   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
1742   if (pFormat[0] != RPC_FC_STRUCT)
1743     EmbeddedPointerFree(pStubMsg, pMemory, pFormat+4);
1744 }
1745 
1746 /* Array helpers */
1747 
1748 static inline void array_compute_and_size_conformance(
1749     unsigned char fc, PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
1750     PFORMAT_STRING pFormat)
1751 {
1752   switch (fc)
1753   {
1754   case RPC_FC_CARRAY:
1755     ComputeConformance(pStubMsg, pMemory, pFormat+4, 0);
1756     SizeConformance(pStubMsg);
1757     break;
1758   case RPC_FC_CVARRAY:
1759     pFormat = ComputeConformance(pStubMsg, pMemory, pFormat + 4, 0);
1760     pFormat = ComputeVariance(pStubMsg, pMemory, pFormat, 0);
1761     SizeConformance(pStubMsg);
1762     break;
1763   case RPC_FC_C_CSTRING:
1764   case RPC_FC_C_WSTRING:
1765     if (pFormat[0] == RPC_FC_C_CSTRING)
1766     {
1767       TRACE("string=%s\n", debugstr_a((const char *)pMemory));
1768       pStubMsg->ActualCount = strlen((const char *)pMemory)+1;
1769     }
1770     else
1771     {
1772       TRACE("string=%s\n", debugstr_w((LPCWSTR)pMemory));
1773       pStubMsg->ActualCount = strlenW((LPCWSTR)pMemory)+1;
1774     }
1775 
1776     if (fc == RPC_FC_STRING_SIZED)
1777       pFormat = ComputeConformance(pStubMsg, pMemory, pFormat + 2, 0);
1778     else
1779       pStubMsg->MaxCount = pStubMsg->ActualCount;
1780 
1781     SizeConformance(pStubMsg);
1782     break;
1783   default:
1784     ERR("unknown array format 0x%x\n", fc);
1785     RpcRaiseException(RPC_X_BAD_STUB_DATA);
1786   }
1787 }
1788 
1789 static inline void array_buffer_size(
1790     unsigned char fc, PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
1791     PFORMAT_STRING pFormat, unsigned char fHasPointers)
1792 {
1793   DWORD size;
1794   DWORD esize;
1795   unsigned char alignment;
1796 
1797   switch (fc)
1798   {
1799   case RPC_FC_CARRAY:
1800     esize = *(const WORD*)(pFormat+2);
1801     alignment = pFormat[1] + 1;
1802 
1803     pFormat = SkipConformance(pStubMsg, pFormat + 4);
1804 
1805     ALIGN_LENGTH(pStubMsg->BufferLength, alignment);
1806 
1807     size = safe_multiply(esize, pStubMsg->MaxCount);
1808     /* conformance value plus array */
1809     safe_buffer_length_increment(pStubMsg, size);
1810 
1811     if (fHasPointers)
1812       EmbeddedPointerBufferSize(pStubMsg, pMemory, pFormat);
1813     break;
1814   case RPC_FC_CVARRAY:
1815     esize = *(const WORD*)(pFormat+2);
1816     alignment = pFormat[1] + 1;
1817 
1818     pFormat = SkipConformance(pStubMsg, pFormat + 4);
1819     pFormat = SkipConformance(pStubMsg, pFormat);
1820 
1821     SizeVariance(pStubMsg);
1822 
1823     ALIGN_LENGTH(pStubMsg->BufferLength, alignment);
1824 
1825     size = safe_multiply(esize, pStubMsg->ActualCount);
1826     safe_buffer_length_increment(pStubMsg, size);
1827 
1828     if (fHasPointers)
1829       EmbeddedPointerBufferSize(pStubMsg, pMemory, pFormat);
1830     break;
1831   case RPC_FC_C_CSTRING:
1832   case RPC_FC_C_WSTRING:
1833     if (fc == RPC_FC_C_CSTRING)
1834       esize = 1;
1835     else
1836       esize = 2;
1837 
1838     SizeVariance(pStubMsg);
1839 
1840     size = safe_multiply(esize, pStubMsg->ActualCount);
1841     safe_buffer_length_increment(pStubMsg, size);
1842     break;
1843   default:
1844     ERR("unknown array format 0x%x\n", fc);
1845     RpcRaiseException(RPC_X_BAD_STUB_DATA);
1846   }
1847 }
1848 
1849 static inline void array_compute_and_write_conformance(
1850     unsigned char fc, PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
1851     PFORMAT_STRING pFormat)
1852 {
1853   switch (fc)
1854   {
1855   case RPC_FC_CARRAY:
1856     ComputeConformance(pStubMsg, pMemory, pFormat+4, 0);
1857     WriteConformance(pStubMsg);
1858     break;
1859   case RPC_FC_CVARRAY:
1860     pFormat = ComputeConformance(pStubMsg, pMemory, pFormat + 4, 0);
1861     pFormat = ComputeVariance(pStubMsg, pMemory, pFormat, 0);
1862     WriteConformance(pStubMsg);
1863     break;
1864   case RPC_FC_C_CSTRING:
1865   case RPC_FC_C_WSTRING:
1866     if (fc == RPC_FC_C_CSTRING)
1867     {
1868       TRACE("string=%s\n", debugstr_a((const char *)pMemory));
1869       pStubMsg->ActualCount = strlen((const char *)pMemory)+1;
1870     }
1871     else
1872     {
1873       TRACE("string=%s\n", debugstr_w((LPCWSTR)pMemory));
1874       pStubMsg->ActualCount = strlenW((LPCWSTR)pMemory)+1;
1875     }
1876     if (pFormat[1] == RPC_FC_STRING_SIZED)
1877       pFormat = ComputeConformance(pStubMsg, pMemory, pFormat + 2, 0);
1878     else
1879       pStubMsg->MaxCount = pStubMsg->ActualCount;
1880     pStubMsg->Offset = 0;
1881     WriteConformance(pStubMsg);
1882     break;
1883   default:
1884     ERR("unknown array format 0x%x\n", fc);
1885     RpcRaiseException(RPC_X_BAD_STUB_DATA);
1886   }
1887 }
1888 
1889 static inline void array_write_variance_and_marshall(
1890     unsigned char fc, PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory,
1891     PFORMAT_STRING pFormat, unsigned char fHasPointers)
1892 {
1893   DWORD size;
1894   DWORD esize;
1895   unsigned char alignment;
1896 
1897   switch (fc)
1898   {
1899   case RPC_FC_CARRAY:
1900     esize = *(const WORD*)(pFormat+2);
1901     alignment = pFormat[1] + 1;
1902 
1903     pFormat = SkipConformance(pStubMsg, pFormat + 4);
1904 
1905     ALIGN_POINTER_CLEAR(pStubMsg->Buffer, alignment);
1906 
1907     size = safe_multiply(esize, pStubMsg->MaxCount);
1908     if (fHasPointers)
1909       pStubMsg->BufferMark = pStubMsg->Buffer;
1910     safe_copy_to_buffer(pStubMsg, pMemory, size);
1911 
1912     if (fHasPointers)
1913       EmbeddedPointerMarshall(pStubMsg, pMemory, pFormat);
1914     break;
1915   case RPC_FC_CVARRAY:
1916     esize = *(const WORD*)(pFormat+2);
1917     alignment = pFormat[1] + 1;
1918 
1919     /* conformance */
1920     pFormat = SkipConformance(pStubMsg, pFormat + 4);
1921     /* variance */
1922     pFormat = SkipConformance(pStubMsg, pFormat);
1923 
1924     WriteVariance(pStubMsg);
1925 
1926     ALIGN_POINTER_CLEAR(pStubMsg->Buffer, alignment);
1927 
1928     size = safe_multiply(esize, pStubMsg->ActualCount);
1929 
1930     if (fHasPointers)
1931       pStubMsg->BufferMark = pStubMsg->Buffer;
1932     safe_copy_to_buffer(pStubMsg, pMemory + pStubMsg->Offset, size);
1933 
1934     if (fHasPointers)
1935       EmbeddedPointerMarshall(pStubMsg, pMemory, pFormat);
1936     break;
1937   case RPC_FC_C_CSTRING:
1938   case RPC_FC_C_WSTRING:
1939     if (fc == RPC_FC_C_CSTRING)
1940       esize = 1;
1941     else
1942       esize = 2;
1943 
1944     WriteVariance(pStubMsg);
1945 
1946     size = safe_multiply(esize, pStubMsg->ActualCount);
1947     safe_copy_to_buffer(pStubMsg, pMemory, size); /* the string itself */
1948     break;
1949   default:
1950     ERR("unknown array format 0x%x\n", fc);
1951     RpcRaiseException(RPC_X_BAD_STUB_DATA);
1952   }
1953 }
1954 
1955 static inline ULONG array_read_conformance(
1956     unsigned char fc, PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat)
1957 {
1958   DWORD esize;
1959 
1960   switch (fc)
1961   {
1962   case RPC_FC_CARRAY:
1963     esize = *(const WORD*)(pFormat+2);
1964     pFormat = ReadConformance(pStubMsg, pFormat+4);
1965     return safe_multiply(esize, pStubMsg->MaxCount);
1966   case RPC_FC_CVARRAY:
1967     esize = *(const WORD*)(pFormat+2);
1968     pFormat = ReadConformance(pStubMsg, pFormat+4);
1969     return safe_multiply(esize, pStubMsg->MaxCount);
1970   case RPC_FC_C_CSTRING:
1971   case RPC_FC_C_WSTRING:
1972     if (fc == RPC_FC_C_CSTRING)
1973       esize = 1;
1974     else
1975       esize = 2;
1976 
1977     if (pFormat[1] == RPC_FC_STRING_SIZED)
1978       ReadConformance(pStubMsg, pFormat + 2);
1979     else
1980       ReadConformance(pStubMsg, NULL);
1981     return safe_multiply(esize, pStubMsg->MaxCount);
1982   default:
1983     ERR("unknown array format 0x%x\n", fc);
1984     RpcRaiseException(RPC_X_BAD_STUB_DATA);
1985   }
1986 }
1987 
1988 static inline ULONG array_read_variance_and_unmarshall(
1989     unsigned char fc, PMIDL_STUB_MESSAGE pStubMsg, unsigned char **ppMemory,
1990     PFORMAT_STRING pFormat, unsigned char fMustAlloc,
1991     unsigned char fUseBufferMemoryServer, unsigned char fUnmarshall)
1992 {
1993   ULONG bufsize, memsize;
1994   WORD esize;
1995   unsigned char alignment;
1996   unsigned char *saved_buffer;
1997   ULONG offset;
1998 
1999   switch (fc)
2000   {
2001   case RPC_FC_CARRAY:
2002     esize = *(const WORD*)(pFormat+2);
2003     alignment = pFormat[1] + 1;
2004 
2005     bufsize = memsize = safe_multiply(esize, pStubMsg->MaxCount);
2006 
2007     pFormat = SkipConformance(pStubMsg, pFormat + 4);
2008 
2009     ALIGN_POINTER(pStubMsg->Buffer, alignment);
2010 
2011     if (fUnmarshall)
2012     {
2013       if (fMustAlloc)
2014         *ppMemory = NdrAllocate(pStubMsg, memsize);
2015       else
2016       {
2017         if (fUseBufferMemoryServer && !pStubMsg->IsClient && !*ppMemory)
2018           /* for servers, we just point straight into the RPC buffer */
2019           *ppMemory = pStubMsg->Buffer;
2020       }
2021 
2022       saved_buffer = pStubMsg->Buffer;
2023       safe_buffer_increment(pStubMsg, bufsize);
2024 
2025       pStubMsg->BufferMark = saved_buffer;
2026       EmbeddedPointerUnmarshall(pStubMsg, saved_buffer, *ppMemory, pFormat, fMustAlloc);
2027 
2028       TRACE("copying %p to %p\n", saved_buffer, *ppMemory);
2029       if (*ppMemory != saved_buffer)
2030         memcpy(*ppMemory, saved_buffer, bufsize);
2031     }
2032     return bufsize;
2033   case RPC_FC_CVARRAY:
2034     esize = *(const WORD*)(pFormat+2);
2035     alignment = pFormat[1] + 1;
2036 
2037     pFormat = SkipConformance(pStubMsg, pFormat + 4);
2038 
2039     pFormat = ReadVariance(pStubMsg, pFormat, pStubMsg->MaxCount);
2040 
2041     ALIGN_POINTER(pStubMsg->Buffer, alignment);
2042 
2043     bufsize = safe_multiply(esize, pStubMsg->ActualCount);
2044     memsize = safe_multiply(esize, pStubMsg->MaxCount);
2045 
2046     if (fUnmarshall)
2047     {
2048       offset = pStubMsg->Offset;
2049 
2050       if (!fMustAlloc && !*ppMemory)
2051         fMustAlloc = TRUE;
2052       if (fMustAlloc)
2053         *ppMemory = NdrAllocate(pStubMsg, memsize);
2054       saved_buffer = pStubMsg->Buffer;
2055       safe_buffer_increment(pStubMsg, bufsize);
2056 
2057       pStubMsg->BufferMark = saved_buffer;
2058       EmbeddedPointerUnmarshall(pStubMsg, saved_buffer, *ppMemory, pFormat,
2059                                 fMustAlloc);
2060 
2061       memcpy(*ppMemory + offset, saved_buffer, bufsize);
2062     }
2063     return bufsize;
2064   case RPC_FC_C_CSTRING:
2065   case RPC_FC_C_WSTRING:
2066     if (fc == RPC_FC_C_CSTRING)
2067       esize = 1;
2068     else
2069       esize = 2;
2070 
2071     ReadVariance(pStubMsg, NULL, pStubMsg->MaxCount);
2072 
2073     if (pFormat[1] != RPC_FC_STRING_SIZED && (pStubMsg->MaxCount != pStubMsg->ActualCount))
2074     {
2075       ERR("buffer size %d must equal memory size %ld for non-sized conformant strings\n",
2076           pStubMsg->ActualCount, pStubMsg->MaxCount);
2077       RpcRaiseException(RPC_S_INVALID_BOUND);
2078     }
2079     if (pStubMsg->Offset)
2080     {
2081       ERR("conformant strings can't have Offset (%d)\n", pStubMsg->Offset);
2082       RpcRaiseException(RPC_S_INVALID_BOUND);
2083     }
2084 
2085     memsize = safe_multiply(esize, pStubMsg->MaxCount);
2086     bufsize = safe_multiply(esize, pStubMsg->ActualCount);
2087 
2088     validate_string_data(pStubMsg, bufsize, esize);
2089 
2090     if (fUnmarshall)
2091     {
2092       if (fMustAlloc)
2093         *ppMemory = NdrAllocate(pStubMsg, memsize);
2094       else
2095       {
2096         if (fUseBufferMemoryServer && !pStubMsg->IsClient &&
2097             !*ppMemory && (pStubMsg->MaxCount == pStubMsg->ActualCount))
2098           /* if the data in the RPC buffer is big enough, we just point
2099            * straight into it */
2100           *ppMemory = pStubMsg->Buffer;
2101         else if (!*ppMemory)
2102           *ppMemory = NdrAllocate(pStubMsg, memsize);
2103       }
2104 
2105       if (*ppMemory == pStubMsg->Buffer)
2106         safe_buffer_increment(pStubMsg, bufsize);
2107       else
2108         safe_copy_from_buffer(pStubMsg, *ppMemory, bufsize);
2109 
2110       if (*pFormat == RPC_FC_C_CSTRING)
2111         TRACE("string=%s\n", debugstr_a((char*)*ppMemory));
2112       else
2113         TRACE("string=%s\n", debugstr_w((LPWSTR)*ppMemory));
2114     }
2115     return bufsize;
2116   default:
2117     ERR("unknown array format 0x%x\n", fc);
2118     RpcRaiseException(RPC_X_BAD_STUB_DATA);
2119   }
2120 }
2121 
2122 static inline void array_memory_size(
2123     unsigned char fc, PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat,
2124     unsigned char fHasPointers)
2125 {
2126   ULONG bufsize, memsize;
2127   DWORD esize;
2128   unsigned char alignment;
2129 
2130   switch (fc)
2131   {
2132   case RPC_FC_CARRAY:
2133     esize = *(const WORD*)(pFormat+2);
2134     alignment = pFormat[1] + 1;
2135 
2136     pFormat = SkipConformance(pStubMsg, pFormat + 4);
2137 
2138     bufsize = memsize = safe_multiply(esize, pStubMsg->MaxCount);
2139     pStubMsg->MemorySize += memsize;
2140 
2141     ALIGN_POINTER(pStubMsg->Buffer, alignment);
2142     if (fHasPointers)
2143       pStubMsg->BufferMark = pStubMsg->Buffer;
2144     safe_buffer_increment(pStubMsg, bufsize);
2145 
2146     if (fHasPointers)
2147       EmbeddedPointerMemorySize(pStubMsg, pFormat);
2148     break;
2149   case RPC_FC_CVARRAY:
2150     esize = *(const WORD*)(pFormat+2);
2151     alignment = pFormat[1] + 1;
2152 
2153     pFormat = SkipConformance(pStubMsg, pFormat + 4);
2154 
2155     pFormat = ReadVariance(pStubMsg, pFormat, pStubMsg->MaxCount);
2156 
2157     bufsize = safe_multiply(esize, pStubMsg->ActualCount);
2158     memsize = safe_multiply(esize, pStubMsg->MaxCount);
2159     pStubMsg->MemorySize += memsize;
2160 
2161     ALIGN_POINTER(pStubMsg->Buffer, alignment);
2162     if (fHasPointers)
2163       pStubMsg->BufferMark = pStubMsg->Buffer;
2164     safe_buffer_increment(pStubMsg, bufsize);
2165 
2166     if (fHasPointers)
2167       EmbeddedPointerMemorySize(pStubMsg, pFormat);
2168     break;
2169   case RPC_FC_C_CSTRING:
2170   case RPC_FC_C_WSTRING:
2171     if (fc == RPC_FC_C_CSTRING)
2172       esize = 1;
2173     else
2174       esize = 2;
2175 
2176     ReadVariance(pStubMsg, NULL, pStubMsg->MaxCount);
2177 
2178     if (pFormat[1] != RPC_FC_STRING_SIZED && (pStubMsg->MaxCount != pStubMsg->ActualCount))
2179     {
2180       ERR("buffer size %d must equal memory size %ld for non-sized conformant strings\n",
2181           pStubMsg->ActualCount, pStubMsg->MaxCount);
2182       RpcRaiseException(RPC_S_INVALID_BOUND);
2183     }
2184     if (pStubMsg->Offset)
2185     {
2186       ERR("conformant strings can't have Offset (%d)\n", pStubMsg->Offset);
2187       RpcRaiseException(RPC_S_INVALID_BOUND);
2188     }
2189 
2190     memsize = safe_multiply(esize, pStubMsg->MaxCount);
2191     bufsize = safe_multiply(esize, pStubMsg->ActualCount);
2192 
2193     validate_string_data(pStubMsg, bufsize, esize);
2194 
2195     safe_buffer_increment(pStubMsg, bufsize);
2196     pStubMsg->MemorySize += memsize;
2197     break;
2198   default:
2199     ERR("unknown array format 0x%x\n", fc);
2200     RpcRaiseException(RPC_X_BAD_STUB_DATA);
2201   }
2202 }
2203 
2204 static inline void array_free(
2205     unsigned char fc, PMIDL_STUB_MESSAGE pStubMsg,
2206     unsigned char *pMemory, PFORMAT_STRING pFormat, unsigned char fHasPointers)
2207 {
2208   switch (fc)
2209   {
2210   case RPC_FC_CARRAY:
2211     pFormat = ComputeConformance(pStubMsg, pMemory, pFormat+4, 0);
2212     if (fHasPointers)
2213       EmbeddedPointerFree(pStubMsg, pMemory, pFormat);
2214     break;
2215   case RPC_FC_CVARRAY:
2216     pFormat = ComputeConformance(pStubMsg, pMemory, pFormat+4, 0);
2217     pFormat = ComputeVariance(pStubMsg, pMemory, pFormat, 0);
2218     if (fHasPointers)
2219       EmbeddedPointerFree(pStubMsg, pMemory, pFormat);
2220     break;
2221   case RPC_FC_C_CSTRING:
2222   case RPC_FC_C_WSTRING:
2223     /* No embedded pointers so nothing to do */
2224     break;
2225   default:
2226     ERR("unknown array format 0x%x\n", fc);
2227     RpcRaiseException(RPC_X_BAD_STUB_DATA);
2228   }
2229 }
2230 
2231 /*
2232  * NdrConformantString:
2233  *
2234  * What MS calls a ConformantString is, in DCE terminology,
2235  * a Varying-Conformant String.
2236  * [
2237  *   maxlen: DWORD (max # of CHARTYPE characters, inclusive of '\0')
2238  *   offset: DWORD (actual string data begins at (offset) CHARTYPE's
2239  *           into unmarshalled string)
2240  *   length: DWORD (# of CHARTYPE characters, inclusive of '\0')
2241  *   [
2242  *     data: CHARTYPE[maxlen]
2243  *   ]
2244  * ], where CHARTYPE is the appropriate character type (specified externally)
2245  *
2246  */
2247 
2248 /***********************************************************************
2249  *            NdrConformantStringMarshall [RPCRT4.@]
2250  */
2251 unsigned char *WINAPI NdrConformantStringMarshall(MIDL_STUB_MESSAGE *pStubMsg,
2252   unsigned char *pszMessage, PFORMAT_STRING pFormat)
2253 {
2254   TRACE("(pStubMsg == ^%p, pszMessage == ^%p, pFormat == ^%p)\n", pStubMsg, pszMessage, pFormat);
2255 
2256   if (pFormat[0] != RPC_FC_C_CSTRING && pFormat[0] != RPC_FC_C_WSTRING) {
2257     ERR("Unhandled string type: %#x\n", pFormat[0]);
2258     RpcRaiseException(RPC_X_BAD_STUB_DATA);
2259   }
2260 
2261   /* allow compiler to optimise inline function by passing constant into
2262    * these functions */
2263   if (pFormat[0] == RPC_FC_C_CSTRING) {
2264     array_compute_and_write_conformance(RPC_FC_C_CSTRING, pStubMsg, pszMessage,
2265                                         pFormat);
2266     array_write_variance_and_marshall(RPC_FC_C_CSTRING, pStubMsg, pszMessage,
2267                                       pFormat, TRUE /* fHasPointers */);
2268   } else {
2269     array_compute_and_write_conformance(RPC_FC_C_WSTRING, pStubMsg, pszMessage,
2270                                         pFormat);
2271     array_write_variance_and_marshall(RPC_FC_C_WSTRING, pStubMsg, pszMessage,
2272                                       pFormat, TRUE /* fHasPointers */);
2273   }
2274 
2275   return NULL;
2276 }
2277 
2278 /***********************************************************************
2279  *           NdrConformantStringBufferSize [RPCRT4.@]
2280  */
2281 void WINAPI NdrConformantStringBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
2282   unsigned char* pMemory, PFORMAT_STRING pFormat)
2283 {
2284   TRACE("(pStubMsg == ^%p, pMemory == ^%p, pFormat == ^%p)\n", pStubMsg, pMemory, pFormat);
2285 
2286   if (pFormat[0] != RPC_FC_C_CSTRING && pFormat[0] != RPC_FC_C_WSTRING) {
2287     ERR("Unhandled string type: %#x\n", pFormat[0]);
2288     RpcRaiseException(RPC_X_BAD_STUB_DATA);
2289   }
2290 
2291   /* allow compiler to optimise inline function by passing constant into
2292    * these functions */
2293   if (pFormat[0] == RPC_FC_C_CSTRING) {
2294     array_compute_and_size_conformance(RPC_FC_C_CSTRING, pStubMsg, pMemory,
2295                                        pFormat);
2296     array_buffer_size(RPC_FC_C_CSTRING, pStubMsg, pMemory, pFormat,
2297                       TRUE /* fHasPointers */);
2298   } else {
2299     array_compute_and_size_conformance(RPC_FC_C_WSTRING, pStubMsg, pMemory,
2300                                        pFormat);
2301     array_buffer_size(RPC_FC_C_WSTRING, pStubMsg, pMemory, pFormat,
2302                       TRUE /* fHasPointers */);
2303   }
2304 }
2305 
2306 /************************************************************************
2307  *            NdrConformantStringMemorySize [RPCRT4.@]
2308  */
2309 ULONG WINAPI NdrConformantStringMemorySize( PMIDL_STUB_MESSAGE pStubMsg,
2310   PFORMAT_STRING pFormat )
2311 {
2312   TRACE("(pStubMsg == ^%p, pFormat == ^%p)\n", pStubMsg, pFormat);
2313 
2314   if (pFormat[0] != RPC_FC_C_CSTRING && pFormat[0] != RPC_FC_C_WSTRING) {
2315     ERR("Unhandled string type: %#x\n", pFormat[0]);
2316     RpcRaiseException(RPC_X_BAD_STUB_DATA);
2317   }
2318 
2319   /* allow compiler to optimise inline function by passing constant into
2320    * these functions */
2321   if (pFormat[0] == RPC_FC_C_CSTRING) {
2322     array_read_conformance(RPC_FC_C_CSTRING, pStubMsg, pFormat);
2323     array_memory_size(RPC_FC_C_CSTRING, pStubMsg, pFormat,
2324                       TRUE /* fHasPointers */);
2325   } else {
2326     array_read_conformance(RPC_FC_C_WSTRING, pStubMsg, pFormat);
2327     array_memory_size(RPC_FC_C_WSTRING, pStubMsg, pFormat,
2328                       TRUE /* fHasPointers */);
2329   }
2330 
2331   return pStubMsg->MemorySize;
2332 }
2333 
2334 /************************************************************************
2335  *           NdrConformantStringUnmarshall [RPCRT4.@]
2336  */
2337 unsigned char *WINAPI NdrConformantStringUnmarshall( PMIDL_STUB_MESSAGE pStubMsg,
2338   unsigned char** ppMemory, PFORMAT_STRING pFormat, unsigned char fMustAlloc )
2339 {
2340   TRACE("(pStubMsg == ^%p, *pMemory == ^%p, pFormat == ^%p, fMustAlloc == %u)\n",
2341     pStubMsg, *ppMemory, pFormat, fMustAlloc);
2342 
2343   if (pFormat[0] != RPC_FC_C_CSTRING && pFormat[0] != RPC_FC_C_WSTRING) {
2344     ERR("Unhandled string type: %#x\n", *pFormat);
2345     RpcRaiseException(RPC_X_BAD_STUB_DATA);
2346   }
2347 
2348   /* allow compiler to optimise inline function by passing constant into
2349    * these functions */
2350   if (pFormat[0] == RPC_FC_C_CSTRING) {
2351     array_read_conformance(RPC_FC_C_CSTRING, pStubMsg, pFormat);
2352     array_read_variance_and_unmarshall(RPC_FC_C_CSTRING, pStubMsg, ppMemory,
2353                                        pFormat, fMustAlloc,
2354                                        TRUE /* fUseBufferMemoryServer */,
2355                                        TRUE /* fUnmarshall */);
2356   } else {
2357     array_read_conformance(RPC_FC_C_WSTRING, pStubMsg, pFormat);
2358     array_read_variance_and_unmarshall(RPC_FC_C_WSTRING, pStubMsg, ppMemory,
2359                                        pFormat, fMustAlloc,
2360                                        TRUE /* fUseBufferMemoryServer */,
2361                                        TRUE /* fUnmarshall */);
2362   }
2363 
2364   return NULL;
2365 }
2366 
2367 /***********************************************************************
2368  *           NdrNonConformantStringMarshall [RPCRT4.@]
2369  */
2370 unsigned char *  WINAPI NdrNonConformantStringMarshall(PMIDL_STUB_MESSAGE pStubMsg,
2371                                 unsigned char *pMemory,
2372                                 PFORMAT_STRING pFormat)
2373 {
2374   ULONG esize, size, maxsize;
2375 
2376   TRACE("(pStubMsg == ^%p, pMemory == ^%p, pFormat == ^%p)\n", pStubMsg, pMemory, pFormat);
2377 
2378   maxsize = *(USHORT *)&pFormat[2];
2379 
2380   if (*pFormat == RPC_FC_CSTRING)
2381   {
2382     ULONG i;
2383     const char *str = (const char *)pMemory;
2384     for (i = 0; i < maxsize && *str; i++, str++)
2385         ;
2386     TRACE("string=%s\n", debugstr_an(str, i));
2387     pStubMsg->ActualCount = i + 1;
2388     esize = 1;
2389   }
2390   else if (*pFormat == RPC_FC_WSTRING)
2391   {
2392     ULONG i;
2393     const WCHAR *str = (const WCHAR *)pMemory;
2394     for (i = 0; i < maxsize && *str; i++, str++)
2395         ;
2396     TRACE("string=%s\n", debugstr_wn(str, i));
2397     pStubMsg->ActualCount = i + 1;
2398     esize = 2;
2399   }
2400   else
2401   {
2402     ERR("Unhandled string type: %#x\n", *pFormat);
2403     RpcRaiseException(RPC_X_BAD_STUB_DATA);
2404   }
2405 
2406   pStubMsg->Offset = 0;
2407   WriteVariance(pStubMsg);
2408 
2409   size = safe_multiply(esize, pStubMsg->ActualCount);
2410   safe_copy_to_buffer(pStubMsg, pMemory, size); /* the string itself */
2411 
2412   return NULL;
2413 }
2414 
2415 /***********************************************************************
2416  *           NdrNonConformantStringUnmarshall [RPCRT4.@]
2417  */
2418 unsigned char *  WINAPI NdrNonConformantStringUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
2419                                 unsigned char **ppMemory,
2420                                 PFORMAT_STRING pFormat,
2421                                 unsigned char fMustAlloc)
2422 {
2423   ULONG bufsize, memsize, esize, maxsize;
2424 
2425   TRACE("(pStubMsg == ^%p, *pMemory == ^%p, pFormat == ^%p, fMustAlloc == %u)\n",
2426     pStubMsg, *ppMemory, pFormat, fMustAlloc);
2427 
2428   maxsize = *(USHORT *)&pFormat[2];
2429 
2430   ReadVariance(pStubMsg, NULL, maxsize);
2431   if (pStubMsg->Offset)
2432   {
2433     ERR("non-conformant strings can't have Offset (%d)\n", pStubMsg->Offset);
2434     RpcRaiseException(RPC_S_INVALID_BOUND);
2435   }
2436 
2437   if (*pFormat == RPC_FC_CSTRING) esize = 1;
2438   else if (*pFormat == RPC_FC_WSTRING) esize = 2;
2439   else
2440   {
2441     ERR("Unhandled string type: %#x\n", *pFormat);
2442     RpcRaiseException(RPC_X_BAD_STUB_DATA);
2443   }
2444 
2445   memsize = esize * maxsize;
2446   bufsize = safe_multiply(esize, pStubMsg->ActualCount);
2447 
2448   validate_string_data(pStubMsg, bufsize, esize);
2449 
2450   if (!fMustAlloc && !*ppMemory)
2451     fMustAlloc = TRUE;
2452   if (fMustAlloc)
2453     *ppMemory = NdrAllocate(pStubMsg, memsize);
2454 
2455   safe_copy_from_buffer(pStubMsg, *ppMemory, bufsize);
2456 
2457   if (*pFormat == RPC_FC_CSTRING) {
2458     TRACE("string=%s\n", debugstr_an((char*)*ppMemory, pStubMsg->ActualCount));
2459   }
2460   else if (*pFormat == RPC_FC_WSTRING) {
2461     TRACE("string=%s\n", debugstr_wn((LPWSTR)*ppMemory, pStubMsg->ActualCount));
2462   }
2463 
2464   return NULL;
2465 }
2466 
2467 /***********************************************************************
2468  *           NdrNonConformantStringBufferSize [RPCRT4.@]
2469  */
2470 void WINAPI NdrNonConformantStringBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
2471                                 unsigned char *pMemory,
2472                                 PFORMAT_STRING pFormat)
2473 {
2474   ULONG esize, maxsize;
2475 
2476   TRACE("(pStubMsg == ^%p, pMemory == ^%p, pFormat == ^%p)\n", pStubMsg, pMemory, pFormat);
2477 
2478   maxsize = *(USHORT *)&pFormat[2];
2479 
2480   SizeVariance(pStubMsg);
2481 
2482   if (*pFormat == RPC_FC_CSTRING)
2483   {
2484     ULONG i;
2485     const char *str = (const char *)pMemory;
2486     for (i = 0; i < maxsize && *str; i++, str++)
2487         ;
2488     TRACE("string=%s\n", debugstr_an(str, i));
2489     pStubMsg->ActualCount = i + 1;
2490     esize = 1;
2491   }
2492   else if (*pFormat == RPC_FC_WSTRING)
2493   {
2494     ULONG i;
2495     const WCHAR *str = (const WCHAR *)pMemory;
2496     for (i = 0; i < maxsize && *str; i++, str++)
2497         ;
2498     TRACE("string=%s\n", debugstr_wn(str, i));
2499     pStubMsg->ActualCount = i + 1;
2500     esize = 2;
2501   }
2502   else
2503   {
2504     ERR("Unhandled string type: %#x\n", *pFormat);
2505     RpcRaiseException(RPC_X_BAD_STUB_DATA);
2506   }
2507 
2508   safe_buffer_length_increment(pStubMsg, safe_multiply(esize, pStubMsg->ActualCount));
2509 }
2510 
2511 /***********************************************************************
2512  *           NdrNonConformantStringMemorySize [RPCRT4.@]
2513  */
2514 ULONG WINAPI NdrNonConformantStringMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
2515                                 PFORMAT_STRING pFormat)
2516 {
2517   ULONG bufsize, memsize, esize, maxsize;
2518 
2519   TRACE("(pStubMsg == ^%p, pFormat == ^%p)\n", pStubMsg, pFormat);
2520 
2521   maxsize = *(USHORT *)&pFormat[2];
2522 
2523   ReadVariance(pStubMsg, NULL, maxsize);
2524 
2525   if (pStubMsg->Offset)
2526   {
2527     ERR("non-conformant strings can't have Offset (%d)\n", pStubMsg->Offset);
2528     RpcRaiseException(RPC_S_INVALID_BOUND);
2529   }
2530 
2531   if (*pFormat == RPC_FC_CSTRING) esize = 1;
2532   else if (*pFormat == RPC_FC_WSTRING) esize = 2;
2533   else
2534   {
2535     ERR("Unhandled string type: %#x\n", *pFormat);
2536     RpcRaiseException(RPC_X_BAD_STUB_DATA);
2537   }
2538 
2539   memsize = esize * maxsize;
2540   bufsize = safe_multiply(esize, pStubMsg->ActualCount);
2541 
2542   validate_string_data(pStubMsg, bufsize, esize);
2543 
2544   safe_buffer_increment(pStubMsg, bufsize);
2545   pStubMsg->MemorySize += memsize;
2546 
2547   return pStubMsg->MemorySize;
2548 }
2549 
2550 /* Complex types */
2551 
2552 #include "pshpack1.h"
2553 typedef struct
2554 {
2555     unsigned char type;
2556     unsigned char flags_type; /* flags in upper nibble, type in lower nibble */
2557     ULONG low_value;
2558     ULONG high_value;
2559 } NDR_RANGE;
2560 #include "poppack.h"
2561 
2562 static unsigned long EmbeddedComplexSize(MIDL_STUB_MESSAGE *pStubMsg,
2563                                          PFORMAT_STRING pFormat)
2564 {
2565   switch (*pFormat) {
2566   case RPC_FC_STRUCT:
2567   case RPC_FC_PSTRUCT:
2568   case RPC_FC_CSTRUCT:
2569   case RPC_FC_BOGUS_STRUCT:
2570   case RPC_FC_SMFARRAY:
2571   case RPC_FC_SMVARRAY:
2572   case RPC_FC_CSTRING:
2573     return *(const WORD*)&pFormat[2];
2574   case RPC_FC_USER_MARSHAL:
2575     return *(const WORD*)&pFormat[4];
2576   case RPC_FC_RANGE: {
2577     switch (((const NDR_RANGE *)pFormat)->flags_type & 0xf) {
2578     case RPC_FC_BYTE:
2579     case RPC_FC_CHAR:
2580     case RPC_FC_SMALL:
2581     case RPC_FC_USMALL:
2582         return sizeof(UCHAR);
2583     case RPC_FC_WCHAR:
2584     case RPC_FC_SHORT:
2585     case RPC_FC_USHORT:
2586         return sizeof(USHORT);
2587     case RPC_FC_LONG:
2588     case RPC_FC_ULONG:
2589     case RPC_FC_ENUM32:
2590         return sizeof(ULONG);
2591     case RPC_FC_FLOAT:
2592         return sizeof(float);
2593     case RPC_FC_DOUBLE:
2594         return sizeof(double);
2595     case RPC_FC_HYPER:
2596         return sizeof(ULONGLONG);
2597     case RPC_FC_ENUM16:
2598         return sizeof(UINT);
2599     default:
2600         ERR("unknown type 0x%x\n", ((const NDR_RANGE *)pFormat)->flags_type & 0xf);
2601         RpcRaiseException(RPC_X_BAD_STUB_DATA);
2602     }
2603   }
2604   case RPC_FC_NON_ENCAPSULATED_UNION:
2605     pFormat += 2;
2606     if (pStubMsg->fHasNewCorrDesc)
2607         pFormat += 6;
2608     else
2609         pFormat += 4;
2610 
2611     pFormat += *(const SHORT*)pFormat;
2612     return *(const SHORT*)pFormat;
2613   case RPC_FC_IP:
2614     return sizeof(void *);
2615   case RPC_FC_WSTRING:
2616     return *(const WORD*)&pFormat[2] * 2;
2617   default:
2618     FIXME("unhandled embedded type %02x\n", *pFormat);
2619   }
2620   return 0;
2621 }
2622 
2623 
2624 static unsigned long EmbeddedComplexMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
2625                                                PFORMAT_STRING pFormat)
2626 {
2627   NDR_MEMORYSIZE m = NdrMemorySizer[*pFormat & NDR_TABLE_MASK];
2628 
2629   if (!m)
2630   {
2631     FIXME("no memorysizer for data type=%02x\n", *pFormat);
2632     return 0;
2633   }
2634 
2635   return m(pStubMsg, pFormat);
2636 }
2637 
2638 
2639 static unsigned char * ComplexMarshall(PMIDL_STUB_MESSAGE pStubMsg,
2640                                        unsigned char *pMemory,
2641                                        PFORMAT_STRING pFormat,
2642                                        PFORMAT_STRING pPointer)
2643 {
2644   PFORMAT_STRING desc;
2645   NDR_MARSHALL m;
2646   unsigned long size;
2647 
2648   while (*pFormat != RPC_FC_END) {
2649     switch (*pFormat) {
2650     case RPC_FC_BYTE:
2651     case RPC_FC_CHAR:
2652     case RPC_FC_SMALL:
2653     case RPC_FC_USMALL:
2654       TRACE("byte=%d <= %p\n", *(WORD*)pMemory, pMemory);
2655       safe_copy_to_buffer(pStubMsg, pMemory, 1);
2656       pMemory += 1;
2657       break;
2658     case RPC_FC_WCHAR:
2659     case RPC_FC_SHORT:
2660     case RPC_FC_USHORT:
2661       TRACE("short=%d <= %p\n", *(WORD*)pMemory, pMemory);
2662       safe_copy_to_buffer(pStubMsg, pMemory, 2);
2663       pMemory += 2;
2664       break;
2665     case RPC_FC_ENUM16:
2666       TRACE("enum16=%d <= %p\n", *(DWORD*)pMemory, pMemory);
2667       if (32767 < *(DWORD*)pMemory)
2668         RpcRaiseException(RPC_X_ENUM_VALUE_OUT_OF_RANGE);
2669       safe_copy_to_buffer(pStubMsg, pMemory, 2);
2670       pMemory += 4;
2671       break;
2672     case RPC_FC_LONG:
2673     case RPC_FC_ULONG:
2674     case RPC_FC_ENUM32:
2675       TRACE("long=%d <= %p\n", *(DWORD*)pMemory, pMemory);
2676       safe_copy_to_buffer(pStubMsg, pMemory, 4);
2677       pMemory += 4;
2678       break;
2679     case RPC_FC_HYPER:
2680       TRACE("longlong=%s <= %p\n", wine_dbgstr_longlong(*(ULONGLONG*)pMemory), pMemory);
2681       safe_copy_to_buffer(pStubMsg, pMemory, 8);
2682       pMemory += 8;
2683       break;
2684     case RPC_FC_POINTER:
2685     {
2686       unsigned char *saved_buffer;
2687       int pointer_buffer_mark_set = 0;
2688       TRACE("pointer=%p <= %p\n", *(unsigned char**)pMemory, pMemory);
2689       TRACE("pStubMsg->Buffer before %p\n", pStubMsg->Buffer);
2690       if (*pPointer != RPC_FC_RP)
2691         ALIGN_POINTER_CLEAR(pStubMsg->Buffer, 4);
2692       saved_buffer = pStubMsg->Buffer;
2693       if (pStubMsg->PointerBufferMark)
2694       {
2695         pStubMsg->Buffer = pStubMsg->PointerBufferMark;
2696         pStubMsg->PointerBufferMark = NULL;
2697         pointer_buffer_mark_set = 1;
2698       }
2699       else if (*pPointer != RPC_FC_RP)
2700         safe_buffer_increment(pStubMsg, 4); /* for pointer ID */
2701       PointerMarshall(pStubMsg, saved_buffer, *(unsigned char**)pMemory, pPointer);
2702       if (pointer_buffer_mark_set)
2703       {
2704         STD_OVERFLOW_CHECK(pStubMsg);
2705         pStubMsg->PointerBufferMark = pStubMsg->Buffer;
2706         pStubMsg->Buffer = saved_buffer;
2707         if (*pPointer != RPC_FC_RP)
2708           safe_buffer_increment(pStubMsg, 4); /* for pointer ID */
2709       }
2710       TRACE("pStubMsg->Buffer after %p\n", pStubMsg->Buffer);
2711       pPointer += 4;
2712       pMemory += 4;
2713       break;
2714     }
2715     case RPC_FC_ALIGNM4:
2716       ALIGN_POINTER(pMemory, 4);
2717       break;
2718     case RPC_FC_ALIGNM8:
2719       ALIGN_POINTER(pMemory, 8);
2720       break;
2721     case RPC_FC_STRUCTPAD1:
2722     case RPC_FC_STRUCTPAD2:
2723     case RPC_FC_STRUCTPAD3:
2724     case RPC_FC_STRUCTPAD4:
2725     case RPC_FC_STRUCTPAD5:
2726     case RPC_FC_STRUCTPAD6:
2727     case RPC_FC_STRUCTPAD7:
2728       pMemory += *pFormat - RPC_FC_STRUCTPAD1 + 1;
2729       break;
2730     case RPC_FC_EMBEDDED_COMPLEX:
2731       pMemory += pFormat[1];
2732       pFormat += 2;
2733       desc = pFormat + *(const SHORT*)pFormat;
2734       size = EmbeddedComplexSize(pStubMsg, desc);
2735       TRACE("embedded complex (size=%ld) <= %p\n", size, pMemory);
2736       m = NdrMarshaller[*desc & NDR_TABLE_MASK];
2737       if (m)
2738       {
2739         /* for some reason interface pointers aren't generated as
2740          * RPC_FC_POINTER, but instead as RPC_FC_EMBEDDED_COMPLEX, yet
2741          * they still need the derefencing treatment that pointers are
2742          * given */
2743         if (*desc == RPC_FC_IP)
2744           m(pStubMsg, *(unsigned char **)pMemory, desc);
2745         else
2746           m(pStubMsg, pMemory, desc);
2747       }
2748       else FIXME("no marshaller for embedded type %02x\n", *desc);
2749       pMemory += size;
2750       pFormat += 2;
2751       continue;
2752     case RPC_FC_PAD:
2753       break;
2754     default:
2755       FIXME("unhandled format 0x%02x\n", *pFormat);
2756     }
2757     pFormat++;
2758   }
2759 
2760   return pMemory;
2761 }
2762 
2763 static unsigned char * ComplexUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
2764                                          unsigned char *pMemory,
2765                                          PFORMAT_STRING pFormat,
2766                                          PFORMAT_STRING pPointer,
2767                                          unsigned char fMustAlloc)
2768 {
2769   PFORMAT_STRING desc;
2770   NDR_UNMARSHALL m;
2771   unsigned long size;
2772 
2773   while (*pFormat != RPC_FC_END) {
2774     switch (*pFormat) {
2775     case RPC_FC_BYTE:
2776     case RPC_FC_CHAR:
2777     case RPC_FC_SMALL:
2778     case RPC_FC_USMALL:
2779       safe_copy_from_buffer(pStubMsg, pMemory, 1);
2780       TRACE("byte=%d => %p\n", *(WORD*)pMemory, pMemory);
2781       pMemory += 1;
2782       break;
2783     case RPC_FC_WCHAR:
2784     case RPC_FC_SHORT:
2785     case RPC_FC_USHORT:
2786       safe_copy_from_buffer(pStubMsg, pMemory, 2);
2787       TRACE("short=%d => %p\n", *(WORD*)pMemory, pMemory);
2788       pMemory += 2;
2789       break;
2790     case RPC_FC_ENUM16:
2791       safe_copy_from_buffer(pStubMsg, pMemory, 2);
2792       *(DWORD*)pMemory &= 0xffff;
2793       TRACE("enum16=%d => %p\n", *(DWORD*)pMemory, pMemory);
2794       if (32767 < *(DWORD*)pMemory)
2795         RpcRaiseException(RPC_X_ENUM_VALUE_OUT_OF_RANGE);
2796       pMemory += 4;
2797       break;
2798     case RPC_FC_LONG:
2799     case RPC_FC_ULONG:
2800     case RPC_FC_ENUM32:
2801       safe_copy_from_buffer(pStubMsg, pMemory, 4);
2802       TRACE("long=%d => %p\n", *(DWORD*)pMemory, pMemory);
2803       pMemory += 4;
2804       break;
2805     case RPC_FC_HYPER:
2806       safe_copy_from_buffer(pStubMsg, pMemory, 8);
2807       TRACE("longlong=%s => %p\n", wine_dbgstr_longlong(*(ULONGLONG*)pMemory), pMemory);
2808       pMemory += 8;
2809       break;
2810     case RPC_FC_POINTER:
2811     {
2812       unsigned char *saved_buffer;
2813       int pointer_buffer_mark_set = 0;
2814       TRACE("pointer => %p\n", pMemory);
2815       if (*pPointer != RPC_FC_RP)
2816         ALIGN_POINTER(pStubMsg->Buffer, 4);
2817       saved_buffer = pStubMsg->Buffer;
2818       if (pStubMsg->PointerBufferMark)
2819       {
2820         pStubMsg->Buffer = pStubMsg->PointerBufferMark;
2821         pStubMsg->PointerBufferMark = NULL;
2822         pointer_buffer_mark_set = 1;
2823       }
2824       else if (*pPointer != RPC_FC_RP)
2825         safe_buffer_increment(pStubMsg, 4); /* for pointer ID */
2826 
2827       PointerUnmarshall(pStubMsg, saved_buffer, (unsigned char**)pMemory, *(unsigned char**)pMemory, pPointer, fMustAlloc);
2828       if (pointer_buffer_mark_set)
2829       {
2830         STD_OVERFLOW_CHECK(pStubMsg);
2831         pStubMsg->PointerBufferMark = pStubMsg->Buffer;
2832         pStubMsg->Buffer = saved_buffer;
2833         if (*pPointer != RPC_FC_RP)
2834           safe_buffer_increment(pStubMsg, 4); /* for pointer ID */
2835       }
2836       pPointer += 4;
2837       pMemory += 4;
2838       break;
2839     }
2840     case RPC_FC_ALIGNM4:
2841       ALIGN_POINTER_CLEAR(pMemory, 4);
2842       break;
2843     case RPC_FC_ALIGNM8:
2844       ALIGN_POINTER_CLEAR(pMemory, 8);
2845       break;
2846     case RPC_FC_STRUCTPAD1:
2847     case RPC_FC_STRUCTPAD2:
2848     case RPC_FC_STRUCTPAD3:
2849     case RPC_FC_STRUCTPAD4:
2850     case RPC_FC_STRUCTPAD5:
2851     case RPC_FC_STRUCTPAD6:
2852     case RPC_FC_STRUCTPAD7:
2853       memset(pMemory, 0, *pFormat - RPC_FC_STRUCTPAD1 + 1);
2854       pMemory += *pFormat - RPC_FC_STRUCTPAD1 + 1;
2855       break;
2856     case RPC_FC_EMBEDDED_COMPLEX:
2857       pMemory += pFormat[1];
2858       pFormat += 2;
2859       desc = pFormat + *(const SHORT*)pFormat;
2860       size = EmbeddedComplexSize(pStubMsg, desc);
2861       TRACE("embedded complex (size=%ld) => %p\n", size, pMemory);
2862       if (fMustAlloc)
2863         /* we can't pass fMustAlloc=TRUE into the marshaller for this type
2864          * since the type is part of the memory block that is encompassed by
2865          * the whole complex type. Memory is forced to allocate when pointers
2866          * are set to NULL, so we emulate that part of fMustAlloc=TRUE by
2867          * clearing the memory we pass in to the unmarshaller */
2868         memset(pMemory, 0, size);
2869       m = NdrUnmarshaller[*desc & NDR_TABLE_MASK];
2870       if (m)
2871       {
2872         /* for some reason interface pointers aren't generated as
2873          * RPC_FC_POINTER, but instead as RPC_FC_EMBEDDED_COMPLEX, yet
2874          * they still need the derefencing treatment that pointers are
2875          * given */
2876         if (*desc == RPC_FC_IP)
2877           m(pStubMsg, (unsigned char **)pMemory, desc, FALSE);
2878         else
2879           m(pStubMsg, &pMemory, desc, FALSE);
2880       }
2881       else FIXME("no unmarshaller for embedded type %02x\n", *desc);
2882       pMemory += size;
2883       pFormat += 2;
2884       continue;
2885     case RPC_FC_PAD:
2886       break;
2887     default:
2888       FIXME("unhandled format %d\n", *pFormat);
2889     }
2890     pFormat++;
2891   }
2892 
2893   return pMemory;
2894 }
2895 
2896 static unsigned char * ComplexBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
2897                                          unsigned char *pMemory,
2898                                          PFORMAT_STRING pFormat,
2899                                          PFORMAT_STRING pPointer)
2900 {
2901   PFORMAT_STRING desc;
2902   NDR_BUFFERSIZE m;
2903   unsigned long size;
2904 
2905   while (*pFormat != RPC_FC_END) {
2906     switch (*pFormat) {
2907     case RPC_FC_BYTE:
2908     case RPC_FC_CHAR:
2909     case RPC_FC_SMALL:
2910     case RPC_FC_USMALL:
2911       safe_buffer_length_increment(pStubMsg, 1);
2912       pMemory += 1;
2913       break;
2914     case RPC_FC_WCHAR:
2915     case RPC_FC_SHORT:
2916     case RPC_FC_USHORT:
2917       safe_buffer_length_increment(pStubMsg, 2);
2918       pMemory += 2;
2919       break;
2920     case RPC_FC_ENUM16:
2921       safe_buffer_length_increment(pStubMsg, 2);
2922       pMemory += 4;
2923       break;
2924     case RPC_FC_LONG:
2925     case RPC_FC_ULONG:
2926     case RPC_FC_ENUM32:
2927       safe_buffer_length_increment(pStubMsg, 4);
2928       pMemory += 4;
2929       break;
2930     case RPC_FC_HYPER:
2931       safe_buffer_length_increment(pStubMsg, 8);
2932       pMemory += 8;
2933       break;
2934     case RPC_FC_POINTER:
2935       if (!pStubMsg->IgnoreEmbeddedPointers)
2936       {
2937         int saved_buffer_length = pStubMsg->BufferLength;
2938         pStubMsg->BufferLength = pStubMsg->PointerLength;
2939         pStubMsg->PointerLength = 0;
2940         if(!pStubMsg->BufferLength)
2941           ERR("BufferLength == 0??\n");
2942         PointerBufferSize(pStubMsg, *(unsigned char**)pMemory, pPointer);
2943         pStubMsg->PointerLength = pStubMsg->BufferLength;
2944         pStubMsg->BufferLength = saved_buffer_length;
2945       }
2946       if (*pPointer != RPC_FC_RP)
2947       {
2948         ALIGN_LENGTH(pStubMsg->BufferLength, 4);
2949         safe_buffer_length_increment(pStubMsg, 4);
2950       }
2951       pPointer += 4;
2952       pMemory += 4;
2953       break;
2954     case RPC_FC_ALIGNM4:
2955       ALIGN_POINTER(pMemory, 4);
2956       break;
2957     case RPC_FC_ALIGNM8:
2958       ALIGN_POINTER(pMemory, 8);
2959       break;
2960     case RPC_FC_STRUCTPAD1:
2961     case RPC_FC_STRUCTPAD2:
2962     case RPC_FC_STRUCTPAD3:
2963     case RPC_FC_STRUCTPAD4:
2964     case RPC_FC_STRUCTPAD5:
2965     case RPC_FC_STRUCTPAD6:
2966     case RPC_FC_STRUCTPAD7:
2967       pMemory += *pFormat - RPC_FC_STRUCTPAD1 + 1;
2968       break;
2969     case RPC_FC_EMBEDDED_COMPLEX:
2970       pMemory += pFormat[1];
2971       pFormat += 2;
2972       desc = pFormat + *(const SHORT*)pFormat;
2973       size = EmbeddedComplexSize(pStubMsg, desc);
2974       m = NdrBufferSizer[*desc & NDR_TABLE_MASK];
2975       if (m)
2976       {
2977         /* for some reason interface pointers aren't generated as
2978          * RPC_FC_POINTER, but instead as RPC_FC_EMBEDDED_COMPLEX, yet
2979          * they still need the derefencing treatment that pointers are
2980          * given */
2981         if (*desc == RPC_FC_IP)
2982           m(pStubMsg, *(unsigned char **)pMemory, desc);
2983         else
2984           m(pStubMsg, pMemory, desc);
2985       }
2986       else FIXME("no buffersizer for embedded type %02x\n", *desc);
2987       pMemory += size;
2988       pFormat += 2;
2989       continue;
2990     case RPC_FC_PAD:
2991       break;
2992     default:
2993       FIXME("unhandled format 0x%02x\n", *pFormat);
2994     }
2995     pFormat++;
2996   }
2997 
2998   return pMemory;
2999 }
3000 
3001 static unsigned char * ComplexFree(PMIDL_STUB_MESSAGE pStubMsg,
3002                                    unsigned char *pMemory,
3003                                    PFORMAT_STRING pFormat,
3004                                    PFORMAT_STRING pPointer)
3005 {
3006   PFORMAT_STRING desc;
3007   NDR_FREE m;
3008   unsigned long size;
3009 
3010   while (*pFormat != RPC_FC_END) {
3011     switch (*pFormat) {
3012     case RPC_FC_BYTE:
3013     case RPC_FC_CHAR:
3014     case RPC_FC_SMALL:
3015     case RPC_FC_USMALL:
3016       pMemory += 1;
3017       break;
3018     case RPC_FC_WCHAR:
3019     case RPC_FC_SHORT:
3020     case RPC_FC_USHORT:
3021       pMemory += 2;
3022       break;
3023     case RPC_FC_LONG:
3024     case RPC_FC_ULONG:
3025     case RPC_FC_ENUM16:
3026     case RPC_FC_ENUM32:
3027       pMemory += 4;
3028       break;
3029     case RPC_FC_HYPER:
3030       pMemory += 8;
3031       break;
3032     case RPC_FC_POINTER:
3033       NdrPointerFree(pStubMsg, *(unsigned char**)pMemory, pPointer);
3034       pPointer += 4;
3035       pMemory += 4;
3036       break;
3037     case RPC_FC_ALIGNM4:
3038       ALIGN_POINTER(pMemory, 4);
3039       break;
3040     case RPC_FC_ALIGNM8:
3041       ALIGN_POINTER(pMemory, 8);
3042       break;
3043     case RPC_FC_STRUCTPAD1:
3044     case RPC_FC_STRUCTPAD2:
3045     case RPC_FC_STRUCTPAD3:
3046     case RPC_FC_STRUCTPAD4:
3047     case RPC_FC_STRUCTPAD5:
3048     case RPC_FC_STRUCTPAD6:
3049     case RPC_FC_STRUCTPAD7:
3050       pMemory += *pFormat - RPC_FC_STRUCTPAD1 + 1;
3051       break;
3052     case RPC_FC_EMBEDDED_COMPLEX:
3053       pMemory += pFormat[1];
3054       pFormat += 2;
3055       desc = pFormat + *(const SHORT*)pFormat;
3056       size = EmbeddedComplexSize(pStubMsg, desc);
3057       m = NdrFreer[*desc & NDR_TABLE_MASK];
3058       if (m)
3059       {
3060         /* for some reason interface pointers aren't generated as
3061          * RPC_FC_POINTER, but instead as RPC_FC_EMBEDDED_COMPLEX, yet
3062          * they still need the derefencing treatment that pointers are
3063          * given */
3064         if (*desc == RPC_FC_IP)
3065           m(pStubMsg, *(unsigned char **)pMemory, desc);
3066         else
3067           m(pStubMsg, pMemory, desc);
3068       }
3069       pMemory += size;
3070       pFormat += 2;
3071       continue;
3072     case RPC_FC_PAD:
3073       break;
3074     default:
3075       FIXME("unhandled format 0x%02x\n", *pFormat);
3076     }
3077     pFormat++;
3078   }
3079 
3080   return pMemory;
3081 }
3082 
3083 static unsigned long ComplexStructMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
3084                                              PFORMAT_STRING pFormat,
3085                                              PFORMAT_STRING pPointer)
3086 {
3087   PFORMAT_STRING desc;
3088   unsigned long size = 0;
3089 
3090   while (*pFormat != RPC_FC_END) {
3091     switch (*pFormat) {
3092     case RPC_FC_BYTE:
3093     case RPC_FC_CHAR:
3094     case RPC_FC_SMALL:
3095     case RPC_FC_USMALL:
3096       size += 1;
3097       safe_buffer_increment(pStubMsg, 1);
3098       break;
3099     case RPC_FC_WCHAR:
3100     case RPC_FC_SHORT:
3101     case RPC_FC_USHORT:
3102       size += 2;
3103       safe_buffer_increment(pStubMsg, 2);
3104       break;
3105     case RPC_FC_ENUM16:
3106       size += 4;
3107       safe_buffer_increment(pStubMsg, 2);
3108       break;
3109     case RPC_FC_LONG:
3110     case RPC_FC_ULONG:
3111     case RPC_FC_ENUM32:
3112       size += 4;
3113       safe_buffer_increment(pStubMsg, 4);
3114       break;
3115     case RPC_FC_HYPER:
3116       size += 8;
3117       safe_buffer_increment(pStubMsg, 8);
3118       break;
3119     case RPC_FC_POINTER:
3120     {
3121       unsigned char *saved_buffer;
3122       int pointer_buffer_mark_set = 0;
3123       if (*pPointer != RPC_FC_RP)
3124         ALIGN_POINTER(pStubMsg->Buffer, 4);
3125       saved_buffer = pStubMsg->Buffer;
3126       if (pStubMsg->PointerBufferMark)
3127       {
3128         pStubMsg->Buffer = pStubMsg->PointerBufferMark;
3129         pStubMsg->PointerBufferMark = NULL;
3130         pointer_buffer_mark_set = 1;
3131       }
3132       else if (*pPointer != RPC_FC_RP)
3133         safe_buffer_increment(pStubMsg, 4); /* for pointer ID */
3134 
3135       if (!pStubMsg->IgnoreEmbeddedPointers)
3136         PointerMemorySize(pStubMsg, saved_buffer, pPointer);
3137       if (pointer_buffer_mark_set)
3138       {
3139         STD_OVERFLOW_CHECK(pStubMsg);
3140         pStubMsg->PointerBufferMark = pStubMsg->Buffer;
3141         pStubMsg->Buffer = saved_buffer;
3142         if (*pPointer != RPC_FC_RP)
3143           safe_buffer_increment(pStubMsg, 4); /* for pointer ID */
3144       }
3145       pPointer += 4;
3146       size += 4;
3147       break;
3148     }
3149     case RPC_FC_ALIGNM4:
3150       ALIGN_LENGTH(size, 4);
3151       break;
3152     case RPC_FC_ALIGNM8:
3153       ALIGN_LENGTH(size, 8);
3154       break;
3155     case RPC_FC_STRUCTPAD1:
3156     case RPC_FC_STRUCTPAD2:
3157     case RPC_FC_STRUCTPAD3:
3158     case RPC_FC_STRUCTPAD4:
3159     case RPC_FC_STRUCTPAD5:
3160     case RPC_FC_STRUCTPAD6:
3161     case RPC_FC_STRUCTPAD7:
3162       size += *pFormat - RPC_FC_STRUCTPAD1 + 1;
3163       break;
3164     case RPC_FC_EMBEDDED_COMPLEX:
3165       size += pFormat[1];
3166       pFormat += 2;
3167       desc = pFormat + *(const SHORT*)pFormat;
3168       size += EmbeddedComplexMemorySize(pStubMsg, desc);
3169       pFormat += 2;
3170       continue;
3171     case RPC_FC_PAD:
3172       break;
3173     default:
3174       FIXME("unhandled format 0x%02x\n", *pFormat);
3175     }
3176     pFormat++;
3177   }
3178 
3179   return size;
3180 }
3181 
3182 unsigned long ComplexStructSize(PMIDL_STUB_MESSAGE pStubMsg,
3183                                 PFORMAT_STRING pFormat)
3184 {
3185   PFORMAT_STRING desc;
3186   unsigned long size = 0;
3187 
3188   while (*pFormat != RPC_FC_END) {
3189     switch (*pFormat) {
3190     case RPC_FC_BYTE:
3191     case RPC_FC_CHAR:
3192     case RPC_FC_SMALL:
3193     case RPC_FC_USMALL:
3194       size += 1;
3195       break;
3196     case RPC_FC_WCHAR:
3197     case RPC_FC_SHORT:
3198     case RPC_FC_USHORT:
3199       size += 2;
3200       break;
3201     case RPC_FC_LONG:
3202     case RPC_FC_ULONG:
3203     case RPC_FC_ENUM16:
3204     case RPC_FC_ENUM32:
3205       size += 4;
3206       break;
3207     case RPC_FC_HYPER:
3208       size += 8;
3209       break;
3210     case RPC_FC_POINTER:
3211       size += sizeof(void *);
3212       break;
3213     case RPC_FC_ALIGNM4:
3214       ALIGN_LENGTH(size, 4);
3215       break;
3216     case RPC_FC_ALIGNM8:
3217       ALIGN_LENGTH(size, 8);
3218       break;
3219     case RPC_FC_STRUCTPAD1:
3220     case RPC_FC_STRUCTPAD2:
3221     case RPC_FC_STRUCTPAD3:
3222     case RPC_FC_STRUCTPAD4:
3223     case RPC_FC_STRUCTPAD5:
3224     case RPC_FC_STRUCTPAD6:
3225     case RPC_FC_STRUCTPAD7:
3226       size += *pFormat - RPC_FC_STRUCTPAD1 + 1;
3227       break;
3228     case RPC_FC_EMBEDDED_COMPLEX:
3229       size += pFormat[1];
3230       pFormat += 2;
3231       desc = pFormat + *(const SHORT*)pFormat;
3232       size += EmbeddedComplexSize(pStubMsg, desc);
3233       pFormat += 2;
3234       continue;
3235     case RPC_FC_PAD:
3236       break;
3237     default:
3238       FIXME("unhandled format 0x%02x\n", *pFormat);
3239     }
3240     pFormat++;
3241   }
3242 
3243   return size;
3244 }
3245 
3246 /***********************************************************************
3247  *           NdrComplexStructMarshall [RPCRT4.@]
3248  */
3249 unsigned char * WINAPI NdrComplexStructMarshall(PMIDL_STUB_MESSAGE pStubMsg,
3250                                                 unsigned char *pMemory,
3251                                                 PFORMAT_STRING pFormat)
3252 {
3253   PFORMAT_STRING conf_array = NULL;
3254   PFORMAT_STRING pointer_desc = NULL;
3255   unsigned char *OldMemory = pStubMsg->Memory;
3256   int pointer_buffer_mark_set = 0;
3257   ULONG count = 0;
3258   ULONG max_count = 0;
3259   ULONG offset = 0;
3260 
3261   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
3262 
3263   if (!pStubMsg->PointerBufferMark)
3264   {
3265     int saved_ignore_embedded = pStubMsg->IgnoreEmbeddedPointers;
3266     /* save buffer length */
3267     unsigned long saved_buffer_length = pStubMsg->BufferLength;
3268 
3269     /* get the buffer pointer after complex array data, but before
3270      * pointer data */
3271     pStubMsg->BufferLength = pStubMsg->Buffer - pStubMsg->BufferStart;
3272     pStubMsg->IgnoreEmbeddedPointers = 1;
3273     NdrComplexStructBufferSize(pStubMsg, pMemory, pFormat);
3274     pStubMsg->IgnoreEmbeddedPointers = saved_ignore_embedded;
3275 
3276     /* save it for use by embedded pointer code later */
3277     pStubMsg->PointerBufferMark = pStubMsg->BufferStart + pStubMsg->BufferLength;
3278     TRACE("difference = 0x%x\n", pStubMsg->PointerBufferMark - pStubMsg->Buffer);
3279     pointer_buffer_mark_set = 1;
3280 
3281     /* restore the original buffer length */
3282     pStubMsg->BufferLength = saved_buffer_length;
3283   }
3284 
3285   ALIGN_POINTER_CLEAR(pStubMsg->Buffer, pFormat[1] + 1);
3286 
3287   pFormat += 4;
3288   if (*(const SHORT*)pFormat) conf_array = pFormat + *(const SHORT*)pFormat;
3289   pFormat += 2;
3290   if (*(const WORD*)pFormat) pointer_desc = pFormat + *(const WORD*)pFormat;
3291   pFormat += 2;
3292 
3293   pStubMsg->Memory = pMemory;
3294 
3295   if (conf_array)
3296   {
3297     unsigned long struct_size = ComplexStructSize(pStubMsg, pFormat);
3298     array_compute_and_write_conformance(conf_array[0], pStubMsg,
3299                                         pMemory + struct_size, conf_array);
3300     /* these could be changed in ComplexMarshall so save them for later */
3301     max_count = pStubMsg->MaxCount;
3302     count = pStubMsg->ActualCount;
3303     offset = pStubMsg->Offset;
3304   }
3305 
3306   pMemory = ComplexMarshall(pStubMsg, pMemory, pFormat, pointer_desc);
3307 
3308   if (conf_array)
3309   {
3310     pStubMsg->MaxCount = max_count;
3311     pStubMsg->ActualCount = count;
3312     pStubMsg->Offset = offset;
3313     array_write_variance_and_marshall(conf_array[0], pStubMsg, pMemory,
3314                                       conf_array, TRUE /* fHasPointers */);
3315   }
3316 
3317   pStubMsg->Memory = OldMemory;
3318 
3319   if (pointer_buffer_mark_set)
3320   {
3321     pStubMsg->Buffer = pStubMsg->PointerBufferMark;
3322     pStubMsg->PointerBufferMark = NULL;
3323   }
3324 
3325   STD_OVERFLOW_CHECK(pStubMsg);
3326 
3327   return NULL;
3328 }
3329 
3330 /***********************************************************************
3331  *           NdrComplexStructUnmarshall [RPCRT4.@]
3332  */
3333 unsigned char * WINAPI NdrComplexStructUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
3334                                                   unsigned char **ppMemory,
3335                                                   PFORMAT_STRING pFormat,
3336                                                   unsigned char fMustAlloc)
3337 {
3338   unsigned size = *(const WORD*)(pFormat+2);
3339   PFORMAT_STRING conf_array = NULL;
3340   PFORMAT_STRING pointer_desc = NULL;
3341   unsigned char *pMemory;
3342   int pointer_buffer_mark_set = 0;
3343   ULONG count = 0;
3344   ULONG max_count = 0;
3345   ULONG offset = 0;
3346   ULONG array_size = 0;
3347 
3348   TRACE("(%p,%p,%p,%d)\n", pStubMsg, ppMemory, pFormat, fMustAlloc);
3349 
3350   if (!pStubMsg->PointerBufferMark)
3351   {
3352     int saved_ignore_embedded = pStubMsg->IgnoreEmbeddedPointers;
3353     /* save buffer pointer */
3354     unsigned char *saved_buffer = pStubMsg->Buffer;
3355 
3356     /* get the buffer pointer after complex array data, but before
3357      * pointer data */
3358     pStubMsg->IgnoreEmbeddedPointers = 1;
3359     NdrComplexStructMemorySize(pStubMsg, pFormat);
3360     pStubMsg->IgnoreEmbeddedPointers = saved_ignore_embedded;
3361 
3362     /* save it for use by embedded pointer code later */
3363     pStubMsg->PointerBufferMark = pStubMsg->Buffer;
3364     TRACE("difference = 0x%lx\n", (unsigned long)(pStubMsg->PointerBufferMark - saved_buffer));
3365     pointer_buffer_mark_set = 1;
3366 
3367     /* restore the original buffer */
3368     pStubMsg->Buffer = saved_buffer;
3369   }
3370 
3371   ALIGN_POINTER(pStubMsg->Buffer, pFormat[1] + 1);
3372 
3373   pFormat += 4;
3374   if (*(const SHORT*)pFormat) conf_array = pFormat + *(const SHORT*)pFormat;
3375   pFormat += 2;
3376   if (*(const WORD*)pFormat) pointer_desc = pFormat + *(const WORD*)pFormat;
3377   pFormat += 2;
3378 
3379   if (conf_array)
3380   {
3381     array_size = array_read_conformance(conf_array[0], pStubMsg, conf_array);
3382     size += array_size;
3383 
3384     /* these could be changed in ComplexMarshall so save them for later */
3385     max_count = pStubMsg->MaxCount;
3386     count = pStubMsg->ActualCount;
3387     offset = pStubMsg->Offset;
3388   }
3389 
3390   if (!fMustAlloc && !*ppMemory)
3391     fMustAlloc = TRUE;
3392   if (fMustAlloc)
3393     *ppMemory = NdrAllocate(pStubMsg, size);
3394 
3395   pMemory = ComplexUnmarshall(pStubMsg, *ppMemory, pFormat, pointer_desc, fMustAlloc);
3396 
3397   if (conf_array)
3398   {
3399     pStubMsg->MaxCount = max_count;
3400     pStubMsg->ActualCount = count;
3401     pStubMsg->Offset = offset;
3402     if (fMustAlloc)
3403       memset(pMemory, 0, array_size);
3404     array_read_variance_and_unmarshall(conf_array[0], pStubMsg, &pMemory,
3405                                        conf_array, FALSE,
3406                                        FALSE /* fUseBufferMemoryServer */,
3407                                        TRUE /* fUnmarshall */);
3408   }
3409 
3410   if (pointer_buffer_mark_set)
3411   {
3412     pStubMsg->Buffer = pStubMsg->PointerBufferMark;
3413     pStubMsg->PointerBufferMark = NULL;
3414   }
3415 
3416   return NULL;
3417 }
3418 
3419 /***********************************************************************
3420  *           NdrComplexStructBufferSize [RPCRT4.@]
3421  */
3422 void WINAPI NdrComplexStructBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
3423                                        unsigned char *pMemory,
3424                                        PFORMAT_STRING pFormat)
3425 {
3426   PFORMAT_STRING conf_array = NULL;
3427   PFORMAT_STRING pointer_desc = NULL;
3428   unsigned char *OldMemory = pStubMsg->Memory;
3429   int pointer_length_set = 0;
3430   ULONG count = 0;
3431   ULONG max_count = 0;
3432   ULONG offset = 0;
3433 
3434   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
3435 
3436   ALIGN_LENGTH(pStubMsg->BufferLength, pFormat[1] + 1);
3437 
3438   if(!pStubMsg->IgnoreEmbeddedPointers && !pStubMsg->PointerLength)
3439   {
3440     int saved_ignore_embedded = pStubMsg->IgnoreEmbeddedPointers;
3441     unsigned long saved_buffer_length = pStubMsg->BufferLength;
3442 
3443     /* get the buffer length after complex struct data, but before
3444      * pointer data */
3445     pStubMsg->IgnoreEmbeddedPointers = 1;
3446     NdrComplexStructBufferSize(pStubMsg, pMemory, pFormat);
3447     pStubMsg->IgnoreEmbeddedPointers = saved_ignore_embedded;
3448 
3449     /* save it for use by embedded pointer code later */
3450     pStubMsg->PointerLength = pStubMsg->BufferLength;
3451     pointer_length_set = 1;
3452     TRACE("difference = 0x%lx\n", pStubMsg->PointerLength - saved_buffer_length);
3453 
3454     /* restore the original buffer length */
3455     pStubMsg->BufferLength = saved_buffer_length;
3456   }
3457 
3458   pFormat += 4;
3459   if (*(const SHORT*)pFormat) conf_array = pFormat + *(const SHORT*)pFormat;
3460   pFormat += 2;
3461   if (*(const WORD*)pFormat) pointer_desc = pFormat + *(const WORD*)pFormat;
3462   pFormat += 2;
3463 
3464   pStubMsg->Memory = pMemory;
3465 
3466   if (conf_array)
3467   {
3468     unsigned long struct_size = ComplexStructSize(pStubMsg, pFormat);
3469     array_compute_and_size_conformance(conf_array[0], pStubMsg, pMemory + struct_size,
3470                                        conf_array);
3471 
3472     /* these could be changed in ComplexMarshall so save them for later */
3473     max_count = pStubMsg->MaxCount;
3474     count = pStubMsg->ActualCount;
3475     offset = pStubMsg->Offset;
3476   }
3477 
3478   pMemory = ComplexBufferSize(pStubMsg, pMemory, pFormat, pointer_desc);
3479 
3480   if (conf_array)
3481   {
3482     pStubMsg->MaxCount = max_count;
3483     pStubMsg->ActualCount = count;
3484     pStubMsg->Offset = offset;
3485     array_buffer_size(conf_array[0], pStubMsg, pMemory, conf_array,
3486                       TRUE /* fHasPointers */);
3487   }
3488 
3489   pStubMsg->Memory = OldMemory;
3490 
3491   if(pointer_length_set)
3492   {
3493     pStubMsg->BufferLength = pStubMsg->PointerLength;
3494     pStubMsg->PointerLength = 0;
3495   }
3496 
3497 }
3498 
3499 /***********************************************************************
3500  *           NdrComplexStructMemorySize [RPCRT4.@]
3501  */
3502 ULONG WINAPI NdrComplexStructMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
3503                                         PFORMAT_STRING pFormat)
3504 {
3505   unsigned size = *(const WORD*)(pFormat+2);
3506   PFORMAT_STRING conf_array = NULL;
3507   PFORMAT_STRING pointer_desc = NULL;
3508   ULONG count = 0;
3509   ULONG max_count = 0;
3510   ULONG offset = 0;
3511 
3512   TRACE("(%p,%p)\n", pStubMsg, pFormat);
3513 
3514   ALIGN_POINTER(pStubMsg->Buffer, pFormat[1] + 1);
3515 
3516   pFormat += 4;
3517   if (*(const SHORT*)pFormat) conf_array = pFormat + *(const SHORT*)pFormat;
3518   pFormat += 2;
3519   if (*(const WORD*)pFormat) pointer_desc = pFormat + *(const WORD*)pFormat;
3520   pFormat += 2;
3521 
3522   if (conf_array)
3523   {
3524     array_read_conformance(conf_array[0], pStubMsg, conf_array);
3525 
3526     /* these could be changed in ComplexStructMemorySize so save them for
3527      * later */
3528     max_count = pStubMsg->MaxCount;
3529     count = pStubMsg->ActualCount;
3530     offset = pStubMsg->Offset;
3531   }
3532 
3533   ComplexStructMemorySize(pStubMsg, pFormat, pointer_desc);
3534 
3535   if (conf_array)
3536   {
3537     pStubMsg->MaxCount = max_count;
3538     pStubMsg->ActualCount = count;
3539     pStubMsg->Offset = offset;
3540     array_memory_size(conf_array[0], pStubMsg, conf_array,
3541                       TRUE /* fHasPointers */);
3542   }
3543 
3544   return size;
3545 }
3546 
3547 /***********************************************************************
3548  *           NdrComplexStructFree [RPCRT4.@]
3549  */
3550 void WINAPI NdrComplexStructFree(PMIDL_STUB_MESSAGE pStubMsg,
3551                                  unsigned char *pMemory,
3552                                  PFORMAT_STRING pFormat)
3553 {
3554   PFORMAT_STRING conf_array = NULL;
3555   PFORMAT_STRING pointer_desc = NULL;
3556   unsigned char *OldMemory = pStubMsg->Memory;
3557 
3558   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
3559 
3560   pFormat += 4;
3561   if (*(const SHORT*)pFormat) conf_array = pFormat + *(const SHORT*)pFormat;
3562   pFormat += 2;
3563   if (*(const WORD*)pFormat) pointer_desc = pFormat + *(const WORD*)pFormat;
3564   pFormat += 2;
3565 
3566   pStubMsg->Memory = pMemory;
3567 
3568   pMemory = ComplexFree(pStubMsg, pMemory, pFormat, pointer_desc);
3569 
3570   if (conf_array)
3571     array_free(conf_array[0], pStubMsg, pMemory, conf_array,
3572                TRUE /* fHasPointers */);
3573 
3574   pStubMsg->Memory = OldMemory;
3575 }
3576 
3577 /***********************************************************************
3578  *           NdrConformantArrayMarshall [RPCRT4.@]
3579  */
3580 unsigned char * WINAPI NdrConformantArrayMarshall(PMIDL_STUB_MESSAGE pStubMsg,
3581                                                   unsigned char *pMemory,
3582                                                   PFORMAT_STRING pFormat)
3583 {
3584   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
3585   if (pFormat[0] != RPC_FC_CARRAY)
3586   {
3587     ERR("invalid format = 0x%x\n", pFormat[0]);
3588     RpcRaiseException(RPC_X_BAD_STUB_DATA);
3589   }
3590 
3591   array_compute_and_write_conformance(RPC_FC_CARRAY, pStubMsg, pMemory,
3592                                       pFormat);
3593   array_write_variance_and_marshall(RPC_FC_CARRAY, pStubMsg, pMemory, pFormat,
3594                                     TRUE /* fHasPointers */);
3595 
3596   return NULL;
3597 }
3598 
3599 /***********************************************************************
3600  *           NdrConformantArrayUnmarshall [RPCRT4.@]