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>
37 #include "wine/winbase16.h"
38 #include "cursoricon.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(icon);
47 BYTE bWidth; /* Width, in pixels, of the image */
48 BYTE bHeight; /* Height, in pixels, of the image */
49 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
50 BYTE bReserved; /* Reserved ( must be 0) */
51 WORD wPlanes; /* Color Planes */
52 WORD wBitCount; /* Bits per pixel */
53 DWORD dwBytesInRes; /* How many bytes in this resource? */
54 DWORD dwImageOffset; /* Where in the file is this image? */
55 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
59 WORD idReserved; /* Reserved (must be 0) */
60 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
61 WORD idCount; /* How many images */
62 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
63 } icoICONDIR, *LPicoICONDIR;
68 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
70 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
71 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
72 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
73 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
75 static void dumpIcoDir ( LPicoICONDIR entry )
77 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
81 /**********************************************************************
84 * Find an entry by id in a resource directory
85 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
87 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
88 WORD id, const void *root )
90 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
93 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
94 min = dir->NumberOfNamedEntries;
95 max = min + dir->NumberOfIdEntries - 1;
98 pos = (min + max) / 2;
99 if (entry[pos].u1.s2.Id == id)
100 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
101 if (entry[pos].u1.s2.Id > id) max = pos - 1;
107 /**********************************************************************
110 * Find a default entry in a resource directory
111 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
113 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
116 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
117 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
118 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s3.OffsetToDirectory);
121 /*************************************************************************
122 * USER32_GetResourceTable
124 static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
126 IMAGE_DOS_HEADER * mz_header;
128 TRACE("%p %p\n", peimage, retptr);
132 mz_header = (IMAGE_DOS_HEADER*) peimage;
134 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
136 if (mz_header->e_cblp == 1) /* .ICO file ? */
138 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
142 return 0; /* failed */
144 if (mz_header->e_lfanew >= pesize) {
145 return 0; /* failed, happens with PKZIP DOS Exes for instance. */
147 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
148 return IMAGE_NT_SIGNATURE;
150 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
152 IMAGE_OS2_HEADER * ne_header;
154 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
156 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
159 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
160 *retptr = (LPBYTE)-1;
162 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
164 return IMAGE_OS2_SIGNATURE;
166 return 0; /* failed */
168 /*************************************************************************
169 * USER32_LoadResource
171 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
173 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
175 *uSize = (DWORD)pNInfo->length << sizeShift;
176 return peimage + ((DWORD)pNInfo->offset << sizeShift);
179 /*************************************************************************
182 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
184 TRACE("%p %p\n", peimage, lpiIDE);
186 *uSize = lpiIDE->dwBytesInRes;
187 return peimage + lpiIDE->dwImageOffset;
190 /*************************************************************************
191 * ICO_GetIconDirectory
193 * Reads .ico file and build phony ICONDIR struct
194 * see http://www.microsoft.com/win32dev/ui/icons.htm
196 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
197 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
199 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
201 CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
202 CURSORICONDIR * lpID; /* icon resource in resource format */
205 TRACE("%p %p\n", peimage, lplpiID);
207 lpcid = (CURSORICONDIR*)peimage;
209 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
212 /* allocate the phony ICONDIR structure */
213 *uSize = lpcid->idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
214 if( (lpID = (CURSORICONDIR*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
216 /* copy the header */
217 lpID->idReserved = lpcid->idReserved;
218 lpID->idType = lpcid->idType;
219 lpID->idCount = lpcid->idCount;
221 /* copy the entries */
222 for( i=0; i < lpcid->idCount; i++ )
224 memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpcid->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
225 lpID->idEntries[i].wResId = i;
228 *lplpiID = (LPicoICONDIR)peimage;
234 /*************************************************************************
235 * ICO_ExtractIconExW [internal]
238 * nIcons = 0: returns number of Icons in file
241 * failure:0; success: icon handle or nr of icons (nIconIndex-1)
243 static HRESULT ICO_ExtractIconExW(
244 LPCWSTR lpszExeFileName,
251 HGLOBAL hRet = E_FAIL;
255 UINT16 iconDirCount = 0,iconCount = 0;
261 TRACE("(file %s,start %d,extract %d\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons);
263 hFile = CreateFileW( lpszExeFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
264 if (hFile == INVALID_HANDLE_VALUE) return hRet;
265 fsizel = GetFileSize(hFile,&fsizeh);
268 fmapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL );
269 CloseHandle( hFile );
272 WARN("CreateFileMapping error %ld\n", GetLastError() );
276 if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0)))
278 WARN("MapViewOfFile error %ld\n", GetLastError() );
279 CloseHandle( fmapping );
282 CloseHandle( fmapping );
284 sig = USER32_GetResourceTable(peimage,fsizel,&pData);
287 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
290 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
291 NE_NAMEINFO *pIconStorage = NULL;
292 NE_NAMEINFO *pIconDir = NULL;
293 LPicoICONDIR lpiID = NULL;
295 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
297 if( pData == (BYTE*)-1 )
299 /* FIXME: pCIDir is allocated on the heap - memory leak */
300 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
303 iconDirCount = 1; iconCount = lpiID->idCount;
304 TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
307 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
309 if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
311 iconDirCount = pTInfo->count;
312 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
313 TRACE("\tfound directory - %i icon families\n", iconDirCount);
315 if( pTInfo->type_id == NE_RSCTYPE_ICON )
317 iconCount = pTInfo->count;
318 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
319 TRACE("\ttotal icons - %i\n", iconCount);
321 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
324 if( (pIconStorage && pIconDir) || lpiID ) /* load resources and create icons */
330 else if( nIconIndex < iconDirCount )
333 if( nIcons > iconDirCount - nIconIndex )
334 nIcons = iconDirCount - nIconIndex;
336 for( i = nIconIndex; i < nIconIndex + nIcons; i++ )
338 /* .ICO files have only one icon directory */
339 if( lpiID == NULL ) /* *.ico */
340 pCIDir = USER32_LoadResource( peimage, pIconDir + i, *(WORD*)pData, &uSize );
341 RetPtr[i-nIconIndex] = LookupIconIdFromDirectoryEx( pCIDir, TRUE, cxDesired, cyDesired, 0);
344 for( icon = nIconIndex; icon < nIconIndex + nIcons; icon++ )
348 pCIDir = ICO_LoadIcon( peimage, lpiID->idEntries + RetPtr[icon-nIconIndex], &uSize);
350 for( i = 0; i < iconCount; i++ )
351 if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
352 pCIDir = USER32_LoadResource( peimage, pIconStorage + i,*(WORD*)pData, &uSize );
355 RetPtr[icon-nIconIndex] = (HICON) CreateIconFromResourceEx(pCIDir,uSize,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
357 RetPtr[icon-nIconIndex] = 0;
366 else if( sig == IMAGE_NT_SIGNATURE )
369 PIMAGE_DOS_HEADER dheader;
370 PIMAGE_NT_HEADERS pe_header;
371 PIMAGE_SECTION_HEADER pe_sections;
372 const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
373 const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
374 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
377 dheader = (PIMAGE_DOS_HEADER)peimage;
378 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, USER32_GetResourceTable checked that */
379 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header)); /* probably makes problems with short PE headers...*/
382 /* search for the root resource directory */
383 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
385 if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
387 if (fsizel < pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData) {
388 FIXME("File %s too short (section is at %ld bytes, real size is %ld)\n",
389 debugstr_w(lpszExeFileName),
390 pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData,
395 /* FIXME: doesn't work when the resources are not in a separate section */
396 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
398 rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
405 WARN("haven't found section for resource directory.\n");
406 goto end; /* failure */
409 /* search for the group icon directory */
410 if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICONW), rootresdir)))
412 WARN("No Icongroupresourcedirectory!\n");
413 goto end; /* failure */
415 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
417 /* only number of icons requested */
421 goto end; /* success */
426 /* search resource id */
428 int iId = abs(nIconIndex);
429 PIMAGE_RESOURCE_DIRECTORY_ENTRY xprdeTmp = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
431 while(n<iconDirCount && xprdeTmp)
433 if(xprdeTmp->u1.s2.Id == iId)
443 WARN("resource id %d not found\n", iId);
444 goto end; /* failure */
449 /* check nIconIndex to be in range */
450 if (nIconIndex >= iconDirCount)
452 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
453 goto end; /* failure */
457 /* assure we don't get too much */
458 if( nIcons > iconDirCount - nIconIndex )
459 nIcons = iconDirCount - nIconIndex;
461 /* starting from specified index */
462 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1) + nIconIndex;
464 for (i=0; i < nIcons; i++,xresent++)
466 const IMAGE_RESOURCE_DIRECTORY *resdir;
468 /* go down this resource entry, name */
469 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s3.OffsetToDirectory));
471 /* default language (0) */
472 resdir = find_entry_default(resdir,rootresdir);
473 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
475 /* lookup address in mapped image for virtual address */
478 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
480 if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
482 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
485 if (igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size > fsizel) {
486 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);
487 goto end; /* failure */
489 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
494 FIXME("no matching real address for icongroup!\n");
495 goto end; /* failure */
497 RetPtr[i] = (HICON)LookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
500 if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICONW),rootresdir)))
502 WARN("No Iconresourcedirectory!\n");
503 goto end; /* failure */
506 for (i=0; i<nIcons; i++)
508 const IMAGE_RESOURCE_DIRECTORY *xresdir;
509 xresdir = find_entry_by_id(iconresdir,RetPtr[i],rootresdir);
510 xresdir = find_entry_default(xresdir,rootresdir);
511 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
514 /* map virtual to address in image */
515 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
517 if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
519 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
521 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
525 WARN("no matching real address found for icondata!\n");
529 RetPtr[i] = (HICON) CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
531 hRet = S_OK; /* return first icon */
532 } /* if(sig == IMAGE_NT_SIGNATURE) */
534 end: UnmapViewOfFile(peimage); /* success */
538 /***********************************************************************
539 * PrivateExtractIconsW [USER32.@]
542 * nIndex = 1: a small and a large icon are extracted.
543 * the higher word of sizeXY contains the size of the small icon, the lower
544 * word the size of the big icon. phicon points to HICON[2].
547 * nIcons > 0: HRESULT
548 * nIcons = 0: the number of icons
551 HRESULT WINAPI PrivateExtractIconsW (
556 HICON * phicon, /* [???] NOTE: HICON* */
557 DWORD w, /* [in] NOTE: 0 */
559 DWORD y ) /* [in] NOTE: 0x80 maybe LR_* constant */
562 TRACE("%s 0x%08x 0x%08lx 0x%08lx %p 0x%08lx 0x%08x 0x%08lx\n",
563 debugstr_w(lpwstrFile),nIndex, sizeX ,sizeY ,phicon,w,nIcons,y );
565 if ((nIcons == 2) && HIWORD(sizeX) && HIWORD(sizeY))
567 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, 1, sizeX & 0xffff, sizeY & 0xffff );
568 if (!SUCCEEDED(ret)) return ret;
569 ret = ICO_ExtractIconExW(lpwstrFile, phicon+1, nIndex, 1, (sizeX>>16) & 0xffff, (sizeY>>16) & 0xffff );
571 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX & 0xffff, sizeY & 0xffff );
575 /***********************************************************************
576 * PrivateExtractIconsA [USER32.@]
579 HRESULT WINAPI PrivateExtractIconsA (
585 DWORD w, /* [in] NOTE: 0 */
587 DWORD y ) /* [in] NOTE: 0x80 */
590 INT len = MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, NULL, 0 );
591 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
593 MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, lpwstrFile, len );
594 ret = PrivateExtractIconsW(
595 lpwstrFile, nIndex, sizeX, sizeY, phicon, w, nIcons, y
598 HeapFree(GetProcessHeap(), 0, lpwstrFile);
602 /***********************************************************************
603 * PrivateExtractIconExW [USER32.@]
605 * if nIcons = -1 it returns the number of icons in any case !!!
607 HRESULT WINAPI PrivateExtractIconExW (
614 DWORD cyicon, cysmicon, cxicon, cxsmicon;
617 TRACE("%s 0x%08lx %p %p 0x%08x\n",
618 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
620 if (nIndex == 1 && phIconSmall && phIconLarge)
623 cxicon = GetSystemMetrics(SM_CXICON);
624 cyicon = GetSystemMetrics(SM_CYICON);
625 cxsmicon = GetSystemMetrics(SM_CXSMICON);
626 cysmicon = GetSystemMetrics(SM_CYSMICON);
628 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon | (cxsmicon<<16), cyicon | (cysmicon<<16),
629 (HICON*) &hIcon, 0, 2, 0 );
630 *phIconLarge = hIcon[0];
631 *phIconSmall = hIcon[1];
639 /* extract n small icons */
640 cxsmicon = GetSystemMetrics(SM_CXSMICON);
641 cysmicon = GetSystemMetrics(SM_CYSMICON);
642 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxsmicon, cysmicon, phIconSmall, 0, nIcons, 0 );
646 /* extract n large icons */
647 cxicon = GetSystemMetrics(SM_CXICON);
648 cyicon = GetSystemMetrics(SM_CYICON);
649 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon, cyicon, phIconLarge, 0, nIcons, 0 );
654 /* get the number of icons */
655 return PrivateExtractIconsW ( lpwstrFile, 0, 0, 0, 0, 0, 0, 0 );
658 /***********************************************************************
659 * PrivateExtractIconExA [USER32.@]
661 HRESULT WINAPI PrivateExtractIconExA (
669 INT len = MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, NULL, 0 );
670 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
672 TRACE("%s 0x%08lx %p %p 0x%08x\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
674 MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, lpwstrFile, len );
675 ret = PrivateExtractIconExW(lpwstrFile,nIndex,phIconLarge, phIconSmall, nIcons);
676 HeapFree(GetProcessHeap(), 0, lpwstrFile);