Changed MoveFileEx to use ntdll functions, and moved it to
[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., 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 NONAMELESSUNION
32 #define NONAMELESSSTRUCT
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "winreg.h"
37
38 #include "objbase.h"
39
40 #include "ndr_misc.h"
41 #include "rpcndr.h"
42 #include "wine/rpcfc.h"
43
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(ole);
47
48 static HMODULE hOLE;
49
50 static HRESULT (WINAPI *COM_GetMarshalSizeMax)(ULONG *,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
51 static HRESULT (WINAPI *COM_MarshalInterface)(LPSTREAM,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
52 static HRESULT (WINAPI *COM_UnmarshalInterface)(LPSTREAM,REFIID,LPVOID*);
53 static HRESULT (WINAPI *COM_ReleaseMarshalData)(LPSTREAM);
54 static HRESULT (WINAPI *COM_GetClassObject)(REFCLSID,DWORD,COSERVERINFO *,REFIID,LPVOID *);
55 static HRESULT (WINAPI *COM_GetPSClsid)(REFIID,CLSID *);
56 static LPVOID (WINAPI *COM_MemAlloc)(ULONG);
57 static void (WINAPI *COM_MemFree)(LPVOID);
58
59 static HMODULE LoadCOM(void)
60 {
61   if (hOLE) return hOLE;
62   hOLE = LoadLibraryA("OLE32.DLL");
63   if (!hOLE) return 0;
64   COM_GetMarshalSizeMax  = (LPVOID)GetProcAddress(hOLE, "CoGetMarshalSizeMax");
65   COM_MarshalInterface   = (LPVOID)GetProcAddress(hOLE, "CoMarshalInterface");
66   COM_UnmarshalInterface = (LPVOID)GetProcAddress(hOLE, "CoUnmarshalInterface");
67   COM_ReleaseMarshalData = (LPVOID)GetProcAddress(hOLE, "CoReleaseMarshalData");
68   COM_GetClassObject     = (LPVOID)GetProcAddress(hOLE, "CoGetClassObject");
69   COM_GetPSClsid         = (LPVOID)GetProcAddress(hOLE, "CoGetPSClsid");
70   COM_MemAlloc = (LPVOID)GetProcAddress(hOLE, "CoTaskMemAlloc");
71   COM_MemFree  = (LPVOID)GetProcAddress(hOLE, "CoTaskMemFree");
72   return hOLE;
73 }
74
75 /* CoMarshalInterface/CoUnmarshalInterface works on streams,
76  * so implement a simple stream on top of the RPC buffer
77  * (which also implements the MInterfacePointer structure) */
78 typedef struct RpcStreamImpl
79 {
80   ICOM_VFIELD(IStream);
81   DWORD RefCount;
82   PMIDL_STUB_MESSAGE pMsg;
83   LPDWORD size;
84   char *data;
85   DWORD pos;
86 } RpcStreamImpl;
87
88 static HRESULT WINAPI RpcStream_QueryInterface(LPSTREAM iface,
89                                               REFIID riid,
90                                               LPVOID *obj)
91 {
92   ICOM_THIS(RpcStreamImpl, iface);
93   if (IsEqualGUID(&IID_IUnknown, riid) ||
94       IsEqualGUID(&IID_ISequentialStream, riid) ||
95       IsEqualGUID(&IID_IStream, riid)) {
96     *obj = This;
97     This->RefCount++;
98     return S_OK;
99   }
100   return E_NOINTERFACE;
101 }
102
103 static ULONG WINAPI RpcStream_AddRef(LPSTREAM iface)
104 {
105   ICOM_THIS(RpcStreamImpl, iface);
106   return ++(This->RefCount);
107 }
108
109 static ULONG WINAPI RpcStream_Release(LPSTREAM iface)
110 {
111   ICOM_THIS(RpcStreamImpl, iface);
112   if (!--(This->RefCount)) {
113     TRACE("size=%ld\n", *This->size);
114     This->pMsg->Buffer = This->data + *This->size;
115     HeapFree(GetProcessHeap(),0,This);
116     return 0;
117   }
118   return This->RefCount;
119 }
120
121 static HRESULT WINAPI RpcStream_Read(LPSTREAM iface,
122                                     void *pv,
123                                     ULONG cb,
124                                     ULONG *pcbRead)
125 {
126   ICOM_THIS(RpcStreamImpl, iface);
127   if (This->pos + cb > *This->size) cb = *This->size - This->pos;
128   if (cb) {
129     memcpy(pv, This->data + This->pos, cb);
130     This->pos += cb;
131   }
132   if (pcbRead) *pcbRead = cb;
133   return S_OK;
134 }
135
136 static HRESULT WINAPI RpcStream_Write(LPSTREAM iface,
137                                      const void *pv,
138                                      ULONG cb,
139                                      ULONG *pcbWritten)
140 {
141   ICOM_THIS(RpcStreamImpl, iface);
142   memcpy(This->data + This->pos, pv, cb);
143   This->pos += cb;
144   if (This->pos > *This->size) *This->size = This->pos;
145   if (pcbWritten) *pcbWritten = cb;
146   return S_OK;
147 }
148
149 static HRESULT WINAPI RpcStream_Seek(LPSTREAM iface,
150                                     LARGE_INTEGER move,
151                                     DWORD origin,
152                                     ULARGE_INTEGER *newPos)
153 {
154   ICOM_THIS(RpcStreamImpl, iface);
155   switch (origin) {
156   case STREAM_SEEK_SET:
157     This->pos = move.u.LowPart;
158     break;
159   case STREAM_SEEK_CUR:
160     This->pos = This->pos + move.u.LowPart;
161     break;
162   case STREAM_SEEK_END:
163     This->pos = *This->size + move.u.LowPart;
164     break;
165   default:
166     return STG_E_INVALIDFUNCTION;
167   }
168   if (newPos) {
169     newPos->u.LowPart = This->pos;
170     newPos->u.HighPart = 0;
171   }
172   return S_OK;
173 }
174
175 static HRESULT WINAPI RpcStream_SetSize(LPSTREAM iface,
176                                        ULARGE_INTEGER newSize)
177 {
178   ICOM_THIS(RpcStreamImpl, iface);
179   *This->size = newSize.u.LowPart;
180   return S_OK;
181 }
182
183 static ICOM_VTABLE(IStream) RpcStream_Vtbl =
184 {
185   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
186   RpcStream_QueryInterface,
187   RpcStream_AddRef,
188   RpcStream_Release,
189   RpcStream_Read,
190   RpcStream_Write,
191   RpcStream_Seek,
192   RpcStream_SetSize,
193   NULL, /* CopyTo */
194   NULL, /* Commit */
195   NULL, /* Revert */
196   NULL, /* LockRegion */
197   NULL, /* UnlockRegion */
198   NULL, /* Stat */
199   NULL  /* Clone */
200 };
201
202 static LPSTREAM RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg, BOOL init)
203 {
204   RpcStreamImpl *This;
205   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RpcStreamImpl));
206   if (!This) return NULL;
207   This->lpVtbl = &RpcStream_Vtbl;
208   This->RefCount = 1;
209   This->pMsg = pStubMsg;
210   This->size = (LPDWORD)pStubMsg->Buffer;
211   This->data = (char*)(This->size + 1);
212   This->pos = 0;
213   if (init) *This->size = 0;
214   TRACE("init size=%ld\n", *This->size);
215   return (LPSTREAM)This;
216 }
217
218 const IID* get_ip_iid(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
219 {
220   const IID *riid;
221   if (!pFormat) return &IID_IUnknown;
222   TRACE("format=%02x %02x\n", pFormat[0], pFormat[1]);
223   if (pFormat[0] != RPC_FC_IP) FIXME("format=%d\n", pFormat[0]);
224   if (pFormat[1] == RPC_FC_CONSTANT_IID) {
225     riid = (const IID *)&pFormat[2];
226   } else {
227     ComputeConformance(pStubMsg, pMemory, pFormat+2, 0);
228     riid = (const IID *)pStubMsg->MaxCount;
229   }
230   if (!riid) riid = &IID_IUnknown;
231   TRACE("got %s\n", debugstr_guid(riid));
232   return riid;
233 }
234
235 /***********************************************************************
236  *           NdrInterfacePointerMarshall [RPCRT4.@]
237  */
238 unsigned char * WINAPI NdrInterfacePointerMarshall(PMIDL_STUB_MESSAGE pStubMsg,
239                                                   unsigned char *pMemory,
240                                                   PFORMAT_STRING pFormat)
241 {
242   const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
243   LPSTREAM stream;
244   HRESULT hr;
245
246   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
247   pStubMsg->MaxCount = 0;
248   if (!LoadCOM()) return NULL;
249   stream = RpcStream_Create(pStubMsg, TRUE);
250   hr = COM_MarshalInterface(stream, riid, (LPUNKNOWN)pMemory,
251                            pStubMsg->dwDestContext, pStubMsg->pvDestContext,
252                            MSHLFLAGS_NORMAL);
253   IStream_Release(stream);
254   return NULL;
255 }
256
257 /***********************************************************************
258  *           NdrInterfacePointerUnmarshall [RPCRT4.@]
259  */
260 unsigned char * WINAPI NdrInterfacePointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
261                                                     unsigned char **ppMemory,
262                                                     PFORMAT_STRING pFormat,
263                                                     unsigned char fMustAlloc)
264 {
265   LPSTREAM stream;
266   HRESULT hr;
267
268   TRACE("(%p,%p,%p,%d)\n", pStubMsg, ppMemory, pFormat, fMustAlloc);
269   if (!LoadCOM()) return NULL;
270   *(LPVOID*)ppMemory = NULL;
271   stream = RpcStream_Create(pStubMsg, FALSE);
272   hr = COM_UnmarshalInterface(stream, &IID_NULL, (LPVOID*)ppMemory);
273   IStream_Release(stream);
274   return NULL;
275 }
276
277 /***********************************************************************
278  *           NdrInterfacePointerBufferSize [RPCRT4.@]
279  */
280 void WINAPI NdrInterfacePointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
281                                          unsigned char *pMemory,
282                                          PFORMAT_STRING pFormat)
283 {
284   const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
285   ULONG size = 0;
286   HRESULT hr;
287
288   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
289   if (!LoadCOM()) return;
290   hr = COM_GetMarshalSizeMax(&size, riid, (LPUNKNOWN)pMemory,
291                             pStubMsg->dwDestContext, pStubMsg->pvDestContext,
292                             MSHLFLAGS_NORMAL);
293   TRACE("size=%ld\n", size);
294   pStubMsg->BufferLength += sizeof(DWORD) + size;
295 }
296
297 /***********************************************************************
298  *           NdrInterfacePointerMemorySize [RPCRT4.@]
299  */
300 unsigned long WINAPI NdrInterfacePointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
301                                                   PFORMAT_STRING pFormat)
302 {
303   FIXME("(%p,%p): stub\n", pStubMsg, pFormat);
304   return 0;
305 }
306
307 /***********************************************************************
308  *           NdrInterfacePointerFree [RPCRT4.@]
309  */
310 void WINAPI NdrInterfacePointerFree(PMIDL_STUB_MESSAGE pStubMsg,
311                                    unsigned char *pMemory,
312                                    PFORMAT_STRING pFormat)
313 {
314   LPUNKNOWN pUnk = (LPUNKNOWN)pMemory;
315   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
316   if (pUnk) IUnknown_Release(pUnk);
317 }
318
319 /***********************************************************************
320  *           NdrOleAllocate [RPCRT4.@]
321  */
322 void * WINAPI NdrOleAllocate(size_t Size)
323 {
324   if (!LoadCOM()) return NULL;
325   return COM_MemAlloc(Size);
326 }
327
328 /***********************************************************************
329  *           NdrOleFree [RPCRT4.@]
330  */
331 void WINAPI NdrOleFree(void *NodeToFree)
332 {
333   if (!LoadCOM()) return;
334   COM_MemFree(NodeToFree);
335 }
336
337 /* internal */
338 HRESULT RPCRT4_GetPSFactory(REFIID riid, LPPSFACTORYBUFFER *pPS)
339 {
340   HRESULT hr;
341   CLSID clsid;
342
343   if (!LoadCOM()) return RPC_E_UNEXPECTED;
344   hr = COM_GetPSClsid(riid, &clsid);
345   if (FAILED(hr)) return hr;
346   hr = COM_GetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
347                           &IID_IPSFactoryBuffer, (LPVOID *)pPS);
348   return hr;
349 }