Correct crypt context init in CryptProtectData/CryptUnprotectData.
[wine] / dlls / ole32 / itemmoniker.c
1 /***************************************************************************************
2  *                            ItemMonikers 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 <assert.h>
22 #include <stdarg.h>
23 #include <string.h>
24
25 #define COBJMACROS
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28
29 #include "winerror.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "wine/debug.h"
35 #include "ole2.h"
36 #include "wine/unicode.h"
37 #include "moniker.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40
41 const CLSID CLSID_ItemMoniker = {
42   0x304, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
43 };
44
45 /* ItemMoniker data structure */
46 typedef struct ItemMonikerImpl{
47
48     IMonikerVtbl*  lpvtbl1;  /* VTable relative to the IMoniker interface.*/
49
50     /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
51      * two monikers are equal. That's whay IROTData interface is implemented by monikers.
52      */
53     IROTDataVtbl*  lpvtbl2;  /* VTable relative to the IROTData interface.*/
54
55     ULONG ref; /* reference counter for this object */
56
57     LPOLESTR itemName; /* item name identified by this ItemMoniker */
58
59     LPOLESTR itemDelimiter; /* Delimiter string */
60
61     IUnknown *pMarshal; /* custom marshaler */
62 } ItemMonikerImpl;
63
64 /********************************************************************************/
65 /* ItemMoniker prototype functions :                                            */
66
67 /* IUnknown prototype functions */
68 static HRESULT WINAPI ItemMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
69 static ULONG   WINAPI ItemMonikerImpl_AddRef(IMoniker* iface);
70 static ULONG   WINAPI ItemMonikerImpl_Release(IMoniker* iface);
71
72 /* IPersist prototype functions */
73 static HRESULT WINAPI ItemMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
74
75 /* IPersistStream prototype functions */
76 static HRESULT WINAPI ItemMonikerImpl_IsDirty(IMoniker* iface);
77 static HRESULT WINAPI ItemMonikerImpl_Load(IMoniker* iface, IStream* pStm);
78 static HRESULT WINAPI ItemMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
79 static HRESULT WINAPI ItemMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
80
81 /* IMoniker prototype functions */
82 static HRESULT WINAPI ItemMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
83 static HRESULT WINAPI ItemMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
84 static HRESULT WINAPI ItemMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
85 static HRESULT WINAPI ItemMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
86 static HRESULT WINAPI ItemMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
87 static HRESULT WINAPI ItemMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
88 static HRESULT WINAPI ItemMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
89 static HRESULT WINAPI ItemMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
90 static HRESULT WINAPI ItemMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pItemTime);
91 static HRESULT WINAPI ItemMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
92 static HRESULT WINAPI ItemMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
93 static HRESULT WINAPI ItemMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
94 static HRESULT WINAPI ItemMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
95 static HRESULT WINAPI ItemMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
96 static HRESULT WINAPI ItemMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
97
98 /* Local function used by ItemMoniker implementation */
99 HRESULT WINAPI ItemMonikerImpl_Construct(ItemMonikerImpl* iface, LPCOLESTR lpszDelim,LPCOLESTR lpszPathName);
100 HRESULT WINAPI ItemMonikerImpl_Destroy(ItemMonikerImpl* iface);
101
102 /********************************************************************************/
103 /* IROTData prototype functions                                                 */
104
105 /* IUnknown prototype functions */
106 static HRESULT WINAPI ItemMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
107 static ULONG   WINAPI ItemMonikerROTDataImpl_AddRef(IROTData* iface);
108 static ULONG   WINAPI ItemMonikerROTDataImpl_Release(IROTData* iface);
109
110 /* IROTData prototype function */
111 static HRESULT WINAPI ItemMonikerROTDataImpl_GetComparisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
112
113 /********************************************************************************/
114 /* Virtual function table for the ItemMonikerImpl class which  include IPersist,*/
115 /* IPersistStream and IMoniker functions.                                       */
116 static IMonikerVtbl VT_ItemMonikerImpl =
117     {
118     ItemMonikerImpl_QueryInterface,
119     ItemMonikerImpl_AddRef,
120     ItemMonikerImpl_Release,
121     ItemMonikerImpl_GetClassID,
122     ItemMonikerImpl_IsDirty,
123     ItemMonikerImpl_Load,
124     ItemMonikerImpl_Save,
125     ItemMonikerImpl_GetSizeMax,
126     ItemMonikerImpl_BindToObject,
127     ItemMonikerImpl_BindToStorage,
128     ItemMonikerImpl_Reduce,
129     ItemMonikerImpl_ComposeWith,
130     ItemMonikerImpl_Enum,
131     ItemMonikerImpl_IsEqual,
132     ItemMonikerImpl_Hash,
133     ItemMonikerImpl_IsRunning,
134     ItemMonikerImpl_GetTimeOfLastChange,
135     ItemMonikerImpl_Inverse,
136     ItemMonikerImpl_CommonPrefixWith,
137     ItemMonikerImpl_RelativePathTo,
138     ItemMonikerImpl_GetDisplayName,
139     ItemMonikerImpl_ParseDisplayName,
140     ItemMonikerImpl_IsSystemMoniker
141 };
142
143 /********************************************************************************/
144 /* Virtual function table for the IROTData class.                               */
145 static IROTDataVtbl VT_ROTDataImpl =
146 {
147     ItemMonikerROTDataImpl_QueryInterface,
148     ItemMonikerROTDataImpl_AddRef,
149     ItemMonikerROTDataImpl_Release,
150     ItemMonikerROTDataImpl_GetComparisonData
151 };
152
153 /*******************************************************************************
154  *        ItemMoniker_QueryInterface
155  *******************************************************************************/
156 HRESULT WINAPI ItemMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
157 {
158     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
159
160   TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
161
162   /* Perform a sanity check on the parameters.*/
163     if ( (This==0) || (ppvObject==0) )
164         return E_INVALIDARG;
165
166   /* Initialize the return parameter */
167   *ppvObject = 0;
168
169   /* Compare the riid with the interface IDs implemented by this object.*/
170   if (IsEqualIID(&IID_IUnknown, riid) ||
171       IsEqualIID(&IID_IPersist, riid) ||
172       IsEqualIID(&IID_IPersistStream, riid) ||
173       IsEqualIID(&IID_IMoniker, riid)
174      )
175       *ppvObject = iface;
176
177     else if (IsEqualIID(&IID_IROTData, riid))
178         *ppvObject = (IROTData*)&(This->lpvtbl2);
179     else if (IsEqualIID(&IID_IMarshal, riid))
180     {
181         HRESULT hr = S_OK;
182         if (!This->pMarshal)
183             hr = MonikerMarshal_Create(iface, &This->pMarshal);
184         if (hr != S_OK)
185             return hr;
186         return IUnknown_QueryInterface(This->pMarshal, riid, ppvObject);
187     }
188
189   /* Check that we obtained an interface.*/
190     if ((*ppvObject)==0)
191         return E_NOINTERFACE;
192
193    /* Query Interface always increases the reference count by one when it is successful */
194   ItemMonikerImpl_AddRef(iface);
195
196   return S_OK;
197 }
198
199 /******************************************************************************
200  *        ItemMoniker_AddRef
201  ******************************************************************************/
202 ULONG WINAPI ItemMonikerImpl_AddRef(IMoniker* iface)
203 {
204     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
205
206     TRACE("(%p)\n",This);
207
208     return InterlockedIncrement(&This->ref);
209 }
210
211 /******************************************************************************
212  *        ItemMoniker_Release
213  ******************************************************************************/
214 ULONG WINAPI ItemMonikerImpl_Release(IMoniker* iface)
215 {
216     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
217     ULONG ref;
218
219     TRACE("(%p)\n",This);
220
221     ref = InterlockedDecrement(&This->ref);
222
223     /* destroy the object if there's no more reference on it */
224     if (ref == 0) ItemMonikerImpl_Destroy(This);
225
226     return ref;
227 }
228
229 /******************************************************************************
230  *        ItemMoniker_GetClassID
231  ******************************************************************************/
232 HRESULT WINAPI ItemMonikerImpl_GetClassID(IMoniker* iface,CLSID *pClassID)
233 {
234     TRACE("(%p,%p)\n",iface,pClassID);
235
236     if (pClassID==NULL)
237         return E_POINTER;
238
239     *pClassID = CLSID_ItemMoniker;
240
241     return S_OK;
242 }
243
244 /******************************************************************************
245  *        ItemMoniker_IsDirty
246  ******************************************************************************/
247 HRESULT WINAPI ItemMonikerImpl_IsDirty(IMoniker* iface)
248 {
249     /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
250        method in the OLE-provided moniker interfaces always return S_FALSE because
251        their internal state never changes. */
252
253     TRACE("(%p)\n",iface);
254
255     return S_FALSE;
256 }
257
258 /******************************************************************************
259  *        ItemMoniker_Load
260  ******************************************************************************/
261 HRESULT WINAPI ItemMonikerImpl_Load(IMoniker* iface,IStream* pStm)
262 {
263
264     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
265     HRESULT res;
266     DWORD delimiterLength,nameLength,lenW;
267     CHAR *itemNameA,*itemDelimiterA;
268     ULONG bread;
269
270     TRACE("\n");
271
272     /* for more details about data read by this function see coments of ItemMonikerImpl_Save function */
273
274     /* read item delimiter string length + 1 */
275     res=IStream_Read(pStm,&delimiterLength,sizeof(DWORD),&bread);
276     if (bread != sizeof(DWORD))
277         return E_FAIL;
278
279     /* read item delimiter string */
280     if (!(itemDelimiterA=HeapAlloc(GetProcessHeap(),0,delimiterLength)))
281         return E_OUTOFMEMORY;
282     res=IStream_Read(pStm,itemDelimiterA,delimiterLength,&bread);
283     if (bread != delimiterLength)
284     {
285         HeapFree( GetProcessHeap(), 0, itemDelimiterA );
286         return E_FAIL;
287     }
288
289     lenW = MultiByteToWideChar( CP_ACP, 0, itemDelimiterA, -1, NULL, 0 );
290     This->itemDelimiter=HeapReAlloc(GetProcessHeap(),0,This->itemDelimiter,lenW*sizeof(WCHAR));
291     if (!This->itemDelimiter)
292     {
293         HeapFree( GetProcessHeap(), 0, itemDelimiterA );
294         return E_OUTOFMEMORY;
295     }
296     MultiByteToWideChar( CP_ACP, 0, itemDelimiterA, -1, This->itemDelimiter, lenW );
297     HeapFree( GetProcessHeap(), 0, itemDelimiterA );
298
299     /* read item name string length + 1*/
300     res=IStream_Read(pStm,&nameLength,sizeof(DWORD),&bread);
301     if (bread != sizeof(DWORD))
302         return E_FAIL;
303
304     /* read item name string */
305     if (!(itemNameA=HeapAlloc(GetProcessHeap(),0,nameLength)))
306         return E_OUTOFMEMORY;
307     res=IStream_Read(pStm,itemNameA,nameLength,&bread);
308     if (bread != nameLength)
309     {
310         HeapFree( GetProcessHeap(), 0, itemNameA );
311         return E_FAIL;
312     }
313
314     lenW = MultiByteToWideChar( CP_ACP, 0, itemNameA, -1, NULL, 0 );
315     This->itemName=HeapReAlloc(GetProcessHeap(),0,This->itemName,lenW*sizeof(WCHAR));
316     if (!This->itemName)
317     {
318         HeapFree( GetProcessHeap(), 0, itemNameA );
319         return E_OUTOFMEMORY;
320     }
321     MultiByteToWideChar( CP_ACP, 0, itemNameA, -1, This->itemName, lenW );
322     HeapFree( GetProcessHeap(), 0, itemNameA );
323
324     return res;
325 }
326
327 /******************************************************************************
328  *        ItemMoniker_Save
329  ******************************************************************************/
330 HRESULT WINAPI ItemMonikerImpl_Save(IMoniker* iface,
331                                     IStream* pStm,/* pointer to the stream where the object is to be saved */
332                                     BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
333 {
334     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
335     HRESULT res;
336     CHAR *itemNameA,*itemDelimiterA;
337
338     /* data written by this function are : 1) DWORD : size of item delimiter string ('\0' included ) */
339     /*                                    2) String (type A): item delimiter string ('\0' included)          */
340     /*                                    3) DWORD : size of item name string ('\0' included)       */
341     /*                                    4) String (type A): item name string ('\0' included)               */
342
343     DWORD nameLength = WideCharToMultiByte( CP_ACP, 0, This->itemName, -1, NULL, 0, NULL, NULL);
344     DWORD delimiterLength = WideCharToMultiByte( CP_ACP, 0, This->itemDelimiter, -1, NULL, 0, NULL, NULL);
345     itemNameA=HeapAlloc(GetProcessHeap(),0,nameLength);
346     itemDelimiterA=HeapAlloc(GetProcessHeap(),0,delimiterLength);
347     WideCharToMultiByte( CP_ACP, 0, This->itemName, -1, itemNameA, nameLength, NULL, NULL);
348     WideCharToMultiByte( CP_ACP, 0, This->itemDelimiter, -1, itemDelimiterA, delimiterLength, NULL, NULL);
349
350     TRACE("%p, %s\n", pStm, fClearDirty ? "TRUE" : "FALSE");
351
352     res=IStream_Write(pStm,&delimiterLength,sizeof(DWORD),NULL);
353     res=IStream_Write(pStm,itemDelimiterA,delimiterLength * sizeof(CHAR),NULL);
354     res=IStream_Write(pStm,&nameLength,sizeof(DWORD),NULL);
355     res=IStream_Write(pStm,itemNameA,nameLength * sizeof(CHAR),NULL);
356
357     return res;
358 }
359
360 /******************************************************************************
361  *        ItemMoniker_GetSizeMax
362  ******************************************************************************/
363 HRESULT WINAPI ItemMonikerImpl_GetSizeMax(IMoniker* iface,
364                                           ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
365 {
366     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
367     DWORD delimiterLength=lstrlenW(This->itemDelimiter)+1;
368     DWORD nameLength=lstrlenW(This->itemName)+1;
369
370     TRACE("(%p,%p)\n",iface,pcbSize);
371
372     if (pcbSize!=NULL)
373         return E_POINTER;
374
375     /* for more details see ItemMonikerImpl_Save coments */
376
377     pcbSize->u.LowPart =  sizeof(DWORD) + /* DWORD which contains delimiter length */
378                         delimiterLength + /* item delimiter string */
379                         sizeof(DWORD) + /* DWORD which contains item name length */
380                         nameLength + /* item name string */
381                         34; /* this constant was added ! because when I tested this function it usually */
382                             /*  returns 34 bytes more than the number of bytes used by IMoniker::Save function */
383     pcbSize->u.HighPart=0;
384
385     return S_OK;
386 }
387
388 /******************************************************************************
389  *         ItemMoniker_Construct (local function)
390  *******************************************************************************/
391 HRESULT WINAPI ItemMonikerImpl_Construct(ItemMonikerImpl* This, LPCOLESTR lpszDelim,LPCOLESTR lpszItem)
392 {
393
394     int sizeStr1=lstrlenW(lpszItem), sizeStr2;
395     static const OLECHAR emptystr[1];
396     LPCOLESTR   delim;
397
398     TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszDelim),debugstr_w(lpszItem));
399
400     /* Initialize the virtual fgunction table. */
401     This->lpvtbl1      = &VT_ItemMonikerImpl;
402     This->lpvtbl2      = &VT_ROTDataImpl;
403     This->ref          = 0;
404     This->pMarshal     = NULL;
405
406     This->itemName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr1+1));
407     if (!This->itemName)
408         return E_OUTOFMEMORY;
409     lstrcpyW(This->itemName,lpszItem);
410
411     if (!lpszDelim)
412         FIXME("lpszDelim is NULL. Using empty string which is possibly wrong.\n");
413
414     delim = lpszDelim ? lpszDelim : emptystr;
415
416     sizeStr2=lstrlenW(delim);
417     This->itemDelimiter=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr2+1));
418     if (!This->itemDelimiter) {
419         HeapFree(GetProcessHeap(),0,This->itemName);
420         return E_OUTOFMEMORY;
421     }
422     lstrcpyW(This->itemDelimiter,delim);
423     return S_OK;
424 }
425
426 /******************************************************************************
427  *        ItemMoniker_Destroy (local function)
428  *******************************************************************************/
429 HRESULT WINAPI ItemMonikerImpl_Destroy(ItemMonikerImpl* This)
430 {
431     TRACE("(%p)\n",This);
432
433     if (This->pMarshal) IUnknown_Release(This->pMarshal);
434     HeapFree(GetProcessHeap(),0,This->itemName);
435     HeapFree(GetProcessHeap(),0,This->itemDelimiter);
436     HeapFree(GetProcessHeap(),0,This);
437
438     return S_OK;
439 }
440
441 /******************************************************************************
442  *                  ItemMoniker_BindToObject
443  ******************************************************************************/
444 HRESULT WINAPI ItemMonikerImpl_BindToObject(IMoniker* iface,
445                                             IBindCtx* pbc,
446                                             IMoniker* pmkToLeft,
447                                             REFIID riid,
448                                             VOID** ppvResult)
449 {
450     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
451
452     HRESULT   res;
453     IID    refid=IID_IOleItemContainer;
454     IOleItemContainer *poic=0;
455
456     TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
457
458     if(ppvResult ==NULL)
459         return E_POINTER;
460
461     if(pmkToLeft==NULL)
462         return E_INVALIDARG;
463
464     *ppvResult=0;
465
466     res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&refid,(void**)&poic);
467
468     if (SUCCEEDED(res)){
469
470         res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,riid,ppvResult);
471
472         IOleItemContainer_Release(poic);
473     }
474
475     return res;
476 }
477
478 /******************************************************************************
479  *        ItemMoniker_BindToStorage
480  ******************************************************************************/
481 HRESULT WINAPI ItemMonikerImpl_BindToStorage(IMoniker* iface,
482                                              IBindCtx* pbc,
483                                              IMoniker* pmkToLeft,
484                                              REFIID riid,
485                                              VOID** ppvResult)
486 {
487     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
488
489     HRESULT   res;
490     IOleItemContainer *poic=0;
491
492     TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
493
494     *ppvResult=0;
495
496     if(pmkToLeft==NULL)
497         return E_INVALIDARG;
498
499     res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
500
501     if (SUCCEEDED(res)){
502
503         res=IOleItemContainer_GetObjectStorage(poic,This->itemName,pbc,riid,ppvResult);
504
505         IOleItemContainer_Release(poic);
506     }
507
508     return res;
509 }
510
511 /******************************************************************************
512  *        ItemMoniker_Reduce
513  ******************************************************************************/
514 HRESULT WINAPI ItemMonikerImpl_Reduce(IMoniker* iface,
515                                       IBindCtx* pbc,
516                                       DWORD dwReduceHowFar,
517                                       IMoniker** ppmkToLeft,
518                                       IMoniker** ppmkReduced)
519 {
520     TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
521
522     if (ppmkReduced==NULL)
523         return E_POINTER;
524
525     ItemMonikerImpl_AddRef(iface);
526
527     *ppmkReduced=iface;
528
529     return MK_S_REDUCED_TO_SELF;
530 }
531 /******************************************************************************
532  *        ItemMoniker_ComposeWith
533  ******************************************************************************/
534 HRESULT WINAPI ItemMonikerImpl_ComposeWith(IMoniker* iface,
535                                            IMoniker* pmkRight,
536                                            BOOL fOnlyIfNotGeneric,
537                                            IMoniker** ppmkComposite)
538 {
539     HRESULT res=S_OK;
540     DWORD mkSys,mkSys2;
541     IEnumMoniker* penumMk=0;
542     IMoniker *pmostLeftMk=0;
543     IMoniker* tempMkComposite=0;
544
545     TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
546
547     if ((ppmkComposite==NULL)||(pmkRight==NULL))
548         return E_POINTER;
549
550     *ppmkComposite=0;
551
552     IMoniker_IsSystemMoniker(pmkRight,&mkSys);
553
554     /* If pmkRight is an anti-moniker, the returned moniker is NULL */
555     if(mkSys==MKSYS_ANTIMONIKER)
556         return res;
557
558     else
559         /* if pmkRight is a composite whose leftmost component is an anti-moniker,           */
560         /* the returned moniker is the composite after the leftmost anti-moniker is removed. */
561
562          if(mkSys==MKSYS_GENERICCOMPOSITE){
563
564             res=IMoniker_Enum(pmkRight,TRUE,&penumMk);
565
566             if (FAILED(res))
567                 return res;
568
569             res=IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL);
570
571             IMoniker_IsSystemMoniker(pmostLeftMk,&mkSys2);
572
573             if(mkSys2==MKSYS_ANTIMONIKER){
574
575                 IMoniker_Release(pmostLeftMk);
576
577                 tempMkComposite=iface;
578                 IMoniker_AddRef(iface);
579
580                 while(IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL)==S_OK){
581
582                     res=CreateGenericComposite(tempMkComposite,pmostLeftMk,ppmkComposite);
583
584                     IMoniker_Release(tempMkComposite);
585                     IMoniker_Release(pmostLeftMk);
586
587                     tempMkComposite=*ppmkComposite;
588                     IMoniker_AddRef(tempMkComposite);
589                 }
590                 return res;
591             }
592             else
593                 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
594          }
595          /* If pmkRight is not an anti-moniker, the method combines the two monikers into a generic
596           composite if fOnlyIfNotGeneric is FALSE; if fOnlyIfNotGeneric is TRUE, the method returns
597           a NULL moniker and a return value of MK_E_NEEDGENERIC */
598           else
599             if (!fOnlyIfNotGeneric)
600                 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
601
602             else
603                 return MK_E_NEEDGENERIC;
604 }
605
606 /******************************************************************************
607  *        ItemMoniker_Enum
608  ******************************************************************************/
609 HRESULT WINAPI ItemMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
610 {
611     TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
612
613     if (ppenumMoniker == NULL)
614         return E_POINTER;
615
616     *ppenumMoniker = NULL;
617
618     return S_OK;
619 }
620
621 /******************************************************************************
622  *        ItemMoniker_IsEqual
623  ******************************************************************************/
624 HRESULT WINAPI ItemMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
625 {
626
627     CLSID clsid;
628     LPOLESTR dispName1,dispName2;
629     IBindCtx* bind;
630     HRESULT res = S_FALSE;
631
632     TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
633
634     if (!pmkOtherMoniker) return S_FALSE;
635
636
637     /* check if both are ItemMoniker */
638     if(FAILED (IMoniker_GetClassID(pmkOtherMoniker,&clsid))) return S_FALSE;
639     if(!IsEqualCLSID(&clsid,&CLSID_ItemMoniker)) return S_FALSE;
640
641     /* check if both displaynames are the same */
642     if(SUCCEEDED ((res = CreateBindCtx(0,&bind)))) {
643         if(SUCCEEDED (IMoniker_GetDisplayName(iface,bind,NULL,&dispName1))) {
644             if(SUCCEEDED (IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&dispName2))) {
645                 if(lstrcmpW(dispName1,dispName2)==0) res = S_OK;
646                 CoTaskMemFree(dispName2);
647             }
648             CoTaskMemFree(dispName1);
649         }
650     }
651     return res;
652 }
653
654 /******************************************************************************
655  *        ItemMoniker_Hash
656  ******************************************************************************/
657 HRESULT WINAPI ItemMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
658 {
659     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
660
661     int  h = 0,i,skip,len;
662     int  off = 0;
663     LPOLESTR val;
664
665     if (pdwHash==NULL)
666         return E_POINTER;
667
668     val =  This->itemName;
669     len = lstrlenW(val);
670
671     if (len < 16) {
672         for (i = len ; i > 0; i--) {
673             h = (h * 37) + val[off++];
674         }
675     } else {
676         /* only sample some characters */
677         skip = len / 8;
678         for (i = len ; i > 0; i -= skip, off += skip) {
679             h = (h * 39) + val[off];
680         }
681     }
682
683     *pdwHash=h;
684
685     return S_OK;
686 }
687
688 /******************************************************************************
689  *        ItemMoniker_IsRunning
690  ******************************************************************************/
691 HRESULT WINAPI ItemMonikerImpl_IsRunning(IMoniker* iface,
692                                          IBindCtx* pbc,
693                                          IMoniker* pmkToLeft,
694                                          IMoniker* pmkNewlyRunning)
695 {
696     IRunningObjectTable* rot;
697     HRESULT res;
698     IOleItemContainer *poic=0;
699     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
700
701     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
702
703     /* If pmkToLeft is NULL, this method returns TRUE if pmkNewlyRunning is non-NULL and is equal to this */
704     /* moniker. Otherwise, the method checks the ROT to see whether this moniker is running.              */
705     if (pmkToLeft==NULL)
706         if ((pmkNewlyRunning!=NULL)&&(IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK))
707             return S_OK;
708         else {
709             if (pbc==NULL)
710                 return E_POINTER;
711
712             res=IBindCtx_GetRunningObjectTable(pbc,&rot);
713
714             if (FAILED(res))
715                 return res;
716
717             res = IRunningObjectTable_IsRunning(rot,iface);
718
719             IRunningObjectTable_Release(rot);
720         }
721     else{
722
723         /* If pmkToLeft is non-NULL, the method calls IMoniker::BindToObject on the pmkToLeft parameter,         */
724         /* requesting an IOleItemContainer interface pointer. The method then calls IOleItemContainer::IsRunning,*/
725         /* passing the string contained within this moniker. */
726
727         res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
728
729         if (SUCCEEDED(res)){
730
731             res=IOleItemContainer_IsRunning(poic,This->itemName);
732
733             IOleItemContainer_Release(poic);
734         }
735     }
736
737     return res;
738 }
739
740 /******************************************************************************
741  *        ItemMoniker_GetTimeOfLastChange
742  ******************************************************************************/
743 HRESULT WINAPI ItemMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
744                                                    IBindCtx* pbc,
745                                                    IMoniker* pmkToLeft,
746                                                    FILETIME* pItemTime)
747 {
748     IRunningObjectTable* rot;
749     HRESULT res;
750     IMoniker *compositeMk;
751
752     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pItemTime);
753
754     if (pItemTime==NULL)
755         return E_INVALIDARG;
756
757     /* If pmkToLeft is NULL, this method returns MK_E_NOTBINDABLE */
758     if (pmkToLeft==NULL)
759
760         return MK_E_NOTBINDABLE;
761     else {
762
763         /* Otherwise, the method creates a composite of pmkToLeft and this moniker and uses the ROT to access  */
764         /* the time of last change. If the object is not in the ROT, the method calls                          */
765         /* IMoniker::GetTimeOfLastChange on the pmkToLeft parameter.                                            */
766
767         res=CreateGenericComposite(pmkToLeft,iface,&compositeMk);
768
769         res=IBindCtx_GetRunningObjectTable(pbc,&rot);
770
771         if (IRunningObjectTable_GetTimeOfLastChange(rot,compositeMk,pItemTime)!=S_OK)
772
773             res=IMoniker_GetTimeOfLastChange(pmkToLeft,pbc,NULL,pItemTime);
774
775         IMoniker_Release(compositeMk);
776     }
777
778     return res;
779 }
780
781 /******************************************************************************
782  *        ItemMoniker_Inverse
783  ******************************************************************************/
784 HRESULT WINAPI ItemMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
785 {
786     TRACE("(%p,%p)\n",iface,ppmk);
787
788     if (ppmk==NULL)
789         return E_POINTER;
790
791     return CreateAntiMoniker(ppmk);
792 }
793
794 /******************************************************************************
795  *        ItemMoniker_CommonPrefixWith
796  ******************************************************************************/
797 HRESULT WINAPI ItemMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
798 {
799     DWORD mkSys;
800     
801     TRACE("(%p,%p)\n", pmkOther, ppmkPrefix);
802
803     IMoniker_IsSystemMoniker(pmkOther,&mkSys);
804     /* If the other moniker is an item moniker that is equal to this moniker, this method sets *ppmkPrefix */
805     /* to this moniker and returns MK_S_US */
806
807     if((mkSys==MKSYS_ITEMMONIKER) && (IMoniker_IsEqual(iface,pmkOther)==S_OK) ){
808
809         *ppmkPrefix=iface;
810
811         IMoniker_AddRef(iface);
812
813         return MK_S_US;
814     }
815     else
816         /* otherwise, the method calls the MonikerCommonPrefixWith function. This function correctly handles */
817         /* the case where the other moniker is a generic composite. */
818         return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
819 }
820
821 /******************************************************************************
822  *        ItemMoniker_RelativePathTo
823  ******************************************************************************/
824 HRESULT WINAPI ItemMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
825 {
826     TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
827
828     if (ppmkRelPath==NULL)
829         return E_POINTER;
830
831     *ppmkRelPath=0;
832
833     return MK_E_NOTBINDABLE;
834 }
835
836 /******************************************************************************
837  *        ItemMoniker_GetDisplayName
838  ******************************************************************************/
839 HRESULT WINAPI ItemMonikerImpl_GetDisplayName(IMoniker* iface,
840                                               IBindCtx* pbc,
841                                               IMoniker* pmkToLeft,
842                                               LPOLESTR *ppszDisplayName)
843 {
844     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
845
846     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
847
848     if (ppszDisplayName==NULL)
849         return E_POINTER;
850
851     if (pmkToLeft!=NULL){
852         return E_INVALIDARG;
853     }
854
855     *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(lstrlenW(This->itemDelimiter)+lstrlenW(This->itemName)+1));
856
857     if (*ppszDisplayName==NULL)
858         return E_OUTOFMEMORY;
859
860     lstrcpyW(*ppszDisplayName,This->itemDelimiter);
861     lstrcatW(*ppszDisplayName,This->itemName);
862
863     TRACE("-- %s\n", debugstr_w(*ppszDisplayName));
864
865     return S_OK;
866 }
867
868 /******************************************************************************
869  *        ItemMoniker_ParseDisplayName
870  ******************************************************************************/
871 HRESULT WINAPI ItemMonikerImpl_ParseDisplayName(IMoniker* iface,
872                                                 IBindCtx* pbc,
873                                                 IMoniker* pmkToLeft,
874                                                 LPOLESTR pszDisplayName,
875                                                 ULONG* pchEaten,
876                                                 IMoniker** ppmkOut)
877 {
878     IOleItemContainer* poic=0;
879     IParseDisplayName* ppdn=0;
880     LPOLESTR displayName;
881     HRESULT res;
882     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
883
884     TRACE("%s\n", debugstr_w(pszDisplayName));
885
886     /* If pmkToLeft is NULL, this method returns MK_E_SYNTAX */
887     if (pmkToLeft==NULL)
888
889         return MK_E_SYNTAX;
890
891     else{
892         /* Otherwise, the method calls IMoniker::BindToObject on the pmkToLeft parameter, requesting an */
893         /* IParseDisplayName interface pointer to the object identified by the moniker, and passes the display */
894         /* name to IParseDisplayName::ParseDisplayName */
895         res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
896
897         if (SUCCEEDED(res)){
898
899             res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,&IID_IParseDisplayName,(void**)&ppdn);
900
901             res=IMoniker_GetDisplayName(iface,pbc,NULL,&displayName);
902
903             res=IParseDisplayName_ParseDisplayName(ppdn,pbc,displayName,pchEaten,ppmkOut);
904
905             IOleItemContainer_Release(poic);
906             IParseDisplayName_Release(ppdn);
907         }
908     }
909     return res;
910 }
911
912 /******************************************************************************
913  *        ItemMoniker_IsSystemMoniker
914  ******************************************************************************/
915 HRESULT WINAPI ItemMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
916 {
917     TRACE("(%p,%p)\n",iface,pwdMksys);
918
919     if (!pwdMksys)
920         return E_POINTER;
921
922     (*pwdMksys)=MKSYS_ITEMMONIKER;
923
924     return S_OK;
925 }
926
927 /*******************************************************************************
928  *        ItemMonikerIROTData_QueryInterface
929  *******************************************************************************/
930 HRESULT WINAPI ItemMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
931 {
932
933     ICOM_THIS_From_IROTData(IMoniker, iface);
934
935     TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
936
937     return ItemMonikerImpl_QueryInterface(This, riid, ppvObject);
938 }
939
940 /***********************************************************************
941  *        ItemMonikerIROTData_AddRef
942  */
943 ULONG   WINAPI ItemMonikerROTDataImpl_AddRef(IROTData *iface)
944 {
945     ICOM_THIS_From_IROTData(IMoniker, iface);
946
947     TRACE("(%p)\n",iface);
948
949     return ItemMonikerImpl_AddRef(This);
950 }
951
952 /***********************************************************************
953  *        ItemMonikerIROTData_Release
954  */
955 ULONG   WINAPI ItemMonikerROTDataImpl_Release(IROTData* iface)
956 {
957     ICOM_THIS_From_IROTData(IMoniker, iface);
958
959     TRACE("(%p)\n",iface);
960
961     return ItemMonikerImpl_Release(This);
962 }
963
964 /******************************************************************************
965  *        ItemMonikerIROTData_GetComparaisonData
966  ******************************************************************************/
967 HRESULT WINAPI ItemMonikerROTDataImpl_GetComparisonData(IROTData* iface,
968                                                          BYTE* pbData,
969                                                          ULONG cbMax,
970                                                          ULONG* pcbData)
971 {
972     ICOM_THIS_From_IROTData(IMoniker, iface);
973     ItemMonikerImpl *This1 = (ItemMonikerImpl *)This;
974     int len = (strlenW(This1->itemName)+1);
975     int i;
976     LPWSTR pszItemName;
977     LPWSTR pszItemDelimiter;
978
979     TRACE("(%p, %lu, %p)\n", pbData, cbMax, pcbData);
980
981     *pcbData = sizeof(CLSID) + sizeof(WCHAR) + len * sizeof(WCHAR);
982     if (cbMax < *pcbData)
983         return E_OUTOFMEMORY;
984
985     /* write CLSID */
986     memcpy(pbData, &CLSID_ItemMoniker, sizeof(CLSID));
987     /* write delimiter */
988     pszItemDelimiter = (LPWSTR)(pbData+sizeof(CLSID));
989     *pszItemDelimiter = *This1->itemDelimiter;
990     /* write name */
991     pszItemName = pszItemDelimiter + 1;
992     for (i = 0; i < len; i++)
993         pszItemName[i] = toupperW(This1->itemName[i]);
994
995     return S_OK;
996 }
997
998 /******************************************************************************
999  *        CreateItemMoniker     [OLE32.@]
1000  ******************************************************************************/
1001 HRESULT WINAPI CreateItemMoniker(LPCOLESTR lpszDelim,LPCOLESTR  lpszItem, LPMONIKER * ppmk)
1002 {
1003     ItemMonikerImpl* newItemMoniker;
1004     HRESULT        hr;
1005
1006     TRACE("(%s,%s,%p)\n",debugstr_w(lpszDelim),debugstr_w(lpszItem),ppmk);
1007
1008     newItemMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ItemMonikerImpl));
1009
1010     if (!newItemMoniker)
1011         return STG_E_INSUFFICIENTMEMORY;
1012
1013     hr = ItemMonikerImpl_Construct(newItemMoniker,lpszDelim,lpszItem);
1014
1015     if (FAILED(hr)){
1016
1017         HeapFree(GetProcessHeap(),0,newItemMoniker);
1018     return hr;
1019     }
1020
1021     return ItemMonikerImpl_QueryInterface((IMoniker*)newItemMoniker,&IID_IMoniker,(void**)ppmk);
1022 }
1023
1024 static HRESULT WINAPI ItemMonikerCF_QueryInterface(LPCLASSFACTORY iface,
1025                                                   REFIID riid, LPVOID *ppv)
1026 {
1027     *ppv = NULL;
1028     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1029     {
1030         *ppv = iface;
1031         IUnknown_AddRef(iface);
1032         return S_OK;
1033     }
1034     return E_NOINTERFACE;
1035 }
1036
1037 static ULONG WINAPI ItemMonikerCF_AddRef(LPCLASSFACTORY iface)
1038 {
1039     return 2; /* non-heap based object */
1040 }
1041
1042 static ULONG WINAPI ItemMonikerCF_Release(LPCLASSFACTORY iface)
1043 {
1044     return 1; /* non-heap based object */
1045 }
1046
1047 static HRESULT WINAPI ItemMonikerCF_CreateInstance(LPCLASSFACTORY iface,
1048     LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1049 {
1050     ItemMonikerImpl* newItemMoniker;
1051     HRESULT  hr;
1052     static const WCHAR wszEmpty[] = { 0 };
1053
1054     TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1055
1056     *ppv = NULL;
1057
1058     if (pUnk)
1059         return CLASS_E_NOAGGREGATION;
1060
1061     newItemMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ItemMonikerImpl));
1062     if (!newItemMoniker)
1063         return E_OUTOFMEMORY;
1064
1065     hr = ItemMonikerImpl_Construct(newItemMoniker, wszEmpty, wszEmpty);
1066
1067     if (SUCCEEDED(hr))
1068         hr = ItemMonikerImpl_QueryInterface((IMoniker*)newItemMoniker, riid, ppv);
1069     if (FAILED(hr))
1070         HeapFree(GetProcessHeap(),0,newItemMoniker);
1071
1072     return hr;
1073 }
1074
1075 static HRESULT WINAPI ItemMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1076 {
1077     FIXME("(%d), stub!\n",fLock);
1078     return S_OK;
1079 }
1080
1081 static const IClassFactoryVtbl ItemMonikerCFVtbl =
1082 {
1083     ItemMonikerCF_QueryInterface,
1084     ItemMonikerCF_AddRef,
1085     ItemMonikerCF_Release,
1086     ItemMonikerCF_CreateInstance,
1087     ItemMonikerCF_LockServer
1088 };
1089 static const IClassFactoryVtbl *ItemMonikerCF = &ItemMonikerCFVtbl;
1090
1091 HRESULT ItemMonikerCF_Create(REFIID riid, LPVOID *ppv)
1092 {
1093     return IClassFactory_QueryInterface((IClassFactory *)&ItemMonikerCF, riid, ppv);
1094 }