Implemented a large number of the msvideo dll routines.
[wine] / dlls / shell32 / iconcache.c
1 /*
2  *      shell icon cache (SIC)
3  *
4  */
5 #include <string.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include "winbase.h"
9 #include "windef.h"
10 #include "wingdi.h"
11 #include "winuser.h"
12 #include "wine/winuser16.h"
13 #include "wine/winbase16.h"
14 #include "neexe.h"
15 #include "cursoricon.h"
16 #include "module.h"
17 #include "heap.h"
18 #include "debugtools.h"
19 #include "winversion.h"
20
21 #include "shellapi.h"
22 #include "pidl.h"
23 #include "shell32_main.h"
24 #include "wine/undocshell.h"
25 #include "shlwapi.h"
26
27 DEFAULT_DEBUG_CHANNEL(shell)
28
29 #include "pshpack1.h"
30
31 typedef struct
32 {
33     BYTE        bWidth;          /* Width, in pixels, of the image      */
34     BYTE        bHeight;         /* Height, in pixels, of the image     */
35     BYTE        bColorCount;     /* Number of colors in image (0 if >=8bpp) */
36     BYTE        bReserved;       /* Reserved ( must be 0)               */
37     WORD        wPlanes;         /* Color Planes                        */
38     WORD        wBitCount;       /* Bits per pixel                      */
39     DWORD       dwBytesInRes;    /* How many bytes in this resource?    */
40     DWORD       dwImageOffset;   /* Where in the file is this image?    */
41 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
42
43 typedef struct
44 {
45     WORD            idReserved;   /* Reserved (must be 0) */
46     WORD            idType;       /* Resource Type (RES_ICON or RES_CURSOR) */
47     WORD            idCount;      /* How many images */
48     icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
49 } icoICONDIR, *LPicoICONDIR;
50
51 #include "poppack.h"
52
53 #if 0
54 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
55 {       
56         TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
57         TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
58         TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n", 
59         entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
60 }
61 static void dumpIcoDir ( LPicoICONDIR entry )
62 {       
63         TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
64 }
65 #endif
66 /*************************************************************************
67  *                              SHELL_GetResourceTable
68  */
69 static DWORD SHELL_GetResourceTable(HFILE hFile, LPBYTE *retptr)
70 {       IMAGE_DOS_HEADER        mz_header;
71         char                    magic[4];
72         int                     size;
73
74         TRACE("0x%08x %p\n", hFile, retptr);  
75
76         *retptr = NULL;
77         _llseek( hFile, 0, SEEK_SET );
78         if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) || (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
79         { if (mz_header.e_cblp == 1)    /* .ICO file ? */
80           { *retptr = (LPBYTE)-1;       /* ICONHEADER.idType, must be 1 */
81             return 1;
82           }
83           else
84             return 0; /* failed */
85         }
86         _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
87
88         if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
89           return 0;
90
91         _llseek( hFile, mz_header.e_lfanew, SEEK_SET);
92
93         if (*(DWORD*)magic  == IMAGE_NT_SIGNATURE)
94           return IMAGE_NT_SIGNATURE;
95
96         if (*(WORD*)magic == IMAGE_OS2_SIGNATURE)
97         { IMAGE_OS2_HEADER      ne_header;
98           LPBYTE                pTypeInfo = (LPBYTE)-1;
99
100           if (_lread(hFile,&ne_header,sizeof(ne_header))!=sizeof(ne_header))
101             return 0;
102
103           if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
104             return 0;
105
106           size = ne_header.ne_restab - ne_header.ne_rsrctab;
107
108           if( size > sizeof(NE_TYPEINFO) )
109           { pTypeInfo = (BYTE*)HeapAlloc( GetProcessHeap(), 0, size);
110             if( pTypeInfo ) 
111             { _llseek(hFile, mz_header.e_lfanew+ne_header.ne_rsrctab, SEEK_SET);
112               if( _lread( hFile, (char*)pTypeInfo, size) != size )
113               { HeapFree( GetProcessHeap(), 0, pTypeInfo); 
114                 pTypeInfo = NULL;
115               }
116             }
117           }
118           *retptr = pTypeInfo;
119           return IMAGE_OS2_SIGNATURE;
120         }
121         return 0; /* failed */
122 }
123 /*************************************************************************
124  *                      SHELL_LoadResource
125  */
126 static BYTE * SHELL_LoadResource( HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
127 {       BYTE*  ptr;
128
129         TRACE("0x%08x %p 0x%08x\n", hFile, pNInfo, sizeShift);
130
131         *uSize = (DWORD)pNInfo->length << sizeShift;
132         if( (ptr = (BYTE*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
133         { _llseek( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
134           _lread( hFile, (char*)ptr, pNInfo->length << sizeShift);
135           return ptr;
136         }
137         return 0;
138 }
139
140 /*************************************************************************
141  *                      ICO_LoadIcon
142  */
143 static BYTE * ICO_LoadIcon( HFILE hFile, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
144 {       BYTE*  ptr;
145
146         TRACE("0x%08x %p\n", hFile, lpiIDE);
147
148         *uSize = lpiIDE->dwBytesInRes;
149         if( (ptr = (BYTE*)HeapAlloc(GetProcessHeap(),0, *uSize)) )
150         { _llseek( hFile, lpiIDE->dwImageOffset, SEEK_SET);
151           _lread( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
152           return ptr;
153         }
154
155         return 0;
156 }
157
158 /*************************************************************************
159  *                      ICO_GetIconDirectory
160  *
161  * Reads .ico file and build phony ICONDIR struct
162  * see http://www.microsoft.com/win32dev/ui/icons.htm
163  */
164 #define HEADER_SIZE             (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
165 #define HEADER_SIZE_FILE        (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
166
167 static BYTE * ICO_GetIconDirectory( HFILE hFile, LPicoICONDIR* lplpiID, ULONG *uSize ) 
168 {       CURSORICONDIR   lpcid;  /* icon resource in resource-dir format */
169         LPicoICONDIR    lpiID;  /* icon resource in file format */
170         int             i;
171
172         TRACE("0x%08x %p\n", hFile, lplpiID); 
173         
174         _llseek( hFile, 0, SEEK_SET );
175         if( _lread(hFile,(char*)&lpcid, HEADER_SIZE_FILE) != HEADER_SIZE_FILE )
176           return 0;
177
178         if( lpcid.idReserved || (lpcid.idType != 1) || (!lpcid.idCount) )
179           return 0;
180
181         i = lpcid.idCount * sizeof(icoICONDIRENTRY);
182         lpiID = (LPicoICONDIR)HeapAlloc( GetProcessHeap(), 0, HEADER_SIZE_FILE + i);
183
184         if( _lread(hFile,(char*)lpiID->idEntries,i) == i )
185         { CURSORICONDIR * lpID;                 /* icon resource in resource format */
186           *uSize = lpcid.idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
187           if( (lpID = (CURSORICONDIR*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
188           {
189             /* copy the header */
190             lpID->idReserved    = lpiID->idReserved = 0;
191             lpID->idType        = lpiID->idType = 1;
192             lpID->idCount       = lpiID->idCount = lpcid.idCount;
193
194             /* copy the entrys */
195             for( i=0; i < lpiID->idCount; i++ )
196             { memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpiID->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
197               lpID->idEntries[i].wResId = i;
198             }
199
200             *lplpiID = lpiID;
201             return (BYTE *)lpID;
202           }
203         }
204         /* fail */
205
206         HeapFree( GetProcessHeap(), 0, lpiID);
207         return 0;
208 }
209
210 /*************************************************************************
211  *
212  * returns
213  *  failure:0; success: icon handle or nr of icons (nIconIndex-1)
214  */
215 HICON WINAPI ICO_ExtractIconEx(LPCSTR lpszExeFileName, HICON * RetPtr, INT nIconIndex, UINT n, UINT cxDesired, UINT cyDesired )
216 {       HGLOBAL         hRet = 0;
217         LPBYTE          pData;
218         OFSTRUCT        ofs;
219         DWORD           sig;
220         HFILE           hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
221         UINT16          iconDirCount = 0,iconCount = 0;
222         LPBYTE          peimage;
223         HANDLE          fmapping;
224         ULONG           uSize;
225         
226         TRACE("(file %s,start %d,extract %d\n", lpszExeFileName, nIconIndex, n);
227
228         if( hFile == HFILE_ERROR || (nIconIndex!=-1 && !n) )
229           return hRet;
230
231         sig = SHELL_GetResourceTable(hFile,&pData);
232
233 /* ico file */
234         if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
235         { BYTE          *pCIDir = 0;
236           NE_TYPEINFO   *pTInfo = (NE_TYPEINFO*)(pData + 2);
237           NE_NAMEINFO   *pIconStorage = NULL;
238           NE_NAMEINFO   *pIconDir = NULL;
239           LPicoICONDIR  lpiID = NULL;
240
241           TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
242
243           if( pData == (BYTE*)-1 )
244           { pCIDir = ICO_GetIconDirectory(hFile, &lpiID, &uSize);       /* check for .ICO file */
245             if( pCIDir ) 
246             { iconDirCount = 1; iconCount = lpiID->idCount;
247               TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
248             }
249           }
250           else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
251           { if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON )      /* find icon directory and icon repository */
252             { iconDirCount = pTInfo->count;
253               pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
254               TRACE("\tfound directory - %i icon families\n", iconDirCount);
255             }
256             if( pTInfo->type_id == NE_RSCTYPE_ICON ) 
257             { iconCount = pTInfo->count;
258               pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
259               TRACE("\ttotal icons - %i\n", iconCount);
260             }
261             pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
262           }
263
264           if( (pIconStorage && pIconDir) || lpiID )       /* load resources and create icons */
265           { if( nIconIndex == (UINT16)-1 )
266             { RetPtr[0] = iconDirCount;
267             }
268             else if( nIconIndex < iconDirCount )
269             { UINT16   i, icon;
270               if( n > iconDirCount - nIconIndex ) 
271                 n = iconDirCount - nIconIndex;
272
273               for( i = nIconIndex; i < nIconIndex + n; i++ ) 
274               { /* .ICO files have only one icon directory */
275
276                 if( lpiID == NULL )     /* *.ico */
277                   pCIDir = SHELL_LoadResource( hFile, pIconDir + i, *(WORD*)pData, &uSize );
278                 RetPtr[i-nIconIndex] = pLookupIconIdFromDirectoryEx( pCIDir, TRUE,  GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0);
279                 HeapFree(GetProcessHeap(), 0, pCIDir); 
280               }
281
282               for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
283               { pCIDir = NULL;
284                 if( lpiID )
285                 { pCIDir = ICO_LoadIcon( hFile, lpiID->idEntries + RetPtr[icon-nIconIndex], &uSize);
286                 }
287                 else
288                 { for( i = 0; i < iconCount; i++ )
289                   { if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
290                     { pCIDir = SHELL_LoadResource( hFile, pIconStorage + i,*(WORD*)pData, &uSize );
291                     }
292                   }
293                 }
294                 if( pCIDir )
295                 { RetPtr[icon-nIconIndex] = (HICON) pCreateIconFromResourceEx(pCIDir,uSize,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
296                 }
297                 else
298                 { RetPtr[icon-nIconIndex] = 0;
299                 }
300               }
301             }
302           }
303           if( lpiID ) 
304             HeapFree( GetProcessHeap(), 0, lpiID);
305           else 
306             HeapFree( GetProcessHeap(), 0, pData);
307         } 
308 /* end ico file */
309
310 /* exe/dll */
311         if( sig == IMAGE_NT_SIGNATURE)
312         { LPBYTE                idata,igdata;
313           PIMAGE_DOS_HEADER     dheader;
314           PIMAGE_NT_HEADERS     pe_header;
315           PIMAGE_SECTION_HEADER pe_sections;
316           PIMAGE_RESOURCE_DIRECTORY     rootresdir,iconresdir,icongroupresdir;
317           PIMAGE_RESOURCE_DATA_ENTRY    idataent,igdataent;
318           PIMAGE_RESOURCE_DIRECTORY_ENTRY       xresent;
319           int                   i,j;
320                   
321           if ( !(fmapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL))) 
322           { WARN("failed to create filemap.\n");        /* FIXME, INVALID_HANDLE_VALUE? */
323             goto end_2;         /* failure */
324           }
325
326           if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0))) 
327           { WARN("failed to mmap filemap.\n");
328             goto end_2;         /* failure */
329           }
330
331           dheader = (PIMAGE_DOS_HEADER)peimage;
332           pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew);     /* it is a pe header, SHELL_GetResourceTable checked that */
333           pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header)); /* probably makes problems with short PE headers...*/
334           rootresdir = NULL;
335
336           for (i=0;i<pe_header->FileHeader.NumberOfSections;i++) 
337           { if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
338               continue;
339             /* FIXME: doesn't work when the resources are not in a seperate section */
340             if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress) 
341             { rootresdir = (PIMAGE_RESOURCE_DIRECTORY)((char*)peimage+pe_sections[i].PointerToRawData);
342               break;
343             }
344           }
345
346           if (!rootresdir) 
347           { WARN("haven't found section for resource directory.\n");
348             goto end_3;         /* failure */
349           }
350   /* search the group icon dir*/
351           if (!(icongroupresdir = GetResDirEntryW(rootresdir,RT_GROUP_ICONW, (DWORD)rootresdir,FALSE))) 
352           { WARN("No Icongroupresourcedirectory!\n");
353             goto end_3;         /* failure */
354           }
355           iconDirCount = icongroupresdir->NumberOfNamedEntries+icongroupresdir->NumberOfIdEntries;
356
357   /* number of icons requested */
358           if( nIconIndex == -1 )
359           { hRet = iconDirCount;
360             goto end_3;         /* success */
361           }
362
363   /* (nIconIndex < 0): extract the icon by resource id */
364           if( nIconIndex < 0 )
365           {
366             int n = 0;
367             int iId = abs(nIconIndex);
368             PIMAGE_RESOURCE_DIRECTORY_ENTRY xprdeTmp = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
369
370             while(n<iconDirCount && xprdeTmp)
371             {              
372               if(xprdeTmp->u1.Id ==  iId)
373               {
374                   nIconIndex = n;
375                   break;
376               }
377               n++;
378               xprdeTmp++;                  
379             }
380             if (nIconIndex < 0)
381             {
382               WARN("resource id %d not found\n", iId);
383               goto end_3;               /* failure */
384             }
385           }
386
387   /* check nIconIndex to be in range */
388           if (nIconIndex >= iconDirCount) 
389           {
390             WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
391             goto end_3;         /* failure */
392           }
393
394           xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);       /* caller just wanted the number of entries */
395
396           if( n > iconDirCount - nIconIndex )   /* assure we don't get too much ... */
397           { n = iconDirCount - nIconIndex;
398           }
399
400           xresent = xresent+nIconIndex;         /* starting from specified index ... */
401
402           for (i=0;i<n;i++,xresent++) 
403           { PIMAGE_RESOURCE_DIRECTORY   resdir;
404
405             /* go down this resource entry, name */
406             resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
407
408             /* default language (0) */
409             resdir = GetResDirEntryW(resdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
410             igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
411
412             /* lookup address in mapped image for virtual address */
413             igdata = NULL;
414
415             for (j=0;j<pe_header->FileHeader.NumberOfSections;j++) 
416             { if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
417                 continue;
418               if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
419                 continue;
420               igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
421             }
422
423             if (!igdata) 
424             { WARN("no matching real address for icongroup!\n");
425               goto end_3;       /* failure */
426             }
427             RetPtr[i] = (HICON)pLookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
428           }
429
430           if (!(iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE))) 
431           { WARN("No Iconresourcedirectory!\n");
432             goto end_3;         /* failure */
433           }
434
435           for (i=0;i<n;i++) 
436           { PIMAGE_RESOURCE_DIRECTORY   xresdir;
437             xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],(DWORD)rootresdir,FALSE);
438             xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
439             idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
440             idata = NULL;
441
442             /* map virtual to address in image */
443             for (j=0;j<pe_header->FileHeader.NumberOfSections;j++) 
444             { if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
445                 continue;
446               if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
447                 continue;
448               idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
449             }
450             if (!idata) 
451             { WARN("no matching real address found for icondata!\n");
452               RetPtr[i]=0;
453               continue;
454             }
455             RetPtr[i] = (HICON) pCreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
456           }
457           hRet = RetPtr[0];     /* return first icon */
458           goto end_3;           /* sucess */
459         }
460         goto end_1;             /* unknown filetype */
461
462 /* cleaning up (try & catch would be nicer:-) ) */
463 end_3:  UnmapViewOfFile(peimage);       /* success */
464 end_2:  CloseHandle(fmapping);
465 end_1:  _lclose( hFile);
466         return hRet;
467 }
468
469 /********************** THE ICON CACHE ********************************/
470
471 #define INVALID_INDEX -1
472
473 typedef struct
474 {       LPCSTR sSourceFile;     /* file (not path!) containing the icon */
475         DWORD dwSourceIndex;    /* index within the file, if it is a resoure ID it will be negated */
476         DWORD dwListIndex;      /* index within the iconlist */
477         DWORD dwFlags;          /* GIL_* flags */
478         DWORD dwAccessTime;
479 } SIC_ENTRY, * LPSIC_ENTRY;
480
481 static HDPA             sic_hdpa = 0;
482 static CRITICAL_SECTION SHELL32_SicCS = CRITICAL_SECTION_INIT;
483
484 /*****************************************************************************
485  * SIC_CompareEntrys                    [called by comctl32.dll]
486  *
487  * NOTES
488  *  Callback for DPA_Search
489  */
490 INT CALLBACK SIC_CompareEntrys( LPVOID p1, LPVOID p2, LPARAM lparam)
491 {       TRACE("%p %p\n", p1, p2);
492
493         if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
494           return 1;
495
496         if (strcasecmp(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
497           return 1;
498
499         return 0;  
500 }
501 /*****************************************************************************
502  * SIC_IconAppend                       [internal]
503  *
504  * NOTES
505  *  appends a icon pair to the end of the cache
506  */
507 static INT SIC_IconAppend (LPCSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
508 {       LPSIC_ENTRY lpsice;
509         INT ret, index, index1;
510         
511         TRACE("%s %i %x %x\n", sSourceFile, dwSourceIndex, hSmallIcon ,hBigIcon);
512
513         lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
514
515         lpsice->sSourceFile = HEAP_strdupA (GetProcessHeap(), 0, PathFindFileNameA(sSourceFile));
516         lpsice->dwSourceIndex = dwSourceIndex;
517         
518         EnterCriticalSection(&SHELL32_SicCS);
519
520         index = pDPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
521         if ( INVALID_INDEX == index )
522         {
523           SHFree(lpsice);
524           ret = INVALID_INDEX;
525         }
526         else
527         {
528           index = pImageList_AddIcon (ShellSmallIconList, hSmallIcon);
529           index1= pImageList_AddIcon (ShellBigIconList, hBigIcon);
530
531           if (index!=index1)
532           {
533             FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
534           }
535           lpsice->dwListIndex = index;
536           ret = lpsice->dwListIndex;
537         }
538
539         LeaveCriticalSection(&SHELL32_SicCS);
540         return ret;     
541 }
542 /****************************************************************************
543  * SIC_LoadIcon                         [internal]
544  *
545  * NOTES
546  *  gets small/big icon by number from a file
547  */
548 static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
549 {       HICON   hiconLarge=0;
550         HICON   hiconSmall=0;
551
552         ICO_ExtractIconEx(sSourceFile, &hiconLarge, dwSourceIndex, 1, 32, 32  );
553         ICO_ExtractIconEx(sSourceFile, &hiconSmall, dwSourceIndex, 1, 16, 16  );
554
555
556         if ( !hiconLarge ||  !hiconSmall)
557         {
558           WARN("failure loading icon %i from %s (%x %x)\n", dwSourceIndex, sSourceFile, hiconLarge, hiconSmall);
559           return -1;
560         }
561         return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);             
562 }
563 /*****************************************************************************
564  * SIC_GetIconIndex                     [internal]
565  *
566  * Parameters
567  *      sSourceFile     [IN]    filename of file containing the icon
568  *      index           [IN]    index/resID (negated) in this file
569  *
570  * NOTES
571  *  look in the cache for a proper icon. if not available the icon is taken
572  *  from the file and cached
573  */
574 INT SIC_GetIconIndex (LPCSTR sSourceFile, INT dwSourceIndex )
575 {       SIC_ENTRY sice;
576         INT ret, index = INVALID_INDEX;
577                 
578         TRACE("%s %i\n", sSourceFile, dwSourceIndex);
579
580         sice.sSourceFile = PathFindFileNameA(sSourceFile);
581         sice.dwSourceIndex = dwSourceIndex;
582         
583         EnterCriticalSection(&SHELL32_SicCS);
584
585         if (NULL != pDPA_GetPtr (sic_hdpa, 0))
586         {
587           index = pDPA_Search (sic_hdpa, &sice, -1L, SIC_CompareEntrys, 0, 0);
588         }
589
590         if ( INVALID_INDEX == index )
591         {
592           ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
593         }
594         else
595         {
596           TRACE("-- found\n");
597           ret = ((LPSIC_ENTRY)pDPA_GetPtr(sic_hdpa, index))->dwListIndex;
598         }
599
600         LeaveCriticalSection(&SHELL32_SicCS);
601         return ret;
602 }
603 /****************************************************************************
604  * SIC_LoadIcon                         [internal]
605  *
606  * NOTES
607  *  retrives the specified icon from the iconcache. if not found try's to load the icon
608  */
609 static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
610 {       INT index;
611
612         TRACE("%s %i\n", sSourceFile, dwSourceIndex);
613
614         index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
615
616         if (INVALID_INDEX == index)
617         {
618           return INVALID_INDEX;
619         }
620
621         if (bSmallIcon)
622           return pImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
623         return pImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
624         
625 }
626 /*****************************************************************************
627  * SIC_Initialize                       [internal]
628  *
629  * NOTES
630  *  hack to load the resources from the shell32.dll under a different dll name 
631  *  will be removed when the resource-compiler is ready
632  */
633 BOOL SIC_Initialize(void)
634 {
635         HICON           hSm, hLg;
636         UINT            index;
637
638         TRACE("\n");
639
640         if (sic_hdpa)   /* already initialized?*/
641           return TRUE;
642           
643         sic_hdpa = pDPA_Create(16);
644         
645         if (!sic_hdpa)
646         {
647           return(FALSE);
648         }
649
650         ShellSmallIconList = pImageList_Create(16,16,ILC_COLORDDB | ILC_MASK,0,0x20);
651         ShellBigIconList = pImageList_Create(32,32,ILC_COLORDDB | ILC_MASK,0,0x20);
652
653         pImageList_SetBkColor(ShellSmallIconList, GetSysColor(COLOR_WINDOW));
654         pImageList_SetBkColor(ShellBigIconList, GetSysColor(COLOR_WINDOW));
655
656         for (index=1; index<39; index++)
657         {
658           hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 16, 16,LR_SHARED);
659           hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 32, 32,LR_SHARED);
660
661           if(!hSm)
662           {
663             hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 16, 16,LR_SHARED);
664             hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 32, 32,LR_SHARED);
665           }
666           SIC_IconAppend ("shell32.dll", index, hSm, hLg);
667         }
668
669         TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
670
671         return TRUE;
672 }
673 /*************************************************************************
674  * SIC_Destroy
675  *
676  * frees the cache
677  */
678 void SIC_Destroy(void)
679 {
680         LPSIC_ENTRY lpsice;
681         int i;
682
683         TRACE("\n");
684
685         EnterCriticalSection(&SHELL32_SicCS);
686
687         if (sic_hdpa && NULL != pDPA_GetPtr (sic_hdpa, 0))
688         {
689           for (i=0; i < pDPA_GetPtrCount(sic_hdpa); ++i)
690           {
691             lpsice = pDPA_GetPtr(sic_hdpa, i); 
692             SHFree(lpsice);
693           }
694           pDPA_Destroy(sic_hdpa);
695         }
696
697         sic_hdpa = NULL;
698
699         LeaveCriticalSection(&SHELL32_SicCS);
700         DeleteCriticalSection(&SHELL32_SicCS);
701 }
702 /*************************************************************************
703  * Shell_GetImageList                   [SHELL32.71]
704  *
705  * PARAMETERS
706  *  imglist[1|2] [OUT] pointer which recive imagelist handles
707  *
708  */
709 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
710 {       TRACE("(%p,%p)\n",lpBigList,lpSmallList);
711         if (lpBigList)
712         { *lpBigList = ShellBigIconList;
713         }
714         if (lpSmallList)
715         { *lpSmallList = ShellSmallIconList;
716         }
717
718         return TRUE;
719 }
720 /*************************************************************************
721  * PidlToSicIndex                       [INTERNAL]
722  *
723  * PARAMETERS
724  *      sh      [IN]    IShellFolder
725  *      pidl    [IN]
726  *      bBigIcon [IN]
727  *      uFlags  [IN]    GIL_*
728  *      pIndex  [OUT]   index within the SIC
729  *
730  */
731 BOOL PidlToSicIndex (
732         IShellFolder * sh,
733         LPITEMIDLIST pidl,
734         BOOL bBigIcon,
735         UINT uFlags,
736         UINT * pIndex)
737 {       
738         IExtractIconA   *ei;
739         char            szIconFile[MAX_PATH];   /* file containing the icon */
740         INT             iSourceIndex;           /* index or resID(negated) in this file */
741         BOOL            ret = FALSE;
742         UINT            dwFlags = 0;
743         
744         TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
745
746         if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
747         {
748           if (SUCCEEDED(IExtractIconA_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
749           {
750             *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
751             ret = TRUE;
752           }
753           IExtractIconA_Release(ei);
754         }
755
756         if (INVALID_INDEX == *pIndex)   /* default icon when failed */
757           *pIndex = 1;
758
759         return ret;
760
761 }
762
763 /*************************************************************************
764  * SHMapPIDLToSystemImageListIndex      [SHELL32.77]
765  *
766  * PARAMETERS
767  *      sh      [IN]            pointer to an instance of IShellFolder 
768  *      pidl    [IN]
769  *      pIndex  [OUT][OPTIONAL] SIC index for big icon
770  *
771  */
772 int WINAPI SHMapPIDLToSystemImageListIndex(
773         LPSHELLFOLDER sh,
774         LPCITEMIDLIST pidl,
775         UINT * pIndex)
776 {
777         UINT    Index;
778
779         TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
780         pdump(pidl);
781         
782         if (pIndex)
783           PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
784         PidlToSicIndex ( sh, pidl, 0, 0, &Index);
785         return Index;
786 }
787
788 /*************************************************************************
789  * Shell_GetCachedImageIndex            [SHELL32.72]
790  *
791  */
792 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc) 
793 {
794         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
795         return SIC_GetIconIndex(szPath, nIndex);
796 }
797
798 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc) 
799 {       INT ret;
800         LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
801         
802         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
803
804         ret = SIC_GetIconIndex(sTemp, nIndex);
805         HeapFree(GetProcessHeap(),0,sTemp);
806         return ret;
807 }
808
809 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
810 {       if( VERSION_OsIsUnicode())
811           return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
812         return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
813 }
814
815 /*************************************************************************
816  * ExtractIconEx                        [shell32.189]
817  */
818 HICON WINAPI ExtractIconExAW ( LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
819 {       if (VERSION_OsIsUnicode())
820           return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
821         return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
822 }
823 /*************************************************************************
824  * ExtractIconExA                       [shell32.190]
825  * RETURNS
826  *  0 no icon found 
827  *  1 file is not valid
828  *  HICON handle of a icon (phiconLarge/Small == NULL)
829  */
830 HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
831 {       HICON ret=0;
832         
833         TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons );
834
835         if (nIconIndex==-1)     /* Number of icons requested */
836           return ICO_ExtractIconEx(lpszFile, NULL, -1, 0, 0, 0  );
837           
838         
839         if (phiconLarge)
840         { ret = ICO_ExtractIconEx(lpszFile, phiconLarge, nIconIndex, nIcons, 32, 32  );
841           if ( nIcons==1)
842           { ret = phiconLarge[0];           
843           }
844         }
845
846         /* if no pointers given and one icon expected, return the handle directly*/
847         if (!phiconLarge && ! phiconSmall && nIcons==1 )
848           phiconSmall = &ret;
849         
850         if (phiconSmall)
851         { ret = ICO_ExtractIconEx(lpszFile, phiconSmall, nIconIndex, nIcons, 16, 16  );
852           if ( nIcons==1 )
853           { ret = phiconSmall[0];
854           }
855         }
856
857         return ret;
858 }
859 /*************************************************************************
860  * ExtractIconExW                       [shell32.191]
861  */
862 HICON WINAPI ExtractIconExW ( LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
863 {       LPSTR sFile;
864         DWORD ret;
865         
866         TRACE("file=%s idx=%i %p %p num=%i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons );
867
868         sFile = HEAP_strdupWtoA (GetProcessHeap(),0,lpszFile);
869         ret = ExtractIconExA ( sFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
870         HeapFree(GetProcessHeap(),0,sFile);
871         return ret;
872 }