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
17 #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);
61 /*************************************************************************
62 * USER32_GetResourceTable
64 static DWORD USER32_GetResourceTable(LPBYTE peimage, LPBYTE *retptr)
66 IMAGE_DOS_HEADER * mz_header;
68 TRACE("%p %p\n", peimage, retptr);
72 mz_header = (IMAGE_DOS_HEADER*) peimage;
74 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
76 if (mz_header->e_cblp == 1) /* .ICO file ? */
78 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
82 return 0; /* failed */
85 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
86 return IMAGE_NT_SIGNATURE;
88 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
90 IMAGE_OS2_HEADER * ne_header;
92 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
94 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
97 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
100 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
102 return IMAGE_OS2_SIGNATURE;
104 return 0; /* failed */
106 /*************************************************************************
107 * USER32_LoadResource
109 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
111 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
113 *uSize = (DWORD)pNInfo->length << sizeShift;
114 return peimage + ((DWORD)pNInfo->offset << sizeShift);
117 /*************************************************************************
120 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
122 TRACE("%p %p\n", peimage, lpiIDE);
124 *uSize = lpiIDE->dwBytesInRes;
125 return peimage + lpiIDE->dwImageOffset;
128 /*************************************************************************
129 * ICO_GetIconDirectory
131 * Reads .ico file and build phony ICONDIR struct
132 * see http://www.microsoft.com/win32dev/ui/icons.htm
134 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
135 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
137 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
139 CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
140 CURSORICONDIR * lpID; /* icon resource in resource format */
143 TRACE("%p %p\n", peimage, lplpiID);
145 lpcid = (CURSORICONDIR*)peimage;
147 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
150 /* allocate the phony ICONDIR structure */
151 *uSize = lpcid->idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
152 if( (lpID = (CURSORICONDIR*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
154 /* copy the header */
155 lpID->idReserved = lpcid->idReserved;
156 lpID->idType = lpcid->idType;
157 lpID->idCount = lpcid->idCount;
159 /* copy the entrys */
160 for( i=0; i < lpcid->idCount; i++ )
162 memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpcid->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
163 lpID->idEntries[i].wResId = i;
166 *lplpiID = (LPicoICONDIR)peimage;
172 /*************************************************************************
173 * ICO_ExtractIconExW [internal]
176 * nIcons = 0: returns number of Icons in file
179 * failure:0; success: icon handle or nr of icons (nIconIndex-1)
181 static HRESULT ICO_ExtractIconExW(
182 LPCWSTR lpszExeFileName,
189 HGLOBAL hRet = E_FAIL;
193 UINT16 iconDirCount = 0,iconCount = 0;
198 TRACE("(file %s,start %d,extract %d\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons);
200 hFile = CreateFileW( lpszExeFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, -1 );
201 if (hFile == INVALID_HANDLE_VALUE) return hRet;
204 fmapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL );
205 CloseHandle( hFile );
208 WARN("CreateFileMapping error %ld\n", GetLastError() );
212 if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0)))
214 WARN("MapViewOfFile error %ld\n", GetLastError() );
215 CloseHandle( fmapping );
218 CloseHandle( fmapping );
220 sig = USER32_GetResourceTable(peimage,&pData);
223 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
226 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
227 NE_NAMEINFO *pIconStorage = NULL;
228 NE_NAMEINFO *pIconDir = NULL;
229 LPicoICONDIR lpiID = NULL;
231 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
233 if( pData == (BYTE*)-1 )
235 /* FIXME: pCIDir is allocated on the heap - memory leak */
236 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
239 iconDirCount = 1; iconCount = lpiID->idCount;
240 TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
243 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
245 if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
247 iconDirCount = pTInfo->count;
248 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
249 TRACE("\tfound directory - %i icon families\n", iconDirCount);
251 if( pTInfo->type_id == NE_RSCTYPE_ICON )
253 iconCount = pTInfo->count;
254 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
255 TRACE("\ttotal icons - %i\n", iconCount);
257 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
260 if( (pIconStorage && pIconDir) || lpiID ) /* load resources and create icons */
266 else if( nIconIndex < iconDirCount )
269 if( nIcons > iconDirCount - nIconIndex )
270 nIcons = iconDirCount - nIconIndex;
272 for( i = nIconIndex; i < nIconIndex + nIcons; i++ )
274 /* .ICO files have only one icon directory */
275 if( lpiID == NULL ) /* *.ico */
276 pCIDir = USER32_LoadResource( peimage, pIconDir + i, *(WORD*)pData, &uSize );
277 RetPtr[i-nIconIndex] = LookupIconIdFromDirectoryEx( pCIDir, TRUE, cxDesired, cyDesired, 0);
280 for( icon = nIconIndex; icon < nIconIndex + nIcons; icon++ )
284 pCIDir = ICO_LoadIcon( peimage, lpiID->idEntries + RetPtr[icon-nIconIndex], &uSize);
286 for( i = 0; i < iconCount; i++ )
287 if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
288 pCIDir = USER32_LoadResource( peimage, pIconStorage + i,*(WORD*)pData, &uSize );
291 RetPtr[icon-nIconIndex] = (HICON) CreateIconFromResourceEx(pCIDir,uSize,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
293 RetPtr[icon-nIconIndex] = 0;
302 else if( sig == IMAGE_NT_SIGNATURE )
305 PIMAGE_DOS_HEADER dheader;
306 PIMAGE_NT_HEADERS pe_header;
307 PIMAGE_SECTION_HEADER pe_sections;
308 const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
309 const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
310 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
313 dheader = (PIMAGE_DOS_HEADER)peimage;
314 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, USER32_GetResourceTable checked that */
315 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header)); /* probably makes problems with short PE headers...*/
318 /* search for the root resource directory */
319 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
321 if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
323 /* FIXME: doesn't work when the resources are not in a seperate section */
324 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
326 rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
333 WARN("haven't found section for resource directory.\n");
334 goto end; /* failure */
337 /* search for the group icon directory */
338 if (!(icongroupresdir = GetResDirEntryW(rootresdir, RT_GROUP_ICONW, rootresdir, FALSE)))
340 WARN("No Icongroupresourcedirectory!\n");
341 goto end; /* failure */
343 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
345 /* only number of icons requested */
349 goto end; /* success */
354 /* search resource id */
356 int iId = abs(nIconIndex);
357 PIMAGE_RESOURCE_DIRECTORY_ENTRY xprdeTmp = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
359 while(n<iconDirCount && xprdeTmp)
361 if(xprdeTmp->u1.Id == iId)
371 WARN("resource id %d not found\n", iId);
372 goto end; /* failure */
377 /* check nIconIndex to be in range */
378 if (nIconIndex >= iconDirCount)
380 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
381 goto end; /* failure */
385 /* assure we don't get too much */
386 if( nIcons > iconDirCount - nIconIndex )
387 nIcons = iconDirCount - nIconIndex;
389 /* starting from specified index */
390 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1) + nIconIndex;
392 for (i=0; i < nIcons; i++,xresent++)
394 const IMAGE_RESOURCE_DIRECTORY *resdir;
396 /* go down this resource entry, name */
397 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
399 /* default language (0) */
400 resdir = GetResDirEntryW(resdir,(LPWSTR)0,rootresdir,TRUE);
401 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
403 /* lookup address in mapped image for virtual address */
406 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
408 if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
410 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
412 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
417 WARN("no matching real address for icongroup!\n");
418 goto end; /* failure */
420 RetPtr[i] = (HICON)LookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
423 if (!(iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,rootresdir,FALSE)))
425 WARN("No Iconresourcedirectory!\n");
426 goto end; /* failure */
429 for (i=0; i<nIcons; i++)
431 const IMAGE_RESOURCE_DIRECTORY *xresdir;
432 xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],rootresdir,FALSE);
433 xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,rootresdir,TRUE);
434 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
437 /* map virtual to address in image */
438 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
440 if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
442 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
444 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
448 WARN("no matching real address found for icondata!\n");
452 RetPtr[i] = (HICON) CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
454 hRet = S_OK; /* return first icon */
455 } /* if(sig == IMAGE_NT_SIGNATURE) */
457 end: UnmapViewOfFile(peimage); /* success */
461 /***********************************************************************
462 * PrivateExtractIconsW [USER32.@]
465 * nIndex = 1: a small and a large icon are extracted.
466 * the higher word of sizeXY contains the size of the small icon, the lower
467 * word the size of the big icon. phicon points to HICON[2].
470 * nIcons > 0: HRESULT
471 * nIcons = 0: the number of icons
474 HRESULT WINAPI PrivateExtractIconsW (
479 HICON * phicon, /* HICON* */
482 DWORD y ) /* 0x80 maybe LR_* constant */
485 TRACE("%s 0x%08x 0x%08lx 0x%08lx %p 0x%08lx 0x%08x 0x%08lx stub\n",
486 debugstr_w(lpwstrFile),nIndex, sizeX ,sizeY ,phicon,w,nIcons,y );
489 if ((nIcons == 2) && HIWORD(sizeX) && HIWORD(sizeY))
491 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, 1, sizeX & 0xffff, sizeY & 0xffff );
492 if (!SUCCEEDED(ret)) return ret;
493 ret = ICO_ExtractIconExW(lpwstrFile, phicon+1, nIndex, 1, (sizeX>>16) & 0xffff, (sizeY>>16) & 0xffff );
497 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX & 0xffff, sizeY & 0xffff );
500 FIXME_(icon)("hicon=%08x ret=0x%08lx\n", *phicon, ret);
505 /***********************************************************************
506 * PrivateExtractIconsA [USER32.@]
509 HRESULT WINAPI PrivateExtractIconsA (
520 LPWSTR lpwstrFile = HEAP_strdupAtoW(GetProcessHeap(), 0, lpstrFile);
522 FIXME_(icon)("%s 0x%08x 0x%08lx 0x%08lx %p 0x%08lx 0x%08x 0x%08lx stub\n",
523 lpstrFile, nIndex, sizeX, sizeY, phicon, w, nIcons, y );
525 ret = PrivateExtractIconsW(lpwstrFile, nIndex, sizeX, sizeY, phicon, w, nIcons, y);
527 FIXME_(icon)("hicon=%08x ret=0x%08lx\n", *phicon, ret);
529 HeapFree(GetProcessHeap(), 0, lpwstrFile);
533 /***********************************************************************
534 * PrivateExtractIconExW [USER32.443]
536 * if nIcons = -1 it returns the number of icons in any case !!!
538 HRESULT WINAPI PrivateExtractIconExW (
545 DWORD cyicon, cysmicon, cxicon, cxsmicon;
548 TRACE("%s 0x%08lx %p %p 0x%08x\n",
549 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
551 if (nIndex == 1 && phIconSmall && phIconLarge)
554 cxicon = GetSystemMetrics(SM_CXICON);
555 cyicon = GetSystemMetrics(SM_CYICON);
556 cxsmicon = GetSystemMetrics(SM_CXSMICON);
557 cysmicon = GetSystemMetrics(SM_CYSMICON);
559 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon | (cxsmicon<<16), cyicon | (cysmicon<<16),
560 (HICON*) &hIcon, 0, 2, 0 );
561 *phIconLarge = hIcon[0];
562 *phIconSmall = hIcon[1];
570 /* extract n small icons */
571 cxsmicon = GetSystemMetrics(SM_CXSMICON);
572 cysmicon = GetSystemMetrics(SM_CYSMICON);
573 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxsmicon, cysmicon, phIconSmall, 0, nIcons, 0 );
577 /* extract n large icons */
578 cxicon = GetSystemMetrics(SM_CXICON);
579 cyicon = GetSystemMetrics(SM_CYICON);
580 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon, cyicon, phIconLarge, 0, nIcons, 0 );
585 /* get the number of icons */
586 return PrivateExtractIconsW ( lpwstrFile, 0, 0, 0, 0, 0, 0, 0 );
589 /***********************************************************************
590 * PrivateExtractIconExA [USER32.442]
592 HRESULT WINAPI PrivateExtractIconExA (
600 LPWSTR lpwstrFile = HEAP_strdupAtoW(GetProcessHeap(), 0, lpstrFile);
602 TRACE("%s 0x%08lx %p %p 0x%08x\n",
603 lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
605 ret = PrivateExtractIconExW(lpwstrFile,nIndex,phIconLarge, phIconSmall, nIcons);
607 HeapFree(GetProcessHeap(), 0, lpwstrFile);