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
12 #include <stdlib.h> /* abs() */
13 #include <sys/types.h>
21 #include "wine/winbase16.h"
22 #include "cursoricon.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(icon);
31 BYTE bWidth; /* Width, in pixels, of the image */
32 BYTE bHeight; /* Height, in pixels, of the image */
33 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
34 BYTE bReserved; /* Reserved ( must be 0) */
35 WORD wPlanes; /* Color Planes */
36 WORD wBitCount; /* Bits per pixel */
37 DWORD dwBytesInRes; /* How many bytes in this resource? */
38 DWORD dwImageOffset; /* Where in the file is this image? */
39 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
43 WORD idReserved; /* Reserved (must be 0) */
44 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
45 WORD idCount; /* How many images */
46 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
47 } icoICONDIR, *LPicoICONDIR;
52 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
54 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
55 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
56 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
57 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
59 static void dumpIcoDir ( LPicoICONDIR entry )
61 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
65 /**********************************************************************
68 * Find an entry by id in a resource directory
69 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
71 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
72 WORD id, const void *root )
74 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
77 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
78 min = dir->NumberOfNamedEntries;
79 max = min + dir->NumberOfIdEntries - 1;
82 pos = (min + max) / 2;
83 if (entry[pos].u1.s2.Id == id)
84 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
85 if (entry[pos].u1.s2.Id > id) max = pos - 1;
91 /**********************************************************************
94 * Find a default entry in a resource directory
95 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
97 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
100 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
101 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
102 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s3.OffsetToDirectory);
105 /*************************************************************************
106 * USER32_GetResourceTable
108 static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
110 IMAGE_DOS_HEADER * mz_header;
112 TRACE("%p %p\n", peimage, retptr);
116 mz_header = (IMAGE_DOS_HEADER*) peimage;
118 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
120 if (mz_header->e_cblp == 1) /* .ICO file ? */
122 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
126 return 0; /* failed */
128 if (mz_header->e_lfanew >= pesize) {
129 return 0; /* failed, happens with PKZIP DOS Exes for instance. */
131 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
132 return IMAGE_NT_SIGNATURE;
134 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
136 IMAGE_OS2_HEADER * ne_header;
138 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
140 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
143 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
144 *retptr = (LPBYTE)-1;
146 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
148 return IMAGE_OS2_SIGNATURE;
150 return 0; /* failed */
152 /*************************************************************************
153 * USER32_LoadResource
155 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
157 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
159 *uSize = (DWORD)pNInfo->length << sizeShift;
160 return peimage + ((DWORD)pNInfo->offset << sizeShift);
163 /*************************************************************************
166 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
168 TRACE("%p %p\n", peimage, lpiIDE);
170 *uSize = lpiIDE->dwBytesInRes;
171 return peimage + lpiIDE->dwImageOffset;
174 /*************************************************************************
175 * ICO_GetIconDirectory
177 * Reads .ico file and build phony ICONDIR struct
178 * see http://www.microsoft.com/win32dev/ui/icons.htm
180 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
181 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
183 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
185 CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
186 CURSORICONDIR * lpID; /* icon resource in resource format */
189 TRACE("%p %p\n", peimage, lplpiID);
191 lpcid = (CURSORICONDIR*)peimage;
193 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
196 /* allocate the phony ICONDIR structure */
197 *uSize = lpcid->idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
198 if( (lpID = (CURSORICONDIR*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
200 /* copy the header */
201 lpID->idReserved = lpcid->idReserved;
202 lpID->idType = lpcid->idType;
203 lpID->idCount = lpcid->idCount;
205 /* copy the entries */
206 for( i=0; i < lpcid->idCount; i++ )
208 memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpcid->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
209 lpID->idEntries[i].wResId = i;
212 *lplpiID = (LPicoICONDIR)peimage;
218 /*************************************************************************
219 * ICO_ExtractIconExW [internal]
222 * nIcons = 0: returns number of Icons in file
225 * failure:0; success: icon handle or nr of icons (nIconIndex-1)
227 static HRESULT ICO_ExtractIconExW(
228 LPCWSTR lpszExeFileName,
235 HGLOBAL hRet = E_FAIL;
239 UINT16 iconDirCount = 0,iconCount = 0;
245 TRACE("(file %s,start %d,extract %d\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons);
247 hFile = CreateFileW( lpszExeFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
248 if (hFile == INVALID_HANDLE_VALUE) return hRet;
249 fsizel = GetFileSize(hFile,&fsizeh);
252 fmapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL );
253 CloseHandle( hFile );
256 WARN("CreateFileMapping error %ld\n", GetLastError() );
260 if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0)))
262 WARN("MapViewOfFile error %ld\n", GetLastError() );
263 CloseHandle( fmapping );
266 CloseHandle( fmapping );
268 sig = USER32_GetResourceTable(peimage,fsizel,&pData);
271 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
274 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
275 NE_NAMEINFO *pIconStorage = NULL;
276 NE_NAMEINFO *pIconDir = NULL;
277 LPicoICONDIR lpiID = NULL;
279 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
281 if( pData == (BYTE*)-1 )
283 /* FIXME: pCIDir is allocated on the heap - memory leak */
284 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
287 iconDirCount = 1; iconCount = lpiID->idCount;
288 TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
291 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
293 if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
295 iconDirCount = pTInfo->count;
296 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
297 TRACE("\tfound directory - %i icon families\n", iconDirCount);
299 if( pTInfo->type_id == NE_RSCTYPE_ICON )
301 iconCount = pTInfo->count;
302 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
303 TRACE("\ttotal icons - %i\n", iconCount);
305 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
308 if( (pIconStorage && pIconDir) || lpiID ) /* load resources and create icons */
314 else if( nIconIndex < iconDirCount )
317 if( nIcons > iconDirCount - nIconIndex )
318 nIcons = iconDirCount - nIconIndex;
320 for( i = nIconIndex; i < nIconIndex + nIcons; i++ )
322 /* .ICO files have only one icon directory */
323 if( lpiID == NULL ) /* *.ico */
324 pCIDir = USER32_LoadResource( peimage, pIconDir + i, *(WORD*)pData, &uSize );
325 RetPtr[i-nIconIndex] = LookupIconIdFromDirectoryEx( pCIDir, TRUE, cxDesired, cyDesired, 0);
328 for( icon = nIconIndex; icon < nIconIndex + nIcons; icon++ )
332 pCIDir = ICO_LoadIcon( peimage, lpiID->idEntries + RetPtr[icon-nIconIndex], &uSize);
334 for( i = 0; i < iconCount; i++ )
335 if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
336 pCIDir = USER32_LoadResource( peimage, pIconStorage + i,*(WORD*)pData, &uSize );
339 RetPtr[icon-nIconIndex] = (HICON) CreateIconFromResourceEx(pCIDir,uSize,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
341 RetPtr[icon-nIconIndex] = 0;
350 else if( sig == IMAGE_NT_SIGNATURE )
353 PIMAGE_DOS_HEADER dheader;
354 PIMAGE_NT_HEADERS pe_header;
355 PIMAGE_SECTION_HEADER pe_sections;
356 const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
357 const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
358 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
361 dheader = (PIMAGE_DOS_HEADER)peimage;
362 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, USER32_GetResourceTable checked that */
363 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header)); /* probably makes problems with short PE headers...*/
366 /* search for the root resource directory */
367 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
369 if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
371 if (fsizel < pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData) {
372 FIXME("File %s too short (section is at %ld bytes, real size is %ld)\n",
373 debugstr_w(lpszExeFileName),
374 pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData,
379 /* FIXME: doesn't work when the resources are not in a separate section */
380 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
382 rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
389 WARN("haven't found section for resource directory.\n");
390 goto end; /* failure */
393 /* search for the group icon directory */
394 if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICONW), rootresdir)))
396 WARN("No Icongroupresourcedirectory!\n");
397 goto end; /* failure */
399 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
401 /* only number of icons requested */
405 goto end; /* success */
410 /* search resource id */
412 int iId = abs(nIconIndex);
413 PIMAGE_RESOURCE_DIRECTORY_ENTRY xprdeTmp = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
415 while(n<iconDirCount && xprdeTmp)
417 if(xprdeTmp->u1.s2.Id == iId)
427 WARN("resource id %d not found\n", iId);
428 goto end; /* failure */
433 /* check nIconIndex to be in range */
434 if (nIconIndex >= iconDirCount)
436 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
437 goto end; /* failure */
441 /* assure we don't get too much */
442 if( nIcons > iconDirCount - nIconIndex )
443 nIcons = iconDirCount - nIconIndex;
445 /* starting from specified index */
446 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1) + nIconIndex;
448 for (i=0; i < nIcons; i++,xresent++)
450 const IMAGE_RESOURCE_DIRECTORY *resdir;
452 /* go down this resource entry, name */
453 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s3.OffsetToDirectory));
455 /* default language (0) */
456 resdir = find_entry_default(resdir,rootresdir);
457 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
459 /* lookup address in mapped image for virtual address */
462 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
464 if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
466 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
469 if (igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size > fsizel) {
470 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);
471 goto end; /* failure */
473 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
478 FIXME("no matching real address for icongroup!\n");
479 goto end; /* failure */
481 RetPtr[i] = (HICON)LookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
484 if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICONW),rootresdir)))
486 WARN("No Iconresourcedirectory!\n");
487 goto end; /* failure */
490 for (i=0; i<nIcons; i++)
492 const IMAGE_RESOURCE_DIRECTORY *xresdir;
493 xresdir = find_entry_by_id(iconresdir,RetPtr[i],rootresdir);
494 xresdir = find_entry_default(xresdir,rootresdir);
495 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
498 /* map virtual to address in image */
499 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
501 if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
503 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
505 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
509 WARN("no matching real address found for icondata!\n");
513 RetPtr[i] = (HICON) CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
515 hRet = S_OK; /* return first icon */
516 } /* if(sig == IMAGE_NT_SIGNATURE) */
518 end: UnmapViewOfFile(peimage); /* success */
522 /***********************************************************************
523 * PrivateExtractIconsW [USER32.@]
526 * nIndex = 1: a small and a large icon are extracted.
527 * the higher word of sizeXY contains the size of the small icon, the lower
528 * word the size of the big icon. phicon points to HICON[2].
531 * nIcons > 0: HRESULT
532 * nIcons = 0: the number of icons
535 HRESULT WINAPI PrivateExtractIconsW (
540 HICON * phicon, /* [???] NOTE: HICON* */
541 DWORD w, /* [in] NOTE: 0 */
543 DWORD y ) /* [in] NOTE: 0x80 maybe LR_* constant */
546 TRACE("%s 0x%08x 0x%08lx 0x%08lx %p 0x%08lx 0x%08x 0x%08lx\n",
547 debugstr_w(lpwstrFile),nIndex, sizeX ,sizeY ,phicon,w,nIcons,y );
549 if ((nIcons == 2) && HIWORD(sizeX) && HIWORD(sizeY))
551 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, 1, sizeX & 0xffff, sizeY & 0xffff );
552 if (!SUCCEEDED(ret)) return ret;
553 ret = ICO_ExtractIconExW(lpwstrFile, phicon+1, nIndex, 1, (sizeX>>16) & 0xffff, (sizeY>>16) & 0xffff );
555 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX & 0xffff, sizeY & 0xffff );
559 /***********************************************************************
560 * PrivateExtractIconsA [USER32.@]
563 HRESULT WINAPI PrivateExtractIconsA (
569 DWORD w, /* [in] NOTE: 0 */
571 DWORD y ) /* [in] NOTE: 0x80 */
574 INT len = MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, NULL, 0 );
575 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
577 MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, lpwstrFile, len );
578 ret = PrivateExtractIconsW(
579 lpwstrFile, nIndex, sizeX, sizeY, phicon, w, nIcons, y
582 HeapFree(GetProcessHeap(), 0, lpwstrFile);
586 /***********************************************************************
587 * PrivateExtractIconExW [USER32.@]
589 * if nIcons = -1 it returns the number of icons in any case !!!
591 HRESULT WINAPI PrivateExtractIconExW (
598 DWORD cyicon, cysmicon, cxicon, cxsmicon;
601 TRACE("%s 0x%08lx %p %p 0x%08x\n",
602 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
604 if (nIndex == 1 && phIconSmall && phIconLarge)
607 cxicon = GetSystemMetrics(SM_CXICON);
608 cyicon = GetSystemMetrics(SM_CYICON);
609 cxsmicon = GetSystemMetrics(SM_CXSMICON);
610 cysmicon = GetSystemMetrics(SM_CYSMICON);
612 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon | (cxsmicon<<16), cyicon | (cysmicon<<16),
613 (HICON*) &hIcon, 0, 2, 0 );
614 *phIconLarge = hIcon[0];
615 *phIconSmall = hIcon[1];
623 /* extract n small icons */
624 cxsmicon = GetSystemMetrics(SM_CXSMICON);
625 cysmicon = GetSystemMetrics(SM_CYSMICON);
626 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxsmicon, cysmicon, phIconSmall, 0, nIcons, 0 );
630 /* extract n large icons */
631 cxicon = GetSystemMetrics(SM_CXICON);
632 cyicon = GetSystemMetrics(SM_CYICON);
633 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon, cyicon, phIconLarge, 0, nIcons, 0 );
638 /* get the number of icons */
639 return PrivateExtractIconsW ( lpwstrFile, 0, 0, 0, 0, 0, 0, 0 );
642 /***********************************************************************
643 * PrivateExtractIconExA [USER32.@]
645 HRESULT WINAPI PrivateExtractIconExA (
653 INT len = MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, NULL, 0 );
654 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
656 TRACE("%s 0x%08lx %p %p 0x%08x\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
658 MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, lpwstrFile, len );
659 ret = PrivateExtractIconExW(lpwstrFile,nIndex,phIconLarge, phIconSmall, nIcons);
660 HeapFree(GetProcessHeap(), 0, lpwstrFile);