dpnet: Assign to structs instead of using memcpy.
[wine.git] / dlls / user32 / exticon.c
blob8e9f9387d8f1a57903d8caa5b5575968bc387d9b
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 "winuser.h"
40 #include "wine/winbase16.h"
41 #include "user_private.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(icon);
46 #include "pshpack1.h"
48 typedef struct
50 BYTE bWidth; /* Width, in pixels, of the image */
51 BYTE bHeight; /* Height, in pixels, of the image */
52 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
53 BYTE bReserved; /* Reserved ( must be 0) */
54 WORD wPlanes; /* Color Planes */
55 WORD wBitCount; /* Bits per pixel */
56 DWORD dwBytesInRes; /* How many bytes in this resource? */
57 DWORD dwImageOffset; /* Where in the file is this image? */
58 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
60 typedef struct
62 WORD idReserved; /* Reserved (must be 0) */
63 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
64 WORD idCount; /* How many images */
65 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
66 } icoICONDIR, *LPicoICONDIR;
68 #include "poppack.h"
70 #if 0
71 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
73 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
74 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
75 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
76 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
78 static void dumpIcoDir ( LPicoICONDIR entry )
80 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
82 #endif
84 /**********************************************************************
85 * find_entry_by_id
87 * Find an entry by id in a resource directory
88 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
90 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
91 WORD id, const void *root )
93 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
94 int min, max, pos;
96 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
97 min = dir->NumberOfNamedEntries;
98 max = min + dir->NumberOfIdEntries - 1;
99 while (min <= max)
101 pos = (min + max) / 2;
102 if (entry[pos].u1.s2.Id == id)
103 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
104 if (entry[pos].u1.s2.Id > id) max = pos - 1;
105 else min = pos + 1;
107 return NULL;
110 /**********************************************************************
111 * find_entry_default
113 * Find a default entry in a resource directory
114 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
116 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
117 const void *root )
119 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
120 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
121 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
124 /*************************************************************************
125 * USER32_GetResourceTable
127 static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
129 IMAGE_DOS_HEADER * mz_header;
131 TRACE("%p %p\n", peimage, retptr);
133 *retptr = NULL;
135 mz_header = (IMAGE_DOS_HEADER*) peimage;
137 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
139 if (mz_header->e_cblp == 1) /* .ICO file ? */
141 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
142 return 1;
144 else
145 return 0; /* failed */
147 if (mz_header->e_lfanew >= pesize) {
148 return 0; /* failed, happens with PKZIP DOS Exes for instance. */
150 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
151 return IMAGE_NT_SIGNATURE;
153 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
155 IMAGE_OS2_HEADER * ne_header;
157 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
159 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
160 return 0;
162 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
163 *retptr = (LPBYTE)-1;
164 else
165 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
167 return IMAGE_OS2_SIGNATURE;
169 return 0; /* failed */
171 /*************************************************************************
172 * USER32_LoadResource
174 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
176 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
178 *uSize = (DWORD)pNInfo->length << sizeShift;
179 return peimage + ((DWORD)pNInfo->offset << sizeShift);
182 /*************************************************************************
183 * ICO_LoadIcon
185 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
187 TRACE("%p %p\n", peimage, lpiIDE);
189 *uSize = lpiIDE->dwBytesInRes;
190 return peimage + lpiIDE->dwImageOffset;
193 /*************************************************************************
194 * ICO_GetIconDirectory
196 * Reads .ico file and build phony ICONDIR struct
197 * see http://www.microsoft.com/win32dev/ui/icons.htm
199 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
200 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
202 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
204 CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
205 CURSORICONDIR * lpID; /* icon resource in resource format */
206 int i;
208 TRACE("%p %p\n", peimage, lplpiID);
210 lpcid = (CURSORICONDIR*)peimage;
212 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
213 return 0;
215 /* allocate the phony ICONDIR structure */
216 *uSize = lpcid->idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
217 if( (lpID = HeapAlloc(GetProcessHeap(),0, *uSize) ))
219 /* copy the header */
220 lpID->idReserved = lpcid->idReserved;
221 lpID->idType = lpcid->idType;
222 lpID->idCount = lpcid->idCount;
224 /* copy the entries */
225 for( i=0; i < lpcid->idCount; i++ )
227 memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpcid->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
228 lpID->idEntries[i].wResId = i;
231 *lplpiID = (LPicoICONDIR)peimage;
232 return (BYTE *)lpID;
234 return 0;
237 /******************************************************************************
238 * struct extract_icons_state [internal]
240 * Used to carry params and state through the iterations of EnumResourceNames in ICO_ExtractIconExW
242 struct extract_icons_state {
243 INT nBaseIndex; /* Zero based index or negated integer id of the first icon to extract. */
244 UINT nIcons; /* Number of icons to extract. */
245 UINT cxDesired; /* Desired horizontal size in pixels (Might be two sizes in HI/LOWORD). */
246 UINT cyDesired; /* Desired vertical size in pixels (Might be two sizes in HI/LOWORD). */
247 UINT fuLoad; /* Flags passed to LoadImage. */
248 INT cIter; /* Iteration counter. */
249 HICON *pahIcon; /* Pointer to an array where the icon handles will be stored. */
250 UINT *paIconId; /* Pointer to an array where the icon identifiers will be stored. */
253 /******************************************************************************
254 * extract_icons_callback [internal]
256 * Callback function of type ENUMRESNAMEPROC for EnumResourceNames. Used in
257 * ICO_ExtractIconsExW.
259 static BOOL CALLBACK extract_icons_callback(HMODULE hModule, LPCWSTR pwszType, LPWSTR pwszName,
260 LONG_PTR lParam)
262 struct extract_icons_state *pState = (struct extract_icons_state *)lParam;
263 INT idCurrent = (INT)pwszName;
265 /* If the handle array pointer is NULL, we just count the number of icons in the module. */
266 if (!pState->pahIcon) {
267 pState->cIter++;
268 return TRUE;
271 /* If we didn't already start extracting icons (cIter == 0), we look if the current
272 * icon is the one we should start with. That's the case if nBaseIndex is negative and
273 * it's absolute value matches the current icon's identifier. Or if nBaseIndex is positive
274 * and we already omitted the first nBaseIndex icons. */
275 if (pState->cIter == 0) {
276 if (pState->nBaseIndex < 0) {
277 if (!IS_INTRESOURCE(pwszName) || idCurrent != -pState->nBaseIndex) {
278 return TRUE;
280 } else if (pState->nBaseIndex > 0) {
281 pState->nBaseIndex--;
282 return TRUE;
286 if (pState->cIter < pState->nIcons) {
287 /* Load the current icon with the size given in the LOWORD of cx/cyDesired */
288 pState->pahIcon[pState->cIter] =
289 LoadImageW(hModule, pwszName, IMAGE_ICON, LOWORD(pState->cxDesired),
290 LOWORD(pState->cyDesired), pState->fuLoad);
291 /* FIXME: The resource's identifier (that is not idCurrent, which is the
292 * identifier of the icon directory) should be stored in paIconId, but I don't
293 * know how to query for it. */
294 if (pState->paIconId) pState->paIconId[pState->cIter] = 0;
295 pState->cIter++;
297 /* If there is a second desired size given in the HIWORDs of cx/cyDesired, load too.
298 * There seems to be an off-by-one error here, since pState->cIter might now be equal
299 * to pState->nIcons, but this is in fact how it works on windows. */
300 if (HIWORD(pState->cxDesired) && HIWORD(pState->cyDesired)) {
301 pState->pahIcon[pState->cIter] =
302 LoadImageW(hModule, pwszName, IMAGE_ICON, HIWORD(pState->cxDesired),
303 HIWORD(pState->cyDesired), pState->fuLoad);
304 if (pState->paIconId) pState->paIconId[pState->cIter] = 0;
305 pState->cIter++;
309 return pState->cIter < pState->nIcons;
312 /*************************************************************************
313 * ICO_ExtractIconExW [internal]
315 * NOTES
316 * nIcons = 0: returns number of Icons in file
318 * returns
319 * invalid file: -1
320 * failure:0;
321 * success: number of icons in file (nIcons = 0) or nr of icons retrieved
323 static UINT ICO_ExtractIconExW(
324 LPCWSTR lpszExeFileName,
325 HICON * RetPtr,
326 INT nIconIndex,
327 UINT nIcons,
328 UINT cxDesired,
329 UINT cyDesired,
330 UINT *pIconId,
331 UINT flags)
333 UINT ret = 0;
334 UINT cx1, cx2, cy1, cy2;
335 LPBYTE pData;
336 DWORD sig;
337 HANDLE hFile;
338 UINT16 iconDirCount = 0,iconCount = 0;
339 LPBYTE peimage;
340 HANDLE fmapping;
341 DWORD fsizeh,fsizel;
342 WCHAR szExePath[MAX_PATH];
343 DWORD dwSearchReturn;
345 TRACE("%s, %d, %d %p 0x%08x\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons, pIconId, flags);
347 dwSearchReturn = SearchPathW(NULL, lpszExeFileName, NULL, sizeof(szExePath) / sizeof(szExePath[0]), szExePath, NULL);
348 if ((dwSearchReturn == 0) || (dwSearchReturn > sizeof(szExePath) / sizeof(szExePath[0])))
350 /* This is very wine specific: If the user tries to extract icons from system dlls,
351 * wine's builtin *.dll.so's will not be found (Even if they would be found it wouldn't
352 * work, since they are not in PE-format). Thus, if the SearchPath call fails, we try
353 * to load the file with LoadLibrary and extract the icons with LoadImage (via
354 * EnumResourceNames).
356 struct extract_icons_state state;
357 HMODULE hDllSo = LoadLibraryW(lpszExeFileName);
359 if (!hDllSo) {
360 WARN("File %s not found or path too long\n", debugstr_w(lpszExeFileName));
361 return -1;
364 state.nBaseIndex = nIconIndex;
365 state.nIcons = nIcons;
366 state.cxDesired = cxDesired;
367 state.cyDesired = cyDesired;
368 state.fuLoad = flags;
369 state.cIter = 0;
370 state.pahIcon = RetPtr;
371 state.paIconId = pIconId;
373 EnumResourceNamesW(hDllSo, MAKEINTRESOURCEW(RT_GROUP_ICON), extract_icons_callback,
374 (LONG_PTR)&state);
375 FreeLibrary(hDllSo);
377 return state.cIter;
380 hFile = CreateFileW(szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
381 if (hFile == INVALID_HANDLE_VALUE) return ret;
382 fsizel = GetFileSize(hFile,&fsizeh);
384 /* Map the file */
385 fmapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
386 CloseHandle(hFile);
387 if (!fmapping)
389 WARN("CreateFileMapping error %d\n", GetLastError() );
390 return 0xFFFFFFFF;
393 if (!(peimage = MapViewOfFile(fmapping, FILE_MAP_READ, 0, 0, 0)))
395 WARN("MapViewOfFile error %d\n", GetLastError() );
396 CloseHandle(fmapping);
397 return 0xFFFFFFFF;
399 CloseHandle(fmapping);
401 cx1 = LOWORD(cxDesired);
402 cx2 = HIWORD(cxDesired);
403 cy1 = LOWORD(cyDesired);
404 cy2 = HIWORD(cyDesired);
406 if (pIconId) /* Invalidate first icon identifier */
407 *pIconId = 0xFFFFFFFF;
409 if (!pIconId) /* if no icon identifier array present use the icon handle array as intermediate storage */
410 pIconId = (UINT*)RetPtr;
412 sig = USER32_GetResourceTable(peimage, fsizel, &pData);
414 /* ico file or NE exe/dll*/
415 if (sig==IMAGE_OS2_SIGNATURE || sig==1) /* .ICO file */
417 BYTE *pCIDir = 0;
418 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
419 NE_NAMEINFO *pIconStorage = NULL;
420 NE_NAMEINFO *pIconDir = NULL;
421 LPicoICONDIR lpiID = NULL;
422 ULONG uSize = 0;
424 TRACE("-- OS2/icon Signature (0x%08x)\n", sig);
426 if (pData == (BYTE*)-1)
428 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
429 if (pCIDir)
431 iconDirCount = 1; iconCount = lpiID->idCount;
432 TRACE("-- icon found %p 0x%08x 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
435 else while (pTInfo->type_id && !(pIconStorage && pIconDir))
437 if (pTInfo->type_id == NE_RSCTYPE_GROUP_ICON) /* find icon directory and icon repository */
439 iconDirCount = pTInfo->count;
440 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
441 TRACE("\tfound directory - %i icon families\n", iconDirCount);
443 if (pTInfo->type_id == NE_RSCTYPE_ICON)
445 iconCount = pTInfo->count;
446 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
447 TRACE("\ttotal icons - %i\n", iconCount);
449 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
452 if ((pIconStorage && pIconDir) || lpiID) /* load resources and create icons */
454 if (nIcons == 0)
456 ret = iconDirCount;
457 if (lpiID) /* *.ico file, deallocate heap pointer*/
458 HeapFree(GetProcessHeap(), 0, pCIDir);
460 else if (nIconIndex < iconDirCount)
462 UINT16 i, icon;
463 if (nIcons > iconDirCount - nIconIndex)
464 nIcons = iconDirCount - nIconIndex;
466 for (i = 0; i < nIcons; i++)
468 /* .ICO files have only one icon directory */
469 if (lpiID == NULL) /* not *.ico */
470 pCIDir = USER32_LoadResource(peimage, pIconDir + i + nIconIndex, *(WORD*)pData, &uSize);
471 pIconId[i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx1, cy1, flags);
472 if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx2, cy2, flags);
474 if (lpiID) /* *.ico file, deallocate heap pointer*/
475 HeapFree(GetProcessHeap(), 0, pCIDir);
477 for (icon = 0; icon < nIcons; icon++)
479 pCIDir = NULL;
480 if (lpiID)
481 pCIDir = ICO_LoadIcon(peimage, lpiID->idEntries + (int)pIconId[icon], &uSize);
482 else
483 for (i = 0; i < iconCount; i++)
484 if (pIconStorage[i].id == ((int)pIconId[icon] | 0x8000) )
485 pCIDir = USER32_LoadResource(peimage, pIconStorage + i, *(WORD*)pData, &uSize);
487 if (pCIDir)
489 RetPtr[icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
490 cx1, cy1, flags);
491 if (cx2 && cy2)
492 RetPtr[++icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
493 cx2, cy2, flags);
495 else
496 RetPtr[icon] = 0;
498 ret = icon; /* return number of retrieved icons */
502 /* end ico file */
504 /* exe/dll */
505 else if( sig == IMAGE_NT_SIGNATURE )
507 LPBYTE idata,igdata;
508 PIMAGE_DOS_HEADER dheader;
509 PIMAGE_NT_HEADERS pe_header;
510 PIMAGE_SECTION_HEADER pe_sections;
511 const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
512 const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
513 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
514 UINT i, j;
516 dheader = (PIMAGE_DOS_HEADER)peimage;
517 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, USER32_GetResourceTable checked that */
518 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER)
519 + pe_header->FileHeader.SizeOfOptionalHeader);
520 rootresdir = NULL;
522 /* search for the root resource directory */
523 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
525 if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
526 continue;
527 if (fsizel < pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData) {
528 FIXME("File %s too short (section is at %d bytes, real size is %d)\n",
529 debugstr_w(lpszExeFileName),
530 pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData,
531 fsizel
533 goto end;
535 /* FIXME: doesn't work when the resources are not in a separate section */
536 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
538 rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
539 break;
543 if (!rootresdir)
545 WARN("haven't found section for resource directory.\n");
546 goto end; /* failure */
549 /* search for the group icon directory */
550 if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICON), rootresdir)))
552 WARN("No Icongroupresourcedirectory!\n");
553 goto end; /* failure */
555 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
557 /* only number of icons requested */
558 if( !pIconId )
560 ret = iconDirCount;
561 goto end; /* success */
564 if( nIconIndex < 0 )
566 /* search resource id */
567 int n = 0;
568 int iId = abs(nIconIndex);
569 const IMAGE_RESOURCE_DIRECTORY_ENTRY* xprdeTmp = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1);
571 while(n<iconDirCount && xprdeTmp)
573 if(xprdeTmp->u1.s2.Id == iId)
575 nIconIndex = n;
576 break;
578 n++;
579 xprdeTmp++;
581 if (nIconIndex < 0)
583 WARN("resource id %d not found\n", iId);
584 goto end; /* failure */
587 else
589 /* check nIconIndex to be in range */
590 if (nIconIndex >= iconDirCount)
592 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
593 goto end; /* failure */
597 /* assure we don't get too much */
598 if( nIcons > iconDirCount - nIconIndex )
599 nIcons = iconDirCount - nIconIndex;
601 /* starting from specified index */
602 xresent = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1) + nIconIndex;
604 for (i=0; i < nIcons; i++,xresent++)
606 const IMAGE_RESOURCE_DIRECTORY *resdir;
608 /* go down this resource entry, name */
609 resdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)rootresdir + xresent->u2.s3.OffsetToDirectory);
611 /* default language (0) */
612 resdir = find_entry_default(resdir,rootresdir);
613 igdataent = (const IMAGE_RESOURCE_DATA_ENTRY*)resdir;
615 /* lookup address in mapped image for virtual address */
616 igdata = NULL;
618 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
620 if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
621 continue;
622 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
623 continue;
625 if (igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size > fsizel) {
626 FIXME("overflow in PE lookup (%s has len %d, have offset %d), short file?\n", debugstr_w(lpszExeFileName), fsizel,
627 igdataent->OffsetToData - pe_sections[j].VirtualAddress + pe_sections[j].PointerToRawData + igdataent->Size);
628 goto end; /* failure */
630 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
633 if (!igdata)
635 FIXME("no matching real address for icongroup!\n");
636 goto end; /* failure */
638 pIconId[i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx1, cy1, flags);
639 if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx2, cy2, flags);
642 if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICON),rootresdir)))
644 WARN("No Iconresourcedirectory!\n");
645 goto end; /* failure */
648 for (i=0; i<nIcons; i++)
650 const IMAGE_RESOURCE_DIRECTORY *xresdir;
651 xresdir = find_entry_by_id(iconresdir, LOWORD(pIconId[i]), rootresdir);
652 if( !xresdir )
654 WARN("icon entry %d not found\n", LOWORD(pIconId[i]));
655 RetPtr[i]=0;
656 continue;
658 xresdir = find_entry_default(xresdir, rootresdir);
659 idataent = (const IMAGE_RESOURCE_DATA_ENTRY*)xresdir;
660 idata = NULL;
662 /* map virtual to address in image */
663 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
665 if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
666 continue;
667 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
668 continue;
669 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
671 if (!idata)
673 WARN("no matching real address found for icondata!\n");
674 RetPtr[i]=0;
675 continue;
677 RetPtr[i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx1, cy1, flags);
678 if (cx2 && cy2)
679 RetPtr[++i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx2, cy2, flags);
681 ret = i; /* return number of retrieved icons */
682 } /* if(sig == IMAGE_NT_SIGNATURE) */
684 end:
685 UnmapViewOfFile(peimage); /* success */
686 return ret;
689 /***********************************************************************
690 * PrivateExtractIconsW [USER32.@]
692 * NOTES
693 * If HIWORD(sizeX) && HIWORD(sizeY) 2 * ((nIcons + 1) MOD 2) icons are
694 * returned, with the LOWORD size icon first and the HIWORD size icon
695 * second.
696 * Also the Windows equivalent does extract icons in a strange way if
697 * nIndex is negative. Our implementation treats a negative nIndex as
698 * looking for that resource identifier for the first icon to retrieve.
700 * FIXME:
701 * should also support 16 bit EXE + DLLs, cursor and animated cursor as
702 * well as bitmap files.
705 UINT WINAPI PrivateExtractIconsW (
706 LPCWSTR lpwstrFile,
707 int nIndex,
708 int sizeX,
709 int sizeY,
710 HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
711 UINT* pIconId, /* [out] pointer to array of nIcons icon identifiers or NULL */
712 UINT nIcons, /* [in] number of icons to retrieve */
713 UINT flags ) /* [in] LR_* flags used by LoadImage */
715 TRACE("%s %d %dx%d %p %p %d 0x%08x\n",
716 debugstr_w(lpwstrFile), nIndex, sizeX, sizeY, phicon, pIconId, nIcons, flags);
718 if ((nIcons & 1) && HIWORD(sizeX) && HIWORD(sizeY))
720 WARN("Uneven number %d of icons requested for small and large icons!\n", nIcons);
722 return ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX, sizeY, pIconId, flags);
725 /***********************************************************************
726 * PrivateExtractIconsA [USER32.@]
729 UINT WINAPI PrivateExtractIconsA (
730 LPCSTR lpstrFile,
731 int nIndex,
732 int sizeX,
733 int sizeY,
734 HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
735 UINT* piconid, /* [out] pointer to array of nIcons icon identifiers or NULL */
736 UINT nIcons, /* [in] number of icons to retrieve */
737 UINT flags ) /* [in] LR_* flags used by LoadImage */
739 UINT ret;
740 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
741 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
743 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
744 ret = PrivateExtractIconsW(lpwstrFile, nIndex, sizeX, sizeY, phicon, piconid, nIcons, flags);
746 HeapFree(GetProcessHeap(), 0, lpwstrFile);
747 return ret;
750 /***********************************************************************
751 * PrivateExtractIconExW [USER32.@]
752 * NOTES
753 * if nIndex == -1 it returns the number of icons in any case !!!
755 UINT WINAPI PrivateExtractIconExW (
756 LPCWSTR lpwstrFile,
757 int nIndex,
758 HICON * phIconLarge,
759 HICON * phIconSmall,
760 UINT nIcons )
762 DWORD cyicon, cysmicon, cxicon, cxsmicon;
763 UINT ret = 0;
765 TRACE("%s %d %p %p %d\n",
766 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
768 if (nIndex == -1)
769 /* get the number of icons */
770 return ICO_ExtractIconExW(lpwstrFile, NULL, 0, 0, 0, 0, NULL, LR_DEFAULTCOLOR);
772 if (nIcons == 1 && phIconSmall && phIconLarge)
774 HICON hIcon[2];
775 cxicon = GetSystemMetrics(SM_CXICON);
776 cyicon = GetSystemMetrics(SM_CYICON);
777 cxsmicon = GetSystemMetrics(SM_CXSMICON);
778 cysmicon = GetSystemMetrics(SM_CYSMICON);
780 ret = ICO_ExtractIconExW(lpwstrFile, (HICON*) &hIcon, nIndex, 2, cxicon | (cxsmicon<<16),
781 cyicon | (cysmicon<<16), NULL, LR_DEFAULTCOLOR);
782 *phIconLarge = hIcon[0];
783 *phIconSmall = hIcon[1];
784 return ret;
787 if (phIconSmall)
789 /* extract n small icons */
790 cxsmicon = GetSystemMetrics(SM_CXSMICON);
791 cysmicon = GetSystemMetrics(SM_CYSMICON);
792 ret = ICO_ExtractIconExW(lpwstrFile, phIconSmall, nIndex, nIcons, cxsmicon,
793 cysmicon, NULL, LR_DEFAULTCOLOR);
795 if (phIconLarge)
797 /* extract n large icons */
798 cxicon = GetSystemMetrics(SM_CXICON);
799 cyicon = GetSystemMetrics(SM_CYICON);
800 ret = ICO_ExtractIconExW(lpwstrFile, phIconLarge, nIndex, nIcons, cxicon,
801 cyicon, NULL, LR_DEFAULTCOLOR);
803 return ret;
806 /***********************************************************************
807 * PrivateExtractIconExA [USER32.@]
809 UINT WINAPI PrivateExtractIconExA (
810 LPCSTR lpstrFile,
811 int nIndex,
812 HICON * phIconLarge,
813 HICON * phIconSmall,
814 UINT nIcons )
816 UINT ret;
817 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
818 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
820 TRACE("%s %d %p %p %d\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
822 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
823 ret = PrivateExtractIconExW(lpwstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
824 HeapFree(GetProcessHeap(), 0, lpwstrFile);
825 return ret;