Fixed buffer overflow.
[wine/multimedia.git] / dlls / shell32 / iconcache.c
blobc5c7709329812b6094d2a5b49615ecdb74d7cc7e
1 /*
2 * shell icon cache (SIC)
4 * Copyright 1998, 1999 Juergen Schmied
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winreg.h"
35 #include "wine/winuser16.h"
36 #include "wine/winbase16.h"
37 #include "heap.h"
38 #include "wine/debug.h"
40 #include "shellapi.h"
41 #include "objbase.h"
42 #include "shlguid.h"
43 #include "pidl.h"
44 #include "shell32_main.h"
45 #include "undocshell.h"
46 #include "shlwapi.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(shell);
50 /********************** THE ICON CACHE ********************************/
52 #define INVALID_INDEX -1
54 typedef struct
56 LPWSTR sSourceFile; /* file (not path!) containing the icon */
57 DWORD dwSourceIndex; /* index within the file, if it is a resoure ID it will be negated */
58 DWORD dwListIndex; /* index within the iconlist */
59 DWORD dwFlags; /* GIL_* flags */
60 DWORD dwAccessTime;
61 } SIC_ENTRY, * LPSIC_ENTRY;
63 static HDPA sic_hdpa = 0;
65 static CRITICAL_SECTION SHELL32_SicCS;
66 static CRITICAL_SECTION_DEBUG critsect_debug =
68 0, 0, &SHELL32_SicCS,
69 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
70 0, 0, { 0, (DWORD)(__FILE__ ": SHELL32_SicCS") }
72 static CRITICAL_SECTION SHELL32_SicCS = { &critsect_debug, -1, 0, 0, 0, 0 };
74 /*****************************************************************************
75 * SIC_CompareEntries
77 * NOTES
78 * Callback for DPA_Search
80 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
81 { TRACE("%p %p %8lx\n", p1, p2, lparam);
83 if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
84 return 1;
86 if (strcmpiW(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
87 return 1;
89 return 0;
91 /*****************************************************************************
92 * SIC_IconAppend [internal]
94 * NOTES
95 * appends an icon pair to the end of the cache
97 static INT SIC_IconAppend (LPCWSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
98 { LPSIC_ENTRY lpsice;
99 INT ret, index, index1;
100 WCHAR path[MAX_PATH];
101 TRACE("%s %i %p %p\n", debugstr_w(sSourceFile), dwSourceIndex, hSmallIcon ,hBigIcon);
103 lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
105 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
106 lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, (strlenW(path)+1)*sizeof(WCHAR) );
107 strcpyW( lpsice->sSourceFile, path );
109 lpsice->dwSourceIndex = dwSourceIndex;
111 EnterCriticalSection(&SHELL32_SicCS);
113 index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
114 if ( INVALID_INDEX == index )
116 HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
117 SHFree(lpsice);
118 ret = INVALID_INDEX;
120 else
122 index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
123 index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
125 if (index!=index1)
127 FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
129 lpsice->dwListIndex = index;
130 ret = lpsice->dwListIndex;
133 LeaveCriticalSection(&SHELL32_SicCS);
134 return ret;
136 /****************************************************************************
137 * SIC_LoadIcon [internal]
139 * NOTES
140 * gets small/big icon by number from a file
142 static INT SIC_LoadIcon (LPCWSTR sSourceFile, INT dwSourceIndex)
143 { HICON hiconLarge=0;
144 HICON hiconSmall=0;
146 PrivateExtractIconsW( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
147 PrivateExtractIconsW( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
149 if ( !hiconLarge || !hiconSmall)
151 WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, debugstr_w(sSourceFile), hiconLarge, hiconSmall);
152 return -1;
154 return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
156 /*****************************************************************************
157 * SIC_GetIconIndex [internal]
159 * Parameters
160 * sSourceFile [IN] filename of file containing the icon
161 * index [IN] index/resID (negated) in this file
163 * NOTES
164 * look in the cache for a proper icon. if not available the icon is taken
165 * from the file and cached
167 INT SIC_GetIconIndex (LPCWSTR sSourceFile, INT dwSourceIndex )
169 SIC_ENTRY sice;
170 INT ret, index = INVALID_INDEX;
171 WCHAR path[MAX_PATH];
173 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
175 GetFullPathNameW(sSourceFile, MAX_PATH, path, NULL);
176 sice.sSourceFile = path;
177 sice.dwSourceIndex = dwSourceIndex;
179 EnterCriticalSection(&SHELL32_SicCS);
181 if (NULL != DPA_GetPtr (sic_hdpa, 0))
183 /* search linear from position 0*/
184 index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
187 if ( INVALID_INDEX == index )
189 ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
191 else
193 TRACE("-- found\n");
194 ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
197 LeaveCriticalSection(&SHELL32_SicCS);
198 return ret;
200 /****************************************************************************
201 * SIC_GetIcon [internal]
203 * NOTES
204 * retrieves the specified icon from the iconcache. if not found tries to load the icon
206 static HICON WINE_UNUSED SIC_GetIcon (LPCWSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
207 { INT index;
209 TRACE("%s %i\n", debugstr_w(sSourceFile), dwSourceIndex);
211 index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
213 if (INVALID_INDEX == index)
215 return (HICON)INVALID_INDEX;
218 if (bSmallIcon)
219 return ImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
220 return ImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
223 /*****************************************************************************
224 * SIC_Initialize [internal]
226 * NOTES
227 * hack to load the resources from the shell32.dll under a different dll name
228 * will be removed when the resource-compiler is ready
230 BOOL SIC_Initialize(void)
232 HICON hSm, hLg;
233 UINT index;
235 TRACE("\n");
237 if (sic_hdpa) /* already initialized?*/
238 return TRUE;
240 sic_hdpa = DPA_Create(16);
242 if (!sic_hdpa)
244 return(FALSE);
247 ShellSmallIconList = ImageList_Create(16,16,ILC_COLOR32|ILC_MASK,0,0x20);
248 ShellBigIconList = ImageList_Create(32,32,ILC_COLOR32|ILC_MASK,0,0x20);
250 ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
251 ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
253 for (index=1; index<39; index++)
255 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 16, 16,LR_SHARED);
256 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 32, 32,LR_SHARED);
258 if(!hSm)
260 hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 16, 16,LR_SHARED);
261 hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 32, 32,LR_SHARED);
263 SIC_IconAppend (swShell32Name, index, hSm, hLg);
266 TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
268 return TRUE;
270 /*************************************************************************
271 * SIC_Destroy
273 * frees the cache
275 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
277 HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
278 SHFree(ptr);
279 return TRUE;
282 void SIC_Destroy(void)
284 TRACE("\n");
286 EnterCriticalSection(&SHELL32_SicCS);
288 if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
290 sic_hdpa = NULL;
292 LeaveCriticalSection(&SHELL32_SicCS);
293 DeleteCriticalSection(&SHELL32_SicCS);
295 /*************************************************************************
296 * Shell_GetImageList [SHELL32.71]
298 * PARAMETERS
299 * imglist[1|2] [OUT] pointer which receives imagelist handles
302 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
303 { TRACE("(%p,%p)\n",lpBigList,lpSmallList);
304 if (lpBigList)
305 { *lpBigList = ShellBigIconList;
307 if (lpSmallList)
308 { *lpSmallList = ShellSmallIconList;
311 return TRUE;
313 /*************************************************************************
314 * PidlToSicIndex [INTERNAL]
316 * PARAMETERS
317 * sh [IN] IShellFolder
318 * pidl [IN]
319 * bBigIcon [IN]
320 * uFlags [IN] GIL_*
321 * pIndex [OUT] index within the SIC
324 BOOL PidlToSicIndex (
325 IShellFolder * sh,
326 LPCITEMIDLIST pidl,
327 BOOL bBigIcon,
328 UINT uFlags,
329 int * pIndex)
331 IExtractIconW *ei;
332 WCHAR szIconFile[MAX_PATH]; /* file containing the icon */
333 INT iSourceIndex; /* index or resID(negated) in this file */
334 BOOL ret = FALSE;
335 UINT dwFlags = 0;
337 TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
339 if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconW, 0, (void **)&ei)))
341 if (SUCCEEDED(IExtractIconW_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
343 *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
344 ret = TRUE;
346 IExtractIconW_Release(ei);
349 if (INVALID_INDEX == *pIndex) /* default icon when failed */
350 *pIndex = 1;
352 return ret;
356 /*************************************************************************
357 * SHMapPIDLToSystemImageListIndex [SHELL32.77]
359 * PARAMETERS
360 * sh [IN] pointer to an instance of IShellFolder
361 * pidl [IN]
362 * pIndex [OUT][OPTIONAL] SIC index for big icon
365 int WINAPI SHMapPIDLToSystemImageListIndex(
366 IShellFolder *sh,
367 LPCITEMIDLIST pidl,
368 int *pIndex)
370 int Index;
372 TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
373 pdump(pidl);
375 if (pIndex)
376 PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
377 PidlToSicIndex ( sh, pidl, 0, 0, &Index);
378 return Index;
381 /*************************************************************************
382 * Shell_GetCachedImageIndex [SHELL32.72]
385 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
387 INT ret, len;
388 LPWSTR szTemp;
390 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
392 len = MultiByteToWideChar( CP_ACP, 0, szPath, -1, NULL, 0 );
393 szTemp = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
394 MultiByteToWideChar( CP_ACP, 0, szPath, -1, szTemp, len );
396 ret = SIC_GetIconIndex( szTemp, nIndex );
398 HeapFree( GetProcessHeap(), 0, szTemp );
400 return ret;
403 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
405 WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
407 return SIC_GetIconIndex(szPath, nIndex);
410 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
411 { if( SHELL_OsIsUnicode())
412 return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
413 return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
416 /*************************************************************************
417 * ExtractIconEx [SHELL32.@]
419 UINT WINAPI ExtractIconExAW(LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
420 { if (SHELL_OsIsUnicode())
421 return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
422 return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
425 /*************************************************************************
426 * ExtractIconExW [SHELL32.@]
427 * RETURNS:
428 * 0 no icon found
429 * -1 file is not valid
430 * or number of icons extracted
432 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
434 TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
436 return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
439 /*************************************************************************
440 * ExtractIconExA [SHELL32.@]
442 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
444 UINT ret;
445 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
446 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
448 TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
450 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
451 ret = ExtractIconExW (lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
452 HeapFree(GetProcessHeap(), 0, lpwstrFile);
453 return ret;
456 /*************************************************************************
457 * ExtractAssociatedIconA (SHELL32.@)
459 * Return icon for given file (either from file itself or from associated
460 * executable) and patch parameters if needed.
462 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
464 HICON hIcon;
465 WORD wDummyIcon = 0;
467 TRACE("\n");
469 if(lpiIcon == NULL)
470 lpiIcon = &wDummyIcon;
472 hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
474 if( hIcon < (HICON)2 )
475 { if( hIcon == (HICON)1 ) /* no icons found in given file */
476 { char tempPath[0x80];
477 HINSTANCE uRet = FindExecutableA(lpIconPath,NULL,tempPath);
479 if( uRet > (HINSTANCE)32 && tempPath[0] )
480 { strcpy(lpIconPath,tempPath);
481 hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
482 if( hIcon > (HICON)2 )
483 return hIcon;
485 else hIcon = 0;
488 if( hIcon == (HICON)1 )
489 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
490 else
491 *lpiIcon = 6; /* generic icon - found nothing */
493 GetModuleFileNameA(hInst, lpIconPath, 0x80);
494 hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
496 return hIcon;
499 /*************************************************************************
500 * ExtractAssociatedIconExW (SHELL32.@)
502 * Return icon for given file (either from file itself or from associated
503 * executable) and patch parameters if needed.
505 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
507 FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
508 return 0;
511 /*************************************************************************
512 * ExtractAssociatedIconExA (SHELL32.@)
514 * Return icon for given file (either from file itself or from associated
515 * executable) and patch parameters if needed.
517 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
519 HICON ret;
520 INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
521 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
523 TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
525 MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
526 ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
527 HeapFree(GetProcessHeap(), 0, lpwstrFile);
528 return ret;
532 /****************************************************************************
533 * SHDefExtractIconW [SHELL32.@]
535 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
536 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
538 UINT ret;
539 HICON hIcons[2];
540 WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
542 ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
543 /* FIXME: deal with uFlags parameter which contains GIL_ flags */
544 if (ret == 0xFFFFFFFF)
545 return E_FAIL;
546 if (ret > 0) {
547 *phiconLarge = hIcons[0];
548 *phiconSmall = hIcons[1];
549 return S_OK;
551 return S_FALSE;
554 /****************************************************************************
555 * SHDefExtractIconA [SHELL32.@]
557 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
558 HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
560 HRESULT ret;
561 INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
562 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
564 TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
566 MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
567 ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
568 HeapFree(GetProcessHeap(), 0, lpwstrFile);
569 return ret;