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"
24 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(icon);
32 BYTE bWidth; /* Width, in pixels, of the image */
33 BYTE bHeight; /* Height, in pixels, of the image */
34 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
35 BYTE bReserved; /* Reserved ( must be 0) */
36 WORD wPlanes; /* Color Planes */
37 WORD wBitCount; /* Bits per pixel */
38 DWORD dwBytesInRes; /* How many bytes in this resource? */
39 DWORD dwImageOffset; /* Where in the file is this image? */
40 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
44 WORD idReserved; /* Reserved (must be 0) */
45 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
46 WORD idCount; /* How many images */
47 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
48 } icoICONDIR, *LPicoICONDIR;
53 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
55 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
56 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
57 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
58 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
60 static void dumpIcoDir ( LPicoICONDIR entry )
62 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
66 /**********************************************************************
69 * Find an entry by id in a resource directory
70 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
72 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
73 WORD id, const void *root )
75 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
78 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
79 min = dir->NumberOfNamedEntries;
80 max = min + dir->NumberOfIdEntries - 1;
83 pos = (min + max) / 2;
84 if (entry[pos].u1.s2.Id == id)
85 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
86 if (entry[pos].u1.s2.Id > id) max = pos - 1;
92 /**********************************************************************
95 * Find a default entry in a resource directory
96 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
98 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
101 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
102 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
103 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s3.OffsetToDirectory);
106 /*************************************************************************
107 * USER32_GetResourceTable
109 static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
111 IMAGE_DOS_HEADER * mz_header;
113 TRACE("%p %p\n", peimage, retptr);
117 mz_header = (IMAGE_DOS_HEADER*) peimage;
119 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
121 if (mz_header->e_cblp == 1) /* .ICO file ? */
123 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
127 return 0; /* failed */
129 if (mz_header->e_lfanew >= pesize) {
130 return 0; /* failed, happens with PKZIP DOS Exes for instance. */
132 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
133 return IMAGE_NT_SIGNATURE;
135 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
137 IMAGE_OS2_HEADER * ne_header;
139 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
141 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
144 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
145 *retptr = (LPBYTE)-1;
147 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
149 return IMAGE_OS2_SIGNATURE;
151 return 0; /* failed */
153 /*************************************************************************
154 * USER32_LoadResource
156 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
158 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
160 *uSize = (DWORD)pNInfo->length << sizeShift;
161 return peimage + ((DWORD)pNInfo->offset << sizeShift);
164 /*************************************************************************
167 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
169 TRACE("%p %p\n", peimage, lpiIDE);
171 *uSize = lpiIDE->dwBytesInRes;
172 return peimage + lpiIDE->dwImageOffset;
175 /*************************************************************************
176 * ICO_GetIconDirectory
178 * Reads .ico file and build phony ICONDIR struct
179 * see http://www.microsoft.com/win32dev/ui/icons.htm
181 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
182 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
184 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
186 CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
187 CURSORICONDIR * lpID; /* icon resource in resource format */
190 TRACE("%p %p\n", peimage, lplpiID);
192 lpcid = (CURSORICONDIR*)peimage;
194 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
197 /* allocate the phony ICONDIR structure */
198 *uSize = lpcid->idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
199 if( (lpID = (CURSORICONDIR*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
201 /* copy the header */
202 lpID->idReserved = lpcid->idReserved;
203 lpID->idType = lpcid->idType;
204 lpID->idCount = lpcid->idCount;
206 /* copy the entrys */
207 for( i=0; i < lpcid->idCount; i++ )
209 memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpcid->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
210 lpID->idEntries[i].wResId = i;
213 *lplpiID = (LPicoICONDIR)peimage;
219 /*************************************************************************
220 * ICO_ExtractIconExW [internal]
223 * nIcons = 0: returns number of Icons in file
226 * failure:0; success: icon handle or nr of icons (nIconIndex-1)
228 static HRESULT ICO_ExtractIconExW(
229 LPCWSTR lpszExeFileName,
236 HGLOBAL hRet = E_FAIL;
240 UINT16 iconDirCount = 0,iconCount = 0;
246 TRACE("(file %s,start %d,extract %d\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons);
248 hFile = CreateFileW( lpszExeFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
249 if (hFile == INVALID_HANDLE_VALUE) return hRet;
250 fsizel = GetFileSize(hFile,&fsizeh);
253 fmapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL );
254 CloseHandle( hFile );
257 WARN("CreateFileMapping error %ld\n", GetLastError() );
261 if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0)))
263 WARN("MapViewOfFile error %ld\n", GetLastError() );
264 CloseHandle( fmapping );
267 CloseHandle( fmapping );
269 sig = USER32_GetResourceTable(peimage,fsizel,&pData);
272 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
275 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
276 NE_NAMEINFO *pIconStorage = NULL;
277 NE_NAMEINFO *pIconDir = NULL;
278 LPicoICONDIR lpiID = NULL;
280 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
282 if( pData == (BYTE*)-1 )
284 /* FIXME: pCIDir is allocated on the heap - memory leak */
285 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
288 iconDirCount = 1; iconCount = lpiID->idCount;
289 TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
292 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
294 if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
296 iconDirCount = pTInfo->count;
297 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
298 TRACE("\tfound directory - %i icon families\n", iconDirCount);
300 if( pTInfo->type_id == NE_RSCTYPE_ICON )
302 iconCount = pTInfo->count;
303 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
304 TRACE("\ttotal icons - %i\n", iconCount);
306 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
309 if( (pIconStorage && pIconDir) || lpiID ) /* load resources and create icons */
315 else if( nIconIndex < iconDirCount )
318 if( nIcons > iconDirCount - nIconIndex )
319 nIcons = iconDirCount - nIconIndex;
321 for( i = nIconIndex; i < nIconIndex + nIcons; i++ )
323 /* .ICO files have only one icon directory */
324 if( lpiID == NULL ) /* *.ico */
325 pCIDir = USER32_LoadResource( peimage, pIconDir + i, *(WORD*)pData, &uSize );
326 RetPtr[i-nIconIndex] = LookupIconIdFromDirectoryEx( pCIDir, TRUE, cxDesired, cyDesired, 0);
329 for( icon = nIconIndex; icon < nIconIndex + nIcons; icon++ )
333 pCIDir = ICO_LoadIcon( peimage, lpiID->idEntries + RetPtr[icon-nIconIndex], &uSize);
335 for( i = 0; i < iconCount; i++ )
336 if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
337 pCIDir = USER32_LoadResource( peimage, pIconStorage + i,*(WORD*)pData, &uSize );
340 RetPtr[icon-nIconIndex] = (HICON) CreateIconFromResourceEx(pCIDir,uSize,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
342 RetPtr[icon-nIconIndex] = 0;
351 else if( sig == IMAGE_NT_SIGNATURE )
354 PIMAGE_DOS_HEADER dheader;
355 PIMAGE_NT_HEADERS pe_header;
356 PIMAGE_SECTION_HEADER pe_sections;
357 const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
358 const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
359 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
362 dheader = (PIMAGE_DOS_HEADER)peimage;
363 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, USER32_GetResourceTable checked that */
364 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header)); /* probably makes problems with short PE headers...*/
367 /* search for the root resource directory */
368 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
370 if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
372 if (fsizel < pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData) {
373 FIXME("File %s too short (section is at %ld bytes, real size is %ld)\n",
374 debugstr_w(lpszExeFileName),
375 pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData,
380 /* FIXME: doesn't work when the resources are not in a separate section */
381 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
383 rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
390 WARN("haven't found section for resource directory.\n");
391 goto end; /* failure */
394 /* search for the group icon directory */
395 if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICONW), rootresdir)))
397 WARN("No Icongroupresourcedirectory!\n");
398 goto end; /* failure */
400 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
402 /* only number of icons requested */
406 goto end; /* success */
411 /* search resource id */
413 int iId = abs(nIconIndex);
414 PIMAGE_RESOURCE_DIRECTORY_ENTRY xprdeTmp = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
416 while(n<iconDirCount && xprdeTmp)
418 if(xprdeTmp->u1.s2.Id == iId)
428 WARN("resource id %d not found\n", iId);
429 goto end; /* failure */
434 /* check nIconIndex to be in range */
435 if (nIconIndex >= iconDirCount)
437 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
438 goto end; /* failure */
442 /* assure we don't get too much */
443 if( nIcons > iconDirCount - nIconIndex )
444 nIcons = iconDirCount - nIconIndex;
446 /* starting from specified index */
447 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1) + nIconIndex;
449 for (i=0; i < nIcons; i++,xresent++)
451 const IMAGE_RESOURCE_DIRECTORY *resdir;
453 /* go down this resource entry, name */
454 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s3.OffsetToDirectory));
456 /* default language (0) */
457 resdir = find_entry_default(resdir,rootresdir);
458 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
460 /* lookup address in mapped image for virtual address */
463 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
465 if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
467 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
470 if (igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size > fsizel) {
471 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);
472 goto end; /* failure */
474 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
479 FIXME("no matching real address for icongroup!\n");
480 goto end; /* failure */
482 RetPtr[i] = (HICON)LookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
485 if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICONW),rootresdir)))
487 WARN("No Iconresourcedirectory!\n");
488 goto end; /* failure */
491 for (i=0; i<nIcons; i++)
493 const IMAGE_RESOURCE_DIRECTORY *xresdir;
494 xresdir = find_entry_by_id(iconresdir,RetPtr[i],rootresdir);
495 xresdir = find_entry_default(xresdir,rootresdir);
496 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
499 /* map virtual to address in image */
500 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
502 if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
504 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
506 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
510 WARN("no matching real address found for icondata!\n");
514 RetPtr[i] = (HICON) CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
516 hRet = S_OK; /* return first icon */
517 } /* if(sig == IMAGE_NT_SIGNATURE) */
519 end: UnmapViewOfFile(peimage); /* success */
523 /***********************************************************************
524 * PrivateExtractIconsW [USER32.@]
527 * nIndex = 1: a small and a large icon are extracted.
528 * the higher word of sizeXY contains the size of the small icon, the lower
529 * word the size of the big icon. phicon points to HICON[2].
532 * nIcons > 0: HRESULT
533 * nIcons = 0: the number of icons
536 HRESULT WINAPI PrivateExtractIconsW (
541 HICON * phicon, /* [???] NOTE: HICON* */
542 DWORD w, /* [in] NOTE: 0 */
544 DWORD y ) /* [in] NOTE: 0x80 maybe LR_* constant */
547 TRACE("%s 0x%08x 0x%08lx 0x%08lx %p 0x%08lx 0x%08x 0x%08lx\n",
548 debugstr_w(lpwstrFile),nIndex, sizeX ,sizeY ,phicon,w,nIcons,y );
550 if ((nIcons == 2) && HIWORD(sizeX) && HIWORD(sizeY))
552 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, 1, sizeX & 0xffff, sizeY & 0xffff );
553 if (!SUCCEEDED(ret)) return ret;
554 ret = ICO_ExtractIconExW(lpwstrFile, phicon+1, nIndex, 1, (sizeX>>16) & 0xffff, (sizeY>>16) & 0xffff );
556 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX & 0xffff, sizeY & 0xffff );
560 /***********************************************************************
561 * PrivateExtractIconsA [USER32.@]
564 HRESULT WINAPI PrivateExtractIconsA (
570 DWORD w, /* [in] NOTE: 0 */
572 DWORD y ) /* [in] NOTE: 0x80 */
575 LPWSTR lpwstrFile = HEAP_strdupAtoW(GetProcessHeap(), 0, lpstrFile);
577 ret = PrivateExtractIconsW(
578 lpwstrFile, nIndex, sizeX, sizeY, phicon, w, nIcons, y
581 HeapFree(GetProcessHeap(), 0, lpwstrFile);
585 /***********************************************************************
586 * PrivateExtractIconExW [USER32.@]
588 * if nIcons = -1 it returns the number of icons in any case !!!
590 HRESULT WINAPI PrivateExtractIconExW (
597 DWORD cyicon, cysmicon, cxicon, cxsmicon;
600 TRACE("%s 0x%08lx %p %p 0x%08x\n",
601 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
603 if (nIndex == 1 && phIconSmall && phIconLarge)
606 cxicon = GetSystemMetrics(SM_CXICON);
607 cyicon = GetSystemMetrics(SM_CYICON);
608 cxsmicon = GetSystemMetrics(SM_CXSMICON);
609 cysmicon = GetSystemMetrics(SM_CYSMICON);
611 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon | (cxsmicon<<16), cyicon | (cysmicon<<16),
612 (HICON*) &hIcon, 0, 2, 0 );
613 *phIconLarge = hIcon[0];
614 *phIconSmall = hIcon[1];
622 /* extract n small icons */
623 cxsmicon = GetSystemMetrics(SM_CXSMICON);
624 cysmicon = GetSystemMetrics(SM_CYSMICON);
625 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxsmicon, cysmicon, phIconSmall, 0, nIcons, 0 );
629 /* extract n large icons */
630 cxicon = GetSystemMetrics(SM_CXICON);
631 cyicon = GetSystemMetrics(SM_CYICON);
632 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon, cyicon, phIconLarge, 0, nIcons, 0 );
637 /* get the number of icons */
638 return PrivateExtractIconsW ( lpwstrFile, 0, 0, 0, 0, 0, 0, 0 );
641 /***********************************************************************
642 * PrivateExtractIconExA [USER32.@]
644 HRESULT WINAPI PrivateExtractIconExA (
652 LPWSTR lpwstrFile = HEAP_strdupAtoW(GetProcessHeap(), 0, lpstrFile);
654 TRACE("%s 0x%08lx %p %p 0x%08x\n",
655 lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
657 ret = PrivateExtractIconExW(lpwstrFile,nIndex,phIconLarge, phIconSmall, nIcons);
659 HeapFree(GetProcessHeap(), 0, lpwstrFile);