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