Sync to Wine-0_9_3:
[reactos.git] / reactos / lib / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * TODO:
21 * - figure out whether we *really* got this right
22 * - check for errors and throw exceptions
23 * - what are the marshalling functions supposed to return?
24 * - finish RpcStream_Vtbl
25 */
26
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #define COBJMACROS
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "winreg.h"
39
40 #include "objbase.h"
41
42 #include "ndr_misc.h"
43 #include "rpcndr.h"
44 #include "wine/rpcfc.h"
45
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(ole);
49
50 static HMODULE hOLE;
51
52 static HRESULT (WINAPI *COM_GetMarshalSizeMax)(ULONG *,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
53 static HRESULT (WINAPI *COM_MarshalInterface)(LPSTREAM,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
54 static HRESULT (WINAPI *COM_UnmarshalInterface)(LPSTREAM,REFIID,LPVOID*);
55 static HRESULT (WINAPI *COM_ReleaseMarshalData)(LPSTREAM);
56 static HRESULT (WINAPI *COM_GetClassObject)(REFCLSID,DWORD,COSERVERINFO *,REFIID,LPVOID *);
57 static HRESULT (WINAPI *COM_GetPSClsid)(REFIID,CLSID *);
58 static LPVOID (WINAPI *COM_MemAlloc)(ULONG);
59 static void (WINAPI *COM_MemFree)(LPVOID);
60
61 static HMODULE LoadCOM(void)
62 {
63 if (hOLE) return hOLE;
64 hOLE = LoadLibraryA("OLE32.DLL");
65 if (!hOLE) return 0;
66 COM_GetMarshalSizeMax = (LPVOID)GetProcAddress(hOLE, "CoGetMarshalSizeMax");
67 COM_MarshalInterface = (LPVOID)GetProcAddress(hOLE, "CoMarshalInterface");
68 COM_UnmarshalInterface = (LPVOID)GetProcAddress(hOLE, "CoUnmarshalInterface");
69 COM_ReleaseMarshalData = (LPVOID)GetProcAddress(hOLE, "CoReleaseMarshalData");
70 COM_GetClassObject = (LPVOID)GetProcAddress(hOLE, "CoGetClassObject");
71 COM_GetPSClsid = (LPVOID)GetProcAddress(hOLE, "CoGetPSClsid");
72 COM_MemAlloc = (LPVOID)GetProcAddress(hOLE, "CoTaskMemAlloc");
73 COM_MemFree = (LPVOID)GetProcAddress(hOLE, "CoTaskMemFree");
74 return hOLE;
75 }
76
77 /* CoMarshalInterface/CoUnmarshalInterface works on streams,
78 * so implement a simple stream on top of the RPC buffer
79 * (which also implements the MInterfacePointer structure) */
80 typedef struct RpcStreamImpl
81 {
82 const IStreamVtbl *lpVtbl;
83 DWORD RefCount;
84 PMIDL_STUB_MESSAGE pMsg;
85 LPDWORD size;
86 char *data;
87 DWORD pos;
88 } RpcStreamImpl;
89
90 static HRESULT WINAPI RpcStream_QueryInterface(LPSTREAM iface,
91 REFIID riid,
92 LPVOID *obj)
93 {
94 RpcStreamImpl *This = (RpcStreamImpl *)iface;
95 if (IsEqualGUID(&IID_IUnknown, riid) ||
96 IsEqualGUID(&IID_ISequentialStream, riid) ||
97 IsEqualGUID(&IID_IStream, riid)) {
98 *obj = This;
99 This->RefCount++;
100 return S_OK;
101 }
102 return E_NOINTERFACE;
103 }
104
105 static ULONG WINAPI RpcStream_AddRef(LPSTREAM iface)
106 {
107 RpcStreamImpl *This = (RpcStreamImpl *)iface;
108 return ++(This->RefCount);
109 }
110
111 static ULONG WINAPI RpcStream_Release(LPSTREAM iface)
112 {
113 RpcStreamImpl *This = (RpcStreamImpl *)iface;
114 if (!--(This->RefCount)) {
115 TRACE("size=%ld\n", *This->size);
116 This->pMsg->Buffer = (unsigned char*)This->data + *This->size;
117 HeapFree(GetProcessHeap(),0,This);
118 return 0;
119 }
120 return This->RefCount;
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 > (char *)This->pMsg->BufferEnd)
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 = (char*)(This->size + 1);
220 This->pos = 0;
221 if (init) *This->size = 0;
222 TRACE("init size=%ld\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 hr = COM_MarshalInterface(stream, riid, (LPUNKNOWN)pMemory,
261 pStubMsg->dwDestContext, pStubMsg->pvDestContext,
262 MSHLFLAGS_NORMAL);
263 IStream_Release(stream);
264 if (FAILED(hr)) {
265 IUnknown_Release((LPUNKNOWN)pMemory);
266 RpcRaiseException(hr);
267 }
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 FIXME("(%p,%p): stub\n", pStubMsg, pFormat);
326 return 0;
327 }
328
329 /***********************************************************************
330 * NdrInterfacePointerFree [RPCRT4.@]
331 */
332 void WINAPI NdrInterfacePointerFree(PMIDL_STUB_MESSAGE pStubMsg,
333 unsigned char *pMemory,
334 PFORMAT_STRING pFormat)
335 {
336 LPUNKNOWN pUnk = (LPUNKNOWN)pMemory;
337 TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
338 if (pUnk) IUnknown_Release(pUnk);
339 }
340
341 /***********************************************************************
342 * NdrOleAllocate [RPCRT4.@]
343 */
344 void * WINAPI NdrOleAllocate(size_t Size)
345 {
346 if (!LoadCOM()) return NULL;
347 return COM_MemAlloc(Size);
348 }
349
350 /***********************************************************************
351 * NdrOleFree [RPCRT4.@]
352 */
353 void WINAPI NdrOleFree(void *NodeToFree)
354 {
355 if (!LoadCOM()) return;
356 COM_MemFree(NodeToFree);
357 }
358
359 /* internal */
360 HRESULT RPCRT4_GetPSFactory(REFIID riid, LPPSFACTORYBUFFER *pPS)
361 {
362 HRESULT hr;
363 CLSID clsid;
364
365 if (!LoadCOM()) return RPC_E_UNEXPECTED;
366 hr = COM_GetPSClsid(riid, &clsid);
367 if (FAILED(hr)) return hr;
368 hr = COM_GetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
369 &IID_IPSFactoryBuffer, (LPVOID *)pPS);
370 return hr;
371 }