rpcrt4: Don't advance the buffer for FC_STRUCTPAD2.
[wine] / dlls / rpcrt4 / ndr_ole.c
1 /*
2  * OLE32 callouts, COM interface marshalling
3  *
4  * Copyright 2001 Ove Kåven, TransGaming Technologies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * TODO:
21  *  - fix the wire-protocol to match MS/RPC
22  *  - finish RpcStream_Vtbl
23  */
24
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #define COBJMACROS
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "winreg.h"
37
38 #include "objbase.h"
39
40 #include "ndr_misc.h"
41 #include "rpcndr.h"
42 #include "wine/rpcfc.h"
43
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(ole);
47
48 static HMODULE hOLE;
49
50 static HRESULT (WINAPI *COM_GetMarshalSizeMax)(ULONG *,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
51 static HRESULT (WINAPI *COM_MarshalInterface)(LPSTREAM,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
52 static HRESULT (WINAPI *COM_UnmarshalInterface)(LPSTREAM,REFIID,LPVOID*);
53 static HRESULT (WINAPI *COM_ReleaseMarshalData)(LPSTREAM);
54 static HRESULT (WINAPI *COM_GetClassObject)(REFCLSID,DWORD,COSERVERINFO *,REFIID,LPVOID *);
55 static HRESULT (WINAPI *COM_GetPSClsid)(REFIID,CLSID *);
56 static LPVOID (WINAPI *COM_MemAlloc)(ULONG);
57 static void (WINAPI *COM_MemFree)(LPVOID);
58
59 static HMODULE LoadCOM(void)
60 {
61   if (hOLE) return hOLE;
62   hOLE = LoadLibraryA("OLE32.DLL");
63   if (!hOLE) return 0;
64   COM_GetMarshalSizeMax  = (LPVOID)GetProcAddress(hOLE, "CoGetMarshalSizeMax");
65   COM_MarshalInterface   = (LPVOID)GetProcAddress(hOLE, "CoMarshalInterface");
66   COM_UnmarshalInterface = (LPVOID)GetProcAddress(hOLE, "CoUnmarshalInterface");
67   COM_ReleaseMarshalData = (LPVOID)GetProcAddress(hOLE, "CoReleaseMarshalData");
68   COM_GetClassObject     = (LPVOID)GetProcAddress(hOLE, "CoGetClassObject");
69   COM_GetPSClsid         = (LPVOID)GetProcAddress(hOLE, "CoGetPSClsid");
70   COM_MemAlloc = (LPVOID)GetProcAddress(hOLE, "CoTaskMemAlloc");
71   COM_MemFree  = (LPVOID)GetProcAddress(hOLE, "CoTaskMemFree");
72   return hOLE;
73 }
74
75 /* CoMarshalInterface/CoUnmarshalInterface works on streams,
76  * so implement a simple stream on top of the RPC buffer
77  * (which also implements the MInterfacePointer structure) */
78 typedef struct RpcStreamImpl
79 {
80   const IStreamVtbl *lpVtbl;
81   DWORD RefCount;
82   PMIDL_STUB_MESSAGE pMsg;
83   LPDWORD size;
84   char *data;
85   DWORD pos;
86 } RpcStreamImpl;
87
88 static HRESULT WINAPI RpcStream_QueryInterface(LPSTREAM iface,
89                                               REFIID riid,
90                                               LPVOID *obj)
91 {
92   RpcStreamImpl *This = (RpcStreamImpl *)iface;
93   if (IsEqualGUID(&IID_IUnknown, riid) ||
94       IsEqualGUID(&IID_ISequentialStream, riid) ||
95       IsEqualGUID(&IID_IStream, riid)) {
96     *obj = This;
97     This->RefCount++;
98     return S_OK;
99   }
100   return E_NOINTERFACE;
101 }
102
103 static ULONG WINAPI RpcStream_AddRef(LPSTREAM iface)
104 {
105   RpcStreamImpl *This = (RpcStreamImpl *)iface;
106   return ++(This->RefCount);
107 }
108
109 static ULONG WINAPI RpcStream_Release(LPSTREAM iface)
110 {
111   RpcStreamImpl *This = (RpcStreamImpl *)iface;
112   if (!--(This->RefCount)) {
113     TRACE("size=%ld\n", *This->size);
114     This->pMsg->Buffer = (unsigned char*)This->data + *This->size;
115     HeapFree(GetProcessHeap(),0,This);
116     return 0;
117   }
118   return This->RefCount;
119 }
120
121 static HRESULT WINAPI RpcStream_Read(LPSTREAM iface,
122                                     void *pv,
123                                     ULONG cb,
124                                     ULONG *pcbRead)
125 {
126   RpcStreamImpl *This = (RpcStreamImpl *)iface;
127   HRESULT hr = S_OK;
128   if (This->pos + cb > *This->size)
129   {
130     cb = *This->size - This->pos;
131     hr = S_FALSE;
132   }
133   if (cb) {
134     memcpy(pv, This->data + This->pos, cb);
135     This->pos += cb;
136   }
137   if (pcbRead) *pcbRead = cb;
138   return hr;
139 }
140
141 static HRESULT WINAPI RpcStream_Write(LPSTREAM iface,
142                                      const void *pv,
143                                      ULONG cb,
144                                      ULONG *pcbWritten)
145 {
146   RpcStreamImpl *This = (RpcStreamImpl *)iface;
147   if (This->data + cb > (char *)This->pMsg->BufferEnd)
148     return STG_E_MEDIUMFULL;
149   memcpy(This->data + This->pos, pv, cb);
150   This->pos += cb;
151   if (This->pos > *This->size) *This->size = This->pos;
152   if (pcbWritten) *pcbWritten = cb;
153   return S_OK;
154 }
155
156 static HRESULT WINAPI RpcStream_Seek(LPSTREAM iface,
157                                     LARGE_INTEGER move,
158                                     DWORD origin,
159                                     ULARGE_INTEGER *newPos)
160 {
161   RpcStreamImpl *This = (RpcStreamImpl *)iface;
162   switch (origin) {
163   case STREAM_SEEK_SET:
164     This->pos = move.u.LowPart;
165     break;
166   case STREAM_SEEK_CUR:
167     This->pos = This->pos + move.u.LowPart;
168     break;
169   case STREAM_SEEK_END:
170     This->pos = *This->size + move.u.LowPart;
171     break;
172   default:
173     return STG_E_INVALIDFUNCTION;
174   }
175   if (newPos) {
176     newPos->u.LowPart = This->pos;
177     newPos->u.HighPart = 0;
178   }
179   return S_OK;
180 }
181
182 static HRESULT WINAPI RpcStream_SetSize(LPSTREAM iface,
183                                        ULARGE_INTEGER newSize)
184 {
185   RpcStreamImpl *This = (RpcStreamImpl *)iface;
186   *This->size = newSize.u.LowPart;
187   return S_OK;
188 }
189
190 static const IStreamVtbl RpcStream_Vtbl =
191 {
192   RpcStream_QueryInterface,
193   RpcStream_AddRef,
194   RpcStream_Release,
195   RpcStream_Read,
196   RpcStream_Write,
197   RpcStream_Seek,
198   RpcStream_SetSize,
199   NULL, /* CopyTo */
200   NULL, /* Commit */
201   NULL, /* Revert */
202   NULL, /* LockRegion */
203   NULL, /* UnlockRegion */
204   NULL, /* Stat */
205   NULL  /* Clone */
206 };
207
208 static LPSTREAM RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg, BOOL init)
209 {
210   RpcStreamImpl *This;
211   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RpcStreamImpl));
212   if (!This) return NULL;
213   This->lpVtbl = &RpcStream_Vtbl;
214   This->RefCount = 1;
215   This->pMsg = pStubMsg;
216   This->size = (LPDWORD)pStubMsg->Buffer;
217   This->data = (char*)(This->size + 1);
218   This->pos = 0;
219   if (init) *This->size = 0;
220   TRACE("init size=%ld\n", *This->size);
221   return (LPSTREAM)This;
222 }
223
224 static const IID* get_ip_iid(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
225 {
226   const IID *riid;
227   if (!pFormat) return &IID_IUnknown;
228   TRACE("format=%02x %02x\n", pFormat[0], pFormat[1]);
229   if (pFormat[0] != RPC_FC_IP) FIXME("format=%d\n", pFormat[0]);
230   if (pFormat[1] == RPC_FC_CONSTANT_IID) {
231     riid = (const IID *)&pFormat[2];
232   } else {
233     ComputeConformance(pStubMsg, pMemory, pFormat+2, 0);
234     riid = (const IID *)pStubMsg->MaxCount;
235   }
236   if (!riid) riid = &IID_IUnknown;
237   TRACE("got %s\n", debugstr_guid(riid));
238   return riid;
239 }
240
241 /***********************************************************************
242  *           NdrInterfacePointerMarshall [RPCRT4.@]
243  */
244 unsigned char * WINAPI NdrInterfacePointerMarshall(PMIDL_STUB_MESSAGE pStubMsg,
245                                                   unsigned char *pMemory,
246                                                   PFORMAT_STRING pFormat)
247 {
248   const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
249   LPSTREAM stream;
250   HRESULT hr;
251
252   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
253   pStubMsg->MaxCount = 0;
254   if (!LoadCOM()) return NULL;
255   if (pStubMsg->Buffer + sizeof(DWORD) <= (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength) {
256     stream = RpcStream_Create(pStubMsg, TRUE);
257     if (stream) {
258       if (pMemory)
259         hr = COM_MarshalInterface(stream, riid, (LPUNKNOWN)pMemory,
260                                   pStubMsg->dwDestContext, pStubMsg->pvDestContext,
261                                   MSHLFLAGS_NORMAL);
262       else
263         hr = S_OK;
264
265       IStream_Release(stream);
266       if (FAILED(hr))
267         RpcRaiseException(hr);
268     }
269   }
270   return NULL;
271 }
272
273 /***********************************************************************
274  *           NdrInterfacePointerUnmarshall [RPCRT4.@]
275  */
276 unsigned char * WINAPI NdrInterfacePointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
277                                                     unsigned char **ppMemory,
278                                                     PFORMAT_STRING pFormat,
279                                                     unsigned char fMustAlloc)
280 {
281   LPSTREAM stream;
282   HRESULT hr;
283
284   TRACE("(%p,%p,%p,%d)\n", pStubMsg, ppMemory, pFormat, fMustAlloc);
285   if (!LoadCOM()) return NULL;
286   *(LPVOID*)ppMemory = NULL;
287   if (pStubMsg->Buffer + sizeof(DWORD) < (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength) {
288     stream = RpcStream_Create(pStubMsg, FALSE);
289     if (stream) {
290       hr = COM_UnmarshalInterface(stream, &IID_NULL, (LPVOID*)ppMemory);
291       IStream_Release(stream);
292       if (FAILED(hr))
293         RpcRaiseException(hr);
294     }
295   }
296   return NULL;
297 }
298
299 /***********************************************************************
300  *           NdrInterfacePointerBufferSize [RPCRT4.@]
301  */
302 void WINAPI NdrInterfacePointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
303                                          unsigned char *pMemory,
304                                          PFORMAT_STRING pFormat)
305 {
306   const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
307   ULONG size = 0;
308   HRESULT hr;
309
310   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
311   if (!LoadCOM()) return;
312   hr = COM_GetMarshalSizeMax(&size, riid, (LPUNKNOWN)pMemory,
313                             pStubMsg->dwDestContext, pStubMsg->pvDestContext,
314                             MSHLFLAGS_NORMAL);
315   TRACE("size=%ld\n", size);
316   pStubMsg->BufferLength += sizeof(DWORD) + size;
317 }
318
319 /***********************************************************************
320  *           NdrInterfacePointerMemorySize [RPCRT4.@]
321  */
322 unsigned long WINAPI NdrInterfacePointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
323                                                   PFORMAT_STRING pFormat)
324 {
325   ULONG size;
326
327   TRACE("(%p,%p)\n", pStubMsg, pFormat);
328
329   size = *(ULONG *)pStubMsg->Buffer;
330   pStubMsg->Buffer += 4;
331   pStubMsg->MemorySize += 4;
332
333   pStubMsg->Buffer += size;
334
335   return pStubMsg->MemorySize;
336 }
337
338 /***********************************************************************
339  *           NdrInterfacePointerFree [RPCRT4.@]
340  */
341 void WINAPI NdrInterfacePointerFree(PMIDL_STUB_MESSAGE pStubMsg,
342                                    unsigned char *pMemory,
343                                    PFORMAT_STRING pFormat)
344 {
345   LPUNKNOWN pUnk = (LPUNKNOWN)pMemory;
346   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
347   if (pUnk) IUnknown_Release(pUnk);
348 }
349
350 /***********************************************************************
351  *           NdrOleAllocate [RPCRT4.@]
352  */
353 void * WINAPI NdrOleAllocate(size_t Size)
354 {
355   if (!LoadCOM()) return NULL;
356   return COM_MemAlloc(Size);
357 }
358
359 /***********************************************************************
360  *           NdrOleFree [RPCRT4.@]
361  */
362 void WINAPI NdrOleFree(void *NodeToFree)
363 {
364   if (!LoadCOM()) return;
365   COM_MemFree(NodeToFree);
366 }