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