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
9 #include <stdlib.h> /* abs() */
10 #include <sys/types.h>
17 #include "wine/winbase16.h"
18 #include "cursoricon.h"
20 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(icon);
28 BYTE bWidth; /* Width, in pixels, of the image */
29 BYTE bHeight; /* Height, in pixels, of the image */
30 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
31 BYTE bReserved; /* Reserved ( must be 0) */
32 WORD wPlanes; /* Color Planes */
33 WORD wBitCount; /* Bits per pixel */
34 DWORD dwBytesInRes; /* How many bytes in this resource? */
35 DWORD dwImageOffset; /* Where in the file is this image? */
36 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
40 WORD idReserved; /* Reserved (must be 0) */
41 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
42 WORD idCount; /* How many images */
43 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
44 } icoICONDIR, *LPicoICONDIR;
49 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
51 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
52 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
53 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
54 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
56 static void dumpIcoDir ( LPicoICONDIR entry )
58 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
62 /**********************************************************************
65 * Find an entry by id in a resource directory
66 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
68 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
69 WORD id, const void *root )
71 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
74 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
75 min = dir->NumberOfNamedEntries;
76 max = min + dir->NumberOfIdEntries - 1;
79 pos = (min + max) / 2;
80 if (entry[pos].u1.Id == id)
81 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s2.OffsetToDirectory);
82 if (entry[pos].u1.Id > id) max = pos - 1;
88 /**********************************************************************
91 * Find a default entry in a resource directory
92 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
94 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
97 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
98 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
99 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s2.OffsetToDirectory);
102 /*************************************************************************
103 * USER32_GetResourceTable
105 static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
107 IMAGE_DOS_HEADER * mz_header;
109 TRACE("%p %p\n", peimage, retptr);
113 mz_header = (IMAGE_DOS_HEADER*) peimage;
115 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
117 if (mz_header->e_cblp == 1) /* .ICO file ? */
119 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
123 return 0; /* failed */
125 if (mz_header->e_lfanew >= pesize) {
126 return 0; /* failed, happens with PKZIP DOS Exes for instance. */
128 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
129 return IMAGE_NT_SIGNATURE;
131 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
133 IMAGE_OS2_HEADER * ne_header;
135 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
137 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
140 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
141 *retptr = (LPBYTE)-1;
143 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
145 return IMAGE_OS2_SIGNATURE;
147 return 0; /* failed */
149 /*************************************************************************
150 * USER32_LoadResource
152 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
154 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
156 *uSize = (DWORD)pNInfo->length << sizeShift;
157 return peimage + ((DWORD)pNInfo->offset << sizeShift);
160 /*************************************************************************
163 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
165 TRACE("%p %p\n", peimage, lpiIDE);
167 *uSize = lpiIDE->dwBytesInRes;
168 return peimage + lpiIDE->dwImageOffset;
171 /*************************************************************************
172 * ICO_GetIconDirectory
174 * Reads .ico file and build phony ICONDIR struct
175 * see http://www.microsoft.com/win32dev/ui/icons.htm
177 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
178 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
180 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
182 CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
183 CURSORICONDIR * lpID; /* icon resource in resource format */
186 TRACE("%p %p\n", peimage, lplpiID);
188 lpcid = (CURSORICONDIR*)peimage;
190 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
193 /* allocate the phony ICONDIR structure */
194 *uSize = lpcid->idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
195 if( (lpID = (CURSORICONDIR*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
197 /* copy the header */
198 lpID->idReserved = lpcid->idReserved;
199 lpID->idType = lpcid->idType;
200 lpID->idCount = lpcid->idCount;
202 /* copy the entrys */
203 for( i=0; i < lpcid->idCount; i++ )
205 memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpcid->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
206 lpID->idEntries[i].wResId = i;
209 *lplpiID = (LPicoICONDIR)peimage;
215 /*************************************************************************
216 * ICO_ExtractIconExW [internal]
219 * nIcons = 0: returns number of Icons in file
222 * failure:0; success: icon handle or nr of icons (nIconIndex-1)
224 static HRESULT ICO_ExtractIconExW(
225 LPCWSTR lpszExeFileName,
232 HGLOBAL hRet = E_FAIL;
236 UINT16 iconDirCount = 0,iconCount = 0;
242 TRACE("(file %s,start %d,extract %d\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons);
244 hFile = CreateFileW( lpszExeFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, -1 );
245 if (hFile == INVALID_HANDLE_VALUE) return hRet;
246 fsizel = GetFileSize(hFile,&fsizeh);
249 fmapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL );
250 CloseHandle( hFile );
253 WARN("CreateFileMapping error %ld\n", GetLastError() );
257 if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0)))
259 WARN("MapViewOfFile error %ld\n", GetLastError() );
260 CloseHandle( fmapping );
263 CloseHandle( fmapping );
265 sig = USER32_GetResourceTable(peimage,fsizel,&pData);
268 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
271 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
272 NE_NAMEINFO *pIconStorage = NULL;
273 NE_NAMEINFO *pIconDir = NULL;
274 LPicoICONDIR lpiID = NULL;
276 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
278 if( pData == (BYTE*)-1 )
280 /* FIXME: pCIDir is allocated on the heap - memory leak */
281 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
284 iconDirCount = 1; iconCount = lpiID->idCount;
285 TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
288 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
290 if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
292 iconDirCount = pTInfo->count;
293 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
294 TRACE("\tfound directory - %i icon families\n", iconDirCount);
296 if( pTInfo->type_id == NE_RSCTYPE_ICON )
298 iconCount = pTInfo->count;
299 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
300 TRACE("\ttotal icons - %i\n", iconCount);
302 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
305 if( (pIconStorage && pIconDir) || lpiID ) /* load resources and create icons */
311 else if( nIconIndex < iconDirCount )
314 if( nIcons > iconDirCount - nIconIndex )
315 nIcons = iconDirCount - nIconIndex;
317 for( i = nIconIndex; i < nIconIndex + nIcons; i++ )
319 /* .ICO files have only one icon directory */
320 if( lpiID == NULL ) /* *.ico */
321 pCIDir = USER32_LoadResource( peimage, pIconDir + i, *(WORD*)pData, &uSize );
322 RetPtr[i-nIconIndex] = LookupIconIdFromDirectoryEx( pCIDir, TRUE, cxDesired, cyDesired, 0);
325 for( icon = nIconIndex; icon < nIconIndex + nIcons; icon++ )
329 pCIDir = ICO_LoadIcon( peimage, lpiID->idEntries + RetPtr[icon-nIconIndex], &uSize);
331 for( i = 0; i < iconCount; i++ )
332 if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
333 pCIDir = USER32_LoadResource( peimage, pIconStorage + i,*(WORD*)pData, &uSize );
336 RetPtr[icon-nIconIndex] = (HICON) CreateIconFromResourceEx(pCIDir,uSize,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
338 RetPtr[icon-nIconIndex] = 0;
347 else if( sig == IMAGE_NT_SIGNATURE )
350 PIMAGE_DOS_HEADER dheader;
351 PIMAGE_NT_HEADERS pe_header;
352 PIMAGE_SECTION_HEADER pe_sections;
353 const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
354 const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
355 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
358 dheader = (PIMAGE_DOS_HEADER)peimage;
359 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, USER32_GetResourceTable checked that */
360 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header)); /* probably makes problems with short PE headers...*/
363 /* search for the root resource directory */
364 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
366 if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
368 if (fsizel < pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData) {
369 FIXME("File %s too short (section is at %ld bytes, real size is %ld)\n",
370 debugstr_w(lpszExeFileName),
371 pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData,
376 /* FIXME: doesn't work when the resources are not in a seperate section */
377 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
379 rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
386 WARN("haven't found section for resource directory.\n");
387 goto end; /* failure */
390 /* search for the group icon directory */
391 if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICONW), rootresdir)))
393 WARN("No Icongroupresourcedirectory!\n");
394 goto end; /* failure */
396 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
398 /* only number of icons requested */
402 goto end; /* success */
407 /* search resource id */
409 int iId = abs(nIconIndex);
410 PIMAGE_RESOURCE_DIRECTORY_ENTRY xprdeTmp = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
412 while(n<iconDirCount && xprdeTmp)
414 if(xprdeTmp->u1.Id == iId)
424 WARN("resource id %d not found\n", iId);
425 goto end; /* failure */
430 /* check nIconIndex to be in range */
431 if (nIconIndex >= iconDirCount)
433 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
434 goto end; /* failure */
438 /* assure we don't get too much */
439 if( nIcons > iconDirCount - nIconIndex )
440 nIcons = iconDirCount - nIconIndex;
442 /* starting from specified index */
443 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1) + nIconIndex;
445 for (i=0; i < nIcons; i++,xresent++)
447 const IMAGE_RESOURCE_DIRECTORY *resdir;
449 /* go down this resource entry, name */
450 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s2.OffsetToDirectory));
452 /* default language (0) */
453 resdir = find_entry_default(resdir,rootresdir);
454 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
456 /* lookup address in mapped image for virtual address */
459 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
461 if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
463 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
466 if (igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size > fsizel) {
467 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);
468 goto end; /* failure */
470 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
475 FIXME("no matching real address for icongroup!\n");
476 goto end; /* failure */
478 RetPtr[i] = (HICON)LookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
481 if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICONW),rootresdir)))
483 WARN("No Iconresourcedirectory!\n");
484 goto end; /* failure */
487 for (i=0; i<nIcons; i++)
489 const IMAGE_RESOURCE_DIRECTORY *xresdir;
490 xresdir = find_entry_by_id(iconresdir,RetPtr[i],rootresdir);
491 xresdir = find_entry_default(xresdir,rootresdir);
492 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
495 /* map virtual to address in image */
496 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
498 if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
500 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
502 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
506 WARN("no matching real address found for icondata!\n");
510 RetPtr[i] = (HICON) CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
512 hRet = S_OK; /* return first icon */
513 } /* if(sig == IMAGE_NT_SIGNATURE) */
515 end: UnmapViewOfFile(peimage); /* success */
519 /***********************************************************************
520 * PrivateExtractIconsW [USER32.@]
523 * nIndex = 1: a small and a large icon are extracted.
524 * the higher word of sizeXY contains the size of the small icon, the lower
525 * word the size of the big icon. phicon points to HICON[2].
528 * nIcons > 0: HRESULT
529 * nIcons = 0: the number of icons
532 HRESULT WINAPI PrivateExtractIconsW (
537 HICON * phicon, /* [???] NOTE: HICON* */
538 DWORD w, /* [in] NOTE: 0 */
540 DWORD y ) /* [in] NOTE: 0x80 maybe LR_* constant */
543 TRACE("%s 0x%08x 0x%08lx 0x%08lx %p 0x%08lx 0x%08x 0x%08lx\n",
544 debugstr_w(lpwstrFile),nIndex, sizeX ,sizeY ,phicon,w,nIcons,y );
546 if ((nIcons == 2) && HIWORD(sizeX) && HIWORD(sizeY))
548 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, 1, sizeX & 0xffff, sizeY & 0xffff );
549 if (!SUCCEEDED(ret)) return ret;
550 ret = ICO_ExtractIconExW(lpwstrFile, phicon+1, nIndex, 1, (sizeX>>16) & 0xffff, (sizeY>>16) & 0xffff );
552 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX & 0xffff, sizeY & 0xffff );
556 /***********************************************************************
557 * PrivateExtractIconsA [USER32.@]
560 HRESULT WINAPI PrivateExtractIconsA (
566 DWORD w, /* [in] NOTE: 0 */
568 DWORD y ) /* [in] NOTE: 0x80 */
571 LPWSTR lpwstrFile = HEAP_strdupAtoW(GetProcessHeap(), 0, lpstrFile);
573 ret = PrivateExtractIconsW(
574 lpwstrFile, nIndex, sizeX, sizeY, phicon, w, nIcons, y
577 HeapFree(GetProcessHeap(), 0, lpwstrFile);
581 /***********************************************************************
582 * PrivateExtractIconExW [USER32.443]
584 * if nIcons = -1 it returns the number of icons in any case !!!
586 HRESULT WINAPI PrivateExtractIconExW (
593 DWORD cyicon, cysmicon, cxicon, cxsmicon;
596 TRACE("%s 0x%08lx %p %p 0x%08x\n",
597 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
599 if (nIndex == 1 && phIconSmall && phIconLarge)
602 cxicon = GetSystemMetrics(SM_CXICON);
603 cyicon = GetSystemMetrics(SM_CYICON);
604 cxsmicon = GetSystemMetrics(SM_CXSMICON);
605 cysmicon = GetSystemMetrics(SM_CYSMICON);
607 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon | (cxsmicon<<16), cyicon | (cysmicon<<16),
608 (HICON*) &hIcon, 0, 2, 0 );
609 *phIconLarge = hIcon[0];
610 *phIconSmall = hIcon[1];
618 /* extract n small icons */
619 cxsmicon = GetSystemMetrics(SM_CXSMICON);
620 cysmicon = GetSystemMetrics(SM_CYSMICON);
621 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxsmicon, cysmicon, phIconSmall, 0, nIcons, 0 );
625 /* extract n large icons */
626 cxicon = GetSystemMetrics(SM_CXICON);
627 cyicon = GetSystemMetrics(SM_CYICON);
628 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon, cyicon, phIconLarge, 0, nIcons, 0 );
633 /* get the number of icons */
634 return PrivateExtractIconsW ( lpwstrFile, 0, 0, 0, 0, 0, 0, 0 );
637 /***********************************************************************
638 * PrivateExtractIconExA [USER32.442]
640 HRESULT WINAPI PrivateExtractIconExA (
648 LPWSTR lpwstrFile = HEAP_strdupAtoW(GetProcessHeap(), 0, lpstrFile);
650 TRACE("%s 0x%08lx %p %p 0x%08x\n",
651 lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
653 ret = PrivateExtractIconExW(lpwstrFile,nIndex,phIconLarge, phIconSmall, nIcons);
655 HeapFree(GetProcessHeap(), 0, lpwstrFile);