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