configure: Make the font warning more explicit about what package is missing.
[wine/multimedia.git] / dlls / user / exticon.c
blobddc1ccd73c2894d7fd05776addfe26422241b329
1 /*
2 * icon extracting
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h> /* abs() */
30 #include <sys/types.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winuser.h"
41 #include "wine/winbase16.h"
42 #include "user_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(icon);
47 #include "pshpack1.h"
49 typedef struct
51 BYTE bWidth; /* Width, in pixels, of the image */
52 BYTE bHeight; /* Height, in pixels, of the image */
53 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
54 BYTE bReserved; /* Reserved ( must be 0) */
55 WORD wPlanes; /* Color Planes */
56 WORD wBitCount; /* Bits per pixel */
57 DWORD dwBytesInRes; /* How many bytes in this resource? */
58 DWORD dwImageOffset; /* Where in the file is this image? */
59 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
61 typedef struct
63 WORD idReserved; /* Reserved (must be 0) */
64 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
65 WORD idCount; /* How many images */
66 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
67 } icoICONDIR, *LPicoICONDIR;
69 #include "poppack.h"
71 #if 0
72 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
74 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
75 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
76 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
77 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
79 static void dumpIcoDir ( LPicoICONDIR entry )
81 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
83 #endif
85 /**********************************************************************
86 * find_entry_by_id
88 * Find an entry by id in a resource directory
89 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
91 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
92 WORD id, const void *root )
94 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
95 int min, max, pos;
97 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
98 min = dir->NumberOfNamedEntries;
99 max = min + dir->NumberOfIdEntries - 1;
100 while (min <= max)
102 pos = (min + max) / 2;
103 if (entry[pos].u1.s2.Id == id)
104 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
105 if (entry[pos].u1.s2.Id > id) max = pos - 1;
106 else min = pos + 1;
108 return NULL;
111 /**********************************************************************
112 * find_entry_default
114 * Find a default entry in a resource directory
115 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
117 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
118 const void *root )
120 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
121 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
122 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
125 /*************************************************************************
126 * USER32_GetResourceTable
128 static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
130 IMAGE_DOS_HEADER * mz_header;
132 TRACE("%p %p\n", peimage, retptr);
134 *retptr = NULL;
136 mz_header = (IMAGE_DOS_HEADER*) peimage;
138 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
140 if (mz_header->e_cblp == 1) /* .ICO file ? */
142 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
143 return 1;
145 else
146 return 0; /* failed */
148 if (mz_header->e_lfanew >= pesize) {
149 return 0; /* failed, happens with PKZIP DOS Exes for instance. */
151 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
152 return IMAGE_NT_SIGNATURE;
154 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
156 IMAGE_OS2_HEADER * ne_header;
158 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
160 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
161 return 0;
163 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
164 *retptr = (LPBYTE)-1;
165 else
166 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
168 return IMAGE_OS2_SIGNATURE;
170 return 0; /* failed */
172 /*************************************************************************
173 * USER32_LoadResource
175 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
177 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
179 *uSize = (DWORD)pNInfo->length << sizeShift;
180 return peimage + ((DWORD)pNInfo->offset << sizeShift);
183 /*************************************************************************
184 * ICO_LoadIcon
186 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
188 TRACE("%p %p\n", peimage, lpiIDE);
190 *uSize = lpiIDE->dwBytesInRes;
191 return peimage + lpiIDE->dwImageOffset;
194 /*************************************************************************
195 * ICO_GetIconDirectory
197 * Reads .ico file and build phony ICONDIR struct
198 * see http://www.microsoft.com/win32dev/ui/icons.htm
200 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
201 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
203 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
205 CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
206 CURSORICONDIR * lpID; /* icon resource in resource format */
207 int i;
209 TRACE("%p %p\n", peimage, lplpiID);
211 lpcid = (CURSORICONDIR*)peimage;
213 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
214 return 0;
216 /* allocate the phony ICONDIR structure */
217 *uSize = lpcid->idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
218 if( (lpID = HeapAlloc(GetProcessHeap(),0, *uSize) ))
220 /* copy the header */
221 lpID->idReserved = lpcid->idReserved;
222 lpID->idType = lpcid->idType;
223 lpID->idCount = lpcid->idCount;
225 /* copy the entries */
226 for( i=0; i < lpcid->idCount; i++ )
228 memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpcid->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
229 lpID->idEntries[i].wResId = i;
232 *lplpiID = (LPicoICONDIR)peimage;
233 return (BYTE *)lpID;
235 return 0;
238 /******************************************************************************
239 * struct extract_icons_state [internal]
241 * Used to carry params and state through the iterations of EnumResourceNames in ICO_ExtractIconExW
243 struct extract_icons_state {
244 INT nBaseIndex; /* Zero based index or negated integer id of the first icon to extract. */
245 UINT nIcons; /* Number of icons to extract. */
246 UINT cxDesired; /* Desired horizontal size in pixels (Might be two sizes in HI/LOWORD). */
247 UINT cyDesired; /* Desired vertical size in pixels (Might be two sizes in HI/LOWORD). */
248 UINT fuLoad; /* Flags passed to LoadImage. */
249 INT cIter; /* Iteration counter. */
250 HICON *pahIcon; /* Pointer to an array where the icon handles will be stored. */
251 UINT *paIconId; /* Pointer to an array where the icon identifiers will be stored. */
254 /******************************************************************************
255 * extract_icons_callback [internal]
257 * Callback function of type ENUMRESNAMEPROC for EnumResourceNames. Used in
258 * ICO_ExtractIconsExW.
260 static BOOL CALLBACK extract_icons_callback(HMODULE hModule, LPCWSTR pwszType, LPWSTR pwszName,
261 LONG_PTR lParam)
263 struct extract_icons_state *pState = (struct extract_icons_state *)lParam;
264 INT idCurrent = (INT)pwszName;
266 /* If the handle array pointer is NULL, we just count the number of icons in the module. */
267 if (!pState->pahIcon) {
268 pState->cIter++;
269 return TRUE;
272 /* If we didn't already start extracting icons (cIter == 0), we look if the current
273 * icon is the one we should start with. That's the case if nBaseIndex is negative and
274 * it's absolute value matches the current icon's identifier. Or if nBaseIndex is positive
275 * and we already omitted the first nBaseIndex icons. */
276 if (pState->cIter == 0) {
277 if (pState->nBaseIndex < 0) {
278 if (!IS_INTRESOURCE(pwszName) || idCurrent != -pState->nBaseIndex) {
279 return TRUE;
281 } else if (pState->nBaseIndex > 0) {
282 pState->nBaseIndex--;
283 return TRUE;
287 if (pState->cIter < pState->nIcons) {
288 /* Load the current icon with the size given in the LOWORD of cx/cyDesired */
289 pState->pahIcon[pState->cIter] =
290 LoadImageW(hModule, pwszName, IMAGE_ICON, LOWORD(pState->cxDesired),
291 LOWORD(pState->cyDesired), pState->fuLoad);
292 /* FIXME: The resource's identifier (that is not idCurrent, which is the
293 * identifier of the icon directory) should be stored in paIconId, but I don't
294 * know how to query for it. */
295 if (pState->paIconId) pState->paIconId[pState->cIter] = 0;
296 pState->cIter++;
298 /* If there is a second desired size given in the HIWORDs of cx/cyDesired, load too.
299 * There seems to be an off-by-one error here, since pState->cIter might now be equal
300 * to pState->nIcons, but this is in fact how it works on windows. */
301 if (HIWORD(pState->cxDesired) && HIWORD(pState->cyDesired)) {
302 pState->pahIcon[pState->cIter] =
303 LoadImageW(hModule, pwszName, IMAGE_ICON, HIWORD(pState->cxDesired),
304 HIWORD(pState->cyDesired), pState->fuLoad);
305 if (pState->paIconId) pState->paIconId[pState->cIter] = 0;
306 pState->cIter++;
310 return pState->cIter < pState->nIcons;
313 /*************************************************************************
314 * ICO_ExtractIconExW [internal]
316 * NOTES
317 * nIcons = 0: returns number of Icons in file
319 * returns
320 * invalid file: -1
321 * failure:0;
322 * success: number of icons in file (nIcons = 0) or nr of icons retrieved
324 static UINT ICO_ExtractIconExW(
325 LPCWSTR lpszExeFileName,
326 HICON * RetPtr,
327 INT nIconIndex,
328 UINT nIcons,
329 UINT cxDesired,
330 UINT cyDesired,
331 UINT *pIconId,
332 UINT flags)
334 UINT ret = 0;
335 UINT cx1, cx2, cy1, cy2;
336 LPBYTE pData;
337 DWORD sig;
338 HANDLE hFile;
339 UINT16 iconDirCount = 0,iconCount = 0;
340 LPBYTE peimage;
341 HANDLE fmapping;
342 ULONG uSize;
343 DWORD fsizeh,fsizel;
344 WCHAR szExePath[MAX_PATH];
345 DWORD dwSearchReturn;
347 TRACE("%s, %d, %d %p 0x%08x\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons, pIconId, flags);
349 dwSearchReturn = SearchPathW(NULL, lpszExeFileName, NULL, sizeof(szExePath) / sizeof(szExePath[0]), szExePath, NULL);
350 if ((dwSearchReturn == 0) || (dwSearchReturn > sizeof(szExePath) / sizeof(szExePath[0])))
352 /* This is very wine specific: If the user tries to extract icon's from system dlls,
353 * wine's build-in *.dll.so's will not be found (Even if they would be found it wouldn't
354 * work, since they are not in PE-format). Thus, if the SearchPath call fails, we try
355 * to load the file with LoadLibary and extract the icons with LoadImage (via
356 * EnumResourceNames).
358 struct extract_icons_state state;
359 HMODULE hDllSo = LoadLibraryW(lpszExeFileName);
361 if (!hDllSo) {
362 WARN("File %s not found or path too long\n", debugstr_w(lpszExeFileName));
363 return -1;
366 state.nBaseIndex = nIconIndex;
367 state.nIcons = nIcons;
368 state.cxDesired = cxDesired;
369 state.cyDesired = cyDesired;
370 state.fuLoad = flags;
371 state.cIter = 0;
372 state.pahIcon = RetPtr;
373 state.paIconId = pIconId;
375 EnumResourceNamesW(hDllSo, MAKEINTRESOURCEW(RT_GROUP_ICON), extract_icons_callback,
376 (LONG_PTR)&state);
377 FreeLibrary(hDllSo);
379 return state.cIter;
382 hFile = CreateFileW(szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
383 if (hFile == INVALID_HANDLE_VALUE) return ret;
384 fsizel = GetFileSize(hFile,&fsizeh);
386 /* Map the file */
387 fmapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
388 CloseHandle(hFile);
389 if (!fmapping)
391 WARN("CreateFileMapping error %ld\n", GetLastError() );
392 return 0xFFFFFFFF;
395 if (!(peimage = MapViewOfFile(fmapping, FILE_MAP_READ, 0, 0, 0)))
397 WARN("MapViewOfFile error %ld\n", GetLastError() );
398 CloseHandle(fmapping);
399 return 0xFFFFFFFF;
401 CloseHandle(fmapping);
403 cx1 = LOWORD(cxDesired);
404 cx2 = HIWORD(cxDesired);
405 cy1 = LOWORD(cyDesired);
406 cy2 = HIWORD(cyDesired);
408 if (pIconId) /* Invalidate first icon identifier */
409 *pIconId = 0xFFFFFFFF;
411 if (!pIconId) /* if no icon identifier array present use the icon handle array as intermediate storage */
412 pIconId = (UINT*)RetPtr;
414 sig = USER32_GetResourceTable(peimage, fsizel, &pData);
416 /* ico file or NE exe/dll*/
417 if (sig==IMAGE_OS2_SIGNATURE || sig==1) /* .ICO file */
419 BYTE *pCIDir = 0;
420 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
421 NE_NAMEINFO *pIconStorage = NULL;
422 NE_NAMEINFO *pIconDir = NULL;
423 LPicoICONDIR lpiID = NULL;
425 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
427 if (pData == (BYTE*)-1)
429 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
430 if (pCIDir)
432 iconDirCount = 1; iconCount = lpiID->idCount;
433 TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
436 else while (pTInfo->type_id && !(pIconStorage && pIconDir))
438 if (pTInfo->type_id == NE_RSCTYPE_GROUP_ICON) /* find icon directory and icon repository */
440 iconDirCount = pTInfo->count;
441 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
442 TRACE("\tfound directory - %i icon families\n", iconDirCount);
444 if (pTInfo->type_id == NE_RSCTYPE_ICON)
446 iconCount = pTInfo->count;
447 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
448 TRACE("\ttotal icons - %i\n", iconCount);
450 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
453 if ((pIconStorage && pIconDir) || lpiID) /* load resources and create icons */
455 if (nIcons == 0)
457 ret = iconDirCount;
458 if (lpiID && pCIDir) /* *.ico file, deallocate heap pointer*/
459 HeapFree(GetProcessHeap(), 0, pCIDir);
461 else if (nIconIndex < iconDirCount)
463 UINT16 i, icon;
464 if (nIcons > iconDirCount - nIconIndex)
465 nIcons = iconDirCount - nIconIndex;
467 for (i = 0; i < nIcons; i++)
469 /* .ICO files have only one icon directory */
470 if (lpiID == NULL) /* not *.ico */
471 pCIDir = USER32_LoadResource(peimage, pIconDir + i + nIconIndex, *(WORD*)pData, &uSize);
472 pIconId[i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx1, cy1, flags);
473 if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx2, cy2, flags);
475 if (lpiID && pCIDir) /* *.ico file, deallocate heap pointer*/
476 HeapFree(GetProcessHeap(), 0, pCIDir);
478 for (icon = 0; icon < nIcons; icon++)
480 pCIDir = NULL;
481 if (lpiID)
482 pCIDir = ICO_LoadIcon(peimage, lpiID->idEntries + (int)pIconId[icon], &uSize);
483 else
484 for (i = 0; i < iconCount; i++)
485 if (pIconStorage[i].id == ((int)pIconId[icon] | 0x8000) )
486 pCIDir = USER32_LoadResource(peimage, pIconStorage + i, *(WORD*)pData, &uSize);
488 if (pCIDir)
490 RetPtr[icon] = (HICON)CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
491 cx1, cy1, flags);
492 if (cx2 && cy2)
493 RetPtr[++icon] = (HICON)CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
494 cx2, cy2, flags);
496 else
497 RetPtr[icon] = 0;
499 ret = icon; /* return number of retrieved icons */
503 /* end ico file */
505 /* exe/dll */
506 else if( sig == IMAGE_NT_SIGNATURE )
508 LPBYTE idata,igdata;
509 PIMAGE_DOS_HEADER dheader;
510 PIMAGE_NT_HEADERS pe_header;
511 PIMAGE_SECTION_HEADER pe_sections;
512 const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
513 const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
514 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
515 UINT i, j;
517 dheader = (PIMAGE_DOS_HEADER)peimage;
518 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, USER32_GetResourceTable checked that */
519 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER)
520 + pe_header->FileHeader.SizeOfOptionalHeader);
521 rootresdir = NULL;
523 /* search for the root resource directory */
524 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
526 if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
527 continue;
528 if (fsizel < pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData) {
529 FIXME("File %s too short (section is at %ld bytes, real size is %ld)\n",
530 debugstr_w(lpszExeFileName),
531 pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData,
532 fsizel
534 goto end;
536 /* FIXME: doesn't work when the resources are not in a separate section */
537 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
539 rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
540 break;
544 if (!rootresdir)
546 WARN("haven't found section for resource directory.\n");
547 goto end; /* failure */
550 /* search for the group icon directory */
551 if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICON), rootresdir)))
553 WARN("No Icongroupresourcedirectory!\n");
554 goto end; /* failure */
556 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
558 /* only number of icons requested */
559 if( !pIconId )
561 ret = iconDirCount;
562 goto end; /* success */
565 if( nIconIndex < 0 )
567 /* search resource id */
568 int n = 0;
569 int iId = abs(nIconIndex);
570 const IMAGE_RESOURCE_DIRECTORY_ENTRY* xprdeTmp = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1);
572 while(n<iconDirCount && xprdeTmp)
574 if(xprdeTmp->u1.s2.Id == iId)
576 nIconIndex = n;
577 break;
579 n++;
580 xprdeTmp++;
582 if (nIconIndex < 0)
584 WARN("resource id %d not found\n", iId);
585 goto end; /* failure */
588 else
590 /* check nIconIndex to be in range */
591 if (nIconIndex >= iconDirCount)
593 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
594 goto end; /* failure */
598 /* assure we don't get too much */
599 if( nIcons > iconDirCount - nIconIndex )
600 nIcons = iconDirCount - nIconIndex;
602 /* starting from specified index */
603 xresent = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1) + nIconIndex;
605 for (i=0; i < nIcons; i++,xresent++)
607 const IMAGE_RESOURCE_DIRECTORY *resdir;
609 /* go down this resource entry, name */
610 resdir = (PIMAGE_RESOURCE_DIRECTORY)((char *)rootresdir + xresent->u2.s3.OffsetToDirectory);
612 /* default language (0) */
613 resdir = find_entry_default(resdir,rootresdir);
614 igdataent = (const IMAGE_RESOURCE_DATA_ENTRY*)resdir;
616 /* lookup address in mapped image for virtual address */
617 igdata = NULL;
619 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
621 if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
622 continue;
623 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
624 continue;
626 if (igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size > fsizel) {
627 FIXME("overflow in PE lookup (%s has len %ld, have offset %ld), short file?\n", debugstr_w(lpszExeFileName), fsizel,
628 igdataent->OffsetToData - pe_sections[j].VirtualAddress + pe_sections[j].PointerToRawData + igdataent->Size);
629 goto end; /* failure */
631 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
634 if (!igdata)
636 FIXME("no matching real address for icongroup!\n");
637 goto end; /* failure */
639 pIconId[i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx1, cy1, flags);
640 if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx2, cy2, flags);
643 if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICON),rootresdir)))
645 WARN("No Iconresourcedirectory!\n");
646 goto end; /* failure */
649 for (i=0; i<nIcons; i++)
651 const IMAGE_RESOURCE_DIRECTORY *xresdir;
652 xresdir = find_entry_by_id(iconresdir, LOWORD(pIconId[i]), rootresdir);
653 if( !xresdir )
655 WARN("icon entry %d not found\n", LOWORD(pIconId[i]));
656 RetPtr[i]=0;
657 continue;
659 xresdir = find_entry_default(xresdir, rootresdir);
660 idataent = (const IMAGE_RESOURCE_DATA_ENTRY*)xresdir;
661 idata = NULL;
663 /* map virtual to address in image */
664 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
666 if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
667 continue;
668 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
669 continue;
670 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
672 if (!idata)
674 WARN("no matching real address found for icondata!\n");
675 RetPtr[i]=0;
676 continue;
678 RetPtr[i] = (HICON) CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000,
679 cx1, cy1, flags);
680 if (cx2 && cy2)
681 RetPtr[++i] = (HICON) CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000,
682 cx2, cy2, flags);
684 ret = i; /* return number of retrieved icons */
685 } /* if(sig == IMAGE_NT_SIGNATURE) */
687 end:
688 UnmapViewOfFile(peimage); /* success */
689 return ret;
692 /***********************************************************************
693 * PrivateExtractIconsW [USER32.@]
695 * NOTES
696 * If HIWORD(sizeX) && HIWORD(sizeY) 2 * ((nIcons + 1) MOD 2) icons are
697 * returned, with the LOWORD size icon first and the HIWORD size icon
698 * second.
699 * Also the Windows equivalent does extract icons in a strange way if
700 * nIndex is negative. Our implementation treats a negative nIndex as
701 * looking for that resource identifier for the first icon to retrieve.
703 * FIXME:
704 * should also support 16 bit EXE + DLLs, cursor and animated cursor as
705 * well as bitmap files.
708 UINT WINAPI PrivateExtractIconsW (
709 LPCWSTR lpwstrFile,
710 int nIndex,
711 int sizeX,
712 int sizeY,
713 HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
714 UINT* pIconId, /* [out] pointer to array of nIcons icon identifiers or NULL */
715 UINT nIcons, /* [in] number of icons to retrieve */
716 UINT flags ) /* [in] LR_* flags used by LoadImage */
718 TRACE("%s %d %dx%d %p %p %d 0x%08x\n",
719 debugstr_w(lpwstrFile), nIndex, sizeX, sizeY, phicon, pIconId, nIcons, flags);
721 if ((nIcons & 1) && HIWORD(sizeX) && HIWORD(sizeY))
723 WARN("Uneven number %d of icons requested for small and large icons!\n", nIcons);
725 return ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX, sizeY, pIconId, flags);
728 /***********************************************************************
729 * PrivateExtractIconsA [USER32.@]
732 UINT WINAPI PrivateExtractIconsA (
733 LPCSTR lpstrFile,
734 int nIndex,
735 int sizeX,
736 int sizeY,
737 HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
738 UINT* piconid, /* [out] pointer to array of nIcons icon identifiers or NULL */
739 UINT nIcons, /* [in] number of icons to retrieve */
740 UINT flags ) /* [in] LR_* flags used by LoadImage */
742 UINT ret;
743 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
744 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
746 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
747 ret = PrivateExtractIconsW(lpwstrFile, nIndex, sizeX, sizeY, phicon, piconid, nIcons, flags);
749 HeapFree(GetProcessHeap(), 0, lpwstrFile);
750 return ret;
753 /***********************************************************************
754 * PrivateExtractIconExW [USER32.@]
755 * NOTES
756 * if nIndex == -1 it returns the number of icons in any case !!!
758 UINT WINAPI PrivateExtractIconExW (
759 LPCWSTR lpwstrFile,
760 int nIndex,
761 HICON * phIconLarge,
762 HICON * phIconSmall,
763 UINT nIcons )
765 DWORD cyicon, cysmicon, cxicon, cxsmicon;
766 UINT ret = 0;
768 TRACE("%s %d %p %p %d\n",
769 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
771 if (nIndex == -1)
772 /* get the number of icons */
773 return ICO_ExtractIconExW(lpwstrFile, NULL, 0, 0, 0, 0, NULL, LR_DEFAULTCOLOR);
775 if (nIcons == 1 && phIconSmall && phIconLarge)
777 HICON hIcon[2];
778 cxicon = GetSystemMetrics(SM_CXICON);
779 cyicon = GetSystemMetrics(SM_CYICON);
780 cxsmicon = GetSystemMetrics(SM_CXSMICON);
781 cysmicon = GetSystemMetrics(SM_CYSMICON);
783 ret = ICO_ExtractIconExW(lpwstrFile, (HICON*) &hIcon, nIndex, 2, cxicon | (cxsmicon<<16),
784 cyicon | (cysmicon<<16), NULL, LR_DEFAULTCOLOR);
785 *phIconLarge = hIcon[0];
786 *phIconSmall = hIcon[1];
787 return ret;
790 if (phIconSmall)
792 /* extract n small icons */
793 cxsmicon = GetSystemMetrics(SM_CXSMICON);
794 cysmicon = GetSystemMetrics(SM_CYSMICON);
795 ret = ICO_ExtractIconExW(lpwstrFile, phIconSmall, nIndex, nIcons, cxsmicon,
796 cysmicon, NULL, LR_DEFAULTCOLOR);
798 if (phIconLarge)
800 /* extract n large icons */
801 cxicon = GetSystemMetrics(SM_CXICON);
802 cyicon = GetSystemMetrics(SM_CYICON);
803 ret = ICO_ExtractIconExW(lpwstrFile, phIconLarge, nIndex, nIcons, cxicon,
804 cyicon, NULL, LR_DEFAULTCOLOR);
806 return ret;
809 /***********************************************************************
810 * PrivateExtractIconExA [USER32.@]
812 UINT WINAPI PrivateExtractIconExA (
813 LPCSTR lpstrFile,
814 int nIndex,
815 HICON * phIconLarge,
816 HICON * phIconSmall,
817 UINT nIcons )
819 UINT ret;
820 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
821 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
823 TRACE("%s %d %p %p %d\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
825 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
826 ret = PrivateExtractIconExW(lpwstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
827 HeapFree(GetProcessHeap(), 0, lpwstrFile);
828 return ret;