4 * taken and slightly changed from shell
5 * this should replace the icon extraction code in shell32 and shell16 once
6 * it needs a serious test for compliance with the native API
8 * Copyright 2000 Juergen Schmied
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <stdlib.h> /* abs() */
29 #include <sys/types.h>
39 #include "wine/winbase16.h"
40 #include "cursoricon.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(icon);
49 BYTE bWidth; /* Width, in pixels, of the image */
50 BYTE bHeight; /* Height, in pixels, of the image */
51 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
52 BYTE bReserved; /* Reserved ( must be 0) */
53 WORD wPlanes; /* Color Planes */
54 WORD wBitCount; /* Bits per pixel */
55 DWORD dwBytesInRes; /* How many bytes in this resource? */
56 DWORD dwImageOffset; /* Where in the file is this image? */
57 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
61 WORD idReserved; /* Reserved (must be 0) */
62 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
63 WORD idCount; /* How many images */
64 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
65 } icoICONDIR, *LPicoICONDIR;
70 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
72 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
73 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
74 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
75 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
77 static void dumpIcoDir ( LPicoICONDIR entry )
79 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
83 /**********************************************************************
86 * Find an entry by id in a resource directory
87 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
89 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
90 WORD id, const void *root )
92 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
95 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
96 min = dir->NumberOfNamedEntries;
97 max = min + dir->NumberOfIdEntries - 1;
100 pos = (min + max) / 2;
101 if (entry[pos].u1.s2.Id == id)
102 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
103 if (entry[pos].u1.s2.Id > id) max = pos - 1;
109 /**********************************************************************
112 * Find a default entry in a resource directory
113 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
115 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
118 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
119 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
120 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s3.OffsetToDirectory);
123 /*************************************************************************
124 * USER32_GetResourceTable
126 static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
128 IMAGE_DOS_HEADER * mz_header;
130 TRACE("%p %p\n", peimage, retptr);
134 mz_header = (IMAGE_DOS_HEADER*) peimage;
136 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
138 if (mz_header->e_cblp == 1) /* .ICO file ? */
140 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
144 return 0; /* failed */
146 if (mz_header->e_lfanew >= pesize) {
147 return 0; /* failed, happens with PKZIP DOS Exes for instance. */
149 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
150 return IMAGE_NT_SIGNATURE;
152 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
154 IMAGE_OS2_HEADER * ne_header;
156 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
158 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
161 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
162 *retptr = (LPBYTE)-1;
164 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
166 return IMAGE_OS2_SIGNATURE;
168 return 0; /* failed */
170 /*************************************************************************
171 * USER32_LoadResource
173 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
175 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
177 *uSize = (DWORD)pNInfo->length << sizeShift;
178 return peimage + ((DWORD)pNInfo->offset << sizeShift);
181 /*************************************************************************
184 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
186 TRACE("%p %p\n", peimage, lpiIDE);
188 *uSize = lpiIDE->dwBytesInRes;
189 return peimage + lpiIDE->dwImageOffset;
192 /*************************************************************************
193 * ICO_GetIconDirectory
195 * Reads .ico file and build phony ICONDIR struct
196 * see http://www.microsoft.com/win32dev/ui/icons.htm
198 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
199 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
201 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
203 CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
204 CURSORICONDIR * lpID; /* icon resource in resource format */
207 TRACE("%p %p\n", peimage, lplpiID);
209 lpcid = (CURSORICONDIR*)peimage;
211 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
214 /* allocate the phony ICONDIR structure */
215 *uSize = lpcid->idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
216 if( (lpID = (CURSORICONDIR*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
218 /* copy the header */
219 lpID->idReserved = lpcid->idReserved;
220 lpID->idType = lpcid->idType;
221 lpID->idCount = lpcid->idCount;
223 /* copy the entries */
224 for( i=0; i < lpcid->idCount; i++ )
226 memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpcid->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
227 lpID->idEntries[i].wResId = i;
230 *lplpiID = (LPicoICONDIR)peimage;
236 /*************************************************************************
237 * ICO_ExtractIconExW [internal]
240 * nIcons = 0: returns number of Icons in file
243 * failure:0; success: icon handle or nr of icons (nIconIndex-1)
245 static HRESULT ICO_ExtractIconExW(
246 LPCWSTR lpszExeFileName,
253 HGLOBAL hRet = E_FAIL;
257 UINT16 iconDirCount = 0,iconCount = 0;
263 TRACE("(file %s,start %d,extract %d\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons);
265 hFile = CreateFileW( lpszExeFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
266 if (hFile == INVALID_HANDLE_VALUE) return hRet;
267 fsizel = GetFileSize(hFile,&fsizeh);
270 fmapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL );
271 CloseHandle( hFile );
274 WARN("CreateFileMapping error %ld\n", GetLastError() );
278 if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0)))
280 WARN("MapViewOfFile error %ld\n", GetLastError() );
281 CloseHandle( fmapping );
284 CloseHandle( fmapping );
286 sig = USER32_GetResourceTable(peimage,fsizel,&pData);
289 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
292 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
293 NE_NAMEINFO *pIconStorage = NULL;
294 NE_NAMEINFO *pIconDir = NULL;
295 LPicoICONDIR lpiID = NULL;
297 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
299 if( pData == (BYTE*)-1 )
301 /* FIXME: pCIDir is allocated on the heap - memory leak */
302 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
305 iconDirCount = 1; iconCount = lpiID->idCount;
306 TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
309 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
311 if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
313 iconDirCount = pTInfo->count;
314 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
315 TRACE("\tfound directory - %i icon families\n", iconDirCount);
317 if( pTInfo->type_id == NE_RSCTYPE_ICON )
319 iconCount = pTInfo->count;
320 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
321 TRACE("\ttotal icons - %i\n", iconCount);
323 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
326 if( (pIconStorage && pIconDir) || lpiID ) /* load resources and create icons */
332 else if( nIconIndex < iconDirCount )
335 if( nIcons > iconDirCount - nIconIndex )
336 nIcons = iconDirCount - nIconIndex;
338 for( i = nIconIndex; i < nIconIndex + nIcons; i++ )
340 /* .ICO files have only one icon directory */
341 if( lpiID == NULL ) /* *.ico */
342 pCIDir = USER32_LoadResource( peimage, pIconDir + i, *(WORD*)pData, &uSize );
343 RetPtr[i-nIconIndex] = (HICON)LookupIconIdFromDirectoryEx( pCIDir, TRUE, cxDesired, cyDesired, 0);
346 for( icon = nIconIndex; icon < nIconIndex + nIcons; icon++ )
350 pCIDir = ICO_LoadIcon( peimage, lpiID->idEntries + (int)RetPtr[icon-nIconIndex], &uSize);
352 for( i = 0; i < iconCount; i++ )
353 if( pIconStorage[i].id == ((int)RetPtr[icon-nIconIndex] | 0x8000) )
354 pCIDir = USER32_LoadResource( peimage, pIconStorage + i,*(WORD*)pData, &uSize );
357 RetPtr[icon-nIconIndex] = (HICON) CreateIconFromResourceEx(pCIDir,uSize,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
359 RetPtr[icon-nIconIndex] = 0;
368 else if( sig == IMAGE_NT_SIGNATURE )
371 PIMAGE_DOS_HEADER dheader;
372 PIMAGE_NT_HEADERS pe_header;
373 PIMAGE_SECTION_HEADER pe_sections;
374 const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
375 const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
376 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
379 dheader = (PIMAGE_DOS_HEADER)peimage;
380 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, USER32_GetResourceTable checked that */
381 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header)); /* probably makes problems with short PE headers...*/
384 /* search for the root resource directory */
385 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
387 if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
389 if (fsizel < pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData) {
390 FIXME("File %s too short (section is at %ld bytes, real size is %ld)\n",
391 debugstr_w(lpszExeFileName),
392 pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData,
397 /* FIXME: doesn't work when the resources are not in a separate section */
398 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
400 rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
407 WARN("haven't found section for resource directory.\n");
408 goto end; /* failure */
411 /* search for the group icon directory */
412 if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICONW), rootresdir)))
414 WARN("No Icongroupresourcedirectory!\n");
415 goto end; /* failure */
417 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
419 /* only number of icons requested */
423 goto end; /* success */
428 /* search resource id */
430 int iId = abs(nIconIndex);
431 PIMAGE_RESOURCE_DIRECTORY_ENTRY xprdeTmp = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
433 while(n<iconDirCount && xprdeTmp)
435 if(xprdeTmp->u1.s2.Id == iId)
445 WARN("resource id %d not found\n", iId);
446 goto end; /* failure */
451 /* check nIconIndex to be in range */
452 if (nIconIndex >= iconDirCount)
454 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
455 goto end; /* failure */
459 /* assure we don't get too much */
460 if( nIcons > iconDirCount - nIconIndex )
461 nIcons = iconDirCount - nIconIndex;
463 /* starting from specified index */
464 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1) + nIconIndex;
466 for (i=0; i < nIcons; i++,xresent++)
468 const IMAGE_RESOURCE_DIRECTORY *resdir;
470 /* go down this resource entry, name */
471 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s3.OffsetToDirectory));
473 /* default language (0) */
474 resdir = find_entry_default(resdir,rootresdir);
475 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
477 /* lookup address in mapped image for virtual address */
480 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
482 if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
484 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
487 if (igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size > fsizel) {
488 FIXME("overflow in PE lookup (%s has len %ld, have offset %ld), short file?\n",debugstr_w(lpszExeFileName),fsizel,igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size);
489 goto end; /* failure */
491 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
496 FIXME("no matching real address for icongroup!\n");
497 goto end; /* failure */
499 RetPtr[i] = (HICON)LookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
502 if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICONW),rootresdir)))
504 WARN("No Iconresourcedirectory!\n");
505 goto end; /* failure */
508 for (i=0; i<nIcons; i++)
510 const IMAGE_RESOURCE_DIRECTORY *xresdir;
511 xresdir = find_entry_by_id(iconresdir,LOWORD(RetPtr[i]),rootresdir);
512 xresdir = find_entry_default(xresdir,rootresdir);
513 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
516 /* map virtual to address in image */
517 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
519 if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
521 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
523 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
527 WARN("no matching real address found for icondata!\n");
531 RetPtr[i] = (HICON) CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
533 hRet = S_OK; /* return first icon */
534 } /* if(sig == IMAGE_NT_SIGNATURE) */
536 end: UnmapViewOfFile(peimage); /* success */
540 /***********************************************************************
541 * PrivateExtractIconsW [USER32.@]
544 * nIndex = 1: a small and a large icon are extracted.
545 * the higher word of sizeXY contains the size of the small icon, the lower
546 * word the size of the big icon. phicon points to HICON[2].
549 * nIcons > 0: HRESULT
550 * nIcons = 0: the number of icons
553 HRESULT WINAPI PrivateExtractIconsW (
558 HICON * phicon, /* [???] NOTE: HICON* */
559 DWORD w, /* [in] NOTE: 0 */
561 DWORD y ) /* [in] NOTE: 0x80 maybe LR_* constant */
564 TRACE("%s 0x%08x 0x%08lx 0x%08lx %p 0x%08lx 0x%08x 0x%08lx\n",
565 debugstr_w(lpwstrFile),nIndex, sizeX ,sizeY ,phicon,w,nIcons,y );
567 if ((nIcons == 2) && HIWORD(sizeX) && HIWORD(sizeY))
569 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, 1, sizeX & 0xffff, sizeY & 0xffff );
570 if (!SUCCEEDED(ret)) return ret;
571 ret = ICO_ExtractIconExW(lpwstrFile, phicon+1, nIndex, 1, (sizeX>>16) & 0xffff, (sizeY>>16) & 0xffff );
573 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX & 0xffff, sizeY & 0xffff );
577 /***********************************************************************
578 * PrivateExtractIconsA [USER32.@]
581 HRESULT WINAPI PrivateExtractIconsA (
587 DWORD w, /* [in] NOTE: 0 */
589 DWORD y ) /* [in] NOTE: 0x80 */
592 INT len = MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, NULL, 0 );
593 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
595 MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, lpwstrFile, len );
596 ret = PrivateExtractIconsW(
597 lpwstrFile, nIndex, sizeX, sizeY, phicon, w, nIcons, y
600 HeapFree(GetProcessHeap(), 0, lpwstrFile);
604 /***********************************************************************
605 * PrivateExtractIconExW [USER32.@]
607 * if nIcons = -1 it returns the number of icons in any case !!!
609 HRESULT WINAPI PrivateExtractIconExW (
616 DWORD cyicon, cysmicon, cxicon, cxsmicon;
619 TRACE("%s 0x%08lx %p %p 0x%08x\n",
620 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
622 if (nIndex == 1 && phIconSmall && phIconLarge)
625 cxicon = GetSystemMetrics(SM_CXICON);
626 cyicon = GetSystemMetrics(SM_CYICON);
627 cxsmicon = GetSystemMetrics(SM_CXSMICON);
628 cysmicon = GetSystemMetrics(SM_CYSMICON);
630 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon | (cxsmicon<<16), cyicon | (cysmicon<<16),
631 (HICON*) &hIcon, 0, 2, 0 );
632 *phIconLarge = hIcon[0];
633 *phIconSmall = hIcon[1];
641 /* extract n small icons */
642 cxsmicon = GetSystemMetrics(SM_CXSMICON);
643 cysmicon = GetSystemMetrics(SM_CYSMICON);
644 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxsmicon, cysmicon, phIconSmall, 0, nIcons, 0 );
648 /* extract n large icons */
649 cxicon = GetSystemMetrics(SM_CXICON);
650 cyicon = GetSystemMetrics(SM_CYICON);
651 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon, cyicon, phIconLarge, 0, nIcons, 0 );
656 /* get the number of icons */
657 return PrivateExtractIconsW ( lpwstrFile, 0, 0, 0, 0, 0, 0, 0 );
660 /***********************************************************************
661 * PrivateExtractIconExA [USER32.@]
663 HRESULT WINAPI PrivateExtractIconExA (
671 INT len = MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, NULL, 0 );
672 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
674 TRACE("%s 0x%08lx %p %p 0x%08x\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
676 MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, lpwstrFile, len );
677 ret = PrivateExtractIconExW(lpwstrFile,nIndex,phIconLarge, phIconSmall, nIcons);
678 HeapFree(GetProcessHeap(), 0, lpwstrFile);