Avoid going past the end of the relocation section. Skip sanity checks
[wine/multimedia.git] / dlls / shell32 / iconcache.c
blob1c7f38e41607189c28dbee44b01d5c2e53c4cb03
1 /*
2 * shell icon cache (SIC)
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"
20 #include "shellapi.h"
21 #include "shlguid.h"
22 #include "pidl.h"
23 #include "shell32_main.h"
24 #include "wine/undocshell.h"
25 #include "shlwapi.h"
27 DEFAULT_DEBUG_CHANNEL(shell)
29 #include "pshpack1.h"
31 typedef struct
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;
43 typedef struct
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;
51 #include "poppack.h"
53 #if 0
54 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
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);
61 static void dumpIcoDir ( LPicoICONDIR entry )
63 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
65 #endif
66 /*************************************************************************
67 * SHELL_GetResourceTable
69 static DWORD SHELL_GetResourceTable(HFILE hFile, LPBYTE *retptr)
70 { IMAGE_DOS_HEADER mz_header;
71 char magic[4];
72 int size;
74 TRACE("0x%08x %p\n", hFile, retptr);
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;
83 else
84 return 0; /* failed */
86 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
88 if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
89 return 0;
91 _llseek( hFile, mz_header.e_lfanew, SEEK_SET);
93 if (*(DWORD*)magic == IMAGE_NT_SIGNATURE)
94 return IMAGE_NT_SIGNATURE;
96 if (*(WORD*)magic == IMAGE_OS2_SIGNATURE)
97 { IMAGE_OS2_HEADER ne_header;
98 LPBYTE pTypeInfo = (LPBYTE)-1;
100 if (_lread(hFile,&ne_header,sizeof(ne_header))!=sizeof(ne_header))
101 return 0;
103 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
104 return 0;
106 size = ne_header.ne_restab - ne_header.ne_rsrctab;
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;
118 *retptr = pTypeInfo;
119 return IMAGE_OS2_SIGNATURE;
121 return 0; /* failed */
123 /*************************************************************************
124 * SHELL_LoadResource
126 static BYTE * SHELL_LoadResource( HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
127 { BYTE* ptr;
129 TRACE("0x%08x %p 0x%08x\n", hFile, pNInfo, sizeShift);
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;
137 return 0;
140 /*************************************************************************
141 * ICO_LoadIcon
143 static BYTE * ICO_LoadIcon( HFILE hFile, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
144 { BYTE* ptr;
146 TRACE("0x%08x %p\n", hFile, lpiIDE);
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;
155 return 0;
158 /*************************************************************************
159 * ICO_GetIconDirectory
161 * Reads .ico file and build phony ICONDIR struct
162 * see http://www.microsoft.com/win32dev/ui/icons.htm
164 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
165 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
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;
172 TRACE("0x%08x %p\n", hFile, lplpiID);
174 _llseek( hFile, 0, SEEK_SET );
175 if( _lread(hFile,(char*)&lpcid, HEADER_SIZE_FILE) != HEADER_SIZE_FILE )
176 return 0;
178 if( lpcid.idReserved || (lpcid.idType != 1) || (!lpcid.idCount) )
179 return 0;
181 i = lpcid.idCount * sizeof(icoICONDIRENTRY);
182 lpiID = (LPicoICONDIR)HeapAlloc( GetProcessHeap(), 0, HEADER_SIZE_FILE + i);
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) ))
189 /* copy the header */
190 lpID->idReserved = lpiID->idReserved = 0;
191 lpID->idType = lpiID->idType = 1;
192 lpID->idCount = lpiID->idCount = lpcid.idCount;
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;
200 *lplpiID = lpiID;
201 return (BYTE *)lpID;
204 /* fail */
206 HeapFree( GetProcessHeap(), 0, lpiID);
207 return 0;
210 /*************************************************************************
212 * returns
213 * failure:0; success: icon handle or nr of icons (nIconIndex-1)
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;
226 TRACE("(file %s,start %d,extract %d\n", lpszExeFileName, nIconIndex, n);
228 if( hFile == HFILE_ERROR || (nIconIndex!=-1 && !n) )
229 return hRet;
231 sig = SHELL_GetResourceTable(hFile,&pData);
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;
241 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
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);
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);
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);
261 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
264 if( (pIconStorage && pIconDir) || lpiID ) /* load resources and create icons */
265 { if( nIconIndex == (UINT16)-1 )
266 { RetPtr[0] = iconDirCount;
268 else if( nIconIndex < iconDirCount )
269 { UINT16 i, icon;
270 if( n > iconDirCount - nIconIndex )
271 n = iconDirCount - nIconIndex;
273 for( i = nIconIndex; i < nIconIndex + n; i++ )
274 { /* .ICO files have only one icon directory */
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);
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);
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 );
294 if( pCIDir )
295 { RetPtr[icon-nIconIndex] = (HICON) pCreateIconFromResourceEx(pCIDir,uSize,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
297 else
298 { RetPtr[icon-nIconIndex] = 0;
303 if( lpiID )
304 HeapFree( GetProcessHeap(), 0, lpiID);
305 else
306 HeapFree( GetProcessHeap(), 0, pData);
308 /* end ico file */
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;
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 */
326 if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0)))
327 { WARN("failed to mmap filemap.\n");
328 goto end_2; /* failure */
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;
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;
346 if (!rootresdir)
347 { WARN("haven't found section for resource directory.\n");
348 goto end_3; /* failure */
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 */
355 iconDirCount = icongroupresdir->NumberOfNamedEntries+icongroupresdir->NumberOfIdEntries;
357 /* number of icons requested */
358 if( nIconIndex == -1 )
359 { hRet = iconDirCount;
360 goto end_3; /* success */
363 /* (nIconIndex < 0): extract the icon by resource id */
364 if( nIconIndex < 0 )
366 int n = 0;
367 int iId = abs(nIconIndex);
368 PIMAGE_RESOURCE_DIRECTORY_ENTRY xprdeTmp = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
370 while(n<iconDirCount && xprdeTmp)
372 if(xprdeTmp->u1.Id == iId)
374 nIconIndex = n;
375 break;
377 n++;
378 xprdeTmp++;
380 if (nIconIndex < 0)
382 WARN("resource id %d not found\n", iId);
383 goto end_3; /* failure */
387 /* check nIconIndex to be in range */
388 if (nIconIndex >= iconDirCount)
390 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
391 goto end_3; /* failure */
394 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1); /* caller just wanted the number of entries */
396 if( n > iconDirCount - nIconIndex ) /* assure we don't get too much ... */
397 { n = iconDirCount - nIconIndex;
400 xresent = xresent+nIconIndex; /* starting from specified index ... */
402 for (i=0;i<n;i++,xresent++)
403 { PIMAGE_RESOURCE_DIRECTORY resdir;
405 /* go down this resource entry, name */
406 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
408 /* default language (0) */
409 resdir = GetResDirEntryW(resdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
410 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
412 /* lookup address in mapped image for virtual address */
413 igdata = NULL;
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);
423 if (!igdata)
424 { WARN("no matching real address for icongroup!\n");
425 goto end_3; /* failure */
427 RetPtr[i] = (HICON)pLookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
430 if (!(iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE)))
431 { WARN("No Iconresourcedirectory!\n");
432 goto end_3; /* failure */
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;
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);
450 if (!idata)
451 { WARN("no matching real address found for icondata!\n");
452 RetPtr[i]=0;
453 continue;
455 RetPtr[i] = (HICON) pCreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
457 hRet = RetPtr[0]; /* return first icon */
458 goto end_3; /* sucess */
460 goto end_1; /* unknown filetype */
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;
469 /********************** THE ICON CACHE ********************************/
471 #define INVALID_INDEX -1
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;
481 static HDPA sic_hdpa = 0;
482 static CRITICAL_SECTION SHELL32_SicCS = CRITICAL_SECTION_INIT;
484 /*****************************************************************************
485 * SIC_CompareEntrys [called by comctl32.dll]
487 * NOTES
488 * Callback for DPA_Search
490 INT CALLBACK SIC_CompareEntrys( LPVOID p1, LPVOID p2, LPARAM lparam)
491 { TRACE("%p %p\n", p1, p2);
493 if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
494 return 1;
496 if (strcasecmp(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
497 return 1;
499 return 0;
501 /*****************************************************************************
502 * SIC_IconAppend [internal]
504 * NOTES
505 * appends a icon pair to the end of the cache
507 static INT SIC_IconAppend (LPCSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
508 { LPSIC_ENTRY lpsice;
509 INT ret, index, index1;
511 TRACE("%s %i %x %x\n", sSourceFile, dwSourceIndex, hSmallIcon ,hBigIcon);
513 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
515 lpsice->sSourceFile = HEAP_strdupA (GetProcessHeap(), 0, PathFindFileNameA(sSourceFile));
516 lpsice->dwSourceIndex = dwSourceIndex;
518 EnterCriticalSection(&SHELL32_SicCS);
520 index = pDPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
521 if ( INVALID_INDEX == index )
523 SHFree(lpsice);
524 ret = INVALID_INDEX;
526 else
528 index = pImageList_AddIcon (ShellSmallIconList, hSmallIcon);
529 index1= pImageList_AddIcon (ShellBigIconList, hBigIcon);
531 if (index!=index1)
533 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
535 lpsice->dwListIndex = index;
536 ret = lpsice->dwListIndex;
539 LeaveCriticalSection(&SHELL32_SicCS);
540 return ret;
542 /****************************************************************************
543 * SIC_LoadIcon [internal]
545 * NOTES
546 * gets small/big icon by number from a file
548 static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
549 { HICON hiconLarge=0;
550 HICON hiconSmall=0;
552 ICO_ExtractIconEx(sSourceFile, &hiconLarge, dwSourceIndex, 1, 32, 32 );
553 ICO_ExtractIconEx(sSourceFile, &hiconSmall, dwSourceIndex, 1, 16, 16 );
556 if ( !hiconLarge || !hiconSmall)
558 WARN("failure loading icon %i from %s (%x %x)\n", dwSourceIndex, sSourceFile, hiconLarge, hiconSmall);
559 return -1;
561 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
563 /*****************************************************************************
564 * SIC_GetIconIndex [internal]
566 * Parameters
567 * sSourceFile [IN] filename of file containing the icon
568 * index [IN] index/resID (negated) in this file
570 * NOTES
571 * look in the cache for a proper icon. if not available the icon is taken
572 * from the file and cached
574 INT SIC_GetIconIndex (LPCSTR sSourceFile, INT dwSourceIndex )
575 { SIC_ENTRY sice;
576 INT ret, index = INVALID_INDEX;
578 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
580 sice.sSourceFile = PathFindFileNameA(sSourceFile);
581 sice.dwSourceIndex = dwSourceIndex;
583 EnterCriticalSection(&SHELL32_SicCS);
585 if (NULL != pDPA_GetPtr (sic_hdpa, 0))
587 index = pDPA_Search (sic_hdpa, &sice, -1L, SIC_CompareEntrys, 0, 0);
590 if ( INVALID_INDEX == index )
592 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
594 else
596 TRACE("-- found\n");
597 ret = ((LPSIC_ENTRY)pDPA_GetPtr(sic_hdpa, index))->dwListIndex;
600 LeaveCriticalSection(&SHELL32_SicCS);
601 return ret;
603 /****************************************************************************
604 * SIC_LoadIcon [internal]
606 * NOTES
607 * retrives the specified icon from the iconcache. if not found try's to load the icon
609 static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
610 { INT index;
612 TRACE("%s %i\n", sSourceFile, dwSourceIndex);
614 index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
616 if (INVALID_INDEX == index)
618 return INVALID_INDEX;
621 if (bSmallIcon)
622 return pImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
623 return pImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
626 /*****************************************************************************
627 * SIC_Initialize [internal]
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
633 BOOL SIC_Initialize(void)
635 HICON hSm, hLg;
636 UINT index;
638 TRACE("\n");
640 if (sic_hdpa) /* already initialized?*/
641 return TRUE;
643 sic_hdpa = pDPA_Create(16);
645 if (!sic_hdpa)
647 return(FALSE);
650 ShellSmallIconList = pImageList_Create(16,16,ILC_COLORDDB | ILC_MASK,0,0x20);
651 ShellBigIconList = pImageList_Create(32,32,ILC_COLORDDB | ILC_MASK,0,0x20);
653 pImageList_SetBkColor(ShellSmallIconList, GetSysColor(COLOR_WINDOW));
654 pImageList_SetBkColor(ShellBigIconList, GetSysColor(COLOR_WINDOW));
656 for (index=1; index<39; index++)
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);
661 if(!hSm)
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);
666 SIC_IconAppend ("shell32.dll", index, hSm, hLg);
669 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
671 return TRUE;
673 /*************************************************************************
674 * SIC_Destroy
676 * frees the cache
678 void SIC_Destroy(void)
680 LPSIC_ENTRY lpsice;
681 int i;
683 TRACE("\n");
685 EnterCriticalSection(&SHELL32_SicCS);
687 if (sic_hdpa && NULL != pDPA_GetPtr (sic_hdpa, 0))
689 for (i=0; i < pDPA_GetPtrCount(sic_hdpa); ++i)
691 lpsice = pDPA_GetPtr(sic_hdpa, i);
692 SHFree(lpsice);
694 pDPA_Destroy(sic_hdpa);
697 sic_hdpa = NULL;
699 LeaveCriticalSection(&SHELL32_SicCS);
700 DeleteCriticalSection(&SHELL32_SicCS);
702 /*************************************************************************
703 * Shell_GetImageList [SHELL32.71]
705 * PARAMETERS
706 * imglist[1|2] [OUT] pointer which recive imagelist handles
709 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
710 { TRACE("(%p,%p)\n",lpBigList,lpSmallList);
711 if (lpBigList)
712 { *lpBigList = ShellBigIconList;
714 if (lpSmallList)
715 { *lpSmallList = ShellSmallIconList;
718 return TRUE;
720 /*************************************************************************
721 * PidlToSicIndex [INTERNAL]
723 * PARAMETERS
724 * sh [IN] IShellFolder
725 * pidl [IN]
726 * bBigIcon [IN]
727 * uFlags [IN] GIL_*
728 * pIndex [OUT] index within the SIC
731 BOOL PidlToSicIndex (
732 IShellFolder * sh,
733 LPITEMIDLIST pidl,
734 BOOL bBigIcon,
735 UINT uFlags,
736 UINT * pIndex)
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;
744 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
746 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
748 if (SUCCEEDED(IExtractIconA_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
750 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
751 ret = TRUE;
753 IExtractIconA_Release(ei);
756 if (INVALID_INDEX == *pIndex) /* default icon when failed */
757 *pIndex = 1;
759 return ret;
763 /*************************************************************************
764 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
766 * PARAMETERS
767 * sh [IN] pointer to an instance of IShellFolder
768 * pidl [IN]
769 * pIndex [OUT][OPTIONAL] SIC index for big icon
772 int WINAPI SHMapPIDLToSystemImageListIndex(
773 LPSHELLFOLDER sh,
774 LPCITEMIDLIST pidl,
775 UINT * pIndex)
777 UINT Index;
779 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
780 pdump(pidl);
782 if (pIndex)
783 PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
784 PidlToSicIndex ( sh, pidl, 0, 0, &Index);
785 return Index;
788 /*************************************************************************
789 * Shell_GetCachedImageIndex [SHELL32.72]
792 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
794 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
795 return SIC_GetIconIndex(szPath, nIndex);
798 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
799 { INT ret;
800 LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
802 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
804 ret = SIC_GetIconIndex(sTemp, nIndex);
805 HeapFree(GetProcessHeap(),0,sTemp);
806 return ret;
809 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
810 { if( SHELL_OsIsUnicode())
811 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
812 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
815 /*************************************************************************
816 * ExtractIconEx [shell32.189]
818 HICON WINAPI ExtractIconExAW ( LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
819 { if (SHELL_OsIsUnicode())
820 return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
821 return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
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)
830 HICON WINAPI ExtractIconExA ( LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
831 { HICON ret=0;
833 TRACE("file=%s idx=%i %p %p num=%i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons );
835 if (nIconIndex==-1) /* Number of icons requested */
836 return ICO_ExtractIconEx(lpszFile, NULL, -1, 0, 0, 0 );
839 if (phiconLarge)
840 { ret = ICO_ExtractIconEx(lpszFile, phiconLarge, nIconIndex, nIcons, 32, 32 );
841 if ( nIcons==1)
842 { ret = phiconLarge[0];
846 /* if no pointers given and one icon expected, return the handle directly*/
847 if (!phiconLarge && ! phiconSmall && nIcons==1 )
848 phiconSmall = &ret;
850 if (phiconSmall)
851 { ret = ICO_ExtractIconEx(lpszFile, phiconSmall, nIconIndex, nIcons, 16, 16 );
852 if ( nIcons==1 )
853 { ret = phiconSmall[0];
857 return ret;
859 /*************************************************************************
860 * ExtractIconExW [shell32.191]
862 HICON WINAPI ExtractIconExW ( LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons )
863 { LPSTR sFile;
864 DWORD ret;
866 TRACE("file=%s idx=%i %p %p num=%i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons );
868 sFile = HEAP_strdupWtoA (GetProcessHeap(),0,lpszFile);
869 ret = ExtractIconExA ( sFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
870 HeapFree(GetProcessHeap(),0,sFile);
871 return ret;