Synchronize with trunk r58457.
[reactos.git] / dll / win32 / 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 #define WIN32_NO_STATUS
26 #define _INC_WINDOWS
27
28 #include <stdarg.h>
29 //#include <stdio.h>
30 //#include <string.h>
31
32 #define COBJMACROS
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
35
36 #include <windef.h>
37 #include <winbase.h>
38 //#include "winerror.h"
39
40 #include <objbase.h>
41
42 #include "ndr_misc.h"
43 //#include "rpcndr.h"
44 //#include "rpcproxy.h"
45 #include <wine/rpcfc.h>
46 //#include "cpsf.h"
47
48 #include <wine/debug.h>
49
50 WINE_DEFAULT_DEBUG_CHANNEL(ole);
51
52 static HMODULE hOLE;
53
54 static HRESULT (WINAPI *COM_GetMarshalSizeMax)(ULONG *,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
55 static HRESULT (WINAPI *COM_MarshalInterface)(LPSTREAM,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
56 static HRESULT (WINAPI *COM_UnmarshalInterface)(LPSTREAM,REFIID,LPVOID*);
57 static HRESULT (WINAPI *COM_ReleaseMarshalData)(LPSTREAM);
58 static HRESULT (WINAPI *COM_GetClassObject)(REFCLSID,DWORD,COSERVERINFO *,REFIID,LPVOID *);
59 static HRESULT (WINAPI *COM_GetPSClsid)(REFIID,CLSID *);
60 static LPVOID (WINAPI *COM_MemAlloc)(ULONG);
61 static void (WINAPI *COM_MemFree)(LPVOID);
62
63 static HMODULE LoadCOM(void)
64 {
65 if (hOLE) return hOLE;
66 hOLE = LoadLibraryA("OLE32.DLL");
67 if (!hOLE) return 0;
68 COM_GetMarshalSizeMax = (LPVOID)GetProcAddress(hOLE, "CoGetMarshalSizeMax");
69 COM_MarshalInterface = (LPVOID)GetProcAddress(hOLE, "CoMarshalInterface");
70 COM_UnmarshalInterface = (LPVOID)GetProcAddress(hOLE, "CoUnmarshalInterface");
71 COM_ReleaseMarshalData = (LPVOID)GetProcAddress(hOLE, "CoReleaseMarshalData");
72 COM_GetClassObject = (LPVOID)GetProcAddress(hOLE, "CoGetClassObject");
73 COM_GetPSClsid = (LPVOID)GetProcAddress(hOLE, "CoGetPSClsid");
74 COM_MemAlloc = (LPVOID)GetProcAddress(hOLE, "CoTaskMemAlloc");
75 COM_MemFree = (LPVOID)GetProcAddress(hOLE, "CoTaskMemFree");
76 return hOLE;
77 }
78
79 /* CoMarshalInterface/CoUnmarshalInterface works on streams,
80 * so implement a simple stream on top of the RPC buffer
81 * (which also implements the MInterfacePointer structure) */
82 typedef struct RpcStreamImpl
83 {
84 IStream IStream_iface;
85 LONG RefCount;
86 PMIDL_STUB_MESSAGE pMsg;
87 LPDWORD size;
88 unsigned char *data;
89 DWORD pos;
90 } RpcStreamImpl;
91
92 static inline RpcStreamImpl *impl_from_IStream(IStream *iface)
93 {
94 return CONTAINING_RECORD(iface, RpcStreamImpl, IStream_iface);
95 }
96
97 static HRESULT WINAPI RpcStream_QueryInterface(LPSTREAM iface,
98 REFIID riid,
99 LPVOID *obj)
100 {
101 RpcStreamImpl *This = impl_from_IStream(iface);
102 if (IsEqualGUID(&IID_IUnknown, riid) ||
103 IsEqualGUID(&IID_ISequentialStream, riid) ||
104 IsEqualGUID(&IID_IStream, riid)) {
105 *obj = This;
106 InterlockedIncrement( &This->RefCount );
107 return S_OK;
108 }
109 return E_NOINTERFACE;
110 }
111
112 static ULONG WINAPI RpcStream_AddRef(LPSTREAM iface)
113 {
114 RpcStreamImpl *This = impl_from_IStream(iface);
115 return InterlockedIncrement( &This->RefCount );
116 }
117
118 static ULONG WINAPI RpcStream_Release(LPSTREAM iface)
119 {
120 RpcStreamImpl *This = impl_from_IStream(iface);
121 ULONG ref = InterlockedDecrement( &This->RefCount );
122 if (!ref) {
123 TRACE("size=%d\n", *This->size);
124 This->pMsg->Buffer = This->data + *This->size;
125 HeapFree(GetProcessHeap(),0,This);
126 return 0;
127 }
128 return ref;
129 }
130
131 static HRESULT WINAPI RpcStream_Read(LPSTREAM iface,
132 void *pv,
133 ULONG cb,
134 ULONG *pcbRead)
135 {
136 RpcStreamImpl *This = impl_from_IStream(iface);
137 HRESULT hr = S_OK;
138 if (This->pos + cb > *This->size)
139 {
140 cb = *This->size - This->pos;
141 hr = S_FALSE;
142 }
143 if (cb) {
144 memcpy(pv, This->data + This->pos, cb);
145 This->pos += cb;
146 }
147 if (pcbRead) *pcbRead = cb;
148 return hr;
149 }
150
151 static HRESULT WINAPI RpcStream_Write(LPSTREAM iface,
152 const void *pv,
153 ULONG cb,
154 ULONG *pcbWritten)
155 {
156 RpcStreamImpl *This = impl_from_IStream(iface);
157 if (This->data + cb > (unsigned char *)This->pMsg->RpcMsg->Buffer + This->pMsg->BufferLength)
158 return STG_E_MEDIUMFULL;
159 memcpy(This->data + This->pos, pv, cb);
160 This->pos += cb;
161 if (This->pos > *This->size) *This->size = This->pos;
162 if (pcbWritten) *pcbWritten = cb;
163 return S_OK;
164 }
165
166 static HRESULT WINAPI RpcStream_Seek(LPSTREAM iface,
167 LARGE_INTEGER move,
168 DWORD origin,
169 ULARGE_INTEGER *newPos)
170 {
171 RpcStreamImpl *This = impl_from_IStream(iface);
172 switch (origin) {
173 case STREAM_SEEK_SET:
174 This->pos = move.u.LowPart;
175 break;
176 case STREAM_SEEK_CUR:
177 This->pos = This->pos + move.u.LowPart;
178 break;
179 case STREAM_SEEK_END:
180 This->pos = *This->size + move.u.LowPart;
181 break;
182 default:
183 return STG_E_INVALIDFUNCTION;
184 }
185 if (newPos) {
186 newPos->u.LowPart = This->pos;
187 newPos->u.HighPart = 0;
188 }
189 return S_OK;
190 }
191
192 static HRESULT WINAPI RpcStream_SetSize(LPSTREAM iface,
193 ULARGE_INTEGER newSize)
194 {
195 RpcStreamImpl *This = impl_from_IStream(iface);
196 *This->size = newSize.u.LowPart;
197 return S_OK;
198 }
199
200 static const IStreamVtbl RpcStream_Vtbl =
201 {
202 RpcStream_QueryInterface,
203 RpcStream_AddRef,
204 RpcStream_Release,
205 RpcStream_Read,
206 RpcStream_Write,
207 RpcStream_Seek,
208 RpcStream_SetSize,
209 NULL, /* CopyTo */
210 NULL, /* Commit */
211 NULL, /* Revert */
212 NULL, /* LockRegion */
213 NULL, /* UnlockRegion */
214 NULL, /* Stat */
215 NULL /* Clone */
216 };
217
218 static LPSTREAM RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg, BOOL init)
219 {
220 RpcStreamImpl *This;
221 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RpcStreamImpl));
222 if (!This) return NULL;
223 This->IStream_iface.lpVtbl = &RpcStream_Vtbl;
224 This->RefCount = 1;
225 This->pMsg = pStubMsg;
226 This->size = (LPDWORD)pStubMsg->Buffer;
227 This->data = (unsigned char*)(This->size + 1);
228 This->pos = 0;
229 if (init) *This->size = 0;
230 TRACE("init size=%d\n", *This->size);
231 return (LPSTREAM)This;
232 }
233
234 static const IID* get_ip_iid(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
235 {
236 const IID *riid;
237 if (!pFormat) return &IID_IUnknown;
238 TRACE("format=%02x %02x\n", pFormat[0], pFormat[1]);
239 if (pFormat[0] != RPC_FC_IP) FIXME("format=%d\n", pFormat[0]);
240 if (pFormat[1] == RPC_FC_CONSTANT_IID) {
241 riid = (const IID *)&pFormat[2];
242 } else {
243 ComputeConformance(pStubMsg, pMemory, pFormat+2, 0);
244 riid = (const IID *)pStubMsg->MaxCount;
245 }
246 if (!riid) riid = &IID_IUnknown;
247 TRACE("got %s\n", debugstr_guid(riid));
248 return riid;
249 }
250
251 /***********************************************************************
252 * NdrInterfacePointerMarshall [RPCRT4.@]
253 */
254 unsigned char * WINAPI NdrInterfacePointerMarshall(PMIDL_STUB_MESSAGE pStubMsg,
255 unsigned char *pMemory,
256 PFORMAT_STRING pFormat)
257 {
258 const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
259 LPSTREAM stream;
260 HRESULT hr;
261
262 TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
263 pStubMsg->MaxCount = 0;
264 if (!LoadCOM()) return NULL;
265 if (pStubMsg->Buffer + sizeof(DWORD) <= (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength) {
266 stream = RpcStream_Create(pStubMsg, TRUE);
267 if (stream) {
268 if (pMemory)
269 hr = COM_MarshalInterface(stream, riid, (LPUNKNOWN)pMemory,
270 pStubMsg->dwDestContext, pStubMsg->pvDestContext,
271 MSHLFLAGS_NORMAL);
272 else
273 hr = S_OK;
274
275 IStream_Release(stream);
276 if (FAILED(hr))
277 RpcRaiseException(hr);
278 }
279 }
280 return NULL;
281 }
282
283 /***********************************************************************
284 * NdrInterfacePointerUnmarshall [RPCRT4.@]
285 */
286 unsigned char * WINAPI NdrInterfacePointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
287 unsigned char **ppMemory,
288 PFORMAT_STRING pFormat,
289 unsigned char fMustAlloc)
290 {
291 LPSTREAM stream;
292 HRESULT hr;
293
294 TRACE("(%p,%p,%p,%d)\n", pStubMsg, ppMemory, pFormat, fMustAlloc);
295 if (!LoadCOM()) return NULL;
296 *(LPVOID*)ppMemory = NULL;
297 if (pStubMsg->Buffer + sizeof(DWORD) < (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength) {
298 stream = RpcStream_Create(pStubMsg, FALSE);
299 if (!stream) RpcRaiseException(E_OUTOFMEMORY);
300 if (*((RpcStreamImpl *)stream)->size != 0)
301 hr = COM_UnmarshalInterface(stream, &IID_NULL, (LPVOID*)ppMemory);
302 else
303 hr = S_OK;
304 IStream_Release(stream);
305 if (FAILED(hr))
306 RpcRaiseException(hr);
307 }
308 return NULL;
309 }
310
311 /***********************************************************************
312 * NdrInterfacePointerBufferSize [RPCRT4.@]
313 */
314 void WINAPI NdrInterfacePointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
315 unsigned char *pMemory,
316 PFORMAT_STRING pFormat)
317 {
318 const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
319 ULONG size = 0;
320
321 TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
322 if (!LoadCOM()) return;
323 COM_GetMarshalSizeMax(&size, riid, (LPUNKNOWN)pMemory,
324 pStubMsg->dwDestContext, pStubMsg->pvDestContext,
325 MSHLFLAGS_NORMAL);
326 TRACE("size=%d\n", size);
327 pStubMsg->BufferLength += sizeof(DWORD) + size;
328 }
329
330 /***********************************************************************
331 * NdrInterfacePointerMemorySize [RPCRT4.@]
332 */
333 ULONG WINAPI NdrInterfacePointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
334 PFORMAT_STRING pFormat)
335 {
336 ULONG size;
337
338 TRACE("(%p,%p)\n", pStubMsg, pFormat);
339
340 size = *(ULONG *)pStubMsg->Buffer;
341 pStubMsg->Buffer += 4;
342 pStubMsg->MemorySize += 4;
343
344 pStubMsg->Buffer += size;
345
346 return pStubMsg->MemorySize;
347 }
348
349 /***********************************************************************
350 * NdrInterfacePointerFree [RPCRT4.@]
351 */
352 void WINAPI NdrInterfacePointerFree(PMIDL_STUB_MESSAGE pStubMsg,
353 unsigned char *pMemory,
354 PFORMAT_STRING pFormat)
355 {
356 LPUNKNOWN pUnk = (LPUNKNOWN)pMemory;
357 TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
358 if (pUnk) IUnknown_Release(pUnk);
359 }
360
361 /***********************************************************************
362 * NdrOleAllocate [RPCRT4.@]
363 */
364 void * WINAPI NdrOleAllocate(SIZE_T Size)
365 {
366 if (!LoadCOM()) return NULL;
367 return COM_MemAlloc(Size);
368 }
369
370 /***********************************************************************
371 * NdrOleFree [RPCRT4.@]
372 */
373 void WINAPI NdrOleFree(void *NodeToFree)
374 {
375 if (!LoadCOM()) return;
376 COM_MemFree(NodeToFree);
377 }
378
379 /***********************************************************************
380 * Helper function to create a proxy.
381 * Probably similar to NdrpCreateProxy.
382 */
383 HRESULT create_proxy(REFIID iid, IUnknown *pUnkOuter, IRpcProxyBuffer **pproxy, void **ppv)
384 {
385 CLSID clsid;
386 IPSFactoryBuffer *psfac;
387 HRESULT r;
388
389 if(!LoadCOM()) return E_FAIL;
390
391 r = COM_GetPSClsid( iid, &clsid );
392 if(FAILED(r)) return r;
393
394 r = COM_GetClassObject( &clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void**)&psfac );
395 if(FAILED(r)) return r;
396
397 r = IPSFactoryBuffer_CreateProxy(psfac, pUnkOuter, iid, pproxy, ppv);
398
399 IPSFactoryBuffer_Release(psfac);
400 return r;
401 }
402
403 /***********************************************************************
404 * Helper function to create a stub.
405 * This probably looks very much like NdrpCreateStub.
406 */
407 HRESULT create_stub(REFIID iid, IUnknown *pUnk, IRpcStubBuffer **ppstub)
408 {
409 CLSID clsid;
410 IPSFactoryBuffer *psfac;
411 HRESULT r;
412
413 if(!LoadCOM()) return E_FAIL;
414
415 r = COM_GetPSClsid( iid, &clsid );
416 if(FAILED(r)) return r;
417
418 r = COM_GetClassObject( &clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void**)&psfac );
419 if(FAILED(r)) return r;
420
421 r = IPSFactoryBuffer_CreateStub(psfac, iid, pUnk, ppstub);
422
423 IPSFactoryBuffer_Release(psfac);
424 return r;
425 }