Import and merge Wine-20041201
[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 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 = 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 if (This->pos + cb > *This->size) cb = *This->size - This->pos;
130 if (cb) {
131 memcpy(pv, This->data + This->pos, cb);
132 This->pos += cb;
133 }
134 if (pcbRead) *pcbRead = cb;
135 return S_OK;
136 }
137
138 static HRESULT WINAPI RpcStream_Write(LPSTREAM iface,
139 const void *pv,
140 ULONG cb,
141 ULONG *pcbWritten)
142 {
143 RpcStreamImpl *This = (RpcStreamImpl *)iface;
144 memcpy(This->data + This->pos, pv, cb);
145 This->pos += cb;
146 if (This->pos > *This->size) *This->size = This->pos;
147 if (pcbWritten) *pcbWritten = cb;
148 return S_OK;
149 }
150
151 static HRESULT WINAPI RpcStream_Seek(LPSTREAM iface,
152 LARGE_INTEGER move,
153 DWORD origin,
154 ULARGE_INTEGER *newPos)
155 {
156 RpcStreamImpl *This = (RpcStreamImpl *)iface;
157 switch (origin) {
158 case STREAM_SEEK_SET:
159 This->pos = move.u.LowPart;
160 break;
161 case STREAM_SEEK_CUR:
162 This->pos = This->pos + move.u.LowPart;
163 break;
164 case STREAM_SEEK_END:
165 This->pos = *This->size + move.u.LowPart;
166 break;
167 default:
168 return STG_E_INVALIDFUNCTION;
169 }
170 if (newPos) {
171 newPos->u.LowPart = This->pos;
172 newPos->u.HighPart = 0;
173 }
174 return S_OK;
175 }
176
177 static HRESULT WINAPI RpcStream_SetSize(LPSTREAM iface,
178 ULARGE_INTEGER newSize)
179 {
180 RpcStreamImpl *This = (RpcStreamImpl *)iface;
181 *This->size = newSize.u.LowPart;
182 return S_OK;
183 }
184
185 static IStreamVtbl RpcStream_Vtbl =
186 {
187 RpcStream_QueryInterface,
188 RpcStream_AddRef,
189 RpcStream_Release,
190 RpcStream_Read,
191 RpcStream_Write,
192 RpcStream_Seek,
193 RpcStream_SetSize,
194 NULL, /* CopyTo */
195 NULL, /* Commit */
196 NULL, /* Revert */
197 NULL, /* LockRegion */
198 NULL, /* UnlockRegion */
199 NULL, /* Stat */
200 NULL /* Clone */
201 };
202
203 static LPSTREAM RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg, BOOL init)
204 {
205 RpcStreamImpl *This;
206 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RpcStreamImpl));
207 if (!This) return NULL;
208 This->lpVtbl = &RpcStream_Vtbl;
209 This->RefCount = 1;
210 This->pMsg = pStubMsg;
211 This->size = (LPDWORD)pStubMsg->Buffer;
212 This->data = (char*)(This->size + 1);
213 This->pos = 0;
214 if (init) *This->size = 0;
215 TRACE("init size=%ld\n", *This->size);
216 return (LPSTREAM)This;
217 }
218
219 const IID* get_ip_iid(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
220 {
221 const IID *riid;
222 if (!pFormat) return &IID_IUnknown;
223 TRACE("format=%02x %02x\n", pFormat[0], pFormat[1]);
224 if (pFormat[0] != RPC_FC_IP) FIXME("format=%d\n", pFormat[0]);
225 if (pFormat[1] == RPC_FC_CONSTANT_IID) {
226 riid = (const IID *)&pFormat[2];
227 } else {
228 ComputeConformance(pStubMsg, pMemory, pFormat+2, 0);
229 riid = (const IID *)pStubMsg->MaxCount;
230 }
231 if (!riid) riid = &IID_IUnknown;
232 TRACE("got %s\n", debugstr_guid(riid));
233 return riid;
234 }
235
236 /***********************************************************************
237 * NdrInterfacePointerMarshall [RPCRT4.@]
238 */
239 unsigned char * WINAPI NdrInterfacePointerMarshall(PMIDL_STUB_MESSAGE pStubMsg,
240 unsigned char *pMemory,
241 PFORMAT_STRING pFormat)
242 {
243 const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
244 LPSTREAM stream;
245 HRESULT hr;
246
247 TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
248 pStubMsg->MaxCount = 0;
249 if (!LoadCOM()) return NULL;
250 stream = RpcStream_Create(pStubMsg, TRUE);
251 hr = COM_MarshalInterface(stream, riid, (LPUNKNOWN)pMemory,
252 pStubMsg->dwDestContext, pStubMsg->pvDestContext,
253 MSHLFLAGS_NORMAL);
254 IStream_Release(stream);
255 return NULL;
256 }
257
258 /***********************************************************************
259 * NdrInterfacePointerUnmarshall [RPCRT4.@]
260 */
261 unsigned char * WINAPI NdrInterfacePointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
262 unsigned char **ppMemory,
263 PFORMAT_STRING pFormat,
264 unsigned char fMustAlloc)
265 {
266 LPSTREAM stream;
267 HRESULT hr;
268
269 TRACE("(%p,%p,%p,%d)\n", pStubMsg, ppMemory, pFormat, fMustAlloc);
270 if (!LoadCOM()) return NULL;
271 *(LPVOID*)ppMemory = NULL;
272 stream = RpcStream_Create(pStubMsg, FALSE);
273 hr = COM_UnmarshalInterface(stream, &IID_NULL, (LPVOID*)ppMemory);
274 IStream_Release(stream);
275 return NULL;
276 }
277
278 /***********************************************************************
279 * NdrInterfacePointerBufferSize [RPCRT4.@]
280 */
281 void WINAPI NdrInterfacePointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
282 unsigned char *pMemory,
283 PFORMAT_STRING pFormat)
284 {
285 const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
286 ULONG size = 0;
287 HRESULT hr;
288
289 TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
290 if (!LoadCOM()) return;
291 hr = COM_GetMarshalSizeMax(&size, riid, (LPUNKNOWN)pMemory,
292 pStubMsg->dwDestContext, pStubMsg->pvDestContext,
293 MSHLFLAGS_NORMAL);
294 TRACE("size=%ld\n", size);
295 pStubMsg->BufferLength += sizeof(DWORD) + size;
296 }
297
298 /***********************************************************************
299 * NdrInterfacePointerMemorySize [RPCRT4.@]
300 */
301 unsigned long WINAPI NdrInterfacePointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
302 PFORMAT_STRING pFormat)
303 {
304 FIXME("(%p,%p): stub\n", pStubMsg, pFormat);
305 return 0;
306 }
307
308 /***********************************************************************
309 * NdrInterfacePointerFree [RPCRT4.@]
310 */
311 void WINAPI NdrInterfacePointerFree(PMIDL_STUB_MESSAGE pStubMsg,
312 unsigned char *pMemory,
313 PFORMAT_STRING pFormat)
314 {
315 LPUNKNOWN pUnk = (LPUNKNOWN)pMemory;
316 TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
317 if (pUnk) IUnknown_Release(pUnk);
318 }
319
320 /***********************************************************************
321 * NdrOleAllocate [RPCRT4.@]
322 */
323 void * WINAPI NdrOleAllocate(size_t Size)
324 {
325 if (!LoadCOM()) return NULL;
326 return COM_MemAlloc(Size);
327 }
328
329 /***********************************************************************
330 * NdrOleFree [RPCRT4.@]
331 */
332 void WINAPI NdrOleFree(void *NodeToFree)
333 {
334 if (!LoadCOM()) return;
335 COM_MemFree(NodeToFree);
336 }
337
338 /* internal */
339 HRESULT RPCRT4_GetPSFactory(REFIID riid, LPPSFACTORYBUFFER *pPS)
340 {
341 HRESULT hr;
342 CLSID clsid;
343
344 if (!LoadCOM()) return RPC_E_UNEXPECTED;
345 hr = COM_GetPSClsid(riid, &clsid);
346 if (FAILED(hr)) return hr;
347 hr = COM_GetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
348 &IID_IPSFactoryBuffer, (LPVOID *)pPS);
349 return hr;
350 }