gdiplus: Added GdipFillPolygon.
[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
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   DWORD RefCount;
83   PMIDL_STUB_MESSAGE pMsg;
84   LPDWORD size;
85   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     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 ++(This->RefCount);
108 }
109
110 static ULONG WINAPI RpcStream_Release(LPSTREAM iface)
111 {
112   RpcStreamImpl *This = (RpcStreamImpl *)iface;
113   if (!--(This->RefCount)) {
114     TRACE("size=%d\n", *This->size);
115     This->pMsg->Buffer = (unsigned char*)This->data + *This->size;
116     HeapFree(GetProcessHeap(),0,This);
117     return 0;
118   }
119   return This->RefCount;
120 }
121
122 static HRESULT WINAPI RpcStream_Read(LPSTREAM iface,
123                                     void *pv,
124                                     ULONG cb,
125                                     ULONG *pcbRead)
126 {
127   RpcStreamImpl *This = (RpcStreamImpl *)iface;
128   HRESULT hr = S_OK;
129   if (This->pos + cb > *This->size)
130   {
131     cb = *This->size - This->pos;
132     hr = S_FALSE;
133   }
134   if (cb) {
135     memcpy(pv, This->data + This->pos, cb);
136     This->pos += cb;
137   }
138   if (pcbRead) *pcbRead = cb;
139   return hr;
140 }
141
142 static HRESULT WINAPI RpcStream_Write(LPSTREAM iface,
143                                      const void *pv,
144                                      ULONG cb,
145                                      ULONG *pcbWritten)
146 {
147   RpcStreamImpl *This = (RpcStreamImpl *)iface;
148   if (This->data + cb > (char *)This->pMsg->BufferEnd)
149     return STG_E_MEDIUMFULL;
150   memcpy(This->data + This->pos, pv, cb);
151   This->pos += cb;
152   if (This->pos > *This->size) *This->size = This->pos;
153   if (pcbWritten) *pcbWritten = cb;
154   return S_OK;
155 }
156
157 static HRESULT WINAPI RpcStream_Seek(LPSTREAM iface,
158                                     LARGE_INTEGER move,
159                                     DWORD origin,
160                                     ULARGE_INTEGER *newPos)
161 {
162   RpcStreamImpl *This = (RpcStreamImpl *)iface;
163   switch (origin) {
164   case STREAM_SEEK_SET:
165     This->pos = move.u.LowPart;
166     break;
167   case STREAM_SEEK_CUR:
168     This->pos = This->pos + move.u.LowPart;
169     break;
170   case STREAM_SEEK_END:
171     This->pos = *This->size + move.u.LowPart;
172     break;
173   default:
174     return STG_E_INVALIDFUNCTION;
175   }
176   if (newPos) {
177     newPos->u.LowPart = This->pos;
178     newPos->u.HighPart = 0;
179   }
180   return S_OK;
181 }
182
183 static HRESULT WINAPI RpcStream_SetSize(LPSTREAM iface,
184                                        ULARGE_INTEGER newSize)
185 {
186   RpcStreamImpl *This = (RpcStreamImpl *)iface;
187   *This->size = newSize.u.LowPart;
188   return S_OK;
189 }
190
191 static const IStreamVtbl RpcStream_Vtbl =
192 {
193   RpcStream_QueryInterface,
194   RpcStream_AddRef,
195   RpcStream_Release,
196   RpcStream_Read,
197   RpcStream_Write,
198   RpcStream_Seek,
199   RpcStream_SetSize,
200   NULL, /* CopyTo */
201   NULL, /* Commit */
202   NULL, /* Revert */
203   NULL, /* LockRegion */
204   NULL, /* UnlockRegion */
205   NULL, /* Stat */
206   NULL  /* Clone */
207 };
208
209 static LPSTREAM RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg, BOOL init)
210 {
211   RpcStreamImpl *This;
212   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RpcStreamImpl));
213   if (!This) return NULL;
214   This->lpVtbl = &RpcStream_Vtbl;
215   This->RefCount = 1;
216   This->pMsg = pStubMsg;
217   This->size = (LPDWORD)pStubMsg->Buffer;
218   This->data = (char*)(This->size + 1);
219   This->pos = 0;
220   if (init) *This->size = 0;
221   TRACE("init size=%d\n", *This->size);
222   return (LPSTREAM)This;
223 }
224
225 static const IID* get_ip_iid(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
226 {
227   const IID *riid;
228   if (!pFormat) return &IID_IUnknown;
229   TRACE("format=%02x %02x\n", pFormat[0], pFormat[1]);
230   if (pFormat[0] != RPC_FC_IP) FIXME("format=%d\n", pFormat[0]);
231   if (pFormat[1] == RPC_FC_CONSTANT_IID) {
232     riid = (const IID *)&pFormat[2];
233   } else {
234     ComputeConformance(pStubMsg, pMemory, pFormat+2, 0);
235     riid = (const IID *)pStubMsg->MaxCount;
236   }
237   if (!riid) riid = &IID_IUnknown;
238   TRACE("got %s\n", debugstr_guid(riid));
239   return riid;
240 }
241
242 /***********************************************************************
243  *           NdrInterfacePointerMarshall [RPCRT4.@]
244  */
245 unsigned char * WINAPI NdrInterfacePointerMarshall(PMIDL_STUB_MESSAGE pStubMsg,
246                                                   unsigned char *pMemory,
247                                                   PFORMAT_STRING pFormat)
248 {
249   const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
250   LPSTREAM stream;
251   HRESULT hr;
252
253   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
254   pStubMsg->MaxCount = 0;
255   if (!LoadCOM()) return NULL;
256   if (pStubMsg->Buffer + sizeof(DWORD) <= (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength) {
257     stream = RpcStream_Create(pStubMsg, TRUE);
258     if (stream) {
259       if (pMemory)
260         hr = COM_MarshalInterface(stream, riid, (LPUNKNOWN)pMemory,
261                                   pStubMsg->dwDestContext, pStubMsg->pvDestContext,
262                                   MSHLFLAGS_NORMAL);
263       else
264         hr = S_OK;
265
266       IStream_Release(stream);
267       if (FAILED(hr))
268         RpcRaiseException(hr);
269     }
270   }
271   return NULL;
272 }
273
274 /***********************************************************************
275  *           NdrInterfacePointerUnmarshall [RPCRT4.@]
276  */
277 unsigned char * WINAPI NdrInterfacePointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
278                                                     unsigned char **ppMemory,
279                                                     PFORMAT_STRING pFormat,
280                                                     unsigned char fMustAlloc)
281 {
282   LPSTREAM stream;
283   HRESULT hr;
284
285   TRACE("(%p,%p,%p,%d)\n", pStubMsg, ppMemory, pFormat, fMustAlloc);
286   if (!LoadCOM()) return NULL;
287   *(LPVOID*)ppMemory = NULL;
288   if (pStubMsg->Buffer + sizeof(DWORD) < (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength) {
289     stream = RpcStream_Create(pStubMsg, FALSE);
290     if (!stream) RpcRaiseException(E_OUTOFMEMORY);
291     if (*((RpcStreamImpl *)stream)->size != 0)
292       hr = COM_UnmarshalInterface(stream, &IID_NULL, (LPVOID*)ppMemory);
293     else
294       hr = S_OK;
295     IStream_Release(stream);
296     if (FAILED(hr))
297         RpcRaiseException(hr);
298   }
299   return NULL;
300 }
301
302 /***********************************************************************
303  *           NdrInterfacePointerBufferSize [RPCRT4.@]
304  */
305 void WINAPI NdrInterfacePointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
306                                          unsigned char *pMemory,
307                                          PFORMAT_STRING pFormat)
308 {
309   const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
310   ULONG size = 0;
311   HRESULT hr;
312
313   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
314   if (!LoadCOM()) return;
315   hr = COM_GetMarshalSizeMax(&size, riid, (LPUNKNOWN)pMemory,
316                             pStubMsg->dwDestContext, pStubMsg->pvDestContext,
317                             MSHLFLAGS_NORMAL);
318   TRACE("size=%d\n", size);
319   pStubMsg->BufferLength += sizeof(DWORD) + size;
320 }
321
322 /***********************************************************************
323  *           NdrInterfacePointerMemorySize [RPCRT4.@]
324  */
325 ULONG WINAPI NdrInterfacePointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
326                                            PFORMAT_STRING pFormat)
327 {
328   ULONG size;
329
330   TRACE("(%p,%p)\n", pStubMsg, pFormat);
331
332   size = *(ULONG *)pStubMsg->Buffer;
333   pStubMsg->Buffer += 4;
334   pStubMsg->MemorySize += 4;
335
336   pStubMsg->Buffer += size;
337
338   return pStubMsg->MemorySize;
339 }
340
341 /***********************************************************************
342  *           NdrInterfacePointerFree [RPCRT4.@]
343  */
344 void WINAPI NdrInterfacePointerFree(PMIDL_STUB_MESSAGE pStubMsg,
345                                    unsigned char *pMemory,
346                                    PFORMAT_STRING pFormat)
347 {
348   LPUNKNOWN pUnk = (LPUNKNOWN)pMemory;
349   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
350   if (pUnk) IUnknown_Release(pUnk);
351 }
352
353 /***********************************************************************
354  *           NdrOleAllocate [RPCRT4.@]
355  */
356 void * WINAPI NdrOleAllocate(size_t Size)
357 {
358   if (!LoadCOM()) return NULL;
359   return COM_MemAlloc(Size);
360 }
361
362 /***********************************************************************
363  *           NdrOleFree [RPCRT4.@]
364  */
365 void WINAPI NdrOleFree(void *NodeToFree)
366 {
367   if (!LoadCOM()) return;
368   COM_MemFree(NodeToFree);
369 }
370
371 /***********************************************************************
372  * Helper function to create a stub.
373  * This probably looks very much like NdrpCreateStub.
374  */
375 HRESULT create_stub(REFIID iid, IUnknown *pUnk, IRpcStubBuffer **ppstub)
376 {
377     CLSID clsid;
378     IPSFactoryBuffer *psfac;
379     HRESULT r;
380
381     if(!LoadCOM()) return E_FAIL;
382
383     r = COM_GetPSClsid( iid, &clsid );
384     if(FAILED(r)) return r;
385
386     r = COM_GetClassObject( &clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void**)&psfac );
387     if(FAILED(r)) return r;
388
389     r = IPSFactoryBuffer_CreateStub(psfac, iid, pUnk, ppstub);
390
391     IPSFactoryBuffer_Release(psfac);
392     return r;
393 }