Added check for duplicate ordinals, and fixed bug it uncovered in
[wine] / dlls / ole32 / bindctx.c
1 /***************************************************************************************
2  *                            BindCtx implementation
3  *
4  *  Copyright 1999  Noomen Hamza
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
21 #include <string.h>
22 #include <assert.h>
23 #include "winerror.h"
24 #include "winbase.h"
25 #include "wine/unicode.h"
26 #include "objbase.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(ole);
30
31 /* represent the first size table and it's increment block size */
32 #define  BLOCK_TAB_SIZE 10
33 #define  MAX_TAB_SIZE   0xFFFFFFFF
34
35 /* data structure of the BindCtx table elements */
36 typedef struct BindCtxObject{
37
38     IUnknown*   pObj; /* point on a bound object */
39
40     LPOLESTR  pkeyObj; /* key associated to this bound object */
41
42     BYTE regType; /* registration type: 1 if RegisterObjectParam and 0 if RegisterObjectBound */
43
44 } BindCtxObject;
45
46 /* BindCtx data strucrture */
47 typedef struct BindCtxImpl{
48
49     ICOM_VFIELD(IBindCtx); /* VTable relative to the IBindCtx interface.*/
50
51     ULONG ref; /* reference counter for this object */
52
53     BindCtxObject* bindCtxTable; /* this is a table in which all bounded objects are stored*/
54     DWORD          bindCtxTableLastIndex;  /* first free index in the table */
55     DWORD          bindCtxTableSize;   /* size table */
56
57     BIND_OPTS2 bindOption2; /* a structure which contains the bind options*/
58
59 } BindCtxImpl;
60
61 /* IBindCtx prototype functions : */
62
63 /* IUnknown functions*/
64 static HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject);
65 static ULONG   WINAPI BindCtxImpl_AddRef(IBindCtx* iface);
66 static ULONG   WINAPI BindCtxImpl_Release(IBindCtx* iface);
67 /* IBindCtx functions */
68 static HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk);
69 static HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk);
70 static HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface);
71 static HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts);
72 static HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts);
73 static HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot);
74 static HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk);
75 static HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk);
76 static HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** ppenum);
77 static HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR pszkey);
78 /* Local functions*/
79 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This);
80 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This);
81 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,IUnknown* punk,LPOLESTR pszkey,DWORD *index);
82
83 /* Virtual function table for the BindCtx class. */
84 static ICOM_VTABLE(IBindCtx) VT_BindCtxImpl =
85     {
86     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
87     BindCtxImpl_QueryInterface,
88     BindCtxImpl_AddRef,
89     BindCtxImpl_Release,
90     BindCtxImpl_RegisterObjectBound,
91     BindCtxImpl_RevokeObjectBound,
92     BindCtxImpl_ReleaseBoundObjects,
93     BindCtxImpl_SetBindOptions,
94     BindCtxImpl_GetBindOptions,
95     BindCtxImpl_GetRunningObjectTable,
96     BindCtxImpl_RegisterObjectParam,
97     BindCtxImpl_GetObjectParam,
98     BindCtxImpl_EnumObjectParam,
99     BindCtxImpl_RevokeObjectParam
100 };
101
102 /*******************************************************************************
103  *        BindCtx_QueryInterface
104  *******************************************************************************/
105 HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject)
106 {
107   ICOM_THIS(BindCtxImpl,iface);
108
109   TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
110
111   /* Perform a sanity check on the parameters.*/
112   if ( (This==0) || (ppvObject==0) )
113       return E_INVALIDARG;
114
115   /* Initialize the return parameter.*/
116   *ppvObject = 0;
117
118   /* Compare the riid with the interface IDs implemented by this object.*/
119   if (IsEqualIID(&IID_IUnknown, riid))
120       *ppvObject = (IBindCtx*)This;
121   else
122       if (IsEqualIID(&IID_IBindCtx, riid))
123           *ppvObject = (IBindCtx*)This;
124
125   /* Check that we obtained an interface.*/
126   if ((*ppvObject)==0)
127       return E_NOINTERFACE;
128
129    /* Query Interface always increases the reference count by one when it is successful */
130   BindCtxImpl_AddRef(iface);
131
132   return S_OK;
133 }
134
135 /******************************************************************************
136  *       BindCtx_AddRef
137  ******************************************************************************/
138 ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
139 {
140     ICOM_THIS(BindCtxImpl,iface);
141
142     TRACE("(%p)\n",This);
143
144     return ++(This->ref);
145 }
146
147 /******************************************************************************
148  *        BindCtx_Release
149  ******************************************************************************/
150 ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
151 {
152     ICOM_THIS(BindCtxImpl,iface);
153
154     TRACE("(%p)\n",This);
155
156     This->ref--;
157
158     if (This->ref==0){
159
160         /* release all registered objects */
161         BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
162
163         BindCtxImpl_Destroy(This);
164
165         return 0;
166     }
167     return This->ref;
168 }
169
170
171 /******************************************************************************
172  *         BindCtx_Construct (local function)
173  *******************************************************************************/
174 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This)
175 {
176     TRACE("(%p)\n",This);
177
178     /* Initialize the virtual function table.*/
179     ICOM_VTBL(This)    = &VT_BindCtxImpl;
180     This->ref          = 0;
181
182     /* Initialize the BIND_OPTS2 structure */
183     This->bindOption2.cbStruct  = sizeof(BIND_OPTS2);
184     This->bindOption2.grfFlags = 0;
185     This->bindOption2.grfMode = STGM_READWRITE;
186     This->bindOption2.dwTickCountDeadline = 0;
187
188     This->bindOption2.dwTrackFlags = 0;
189     This->bindOption2.dwClassContext = CLSCTX_SERVER;
190     This->bindOption2.locale = 1033;
191     This->bindOption2.pServerInfo = 0;
192
193     /* Initialize the bindctx table */
194     This->bindCtxTableSize=BLOCK_TAB_SIZE;
195     This->bindCtxTableLastIndex=0;
196     This->bindCtxTable= HeapAlloc(GetProcessHeap(), 0,This->bindCtxTableSize*sizeof(BindCtxObject));
197
198     if (This->bindCtxTable==NULL)
199         return E_OUTOFMEMORY;
200
201     return S_OK;
202 }
203
204 /******************************************************************************
205  *        BindCtx_Destroy    (local function)
206  *******************************************************************************/
207 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This)
208 {
209     TRACE("(%p)\n",This);
210
211     /* free the table space memory */
212     HeapFree(GetProcessHeap(),0,This->bindCtxTable);
213
214     /* free the bindctx structure */
215     HeapFree(GetProcessHeap(),0,This);
216
217     return S_OK;
218 }
219
220
221 /******************************************************************************
222  *        BindCtx_RegisterObjectBound
223  ******************************************************************************/
224 HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
225 {
226
227     ICOM_THIS(BindCtxImpl,iface);
228     DWORD lastIndex=This->bindCtxTableLastIndex;
229     BindCtxObject cell;
230
231     TRACE("(%p,%p)\n",This,punk);
232
233     if (punk==NULL)
234         return E_POINTER;
235
236     IUnknown_AddRef(punk);
237
238     /* put the object in the first free element in the table */
239     This->bindCtxTable[lastIndex].pObj = punk;
240     This->bindCtxTable[lastIndex].pkeyObj = NULL;
241     This->bindCtxTable[lastIndex].regType = 0;
242     cell=This->bindCtxTable[lastIndex];
243     lastIndex= ++This->bindCtxTableLastIndex;
244
245     if (lastIndex == This->bindCtxTableSize){ /* the table is full so it must be resized */
246
247         if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
248             FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
249             return E_FAIL;
250         }
251
252         This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
253
254         This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
255                                          This->bindCtxTableSize * sizeof(BindCtxObject));
256         if (!This->bindCtxTable)
257             return E_OUTOFMEMORY;
258     }
259     return S_OK;
260 }
261
262 /******************************************************************************
263  *        BindCtx_RevokeObjectBound
264  ******************************************************************************/
265 HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
266 {
267     DWORD index,j;
268
269     ICOM_THIS(BindCtxImpl,iface);
270
271     TRACE("(%p,%p)\n",This,punk);
272
273     /* check if the object was registred or not */
274     if (BindCtxImpl_GetObjectIndex(This,punk,NULL,&index)==S_FALSE)
275
276         return MK_E_NOTBOUND;
277
278     IUnknown_Release(This->bindCtxTable[index].pObj);
279
280     /* left-shift all elements in the right side of the current revoked object */
281     for(j=index; j<This->bindCtxTableLastIndex-1; j++)
282         This->bindCtxTable[j]= This->bindCtxTable[j+1];
283
284     This->bindCtxTableLastIndex--;
285
286     return S_OK;
287 }
288
289 /******************************************************************************
290  *        BindCtx_ReleaseBoundObjects
291  ******************************************************************************/
292 HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface)
293 {
294     DWORD i;
295
296     ICOM_THIS(BindCtxImpl,iface);
297
298     TRACE("(%p)\n",This);
299
300     for(i=0;i<This->bindCtxTableLastIndex;i++)
301        IUnknown_Release(This->bindCtxTable[i].pObj);
302
303     This->bindCtxTableLastIndex = 0;
304
305     return S_OK;
306 }
307
308 /******************************************************************************
309  *        BindCtx_SetBindOptions
310  ******************************************************************************/
311 HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts)
312 {
313     ICOM_THIS(BindCtxImpl,iface);
314
315     TRACE("(%p,%p)\n",This,pbindopts);
316
317     if (pbindopts==NULL)
318         return E_POINTER;
319
320     if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
321     {
322         WARN("invalid size\n");
323         return E_INVALIDARG; /* FIXME : not verified */
324     }
325     memcpy(&This->bindOption2, pbindopts, pbindopts->cbStruct);
326     return S_OK;
327 }
328
329 /******************************************************************************
330  *        BindCtx_GetBindOptions
331  ******************************************************************************/
332 HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts)
333 {
334     ICOM_THIS(BindCtxImpl,iface);
335
336     TRACE("(%p,%p)\n",This,pbindopts);
337
338     if (pbindopts==NULL)
339         return E_POINTER;
340
341     if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
342     {
343         WARN("invalid size\n");
344         return E_INVALIDARG; /* FIXME : not verified */
345     }
346     memcpy(pbindopts, &This->bindOption2, pbindopts->cbStruct);
347     return S_OK;
348 }
349
350 /******************************************************************************
351  *        BindCtx_GetRunningObjectTable
352  ******************************************************************************/
353 HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot)
354 {
355     HRESULT res;
356
357     ICOM_THIS(BindCtxImpl,iface);
358
359     TRACE("(%p,%p)\n",This,pprot);
360
361     if (pprot==NULL)
362         return E_POINTER;
363
364     res=GetRunningObjectTable(0, pprot);
365
366     return res;
367 }
368
369 /******************************************************************************
370  *        BindCtx_RegisterObjectParam
371  ******************************************************************************/
372 HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk)
373 {
374     DWORD index=0;
375     ICOM_THIS(BindCtxImpl,iface);
376
377     TRACE("(%p,%s,%p)\n",This,debugstr_w(pszkey),punk);
378
379     if (punk==NULL)
380         return E_INVALIDARG;
381
382     IUnknown_AddRef(punk);
383
384     if (pszkey!=NULL && BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_OK)
385     {
386         TRACE("Overwriting existing key\n");
387         if(This->bindCtxTable[index].pObj!=NULL)
388             IUnknown_Release(This->bindCtxTable[index].pObj);
389         This->bindCtxTable[index].pObj=punk;
390         return S_OK;
391     }
392     This->bindCtxTable[This->bindCtxTableLastIndex].pObj = punk;
393     This->bindCtxTable[This->bindCtxTableLastIndex].regType = 1;
394
395     if (pszkey==NULL)
396
397         This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=NULL;
398
399     else{
400
401         This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=
402             HeapAlloc(GetProcessHeap(),0,(sizeof(WCHAR)*(1+lstrlenW(pszkey))));
403
404         if (This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj==NULL)
405             return E_OUTOFMEMORY;
406         strcpyW(This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj,pszkey);
407     }
408
409     This->bindCtxTableLastIndex++;
410
411     if (This->bindCtxTableLastIndex == This->bindCtxTableSize){ /* table is full ! must be resized */
412
413         This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
414
415         if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
416             FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
417             return E_FAIL;
418         }
419         This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
420                                          This->bindCtxTableSize * sizeof(BindCtxObject));
421         if (!This->bindCtxTable)
422             return E_OUTOFMEMORY;
423     }
424     return S_OK;
425 }
426 /******************************************************************************
427  *        BindCtx_GetObjectParam
428  ******************************************************************************/
429 HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk)
430 {
431     DWORD index;
432     ICOM_THIS(BindCtxImpl,iface);
433
434     TRACE("(%p,%s,%p)\n",This,debugstr_w(pszkey),punk);
435
436     if (punk==NULL)
437         return E_POINTER;
438
439     *punk=0;
440
441     if (BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_FALSE)
442         return E_FAIL;
443
444     IUnknown_AddRef(This->bindCtxTable[index].pObj);
445
446     *punk = This->bindCtxTable[index].pObj;
447
448     return S_OK;
449 }
450
451 /******************************************************************************
452  *        BindCtx_RevokeObjectParam
453  ******************************************************************************/
454 HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR ppenum)
455 {
456     DWORD index,j;
457
458     ICOM_THIS(BindCtxImpl,iface);
459
460     TRACE("(%p,%s)\n",This,debugstr_w(ppenum));
461
462     if (BindCtxImpl_GetObjectIndex(This,NULL,ppenum,&index)==S_FALSE)
463         return E_FAIL;
464
465     /* release the object if it's found */
466     IUnknown_Release(This->bindCtxTable[index].pObj);
467
468     /* remove the object from the table with a left-shifting of all objects in the right side */
469     for(j=index; j<This->bindCtxTableLastIndex-1; j++)
470         This->bindCtxTable[j]= This->bindCtxTable[j+1];
471
472     This->bindCtxTableLastIndex--;
473
474     return S_OK;
475 }
476
477 /******************************************************************************
478  *        BindCtx_EnumObjectParam
479  ******************************************************************************/
480 HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
481 {
482     FIXME("(%p,%p),stub!\n",iface,pszkey);
483     return E_NOTIMPL;
484 }
485
486 /********************************************************************************
487  *        GetObjectIndex (local function)
488  ********************************************************************************/
489 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
490                                           IUnknown* punk,
491                                           LPOLESTR pszkey,
492                                           DWORD *index)
493 {
494
495     DWORD i;
496     BYTE found=0;
497
498     TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
499
500     if (punk==NULL)
501         /* search object identified by a register key */
502         for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++){
503
504             if(This->bindCtxTable[i].regType==1){
505
506                 if ( ( (This->bindCtxTable[i].pkeyObj==NULL) && (pszkey==NULL) ) ||
507                      ( (This->bindCtxTable[i].pkeyObj!=NULL) &&
508                        (pszkey!=NULL) &&
509                        (lstrcmpW(This->bindCtxTable[i].pkeyObj,pszkey)==0)
510                      )
511                    )
512
513                     found=1;
514             }
515         }
516     else
517         /* search object identified by a moniker*/
518         for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
519             if(This->bindCtxTable[i].pObj==punk)
520                 found=1;
521
522     if (index != NULL)
523         *index=i-1;
524
525     if (found)
526         return S_OK;
527     TRACE("key not found\n");
528     return S_FALSE;
529 }
530
531 /******************************************************************************
532  *        CreateBindCtx16
533  ******************************************************************************/
534 HRESULT WINAPI CreateBindCtx16(DWORD reserved, LPBC * ppbc)
535 {
536     FIXME("(%ld,%p),stub!\n",reserved,ppbc);
537     return E_NOTIMPL;
538 }
539
540 /******************************************************************************
541  *        CreateBindCtx (OLE32.52)
542  ******************************************************************************/
543 HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
544 {
545     BindCtxImpl* newBindCtx = 0;
546     HRESULT hr;
547     IID riid=IID_IBindCtx;
548
549     TRACE("(%ld,%p)\n",reserved,ppbc);
550
551     newBindCtx = HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
552
553     if (newBindCtx == 0)
554         return E_OUTOFMEMORY;
555
556     hr = BindCtxImpl_Construct(newBindCtx);
557
558     if (FAILED(hr)){
559
560         HeapFree(GetProcessHeap(),0,newBindCtx);
561         return hr;
562     }
563
564     hr = BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&riid,(void**)ppbc);
565
566     return hr;
567 }
568
569 HRESULT WINAPI BindMoniker(LPMONIKER pmk, DWORD grfOpt, REFIID riid, LPVOID * ppvResult)
570 {
571     HRESULT res;
572     IBindCtx * pbc;
573
574     TRACE("(%p, %lx, %s, %p)\n", pmk, grfOpt, debugstr_guid(riid), ppvResult);
575
576     res = CreateBindCtx(grfOpt, &pbc);
577     if (SUCCEEDED(res))
578         res = IMoniker_BindToObject(pmk, pbc, NULL, riid, ppvResult);
579     return res;
580 }