Added support for creating extra symlinks for .so files that contain
[wine] / dlls / ole32 / ifs.c
1 /*
2  *      basic interfaces
3  *
4  *      Copyright 1997  Marcus Meissner
5  */
6
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <assert.h>
11 #include "winerror.h"
12 #include "ldt.h"
13 #include "heap.h"
14 #include "wine/winbase16.h"
15 #include "wine/obj_base.h"
16 #include "local.h"
17 #include "module.h"
18 #include "debugtools.h"
19
20 #include "ifs.h"
21
22 DEFAULT_DEBUG_CHANNEL(relay)
23
24 /* --- IUnknown implementation */
25
26 typedef struct
27 {
28     /* IUnknown fields */
29     ICOM_VFIELD(IUnknown);
30     DWORD                  ref;
31 } IUnknownImpl;
32
33 /******************************************************************************
34  *              IUnknown_AddRef [VTABLE:IUNKNOWN.1]
35  */
36 static ULONG WINAPI IUnknown_fnAddRef(LPUNKNOWN iface) { 
37         ICOM_THIS(IUnknownImpl,iface);
38         TRACE("(%p)->AddRef()\n",This);
39         return ++(This->ref);
40 }
41
42 /******************************************************************************
43  * IUnknown_Release [VTABLE:IUNKNOWN.2]
44  */
45 static ULONG WINAPI IUnknown_fnRelease(LPUNKNOWN iface) {
46         ICOM_THIS(IUnknownImpl,iface);
47         TRACE("(%p)->Release()\n",This);
48         if (!--(This->ref)) {
49                 HeapFree(GetProcessHeap(),0,This);
50                 return 0;
51         }
52         return This->ref;
53 }
54
55 /******************************************************************************
56  * IUnknown_QueryInterface [VTABLE:IUNKNOWN.0]
57  */
58 static HRESULT WINAPI IUnknown_fnQueryInterface(LPUNKNOWN iface,REFIID refiid,LPVOID *obj) {
59         ICOM_THIS(IUnknownImpl,iface);
60
61         TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(refiid),obj);
62
63         if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
64                 *obj = This;
65                 return 0; 
66         }
67         return OLE_E_ENUM_NOMORE; 
68 }
69
70 static ICOM_VTABLE(IUnknown) uvt = 
71 {
72         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
73         IUnknown_fnQueryInterface,
74         IUnknown_fnAddRef,
75         IUnknown_fnRelease
76 };
77
78 /******************************************************************************
79  * IUnknown_Constructor [INTERNAL]
80  */
81 LPUNKNOWN
82 IUnknown_Constructor() {
83         IUnknownImpl*   unk;
84
85         unk = (IUnknownImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IUnknownImpl));
86         ICOM_VTBL(unk)  = &uvt;
87         unk->ref        = 1;
88         return (LPUNKNOWN)unk;
89 }
90
91
92 /* --- IMalloc16 implementation */
93
94
95 typedef struct
96 {
97         /* IUnknown fields */
98         ICOM_VFIELD(IMalloc16);
99         DWORD                   ref;
100         /* IMalloc16 fields */
101         /* Gmm, I think one is not enough, we should probably manage a list of
102          * heaps
103  */
104         HGLOBAL16 heap;
105 } IMalloc16Impl;
106
107 /******************************************************************************
108  *              IMalloc16_QueryInterface        [COMPOBJ.500]
109  */
110 HRESULT WINAPI IMalloc16_fnQueryInterface(IMalloc16* iface,REFIID refiid,LPVOID *obj) {
111         ICOM_THIS(IMalloc16Impl,iface);
112
113         TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(refiid),obj);
114         if (    !memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown)) ||
115                 !memcmp(&IID_IMalloc,refiid,sizeof(IID_IMalloc))
116         ) {
117                 *obj = This;
118                 return 0;
119         }
120         return OLE_E_ENUM_NOMORE; 
121 }
122
123 /******************************************************************************
124  *              IMalloc16_AddRef        [COMPOBJ.501]
125  */
126 ULONG WINAPI IMalloc16_fnAddRef(IMalloc16* iface) {
127         ICOM_THIS(IMalloc16Impl,iface);
128         TRACE("(%p)->AddRef()\n",This);
129         return 1; /* cannot be freed */
130 }
131
132 /******************************************************************************
133  *              IMalloc16_Release       [COMPOBJ.502]
134  */
135 ULONG WINAPI IMalloc16_fnRelease(IMalloc16* iface) {
136         ICOM_THIS(IMalloc16Impl,iface);
137         TRACE("(%p)->Release()\n",This);
138         return 1; /* cannot be freed */
139 }
140
141 /******************************************************************************
142  * IMalloc16_Alloc [COMPOBJ.503]
143  */
144 LPVOID WINAPI IMalloc16_fnAlloc(IMalloc16* iface,DWORD cb) {
145         ICOM_THIS(IMalloc16Impl,iface);
146         TRACE("(%p)->Alloc(%ld)\n",This,cb);
147         return (LPVOID)PTR_SEG_OFF_TO_SEGPTR(This->heap,LOCAL_Alloc(This->heap,0,cb));
148 }
149
150 /******************************************************************************
151  * IMalloc16_Realloc [COMPOBJ.504]
152  */
153 LPVOID WINAPI IMalloc16_fnRealloc(IMalloc16* iface,LPVOID pv,DWORD cb) {
154         ICOM_THIS(IMalloc16Impl,iface);
155         TRACE("(%p)->Realloc(%p,%ld)\n",This,pv,cb);
156         return (LPVOID)PTR_SEG_OFF_TO_SEGPTR(This->heap,LOCAL_ReAlloc(This->heap,0,LOWORD(pv),cb));
157 }
158
159 /******************************************************************************
160  * IMalloc16_Free [COMPOBJ.505]
161  */
162 VOID WINAPI IMalloc16_fnFree(IMalloc16* iface,LPVOID pv) {
163         ICOM_THIS(IMalloc16Impl,iface);
164         TRACE("(%p)->Free(%p)\n",This,pv);
165         LOCAL_Free(This->heap,LOWORD(pv));
166 }
167
168 /******************************************************************************
169  * IMalloc16_GetSize [COMPOBJ.506]
170  */
171 DWORD WINAPI IMalloc16_fnGetSize(const IMalloc16* iface,LPVOID pv) {
172         ICOM_CTHIS(IMalloc16Impl,iface);
173         TRACE("(%p)->GetSize(%p)\n",This,pv);
174         return LOCAL_Size(This->heap,LOWORD(pv));
175 }
176
177 /******************************************************************************
178  * IMalloc16_DidAlloc [COMPOBJ.507]
179  */
180 INT16 WINAPI IMalloc16_fnDidAlloc(const IMalloc16* iface,LPVOID pv) {
181         ICOM_CTHIS(IMalloc16,iface);
182         TRACE("(%p)->DidAlloc(%p)\n",This,pv);
183         return (INT16)-1;
184 }
185
186 /******************************************************************************
187  * IMalloc16_HeapMinimize [COMPOBJ.508]
188  */
189 LPVOID WINAPI IMalloc16_fnHeapMinimize(IMalloc16* iface) {
190         ICOM_THIS(IMalloc16Impl,iface);
191         TRACE("(%p)->HeapMinimize()\n",This);
192         return NULL;
193 }
194
195 static ICOM_VTABLE(IMalloc16)* msegvt16 = NULL;
196
197 /******************************************************************************
198  * IMalloc16_Constructor [VTABLE]
199  */
200 LPMALLOC16
201 IMalloc16_Constructor() {
202         IMalloc16Impl*  This;
203         HMODULE16       hcomp = GetModuleHandle16("COMPOBJ");
204
205         This = (IMalloc16Impl*)SEGPTR_NEW(IMalloc16Impl);
206         if (!msegvt16) {
207             msegvt16 = SEGPTR_NEW(ICOM_VTABLE(IMalloc16));
208
209 #define VTENT(x) msegvt16->fn##x = (void*)WIN32_GetProcAddress16(hcomp,"IMalloc16_"#x);assert(msegvt16->fn##x)
210             VTENT(QueryInterface);
211             VTENT(AddRef);
212             VTENT(Release);
213             VTENT(Alloc);
214             VTENT(Realloc);
215             VTENT(Free);
216             VTENT(GetSize);
217             VTENT(DidAlloc);
218             VTENT(HeapMinimize);
219 #undef VTENT
220         }
221         ICOM_VTBL(This) = (ICOM_VTABLE(IMalloc16)*)SEGPTR_GET(msegvt16);
222         This->ref = 1;
223         /* FIXME: implement multiple heaps */
224         This->heap = GlobalAlloc16(GMEM_MOVEABLE,64000);
225         LocalInit16(This->heap,0,64000);
226         return (LPMALLOC16)SEGPTR_GET(This);
227 }
228
229
230 /* --- IMalloc32 implementation */
231
232 typedef struct
233 {
234         /* IUnknown fields */
235         ICOM_VFIELD(IMalloc);
236         DWORD                   ref;
237 } IMalloc32Impl;
238
239 /******************************************************************************
240  *              IMalloc32_QueryInterface        [VTABLE]
241  */
242 static HRESULT WINAPI IMalloc_fnQueryInterface(LPMALLOC iface,REFIID refiid,LPVOID *obj) {
243         ICOM_THIS(IMalloc32Impl,iface);
244
245         TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(refiid),obj);
246         if (    !memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown)) ||
247                 !memcmp(&IID_IMalloc,refiid,sizeof(IID_IMalloc))
248         ) {
249                 *obj = This;
250                 return S_OK;
251         }
252         return OLE_E_ENUM_NOMORE; 
253 }
254
255 /******************************************************************************
256  *              IMalloc32_AddRef        [VTABLE]
257  */
258 static ULONG WINAPI IMalloc_fnAddRef(LPMALLOC iface) {
259         ICOM_THIS(IMalloc32Impl,iface);
260         TRACE("(%p)->AddRef()\n",This);
261         return 1; /* cannot be freed */
262 }
263
264 /******************************************************************************
265  *              IMalloc32_Release       [VTABLE]
266  */
267 static ULONG WINAPI IMalloc_fnRelease(LPMALLOC iface) {
268         ICOM_THIS(IMalloc32Impl,iface);
269         TRACE("(%p)->Release()\n",This);
270         return 1; /* cannot be freed */
271 }
272
273 /******************************************************************************
274  * IMalloc32_Alloc [VTABLE]
275  */
276 static LPVOID WINAPI IMalloc_fnAlloc(LPMALLOC iface,DWORD cb) {
277         ICOM_THIS(IMalloc32Impl,iface);
278         TRACE("(%p)->Alloc(%ld)\n",This,cb);
279         return HeapAlloc(GetProcessHeap(),0,cb);
280 }
281
282 /******************************************************************************
283  * IMalloc32_Realloc [VTABLE]
284  */
285 static LPVOID WINAPI IMalloc_fnRealloc(LPMALLOC iface,LPVOID pv,DWORD cb) {
286         ICOM_THIS(IMalloc32Impl,iface);
287         TRACE("(%p)->Realloc(%p,%ld)\n",This,pv,cb);
288         return HeapReAlloc(GetProcessHeap(),0,pv,cb);
289 }
290
291 /******************************************************************************
292  * IMalloc32_Free [VTABLE]
293  */
294 static VOID WINAPI IMalloc_fnFree(LPMALLOC iface,LPVOID pv) {
295         ICOM_THIS(IMalloc32Impl,iface);
296         TRACE("(%p)->Free(%p)\n",This,pv);
297         HeapFree(GetProcessHeap(),0,pv);
298 }
299
300 /******************************************************************************
301  * IMalloc32_GetSize [VTABLE]
302  */
303 static DWORD WINAPI IMalloc_fnGetSize(const IMalloc* iface,LPVOID pv) {
304         ICOM_CTHIS(IMalloc,iface);
305         TRACE("(%p)->GetSize(%p)\n",This,pv);
306         return HeapSize(GetProcessHeap(),0,pv);
307 }
308
309 /******************************************************************************
310  * IMalloc32_DidAlloc [VTABLE]
311  */
312 static INT WINAPI IMalloc_fnDidAlloc(const IMalloc* iface,LPVOID pv) {
313         ICOM_CTHIS(IMalloc32Impl,iface);
314         TRACE("(%p)->DidAlloc(%p)\n",This,pv);
315         return -1;
316 }
317
318 /******************************************************************************
319  * IMalloc32_HeapMinimize [VTABLE]
320  */
321 static LPVOID WINAPI IMalloc_fnHeapMinimize(LPMALLOC iface) {
322         ICOM_THIS(IMalloc32Impl,iface);
323         TRACE("(%p)->HeapMinimize()\n",This);
324         return NULL;
325 }
326
327 static ICOM_VTABLE(IMalloc) VT_IMalloc32 = 
328 {
329     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
330     IMalloc_fnQueryInterface,
331     IMalloc_fnAddRef,
332   IMalloc_fnRelease,
333   IMalloc_fnAlloc,
334   IMalloc_fnRealloc,
335   IMalloc_fnFree,
336   IMalloc_fnGetSize,
337   IMalloc_fnDidAlloc,
338   IMalloc_fnHeapMinimize
339 };
340
341 /******************************************************************************
342  * IMalloc32_Constructor [VTABLE]
343  */
344 LPMALLOC
345 IMalloc_Constructor() {
346         IMalloc32Impl* This;
347
348         This = (IMalloc32Impl*)HeapAlloc(GetProcessHeap(),0,sizeof(IMalloc32Impl));
349         ICOM_VTBL(This) = &VT_IMalloc32;
350         This->ref = 1;
351         return (LPMALLOC)This;
352 }
353
354 /****************************************************************************
355  * API Functions
356  */
357
358 /******************************************************************************
359  *              IsValidInterface32      [OLE32.78]
360  *
361  * RETURNS
362  *  True, if the passed pointer is a valid interface
363  */
364 BOOL WINAPI IsValidInterface(
365         LPUNKNOWN punk  /* [in] interface to be tested */
366 ) {
367         return !(
368                 IsBadReadPtr(punk,4)                                    ||
369                 IsBadReadPtr(ICOM_VTBL(punk),4)                         ||
370                 IsBadReadPtr(ICOM_VTBL(punk)->fnQueryInterface,9)       ||
371                 IsBadCodePtr(ICOM_VTBL(punk)->fnQueryInterface)
372         );
373 }