Fix the bounds checking in SHGetPathFromIDListA/W.
[wine] / dlls / shell32 / iconcache.c
1 /*
2  *      shell icon cache (SIC)
3  *
4  * Copyright 1998, 1999 Juergen Schmied
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 "config.h"
22 #include "wine/port.h"
23
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"
39
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"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(shell);
49
50 /********************** THE ICON CACHE ********************************/
51
52 #define INVALID_INDEX -1
53
54 typedef struct
55 {
56         LPSTR 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;
62
63 static HDPA             sic_hdpa = 0;
64
65 static CRITICAL_SECTION SHELL32_SicCS;
66 static CRITICAL_SECTION_DEBUG critsect_debug =
67 {
68     0, 0, &SHELL32_SicCS,
69     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
70       0, 0, { 0, (DWORD)(__FILE__ ": SHELL32_SicCS") }
71 };
72 static CRITICAL_SECTION SHELL32_SicCS = { &critsect_debug, -1, 0, 0, 0, 0 };
73
74 /*****************************************************************************
75  * SIC_CompareEntries
76  *
77  * NOTES
78  *  Callback for DPA_Search
79  */
80 static INT CALLBACK SIC_CompareEntries( LPVOID p1, LPVOID p2, LPARAM lparam)
81 {       TRACE("%p %p %8lx\n", p1, p2, lparam);
82
83         if (((LPSIC_ENTRY)p1)->dwSourceIndex != ((LPSIC_ENTRY)p2)->dwSourceIndex) /* first the faster one*/
84           return 1;
85
86         if (strcasecmp(((LPSIC_ENTRY)p1)->sSourceFile,((LPSIC_ENTRY)p2)->sSourceFile))
87           return 1;
88
89         return 0;
90 }
91 /*****************************************************************************
92  * SIC_IconAppend                       [internal]
93  *
94  * NOTES
95  *  appends an icon pair to the end of the cache
96  */
97 static INT SIC_IconAppend (LPCSTR sSourceFile, INT dwSourceIndex, HICON hSmallIcon, HICON hBigIcon)
98 {       LPSIC_ENTRY lpsice;
99         INT ret, index, index1;
100        char path[MAX_PATH];
101         TRACE("%s %i %p %p\n", sSourceFile, dwSourceIndex, hSmallIcon ,hBigIcon);
102
103         lpsice = (LPSIC_ENTRY) SHAlloc (sizeof (SIC_ENTRY));
104
105        GetFullPathNameA(sSourceFile, MAX_PATH, path, NULL);
106         lpsice->sSourceFile = HeapAlloc( GetProcessHeap(), 0, strlen(path)+1 );
107         strcpy( lpsice->sSourceFile, path );
108
109         lpsice->dwSourceIndex = dwSourceIndex;
110
111         EnterCriticalSection(&SHELL32_SicCS);
112
113         index = DPA_InsertPtr(sic_hdpa, 0x7fff, lpsice);
114         if ( INVALID_INDEX == index )
115         {
116           HeapFree(GetProcessHeap(), 0, lpsice->sSourceFile);
117           SHFree(lpsice);
118           ret = INVALID_INDEX;
119         }
120         else
121         {
122           index = ImageList_AddIcon (ShellSmallIconList, hSmallIcon);
123           index1= ImageList_AddIcon (ShellBigIconList, hBigIcon);
124
125           if (index!=index1)
126           {
127             FIXME("iconlists out of sync 0x%x 0x%x\n", index, index1);
128           }
129           lpsice->dwListIndex = index;
130           ret = lpsice->dwListIndex;
131         }
132
133         LeaveCriticalSection(&SHELL32_SicCS);
134         return ret;
135 }
136 /****************************************************************************
137  * SIC_LoadIcon                         [internal]
138  *
139  * NOTES
140  *  gets small/big icon by number from a file
141  */
142 static INT SIC_LoadIcon (LPCSTR sSourceFile, INT dwSourceIndex)
143 {       HICON   hiconLarge=0;
144         HICON   hiconSmall=0;
145
146         PrivateExtractIconsA( sSourceFile, dwSourceIndex, 32, 32, &hiconLarge, 0, 1, 0 );
147         PrivateExtractIconsA( sSourceFile, dwSourceIndex, 16, 16, &hiconSmall, 0, 1, 0 );
148
149         if ( !hiconLarge ||  !hiconSmall)
150         {
151           WARN("failure loading icon %i from %s (%p %p)\n", dwSourceIndex, sSourceFile, hiconLarge, hiconSmall);
152           return -1;
153         }
154         return SIC_IconAppend (sSourceFile, dwSourceIndex, hiconSmall, hiconLarge);
155 }
156 /*****************************************************************************
157  * SIC_GetIconIndex                     [internal]
158  *
159  * Parameters
160  *      sSourceFile     [IN]    filename of file containing the icon
161  *      index           [IN]    index/resID (negated) in this file
162  *
163  * NOTES
164  *  look in the cache for a proper icon. if not available the icon is taken
165  *  from the file and cached
166  */
167 INT SIC_GetIconIndex (LPCSTR sSourceFile, INT dwSourceIndex )
168 {       SIC_ENTRY sice;
169         INT ret, index = INVALID_INDEX;
170        char path[MAX_PATH];
171
172         TRACE("%s %i\n", sSourceFile, dwSourceIndex);
173
174        GetFullPathNameA(sSourceFile, MAX_PATH, path, NULL);
175        sice.sSourceFile = path;
176         sice.dwSourceIndex = dwSourceIndex;
177
178         EnterCriticalSection(&SHELL32_SicCS);
179
180         if (NULL != DPA_GetPtr (sic_hdpa, 0))
181         {
182           /* search linear from position 0*/
183           index = DPA_Search (sic_hdpa, &sice, 0, SIC_CompareEntries, 0, 0);
184         }
185
186         if ( INVALID_INDEX == index )
187         {
188           ret = SIC_LoadIcon (sSourceFile, dwSourceIndex);
189         }
190         else
191         {
192           TRACE("-- found\n");
193           ret = ((LPSIC_ENTRY)DPA_GetPtr(sic_hdpa, index))->dwListIndex;
194         }
195
196         LeaveCriticalSection(&SHELL32_SicCS);
197         return ret;
198 }
199 /****************************************************************************
200  * SIC_GetIcon                          [internal]
201  *
202  * NOTES
203  *  retrieves the specified icon from the iconcache. if not found tries to load the icon
204  */
205 static HICON WINE_UNUSED SIC_GetIcon (LPCSTR sSourceFile, INT dwSourceIndex, BOOL bSmallIcon )
206 {       INT index;
207
208         TRACE("%s %i\n", sSourceFile, dwSourceIndex);
209
210         index = SIC_GetIconIndex(sSourceFile, dwSourceIndex);
211
212         if (INVALID_INDEX == index)
213         {
214           return (HICON)INVALID_INDEX;
215         }
216
217         if (bSmallIcon)
218           return ImageList_GetIcon(ShellSmallIconList, index, ILD_NORMAL);
219         return ImageList_GetIcon(ShellBigIconList, index, ILD_NORMAL);
220
221 }
222 /*****************************************************************************
223  * SIC_Initialize                       [internal]
224  *
225  * NOTES
226  *  hack to load the resources from the shell32.dll under a different dll name
227  *  will be removed when the resource-compiler is ready
228  */
229 BOOL SIC_Initialize(void)
230 {
231         HICON           hSm, hLg;
232         UINT            index;
233
234         TRACE("\n");
235
236         if (sic_hdpa)   /* already initialized?*/
237           return TRUE;
238
239         sic_hdpa = DPA_Create(16);
240
241         if (!sic_hdpa)
242         {
243           return(FALSE);
244         }
245
246        ShellSmallIconList = ImageList_Create(16,16,ILC_COLOR32|ILC_MASK,0,0x20);
247        ShellBigIconList = ImageList_Create(32,32,ILC_COLOR32|ILC_MASK,0,0x20);
248
249        ImageList_SetBkColor(ShellSmallIconList, CLR_NONE);
250        ImageList_SetBkColor(ShellBigIconList, CLR_NONE);
251
252         for (index=1; index<39; index++)
253         {
254           hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 16, 16,LR_SHARED);
255           hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(index), IMAGE_ICON, 32, 32,LR_SHARED);
256
257           if(!hSm)
258           {
259             hSm = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 16, 16,LR_SHARED);
260             hLg = LoadImageA(shell32_hInstance, MAKEINTRESOURCEA(0), IMAGE_ICON, 32, 32,LR_SHARED);
261           }
262          SIC_IconAppend (sShell32Name, index, hSm, hLg);
263         }
264
265         TRACE("hIconSmall=%p hIconBig=%p\n",ShellSmallIconList, ShellBigIconList);
266
267         return TRUE;
268 }
269 /*************************************************************************
270  * SIC_Destroy
271  *
272  * frees the cache
273  */
274 static INT CALLBACK sic_free( LPVOID ptr, LPVOID lparam )
275 {
276         HeapFree(GetProcessHeap(), 0, ((LPSIC_ENTRY)ptr)->sSourceFile);
277         SHFree(ptr);
278         return TRUE;
279 }
280
281 void SIC_Destroy(void)
282 {
283         TRACE("\n");
284
285         EnterCriticalSection(&SHELL32_SicCS);
286
287         if (sic_hdpa) DPA_DestroyCallback(sic_hdpa, sic_free, NULL );
288
289         sic_hdpa = NULL;
290
291         LeaveCriticalSection(&SHELL32_SicCS);
292         DeleteCriticalSection(&SHELL32_SicCS);
293 }
294 /*************************************************************************
295  * Shell_GetImageList                   [SHELL32.71]
296  *
297  * PARAMETERS
298  *  imglist[1|2] [OUT] pointer which recive imagelist handles
299  *
300  */
301 BOOL WINAPI Shell_GetImageList(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList)
302 {       TRACE("(%p,%p)\n",lpBigList,lpSmallList);
303         if (lpBigList)
304         { *lpBigList = ShellBigIconList;
305         }
306         if (lpSmallList)
307         { *lpSmallList = ShellSmallIconList;
308         }
309
310         return TRUE;
311 }
312 /*************************************************************************
313  * PidlToSicIndex                       [INTERNAL]
314  *
315  * PARAMETERS
316  *      sh      [IN]    IShellFolder
317  *      pidl    [IN]
318  *      bBigIcon [IN]
319  *      uFlags  [IN]    GIL_*
320  *      pIndex  [OUT]   index within the SIC
321  *
322  */
323 BOOL PidlToSicIndex (
324         IShellFolder * sh,
325         LPCITEMIDLIST pidl,
326         BOOL bBigIcon,
327         UINT uFlags,
328         UINT * pIndex)
329 {
330         IExtractIconA   *ei;
331         char            szIconFile[MAX_PATH];   /* file containing the icon */
332         INT             iSourceIndex;           /* index or resID(negated) in this file */
333         BOOL            ret = FALSE;
334         UINT            dwFlags = 0;
335
336         TRACE("sf=%p pidl=%p %s\n", sh, pidl, bBigIcon?"Big":"Small");
337
338         if (SUCCEEDED (IShellFolder_GetUIObjectOf(sh, 0, 1, &pidl, &IID_IExtractIconA, 0, (void **)&ei)))
339         {
340           if (SUCCEEDED(IExtractIconA_GetIconLocation(ei, uFlags, szIconFile, MAX_PATH, &iSourceIndex, &dwFlags)))
341           {
342             *pIndex = SIC_GetIconIndex(szIconFile, iSourceIndex);
343             ret = TRUE;
344           }
345           IExtractIconA_Release(ei);
346         }
347
348         if (INVALID_INDEX == *pIndex)   /* default icon when failed */
349           *pIndex = 1;
350
351         return ret;
352
353 }
354
355 /*************************************************************************
356  * SHMapPIDLToSystemImageListIndex      [SHELL32.77]
357  *
358  * PARAMETERS
359  *      sh      [IN]            pointer to an instance of IShellFolder
360  *      pidl    [IN]
361  *      pIndex  [OUT][OPTIONAL] SIC index for big icon
362  *
363  */
364 int WINAPI SHMapPIDLToSystemImageListIndex(
365         LPSHELLFOLDER sh,
366         LPCITEMIDLIST pidl,
367         UINT * pIndex)
368 {
369         UINT    Index;
370
371         TRACE("(SF=%p,pidl=%p,%p)\n",sh,pidl,pIndex);
372         pdump(pidl);
373
374         if (pIndex)
375           PidlToSicIndex ( sh, pidl, 1, 0, pIndex);
376         PidlToSicIndex ( sh, pidl, 0, 0, &Index);
377         return Index;
378 }
379
380 /*************************************************************************
381  * Shell_GetCachedImageIndex            [SHELL32.72]
382  *
383  */
384 INT WINAPI Shell_GetCachedImageIndexA(LPCSTR szPath, INT nIndex, BOOL bSimulateDoc)
385 {
386         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_a(szPath), nIndex, bSimulateDoc);
387         return SIC_GetIconIndex(szPath, nIndex);
388 }
389
390 INT WINAPI Shell_GetCachedImageIndexW(LPCWSTR szPath, INT nIndex, BOOL bSimulateDoc)
391 {       INT ret;
392         LPSTR sTemp = HEAP_strdupWtoA (GetProcessHeap(),0,szPath);
393
394         WARN("(%s,%08x,%08x) semi-stub.\n",debugstr_w(szPath), nIndex, bSimulateDoc);
395
396         ret = SIC_GetIconIndex(sTemp, nIndex);
397         HeapFree(GetProcessHeap(),0,sTemp);
398         return ret;
399 }
400
401 INT WINAPI Shell_GetCachedImageIndexAW(LPCVOID szPath, INT nIndex, BOOL bSimulateDoc)
402 {       if( SHELL_OsIsUnicode())
403           return Shell_GetCachedImageIndexW(szPath, nIndex, bSimulateDoc);
404         return Shell_GetCachedImageIndexA(szPath, nIndex, bSimulateDoc);
405 }
406
407 /*************************************************************************
408  * ExtractIconEx                        [SHELL32.@]
409  */
410 UINT WINAPI ExtractIconExAW(LPCVOID lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
411 {       if (SHELL_OsIsUnicode())
412           return ExtractIconExW ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
413         return ExtractIconExA ( lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
414 }
415
416 /*************************************************************************
417  * ExtractIconExW                       [SHELL32.@]
418  * RETURNS: 
419  *  0 no icon found
420  *  -1 file is not valid
421  *  or number of icons extracted
422  */
423 UINT WINAPI ExtractIconExW(LPCWSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
424 {
425         TRACE("%s %i %p %p %i\n", debugstr_w(lpszFile), nIconIndex, phiconLarge, phiconSmall, nIcons);
426
427         return PrivateExtractIconExW(lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
428 }
429
430 /*************************************************************************
431  * ExtractIconExA                       [SHELL32.@]
432  */
433 UINT WINAPI ExtractIconExA(LPCSTR lpszFile, INT nIconIndex, HICON * phiconLarge, HICON * phiconSmall, UINT nIcons)
434 {
435     UINT ret;
436     INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
437     LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
438
439     TRACE("%s %i %p %p %i\n", lpszFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
440
441     MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
442     ret = ExtractIconExW (lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIcons);
443     HeapFree(GetProcessHeap(), 0, lpwstrFile);
444     return ret;
445 }
446
447 /*************************************************************************
448  *                              ExtractAssociatedIconA (SHELL32.@)
449  *
450  * Return icon for given file (either from file itself or from associated
451  * executable) and patch parameters if needed.
452  */
453 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
454 {       
455         HICON hIcon;
456         WORD wDummyIcon = 0;
457         
458         TRACE("\n");
459
460         if(lpiIcon == NULL)
461             lpiIcon = &wDummyIcon;
462
463         hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
464
465         if( hIcon < (HICON)2 )
466         { if( hIcon == (HICON)1 ) /* no icons found in given file */
467           { char  tempPath[0x80];
468             HINSTANCE uRet = FindExecutableA(lpIconPath,NULL,tempPath);
469
470             if( uRet > (HINSTANCE)32 && tempPath[0] )
471             { strcpy(lpIconPath,tempPath);
472               hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
473               if( hIcon > (HICON)2 )
474                 return hIcon;
475             }
476             else hIcon = 0;
477           }
478
479           if( hIcon == (HICON)1 )
480             *lpiIcon = 2;   /* MSDOS icon - we found .exe but no icons in it */
481           else
482             *lpiIcon = 6;   /* generic icon - found nothing */
483
484           GetModuleFileNameA(hInst, lpIconPath, 0x80);
485           hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
486         }
487         return hIcon;
488 }
489
490 /*************************************************************************
491  *                              ExtractAssociatedIconExW (SHELL32.@)
492  *
493  * Return icon for given file (either from file itself or from associated
494  * executable) and patch parameters if needed.
495  */
496 HICON WINAPI ExtractAssociatedIconExW(HINSTANCE hInst, LPWSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
497 {
498   FIXME("%p %s %p %p): stub\n", hInst, debugstr_w(lpIconPath), lpiIconIdx, lpiIconId);
499   return 0;
500 }
501
502 /*************************************************************************
503  *                              ExtractAssociatedIconExA (SHELL32.@)
504  *
505  * Return icon for given file (either from file itself or from associated
506  * executable) and patch parameters if needed.
507  */
508 HICON WINAPI ExtractAssociatedIconExA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIconIdx, LPWORD lpiIconId)
509 {
510   HICON ret;
511   INT len = MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, NULL, 0 );
512   LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
513
514   TRACE("%p %s %p %p)\n", hInst, lpIconPath, lpiIconIdx, lpiIconId);
515
516   MultiByteToWideChar( CP_ACP, 0, lpIconPath, -1, lpwstrFile, len );
517   ret = ExtractAssociatedIconExW(hInst, lpwstrFile, lpiIconIdx, lpiIconId);
518   HeapFree(GetProcessHeap(), 0, lpwstrFile);
519   return ret;
520 }
521
522
523 /****************************************************************************
524  * SHDefExtractIconW            [SHELL32.@]
525  */
526 HRESULT WINAPI SHDefExtractIconW(LPCWSTR pszIconFile, int iIndex, UINT uFlags,
527                                  HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
528 {
529         UINT ret;
530         HICON hIcons[2];
531         WARN("%s %d 0x%08x %p %p %d, semi-stub\n", debugstr_w(pszIconFile), iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
532
533         ret = PrivateExtractIconsW(pszIconFile, iIndex, nIconSize, nIconSize, hIcons, NULL, 2, LR_DEFAULTCOLOR);
534         /* FIXME: deal with uFlags parameter which contains GIL_ flags */
535         if (ret == 0xFFFFFFFF)
536           return E_FAIL;
537         if (ret > 0) {
538           *phiconLarge = hIcons[0];
539           *phiconSmall = hIcons[1];
540           return S_OK;
541         }
542         return S_FALSE;
543 }
544
545 /****************************************************************************
546  * SHDefExtractIconA            [SHELL32.@]
547  */
548 HRESULT WINAPI SHDefExtractIconA(LPCSTR pszIconFile, int iIndex, UINT uFlags,
549                                  HICON* phiconLarge, HICON* phiconSmall, UINT nIconSize)
550 {
551   HRESULT ret;
552   INT len = MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, NULL, 0);
553   LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
554
555   TRACE("%s %d 0x%08x %p %p %d\n", pszIconFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
556
557   MultiByteToWideChar(CP_ACP, 0, pszIconFile, -1, lpwstrFile, len);
558   ret = SHDefExtractIconW(lpwstrFile, iIndex, uFlags, phiconLarge, phiconSmall, nIconSize);
559   HeapFree(GetProcessHeap(), 0, lpwstrFile);
560   return ret;
561 }